birq.c 7.2 KB

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