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.
27 #include "interface.h"
29 unsigned int debug_mask = 0;
30 const char *main_path = DEFAULT_MAIN_PATH;
31 const char *resolv_conf = DEFAULT_RESOLV_CONF;
32 static char **global_argv;
34 static struct list_head process_list = LIST_HEAD_INIT(process_list);
36 #define DEFAULT_LOG_LEVEL L_NOTICE
38 static int log_level = DEFAULT_LOG_LEVEL;
39 static const int log_class[] = {
41 [L_WARNING] = LOG_WARNING,
42 [L_NOTICE] = LOG_NOTICE,
48 #define use_syslog false
50 static bool use_syslog = true;
55 netifd_delete_process(struct netifd_process *proc)
57 list_del(&proc->list);
58 ustream_free(&proc->log.stream);
59 close(proc->log.fd.fd);
63 netifd_log_message(int priority, const char *format, ...)
67 if (priority > log_level)
72 vsyslog(log_class[priority], format, vl);
74 vfprintf(stderr, format, vl);
79 netifd_process_log_read_cb(struct ustream *s, int bytes)
81 struct netifd_process *proc;
82 const char *log_prefix;
86 proc = container_of(s, struct netifd_process, log.stream);
87 log_prefix = proc->log_prefix;
89 log_prefix = "process";
94 data = ustream_get_read_buf(s, &len);
98 newline = strchr(data, '\n');
100 if (proc->log_overflow) {
102 len = newline + 1 - data;
103 proc->log_overflow = false;
105 } else if (newline) {
107 len = newline + 1 - data;
108 netifd_log_message(L_NOTICE, "%s (%d): %s\n",
109 log_prefix, proc->uloop.pid, data);
110 } else if (len == s->r.buffer_len) {
111 netifd_log_message(L_NOTICE, "%s (%d): %s [...]\n",
112 log_prefix, proc->uloop.pid, data);
113 proc->log_overflow = true;
117 ustream_consume(s, len);
122 netifd_process_cb(struct uloop_process *proc, int ret)
124 struct netifd_process *np;
125 np = container_of(proc, struct netifd_process, uloop);
127 while (ustream_poll(&np->log.stream));
128 netifd_delete_process(np);
129 return np->cb(np, ret);
133 netifd_start_process(const char **argv, char **env, struct netifd_process *proc)
138 netifd_kill_process(proc);
143 if ((pid = fork()) < 0)
153 if (proc->dir_fd >= 0)
154 fchdir(proc->dir_fd);
163 execvp(argv[0], (char **) argv);
171 proc->uloop.cb = netifd_process_cb;
172 proc->uloop.pid = pid;
173 uloop_process_add(&proc->uloop);
174 list_add_tail(&proc->list, &process_list);
176 system_fd_set_cloexec(pfds[0]);
177 proc->log.stream.string_data = true;
178 proc->log.stream.notify_read = netifd_process_log_read_cb;
179 ustream_fd_init(&proc->log, pfds[0]);
190 netifd_kill_process(struct netifd_process *proc)
192 if (!proc->uloop.pending)
195 kill(proc->uloop.pid, SIGKILL);
196 uloop_process_delete(&proc->uloop);
197 netifd_delete_process(proc);
200 static void netifd_do_restart(struct uloop_timeout *timeout)
202 execvp(global_argv[0], global_argv);
205 static void netifd_do_reload(struct uloop_timeout *timeout)
210 static struct uloop_timeout main_timer;
212 void netifd_reload(void)
214 main_timer.cb = netifd_do_reload;
215 uloop_timeout_set(&main_timer, 100);
218 void netifd_restart(void)
220 main_timer.cb = netifd_do_restart;
221 interface_set_down(NULL);
222 uloop_timeout_set(&main_timer, 1000);
225 static int usage(const char *progname)
227 fprintf(stderr, "Usage: %s [options]\n"
229 " -d <mask>: Mask for debug messages\n"
230 " -s <path>: Path to the ubus socket\n"
231 " -p <path>: Path to netifd addons (default: %s)\n"
232 " -h <path>: Path to the hotplug script\n"
233 " -r <path>: Path to resolv.conf\n"
234 " -l <level>: Log output level (default: %d)\n"
235 " -S: Use stderr instead of syslog for log messages\n"
236 " (default: "DEFAULT_HOTPLUG_PATH")\n"
237 "\n", progname, main_path, DEFAULT_LOG_LEVEL);
243 netifd_handle_signal(int signo)
249 netifd_setup_signals(void)
253 memset(&s, 0, sizeof(s));
254 s.sa_handler = netifd_handle_signal;
256 sigaction(SIGINT, &s, NULL);
257 sigaction(SIGTERM, &s, NULL);
258 sigaction(SIGUSR1, &s, NULL);
259 sigaction(SIGUSR2, &s, NULL);
261 s.sa_handler = SIG_IGN;
262 sigaction(SIGPIPE, &s, NULL);
266 netifd_kill_processes(void)
268 struct netifd_process *proc, *tmp;
270 list_for_each_entry_safe(proc, tmp, &process_list, list)
271 netifd_kill_process(proc);
274 int main(int argc, char **argv)
276 const char *socket = NULL;
281 while ((ch = getopt(argc, argv, "d:s:p:h:r:l:S")) != -1) {
284 debug_mask = strtoul(optarg, NULL, 0);
293 hotplug_cmd_path = optarg;
296 resolv_conf = optarg;
299 log_level = atoi(optarg);
300 if (log_level >= ARRAY_SIZE(log_class))
301 log_level = ARRAY_SIZE(log_class) - 1;
309 return usage(argv[0]);
314 openlog("netifd", 0, LOG_DAEMON);
316 netifd_setup_signals();
317 if (netifd_ubus_init(socket) < 0) {
318 fprintf(stderr, "Failed to connect to ubus\n");
323 fprintf(stderr, "Failed to initialize system control\n");
330 netifd_kill_processes();