Skip to content

Commit

Permalink
process: don't close stdio fds during spawn
Browse files Browse the repository at this point in the history
This is needed when closed stdio fd is reused for uv_spawn pipe.
Fixes joyent#1211
  • Loading branch information
tonistiigi committed Mar 26, 2014
1 parent cd6db8b commit fb21509
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/unix/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ static void uv__process_child_init(uv_process_options_t options,
if (fd <= 2)
uv__nonblock(fd, 0);

if (close_fd != -1)
if (close_fd >= stdio_count)
close(close_fd);
}

Expand Down
2 changes: 2 additions & 0 deletions test/test-list.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ TEST_DECLARE (spawn_setgid_fails)
TEST_DECLARE (spawn_stdout_to_file)
TEST_DECLARE (spawn_stdout_and_stderr_to_file)
TEST_DECLARE (spawn_auto_unref)
TEST_DECLARE (spawn_closed_process_io)
TEST_DECLARE (fs_poll)
TEST_DECLARE (kill)
TEST_DECLARE (fs_file_noent)
Expand Down Expand Up @@ -443,6 +444,7 @@ TASK_LIST_START
TEST_ENTRY (spawn_stdout_to_file)
TEST_ENTRY (spawn_stdout_and_stderr_to_file)
TEST_ENTRY (spawn_auto_unref)
TEST_ENTRY (spawn_closed_process_io)
TEST_ENTRY (fs_poll)
TEST_ENTRY (kill)

Expand Down
33 changes: 33 additions & 0 deletions test/test-spawn.c
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,39 @@ TEST_IMPL(spawn_same_stdout_stderr) {
}


TEST_IMPL(spawn_closed_process_io) {
uv_pipe_t in;
uv_write_t write_req;
uv_buf_t buf;
uv_stdio_container_t stdio[2];
static char buffer[] = "hello-from-spawn_stdin";

init_process_options("spawn_helper1", exit_cb);

uv_pipe_init(uv_default_loop(), &in, 0);
options.stdio = stdio;
options.stdio[0].flags = UV_CREATE_PIPE | UV_READABLE_PIPE;
options.stdio[0].data.stream = (uv_stream_t*) &in;
options.stdio_count = 1;

close(0); // Close process stdin.

ASSERT(0 == uv_spawn(uv_default_loop(), &process, options));

buf.base = buffer;
buf.len = sizeof(buffer);
ASSERT(0 == uv_write(&write_req, (uv_stream_t*) &in, &buf, 1, write_cb));

ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));

ASSERT(exit_cb_called == 1);
ASSERT(close_cb_called == 2); /* process, child stdin */

MAKE_VALGRIND_HAPPY();
return 0;
}


TEST_IMPL(kill) {
int r;
uv_err_t err;
Expand Down

0 comments on commit fb21509

Please sign in to comment.