From dfdc3b2d116f22a48532d8270c1a830ed68b85b8 Mon Sep 17 00:00:00 2001 From: apio Date: Wed, 12 Oct 2022 17:14:49 +0200 Subject: [PATCH] libc: close fds 0 and 1 before opening stdout and stderr --- libs/libc/src/init.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/libs/libc/src/init.cpp b/libs/libc/src/init.cpp index b17cbd46..72587cc7 100644 --- a/libs/libc/src/init.cpp +++ b/libs/libc/src/init.cpp @@ -2,9 +2,13 @@ #include #include #include +#include extern "C" void initialize_libc() { + close(0); // If it was already open, close it + close(1); // If it was already open, close it + errno = 0; // If it was not open. the kernel will throw us EBADF. Let's ignore that, since we don't care. __stderr = fopen("/dev/console", "rw"); if (!stderr) exit(errno); __stdout = fopen("/dev/console", "rw");