make static linking work for upgraded
[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
19 #include "procd.h"
20 #include "syslog.h"
21 #include "plug/hotplug.h"
22 #include "watchdog.h"
23 #include "service/service.h"
24
25 enum {
26 STATE_NONE = 0,
27 STATE_EARLY,
28 STATE_INIT,
29 STATE_RUNNING,
30 STATE_SHUTDOWN,
31 STATE_HALT,
32 __STATE_MAX,
33 };
34
35 static int state = STATE_NONE;
36 static int reboot_event;
37
38 static void state_enter(void)
39 {
40 char ubus_cmd[] = "/sbin/ubusd";
41
42 switch (state) {
43 case STATE_EARLY:
44 LOG("- early -\n");
45 watchdog_init(0);
46 hotplug("/etc/hotplug.json");
47 procd_coldplug();
48 break;
49
50 case STATE_INIT:
51 // try to reopen incase the wdt was not available before coldplug
52 watchdog_init(0);
53 LOG("- ubus -\n");
54 procd_connect_ubus();
55
56 LOG("- init -\n");
57 service_init();
58 service_start_early("ubus", ubus_cmd);
59
60 procd_inittab();
61 procd_inittab_run("respawn");
62 procd_inittab_run("askconsole");
63 procd_inittab_run("askfirst");
64 procd_inittab_run("sysinit");
65 break;
66
67 case STATE_RUNNING:
68 LOG("- init complete -\n");
69 break;
70
71 case STATE_SHUTDOWN:
72 LOG("- shutdown -\n");
73 procd_inittab_run("shutdown");
74 sync();
75 break;
76
77 case STATE_HALT:
78 LOG("- reboot -\n");
79 reboot(reboot_event);
80 break;
81
82 default:
83 ERROR("Unhandled state %d\n", state);
84 return;
85 };
86 }
87
88 void procd_state_next(void)
89 {
90 DEBUG(4, "Change state %d -> %d\n", state, state + 1);
91 state++;
92 state_enter();
93 }
94
95 void procd_shutdown(int event)
96 {
97 DEBUG(2, "Shutting down system with event %x\n", event);
98 reboot_event = event;
99 state = STATE_SHUTDOWN;
100 state_enter();
101 }