1 // Copyright Joyent, Inc. and other Node contributors.
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
22 var StringDecoder = require('string_decoder').StringDecoder;
23 var EventEmitter = require('events').EventEmitter;
24 var net = require('net');
25 var dgram = require('dgram');
26 var assert = require('assert');
27 var util = require('util');
29 var Process = process.binding('process_wrap').Process;
30 var uv = process.binding('uv');
32 var spawn_sync; // Lazy-loaded process.binding('spawn_sync')
33 var constants; // Lazy-loaded process.binding('constants')
35 var errnoException = util._errnoException;
38 function handleWrapGetter(name, callback) {
41 Object.defineProperty(handleWraps, name, {
43 if (!util.isUndefined(cons)) return cons;
44 return cons = callback();
49 handleWrapGetter('Pipe', function() {
50 return process.binding('pipe_wrap').Pipe;
53 handleWrapGetter('TTY', function() {
54 return process.binding('tty_wrap').TTY;
57 handleWrapGetter('TCP', function() {
58 return process.binding('tcp_wrap').TCP;
61 handleWrapGetter('UDP', function() {
62 return process.binding('udp_wrap').UDP;
65 // constructors for lazy loading
66 function createPipe(ipc) {
67 return new handleWraps.Pipe(ipc);
70 function createSocket(pipe, readable) {
71 var s = new net.Socket({ handle: pipe });
85 // this object contain function to convert TCP objects to native handle objects
87 var handleConversion = {
89 simultaneousAccepts: true,
91 send: function(message, handle) {
95 got: function(message, handle, emit) {
101 simultaneousAccepts: true,
103 send: function(message, server) {
104 return server._handle;
107 got: function(message, handle, emit) {
108 var server = new net.Server();
109 server.listen(handle, function() {
116 send: function(message, socket) {
120 // if the socket was created by net.Server
122 // the slave should keep track of the socket
123 message.key = socket.server._connectionKey;
125 var firstTime = !this._channel.sockets.send[message.key];
126 var socketList = getSocketList('send', this, message.key);
128 // the server should no longer expose a .connection property
129 // and when asked to close it should query the socket status from
131 if (firstTime) socket.server._setupSlave(socketList);
133 // Act like socket is detached
134 socket.server._connections--;
137 // remove handle from socket object, it will be closed when the socket
139 var handle = socket._handle;
140 handle.onread = function() {};
141 socket._handle = null;
146 postSend: function(handle) {
147 // Close the Socket handle after sending it
152 got: function(message, handle, emit) {
153 var socket = new net.Socket({handle: handle});
154 socket.readable = socket.writable = true;
156 // if the socket was created by net.Server we will track the socket
159 // add socket to connections list
160 var socketList = getSocketList('got', this, message.key);
171 simultaneousAccepts: false,
173 send: function(message, handle) {
177 got: function(message, handle, emit) {
183 simultaneousAccepts: false,
185 send: function(message, socket) {
186 message.dgramType = socket.type;
188 return socket._handle;
191 got: function(message, handle, emit) {
192 var socket = new dgram.Socket(message.dgramType);
194 socket.bind(handle, function() {
201 // This object keep track of the socket there are sended
202 function SocketListSend(slave, key) {
203 EventEmitter.call(this);
208 util.inherits(SocketListSend, EventEmitter);
210 SocketListSend.prototype._request = function(msg, cmd, callback) {
213 if (!this.slave.connected) return onclose();
214 this.slave.send(msg);
217 self.slave.removeListener('internalMessage', onreply);
218 callback(new Error('Slave closed before reply'));
221 function onreply(msg) {
222 if (!(msg.cmd === cmd && msg.key === self.key)) return;
223 self.slave.removeListener('disconnect', onclose);
224 self.slave.removeListener('internalMessage', onreply);
229 this.slave.once('disconnect', onclose);
230 this.slave.on('internalMessage', onreply);
233 SocketListSend.prototype.close = function close(callback) {
235 cmd: 'NODE_SOCKET_NOTIFY_CLOSE',
237 }, 'NODE_SOCKET_ALL_CLOSED', callback);
240 SocketListSend.prototype.getConnections = function getConnections(callback) {
242 cmd: 'NODE_SOCKET_GET_COUNT',
244 }, 'NODE_SOCKET_COUNT', function(err, msg) {
245 if (err) return callback(err);
246 callback(null, msg.count);
250 // This object keep track of the socket there are received
251 function SocketListReceive(slave, key) {
252 EventEmitter.call(this);
256 this.connections = 0;
261 if (!self.slave.connected) return;
264 cmd: 'NODE_SOCKET_ALL_CLOSED',
269 this.slave.on('internalMessage', function(msg) {
270 if (msg.key !== self.key) return;
272 if (msg.cmd === 'NODE_SOCKET_NOTIFY_CLOSE') {
274 if (self.connections === 0) return onempty();
276 // Wait for sockets to get closed
277 self.once('empty', onempty);
278 } else if (msg.cmd === 'NODE_SOCKET_GET_COUNT') {
279 if (!self.slave.connected) return;
281 cmd: 'NODE_SOCKET_COUNT',
283 count: self.connections
288 util.inherits(SocketListReceive, EventEmitter);
290 SocketListReceive.prototype.add = function(obj) {
295 // Notify previous owner of socket about its state change
296 obj.socket.once('close', function() {
299 if (self.connections === 0) self.emit('empty');
303 function getSocketList(type, slave, key) {
304 var sockets = slave._channel.sockets[type];
305 var socketList = sockets[key];
307 var Construct = type === 'send' ? SocketListSend : SocketListReceive;
308 socketList = sockets[key] = new Construct(slave, key);
313 var INTERNAL_PREFIX = 'NODE_';
314 function handleMessage(target, message, handle) {
315 var eventName = 'message';
316 if (!util.isNull(message) &&
317 util.isObject(message) &&
318 util.isString(message.cmd) &&
319 message.cmd.length > INTERNAL_PREFIX.length &&
320 message.cmd.slice(0, INTERNAL_PREFIX.length) === INTERNAL_PREFIX) {
321 eventName = 'internalMessage';
323 target.emit(eventName, message, handle);
326 function setupChannel(target, channel) {
327 target._channel = channel;
328 target._handleQueue = null;
330 var decoder = new StringDecoder('utf8');
332 channel.buffering = false;
333 channel.onread = function(nread, pool, recvHandle) {
334 // TODO(bnoordhuis) Check that nread > 0.
336 jsonBuffer += decoder.write(pool);
340 //Linebreak is used as a message end sign
341 while ((i = jsonBuffer.indexOf('\n', start)) >= 0) {
342 var json = jsonBuffer.slice(start, i);
343 var message = JSON.parse(json);
345 // There will be at most one NODE_HANDLE message in every chunk we
346 // read because SCM_RIGHTS messages don't get coalesced. Make sure
347 // that we deliver the handle with the right message however.
348 if (message && message.cmd === 'NODE_HANDLE')
349 handleMessage(target, message, recvHandle);
351 handleMessage(target, message, undefined);
355 jsonBuffer = jsonBuffer.slice(start);
356 this.buffering = jsonBuffer.length !== 0;
359 this.buffering = false;
361 channel.onread = nop;
367 // object where socket lists will live
368 channel.sockets = { got: {}, send: {} };
370 // handlers will go through this
371 target.on('internalMessage', function(message, handle) {
372 // Once acknowledged - continue sending handles.
373 if (message.cmd === 'NODE_HANDLE_ACK') {
374 assert(util.isArray(target._handleQueue));
375 var queue = target._handleQueue;
376 target._handleQueue = null;
378 queue.forEach(function(args) {
379 target._send(args.message, args.handle, false);
382 // Process a pending disconnect (if any).
383 if (!target.connected && target._channel && !target._handleQueue)
384 target._disconnect();
389 if (message.cmd !== 'NODE_HANDLE') return;
391 // Acknowledge handle receival. Don't emit error events (for example if
392 // the other side has disconnected) because this call to send() is not
393 // initiated by the user and it shouldn't be fatal to be unable to ACK
395 target._send({ cmd: 'NODE_HANDLE_ACK' }, null, true);
397 var obj = handleConversion[message.type];
399 // Update simultaneous accepts on Windows
400 if (process.platform === 'win32') {
401 handle._simultaneousAccepts = false;
402 net._setSimultaneousAccepts(handle);
405 // Convert handle object
406 obj.got.call(this, message, handle, function(handle) {
407 handleMessage(target, message.msg, handle);
411 target.send = function(message, handle) {
413 this.emit('error', new Error('channel closed'));
415 this._send(message, handle, false);
418 target._send = function(message, handle, swallowErrors) {
419 assert(this.connected || this._channel);
421 if (util.isUndefined(message))
422 throw new TypeError('message cannot be undefined');
424 // package messages with a handle object
426 // this message will be handled by an internalMessage event handler
433 if (handle instanceof net.Socket) {
434 message.type = 'net.Socket';
435 } else if (handle instanceof net.Server) {
436 message.type = 'net.Server';
437 } else if (handle instanceof process.binding('tcp_wrap').TCP ||
438 handle instanceof process.binding('pipe_wrap').Pipe) {
439 message.type = 'net.Native';
440 } else if (handle instanceof dgram.Socket) {
441 message.type = 'dgram.Socket';
442 } else if (handle instanceof process.binding('udp_wrap').UDP) {
443 message.type = 'dgram.Native';
445 throw new TypeError("This handle type can't be sent");
448 // Queue-up message and handle if we haven't received ACK yet.
449 if (this._handleQueue) {
450 this._handleQueue.push({ message: message.msg, handle: handle });
454 var obj = handleConversion[message.type];
456 // convert TCP object to native handle object
458 handleConversion[message.type].send.call(target, message, handle);
460 // If handle was sent twice, or it is impossible to get native handle
461 // out of it - just send a text without the handle.
463 message = message.msg;
465 // Update simultaneous accepts on Windows
466 if (obj.simultaneousAccepts) {
467 net._setSimultaneousAccepts(handle);
469 } else if (this._handleQueue &&
470 !(message && message.cmd === 'NODE_HANDLE_ACK')) {
471 // Queue request anyway to avoid out-of-order messages.
472 this._handleQueue.push({ message: message, handle: null });
476 var req = { oncomplete: nop };
477 var string = JSON.stringify(message) + '\n';
478 var err = channel.writeUtf8String(req, string, handle);
482 this.emit('error', errnoException(err, 'write'));
483 } else if (handle && !this._handleQueue) {
484 this._handleQueue = [];
487 if (obj && obj.postSend) {
488 req.oncomplete = obj.postSend.bind(null, handle);
491 /* If the master is > 2 read() calls behind, please stop sending. */
492 return channel.writeQueueSize < (65536 * 2);
495 // connected will be set to false immediately when a disconnect() is
496 // requested, even though the channel might still be alive internally to
497 // process queued messages. The three states are distinguished as follows:
498 // - disconnect() never requested: _channel is not null and connected
500 // - disconnect() requested, messages in the queue: _channel is not null
501 // and connected is false
502 // - disconnect() requested, channel actually disconnected: _channel is
503 // null and connected is false
504 target.connected = true;
506 target.disconnect = function() {
507 if (!this.connected) {
508 this.emit('error', new Error('IPC channel is already disconnected'));
512 // Do not allow any new messages to be written.
513 this.connected = false;
515 // If there are no queued messages, disconnect immediately. Otherwise,
516 // postpone the disconnect so that it happens internally after the
518 if (!this._handleQueue)
522 target._disconnect = function() {
523 assert(this._channel);
525 // This marks the fact that the channel is actually disconnected.
526 this._channel = null;
534 target.emit('disconnect');
537 // If a message is being read, then wait for it to complete.
538 if (channel.buffering) {
539 this.once('message', finish);
540 this.once('internalMessage', finish);
545 process.nextTick(finish);
554 exports.fork = function(modulePath /*, args, options*/) {
556 // Get options and args arguments.
557 var options, args, execArgv;
558 if (util.isArray(arguments[1])) {
560 options = util._extend({}, arguments[2]);
563 options = util._extend({}, arguments[1]);
566 // Prepare arguments for fork:
567 execArgv = options.execArgv || process.execArgv;
568 args = execArgv.concat([modulePath], args);
570 // Leave stdin open for the IPC channel. stdout and stderr should be the
571 // same as the parent's if silent isn't set.
572 options.stdio = options.silent ? ['pipe', 'pipe', 'pipe', 'ipc'] :
575 options.execPath = options.execPath || process.execPath;
577 return spawn(options.execPath, args, options);
581 exports._forkChild = function(fd) {
582 // set process.send()
583 var p = createPipe(true);
586 setupChannel(process, p);
589 process.on('newListener', function(name) {
590 if (name !== 'message' && name !== 'disconnect') return;
591 if (++refs === 1) p.ref();
593 process.on('removeListener', function(name) {
594 if (name !== 'message' && name !== 'disconnect') return;
595 if (--refs === 0) p.unref();
600 function normalizeExecArgs(command /*, options, callback */) {
601 var file, args, options, callback;
603 if (util.isFunction(arguments[1])) {
605 callback = arguments[1];
607 options = arguments[1];
608 callback = arguments[2];
611 if (process.platform === 'win32') {
612 file = process.env.comspec || 'cmd.exe';
613 args = ['/s', '/c', '"' + command + '"'];
614 // Make a shallow copy before patching so we don't clobber the user's
616 options = util._extend({}, options);
617 options.windowsVerbatimArguments = true;
620 args = ['-c', command];
623 if (options && options.shell)
624 file = options.shell;
636 exports.exec = function(command /*, options, callback */) {
637 var opts = normalizeExecArgs.apply(null, arguments);
638 return exports.execFile(opts.file,
645 exports.execFile = function(file /* args, options, callback */) {
650 maxBuffer: 200 * 1024,
651 killSignal: 'SIGTERM',
656 // Parse the parameters.
658 if (util.isFunction(arguments[arguments.length - 1])) {
659 callback = arguments[arguments.length - 1];
662 if (util.isArray(arguments[1])) {
664 options = util._extend(options, arguments[2]);
667 options = util._extend(options, arguments[1]);
670 var child = spawn(file, args, {
675 windowsVerbatimArguments: !!options.windowsVerbatimArguments
681 if (options.encoding !== 'buffer' && Buffer.isEncoding(options.encoding)) {
682 encoding = options.encoding;
698 function exithandler(code, signal) {
703 clearTimeout(timeoutId);
707 if (!callback) return;
713 stdout = Buffer.concat(_stdout);
714 stderr = Buffer.concat(_stderr);
721 // Will be handled later
722 } else if (code === 0 && signal === null) {
723 callback(null, stdout, stderr);
728 if (args.length !== 0)
729 cmd += ' ' + args.join(' ');
732 ex = new Error('Command failed: ' + cmd + '\n' + stderr);
733 ex.killed = child.killed || killed;
734 ex.code = code < 0 ? uv.errname(code) : code;
739 callback(ex, stdout, stderr);
742 function errorhandler(e) {
744 child.stdout.destroy();
745 child.stderr.destroy();
750 child.stdout.destroy();
751 child.stderr.destroy();
755 child.kill(options.killSignal);
762 if (options.timeout > 0) {
763 timeoutId = setTimeout(function() {
769 child.stdout.addListener('data', function(chunk) {
770 stdoutLen += chunk.length;
772 if (stdoutLen > options.maxBuffer) {
773 ex = new Error('stdout maxBuffer exceeded.');
783 child.stderr.addListener('data', function(chunk) {
784 stderrLen += chunk.length;
786 if (stderrLen > options.maxBuffer) {
787 ex = new Error('stderr maxBuffer exceeded.');
798 child.stderr.setEncoding(encoding);
799 child.stdout.setEncoding(encoding);
802 child.addListener('close', exithandler);
803 child.addListener('error', errorhandler);
808 var _deprecatedCustomFds = util.deprecate(function(options) {
809 options.stdio = options.customFds.map(function(fd) {
810 return fd === -1 ? 'pipe' : fd;
812 }, 'child_process: customFds option is deprecated, use stdio instead.');
814 function _convertCustomFds(options) {
815 if (options && options.customFds && !options.stdio) {
816 _deprecatedCustomFds(options);
821 function _validateStdio(stdio, sync) {
825 // Replace shortcut with an array
826 if (util.isString(stdio)) {
828 case 'ignore': stdio = ['ignore', 'ignore', 'ignore']; break;
829 case 'pipe': stdio = ['pipe', 'pipe', 'pipe']; break;
830 case 'inherit': stdio = [0, 1, 2]; break;
831 default: throw new TypeError('Incorrect value of stdio option: ' + stdio);
833 } else if (!util.isArray(stdio)) {
834 throw new TypeError('Incorrect value of stdio option: ' +
835 util.inspect(stdio));
838 // At least 3 stdio will be created
839 // Don't concat() a new Array() because it would be sparse, and
840 // stdio.reduce() would skip the sparse elements of stdio.
841 // See http://stackoverflow.com/a/5501711/3561
842 while (stdio.length < 3) stdio.push(undefined);
844 // Translate stdio into C++-readable form
845 // (i.e. PipeWraps or fds)
846 stdio = stdio.reduce(function(acc, stdio, i) {
848 acc.filter(function(stdio) {
849 return stdio.type === 'pipe' || stdio.type === 'ipc';
850 }).forEach(function(stdio) {
852 stdio.handle.close();
857 if (util.isNullOrUndefined(stdio)) {
858 stdio = i < 3 ? 'pipe' : 'ignore';
861 if (stdio === null || stdio === 'ignore') {
862 acc.push({type: 'ignore'});
863 } else if (stdio === 'pipe' || util.isNumber(stdio) && stdio < 0) {
871 a.handle = createPipe();
874 } else if (stdio === 'ipc') {
875 if (sync || !util.isUndefined(ipc)) {
876 // Cleanup previously created pipes
879 throw Error('Child process can have only one IPC pipe');
881 throw Error('You cannot use IPC with synchronous forks');
884 ipc = createPipe(true);
892 } else if (stdio === 'inherit') {
897 } else if (util.isNumber(stdio) || util.isNumber(stdio.fd)) {
900 fd: stdio.fd || stdio
902 } else if (getHandleWrapType(stdio) || getHandleWrapType(stdio.handle) ||
903 getHandleWrapType(stdio._handle)) {
904 var handle = getHandleWrapType(stdio) ?
906 getHandleWrapType(stdio.handle) ? stdio.handle : stdio._handle;
910 wrapType: getHandleWrapType(handle),
913 } else if (util.isBuffer(stdio) || util.isString(stdio)) {
916 throw new TypeError('Asynchronous forks do not support Buffer input: ' +
917 util.inspect(stdio));
922 throw new TypeError('Incorrect value for stdio stream: ' +
923 util.inspect(stdio));
929 return {stdio: stdio, ipc: ipc, ipcFd: ipcFd};
933 function normalizeSpawnArguments(file /*, args, options*/) {
936 if (Array.isArray(arguments[1])) {
937 args = arguments[1].slice(0);
938 options = arguments[2];
939 } else if (arguments[1] !== undefined && !util.isObject(arguments[1])) {
940 throw new TypeError('Incorrect value of args option');
943 options = arguments[1];
946 if (options === undefined)
948 else if (!util.isObject(options))
949 throw new TypeError('options argument must be an object');
953 var env = options.env || process.env;
956 for (var key in env) {
957 envPairs.push(key + '=' + env[key]);
960 _convertCustomFds(options);
971 var spawn = exports.spawn = function(/*file, args, options*/) {
972 var opts = normalizeSpawnArguments.apply(null, arguments);
973 var options = opts.options;
974 var child = new ChildProcess();
980 windowsVerbatimArguments: !!options.windowsVerbatimArguments,
981 detached: !!options.detached,
982 envPairs: opts.envPairs,
983 stdio: options.stdio,
992 function maybeClose(subprocess) {
993 subprocess._closesGot++;
995 if (subprocess._closesGot == subprocess._closesNeeded) {
996 subprocess.emit('close', subprocess.exitCode, subprocess.signalCode);
1001 function ChildProcess() {
1002 EventEmitter.call(this);
1004 // Initialize TCPWrap and PipeWrap
1005 process.binding('tcp_wrap');
1006 process.binding('pipe_wrap');
1010 this._closesNeeded = 1;
1011 this._closesGot = 0;
1012 this.connected = false;
1014 this.signalCode = null;
1015 this.exitCode = null;
1016 this.killed = false;
1017 this.spawnfile = null;
1019 this._handle = new Process();
1020 this._handle.owner = this;
1022 this._handle.onexit = function(exitCode, signalCode) {
1024 // follow 0.4.x behaviour:
1026 // - normally terminated processes don't touch this.signalCode
1027 // - signaled processes don't touch this.exitCode
1031 // - spawn failures are reported with exitCode < 0
1033 var syscall = self.spawnfile ? 'spawn ' + self.spawnfile : 'spawn';
1034 var err = (exitCode < 0) ? errnoException(exitCode, syscall) : null;
1037 self.signalCode = signalCode;
1039 self.exitCode = exitCode;
1043 self.stdin.destroy();
1046 self._handle.close();
1047 self._handle = null;
1051 err.path = self.spawnfile;
1053 self.emit('error', err);
1055 self.emit('exit', self.exitCode, self.signalCode);
1058 // if any of the stdio streams have not been touched,
1059 // then pull all the data through so that it can get the
1060 // eof and emit a 'close' event.
1061 // Do it on nextTick so that the user has one last chance
1062 // to consume the output, if for example they only want to
1063 // start reading the data once the process exits.
1064 process.nextTick(function() {
1071 util.inherits(ChildProcess, EventEmitter);
1074 function flushStdio(subprocess) {
1075 if (subprocess.stdio == null) return;
1076 subprocess.stdio.forEach(function(stream, fd, stdio) {
1077 if (!stream || !stream.readable || stream._consuming ||
1078 stream._readableState.flowing)
1086 function getHandleWrapType(stream) {
1087 if (stream instanceof handleWraps.Pipe) return 'pipe';
1088 if (stream instanceof handleWraps.TTY) return 'tty';
1089 if (stream instanceof handleWraps.TCP) return 'tcp';
1090 if (stream instanceof handleWraps.UDP) return 'udp';
1096 ChildProcess.prototype.spawn = function(options) {
1100 // If no `stdio` option was given - use default
1101 stdio = options.stdio || 'pipe';
1103 stdio = _validateStdio(stdio, false);
1106 ipcFd = stdio.ipcFd;
1107 stdio = options.stdio = stdio.stdio;
1109 if (!util.isUndefined(ipc)) {
1110 // Let child process know about opened IPC channel
1111 options.envPairs = options.envPairs || [];
1112 options.envPairs.push('NODE_CHANNEL_FD=' + ipcFd);
1115 this.spawnfile = options.file;
1117 var err = this._handle.spawn(options);
1119 // Run-time errors should emit an error, not throw an exception.
1120 if (err === uv.UV_EAGAIN ||
1121 err === uv.UV_EMFILE ||
1122 err === uv.UV_ENFILE ||
1123 err === uv.UV_ENOENT) {
1124 process.nextTick(function() {
1125 self._handle.onexit(err);
1127 // There is no point in continuing when we've hit EMFILE or ENFILE
1128 // because we won't be able to set up the stdio file descriptors.
1129 // It's kind of silly that the de facto spec for ENOENT (the test suite)
1130 // mandates that stdio _is_ set up, even if there is no process on the
1131 // receiving end, but it is what it is.
1132 if (err !== uv.UV_ENOENT) return err;
1134 // Close all opened fds on error
1135 stdio.forEach(function(stdio) {
1136 if (stdio.type === 'pipe') {
1137 stdio.handle.close();
1141 this._handle.close();
1142 this._handle = null;
1143 throw errnoException(err, 'spawn');
1146 this.pid = this._handle.pid;
1148 stdio.forEach(function(stdio, i) {
1149 if (stdio.type === 'ignore') return;
1152 self._closesNeeded++;
1157 // when i === 0 - we're dealing with stdin
1158 // (which is the only one writable pipe)
1159 stdio.socket = createSocket(self.pid !== 0 ? stdio.handle : null, i > 0);
1161 if (i > 0 && self.pid !== 0) {
1162 self._closesNeeded++;
1163 stdio.socket.on('close', function() {
1170 this.stdin = stdio.length >= 1 && !util.isUndefined(stdio[0].socket) ?
1171 stdio[0].socket : null;
1172 this.stdout = stdio.length >= 2 && !util.isUndefined(stdio[1].socket) ?
1173 stdio[1].socket : null;
1174 this.stderr = stdio.length >= 3 && !util.isUndefined(stdio[2].socket) ?
1175 stdio[2].socket : null;
1177 this.stdio = stdio.map(function(stdio) {
1178 return util.isUndefined(stdio.socket) ? null : stdio.socket;
1181 // Add .send() method and start listening for IPC data
1182 if (!util.isUndefined(ipc)) setupChannel(this, ipc);
1188 ChildProcess.prototype.kill = function(sig) {
1192 constants = process.binding('constants');
1198 signal = constants['SIGTERM'];
1200 signal = constants[sig];
1203 if (util.isUndefined(signal)) {
1204 throw new Error('Unknown signal: ' + sig);
1208 var err = this._handle.kill(signal);
1214 if (err === uv.UV_ESRCH) {
1216 } else if (err === uv.UV_EINVAL || err === uv.UV_ENOSYS) {
1217 /* The underlying platform doesn't support this signal. */
1218 throw errnoException(err, 'kill');
1220 /* Other error, almost certainly EPERM. */
1221 this.emit('error', errnoException(err, 'kill'));
1225 /* Kill didn't succeed. */
1230 ChildProcess.prototype.ref = function() {
1231 if (this._handle) this._handle.ref();
1235 ChildProcess.prototype.unref = function() {
1236 if (this._handle) this._handle.unref();
1240 function lookupSignal(signal) {
1241 if (typeof signal === 'number')
1245 constants = process.binding('constants');
1247 if (!(signal in constants))
1248 throw new Error('Unknown signal: ' + signal);
1250 return constants[signal];
1254 function spawnSync(/*file, args, options*/) {
1255 var opts = normalizeSpawnArguments.apply(null, arguments);
1257 var options = opts.options;
1261 options.file = opts.file;
1262 options.args = opts.args;
1264 if (options.killSignal)
1265 options.killSignal = lookupSignal(options.killSignal);
1267 options.stdio = _validateStdio(options.stdio || 'pipe', true).stdio;
1269 if (options.input) {
1270 var stdin = options.stdio[0] = util._extend({}, options.stdio[0]);
1271 stdin.input = options.input;
1274 // We may want to pass data in on any given fd, ensure it is a valid buffer
1275 for (i = 0; i < options.stdio.length; i++) {
1276 var input = options.stdio[i] && options.stdio[i].input;
1277 if (input != null) {
1278 var pipe = options.stdio[i] = util._extend({}, options.stdio[i]);
1279 if (Buffer.isBuffer(input))
1281 else if (util.isString(input))
1282 pipe.input = new Buffer(input, options.encoding);
1284 throw new TypeError(util.format(
1285 'stdio[%d] should be Buffer or string not %s',
1292 spawn_sync = process.binding('spawn_sync');
1294 var result = spawn_sync.spawn(options);
1296 if (result.output && options.encoding) {
1297 for (i = 0; i < result.output.length; i++) {
1298 if (!result.output[i])
1300 result.output[i] = result.output[i].toString(options.encoding);
1304 result.stdout = result.output && result.output[1];
1305 result.stderr = result.output && result.output[2];
1308 result.error = errnoException(result.error, 'spawnSync');
1310 util._extend(result, opts);
1314 exports.spawnSync = spawnSync;
1317 function checkExecSyncError(ret) {
1318 if (ret.error || ret.status !== 0) {
1319 var err = ret.error;
1323 var msg = 'Command failed: ' +
1324 (ret.cmd ? ret.cmd : ret.args.join(' ')) +
1325 (ret.stderr ? '\n' + ret.stderr.toString() : '');
1326 err = new Error(msg);
1329 util._extend(err, ret);
1337 function execFileSync(/*command, options*/) {
1338 var opts = normalizeSpawnArguments.apply(null, arguments);
1339 var inheritStderr = !opts.options.stdio;
1341 var ret = spawnSync(opts.file, opts.args.slice(1), opts.options);
1344 process.stderr.write(ret.stderr);
1346 var err = checkExecSyncError(ret);
1353 exports.execFileSync = execFileSync;
1356 function execSync(/*comand, options*/) {
1357 var opts = normalizeExecArgs.apply(null, arguments);
1358 var inheritStderr = opts.options ? !opts.options.stdio : true;
1360 var ret = spawnSync(opts.file, opts.args, opts.options);
1364 process.stderr.write(ret.stderr);
1366 var err = checkExecSyncError(ret);
1373 exports.execSync = execSync;