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>
24 #include <libubox/utils.h>
25 #include <libubox/list.h>
42 void (*cb
) (struct init_action
*a
);
47 struct list_head list
;
53 struct init_handler
*handler
;
54 struct uloop_process proc
;
57 struct uloop_timeout tout
;
60 static const char *tab
= "/etc/inittab";
61 static char *ask
= "/sbin/askfirst";
63 static LIST_HEAD(actions
);
65 static void fork_worker(struct init_action
*a
)
69 execvp(a
->argv
[0], a
->argv
);
70 ERROR("Failed to execute %s\n", a
->argv
[0]);
74 if (a
->proc
.pid
> 0) {
75 DEBUG(2, "Launched new %s action, pid=%d\n",
78 uloop_process_add(&a
->proc
);
82 static void child_exit(struct uloop_process
*proc
, int ret
)
84 struct init_action
*a
= container_of(proc
, struct init_action
, proc
);
86 DEBUG(2, "pid:%d\n", proc
->pid
);
87 uloop_timeout_set(&a
->tout
, a
->respawn
);
90 static void respawn(struct uloop_timeout
*tout
)
92 struct init_action
*a
= container_of(tout
, struct init_action
, tout
);
96 static void rcdone(struct runqueue
*q
)
101 static void runrc(struct init_action
*a
)
103 if (!a
->argv
[1] || !a
->argv
[2]) {
104 ERROR("valid format is rcS <S|K> <param>\n");
107 rcS(a
->argv
[1], a
->argv
[2], rcdone
);
110 static void askfirst(struct init_action
*a
)
118 if (i
|| (console
&& !strcmp(console
, a
->id
))) {
119 DEBUG(2, "Skipping %s\n", a
->id
);
123 a
->tout
.cb
= respawn
;
124 for (i
= MAX_ARGS
- 2; i
>= 2; i
--)
125 a
->argv
[i
] = a
->argv
[i
- 2];
130 a
->proc
.cb
= child_exit
;
134 static void askconsole(struct init_action
*a
)
137 char line
[256], *tty
;
138 int i
, r
, fd
= open("/proc/cmdline", O_RDONLY
);
140 regmatch_t matches
[2];
145 r
= read(fd
, line
, sizeof(line
) - 1);
149 regcomp(&pat_cmdline
, "console=([a-zA-Z0-9]*)", REG_EXTENDED
);
150 if (regexec(&pat_cmdline
, line
, 2, matches
, 0))
152 line
[matches
[1].rm_eo
] = '\0';
153 tty
= &line
[matches
[1].rm_so
];
159 DEBUG(2, "skipping %s\n", tty
);
162 console
= strdup(tty
);
164 a
->tout
.cb
= respawn
;
165 for (i
= MAX_ARGS
- 2; i
>= 2; i
--)
166 a
->argv
[i
] = a
->argv
[i
- 2];
168 a
->argv
[1] = strdup(tty
);
171 a
->proc
.cb
= child_exit
;
175 regfree(&pat_cmdline
);
178 static struct init_handler handlers
[] = {
190 .name
= "askconsole",
196 static int add_action(struct init_action
*a
, const char *name
)
200 for (i
= 0; i
< ARRAY_SIZE(handlers
); i
++)
201 if (!strcmp(handlers
[i
].name
, name
)) {
202 a
->handler
= &handlers
[i
];
203 list_add_tail(&a
->list
, &actions
);
206 ERROR("Unknown init handler %s\n", name
);
210 void procd_inittab_run(const char *handler
)
212 struct init_action
*a
;
214 list_for_each_entry(a
, &actions
, list
)
215 if (!strcmp(a
->handler
->name
, handler
)) {
216 if (a
->handler
->multi
) {
225 void procd_inittab(void)
228 FILE *fp
= fopen(tab
, "r");
229 struct init_action
*a
;
231 regmatch_t matches
[5];
235 ERROR("Failed to open %s\n", tab
);
239 regcomp(&pat_inittab
, "([a-zA-Z0-9]*):([a-zA-Z0-9]*):([a-zA-Z0-9]*):([a-zA-Z0-9/[.-.]. ]*)", REG_EXTENDED
);
240 line
= malloc(LINE_LEN
);
241 a
= malloc(sizeof(struct init_action
));
242 memset(a
, 0, sizeof(struct init_action
));
244 while (fgets(line
, LINE_LEN
, fp
)) {
245 char *tags
[TAG_PROCESS
+ 1];
252 if (regexec(&pat_inittab
, line
, 5, matches
, 0))
255 DEBUG(2, "Parsing inittab - %s", line
);
257 for (i
= TAG_ID
; i
<= TAG_PROCESS
; i
++) {
258 line
[matches
[i
].rm_eo
] = '\0';
259 tags
[i
] = &line
[matches
[i
+ 1].rm_so
];
262 tok
= strtok(tags
[TAG_PROCESS
], " ");
263 for (i
= 0; i
< (MAX_ARGS
- i
- 1) && tok
; i
++) {
265 tok
= strtok(NULL
, " ");
268 a
->id
= tags
[TAG_ID
];
271 if (add_action(a
, tags
[TAG_ACTION
]))
273 line
= malloc(LINE_LEN
);
274 a
= malloc(sizeof(struct init_action
));
275 memset(a
, 0, sizeof(struct init_action
));
281 regfree(&pat_inittab
);