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>
41 void (*cb) (struct init_action *a);
46 struct list_head list;
52 struct init_handler *handler;
53 struct uloop_process proc;
57 struct uloop_timeout tout;
60 static const char *tab = "/etc/inittab";
61 static char *ask = "/sbin/askfirst";
63 static struct init_action *pending;
65 static LIST_HEAD(actions);
67 static void fork_script(struct init_action *a)
71 execvp(a->argv[0], a->argv);
72 ERROR("Failed to execute %s\n", a->argv[0]);
76 if (a->proc.pid > 0) {
77 DEBUG(2, "Launched new %s action, pid=%d\n",
80 uloop_process_add(&a->proc);
84 static void child_exit(struct uloop_process *proc, int ret)
86 struct init_action *a = container_of(proc, struct init_action, proc);
88 DEBUG(2, "pid:%d\n", proc->pid);
90 uloop_timeout_set(&a->tout, a->respawn);
98 static void respawn(struct uloop_timeout *tout)
100 struct init_action *a = container_of(tout, struct init_action, tout);
104 static void runscript(struct init_action *a)
106 a->proc.cb = child_exit;
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;
174 regfree(&pat_cmdline);
177 static struct init_handler handlers[] = {
189 .name = "askconsole",
195 static int add_action(struct init_action *a, const char *name)
199 for (i = 0; i < ARRAY_SIZE(handlers); i++)
200 if (!strcmp(handlers[i].name, name)) {
201 a->handler = &handlers[i];
202 list_add_tail(&a->list, &actions);
205 ERROR("Unknown init handler %s\n", name);
209 void procd_inittab_run(const char *handler)
211 struct init_action *a;
213 list_for_each_entry(a, &actions, list)
214 if (!strcmp(a->handler->name, handler)) {
215 if (a->handler->atomic) {
219 if (pending || a->pending)
227 void procd_inittab(void)
230 FILE *fp = fopen(tab, "r");
231 struct init_action *a;
233 regmatch_t matches[5];
237 ERROR("Failed to open %s\n", tab);
241 regcomp(&pat_inittab, "([a-zA-Z0-9]*):([a-zA-Z0-9]*):([a-zA-Z0-9]*):([a-zA-Z0-9/[.-.]. ]*)", REG_EXTENDED);
242 line = malloc(LINE_LEN);
243 a = malloc(sizeof(struct init_action));
244 memset(a, 0, sizeof(struct init_action));
246 while (fgets(line, LINE_LEN, fp)) {
247 char *tags[TAG_PROCESS + 1];
254 if (regexec(&pat_inittab, line, 5, matches, 0))
257 DEBUG(2, "Parsing inittab - %s", line);
259 for (i = TAG_ID; i <= TAG_PROCESS; i++) {
260 line[matches[i].rm_eo] = '\0';
261 tags[i] = &line[matches[i + 1].rm_so];
264 tok = strtok(tags[TAG_PROCESS], " ");
265 for (i = 0; i < (MAX_ARGS - i - 1) && tok; i++) {
267 tok = strtok(NULL, " ");
270 a->id = tags[TAG_ID];
273 if (add_action(a, tags[TAG_ACTION]))
275 line = malloc(LINE_LEN);
276 a = malloc(sizeof(struct init_action));
277 memset(a, 0, sizeof(struct init_action));
283 regfree(&pat_inittab);