2015-05-22 16:52:05 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const StringDecoder = require('string_decoder').StringDecoder;
|
2015-09-17 00:45:29 +02:00
|
|
|
const EventEmitter = require('events');
|
2015-05-22 16:52:05 +02:00
|
|
|
const net = require('net');
|
|
|
|
const dgram = require('dgram');
|
|
|
|
const util = require('util');
|
|
|
|
const assert = require('assert');
|
|
|
|
|
|
|
|
const Process = process.binding('process_wrap').Process;
|
|
|
|
const WriteWrap = process.binding('stream_wrap').WriteWrap;
|
|
|
|
const uv = process.binding('uv');
|
|
|
|
const Pipe = process.binding('pipe_wrap').Pipe;
|
|
|
|
const TTY = process.binding('tty_wrap').TTY;
|
|
|
|
const TCP = process.binding('tcp_wrap').TCP;
|
|
|
|
const UDP = process.binding('udp_wrap').UDP;
|
|
|
|
const SocketList = require('internal/socket_list');
|
2017-01-06 02:12:26 +01:00
|
|
|
const { isUint8Array } = process.binding('util');
|
2016-12-23 10:06:20 +01:00
|
|
|
const { convertToValidSignal } = require('internal/util');
|
2015-05-22 16:52:05 +02:00
|
|
|
|
|
|
|
const errnoException = util._errnoException;
|
|
|
|
const SocketListSend = SocketList.SocketListSend;
|
|
|
|
const SocketListReceive = SocketList.SocketListReceive;
|
|
|
|
|
|
|
|
// this object contain function to convert TCP objects to native handle objects
|
|
|
|
// and back again.
|
|
|
|
const handleConversion = {
|
|
|
|
'net.Native': {
|
|
|
|
simultaneousAccepts: true,
|
|
|
|
|
2016-02-16 21:12:56 +01:00
|
|
|
send: function(message, handle, options) {
|
2015-05-22 16:52:05 +02:00
|
|
|
return handle;
|
|
|
|
},
|
|
|
|
|
|
|
|
got: function(message, handle, emit) {
|
|
|
|
emit(handle);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
'net.Server': {
|
|
|
|
simultaneousAccepts: true,
|
|
|
|
|
2016-02-16 21:12:56 +01:00
|
|
|
send: function(message, server, options) {
|
2015-05-22 16:52:05 +02:00
|
|
|
return server._handle;
|
|
|
|
},
|
|
|
|
|
|
|
|
got: function(message, handle, emit) {
|
|
|
|
var server = new net.Server();
|
|
|
|
server.listen(handle, function() {
|
|
|
|
emit(server);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
'net.Socket': {
|
2016-02-16 21:12:56 +01:00
|
|
|
send: function(message, socket, options) {
|
2015-05-22 16:52:05 +02:00
|
|
|
if (!socket._handle)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// if the socket was created by net.Server
|
|
|
|
if (socket.server) {
|
|
|
|
// the slave should keep track of the socket
|
|
|
|
message.key = socket.server._connectionKey;
|
|
|
|
|
2016-10-27 20:36:49 +02:00
|
|
|
var firstTime = !this.channel.sockets.send[message.key];
|
2015-05-22 16:52:05 +02:00
|
|
|
var socketList = getSocketList('send', this, message.key);
|
|
|
|
|
|
|
|
// the server should no longer expose a .connection property
|
|
|
|
// and when asked to close it should query the socket status from
|
|
|
|
// the slaves
|
|
|
|
if (firstTime) socket.server._setupSlave(socketList);
|
|
|
|
|
|
|
|
// Act like socket is detached
|
2016-02-17 04:18:10 +01:00
|
|
|
if (!options.keepOpen)
|
|
|
|
socket.server._connections--;
|
2015-05-22 16:52:05 +02:00
|
|
|
}
|
|
|
|
|
2016-02-17 04:18:10 +01:00
|
|
|
var handle = socket._handle;
|
|
|
|
|
2015-05-22 16:52:05 +02:00
|
|
|
// remove handle from socket object, it will be closed when the socket
|
|
|
|
// will be sent
|
2016-02-17 04:18:10 +01:00
|
|
|
if (!options.keepOpen) {
|
2016-08-18 19:38:53 +02:00
|
|
|
handle.onread = nop;
|
2016-02-17 04:18:10 +01:00
|
|
|
socket._handle = null;
|
|
|
|
}
|
2015-05-22 16:52:05 +02:00
|
|
|
|
|
|
|
return handle;
|
|
|
|
},
|
|
|
|
|
2016-06-16 23:00:16 +02:00
|
|
|
postSend: function(handle, options, target) {
|
|
|
|
// Store the handle after successfully sending it, so it can be closed
|
|
|
|
// when the NODE_HANDLE_ACK is received. If the handle could not be sent,
|
|
|
|
// just close it.
|
|
|
|
if (handle && !options.keepOpen) {
|
|
|
|
if (target) {
|
|
|
|
// There can only be one _pendingHandle as passing handles are
|
|
|
|
// processed one at a time: handles are stored in _handleQueue while
|
|
|
|
// waiting for the NODE_HANDLE_ACK of the current passing handle.
|
|
|
|
assert(!target._pendingHandle);
|
|
|
|
target._pendingHandle = handle;
|
|
|
|
} else {
|
|
|
|
handle.close();
|
|
|
|
}
|
|
|
|
}
|
2015-05-22 16:52:05 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
got: function(message, handle, emit) {
|
|
|
|
var socket = new net.Socket({handle: handle});
|
|
|
|
socket.readable = socket.writable = true;
|
|
|
|
|
|
|
|
// if the socket was created by net.Server we will track the socket
|
|
|
|
if (message.key) {
|
|
|
|
|
|
|
|
// add socket to connections list
|
|
|
|
var socketList = getSocketList('got', this, message.key);
|
|
|
|
socketList.add({
|
|
|
|
socket: socket
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
emit(socket);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
'dgram.Native': {
|
|
|
|
simultaneousAccepts: false,
|
|
|
|
|
2016-02-16 21:12:56 +01:00
|
|
|
send: function(message, handle, options) {
|
2015-05-22 16:52:05 +02:00
|
|
|
return handle;
|
|
|
|
},
|
|
|
|
|
|
|
|
got: function(message, handle, emit) {
|
|
|
|
emit(handle);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
'dgram.Socket': {
|
|
|
|
simultaneousAccepts: false,
|
|
|
|
|
2016-02-16 21:12:56 +01:00
|
|
|
send: function(message, socket, options) {
|
2015-05-22 16:52:05 +02:00
|
|
|
message.dgramType = socket.type;
|
|
|
|
|
|
|
|
return socket._handle;
|
|
|
|
},
|
|
|
|
|
|
|
|
got: function(message, handle, emit) {
|
|
|
|
var socket = new dgram.Socket(message.dgramType);
|
|
|
|
|
|
|
|
socket.bind(handle, function() {
|
|
|
|
emit(socket);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
function ChildProcess() {
|
|
|
|
EventEmitter.call(this);
|
|
|
|
|
|
|
|
this._closesNeeded = 1;
|
|
|
|
this._closesGot = 0;
|
|
|
|
this.connected = false;
|
|
|
|
|
|
|
|
this.signalCode = null;
|
|
|
|
this.exitCode = null;
|
|
|
|
this.killed = false;
|
|
|
|
this.spawnfile = null;
|
|
|
|
|
|
|
|
this._handle = new Process();
|
|
|
|
this._handle.owner = this;
|
|
|
|
|
2017-04-11 21:53:04 +02:00
|
|
|
this._handle.onexit = (exitCode, signalCode) => {
|
2015-05-22 16:52:05 +02:00
|
|
|
if (signalCode) {
|
2017-04-11 21:53:04 +02:00
|
|
|
this.signalCode = signalCode;
|
2015-05-22 16:52:05 +02:00
|
|
|
} else {
|
2017-04-11 21:53:04 +02:00
|
|
|
this.exitCode = exitCode;
|
2015-05-22 16:52:05 +02:00
|
|
|
}
|
|
|
|
|
2017-04-11 21:53:04 +02:00
|
|
|
if (this.stdin) {
|
|
|
|
this.stdin.destroy();
|
2015-05-22 16:52:05 +02:00
|
|
|
}
|
|
|
|
|
2017-04-11 21:53:04 +02:00
|
|
|
this._handle.close();
|
|
|
|
this._handle = null;
|
2015-05-22 16:52:05 +02:00
|
|
|
|
|
|
|
if (exitCode < 0) {
|
2017-04-11 21:53:04 +02:00
|
|
|
var syscall = this.spawnfile ? 'spawn ' + this.spawnfile : 'spawn';
|
|
|
|
const err = errnoException(exitCode, syscall);
|
|
|
|
|
|
|
|
if (this.spawnfile)
|
|
|
|
err.path = this.spawnfile;
|
2015-05-22 16:52:05 +02:00
|
|
|
|
2017-04-11 21:53:04 +02:00
|
|
|
err.spawnargs = this.spawnargs.slice(1);
|
|
|
|
this.emit('error', err);
|
2015-05-22 16:52:05 +02:00
|
|
|
} else {
|
2017-04-11 21:53:04 +02:00
|
|
|
this.emit('exit', this.exitCode, this.signalCode);
|
2015-05-22 16:52:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// if any of the stdio streams have not been touched,
|
|
|
|
// then pull all the data through so that it can get the
|
|
|
|
// eof and emit a 'close' event.
|
|
|
|
// Do it on nextTick so that the user has one last chance
|
|
|
|
// to consume the output, if for example they only want to
|
|
|
|
// start reading the data once the process exits.
|
2017-04-11 21:53:04 +02:00
|
|
|
process.nextTick(flushStdio, this);
|
2015-05-22 16:52:05 +02:00
|
|
|
|
2017-04-11 21:53:04 +02:00
|
|
|
maybeClose(this);
|
2015-05-22 16:52:05 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
util.inherits(ChildProcess, EventEmitter);
|
|
|
|
|
|
|
|
|
|
|
|
function flushStdio(subprocess) {
|
2017-02-14 04:39:24 +01:00
|
|
|
const stdio = subprocess.stdio;
|
|
|
|
|
|
|
|
if (stdio == null) return;
|
|
|
|
|
|
|
|
for (var i = 0; i < stdio.length; i++) {
|
|
|
|
const stream = stdio[i];
|
2016-02-02 06:57:24 +01:00
|
|
|
if (!stream || !stream.readable || stream._readableState.readableListening)
|
2017-02-14 04:39:24 +01:00
|
|
|
continue;
|
2015-05-22 16:52:05 +02:00
|
|
|
stream.resume();
|
2017-02-14 04:39:24 +01:00
|
|
|
}
|
2015-05-22 16:52:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function createSocket(pipe, readable) {
|
|
|
|
var s = new net.Socket({ handle: pipe });
|
|
|
|
|
|
|
|
if (readable) {
|
|
|
|
s.writable = false;
|
|
|
|
s.readable = true;
|
|
|
|
} else {
|
|
|
|
s.writable = true;
|
|
|
|
s.readable = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function getHandleWrapType(stream) {
|
|
|
|
if (stream instanceof Pipe) return 'pipe';
|
|
|
|
if (stream instanceof TTY) return 'tty';
|
|
|
|
if (stream instanceof TCP) return 'tcp';
|
|
|
|
if (stream instanceof UDP) return 'udp';
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ChildProcess.prototype.spawn = function(options) {
|
2016-01-12 22:04:50 +01:00
|
|
|
var ipc;
|
|
|
|
var ipcFd;
|
2017-02-14 04:39:24 +01:00
|
|
|
var i;
|
2017-04-11 21:53:04 +02:00
|
|
|
|
2017-04-11 22:37:07 +02:00
|
|
|
if (options === null || typeof options !== 'object')
|
|
|
|
throw new TypeError('"options" must be an object');
|
|
|
|
|
2016-01-12 22:04:50 +01:00
|
|
|
// If no `stdio` option was given - use default
|
|
|
|
var stdio = options.stdio || 'pipe';
|
2015-05-22 16:52:05 +02:00
|
|
|
|
|
|
|
stdio = _validateStdio(stdio, false);
|
|
|
|
|
|
|
|
ipc = stdio.ipc;
|
|
|
|
ipcFd = stdio.ipcFd;
|
|
|
|
stdio = options.stdio = stdio.stdio;
|
|
|
|
|
|
|
|
if (ipc !== undefined) {
|
|
|
|
// Let child process know about opened IPC channel
|
2017-04-11 22:37:07 +02:00
|
|
|
if (options.envPairs === undefined)
|
|
|
|
options.envPairs = [];
|
|
|
|
else if (!Array.isArray(options.envPairs))
|
|
|
|
throw new TypeError('"envPairs" must be an array');
|
|
|
|
|
2015-05-22 16:52:05 +02:00
|
|
|
options.envPairs.push('NODE_CHANNEL_FD=' + ipcFd);
|
|
|
|
}
|
|
|
|
|
2017-04-11 22:37:07 +02:00
|
|
|
if (typeof options.file === 'string')
|
|
|
|
this.spawnfile = options.file;
|
|
|
|
else
|
|
|
|
throw new TypeError('"file" must be a string');
|
|
|
|
|
|
|
|
if (Array.isArray(options.args))
|
|
|
|
this.spawnargs = options.args;
|
|
|
|
else if (options.args === undefined)
|
|
|
|
this.spawnargs = [];
|
|
|
|
else
|
|
|
|
throw new TypeError('"args" must be an array');
|
2015-05-22 16:52:05 +02:00
|
|
|
|
|
|
|
var err = this._handle.spawn(options);
|
|
|
|
|
|
|
|
// Run-time errors should emit an error, not throw an exception.
|
|
|
|
if (err === uv.UV_EAGAIN ||
|
|
|
|
err === uv.UV_EMFILE ||
|
|
|
|
err === uv.UV_ENFILE ||
|
|
|
|
err === uv.UV_ENOENT) {
|
2017-04-11 21:53:04 +02:00
|
|
|
process.nextTick(onErrorNT, this, err);
|
2015-05-22 16:52:05 +02:00
|
|
|
// There is no point in continuing when we've hit EMFILE or ENFILE
|
|
|
|
// because we won't be able to set up the stdio file descriptors.
|
|
|
|
// It's kind of silly that the de facto spec for ENOENT (the test suite)
|
|
|
|
// mandates that stdio _is_ set up, even if there is no process on the
|
|
|
|
// receiving end, but it is what it is.
|
|
|
|
if (err !== uv.UV_ENOENT) return err;
|
|
|
|
} else if (err) {
|
|
|
|
// Close all opened fds on error
|
2017-02-14 04:39:24 +01:00
|
|
|
for (i = 0; i < stdio.length; i++) {
|
|
|
|
const stream = stdio[i];
|
|
|
|
if (stream.type === 'pipe') {
|
|
|
|
stream.handle.close();
|
2015-05-22 16:52:05 +02:00
|
|
|
}
|
2017-02-14 04:39:24 +01:00
|
|
|
}
|
2015-05-22 16:52:05 +02:00
|
|
|
|
|
|
|
this._handle.close();
|
|
|
|
this._handle = null;
|
|
|
|
throw errnoException(err, 'spawn');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.pid = this._handle.pid;
|
|
|
|
|
2017-02-14 04:39:24 +01:00
|
|
|
for (i = 0; i < stdio.length; i++) {
|
|
|
|
const stream = stdio[i];
|
|
|
|
if (stream.type === 'ignore') continue;
|
2015-05-22 16:52:05 +02:00
|
|
|
|
2017-02-14 04:39:24 +01:00
|
|
|
if (stream.ipc) {
|
2017-04-11 21:53:04 +02:00
|
|
|
this._closesNeeded++;
|
2017-02-14 04:39:24 +01:00
|
|
|
continue;
|
2015-05-22 16:52:05 +02:00
|
|
|
}
|
|
|
|
|
2017-02-14 04:39:24 +01:00
|
|
|
if (stream.handle) {
|
2015-05-22 16:52:05 +02:00
|
|
|
// when i === 0 - we're dealing with stdin
|
|
|
|
// (which is the only one writable pipe)
|
2017-04-11 21:53:04 +02:00
|
|
|
stream.socket = createSocket(this.pid !== 0 ?
|
2017-02-14 04:39:24 +01:00
|
|
|
stream.handle : null, i > 0);
|
2015-05-22 16:52:05 +02:00
|
|
|
|
2017-04-11 21:53:04 +02:00
|
|
|
if (i > 0 && this.pid !== 0) {
|
|
|
|
this._closesNeeded++;
|
|
|
|
stream.socket.on('close', () => {
|
|
|
|
maybeClose(this);
|
2015-05-22 16:52:05 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2017-02-14 04:39:24 +01:00
|
|
|
}
|
2015-05-22 16:52:05 +02:00
|
|
|
|
|
|
|
this.stdin = stdio.length >= 1 && stdio[0].socket !== undefined ?
|
|
|
|
stdio[0].socket : null;
|
|
|
|
this.stdout = stdio.length >= 2 && stdio[1].socket !== undefined ?
|
|
|
|
stdio[1].socket : null;
|
|
|
|
this.stderr = stdio.length >= 3 && stdio[2].socket !== undefined ?
|
|
|
|
stdio[2].socket : null;
|
|
|
|
|
2017-04-11 21:53:04 +02:00
|
|
|
this.stdio = [];
|
|
|
|
|
|
|
|
for (i = 0; i < stdio.length; i++)
|
|
|
|
this.stdio.push(stdio[i].socket === undefined ? null : stdio[i].socket);
|
2015-05-22 16:52:05 +02:00
|
|
|
|
|
|
|
// Add .send() method and start listening for IPC data
|
|
|
|
if (ipc !== undefined) setupChannel(this, ipc);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
function onErrorNT(self, err) {
|
|
|
|
self._handle.onexit(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ChildProcess.prototype.kill = function(sig) {
|
2016-12-23 10:06:20 +01:00
|
|
|
const signal = sig === 0 ? sig :
|
|
|
|
convertToValidSignal(sig === undefined ? 'SIGTERM' : sig);
|
2015-05-22 16:52:05 +02:00
|
|
|
|
|
|
|
if (this._handle) {
|
|
|
|
var err = this._handle.kill(signal);
|
|
|
|
if (err === 0) {
|
|
|
|
/* Success. */
|
|
|
|
this.killed = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (err === uv.UV_ESRCH) {
|
|
|
|
/* Already dead. */
|
|
|
|
} else if (err === uv.UV_EINVAL || err === uv.UV_ENOSYS) {
|
|
|
|
/* The underlying platform doesn't support this signal. */
|
|
|
|
throw errnoException(err, 'kill');
|
|
|
|
} else {
|
|
|
|
/* Other error, almost certainly EPERM. */
|
|
|
|
this.emit('error', errnoException(err, 'kill'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Kill didn't succeed. */
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
ChildProcess.prototype.ref = function() {
|
|
|
|
if (this._handle) this._handle.ref();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
ChildProcess.prototype.unref = function() {
|
|
|
|
if (this._handle) this._handle.unref();
|
|
|
|
};
|
|
|
|
|
2017-02-03 17:23:10 +01:00
|
|
|
class Control extends EventEmitter {
|
|
|
|
constructor(channel) {
|
|
|
|
super();
|
|
|
|
this.channel = channel;
|
|
|
|
this.refs = 0;
|
|
|
|
}
|
|
|
|
ref() {
|
|
|
|
if (++this.refs === 1) {
|
|
|
|
this.channel.ref();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unref() {
|
|
|
|
if (--this.refs === 0) {
|
|
|
|
this.channel.unref();
|
|
|
|
this.emit('unref');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-22 16:52:05 +02:00
|
|
|
|
|
|
|
function setupChannel(target, channel) {
|
2016-10-27 20:36:49 +02:00
|
|
|
target.channel = channel;
|
|
|
|
|
|
|
|
// _channel can be deprecated in version 8
|
|
|
|
Object.defineProperty(target, '_channel', {
|
|
|
|
get() { return target.channel; },
|
|
|
|
set(val) { target.channel = val; },
|
|
|
|
enumerable: true
|
|
|
|
});
|
|
|
|
|
2015-05-22 16:52:05 +02:00
|
|
|
target._handleQueue = null;
|
2016-06-16 23:00:16 +02:00
|
|
|
target._pendingHandle = null;
|
2015-05-22 16:52:05 +02:00
|
|
|
|
2017-02-03 17:23:10 +01:00
|
|
|
const control = new Control(channel);
|
2015-08-31 00:49:34 +02:00
|
|
|
|
2015-05-22 16:52:05 +02:00
|
|
|
var decoder = new StringDecoder('utf8');
|
|
|
|
var jsonBuffer = '';
|
|
|
|
channel.buffering = false;
|
|
|
|
channel.onread = function(nread, pool, recvHandle) {
|
|
|
|
// TODO(bnoordhuis) Check that nread > 0.
|
|
|
|
if (pool) {
|
2016-12-31 17:09:52 +01:00
|
|
|
// Linebreak is used as a message end sign
|
|
|
|
var chunks = decoder.write(pool).split('\n');
|
|
|
|
var numCompleteChunks = chunks.length - 1;
|
|
|
|
// Last line does not have trailing linebreak
|
|
|
|
var incompleteChunk = chunks[numCompleteChunks];
|
|
|
|
if (numCompleteChunks === 0) {
|
|
|
|
jsonBuffer += incompleteChunk;
|
|
|
|
this.buffering = jsonBuffer.length !== 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
chunks[0] = jsonBuffer + chunks[0];
|
2015-05-22 16:52:05 +02:00
|
|
|
|
2016-12-31 17:09:52 +01:00
|
|
|
for (var i = 0; i < numCompleteChunks; i++) {
|
|
|
|
var message = JSON.parse(chunks[i]);
|
2015-05-22 16:52:05 +02:00
|
|
|
|
|
|
|
// There will be at most one NODE_HANDLE message in every chunk we
|
|
|
|
// read because SCM_RIGHTS messages don't get coalesced. Make sure
|
|
|
|
// that we deliver the handle with the right message however.
|
|
|
|
if (message && message.cmd === 'NODE_HANDLE')
|
|
|
|
handleMessage(target, message, recvHandle);
|
|
|
|
else
|
|
|
|
handleMessage(target, message, undefined);
|
|
|
|
}
|
2016-12-31 17:09:52 +01:00
|
|
|
jsonBuffer = incompleteChunk;
|
2015-05-22 16:52:05 +02:00
|
|
|
this.buffering = jsonBuffer.length !== 0;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
this.buffering = false;
|
|
|
|
target.disconnect();
|
|
|
|
channel.onread = nop;
|
|
|
|
channel.close();
|
2016-10-27 20:36:49 +02:00
|
|
|
target.channel = null;
|
2015-05-22 16:52:05 +02:00
|
|
|
maybeClose(target);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// object where socket lists will live
|
|
|
|
channel.sockets = { got: {}, send: {} };
|
|
|
|
|
|
|
|
// handlers will go through this
|
|
|
|
target.on('internalMessage', function(message, handle) {
|
|
|
|
// Once acknowledged - continue sending handles.
|
|
|
|
if (message.cmd === 'NODE_HANDLE_ACK') {
|
2016-06-16 23:00:16 +02:00
|
|
|
if (target._pendingHandle) {
|
|
|
|
target._pendingHandle.close();
|
|
|
|
target._pendingHandle = null;
|
|
|
|
}
|
|
|
|
|
2015-05-22 16:52:05 +02:00
|
|
|
assert(Array.isArray(target._handleQueue));
|
|
|
|
var queue = target._handleQueue;
|
|
|
|
target._handleQueue = null;
|
|
|
|
|
2016-12-31 17:09:52 +01:00
|
|
|
for (var i = 0; i < queue.length; i++) {
|
|
|
|
var args = queue[i];
|
2016-02-16 21:12:56 +01:00
|
|
|
target._send(args.message, args.handle, args.options, args.callback);
|
2016-12-31 17:09:52 +01:00
|
|
|
}
|
2015-05-22 16:52:05 +02:00
|
|
|
|
|
|
|
// Process a pending disconnect (if any).
|
2016-10-27 20:36:49 +02:00
|
|
|
if (!target.connected && target.channel && !target._handleQueue)
|
2015-05-22 16:52:05 +02:00
|
|
|
target._disconnect();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (message.cmd !== 'NODE_HANDLE') return;
|
|
|
|
|
|
|
|
// Acknowledge handle receival. Don't emit error events (for example if
|
|
|
|
// the other side has disconnected) because this call to send() is not
|
|
|
|
// initiated by the user and it shouldn't be fatal to be unable to ACK
|
|
|
|
// a message.
|
|
|
|
target._send({ cmd: 'NODE_HANDLE_ACK' }, null, true);
|
|
|
|
|
|
|
|
var obj = handleConversion[message.type];
|
|
|
|
|
|
|
|
// Update simultaneous accepts on Windows
|
|
|
|
if (process.platform === 'win32') {
|
|
|
|
handle._simultaneousAccepts = false;
|
|
|
|
net._setSimultaneousAccepts(handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert handle object
|
|
|
|
obj.got.call(this, message, handle, function(handle) {
|
|
|
|
handleMessage(target, message.msg, handle);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-02-16 21:12:56 +01:00
|
|
|
target.send = function(message, handle, options, callback) {
|
2015-08-31 00:49:34 +02:00
|
|
|
if (typeof handle === 'function') {
|
|
|
|
callback = handle;
|
|
|
|
handle = undefined;
|
2016-02-16 21:12:56 +01:00
|
|
|
options = undefined;
|
|
|
|
} else if (typeof options === 'function') {
|
|
|
|
callback = options;
|
|
|
|
options = undefined;
|
|
|
|
} else if (options !== undefined &&
|
|
|
|
(options === null || typeof options !== 'object')) {
|
|
|
|
throw new TypeError('"options" argument must be an object');
|
2015-08-31 00:49:34 +02:00
|
|
|
}
|
2016-02-16 21:12:56 +01:00
|
|
|
|
|
|
|
options = Object.assign({swallowErrors: false}, options);
|
|
|
|
|
2015-08-31 00:49:34 +02:00
|
|
|
if (this.connected) {
|
2016-02-16 21:12:56 +01:00
|
|
|
return this._send(message, handle, options, callback);
|
2015-08-31 00:49:34 +02:00
|
|
|
}
|
|
|
|
const ex = new Error('channel closed');
|
|
|
|
if (typeof callback === 'function') {
|
|
|
|
process.nextTick(callback, ex);
|
|
|
|
} else {
|
|
|
|
this.emit('error', ex); // FIXME(bnoordhuis) Defer to next tick.
|
|
|
|
}
|
2015-10-25 19:48:41 +01:00
|
|
|
return false;
|
2015-05-22 16:52:05 +02:00
|
|
|
};
|
|
|
|
|
2016-02-16 21:12:56 +01:00
|
|
|
target._send = function(message, handle, options, callback) {
|
2016-10-27 20:36:49 +02:00
|
|
|
assert(this.connected || this.channel);
|
2015-05-22 16:52:05 +02:00
|
|
|
|
|
|
|
if (message === undefined)
|
2015-10-14 23:10:25 +02:00
|
|
|
throw new TypeError('"message" argument cannot be undefined');
|
2015-05-22 16:52:05 +02:00
|
|
|
|
2016-02-16 21:12:56 +01:00
|
|
|
// Support legacy function signature
|
|
|
|
if (typeof options === 'boolean') {
|
|
|
|
options = {swallowErrors: options};
|
|
|
|
}
|
|
|
|
|
2015-05-22 16:52:05 +02:00
|
|
|
// package messages with a handle object
|
|
|
|
if (handle) {
|
|
|
|
// this message will be handled by an internalMessage event handler
|
|
|
|
message = {
|
|
|
|
cmd: 'NODE_HANDLE',
|
|
|
|
type: null,
|
|
|
|
msg: message
|
|
|
|
};
|
|
|
|
|
|
|
|
if (handle instanceof net.Socket) {
|
|
|
|
message.type = 'net.Socket';
|
|
|
|
} else if (handle instanceof net.Server) {
|
|
|
|
message.type = 'net.Server';
|
|
|
|
} else if (handle instanceof TCP || handle instanceof Pipe) {
|
|
|
|
message.type = 'net.Native';
|
|
|
|
} else if (handle instanceof dgram.Socket) {
|
|
|
|
message.type = 'dgram.Socket';
|
|
|
|
} else if (handle instanceof UDP) {
|
|
|
|
message.type = 'dgram.Native';
|
|
|
|
} else {
|
2015-10-14 23:10:25 +02:00
|
|
|
throw new TypeError('This handle type can\'t be sent');
|
2015-05-22 16:52:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Queue-up message and handle if we haven't received ACK yet.
|
|
|
|
if (this._handleQueue) {
|
2015-08-31 00:49:34 +02:00
|
|
|
this._handleQueue.push({
|
|
|
|
callback: callback,
|
|
|
|
handle: handle,
|
2016-02-16 21:12:56 +01:00
|
|
|
options: options,
|
2015-08-31 00:49:34 +02:00
|
|
|
message: message.msg,
|
|
|
|
});
|
2015-10-25 19:48:41 +01:00
|
|
|
return this._handleQueue.length === 1;
|
2015-05-22 16:52:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var obj = handleConversion[message.type];
|
|
|
|
|
|
|
|
// convert TCP object to native handle object
|
2016-02-16 21:12:56 +01:00
|
|
|
handle = handleConversion[message.type].send.call(target,
|
|
|
|
message,
|
|
|
|
handle,
|
|
|
|
options);
|
2015-05-22 16:52:05 +02:00
|
|
|
|
|
|
|
// If handle was sent twice, or it is impossible to get native handle
|
|
|
|
// out of it - just send a text without the handle.
|
|
|
|
if (!handle)
|
|
|
|
message = message.msg;
|
|
|
|
|
|
|
|
// Update simultaneous accepts on Windows
|
|
|
|
if (obj.simultaneousAccepts) {
|
|
|
|
net._setSimultaneousAccepts(handle);
|
|
|
|
}
|
|
|
|
} else if (this._handleQueue &&
|
|
|
|
!(message && message.cmd === 'NODE_HANDLE_ACK')) {
|
|
|
|
// Queue request anyway to avoid out-of-order messages.
|
2015-08-31 00:49:34 +02:00
|
|
|
this._handleQueue.push({
|
|
|
|
callback: callback,
|
|
|
|
handle: null,
|
2016-02-16 21:12:56 +01:00
|
|
|
options: options,
|
2015-08-31 00:49:34 +02:00
|
|
|
message: message,
|
|
|
|
});
|
2015-10-25 19:48:41 +01:00
|
|
|
return this._handleQueue.length === 1;
|
2015-05-22 16:52:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var req = new WriteWrap();
|
2015-08-31 00:49:34 +02:00
|
|
|
req.async = false;
|
|
|
|
|
2015-05-22 16:52:05 +02:00
|
|
|
var string = JSON.stringify(message) + '\n';
|
|
|
|
var err = channel.writeUtf8String(req, string, handle);
|
|
|
|
|
2015-08-31 00:49:34 +02:00
|
|
|
if (err === 0) {
|
2016-06-16 23:00:16 +02:00
|
|
|
if (handle) {
|
|
|
|
if (!this._handleQueue)
|
|
|
|
this._handleQueue = [];
|
|
|
|
if (obj && obj.postSend)
|
|
|
|
obj.postSend(handle, options, target);
|
|
|
|
}
|
|
|
|
|
2015-08-31 00:49:34 +02:00
|
|
|
req.oncomplete = function() {
|
|
|
|
if (this.async === true)
|
|
|
|
control.unref();
|
|
|
|
if (typeof callback === 'function')
|
|
|
|
callback(null);
|
|
|
|
};
|
|
|
|
if (req.async === true) {
|
|
|
|
control.ref();
|
|
|
|
} else {
|
|
|
|
process.nextTick(function() { req.oncomplete(); });
|
|
|
|
}
|
2016-01-19 02:59:18 +01:00
|
|
|
} else {
|
|
|
|
// Cleanup handle on error
|
|
|
|
if (obj && obj.postSend)
|
2016-02-16 21:12:56 +01:00
|
|
|
obj.postSend(handle, options);
|
2016-01-19 02:59:18 +01:00
|
|
|
|
2016-02-16 21:12:56 +01:00
|
|
|
if (!options.swallowErrors) {
|
2016-01-19 02:59:18 +01:00
|
|
|
const ex = errnoException(err, 'write');
|
|
|
|
if (typeof callback === 'function') {
|
|
|
|
process.nextTick(callback, ex);
|
|
|
|
} else {
|
|
|
|
this.emit('error', ex); // FIXME(bnoordhuis) Defer to next tick.
|
|
|
|
}
|
2015-08-31 00:49:34 +02:00
|
|
|
}
|
2015-05-22 16:52:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If the master is > 2 read() calls behind, please stop sending. */
|
|
|
|
return channel.writeQueueSize < (65536 * 2);
|
|
|
|
};
|
|
|
|
|
|
|
|
// connected will be set to false immediately when a disconnect() is
|
|
|
|
// requested, even though the channel might still be alive internally to
|
|
|
|
// process queued messages. The three states are distinguished as follows:
|
2016-10-27 20:36:49 +02:00
|
|
|
// - disconnect() never requested: channel is not null and connected
|
2015-05-22 16:52:05 +02:00
|
|
|
// is true
|
2016-10-27 20:36:49 +02:00
|
|
|
// - disconnect() requested, messages in the queue: channel is not null
|
2015-05-22 16:52:05 +02:00
|
|
|
// and connected is false
|
2016-10-27 20:36:49 +02:00
|
|
|
// - disconnect() requested, channel actually disconnected: channel is
|
2015-05-22 16:52:05 +02:00
|
|
|
// null and connected is false
|
|
|
|
target.connected = true;
|
|
|
|
|
|
|
|
target.disconnect = function() {
|
|
|
|
if (!this.connected) {
|
|
|
|
this.emit('error', new Error('IPC channel is already disconnected'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do not allow any new messages to be written.
|
|
|
|
this.connected = false;
|
|
|
|
|
|
|
|
// If there are no queued messages, disconnect immediately. Otherwise,
|
|
|
|
// postpone the disconnect so that it happens internally after the
|
|
|
|
// queue is flushed.
|
|
|
|
if (!this._handleQueue)
|
|
|
|
this._disconnect();
|
|
|
|
};
|
|
|
|
|
|
|
|
target._disconnect = function() {
|
2016-10-27 20:36:49 +02:00
|
|
|
assert(this.channel);
|
2015-05-22 16:52:05 +02:00
|
|
|
|
|
|
|
// This marks the fact that the channel is actually disconnected.
|
2016-10-27 20:36:49 +02:00
|
|
|
this.channel = null;
|
2015-05-22 16:52:05 +02:00
|
|
|
|
2016-06-16 23:00:16 +02:00
|
|
|
if (this._pendingHandle) {
|
|
|
|
this._pendingHandle.close();
|
|
|
|
this._pendingHandle = null;
|
|
|
|
}
|
|
|
|
|
2015-05-22 16:52:05 +02:00
|
|
|
var fired = false;
|
|
|
|
function finish() {
|
|
|
|
if (fired) return;
|
|
|
|
fired = true;
|
|
|
|
|
|
|
|
channel.close();
|
|
|
|
target.emit('disconnect');
|
|
|
|
}
|
|
|
|
|
|
|
|
// If a message is being read, then wait for it to complete.
|
|
|
|
if (channel.buffering) {
|
|
|
|
this.once('message', finish);
|
|
|
|
this.once('internalMessage', finish);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
process.nextTick(finish);
|
|
|
|
};
|
|
|
|
|
|
|
|
channel.readStart();
|
2015-08-31 00:49:34 +02:00
|
|
|
return control;
|
2015-05-22 16:52:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const INTERNAL_PREFIX = 'NODE_';
|
|
|
|
function handleMessage(target, message, handle) {
|
2016-10-27 20:36:49 +02:00
|
|
|
if (!target.channel)
|
2015-12-24 22:00:29 +01:00
|
|
|
return;
|
|
|
|
|
2015-05-22 16:52:05 +02:00
|
|
|
var eventName = 'message';
|
|
|
|
if (message !== null &&
|
|
|
|
typeof message === 'object' &&
|
|
|
|
typeof message.cmd === 'string' &&
|
|
|
|
message.cmd.length > INTERNAL_PREFIX.length &&
|
|
|
|
message.cmd.slice(0, INTERNAL_PREFIX.length) === INTERNAL_PREFIX) {
|
|
|
|
eventName = 'internalMessage';
|
|
|
|
}
|
2016-05-21 01:10:48 +02:00
|
|
|
process.nextTick(() => {
|
|
|
|
target.emit(eventName, message, handle);
|
|
|
|
});
|
2015-05-22 16:52:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function nop() { }
|
|
|
|
|
|
|
|
function _validateStdio(stdio, sync) {
|
2016-01-12 22:04:50 +01:00
|
|
|
var ipc;
|
|
|
|
var ipcFd;
|
2015-05-22 16:52:05 +02:00
|
|
|
|
|
|
|
// Replace shortcut with an array
|
|
|
|
if (typeof stdio === 'string') {
|
|
|
|
switch (stdio) {
|
|
|
|
case 'ignore': stdio = ['ignore', 'ignore', 'ignore']; break;
|
|
|
|
case 'pipe': stdio = ['pipe', 'pipe', 'pipe']; break;
|
|
|
|
case 'inherit': stdio = [0, 1, 2]; break;
|
|
|
|
default: throw new TypeError('Incorrect value of stdio option: ' + stdio);
|
|
|
|
}
|
|
|
|
} else if (!Array.isArray(stdio)) {
|
|
|
|
throw new TypeError('Incorrect value of stdio option: ' +
|
|
|
|
util.inspect(stdio));
|
|
|
|
}
|
|
|
|
|
|
|
|
// At least 3 stdio will be created
|
|
|
|
// Don't concat() a new Array() because it would be sparse, and
|
|
|
|
// stdio.reduce() would skip the sparse elements of stdio.
|
|
|
|
// See http://stackoverflow.com/a/5501711/3561
|
|
|
|
while (stdio.length < 3) stdio.push(undefined);
|
|
|
|
|
|
|
|
// Translate stdio into C++-readable form
|
|
|
|
// (i.e. PipeWraps or fds)
|
|
|
|
stdio = stdio.reduce(function(acc, stdio, i) {
|
|
|
|
function cleanup() {
|
2017-04-11 21:53:04 +02:00
|
|
|
for (var i = 0; i < acc.length; i++) {
|
|
|
|
if ((acc[i].type === 'pipe' || acc[i].type === 'ipc') && acc[i].handle)
|
|
|
|
acc[i].handle.close();
|
|
|
|
}
|
2015-05-22 16:52:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Defaults
|
2017-02-14 04:39:24 +01:00
|
|
|
if (stdio == null) {
|
2015-05-22 16:52:05 +02:00
|
|
|
stdio = i < 3 ? 'pipe' : 'ignore';
|
|
|
|
}
|
|
|
|
|
2017-02-14 04:39:24 +01:00
|
|
|
if (stdio === 'ignore') {
|
2015-05-22 16:52:05 +02:00
|
|
|
acc.push({type: 'ignore'});
|
|
|
|
} else if (stdio === 'pipe' || typeof stdio === 'number' && stdio < 0) {
|
|
|
|
var a = {
|
|
|
|
type: 'pipe',
|
|
|
|
readable: i === 0,
|
|
|
|
writable: i !== 0
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!sync)
|
|
|
|
a.handle = new Pipe();
|
|
|
|
|
|
|
|
acc.push(a);
|
|
|
|
} else if (stdio === 'ipc') {
|
|
|
|
if (sync || ipc !== undefined) {
|
|
|
|
// Cleanup previously created pipes
|
|
|
|
cleanup();
|
|
|
|
if (!sync)
|
|
|
|
throw new Error('Child process can have only one IPC pipe');
|
|
|
|
else
|
|
|
|
throw new Error('You cannot use IPC with synchronous forks');
|
|
|
|
}
|
|
|
|
|
|
|
|
ipc = new Pipe(true);
|
|
|
|
ipcFd = i;
|
|
|
|
|
|
|
|
acc.push({
|
|
|
|
type: 'pipe',
|
|
|
|
handle: ipc,
|
|
|
|
ipc: true
|
|
|
|
});
|
|
|
|
} else if (stdio === 'inherit') {
|
|
|
|
acc.push({
|
|
|
|
type: 'inherit',
|
|
|
|
fd: i
|
|
|
|
});
|
|
|
|
} else if (typeof stdio === 'number' || typeof stdio.fd === 'number') {
|
|
|
|
acc.push({
|
|
|
|
type: 'fd',
|
2015-09-07 19:25:25 +02:00
|
|
|
fd: typeof stdio === 'number' ? stdio : stdio.fd
|
2015-05-22 16:52:05 +02:00
|
|
|
});
|
|
|
|
} else if (getHandleWrapType(stdio) || getHandleWrapType(stdio.handle) ||
|
|
|
|
getHandleWrapType(stdio._handle)) {
|
|
|
|
var handle = getHandleWrapType(stdio) ?
|
|
|
|
stdio :
|
|
|
|
getHandleWrapType(stdio.handle) ? stdio.handle : stdio._handle;
|
|
|
|
|
|
|
|
acc.push({
|
|
|
|
type: 'wrap',
|
|
|
|
wrapType: getHandleWrapType(handle),
|
|
|
|
handle: handle
|
|
|
|
});
|
2017-01-06 02:12:26 +01:00
|
|
|
} else if (isUint8Array(stdio) || typeof stdio === 'string') {
|
2015-05-22 16:52:05 +02:00
|
|
|
if (!sync) {
|
|
|
|
cleanup();
|
2017-01-06 02:12:26 +01:00
|
|
|
throw new TypeError('Asynchronous forks do not support ' +
|
|
|
|
'Buffer, Uint8Array or string input: ' +
|
2015-05-22 16:52:05 +02:00
|
|
|
util.inspect(stdio));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Cleanup
|
|
|
|
cleanup();
|
|
|
|
throw new TypeError('Incorrect value for stdio stream: ' +
|
|
|
|
util.inspect(stdio));
|
|
|
|
}
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, []);
|
|
|
|
|
2017-04-11 21:53:04 +02:00
|
|
|
return { stdio, ipc, ipcFd };
|
2015-05-22 16:52:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function getSocketList(type, slave, key) {
|
2016-10-27 20:36:49 +02:00
|
|
|
var sockets = slave.channel.sockets[type];
|
2015-05-22 16:52:05 +02:00
|
|
|
var socketList = sockets[key];
|
|
|
|
if (!socketList) {
|
|
|
|
var Construct = type === 'send' ? SocketListSend : SocketListReceive;
|
|
|
|
socketList = sockets[key] = new Construct(slave, key);
|
|
|
|
}
|
|
|
|
return socketList;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function maybeClose(subprocess) {
|
|
|
|
subprocess._closesGot++;
|
|
|
|
|
2017-02-14 04:39:24 +01:00
|
|
|
if (subprocess._closesGot === subprocess._closesNeeded) {
|
2015-05-22 16:52:05 +02:00
|
|
|
subprocess.emit('close', subprocess.exitCode, subprocess.signalCode);
|
|
|
|
}
|
|
|
|
}
|
2017-02-15 23:29:00 +01:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
ChildProcess,
|
|
|
|
setupChannel,
|
|
|
|
_validateStdio,
|
|
|
|
getSocketList
|
|
|
|
};
|