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