fix behaviour during sysupgrade
[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();
44                 hotplug("/etc/hotplug.json");
45                 procd_coldplug();
46                 break;
47
48         case STATE_INIT:
49                 LOG("- init -\n");
50                 log_init();
51                 procd_connect_ubus();
52                 procd_inittab();
53                 procd_inittab_run("askconsole");
54                 procd_inittab_run("askfirst");
55                 procd_inittab_run("sysinit");
56                 break;
57
58         case STATE_RUNNING:
59                 LOG("- init complete -\n");
60                 break;
61
62         case STATE_SHUTDOWN:
63                 LOG("- shutdown -\n");
64                 procd_inittab_run("shutdown");
65                 break;
66
67         case STATE_HALT:
68                 LOG("- reboot -\nprocd says good bye !\n");
69                 sync();
70                 sleep(1);
71                 reboot(reboot_event);
72                 break;
73
74         default:
75                 ERROR("Unhandled state %d\n", state);
76                 return;
77         };
78 }
79
80 void procd_state_next(void)
81 {
82         DEBUG(2, "Change state %d -> %d\n", state, state + 1);
83         state++;
84         state_enter();
85 }
86
87 void procd_shutdown(int event)
88 {
89         DEBUG(1, "Shutting down system with event %x\n", reboot_event);
90         reboot_event = event;
91         state = STATE_SHUTDOWN;
92         state_enter();
93 }