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