Kill processes on shutdown
[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                 LOG("- reboot -\n");
89                 reboot(reboot_event);
90                 break;
91
92         default:
93                 ERROR("Unhandled state %d\n", state);
94                 return;
95         };
96 }
97
98 void procd_state_next(void)
99 {
100         DEBUG(4, "Change state %d -> %d\n", state, state + 1);
101         state++;
102         state_enter();
103 }
104
105 void procd_shutdown(int event)
106 {
107         if (state >= STATE_SHUTDOWN)
108                 return;
109         DEBUG(2, "Shutting down system with event %x\n", event);
110         reboot_event = event;
111         state = STATE_SHUTDOWN;
112         state_enter();
113 }