528396e57534553309cdf482b87ba837772984a1
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_exist(const char *dev
)
72 dfd
= open("/dev", O_PATH
|O_DIRECTORY
);
77 fd
= openat(dfd
, dev
, O_RDONLY
);
87 static void fork_worker(struct init_action
*a
)
95 if (patch_stdio(a
->id
))
96 ERROR("Failed to setup i/o redirection\n");
98 ioctl(STDIN_FILENO
, TIOCSCTTY
, 1);
99 tcsetpgrp(STDIN_FILENO
, p
);
101 execvp(a
->argv
[0], a
->argv
);
102 ERROR("Failed to execute %s\n", a
->argv
[0]);
106 if (a
->proc
.pid
> 0) {
107 DEBUG(4, "Launched new %s action, pid=%d\n",
110 uloop_process_add(&a
->proc
);
114 static void child_exit(struct uloop_process
*proc
, int ret
)
116 struct init_action
*a
= container_of(proc
, struct init_action
, proc
);
118 DEBUG(4, "pid:%d\n", proc
->pid
);
119 uloop_timeout_set(&a
->tout
, a
->respawn
);
122 static void respawn(struct uloop_timeout
*tout
)
124 struct init_action
*a
= container_of(tout
, struct init_action
, tout
);
128 static void rcdone(struct runqueue
*q
)
133 static void runrc(struct init_action
*a
)
135 if (!a
->argv
[1] || !a
->argv
[2]) {
136 ERROR("valid format is rcS <S|K> <param>\n");
140 /* proceed even if no init or shutdown scripts run */
141 if (rcS(a
->argv
[1], a
->argv
[2], rcdone
))
145 static void askfirst(struct init_action
*a
)
149 if (!dev_exist(a
->id
) || (console
&& !strcmp(console
, a
->id
))) {
150 DEBUG(4, "Skipping %s\n", a
->id
);
154 a
->tout
.cb
= respawn
;
155 for (i
= MAX_ARGS
- 1; i
>= 1; i
--)
156 a
->argv
[i
] = a
->argv
[i
- 1];
160 a
->proc
.cb
= child_exit
;
164 static void askconsole(struct init_action
*a
)
166 char line
[256], *tty
, *split
;
169 tty
= get_cmdline_val("console", line
, sizeof(line
));
171 split
= strchr(tty
, ',');
175 if (!dev_exist(tty
)) {
176 DEBUG(4, "skipping %s\n", tty
);
180 console
= strdup(tty
);
188 a
->tout
.cb
= respawn
;
189 for (i
= MAX_ARGS
- 1; i
>= 1; i
--)
190 a
->argv
[i
] = a
->argv
[i
- 1];
194 a
->proc
.cb
= child_exit
;
198 static void rcrespawn(struct init_action
*a
)
200 a
->tout
.cb
= respawn
;
203 a
->proc
.cb
= child_exit
;
207 static struct init_handler handlers
[] = {
219 .name
= "askconsole",
229 static int add_action(struct init_action
*a
, const char *name
)
233 for (i
= 0; i
< ARRAY_SIZE(handlers
); i
++)
234 if (!strcmp(handlers
[i
].name
, name
)) {
235 a
->handler
= &handlers
[i
];
236 list_add_tail(&a
->list
, &actions
);
239 ERROR("Unknown init handler %s\n", name
);
243 void procd_inittab_run(const char *handler
)
245 struct init_action
*a
;
247 list_for_each_entry(a
, &actions
, list
)
248 if (!strcmp(a
->handler
->name
, handler
)) {
249 if (a
->handler
->multi
) {
258 void procd_inittab(void)
261 FILE *fp
= fopen(tab
, "r");
262 struct init_action
*a
;
264 regmatch_t matches
[5];
268 ERROR("Failed to open %s\n", tab
);
272 regcomp(&pat_inittab
, "([a-zA-Z0-9]*):([a-zA-Z0-9]*):([a-zA-Z0-9]*):(.*)", REG_EXTENDED
);
273 line
= malloc(LINE_LEN
);
274 a
= malloc(sizeof(struct init_action
));
275 memset(a
, 0, sizeof(struct init_action
));
277 while (fgets(line
, LINE_LEN
, fp
)) {
278 char *tags
[TAG_PROCESS
+ 1];
281 int len
= strlen(line
);
283 while (isspace(line
[len
- 1]))
290 if (regexec(&pat_inittab
, line
, 5, matches
, 0))
293 DEBUG(4, "Parsing inittab - %s", line
);
295 for (i
= TAG_ID
; i
<= TAG_PROCESS
; i
++) {
296 line
[matches
[i
].rm_eo
] = '\0';
297 tags
[i
] = &line
[matches
[i
+ 1].rm_so
];
300 tok
= strtok(tags
[TAG_PROCESS
], " ");
301 for (i
= 0; i
< (MAX_ARGS
- 1) && tok
; i
++) {
303 tok
= strtok(NULL
, " ");
306 a
->id
= tags
[TAG_ID
];
309 if (add_action(a
, tags
[TAG_ACTION
]))
311 line
= malloc(LINE_LEN
);
312 a
= malloc(sizeof(struct init_action
));
313 memset(a
, 0, sizeof(struct init_action
));
319 regfree(&pat_inittab
);