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>
43 void (*cb
) (struct init_action
*a
);
48 struct list_head list
;
54 struct init_handler
*handler
;
55 struct uloop_process proc
;
58 struct uloop_timeout tout
;
61 static const char *tab
= "/etc/inittab";
62 static char *ask
= "/sbin/askfirst";
64 static LIST_HEAD(actions
);
66 static void fork_worker(struct init_action
*a
)
70 execvp(a
->argv
[0], a
->argv
);
71 ERROR("Failed to execute %s\n", a
->argv
[0]);
75 if (a
->proc
.pid
> 0) {
76 DEBUG(4, "Launched new %s action, pid=%d\n",
79 uloop_process_add(&a
->proc
);
83 static void child_exit(struct uloop_process
*proc
, int ret
)
85 struct init_action
*a
= container_of(proc
, struct init_action
, proc
);
87 DEBUG(4, "pid:%d\n", proc
->pid
);
88 uloop_timeout_set(&a
->tout
, a
->respawn
);
91 static void respawn(struct uloop_timeout
*tout
)
93 struct init_action
*a
= container_of(tout
, struct init_action
, tout
);
97 static void rcdone(struct runqueue
*q
)
102 static void runrc(struct init_action
*a
)
104 if (!a
->argv
[1] || !a
->argv
[2]) {
105 ERROR("valid format is rcS <S|K> <param>\n");
108 rcS(a
->argv
[1], a
->argv
[2], rcdone
);
111 static void askfirst(struct init_action
*a
)
119 if (i
|| (console
&& !strcmp(console
, a
->id
))) {
120 DEBUG(4, "Skipping %s\n", a
->id
);
124 a
->tout
.cb
= respawn
;
125 for (i
= MAX_ARGS
- 2; i
>= 2; i
--)
126 a
->argv
[i
] = a
->argv
[i
- 2];
131 a
->proc
.cb
= child_exit
;
135 static void askconsole(struct init_action
*a
)
138 char line
[256], *tty
;
139 int i
, r
, fd
= open("/proc/cmdline", O_RDONLY
);
141 regmatch_t matches
[2];
146 r
= read(fd
, line
, sizeof(line
) - 1);
150 regcomp(&pat_cmdline
, "console=([a-zA-Z0-9]*)", REG_EXTENDED
);
151 if (regexec(&pat_cmdline
, line
, 2, matches
, 0))
153 line
[matches
[1].rm_eo
] = '\0';
154 tty
= &line
[matches
[1].rm_so
];
160 DEBUG(4, "skipping %s\n", tty
);
163 console
= strdup(tty
);
165 a
->tout
.cb
= respawn
;
166 for (i
= MAX_ARGS
- 2; i
>= 2; i
--)
167 a
->argv
[i
] = a
->argv
[i
- 2];
169 a
->argv
[1] = strdup(tty
);
172 a
->proc
.cb
= child_exit
;
176 regfree(&pat_cmdline
);
179 static void rcrespawn(struct init_action
*a
)
181 a
->tout
.cb
= respawn
;
184 a
->proc
.cb
= child_exit
;
188 static struct init_handler handlers
[] = {
200 .name
= "askconsole",
210 static int add_action(struct init_action
*a
, const char *name
)
214 for (i
= 0; i
< ARRAY_SIZE(handlers
); i
++)
215 if (!strcmp(handlers
[i
].name
, name
)) {
216 a
->handler
= &handlers
[i
];
217 list_add_tail(&a
->list
, &actions
);
220 ERROR("Unknown init handler %s\n", name
);
224 void procd_inittab_run(const char *handler
)
226 struct init_action
*a
;
228 list_for_each_entry(a
, &actions
, list
)
229 if (!strcmp(a
->handler
->name
, handler
)) {
230 if (a
->handler
->multi
) {
239 void procd_inittab(void)
242 FILE *fp
= fopen(tab
, "r");
243 struct init_action
*a
;
245 regmatch_t matches
[5];
249 ERROR("Failed to open %s\n", tab
);
253 regcomp(&pat_inittab
, "([a-zA-Z0-9]*):([a-zA-Z0-9]*):([a-zA-Z0-9]*):(.*)", REG_EXTENDED
);
254 line
= malloc(LINE_LEN
);
255 a
= malloc(sizeof(struct init_action
));
256 memset(a
, 0, sizeof(struct init_action
));
258 while (fgets(line
, LINE_LEN
, fp
)) {
259 char *tags
[TAG_PROCESS
+ 1];
262 int len
= strlen(line
);
264 while (isspace(line
[len
- 1]))
271 if (regexec(&pat_inittab
, line
, 5, matches
, 0))
274 DEBUG(4, "Parsing inittab - %s", line
);
276 for (i
= TAG_ID
; i
<= TAG_PROCESS
; i
++) {
277 line
[matches
[i
].rm_eo
] = '\0';
278 tags
[i
] = &line
[matches
[i
+ 1].rm_so
];
281 tok
= strtok(tags
[TAG_PROCESS
], " ");
282 for (i
= 0; i
< (MAX_ARGS
- i
- 1) && tok
; i
++) {
284 tok
= strtok(NULL
, " ");
287 a
->id
= tags
[TAG_ID
];
290 if (add_action(a
, tags
[TAG_ACTION
]))
292 line
= malloc(LINE_LEN
);
293 a
= malloc(sizeof(struct init_action
));
294 memset(a
, 0, sizeof(struct init_action
));
300 regfree(&pat_inittab
);