]> git.ipfire.org Git - pakfire.git/commitdiff
pty: Hack to avoid lock-up when we don't have a newline
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 19 Oct 2024 13:32:29 +0000 (13:32 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 19 Oct 2024 13:32:29 +0000 (13:32 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/pty.c

index 3307b6d40cb8b6a38d1f2b2c035be38400be09c4..7256d89619b5ccde3fd599237ceaf678235f61d9 100644 (file)
@@ -436,7 +436,7 @@ static int pakfire_pty_drain_buffer(struct pakfire_pty* pty, int fd, struct pakf
                // No newline found
                if (!eol) {
                        if (!pakfire_pty_buffer_is_full(pty, stdio))
-                               return 0;
+                               return -ENOMSG;
 
                        CTX_DEBUG(pty->ctx, "Buffer is full. Sending all content\n");
                        eol = stdio->buffer + stdio->buffered - 1;
@@ -482,14 +482,14 @@ static int pakfire_pty_forward(struct pakfire_pty* pty) {
                        return -EINVAL;
        }
 
-       //printf("GOT HERE %x, %x, %x\n", pty->stdin.io, pty->stdout.io, pty->master.io);
-
        while (
                        ((pty->stdin.io & PAKFIRE_PTY_READY_TO_READ) && !pakfire_pty_buffer_is_full(pty, &pty->stdin)) ||
                        ((pty->master.io & PAKFIRE_PTY_READY_TO_WRITE) && pakfire_pty_buffer_has_data(pty, &pty->stdin)) ||
                        ((pty->master.io & PAKFIRE_PTY_READY_TO_READ) && !pakfire_pty_buffer_is_full(pty, &pty->stdout)) ||
                        ((pty->stdout.io & PAKFIRE_PTY_READY_TO_WRITE) && pakfire_pty_buffer_has_data(pty, &pty->stdout))
                ) {
+               // CTX_DEBUG(pty->ctx, "PTY forward stdin=%x %zu, stdout=%x %zu, %x\n",
+               //      pty->stdin.io, pty->stdin.buffered, pty->stdout.io, pty->stdout.buffered, pty->master.io);
 
                // Read from standard input
                if (pty->stdin.io & PAKFIRE_PTY_READY_TO_READ) {
@@ -598,6 +598,14 @@ static int pakfire_pty_forward(struct pakfire_pty* pty) {
                                                        pty->stdout.io |= PAKFIRE_PTY_HANGUP;
                                                        break;
 
+                                               // This is a special hack for when we have data in the buffer
+                                               // but cannot send it to the callback, yet, because we have not
+                                               // read to the end of the line.
+                                               // To avoid getting stuck in this loop for forever, we simply
+                                               // exit and let the loop call us again.
+                                               case ENOMSG:
+                                                       return 0;
+
                                                default:
                                                        CTX_ERROR(pty->ctx, "Failed writing to standard output: %s\n", strerror(-r));
                                                        goto ERROR;