main.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. * netifd - network interface daemon
  3. * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2
  7. * as published by the Free Software Foundation
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <getopt.h>
  18. #include <unistd.h>
  19. #include <signal.h>
  20. #include <stdarg.h>
  21. #include <syslog.h>
  22. #include "netifd.h"
  23. #include "ubus.h"
  24. #include "config.h"
  25. #include "system.h"
  26. #include "interface.h"
  27. unsigned int debug_mask = 0;
  28. const char *main_path = DEFAULT_MAIN_PATH;
  29. const char *resolv_conf = DEFAULT_RESOLV_CONF;
  30. static char **global_argv;
  31. static struct list_head process_list = LIST_HEAD_INIT(process_list);
  32. #define DEFAULT_LOG_LEVEL L_NOTICE
  33. static int log_level = DEFAULT_LOG_LEVEL;
  34. static const int log_class[] = {
  35. [L_CRIT] = LOG_CRIT,
  36. [L_WARNING] = LOG_WARNING,
  37. [L_NOTICE] = LOG_NOTICE,
  38. [L_INFO] = LOG_INFO,
  39. [L_DEBUG] = LOG_DEBUG
  40. };
  41. #ifdef DUMMY_MODE
  42. #define use_syslog false
  43. #else
  44. static bool use_syslog = true;
  45. #endif
  46. static void
  47. netifd_delete_process(struct netifd_process *proc)
  48. {
  49. list_del(&proc->list);
  50. ustream_free(&proc->log.stream);
  51. close(proc->log.fd.fd);
  52. }
  53. void
  54. netifd_log_message(int priority, const char *format, ...)
  55. {
  56. va_list vl;
  57. if (priority > log_level)
  58. return;
  59. va_start(vl, format);
  60. if (use_syslog)
  61. vsyslog(log_class[priority], format, vl);
  62. else
  63. vfprintf(stderr, format, vl);
  64. va_end(vl);
  65. }
  66. static void
  67. netifd_process_log_read_cb(struct ustream *s, int bytes)
  68. {
  69. struct netifd_process *proc;
  70. const char *log_prefix;
  71. char *data;
  72. int len = 0;
  73. proc = container_of(s, struct netifd_process, log.stream);
  74. log_prefix = proc->log_prefix;
  75. if (!log_prefix)
  76. log_prefix = "process";
  77. do {
  78. char *newline;
  79. data = ustream_get_read_buf(s, &len);
  80. if (!len)
  81. break;
  82. newline = strchr(data, '\n');
  83. if (proc->log_overflow) {
  84. if (newline) {
  85. len = newline + 1 - data;
  86. proc->log_overflow = false;
  87. }
  88. } else if (newline) {
  89. *newline = 0;
  90. len = newline + 1 - data;
  91. netifd_log_message(L_NOTICE, "%s (%d): %s\n",
  92. log_prefix, proc->uloop.pid, data);
  93. } else if (len == s->r.buffer_len) {
  94. netifd_log_message(L_NOTICE, "%s (%d): %s [...]\n",
  95. log_prefix, proc->uloop.pid, data);
  96. proc->log_overflow = true;
  97. } else
  98. break;
  99. ustream_consume(s, len);
  100. } while (1);
  101. }
  102. static void
  103. netifd_process_cb(struct uloop_process *proc, int ret)
  104. {
  105. struct netifd_process *np;
  106. np = container_of(proc, struct netifd_process, uloop);
  107. while (ustream_poll(&np->log.stream));
  108. netifd_delete_process(np);
  109. return np->cb(np, ret);
  110. }
  111. int
  112. netifd_start_process(const char **argv, char **env, struct netifd_process *proc)
  113. {
  114. int pfds[2];
  115. int pid;
  116. netifd_kill_process(proc);
  117. if (pipe(pfds) < 0)
  118. return -1;
  119. if ((pid = fork()) < 0)
  120. goto error;
  121. if (!pid) {
  122. if (env) {
  123. while (*env) {
  124. putenv(*env);
  125. env++;
  126. }
  127. }
  128. if (proc->dir_fd >= 0)
  129. fchdir(proc->dir_fd);
  130. dup2(pfds[1], 0);
  131. dup2(pfds[1], 1);
  132. dup2(pfds[1], 2);
  133. close(pfds[0]);
  134. close(pfds[1]);
  135. execvp(argv[0], (char **) argv);
  136. exit(127);
  137. }
  138. if (pid < 0)
  139. goto error;
  140. close(pfds[1]);
  141. proc->uloop.cb = netifd_process_cb;
  142. proc->uloop.pid = pid;
  143. uloop_process_add(&proc->uloop);
  144. list_add_tail(&proc->list, &process_list);
  145. system_fd_set_cloexec(pfds[0]);
  146. proc->log.stream.string_data = true;
  147. proc->log.stream.notify_read = netifd_process_log_read_cb;
  148. ustream_fd_init(&proc->log, pfds[0]);
  149. return 0;
  150. error:
  151. close(pfds[0]);
  152. close(pfds[1]);
  153. return -1;
  154. }
  155. void
  156. netifd_kill_process(struct netifd_process *proc)
  157. {
  158. if (!proc->uloop.pending)
  159. return;
  160. kill(proc->uloop.pid, SIGKILL);
  161. uloop_process_delete(&proc->uloop);
  162. netifd_delete_process(proc);
  163. }
  164. static void netifd_do_restart(struct uloop_timeout *timeout)
  165. {
  166. execvp(global_argv[0], global_argv);
  167. }
  168. static void netifd_do_reload(struct uloop_timeout *timeout)
  169. {
  170. config_init_all();
  171. }
  172. static struct uloop_timeout main_timer;
  173. void netifd_reload(void)
  174. {
  175. main_timer.cb = netifd_do_reload;
  176. uloop_timeout_set(&main_timer, 100);
  177. }
  178. void netifd_restart(void)
  179. {
  180. main_timer.cb = netifd_do_restart;
  181. interface_set_down(NULL);
  182. uloop_timeout_set(&main_timer, 1000);
  183. }
  184. static int usage(const char *progname)
  185. {
  186. fprintf(stderr, "Usage: %s [options]\n"
  187. "Options:\n"
  188. " -d <mask>: Mask for debug messages\n"
  189. " -s <path>: Path to the ubus socket\n"
  190. " -p <path>: Path to netifd addons (default: %s)\n"
  191. " -h <path>: Path to the hotplug script\n"
  192. " -r <path>: Path to resolv.conf\n"
  193. " -l <level>: Log output level (default: %d)\n"
  194. " -S: Use stderr instead of syslog for log messages\n"
  195. " (default: "DEFAULT_HOTPLUG_PATH")\n"
  196. "\n", progname, main_path, DEFAULT_LOG_LEVEL);
  197. return 1;
  198. }
  199. static void
  200. netifd_handle_signal(int signo)
  201. {
  202. uloop_end();
  203. }
  204. static void
  205. netifd_setup_signals(void)
  206. {
  207. struct sigaction s;
  208. memset(&s, 0, sizeof(s));
  209. s.sa_handler = netifd_handle_signal;
  210. s.sa_flags = 0;
  211. sigaction(SIGINT, &s, NULL);
  212. sigaction(SIGTERM, &s, NULL);
  213. sigaction(SIGUSR1, &s, NULL);
  214. sigaction(SIGUSR2, &s, NULL);
  215. s.sa_handler = SIG_IGN;
  216. sigaction(SIGPIPE, &s, NULL);
  217. }
  218. static void
  219. netifd_kill_processes(void)
  220. {
  221. struct netifd_process *proc, *tmp;
  222. list_for_each_entry_safe(proc, tmp, &process_list, list)
  223. netifd_kill_process(proc);
  224. }
  225. int main(int argc, char **argv)
  226. {
  227. const char *socket = NULL;
  228. int ch;
  229. global_argv = argv;
  230. while ((ch = getopt(argc, argv, "d:s:p:h:r:l:S")) != -1) {
  231. switch(ch) {
  232. case 'd':
  233. debug_mask = strtoul(optarg, NULL, 0);
  234. break;
  235. case 's':
  236. socket = optarg;
  237. break;
  238. case 'p':
  239. main_path = optarg;
  240. break;
  241. case 'h':
  242. hotplug_cmd_path = optarg;
  243. break;
  244. case 'r':
  245. resolv_conf = optarg;
  246. break;
  247. case 'l':
  248. log_level = atoi(optarg);
  249. if (log_level >= ARRAY_SIZE(log_class))
  250. log_level = ARRAY_SIZE(log_class) - 1;
  251. break;
  252. #ifndef DUMMY_MODE
  253. case 'S':
  254. use_syslog = false;
  255. break;
  256. #endif
  257. default:
  258. return usage(argv[0]);
  259. }
  260. }
  261. if (use_syslog)
  262. openlog("netifd", 0, LOG_DAEMON);
  263. netifd_setup_signals();
  264. if (netifd_ubus_init(socket) < 0) {
  265. fprintf(stderr, "Failed to connect to ubus\n");
  266. return 1;
  267. }
  268. if (system_init()) {
  269. fprintf(stderr, "Failed to initialize system control\n");
  270. return 1;
  271. }
  272. config_init_all();
  273. uloop_run();
  274. netifd_kill_processes();
  275. netifd_ubus_done();
  276. if (use_syslog)
  277. closelog();
  278. return 0;
  279. }