birq.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. #ifndef VERSION
  29. #define VERSION 1.0.0
  30. #endif
  31. #define QUOTE(t) #t
  32. #define version(v) printf("%s\n", v)
  33. #define BIRQ_PIDFILE "/var/run/birq.pid"
  34. /* Global signal vars */
  35. static volatile int sigterm = 0;
  36. static void sighandler(int signo);
  37. static void help(int status, const char *argv0);
  38. struct options *opts_init(void);
  39. void opts_free(struct options *opts);
  40. static int opts_parse(int argc, char *argv[], struct options *opts);
  41. static int nl_init(void);
  42. static void nl_close(int nl);
  43. static int nl_poll(int nl, int timeout);
  44. /* Command line options */
  45. struct options {
  46. char *pidfile;
  47. int debug; /* Don't daemonize in debug mode */
  48. int log_facility;
  49. };
  50. /*--------------------------------------------------------- */
  51. int main(int argc, char **argv)
  52. {
  53. int retval = -1;
  54. struct options *opts = NULL;
  55. int pidfd = -1;
  56. int rescan = 0; /* sysfs rescan needed */
  57. /* Signal vars */
  58. struct sigaction sig_act;
  59. sigset_t sig_set;
  60. /* NetLink vars */
  61. int nl = -1; /* NetLink socket */
  62. /* Parse command line options */
  63. opts = opts_init();
  64. if (opts_parse(argc, argv, opts))
  65. goto err;
  66. /* Initialize syslog */
  67. openlog(argv[0], LOG_CONS, opts->log_facility);
  68. syslog(LOG_ERR, "Start daemon.\n");
  69. /* Init NetLink socket */
  70. if ((nl = nl_init()) < 0)
  71. goto err;
  72. /* Fork the daemon */
  73. if (!opts->debug) {
  74. /* Daemonize */
  75. if (daemon(0, 0) < 0) {
  76. syslog(LOG_ERR, "Can't daemonize\n");
  77. goto err;
  78. }
  79. /* Write pidfile */
  80. if ((pidfd = open(opts->pidfile,
  81. O_WRONLY | O_CREAT | O_EXCL | O_TRUNC,
  82. S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0) {
  83. syslog(LOG_WARNING, "Can't open pidfile %s: %s",
  84. opts->pidfile, strerror(errno));
  85. } else {
  86. char str[20];
  87. snprintf(str, sizeof(str), "%u\n", getpid());
  88. if (write(pidfd, str, strlen(str)) < 0)
  89. syslog(LOG_WARNING, "Can't write to %s: %s",
  90. opts->pidfile, strerror(errno));
  91. close(pidfd);
  92. }
  93. }
  94. /* Set signal handler */
  95. sigemptyset(&sig_set);
  96. sigaddset(&sig_set, SIGTERM);
  97. sigaddset(&sig_set, SIGINT);
  98. sigaddset(&sig_set, SIGQUIT);
  99. sig_act.sa_flags = 0;
  100. sig_act.sa_mask = sig_set;
  101. sig_act.sa_handler = &sighandler;
  102. sigaction(SIGTERM, &sig_act, NULL);
  103. sigaction(SIGINT, &sig_act, NULL);
  104. sigaction(SIGQUIT, &sig_act, NULL);
  105. /* Main loop */
  106. while (!sigterm) {
  107. int n;
  108. n = nl_poll(nl, 5);
  109. if (n < 0) {
  110. if (-2 == n) /* EINTR */
  111. continue;
  112. break;
  113. }
  114. if (n > 0) {
  115. rescan = 1;
  116. continue;
  117. }
  118. if (rescan) {
  119. fprintf(stdout, "Rescanning...\n");
  120. rescan = 0;
  121. }
  122. }
  123. retval = 0;
  124. err:
  125. /* Close NetLink socket */
  126. nl_close(nl);
  127. /* Remove pidfile */
  128. if (pidfd >= 0) {
  129. if (unlink(opts->pidfile) < 0) {
  130. syslog(LOG_ERR, "Can't remove pid-file %s: %s\n",
  131. opts->pidfile, strerror(errno));
  132. }
  133. }
  134. /* Free command line options */
  135. opts_free(opts);
  136. syslog(LOG_ERR, "Stop daemon.\n");
  137. return retval;
  138. }
  139. static int nl_init(void)
  140. {
  141. struct sockaddr_nl nl_addr;
  142. int nl;
  143. memset(&nl_addr, 0, sizeof(nl_addr));
  144. nl_addr.nl_family = AF_NETLINK;
  145. nl_addr.nl_pad = 0;
  146. nl_addr.nl_pid = 0; /* Let kernel to assign id */
  147. nl_addr.nl_groups = -1; /* Listen all multicast */
  148. if ((nl = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT)) < 0) {
  149. fprintf(stderr, "Error: Can't create socket\n");
  150. return -1;
  151. }
  152. if (bind(nl, (void *)&nl_addr, sizeof(nl_addr))) {
  153. fprintf(stderr, "Error: Can't bind NetLink\n");
  154. return -1;
  155. }
  156. return nl;
  157. }
  158. static void nl_close(int nl)
  159. {
  160. if (nl >= 0)
  161. close(nl);
  162. }
  163. static int nl_poll(int nl, int timeout)
  164. {
  165. struct pollfd pfd;
  166. char buf[10];
  167. int n;
  168. pfd.events = POLLIN;
  169. pfd.fd = nl;
  170. n = poll(&pfd, 1, (timeout * 1000));
  171. if (n < 0) {
  172. if (EINTR == errno)
  173. return -2;
  174. return -1;
  175. }
  176. /* Some device-related event */
  177. /* Read all messages. We don't need a message content. */
  178. if (n > 0)
  179. while (recv(nl, buf, sizeof(buf), MSG_DONTWAIT) > 0);
  180. return n;
  181. }
  182. /*--------------------------------------------------------- */
  183. /*
  184. * Signal handler for temination signals (like SIGTERM, SIGINT, ...)
  185. */
  186. static void sighandler(int signo)
  187. {
  188. sigterm = 1;
  189. }
  190. /*--------------------------------------------------------- */
  191. /* Initialize option structure by defaults */
  192. struct options *opts_init(void)
  193. {
  194. struct options *opts = NULL;
  195. opts = malloc(sizeof(*opts));
  196. assert(opts);
  197. opts->debug = 0; /* daemonize by default */
  198. opts->pidfile = strdup(BIRQ_PIDFILE);
  199. opts->log_facility = LOG_DAEMON;
  200. return opts;
  201. }
  202. /*--------------------------------------------------------- */
  203. /* Free option structure */
  204. void opts_free(struct options *opts)
  205. {
  206. if (opts->pidfile)
  207. free(opts->pidfile);
  208. free(opts);
  209. }
  210. /*--------------------------------------------------------- */
  211. /* Parse command line options */
  212. static int opts_parse(int argc, char *argv[], struct options *opts)
  213. {
  214. static const char *shortopts = "hvp:dO:";
  215. #ifdef HAVE_GETOPT_H
  216. static const struct option longopts[] = {
  217. {"help", 0, NULL, 'h'},
  218. {"version", 0, NULL, 'v'},
  219. {"pid", 1, NULL, 'p'},
  220. {"debug", 0, NULL, 'd'},
  221. {"facility", 1, NULL, 'O'},
  222. {NULL, 0, NULL, 0}
  223. };
  224. #endif
  225. optind = 1;
  226. while(1) {
  227. int opt;
  228. #ifdef HAVE_GETOPT_H
  229. opt = getopt_long(argc, argv, shortopts, longopts, NULL);
  230. #else
  231. opt = getopt(argc, argv, shortopts);
  232. #endif
  233. if (-1 == opt)
  234. break;
  235. switch (opt) {
  236. case 'p':
  237. if (opts->pidfile)
  238. free(opts->pidfile);
  239. opts->pidfile = strdup(optarg);
  240. break;
  241. case 'd':
  242. opts->debug = 1;
  243. break;
  244. case 'O':
  245. if (lub_log_facility(optarg, &(opts->log_facility))) {
  246. fprintf(stderr, "Error: Illegal syslog facility %s.\n", optarg);
  247. help(-1, argv[0]);
  248. exit(-1);
  249. }
  250. break;
  251. case 'h':
  252. help(0, argv[0]);
  253. exit(0);
  254. break;
  255. case 'v':
  256. version(VERSION);
  257. exit(0);
  258. break;
  259. default:
  260. help(-1, argv[0]);
  261. exit(-1);
  262. break;
  263. }
  264. }
  265. return 0;
  266. }
  267. /*--------------------------------------------------------- */
  268. /* Print help message */
  269. static void help(int status, const char *argv0)
  270. {
  271. const char *name = NULL;
  272. if (!argv0)
  273. return;
  274. /* Find the basename */
  275. name = strrchr(argv0, '/');
  276. if (name)
  277. name++;
  278. else
  279. name = argv0;
  280. if (status != 0) {
  281. fprintf(stderr, "Try `%s -h' for more information.\n",
  282. name);
  283. } else {
  284. printf("Usage: %s [options]\n", name);
  285. printf("Daemon to store user configuration (i.e. commands). "
  286. "The part of the klish project.\n");
  287. printf("Options:\n");
  288. printf("\t-v, --version\tPrint version.\n");
  289. printf("\t-h, --help\tPrint this help.\n");
  290. printf("\t-d, --debug\tDebug mode. Don't daemonize.\n");
  291. printf("\t-p <path>, --pid=<path>\tFile to save daemon's PID to.\n");
  292. printf("\t-O, --facility\tSyslog facility. Default is DAEMON.\n");
  293. }
  294. }