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
);
36 #define DEFAULT_LOG_LEVEL L_NOTICE
38 static int log_level
= DEFAULT_LOG_LEVEL
;
39 static const int log_class
[] = {
41 [L_WARNING
] = LOG_WARNING
,
42 [L_NOTICE
] = LOG_NOTICE
,
48 #define use_syslog false
50 static bool use_syslog
= true;
55 netifd_delete_process(struct netifd_process
*proc
)
57 if (proc
->uloop
.pending
)
58 uloop_process_delete(&proc
->uloop
);
59 list_del(&proc
->list
);
60 uloop_fd_delete(&proc
->log_uloop
);
61 close(proc
->log_uloop
.fd
);
69 netifd_log_message(int priority
, const char *format
, ...)
73 if (priority
> log_level
)
78 vsyslog(log_class
[priority
], format
, vl
);
80 vfprintf(stderr
, format
, vl
);
85 netifd_process_log_cb(struct uloop_fd
*fd
, unsigned int events
)
87 struct netifd_process
*proc
;
88 const char *log_prefix
;
90 int maxlen
, len
, read_len
;
92 proc
= container_of(fd
, struct netifd_process
, log_uloop
);
95 proc
->log_buf
= malloc(LOG_BUF_SIZE
+ 1);
97 log_prefix
= proc
->log_prefix
;
99 log_prefix
= "process";
102 buf
= proc
->log_buf
+ proc
->log_buf_ofs
;
103 maxlen
= LOG_BUF_SIZE
- proc
->log_buf_ofs
;
104 read_len
= len
= read(fd
->fd
, buf
, maxlen
);
113 proc
->log_buf_ofs
+= len
;
115 len
= proc
->log_buf_ofs
;
118 cur
= memchr(buf
, '\n', len
);
124 if (!proc
->log_overflow
)
125 netifd_log_message(L_NOTICE
, "%s (%d): %s\n",
126 log_prefix
, proc
->uloop
.pid
, buf
);
128 proc
->log_overflow
= false;
135 if (buf
> proc
->log_buf
&& len
> 0)
136 memmove(proc
->log_buf
, buf
, len
);
138 if (len
== LOG_BUF_SIZE
) {
139 if (!proc
->log_overflow
) {
140 proc
->log_buf
[LOG_BUF_SIZE
] = 0;
141 netifd_log_message(L_NOTICE
, "%s (%d): %s [...]\n",
142 log_prefix
, proc
->uloop
.pid
, proc
->log_buf
);
143 proc
->log_overflow
= true;
147 proc
->log_buf_ofs
= len
;
158 netifd_process_cb(struct uloop_process
*proc
, int ret
)
160 struct netifd_process
*np
;
161 np
= container_of(proc
, struct netifd_process
, uloop
);
162 netifd_process_log_cb(&np
->log_uloop
, 0);
163 netifd_delete_process(np
);
164 return np
->cb(np
, ret
);
168 netifd_start_process(const char **argv
, char **env
, struct netifd_process
*proc
)
173 netifd_kill_process(proc
);
178 if ((pid
= fork()) < 0)
188 if (proc
->dir_fd
>= 0)
189 fchdir(proc
->dir_fd
);
198 execvp(argv
[0], (char **) argv
);
206 proc
->uloop
.cb
= netifd_process_cb
;
207 proc
->uloop
.pid
= pid
;
208 uloop_process_add(&proc
->uloop
);
209 list_add_tail(&proc
->list
, &process_list
);
211 system_fd_set_cloexec(pfds
[0]);
212 proc
->log_buf_ofs
= 0;
213 proc
->log_uloop
.fd
= pfds
[0];
214 proc
->log_uloop
.cb
= netifd_process_log_cb
;
215 uloop_fd_add(&proc
->log_uloop
, ULOOP_EDGE_TRIGGER
| ULOOP_READ
);
226 netifd_kill_process(struct netifd_process
*proc
)
228 if (!proc
->uloop
.pending
)
231 kill(proc
->uloop
.pid
, SIGKILL
);
232 netifd_delete_process(proc
);
235 static void netifd_do_restart(struct uloop_timeout
*timeout
)
237 execvp(global_argv
[0], global_argv
);
240 static void netifd_do_reload(struct uloop_timeout
*timeout
)
245 static struct uloop_timeout main_timer
;
247 void netifd_reload(void)
249 main_timer
.cb
= netifd_do_reload
;
250 uloop_timeout_set(&main_timer
, 100);
253 void netifd_restart(void)
255 main_timer
.cb
= netifd_do_restart
;
256 interface_set_down(NULL
);
257 uloop_timeout_set(&main_timer
, 1000);
260 static int usage(const char *progname
)
262 fprintf(stderr
, "Usage: %s [options]\n"
264 " -d <mask>: Mask for debug messages\n"
265 " -s <path>: Path to the ubus socket\n"
266 " -p <path>: Path to netifd addons (default: %s)\n"
267 " -h <path>: Path to the hotplug script\n"
268 " -r <path>: Path to resolv.conf\n"
269 " -l <level>: Log output level (default: %d)\n"
270 " -S: Use stderr instead of syslog for log messages\n"
271 " (default: "DEFAULT_HOTPLUG_PATH
")\n"
272 "\n", progname
, main_path
, DEFAULT_LOG_LEVEL
);
278 netifd_handle_signal(int signo
)
284 netifd_setup_signals(void)
288 memset(&s
, 0, sizeof(s
));
289 s
.sa_handler
= netifd_handle_signal
;
291 sigaction(SIGINT
, &s
, NULL
);
292 sigaction(SIGTERM
, &s
, NULL
);
293 sigaction(SIGUSR1
, &s
, NULL
);
294 sigaction(SIGUSR2
, &s
, NULL
);
296 s
.sa_handler
= SIG_IGN
;
297 sigaction(SIGPIPE
, &s
, NULL
);
301 netifd_kill_processes(void)
303 struct netifd_process
*proc
, *tmp
;
305 list_for_each_entry_safe(proc
, tmp
, &process_list
, list
)
306 netifd_kill_process(proc
);
309 int main(int argc
, char **argv
)
311 const char *socket
= NULL
;
316 while ((ch
= getopt(argc
, argv
, "d:s:p:h:r:l:S")) != -1) {
319 debug_mask
= strtoul(optarg
, NULL
, 0);
328 hotplug_cmd_path
= optarg
;
331 resolv_conf
= optarg
;
334 log_level
= atoi(optarg
);
335 if (log_level
>= ARRAY_SIZE(log_class
))
336 log_level
= ARRAY_SIZE(log_class
) - 1;
344 return usage(argv
[0]);
349 openlog("netifd", 0, LOG_DAEMON
);
351 netifd_setup_signals();
352 if (netifd_ubus_init(socket
) < 0) {
353 fprintf(stderr
, "Failed to connect to ubus\n");
358 fprintf(stderr
, "Failed to initialize system control\n");
365 netifd_kill_processes();