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
);
25 #define use_syslog false
27 static bool use_syslog
= true;
32 netifd_delete_process(struct netifd_process
*proc
)
34 if (proc
->uloop
.pending
)
35 uloop_process_delete(&proc
->uloop
);
36 list_del(&proc
->list
);
37 netifd_fd_delete(&proc
->log_fd
);
41 netifd_log_message(int priority
, const char *format
, ...)
47 vsyslog(priority
, format
, vl
);
49 vfprintf(stderr
, format
, vl
);
54 netifd_process_log_cb(struct uloop_fd
*fd
, unsigned int events
)
56 struct netifd_process
*proc
;
57 const char *log_prefix
;
59 int maxlen
, len
, read_len
;
61 proc
= container_of(fd
, struct netifd_process
, log_uloop
);
64 proc
->log_buf
= malloc(LOG_BUF_SIZE
+ 1);
66 buf
= proc
->log_buf
+ proc
->log_buf_ofs
;
67 maxlen
= LOG_BUF_SIZE
- proc
->log_buf_ofs
;
69 log_prefix
= proc
->log_prefix
;
71 log_prefix
= "process";
74 read_len
= len
= read(fd
->fd
, buf
, maxlen
);
81 proc
->log_buf_ofs
+= len
;
85 while ((cur
= memchr(cur
, '\n', len
))) {
88 if (!proc
->log_overflow
)
89 netifd_log_message(LOG_NOTICE
, "%s (%d): %s\n",
90 log_prefix
, proc
->uloop
.pid
, buf
);
92 proc
->log_overflow
= false;
99 if (buf
> proc
->log_buf
&& len
> 0)
100 memmove(buf
, proc
->log_buf
, len
);
102 if (len
== LOG_BUF_SIZE
) {
103 if (!proc
->log_overflow
) {
104 proc
->log_buf
[LOG_BUF_SIZE
] = 0;
105 netifd_log_message(LOG_NOTICE
, "%s (%d): %s [...]\n",
106 log_prefix
, proc
->uloop
.pid
, proc
->log_buf
);
107 proc
->log_overflow
= true;
111 proc
->log_buf_ofs
= len
;
113 if (read_len
== maxlen
)
118 netifd_process_cb(struct uloop_process
*proc
, int ret
)
120 struct netifd_process
*np
;
121 np
= container_of(proc
, struct netifd_process
, uloop
);
122 netifd_process_log_cb(&np
->log_uloop
, 0);
123 netifd_delete_process(np
);
124 return np
->cb(np
, ret
);
128 netifd_start_process(const char **argv
, char **env
, struct netifd_process
*proc
)
130 struct netifd_fd
*fd
;
134 netifd_kill_process(proc
);
139 if ((pid
= fork()) < 0)
149 if (proc
->dir_fd
>= 0)
150 fchdir(proc
->dir_fd
);
152 /* close all non-essential fds */
153 list_for_each_entry(fd
, &fds
, list
) {
154 if (fd
->proc
== proc
)
166 execvp(argv
[0], (char **) argv
);
174 proc
->uloop
.cb
= netifd_process_cb
;
175 proc
->uloop
.pid
= pid
;
176 uloop_process_add(&proc
->uloop
);
177 list_add_tail(&proc
->list
, &process_list
);
179 proc
->log_uloop
.fd
= proc
->log_fd
.fd
= pfds
[0];
180 proc
->log_uloop
.cb
= netifd_process_log_cb
;
181 netifd_fd_add(&proc
->log_fd
);
182 uloop_fd_add(&proc
->log_uloop
, ULOOP_EDGE_TRIGGER
| ULOOP_READ
);
193 netifd_kill_process(struct netifd_process
*proc
)
195 if (!proc
->uloop
.pending
)
198 kill(proc
->uloop
.pid
, SIGTERM
);
199 netifd_delete_process(proc
);
203 netifd_fd_add(struct netifd_fd
*fd
)
205 list_add_tail(&fd
->list
, &fds
);
209 netifd_fd_delete(struct netifd_fd
*fd
)
214 static void netifd_do_restart(struct uloop_timeout
*timeout
)
216 execvp(global_argv
[0], global_argv
);
219 static void netifd_do_reload(struct uloop_timeout
*timeout
)
221 config_init_interfaces(NULL
);
224 static struct uloop_timeout main_timer
;
226 void netifd_reload(void)
228 main_timer
.cb
= netifd_do_reload
;
229 uloop_timeout_set(&main_timer
, 100);
232 void netifd_restart(void)
234 main_timer
.cb
= netifd_do_restart
;
235 interface_set_down(NULL
);
236 uloop_timeout_set(&main_timer
, 1000);
239 static int usage(const char *progname
)
241 fprintf(stderr
, "Usage: %s [options]\n"
243 " -d <mask>: Mask for debug messages\n"
244 " -s <path>: Path to the ubus socket\n"
245 " -p <path>: Path to netifd addons (default: %s)\n"
246 " -h <path>: Path to the hotplug script\n"
247 " -r <path>: Path to resolv.conf\n"
248 " -S: Use stderr instead of syslog for log messages\n"
249 " (default: "DEFAULT_HOTPLUG_PATH
")\n"
250 "\n", progname
, main_path
);
256 netifd_handle_signal(int signo
)
262 netifd_setup_signals(void)
266 memset(&s
, 0, sizeof(s
));
267 s
.sa_handler
= netifd_handle_signal
;
269 sigaction(SIGINT
, &s
, NULL
);
270 sigaction(SIGTERM
, &s
, NULL
);
271 sigaction(SIGUSR1
, &s
, NULL
);
272 sigaction(SIGUSR2
, &s
, NULL
);
274 s
.sa_handler
= SIG_IGN
;
275 sigaction(SIGPIPE
, &s
, NULL
);
279 netifd_kill_processes(void)
281 struct netifd_process
*proc
, *tmp
;
283 list_for_each_entry_safe(proc
, tmp
, &process_list
, list
)
284 netifd_kill_process(proc
);
287 int main(int argc
, char **argv
)
289 const char *socket
= NULL
;
294 while ((ch
= getopt(argc
, argv
, "d:s:p:h:r:S")) != -1) {
297 debug_mask
= strtoul(optarg
, NULL
, 0);
306 hotplug_cmd_path
= optarg
;
309 resolv_conf
= optarg
;
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");
331 fprintf(stderr
, "Failed to initialize system control\n");
335 config_init_interfaces(NULL
);
338 netifd_kill_processes();