]> git.ipfire.org Git - pakfire.git/commitdiff
jail: PTY forwarding: Call the output callback if available
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 19 Dec 2023 13:10:02 +0000 (13:10 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 19 Dec 2023 13:10:02 +0000 (13:10 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/jail.c

index 83e7af91b0317ec1e4bcda684e38c27762477478..764bb5a804517d5fdec71c6b0cd41170a4e9f311 100644 (file)
@@ -1122,10 +1122,24 @@ static int pakfire_jail_forward_pty(struct pakfire_jail* jail, struct pakfire_ja
 
        // Write to standard output
        if (ctx->pty.stdout.flags & PAKFIRE_JAIL_PTY_READY_TO_WRITE) {
-               r = pakfire_jail_drain_buffer(jail, ctx->pty.stdout.fd, &ctx->pty.stdout.buffer);
-               if (r) {
-                       CTX_ERROR(jail->ctx, "Failed writing to standard output: %s\n", strerror(-r));
-                       return r;
+               // If we have a callback, we will send any output to the callback
+               if (ctx->communicate.out) {
+                       r = pakfire_jail_drain_buffer_with_callback(jail, &ctx->pty.stdout.buffer,
+                               LOG_INFO, ctx->communicate.out, ctx->communicate.data);
+                       if (r)
+                               return r;
+
+               // If we have a file descriptor, we will forward any output
+               } else if (ctx->pty.stdout.fd >= 0) {
+                       r = pakfire_jail_drain_buffer(jail, ctx->pty.stdout.fd, &ctx->pty.stdout.buffer);
+                       if (r) {
+                               CTX_ERROR(jail->ctx, "Failed writing to standard output: %s\n", strerror(-r));
+                               return r;
+                       }
+
+               // Otherwise we log a message
+               } else {
+                       CTX_ERROR(jail->ctx, "No output configured for the PTY\n");
                }
 
                // We are done writing for now
@@ -1389,9 +1403,10 @@ int pakfire_jail_capture_stdout(struct pakfire* pakfire, void* data,
 
        // Append everything from stdout to a buffer
        if (output && priority == LOG_INFO) {
-               r = asprintf(output, "%s%s", (output && *output) ? *output : "", line);
+               r = asprintf(output, "%s%.*s", (output && *output) ? *output : "", (int)length, line);
                if (r < 0)
-                       return 1;
+                       return -errno;
+
                return 0;
        }