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.
26 #include "interface.h"
31 unsigned int debug_mask
= 0;
32 const char *main_path
= DEFAULT_MAIN_PATH
;
33 const char *config_path
= DEFAULT_CONFIG_PATH
;
34 const char *resolv_conf
= DEFAULT_RESOLV_CONF
;
35 static char **global_argv
;
37 static struct list_head process_list
= LIST_HEAD_INIT(process_list
);
38 static struct udebug ud
;
39 static struct udebug_buf udb_log
;
40 struct udebug_buf udb_nl
;
41 static const struct udebug_buf_meta meta_log
= {
43 .format
= UDEBUG_FORMAT_STRING
,
45 static const struct udebug_buf_meta meta_nl
= {
47 .format
= UDEBUG_FORMAT_PACKET
,
48 .sub_format
= UDEBUG_DLT_NETLINK
,
50 static struct udebug_ubus_ring rings
[] = {
54 .default_entries
= 1024,
55 .default_size
= 64 * 1024,
60 .default_entries
= 1024,
61 .default_size
= 64 * 1024,
65 #define DEFAULT_LOG_LEVEL L_NOTICE
67 static int log_level
= DEFAULT_LOG_LEVEL
;
68 static const int log_class
[] = {
70 [L_WARNING
] = LOG_WARNING
,
71 [L_NOTICE
] = LOG_NOTICE
,
77 #define use_syslog false
79 static bool use_syslog
= true;
84 netifd_delete_process(struct netifd_process
*proc
)
86 while (ustream_poll(&proc
->log
.stream
));
87 list_del(&proc
->list
);
88 ustream_free(&proc
->log
.stream
);
89 close(proc
->log
.fd
.fd
);
93 netifd_udebug_vprintf(const char *format
, va_list ap
)
95 if (!udebug_buf_valid(&udb_log
))
98 udebug_entry_init(&udb_log
);
99 udebug_entry_vprintf(&udb_log
, format
, ap
);
100 udebug_entry_add(&udb_log
);
103 void netifd_udebug_printf(const char *format
, ...)
107 va_start(ap
, format
);
108 netifd_udebug_vprintf(format
, ap
);
112 void netifd_udebug_config(struct udebug_ubus
*ctx
, struct blob_attr
*data
,
115 udebug_ubus_apply_config(&ud
, rings
, ARRAY_SIZE(rings
), data
, enabled
);
119 __attribute__((format(printf
, 2, 0)))
120 netifd_log_message(int priority
, const char *format
, ...)
124 if (priority
> log_level
)
127 va_start(vl
, format
);
128 netifd_udebug_vprintf(format
, vl
);
130 vsyslog(log_class
[priority
], format
, vl
);
132 vfprintf(stderr
, format
, vl
);
137 netifd_process_log_read_cb(struct ustream
*s
, int bytes
)
139 struct netifd_process
*proc
;
140 const char *log_prefix
;
144 proc
= container_of(s
, struct netifd_process
, log
.stream
);
145 log_prefix
= proc
->log_prefix
;
147 log_prefix
= "process";
152 data
= ustream_get_read_buf(s
, &len
);
156 newline
= strchr(data
, '\n');
158 if (proc
->log_overflow
) {
160 len
= newline
+ 1 - data
;
161 proc
->log_overflow
= false;
163 } else if (newline
) {
165 len
= newline
+ 1 - data
;
166 netifd_log_message(L_NOTICE
, "%s (%d): %s\n",
167 log_prefix
, proc
->uloop
.pid
, data
);
168 } else if (len
== s
->r
.buffer_len
) {
169 netifd_log_message(L_NOTICE
, "%s (%d): %s [...]\n",
170 log_prefix
, proc
->uloop
.pid
, data
);
171 proc
->log_overflow
= true;
175 ustream_consume(s
, len
);
180 netifd_process_cb(struct uloop_process
*proc
, int ret
)
182 struct netifd_process
*np
;
183 np
= container_of(proc
, struct netifd_process
, uloop
);
185 netifd_delete_process(np
);
191 netifd_start_process(const char **argv
, char **env
, struct netifd_process
*proc
)
196 netifd_kill_process(proc
);
201 if ((pid
= fork()) < 0)
213 if (proc
->dir_fd
>= 0)
214 if (fchdir(proc
->dir_fd
)) {}
218 for (i
= 0; i
<= 2; i
++) {
228 execvp(argv
[0], (char **) argv
);
233 proc
->uloop
.cb
= netifd_process_cb
;
234 proc
->uloop
.pid
= pid
;
235 uloop_process_add(&proc
->uloop
);
236 list_add_tail(&proc
->list
, &process_list
);
238 system_fd_set_cloexec(pfds
[0]);
239 proc
->log
.stream
.string_data
= true;
240 proc
->log
.stream
.notify_read
= netifd_process_log_read_cb
;
241 ustream_fd_init(&proc
->log
, pfds
[0]);
252 netifd_kill_process(struct netifd_process
*proc
)
254 if (!proc
->uloop
.pending
)
257 kill(proc
->uloop
.pid
, SIGKILL
);
258 uloop_process_delete(&proc
->uloop
);
259 netifd_delete_process(proc
);
262 static void netifd_do_restart(struct uloop_timeout
*timeout
)
264 execvp(global_argv
[0], global_argv
);
267 int netifd_reload(void)
269 return config_init_all();
272 void netifd_restart(void)
274 static struct uloop_timeout main_timer
= {
275 .cb
= netifd_do_restart
278 interface_set_down(NULL
);
279 uloop_timeout_set(&main_timer
, 1000);
282 static int usage(const char *progname
)
284 fprintf(stderr
, "Usage: %s [options]\n"
286 " -d <mask>: Mask for debug messages\n"
287 " -s <path>: Path to the ubus socket\n"
288 " -p <path>: Path to netifd addons (default: %s)\n"
289 " -c <path>: Path to UCI configuration\n"
290 " -h <path>: Path to the hotplug script\n"
291 " (default: "DEFAULT_HOTPLUG_PATH
")\n"
292 " -r <path>: Path to resolv.conf\n"
293 " -l <level>: Log output level (default: %d)\n"
294 " -S: Use stderr instead of syslog for log messages\n"
295 "\n", progname
, main_path
, DEFAULT_LOG_LEVEL
);
301 netifd_handle_signal(int signo
)
307 netifd_setup_signals(void)
311 memset(&s
, 0, sizeof(s
));
312 s
.sa_handler
= netifd_handle_signal
;
314 sigaction(SIGINT
, &s
, NULL
);
315 sigaction(SIGTERM
, &s
, NULL
);
316 sigaction(SIGUSR1
, &s
, NULL
);
317 sigaction(SIGUSR2
, &s
, NULL
);
319 s
.sa_handler
= SIG_IGN
;
320 sigaction(SIGPIPE
, &s
, NULL
);
324 netifd_kill_processes(void)
326 struct netifd_process
*proc
, *tmp
;
328 list_for_each_entry_safe(proc
, tmp
, &process_list
, list
)
329 netifd_kill_process(proc
);
332 int main(int argc
, char **argv
)
334 const char *socket
= NULL
;
339 while ((ch
= getopt(argc
, argv
, "d:s:p:c:h:r:l:S")) != -1) {
342 debug_mask
= strtoul(optarg
, NULL
, 0);
351 config_path
= optarg
;
354 hotplug_cmd_path
= optarg
;
357 resolv_conf
= optarg
;
360 log_level
= atoi(optarg
);
361 if (log_level
>= (int)ARRAY_SIZE(log_class
))
362 log_level
= (int)ARRAY_SIZE(log_class
) - 1;
370 return usage(argv
[0]);
375 openlog("netifd", 0, LOG_DAEMON
);
377 netifd_setup_signals();
380 udebug_auto_connect(&ud
, NULL
);
381 for (size_t i
= 0; i
< ARRAY_SIZE(rings
); i
++)
382 udebug_ubus_ring_init(&ud
, &rings
[i
]);
384 if (netifd_ubus_init(socket
) < 0) {
385 fprintf(stderr
, "Failed to connect to ubus\n");
394 fprintf(stderr
, "Failed to initialize system control\n");
401 netifd_kill_processes();