From 9f4336a739a67e38046ec235ce055e2aa20557b7 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Fri, 27 Jun 2025 14:39:07 +0000 Subject: [PATCH] pty: Create its own type Signed-off-by: Michael Tremer --- src/pakfire/jail.c | 2 +- src/pakfire/pty.c | 68 +++++++++++++++++++++++----------------------- src/pakfire/pty.h | 12 ++++---- 3 files changed, 40 insertions(+), 42 deletions(-) diff --git a/src/pakfire/jail.c b/src/pakfire/jail.c index 13c3a34b..2ac7bfa0 100644 --- a/src/pakfire/jail.c +++ b/src/pakfire/jail.c @@ -129,7 +129,7 @@ typedef struct pakfire_jail_exec { int completed_fd; // PTY - struct pakfire_pty* pty; + pakfire_pty* pty; // Pipes struct pakfire_jail_pipes { diff --git a/src/pakfire/pty.c b/src/pakfire/pty.c index 46819e1e..91e773a4 100644 --- a/src/pakfire/pty.c +++ b/src/pakfire/pty.c @@ -39,7 +39,7 @@ #define MAX_LINE_LENGTH 16384 -struct pakfire_pty_stdio { +typedef struct pakfire_pty_stdio { // File Descriptor int fd; unsigned close_fd:1; @@ -60,7 +60,7 @@ struct pakfire_pty_stdio { // Event Source sd_event_source* event; -}; +} pakfire_pty_stdio; struct pakfire_pty { pakfire_ctx* ctx; @@ -73,14 +73,14 @@ struct pakfire_pty { int socket[2]; // The master PTY - struct pakfire_pty_stdio master; + pakfire_pty_stdio master; // The path to the PTY char path[PATH_MAX]; // Standard Input - struct pakfire_pty_stdio stdin; - struct pakfire_pty_stdio stdout; + pakfire_pty_stdio stdin; + pakfire_pty_stdio stdout; // SIGWINCH Event sd_event_source* sigwinch_event; @@ -97,7 +97,7 @@ struct pakfire_pty { } state; }; -static int pakfire_pty_same_inode(struct pakfire_pty* pty, int fd1, int fd2) { +static int pakfire_pty_same_inode(pakfire_pty* pty, int fd1, int fd2) { struct stat stat1; struct stat stat2; int r; @@ -128,8 +128,8 @@ static int pakfire_pty_same_inode(struct pakfire_pty* pty, int fd1, int fd2) { return (stat1.st_dev == stat2.st_dev) && (stat1.st_ino == stat2.st_ino); } -static int pakfire_pty_restore_attrs(struct pakfire_pty* pty, - struct pakfire_pty_stdio* stdio) { +static int pakfire_pty_restore_attrs(pakfire_pty* pty, + pakfire_pty_stdio* stdio) { int flags; int r; @@ -172,8 +172,8 @@ static int pakfire_pty_restore_attrs(struct pakfire_pty* pty, return 0; } -static int pakfire_pty_store_attrs(struct pakfire_pty* pty, - struct pakfire_pty_stdio* stdio) { +static int pakfire_pty_store_attrs(pakfire_pty* pty, + pakfire_pty_stdio* stdio) { int r; // Store all attributes @@ -197,7 +197,7 @@ static int pakfire_pty_store_attrs(struct pakfire_pty* pty, return 0; } -static int pakfire_pty_disconnect(struct pakfire_pty* pty) { +static int pakfire_pty_disconnect(pakfire_pty* pty) { DEBUG(pty->ctx, "Disconnecting the PTY\n"); // Clear events @@ -223,7 +223,7 @@ static int pakfire_pty_disconnect(struct pakfire_pty* pty) { return 0; } -static int pakfire_pty_drained(struct pakfire_pty* pty) { +static int pakfire_pty_drained(pakfire_pty* pty) { int q; int r; @@ -265,7 +265,7 @@ static int pakfire_pty_drained(struct pakfire_pty* pty) { return 1; } -static int pakfire_pty_done(struct pakfire_pty* pty, int code) { +static int pakfire_pty_done(pakfire_pty* pty, int code) { // Don't run this more than once if (pty->state == PAKFIRE_PTY_STATE_DONE) return 0; @@ -288,7 +288,7 @@ static int pakfire_pty_done(struct pakfire_pty* pty, int code) { /* This function handles the forwarding between the different pipes... */ -static int pakfire_pty_forward(struct pakfire_pty* pty) { +static int pakfire_pty_forward(pakfire_pty* pty) { int did_something; int r; @@ -529,7 +529,7 @@ ERROR: Forwards any window size changes. */ static int pakfire_pty_SIGWINCH(sd_event_source* source, const struct signalfd_siginfo* si, void* data) { - struct pakfire_pty* pty = data; + pakfire_pty* pty = data; struct winsize size; int r; @@ -550,7 +550,7 @@ static int pakfire_pty_SIGWINCH(sd_event_source* source, const struct signalfd_s return 0; } -static int pakfire_pty_drain(struct pakfire_pty* pty) { +static int pakfire_pty_drain(pakfire_pty* pty) { // Log action DEBUG(pty->ctx, "Draining requested\n"); @@ -561,7 +561,7 @@ static int pakfire_pty_drain(struct pakfire_pty* pty) { } static int pakfire_pty_exit(sd_event_source* source, void* data) { - struct pakfire_pty* self = data; + pakfire_pty* self = data; int r; DEBUG(self->ctx, "Exiting the PTY...\n"); @@ -586,7 +586,7 @@ static int pakfire_pty_exit(sd_event_source* source, void* data) { return pakfire_pty_done(self, 0); } -static int pakfire_pty_activity(struct pakfire_pty* pty, struct pakfire_pty_stdio* stdio, uint32_t events) { +static int pakfire_pty_activity(pakfire_pty* pty, pakfire_pty_stdio* stdio, uint32_t events) { // Do we have data to read? if (events & (EPOLLIN|EPOLLHUP)) stdio->io |= PAKFIRE_PTY_READY_TO_READ; @@ -603,19 +603,19 @@ static int pakfire_pty_activity(struct pakfire_pty* pty, struct pakfire_pty_stdi } static int pakfire_pty_master(sd_event_source* source, int fd, uint32_t events, void* data) { - struct pakfire_pty* pty = data; + pakfire_pty* pty = data; return pakfire_pty_activity(pty, &pty->master, events); } static int pakfire_pty_stdin(sd_event_source* source, int fd, uint32_t events, void* data) { - struct pakfire_pty* pty = data; + pakfire_pty* pty = data; return pakfire_pty_activity(pty, &pty->stdin, events); } static int pakfire_pty_stdout(sd_event_source* source, int fd, uint32_t events, void* data) { - struct pakfire_pty* pty = data; + pakfire_pty* pty = data; return pakfire_pty_activity(pty, &pty->stdout, events); } @@ -696,7 +696,7 @@ static void pakfire_pty_make_raw(struct termios* termios) { ); } -static int pakfire_pty_enable_raw_mode(struct pakfire_pty* pty) { +static int pakfire_pty_enable_raw_mode(pakfire_pty* pty) { struct termios raw_attrs; int same; int r; @@ -761,7 +761,7 @@ static int pakfire_pty_enable_raw_mode(struct pakfire_pty* pty) { return 0; } -static int pakfire_pty_reopen(struct pakfire_pty* pty, int fd, int flags) { +static int pakfire_pty_reopen(pakfire_pty* pty, int fd, int flags) { char path[PATH_MAX]; int existing_flags; off_t offset; @@ -803,7 +803,7 @@ static int pakfire_pty_reopen(struct pakfire_pty* pty, int fd, int flags) { /* Sets up PTY forwarding... */ -static int pakfire_pty_setup_forwarding(struct pakfire_pty* pty) { +static int pakfire_pty_setup_forwarding(pakfire_pty* pty) { struct winsize size; int r; @@ -899,7 +899,7 @@ static int pakfire_pty_setup_forwarding(struct pakfire_pty* pty) { /* Sends the master file descriptor to the parent process... */ -static int pakfire_pty_send_master(struct pakfire_pty* pty) { +static int pakfire_pty_send_master(pakfire_pty* pty) { const size_t payload_length = sizeof(pty->master.fd); char buffer[CMSG_SPACE(payload_length)]; int r; @@ -938,7 +938,7 @@ static int pakfire_pty_send_master(struct pakfire_pty* pty) { /* Received the master file descriptor from the child process... */ -static int pakfire_pty_recv_master(struct pakfire_pty* pty) { +static int pakfire_pty_recv_master(pakfire_pty* pty) { const size_t payload_length = sizeof(pty->master.fd); char buffer[CMSG_SPACE(payload_length)]; int r; @@ -976,7 +976,7 @@ static int pakfire_pty_recv_master(struct pakfire_pty* pty) { Called when the master socket is being received from the child process. */ static int pakfire_pty_setup(sd_event_source* source, int fd, uint32_t events, void* data) { - struct pakfire_pty* pty = data; + pakfire_pty* pty = data; int r; // Receive the master file descriptor @@ -1018,7 +1018,7 @@ static int pakfire_pty_setup(sd_event_source* source, int fd, uint32_t events, v return 0; } -static void pakfire_pty_free(struct pakfire_pty* pty) { +static void pakfire_pty_free(pakfire_pty* pty) { pakfire_pty_disconnect(pty); if (pty->socket[0] >= 0) @@ -1040,8 +1040,8 @@ static void pakfire_pty_free(struct pakfire_pty* pty) { free(pty); } -int pakfire_pty_create(struct pakfire_pty** pty, pakfire_ctx* ctx, sd_event* loop) { - struct pakfire_pty* p = NULL; +int pakfire_pty_create(pakfire_pty** pty, pakfire_ctx* ctx, sd_event* loop) { + pakfire_pty* p = NULL; int r; // Allocate a new object @@ -1098,13 +1098,13 @@ ERROR: return r; } -struct pakfire_pty* pakfire_pty_ref(struct pakfire_pty* pty) { +pakfire_pty* pakfire_pty_ref(pakfire_pty* pty) { ++pty->nrefs; return pty; } -struct pakfire_pty* pakfire_pty_unref(struct pakfire_pty* pty) { +pakfire_pty* pakfire_pty_unref(pakfire_pty* pty) { if (--pty->nrefs > 0) return pty; @@ -1115,7 +1115,7 @@ struct pakfire_pty* pakfire_pty_unref(struct pakfire_pty* pty) { /* Sets up the terminal in the child process... */ -static int pakfire_pty_setup_terminal(struct pakfire_pty* pty) { +static int pakfire_pty_setup_terminal(pakfire_pty* pty) { int fd = -EBADF; int r; @@ -1163,7 +1163,7 @@ ERROR: /* Allocates a new PTY and must be called from the child process... */ -int pakfire_pty_open(struct pakfire_pty* pty) { +int pakfire_pty_open(pakfire_pty* pty) { int r; // Allocate a new PTY diff --git a/src/pakfire/pty.h b/src/pakfire/pty.h index c4653f2a..ef485661 100644 --- a/src/pakfire/pty.h +++ b/src/pakfire/pty.h @@ -25,16 +25,14 @@ #include #include -#include -#include -struct pakfire_pty; +typedef struct pakfire_pty pakfire_pty; -int pakfire_pty_create(struct pakfire_pty** pty, pakfire_ctx* ctx, sd_event* loop); +int pakfire_pty_create(pakfire_pty** pty, pakfire_ctx* ctx, sd_event* loop); -struct pakfire_pty* pakfire_pty_ref(struct pakfire_pty* pty); -struct pakfire_pty* pakfire_pty_unref(struct pakfire_pty* pty); +pakfire_pty* pakfire_pty_ref(pakfire_pty* pty); +pakfire_pty* pakfire_pty_unref(pakfire_pty* pty); -int pakfire_pty_open(struct pakfire_pty* pty); +int pakfire_pty_open(pakfire_pty* pty); #endif /* PAKFIRE_PTY_H */ -- 2.47.2