7621b3078913261bab03b488cb40a9b0ba08d96a
[project/procd.git] / initd / init.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/wait.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <sys/reboot.h>
19
20 #include <libubox/uloop.h>
21 #include <libubus.h>
22
23 #include <stdlib.h>
24 #include <fcntl.h>
25 #include <getopt.h>
26 #include <libgen.h>
27 #include <regex.h>
28 #include <unistd.h>
29 #include <stdio.h>
30
31 #include "init.h"
32 #include "../watchdog.h"
33
34 unsigned int debug = 0;
35
36 static void
37 signal_shutdown(int signal, siginfo_t *siginfo, void *data)
38 {
39         fprintf(stderr, "reboot\n");
40         fflush(stderr);
41         sync();
42         sleep(2);
43         reboot(RB_AUTOBOOT);
44         while (1)
45                 ;
46 }
47
48 static struct sigaction sa_shutdown = {
49         .sa_sigaction = signal_shutdown,
50         .sa_flags = SA_SIGINFO
51 };
52
53 static void
54 cmdline(void)
55 {
56         char line[256];
57         int r, fd = open("/proc/cmdline", O_RDONLY);
58         regex_t pat_cmdline;
59         regmatch_t matches[2];
60
61         if (fd < 0)
62                 return;
63
64         r = read(fd, line, sizeof(line) - 1);
65         line[r] = '\0';
66         close(fd);
67
68         regcomp(&pat_cmdline, "init_debug=([0-9]+)", REG_EXTENDED);
69         if (!regexec(&pat_cmdline, line, 2, matches, 0)) {
70                 line[matches[1].rm_eo] = '\0';
71                 debug = atoi(&line[matches[1].rm_so]);
72         }
73         regfree(&pat_cmdline);
74 }
75
76 int
77 main(int argc, char **argv)
78 {
79         pid_t pid;
80
81         sigaction(SIGTERM, &sa_shutdown, NULL);
82         sigaction(SIGUSR1, &sa_shutdown, NULL);
83         sigaction(SIGUSR2, &sa_shutdown, NULL);
84
85         early();
86         cmdline();
87         watchdog_init(1);
88
89         pid = fork();
90         if (!pid) {
91                 char *kmod[] = { "/sbin/kmodloader", "/etc/modules-boot.d/", NULL };
92
93                 if (debug < 3) {
94                         int fd = open("/dev/null", O_RDWR);
95
96                         if (fd > -1) {
97                                 dup2(fd, STDIN_FILENO);
98                                 dup2(fd, STDOUT_FILENO);
99                                 dup2(fd, STDERR_FILENO);
100                                 if (fd > STDERR_FILENO)
101                                         close(fd);
102                         }
103                 }
104                 execvp(kmod[0], kmod);
105                 ERROR("Failed to start kmodloader\n");
106                 exit(-1);
107         }
108         if (pid <= 0)
109                 ERROR("Failed to start kmodloader instance\n");
110         else
111                 waitpid(pid, NULL, 0);
112         uloop_init();
113         preinit();
114         uloop_run();
115
116         return 0;
117 }