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