[sshd] get rid of pipe hack
Change-Id: I78e404df984aae039c91051d60a9a98e9bb480b3
diff --git a/fuchsia/fuchsia-compat.c b/fuchsia/fuchsia-compat.c
index 3e7f22e..3aed8d9 100644
--- a/fuchsia/fuchsia-compat.c
+++ b/fuchsia/fuchsia-compat.c
@@ -139,191 +139,8 @@
return NULL;
}
-// Write to a non-blocking fd, blocking until writing has completed or an error has occurred.
-static bool blocking_write(int fd, const char* buffer, size_t length) {
- uint32_t events;
- size_t offset = 0;
- while (offset < length) {
- ssize_t length_written = write(fd, buffer + offset, length - offset);
- if (length_written == -1 && errno == EAGAIN) {
- if (fdio_wait_fd(fd, FDIO_EVT_WRITABLE|FDIO_EVT_PEER_CLOSED, &events, ZX_TIME_INFINITE) < 0) {
- // Wait failed.
- return false;
- }
- if ((events & FDIO_EVT_PEER_CLOSED) != 0){
- return false;
- }
- continue;
- }
- if (length_written == 0) {
- // EOF or error.
- return false;
- }
- offset += length_written;
- }
- return true;
-}
-
-// A thread that processes output.
-// Currently just nothing but shuffle bytes.
-static void* process_input_thread_func(void* voidp) {
- pthread_detach(pthread_self());
-
- int* fds = voidp;
- char buf[FDIO_CHUNK_SIZE];
-
- for (;;) {
- uint32_t events;
- int length = read(fds[0], buf, sizeof(buf));
- if (length == -1 && errno == EAGAIN) {
- if (fdio_wait_fd(fds[0], FDIO_EVT_READABLE|FDIO_EVT_PEER_CLOSED, &events, ZX_TIME_INFINITE) < 0) {
- // Wait failed.
- break;
- }
- if ((events & FDIO_EVT_PEER_CLOSED) != 0) {
- break;
- }
- continue;
- }
- if (length <= 0) {
- // EOF or error.
- break;
- }
- if (!blocking_write(fds[1], buf, length)) {
- break;
- }
- }
-
- close(fds[0]);
- close(fds[1]);
-
- free(fds);
-
- return NULL;
-}
-
-// Start an input processing thread that reads from fd.
-// The returned fd will receive the processed input.
-static int run_input_processing_thread(int fd) {
- int pip[2];
- if (pipe(pip) < 0) {
- fprintf(stderr, "Error creating pipe: %s\n", strerror(errno));
- return -1;
- }
-
- // Allocate the structure to send the fds to the thread.
- int* fds = malloc(2 * sizeof(int));
- if (!fds) {
- fprintf(stderr, "Malloc failed.\n");
- return -1;
- }
- fds[0] = fd;
- fds[1] = pip[1];
-
- // Start the thread.
- pthread_t processing_thread;
- if (pthread_create(&processing_thread, NULL, process_input_thread_func, fds) != 0) {
- fprintf(stderr, "Failed to create input processing thread for %d: %s\n", fd, strerror(errno));
- return -1;
- }
-
- return pip[0];
-}
-
-// A thread that processes output.
-// Currently just does \n -> \r\n translation.
-static void* process_output_thread_func(void* voidp) {
- pthread_detach(pthread_self());
-
- int* fds = voidp;
- char buf[FDIO_CHUNK_SIZE];
-
- for (;;) {
- int length = read(fds[0], buf, sizeof(buf));
- if (length < 0) {
- uint32_t events;
- if (fdio_wait_fd(fds[0], FDIO_EVT_READABLE|FDIO_EVT_PEER_CLOSED, &events, ZX_TIME_INFINITE) < 0) {
- // Wait failed.
- break;
- }
- if ((events & FDIO_EVT_PEER_CLOSED) != 0) {
- break;
- }
- continue;
- }
- if (length == 0) {
- // EOF or error.
- break;
- }
- char* p = buf;
- while (length > 0) {
- char* lf = memchr(p, '\n', length);
- if (lf == NULL) {
- // No \n found.
- if (!blocking_write(fds[1], p, length)) {
- goto out_end;
- }
- break;
- } else {
- // \n found at lf.
- if (lf != p) {
- // There are some bytes to print there first.
- if (!blocking_write(fds[1], p, lf - p)) {
- goto out_end;
- }
- }
- // Send \r\n.
- const char* crlf = "\r\n";
- if (!blocking_write(fds[1], crlf, 2)) {
- goto out_end;
- }
- // Skip over the \n and what came before it.
- length -= (lf - p) + 1;
- p = lf + 1;
- }
- }
- }
-out_end:
-
- close(fds[0]);
- close(fds[1]);
-
- free(fds);
-
- return NULL;
-}
-
-// Start an output processing thread that reads from fd.
-// The returned fd will receive the processed output.
-static int run_output_processing_thread(int fd) {
- // Create a pipe to return processed bytes.
- int pip[2];
- if (pipe(pip) < 0) {
- fprintf(stderr, "Error creating pipe: %s\n", strerror(errno));
- return -1;
- }
-
- // Allocate the structure to send the fds to the thread.
- int* fds = malloc(2 * sizeof(int));
- if (!fds) {
- fprintf(stderr, "Malloc failed.\n");
- return -1;
- }
- fds[0] = pip[0];
- fds[1] = fd;
-
- // Start the thread.
- pthread_t processing_thread;
- if (pthread_create(&processing_thread, NULL, process_output_thread_func, fds) != 0) {
- fprintf(stderr, "Failed to create output processing thread for %d: %s\n", fd, strerror(errno));
- return -1;
- }
-
- return pip[1];
-}
-
// Note: **env is consumed by this function and will be freed.
-pid_t fuchsia_launch_child(const char* command, char** env, int in, int out, int err, bool transform) {
+pid_t fuchsia_launch_child(const char* command, char** env, int in, int out, int err) {
const char* argv[ARGV_MAX];
int argc = 1;
argv[0] = "/boot/bin/sh";
@@ -335,17 +152,6 @@
}
argv[argc] = NULL;
- if (transform) {
- in = run_input_processing_thread(in);
- bool same = (out == err);
- out = run_output_processing_thread(out);
- if (same) {
- err = out;
- } else {
- err = run_output_processing_thread(err);
- }
- }
-
// TODO(CF-578): replace most of this with a "shell runner" instead
zx_status_t status;
loader_service_t *ldsvc;
diff --git a/fuchsia/fuchsia-compat.h b/fuchsia/fuchsia-compat.h
index ba8b273..399a90b 100644
--- a/fuchsia/fuchsia-compat.h
+++ b/fuchsia/fuchsia-compat.h
@@ -12,8 +12,7 @@
int chroot(const char* path);
// Note: char** env is consumed by this function and will be freed.
-int fuchsia_launch_child(const char* command, char** env, int in, int out,
- int err, bool transform);
+int fuchsia_launch_child(const char* command, char** env, int in, int out, int err);
// This implements the subset of select() functionality used by openssh.
// Uses void* instead of fd_set* for the read/write fd sets since this compat
diff --git a/session.c b/session.c
index 8a2c20f..86b8515 100644
--- a/session.c
+++ b/session.c
@@ -454,7 +454,7 @@
#ifdef __Fuchsia__
char** env = do_setup_env(ssh, s, "");
- pid = fuchsia_launch_child(command, env, pin[0], pout[1], perr[1], false);
+ pid = fuchsia_launch_child(command, env, pin[0], pout[1], perr[1]);
if (pid <= 0) {
#ifdef USE_PIPES
close(pin[0]);
@@ -629,7 +629,7 @@
#ifdef __Fuchsia__
char** env = do_setup_env(ssh, s, "");
- pid = fuchsia_launch_child(command, env, ttyfd, ttyfd, ttyfd, true);
+ pid = fuchsia_launch_child(command, env, ttyfd, ttyfd, ttyfd);
#else
/* Fork the child. */