Show the shutdown sequence on the active virtual terminal
[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         chdir("/dev");
47         freopen(tty, "r", stdin);
48         freopen(tty, "w", stdout);
49         freopen(tty, "w", stderr);
50         chdir("/");
51         fcntl(STDERR_FILENO, F_SETFL, fcntl(STDERR_FILENO, F_GETFL) | O_NONBLOCK);
52 }
53
54 static void set_console(void)
55 {
56         const char* tty;
57         char* split;
58         char line[ 20 ];
59         const char* try[] = { "tty0", "console", NULL }; /* Try the most common outputs */
60         int f, i = 0;
61
62         tty = get_cmdline_val("console",line,sizeof(line));
63         if (tty != NULL) {
64                 split = strchr(tty, ',');
65                 if ( split != NULL )
66                         *split = '\0';
67         } else {
68                 // Try a default
69                 tty=try[i];
70                 i++;
71         }
72
73         chdir("/dev");
74         while (tty!=NULL) {
75                 f = open(tty, O_RDONLY);
76                 if (f >= 0) {
77                         close(f);
78                         break;
79                 }
80
81                 tty=try[i];
82                 i++;
83         }
84         chdir("/");
85
86         if (tty != NULL)
87                 set_stdio(tty);
88 }
89
90 static void state_enter(void)
91 {
92         char ubus_cmd[] = "/sbin/ubusd";
93
94         switch (state) {
95         case STATE_EARLY:
96                 LOG("- early -\n");
97                 watchdog_init(0);
98                 hotplug("/etc/hotplug.json");
99                 procd_coldplug();
100                 break;
101
102         case STATE_UBUS:
103                 // try to reopen incase the wdt was not available before coldplug
104                 watchdog_init(0);
105                 set_stdio("console");
106                 LOG("- ubus -\n");
107                 procd_connect_ubus();
108                 service_init();
109                 service_start_early("ubus", ubus_cmd);
110                 break;
111
112         case STATE_INIT:
113                 LOG("- init -\n");
114                 procd_inittab();
115                 procd_inittab_run("respawn");
116                 procd_inittab_run("askconsole");
117                 procd_inittab_run("askfirst");
118                 procd_inittab_run("sysinit");
119                 break;
120
121         case STATE_RUNNING:
122                 LOG("- init complete -\n");
123                 break;
124
125         case STATE_SHUTDOWN:
126                 /* Redirect output to the console for the users' benefit */
127                 set_console();
128                 LOG("- shutdown -\n");
129                 procd_inittab_run("shutdown");
130                 sync();
131                 break;
132
133         case STATE_HALT:
134                 LOG("- SIGTERM processes -\n");
135                 kill(-1, SIGTERM);
136                 sync();
137                 sleep(1);
138                 LOG("- SIGKILL processes -\n");
139                 kill(-1, SIGKILL);
140                 sync();
141                 sleep(1);
142                 if (reboot_event == RB_POWER_OFF)
143                         LOG("- power down -\n");
144                 else
145                         LOG("- reboot -\n");
146
147                 /* Allow time for last message to reach serial console, etc */
148                 sleep(1);
149
150                 /* We have to fork here, since the kernel calls do_exit(EXIT_SUCCESS)
151                  * in linux/kernel/sys.c, which can cause the machine to panic when
152                  * the init process exits... */
153                 if (!vfork( )) { /* child */
154                         reboot(reboot_event);
155                         _exit(EXIT_SUCCESS);
156                 }
157
158                 while (1)
159                         sleep(1);
160                 break;
161
162         default:
163                 ERROR("Unhandled state %d\n", state);
164                 return;
165         };
166 }
167
168 void procd_state_next(void)
169 {
170         DEBUG(4, "Change state %d -> %d\n", state, state + 1);
171         state++;
172         state_enter();
173 }
174
175 void procd_state_ubus_connect(void)
176 {
177         if (state == STATE_UBUS)
178                 procd_state_next();
179 }
180
181 void procd_shutdown(int event)
182 {
183         if (state >= STATE_SHUTDOWN)
184                 return;
185         DEBUG(2, "Shutting down system with event %x\n", event);
186         reboot_event = event;
187         state = STATE_SHUTDOWN;
188         state_enter();
189 }