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.
16 #include <sys/types.h>
18 #include <sys/ioctl.h>
27 #include <libubox/utils.h>
28 #include <libubox/list.h>
30 #include "utils/utils.h"
35 #define O_PATH 010000000
50 void (*cb
) (struct init_action
*a
);
55 struct list_head list
;
61 struct init_handler
*handler
;
62 struct uloop_process proc
;
65 struct uloop_timeout tout
;
68 static const char *tab
= "/etc/inittab";
69 static char *ask
= "/sbin/askfirst";
71 static LIST_HEAD(actions
);
73 static int dev_exist(const char *dev
)
77 dfd
= open("/dev", O_PATH
|O_DIRECTORY
);
82 fd
= openat(dfd
, dev
, O_RDONLY
);
92 static void fork_worker(struct init_action
*a
)
100 if (patch_stdio(a
->id
))
101 ERROR("Failed to setup i/o redirection\n");
103 ioctl(STDIN_FILENO
, TIOCSCTTY
, 1);
104 tcsetpgrp(STDIN_FILENO
, p
);
106 execvp(a
->argv
[0], a
->argv
);
107 ERROR("Failed to execute %s: %m\n", a
->argv
[0]);
111 if (a
->proc
.pid
> 0) {
112 DEBUG(4, "Launched new %s action, pid=%d\n",
115 uloop_process_add(&a
->proc
);
119 static void child_exit(struct uloop_process
*proc
, int ret
)
121 struct init_action
*a
= container_of(proc
, struct init_action
, proc
);
123 DEBUG(4, "pid:%d, exitcode:%d\n", proc
->pid
, ret
);
129 if (!dev_exist(a
->id
)) {
130 DEBUG(4, "Skipping respawn: device '%s' does not exist anymore\n", a
->id
);
134 uloop_timeout_set(&a
->tout
, a
->respawn
);
137 static void respawn(struct uloop_timeout
*tout
)
139 struct init_action
*a
= container_of(tout
, struct init_action
, tout
);
144 static void rcdone(struct runqueue
*q
)
149 static void runrc(struct init_action
*a
)
151 if (!a
->argv
[1] || !a
->argv
[2]) {
152 ERROR("valid format is rcS <S|K> <param>\n");
156 /* proceed even if no init or shutdown scripts run */
157 if (rcS(a
->argv
[1], a
->argv
[2], rcdone
))
161 static void askfirst(struct init_action
*a
)
165 if (!dev_exist(a
->id
) || (console
&& !strcmp(console
, a
->id
))) {
166 DEBUG(4, "Skipping %s\n", a
->id
);
170 a
->tout
.cb
= respawn
;
171 /* shift arguments only if not yet done */
172 if (a
->argv
[0] != ask
) {
173 for (i
= MAX_ARGS
- 1; i
>= 1; i
--)
174 a
->argv
[i
] = a
->argv
[i
- 1];
179 a
->proc
.cb
= child_exit
;
184 static void askconsole(struct init_action
*a
)
186 char line
[256], *tty
, *split
;
189 /* First, try console= on the kernel command line,
190 * then fallback to /sys/class/tty/console/active,
191 * which should work when linux,stdout-path (or equivalent)
192 * is in the device tree
194 tty
= get_cmdline_val("console", line
, sizeof(line
));
196 if (dev_exist("console"))
199 tty
= get_active_console(line
, sizeof(line
));
202 split
= strchr(tty
, ',');
206 if (!dev_exist(tty
)) {
207 DEBUG(4, "skipping %s\n", tty
);
211 console
= strdup(tty
);
219 a
->tout
.cb
= respawn
;
220 /* shift arguments only if not yet done */
221 if (a
->argv
[0] != ask
) {
222 for (i
= MAX_ARGS
- 1; i
>= 1; i
--)
223 a
->argv
[i
] = a
->argv
[i
- 1];
228 a
->proc
.cb
= child_exit
;
233 static void rcrespawn(struct init_action
*a
)
235 a
->tout
.cb
= respawn
;
238 a
->proc
.cb
= child_exit
;
243 static struct init_handler handlers
[] = {
255 .name
= "askconsole",
263 .name
= "askconsolelate",
267 .name
= "respawnlate",
273 static int add_action(struct init_action
*a
, const char *name
)
277 for (i
= 0; i
< ARRAY_SIZE(handlers
); i
++)
278 if (!strcmp(handlers
[i
].name
, name
)) {
279 a
->handler
= &handlers
[i
];
280 list_add_tail(&a
->list
, &actions
);
283 ERROR("Unknown init handler %s\n", name
);
287 void procd_inittab_run(const char *handler
)
289 struct init_action
*a
;
291 list_for_each_entry(a
, &actions
, list
)
292 if (!strcmp(a
->handler
->name
, handler
)) {
294 if (!a
->handler
->multi
)
299 void procd_inittab_kill(void)
301 struct init_action
*a
;
303 list_for_each_entry(a
, &actions
, list
) {
306 kill(a
->proc
.pid
, SIGKILL
);
310 void procd_inittab(void)
313 FILE *fp
= fopen(tab
, "r");
314 struct init_action
*a
;
316 regmatch_t matches
[5];
320 ERROR("Failed to open %s: %m\n", tab
);
324 regcomp(&pat_inittab
, "([a-zA-Z0-9]*):([a-zA-Z0-9]*):([a-zA-Z0-9]*):(.*)", REG_EXTENDED
);
325 line
= malloc(LINE_LEN
);
326 a
= calloc(1, sizeof(struct init_action
));
328 while (fgets(line
, LINE_LEN
, fp
)) {
329 char *tags
[TAG_PROCESS
+ 1];
332 int len
= strlen(line
);
334 while (isspace(line
[len
- 1]))
341 if (regexec(&pat_inittab
, line
, 5, matches
, 0))
344 DEBUG(4, "Parsing inittab - %s\n", line
);
346 for (i
= TAG_ID
; i
<= TAG_PROCESS
; i
++) {
347 line
[matches
[i
].rm_eo
] = '\0';
348 tags
[i
] = &line
[matches
[i
+ 1].rm_so
];
351 tok
= strtok(tags
[TAG_PROCESS
], " ");
352 for (i
= 0; i
< (MAX_ARGS
- 1) && tok
; i
++) {
354 tok
= strtok(NULL
, " ");
357 a
->id
= tags
[TAG_ID
];
360 if (add_action(a
, tags
[TAG_ACTION
]))
362 line
= malloc(LINE_LEN
);
363 a
= calloc(1, sizeof(struct init_action
));
369 regfree(&pat_inittab
);