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 09d80cc
Show file tree
Hide file tree
Showing 3 changed files with 43 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
40 changes: 40 additions & 0 deletions test/test-spawn.c
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,46 @@ TEST_IMPL(spawn_same_stdout_stderr) {
}


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

r = uv_tty_init(uv_default_loop(), &stdin, 0, 1);
ASSERT(r == 0);
uv_close((uv_handle_t*)&stdin, close_cb);

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;

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

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

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

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

MAKE_VALGRIND_HAPPY();
return 0;
}


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

0 comments on commit 09d80cc

Please sign in to comment.