instance: avoid dumping invalid service instances - prevents a potential crash
[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_open(const char *dev)
69 {
70 int fd = -1;
71
72 if (dev) {
73 if (chdir("/dev"))
74 ERROR("failed to change dir to /dev\n");
75 fd = open(dev, O_RDWR);
76 if (chdir("/"))
77 ERROR("failed to change dir to /\n");
78 }
79
80 return fd;
81 }
82
83 static int dev_exist(const char *dev)
84 {
85 int res;
86
87 res = dev_open(dev);
88 if (res != -1)
89 close(res);
90
91 return (res != -1);
92 }
93
94 static void fork_worker(struct init_action *a)
95 {
96 int fd;
97 pid_t p;
98
99 a->proc.pid = fork();
100 if (!a->proc.pid) {
101 p = setsid();
102
103 fd = dev_open(a->id);
104 if (fd != -1)
105 {
106 dup2(fd, STDIN_FILENO);
107 dup2(fd, STDOUT_FILENO);
108 dup2(fd, STDERR_FILENO);
109 if (fd > STDERR_FILENO)
110 close(fd);
111 }
112
113 ioctl(STDIN_FILENO, TIOCSCTTY, 1);
114 tcsetpgrp(STDIN_FILENO, p);
115
116 execvp(a->argv[0], a->argv);
117 ERROR("Failed to execute %s\n", a->argv[0]);
118 exit(-1);
119 }
120
121 if (a->proc.pid > 0) {
122 DEBUG(4, "Launched new %s action, pid=%d\n",
123 a->handler->name,
124 (int) a->proc.pid);
125 uloop_process_add(&a->proc);
126 }
127 }
128
129 static void child_exit(struct uloop_process *proc, int ret)
130 {
131 struct init_action *a = container_of(proc, struct init_action, proc);
132
133 DEBUG(4, "pid:%d\n", proc->pid);
134 uloop_timeout_set(&a->tout, a->respawn);
135 }
136
137 static void respawn(struct uloop_timeout *tout)
138 {
139 struct init_action *a = container_of(tout, struct init_action, tout);
140 fork_worker(a);
141 }
142
143 static void rcdone(struct runqueue *q)
144 {
145 procd_state_next();
146 }
147
148 static void runrc(struct init_action *a)
149 {
150 if (!a->argv[1] || !a->argv[2]) {
151 ERROR("valid format is rcS <S|K> <param>\n");
152 return;
153 }
154 rcS(a->argv[1], a->argv[2], rcdone);
155 }
156
157 static void askfirst(struct init_action *a)
158 {
159 int i;
160
161 if (!dev_exist(a->id) || (console && !strcmp(console, a->id))) {
162 DEBUG(4, "Skipping %s\n", a->id);
163 return;
164 }
165
166 a->tout.cb = respawn;
167 for (i = MAX_ARGS - 1; i >= 1; i--)
168 a->argv[i] = a->argv[i - 1];
169 a->argv[0] = ask;
170 a->respawn = 500;
171
172 a->proc.cb = child_exit;
173 fork_worker(a);
174 }
175
176 static void askconsole(struct init_action *a)
177 {
178 char line[256], *tty, *split;
179 int i;
180
181 tty = get_cmdline_val("console", line, sizeof(line));
182 if (tty != NULL) {
183 split = strchr(tty, ',');
184 if (split != NULL)
185 *split = '\0';
186
187 if (!dev_exist(tty)) {
188 DEBUG(4, "skipping %s\n", tty);
189 return;
190 }
191
192 console = strdup(tty);
193 a->id = strdup(tty);
194 }
195 else {
196 console = NULL;
197 a->id = NULL;
198 }
199
200 a->tout.cb = respawn;
201 for (i = MAX_ARGS - 1; i >= 1; i--)
202 a->argv[i] = a->argv[i - 1];
203 a->argv[0] = ask;
204 a->respawn = 500;
205
206 a->proc.cb = child_exit;
207 fork_worker(a);
208 }
209
210 static void rcrespawn(struct init_action *a)
211 {
212 a->tout.cb = respawn;
213 a->respawn = 500;
214
215 a->proc.cb = child_exit;
216 fork_worker(a);
217 }
218
219 static struct init_handler handlers[] = {
220 {
221 .name = "sysinit",
222 .cb = runrc,
223 }, {
224 .name = "shutdown",
225 .cb = runrc,
226 }, {
227 .name = "askfirst",
228 .cb = askfirst,
229 .multi = 1,
230 }, {
231 .name = "askconsole",
232 .cb = askconsole,
233 .multi = 1,
234 }, {
235 .name = "respawn",
236 .cb = rcrespawn,
237 .multi = 1,
238 }
239 };
240
241 static int add_action(struct init_action *a, const char *name)
242 {
243 int i;
244
245 for (i = 0; i < ARRAY_SIZE(handlers); i++)
246 if (!strcmp(handlers[i].name, name)) {
247 a->handler = &handlers[i];
248 list_add_tail(&a->list, &actions);
249 return 0;
250 }
251 ERROR("Unknown init handler %s\n", name);
252 return -1;
253 }
254
255 void procd_inittab_run(const char *handler)
256 {
257 struct init_action *a;
258
259 list_for_each_entry(a, &actions, list)
260 if (!strcmp(a->handler->name, handler)) {
261 if (a->handler->multi) {
262 a->handler->cb(a);
263 continue;
264 }
265 a->handler->cb(a);
266 break;
267 }
268 }
269
270 void procd_inittab(void)
271 {
272 #define LINE_LEN 128
273 FILE *fp = fopen(tab, "r");
274 struct init_action *a;
275 regex_t pat_inittab;
276 regmatch_t matches[5];
277 char *line;
278
279 if (!fp) {
280 ERROR("Failed to open %s\n", tab);
281 return;
282 }
283
284 regcomp(&pat_inittab, "([a-zA-Z0-9]*):([a-zA-Z0-9]*):([a-zA-Z0-9]*):(.*)", REG_EXTENDED);
285 line = malloc(LINE_LEN);
286 a = malloc(sizeof(struct init_action));
287 memset(a, 0, sizeof(struct init_action));
288
289 while (fgets(line, LINE_LEN, fp)) {
290 char *tags[TAG_PROCESS + 1];
291 char *tok;
292 int i;
293 int len = strlen(line);
294
295 while (isspace(line[len - 1]))
296 len--;
297 line[len] = 0;
298
299 if (*line == '#')
300 continue;
301
302 if (regexec(&pat_inittab, line, 5, matches, 0))
303 continue;
304
305 DEBUG(4, "Parsing inittab - %s", line);
306
307 for (i = TAG_ID; i <= TAG_PROCESS; i++) {
308 line[matches[i].rm_eo] = '\0';
309 tags[i] = &line[matches[i + 1].rm_so];
310 };
311
312 tok = strtok(tags[TAG_PROCESS], " ");
313 for (i = 0; i < (MAX_ARGS - 1) && tok; i++) {
314 a->argv[i] = tok;
315 tok = strtok(NULL, " ");
316 }
317 a->argv[i] = NULL;
318 a->id = tags[TAG_ID];
319 a->line = line;
320
321 if (add_action(a, tags[TAG_ACTION]))
322 continue;
323 line = malloc(LINE_LEN);
324 a = malloc(sizeof(struct init_action));
325 memset(a, 0, sizeof(struct init_action));
326 }
327
328 fclose(fp);
329 free(line);
330 free(a);
331 regfree(&pat_inittab);
332 }