92c6f4969695d696fbb5952efac552bd7b20b516
[project/netifd.git] / main.c
1 /*
2 * netifd - network interface daemon
3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
4 *
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
8 *
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.
13 */
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <getopt.h>
18 #include <signal.h>
19 #include <stdarg.h>
20 #include <syslog.h>
21
22 #include "netifd.h"
23 #include "ubus.h"
24 #include "config.h"
25 #include "system.h"
26 #include "interface.h"
27
28 unsigned int debug_mask = 0;
29 const char *main_path = DEFAULT_MAIN_PATH;
30 const char *resolv_conf = DEFAULT_RESOLV_CONF;
31 static char **global_argv;
32
33 static struct list_head process_list = LIST_HEAD_INIT(process_list);
34
35 #define DEFAULT_LOG_LEVEL L_NOTICE
36
37 static int log_level = DEFAULT_LOG_LEVEL;
38 static const int log_class[] = {
39 [L_CRIT] = LOG_CRIT,
40 [L_WARNING] = LOG_WARNING,
41 [L_NOTICE] = LOG_NOTICE,
42 [L_INFO] = LOG_INFO,
43 [L_DEBUG] = LOG_DEBUG
44 };
45
46 #ifdef DUMMY_MODE
47 #define use_syslog false
48 #else
49 static bool use_syslog = true;
50 #endif
51
52
53 static void
54 netifd_delete_process(struct netifd_process *proc)
55 {
56 list_del(&proc->list);
57 ustream_free(&proc->log.stream);
58 close(proc->log.fd.fd);
59 }
60
61 void
62 netifd_log_message(int priority, const char *format, ...)
63 {
64 va_list vl;
65
66 if (priority > log_level)
67 return;
68
69 va_start(vl, format);
70 if (use_syslog)
71 vsyslog(log_class[priority], format, vl);
72 else
73 vfprintf(stderr, format, vl);
74 va_end(vl);
75 }
76
77 static void
78 netifd_process_log_read_cb(struct ustream *s, int bytes)
79 {
80 struct netifd_process *proc;
81 const char *log_prefix;
82 char *data;
83 int len = 0;
84
85 proc = container_of(s, struct netifd_process, log.stream);
86 log_prefix = proc->log_prefix;
87 if (!log_prefix)
88 log_prefix = "process";
89
90 do {
91 char *newline;
92
93 data = ustream_get_read_buf(s, &len);
94 if (!len)
95 break;
96
97 newline = strchr(data, '\n');
98
99 if (proc->log_overflow) {
100 if (newline) {
101 len = newline + 1 - data;
102 proc->log_overflow = false;
103 }
104 } else if (newline) {
105 *newline = 0;
106 len = newline + 1 - data;
107 netifd_log_message(L_NOTICE, "%s (%d): %s\n",
108 log_prefix, proc->uloop.pid, data);
109 } else if (len == s->r.buffer_len) {
110 netifd_log_message(L_NOTICE, "%s (%d): %s [...]\n",
111 log_prefix, proc->uloop.pid, data);
112 proc->log_overflow = true;
113 } else
114 break;
115
116 ustream_consume(s, len);
117 } while (1);
118 }
119
120 static void
121 netifd_process_cb(struct uloop_process *proc, int ret)
122 {
123 struct netifd_process *np;
124 np = container_of(proc, struct netifd_process, uloop);
125
126 while (ustream_poll(&np->log.stream));
127 netifd_delete_process(np);
128 return np->cb(np, ret);
129 }
130
131 int
132 netifd_start_process(const char **argv, char **env, struct netifd_process *proc)
133 {
134 int pfds[2];
135 int pid;
136
137 netifd_kill_process(proc);
138
139 if (pipe(pfds) < 0)
140 return -1;
141
142 if ((pid = fork()) < 0)
143 goto error;
144
145 if (!pid) {
146 if (env) {
147 while (*env) {
148 putenv(*env);
149 env++;
150 }
151 }
152 if (proc->dir_fd >= 0)
153 fchdir(proc->dir_fd);
154
155 dup2(pfds[1], 0);
156 dup2(pfds[1], 1);
157 dup2(pfds[1], 2);
158
159 close(pfds[0]);
160 close(pfds[1]);
161
162 execvp(argv[0], (char **) argv);
163 exit(127);
164 }
165
166 if (pid < 0)
167 goto error;
168
169 close(pfds[1]);
170 proc->uloop.cb = netifd_process_cb;
171 proc->uloop.pid = pid;
172 uloop_process_add(&proc->uloop);
173 list_add_tail(&proc->list, &process_list);
174
175 system_fd_set_cloexec(pfds[0]);
176 proc->log.stream.string_data = true;
177 proc->log.stream.notify_read = netifd_process_log_read_cb;
178 ustream_fd_init(&proc->log, pfds[0]);
179
180 return 0;
181
182 error:
183 close(pfds[0]);
184 close(pfds[1]);
185 return -1;
186 }
187
188 void
189 netifd_kill_process(struct netifd_process *proc)
190 {
191 if (!proc->uloop.pending)
192 return;
193
194 kill(proc->uloop.pid, SIGKILL);
195 uloop_process_delete(&proc->uloop);
196 netifd_delete_process(proc);
197 }
198
199 static void netifd_do_restart(struct uloop_timeout *timeout)
200 {
201 execvp(global_argv[0], global_argv);
202 }
203
204 static void netifd_do_reload(struct uloop_timeout *timeout)
205 {
206 config_init_all();
207 }
208
209 static struct uloop_timeout main_timer;
210
211 void netifd_reload(void)
212 {
213 main_timer.cb = netifd_do_reload;
214 uloop_timeout_set(&main_timer, 100);
215 }
216
217 void netifd_restart(void)
218 {
219 main_timer.cb = netifd_do_restart;
220 interface_set_down(NULL);
221 uloop_timeout_set(&main_timer, 1000);
222 }
223
224 static int usage(const char *progname)
225 {
226 fprintf(stderr, "Usage: %s [options]\n"
227 "Options:\n"
228 " -d <mask>: Mask for debug messages\n"
229 " -s <path>: Path to the ubus socket\n"
230 " -p <path>: Path to netifd addons (default: %s)\n"
231 " -h <path>: Path to the hotplug script\n"
232 " -r <path>: Path to resolv.conf\n"
233 " -l <level>: Log output level (default: %d)\n"
234 " -S: Use stderr instead of syslog for log messages\n"
235 " (default: "DEFAULT_HOTPLUG_PATH")\n"
236 "\n", progname, main_path, DEFAULT_LOG_LEVEL);
237
238 return 1;
239 }
240
241 static void
242 netifd_handle_signal(int signo)
243 {
244 uloop_end();
245 }
246
247 static void
248 netifd_setup_signals(void)
249 {
250 struct sigaction s;
251
252 memset(&s, 0, sizeof(s));
253 s.sa_handler = netifd_handle_signal;
254 s.sa_flags = 0;
255 sigaction(SIGINT, &s, NULL);
256 sigaction(SIGTERM, &s, NULL);
257 sigaction(SIGUSR1, &s, NULL);
258 sigaction(SIGUSR2, &s, NULL);
259
260 s.sa_handler = SIG_IGN;
261 sigaction(SIGPIPE, &s, NULL);
262 }
263
264 static void
265 netifd_kill_processes(void)
266 {
267 struct netifd_process *proc, *tmp;
268
269 list_for_each_entry_safe(proc, tmp, &process_list, list)
270 netifd_kill_process(proc);
271 }
272
273 int main(int argc, char **argv)
274 {
275 const char *socket = NULL;
276 int ch;
277
278 global_argv = argv;
279
280 while ((ch = getopt(argc, argv, "d:s:p:h:r:l:S")) != -1) {
281 switch(ch) {
282 case 'd':
283 debug_mask = strtoul(optarg, NULL, 0);
284 break;
285 case 's':
286 socket = optarg;
287 break;
288 case 'p':
289 main_path = optarg;
290 break;
291 case 'h':
292 hotplug_cmd_path = optarg;
293 break;
294 case 'r':
295 resolv_conf = optarg;
296 break;
297 case 'l':
298 log_level = atoi(optarg);
299 if (log_level >= ARRAY_SIZE(log_class))
300 log_level = ARRAY_SIZE(log_class) - 1;
301 break;
302 #ifndef DUMMY_MODE
303 case 'S':
304 use_syslog = false;
305 break;
306 #endif
307 default:
308 return usage(argv[0]);
309 }
310 }
311
312 if (use_syslog)
313 openlog("netifd", 0, LOG_DAEMON);
314
315 netifd_setup_signals();
316 if (netifd_ubus_init(socket) < 0) {
317 fprintf(stderr, "Failed to connect to ubus\n");
318 return 1;
319 }
320
321 if (system_init()) {
322 fprintf(stderr, "Failed to initialize system control\n");
323 return 1;
324 }
325
326 config_init_all();
327
328 uloop_run();
329 netifd_kill_processes();
330
331 netifd_ubus_done();
332
333 if (use_syslog)
334 closelog();
335
336 return 0;
337 }