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