// Prepare standard input for writing
fd = pakfire_jail_get_pipe_to_write(jail, &ctx->pipes.stdin);
if (fd >= 0) {
+ r = pakfire_fd_set_non_blocking(jail->ctx, fd);
+ if (r < 0)
+ return r;
+
+ // Add the file descriptor to the event loop
r = sd_event_add_io(ctx->loop, NULL, fd, EPOLLOUT|EPOLLHUP|EPOLLET, pakfire_jail_stdin, ctx);
if (r < 0) {
ERROR(jail->ctx, "Failed to register standard input for writing: %s\n", strerror(-r));
// Prepare standard output for reading
fd = pakfire_jail_get_pipe_to_read(jail, &ctx->pipes.stdout);
if (fd >= 0) {
+ r = pakfire_fd_set_non_blocking(jail->ctx, fd);
+ if (r < 0)
+ return r;
+
+ // Add the file descriptor to the event loop
r = sd_event_add_io(ctx->loop, NULL, fd, EPOLLIN|EPOLLHUP|EPOLLET, pakfire_jail_stdout, ctx);
if (r < 0) {
ERROR(jail->ctx, "Failed to register standard output for reading: %s\n", strerror(-r));
// Prepare standard error for reading
fd = pakfire_jail_get_pipe_to_read(jail, &ctx->pipes.stderr);
if (fd >= 0) {
+ r = pakfire_fd_set_non_blocking(jail->ctx, fd);
+ if (r < 0)
+ return r;
+
+ // Add the file descriptor to the event loop
r = sd_event_add_io(ctx->loop, NULL, fd, EPOLLIN|EPOLLHUP|EPOLLET, pakfire_jail_stderr, ctx);
if (r < 0) {
ERROR(jail->ctx, "Failed to register standard error for reading: %s\n", strerror(-r));
// Otherwise we are running a non-interactive session
} else {
// Create a pipe for the standard input only when a callback is set
- if (stdin_callback) {
- r = pakfire_jail_setup_pipe(jail, &ctx.pipes.stdin, O_NONBLOCK);
+ if (input_callback) {
+ r = pakfire_jail_setup_pipe(jail, &ctx.pipes.stdin, 0);
if (r < 0)
goto ERROR;
}
// Create a pipe for standard output
- r = pakfire_jail_setup_pipe(jail, &ctx.pipes.stdout, O_NONBLOCK);
+ r = pakfire_jail_setup_pipe(jail, &ctx.pipes.stdout, 0);
if (r < 0)
goto ERROR;
// Create a pipe for standard error
- r = pakfire_jail_setup_pipe(jail, &ctx.pipes.stderr, O_NONBLOCK);
+ r = pakfire_jail_setup_pipe(jail, &ctx.pipes.stderr, 0);
if (r < 0)
goto ERROR;
return 0;
}
+
+int pakfire_fd_set_non_blocking(struct pakfire_ctx* ctx, int fd) {
+ int flags;
+ int r;
+
+ // Fetch the flags
+ flags = fcntl(fd, F_GETFL, 0);
+ if (flags < 0) {
+ ERROR(ctx, "Could not set flags for file descriptor %d: %m\n", fd);
+ return -errno;
+ }
+
+ // Turn on non-blocking mode
+ r = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
+ if (r < 0) {
+ ERROR(ctx, "Could not set flags for file descriptor %d: %m\n", fd);
+ return -errno;
+ }
+
+ return 0;
+}