add copyright headers
[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 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 struct netifd_fd *fd;
171 int pfds[2];
172 int pid;
173
174 netifd_kill_process(proc);
175
176 if (pipe(pfds) < 0)
177 return -1;
178
179 if ((pid = fork()) < 0)
180 goto error;
181
182 if (!pid) {
183 if (env) {
184 while (*env) {
185 putenv(*env);
186 env++;
187 }
188 }
189 if (proc->dir_fd >= 0)
190 fchdir(proc->dir_fd);
191
192 /* close all non-essential fds */
193 list_for_each_entry(fd, &fds, list) {
194 if (fd->proc == proc)
195 continue;
196 close(fd->fd);
197 }
198
199 dup2(pfds[1], 0);
200 dup2(pfds[1], 1);
201 dup2(pfds[1], 2);
202
203 close(pfds[0]);
204 close(pfds[1]);
205
206 execvp(argv[0], (char **) argv);
207 exit(127);
208 }
209
210 if (pid < 0)
211 goto error;
212
213 close(pfds[1]);
214 proc->uloop.cb = netifd_process_cb;
215 proc->uloop.pid = pid;
216 uloop_process_add(&proc->uloop);
217 list_add_tail(&proc->list, &process_list);
218
219 proc->log_buf_ofs = 0;
220 proc->log_uloop.fd = proc->log_fd.fd = pfds[0];
221 proc->log_uloop.cb = netifd_process_log_cb;
222 netifd_fd_add(&proc->log_fd);
223 uloop_fd_add(&proc->log_uloop, ULOOP_EDGE_TRIGGER | ULOOP_READ);
224
225 return 0;
226
227 error:
228 close(pfds[0]);
229 close(pfds[1]);
230 return -1;
231 }
232
233 void
234 netifd_kill_process(struct netifd_process *proc)
235 {
236 if (!proc->uloop.pending)
237 return;
238
239 kill(proc->uloop.pid, SIGKILL);
240 netifd_delete_process(proc);
241 }
242
243 void
244 netifd_fd_add(struct netifd_fd *fd)
245 {
246 list_add_tail(&fd->list, &fds);
247 }
248
249 void
250 netifd_fd_delete(struct netifd_fd *fd)
251 {
252 list_del(&fd->list);
253 }
254
255 static void netifd_do_restart(struct uloop_timeout *timeout)
256 {
257 execvp(global_argv[0], global_argv);
258 }
259
260 static void netifd_do_reload(struct uloop_timeout *timeout)
261 {
262 config_init_all();
263 }
264
265 static struct uloop_timeout main_timer;
266
267 void netifd_reload(void)
268 {
269 main_timer.cb = netifd_do_reload;
270 uloop_timeout_set(&main_timer, 100);
271 }
272
273 void netifd_restart(void)
274 {
275 main_timer.cb = netifd_do_restart;
276 interface_set_down(NULL);
277 uloop_timeout_set(&main_timer, 1000);
278 }
279
280 static int usage(const char *progname)
281 {
282 fprintf(stderr, "Usage: %s [options]\n"
283 "Options:\n"
284 " -d <mask>: Mask for debug messages\n"
285 " -s <path>: Path to the ubus socket\n"
286 " -p <path>: Path to netifd addons (default: %s)\n"
287 " -h <path>: Path to the hotplug script\n"
288 " -r <path>: Path to resolv.conf\n"
289 " -l <level>: Log output level (default: %d)\n"
290 " -S: Use stderr instead of syslog for log messages\n"
291 " (default: "DEFAULT_HOTPLUG_PATH")\n"
292 "\n", progname, main_path, DEFAULT_LOG_LEVEL);
293
294 return 1;
295 }
296
297 static void
298 netifd_handle_signal(int signo)
299 {
300 uloop_end();
301 }
302
303 static void
304 netifd_setup_signals(void)
305 {
306 struct sigaction s;
307
308 memset(&s, 0, sizeof(s));
309 s.sa_handler = netifd_handle_signal;
310 s.sa_flags = 0;
311 sigaction(SIGINT, &s, NULL);
312 sigaction(SIGTERM, &s, NULL);
313 sigaction(SIGUSR1, &s, NULL);
314 sigaction(SIGUSR2, &s, NULL);
315
316 s.sa_handler = SIG_IGN;
317 sigaction(SIGPIPE, &s, NULL);
318 }
319
320 static void
321 netifd_kill_processes(void)
322 {
323 struct netifd_process *proc, *tmp;
324
325 list_for_each_entry_safe(proc, tmp, &process_list, list)
326 netifd_kill_process(proc);
327 }
328
329 int main(int argc, char **argv)
330 {
331 const char *socket = NULL;
332 int ch;
333
334 global_argv = argv;
335
336 while ((ch = getopt(argc, argv, "d:s:p:h:r:l:S")) != -1) {
337 switch(ch) {
338 case 'd':
339 debug_mask = strtoul(optarg, NULL, 0);
340 break;
341 case 's':
342 socket = optarg;
343 break;
344 case 'p':
345 main_path = optarg;
346 break;
347 case 'h':
348 hotplug_cmd_path = optarg;
349 break;
350 case 'r':
351 resolv_conf = optarg;
352 break;
353 case 'l':
354 log_level = atoi(optarg);
355 if (log_level >= ARRAY_SIZE(log_class))
356 log_level = ARRAY_SIZE(log_class) - 1;
357 break;
358 #ifndef DUMMY_MODE
359 case 'S':
360 use_syslog = false;
361 break;
362 #endif
363 default:
364 return usage(argv[0]);
365 }
366 }
367
368 if (use_syslog)
369 openlog("netifd", 0, LOG_DAEMON);
370
371 netifd_setup_signals();
372 if (netifd_ubus_init(socket) < 0) {
373 fprintf(stderr, "Failed to connect to ubus\n");
374 return 1;
375 }
376
377 if (system_init()) {
378 fprintf(stderr, "Failed to initialize system control\n");
379 return 1;
380 }
381
382 config_init_all();
383
384 uloop_run();
385 netifd_kill_processes();
386
387 netifd_ubus_done();
388
389 if (use_syslog)
390 closelog();
391
392 return 0;
393 }