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
34 static int log_level = DEFAULT_LOG_LEVEL;
35 static const int log_class[] = {
37 [L_WARNING] = LOG_WARNING,
38 [L_NOTICE] = LOG_NOTICE,
44 #define use_syslog false
46 static bool use_syslog = true;
51 netifd_delete_process(struct netifd_process *proc)
53 if (proc->uloop.pending)
54 uloop_process_delete(&proc->uloop);
55 list_del(&proc->list);
56 netifd_fd_delete(&proc->log_fd);
60 netifd_log_message(int priority, const char *format, ...)
64 if (priority > log_level)
69 vsyslog(log_class[priority], format, vl);
71 vfprintf(stderr, format, vl);
76 netifd_process_log_cb(struct uloop_fd *fd, unsigned int events)
78 struct netifd_process *proc;
79 const char *log_prefix;
81 int maxlen, len, read_len;
83 proc = container_of(fd, struct netifd_process, log_uloop);
86 proc->log_buf = malloc(LOG_BUF_SIZE + 1);
88 buf = proc->log_buf + proc->log_buf_ofs;
89 maxlen = LOG_BUF_SIZE - proc->log_buf_ofs;
91 log_prefix = proc->log_prefix;
93 log_prefix = "process";
96 read_len = len = read(fd->fd, buf, maxlen);
103 proc->log_buf_ofs += len;
107 while ((cur = memchr(cur, '\n', len))) {
110 if (!proc->log_overflow)
111 netifd_log_message(L_NOTICE, "%s (%d): %s\n",
112 log_prefix, proc->uloop.pid, buf);
114 proc->log_overflow = false;
121 if (buf > proc->log_buf && len > 0)
122 memmove(buf, proc->log_buf, len);
124 if (len == LOG_BUF_SIZE) {
125 if (!proc->log_overflow) {
126 proc->log_buf[LOG_BUF_SIZE] = 0;
127 netifd_log_message(L_NOTICE, "%s (%d): %s [...]\n",
128 log_prefix, proc->uloop.pid, proc->log_buf);
129 proc->log_overflow = true;
133 proc->log_buf_ofs = len;
135 if (read_len == maxlen)
144 netifd_process_cb(struct uloop_process *proc, int ret)
146 struct netifd_process *np;
147 np = container_of(proc, struct netifd_process, uloop);
148 netifd_process_log_cb(&np->log_uloop, 0);
149 netifd_delete_process(np);
150 return np->cb(np, ret);
154 netifd_start_process(const char **argv, char **env, struct netifd_process *proc)
156 struct netifd_fd *fd;
160 netifd_kill_process(proc);
165 if ((pid = fork()) < 0)
175 if (proc->dir_fd >= 0)
176 fchdir(proc->dir_fd);
178 /* close all non-essential fds */
179 list_for_each_entry(fd, &fds, list) {
180 if (fd->proc == proc)
192 execvp(argv[0], (char **) argv);
200 proc->uloop.cb = netifd_process_cb;
201 proc->uloop.pid = pid;
202 uloop_process_add(&proc->uloop);
203 list_add_tail(&proc->list, &process_list);
205 proc->log_uloop.fd = proc->log_fd.fd = pfds[0];
206 proc->log_uloop.cb = netifd_process_log_cb;
207 netifd_fd_add(&proc->log_fd);
208 uloop_fd_add(&proc->log_uloop, ULOOP_EDGE_TRIGGER | ULOOP_READ);
219 netifd_kill_process(struct netifd_process *proc)
221 if (!proc->uloop.pending)
224 kill(proc->uloop.pid, SIGTERM);
225 netifd_delete_process(proc);
229 netifd_fd_add(struct netifd_fd *fd)
231 list_add_tail(&fd->list, &fds);
235 netifd_fd_delete(struct netifd_fd *fd)
240 static void netifd_do_restart(struct uloop_timeout *timeout)
242 execvp(global_argv[0], global_argv);
245 static void netifd_do_reload(struct uloop_timeout *timeout)
247 config_init_interfaces(NULL);
250 static struct uloop_timeout main_timer;
252 void netifd_reload(void)
254 main_timer.cb = netifd_do_reload;
255 uloop_timeout_set(&main_timer, 100);
258 void netifd_restart(void)
260 main_timer.cb = netifd_do_restart;
261 interface_set_down(NULL);
262 uloop_timeout_set(&main_timer, 1000);
265 static int usage(const char *progname)
267 fprintf(stderr, "Usage: %s [options]\n"
269 " -d <mask>: Mask for debug messages\n"
270 " -s <path>: Path to the ubus socket\n"
271 " -p <path>: Path to netifd addons (default: %s)\n"
272 " -h <path>: Path to the hotplug script\n"
273 " -r <path>: Path to resolv.conf\n"
274 " -l <level>: Log output level (default: %d)\n"
275 " -S: Use stderr instead of syslog for log messages\n"
276 " (default: "DEFAULT_HOTPLUG_PATH")\n"
277 "\n", progname, main_path, DEFAULT_LOG_LEVEL);
283 netifd_handle_signal(int signo)
289 netifd_setup_signals(void)
293 memset(&s, 0, sizeof(s));
294 s.sa_handler = netifd_handle_signal;
296 sigaction(SIGINT, &s, NULL);
297 sigaction(SIGTERM, &s, NULL);
298 sigaction(SIGUSR1, &s, NULL);
299 sigaction(SIGUSR2, &s, NULL);
301 s.sa_handler = SIG_IGN;
302 sigaction(SIGPIPE, &s, NULL);
306 netifd_kill_processes(void)
308 struct netifd_process *proc, *tmp;
310 list_for_each_entry_safe(proc, tmp, &process_list, list)
311 netifd_kill_process(proc);
314 int main(int argc, char **argv)
316 const char *socket = NULL;
321 while ((ch = getopt(argc, argv, "d:s:p:h:r:l:S")) != -1) {
324 debug_mask = strtoul(optarg, NULL, 0);
333 hotplug_cmd_path = optarg;
336 resolv_conf = optarg;
339 log_level = atoi(optarg);
340 if (log_level >= ARRAY_SIZE(log_class))
341 log_level = ARRAY_SIZE(log_class) - 1;
349 return usage(argv[0]);
354 openlog("netifd", 0, LOG_DAEMON);
356 netifd_setup_signals();
357 if (netifd_ubus_init(socket) < 0) {
358 fprintf(stderr, "Failed to connect to ubus\n");
363 fprintf(stderr, "Failed to initialize system control\n");
367 config_init_interfaces(NULL);
370 netifd_kill_processes();