main.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. static struct udebug ud;
  36. static struct udebug_buf udb_log;
  37. struct udebug_buf udb_nl;
  38. static const struct udebug_buf_meta meta_log = {
  39. .name = "netifd_log",
  40. .format = UDEBUG_FORMAT_STRING,
  41. };
  42. static const struct udebug_buf_meta meta_nl = {
  43. .name = "netifd_nl",
  44. .format = UDEBUG_FORMAT_PACKET,
  45. .sub_format = UDEBUG_DLT_NETLINK,
  46. };
  47. static struct udebug_ubus_ring rings[] = {
  48. {
  49. .buf = &udb_log,
  50. .meta = &meta_log,
  51. .default_entries = 1024,
  52. .default_size = 64 * 1024,
  53. },
  54. {
  55. .buf = &udb_nl,
  56. .meta = &meta_nl,
  57. .default_entries = 1024,
  58. .default_size = 64 * 1024,
  59. },
  60. };
  61. #define DEFAULT_LOG_LEVEL L_NOTICE
  62. static int log_level = DEFAULT_LOG_LEVEL;
  63. static const int log_class[] = {
  64. [L_CRIT] = LOG_CRIT,
  65. [L_WARNING] = LOG_WARNING,
  66. [L_NOTICE] = LOG_NOTICE,
  67. [L_INFO] = LOG_INFO,
  68. [L_DEBUG] = LOG_DEBUG
  69. };
  70. #ifdef DUMMY_MODE
  71. #define use_syslog false
  72. #else
  73. static bool use_syslog = true;
  74. #endif
  75. static void
  76. netifd_delete_process(struct netifd_process *proc)
  77. {
  78. while (ustream_poll(&proc->log.stream));
  79. list_del(&proc->list);
  80. ustream_free(&proc->log.stream);
  81. close(proc->log.fd.fd);
  82. }
  83. static void
  84. netifd_udebug_vprintf(const char *format, va_list ap)
  85. {
  86. if (!udebug_buf_valid(&udb_log))
  87. return;
  88. udebug_entry_init(&udb_log);
  89. udebug_entry_vprintf(&udb_log, format, ap);
  90. udebug_entry_add(&udb_log);
  91. }
  92. void netifd_udebug_printf(const char *format, ...)
  93. {
  94. va_list ap;
  95. va_start(ap, format);
  96. netifd_udebug_vprintf(format, ap);
  97. va_end(ap);
  98. }
  99. void netifd_udebug_config(struct udebug_ubus *ctx, struct blob_attr *data,
  100. bool enabled)
  101. {
  102. udebug_ubus_apply_config(&ud, rings, ARRAY_SIZE(rings), data, enabled);
  103. }
  104. void
  105. __attribute__((format(printf, 2, 0)))
  106. netifd_log_message(int priority, const char *format, ...)
  107. {
  108. va_list vl;
  109. if (priority > log_level)
  110. return;
  111. va_start(vl, format);
  112. netifd_udebug_vprintf(format, vl);
  113. va_end(vl);
  114. va_start(vl, format);
  115. if (use_syslog)
  116. vsyslog(log_class[priority], format, vl);
  117. else
  118. vfprintf(stderr, format, vl);
  119. va_end(vl);
  120. }
  121. static void
  122. netifd_process_log_read_cb(struct ustream *s, int bytes)
  123. {
  124. struct netifd_process *proc;
  125. const char *log_prefix;
  126. char *data;
  127. int len = 0;
  128. proc = container_of(s, struct netifd_process, log.stream);
  129. log_prefix = proc->log_prefix;
  130. if (!log_prefix)
  131. log_prefix = "process";
  132. do {
  133. char *newline;
  134. data = ustream_get_read_buf(s, &len);
  135. if (!len)
  136. break;
  137. newline = strchr(data, '\n');
  138. if (proc->log_overflow) {
  139. if (newline) {
  140. len = newline + 1 - data;
  141. proc->log_overflow = false;
  142. }
  143. } else if (newline) {
  144. *newline = 0;
  145. len = newline + 1 - data;
  146. netifd_log_message(L_NOTICE, "%s (%d): %s\n",
  147. log_prefix, proc->uloop.pid, data);
  148. } else if (len == s->r.buffer_len) {
  149. netifd_log_message(L_NOTICE, "%s (%d): %s [...]\n",
  150. log_prefix, proc->uloop.pid, data);
  151. proc->log_overflow = true;
  152. } else
  153. break;
  154. ustream_consume(s, len);
  155. } while (1);
  156. }
  157. static void
  158. netifd_process_cb(struct uloop_process *proc, int ret)
  159. {
  160. struct netifd_process *np;
  161. np = container_of(proc, struct netifd_process, uloop);
  162. netifd_delete_process(np);
  163. np->cb(np, ret);
  164. return;
  165. }
  166. int
  167. netifd_start_process(const char **argv, char **env, struct netifd_process *proc)
  168. {
  169. int pfds[2];
  170. int pid;
  171. netifd_kill_process(proc);
  172. if (pipe(pfds) < 0)
  173. return -1;
  174. if ((pid = fork()) < 0)
  175. goto error;
  176. if (!pid) {
  177. int i;
  178. if (env) {
  179. while (*env) {
  180. putenv(*env);
  181. env++;
  182. }
  183. }
  184. if (proc->dir_fd >= 0)
  185. if (fchdir(proc->dir_fd)) {}
  186. close(pfds[0]);
  187. for (i = 0; i <= 2; i++) {
  188. if (pfds[1] == i)
  189. continue;
  190. dup2(pfds[1], i);
  191. }
  192. if (pfds[1] > 2)
  193. close(pfds[1]);
  194. execvp(argv[0], (char **) argv);
  195. exit(127);
  196. }
  197. close(pfds[1]);
  198. proc->uloop.cb = netifd_process_cb;
  199. proc->uloop.pid = pid;
  200. uloop_process_add(&proc->uloop);
  201. list_add_tail(&proc->list, &process_list);
  202. system_fd_set_cloexec(pfds[0]);
  203. proc->log.stream.string_data = true;
  204. proc->log.stream.notify_read = netifd_process_log_read_cb;
  205. ustream_fd_init(&proc->log, pfds[0]);
  206. return 0;
  207. error:
  208. close(pfds[0]);
  209. close(pfds[1]);
  210. return -1;
  211. }
  212. void
  213. netifd_kill_process(struct netifd_process *proc)
  214. {
  215. if (!proc->uloop.pending)
  216. return;
  217. kill(proc->uloop.pid, SIGKILL);
  218. uloop_process_delete(&proc->uloop);
  219. netifd_delete_process(proc);
  220. }
  221. static void netifd_do_restart(struct uloop_timeout *timeout)
  222. {
  223. execvp(global_argv[0], global_argv);
  224. }
  225. int netifd_reload(void)
  226. {
  227. return config_init_all();
  228. }
  229. void netifd_restart(void)
  230. {
  231. static struct uloop_timeout main_timer = {
  232. .cb = netifd_do_restart
  233. };
  234. interface_set_down(NULL);
  235. uloop_timeout_set(&main_timer, 1000);
  236. }
  237. static int usage(const char *progname)
  238. {
  239. fprintf(stderr, "Usage: %s [options]\n"
  240. "Options:\n"
  241. " -d <mask>: Mask for debug messages\n"
  242. " -s <path>: Path to the ubus socket\n"
  243. " -p <path>: Path to netifd addons (default: %s)\n"
  244. " -c <path>: Path to UCI configuration\n"
  245. " -h <path>: Path to the hotplug script\n"
  246. " (default: "DEFAULT_HOTPLUG_PATH")\n"
  247. " -r <path>: Path to resolv.conf\n"
  248. " -l <level>: Log output level (default: %d)\n"
  249. " -S: Use stderr instead of syslog for log messages\n"
  250. "\n", progname, main_path, DEFAULT_LOG_LEVEL);
  251. return 1;
  252. }
  253. static void
  254. netifd_handle_signal(int signo)
  255. {
  256. uloop_end();
  257. }
  258. static void
  259. netifd_setup_signals(void)
  260. {
  261. struct sigaction s;
  262. memset(&s, 0, sizeof(s));
  263. s.sa_handler = netifd_handle_signal;
  264. s.sa_flags = 0;
  265. sigaction(SIGINT, &s, NULL);
  266. sigaction(SIGTERM, &s, NULL);
  267. sigaction(SIGUSR1, &s, NULL);
  268. sigaction(SIGUSR2, &s, NULL);
  269. s.sa_handler = SIG_IGN;
  270. sigaction(SIGPIPE, &s, NULL);
  271. }
  272. static void
  273. netifd_kill_processes(void)
  274. {
  275. struct netifd_process *proc, *tmp;
  276. list_for_each_entry_safe(proc, tmp, &process_list, list)
  277. netifd_kill_process(proc);
  278. }
  279. int main(int argc, char **argv)
  280. {
  281. const char *socket = NULL;
  282. int ch;
  283. global_argv = argv;
  284. while ((ch = getopt(argc, argv, "d:s:p:c:h:r:l:S")) != -1) {
  285. switch(ch) {
  286. case 'd':
  287. debug_mask = strtoul(optarg, NULL, 0);
  288. break;
  289. case 's':
  290. socket = optarg;
  291. break;
  292. case 'p':
  293. main_path = optarg;
  294. break;
  295. case 'c':
  296. config_path = optarg;
  297. break;
  298. case 'h':
  299. hotplug_cmd_path = optarg;
  300. break;
  301. case 'r':
  302. resolv_conf = optarg;
  303. break;
  304. case 'l':
  305. log_level = atoi(optarg);
  306. if (log_level >= (int)ARRAY_SIZE(log_class))
  307. log_level = (int)ARRAY_SIZE(log_class) - 1;
  308. break;
  309. #ifndef DUMMY_MODE
  310. case 'S':
  311. use_syslog = false;
  312. break;
  313. #endif
  314. default:
  315. return usage(argv[0]);
  316. }
  317. }
  318. if (use_syslog)
  319. openlog("netifd", 0, LOG_DAEMON);
  320. netifd_setup_signals();
  321. uloop_init();
  322. udebug_init(&ud);
  323. udebug_auto_connect(&ud, NULL);
  324. for (size_t i = 0; i < ARRAY_SIZE(rings); i++)
  325. udebug_ubus_ring_init(&ud, &rings[i]);
  326. if (netifd_ubus_init(socket) < 0) {
  327. fprintf(stderr, "Failed to connect to ubus\n");
  328. return 1;
  329. }
  330. proto_shell_init();
  331. extdev_init();
  332. wireless_init();
  333. if (system_init()) {
  334. fprintf(stderr, "Failed to initialize system control\n");
  335. return 1;
  336. }
  337. config_init_all();
  338. uloop_run();
  339. netifd_kill_processes();
  340. netifd_ubus_done();
  341. if (use_syslog)
  342. closelog();
  343. return 0;
  344. }