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
);
62 close(proc
->log_fd
.fd
);
70 netifd_log_message(int priority
, const char *format
, ...)
74 if (priority
> log_level
)
79 vsyslog(log_class
[priority
], format
, vl
);
81 vfprintf(stderr
, format
, vl
);
86 netifd_process_log_cb(struct uloop_fd
*fd
, unsigned int events
)
88 struct netifd_process
*proc
;
89 const char *log_prefix
;
91 int maxlen
, len
, read_len
;
93 proc
= container_of(fd
, struct netifd_process
, log_uloop
);
96 proc
->log_buf
= malloc(LOG_BUF_SIZE
+ 1);
98 log_prefix
= proc
->log_prefix
;
100 log_prefix
= "process";
103 buf
= proc
->log_buf
+ proc
->log_buf_ofs
;
104 maxlen
= LOG_BUF_SIZE
- proc
->log_buf_ofs
;
105 read_len
= len
= read(fd
->fd
, buf
, maxlen
);
114 proc
->log_buf_ofs
+= len
;
116 len
= proc
->log_buf_ofs
;
119 cur
= memchr(buf
, '\n', len
);
125 if (!proc
->log_overflow
)
126 netifd_log_message(L_NOTICE
, "%s (%d): %s\n",
127 log_prefix
, proc
->uloop
.pid
, buf
);
129 proc
->log_overflow
= false;
136 if (buf
> proc
->log_buf
&& len
> 0)
137 memmove(proc
->log_buf
, buf
, len
);
139 if (len
== LOG_BUF_SIZE
) {
140 if (!proc
->log_overflow
) {
141 proc
->log_buf
[LOG_BUF_SIZE
] = 0;
142 netifd_log_message(L_NOTICE
, "%s (%d): %s [...]\n",
143 log_prefix
, proc
->uloop
.pid
, proc
->log_buf
);
144 proc
->log_overflow
= true;
148 proc
->log_buf_ofs
= len
;
159 netifd_process_cb(struct uloop_process
*proc
, int ret
)
161 struct netifd_process
*np
;
162 np
= container_of(proc
, struct netifd_process
, uloop
);
163 netifd_process_log_cb(&np
->log_uloop
, 0);
164 netifd_delete_process(np
);
165 return np
->cb(np
, ret
);
169 netifd_start_process(const char **argv
, char **env
, struct netifd_process
*proc
)
171 struct netifd_fd
*fd
;
175 netifd_kill_process(proc
);
180 if ((pid
= fork()) < 0)
190 if (proc
->dir_fd
>= 0)
191 fchdir(proc
->dir_fd
);
193 /* close all non-essential fds */
194 list_for_each_entry(fd
, &fds
, list
) {
195 if (fd
->proc
== proc
)
207 execvp(argv
[0], (char **) argv
);
215 proc
->uloop
.cb
= netifd_process_cb
;
216 proc
->uloop
.pid
= pid
;
217 uloop_process_add(&proc
->uloop
);
218 list_add_tail(&proc
->list
, &process_list
);
220 proc
->log_buf_ofs
= 0;
221 proc
->log_uloop
.fd
= proc
->log_fd
.fd
= pfds
[0];
222 proc
->log_uloop
.cb
= netifd_process_log_cb
;
223 netifd_fd_add(&proc
->log_fd
);
224 uloop_fd_add(&proc
->log_uloop
, ULOOP_EDGE_TRIGGER
| ULOOP_READ
);
235 netifd_kill_process(struct netifd_process
*proc
)
237 if (!proc
->uloop
.pending
)
240 kill(proc
->uloop
.pid
, SIGKILL
);
241 netifd_delete_process(proc
);
245 netifd_fd_add(struct netifd_fd
*fd
)
247 list_add_tail(&fd
->list
, &fds
);
251 netifd_fd_delete(struct netifd_fd
*fd
)
256 static void netifd_do_restart(struct uloop_timeout
*timeout
)
258 execvp(global_argv
[0], global_argv
);
261 static void netifd_do_reload(struct uloop_timeout
*timeout
)
266 static struct uloop_timeout main_timer
;
268 void netifd_reload(void)
270 main_timer
.cb
= netifd_do_reload
;
271 uloop_timeout_set(&main_timer
, 100);
274 void netifd_restart(void)
276 main_timer
.cb
= netifd_do_restart
;
277 interface_set_down(NULL
);
278 uloop_timeout_set(&main_timer
, 1000);
281 static int usage(const char *progname
)
283 fprintf(stderr
, "Usage: %s [options]\n"
285 " -d <mask>: Mask for debug messages\n"
286 " -s <path>: Path to the ubus socket\n"
287 " -p <path>: Path to netifd addons (default: %s)\n"
288 " -h <path>: Path to the hotplug script\n"
289 " -r <path>: Path to resolv.conf\n"
290 " -l <level>: Log output level (default: %d)\n"
291 " -S: Use stderr instead of syslog for log messages\n"
292 " (default: "DEFAULT_HOTPLUG_PATH
")\n"
293 "\n", progname
, main_path
, DEFAULT_LOG_LEVEL
);
299 netifd_handle_signal(int signo
)
305 netifd_setup_signals(void)
309 memset(&s
, 0, sizeof(s
));
310 s
.sa_handler
= netifd_handle_signal
;
312 sigaction(SIGINT
, &s
, NULL
);
313 sigaction(SIGTERM
, &s
, NULL
);
314 sigaction(SIGUSR1
, &s
, NULL
);
315 sigaction(SIGUSR2
, &s
, NULL
);
317 s
.sa_handler
= SIG_IGN
;
318 sigaction(SIGPIPE
, &s
, NULL
);
322 netifd_kill_processes(void)
324 struct netifd_process
*proc
, *tmp
;
326 list_for_each_entry_safe(proc
, tmp
, &process_list
, list
)
327 netifd_kill_process(proc
);
330 int main(int argc
, char **argv
)
332 const char *socket
= NULL
;
337 while ((ch
= getopt(argc
, argv
, "d:s:p:h:r:l:S")) != -1) {
340 debug_mask
= strtoul(optarg
, NULL
, 0);
349 hotplug_cmd_path
= optarg
;
352 resolv_conf
= optarg
;
355 log_level
= atoi(optarg
);
356 if (log_level
>= ARRAY_SIZE(log_class
))
357 log_level
= ARRAY_SIZE(log_class
) - 1;
365 return usage(argv
[0]);
370 openlog("netifd", 0, LOG_DAEMON
);
372 netifd_setup_signals();
373 if (netifd_ubus_init(socket
) < 0) {
374 fprintf(stderr
, "Failed to connect to ubus\n");
379 fprintf(stderr
, "Failed to initialize system control\n");
386 netifd_kill_processes();