2 * netifd - network interface daemon
3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
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
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.
26 #include "interface.h"
28 unsigned int debug_mask = 0;
29 const char *main_path = DEFAULT_MAIN_PATH;
30 const char *resolv_conf = DEFAULT_RESOLV_CONF;
31 static char **global_argv;
33 static struct list_head process_list = LIST_HEAD_INIT(process_list);
35 #define DEFAULT_LOG_LEVEL L_NOTICE
37 static int log_level = DEFAULT_LOG_LEVEL;
38 static const int log_class[] = {
40 [L_WARNING] = LOG_WARNING,
41 [L_NOTICE] = LOG_NOTICE,
47 #define use_syslog false
49 static bool use_syslog = true;
54 netifd_delete_process(struct netifd_process *proc)
56 list_del(&proc->list);
57 ustream_free(&proc->log.stream);
58 close(proc->log.fd.fd);
62 netifd_log_message(int priority, const char *format, ...)
66 if (priority > log_level)
71 vsyslog(log_class[priority], format, vl);
73 vfprintf(stderr, format, vl);
78 netifd_process_log_read_cb(struct ustream *s, int bytes)
80 struct netifd_process *proc;
81 const char *log_prefix;
85 proc = container_of(s, struct netifd_process, log.stream);
86 log_prefix = proc->log_prefix;
88 log_prefix = "process";
93 data = ustream_get_read_buf(s, &len);
97 newline = strchr(data, '\n');
99 if (proc->log_overflow) {
101 len = newline + 1 - data;
102 proc->log_overflow = false;
104 } else if (newline) {
106 len = newline + 1 - data;
107 netifd_log_message(L_NOTICE, "%s (%d): %s\n",
108 log_prefix, proc->uloop.pid, data);
109 } else if (len == s->r.buffer_len) {
110 netifd_log_message(L_NOTICE, "%s (%d): %s [...]\n",
111 log_prefix, proc->uloop.pid, data);
112 proc->log_overflow = true;
116 ustream_consume(s, len);
121 netifd_process_cb(struct uloop_process *proc, int ret)
123 struct netifd_process *np;
124 np = container_of(proc, struct netifd_process, uloop);
126 while (ustream_poll(&np->log.stream));
127 netifd_delete_process(np);
128 return np->cb(np, ret);
132 netifd_start_process(const char **argv, char **env, struct netifd_process *proc)
137 netifd_kill_process(proc);
142 if ((pid = fork()) < 0)
154 if (proc->dir_fd >= 0)
155 if (fchdir(proc->dir_fd)) {}
159 for (i = 0; i <= 2; i++) {
169 execvp(argv[0], (char **) argv);
177 proc->uloop.cb = netifd_process_cb;
178 proc->uloop.pid = pid;
179 uloop_process_add(&proc->uloop);
180 list_add_tail(&proc->list, &process_list);
182 system_fd_set_cloexec(pfds[0]);
183 proc->log.stream.string_data = true;
184 proc->log.stream.notify_read = netifd_process_log_read_cb;
185 ustream_fd_init(&proc->log, pfds[0]);
196 netifd_kill_process(struct netifd_process *proc)
198 if (!proc->uloop.pending)
201 kill(proc->uloop.pid, SIGKILL);
202 uloop_process_delete(&proc->uloop);
203 netifd_delete_process(proc);
206 static void netifd_do_restart(struct uloop_timeout *timeout)
208 execvp(global_argv[0], global_argv);
211 static void netifd_do_reload(struct uloop_timeout *timeout)
216 static struct uloop_timeout main_timer;
218 void netifd_reload(void)
220 main_timer.cb = netifd_do_reload;
221 uloop_timeout_set(&main_timer, 100);
224 void netifd_restart(void)
226 main_timer.cb = netifd_do_restart;
227 interface_set_down(NULL);
228 uloop_timeout_set(&main_timer, 1000);
231 static int usage(const char *progname)
233 fprintf(stderr, "Usage: %s [options]\n"
235 " -d <mask>: Mask for debug messages\n"
236 " -s <path>: Path to the ubus socket\n"
237 " -p <path>: Path to netifd addons (default: %s)\n"
238 " -h <path>: Path to the hotplug script\n"
239 " -r <path>: Path to resolv.conf\n"
240 " -l <level>: Log output level (default: %d)\n"
241 " -S: Use stderr instead of syslog for log messages\n"
242 " (default: "DEFAULT_HOTPLUG_PATH")\n"
243 "\n", progname, main_path, DEFAULT_LOG_LEVEL);
249 netifd_handle_signal(int signo)
255 netifd_setup_signals(void)
259 memset(&s, 0, sizeof(s));
260 s.sa_handler = netifd_handle_signal;
262 sigaction(SIGINT, &s, NULL);
263 sigaction(SIGTERM, &s, NULL);
264 sigaction(SIGUSR1, &s, NULL);
265 sigaction(SIGUSR2, &s, NULL);
267 s.sa_handler = SIG_IGN;
268 sigaction(SIGPIPE, &s, NULL);
272 netifd_kill_processes(void)
274 struct netifd_process *proc, *tmp;
276 list_for_each_entry_safe(proc, tmp, &process_list, list)
277 netifd_kill_process(proc);
280 int main(int argc, char **argv)
282 const char *socket = NULL;
287 while ((ch = getopt(argc, argv, "d:s:p:h:r:l:S")) != -1) {
290 debug_mask = strtoul(optarg, NULL, 0);
299 hotplug_cmd_path = optarg;
302 resolv_conf = optarg;
305 log_level = atoi(optarg);
306 if (log_level >= ARRAY_SIZE(log_class))
307 log_level = ARRAY_SIZE(log_class) - 1;
315 return usage(argv[0]);
320 openlog("netifd", 0, LOG_DAEMON);
322 netifd_setup_signals();
323 if (netifd_ubus_init(socket) < 0) {
324 fprintf(stderr, "Failed to connect to ubus\n");
329 fprintf(stderr, "Failed to initialize system control\n");
336 netifd_kill_processes();