X-Git-Url: http://git.archive.openwrt.org/?a=blobdiff_plain;f=state.c;h=0896e1ae34f6430ad7abdee5ba5d46f3dfc238a3;hb=07c7864d49723b1264ee8bcd6861ea92f679ee98;hp=41e4471a41fe7b6f50acfb3a2b60d4782292d0bf;hpb=f45672d80bf2fec4ccb7363de1da6adb9e3f4421;p=project%2Fprocd.git diff --git a/state.c b/state.c index 41e4471..0896e1a 100644 --- a/state.c +++ b/state.c @@ -12,7 +12,9 @@ * GNU General Public License for more details. */ +#include #include +#include #include #include #include @@ -23,6 +25,7 @@ #include "plug/hotplug.h" #include "watchdog.h" #include "service/service.h" +#include "utils/utils.h" enum { STATE_NONE = 0, @@ -38,6 +41,52 @@ enum { static int state = STATE_NONE; static int reboot_event; +static void set_stdio(const char* tty) +{ + chdir("/dev"); + freopen(tty, "r", stdin); + freopen(tty, "w", stdout); + freopen(tty, "w", stderr); + chdir("/"); + fcntl(STDERR_FILENO, F_SETFL, fcntl(STDERR_FILENO, F_GETFL) | O_NONBLOCK); +} + +static void set_console(void) +{ + const char* tty; + char* split; + char line[ 20 ]; + const char* try[] = { "tty0", "console", NULL }; /* Try the most common outputs */ + int f, i = 0; + + tty = get_cmdline_val("console",line,sizeof(line)); + if (tty != NULL) { + split = strchr(tty, ','); + if ( split != NULL ) + *split = '\0'; + } else { + // Try a default + tty=try[i]; + i++; + } + + chdir("/dev"); + while (tty!=NULL) { + f = open(tty, O_RDONLY); + if (f >= 0) { + close(f); + break; + } + + tty=try[i]; + i++; + } + chdir("/"); + + if (tty != NULL) + set_stdio(tty); +} + static void state_enter(void) { char ubus_cmd[] = "/sbin/ubusd"; @@ -53,6 +102,7 @@ static void state_enter(void) case STATE_UBUS: // try to reopen incase the wdt was not available before coldplug watchdog_init(0); + set_stdio("console"); LOG("- ubus -\n"); procd_connect_ubus(); service_init(); @@ -73,12 +123,16 @@ static void state_enter(void) break; case STATE_SHUTDOWN: + /* Redirect output to the console for the users' benefit */ + set_console(); LOG("- shutdown -\n"); procd_inittab_run("shutdown"); sync(); break; case STATE_HALT: + // To prevent killed processes from interrupting the sleep + signal(SIGCHLD, SIG_IGN); LOG("- SIGTERM processes -\n"); kill(-1, SIGTERM); sync();