From 40298deee7cd1e72cb5e34ef98e07bc6adff2573 Mon Sep 17 00:00:00 2001 From: Steve Bennett Date: Fri, 2 Jul 2021 19:51:43 +1000 Subject: exec: support stdin fd being closed Don't try to dup2() a file descriptor that is already correct. This can happen if, for example, stdin is closed and exec redirects stdin from a file, thus opening the input as file descriptor 0. Fixes #201 Signed-off-by: Steve Bennett --- jim-exec.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/jim-exec.c b/jim-exec.c index 5a21a1f..22bbdb8 100644 --- a/jim-exec.c +++ b/jim-exec.c @@ -1046,17 +1046,17 @@ badargs: if (pid == 0) { /* Child */ /* Set up stdin, stdout, stderr */ - if (inputId != -1) { + if (inputId != -1 && inputId != fileno(stdin)) { dup2(inputId, fileno(stdin)); close(inputId); } - if (outputId != -1) { + if (outputId != -1 && outputId != fileno(stdout)) { dup2(outputId, fileno(stdout)); if (outputId != errorId) { close(outputId); } } - if (errorId != -1) { + if (errorId != -1 && errorId != fileno(stderr)) { dup2(errorId, fileno(stderr)); close(errorId); } -- cgit v1.1