main.c 6.7 KB

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