Skip to content
Browse files

Initial stdin support

Not correct yet.
  • Loading branch information...
1 parent 8ddd5e5 commit 847d87fd3f74a675ca4bcf1175153851b0cdcc68 @felixge felixge committed with piscisaureus Sep 13, 2011
Showing with 53 additions and 1 deletion.
  1. +3 −0 include/uv.h
  2. +28 −0 src/unix/process.c
  3. +2 −0 test/test-list.h
  4. +20 −1 test/test-spawn-sync.c
View
3 include/uv.h
@@ -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;
View
28 src/unix/process.c
@@ -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;
@@ -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;
@@ -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);
@@ -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 */
@@ -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]);
}
@@ -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]);
}
@@ -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]);
}
View
2 test/test-list.h
@@ -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)
@@ -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)
View
21 test/test-spawn-sync.c
@@ -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) {
@@ -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();

0 comments on commit 847d87f

Please sign in to comment.
Something went wrong with that request. Please try again.