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