Fix 'reboot' message when the system is powering down
[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 <sys/reboot.h>
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <sys/types.h>
19 #include <signal.h>
20
21 #include "procd.h"
22 #include "syslog.h"
23 #include "plug/hotplug.h"
24 #include "watchdog.h"
25 #include "service/service.h"
26
27 enum {
28         STATE_NONE = 0,
29         STATE_EARLY,
30         STATE_INIT,
31         STATE_RUNNING,
32         STATE_SHUTDOWN,
33         STATE_HALT,
34         __STATE_MAX,
35 };
36
37 static int state = STATE_NONE;
38 static int reboot_event;
39
40 static void state_enter(void)
41 {
42         char ubus_cmd[] = "/sbin/ubusd";
43
44         switch (state) {
45         case STATE_EARLY:
46                 LOG("- early -\n");
47                 watchdog_init(0);
48                 hotplug("/etc/hotplug.json");
49                 procd_coldplug();
50                 break;
51
52         case STATE_INIT:
53                 // try to reopen incase the wdt was not available before coldplug
54                 watchdog_init(0);
55                 LOG("- ubus -\n");
56                 procd_connect_ubus();
57
58                 LOG("- init -\n");
59                 service_init();
60                 service_start_early("ubus", ubus_cmd);
61
62                 procd_inittab();
63                 procd_inittab_run("respawn");
64                 procd_inittab_run("askconsole");
65                 procd_inittab_run("askfirst");
66                 procd_inittab_run("sysinit");
67                 break;
68
69         case STATE_RUNNING:
70                 LOG("- init complete -\n");
71                 break;
72
73         case STATE_SHUTDOWN:
74                 LOG("- shutdown -\n");
75                 procd_inittab_run("shutdown");
76                 sync();
77                 break;
78
79         case STATE_HALT:
80                 LOG("- SIGTERM processes -\n");
81                 kill(-1, SIGTERM);
82                 sync();
83                 sleep(1);
84                 LOG("- SIGKILL processes -\n");
85                 kill(-1, SIGKILL);
86                 sync();
87                 sleep(1);
88                 if (reboot_event == RB_POWER_OFF)
89                         LOG("- power down -\n");
90                 else
91                         LOG("- reboot -\n");
92
93                 /* Allow time for last message to reach serial console, etc */
94                 sleep(1);
95
96                 /* We have to fork here, since the kernel calls do_exit(EXIT_SUCCESS)
97                  * in linux/kernel/sys.c, which can cause the machine to panic when
98                  * the init process exits... */
99                 if (!vfork( )) { /* child */
100                         reboot(reboot_event);
101                         _exit(EXIT_SUCCESS);
102                 }
103
104                 while (1)
105                         sleep(1);
106                 break;
107
108         default:
109                 ERROR("Unhandled state %d\n", state);
110                 return;
111         };
112 }
113
114 void procd_state_next(void)
115 {
116         DEBUG(4, "Change state %d -> %d\n", state, state + 1);
117         state++;
118         state_enter();
119 }
120
121 void procd_shutdown(int event)
122 {
123         if (state >= STATE_SHUTDOWN)
124                 return;
125         DEBUG(2, "Shutting down system with event %x\n", event);
126         reboot_event = event;
127         state = STATE_SHUTDOWN;
128         state_enter();
129 }