birq.c 7.2 KB

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