main.c 6.6 KB

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