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