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