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