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