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>
17 #include <sys/ioctl.h>
26 #include <libubox/utils.h>
27 #include <libubox/list.h>
29 #include "utils/utils.h"
45 void (*cb
) (struct init_action
*a
);
50 struct list_head list
;
56 struct init_handler
*handler
;
57 struct uloop_process proc
;
60 struct uloop_timeout tout
;
63 static const char *tab
= "/etc/inittab";
64 static char *ask
= "/sbin/askfirst";
66 static LIST_HEAD(actions
);
68 static int dev_open(const char *dev
)
74 ERROR("failed to change dir to /dev\n");
75 fd
= open(dev
, O_RDWR
);
77 ERROR("failed to change dir to /\n");
83 static int dev_exist(const char *dev
)
94 static void fork_worker(struct init_action
*a
)
103 fd
= dev_open(a
->id
);
106 dup2(fd
, STDIN_FILENO
);
107 dup2(fd
, STDOUT_FILENO
);
108 dup2(fd
, STDERR_FILENO
);
109 if (fd
> STDERR_FILENO
)
113 ioctl(STDIN_FILENO
, TIOCSCTTY
, 1);
114 tcsetpgrp(STDIN_FILENO
, p
);
116 execvp(a
->argv
[0], a
->argv
);
117 ERROR("Failed to execute %s\n", a
->argv
[0]);
121 if (a
->proc
.pid
> 0) {
122 DEBUG(4, "Launched new %s action, pid=%d\n",
125 uloop_process_add(&a
->proc
);
129 static void child_exit(struct uloop_process
*proc
, int ret
)
131 struct init_action
*a
= container_of(proc
, struct init_action
, proc
);
133 DEBUG(4, "pid:%d\n", proc
->pid
);
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
);
143 static void rcdone(struct runqueue
*q
)
148 static void runrc(struct init_action
*a
)
150 if (!a
->argv
[1] || !a
->argv
[2]) {
151 ERROR("valid format is rcS <S|K> <param>\n");
155 /* proceed even if no init or shutdown scripts run */
156 if (rcS(a
->argv
[1], a
->argv
[2], rcdone
))
160 static void askfirst(struct init_action
*a
)
164 if (!dev_exist(a
->id
) || (console
&& !strcmp(console
, a
->id
))) {
165 DEBUG(4, "Skipping %s\n", a
->id
);
169 a
->tout
.cb
= respawn
;
170 for (i
= MAX_ARGS
- 1; i
>= 1; i
--)
171 a
->argv
[i
] = a
->argv
[i
- 1];
175 a
->proc
.cb
= child_exit
;
179 static void askconsole(struct init_action
*a
)
181 char line
[256], *tty
, *split
;
184 tty
= get_cmdline_val("console", line
, sizeof(line
));
186 split
= strchr(tty
, ',');
190 if (!dev_exist(tty
)) {
191 DEBUG(4, "skipping %s\n", tty
);
195 console
= strdup(tty
);
203 a
->tout
.cb
= respawn
;
204 for (i
= MAX_ARGS
- 1; i
>= 1; i
--)
205 a
->argv
[i
] = a
->argv
[i
- 1];
209 a
->proc
.cb
= child_exit
;
213 static void rcrespawn(struct init_action
*a
)
215 a
->tout
.cb
= respawn
;
218 a
->proc
.cb
= child_exit
;
222 static struct init_handler handlers
[] = {
234 .name
= "askconsole",
244 static int add_action(struct init_action
*a
, const char *name
)
248 for (i
= 0; i
< ARRAY_SIZE(handlers
); i
++)
249 if (!strcmp(handlers
[i
].name
, name
)) {
250 a
->handler
= &handlers
[i
];
251 list_add_tail(&a
->list
, &actions
);
254 ERROR("Unknown init handler %s\n", name
);
258 void procd_inittab_run(const char *handler
)
260 struct init_action
*a
;
262 list_for_each_entry(a
, &actions
, list
)
263 if (!strcmp(a
->handler
->name
, handler
)) {
264 if (a
->handler
->multi
) {
273 void procd_inittab(void)
276 FILE *fp
= fopen(tab
, "r");
277 struct init_action
*a
;
279 regmatch_t matches
[5];
283 ERROR("Failed to open %s\n", tab
);
287 regcomp(&pat_inittab
, "([a-zA-Z0-9]*):([a-zA-Z0-9]*):([a-zA-Z0-9]*):(.*)", REG_EXTENDED
);
288 line
= malloc(LINE_LEN
);
289 a
= malloc(sizeof(struct init_action
));
290 memset(a
, 0, sizeof(struct init_action
));
292 while (fgets(line
, LINE_LEN
, fp
)) {
293 char *tags
[TAG_PROCESS
+ 1];
296 int len
= strlen(line
);
298 while (isspace(line
[len
- 1]))
305 if (regexec(&pat_inittab
, line
, 5, matches
, 0))
308 DEBUG(4, "Parsing inittab - %s", line
);
310 for (i
= TAG_ID
; i
<= TAG_PROCESS
; i
++) {
311 line
[matches
[i
].rm_eo
] = '\0';
312 tags
[i
] = &line
[matches
[i
+ 1].rm_so
];
315 tok
= strtok(tags
[TAG_PROCESS
], " ");
316 for (i
= 0; i
< (MAX_ARGS
- 1) && tok
; i
++) {
318 tok
= strtok(NULL
, " ");
321 a
->id
= tags
[TAG_ID
];
324 if (add_action(a
, tags
[TAG_ACTION
]))
326 line
= malloc(LINE_LEN
);
327 a
= malloc(sizeof(struct init_action
));
328 memset(a
, 0, sizeof(struct init_action
));
334 regfree(&pat_inittab
);