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.
28 #include "interface.h"
33 unsigned int debug_mask
= 0;
34 const char *main_path
= DEFAULT_MAIN_PATH
;
35 const char *config_path
= DEFAULT_CONFIG_PATH
;
36 const char *resolv_conf
= DEFAULT_RESOLV_CONF
;
37 static char **global_argv
;
39 static struct list_head process_list
= LIST_HEAD_INIT(process_list
);
40 static struct udebug ud
;
41 static struct udebug_buf udb
;
42 static bool udebug_enabled
;
44 #define DEFAULT_LOG_LEVEL L_NOTICE
46 static int log_level
= DEFAULT_LOG_LEVEL
;
47 static const int log_class
[] = {
49 [L_WARNING
] = LOG_WARNING
,
50 [L_NOTICE
] = LOG_NOTICE
,
56 #define use_syslog false
58 static bool use_syslog
= true;
63 netifd_delete_process(struct netifd_process
*proc
)
65 while (ustream_poll(&proc
->log
.stream
));
66 list_del(&proc
->list
);
67 ustream_free(&proc
->log
.stream
);
68 close(proc
->log
.fd
.fd
);
72 netifd_udebug_vprintf(const char *format
, va_list ap
)
77 udebug_entry_init(&udb
);
78 udebug_entry_vprintf(&udb
, format
, ap
);
79 udebug_entry_add(&udb
);
82 void netifd_udebug_printf(const char *format
, ...)
87 netifd_udebug_vprintf(format
, ap
);
91 void netifd_udebug_set_enabled(bool val
)
93 static const struct udebug_buf_meta meta
= {
95 .format
= UDEBUG_FORMAT_STRING
,
98 if (udebug_enabled
== val
)
101 udebug_enabled
= val
;
103 udebug_buf_free(&udb
);
109 udebug_auto_connect(&ud
, NULL
);
110 udebug_buf_init(&udb
, 1024, 64 * 1024);
111 udebug_buf_add(&ud
, &udb
, &meta
);
115 __attribute__((format(printf
, 2, 0)))
116 netifd_log_message(int priority
, const char *format
, ...)
120 if (priority
> log_level
)
123 va_start(vl
, format
);
124 netifd_udebug_vprintf(format
, vl
);
126 vsyslog(log_class
[priority
], format
, vl
);
128 vfprintf(stderr
, format
, vl
);
133 netifd_process_log_read_cb(struct ustream
*s
, int bytes
)
135 struct netifd_process
*proc
;
136 const char *log_prefix
;
140 proc
= container_of(s
, struct netifd_process
, log
.stream
);
141 log_prefix
= proc
->log_prefix
;
143 log_prefix
= "process";
148 data
= ustream_get_read_buf(s
, &len
);
152 newline
= strchr(data
, '\n');
154 if (proc
->log_overflow
) {
156 len
= newline
+ 1 - data
;
157 proc
->log_overflow
= false;
159 } else if (newline
) {
161 len
= newline
+ 1 - data
;
162 netifd_log_message(L_NOTICE
, "%s (%d): %s\n",
163 log_prefix
, proc
->uloop
.pid
, data
);
164 } else if (len
== s
->r
.buffer_len
) {
165 netifd_log_message(L_NOTICE
, "%s (%d): %s [...]\n",
166 log_prefix
, proc
->uloop
.pid
, data
);
167 proc
->log_overflow
= true;
171 ustream_consume(s
, len
);
176 netifd_process_cb(struct uloop_process
*proc
, int ret
)
178 struct netifd_process
*np
;
179 np
= container_of(proc
, struct netifd_process
, uloop
);
181 netifd_delete_process(np
);
187 netifd_start_process(const char **argv
, char **env
, struct netifd_process
*proc
)
192 netifd_kill_process(proc
);
197 if ((pid
= fork()) < 0)
209 if (proc
->dir_fd
>= 0)
210 if (fchdir(proc
->dir_fd
)) {}
214 for (i
= 0; i
<= 2; i
++) {
224 execvp(argv
[0], (char **) argv
);
229 proc
->uloop
.cb
= netifd_process_cb
;
230 proc
->uloop
.pid
= pid
;
231 uloop_process_add(&proc
->uloop
);
232 list_add_tail(&proc
->list
, &process_list
);
234 system_fd_set_cloexec(pfds
[0]);
235 proc
->log
.stream
.string_data
= true;
236 proc
->log
.stream
.notify_read
= netifd_process_log_read_cb
;
237 ustream_fd_init(&proc
->log
, pfds
[0]);
248 netifd_kill_process(struct netifd_process
*proc
)
250 if (!proc
->uloop
.pending
)
253 kill(proc
->uloop
.pid
, SIGKILL
);
254 uloop_process_delete(&proc
->uloop
);
255 netifd_delete_process(proc
);
258 static void netifd_do_restart(struct uloop_timeout
*timeout
)
260 execvp(global_argv
[0], global_argv
);
263 int netifd_reload(void)
265 return config_init_all();
268 void netifd_restart(void)
270 static struct uloop_timeout main_timer
= {
271 .cb
= netifd_do_restart
274 interface_set_down(NULL
);
275 uloop_timeout_set(&main_timer
, 1000);
278 static int usage(const char *progname
)
280 fprintf(stderr
, "Usage: %s [options]\n"
282 " -d <mask>: Mask for debug messages\n"
283 " -s <path>: Path to the ubus socket\n"
284 " -p <path>: Path to netifd addons (default: %s)\n"
285 " -c <path>: Path to UCI configuration\n"
286 " -h <path>: Path to the hotplug script\n"
287 " (default: "DEFAULT_HOTPLUG_PATH
")\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 "\n", progname
, main_path
, DEFAULT_LOG_LEVEL
);
297 netifd_handle_signal(int signo
)
303 netifd_setup_signals(void)
307 memset(&s
, 0, sizeof(s
));
308 s
.sa_handler
= netifd_handle_signal
;
310 sigaction(SIGINT
, &s
, NULL
);
311 sigaction(SIGTERM
, &s
, NULL
);
312 sigaction(SIGUSR1
, &s
, NULL
);
313 sigaction(SIGUSR2
, &s
, NULL
);
315 s
.sa_handler
= SIG_IGN
;
316 sigaction(SIGPIPE
, &s
, NULL
);
320 netifd_kill_processes(void)
322 struct netifd_process
*proc
, *tmp
;
324 list_for_each_entry_safe(proc
, tmp
, &process_list
, list
)
325 netifd_kill_process(proc
);
328 int main(int argc
, char **argv
)
330 const char *socket
= NULL
;
335 while ((ch
= getopt(argc
, argv
, "d:s:p:c:h:r:l:S")) != -1) {
338 debug_mask
= strtoul(optarg
, NULL
, 0);
347 config_path
= optarg
;
350 hotplug_cmd_path
= optarg
;
353 resolv_conf
= optarg
;
356 log_level
= atoi(optarg
);
357 if (log_level
>= (int)ARRAY_SIZE(log_class
))
358 log_level
= (int)ARRAY_SIZE(log_class
) - 1;
366 return usage(argv
[0]);
371 openlog("netifd", 0, LOG_DAEMON
);
373 netifd_setup_signals();
374 if (netifd_ubus_init(socket
) < 0) {
375 fprintf(stderr
, "Failed to connect to ubus\n");
384 fprintf(stderr
, "Failed to initialize system control\n");
391 netifd_kill_processes();