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
26 static int log_level
= DEFAULT_LOG_LEVEL
;
27 static const int log_class
[] = {
29 [L_WARNING
] = LOG_WARNING
,
30 [L_NOTICE
] = LOG_NOTICE
,
36 #define use_syslog false
38 static bool use_syslog
= true;
43 netifd_delete_process(struct netifd_process
*proc
)
45 if (proc
->uloop
.pending
)
46 uloop_process_delete(&proc
->uloop
);
47 list_del(&proc
->list
);
48 netifd_fd_delete(&proc
->log_fd
);
56 netifd_log_message(int priority
, const char *format
, ...)
60 if (priority
> log_level
)
65 vsyslog(log_class
[priority
], format
, vl
);
67 vfprintf(stderr
, format
, vl
);
72 netifd_process_log_cb(struct uloop_fd
*fd
, unsigned int events
)
74 struct netifd_process
*proc
;
75 const char *log_prefix
;
76 char *buf
, *start
, *cur
;
77 int maxlen
, len
, read_len
;
79 proc
= container_of(fd
, struct netifd_process
, log_uloop
);
82 proc
->log_buf
= malloc(LOG_BUF_SIZE
+ 1);
84 log_prefix
= proc
->log_prefix
;
86 log_prefix
= "process";
89 buf
= proc
->log_buf
+ proc
->log_buf_ofs
;
90 maxlen
= LOG_BUF_SIZE
- proc
->log_buf_ofs
;
91 read_len
= len
= read(fd
->fd
, buf
, maxlen
);
100 proc
->log_buf_ofs
+= len
;
103 start
= proc
->log_buf
;
105 cur
= memchr(cur
, '\n', len
);
111 if (!proc
->log_overflow
)
112 netifd_log_message(L_NOTICE
, "%s (%d): %s\n",
113 log_prefix
, proc
->uloop
.pid
, start
);
115 proc
->log_overflow
= false;
122 if (buf
> proc
->log_buf
&& len
> 0)
123 memmove(proc
->log_buf
, buf
, len
);
125 if (len
== LOG_BUF_SIZE
) {
126 if (!proc
->log_overflow
) {
127 proc
->log_buf
[LOG_BUF_SIZE
] = 0;
128 netifd_log_message(L_NOTICE
, "%s (%d): %s [...]\n",
129 log_prefix
, proc
->uloop
.pid
, proc
->log_buf
);
130 proc
->log_overflow
= true;
134 proc
->log_buf_ofs
= len
;
145 netifd_process_cb(struct uloop_process
*proc
, int ret
)
147 struct netifd_process
*np
;
148 np
= container_of(proc
, struct netifd_process
, uloop
);
149 netifd_process_log_cb(&np
->log_uloop
, 0);
150 netifd_delete_process(np
);
151 return np
->cb(np
, ret
);
155 netifd_start_process(const char **argv
, char **env
, struct netifd_process
*proc
)
157 struct netifd_fd
*fd
;
161 netifd_kill_process(proc
);
166 if ((pid
= fork()) < 0)
176 if (proc
->dir_fd
>= 0)
177 fchdir(proc
->dir_fd
);
179 /* close all non-essential fds */
180 list_for_each_entry(fd
, &fds
, list
) {
181 if (fd
->proc
== proc
)
193 execvp(argv
[0], (char **) argv
);
201 proc
->uloop
.cb
= netifd_process_cb
;
202 proc
->uloop
.pid
= pid
;
203 uloop_process_add(&proc
->uloop
);
204 list_add_tail(&proc
->list
, &process_list
);
206 proc
->log_buf_ofs
= 0;
207 proc
->log_uloop
.fd
= proc
->log_fd
.fd
= pfds
[0];
208 proc
->log_uloop
.cb
= netifd_process_log_cb
;
209 netifd_fd_add(&proc
->log_fd
);
210 uloop_fd_add(&proc
->log_uloop
, ULOOP_EDGE_TRIGGER
| ULOOP_READ
);
221 netifd_kill_process(struct netifd_process
*proc
)
223 if (!proc
->uloop
.pending
)
226 kill(proc
->uloop
.pid
, SIGKILL
);
227 netifd_delete_process(proc
);
231 netifd_fd_add(struct netifd_fd
*fd
)
233 list_add_tail(&fd
->list
, &fds
);
237 netifd_fd_delete(struct netifd_fd
*fd
)
242 static void netifd_do_restart(struct uloop_timeout
*timeout
)
244 execvp(global_argv
[0], global_argv
);
247 static void netifd_do_reload(struct uloop_timeout
*timeout
)
252 static struct uloop_timeout main_timer
;
254 void netifd_reload(void)
256 main_timer
.cb
= netifd_do_reload
;
257 uloop_timeout_set(&main_timer
, 100);
260 void netifd_restart(void)
262 main_timer
.cb
= netifd_do_restart
;
263 interface_set_down(NULL
);
264 uloop_timeout_set(&main_timer
, 1000);
267 static int usage(const char *progname
)
269 fprintf(stderr
, "Usage: %s [options]\n"
271 " -d <mask>: Mask for debug messages\n"
272 " -s <path>: Path to the ubus socket\n"
273 " -p <path>: Path to netifd addons (default: %s)\n"
274 " -h <path>: Path to the hotplug script\n"
275 " -r <path>: Path to resolv.conf\n"
276 " -l <level>: Log output level (default: %d)\n"
277 " -S: Use stderr instead of syslog for log messages\n"
278 " (default: "DEFAULT_HOTPLUG_PATH
")\n"
279 "\n", progname
, main_path
, DEFAULT_LOG_LEVEL
);
285 netifd_handle_signal(int signo
)
291 netifd_setup_signals(void)
295 memset(&s
, 0, sizeof(s
));
296 s
.sa_handler
= netifd_handle_signal
;
298 sigaction(SIGINT
, &s
, NULL
);
299 sigaction(SIGTERM
, &s
, NULL
);
300 sigaction(SIGUSR1
, &s
, NULL
);
301 sigaction(SIGUSR2
, &s
, NULL
);
303 s
.sa_handler
= SIG_IGN
;
304 sigaction(SIGPIPE
, &s
, NULL
);
308 netifd_kill_processes(void)
310 struct netifd_process
*proc
, *tmp
;
312 list_for_each_entry_safe(proc
, tmp
, &process_list
, list
)
313 netifd_kill_process(proc
);
316 int main(int argc
, char **argv
)
318 const char *socket
= NULL
;
323 while ((ch
= getopt(argc
, argv
, "d:s:p:h:r:l:S")) != -1) {
326 debug_mask
= strtoul(optarg
, NULL
, 0);
335 hotplug_cmd_path
= optarg
;
338 resolv_conf
= optarg
;
341 log_level
= atoi(optarg
);
342 if (log_level
>= ARRAY_SIZE(log_class
))
343 log_level
= ARRAY_SIZE(log_class
) - 1;
351 return usage(argv
[0]);
356 openlog("netifd", 0, LOG_DAEMON
);
358 netifd_setup_signals();
359 if (netifd_ubus_init(socket
) < 0) {
360 fprintf(stderr
, "Failed to connect to ubus\n");
365 fprintf(stderr
, "Failed to initialize system control\n");
372 netifd_kill_processes();