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