02fd9ffa1b7b08135408b8fdf89c2c2fa9422085
14 #include "interface.h"
16 unsigned int debug_mask
= 0;
17 const char *main_path
= DEFAULT_MAIN_PATH
;
18 const char *resolv_conf
= DEFAULT_RESOLV_CONF
;
19 static char **global_argv
;
21 static struct list_head process_list
= LIST_HEAD_INIT(process_list
);
22 static struct list_head fds
= LIST_HEAD_INIT(fds
);
24 #define DEFAULT_LOG_LEVEL L_NOTICE
34 static int log_level
= DEFAULT_LOG_LEVEL
;
35 static const int log_class
[] = {
37 [L_WARNING
] = LOG_WARNING
,
38 [L_NOTICE
] = LOG_NOTICE
,
44 #define use_syslog false
46 static bool use_syslog
= true;
51 netifd_delete_process(struct netifd_process
*proc
)
53 if (proc
->uloop
.pending
)
54 uloop_process_delete(&proc
->uloop
);
55 list_del(&proc
->list
);
56 netifd_fd_delete(&proc
->log_fd
);
60 netifd_log_message(int priority
, const char *format
, ...)
64 if (priority
> log_level
)
69 vsyslog(log_class
[priority
], format
, vl
);
71 vfprintf(stderr
, format
, vl
);
76 netifd_process_log_cb(struct uloop_fd
*fd
, unsigned int events
)
78 struct netifd_process
*proc
;
79 const char *log_prefix
;
81 int maxlen
, len
, read_len
;
83 proc
= container_of(fd
, struct netifd_process
, log_uloop
);
86 proc
->log_buf
= malloc(LOG_BUF_SIZE
+ 1);
88 buf
= proc
->log_buf
+ proc
->log_buf_ofs
;
89 maxlen
= LOG_BUF_SIZE
- proc
->log_buf_ofs
;
91 log_prefix
= proc
->log_prefix
;
93 log_prefix
= "process";
96 read_len
= len
= read(fd
->fd
, buf
, maxlen
);
103 proc
->log_buf_ofs
+= len
;
107 while ((cur
= memchr(cur
, '\n', len
))) {
110 if (!proc
->log_overflow
)
111 netifd_log_message(L_NOTICE
, "%s (%d): %s\n",
112 log_prefix
, proc
->uloop
.pid
, buf
);
114 proc
->log_overflow
= false;
121 if (buf
> proc
->log_buf
&& len
> 0)
122 memmove(buf
, proc
->log_buf
, len
);
124 if (len
== LOG_BUF_SIZE
) {
125 if (!proc
->log_overflow
) {
126 proc
->log_buf
[LOG_BUF_SIZE
] = 0;
127 netifd_log_message(L_NOTICE
, "%s (%d): %s [...]\n",
128 log_prefix
, proc
->uloop
.pid
, proc
->log_buf
);
129 proc
->log_overflow
= true;
133 proc
->log_buf_ofs
= len
;
135 if (read_len
== maxlen
)
140 netifd_process_cb(struct uloop_process
*proc
, int ret
)
142 struct netifd_process
*np
;
143 np
= container_of(proc
, struct netifd_process
, uloop
);
144 netifd_process_log_cb(&np
->log_uloop
, 0);
145 netifd_delete_process(np
);
146 return np
->cb(np
, ret
);
150 netifd_start_process(const char **argv
, char **env
, struct netifd_process
*proc
)
152 struct netifd_fd
*fd
;
156 netifd_kill_process(proc
);
161 if ((pid
= fork()) < 0)
171 if (proc
->dir_fd
>= 0)
172 fchdir(proc
->dir_fd
);
174 /* close all non-essential fds */
175 list_for_each_entry(fd
, &fds
, list
) {
176 if (fd
->proc
== proc
)
188 execvp(argv
[0], (char **) argv
);
196 proc
->uloop
.cb
= netifd_process_cb
;
197 proc
->uloop
.pid
= pid
;
198 uloop_process_add(&proc
->uloop
);
199 list_add_tail(&proc
->list
, &process_list
);
201 proc
->log_uloop
.fd
= proc
->log_fd
.fd
= pfds
[0];
202 proc
->log_uloop
.cb
= netifd_process_log_cb
;
203 netifd_fd_add(&proc
->log_fd
);
204 uloop_fd_add(&proc
->log_uloop
, ULOOP_EDGE_TRIGGER
| ULOOP_READ
);
215 netifd_kill_process(struct netifd_process
*proc
)
217 if (!proc
->uloop
.pending
)
220 kill(proc
->uloop
.pid
, SIGTERM
);
221 netifd_delete_process(proc
);
225 netifd_fd_add(struct netifd_fd
*fd
)
227 list_add_tail(&fd
->list
, &fds
);
231 netifd_fd_delete(struct netifd_fd
*fd
)
236 static void netifd_do_restart(struct uloop_timeout
*timeout
)
238 execvp(global_argv
[0], global_argv
);
241 static void netifd_do_reload(struct uloop_timeout
*timeout
)
243 config_init_interfaces(NULL
);
246 static struct uloop_timeout main_timer
;
248 void netifd_reload(void)
250 main_timer
.cb
= netifd_do_reload
;
251 uloop_timeout_set(&main_timer
, 100);
254 void netifd_restart(void)
256 main_timer
.cb
= netifd_do_restart
;
257 interface_set_down(NULL
);
258 uloop_timeout_set(&main_timer
, 1000);
261 static int usage(const char *progname
)
263 fprintf(stderr
, "Usage: %s [options]\n"
265 " -d <mask>: Mask for debug messages\n"
266 " -s <path>: Path to the ubus socket\n"
267 " -p <path>: Path to netifd addons (default: %s)\n"
268 " -h <path>: Path to the hotplug script\n"
269 " -r <path>: Path to resolv.conf\n"
270 " -l <level>: Log output level (default: %d)\n"
271 " -S: Use stderr instead of syslog for log messages\n"
272 " (default: "DEFAULT_HOTPLUG_PATH
")\n"
273 "\n", progname
, main_path
, DEFAULT_LOG_LEVEL
);
279 netifd_handle_signal(int signo
)
285 netifd_setup_signals(void)
289 memset(&s
, 0, sizeof(s
));
290 s
.sa_handler
= netifd_handle_signal
;
292 sigaction(SIGINT
, &s
, NULL
);
293 sigaction(SIGTERM
, &s
, NULL
);
294 sigaction(SIGUSR1
, &s
, NULL
);
295 sigaction(SIGUSR2
, &s
, NULL
);
297 s
.sa_handler
= SIG_IGN
;
298 sigaction(SIGPIPE
, &s
, NULL
);
302 netifd_kill_processes(void)
304 struct netifd_process
*proc
, *tmp
;
306 list_for_each_entry_safe(proc
, tmp
, &process_list
, list
)
307 netifd_kill_process(proc
);
310 int main(int argc
, char **argv
)
312 const char *socket
= NULL
;
317 while ((ch
= getopt(argc
, argv
, "d:s:p:h:r:l:S")) != -1) {
320 debug_mask
= strtoul(optarg
, NULL
, 0);
329 hotplug_cmd_path
= optarg
;
332 resolv_conf
= optarg
;
335 log_level
= atoi(optarg
);
336 if (log_level
>= ARRAY_SIZE(log_class
))
337 log_level
= ARRAY_SIZE(log_class
) - 1;
345 return usage(argv
[0]);
350 openlog("netifd", 0, LOG_DAEMON
);
352 netifd_setup_signals();
353 if (netifd_ubus_init(socket
) < 0) {
354 fprintf(stderr
, "Failed to connect to ubus\n");
359 fprintf(stderr
, "Failed to initialize system control\n");
363 config_init_interfaces(NULL
);
366 netifd_kill_processes();