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.
17 #include <sys/reboot.h>
21 #include <sys/types.h>
24 #include "container.h"
27 #include "plug/hotplug.h"
29 #include "service/service.h"
30 #include "utils/utils.h"
43 static int state
= STATE_NONE
;
44 static int reboot_event
;
46 static void set_stdio(const char* tty
)
49 !freopen(tty
, "r", stdin
) ||
50 !freopen(tty
, "w", stdout
) ||
51 !freopen(tty
, "w", stderr
) ||
53 ERROR("failed to set stdio: %m\n");
55 fcntl(STDERR_FILENO
, F_SETFL
, fcntl(STDERR_FILENO
, F_GETFL
) | O_NONBLOCK
);
58 static void set_console(void)
63 const char* try[] = { "tty0", "console", NULL
}; /* Try the most common outputs */
66 tty
= get_cmdline_val("console",line
,sizeof(line
));
68 split
= strchr(tty
, ',');
78 ERROR("failed to change dir to /dev: %m\n");
82 f
= open(tty
, O_RDONLY
);
92 ERROR("failed to change dir to /: %m\n");
98 static void perform_halt()
100 if (reboot_event
== RB_POWER_OFF
)
101 LOG("- power down -\n");
105 /* Allow time for last message to reach serial console, etc */
108 if (is_container()) {
109 reboot(reboot_event
);
114 /* We have to fork here, since the kernel calls do_exit(EXIT_SUCCESS)
115 * in linux/kernel/sys.c, which can cause the machine to panic when
116 * the init process exits... */
117 if (!vfork()) { /* child */
118 reboot(reboot_event
);
126 static void state_enter(void)
128 char ubus_cmd
[] = "/sbin/ubusd";
135 hotplug("/etc/hotplug.json");
140 // try to reopen incase the wdt was not available before coldplug
142 set_stdio("console");
143 p
= getpwnam("ubus");
147 mkdir(p
->pw_dir
, 0755);
148 ret
= chown(p
->pw_dir
, p
->pw_uid
, p
->pw_gid
);
150 LOG("- ubus - failed to chown(%s)\n", p
->pw_dir
);
152 LOG("- ubus (running as root!) -\n");
155 procd_connect_ubus();
156 service_start_early("ubus", ubus_cmd
, p
?"ubus":NULL
, p
?"ubus":NULL
);
162 procd_inittab_run("respawn");
163 procd_inittab_run("askconsole");
164 procd_inittab_run("askfirst");
165 procd_inittab_run("sysinit");
167 // switch to syslog log channel
168 ulog_open(ULOG_SYSLOG
, LOG_DAEMON
, "procd");
172 LOG("- init complete -\n");
173 procd_inittab_run("respawnlate");
174 procd_inittab_run("askconsolelate");
178 /* Redirect output to the console for the users' benefit */
180 LOG("- shutdown -\n");
181 procd_inittab_run("shutdown");
186 // To prevent killed processes from interrupting the sleep
187 signal(SIGCHLD
, SIG_IGN
);
188 LOG("- SIGTERM processes -\n");
192 LOG("- SIGKILL processes -\n");
204 ERROR("Unhandled state %d\n", state
);
209 void procd_state_next(void)
211 DEBUG(4, "Change state %d -> %d\n", state
, state
+ 1);
216 void procd_state_ubus_connect(void)
218 if (state
== STATE_UBUS
)
222 void procd_shutdown(int event
)
224 if (state
>= STATE_SHUTDOWN
)
226 DEBUG(2, "Shutting down system with event %x\n", event
);
227 reboot_event
= event
;
228 state
= STATE_SHUTDOWN
;