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