add a rcS helper
[project/procd.git] / inittab.c
1 /*
2  * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3  * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
4  *
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
8  *
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.
13  */
14
15 #include <sys/types.h>
16 #include <sys/stat.h>
17
18 #include <fcntl.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <regex.h>
23
24 #include <libubox/utils.h>
25 #include <libubox/list.h>
26
27 #include "procd.h"
28 #include "rcS.h"
29
30 #define TAG_ID          0
31 #define TAG_RUNLVL      1
32 #define TAG_ACTION      2
33 #define TAG_PROCESS     3
34
35 #define MAX_ARGS        8
36
37 struct init_action;
38 const char *console;
39
40 struct init_handler {
41         const char *name;
42         void (*cb) (struct init_action *a);
43         int multi;
44 };
45
46 struct init_action {
47         struct list_head list;
48
49         char *id;
50         char *argv[MAX_ARGS];
51         char *line;
52
53         struct init_handler *handler;
54         struct uloop_process proc;
55
56         int respawn;
57         struct uloop_timeout tout;
58 };
59
60 static const char *tab = "/etc/inittab";
61 static char *ask = "/sbin/askfirst";
62
63 static LIST_HEAD(actions);
64
65 static void fork_worker(struct init_action *a)
66 {
67         a->proc.pid = fork();
68         if (!a->proc.pid) {
69                 execvp(a->argv[0], a->argv);
70                 ERROR("Failed to execute %s\n", a->argv[0]);
71                 exit(-1);
72         }
73
74         if (a->proc.pid > 0) {
75                 DEBUG(2, "Launched new %s action, pid=%d\n",
76                                         a->handler->name,
77                                         (int) a->proc.pid);
78                 uloop_process_add(&a->proc);
79         }
80 }
81
82 static void child_exit(struct uloop_process *proc, int ret)
83 {
84         struct init_action *a = container_of(proc, struct init_action, proc);
85
86         DEBUG(2, "pid:%d\n", proc->pid);
87         uloop_timeout_set(&a->tout, a->respawn);
88 }
89
90 static void respawn(struct uloop_timeout *tout)
91 {
92         struct init_action *a = container_of(tout, struct init_action, tout);
93         fork_worker(a);
94 }
95
96 static void rcdone(struct runqueue *q)
97 {
98         procd_state_next();
99 }
100
101 static void runrc(struct init_action *a)
102 {
103         if (!a->argv[1] || !a->argv[2]) {
104                 ERROR("valid format is rcS <S|K> <param>\n");
105                 return;
106         }
107         rcS(a->argv[1], a->argv[2], rcdone);
108 }
109
110 static void askfirst(struct init_action *a)
111 {
112         struct stat s;
113         int i;
114
115         chdir("/dev");
116         i = stat(a->id, &s);
117         chdir("/");
118         if (i || (console && !strcmp(console, a->id))) {
119                 DEBUG(2, "Skipping %s\n", a->id);
120                 return;
121         }
122
123         a->tout.cb = respawn;
124         for (i = MAX_ARGS - 2; i >= 2; i--)
125                 a->argv[i] = a->argv[i - 2];
126         a->argv[0] = ask;
127         a->argv[1] = a->id;
128         a->respawn = 500;
129
130         a->proc.cb = child_exit;
131         fork_worker(a);
132 }
133
134 static void askconsole(struct init_action *a)
135 {
136         struct stat s;
137         char line[256], *tty;
138         int i, r, fd = open("/proc/cmdline", O_RDONLY);
139         regex_t pat_cmdline;
140         regmatch_t matches[2];
141
142         if (!fd)
143                 return;
144
145         r = read(fd, line, sizeof(line) - 1);
146         line[r] = '\0';
147         close(fd);
148
149         regcomp(&pat_cmdline, "console=([a-zA-Z0-9]*)", REG_EXTENDED);
150         if (regexec(&pat_cmdline, line, 2, matches, 0))
151                 goto err_out;
152         line[matches[1].rm_eo] = '\0';
153         tty = &line[matches[1].rm_so];
154
155         chdir("/dev");
156         i = stat(tty, &s);
157         chdir("/");
158         if (i) {
159                 DEBUG(2, "skipping %s\n", tty);
160                 goto err_out;
161         }
162         console = strdup(tty);
163
164         a->tout.cb = respawn;
165         for (i = MAX_ARGS - 2; i >= 2; i--)
166                 a->argv[i] = a->argv[i - 2];
167         a->argv[0] = ask;
168         a->argv[1] = strdup(tty);
169         a->respawn = 500;
170
171         a->proc.cb = child_exit;
172         fork_worker(a);
173
174 err_out:
175         regfree(&pat_cmdline);
176 }
177
178 static struct init_handler handlers[] = {
179         {
180                 .name = "sysinit",
181                 .cb = runrc,
182         }, {
183                 .name = "shutdown",
184                 .cb = runrc,
185         }, {
186                 .name = "askfirst",
187                 .cb = askfirst,
188                 .multi = 1,
189         }, {
190                 .name = "askconsole",
191                 .cb = askconsole,
192                 .multi = 1,
193         }
194 };
195
196 static int add_action(struct init_action *a, const char *name)
197 {
198         int i;
199
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);
204                         return 0;
205                 }
206         ERROR("Unknown init handler %s\n", name);
207         return -1;
208 }
209
210 void procd_inittab_run(const char *handler)
211 {
212         struct init_action *a;
213
214         list_for_each_entry(a, &actions, list)
215                 if (!strcmp(a->handler->name, handler)) {
216                         if (a->handler->multi) {
217                                 a->handler->cb(a);
218                                 continue;
219                         }
220                         a->handler->cb(a);
221                         break;
222                 }
223 }
224
225 void procd_inittab(void)
226 {
227 #define LINE_LEN        128
228         FILE *fp = fopen(tab, "r");
229         struct init_action *a;
230         regex_t pat_inittab;
231         regmatch_t matches[5];
232         char *line;
233
234         if (!fp) {
235                 ERROR("Failed to open %s\n", tab);
236                 return;
237         }
238
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));
243
244         while (fgets(line, LINE_LEN, fp)) {
245                 char *tags[TAG_PROCESS + 1];
246                 char *tok;
247                 int i;
248
249                 if (*line == '#')
250                         continue;
251
252                 if (regexec(&pat_inittab, line, 5, matches, 0))
253                         continue;
254
255                 DEBUG(2, "Parsing inittab - %s", line);
256
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];
260                 };
261
262                 tok = strtok(tags[TAG_PROCESS], " ");
263                 for (i = 0; i < (MAX_ARGS - i - 1) && tok; i++) {
264                         a->argv[i] = tok;
265                         tok = strtok(NULL, " ");
266                 }
267                 a->argv[i] = NULL;
268                 a->id = tags[TAG_ID];
269                 a->line = line;
270
271                 if (add_action(a, tags[TAG_ACTION]))
272                         continue;
273                 line = malloc(LINE_LEN);
274                 a = malloc(sizeof(struct init_action));
275                 memset(a, 0, sizeof(struct init_action));
276         }
277
278         fclose(fp);
279         free(line);
280         free(a);
281         regfree(&pat_inittab);
282 }