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);
35 static struct list_head fds = LIST_HEAD_INIT(fds);
37 #define DEFAULT_LOG_LEVEL L_NOTICE
39 static int log_level = DEFAULT_LOG_LEVEL;
40 static const int log_class[] = {
42 [L_WARNING] = LOG_WARNING,
43 [L_NOTICE] = LOG_NOTICE,
49 #define use_syslog false
51 static bool use_syslog = true;
56 netifd_delete_process(struct netifd_process *proc)
58 if (proc->uloop.pending)
59 uloop_process_delete(&proc->uloop);
60 list_del(&proc->list);
61 netifd_fd_delete(&proc->log_fd);
69 netifd_log_message(int priority, const char *format, ...)
73 if (priority > log_level)
78 vsyslog(log_class[priority], format, vl);
80 vfprintf(stderr, format, vl);
85 netifd_process_log_cb(struct uloop_fd *fd, unsigned int events)
87 struct netifd_process *proc;
88 const char *log_prefix;
90 int maxlen, len, read_len;
92 proc = container_of(fd, struct netifd_process, log_uloop);
95 proc->log_buf = malloc(LOG_BUF_SIZE + 1);
97 log_prefix = proc->log_prefix;
99 log_prefix = "process";
102 buf = proc->log_buf + proc->log_buf_ofs;
103 maxlen = LOG_BUF_SIZE - proc->log_buf_ofs;
104 read_len = len = read(fd->fd, buf, maxlen);
113 proc->log_buf_ofs += len;
115 len = proc->log_buf_ofs;
118 cur = memchr(buf, '\n', len);
124 if (!proc->log_overflow)
125 netifd_log_message(L_NOTICE, "%s (%d): %s\n",
126 log_prefix, proc->uloop.pid, buf);
128 proc->log_overflow = false;
135 if (buf > proc->log_buf && len > 0)
136 memmove(proc->log_buf, buf, len);
138 if (len == LOG_BUF_SIZE) {
139 if (!proc->log_overflow) {
140 proc->log_buf[LOG_BUF_SIZE] = 0;
141 netifd_log_message(L_NOTICE, "%s (%d): %s [...]\n",
142 log_prefix, proc->uloop.pid, proc->log_buf);
143 proc->log_overflow = true;
147 proc->log_buf_ofs = len;
158 netifd_process_cb(struct uloop_process *proc, int ret)
160 struct netifd_process *np;
161 np = container_of(proc, struct netifd_process, uloop);
162 netifd_process_log_cb(&np->log_uloop, 0);
163 netifd_delete_process(np);
164 return np->cb(np, ret);
168 netifd_start_process(const char **argv, char **env, struct netifd_process *proc)
170 struct netifd_fd *fd;
174 netifd_kill_process(proc);
179 if ((pid = fork()) < 0)
189 if (proc->dir_fd >= 0)
190 fchdir(proc->dir_fd);
192 /* close all non-essential fds */
193 list_for_each_entry(fd, &fds, list) {
194 if (fd->proc == proc)
206 execvp(argv[0], (char **) argv);
214 proc->uloop.cb = netifd_process_cb;
215 proc->uloop.pid = pid;
216 uloop_process_add(&proc->uloop);
217 list_add_tail(&proc->list, &process_list);
219 proc->log_buf_ofs = 0;
220 proc->log_uloop.fd = proc->log_fd.fd = pfds[0];
221 proc->log_uloop.cb = netifd_process_log_cb;
222 netifd_fd_add(&proc->log_fd);
223 uloop_fd_add(&proc->log_uloop, ULOOP_EDGE_TRIGGER | ULOOP_READ);
234 netifd_kill_process(struct netifd_process *proc)
236 if (!proc->uloop.pending)
239 kill(proc->uloop.pid, SIGKILL);
240 netifd_delete_process(proc);
244 netifd_fd_add(struct netifd_fd *fd)
246 list_add_tail(&fd->list, &fds);
250 netifd_fd_delete(struct netifd_fd *fd)
255 static void netifd_do_restart(struct uloop_timeout *timeout)
257 execvp(global_argv[0], global_argv);
260 static void netifd_do_reload(struct uloop_timeout *timeout)
265 static struct uloop_timeout main_timer;
267 void netifd_reload(void)
269 main_timer.cb = netifd_do_reload;
270 uloop_timeout_set(&main_timer, 100);
273 void netifd_restart(void)
275 main_timer.cb = netifd_do_restart;
276 interface_set_down(NULL);
277 uloop_timeout_set(&main_timer, 1000);
280 static int usage(const char *progname)
282 fprintf(stderr, "Usage: %s [options]\n"
284 " -d <mask>: Mask for debug messages\n"
285 " -s <path>: Path to the ubus socket\n"
286 " -p <path>: Path to netifd addons (default: %s)\n"
287 " -h <path>: Path to the hotplug script\n"
288 " -r <path>: Path to resolv.conf\n"
289 " -l <level>: Log output level (default: %d)\n"
290 " -S: Use stderr instead of syslog for log messages\n"
291 " (default: "DEFAULT_HOTPLUG_PATH")\n"
292 "\n", progname, main_path, DEFAULT_LOG_LEVEL);
298 netifd_handle_signal(int signo)
304 netifd_setup_signals(void)
308 memset(&s, 0, sizeof(s));
309 s.sa_handler = netifd_handle_signal;
311 sigaction(SIGINT, &s, NULL);
312 sigaction(SIGTERM, &s, NULL);
313 sigaction(SIGUSR1, &s, NULL);
314 sigaction(SIGUSR2, &s, NULL);
316 s.sa_handler = SIG_IGN;
317 sigaction(SIGPIPE, &s, NULL);
321 netifd_kill_processes(void)
323 struct netifd_process *proc, *tmp;
325 list_for_each_entry_safe(proc, tmp, &process_list, list)
326 netifd_kill_process(proc);
329 int main(int argc, char **argv)
331 const char *socket = NULL;
336 while ((ch = getopt(argc, argv, "d:s:p:h:r:l:S")) != -1) {
339 debug_mask = strtoul(optarg, NULL, 0);
348 hotplug_cmd_path = optarg;
351 resolv_conf = optarg;
354 log_level = atoi(optarg);
355 if (log_level >= ARRAY_SIZE(log_class))
356 log_level = ARRAY_SIZE(log_class) - 1;
364 return usage(argv[0]);
369 openlog("netifd", 0, LOG_DAEMON);
371 netifd_setup_signals();
372 if (netifd_ubus_init(socket) < 0) {
373 fprintf(stderr, "Failed to connect to ubus\n");
378 fprintf(stderr, "Failed to initialize system control\n");
385 netifd_kill_processes();