main.c 6.7 KB

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