]> git.ipfire.org Git - pakfire.git/commitdiff
PTY: Connect stdout/stderr to /dev/null unless something else was requested
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 16 Feb 2025 14:20:09 +0000 (14:20 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 16 Feb 2025 14:20:09 +0000 (14:20 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/jail.c
src/pakfire/pty.c
src/pakfire/pty.h

index 3132e40a0d7da0090355f0c893d1b2ea82db4add..49696ed8bc2342e1d7d1e30c22801b9308584615 100644 (file)
@@ -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;
        }
 
        /*
index c5707b388fcc777ba85fcd1de71a30da4f0f7aa5..f7ddd854cc19717b7fc2fbe85f2d71cc735ca259 100644 (file)
@@ -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:
index b9c5fdedd8010dde14e9788c08a897f5f2ba5b56..a8374def06836b85967f0e4db6d26924193891df 100644 (file)
@@ -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,