From: Michael Tremer Date: Sun, 6 Oct 2024 17:48:18 +0000 (+0000) Subject: pty: Fallback to the original file descriptor if the clone fails X-Git-Tag: 0.9.30~1126 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e4c5af89e6a960386116d25e9300504a0a919c97;p=pakfire.git pty: Fallback to the original file descriptor if the clone fails Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/pty.c b/src/libpakfire/pty.c index 5ac956e79..9e986f76b 100644 --- a/src/libpakfire/pty.c +++ b/src/libpakfire/pty.c @@ -36,6 +36,7 @@ struct pakfire_pty_stdio { // File Descriptor int fd; + unsigned close_fd:1; // Buffer char buffer[64 * 1024]; @@ -163,7 +164,9 @@ static int pakfire_pty_restore_attrs(struct pakfire_pty* pty, } // Close the file descriptor - close(stdio->fd); + if (stdio->close_fd) + close(stdio->fd); + stdio->fd = -1; return 0; @@ -595,16 +598,28 @@ static int pakfire_pty_setup_forwarding(struct pakfire_pty* pty) { if (!pakfire_pty_has_flag(pty, PAKFIRE_PTY_READ_ONLY)) { pty->stdin.fd = pakfire_pty_reopen(pty, STDIN_FILENO, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NONBLOCK); if (pty->stdin.fd < 0) { - CTX_ERROR(pty->ctx, "Could not re-open standard input: %s\n", strerror(-pty->stdin.fd)); - return -pty->stdin.fd; + CTX_DEBUG(pty->ctx, "Could not re-open standard input: %s. Ignoring.\n", strerror(-pty->stdin.fd)); + + // Use the original file descriptor + pty->stdin.fd = STDIN_FILENO; + + // Request to close the file descriptor afterwards + } else { + pty->stdin.close_fd = 1; } } // Connect to standard output pty->stdout.fd = pakfire_pty_reopen(pty, STDOUT_FILENO, O_WRONLY|O_CLOEXEC|O_NOCTTY|O_NONBLOCK); if (pty->stdout.fd < 0) { - CTX_ERROR(pty->ctx, "Could not re-open standard output: %s\n", strerror(-pty->stdout.fd)); - return -pty->stdout.fd; + CTX_DEBUG(pty->ctx, "Could not re-open standard output: %s. Ignoring.\n", strerror(-pty->stdout.fd)); + + // Use the original file descriptor + pty->stdout.fd = STDOUT_FILENO; + + // Request to close the file descriptor afterwards + } else { + pty->stdout.close_fd = 1; } // Copy the terminal dimensions to the PTY