if (!buffer->used)
return 0;
+ // Do not try to write to an invalid file descriptor
+ if (fd < 0)
+ return 0;
+
// Drain the buffer
r = write(fd, buffer->data, buffer->used);
struct termios raw_attrs;
int r;
+ // Skip if we don't know the file descriptor
+ if (stdio->fd < 0)
+ return 0;
+
// Skip everything if fd is not a TTY
if (!isatty(stdio->fd))
return 0;
const struct pakfire_jail_pty_stdio* stdio) {
int r;
+ // Skip if we don't know the file descriptor
+ if (stdio->fd < 0)
+ return 0;
+
// Skip everything if fd is not a TTY
if (!isatty(stdio->fd))
return 0;
// Store the file descriptor
ctx->pty.master.fd = fd;
- // Configure stdin/stdout
- ctx->pty.stdin.fd = STDIN_FILENO;
- ctx->pty.stdout.fd = STDOUT_FILENO;
+ // Add the master to the event loop
+ r = pakfire_jail_epoll_add_fd(jail, epollfd, ctx->pty.master.fd, EPOLLIN|EPOLLOUT|EPOLLET);
+ if (r)
+ return r;
- // Fetch dimensions
- if (isatty(ctx->pty.stdout.fd)) {
- r = ioctl(ctx->pty.stdout.fd, TIOCGWINSZ, &size);
- if (r) {
- CTX_ERROR(jail->ctx, "Failed to determine terminal dimensions: %s\n", strerror(errno));
- return -errno;
- }
+ if (ctx->flags & PAKFIRE_JAIL_PTY_FORWARDING) {
+ // Configure stdin/stdout
+ ctx->pty.stdin.fd = STDIN_FILENO;
+ ctx->pty.stdout.fd = STDOUT_FILENO;
- // Set dimensions
- r = ioctl(ctx->pty.master.fd, TIOCSWINSZ, &size);
- if (r) {
- CTX_ERROR(jail->ctx, "Failed setting dimensions: %s\n", strerror(errno));
- return -errno;
- }
- }
+ // Fetch dimensions
+ if (isatty(ctx->pty.stdout.fd)) {
+ r = ioctl(ctx->pty.stdout.fd, TIOCGWINSZ, &size);
+ if (r) {
+ CTX_ERROR(jail->ctx, "Failed to determine terminal dimensions: %s\n", strerror(errno));
+ return -errno;
+ }
- // Enable RAW mode on standard input
- r = pakfire_jail_enable_raw_mode(jail, &ctx->pty.stdin);
- if (r)
- return r;
+ // Set dimensions
+ r = ioctl(ctx->pty.master.fd, TIOCSWINSZ, &size);
+ if (r) {
+ CTX_ERROR(jail->ctx, "Failed setting dimensions: %s\n", strerror(errno));
+ return -errno;
+ }
+ }
- // Enable RAW mode on standard output
- r = pakfire_jail_enable_raw_mode(jail, &ctx->pty.stdout);
- if (r)
- return r;
+ // Enable RAW mode on standard input
+ r = pakfire_jail_enable_raw_mode(jail, &ctx->pty.stdin);
+ if (r)
+ return r;
- // Add the master to the event loop
- r = pakfire_jail_epoll_add_fd(jail, epollfd, ctx->pty.master.fd, EPOLLIN|EPOLLOUT|EPOLLET);
- if (r)
- return r;
+ // Enable RAW mode on standard output
+ r = pakfire_jail_enable_raw_mode(jail, &ctx->pty.stdout);
+ if (r)
+ return r;
- // Add standard input to the event loop
- r = pakfire_jail_epoll_add_fd(jail, epollfd, ctx->pty.stdin.fd, EPOLLIN|EPOLLET);
- if (r)
- return r;
+ // Add standard input to the event loop
+ r = pakfire_jail_epoll_add_fd(jail, epollfd, ctx->pty.stdin.fd, EPOLLIN|EPOLLET);
+ if (r)
+ return r;
- // Add standard output to the event loop
- r = pakfire_jail_epoll_add_fd(jail, epollfd, ctx->pty.stdout.fd, EPOLLOUT|EPOLLET);
- if (r)
- return r;
+ // Add standard output to the event loop
+ r = pakfire_jail_epoll_add_fd(jail, epollfd, ctx->pty.stdout.fd, EPOLLOUT|EPOLLET);
+ if (r)
+ return r;
+ }
return 0;
}