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
);
39 #define DEFAULT_LOG_LEVEL L_NOTICE
41 static int log_level
= DEFAULT_LOG_LEVEL
;
42 static const int log_class
[] = {
44 [L_WARNING
] = LOG_WARNING
,
45 [L_NOTICE
] = LOG_NOTICE
,
51 #define use_syslog false
53 static bool use_syslog
= true;
58 netifd_delete_process(struct netifd_process
*proc
)
60 while (ustream_poll(&proc
->log
.stream
));
61 list_del(&proc
->list
);
62 ustream_free(&proc
->log
.stream
);
63 close(proc
->log
.fd
.fd
);
67 __attribute__((format(printf
, 2, 0)))
68 netifd_log_message(int priority
, const char *format
, ...)
72 if (priority
> log_level
)
77 vsyslog(log_class
[priority
], format
, vl
);
79 vfprintf(stderr
, format
, vl
);
84 netifd_process_log_read_cb(struct ustream
*s
, int bytes
)
86 struct netifd_process
*proc
;
87 const char *log_prefix
;
91 proc
= container_of(s
, struct netifd_process
, log
.stream
);
92 log_prefix
= proc
->log_prefix
;
94 log_prefix
= "process";
99 data
= ustream_get_read_buf(s
, &len
);
103 newline
= strchr(data
, '\n');
105 if (proc
->log_overflow
) {
107 len
= newline
+ 1 - data
;
108 proc
->log_overflow
= false;
110 } else if (newline
) {
112 len
= newline
+ 1 - data
;
113 netifd_log_message(L_NOTICE
, "%s (%d): %s\n",
114 log_prefix
, proc
->uloop
.pid
, data
);
115 } else if (len
== s
->r
.buffer_len
) {
116 netifd_log_message(L_NOTICE
, "%s (%d): %s [...]\n",
117 log_prefix
, proc
->uloop
.pid
, data
);
118 proc
->log_overflow
= true;
122 ustream_consume(s
, len
);
127 netifd_process_cb(struct uloop_process
*proc
, int ret
)
129 struct netifd_process
*np
;
130 np
= container_of(proc
, struct netifd_process
, uloop
);
132 netifd_delete_process(np
);
138 netifd_start_process(const char **argv
, char **env
, struct netifd_process
*proc
)
143 netifd_kill_process(proc
);
148 if ((pid
= fork()) < 0)
160 if (proc
->dir_fd
>= 0)
161 if (fchdir(proc
->dir_fd
)) {}
165 for (i
= 0; i
<= 2; i
++) {
175 execvp(argv
[0], (char **) argv
);
180 proc
->uloop
.cb
= netifd_process_cb
;
181 proc
->uloop
.pid
= pid
;
182 uloop_process_add(&proc
->uloop
);
183 list_add_tail(&proc
->list
, &process_list
);
185 system_fd_set_cloexec(pfds
[0]);
186 proc
->log
.stream
.string_data
= true;
187 proc
->log
.stream
.notify_read
= netifd_process_log_read_cb
;
188 ustream_fd_init(&proc
->log
, pfds
[0]);
199 netifd_kill_process(struct netifd_process
*proc
)
201 if (!proc
->uloop
.pending
)
204 kill(proc
->uloop
.pid
, SIGKILL
);
205 uloop_process_delete(&proc
->uloop
);
206 netifd_delete_process(proc
);
209 static void netifd_do_restart(struct uloop_timeout
*timeout
)
211 execvp(global_argv
[0], global_argv
);
214 int netifd_reload(void)
216 return config_init_all();
219 void netifd_restart(void)
221 static struct uloop_timeout main_timer
= {
222 .cb
= netifd_do_restart
225 interface_set_down(NULL
);
226 uloop_timeout_set(&main_timer
, 1000);
229 static int usage(const char *progname
)
231 fprintf(stderr
, "Usage: %s [options]\n"
233 " -d <mask>: Mask for debug messages\n"
234 " -s <path>: Path to the ubus socket\n"
235 " -p <path>: Path to netifd addons (default: %s)\n"
236 " -c <path>: Path to UCI configuration\n"
237 " -h <path>: Path to the hotplug script\n"
238 " (default: "DEFAULT_HOTPLUG_PATH
")\n"
239 " -r <path>: Path to resolv.conf\n"
240 " -l <level>: Log output level (default: %d)\n"
241 " -S: Use stderr instead of syslog for log messages\n"
242 "\n", progname
, main_path
, DEFAULT_LOG_LEVEL
);
248 netifd_handle_signal(int signo
)
254 netifd_setup_signals(void)
258 memset(&s
, 0, sizeof(s
));
259 s
.sa_handler
= netifd_handle_signal
;
261 sigaction(SIGINT
, &s
, NULL
);
262 sigaction(SIGTERM
, &s
, NULL
);
263 sigaction(SIGUSR1
, &s
, NULL
);
264 sigaction(SIGUSR2
, &s
, NULL
);
266 s
.sa_handler
= SIG_IGN
;
267 sigaction(SIGPIPE
, &s
, NULL
);
271 netifd_kill_processes(void)
273 struct netifd_process
*proc
, *tmp
;
275 list_for_each_entry_safe(proc
, tmp
, &process_list
, list
)
276 netifd_kill_process(proc
);
279 int main(int argc
, char **argv
)
281 const char *socket
= NULL
;
286 while ((ch
= getopt(argc
, argv
, "d:s:p:c:h:r:l:S")) != -1) {
289 debug_mask
= strtoul(optarg
, NULL
, 0);
298 config_path
= optarg
;
301 hotplug_cmd_path
= optarg
;
304 resolv_conf
= optarg
;
307 log_level
= atoi(optarg
);
308 if (log_level
>= (int)ARRAY_SIZE(log_class
))
309 log_level
= (int)ARRAY_SIZE(log_class
) - 1;
317 return usage(argv
[0]);
322 openlog("netifd", 0, LOG_DAEMON
);
324 netifd_setup_signals();
325 if (netifd_ubus_init(socket
) < 0) {
326 fprintf(stderr
, "Failed to connect to ubus\n");
335 fprintf(stderr
, "Failed to initialize system control\n");
342 netifd_kill_processes();