]> git.ipfire.org Git - pakfire.git/commitdiff
pty: Connect stdin to /dev/null when not connected
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 15 Feb 2025 11:40:53 +0000 (11:40 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 15 Feb 2025 11:40:53 +0000 (11:40 +0000)
We don't want to run an interactive console and nothing should think
that we are.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/jail.c
src/pakfire/pty.c

index 0d7e1d42bfea186f1999b8078f69c11c55752c19..3132e40a0d7da0090355f0c893d1b2ea82db4add 100644 (file)
@@ -1290,7 +1290,7 @@ static int pakfire_jail_child(struct pakfire_jail* jail, struct pakfire_jail_exe
 
        // Open a new PTY
        r = pakfire_pty_open(ctx->pty);
-       if (r) {
+       if (r < 0) {
                ERROR(jail->ctx, "Could not open a new PTY: %s\n", strerror(-r));
                return r;
        }
index 43dc7c292e61485208170e797afef54bd28a4999..b056d6069471657b35dcacc4fef6ad8d40766df9 100644 (file)
@@ -1330,6 +1330,33 @@ struct pakfire_pty* pakfire_pty_unref(struct pakfire_pty* pty) {
        return NULL;
 }
 
+static int pakfire_pty_connect_null(struct pakfire_pty* pty) {
+       int fd = -EBADF;
+       int r;
+
+       // Open /dev/null
+       fd = open("/dev/null", O_RDONLY);
+       if (fd < 0) {
+               ERROR(pty->ctx, "Failed to open /dev/null: %m\n");
+               r = -errno;
+               goto ERROR;
+       }
+
+       // Copy to the desired file descriptor
+       r = dup2(fd, STDIN_FILENO);
+       if (r < 0) {
+               ERROR(pty->ctx, "Failed to duplicate the file descriptor: %m\n");
+               r = -errno;
+               goto ERROR;
+       }
+
+ERROR:
+       if (fd >= 0)
+               close(fd);
+
+       return r;
+}
+
 /*
        Sets up the terminal in the child process...
 */
@@ -1348,11 +1375,19 @@ static int pakfire_pty_setup_terminal(struct pakfire_pty* pty) {
        DEBUG(pty->ctx, "Opened a new terminal %d\n", fd);
 
        // Connect the new terminal to standard input
-       r = dup2(fd, STDIN_FILENO);
-       if (r < 0) {
-               ERROR(pty->ctx, "Failed to open standard input: %s\n", strerror(errno));
-               r = -errno;
-               goto ERROR;
+       if (pakfire_pty_has_flag(pty, PAKFIRE_PTY_CONNECT_STDIN)) {
+               r = dup2(fd, STDIN_FILENO);
+               if (r < 0) {
+                       ERROR(pty->ctx, "Failed to open standard input: %s\n", strerror(errno));
+                       r = -errno;
+                       goto ERROR;
+               }
+
+       // Otherwise we connect standard input to /dev/null
+       } else {
+               r = pakfire_pty_connect_null(pty);
+               if (r < 0)
+                       goto ERROR;
        }
 
        // Connect the new terminal to standard output