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