From ea32200a0ff9a85fd0a063b7dfdfa917a05af182 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sat, 22 Mar 2025 16:56:59 +0000 Subject: [PATCH] jail: Use log stream to perform line-buffered output This helps us to split the complexity between the PTY and some extra buffering code. Signed-off-by: Michael Tremer --- src/pakfire/jail.c | 9 ++++++--- src/pakfire/jail.h | 2 +- src/pakfire/pty.c | 24 ++++++++++++++++++++++++ src/pakfire/pty.h | 4 ++++ 4 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/pakfire/jail.c b/src/pakfire/jail.c index d004c79c..2540ae50 100644 --- a/src/pakfire/jail.c +++ b/src/pakfire/jail.c @@ -1408,7 +1408,12 @@ static int __pakfire_jail_exec(struct pakfire_jail* jail, // Do nothing if we have a callback set } else if (stdout_callback) { - // Nothing + // Perform line-buffering on the callback + r = pakfire_pty_log_stream(ctx.pty, stdout_callback, stdout_data); + if (r < 0) { + ERROR(jail->ctx, "Failed to setup log stream: %s\n", strerror(-r)); + goto ERROR; + } // Capture Output? } else if (output) { @@ -1436,8 +1441,6 @@ static int __pakfire_jail_exec(struct pakfire_jail* jail, // Configure the callbacks if (stdin_callback) pakfire_pty_set_stdin_callback(ctx.pty, stdin_callback, stdin_data); - if (stdout_callback) - pakfire_pty_set_stdout_callback(ctx.pty, stdout_callback, stdout_data); // Setup pipes for logging // INFO diff --git a/src/pakfire/jail.h b/src/pakfire/jail.h index c342cdc0..bfb5c490 100644 --- a/src/pakfire/jail.h +++ b/src/pakfire/jail.h @@ -67,7 +67,7 @@ int pakfire_jail_set_cgroup(struct pakfire_jail* jail, struct pakfire_cgroup* cg int pakfire_jail_communicate( struct pakfire_jail* jail, const char* argv[], struct pakfire_env* env, int flags, pakfire_pty_stdin_callback stdin_callback, void* stdin_data, - pakfire_pty_stdout_callback stdout_callback, void* stdout_data); + pakfire_log_stream_callback stdout_callback, void* stdout_data); // Convenience functions int pakfire_jail_run(struct pakfire* pakfire, diff --git a/src/pakfire/pty.c b/src/pakfire/pty.c index 9f87efc4..aeca59cb 100644 --- a/src/pakfire/pty.c +++ b/src/pakfire/pty.c @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -108,6 +109,9 @@ struct pakfire_pty { // Stream for output FILE* output; + + // Log Stream + struct pakfire_log_stream* stream; }; static int pakfire_pty_same_inode(struct pakfire_pty* pty, int fd1, int fd2) { @@ -1183,6 +1187,8 @@ static void pakfire_pty_free(struct pakfire_pty* pty) { if (pty->exit_event) sd_event_source_unref(pty->exit_event); + if (pty->stream) + pakfire_log_stream_unref(pty->stream); if (pty->loop) sd_event_unref(pty->loop); if (pty->ctx) @@ -1475,6 +1481,24 @@ int pakfire_pty_capture_output(struct pakfire_pty* self, char** output, size_t* return 0; } +/* + Log Stream +*/ +int pakfire_pty_log_stream(struct pakfire_pty* self, + pakfire_log_stream_callback callback, void* data) { + int r; + + // Create a new log stream + r = pakfire_log_stream_create(&self->stream, self->ctx, callback, data); + if (r < 0) + return r; + + // Register the callback + pakfire_pty_set_stdout_callback(self, pakfire_log_stream_pty, self->stream); + + return 0; +} + /* Interactive Mode */ diff --git a/src/pakfire/pty.h b/src/pakfire/pty.h index a45e53f0..e2a4e20e 100644 --- a/src/pakfire/pty.h +++ b/src/pakfire/pty.h @@ -26,6 +26,7 @@ #include #include +#include struct pakfire_pty; @@ -39,6 +40,9 @@ int pakfire_pty_open(struct pakfire_pty* pty); int pakfire_pty_interactive(struct pakfire_pty* pty); int pakfire_pty_capture_output(struct pakfire_pty* pty, char** output, size_t* length); +int pakfire_pty_log_stream(struct pakfire_pty* pty, + pakfire_log_stream_callback callback, void* data); + typedef ssize_t (*pakfire_pty_stdin_callback)( struct pakfire_ctx* ctx, void* data, char* buffer, size_t length); typedef int (*pakfire_pty_stdout_callback)( -- 2.39.5