]> git.ipfire.org Git - pakfire.git/commitdiff
pty: Fallback to the original file descriptor if the clone fails
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 6 Oct 2024 17:48:18 +0000 (17:48 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 6 Oct 2024 17:48:18 +0000 (17:48 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/pty.c

index 5ac956e79ac02ae8b60bc5aa7f0b3a119c0e1c10..9e986f76bc0c73c60c086b6fdcd3f9bef6d88d89 100644 (file)
@@ -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