netifd: Do not return values in void function
[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 #include "wireless.h"
28 #include "proto.h"
29 #include "extdev.h"
30
31 unsigned int debug_mask = 0;
32 const char *main_path = DEFAULT_MAIN_PATH;
33 const char *config_path = DEFAULT_CONFIG_PATH;
34 const char *resolv_conf = DEFAULT_RESOLV_CONF;
35 static char **global_argv;
36
37 static struct list_head process_list = LIST_HEAD_INIT(process_list);
38
39 #define DEFAULT_LOG_LEVEL L_NOTICE
40
41 static int log_level = DEFAULT_LOG_LEVEL;
42 static const int log_class[] = {
43 [L_CRIT] = LOG_CRIT,
44 [L_WARNING] = LOG_WARNING,
45 [L_NOTICE] = LOG_NOTICE,
46 [L_INFO] = LOG_INFO,
47 [L_DEBUG] = LOG_DEBUG
48 };
49
50 #ifdef DUMMY_MODE
51 #define use_syslog false
52 #else
53 static bool use_syslog = true;
54 #endif
55
56
57 static void
58 netifd_delete_process(struct netifd_process *proc)
59 {
60 while (ustream_poll(&proc->log.stream));
61 list_del(&proc->list);
62 ustream_free(&proc->log.stream);
63 close(proc->log.fd.fd);
64 }
65
66 void
67 netifd_log_message(int priority, const char *format, ...)
68 {
69 va_list vl;
70
71 if (priority > log_level)
72 return;
73
74 va_start(vl, format);
75 if (use_syslog)
76 vsyslog(log_class[priority], format, vl);
77 else
78 vfprintf(stderr, format, vl);
79 va_end(vl);
80 }
81
82 static void
83 netifd_process_log_read_cb(struct ustream *s, int bytes)
84 {
85 struct netifd_process *proc;
86 const char *log_prefix;
87 char *data;
88 int len = 0;
89
90 proc = container_of(s, struct netifd_process, log.stream);
91 log_prefix = proc->log_prefix;
92 if (!log_prefix)
93 log_prefix = "process";
94
95 do {
96 char *newline;
97
98 data = ustream_get_read_buf(s, &len);
99 if (!len)
100 break;
101
102 newline = strchr(data, '\n');
103
104 if (proc->log_overflow) {
105 if (newline) {
106 len = newline + 1 - data;
107 proc->log_overflow = false;
108 }
109 } else if (newline) {
110 *newline = 0;
111 len = newline + 1 - data;
112 netifd_log_message(L_NOTICE, "%s (%d): %s\n",
113 log_prefix, proc->uloop.pid, data);
114 } else if (len == s->r.buffer_len) {
115 netifd_log_message(L_NOTICE, "%s (%d): %s [...]\n",
116 log_prefix, proc->uloop.pid, data);
117 proc->log_overflow = true;
118 } else
119 break;
120
121 ustream_consume(s, len);
122 } while (1);
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 netifd_delete_process(np);
132 np->cb(np, ret);
133 return;
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 int i;
152
153 if (env) {
154 while (*env) {
155 putenv(*env);
156 env++;
157 }
158 }
159 if (proc->dir_fd >= 0)
160 if (fchdir(proc->dir_fd)) {}
161
162 close(pfds[0]);
163
164 for (i = 0; i <= 2; i++) {
165 if (pfds[1] == i)
166 continue;
167
168 dup2(pfds[1], i);
169 }
170
171 if (pfds[1] > 2)
172 close(pfds[1]);
173
174 execvp(argv[0], (char **) argv);
175 exit(127);
176 }
177
178 close(pfds[1]);
179 proc->uloop.cb = netifd_process_cb;
180 proc->uloop.pid = pid;
181 uloop_process_add(&proc->uloop);
182 list_add_tail(&proc->list, &process_list);
183
184 system_fd_set_cloexec(pfds[0]);
185 proc->log.stream.string_data = true;
186 proc->log.stream.notify_read = netifd_process_log_read_cb;
187 ustream_fd_init(&proc->log, pfds[0]);
188
189 return 0;
190
191 error:
192 close(pfds[0]);
193 close(pfds[1]);
194 return -1;
195 }
196
197 void
198 netifd_kill_process(struct netifd_process *proc)
199 {
200 if (!proc->uloop.pending)
201 return;
202
203 kill(proc->uloop.pid, SIGKILL);
204 uloop_process_delete(&proc->uloop);
205 netifd_delete_process(proc);
206 }
207
208 static void netifd_do_restart(struct uloop_timeout *timeout)
209 {
210 execvp(global_argv[0], global_argv);
211 }
212
213 int netifd_reload(void)
214 {
215 return config_init_all();
216 }
217
218 void netifd_restart(void)
219 {
220 static struct uloop_timeout main_timer = {
221 .cb = netifd_do_restart
222 };
223
224 interface_set_down(NULL);
225 uloop_timeout_set(&main_timer, 1000);
226 }
227
228 static int usage(const char *progname)
229 {
230 fprintf(stderr, "Usage: %s [options]\n"
231 "Options:\n"
232 " -d <mask>: Mask for debug messages\n"
233 " -s <path>: Path to the ubus socket\n"
234 " -p <path>: Path to netifd addons (default: %s)\n"
235 " -c <path>: Path to UCI configuration\n"
236 " -h <path>: Path to the hotplug script\n"
237 " (default: "DEFAULT_HOTPLUG_PATH")\n"
238 " -r <path>: Path to resolv.conf\n"
239 " -l <level>: Log output level (default: %d)\n"
240 " -S: Use stderr instead of syslog for log messages\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:c: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 'c':
297 config_path = optarg;
298 break;
299 case 'h':
300 hotplug_cmd_path = optarg;
301 break;
302 case 'r':
303 resolv_conf = optarg;
304 break;
305 case 'l':
306 log_level = atoi(optarg);
307 if (log_level >= (int)ARRAY_SIZE(log_class))
308 log_level = (int)ARRAY_SIZE(log_class) - 1;
309 break;
310 #ifndef DUMMY_MODE
311 case 'S':
312 use_syslog = false;
313 break;
314 #endif
315 default:
316 return usage(argv[0]);
317 }
318 }
319
320 if (use_syslog)
321 openlog("netifd", 0, LOG_DAEMON);
322
323 netifd_setup_signals();
324 if (netifd_ubus_init(socket) < 0) {
325 fprintf(stderr, "Failed to connect to ubus\n");
326 return 1;
327 }
328
329 proto_shell_init();
330 extdev_init();
331 wireless_init();
332
333 if (system_init()) {
334 fprintf(stderr, "Failed to initialize system control\n");
335 return 1;
336 }
337
338 config_init_all();
339
340 uloop_run();
341 netifd_kill_processes();
342
343 netifd_ubus_done();
344
345 if (use_syslog)
346 closelog();
347
348 return 0;
349 }