From 49cbdf5820b9293bf3e45af8ac38c14268324c92 Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Sun, 26 Oct 2025 15:51:10 +0100 Subject: init: do not replace console if already attached to a pipe If any of stdin/stdout/stderr is connected to a pipe, then it means that init was called under supervision of another process, which typically matches what is done inside containers. In this case we do not want to replace them with /dev/console. So let's add a test for this specific case. --- init/init.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/init/init.c b/init/init.c index a584d9b..fb1e820 100644 --- a/init/init.c +++ b/init/init.c @@ -1141,8 +1141,17 @@ static int recursive_mkdir(const char *path, mode_t mode) /* tries to open /dev/console, and maps 0,1,2 on it if successful */ static void reopen_console() { + struct stat statf; int fd; + /* if any of 0/1/2 is connected to a pipe, there's an upper caller, and + * it generally means we're called inside a container so we must no + * reopen the console or we'll lose connectivity to that controller. + */ + for (fd = 0; fd < 3; fd++) + if (fstat(fd, &statf) == 0 && S_ISFIFO(statf.st_mode)) + return; + fd = open("/dev/console", O_RDWR, 0); // fd = 0 (stdin) or -1 (error) if (fd < 0) return; -- 2.17.5