From: Michael Tremer Date: Fri, 8 Dec 2023 16:45:39 +0000 (+0000) Subject: jail: Create a better struct to hold all PTY related stuff X-Git-Tag: 0.9.30~1287 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=c40722e3e03f276fb5d72159333df55b68d623c5;p=pakfire.git jail: Create a better struct to hold all PTY related stuff Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/jail.c b/src/libpakfire/jail.c index 264664ea8..6313d894f 100644 --- a/src/libpakfire/jail.c +++ b/src/libpakfire/jail.c @@ -38,6 +38,7 @@ #include #include #include +#include // libnl3 #include @@ -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