return NULL;
}
-static int pakfire_pty_connect_null(struct pakfire_pty* pty) {
+static int pakfire_pty_connect_null(struct pakfire_pty* pty, int fileno) {
int fd = -EBADF;
+ int flags = 0;
int r;
+ switch (fileno) {
+ case STDIN_FILENO:
+ flags |= O_RDONLY;
+ break;
+
+ case STDOUT_FILENO:
+ case STDERR_FILENO:
+ flags |= O_WRONLY;
+ break;
+
+ default:
+ return -EINVAL;
+ }
+
+
// Open /dev/null
- fd = open("/dev/null", O_RDONLY);
+ fd = open("/dev/null", flags);
if (fd < 0) {
ERROR(pty->ctx, "Failed to open /dev/null: %m\n");
r = -errno;
}
// Copy to the desired file descriptor
- r = dup2(fd, STDIN_FILENO);
+ r = dup2(fd, fileno);
if (r < 0) {
ERROR(pty->ctx, "Failed to duplicate the file descriptor: %m\n");
r = -errno;
// Otherwise we connect standard input to /dev/null
} else {
- r = pakfire_pty_connect_null(pty);
+ r = pakfire_pty_connect_null(pty, STDIN_FILENO);
if (r < 0)
goto ERROR;
}
// Connect the new terminal to standard output
- r = dup2(fd, STDOUT_FILENO);
- if (r < 0) {
- ERROR(pty->ctx, "Failed to open standard output: %s\n", strerror(errno));
- r = -errno;
- goto ERROR;
+ if (pakfire_pty_has_flag(pty, PAKFIRE_PTY_CONNECT_STDOUT)) {
+ r = dup2(fd, STDOUT_FILENO);
+ if (r < 0) {
+ ERROR(pty->ctx, "Failed to open standard output: %s\n", strerror(errno));
+ r = -errno;
+ goto ERROR;
+ }
+
+ // Otherwise we connect standard output to /dev/null
+ } else {
+ r = pakfire_pty_connect_null(pty, STDOUT_FILENO);
+ if (r < 0)
+ goto ERROR;
}
// Connect the new terminal to standard error
- r = dup2(fd, STDERR_FILENO);
- if (r < 0) {
- ERROR(pty->ctx, "Failed to open standard error: %s\n", strerror(errno));
- r = -errno;
- goto ERROR;
+ if (pakfire_pty_has_flag(pty, PAKFIRE_PTY_CONNECT_STDERR)) {
+ r = dup2(fd, STDERR_FILENO);
+ if (r < 0) {
+ ERROR(pty->ctx, "Failed to open standard error: %s\n", strerror(errno));
+ r = -errno;
+ goto ERROR;
+ }
+
+ // Otherwise we connect standard error to /dev/null
+ } else {
+ r = pakfire_pty_connect_null(pty, STDERR_FILENO);
+ if (r < 0)
+ goto ERROR;
}
ERROR:
enum pakfire_pty_flags {
PAKFIRE_PTY_CONNECT_STDIN = (1 << 0),
PAKFIRE_PTY_CONNECT_STDOUT = (1 << 1),
- PAKFIRE_PTY_INTERACTIVE = PAKFIRE_PTY_CONNECT_STDIN|PAKFIRE_PTY_CONNECT_STDOUT,
- PAKFIRE_PTY_CAPTURE_OUTPUT = (1 << 2),
+ PAKFIRE_PTY_CONNECT_STDERR = (1 << 2),
+ PAKFIRE_PTY_INTERACTIVE = \
+ PAKFIRE_PTY_CONNECT_STDIN | \
+ PAKFIRE_PTY_CONNECT_STDOUT| \
+ PAKFIRE_PTY_CONNECT_STDERR,
+ PAKFIRE_PTY_CAPTURE_OUTPUT = (1 << 3),
};
int pakfire_pty_create(struct pakfire_pty** pty,