birq.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. }
  158. end:
  159. /* Free data structures */
  160. irq_list_free(irqs);
  161. cpu_list_free(cpus);
  162. retval = 0;
  163. err:
  164. /* Close NetLink socket */
  165. nl_close(nl_fds);
  166. /* Remove pidfile */
  167. if (pidfd >= 0) {
  168. if (unlink(opts->pidfile) < 0) {
  169. syslog(LOG_ERR, "Can't remove pid-file %s: %s\n",
  170. opts->pidfile, strerror(errno));
  171. }
  172. }
  173. /* Free command line options */
  174. opts_free(opts);
  175. syslog(LOG_ERR, "Stop daemon.\n");
  176. return retval;
  177. }
  178. /*--------------------------------------------------------- */
  179. /*
  180. * Signal handler for temination signals (like SIGTERM, SIGINT, ...)
  181. */
  182. static void sighandler(int signo)
  183. {
  184. sigterm = 1;
  185. }
  186. /*--------------------------------------------------------- */
  187. /*
  188. * Force IRQ rescan (SIGUSR1)
  189. */
  190. static void rescan_sighandler(int signo)
  191. {
  192. rescan = 1;
  193. }
  194. /*--------------------------------------------------------- */
  195. /* Initialize option structure by defaults */
  196. static struct options *opts_init(void)
  197. {
  198. struct options *opts = NULL;
  199. opts = malloc(sizeof(*opts));
  200. assert(opts);
  201. opts->debug = 0; /* daemonize by default */
  202. opts->pidfile = strdup(BIRQ_PIDFILE);
  203. opts->log_facility = LOG_DAEMON;
  204. return opts;
  205. }
  206. /*--------------------------------------------------------- */
  207. /* Free option structure */
  208. static void opts_free(struct options *opts)
  209. {
  210. if (opts->pidfile)
  211. free(opts->pidfile);
  212. free(opts);
  213. }
  214. /*--------------------------------------------------------- */
  215. /* Parse command line options */
  216. static int opts_parse(int argc, char *argv[], struct options *opts)
  217. {
  218. static const char *shortopts = "hvp:dO:";
  219. #ifdef HAVE_GETOPT_H
  220. static const struct option longopts[] = {
  221. {"help", 0, NULL, 'h'},
  222. {"version", 0, NULL, 'v'},
  223. {"pid", 1, NULL, 'p'},
  224. {"debug", 0, NULL, 'd'},
  225. {"facility", 1, NULL, 'O'},
  226. {NULL, 0, NULL, 0}
  227. };
  228. #endif
  229. optind = 1;
  230. while(1) {
  231. int opt;
  232. #ifdef HAVE_GETOPT_H
  233. opt = getopt_long(argc, argv, shortopts, longopts, NULL);
  234. #else
  235. opt = getopt(argc, argv, shortopts);
  236. #endif
  237. if (-1 == opt)
  238. break;
  239. switch (opt) {
  240. case 'p':
  241. if (opts->pidfile)
  242. free(opts->pidfile);
  243. opts->pidfile = strdup(optarg);
  244. break;
  245. case 'd':
  246. opts->debug = 1;
  247. break;
  248. case 'O':
  249. if (lub_log_facility(optarg, &(opts->log_facility))) {
  250. fprintf(stderr, "Error: Illegal syslog facility %s.\n", optarg);
  251. help(-1, argv[0]);
  252. exit(-1);
  253. }
  254. break;
  255. case 'h':
  256. help(0, argv[0]);
  257. exit(0);
  258. break;
  259. case 'v':
  260. version(VERSION);
  261. exit(0);
  262. break;
  263. default:
  264. help(-1, argv[0]);
  265. exit(-1);
  266. break;
  267. }
  268. }
  269. return 0;
  270. }
  271. /*--------------------------------------------------------- */
  272. /* Print help message */
  273. static void help(int status, const char *argv0)
  274. {
  275. const char *name = NULL;
  276. if (!argv0)
  277. return;
  278. /* Find the basename */
  279. name = strrchr(argv0, '/');
  280. if (name)
  281. name++;
  282. else
  283. name = argv0;
  284. if (status != 0) {
  285. fprintf(stderr, "Try `%s -h' for more information.\n",
  286. name);
  287. } else {
  288. printf("Usage: %s [options]\n", name);
  289. printf("Daemon to store user configuration (i.e. commands). "
  290. "The part of the klish project.\n");
  291. printf("Options:\n");
  292. printf("\t-v, --version\tPrint version.\n");
  293. printf("\t-h, --help\tPrint this help.\n");
  294. printf("\t-d, --debug\tDebug mode. Don't daemonize.\n");
  295. printf("\t-p <path>, --pid=<path>\tFile to save daemon's PID to.\n");
  296. printf("\t-O, --facility\tSyslog facility. Default is DAEMON.\n");
  297. }
  298. }