2 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
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
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.
15 #include <sys/types.h>
24 #include <libubox/utils.h>
25 #include <libubox/list.h>
42 void (*cb) (struct init_action *a);
47 struct list_head list;
53 struct init_handler *handler;
54 struct uloop_process proc;
57 struct uloop_timeout tout;
60 static const char *tab = "/etc/inittab";
61 static char *ask = "/sbin/askfirst";
63 static LIST_HEAD(actions);
65 static void fork_worker(struct init_action *a)
69 execvp(a->argv[0], a->argv);
70 ERROR("Failed to execute %s\n", a->argv[0]);
74 if (a->proc.pid > 0) {
75 DEBUG(2, "Launched new %s action, pid=%d\n",
78 uloop_process_add(&a->proc);
82 static void child_exit(struct uloop_process *proc, int ret)
84 struct init_action *a = container_of(proc, struct init_action, proc);
86 DEBUG(2, "pid:%d\n", proc->pid);
87 uloop_timeout_set(&a->tout, a->respawn);
90 static void respawn(struct uloop_timeout *tout)
92 struct init_action *a = container_of(tout, struct init_action, tout);
96 static void rcdone(struct runqueue *q)
101 static void runrc(struct init_action *a)
103 if (!a->argv[1] || !a->argv[2]) {
104 ERROR("valid format is rcS <S|K> <param>\n");
107 rcS(a->argv[1], a->argv[2], rcdone);
110 static void askfirst(struct init_action *a)
118 if (i || (console && !strcmp(console, a->id))) {
119 DEBUG(2, "Skipping %s\n", a->id);
123 a->tout.cb = respawn;
124 for (i = MAX_ARGS - 2; i >= 2; i--)
125 a->argv[i] = a->argv[i - 2];
130 a->proc.cb = child_exit;
134 static void askconsole(struct init_action *a)
137 char line[256], *tty;
138 int i, r, fd = open("/proc/cmdline", O_RDONLY);
140 regmatch_t matches[2];
145 r = read(fd, line, sizeof(line) - 1);
149 regcomp(&pat_cmdline, "console=([a-zA-Z0-9]*)", REG_EXTENDED);
150 if (regexec(&pat_cmdline, line, 2, matches, 0))
152 line[matches[1].rm_eo] = '\0';
153 tty = &line[matches[1].rm_so];
159 DEBUG(2, "skipping %s\n", tty);
162 console = strdup(tty);
164 a->tout.cb = respawn;
165 for (i = MAX_ARGS - 2; i >= 2; i--)
166 a->argv[i] = a->argv[i - 2];
168 a->argv[1] = strdup(tty);
171 a->proc.cb = child_exit;
175 regfree(&pat_cmdline);
178 static void rcrespawn(struct init_action *a)
180 a->tout.cb = respawn;
183 a->proc.cb = child_exit;
187 static struct init_handler handlers[] = {
199 .name = "askconsole",
209 static int add_action(struct init_action *a, const char *name)
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);
219 ERROR("Unknown init handler %s\n", name);
223 void procd_inittab_run(const char *handler)
225 struct init_action *a;
227 list_for_each_entry(a, &actions, list)
228 if (!strcmp(a->handler->name, handler)) {
229 if (a->handler->multi) {
238 void procd_inittab(void)
241 FILE *fp = fopen(tab, "r");
242 struct init_action *a;
244 regmatch_t matches[5];
248 ERROR("Failed to open %s\n", tab);
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));
257 while (fgets(line, LINE_LEN, fp)) {
258 char *tags[TAG_PROCESS + 1];
265 if (regexec(&pat_inittab, line, 5, matches, 0))
268 DEBUG(2, "Parsing inittab - %s", line);
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];
275 tok = strtok(tags[TAG_PROCESS], " ");
276 for (i = 0; i < (MAX_ARGS - i - 1) && tok; i++) {
278 tok = strtok(NULL, " ");
281 a->id = tags[TAG_ID];
284 if (add_action(a, tags[TAG_ACTION]))
286 line = malloc(LINE_LEN);
287 a = malloc(sizeof(struct init_action));
288 memset(a, 0, sizeof(struct init_action));
294 regfree(&pat_inittab);