inittab: use patch_stdio() for stdio redirection
[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         pid_t p;
97
98         a->proc.pid = fork();
99         if (!a->proc.pid) {
100                 p = setsid();
101
102                 if (patch_stdio(a->id))
103                         ERROR("Failed to setup i/o redirection\n");
104
105                 ioctl(STDIN_FILENO, TIOCSCTTY, 1);
106                 tcsetpgrp(STDIN_FILENO, p);
107
108                 execvp(a->argv[0], a->argv);
109                 ERROR("Failed to execute %s\n", a->argv[0]);
110                 exit(-1);
111         }
112
113         if (a->proc.pid > 0) {
114                 DEBUG(4, "Launched new %s action, pid=%d\n",
115                                         a->handler->name,
116                                         (int) a->proc.pid);
117                 uloop_process_add(&a->proc);
118         }
119 }
120
121 static void child_exit(struct uloop_process *proc, int ret)
122 {
123         struct init_action *a = container_of(proc, struct init_action, proc);
124
125         DEBUG(4, "pid:%d\n", proc->pid);
126         uloop_timeout_set(&a->tout, a->respawn);
127 }
128
129 static void respawn(struct uloop_timeout *tout)
130 {
131         struct init_action *a = container_of(tout, struct init_action, tout);
132         fork_worker(a);
133 }
134
135 static void rcdone(struct runqueue *q)
136 {
137         procd_state_next();
138 }
139
140 static void runrc(struct init_action *a)
141 {
142         if (!a->argv[1] || !a->argv[2]) {
143                 ERROR("valid format is rcS <S|K> <param>\n");
144                 return;
145         }
146
147         /* proceed even if no init or shutdown scripts run */
148         if (rcS(a->argv[1], a->argv[2], rcdone))
149                 rcdone(NULL);
150 }
151
152 static void askfirst(struct init_action *a)
153 {
154         int i;
155
156         if (!dev_exist(a->id) || (console && !strcmp(console, a->id))) {
157                 DEBUG(4, "Skipping %s\n", a->id);
158                 return;
159         }
160
161         a->tout.cb = respawn;
162         for (i = MAX_ARGS - 1; i >= 1; i--)
163                 a->argv[i] = a->argv[i - 1];
164         a->argv[0] = ask;
165         a->respawn = 500;
166
167         a->proc.cb = child_exit;
168         fork_worker(a);
169 }
170
171 static void askconsole(struct init_action *a)
172 {
173         char line[256], *tty, *split;
174         int i;
175
176         tty = get_cmdline_val("console", line, sizeof(line));
177         if (tty != NULL) {
178                 split = strchr(tty, ',');
179                 if (split != NULL)
180                         *split = '\0';
181
182                 if (!dev_exist(tty)) {
183                         DEBUG(4, "skipping %s\n", tty);
184                         return;
185                 }
186
187                 console = strdup(tty);
188                 a->id = strdup(tty);
189         }
190         else {
191                 console = NULL;
192                 a->id = NULL;
193         }
194
195         a->tout.cb = respawn;
196         for (i = MAX_ARGS - 1; i >= 1; i--)
197                 a->argv[i] = a->argv[i - 1];
198         a->argv[0] = ask;
199         a->respawn = 500;
200
201         a->proc.cb = child_exit;
202         fork_worker(a);
203 }
204
205 static void rcrespawn(struct init_action *a)
206 {
207         a->tout.cb = respawn;
208         a->respawn = 500;
209
210         a->proc.cb = child_exit;
211         fork_worker(a);
212 }
213
214 static struct init_handler handlers[] = {
215         {
216                 .name = "sysinit",
217                 .cb = runrc,
218         }, {
219                 .name = "shutdown",
220                 .cb = runrc,
221         }, {
222                 .name = "askfirst",
223                 .cb = askfirst,
224                 .multi = 1,
225         }, {
226                 .name = "askconsole",
227                 .cb = askconsole,
228                 .multi = 1,
229         }, {
230                 .name = "respawn",
231                 .cb = rcrespawn,
232                 .multi = 1,
233         }
234 };
235
236 static int add_action(struct init_action *a, const char *name)
237 {
238         int i;
239
240         for (i = 0; i < ARRAY_SIZE(handlers); i++)
241                 if (!strcmp(handlers[i].name, name)) {
242                         a->handler = &handlers[i];
243                         list_add_tail(&a->list, &actions);
244                         return 0;
245                 }
246         ERROR("Unknown init handler %s\n", name);
247         return -1;
248 }
249
250 void procd_inittab_run(const char *handler)
251 {
252         struct init_action *a;
253
254         list_for_each_entry(a, &actions, list)
255                 if (!strcmp(a->handler->name, handler)) {
256                         if (a->handler->multi) {
257                                 a->handler->cb(a);
258                                 continue;
259                         }
260                         a->handler->cb(a);
261                         break;
262                 }
263 }
264
265 void procd_inittab(void)
266 {
267 #define LINE_LEN        128
268         FILE *fp = fopen(tab, "r");
269         struct init_action *a;
270         regex_t pat_inittab;
271         regmatch_t matches[5];
272         char *line;
273
274         if (!fp) {
275                 ERROR("Failed to open %s\n", tab);
276                 return;
277         }
278
279         regcomp(&pat_inittab, "([a-zA-Z0-9]*):([a-zA-Z0-9]*):([a-zA-Z0-9]*):(.*)", REG_EXTENDED);
280         line = malloc(LINE_LEN);
281         a = malloc(sizeof(struct init_action));
282         memset(a, 0, sizeof(struct init_action));
283
284         while (fgets(line, LINE_LEN, fp)) {
285                 char *tags[TAG_PROCESS + 1];
286                 char *tok;
287                 int i;
288                 int len = strlen(line);
289
290                 while (isspace(line[len - 1]))
291                         len--;
292                 line[len] = 0;
293
294                 if (*line == '#')
295                         continue;
296
297                 if (regexec(&pat_inittab, line, 5, matches, 0))
298                         continue;
299
300                 DEBUG(4, "Parsing inittab - %s", line);
301
302                 for (i = TAG_ID; i <= TAG_PROCESS; i++) {
303                         line[matches[i].rm_eo] = '\0';
304                         tags[i] = &line[matches[i + 1].rm_so];
305                 };
306
307                 tok = strtok(tags[TAG_PROCESS], " ");
308                 for (i = 0; i < (MAX_ARGS - 1) && tok; i++) {
309                         a->argv[i] = tok;
310                         tok = strtok(NULL, " ");
311                 }
312                 a->argv[i] = NULL;
313                 a->id = tags[TAG_ID];
314                 a->line = line;
315
316                 if (add_action(a, tags[TAG_ACTION]))
317                         continue;
318                 line = malloc(LINE_LEN);
319                 a = malloc(sizeof(struct init_action));
320                 memset(a, 0, sizeof(struct init_action));
321         }
322
323         fclose(fp);
324         free(line);
325         free(a);
326         regfree(&pat_inittab);
327 }