011d7a67e644841ed5ed7cb95e4f88c385b6cceb
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\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\n", proc
->pid
);
124 uloop_timeout_set(&a
->tout
, a
->respawn
);
127 static void respawn(struct uloop_timeout
*tout
)
129 struct init_action
*a
= container_of(tout
, struct init_action
, tout
);
133 static void rcdone(struct runqueue
*q
)
138 static void runrc(struct init_action
*a
)
140 if (!a
->argv
[1] || !a
->argv
[2]) {
141 ERROR("valid format is rcS <S|K> <param>\n");
145 /* proceed even if no init or shutdown scripts run */
146 if (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",
232 .name
= "askconsolelate",
236 .name
= "respawnlate",
242 static int add_action(struct init_action
*a
, const char *name
)
246 for (i
= 0; i
< ARRAY_SIZE(handlers
); i
++)
247 if (!strcmp(handlers
[i
].name
, name
)) {
248 a
->handler
= &handlers
[i
];
249 list_add_tail(&a
->list
, &actions
);
252 ERROR("Unknown init handler %s\n", name
);
256 void procd_inittab_run(const char *handler
)
258 struct init_action
*a
;
260 list_for_each_entry(a
, &actions
, list
)
261 if (!strcmp(a
->handler
->name
, handler
)) {
262 if (a
->handler
->multi
) {
271 void procd_inittab(void)
274 FILE *fp
= fopen(tab
, "r");
275 struct init_action
*a
;
277 regmatch_t matches
[5];
281 ERROR("Failed to open %s\n", tab
);
285 regcomp(&pat_inittab
, "([a-zA-Z0-9]*):([a-zA-Z0-9]*):([a-zA-Z0-9]*):(.*)", REG_EXTENDED
);
286 line
= malloc(LINE_LEN
);
287 a
= malloc(sizeof(struct init_action
));
288 memset(a
, 0, sizeof(struct init_action
));
290 while (fgets(line
, LINE_LEN
, fp
)) {
291 char *tags
[TAG_PROCESS
+ 1];
294 int len
= strlen(line
);
296 while (isspace(line
[len
- 1]))
303 if (regexec(&pat_inittab
, line
, 5, matches
, 0))
306 DEBUG(4, "Parsing inittab - %s", line
);
308 for (i
= TAG_ID
; i
<= TAG_PROCESS
; i
++) {
309 line
[matches
[i
].rm_eo
] = '\0';
310 tags
[i
] = &line
[matches
[i
+ 1].rm_so
];
313 tok
= strtok(tags
[TAG_PROCESS
], " ");
314 for (i
= 0; i
< (MAX_ARGS
- 1) && tok
; i
++) {
316 tok
= strtok(NULL
, " ");
319 a
->id
= tags
[TAG_ID
];
322 if (add_action(a
, tags
[TAG_ACTION
]))
324 line
= malloc(LINE_LEN
);
325 a
= malloc(sizeof(struct init_action
));
326 memset(a
, 0, sizeof(struct init_action
));
332 regfree(&pat_inittab
);