birq.c 6.8 KB

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