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>
25 #include <libubox/utils.h>
26 #include <libubox/list.h>
28 #include "utils/utils.h"
44 void (*cb) (struct init_action *a);
49 struct list_head list;
55 struct init_handler *handler;
56 struct uloop_process proc;
59 struct uloop_timeout tout;
62 static const char *tab = "/etc/inittab";
63 static char *ask = "/sbin/askfirst";
65 static LIST_HEAD(actions);
67 static void fork_worker(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(4, "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(4, "pid:%d\n", proc->pid);
89 uloop_timeout_set(&a->tout, a->respawn);
92 static void respawn(struct uloop_timeout *tout)
94 struct init_action *a = container_of(tout, struct init_action, tout);
98 static void rcdone(struct runqueue *q)
103 static void runrc(struct init_action *a)
105 if (!a->argv[1] || !a->argv[2]) {
106 ERROR("valid format is rcS <S|K> <param>\n");
109 rcS(a->argv[1], a->argv[2], rcdone);
112 static void askfirst(struct init_action *a)
120 if (i || (console && !strcmp(console, a->id))) {
121 DEBUG(4, "Skipping %s\n", a->id);
125 a->tout.cb = respawn;
126 for (i = MAX_ARGS - 2; i >= 2; i--)
127 a->argv[i] = a->argv[i - 2];
132 a->proc.cb = child_exit;
136 static void askconsole(struct init_action *a)
139 char line[256], *tty, *split;
142 tty = get_cmdline_val("console", line, sizeof(line));
143 split=strchr(tty, ',');
151 DEBUG(4, "skipping %s\n", tty);
154 console = strdup(tty);
156 a->tout.cb = respawn;
157 for (i = MAX_ARGS - 2; i >= 2; i--)
158 a->argv[i] = a->argv[i - 2];
160 a->argv[1] = strdup(tty);
163 a->proc.cb = child_exit;
167 static void rcrespawn(struct init_action *a)
169 a->tout.cb = respawn;
172 a->proc.cb = child_exit;
176 static struct init_handler handlers[] = {
188 .name = "askconsole",
198 static int add_action(struct init_action *a, const char *name)
202 for (i = 0; i < ARRAY_SIZE(handlers); i++)
203 if (!strcmp(handlers[i].name, name)) {
204 a->handler = &handlers[i];
205 list_add_tail(&a->list, &actions);
208 ERROR("Unknown init handler %s\n", name);
212 void procd_inittab_run(const char *handler)
214 struct init_action *a;
216 list_for_each_entry(a, &actions, list)
217 if (!strcmp(a->handler->name, handler)) {
218 if (a->handler->multi) {
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]*):(.*)", 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];
250 int len = strlen(line);
252 while (isspace(line[len - 1]))
259 if (regexec(&pat_inittab, line, 5, matches, 0))
262 DEBUG(4, "Parsing inittab - %s", line);
264 for (i = TAG_ID; i <= TAG_PROCESS; i++) {
265 line[matches[i].rm_eo] = '\0';
266 tags[i] = &line[matches[i + 1].rm_so];
269 tok = strtok(tags[TAG_PROCESS], " ");
270 for (i = 0; i < (MAX_ARGS - 1) && tok; i++) {
272 tok = strtok(NULL, " ");
275 a->id = tags[TAG_ID];
278 if (add_action(a, tags[TAG_ACTION]))
280 line = malloc(LINE_LEN);
281 a = malloc(sizeof(struct init_action));
282 memset(a, 0, sizeof(struct init_action));
288 regfree(&pat_inittab);