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