14 #include "interface.h"
16 unsigned int debug_mask = 0;
17 const char *main_path = DEFAULT_MAIN_PATH;
18 const char *resolv_conf = DEFAULT_RESOLV_CONF;
19 static char **global_argv;
21 static struct list_head process_list = LIST_HEAD_INIT(process_list);
22 static struct list_head fds = LIST_HEAD_INIT(fds);
24 #define DEFAULT_LOG_LEVEL L_NOTICE
26 static int log_level = DEFAULT_LOG_LEVEL;
27 static const int log_class[] = {
29 [L_WARNING] = LOG_WARNING,
30 [L_NOTICE] = LOG_NOTICE,
36 #define use_syslog false
38 static bool use_syslog = true;
43 netifd_delete_process(struct netifd_process *proc)
45 if (proc->uloop.pending)
46 uloop_process_delete(&proc->uloop);
47 list_del(&proc->list);
48 netifd_fd_delete(&proc->log_fd);
56 netifd_log_message(int priority, const char *format, ...)
60 if (priority > log_level)
65 vsyslog(log_class[priority], format, vl);
67 vfprintf(stderr, format, vl);
72 netifd_process_log_cb(struct uloop_fd *fd, unsigned int events)
74 struct netifd_process *proc;
75 const char *log_prefix;
77 int maxlen, len, read_len;
79 proc = container_of(fd, struct netifd_process, log_uloop);
82 proc->log_buf = malloc(LOG_BUF_SIZE + 1);
84 log_prefix = proc->log_prefix;
86 log_prefix = "process";
89 buf = proc->log_buf + proc->log_buf_ofs;
90 maxlen = LOG_BUF_SIZE - proc->log_buf_ofs;
91 read_len = len = read(fd->fd, buf, maxlen);
100 proc->log_buf_ofs += len;
104 while (len > 0 && (cur = memchr(cur, '\n', len))) {
107 if (!proc->log_overflow)
108 netifd_log_message(L_NOTICE, "%s (%d): %s\n",
109 log_prefix, proc->uloop.pid, buf);
111 proc->log_overflow = false;
118 if (buf > proc->log_buf && len > 0)
119 memmove(buf, proc->log_buf, len);
121 if (len == LOG_BUF_SIZE) {
122 if (!proc->log_overflow) {
123 proc->log_buf[LOG_BUF_SIZE] = 0;
124 netifd_log_message(L_NOTICE, "%s (%d): %s [...]\n",
125 log_prefix, proc->uloop.pid, proc->log_buf);
126 proc->log_overflow = true;
130 proc->log_buf_ofs = len;
141 netifd_process_cb(struct uloop_process *proc, int ret)
143 struct netifd_process *np;
144 np = container_of(proc, struct netifd_process, uloop);
145 netifd_process_log_cb(&np->log_uloop, 0);
146 netifd_delete_process(np);
147 return np->cb(np, ret);
151 netifd_start_process(const char **argv, char **env, struct netifd_process *proc)
153 struct netifd_fd *fd;
157 netifd_kill_process(proc);
162 if ((pid = fork()) < 0)
172 if (proc->dir_fd >= 0)
173 fchdir(proc->dir_fd);
175 /* close all non-essential fds */
176 list_for_each_entry(fd, &fds, list) {
177 if (fd->proc == proc)
189 execvp(argv[0], (char **) argv);
197 proc->uloop.cb = netifd_process_cb;
198 proc->uloop.pid = pid;
199 uloop_process_add(&proc->uloop);
200 list_add_tail(&proc->list, &process_list);
202 proc->log_buf_ofs = 0;
203 proc->log_uloop.fd = proc->log_fd.fd = pfds[0];
204 proc->log_uloop.cb = netifd_process_log_cb;
205 netifd_fd_add(&proc->log_fd);
206 uloop_fd_add(&proc->log_uloop, ULOOP_EDGE_TRIGGER | ULOOP_READ);
217 netifd_kill_process(struct netifd_process *proc)
219 if (!proc->uloop.pending)
222 kill(proc->uloop.pid, SIGKILL);
223 netifd_delete_process(proc);
227 netifd_fd_add(struct netifd_fd *fd)
229 list_add_tail(&fd->list, &fds);
233 netifd_fd_delete(struct netifd_fd *fd)
238 static void netifd_do_restart(struct uloop_timeout *timeout)
240 execvp(global_argv[0], global_argv);
243 static void netifd_do_reload(struct uloop_timeout *timeout)
248 static struct uloop_timeout main_timer;
250 void netifd_reload(void)
252 main_timer.cb = netifd_do_reload;
253 uloop_timeout_set(&main_timer, 100);
256 void netifd_restart(void)
258 main_timer.cb = netifd_do_restart;
259 interface_set_down(NULL);
260 uloop_timeout_set(&main_timer, 1000);
263 static int usage(const char *progname)
265 fprintf(stderr, "Usage: %s [options]\n"
267 " -d <mask>: Mask for debug messages\n"
268 " -s <path>: Path to the ubus socket\n"
269 " -p <path>: Path to netifd addons (default: %s)\n"
270 " -h <path>: Path to the hotplug script\n"
271 " -r <path>: Path to resolv.conf\n"
272 " -l <level>: Log output level (default: %d)\n"
273 " -S: Use stderr instead of syslog for log messages\n"
274 " (default: "DEFAULT_HOTPLUG_PATH")\n"
275 "\n", progname, main_path, DEFAULT_LOG_LEVEL);
281 netifd_handle_signal(int signo)
287 netifd_setup_signals(void)
291 memset(&s, 0, sizeof(s));
292 s.sa_handler = netifd_handle_signal;
294 sigaction(SIGINT, &s, NULL);
295 sigaction(SIGTERM, &s, NULL);
296 sigaction(SIGUSR1, &s, NULL);
297 sigaction(SIGUSR2, &s, NULL);
299 s.sa_handler = SIG_IGN;
300 sigaction(SIGPIPE, &s, NULL);
304 netifd_kill_processes(void)
306 struct netifd_process *proc, *tmp;
308 list_for_each_entry_safe(proc, tmp, &process_list, list)
309 netifd_kill_process(proc);
312 int main(int argc, char **argv)
314 const char *socket = NULL;
319 while ((ch = getopt(argc, argv, "d:s:p:h:r:l:S")) != -1) {
322 debug_mask = strtoul(optarg, NULL, 0);
331 hotplug_cmd_path = optarg;
334 resolv_conf = optarg;
337 log_level = atoi(optarg);
338 if (log_level >= ARRAY_SIZE(log_class))
339 log_level = ARRAY_SIZE(log_class) - 1;
347 return usage(argv[0]);
352 openlog("netifd", 0, LOG_DAEMON);
354 netifd_setup_signals();
355 if (netifd_ubus_init(socket) < 0) {
356 fprintf(stderr, "Failed to connect to ubus\n");
361 fprintf(stderr, "Failed to initialize system control\n");
368 netifd_kill_processes();