simplify log buffer handling
[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         len = proc->log_buf_ofs;
103         buf = proc->log_buf;
104         while (len > 0) {
105                 cur = memchr(buf, '\n', len);
106                 if (!cur)
107                         break;
108
109                 *cur = 0;
110
111                 if (!proc->log_overflow)
112                         netifd_log_message(L_NOTICE, "%s (%d): %s\n",
113                                 log_prefix, proc->uloop.pid, buf);
114                 else
115                         proc->log_overflow = false;
116
117                 cur++;
118                 len -= cur - buf;
119                 buf = cur;
120         }
121
122         if (buf > proc->log_buf && len > 0)
123                 memmove(proc->log_buf, buf, len);
124
125         if (len == LOG_BUF_SIZE) {
126                 if (!proc->log_overflow) {
127                         proc->log_buf[LOG_BUF_SIZE] = 0;
128                         netifd_log_message(L_NOTICE, "%s (%d): %s [...]\n",
129                                 log_prefix, proc->uloop.pid, proc->log_buf);
130                         proc->log_overflow = true;
131                 }
132                 len = 0;
133         }
134         proc->log_buf_ofs = len;
135
136         if (read_len > 0)
137                 goto retry;
138
139 out:
140         if (fd->eof)
141                 uloop_fd_delete(fd);
142 }
143
144 static void
145 netifd_process_cb(struct uloop_process *proc, int ret)
146 {
147         struct netifd_process *np;
148         np = container_of(proc, struct netifd_process, uloop);
149         netifd_process_log_cb(&np->log_uloop, 0);
150         netifd_delete_process(np);
151         return np->cb(np, ret);
152 }
153
154 int
155 netifd_start_process(const char **argv, char **env, struct netifd_process *proc)
156 {
157         struct netifd_fd *fd;
158         int pfds[2];
159         int pid;
160
161         netifd_kill_process(proc);
162
163         if (pipe(pfds) < 0)
164                 return -1;
165
166         if ((pid = fork()) < 0)
167                 goto error;
168
169         if (!pid) {
170                 if (env) {
171                         while (*env) {
172                                 putenv(*env);
173                                 env++;
174                         }
175                 }
176                 if (proc->dir_fd >= 0)
177                         fchdir(proc->dir_fd);
178
179                 /* close all non-essential fds */
180                 list_for_each_entry(fd, &fds, list) {
181                         if (fd->proc == proc)
182                                 continue;
183                         close(fd->fd);
184                 }
185
186                 dup2(pfds[1], 0);
187                 dup2(pfds[1], 1);
188                 dup2(pfds[1], 2);
189
190                 close(pfds[0]);
191                 close(pfds[1]);
192
193                 execvp(argv[0], (char **) argv);
194                 exit(127);
195         }
196
197         if (pid < 0)
198                 goto error;
199
200         close(pfds[1]);
201         proc->uloop.cb = netifd_process_cb;
202         proc->uloop.pid = pid;
203         uloop_process_add(&proc->uloop);
204         list_add_tail(&proc->list, &process_list);
205
206         proc->log_buf_ofs = 0;
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, SIGKILL);
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_all();
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_all();
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 }