procd: guard fork_worker calls
[oweals/procd.git] / inittab.c
1 /*
2  * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3  * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License version 2.1
7  * as published by the Free Software Foundation
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #define _GNU_SOURCE
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <sys/ioctl.h>
19
20 #include <fcntl.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <regex.h>
25 #include <ctype.h>
26
27 #include <libubox/utils.h>
28 #include <libubox/list.h>
29
30 #include "utils/utils.h"
31 #include "procd.h"
32 #include "rcS.h"
33
34 #ifndef O_PATH
35 #define O_PATH          010000000
36 #endif
37
38 #define TAG_ID          0
39 #define TAG_RUNLVL      1
40 #define TAG_ACTION      2
41 #define TAG_PROCESS     3
42
43 #define MAX_ARGS        8
44
45 struct init_action;
46 char *console = NULL;
47
48 struct init_handler {
49         const char *name;
50         void (*cb) (struct init_action *a);
51         int multi;
52 };
53
54 struct init_action {
55         struct list_head list;
56
57         char *id;
58         char *argv[MAX_ARGS];
59         char *line;
60
61         struct init_handler *handler;
62         struct uloop_process proc;
63
64         int respawn;
65         struct uloop_timeout tout;
66 };
67
68 static const char *tab = "/etc/inittab";
69 static char *ask = "/sbin/askfirst";
70
71 static LIST_HEAD(actions);
72
73 static int dev_exist(const char *dev)
74 {
75         int dfd, fd;
76
77         dfd = open("/dev", O_PATH|O_DIRECTORY);
78
79         if (dfd < 0)
80                 return 0;
81
82         fd = openat(dfd, dev, O_RDONLY);
83         close(dfd);
84
85         if (fd < 0)
86                 return 0;
87
88         close(fd);
89         return 1;
90 }
91
92 static void fork_worker(struct init_action *a)
93 {
94         pid_t p;
95
96         a->proc.pid = fork();
97         if (!a->proc.pid) {
98                 p = setsid();
99
100                 if (patch_stdio(a->id))
101                         ERROR("Failed to setup i/o redirection\n");
102
103                 ioctl(STDIN_FILENO, TIOCSCTTY, 1);
104                 tcsetpgrp(STDIN_FILENO, p);
105
106                 execvp(a->argv[0], a->argv);
107                 ERROR("Failed to execute %s: %m\n", a->argv[0]);
108                 exit(-1);
109         }
110
111         if (a->proc.pid > 0) {
112                 DEBUG(4, "Launched new %s action, pid=%d\n",
113                                         a->handler->name,
114                                         (int) a->proc.pid);
115                 uloop_process_add(&a->proc);
116         }
117 }
118
119 static void child_exit(struct uloop_process *proc, int ret)
120 {
121         struct init_action *a = container_of(proc, struct init_action, proc);
122
123         DEBUG(4, "pid:%d, exitcode:%d\n", proc->pid, ret);
124         proc->pid = 0;
125
126         uloop_timeout_set(&a->tout, a->respawn);
127 }
128
129 static void respawn(struct uloop_timeout *tout)
130 {
131         struct init_action *a = container_of(tout, struct init_action, tout);
132         if (!a->proc.pid)
133                 fork_worker(a);
134 }
135
136 static void rcdone(struct runqueue *q)
137 {
138         procd_state_next();
139 }
140
141 static void runrc(struct init_action *a)
142 {
143         if (!a->argv[1] || !a->argv[2]) {
144                 ERROR("valid format is rcS <S|K> <param>\n");
145                 return;
146         }
147
148         /* proceed even if no init or shutdown scripts run */
149         if (rcS(a->argv[1], a->argv[2], rcdone))
150                 rcdone(NULL);
151 }
152
153 static void askfirst(struct init_action *a)
154 {
155         int i;
156
157         if (!dev_exist(a->id) || (console && !strcmp(console, a->id))) {
158                 DEBUG(4, "Skipping %s\n", a->id);
159                 return;
160         }
161
162         a->tout.cb = respawn;
163         for (i = MAX_ARGS - 1; i >= 1; i--)
164                 a->argv[i] = a->argv[i - 1];
165         a->argv[0] = ask;
166         a->respawn = 500;
167
168         a->proc.cb = child_exit;
169         if (!a->proc.pid)
170                 fork_worker(a);
171 }
172
173 static void askconsole(struct init_action *a)
174 {
175         char line[256], *tty, *split;
176         int i;
177
178         tty = get_cmdline_val("console", line, sizeof(line));
179         if (tty != NULL) {
180                 split = strchr(tty, ',');
181                 if (split != NULL)
182                         *split = '\0';
183
184                 if (!dev_exist(tty)) {
185                         DEBUG(4, "skipping %s\n", tty);
186                         return;
187                 }
188
189                 console = strdup(tty);
190                 a->id = strdup(tty);
191         }
192         else {
193                 console = NULL;
194                 a->id = NULL;
195         }
196
197         a->tout.cb = respawn;
198         for (i = MAX_ARGS - 1; i >= 1; i--)
199                 a->argv[i] = a->argv[i - 1];
200         a->argv[0] = ask;
201         a->respawn = 500;
202
203         a->proc.cb = child_exit;
204         if (!a->proc.pid)
205                 fork_worker(a);
206 }
207
208 static void rcrespawn(struct init_action *a)
209 {
210         a->tout.cb = respawn;
211         a->respawn = 500;
212
213         a->proc.cb = child_exit;
214         if (!a->proc.pid)
215                 fork_worker(a);
216 }
217
218 static struct init_handler handlers[] = {
219         {
220                 .name = "sysinit",
221                 .cb = runrc,
222         }, {
223                 .name = "shutdown",
224                 .cb = runrc,
225         }, {
226                 .name = "askfirst",
227                 .cb = askfirst,
228                 .multi = 1,
229         }, {
230                 .name = "askconsole",
231                 .cb = askconsole,
232                 .multi = 1,
233         }, {
234                 .name = "respawn",
235                 .cb = rcrespawn,
236                 .multi = 1,
237         }, {
238                 .name = "askconsolelate",
239                 .cb = askconsole,
240                 .multi = 1,
241         }, {
242                 .name = "respawnlate",
243                 .cb = rcrespawn,
244                 .multi = 1,
245         }
246 };
247
248 static int add_action(struct init_action *a, const char *name)
249 {
250         int i;
251
252         for (i = 0; i < ARRAY_SIZE(handlers); i++)
253                 if (!strcmp(handlers[i].name, name)) {
254                         a->handler = &handlers[i];
255                         list_add_tail(&a->list, &actions);
256                         return 0;
257                 }
258         ERROR("Unknown init handler %s\n", name);
259         return -1;
260 }
261
262 void procd_inittab_run(const char *handler)
263 {
264         struct init_action *a;
265
266         list_for_each_entry(a, &actions, list)
267                 if (!strcmp(a->handler->name, handler)) {
268                         if (a->handler->multi) {
269                                 a->handler->cb(a);
270                                 continue;
271                         }
272                         a->handler->cb(a);
273                         break;
274                 }
275 }
276
277 void procd_inittab(void)
278 {
279 #define LINE_LEN        128
280         FILE *fp = fopen(tab, "r");
281         struct init_action *a;
282         regex_t pat_inittab;
283         regmatch_t matches[5];
284         char *line;
285
286         if (!fp) {
287                 ERROR("Failed to open %s: %m\n", tab);
288                 return;
289         }
290
291         regcomp(&pat_inittab, "([a-zA-Z0-9]*):([a-zA-Z0-9]*):([a-zA-Z0-9]*):(.*)", REG_EXTENDED);
292         line = malloc(LINE_LEN);
293         a = calloc(1, sizeof(struct init_action));
294
295         while (fgets(line, LINE_LEN, fp)) {
296                 char *tags[TAG_PROCESS + 1];
297                 char *tok;
298                 int i;
299                 int len = strlen(line);
300
301                 while (isspace(line[len - 1]))
302                         len--;
303                 line[len] = 0;
304
305                 if (*line == '#')
306                         continue;
307
308                 if (regexec(&pat_inittab, line, 5, matches, 0))
309                         continue;
310
311                 DEBUG(4, "Parsing inittab - %s\n", line);
312
313                 for (i = TAG_ID; i <= TAG_PROCESS; i++) {
314                         line[matches[i].rm_eo] = '\0';
315                         tags[i] = &line[matches[i + 1].rm_so];
316                 };
317
318                 tok = strtok(tags[TAG_PROCESS], " ");
319                 for (i = 0; i < (MAX_ARGS - 1) && tok; i++) {
320                         a->argv[i] = tok;
321                         tok = strtok(NULL, " ");
322                 }
323                 a->argv[i] = NULL;
324                 a->id = tags[TAG_ID];
325                 a->line = line;
326
327                 if (add_action(a, tags[TAG_ACTION]))
328                         continue;
329                 line = malloc(LINE_LEN);
330                 a = calloc(1, sizeof(struct init_action));
331         }
332
333         fclose(fp);
334         free(line);
335         free(a);
336         regfree(&pat_inittab);
337 }