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