procd: shift arguments for askfirst only once
[project/procd.git] / state.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 <fcntl.h>
16 #include <sys/reboot.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <sys/types.h>
21 #include <signal.h>
22
23 #include "container.h"
24 #include "procd.h"
25 #include "syslog.h"
26 #include "plug/hotplug.h"
27 #include "watchdog.h"
28 #include "service/service.h"
29 #include "utils/utils.h"
30
31 enum {
32 STATE_NONE = 0,
33 STATE_EARLY,
34 STATE_UBUS,
35 STATE_INIT,
36 STATE_RUNNING,
37 STATE_SHUTDOWN,
38 STATE_HALT,
39 __STATE_MAX,
40 };
41
42 static int state = STATE_NONE;
43 static int reboot_event;
44
45 static void set_stdio(const char* tty)
46 {
47 if (chdir("/dev") ||
48 !freopen(tty, "r", stdin) ||
49 !freopen(tty, "w", stdout) ||
50 !freopen(tty, "w", stderr) ||
51 chdir("/"))
52 ERROR("failed to set stdio: %m\n");
53 else
54 fcntl(STDERR_FILENO, F_SETFL, fcntl(STDERR_FILENO, F_GETFL) | O_NONBLOCK);
55 }
56
57 static void set_console(void)
58 {
59 const char* tty;
60 char* split;
61 char line[ 20 ];
62 const char* try[] = { "tty0", "console", NULL }; /* Try the most common outputs */
63 int f, i = 0;
64
65 tty = get_cmdline_val("console",line,sizeof(line));
66 if (tty != NULL) {
67 split = strchr(tty, ',');
68 if ( split != NULL )
69 *split = '\0';
70 } else {
71 // Try a default
72 tty=try[i];
73 i++;
74 }
75
76 if (chdir("/dev")) {
77 ERROR("failed to change dir to /dev: %m\n");
78 return;
79 }
80 while (tty!=NULL) {
81 f = open(tty, O_RDONLY);
82 if (f >= 0) {
83 close(f);
84 break;
85 }
86
87 tty=try[i];
88 i++;
89 }
90 if (chdir("/"))
91 ERROR("failed to change dir to /: %m\n");
92
93 if (tty != NULL)
94 set_stdio(tty);
95 }
96
97 static void state_enter(void)
98 {
99 char ubus_cmd[] = "/sbin/ubusd";
100
101 switch (state) {
102 case STATE_EARLY:
103 LOG("- early -\n");
104 watchdog_init(0);
105 hotplug("/etc/hotplug.json");
106 procd_coldplug();
107 break;
108
109 case STATE_UBUS:
110 // try to reopen incase the wdt was not available before coldplug
111 watchdog_init(0);
112 set_stdio("console");
113 LOG("- ubus -\n");
114 procd_connect_ubus();
115 service_start_early("ubus", ubus_cmd);
116 break;
117
118 case STATE_INIT:
119 LOG("- init -\n");
120 procd_inittab();
121 procd_inittab_run("respawn");
122 procd_inittab_run("askconsole");
123 procd_inittab_run("askfirst");
124 procd_inittab_run("sysinit");
125
126 // switch to syslog log channel
127 ulog_open(ULOG_SYSLOG, LOG_DAEMON, "procd");
128 break;
129
130 case STATE_RUNNING:
131 LOG("- init complete -\n");
132 procd_inittab_run("respawnlate");
133 procd_inittab_run("askconsolelate");
134 break;
135
136 case STATE_SHUTDOWN:
137 /* Redirect output to the console for the users' benefit */
138 set_console();
139 LOG("- shutdown -\n");
140 procd_inittab_run("shutdown");
141 sync();
142 break;
143
144 case STATE_HALT:
145 // To prevent killed processes from interrupting the sleep
146 signal(SIGCHLD, SIG_IGN);
147 LOG("- SIGTERM processes -\n");
148 kill(-1, SIGTERM);
149 sync();
150 sleep(1);
151 LOG("- SIGKILL processes -\n");
152 kill(-1, SIGKILL);
153 sync();
154 sleep(1);
155 #ifndef DISABLE_INIT
156 if (reboot_event == RB_POWER_OFF)
157 LOG("- power down -\n");
158 else
159 LOG("- reboot -\n");
160
161 if (!is_container()) {
162 /* Allow time for last message to reach serial console, etc */
163 sleep(1);
164
165 /* We have to fork here, since the kernel calls do_exit(EXIT_SUCCESS)
166 * in linux/kernel/sys.c, which can cause the machine to panic when
167 * the init process exits... */
168 if (!vfork( )) { /* child */
169 reboot(reboot_event);
170 _exit(EXIT_SUCCESS);
171 }
172
173 while (1)
174 sleep(1);
175 } else
176 exit(0);
177 #else
178 exit(0);
179 #endif
180 break;
181
182 default:
183 ERROR("Unhandled state %d\n", state);
184 return;
185 };
186 }
187
188 void procd_state_next(void)
189 {
190 DEBUG(4, "Change state %d -> %d\n", state, state + 1);
191 state++;
192 state_enter();
193 }
194
195 void procd_state_ubus_connect(void)
196 {
197 if (state == STATE_UBUS)
198 procd_state_next();
199 }
200
201 void procd_shutdown(int event)
202 {
203 if (state >= STATE_SHUTDOWN)
204 return;
205 DEBUG(2, "Shutting down system with event %x\n", event);
206 reboot_event = event;
207 state = STATE_SHUTDOWN;
208 state_enter();
209 }