Tunnel don't fragment bit configurable
[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                 int i;
147
148                 if (env) {
149                         while (*env) {
150                                 putenv(*env);
151                                 env++;
152                         }
153                 }
154                 if (proc->dir_fd >= 0)
155                         fchdir(proc->dir_fd);
156
157                 close(pfds[0]);
158
159                 for (i = 0; i <= 2; i++) {
160                         if (pfds[1] == i)
161                                 continue;
162
163                         dup2(pfds[1], i);
164                 }
165
166                 if (pfds[1] > 2)
167                         close(pfds[1]);
168
169                 execvp(argv[0], (char **) argv);
170                 exit(127);
171         }
172
173         if (pid < 0)
174                 goto error;
175
176         close(pfds[1]);
177         proc->uloop.cb = netifd_process_cb;
178         proc->uloop.pid = pid;
179         uloop_process_add(&proc->uloop);
180         list_add_tail(&proc->list, &process_list);
181
182         system_fd_set_cloexec(pfds[0]);
183         proc->log.stream.string_data = true;
184         proc->log.stream.notify_read = netifd_process_log_read_cb;
185         ustream_fd_init(&proc->log, pfds[0]);
186
187         return 0;
188
189 error:
190         close(pfds[0]);
191         close(pfds[1]);
192         return -1;
193 }
194
195 void
196 netifd_kill_process(struct netifd_process *proc)
197 {
198         if (!proc->uloop.pending)
199                 return;
200
201         kill(proc->uloop.pid, SIGKILL);
202         uloop_process_delete(&proc->uloop);
203         netifd_delete_process(proc);
204 }
205
206 static void netifd_do_restart(struct uloop_timeout *timeout)
207 {
208         execvp(global_argv[0], global_argv);
209 }
210
211 static void netifd_do_reload(struct uloop_timeout *timeout)
212 {
213         config_init_all();
214 }
215
216 static struct uloop_timeout main_timer;
217
218 void netifd_reload(void)
219 {
220         main_timer.cb = netifd_do_reload;
221         uloop_timeout_set(&main_timer, 100);
222 }
223
224 void netifd_restart(void)
225 {
226         main_timer.cb = netifd_do_restart;
227         interface_set_down(NULL);
228         uloop_timeout_set(&main_timer, 1000);
229 }
230
231 static int usage(const char *progname)
232 {
233         fprintf(stderr, "Usage: %s [options]\n"
234                 "Options:\n"
235                 " -d <mask>:            Mask for debug messages\n"
236                 " -s <path>:            Path to the ubus socket\n"
237                 " -p <path>:            Path to netifd addons (default: %s)\n"
238                 " -h <path>:            Path to the hotplug script\n"
239                 " -r <path>:            Path to resolv.conf\n"
240                 " -l <level>:           Log output level (default: %d)\n"
241                 " -S:                   Use stderr instead of syslog for log messages\n"
242                 "                       (default: "DEFAULT_HOTPLUG_PATH")\n"
243                 "\n", progname, main_path, DEFAULT_LOG_LEVEL);
244
245         return 1;
246 }
247
248 static void
249 netifd_handle_signal(int signo)
250 {
251         uloop_end();
252 }
253
254 static void
255 netifd_setup_signals(void)
256 {
257         struct sigaction s;
258
259         memset(&s, 0, sizeof(s));
260         s.sa_handler = netifd_handle_signal;
261         s.sa_flags = 0;
262         sigaction(SIGINT, &s, NULL);
263         sigaction(SIGTERM, &s, NULL);
264         sigaction(SIGUSR1, &s, NULL);
265         sigaction(SIGUSR2, &s, NULL);
266
267         s.sa_handler = SIG_IGN;
268         sigaction(SIGPIPE, &s, NULL);
269 }
270
271 static void
272 netifd_kill_processes(void)
273 {
274         struct netifd_process *proc, *tmp;
275
276         list_for_each_entry_safe(proc, tmp, &process_list, list)
277                 netifd_kill_process(proc);
278 }
279
280 int main(int argc, char **argv)
281 {
282         const char *socket = NULL;
283         int ch;
284
285         global_argv = argv;
286
287         while ((ch = getopt(argc, argv, "d:s:p:h:r:l:S")) != -1) {
288                 switch(ch) {
289                 case 'd':
290                         debug_mask = strtoul(optarg, NULL, 0);
291                         break;
292                 case 's':
293                         socket = optarg;
294                         break;
295                 case 'p':
296                         main_path = optarg;
297                         break;
298                 case 'h':
299                         hotplug_cmd_path = optarg;
300                         break;
301                 case 'r':
302                         resolv_conf = optarg;
303                         break;
304                 case 'l':
305                         log_level = atoi(optarg);
306                         if (log_level >= ARRAY_SIZE(log_class))
307                                 log_level = ARRAY_SIZE(log_class) - 1;
308                         break;
309 #ifndef DUMMY_MODE
310                 case 'S':
311                         use_syslog = false;
312                         break;
313 #endif
314                 default:
315                         return usage(argv[0]);
316                 }
317         }
318
319         if (use_syslog)
320                 openlog("netifd", 0, LOG_DAEMON);
321
322         netifd_setup_signals();
323         if (netifd_ubus_init(socket) < 0) {
324                 fprintf(stderr, "Failed to connect to ubus\n");
325                 return 1;
326         }
327
328         if (system_init()) {
329                 fprintf(stderr, "Failed to initialize system control\n");
330                 return 1;
331         }
332
333         config_init_all();
334
335         uloop_run();
336         netifd_kill_processes();
337
338         netifd_ubus_done();
339
340         if (use_syslog)
341                 closelog();
342
343         return 0;
344 }