From dd8eb278d4c2f1b96828a280937e6159bc583f8c Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Sun, 26 Oct 2025 14:42:39 +0100 Subject: init: make reopen_console() more robust against lack of console Better not close the existing console until open() succeeds. This will be useful with containers where stdio is mapped to pipes and where /dev/console doesn't exist. --- init/init.c | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/init/init.c b/init/init.c index 74575f7..a584d9b 100644 --- a/init/init.c +++ b/init/init.c @@ -1138,24 +1138,21 @@ static int recursive_mkdir(const char *path, mode_t mode) return ret; } -/* closes 0,1,2 and opens /dev/console on them instead */ +/* tries to open /dev/console, and maps 0,1,2 on it if successful */ static void reopen_console() { - int i, fd, oldfd; - - oldfd = dup2(0, 3); // keep a valid console on fd 3 - - for (i = 0; i < 3; i++) - close(i); + int fd; fd = open("/dev/console", O_RDWR, 0); // fd = 0 (stdin) or -1 (error) if (fd < 0) - dup2(oldfd, 0); // restore 0 from old console - - close(oldfd); - dup2(0, 1); // stdout - dup2(0, 2); // stderr - + return; + + /* OK, let's replace 0/1/2 with this console */ + dup2(fd, 0); + dup2(fd, 1); + dup2(fd, 2); + if (fd > 2) + close (fd); debug("init/info : reopened /dev/console\n"); } -- 2.17.5