]> git.ipfire.org Git - pakfire.git/commitdiff
jail: Create a better struct to hold all PTY related stuff
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 8 Dec 2023 16:45:39 +0000 (16:45 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 8 Dec 2023 17:20:34 +0000 (17:20 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/jail.c

index 264664ea819badff2ad354ceaf967b6693d56633..6313d894f1c647ab9cf9435541594a95984e9c6d 100644 (file)
@@ -38,6 +38,7 @@
 #include <sys/timerfd.h>
 #include <sys/types.h>
 #include <sys/wait.h>
+#include <termios.h>
 
 // libnl3
 #include <net/if.h>
@@ -178,9 +179,32 @@ struct pakfire_jail_exec {
        struct pakfire_cgroup* cgroup;
        struct pakfire_cgroup_stats cgroup_stats;
 
-       // Console
-       char console[PATH_MAX];
-       int consolefd;
+       // PTY
+       struct pakfire_jail_pty {
+               // The path to the console
+               char console[PATH_MAX];
+
+               // The master fd
+               struct pakfire_jail_pty_master {
+                       int fd;
+
+                       enum pakfire_jail_pty_flags {
+                               PAKFIRE_JAIL_PTY_READY_TO_READ  = (1 << 0),
+                               PAKFIRE_JAIL_PTY_READY_TO_WRITE = (1 << 1),
+                       } flags;
+               } master;
+
+               // Standard Input
+               struct pakfire_jail_pty_stdio {
+                       int fd;
+                       char buffer[LINE_MAX];
+                       struct termios attrs;
+                       enum pakfire_jail_pty_flags flags;
+               } stdin;
+
+               // Standard Output
+               struct pakfire_jail_pty_stdio stdout;
+       } pty;
 };
 
 static int clone3(struct clone_args* args, size_t size) {
@@ -1047,7 +1071,7 @@ static int pakfire_jail_wait(struct pakfire_jail* jail, struct pakfire_jail_exec
                                                goto ERROR;
 
                                        // Setup PTY forwarding
-                                       if (ctx->consolefd < 0) {
+                                       if (ctx->pty.master.fd < 0) {
                                                r = pakfire_jail_setup_pty_forwarding(jail, ctx, epollfd, fd);
                                                if (r) {
                                                        CTX_ERROR(jail->ctx, "Failed setting up PTY forwarding: %s\n", strerror(-r));
@@ -1714,20 +1738,20 @@ static int pakfire_jail_open_pty(struct pakfire_jail* jail, struct pakfire_jail_
        int r;
 
        // Allocate a new PTY
-       ctx->consolefd = posix_openpt(O_RDWR|O_NONBLOCK|O_NOCTTY|O_CLOEXEC);
-       if (ctx->consolefd < 0)
+       ctx->pty.master.fd = posix_openpt(O_RDWR|O_NONBLOCK|O_NOCTTY|O_CLOEXEC);
+       if (ctx->pty.master.fd < 0)
                return -errno;
 
        // Fetch the path
-       r = ptsname_r(ctx->consolefd, ctx->console, sizeof(ctx->console));
+       r = ptsname_r(ctx->pty.master.fd, ctx->pty.console, sizeof(ctx->pty.console));
        if (r)
                return -r;
 
-       CTX_DEBUG(jail->ctx, "Allocated console at %s (%d)\n", ctx->console, ctx->consolefd);
+       CTX_DEBUG(jail->ctx, "Allocated console at %s (%d)\n", ctx->pty.console, ctx->pty.master.fd);
 
 #if 0
        // Create a symlink
-       r = pakfire_symlink(jail->ctx, "/dev/console", ctx->console);
+       r = pakfire_symlink(jail->ctx, "/dev/console", ctx->pty.console);
        if (r)
                return r;
 #endif
@@ -1882,15 +1906,15 @@ static int pakfire_jail_child(struct pakfire_jail* jail, struct pakfire_jail_exe
        }
 
        // Send the PTY master to the parent process
-       r = pakfire_jail_send_fd(jail, socket_send, ctx->consolefd);
+       r = pakfire_jail_send_fd(jail, socket_send, ctx->pty.master.fd);
        if (r) {
                CTX_ERROR(jail->ctx, "Failed sending the PTY master to the parent: %s\n", strerror(-r));
                return r;
        }
 
        // Close the master of the PTY
-       close(ctx->consolefd);
-       ctx->consolefd = -1;
+       close(ctx->pty.master.fd);
+       ctx->pty.master.fd = -1;
 
        // Close the socket
        close(socket_send);
@@ -2029,7 +2053,18 @@ static int __pakfire_jail_exec(struct pakfire_jail* jail, const char* argv[],
 
                .pidfd = -1,
 
-               .consolefd = -1,
+               // PTY
+               .pty = {
+                       .master = {
+                               .fd = -1,
+                       },
+                       .stdin = {
+                               .fd = -1,
+                       },
+                       .stdout = {
+                               .fd = -1,
+                       },
+               },
        };
 
        DEBUG(jail->pakfire, "Executing jail...\n");
@@ -2197,8 +2232,8 @@ ERROR:
        pakfire_jail_close_pipe(jail, ctx.pipes.stderr);
        if (ctx.pidfd >= 0)
                close(ctx.pidfd);
-       if (ctx.consolefd >= 0)
-               close(ctx.consolefd);
+       if (ctx.pty.master.fd >= 0)
+               close(ctx.pty.master.fd);
        pakfire_jail_close_pipe(jail, ctx.pipes.log_INFO);
        pakfire_jail_close_pipe(jail, ctx.pipes.log_ERROR);
 #ifdef ENABLE_DEBUG