Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
process.kill to use uv_kill
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Zinkovsky committed Nov 3, 2011
1 parent 359a65a commit 24a69d2
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 34 deletions.
15 changes: 7 additions & 8 deletions src/node.cc
Expand Up @@ -1623,26 +1623,25 @@ v8::Handle<v8::Value> MemoryUsage(const v8::Arguments& args) {
}


#ifdef __POSIX__

Handle<Value> Kill(const Arguments& args) {
HandleScope scope;

if (args.Length() != 2) {
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<Object> exports);

Expand Down Expand Up @@ -2128,9 +2127,9 @@ Handle<Object> 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);

Expand Down
4 changes: 4 additions & 0 deletions src/node.h
Expand Up @@ -54,6 +54,10 @@
#define PATH_MAX MAX_PATH
#endif

#ifdef _WIN32
# define SIGKILL 9
#endif

#include <uv.h>
#include <v8.h>
#include <sys/types.h> /* struct stat */
Expand Down
48 changes: 27 additions & 21 deletions src/node.js
Expand Up @@ -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');
Expand Down Expand Up @@ -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() {
Expand Down
5 changes: 0 additions & 5 deletions test/simple/test-process-kill-null.js
Expand Up @@ -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;
Expand Down

0 comments on commit 24a69d2

Please sign in to comment.