From d73a270fb1ea3c100f54413cebd9ab1fc847a452 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Fri, 18 Apr 2025 18:24:48 +0300 Subject: [PATCH] Shell: Save and restore stdin flags after child returns If child makes stdin nonblocking, Shell now restores it back to "normal" flags --- userspace/programs/Shell/Execute.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/userspace/programs/Shell/Execute.cpp b/userspace/programs/Shell/Execute.cpp index a4ee5c9a..2be027eb 100644 --- a/userspace/programs/Shell/Execute.cpp +++ b/userspace/programs/Shell/Execute.cpp @@ -4,6 +4,7 @@ #include +#include #include #include #include @@ -192,6 +193,10 @@ BAN::ErrorOr Execute::execute_command(const PipedCommand& piped_command) return result; }; + const int stdin_flags = fcntl(STDIN_FILENO, F_GETFL); + if (stdin_flags == -1) + perror("fcntl"); + for (size_t i = 0; i < piped_command.commands.size(); i++) { int new_pipe[2] { STDIN_FILENO, STDOUT_FILENO }; @@ -278,8 +283,13 @@ BAN::ErrorOr Execute::execute_command(const PipedCommand& piped_command) ASSERT_NOT_REACHED(); } - if (isatty(STDIN_FILENO) && tcsetpgrp(0, getpgrp()) == -1) - perror("tcsetpgrp"); + if (isatty(STDIN_FILENO)) + { + if (tcsetpgrp(0, getpgrp()) == -1) + perror("tcsetpgrp"); + if (stdin_flags != -1 && fcntl(STDIN_FILENO, F_SETFL, stdin_flags) == -1) + perror("fcntl"); + } m_last_return_value = child_codes.back(); return {};