Skip to content

Commit

Permalink
Initial stdin support
Browse files Browse the repository at this point in the history
Not correct yet.
  • Loading branch information
felixge authored and piscisaureus committed Oct 1, 2011
1 parent 8ddd5e5 commit 847d87f
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
3 changes: 3 additions & 0 deletions include/uv.h
Expand Up @@ -885,6 +885,9 @@ typedef struct uv_spawn_sync_t{

int combine;

char *stdin_buf;
int stdin_size;

char *stdout_buf;
int stdout_read;
int stdout_size;
Expand Down
28 changes: 28 additions & 0 deletions src/unix/process.c
Expand Up @@ -313,6 +313,7 @@ void SyncCHLDHandler(int sig) {
}

int uv_spawn_sync(uv_loop_t* loop, uv_spawn_sync_t* spawn) {
int stdin_pipe[2];
int stdout_pipe[2];
int stderr_pipe[2];
int nfds;
Expand All @@ -330,6 +331,11 @@ int uv_spawn_sync(uv_loop_t* loop, uv_spawn_sync_t* spawn) {
spawn->stdout_read = 0;
spawn->stderr_read = 0;

if (spawn->stdin_buf && pipe(stdin_pipe)) {
uv_err_new(loop, errno);
return -1;
}

if (spawn->stdout_buf && pipe(stdout_pipe)) {
uv_err_new(loop, errno);
return -1;
Expand All @@ -351,6 +357,11 @@ int uv_spawn_sync(uv_loop_t* loop, uv_spawn_sync_t* spawn) {
return -1;

case 0: /* child */
if (spawn->stdin_buf) {
close(stdin_pipe[1]); /* close write end */
dup2(stdin_pipe[0], STDIN_FILENO);
}

if (spawn->stdout_buf) {
close(stdout_pipe[0]); /* close read end */
dup2(stdout_pipe[1], STDOUT_FILENO);
Expand Down Expand Up @@ -385,6 +396,11 @@ int uv_spawn_sync(uv_loop_t* loop, uv_spawn_sync_t* spawn) {
nfds = MAX(nfds, stderr_pipe[0]);
}

if (spawn->stdin_buf) {
int c = write(stdin_pipe[1], spawn->stdin_buf, spawn->stdin_size);
close(stdin_pipe[0]); /* close the read end */
}

nfds = nfds + 1;
start_time = uv_now(loop); /* time in ms */

Expand Down Expand Up @@ -437,6 +453,10 @@ int uv_spawn_sync(uv_loop_t* loop, uv_spawn_sync_t* spawn) {
close(sigchld_pipe[0]);
close(sigchld_pipe[1]);

if (spawn->stdin_buf) {
close(stdin_pipe[1]);
}

if (spawn->stdout_buf) {
close(stdout_pipe[0]);
}
Expand Down Expand Up @@ -497,6 +517,10 @@ int uv_spawn_sync(uv_loop_t* loop, uv_spawn_sync_t* spawn) {
close(sigchld_pipe[0]);
close(sigchld_pipe[1]);

if (spawn->stdin_buf) {
close(stdin_pipe[1]);
}

if (spawn->stdout_buf) {
close(stdout_pipe[0]);
}
Expand All @@ -523,6 +547,10 @@ int uv_spawn_sync(uv_loop_t* loop, uv_spawn_sync_t* spawn) {
close(sigchld_pipe[0]);
close(sigchld_pipe[1]);

if (spawn->stdin_buf) {
close(stdin_pipe[1]);
}

if (spawn->stdout_buf) {
close(stdout_pipe[0]);
}
Expand Down
2 changes: 2 additions & 0 deletions test/test-list.h
Expand Up @@ -87,6 +87,7 @@ TEST_DECLARE (spawn_sync_stderr)
TEST_DECLARE (spawn_sync_stdout_overflow)
TEST_DECLARE (spawn_sync_stderr_overflow)
TEST_DECLARE (spawn_sync_combine_stdio)
TEST_DECLARE (spawn_sync_stdin)
TEST_DECLARE (spawn_sync_timeout)
TEST_DECLARE (fs_file_noent)
TEST_DECLARE (fs_file_async)
Expand Down Expand Up @@ -218,6 +219,7 @@ TASK_LIST_START
TEST_ENTRY (spawn_sync_stdout_overflow)
TEST_ENTRY (spawn_sync_stderr_overflow)
TEST_ENTRY (spawn_sync_combine_stdio)
TEST_ENTRY (spawn_sync_stdin)
TEST_ENTRY (spawn_sync_timeout)
#ifdef _WIN32
TEST_ENTRY (spawn_detect_pipe_name_collisions_on_windows)
Expand Down
21 changes: 20 additions & 1 deletion test/test-spawn-sync.c
Expand Up @@ -47,7 +47,7 @@ static void init_process_options(char* test) {
spawn.stdout_size = 1024;
spawn.stdout_buf = malloc(spawn.stdout_size);
spawn.stderr_size = 1024;
spawn.stderr_buf = malloc(spawn.stdout_size);
spawn.stderr_buf = malloc(spawn.stderr_size);
}

void debug(int r) {
Expand Down Expand Up @@ -218,6 +218,25 @@ TEST_IMPL(spawn_sync_combine_stdio) {
return 0;
}

TEST_IMPL(spawn_sync_stdin) {
int r;
uv_init();

init_process_options("spawn_helper_stdin");

spawn.stdin_buf = "stdin\n";
spawn.stdin_size = strlen(spawn.stdin_buf);

r = uv_spawn_sync(uv_default_loop(), &spawn);
debug(r);

ASSERT(r == 0);
ASSERT(strcmp(spawn.stdout_buf, spawn.stdin_buf) == 0);
ASSERT(spawn.stdout_read == strlen(spawn.stdin_buf));

return 0;
}

TEST_IMPL(spawn_sync_timeout) {
int r;
uv_init();
Expand Down

0 comments on commit 847d87f

Please sign in to comment.