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