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