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);
52 netifd_log_message(int priority, const char *format, ...)
56 if (priority > log_level)
61 vsyslog(log_class[priority], format, vl);
63 vfprintf(stderr, format, vl);
68 netifd_process_log_cb(struct uloop_fd *fd, unsigned int events)
70 struct netifd_process *proc;
71 const char *log_prefix;
73 int maxlen, len, read_len;
75 proc = container_of(fd, struct netifd_process, log_uloop);
78 proc->log_buf = malloc(LOG_BUF_SIZE + 1);
80 buf = proc->log_buf + proc->log_buf_ofs;
81 maxlen = LOG_BUF_SIZE - proc->log_buf_ofs;
83 log_prefix = proc->log_prefix;
85 log_prefix = "process";
88 read_len = len = read(fd->fd, buf, maxlen);
97 proc->log_buf_ofs += len;
101 while ((cur = memchr(cur, '\n', len))) {
104 if (!proc->log_overflow)
105 netifd_log_message(L_NOTICE, "%s (%d): %s\n",
106 log_prefix, proc->uloop.pid, buf);
108 proc->log_overflow = false;
115 if (buf > proc->log_buf && len > 0)
116 memmove(buf, proc->log_buf, len);
118 if (len == LOG_BUF_SIZE) {
119 if (!proc->log_overflow) {
120 proc->log_buf[LOG_BUF_SIZE] = 0;
121 netifd_log_message(L_NOTICE, "%s (%d): %s [...]\n",
122 log_prefix, proc->uloop.pid, proc->log_buf);
123 proc->log_overflow = true;
127 proc->log_buf_ofs = len;
129 if (read_len == maxlen)
138 netifd_process_cb(struct uloop_process *proc, int ret)
140 struct netifd_process *np;
141 np = container_of(proc, struct netifd_process, uloop);
142 netifd_process_log_cb(&np->log_uloop, 0);
143 netifd_delete_process(np);
144 return np->cb(np, ret);
148 netifd_start_process(const char **argv, char **env, struct netifd_process *proc)
150 struct netifd_fd *fd;
154 netifd_kill_process(proc);
159 if ((pid = fork()) < 0)
169 if (proc->dir_fd >= 0)
170 fchdir(proc->dir_fd);
172 /* close all non-essential fds */
173 list_for_each_entry(fd, &fds, list) {
174 if (fd->proc == proc)
186 execvp(argv[0], (char **) argv);
194 proc->uloop.cb = netifd_process_cb;
195 proc->uloop.pid = pid;
196 uloop_process_add(&proc->uloop);
197 list_add_tail(&proc->list, &process_list);
199 proc->log_uloop.fd = proc->log_fd.fd = pfds[0];
200 proc->log_uloop.cb = netifd_process_log_cb;
201 netifd_fd_add(&proc->log_fd);
202 uloop_fd_add(&proc->log_uloop, ULOOP_EDGE_TRIGGER | ULOOP_READ);
213 netifd_kill_process(struct netifd_process *proc)
215 if (!proc->uloop.pending)
218 kill(proc->uloop.pid, SIGTERM);
219 netifd_delete_process(proc);
223 netifd_fd_add(struct netifd_fd *fd)
225 list_add_tail(&fd->list, &fds);
229 netifd_fd_delete(struct netifd_fd *fd)
234 static void netifd_do_restart(struct uloop_timeout *timeout)
236 execvp(global_argv[0], global_argv);
239 static void netifd_do_reload(struct uloop_timeout *timeout)
241 config_init_interfaces(NULL);
244 static struct uloop_timeout main_timer;
246 void netifd_reload(void)
248 main_timer.cb = netifd_do_reload;
249 uloop_timeout_set(&main_timer, 100);
252 void netifd_restart(void)
254 main_timer.cb = netifd_do_restart;
255 interface_set_down(NULL);
256 uloop_timeout_set(&main_timer, 1000);
259 static int usage(const char *progname)
261 fprintf(stderr, "Usage: %s [options]\n"
263 " -d <mask>: Mask for debug messages\n"
264 " -s <path>: Path to the ubus socket\n"
265 " -p <path>: Path to netifd addons (default: %s)\n"
266 " -h <path>: Path to the hotplug script\n"
267 " -r <path>: Path to resolv.conf\n"
268 " -l <level>: Log output level (default: %d)\n"
269 " -S: Use stderr instead of syslog for log messages\n"
270 " (default: "DEFAULT_HOTPLUG_PATH")\n"
271 "\n", progname, main_path, DEFAULT_LOG_LEVEL);
277 netifd_handle_signal(int signo)
283 netifd_setup_signals(void)
287 memset(&s, 0, sizeof(s));
288 s.sa_handler = netifd_handle_signal;
290 sigaction(SIGINT, &s, NULL);
291 sigaction(SIGTERM, &s, NULL);
292 sigaction(SIGUSR1, &s, NULL);
293 sigaction(SIGUSR2, &s, NULL);
295 s.sa_handler = SIG_IGN;
296 sigaction(SIGPIPE, &s, NULL);
300 netifd_kill_processes(void)
302 struct netifd_process *proc, *tmp;
304 list_for_each_entry_safe(proc, tmp, &process_list, list)
305 netifd_kill_process(proc);
308 int main(int argc, char **argv)
310 const char *socket = NULL;
315 while ((ch = getopt(argc, argv, "d:s:p:h:r:l:S")) != -1) {
318 debug_mask = strtoul(optarg, NULL, 0);
327 hotplug_cmd_path = optarg;
330 resolv_conf = optarg;
333 log_level = atoi(optarg);
334 if (log_level >= ARRAY_SIZE(log_class))
335 log_level = ARRAY_SIZE(log_class) - 1;
343 return usage(argv[0]);
348 openlog("netifd", 0, LOG_DAEMON);
350 netifd_setup_signals();
351 if (netifd_ubus_init(socket) < 0) {
352 fprintf(stderr, "Failed to connect to ubus\n");
357 fprintf(stderr, "Failed to initialize system control\n");
361 config_init_interfaces(NULL);
364 netifd_kill_processes();