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