birq.c 8.3 KB

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