2011-08-01 01:00:47 +02:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
|
|
var EventEmitter = require('events').EventEmitter;
|
2011-10-07 22:58:49 +02:00
|
|
|
var net = require('net');
|
2011-08-01 01:00:47 +02:00
|
|
|
var Process = process.binding('process_wrap').Process;
|
|
|
|
var inherits = require('util').inherits;
|
2011-08-04 18:40:41 +02:00
|
|
|
var constants; // if (!constants) constants = process.binding('constants');
|
2011-08-01 01:00:47 +02:00
|
|
|
|
2011-10-07 09:57:37 +02:00
|
|
|
var Pipe;
|
|
|
|
|
2011-08-01 01:00:47 +02:00
|
|
|
|
|
|
|
// constructors for lazy loading
|
2011-10-07 09:57:37 +02:00
|
|
|
function createPipe(ipc) {
|
|
|
|
// Lazy load
|
|
|
|
if (!Pipe) {
|
2011-12-04 09:09:02 +01:00
|
|
|
Pipe = process.binding('pipe_wrap').Pipe;
|
2011-10-07 09:57:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return new Pipe(ipc);
|
2011-08-01 01:00:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function createSocket(pipe, readable) {
|
2011-10-07 22:58:49 +02:00
|
|
|
var s = new net.Socket({ handle: pipe });
|
2011-08-01 01:00:47 +02:00
|
|
|
|
|
|
|
if (readable) {
|
|
|
|
s.writable = false;
|
|
|
|
s.readable = true;
|
|
|
|
s.resume();
|
|
|
|
} else {
|
|
|
|
s.writable = true;
|
|
|
|
s.readable = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2011-08-05 09:36:39 +02:00
|
|
|
function mergeOptions(target, overrides) {
|
|
|
|
if (overrides) {
|
|
|
|
var keys = Object.keys(overrides);
|
|
|
|
for (var i = 0, len = keys.length; i < len; i++) {
|
|
|
|
var k = keys[i];
|
|
|
|
if (overrides[k] !== undefined) {
|
|
|
|
target[k] = overrides[k];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
2011-08-01 01:00:47 +02:00
|
|
|
|
2011-10-07 09:57:37 +02:00
|
|
|
function setupChannel(target, channel) {
|
2011-11-01 09:35:48 +01:00
|
|
|
var isWindows = process.platform === 'win32';
|
2011-10-07 09:57:37 +02:00
|
|
|
target._channel = channel;
|
|
|
|
|
|
|
|
var jsonBuffer = '';
|
|
|
|
|
2011-11-01 09:35:48 +01:00
|
|
|
if (isWindows) {
|
|
|
|
var setSimultaneousAccepts = function(handle) {
|
2011-12-09 13:00:40 +01:00
|
|
|
var simultaneousAccepts = (process.env.NODE_MANY_ACCEPTS &&
|
|
|
|
process.env.NODE_MANY_ACCEPTS != '0') ? true : false;
|
2011-11-01 09:35:48 +01:00
|
|
|
|
|
|
|
if (handle._simultaneousAccepts != simultaneousAccepts) {
|
|
|
|
handle.setSimultaneousAccepts(simultaneousAccepts);
|
|
|
|
handle._simultaneousAccepts = simultaneousAccepts;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-08 01:43:53 +02:00
|
|
|
channel.onread = function(pool, offset, length, recvHandle) {
|
2011-11-01 09:35:48 +01:00
|
|
|
if (recvHandle && setSimultaneousAccepts) {
|
|
|
|
// Update simultaneous accepts on Windows
|
|
|
|
setSimultaneousAccepts(recvHandle);
|
|
|
|
}
|
|
|
|
|
2011-10-07 09:57:37 +02:00
|
|
|
if (pool) {
|
2011-10-07 22:58:49 +02:00
|
|
|
jsonBuffer += pool.toString('ascii', offset, offset + length);
|
|
|
|
|
2011-10-12 02:54:55 +02:00
|
|
|
var i, start = 0;
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
|
|
//Linebreak is used as a message end sign
|
2011-10-12 02:54:55 +02:00
|
|
|
while ((i = jsonBuffer.indexOf('\n', start)) >= 0) {
|
|
|
|
var json = jsonBuffer.slice(start, i);
|
2011-10-07 22:58:49 +02:00
|
|
|
var message = JSON.parse(json);
|
|
|
|
|
2011-12-20 10:42:48 +01:00
|
|
|
//Filter out internal messages
|
|
|
|
//if cmd property begin with "_NODE"
|
|
|
|
if (message !== null &&
|
|
|
|
typeof message === 'object' &&
|
|
|
|
typeof message.cmd === 'string' &&
|
|
|
|
message.cmd.indexOf('NODE_') === 0) {
|
2012-01-10 23:07:12 +01:00
|
|
|
target.emit('internalMessage', message, recvHandle);
|
2011-12-20 10:42:48 +01:00
|
|
|
}
|
|
|
|
//Non-internal message
|
|
|
|
else {
|
|
|
|
target.emit('message', message, recvHandle);
|
|
|
|
}
|
|
|
|
|
2011-12-09 13:00:40 +01:00
|
|
|
start = i + 1;
|
2011-10-07 09:57:37 +02:00
|
|
|
}
|
2011-10-12 02:54:55 +02:00
|
|
|
jsonBuffer = jsonBuffer.slice(start);
|
2011-10-07 22:58:49 +02:00
|
|
|
|
2011-10-07 09:57:37 +02:00
|
|
|
} else {
|
|
|
|
channel.close();
|
|
|
|
target._channel = null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-10-08 01:43:53 +02:00
|
|
|
target.send = function(message, sendHandle) {
|
2011-12-18 01:26:00 +01:00
|
|
|
if (typeof message === 'undefined') {
|
|
|
|
throw new TypeError('message cannot be undefined');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!target._channel) throw new Error("channel closed");
|
2011-10-07 09:57:37 +02:00
|
|
|
|
|
|
|
// For overflow protection don't write if channel queue is too deep.
|
|
|
|
if (channel.writeQueueSize > 1024 * 1024) {
|
2011-11-04 16:06:48 +01:00
|
|
|
return false;
|
2011-10-07 09:57:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var buffer = Buffer(JSON.stringify(message) + '\n');
|
|
|
|
|
2011-11-01 09:35:48 +01:00
|
|
|
if (sendHandle && setSimultaneousAccepts) {
|
|
|
|
// Update simultaneous accepts on Windows
|
|
|
|
setSimultaneousAccepts(sendHandle);
|
|
|
|
}
|
|
|
|
|
2011-10-08 01:43:53 +02:00
|
|
|
var writeReq = channel.write(buffer, 0, buffer.length, sendHandle);
|
2011-10-07 09:57:37 +02:00
|
|
|
|
|
|
|
if (!writeReq) {
|
2011-12-09 13:00:40 +01:00
|
|
|
throw new Error(errno + 'cannot write to IPC channel.');
|
2011-10-07 09:57:37 +02:00
|
|
|
}
|
|
|
|
|
2011-10-07 22:58:49 +02:00
|
|
|
writeReq.oncomplete = nop;
|
|
|
|
|
2011-10-07 09:57:37 +02:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
channel.readStart();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function nop() { }
|
|
|
|
|
|
|
|
|
|
|
|
exports.fork = function(modulePath, args, options) {
|
|
|
|
if (!options) options = {};
|
|
|
|
|
2011-10-14 10:04:04 +02:00
|
|
|
args = args ? args.slice(0) : [];
|
2011-10-07 09:57:37 +02:00
|
|
|
args.unshift(modulePath);
|
|
|
|
|
|
|
|
if (options.stdinStream) {
|
2011-12-09 13:00:40 +01:00
|
|
|
throw new Error('stdinStream not allowed for fork()');
|
2011-10-07 09:57:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (options.customFds) {
|
2011-12-09 13:00:40 +01:00
|
|
|
throw new Error('customFds not allowed for fork()');
|
2011-10-07 09:57:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Leave stdin open for the IPC channel. stdout and stderr should be the
|
2011-12-17 11:52:40 +01:00
|
|
|
// same as the parent's if silent isn't set.
|
|
|
|
options.customFds = (options.silent ? [-1, -1, -1] : [-1, 1, 2]);
|
2011-10-07 09:57:37 +02:00
|
|
|
|
|
|
|
// Just need to set this - child process won't actually use the fd.
|
|
|
|
// For backwards compat - this can be changed to 'NODE_CHANNEL' before v0.6.
|
2011-10-10 23:49:02 +02:00
|
|
|
if (!options.env) options.env = { };
|
|
|
|
options.env.NODE_CHANNEL_FD = 42;
|
2011-10-07 09:57:37 +02:00
|
|
|
|
|
|
|
// stdin is the IPC channel.
|
2012-01-06 00:42:10 +01:00
|
|
|
if (!options.thread) options.stdinStream = createPipe(true);
|
2011-10-07 09:57:37 +02:00
|
|
|
|
|
|
|
var child = spawn(process.execPath, args, options);
|
|
|
|
|
2012-01-06 00:42:10 +01:00
|
|
|
if (!options.thread) setupChannel(child, options.stdinStream);
|
2011-10-07 09:57:37 +02:00
|
|
|
|
|
|
|
child.on('exit', function() {
|
|
|
|
if (child._channel) {
|
|
|
|
child._channel.close();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return child;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
exports._forkChild = function() {
|
|
|
|
// set process.send()
|
|
|
|
var p = createPipe(true);
|
|
|
|
p.open(0);
|
|
|
|
setupChannel(process, p);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-08-01 01:24:29 +02:00
|
|
|
exports.exec = function(command /*, options, callback */) {
|
2011-08-05 09:36:39 +02:00
|
|
|
var file, args, options, callback;
|
|
|
|
|
|
|
|
if (typeof arguments[1] === 'function') {
|
|
|
|
options = undefined;
|
|
|
|
callback = arguments[1];
|
2011-08-02 02:40:46 +02:00
|
|
|
} else {
|
2011-08-05 09:36:39 +02:00
|
|
|
options = arguments[1];
|
|
|
|
callback = arguments[2];
|
2011-08-02 02:40:46 +02:00
|
|
|
}
|
2011-08-05 09:36:39 +02:00
|
|
|
|
|
|
|
if (process.platform === 'win32') {
|
|
|
|
file = 'cmd.exe';
|
|
|
|
args = ['/s', '/c', '"' + command + '"'];
|
|
|
|
// Make a shallow copy before patching so we don't clobber the user's
|
|
|
|
// options object.
|
|
|
|
options = mergeOptions({}, options);
|
|
|
|
options.windowsVerbatimArguments = true;
|
|
|
|
} else {
|
|
|
|
file = '/bin/sh';
|
|
|
|
args = ['-c', command];
|
|
|
|
}
|
|
|
|
return exports.execFile(file, args, options, callback);
|
2011-08-01 01:24:29 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
exports.execFile = function(file /* args, options, callback */) {
|
|
|
|
var args, optionArg, callback;
|
|
|
|
var options = {
|
|
|
|
encoding: 'utf8',
|
|
|
|
timeout: 0,
|
|
|
|
maxBuffer: 200 * 1024,
|
|
|
|
killSignal: 'SIGTERM',
|
|
|
|
setsid: false,
|
|
|
|
cwd: null,
|
|
|
|
env: null
|
|
|
|
};
|
|
|
|
|
|
|
|
// Parse the parameters.
|
|
|
|
|
|
|
|
if (typeof arguments[arguments.length - 1] === 'function') {
|
|
|
|
callback = arguments[arguments.length - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Array.isArray(arguments[1])) {
|
|
|
|
args = arguments[1];
|
|
|
|
if (typeof arguments[2] === 'object') optionArg = arguments[2];
|
|
|
|
} else {
|
|
|
|
args = [];
|
|
|
|
if (typeof arguments[1] === 'object') optionArg = arguments[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge optionArg into options
|
2011-08-05 09:36:39 +02:00
|
|
|
mergeOptions(options, optionArg);
|
2011-08-01 01:24:29 +02:00
|
|
|
|
|
|
|
var child = spawn(file, args, {
|
|
|
|
cwd: options.cwd,
|
2011-08-05 09:36:39 +02:00
|
|
|
env: options.env,
|
|
|
|
windowsVerbatimArguments: !!options.windowsVerbatimArguments
|
2011-08-01 01:24:29 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
var stdout = '';
|
|
|
|
var stderr = '';
|
|
|
|
var killed = false;
|
|
|
|
var exited = false;
|
|
|
|
var timeoutId;
|
|
|
|
|
|
|
|
var err;
|
|
|
|
|
|
|
|
function exithandler(code, signal) {
|
|
|
|
if (exited) return;
|
|
|
|
exited = true;
|
|
|
|
|
|
|
|
if (timeoutId) {
|
|
|
|
clearTimeout(timeoutId);
|
|
|
|
timeoutId = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!callback) return;
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
callback(err, stdout, stderr);
|
|
|
|
} else if (code === 0 && signal === null) {
|
|
|
|
callback(null, stdout, stderr);
|
|
|
|
} else {
|
|
|
|
var e = new Error('Command failed: ' + stderr);
|
|
|
|
e.killed = child.killed || killed;
|
|
|
|
e.code = code;
|
|
|
|
e.signal = signal;
|
|
|
|
callback(e, stdout, stderr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function kill() {
|
|
|
|
killed = true;
|
|
|
|
child.kill(options.killSignal);
|
|
|
|
process.nextTick(function() {
|
|
|
|
exithandler(null, options.killSignal);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.timeout > 0) {
|
|
|
|
timeoutId = setTimeout(function() {
|
|
|
|
kill();
|
|
|
|
timeoutId = null;
|
|
|
|
}, options.timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
child.stdout.setEncoding(options.encoding);
|
|
|
|
child.stderr.setEncoding(options.encoding);
|
|
|
|
|
|
|
|
child.stdout.addListener('data', function(chunk) {
|
|
|
|
stdout += chunk;
|
|
|
|
if (stdout.length > options.maxBuffer) {
|
|
|
|
err = new Error('maxBuffer exceeded.');
|
|
|
|
kill();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
child.stderr.addListener('data', function(chunk) {
|
|
|
|
stderr += chunk;
|
|
|
|
if (stderr.length > options.maxBuffer) {
|
|
|
|
err = new Error('maxBuffer exceeded.');
|
|
|
|
kill();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
child.addListener('exit', exithandler);
|
|
|
|
|
|
|
|
return child;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-08-01 01:00:47 +02:00
|
|
|
var spawn = exports.spawn = function(file, args, options) {
|
2011-10-30 16:49:20 +01:00
|
|
|
args = args ? args.slice(0) : [];
|
2011-08-01 01:00:47 +02:00
|
|
|
args.unshift(file);
|
|
|
|
|
2011-08-01 02:51:41 +02:00
|
|
|
var env = (options ? options.env : null) || process.env;
|
|
|
|
var envPairs = [];
|
|
|
|
for (var key in env) {
|
|
|
|
envPairs.push(key + '=' + env[key]);
|
|
|
|
}
|
|
|
|
|
2012-01-06 00:42:10 +01:00
|
|
|
var child = (options && options.thread) ? (new Isolate) : (new ChildProcess);
|
2011-09-14 21:33:38 +02:00
|
|
|
|
2011-08-01 01:00:47 +02:00
|
|
|
child.spawn({
|
|
|
|
file: file,
|
|
|
|
args: args,
|
2011-08-01 02:51:41 +02:00
|
|
|
cwd: options ? options.cwd : null,
|
2011-08-05 09:36:39 +02:00
|
|
|
windowsVerbatimArguments: !!(options && options.windowsVerbatimArguments),
|
2011-09-14 21:33:38 +02:00
|
|
|
envPairs: envPairs,
|
2011-10-07 09:57:37 +02:00
|
|
|
customFds: options ? options.customFds : null,
|
2012-01-06 19:28:20 +01:00
|
|
|
stdinStream: options ? options.stdinStream : null,
|
|
|
|
options: options
|
2011-08-01 01:00:47 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return child;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
function maybeExit(subprocess) {
|
|
|
|
subprocess._closesGot++;
|
|
|
|
|
|
|
|
if (subprocess._closesGot == subprocess._closesNeeded) {
|
|
|
|
subprocess.emit('exit', subprocess.exitCode, subprocess.signalCode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function ChildProcess() {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
this._closesNeeded = 1;
|
|
|
|
this._closesGot = 0;
|
|
|
|
|
|
|
|
this.signalCode = null;
|
|
|
|
this.exitCode = null;
|
2011-11-02 17:52:47 +01:00
|
|
|
this.killed = false;
|
2011-08-01 01:00:47 +02:00
|
|
|
|
|
|
|
this._internal = new Process();
|
|
|
|
this._internal.onexit = function(exitCode, signalCode) {
|
2011-08-04 18:40:41 +02:00
|
|
|
//
|
|
|
|
// follow 0.4.x behaviour:
|
|
|
|
//
|
|
|
|
// - normally terminated processes don't touch this.signalCode
|
|
|
|
// - signaled processes don't touch this.exitCode
|
|
|
|
//
|
|
|
|
if (signalCode) {
|
|
|
|
self.signalCode = signalCode;
|
|
|
|
} else {
|
|
|
|
self.exitCode = exitCode;
|
|
|
|
}
|
2011-08-01 01:00:47 +02:00
|
|
|
|
|
|
|
if (self.stdin) {
|
|
|
|
self.stdin.destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
self._internal.close();
|
|
|
|
self._internal = null;
|
|
|
|
|
|
|
|
maybeExit(self);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
inherits(ChildProcess, EventEmitter);
|
|
|
|
|
|
|
|
|
|
|
|
function setStreamOption(name, index, options) {
|
2011-10-07 09:57:37 +02:00
|
|
|
// Skip if we already have options.stdinStream
|
|
|
|
if (options[name]) return;
|
|
|
|
|
2011-08-01 01:00:47 +02:00
|
|
|
if (options.customFds &&
|
|
|
|
typeof options.customFds[index] == 'number' &&
|
|
|
|
options.customFds[index] !== -1) {
|
|
|
|
if (options.customFds[index] === index) {
|
|
|
|
options[name] = null;
|
|
|
|
} else {
|
2011-10-05 00:08:18 +02:00
|
|
|
throw new Error('customFds not yet supported');
|
2011-08-01 01:00:47 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
options[name] = createPipe();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ChildProcess.prototype.spawn = function(options) {
|
|
|
|
var self = this;
|
|
|
|
|
2011-10-05 00:08:18 +02:00
|
|
|
setStreamOption('stdinStream', 0, options);
|
|
|
|
setStreamOption('stdoutStream', 1, options);
|
|
|
|
setStreamOption('stderrStream', 2, options);
|
2011-08-01 01:00:47 +02:00
|
|
|
|
|
|
|
var r = this._internal.spawn(options);
|
|
|
|
|
2011-08-01 19:19:41 +02:00
|
|
|
if (r) {
|
|
|
|
if (options.stdinStream) {
|
|
|
|
options.stdinStream.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.stdoutStream) {
|
|
|
|
options.stdoutStream.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.stderrStream) {
|
|
|
|
options.stderrStream.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
this._internal.close();
|
|
|
|
this._internal = null;
|
2011-11-28 20:30:48 +01:00
|
|
|
throw errnoException(errno, 'spawn');
|
2011-08-01 19:19:41 +02:00
|
|
|
}
|
|
|
|
|
2011-08-01 01:00:47 +02:00
|
|
|
this.pid = this._internal.pid;
|
|
|
|
|
|
|
|
if (options.stdinStream) {
|
|
|
|
this.stdin = createSocket(options.stdinStream, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.stdoutStream) {
|
|
|
|
this.stdout = createSocket(options.stdoutStream, true);
|
|
|
|
this._closesNeeded++;
|
|
|
|
this.stdout.on('close', function() {
|
|
|
|
maybeExit(self);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.stderrStream) {
|
|
|
|
this.stderr = createSocket(options.stderrStream, true);
|
|
|
|
this._closesNeeded++;
|
|
|
|
this.stderr.on('close', function() {
|
|
|
|
maybeExit(self);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
};
|
|
|
|
|
2011-08-04 18:40:41 +02:00
|
|
|
|
2011-08-01 19:19:41 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-08-01 01:00:47 +02:00
|
|
|
|
|
|
|
ChildProcess.prototype.kill = function(sig) {
|
2011-08-04 18:40:41 +02:00
|
|
|
if (!constants) {
|
|
|
|
constants = process.binding('constants');
|
|
|
|
}
|
|
|
|
|
|
|
|
sig = sig || 'SIGTERM';
|
|
|
|
var signal = constants[sig];
|
|
|
|
|
|
|
|
if (!signal) {
|
|
|
|
throw new Error('Unknown signal: ' + sig);
|
|
|
|
}
|
|
|
|
|
2011-08-04 21:24:38 +02:00
|
|
|
if (this._internal) {
|
2011-10-13 01:11:55 +02:00
|
|
|
this.killed = true;
|
2011-08-04 18:40:41 +02:00
|
|
|
var r = this._internal.kill(signal);
|
|
|
|
// TODO: raise error if r == -1?
|
|
|
|
}
|
2011-08-01 01:00:47 +02:00
|
|
|
};
|
2012-01-06 00:42:10 +01:00
|
|
|
|
|
|
|
|
|
|
|
// Lazy loaded.
|
|
|
|
var isolates = null;
|
|
|
|
|
|
|
|
|
|
|
|
function Isolate() {
|
|
|
|
if (!process.features.isolates) {
|
|
|
|
throw new Error('Compiled without isolates support.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isolates) {
|
|
|
|
isolates = process.binding('isolates');
|
|
|
|
}
|
|
|
|
|
|
|
|
this._handle = null;
|
|
|
|
}
|
|
|
|
inherits(Isolate, EventEmitter); // maybe inherit from ChildProcess?
|
|
|
|
|
|
|
|
|
|
|
|
Isolate.prototype.spawn = function(options) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
if (self._handle) throw new Error('Isolate already running.');
|
2012-01-06 19:28:20 +01:00
|
|
|
self._handle = isolates.create(options.args, options.options);
|
2012-01-06 00:42:10 +01:00
|
|
|
if (!self._handle) throw new Error('Cannot create isolate.');
|
|
|
|
|
|
|
|
self._handle.onmessage = function(msg) {
|
|
|
|
msg = JSON.parse('' + msg);
|
|
|
|
self.emit('message', msg);
|
|
|
|
};
|
|
|
|
|
|
|
|
self._handle.onexit = function() {
|
|
|
|
self._handle = null;
|
|
|
|
self.emit('exit');
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Isolate.prototype.kill = function(sig) {
|
|
|
|
if (!this._handle) throw new Error('Isolate not running.');
|
|
|
|
// ignore silently for now, need a way to signal the other thread
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Isolate.prototype.send = function(msg) {
|
|
|
|
if (typeof msg === 'undefined') throw new TypeError('Bad argument.');
|
|
|
|
if (!this._handle) throw new Error('Isolate not running.');
|
|
|
|
msg = JSON.stringify(msg);
|
|
|
|
msg = new Buffer(msg);
|
|
|
|
return this._handle.send(msg);
|
|
|
|
};
|