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