birq.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * birq
  3. *
  4. * Balance IRQ
  5. *
  6. */
  7. #ifdef HAVE_CONFIG_H
  8. #include "config.h"
  9. #endif /* HAVE_CONFIG_H */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <unistd.h>
  13. #include <sys/types.h>
  14. #include <errno.h>
  15. #include <assert.h>
  16. #include <string.h>
  17. #include <signal.h>
  18. #include <syslog.h>
  19. #include <fcntl.h>
  20. #ifdef HAVE_GETOPT_H
  21. #include <getopt.h>
  22. #endif
  23. #include <sys/poll.h>
  24. #include <sys/socket.h>
  25. #include <linux/types.h>
  26. #include <linux/netlink.h>
  27. #include "lub/log.h"
  28. #include "lub/list.h"
  29. #include "irq.h"
  30. #include "nl.h"
  31. #define BIRQ_PIDFILE "/var/run/birq.pid"
  32. #define BIRQ_INTERVAL 5 /* in seconds */
  33. #ifndef VERSION
  34. #define VERSION 1.0.0
  35. #endif
  36. #define QUOTE(t) #t
  37. #define version(v) printf("%s\n", v)
  38. /* Global signal vars */
  39. static volatile int sigterm = 0;
  40. static void sighandler(int signo);
  41. static void help(int status, const char *argv0);
  42. static struct options *opts_init(void);
  43. static void opts_free(struct options *opts);
  44. static int opts_parse(int argc, char *argv[], struct options *opts);
  45. /* Command line options */
  46. struct options {
  47. char *pidfile;
  48. int debug; /* Don't daemonize in debug mode */
  49. int log_facility;
  50. };
  51. /*--------------------------------------------------------- */
  52. int main(int argc, char **argv)
  53. {
  54. int retval = -1;
  55. struct options *opts = NULL;
  56. int pidfd = -1;
  57. int rescan = 1; /* sysfs rescan needed */
  58. /* Signal vars */
  59. struct sigaction sig_act;
  60. sigset_t sig_set;
  61. /* NetLink vars */
  62. int nl = -1; /* NetLink socket */
  63. /* IRQ list. It contain all found irqs. */
  64. lub_list_t *irqs;
  65. /* Parse command line options */
  66. opts = opts_init();
  67. if (opts_parse(argc, argv, opts))
  68. goto err;
  69. /* Initialize syslog */
  70. openlog(argv[0], LOG_CONS, opts->log_facility);
  71. syslog(LOG_ERR, "Start daemon.\n");
  72. /* Init NetLink socket */
  73. if ((nl = nl_init()) < 0)
  74. goto err;
  75. /* Fork the daemon */
  76. if (!opts->debug) {
  77. /* Daemonize */
  78. if (daemon(0, 0) < 0) {
  79. syslog(LOG_ERR, "Can't daemonize\n");
  80. goto err;
  81. }
  82. /* Write pidfile */
  83. if ((pidfd = open(opts->pidfile,
  84. O_WRONLY | O_CREAT | O_EXCL | O_TRUNC,
  85. S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0) {
  86. syslog(LOG_WARNING, "Can't open pidfile %s: %s",
  87. opts->pidfile, strerror(errno));
  88. } else {
  89. char str[20];
  90. snprintf(str, sizeof(str), "%u\n", getpid());
  91. if (write(pidfd, str, strlen(str)) < 0)
  92. syslog(LOG_WARNING, "Can't write to %s: %s",
  93. opts->pidfile, strerror(errno));
  94. close(pidfd);
  95. }
  96. }
  97. /* Set signal handler */
  98. sigemptyset(&sig_set);
  99. sigaddset(&sig_set, SIGTERM);
  100. sigaddset(&sig_set, SIGINT);
  101. sigaddset(&sig_set, SIGQUIT);
  102. sig_act.sa_flags = 0;
  103. sig_act.sa_mask = sig_set;
  104. sig_act.sa_handler = &sighandler;
  105. sigaction(SIGTERM, &sig_act, NULL);
  106. sigaction(SIGINT, &sig_act, NULL);
  107. sigaction(SIGQUIT, &sig_act, NULL);
  108. /* Prepare data structures */
  109. irqs = lub_list_new(NULL);
  110. /* Main loop */
  111. while (!sigterm) {
  112. int n;
  113. if (rescan) {
  114. fprintf(stdout, "Scanning hardware...\n");
  115. rescan = 0;
  116. irqs_populate(irqs);
  117. }
  118. /* Timeout and poll for new devices */
  119. while ((n = nl_poll(nl, BIRQ_INTERVAL)) != 0) {
  120. if (-1 == n) {
  121. fprintf(stderr,
  122. "Error: Broken NetLink socket.\n");
  123. goto end;
  124. }
  125. if (-2 == n) /* EINTR */
  126. break;
  127. if (n > 0) {
  128. rescan = 1;
  129. continue;
  130. }
  131. }
  132. }
  133. end:
  134. /* Free data structures */
  135. lub_list_free(irqs);
  136. retval = 0;
  137. err:
  138. /* Close NetLink socket */
  139. nl_close(nl);
  140. /* Remove pidfile */
  141. if (pidfd >= 0) {
  142. if (unlink(opts->pidfile) < 0) {
  143. syslog(LOG_ERR, "Can't remove pid-file %s: %s\n",
  144. opts->pidfile, strerror(errno));
  145. }
  146. }
  147. /* Free command line options */
  148. opts_free(opts);
  149. syslog(LOG_ERR, "Stop daemon.\n");
  150. return retval;
  151. }
  152. /*--------------------------------------------------------- */
  153. /*
  154. * Signal handler for temination signals (like SIGTERM, SIGINT, ...)
  155. */
  156. static void sighandler(int signo)
  157. {
  158. sigterm = 1;
  159. }
  160. /*--------------------------------------------------------- */
  161. /* Initialize option structure by defaults */
  162. static struct options *opts_init(void)
  163. {
  164. struct options *opts = NULL;
  165. opts = malloc(sizeof(*opts));
  166. assert(opts);
  167. opts->debug = 0; /* daemonize by default */
  168. opts->pidfile = strdup(BIRQ_PIDFILE);
  169. opts->log_facility = LOG_DAEMON;
  170. return opts;
  171. }
  172. /*--------------------------------------------------------- */
  173. /* Free option structure */
  174. static void opts_free(struct options *opts)
  175. {
  176. if (opts->pidfile)
  177. free(opts->pidfile);
  178. free(opts);
  179. }
  180. /*--------------------------------------------------------- */
  181. /* Parse command line options */
  182. static int opts_parse(int argc, char *argv[], struct options *opts)
  183. {
  184. static const char *shortopts = "hvp:dO:";
  185. #ifdef HAVE_GETOPT_H
  186. static const struct option longopts[] = {
  187. {"help", 0, NULL, 'h'},
  188. {"version", 0, NULL, 'v'},
  189. {"pid", 1, NULL, 'p'},
  190. {"debug", 0, NULL, 'd'},
  191. {"facility", 1, NULL, 'O'},
  192. {NULL, 0, NULL, 0}
  193. };
  194. #endif
  195. optind = 1;
  196. while(1) {
  197. int opt;
  198. #ifdef HAVE_GETOPT_H
  199. opt = getopt_long(argc, argv, shortopts, longopts, NULL);
  200. #else
  201. opt = getopt(argc, argv, shortopts);
  202. #endif
  203. if (-1 == opt)
  204. break;
  205. switch (opt) {
  206. case 'p':
  207. if (opts->pidfile)
  208. free(opts->pidfile);
  209. opts->pidfile = strdup(optarg);
  210. break;
  211. case 'd':
  212. opts->debug = 1;
  213. break;
  214. case 'O':
  215. if (lub_log_facility(optarg, &(opts->log_facility))) {
  216. fprintf(stderr, "Error: Illegal syslog facility %s.\n", optarg);
  217. help(-1, argv[0]);
  218. exit(-1);
  219. }
  220. break;
  221. case 'h':
  222. help(0, argv[0]);
  223. exit(0);
  224. break;
  225. case 'v':
  226. version(VERSION);
  227. exit(0);
  228. break;
  229. default:
  230. help(-1, argv[0]);
  231. exit(-1);
  232. break;
  233. }
  234. }
  235. return 0;
  236. }
  237. /*--------------------------------------------------------- */
  238. /* Print help message */
  239. static void help(int status, const char *argv0)
  240. {
  241. const char *name = NULL;
  242. if (!argv0)
  243. return;
  244. /* Find the basename */
  245. name = strrchr(argv0, '/');
  246. if (name)
  247. name++;
  248. else
  249. name = argv0;
  250. if (status != 0) {
  251. fprintf(stderr, "Try `%s -h' for more information.\n",
  252. name);
  253. } else {
  254. printf("Usage: %s [options]\n", name);
  255. printf("Daemon to store user configuration (i.e. commands). "
  256. "The part of the klish project.\n");
  257. printf("Options:\n");
  258. printf("\t-v, --version\tPrint version.\n");
  259. printf("\t-h, --help\tPrint this help.\n");
  260. printf("\t-d, --debug\tDebug mode. Don't daemonize.\n");
  261. printf("\t-p <path>, --pid=<path>\tFile to save daemon's PID to.\n");
  262. printf("\t-O, --facility\tSyslog facility. Default is DAEMON.\n");
  263. }
  264. }