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"
46 void (*cb
) (struct init_action
*a
);
51 struct list_head list
;
57 struct init_handler
*handler
;
58 struct uloop_process proc
;
61 struct uloop_timeout tout
;
64 static const char *tab
= "/etc/inittab";
65 static char *ask
= "/sbin/askfirst";
67 static LIST_HEAD(actions
);
69 static int dev_exist(const char *dev
)
73 dfd
= open("/dev", O_PATH
|O_DIRECTORY
);
78 fd
= openat(dfd
, dev
, O_RDONLY
);
88 static void fork_worker(struct init_action
*a
)
96 if (patch_stdio(a
->id
))
97 ERROR("Failed to setup i/o redirection\n");
99 ioctl(STDIN_FILENO
, TIOCSCTTY
, 1);
100 tcsetpgrp(STDIN_FILENO
, p
);
102 execvp(a
->argv
[0], a
->argv
);
103 ERROR("Failed to execute %s\n", a
->argv
[0]);
107 if (a
->proc
.pid
> 0) {
108 DEBUG(4, "Launched new %s action, pid=%d\n",
111 uloop_process_add(&a
->proc
);
115 static void child_exit(struct uloop_process
*proc
, int ret
)
117 struct init_action
*a
= container_of(proc
, struct init_action
, proc
);
119 DEBUG(4, "pid:%d\n", proc
->pid
);
120 uloop_timeout_set(&a
->tout
, a
->respawn
);
123 static void respawn(struct uloop_timeout
*tout
)
125 struct init_action
*a
= container_of(tout
, struct init_action
, tout
);
129 static void rcdone(struct runqueue
*q
)
134 static void runrc(struct init_action
*a
)
136 if (!a
->argv
[1] || !a
->argv
[2]) {
137 ERROR("valid format is rcS <S|K> <param>\n");
141 /* proceed even if no init or shutdown scripts run */
142 if (rcS(a
->argv
[1], a
->argv
[2], rcdone
))
146 static void askfirst(struct init_action
*a
)
150 if (!dev_exist(a
->id
) || (console
&& !strcmp(console
, a
->id
))) {
151 DEBUG(4, "Skipping %s\n", a
->id
);
155 a
->tout
.cb
= respawn
;
156 for (i
= MAX_ARGS
- 1; i
>= 1; i
--)
157 a
->argv
[i
] = a
->argv
[i
- 1];
161 a
->proc
.cb
= child_exit
;
165 static void askconsole(struct init_action
*a
)
167 char line
[256], *tty
, *split
;
170 tty
= get_cmdline_val("console", line
, sizeof(line
));
172 split
= strchr(tty
, ',');
176 if (!dev_exist(tty
)) {
177 DEBUG(4, "skipping %s\n", tty
);
181 console
= strdup(tty
);
189 a
->tout
.cb
= respawn
;
190 for (i
= MAX_ARGS
- 1; i
>= 1; i
--)
191 a
->argv
[i
] = a
->argv
[i
- 1];
195 a
->proc
.cb
= child_exit
;
199 static void rcrespawn(struct init_action
*a
)
201 a
->tout
.cb
= respawn
;
204 a
->proc
.cb
= child_exit
;
208 static struct init_handler handlers
[] = {
220 .name
= "askconsole",
230 static int add_action(struct init_action
*a
, const char *name
)
234 for (i
= 0; i
< ARRAY_SIZE(handlers
); i
++)
235 if (!strcmp(handlers
[i
].name
, name
)) {
236 a
->handler
= &handlers
[i
];
237 list_add_tail(&a
->list
, &actions
);
240 ERROR("Unknown init handler %s\n", name
);
244 void procd_inittab_run(const char *handler
)
246 struct init_action
*a
;
248 list_for_each_entry(a
, &actions
, list
)
249 if (!strcmp(a
->handler
->name
, handler
)) {
250 if (a
->handler
->multi
) {
259 void procd_inittab(void)
262 FILE *fp
= fopen(tab
, "r");
263 struct init_action
*a
;
265 regmatch_t matches
[5];
269 ERROR("Failed to open %s\n", tab
);
273 regcomp(&pat_inittab
, "([a-zA-Z0-9]*):([a-zA-Z0-9]*):([a-zA-Z0-9]*):(.*)", REG_EXTENDED
);
274 line
= malloc(LINE_LEN
);
275 a
= malloc(sizeof(struct init_action
));
276 memset(a
, 0, sizeof(struct init_action
));
278 while (fgets(line
, LINE_LEN
, fp
)) {
279 char *tags
[TAG_PROCESS
+ 1];
282 int len
= strlen(line
);
284 while (isspace(line
[len
- 1]))
291 if (regexec(&pat_inittab
, line
, 5, matches
, 0))
294 DEBUG(4, "Parsing inittab - %s", line
);
296 for (i
= TAG_ID
; i
<= TAG_PROCESS
; i
++) {
297 line
[matches
[i
].rm_eo
] = '\0';
298 tags
[i
] = &line
[matches
[i
+ 1].rm_so
];
301 tok
= strtok(tags
[TAG_PROCESS
], " ");
302 for (i
= 0; i
< (MAX_ARGS
- 1) && tok
; i
++) {
304 tok
= strtok(NULL
, " ");
307 a
->id
= tags
[TAG_ID
];
310 if (add_action(a
, tags
[TAG_ACTION
]))
312 line
= malloc(LINE_LEN
);
313 a
= malloc(sizeof(struct init_action
));
314 memset(a
, 0, sizeof(struct init_action
));
320 regfree(&pat_inittab
);