]> git.ipfire.org Git - pakfire.git/commitdiff
jail: Use log stream to perform line-buffered output
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 22 Mar 2025 16:56:59 +0000 (16:56 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 22 Mar 2025 16:56:59 +0000 (16:56 +0000)
This helps us to split the complexity between the PTY and some extra
buffering code.

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

index d004c79c04a1bb3b6ab50d8e36659879a178f1ab..2540ae502ff9c963e73270d730559ff8d45e63a2 100644 (file)
@@ -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
index c342cdc0aa2bf12eba76bc2895fbebfd5bd5dd37..bfb5c490b908d7838cb2d9a38a3163435325d87b 100644 (file)
@@ -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,
index 9f87efc43575bb254902d683ca76c7558c2b18ff..aeca59cb95bf8f8062eeca36d3ec13840e1257cb 100644 (file)
@@ -32,6 +32,7 @@
 #include <pakfire/ctx.h>
 #include <pakfire/file.h>
 #include <pakfire/filelist.h>
+#include <pakfire/log_stream.h>
 #include <pakfire/pty.h>
 #include <pakfire/string.h>
 #include <pakfire/util.h>
@@ -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
 */
index a45e53f0f40bdb8587d3438427413838ee1eaffc..e2a4e20e6c131edfc1896bb1ce11017f0680325a 100644 (file)
@@ -26,6 +26,7 @@
 
 #include <pakfire/ctx.h>
 #include <pakfire/filelist.h>
+#include <pakfire/log_stream.h>
 
 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)(