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