2c2270cf68d367bcb882e0467d2bfd2456111daa
[project/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 if (!dev_exist(a->id)) {
127 DEBUG(4, "Skipping respawn: device '%s' does not exist anymore\n", a->id);
128 return;
129 }
130
131 uloop_timeout_set(&a->tout, a->respawn);
132 }
133
134 static void respawn(struct uloop_timeout *tout)
135 {
136 struct init_action *a = container_of(tout, struct init_action, tout);
137 if (!a->proc.pid)
138 fork_worker(a);
139 }
140
141 static void rcdone(struct runqueue *q)
142 {
143 procd_state_next();
144 }
145
146 static void runrc(struct init_action *a)
147 {
148 if (!a->argv[1] || !a->argv[2]) {
149 ERROR("valid format is rcS <S|K> <param>\n");
150 return;
151 }
152
153 /* proceed even if no init or shutdown scripts run */
154 if (rcS(a->argv[1], a->argv[2], rcdone))
155 rcdone(NULL);
156 }
157
158 static void askfirst(struct init_action *a)
159 {
160 int i;
161
162 if (!dev_exist(a->id) || (console && !strcmp(console, a->id))) {
163 DEBUG(4, "Skipping %s\n", a->id);
164 return;
165 }
166
167 a->tout.cb = respawn;
168 /* shift arguments only if not yet done */
169 if (a->argv[0] != ask) {
170 for (i = MAX_ARGS - 1; i >= 1; i--)
171 a->argv[i] = a->argv[i - 1];
172 a->argv[0] = ask;
173 }
174 a->respawn = 500;
175
176 a->proc.cb = child_exit;
177 if (!a->proc.pid)
178 fork_worker(a);
179 }
180
181 static void askconsole(struct init_action *a)
182 {
183 char line[256], *tty, *split;
184 int i;
185
186 /* First, try console= on the kernel command line,
187 * then fallback to /sys/class/tty/console/active,
188 * which should work when linux,stdout-path (or equivalent)
189 * is in the device tree
190 */
191 tty = get_cmdline_val("console", line, sizeof(line));
192 if (tty == NULL) {
193 tty = get_active_console(line, sizeof(line));
194 }
195 if (tty != NULL) {
196 split = strchr(tty, ',');
197 if (split != NULL)
198 *split = '\0';
199
200 if (!dev_exist(tty)) {
201 DEBUG(4, "skipping %s\n", tty);
202 return;
203 }
204
205 console = strdup(tty);
206 a->id = strdup(tty);
207 }
208 else {
209 console = NULL;
210 a->id = NULL;
211 }
212
213 a->tout.cb = respawn;
214 /* shift arguments only if not yet done */
215 if (a->argv[0] != ask) {
216 for (i = MAX_ARGS - 1; i >= 1; i--)
217 a->argv[i] = a->argv[i - 1];
218 a->argv[0] = ask;
219 }
220 a->respawn = 500;
221
222 a->proc.cb = child_exit;
223 if (!a->proc.pid)
224 fork_worker(a);
225 }
226
227 static void rcrespawn(struct init_action *a)
228 {
229 a->tout.cb = respawn;
230 a->respawn = 500;
231
232 a->proc.cb = child_exit;
233 if (!a->proc.pid)
234 fork_worker(a);
235 }
236
237 static struct init_handler handlers[] = {
238 {
239 .name = "sysinit",
240 .cb = runrc,
241 }, {
242 .name = "shutdown",
243 .cb = runrc,
244 }, {
245 .name = "askfirst",
246 .cb = askfirst,
247 .multi = 1,
248 }, {
249 .name = "askconsole",
250 .cb = askconsole,
251 .multi = 1,
252 }, {
253 .name = "respawn",
254 .cb = rcrespawn,
255 .multi = 1,
256 }, {
257 .name = "askconsolelate",
258 .cb = askconsole,
259 .multi = 1,
260 }, {
261 .name = "respawnlate",
262 .cb = rcrespawn,
263 .multi = 1,
264 }
265 };
266
267 static int add_action(struct init_action *a, const char *name)
268 {
269 int i;
270
271 for (i = 0; i < ARRAY_SIZE(handlers); i++)
272 if (!strcmp(handlers[i].name, name)) {
273 a->handler = &handlers[i];
274 list_add_tail(&a->list, &actions);
275 return 0;
276 }
277 ERROR("Unknown init handler %s\n", name);
278 return -1;
279 }
280
281 void procd_inittab_run(const char *handler)
282 {
283 struct init_action *a;
284
285 list_for_each_entry(a, &actions, list)
286 if (!strcmp(a->handler->name, handler)) {
287 a->handler->cb(a);
288 if (!a->handler->multi)
289 break;
290 }
291 }
292
293 void procd_inittab(void)
294 {
295 #define LINE_LEN 128
296 FILE *fp = fopen(tab, "r");
297 struct init_action *a;
298 regex_t pat_inittab;
299 regmatch_t matches[5];
300 char *line;
301
302 if (!fp) {
303 ERROR("Failed to open %s: %m\n", tab);
304 return;
305 }
306
307 regcomp(&pat_inittab, "([a-zA-Z0-9]*):([a-zA-Z0-9]*):([a-zA-Z0-9]*):(.*)", REG_EXTENDED);
308 line = malloc(LINE_LEN);
309 a = calloc(1, sizeof(struct init_action));
310
311 while (fgets(line, LINE_LEN, fp)) {
312 char *tags[TAG_PROCESS + 1];
313 char *tok;
314 int i;
315 int len = strlen(line);
316
317 while (isspace(line[len - 1]))
318 len--;
319 line[len] = 0;
320
321 if (*line == '#')
322 continue;
323
324 if (regexec(&pat_inittab, line, 5, matches, 0))
325 continue;
326
327 DEBUG(4, "Parsing inittab - %s\n", line);
328
329 for (i = TAG_ID; i <= TAG_PROCESS; i++) {
330 line[matches[i].rm_eo] = '\0';
331 tags[i] = &line[matches[i + 1].rm_so];
332 };
333
334 tok = strtok(tags[TAG_PROCESS], " ");
335 for (i = 0; i < (MAX_ARGS - 1) && tok; i++) {
336 a->argv[i] = tok;
337 tok = strtok(NULL, " ");
338 }
339 a->argv[i] = NULL;
340 a->id = tags[TAG_ID];
341 a->line = line;
342
343 if (add_action(a, tags[TAG_ACTION]))
344 continue;
345 line = malloc(LINE_LEN);
346 a = calloc(1, sizeof(struct init_action));
347 }
348
349 fclose(fp);
350 free(line);
351 free(a);
352 regfree(&pat_inittab);
353 }