4ee86ebe642530ef2f6d605e10bc000eb38e224b
[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 <unistd.h>
19 #include <signal.h>
20 #include <stdarg.h>
21 #include <syslog.h>
22
23 #include "netifd.h"
24 #include "ubus.h"
25 #include "config.h"
26 #include "system.h"
27 #include "interface.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 static struct list_head fds = LIST_HEAD_INIT(fds);
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         if (proc->uloop.pending)
59                 uloop_process_delete(&proc->uloop);
60         list_del(&proc->list);
61         netifd_fd_delete(&proc->log_fd);
62         close(proc->log_fd.fd);
63         if (proc->log_buf) {
64                 free(proc->log_buf);
65                 proc->log_buf = NULL;
66         }
67 }
68
69 void
70 netifd_log_message(int priority, const char *format, ...)
71 {
72         va_list vl;
73
74         if (priority > log_level)
75                 return;
76
77         va_start(vl, format);
78         if (use_syslog)
79                 vsyslog(log_class[priority], format, vl);
80         else
81                 vfprintf(stderr, format, vl);
82         va_end(vl);
83 }
84
85 static void
86 netifd_process_log_cb(struct uloop_fd *fd, unsigned int events)
87 {
88         struct netifd_process *proc;
89         const char *log_prefix;
90         char *buf, *cur;
91         int maxlen, len, read_len;
92
93         proc = container_of(fd, struct netifd_process, log_uloop);
94
95         if (!proc->log_buf)
96                 proc->log_buf = malloc(LOG_BUF_SIZE + 1);
97
98         log_prefix = proc->log_prefix;
99         if (!log_prefix)
100                 log_prefix = "process";
101
102 retry:
103         buf = proc->log_buf + proc->log_buf_ofs;
104         maxlen = LOG_BUF_SIZE - proc->log_buf_ofs;
105         read_len = len = read(fd->fd, buf, maxlen);
106         if (len < 0) {
107                 if (errno == EINTR)
108                         goto retry;
109
110                 goto out;
111         } else if (len == 0)
112                 goto out;
113
114         proc->log_buf_ofs += len;
115
116         len = proc->log_buf_ofs;
117         buf = proc->log_buf;
118         while (len > 0) {
119                 cur = memchr(buf, '\n', len);
120                 if (!cur)
121                         break;
122
123                 *cur = 0;
124
125                 if (!proc->log_overflow)
126                         netifd_log_message(L_NOTICE, "%s (%d): %s\n",
127                                 log_prefix, proc->uloop.pid, buf);
128                 else
129                         proc->log_overflow = false;
130
131                 cur++;
132                 len -= cur - buf;
133                 buf = cur;
134         }
135
136         if (buf > proc->log_buf && len > 0)
137                 memmove(proc->log_buf, buf, len);
138
139         if (len == LOG_BUF_SIZE) {
140                 if (!proc->log_overflow) {
141                         proc->log_buf[LOG_BUF_SIZE] = 0;
142                         netifd_log_message(L_NOTICE, "%s (%d): %s [...]\n",
143                                 log_prefix, proc->uloop.pid, proc->log_buf);
144                         proc->log_overflow = true;
145                 }
146                 len = 0;
147         }
148         proc->log_buf_ofs = len;
149
150         if (read_len > 0)
151                 goto retry;
152
153 out:
154         if (fd->eof)
155                 uloop_fd_delete(fd);
156 }
157
158 static void
159 netifd_process_cb(struct uloop_process *proc, int ret)
160 {
161         struct netifd_process *np;
162         np = container_of(proc, struct netifd_process, uloop);
163         netifd_process_log_cb(&np->log_uloop, 0);
164         netifd_delete_process(np);
165         return np->cb(np, ret);
166 }
167
168 int
169 netifd_start_process(const char **argv, char **env, struct netifd_process *proc)
170 {
171         struct netifd_fd *fd;
172         int pfds[2];
173         int pid;
174
175         netifd_kill_process(proc);
176
177         if (pipe(pfds) < 0)
178                 return -1;
179
180         if ((pid = fork()) < 0)
181                 goto error;
182
183         if (!pid) {
184                 if (env) {
185                         while (*env) {
186                                 putenv(*env);
187                                 env++;
188                         }
189                 }
190                 if (proc->dir_fd >= 0)
191                         fchdir(proc->dir_fd);
192
193                 /* close all non-essential fds */
194                 list_for_each_entry(fd, &fds, list) {
195                         if (fd->proc == proc)
196                                 continue;
197                         close(fd->fd);
198                 }
199
200                 dup2(pfds[1], 0);
201                 dup2(pfds[1], 1);
202                 dup2(pfds[1], 2);
203
204                 close(pfds[0]);
205                 close(pfds[1]);
206
207                 execvp(argv[0], (char **) argv);
208                 exit(127);
209         }
210
211         if (pid < 0)
212                 goto error;
213
214         close(pfds[1]);
215         proc->uloop.cb = netifd_process_cb;
216         proc->uloop.pid = pid;
217         uloop_process_add(&proc->uloop);
218         list_add_tail(&proc->list, &process_list);
219
220         proc->log_buf_ofs = 0;
221         proc->log_uloop.fd = proc->log_fd.fd = pfds[0];
222         proc->log_uloop.cb = netifd_process_log_cb;
223         netifd_fd_add(&proc->log_fd);
224         uloop_fd_add(&proc->log_uloop, ULOOP_EDGE_TRIGGER | ULOOP_READ);
225
226         return 0;
227
228 error:
229         close(pfds[0]);
230         close(pfds[1]);
231         return -1;
232 }
233
234 void
235 netifd_kill_process(struct netifd_process *proc)
236 {
237         if (!proc->uloop.pending)
238                 return;
239
240         kill(proc->uloop.pid, SIGKILL);
241         netifd_delete_process(proc);
242 }
243
244 void
245 netifd_fd_add(struct netifd_fd *fd)
246 {
247         list_add_tail(&fd->list, &fds);
248 }
249
250 void
251 netifd_fd_delete(struct netifd_fd *fd)
252 {
253         list_del(&fd->list);
254 }
255
256 static void netifd_do_restart(struct uloop_timeout *timeout)
257 {
258         execvp(global_argv[0], global_argv);
259 }
260
261 static void netifd_do_reload(struct uloop_timeout *timeout)
262 {
263         config_init_all();
264 }
265
266 static struct uloop_timeout main_timer;
267
268 void netifd_reload(void)
269 {
270         main_timer.cb = netifd_do_reload;
271         uloop_timeout_set(&main_timer, 100);
272 }
273
274 void netifd_restart(void)
275 {
276         main_timer.cb = netifd_do_restart;
277         interface_set_down(NULL);
278         uloop_timeout_set(&main_timer, 1000);
279 }
280
281 static int usage(const char *progname)
282 {
283         fprintf(stderr, "Usage: %s [options]\n"
284                 "Options:\n"
285                 " -d <mask>:            Mask for debug messages\n"
286                 " -s <path>:            Path to the ubus socket\n"
287                 " -p <path>:            Path to netifd addons (default: %s)\n"
288                 " -h <path>:            Path to the hotplug script\n"
289                 " -r <path>:            Path to resolv.conf\n"
290                 " -l <level>:           Log output level (default: %d)\n"
291                 " -S:                   Use stderr instead of syslog for log messages\n"
292                 "                       (default: "DEFAULT_HOTPLUG_PATH")\n"
293                 "\n", progname, main_path, DEFAULT_LOG_LEVEL);
294
295         return 1;
296 }
297
298 static void
299 netifd_handle_signal(int signo)
300 {
301         uloop_end();
302 }
303
304 static void
305 netifd_setup_signals(void)
306 {
307         struct sigaction s;
308
309         memset(&s, 0, sizeof(s));
310         s.sa_handler = netifd_handle_signal;
311         s.sa_flags = 0;
312         sigaction(SIGINT, &s, NULL);
313         sigaction(SIGTERM, &s, NULL);
314         sigaction(SIGUSR1, &s, NULL);
315         sigaction(SIGUSR2, &s, NULL);
316
317         s.sa_handler = SIG_IGN;
318         sigaction(SIGPIPE, &s, NULL);
319 }
320
321 static void
322 netifd_kill_processes(void)
323 {
324         struct netifd_process *proc, *tmp;
325
326         list_for_each_entry_safe(proc, tmp, &process_list, list)
327                 netifd_kill_process(proc);
328 }
329
330 int main(int argc, char **argv)
331 {
332         const char *socket = NULL;
333         int ch;
334
335         global_argv = argv;
336
337         while ((ch = getopt(argc, argv, "d:s:p:h:r:l:S")) != -1) {
338                 switch(ch) {
339                 case 'd':
340                         debug_mask = strtoul(optarg, NULL, 0);
341                         break;
342                 case 's':
343                         socket = optarg;
344                         break;
345                 case 'p':
346                         main_path = optarg;
347                         break;
348                 case 'h':
349                         hotplug_cmd_path = optarg;
350                         break;
351                 case 'r':
352                         resolv_conf = optarg;
353                         break;
354                 case 'l':
355                         log_level = atoi(optarg);
356                         if (log_level >= ARRAY_SIZE(log_class))
357                                 log_level = ARRAY_SIZE(log_class) - 1;
358                         break;
359 #ifndef DUMMY_MODE
360                 case 'S':
361                         use_syslog = false;
362                         break;
363 #endif
364                 default:
365                         return usage(argv[0]);
366                 }
367         }
368
369         if (use_syslog)
370                 openlog("netifd", 0, LOG_DAEMON);
371
372         netifd_setup_signals();
373         if (netifd_ubus_init(socket) < 0) {
374                 fprintf(stderr, "Failed to connect to ubus\n");
375                 return 1;
376         }
377
378         if (system_init()) {
379                 fprintf(stderr, "Failed to initialize system control\n");
380                 return 1;
381         }
382
383         config_init_all();
384
385         uloop_run();
386         netifd_kill_processes();
387
388         netifd_ubus_done();
389
390         if (use_syslog)
391                 closelog();
392
393         return 0;
394 }