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