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 int dev_open(const char *dev)
73 fd = open( dev, O_RDWR);
80 static int dev_exist(const char *dev)
92 static void fork_worker(struct init_action *a)
100 fd = dev_open(a->id);
103 dup2(fd, STDIN_FILENO);
104 dup2(fd, STDOUT_FILENO);
105 dup2(fd, STDERR_FILENO);
109 execvp(a->argv[0], a->argv);
110 ERROR("Failed to execute %s\n", a->argv[0]);
114 if (a->proc.pid > 0) {
115 DEBUG(4, "Launched new %s action, pid=%d\n",
118 uloop_process_add(&a->proc);
122 static void child_exit(struct uloop_process *proc, int ret)
124 struct init_action *a = container_of(proc, struct init_action, proc);
126 DEBUG(4, "pid:%d\n", proc->pid);
127 uloop_timeout_set(&a->tout, a->respawn);
130 static void respawn(struct uloop_timeout *tout)
132 struct init_action *a = container_of(tout, struct init_action, tout);
136 static void rcdone(struct runqueue *q)
141 static void runrc(struct init_action *a)
143 if (!a->argv[1] || !a->argv[2]) {
144 ERROR("valid format is rcS <S|K> <param>\n");
147 rcS(a->argv[1], a->argv[2], rcdone);
150 static void askfirst(struct init_action *a)
154 if (!dev_exist(a->id) || (console && !strcmp(console, a->id))) {
155 DEBUG(4, "Skipping %s\n", a->id);
159 a->tout.cb = respawn;
160 for (i = MAX_ARGS - 1; i >= 1; i--)
161 a->argv[i] = a->argv[i - 1];
165 a->proc.cb = child_exit;
169 static void askconsole(struct init_action *a)
171 char line[256], *tty, *split;
174 tty = get_cmdline_val("console", line, sizeof(line));
176 split = strchr(tty, ',');
180 if (!dev_exist(tty)) {
181 DEBUG(4, "skipping %s\n", tty);
185 console = strdup(tty);
193 a->tout.cb = respawn;
194 for (i = MAX_ARGS - 1; i >= 1; i--)
195 a->argv[i] = a->argv[i - 1];
199 a->proc.cb = child_exit;
203 static void rcrespawn(struct init_action *a)
205 a->tout.cb = respawn;
208 a->proc.cb = child_exit;
212 static struct init_handler handlers[] = {
224 .name = "askconsole",
234 static int add_action(struct init_action *a, const char *name)
238 for (i = 0; i < ARRAY_SIZE(handlers); i++)
239 if (!strcmp(handlers[i].name, name)) {
240 a->handler = &handlers[i];
241 list_add_tail(&a->list, &actions);
244 ERROR("Unknown init handler %s\n", name);
248 void procd_inittab_run(const char *handler)
250 struct init_action *a;
252 list_for_each_entry(a, &actions, list)
253 if (!strcmp(a->handler->name, handler)) {
254 if (a->handler->multi) {
263 void procd_inittab(void)
266 FILE *fp = fopen(tab, "r");
267 struct init_action *a;
269 regmatch_t matches[5];
273 ERROR("Failed to open %s\n", tab);
277 regcomp(&pat_inittab, "([a-zA-Z0-9]*):([a-zA-Z0-9]*):([a-zA-Z0-9]*):(.*)", REG_EXTENDED);
278 line = malloc(LINE_LEN);
279 a = malloc(sizeof(struct init_action));
280 memset(a, 0, sizeof(struct init_action));
282 while (fgets(line, LINE_LEN, fp)) {
283 char *tags[TAG_PROCESS + 1];
286 int len = strlen(line);
288 while (isspace(line[len - 1]))
295 if (regexec(&pat_inittab, line, 5, matches, 0))
298 DEBUG(4, "Parsing inittab - %s", line);
300 for (i = TAG_ID; i <= TAG_PROCESS; i++) {
301 line[matches[i].rm_eo] = '\0';
302 tags[i] = &line[matches[i + 1].rm_so];
305 tok = strtok(tags[TAG_PROCESS], " ");
306 for (i = 0; i < (MAX_ARGS - 1) && tok; i++) {
308 tok = strtok(NULL, " ");
311 a->id = tags[TAG_ID];
314 if (add_action(a, tags[TAG_ACTION]))
316 line = malloc(LINE_LEN);
317 a = malloc(sizeof(struct init_action));
318 memset(a, 0, sizeof(struct init_action));
324 regfree(&pat_inittab);