From 769a35024f399e051f16dd6c8e663f82ed37cbf2 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Wed, 3 Mar 2010 10:45:58 -0800 Subject: [PATCH] Allow passing env to child process --- src/node_child_process.cc | 8 +++++--- src/node_child_process.h | 2 +- test/simple/test-child-process-env.js | 12 ++++++++++++ 3 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 test/simple/test-child-process-env.js diff --git a/src/node_child_process.cc b/src/node_child_process.cc index 4dc3d437af..6902455dd0 100644 --- a/src/node_child_process.cc +++ b/src/node_child_process.cc @@ -9,6 +9,8 @@ #include #include +extern char **environ; + namespace node { using namespace v8; @@ -276,7 +278,7 @@ static inline int SetNonBlocking(int fd) { // Note that args[0] must be the same as the "file" param. This is an // execvp() requirement. -int ChildProcess::Spawn(const char *file, char *const args[], char *const env[]) { +int ChildProcess::Spawn(const char *file, char *const args[], char **env) { assert(pid_ == 0); assert(stdout_fd_ == -1); assert(stderr_fd_ == -1); @@ -315,11 +317,11 @@ int ChildProcess::Spawn(const char *file, char *const args[], char *const env[]) close(stdin_pipe[1]); // close write end dup2(stdin_pipe[0], STDIN_FILENO); + environ = env; + execvp(file, args); perror("execvp()"); _exit(127); - - // TODO search PATH and use: execve(file, argv, env); } // Parent. diff --git a/src/node_child_process.h b/src/node_child_process.h index 1cdd974d09..b37db9f36a 100644 --- a/src/node_child_process.h +++ b/src/node_child_process.h @@ -28,7 +28,7 @@ class ChildProcess : EventEmitter { ChildProcess(); ~ChildProcess(); - int Spawn(const char *file, char *const argv[], char *const env[]); + int Spawn(const char *file, char *const argv[], char **env); int Write(const char *str, size_t len); int Close(void); int Kill(int sig); diff --git a/test/simple/test-child-process-env.js b/test/simple/test-child-process-env.js new file mode 100644 index 0000000000..4b5d902030 --- /dev/null +++ b/test/simple/test-child-process-env.js @@ -0,0 +1,12 @@ +process.mixin(require("../common")); +child = process.createChildProcess('/usr/bin/env', [], {'HELLO' : 'WORLD'}); +response = ""; + +child.addListener("output", function (chunk) { + puts("stdout: " + JSON.stringify(chunk)); + if (chunk) response += chunk; +}); + +process.addListener('exit', function () { + assert.ok(response.indexOf('HELLO=WORLD') >= 0); +});