]> git.ipfire.org Git - pakfire.git/blobdiff - src/libpakfire/jail.c
jail: Refactor how to drain logging buffers with callbacks
[pakfire.git] / src / libpakfire / jail.c
index bdf3f320d545b57b264a183a497dcf698108ec72..29b0198a8a09c0bfc8846b1a2699d3f03c13709d 100644 (file)
@@ -144,10 +144,6 @@ struct pakfire_jail_exec {
 
        // Log pipes
        struct pakfire_jail_pipes {
-               int stdin[2];
-               int stdout[2];
-               int stderr[2];
-
                // Logging
                int log_INFO[2];
                int log_ERROR[2];
@@ -197,8 +193,9 @@ struct pakfire_jail_exec {
                // Standard Input
                struct pakfire_jail_pty_stdio {
                        int fd;
-                       char buffer[LINE_MAX];
+                       struct pakfire_log_buffer buffer;
                        struct termios attrs;
+                       int fdflags;
                        enum pakfire_jail_pty_flags flags;
                } stdin;
 
@@ -598,66 +595,70 @@ static int pakfire_jail_log_buffer_is_full(const struct pakfire_log_buffer* buff
        return (sizeof(buffer->data) == buffer->used);
 }
 
-/*
-       This function reads as much data as it can from the file descriptor.
-       If it finds a whole line in it, it will send it to the logger and repeat the process.
-       If not newline character is found, it will try to read more data until it finds one.
-*/
-static int pakfire_jail_handle_log(struct pakfire_jail* jail,
-               struct pakfire_jail_exec* ctx, int priority, int fd,
-               struct pakfire_log_buffer* buffer, pakfire_jail_communicate_out callback, void* data) {
-       char line[BUFFER_SIZE + 1];
+static int pakfire_jail_fill_buffer(struct pakfire_jail* jail, int fd, struct pakfire_log_buffer* buffer) {
+       int r;
 
-       // Fill up buffer from fd
-       if (buffer->used < sizeof(buffer->data)) {
-               ssize_t bytes_read = read(fd, buffer->data + buffer->used,
-                               sizeof(buffer->data) - buffer->used);
-
-               // Handle errors
-               if (bytes_read < 0) {
-                       ERROR(jail->pakfire, "Could not read from fd %d: %m\n", fd);
-                       return -1;
+       // Skip this if there is not space left in the buffer
+       if (buffer->used >= sizeof(buffer->data))
+               return 0;
+
+       // Fill the buffer
+       r = read(fd, buffer->data + buffer->used, sizeof(buffer->data) - buffer->used);
+
+       // Handle errors
+       if (r < 0) {
+               switch (errno) {
+                       case EAGAIN:
+                       case EIO:
+                               break;
+
+                       default:
+                               return -errno;
                }
 
-               // Update buffer size
-               buffer->used += bytes_read;
+       // EOF
+       } else if (r == 0) {
+               // XXX What to do here?
+
+       // Successful read
+       } else {
+               buffer->used += r;
        }
 
-       // See if we have any lines that we can write
+       return 0;
+}
+
+static int pakfire_jail_drain_buffer_with_callback(struct pakfire_jail* jail,
+               struct pakfire_log_buffer* buffer, int priority, pakfire_jail_communicate_out callback, void* data) {
+       const char* eol = NULL;
+       int r;
+
        while (buffer->used) {
                // Search for the end of the first line
-               char* eol = memchr(buffer->data, '\n', buffer->used);
+               eol = memchr(buffer->data, '\n', buffer->used);
 
                // No newline found
                if (!eol) {
-                       // If the buffer is full, we send the content to the logger and try again
-                       // This should not happen in practise
+                       // If the buffer is full, we send the entire content to make space.
                        if (pakfire_jail_log_buffer_is_full(buffer)) {
-                               DEBUG(jail->pakfire, "Logging buffer is full. Sending all content\n");
+                               CTX_DEBUG(jail->ctx, "Buffer is full. Sending all content\n");
 
-                               eol = buffer->data + sizeof(buffer->data) - 1;
+                               eol = buffer->data + buffer->used - 1;
 
-                       // Otherwise we might have only read parts of the output
-                       } else
+                       // Otherwise we might have only read parts of the output...
+                       } else {
                                break;
+                       }
                }
 
                // Find the length of the string
-               size_t length = eol - buffer->data + 1;
-
-               // Copy the line into the buffer
-               memcpy(line, buffer->data, length);
-
-               // Terminate the string
-               line[length] = '\0';
+               const size_t length = eol - buffer->data + 1;
 
-               // Log the line
-               if (callback) {
-                       int r = callback(jail->pakfire, data, priority, line, length);
-                       if (r) {
-                               ERROR(jail->pakfire, "The logging callback returned an error: %d\n", r);
-                               return r;
-                       }
+               // Call the callback
+               r = callback(jail->pakfire, data, priority, buffer->data, length);
+               if (r) {
+                       CTX_ERROR(jail->ctx, "The logging callback returned an error: %d\n", r);
+                       return r;
                }
 
                // Remove line from buffer
@@ -668,6 +669,61 @@ static int pakfire_jail_handle_log(struct pakfire_jail* jail,
        return 0;
 }
 
+static int pakfire_jail_drain_buffer(struct pakfire_jail* jail, int fd, struct pakfire_log_buffer* buffer) {
+       int r;
+
+       // Nothing to do if the buffer is empty
+       if (!buffer->used)
+               return 0;
+
+       // Drain the buffer
+       r = write(fd, buffer->data, buffer->used);
+
+       // Handle errors
+       if (r < 0) {
+               switch (errno) {
+                       case EAGAIN:
+                       case EIO:
+                               break;
+
+                       default:
+                               return -errno;
+               }
+
+       // Successful write
+       } else {
+               memmove(buffer->data, buffer->data + r, buffer->used - r);
+
+               buffer->used -= r;
+       }
+
+       return 0;
+}
+
+/*
+       This function reads as much data as it can from the file descriptor.
+       If it finds a whole line in it, it will send it to the logger and repeat the process.
+       If not newline character is found, it will try to read more data until it finds one.
+*/
+static int pakfire_jail_handle_log(struct pakfire_jail* jail, struct pakfire_jail_exec* ctx,
+               int priority, int fd, struct pakfire_log_buffer* buffer,
+               pakfire_jail_communicate_out callback, void* data) {
+       int r;
+
+       // Fill up buffer from fd
+       r = pakfire_jail_fill_buffer(jail, fd, buffer);
+       if (r)
+               return r;
+
+       // Drain the buffer
+       r = pakfire_jail_drain_buffer_with_callback(jail, buffer, priority, callback, data);
+       if (r)
+               return r;
+
+       return 0;
+}
+
+#if 0
 static int pakfire_jail_stream_stdin(struct pakfire_jail* jail,
                struct pakfire_jail_exec* ctx, const int fd) {
        int r;
@@ -705,6 +761,7 @@ static int pakfire_jail_stream_stdin(struct pakfire_jail* jail,
 
        return r;
 }
+#endif
 
 static int pakfire_jail_recv_fd(struct pakfire_jail* jail, int socket, int* fd) {
        const size_t payload_length = sizeof(fd);
@@ -869,6 +926,14 @@ static int pakfire_jail_enable_raw_mode(struct pakfire_jail* jail,
        struct termios raw_attrs;
        int r;
 
+       // Store flags
+       stdio->fdflags = fcntl(stdio->fd, F_GETFL);
+       if (stdio->fdflags < 0) {
+               CTX_ERROR(jail->ctx, "Could not fetch flags from fd %d: %s\n",
+                       stdio->fd, strerror(errno));
+               return -errno;
+       }
+
        // Fetch all attributes
        r = tcgetattr(stdio->fd, &stdio->attrs);
        if (r) {
@@ -883,6 +948,17 @@ static int pakfire_jail_enable_raw_mode(struct pakfire_jail* jail,
        // Make it RAW
        cfmakeraw(&raw_attrs);
 
+       switch (stdio->fd) {
+               case STDIN_FILENO:
+                       raw_attrs.c_oflag = stdio->attrs.c_oflag;
+                       break;
+
+               case STDOUT_FILENO:
+                       raw_attrs.c_iflag = stdio->attrs.c_iflag;
+                       raw_attrs.c_lflag = stdio->attrs.c_lflag;
+                       break;
+       }
+
        // Restore the attributes
        r = tcsetattr(stdio->fd, TCSANOW, &raw_attrs);
        if (r) {
@@ -894,16 +970,27 @@ static int pakfire_jail_enable_raw_mode(struct pakfire_jail* jail,
        return 0;
 }
 
-static void pakfire_jail_restore_attrs(struct pakfire_jail* jail,
+static int pakfire_jail_restore_attrs(struct pakfire_jail* jail,
                const struct pakfire_jail_pty_stdio* stdio) {
        int r;
 
+       // Restore the flags
+       r = fcntl(stdio->fd, F_SETFL, stdio->fdflags);
+       if (r < 0) {
+               CTX_ERROR(jail->ctx, "Could not set flags for file descriptor %d: %s\n",
+                       stdio->fd, strerror(errno));
+               return -errno;
+       }
+
        // Restore the attributes
        r = tcsetattr(stdio->fd, TCSANOW, &stdio->attrs);
        if (r) {
-               CTX_DEBUG(jail->ctx, "Could not restore terminal attributes for %d, ignoring: %s\n",
+               CTX_ERROR(jail->ctx, "Could not restore terminal attributes for %d, ignoring: %s\n",
                        stdio->fd, strerror(errno));
+               return -errno;
        }
+
+       return 0;
 }
 
 static int pakfire_jail_setup_pty_forwarding(struct pakfire_jail* jail,
@@ -962,6 +1049,68 @@ static int pakfire_jail_setup_pty_forwarding(struct pakfire_jail* jail,
        return 0;
 }
 
+static int pakfire_jail_forward_pty(struct pakfire_jail* jail, struct pakfire_jail_exec* ctx) {
+       int r;
+
+       // Read from standard input
+       if (ctx->pty.stdin.flags & PAKFIRE_JAIL_PTY_READY_TO_READ) {
+               r = pakfire_jail_fill_buffer(jail, ctx->pty.stdin.fd, &ctx->pty.stdin.buffer);
+               if (r) {
+                       CTX_ERROR(jail->ctx, "Failed reading from standard input: %s\n", strerror(-r));
+                       return r;
+               }
+
+               // We are done reading for now
+               ctx->pty.stdin.flags &= ~PAKFIRE_JAIL_PTY_READY_TO_READ;
+
+               // But we may have data to write
+               if (ctx->pty.stdin.buffer.used)
+                       ctx->pty.master.flags |= PAKFIRE_JAIL_PTY_READY_TO_WRITE;
+       }
+
+       // Write to the master
+       if (ctx->pty.master.flags & PAKFIRE_JAIL_PTY_READY_TO_WRITE) {
+               r = pakfire_jail_drain_buffer(jail, ctx->pty.master.fd, &ctx->pty.stdin.buffer);
+               if (r) {
+                       CTX_ERROR(jail->ctx, "Failed writing to the PTY: %s\n", strerror(-r));
+                       return r;
+               }
+
+               // We are done writing for now
+               ctx->pty.master.flags &= ~PAKFIRE_JAIL_PTY_READY_TO_WRITE;
+       }
+
+       // Read from the master
+       if (ctx->pty.master.flags & PAKFIRE_JAIL_PTY_READY_TO_READ) {
+               r = pakfire_jail_fill_buffer(jail, ctx->pty.master.fd, &ctx->pty.stdout.buffer);
+               if (r) {
+                       CTX_ERROR(jail->ctx, "Failed reading from the PTY: %s\n", strerror(-r));
+                       return r;
+               }
+
+               // We are done reading for now
+               ctx->pty.master.flags &= ~PAKFIRE_JAIL_PTY_READY_TO_READ;
+
+               // But we may have data to write
+               if (ctx->pty.stdout.buffer.used)
+                       ctx->pty.stdout.flags |= PAKFIRE_JAIL_PTY_READY_TO_WRITE;
+       }
+
+       // 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;
+               }
+
+               // We are done writing for now
+               ctx->pty.stdout.flags &= ~PAKFIRE_JAIL_PTY_READY_TO_WRITE;
+       }
+
+       return 0;
+}
+
 static int pakfire_jail_wait(struct pakfire_jail* jail, struct pakfire_jail_exec* ctx) {
        int epollfd = -1;
        struct epoll_event events[EPOLL_MAX_EVENTS];
@@ -969,9 +1118,6 @@ static int pakfire_jail_wait(struct pakfire_jail* jail, struct pakfire_jail_exec
        int r = 0;
 
        // Fetch file descriptors from context
-       const int stdin = pakfire_jail_get_pipe_to_write(jail, &ctx->pipes.stdin);
-       const int stdout = pakfire_jail_get_pipe_to_read(jail, &ctx->pipes.stdout);
-       const int stderr = pakfire_jail_get_pipe_to_read(jail, &ctx->pipes.stderr);
        const int pidfd  = ctx->pidfd;
 
        // Fetch the UNIX domain socket
@@ -992,11 +1138,6 @@ static int pakfire_jail_wait(struct pakfire_jail* jail, struct pakfire_jail_exec
                const int fd;
                const int events;
        } fds[] = {
-               // Standard input/output
-               { stdin,  EPOLLOUT },
-               { stdout, EPOLLIN },
-               { stderr, EPOLLIN },
-
                // Timer
                { timerfd, EPOLLIN },
 
@@ -1057,15 +1198,48 @@ static int pakfire_jail_wait(struct pakfire_jail* jail, struct pakfire_jail_exec
                        int e  = events[i].events;
                        int fd = events[i].data.fd;
 
-                       struct pakfire_log_buffer* buffer = NULL;
-                       pakfire_jail_communicate_out callback = NULL;
-                       void* data = NULL;
-                       int priority;
+                       // Handle PTY forwarding events
+                       if (ctx->pty.master.fd == fd) {
+                               if (e & (EPOLLIN|EPOLLHUP))
+                                       ctx->pty.master.flags |= PAKFIRE_JAIL_PTY_READY_TO_READ;
+
+                               if (e & (EPOLLOUT|EPOLLHUP))
+                                       ctx->pty.master.flags |= PAKFIRE_JAIL_PTY_READY_TO_WRITE;
+
+                               // Perform the work
+                               r = pakfire_jail_forward_pty(jail, ctx);
+                               if (r) {
+                                       CTX_ERROR(jail->ctx, "Failed forwarding the PTY: %s\n", strerror(-r));
+                                       goto ERROR;
+                               }
+
+                       // Handle standard input
+                       } else if (ctx->pty.stdin.fd == fd) {
+                               if (e & (EPOLLIN|EPOLLHUP))
+                                       ctx->pty.stdin.flags |= PAKFIRE_JAIL_PTY_READY_TO_READ;
 
-                       // Check if there is any data to be read
-                       if (e & EPOLLIN) {
-                               // Handle any changes to the PIDFD
-                               if (fd == pidfd) {
+                               // Perform the work
+                               r = pakfire_jail_forward_pty(jail, ctx);
+                               if (r) {
+                                       CTX_ERROR(jail->ctx, "Failed forwarding the PTY: %s\n", strerror(-r));
+                                       goto ERROR;
+                               }
+
+                       // Handle standard output
+                       } else if (ctx->pty.stdout.fd == fd) {
+                               if (e & (EPOLLOUT|EPOLLHUP))
+                                       ctx->pty.stdout.flags |= PAKFIRE_JAIL_PTY_READY_TO_WRITE;
+
+                               // Perform the work
+                               r = pakfire_jail_forward_pty(jail, ctx);
+                               if (r) {
+                                       CTX_ERROR(jail->ctx, "Failed forwarding the PTY: %s\n", strerror(-r));
+                                       goto ERROR;
+                               }
+
+                       // Handle any changes to the PIDFD
+                       } else if (pidfd == fd) {
+                               if (e & EPOLLIN) {
                                        // Call waidid() and store the result
                                        r = waitid(P_PIDFD, ctx->pidfd, &ctx->status, WEXITED);
                                        if (r) {
@@ -1076,10 +1250,11 @@ static int pakfire_jail_wait(struct pakfire_jail* jail, struct pakfire_jail_exec
                                        // Mark that we have ended so that we will process the remaining
                                        // events from epoll() now, but won't restart the outer loop.
                                        ended = 1;
-                                       continue;
+                               }
 
-                               // Handle timer events
-                               } else if (fd == timerfd) {
+                       // Handle timer events
+                       } else if (timerfd == fd) {
+                               if (e & EPOLLIN) {
                                        DEBUG(jail->pakfire, "Timer event received\n");
 
                                        // Disarm the timer
@@ -1101,60 +1276,11 @@ static int pakfire_jail_wait(struct pakfire_jail* jail, struct pakfire_jail_exec
                                                        goto ERROR;
                                                }
                                        }
+                               }
 
-                                       // Don't fall through to log processing
-                                       continue;
-
-                               // Handle logging messages
-                               } else if (fd == log_INFO) {
-                                       buffer = &ctx->buffers.log_INFO;
-                                       priority = LOG_INFO;
-
-                                       callback = pakfire_jail_log;
-
-                               } else if (fd == log_ERROR) {
-                                       buffer = &ctx->buffers.log_ERROR;
-                                       priority = LOG_ERR;
-
-                                       callback = pakfire_jail_log;
-
-#ifdef ENABLE_DEBUG
-                               } else if (fd == log_DEBUG) {
-                                       buffer = &ctx->buffers.log_DEBUG;
-                                       priority = LOG_DEBUG;
-
-                                       callback = pakfire_jail_log;
-#endif /* ENABLE_DEBUG */
-
-                               // Handle anything from the log pipes
-                               } else if (fd == stdout) {
-                                       buffer = &ctx->buffers.stdout;
-                                       priority = LOG_INFO;
-
-                                       // Send any output to the default logger if no callback is set
-                                       if (ctx->communicate.out) {
-                                               callback = ctx->communicate.out;
-                                               data     = ctx->communicate.data;
-                                       } else {
-                                               callback = jail->callbacks.log;
-                                               data     = jail->callbacks.log_data;
-                                       }
-
-                               } else if (fd == stderr) {
-                                       buffer = &ctx->buffers.stderr;
-                                       priority = LOG_ERR;
-
-                                       // Send any output to the default logger if no callback is set
-                                       if (ctx->communicate.out) {
-                                               callback = ctx->communicate.out;
-                                               data     = ctx->communicate.data;
-                                       } else {
-                                               callback = jail->callbacks.log;
-                                               data     = jail->callbacks.log_data;
-                                       }
-
-                               // Handle socket messages
-                               } else if (fd == socket_recv) {
+                       // Handle socket messages
+                       } else if (socket_recv == fd) {
+                               if (e & EPOLLIN) {
                                        // Receive the passed FD
                                        r = pakfire_jail_recv_fd(jail, socket_recv, &fd);
                                        if (r)
@@ -1168,37 +1294,41 @@ static int pakfire_jail_wait(struct pakfire_jail* jail, struct pakfire_jail_exec
                                                        goto ERROR;
                                                }
                                        }
+                               }
 
-                                       // Don't fall through to log processing
-                                       continue;
-
-                               } else {
-                                       DEBUG(jail->pakfire, "Received invalid file descriptor %d\n", fd);
-                                       continue;
+                       // Handle log INFO messages
+                       } else if (log_INFO == fd) {
+                               if (e & EPOLLIN) {
+                                       r = pakfire_jail_handle_log(jail, ctx, LOG_INFO, fd,
+                                               &ctx->buffers.log_INFO, pakfire_jail_log, NULL);
+                                       if (r)
+                                               goto ERROR;
                                }
 
-                               // Handle log event
-                               r = pakfire_jail_handle_log(jail, ctx, priority, fd, buffer, callback, data);
-                               if (r)
-                                       goto ERROR;
-                       }
+                       // Handle log ERROR messages
+                       } else if (log_ERROR == fd) {
+                               if (e & EPOLLIN) {
+                                       r = pakfire_jail_handle_log(jail, ctx, LOG_ERR, fd,
+                                               &ctx->buffers.log_ERROR, pakfire_jail_log, NULL);
+                                       if (r)
+                                               goto ERROR;
+                               }
 
-                       if (e & EPOLLOUT) {
-                               // Handle standard input
-                               if (fd == stdin) {
-                                       r = pakfire_jail_stream_stdin(jail, ctx, fd);
-                                       if (r) {
-                                               switch (errno) {
-                                                       // Ignore if we filled up the buffer
-                                                       case EAGAIN:
-                                                               break;
-
-                                                       default:
-                                                               ERROR(jail->pakfire, "Could not write to stdin: %m\n");
-                                                               goto ERROR;
-                                               }
-                                       }
+#ifdef ENABLE_DEBUG
+                       // Handle log DEBUG messages
+                       } else if (log_DEBUG == fd) {
+                               if (e & EPOLLIN) {
+                                       r = pakfire_jail_handle_log(jail, ctx, LOG_DEBUG, fd,
+                                               &ctx->buffers.log_DEBUG, pakfire_jail_log, NULL);
+                                       if (r)
+                                               goto ERROR;
                                }
+#endif /* ENABLE_DEBUG */
+
+                       // Log a message for anything else
+                       } else {
+                               DEBUG(jail->pakfire, "Received invalid file descriptor %d\n", fd);
+                               continue;
                        }
 
                        // Check if any file descriptors have been closed
@@ -1220,8 +1350,10 @@ ERROR:
                close(timerfd);
 
        // Restore any changed terminal attributes
-       pakfire_jail_restore_attrs(jail, &ctx->pty.stdin);
-       pakfire_jail_restore_attrs(jail, &ctx->pty.stdout);
+       if (ctx->pty.stdin.fd >= 0)
+               pakfire_jail_restore_attrs(jail, &ctx->pty.stdin);
+       if (ctx->pty.stdout.fd >= 0)
+               pakfire_jail_restore_attrs(jail, &ctx->pty.stdout);
 
        return r;
 }
@@ -1843,16 +1975,58 @@ static int pakfire_jail_open_pty(struct pakfire_jail* jail, struct pakfire_jail_
 
        CTX_DEBUG(jail->ctx, "Allocated console at %s (%d)\n", ctx->pty.console, ctx->pty.master.fd);
 
-#if 0
+       // Unlock the master device
+       r = unlockpt(ctx->pty.master.fd);
+       if (r) {
+               CTX_ERROR(jail->ctx, "Could not unlock the PTY: %s\n", strerror(errno));
+               return -errno;
+       }
+
        // Create a symlink
-       r = pakfire_symlink(jail->ctx, "/dev/console", ctx->pty.console);
+       r = pakfire_symlink(jail->ctx, ctx->pty.console, "/dev/console");
        if (r)
                return r;
-#endif
 
        return r;
 }
 
+static int pakfire_jail_setup_terminal(struct pakfire_jail* jail, struct pakfire_jail_exec* ctx) {
+       int fd;
+       int r;
+
+       // Open a new terminal
+       fd = open("/dev/console", O_RDWR|O_NOCTTY);
+       if (fd < 0) {
+               CTX_ERROR(jail->ctx, "Failed to open a new terminal: %s\n", strerror(errno));
+               return -errno;
+       }
+
+       CTX_DEBUG(jail->ctx, "Opened a new terminal %d\n", fd);
+
+       // Connect the new terminal to standard input
+       r = dup2(fd, STDIN_FILENO);
+       if (r < 0) {
+               CTX_ERROR(jail->ctx, "Failed to open standard input: %s\n", strerror(errno));
+               return -errno;
+       }
+
+       // Connect the new terminal to standard output
+       r = dup2(fd, STDOUT_FILENO);
+       if (r < 0) {
+               CTX_ERROR(jail->ctx, "Failed to open standard output: %s\n", strerror(errno));
+               return -errno;
+       }
+
+       // Connect the new terminal to standard error
+       r = dup2(fd, STDERR_FILENO);
+       if (r < 0) {
+               CTX_ERROR(jail->ctx, "Failed to open standard error: %s\n", strerror(errno));
+               return -errno;
+       }
+
+       return 0;
+}
+
 static int pakfire_jail_child(struct pakfire_jail* jail, struct pakfire_jail_exec* ctx,
                const char* argv[]) {
        int r;
@@ -1983,14 +2157,12 @@ static int pakfire_jail_child(struct pakfire_jail* jail, struct pakfire_jail_exe
                }
        }
 
-#if 0
        // Create a new session
        r = setsid();
        if (r < 0) {
                CTX_ERROR(jail->ctx, "Could not create a new session: %s\n", strerror(errno));
                return r;
        }
-#endif
 
        // Allocate a new PTY
        r = pakfire_jail_open_pty(jail, ctx);
@@ -2006,6 +2178,11 @@ static int pakfire_jail_child(struct pakfire_jail* jail, struct pakfire_jail_exe
                return r;
        }
 
+       // Setup the terminal
+       r = pakfire_jail_setup_terminal(jail, ctx);
+       if (r)
+               return r;
+
        // Close the master of the PTY
        close(ctx->pty.master.fd);
        ctx->pty.master.fd = -1;
@@ -2020,41 +2197,6 @@ static int pakfire_jail_child(struct pakfire_jail* jail, struct pakfire_jail_exe
        close(ctx->pipes.log_DEBUG[0]);
 #endif /* ENABLE_DEBUG */
 
-       // Connect standard input
-       if (ctx->pipes.stdin[0] >= 0) {
-               r = dup2(ctx->pipes.stdin[0], STDIN_FILENO);
-               if (r < 0) {
-                       ERROR(jail->pakfire, "Could not connect fd %d to stdin: %m\n",
-                               ctx->pipes.stdin[0]);
-
-                       return 1;
-               }
-       }
-
-       // Connect standard output and error
-       if (ctx->pipes.stdout[1] >= 0 && ctx->pipes.stderr[1] >= 0) {
-               r = dup2(ctx->pipes.stdout[1], STDOUT_FILENO);
-               if (r < 0) {
-                       ERROR(jail->pakfire, "Could not connect fd %d to stdout: %m\n",
-                               ctx->pipes.stdout[1]);
-
-                       return 1;
-               }
-
-               r = dup2(ctx->pipes.stderr[1], STDERR_FILENO);
-               if (r < 0) {
-                       ERROR(jail->pakfire, "Could not connect fd %d to stderr: %m\n",
-                               ctx->pipes.stderr[1]);
-
-                       return 1;
-               }
-
-               // Close the pipe (as we have moved the original file descriptors)
-               pakfire_jail_close_pipe(jail, ctx->pipes.stdin);
-               pakfire_jail_close_pipe(jail, ctx->pipes.stdout);
-               pakfire_jail_close_pipe(jail, ctx->pipes.stderr);
-       }
-
        // Reset open file limit (http://0pointer.net/blog/file-descriptor-limits.html)
        r = pakfire_rlimit_reset_nofile(jail->pakfire);
        if (r)
@@ -2109,7 +2251,6 @@ static int pakfire_jail_child(struct pakfire_jail* jail, struct pakfire_jail_exe
 
 // Run a command in the jail
 static int __pakfire_jail_exec(struct pakfire_jail* jail, const char* argv[],
-               const int interactive,
                pakfire_jail_communicate_in  communicate_in,
                pakfire_jail_communicate_out communicate_out,
                void* data, int flags) {
@@ -2129,9 +2270,6 @@ static int __pakfire_jail_exec(struct pakfire_jail* jail, const char* argv[],
                .socket = { -1, -1 },
 
                .pipes = {
-                       .stdin     = { -1, -1 },
-                       .stdout    = { -1, -1 },
-                       .stderr    = { -1, -1 },
                        .log_INFO  = { -1, -1 },
                        .log_ERROR = { -1, -1 },
 #ifdef ENABLE_DEBUG
@@ -2164,7 +2302,7 @@ static int __pakfire_jail_exec(struct pakfire_jail* jail, const char* argv[],
        DEBUG(jail->pakfire, "Executing jail...\n");
 
        // Enable networking in interactive mode
-       if (interactive)
+       if (ctx.flags & PAKFIRE_JAIL_PTY_FORWARDING)
                ctx.flags |= PAKFIRE_JAIL_HAS_NETWORKING;
 
        /*
@@ -2185,26 +2323,6 @@ static int __pakfire_jail_exec(struct pakfire_jail* jail, const char* argv[],
                goto ERROR;
        }
 
-       // Create pipes to communicate with child process if we are not running interactively
-       if (!interactive) {
-               // stdin (only if callback is set)
-               if (ctx.communicate.in) {
-                       r = pakfire_jail_setup_pipe(jail, &ctx.pipes.stdin, 0);
-                       if (r)
-                               goto ERROR;
-               }
-
-               // stdout
-               r = pakfire_jail_setup_pipe(jail, &ctx.pipes.stdout, 0);
-               if (r)
-                       goto ERROR;
-
-               // stderr
-               r = pakfire_jail_setup_pipe(jail, &ctx.pipes.stderr, 0);
-               if (r)
-                       goto ERROR;
-       }
-
        // Setup pipes for logging
        // INFO
        r = pakfire_jail_setup_pipe(jail, &ctx.pipes.log_INFO, O_CLOEXEC);
@@ -2321,9 +2439,6 @@ ERROR:
        }
 
        // Close any file descriptors
-       pakfire_jail_close_pipe(jail, ctx.pipes.stdin);
-       pakfire_jail_close_pipe(jail, ctx.pipes.stdout);
-       pakfire_jail_close_pipe(jail, ctx.pipes.stderr);
        if (ctx.pidfd >= 0)
                close(ctx.pidfd);
        if (ctx.pty.master.fd >= 0)
@@ -2344,19 +2459,21 @@ PAKFIRE_EXPORT int pakfire_jail_exec(
                pakfire_jail_communicate_in  callback_in,
                pakfire_jail_communicate_out callback_out,
                void* data, int flags) {
-       return __pakfire_jail_exec(jail, argv, 0, callback_in, callback_out, data, flags);
+       return __pakfire_jail_exec(jail, argv, callback_in, callback_out, data, flags);
 }
 
 static int pakfire_jail_exec_interactive(
                struct pakfire_jail* jail, const char* argv[], int flags) {
        int r;
 
+       flags |= PAKFIRE_JAIL_PTY_FORWARDING;
+
        // Setup interactive stuff
        r = pakfire_jail_setup_interactive_env(jail);
        if (r)
                return r;
 
-       return __pakfire_jail_exec(jail, argv, 1, NULL, NULL, NULL, flags);
+       return __pakfire_jail_exec(jail, argv, NULL, NULL, NULL, flags);
 }
 
 int pakfire_jail_exec_script(struct pakfire_jail* jail,