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