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