From 24a69d22a0e7c3e089ab56f5b3800a69a44c6963 Mon Sep 17 00:00:00 2001 From: Igor Zinkovsky Date: Wed, 2 Nov 2011 15:06:48 -0700 Subject: [PATCH] process.kill to use uv_kill --- src/node.cc | 15 ++++----- src/node.h | 4 +++ src/node.js | 48 +++++++++++++++------------ test/simple/test-process-kill-null.js | 5 --- 4 files changed, 38 insertions(+), 34 deletions(-) diff --git a/src/node.cc b/src/node.cc index 751d8ef061..07911ccce5 100644 --- a/src/node.cc +++ b/src/node.cc @@ -1623,8 +1623,6 @@ v8::Handle MemoryUsage(const v8::Arguments& args) { } -#ifdef __POSIX__ - Handle Kill(const Arguments& args) { HandleScope scope; @@ -1632,17 +1630,18 @@ Handle Kill(const Arguments& args) { return ThrowException(Exception::Error(String::New("Bad argument."))); } - pid_t pid = args[0]->IntegerValue(); + int pid = args[0]->IntegerValue(); int sig = args[1]->Int32Value(); - int r = kill(pid, sig); + uv_err_t err = uv_kill(pid, sig); - if (r != 0) return ThrowException(ErrnoException(errno, "kill")); + if (err.code != UV_OK) { + SetErrno(err); + return scope.Close(Integer::New(-1)); + } return Undefined(); } -#endif // __POSIX__ - typedef void (UV_DYNAMIC* extInit)(Handle exports); @@ -2128,9 +2127,9 @@ Handle SetupProcessObject(int argc, char *argv[]) { NODE_SET_METHOD(process, "setgid", SetGid); NODE_SET_METHOD(process, "getgid", GetGid); +#endif // __POSIX__ NODE_SET_METHOD(process, "_kill", Kill); -#endif // __POSIX__ NODE_SET_METHOD(process, "dlopen", DLOpen); diff --git a/src/node.h b/src/node.h index cd56591a82..aebb87db00 100644 --- a/src/node.h +++ b/src/node.h @@ -54,6 +54,10 @@ #define PATH_MAX MAX_PATH #endif +#ifdef _WIN32 +# define SIGKILL 9 +#endif + #include #include #include /* struct stat */ diff --git a/src/node.js b/src/node.js index 403c618307..c3f7f9f937 100644 --- a/src/node.js +++ b/src/node.js @@ -208,6 +208,16 @@ }; }; + function errnoException(errorno, syscall) { + // TODO make this more compatible with ErrnoException from src/node.cc + // Once all of Node is using this function the ErrnoException from + // src/node.cc should be removed. + var e = new Error(syscall + ' ' + errorno); + e.errno = e.code = errorno; + e.syscall = syscall; + return e; + } + function createWritableStdioStream(fd) { var stream; var tty_wrap = process.binding('tty_wrap'); @@ -318,34 +328,30 @@ }; startup.processKillAndExit = function() { - var isWindows = process.platform === 'win32'; - process.exit = function(code) { process.emit('exit', code || 0); process.reallyExit(code || 0); }; - if (isWindows) { - process.kill = function(pid, sig) { - console.warn('process.kill() is not supported on Windows. Use ' + - 'child.kill() to kill a process that was started ' + - 'with child_process.spawn().'); - } - } else { - process.kill = function(pid, sig) { - // preserve null signal - if (0 === sig) { - process._kill(pid, 0); + process.kill = function(pid, sig) { + var r; + + // preserve null signal + if (0 === sig) { + r = process._kill(pid, 0); + } else { + sig = sig || 'SIGTERM'; + if (startup.lazyConstants()[sig]) { + r = process._kill(pid, startup.lazyConstants()[sig]); } else { - sig = sig || 'SIGTERM'; - if (startup.lazyConstants()[sig]) { - process._kill(pid, startup.lazyConstants()[sig]); - } else { - throw new Error('Unknown signal: ' + sig); - } + throw new Error('Unknown signal: ' + sig); } - }; - } + } + + if (r) { + throw errnoException('kill', errno); + } + }; }; startup.processSignalHandlers = function() { diff --git a/test/simple/test-process-kill-null.js b/test/simple/test-process-kill-null.js index 1b9ba17156..bf096a6442 100644 --- a/test/simple/test-process-kill-null.js +++ b/test/simple/test-process-kill-null.js @@ -19,11 +19,6 @@ // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE // USE OR OTHER DEALINGS IN THE SOFTWARE. -if (process.platform === 'win32') { - console.warn('Skipping because process.kill is not supported on windows'); - process.exit(0); -} - var assert = require('assert'); var spawn = require('child_process').spawn;