2 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
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
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.
16 #include <sys/reboot.h>
20 #include <sys/types.h>
23 #include "container.h"
26 #include "plug/hotplug.h"
28 #include "service/service.h"
29 #include "utils/utils.h"
42 static int state
= STATE_NONE
;
43 static int reboot_event
;
45 static void set_stdio(const char* tty
)
48 !freopen(tty
, "r", stdin
) ||
49 !freopen(tty
, "w", stdout
) ||
50 !freopen(tty
, "w", stderr
) ||
52 ERROR("failed to set stdio: %m\n");
54 fcntl(STDERR_FILENO
, F_SETFL
, fcntl(STDERR_FILENO
, F_GETFL
) | O_NONBLOCK
);
57 static void set_console(void)
62 const char* try[] = { "tty0", "console", NULL
}; /* Try the most common outputs */
65 tty
= get_cmdline_val("console",line
,sizeof(line
));
67 split
= strchr(tty
, ',');
77 ERROR("failed to change dir to /dev: %m\n");
81 f
= open(tty
, O_RDONLY
);
91 ERROR("failed to change dir to /: %m\n");
97 static void perform_halt()
99 if (reboot_event
== RB_POWER_OFF
)
100 LOG("- power down -\n");
104 /* Allow time for last message to reach serial console, etc */
107 if (is_container()) {
108 reboot(reboot_event
);
113 /* We have to fork here, since the kernel calls do_exit(EXIT_SUCCESS)
114 * in linux/kernel/sys.c, which can cause the machine to panic when
115 * the init process exits... */
116 if (!vfork()) { /* child */
117 reboot(reboot_event
);
125 static void state_enter(void)
127 char ubus_cmd
[] = "/sbin/ubusd";
133 hotplug("/etc/hotplug.json");
138 // try to reopen incase the wdt was not available before coldplug
140 set_stdio("console");
142 procd_connect_ubus();
143 service_start_early("ubus", ubus_cmd
);
149 procd_inittab_run("respawn");
150 procd_inittab_run("askconsole");
151 procd_inittab_run("askfirst");
152 procd_inittab_run("sysinit");
154 // switch to syslog log channel
155 ulog_open(ULOG_SYSLOG
, LOG_DAEMON
, "procd");
159 LOG("- init complete -\n");
160 procd_inittab_run("respawnlate");
161 procd_inittab_run("askconsolelate");
165 /* Redirect output to the console for the users' benefit */
167 LOG("- shutdown -\n");
168 procd_inittab_run("shutdown");
173 // To prevent killed processes from interrupting the sleep
174 signal(SIGCHLD
, SIG_IGN
);
175 LOG("- SIGTERM processes -\n");
179 LOG("- SIGKILL processes -\n");
191 ERROR("Unhandled state %d\n", state
);
196 void procd_state_next(void)
198 DEBUG(4, "Change state %d -> %d\n", state
, state
+ 1);
203 void procd_state_ubus_connect(void)
205 if (state
== STATE_UBUS
)
209 void procd_shutdown(int event
)
211 if (state
>= STATE_SHUTDOWN
)
213 DEBUG(2, "Shutting down system with event %x\n", event
);
214 reboot_event
= event
;
215 state
= STATE_SHUTDOWN
;