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