birq.c 8.1 KB

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