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