From: Michael Tremer Date: Sun, 16 Feb 2025 14:20:09 +0000 (+0000) Subject: PTY: Connect stdout/stderr to /dev/null unless something else was requested X-Git-Tag: 0.9.30~8 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=e5bba5ad0eb43109c46371589726aa8ccf7f0be0;p=pakfire.git PTY: Connect stdout/stderr to /dev/null unless something else was requested Signed-off-by: Michael Tremer --- diff --git a/src/pakfire/jail.c b/src/pakfire/jail.c index 3132e40a..49696ed8 100644 --- a/src/pakfire/jail.c +++ b/src/pakfire/jail.c @@ -1423,7 +1423,7 @@ static int __pakfire_jail_exec(struct pakfire_jail* jail, // Otherwise we dump everything to the console } else { - pty_flags |= PAKFIRE_PTY_CONNECT_STDOUT; + pty_flags |= PAKFIRE_PTY_CONNECT_STDOUT|PAKFIRE_PTY_CONNECT_STDERR; } /* diff --git a/src/pakfire/pty.c b/src/pakfire/pty.c index c5707b38..f7ddd854 100644 --- a/src/pakfire/pty.c +++ b/src/pakfire/pty.c @@ -1330,12 +1330,28 @@ struct pakfire_pty* pakfire_pty_unref(struct pakfire_pty* pty) { return NULL; } -static int pakfire_pty_connect_null(struct pakfire_pty* pty) { +static int pakfire_pty_connect_null(struct pakfire_pty* pty, int fileno) { int fd = -EBADF; + int flags = 0; int r; + switch (fileno) { + case STDIN_FILENO: + flags |= O_RDONLY; + break; + + case STDOUT_FILENO: + case STDERR_FILENO: + flags |= O_WRONLY; + break; + + default: + return -EINVAL; + } + + // Open /dev/null - fd = open("/dev/null", O_RDONLY); + fd = open("/dev/null", flags); if (fd < 0) { ERROR(pty->ctx, "Failed to open /dev/null: %m\n"); r = -errno; @@ -1343,7 +1359,7 @@ static int pakfire_pty_connect_null(struct pakfire_pty* pty) { } // Copy to the desired file descriptor - r = dup2(fd, STDIN_FILENO); + r = dup2(fd, fileno); if (r < 0) { ERROR(pty->ctx, "Failed to duplicate the file descriptor: %m\n"); r = -errno; @@ -1385,25 +1401,41 @@ static int pakfire_pty_setup_terminal(struct pakfire_pty* pty) { // Otherwise we connect standard input to /dev/null } else { - r = pakfire_pty_connect_null(pty); + r = pakfire_pty_connect_null(pty, STDIN_FILENO); if (r < 0) goto ERROR; } // Connect the new terminal to standard output - r = dup2(fd, STDOUT_FILENO); - if (r < 0) { - ERROR(pty->ctx, "Failed to open standard output: %s\n", strerror(errno)); - r = -errno; - goto ERROR; + if (pakfire_pty_has_flag(pty, PAKFIRE_PTY_CONNECT_STDOUT)) { + r = dup2(fd, STDOUT_FILENO); + if (r < 0) { + ERROR(pty->ctx, "Failed to open standard output: %s\n", strerror(errno)); + r = -errno; + goto ERROR; + } + + // Otherwise we connect standard output to /dev/null + } else { + r = pakfire_pty_connect_null(pty, STDOUT_FILENO); + if (r < 0) + goto ERROR; } // Connect the new terminal to standard error - r = dup2(fd, STDERR_FILENO); - if (r < 0) { - ERROR(pty->ctx, "Failed to open standard error: %s\n", strerror(errno)); - r = -errno; - goto ERROR; + if (pakfire_pty_has_flag(pty, PAKFIRE_PTY_CONNECT_STDERR)) { + r = dup2(fd, STDERR_FILENO); + if (r < 0) { + ERROR(pty->ctx, "Failed to open standard error: %s\n", strerror(errno)); + r = -errno; + goto ERROR; + } + + // Otherwise we connect standard error to /dev/null + } else { + r = pakfire_pty_connect_null(pty, STDERR_FILENO); + if (r < 0) + goto ERROR; } ERROR: diff --git a/src/pakfire/pty.h b/src/pakfire/pty.h index b9c5fded..a8374def 100644 --- a/src/pakfire/pty.h +++ b/src/pakfire/pty.h @@ -32,8 +32,12 @@ struct pakfire_pty; enum pakfire_pty_flags { PAKFIRE_PTY_CONNECT_STDIN = (1 << 0), PAKFIRE_PTY_CONNECT_STDOUT = (1 << 1), - PAKFIRE_PTY_INTERACTIVE = PAKFIRE_PTY_CONNECT_STDIN|PAKFIRE_PTY_CONNECT_STDOUT, - PAKFIRE_PTY_CAPTURE_OUTPUT = (1 << 2), + PAKFIRE_PTY_CONNECT_STDERR = (1 << 2), + PAKFIRE_PTY_INTERACTIVE = \ + PAKFIRE_PTY_CONNECT_STDIN | \ + PAKFIRE_PTY_CONNECT_STDOUT| \ + PAKFIRE_PTY_CONNECT_STDERR, + PAKFIRE_PTY_CAPTURE_OUTPUT = (1 << 3), }; int pakfire_pty_create(struct pakfire_pty** pty,