PageRenderTime 50ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/child_process.js

https://gitlab.com/GeekSir/node
JavaScript | 1395 lines | 982 code | 278 blank | 135 comment | 249 complexity | 89ccb4db6a43eda1d15c044bfe6a47f2 MD5 | raw file
Possible License(s): 0BSD, Apache-2.0, MPL-2.0-no-copyleft-exception, JSON, WTFPL, CC-BY-SA-3.0, Unlicense, ISC, BSD-3-Clause, MIT, AGPL-3.0
  1. // Copyright Joyent, Inc. and other Node contributors.
  2. //
  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:
  10. //
  11. // The above copyright notice and this permission notice shall be included
  12. // in all copies or substantial portions of the Software.
  13. //
  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.
  21. 'use strict';
  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');
  28. var Process = process.binding('process_wrap').Process;
  29. var WriteWrap = process.binding('stream_wrap').WriteWrap;
  30. var uv = process.binding('uv');
  31. var spawn_sync; // Lazy-loaded process.binding('spawn_sync')
  32. var constants; // Lazy-loaded process.binding('constants')
  33. var errnoException = util._errnoException;
  34. var handleWraps = {};
  35. function handleWrapGetter(name, callback) {
  36. var cons;
  37. Object.defineProperty(handleWraps, name, {
  38. get: function() {
  39. if (!util.isUndefined(cons)) return cons;
  40. return cons = callback();
  41. }
  42. });
  43. }
  44. handleWrapGetter('Pipe', function() {
  45. return process.binding('pipe_wrap').Pipe;
  46. });
  47. handleWrapGetter('TTY', function() {
  48. return process.binding('tty_wrap').TTY;
  49. });
  50. handleWrapGetter('TCP', function() {
  51. return process.binding('tcp_wrap').TCP;
  52. });
  53. handleWrapGetter('UDP', function() {
  54. return process.binding('udp_wrap').UDP;
  55. });
  56. // constructors for lazy loading
  57. function createPipe(ipc) {
  58. return new handleWraps.Pipe(ipc);
  59. }
  60. function createSocket(pipe, readable) {
  61. var s = new net.Socket({ handle: pipe });
  62. if (readable) {
  63. s.writable = false;
  64. s.readable = true;
  65. } else {
  66. s.writable = true;
  67. s.readable = false;
  68. }
  69. return s;
  70. }
  71. // this object contain function to convert TCP objects to native handle objects
  72. // and back again.
  73. var handleConversion = {
  74. 'net.Native': {
  75. simultaneousAccepts: true,
  76. send: function(message, handle) {
  77. return handle;
  78. },
  79. got: function(message, handle, emit) {
  80. emit(handle);
  81. }
  82. },
  83. 'net.Server': {
  84. simultaneousAccepts: true,
  85. send: function(message, server) {
  86. return server._handle;
  87. },
  88. got: function(message, handle, emit) {
  89. var server = new net.Server();
  90. server.listen(handle, function() {
  91. emit(server);
  92. });
  93. }
  94. },
  95. 'net.Socket': {
  96. send: function(message, socket) {
  97. if (!socket._handle)
  98. return;
  99. // if the socket was created by net.Server
  100. if (socket.server) {
  101. // the slave should keep track of the socket
  102. message.key = socket.server._connectionKey;
  103. var firstTime = !this._channel.sockets.send[message.key];
  104. var socketList = getSocketList('send', this, message.key);
  105. // the server should no longer expose a .connection property
  106. // and when asked to close it should query the socket status from
  107. // the slaves
  108. if (firstTime) socket.server._setupSlave(socketList);
  109. // Act like socket is detached
  110. socket.server._connections--;
  111. }
  112. // remove handle from socket object, it will be closed when the socket
  113. // will be sent
  114. var handle = socket._handle;
  115. handle.onread = function() {};
  116. socket._handle = null;
  117. return handle;
  118. },
  119. postSend: function(handle) {
  120. // Close the Socket handle after sending it
  121. if (handle)
  122. handle.close();
  123. },
  124. got: function(message, handle, emit) {
  125. var socket = new net.Socket({handle: handle});
  126. socket.readable = socket.writable = true;
  127. // if the socket was created by net.Server we will track the socket
  128. if (message.key) {
  129. // add socket to connections list
  130. var socketList = getSocketList('got', this, message.key);
  131. socketList.add({
  132. socket: socket
  133. });
  134. }
  135. emit(socket);
  136. }
  137. },
  138. 'dgram.Native': {
  139. simultaneousAccepts: false,
  140. send: function(message, handle) {
  141. return handle;
  142. },
  143. got: function(message, handle, emit) {
  144. emit(handle);
  145. }
  146. },
  147. 'dgram.Socket': {
  148. simultaneousAccepts: false,
  149. send: function(message, socket) {
  150. message.dgramType = socket.type;
  151. return socket._handle;
  152. },
  153. got: function(message, handle, emit) {
  154. var socket = new dgram.Socket(message.dgramType);
  155. socket.bind(handle, function() {
  156. emit(socket);
  157. });
  158. }
  159. }
  160. };
  161. // This object keep track of the socket there are sended
  162. function SocketListSend(slave, key) {
  163. EventEmitter.call(this);
  164. this.key = key;
  165. this.slave = slave;
  166. }
  167. util.inherits(SocketListSend, EventEmitter);
  168. SocketListSend.prototype._request = function(msg, cmd, callback) {
  169. var self = this;
  170. if (!this.slave.connected) return onclose();
  171. this.slave.send(msg);
  172. function onclose() {
  173. self.slave.removeListener('internalMessage', onreply);
  174. callback(new Error('Slave closed before reply'));
  175. };
  176. function onreply(msg) {
  177. if (!(msg.cmd === cmd && msg.key === self.key)) return;
  178. self.slave.removeListener('disconnect', onclose);
  179. self.slave.removeListener('internalMessage', onreply);
  180. callback(null, msg);
  181. };
  182. this.slave.once('disconnect', onclose);
  183. this.slave.on('internalMessage', onreply);
  184. };
  185. SocketListSend.prototype.close = function close(callback) {
  186. this._request({
  187. cmd: 'NODE_SOCKET_NOTIFY_CLOSE',
  188. key: this.key
  189. }, 'NODE_SOCKET_ALL_CLOSED', callback);
  190. };
  191. SocketListSend.prototype.getConnections = function getConnections(callback) {
  192. this._request({
  193. cmd: 'NODE_SOCKET_GET_COUNT',
  194. key: this.key
  195. }, 'NODE_SOCKET_COUNT', function(err, msg) {
  196. if (err) return callback(err);
  197. callback(null, msg.count);
  198. });
  199. };
  200. // This object keep track of the socket there are received
  201. function SocketListReceive(slave, key) {
  202. EventEmitter.call(this);
  203. var self = this;
  204. this.connections = 0;
  205. this.key = key;
  206. this.slave = slave;
  207. function onempty() {
  208. if (!self.slave.connected) return;
  209. self.slave.send({
  210. cmd: 'NODE_SOCKET_ALL_CLOSED',
  211. key: self.key
  212. });
  213. }
  214. this.slave.on('internalMessage', function(msg) {
  215. if (msg.key !== self.key) return;
  216. if (msg.cmd === 'NODE_SOCKET_NOTIFY_CLOSE') {
  217. // Already empty
  218. if (self.connections === 0) return onempty();
  219. // Wait for sockets to get closed
  220. self.once('empty', onempty);
  221. } else if (msg.cmd === 'NODE_SOCKET_GET_COUNT') {
  222. if (!self.slave.connected) return;
  223. self.slave.send({
  224. cmd: 'NODE_SOCKET_COUNT',
  225. key: self.key,
  226. count: self.connections
  227. });
  228. }
  229. });
  230. }
  231. util.inherits(SocketListReceive, EventEmitter);
  232. SocketListReceive.prototype.add = function(obj) {
  233. var self = this;
  234. this.connections++;
  235. // Notify previous owner of socket about its state change
  236. obj.socket.once('close', function() {
  237. self.connections--;
  238. if (self.connections === 0) self.emit('empty');
  239. });
  240. };
  241. function getSocketList(type, slave, key) {
  242. var sockets = slave._channel.sockets[type];
  243. var socketList = sockets[key];
  244. if (!socketList) {
  245. var Construct = type === 'send' ? SocketListSend : SocketListReceive;
  246. socketList = sockets[key] = new Construct(slave, key);
  247. }
  248. return socketList;
  249. }
  250. var INTERNAL_PREFIX = 'NODE_';
  251. function handleMessage(target, message, handle) {
  252. var eventName = 'message';
  253. if (!util.isNull(message) &&
  254. util.isObject(message) &&
  255. util.isString(message.cmd) &&
  256. message.cmd.length > INTERNAL_PREFIX.length &&
  257. message.cmd.slice(0, INTERNAL_PREFIX.length) === INTERNAL_PREFIX) {
  258. eventName = 'internalMessage';
  259. }
  260. target.emit(eventName, message, handle);
  261. }
  262. function setupChannel(target, channel) {
  263. target._channel = channel;
  264. target._handleQueue = null;
  265. var decoder = new StringDecoder('utf8');
  266. var jsonBuffer = '';
  267. channel.buffering = false;
  268. channel.onread = function(nread, pool, recvHandle) {
  269. // TODO(bnoordhuis) Check that nread > 0.
  270. if (pool) {
  271. jsonBuffer += decoder.write(pool);
  272. var i, start = 0;
  273. //Linebreak is used as a message end sign
  274. while ((i = jsonBuffer.indexOf('\n', start)) >= 0) {
  275. var json = jsonBuffer.slice(start, i);
  276. var message = JSON.parse(json);
  277. // There will be at most one NODE_HANDLE message in every chunk we
  278. // read because SCM_RIGHTS messages don't get coalesced. Make sure
  279. // that we deliver the handle with the right message however.
  280. if (message && message.cmd === 'NODE_HANDLE')
  281. handleMessage(target, message, recvHandle);
  282. else
  283. handleMessage(target, message, undefined);
  284. start = i + 1;
  285. }
  286. jsonBuffer = jsonBuffer.slice(start);
  287. this.buffering = jsonBuffer.length !== 0;
  288. } else {
  289. this.buffering = false;
  290. target.disconnect();
  291. channel.onread = nop;
  292. channel.close();
  293. maybeClose(target);
  294. }
  295. };
  296. // object where socket lists will live
  297. channel.sockets = { got: {}, send: {} };
  298. // handlers will go through this
  299. target.on('internalMessage', function(message, handle) {
  300. // Once acknowledged - continue sending handles.
  301. if (message.cmd === 'NODE_HANDLE_ACK') {
  302. assert(util.isArray(target._handleQueue));
  303. var queue = target._handleQueue;
  304. target._handleQueue = null;
  305. queue.forEach(function(args) {
  306. target._send(args.message, args.handle, false);
  307. });
  308. // Process a pending disconnect (if any).
  309. if (!target.connected && target._channel && !target._handleQueue)
  310. target._disconnect();
  311. return;
  312. }
  313. if (message.cmd !== 'NODE_HANDLE') return;
  314. // Acknowledge handle receival. Don't emit error events (for example if
  315. // the other side has disconnected) because this call to send() is not
  316. // initiated by the user and it shouldn't be fatal to be unable to ACK
  317. // a message.
  318. target._send({ cmd: 'NODE_HANDLE_ACK' }, null, true);
  319. var obj = handleConversion[message.type];
  320. // Update simultaneous accepts on Windows
  321. if (process.platform === 'win32') {
  322. handle._simultaneousAccepts = false;
  323. net._setSimultaneousAccepts(handle);
  324. }
  325. // Convert handle object
  326. obj.got.call(this, message, handle, function(handle) {
  327. handleMessage(target, message.msg, handle);
  328. });
  329. });
  330. target.send = function(message, handle) {
  331. if (!this.connected)
  332. this.emit('error', new Error('channel closed'));
  333. else
  334. this._send(message, handle, false);
  335. };
  336. target._send = function(message, handle, swallowErrors) {
  337. assert(this.connected || this._channel);
  338. if (util.isUndefined(message))
  339. throw new TypeError('message cannot be undefined');
  340. // package messages with a handle object
  341. if (handle) {
  342. // this message will be handled by an internalMessage event handler
  343. message = {
  344. cmd: 'NODE_HANDLE',
  345. type: null,
  346. msg: message
  347. };
  348. if (handle instanceof net.Socket) {
  349. message.type = 'net.Socket';
  350. } else if (handle instanceof net.Server) {
  351. message.type = 'net.Server';
  352. } else if (handle instanceof process.binding('tcp_wrap').TCP ||
  353. handle instanceof process.binding('pipe_wrap').Pipe) {
  354. message.type = 'net.Native';
  355. } else if (handle instanceof dgram.Socket) {
  356. message.type = 'dgram.Socket';
  357. } else if (handle instanceof process.binding('udp_wrap').UDP) {
  358. message.type = 'dgram.Native';
  359. } else {
  360. throw new TypeError("This handle type can't be sent");
  361. }
  362. // Queue-up message and handle if we haven't received ACK yet.
  363. if (this._handleQueue) {
  364. this._handleQueue.push({ message: message.msg, handle: handle });
  365. return;
  366. }
  367. var obj = handleConversion[message.type];
  368. // convert TCP object to native handle object
  369. handle =
  370. handleConversion[message.type].send.call(target, message, handle);
  371. // If handle was sent twice, or it is impossible to get native handle
  372. // out of it - just send a text without the handle.
  373. if (!handle)
  374. message = message.msg;
  375. // Update simultaneous accepts on Windows
  376. if (obj.simultaneousAccepts) {
  377. net._setSimultaneousAccepts(handle);
  378. }
  379. } else if (this._handleQueue &&
  380. !(message && message.cmd === 'NODE_HANDLE_ACK')) {
  381. // Queue request anyway to avoid out-of-order messages.
  382. this._handleQueue.push({ message: message, handle: null });
  383. return;
  384. }
  385. var req = new WriteWrap();
  386. req.oncomplete = nop;
  387. var string = JSON.stringify(message) + '\n';
  388. var err = channel.writeUtf8String(req, string, handle);
  389. if (err) {
  390. if (!swallowErrors)
  391. this.emit('error', errnoException(err, 'write'));
  392. } else if (handle && !this._handleQueue) {
  393. this._handleQueue = [];
  394. }
  395. if (obj && obj.postSend) {
  396. req.oncomplete = obj.postSend.bind(null, handle);
  397. }
  398. /* If the master is > 2 read() calls behind, please stop sending. */
  399. return channel.writeQueueSize < (65536 * 2);
  400. };
  401. // connected will be set to false immediately when a disconnect() is
  402. // requested, even though the channel might still be alive internally to
  403. // process queued messages. The three states are distinguished as follows:
  404. // - disconnect() never requested: _channel is not null and connected
  405. // is true
  406. // - disconnect() requested, messages in the queue: _channel is not null
  407. // and connected is false
  408. // - disconnect() requested, channel actually disconnected: _channel is
  409. // null and connected is false
  410. target.connected = true;
  411. target.disconnect = function() {
  412. if (!this.connected) {
  413. this.emit('error', new Error('IPC channel is already disconnected'));
  414. return;
  415. }
  416. // Do not allow any new messages to be written.
  417. this.connected = false;
  418. // If there are no queued messages, disconnect immediately. Otherwise,
  419. // postpone the disconnect so that it happens internally after the
  420. // queue is flushed.
  421. if (!this._handleQueue)
  422. this._disconnect();
  423. };
  424. target._disconnect = function() {
  425. assert(this._channel);
  426. // This marks the fact that the channel is actually disconnected.
  427. this._channel = null;
  428. var fired = false;
  429. function finish() {
  430. if (fired) return;
  431. fired = true;
  432. channel.close();
  433. target.emit('disconnect');
  434. }
  435. // If a message is being read, then wait for it to complete.
  436. if (channel.buffering) {
  437. this.once('message', finish);
  438. this.once('internalMessage', finish);
  439. return;
  440. }
  441. process.nextTick(finish);
  442. };
  443. channel.readStart();
  444. }
  445. function nop() { }
  446. exports.fork = function(modulePath /*, args, options*/) {
  447. // Get options and args arguments.
  448. var options, args, execArgv;
  449. if (util.isArray(arguments[1])) {
  450. args = arguments[1];
  451. options = util._extend({}, arguments[2]);
  452. } else if (arguments[1] && typeof arguments[1] !== 'object') {
  453. throw new TypeError('Incorrect value of args option');
  454. } else {
  455. args = [];
  456. options = util._extend({}, arguments[1]);
  457. }
  458. // Prepare arguments for fork:
  459. execArgv = options.execArgv || process.execArgv;
  460. args = execArgv.concat([modulePath], args);
  461. // Leave stdin open for the IPC channel. stdout and stderr should be the
  462. // same as the parent's if silent isn't set.
  463. options.stdio = options.silent ? ['pipe', 'pipe', 'pipe', 'ipc'] :
  464. [0, 1, 2, 'ipc'];
  465. options.execPath = options.execPath || process.execPath;
  466. return spawn(options.execPath, args, options);
  467. };
  468. exports._forkChild = function(fd) {
  469. // set process.send()
  470. var p = createPipe(true);
  471. p.open(fd);
  472. // p.open() puts the file descriptor in non-blocking mode
  473. // but it must be synchronous for backwards compatibility.
  474. var err = p.setBlocking(true);
  475. if (err)
  476. throw errnoException(err, 'setBlocking');
  477. p.unref();
  478. setupChannel(process, p);
  479. var refs = 0;
  480. process.on('newListener', function(name) {
  481. if (name !== 'message' && name !== 'disconnect') return;
  482. if (++refs === 1) p.ref();
  483. });
  484. process.on('removeListener', function(name) {
  485. if (name !== 'message' && name !== 'disconnect') return;
  486. if (--refs === 0) p.unref();
  487. });
  488. };
  489. function normalizeExecArgs(command /*, options, callback */) {
  490. var file, args, options, callback;
  491. if (util.isFunction(arguments[1])) {
  492. options = undefined;
  493. callback = arguments[1];
  494. } else {
  495. options = arguments[1];
  496. callback = arguments[2];
  497. }
  498. if (process.platform === 'win32') {
  499. file = process.env.comspec || 'cmd.exe';
  500. args = ['/s', '/c', '"' + command + '"'];
  501. // Make a shallow copy before patching so we don't clobber the user's
  502. // options object.
  503. options = util._extend({}, options);
  504. options.windowsVerbatimArguments = true;
  505. } else {
  506. file = '/bin/sh';
  507. args = ['-c', command];
  508. }
  509. if (options && options.shell)
  510. file = options.shell;
  511. return {
  512. cmd: command,
  513. file: file,
  514. args: args,
  515. options: options,
  516. callback: callback
  517. };
  518. }
  519. exports.exec = function(command /*, options, callback */) {
  520. var opts = normalizeExecArgs.apply(null, arguments);
  521. return exports.execFile(opts.file,
  522. opts.args,
  523. opts.options,
  524. opts.callback);
  525. };
  526. exports.execFile = function(file /* args, options, callback */) {
  527. var args = [], callback;
  528. var options = {
  529. encoding: 'utf8',
  530. timeout: 0,
  531. maxBuffer: 200 * 1024,
  532. killSignal: 'SIGTERM',
  533. cwd: null,
  534. env: null
  535. };
  536. // Parse the optional positional parameters.
  537. var pos = 1;
  538. if (pos < arguments.length && Array.isArray(arguments[pos])) {
  539. args = arguments[pos++];
  540. } else if (pos < arguments.length && arguments[pos] == null) {
  541. pos++;
  542. }
  543. if (pos < arguments.length && util.isObject(arguments[pos])) {
  544. options = util._extend(options, arguments[pos++]);
  545. } else if (pos < arguments.length && arguments[pos] == null) {
  546. pos++;
  547. }
  548. if (pos < arguments.length && util.isFunction(arguments[pos])) {
  549. callback = arguments[pos++];
  550. }
  551. if (pos === 1 && arguments.length > 1) {
  552. throw new TypeError('Incorrect value of args option');
  553. }
  554. var child = spawn(file, args, {
  555. cwd: options.cwd,
  556. env: options.env,
  557. gid: options.gid,
  558. uid: options.uid,
  559. windowsVerbatimArguments: !!options.windowsVerbatimArguments
  560. });
  561. var encoding;
  562. var _stdout;
  563. var _stderr;
  564. if (options.encoding !== 'buffer' && Buffer.isEncoding(options.encoding)) {
  565. encoding = options.encoding;
  566. _stdout = '';
  567. _stderr = '';
  568. } else {
  569. _stdout = [];
  570. _stderr = [];
  571. encoding = null;
  572. }
  573. var stdoutLen = 0;
  574. var stderrLen = 0;
  575. var killed = false;
  576. var exited = false;
  577. var timeoutId;
  578. var ex = null;
  579. function exithandler(code, signal) {
  580. if (exited) return;
  581. exited = true;
  582. if (timeoutId) {
  583. clearTimeout(timeoutId);
  584. timeoutId = null;
  585. }
  586. if (!callback) return;
  587. // merge chunks
  588. var stdout;
  589. var stderr;
  590. if (!encoding) {
  591. stdout = Buffer.concat(_stdout);
  592. stderr = Buffer.concat(_stderr);
  593. } else {
  594. stdout = _stdout;
  595. stderr = _stderr;
  596. }
  597. if (ex) {
  598. // Will be handled later
  599. } else if (code === 0 && signal === null) {
  600. callback(null, stdout, stderr);
  601. return;
  602. }
  603. var cmd = file;
  604. if (args.length !== 0)
  605. cmd += ' ' + args.join(' ');
  606. if (!ex) {
  607. ex = new Error('Command failed: ' + cmd + '\n' + stderr);
  608. ex.killed = child.killed || killed;
  609. ex.code = code < 0 ? uv.errname(code) : code;
  610. ex.signal = signal;
  611. }
  612. ex.cmd = cmd;
  613. callback(ex, stdout, stderr);
  614. }
  615. function errorhandler(e) {
  616. ex = e;
  617. child.stdout.destroy();
  618. child.stderr.destroy();
  619. exithandler();
  620. }
  621. function kill() {
  622. child.stdout.destroy();
  623. child.stderr.destroy();
  624. killed = true;
  625. try {
  626. child.kill(options.killSignal);
  627. } catch (e) {
  628. ex = e;
  629. exithandler();
  630. }
  631. }
  632. if (options.timeout > 0) {
  633. timeoutId = setTimeout(function() {
  634. kill();
  635. timeoutId = null;
  636. }, options.timeout);
  637. }
  638. child.stdout.addListener('data', function(chunk) {
  639. stdoutLen += chunk.length;
  640. if (stdoutLen > options.maxBuffer) {
  641. ex = new Error('stdout maxBuffer exceeded.');
  642. kill();
  643. } else {
  644. if (!encoding)
  645. _stdout.push(chunk);
  646. else
  647. _stdout += chunk;
  648. }
  649. });
  650. child.stderr.addListener('data', function(chunk) {
  651. stderrLen += chunk.length;
  652. if (stderrLen > options.maxBuffer) {
  653. ex = new Error('stderr maxBuffer exceeded.');
  654. kill();
  655. } else {
  656. if (!encoding)
  657. _stderr.push(chunk);
  658. else
  659. _stderr += chunk;
  660. }
  661. });
  662. if (encoding) {
  663. child.stderr.setEncoding(encoding);
  664. child.stdout.setEncoding(encoding);
  665. }
  666. child.addListener('close', exithandler);
  667. child.addListener('error', errorhandler);
  668. return child;
  669. };
  670. var _deprecatedCustomFds = util.deprecate(function(options) {
  671. options.stdio = options.customFds.map(function(fd) {
  672. return fd === -1 ? 'pipe' : fd;
  673. });
  674. }, 'child_process: customFds option is deprecated, use stdio instead.');
  675. function _convertCustomFds(options) {
  676. if (options && options.customFds && !options.stdio) {
  677. _deprecatedCustomFds(options);
  678. }
  679. }
  680. function _validateStdio(stdio, sync) {
  681. var ipc,
  682. ipcFd;
  683. // Replace shortcut with an array
  684. if (util.isString(stdio)) {
  685. switch (stdio) {
  686. case 'ignore': stdio = ['ignore', 'ignore', 'ignore']; break;
  687. case 'pipe': stdio = ['pipe', 'pipe', 'pipe']; break;
  688. case 'inherit': stdio = [0, 1, 2]; break;
  689. default: throw new TypeError('Incorrect value of stdio option: ' + stdio);
  690. }
  691. } else if (!util.isArray(stdio)) {
  692. throw new TypeError('Incorrect value of stdio option: ' +
  693. util.inspect(stdio));
  694. }
  695. // At least 3 stdio will be created
  696. // Don't concat() a new Array() because it would be sparse, and
  697. // stdio.reduce() would skip the sparse elements of stdio.
  698. // See http://stackoverflow.com/a/5501711/3561
  699. while (stdio.length < 3) stdio.push(undefined);
  700. // Translate stdio into C++-readable form
  701. // (i.e. PipeWraps or fds)
  702. stdio = stdio.reduce(function(acc, stdio, i) {
  703. function cleanup() {
  704. acc.filter(function(stdio) {
  705. return stdio.type === 'pipe' || stdio.type === 'ipc';
  706. }).forEach(function(stdio) {
  707. if (stdio.handle)
  708. stdio.handle.close();
  709. });
  710. }
  711. // Defaults
  712. if (util.isNullOrUndefined(stdio)) {
  713. stdio = i < 3 ? 'pipe' : 'ignore';
  714. }
  715. if (stdio === null || stdio === 'ignore') {
  716. acc.push({type: 'ignore'});
  717. } else if (stdio === 'pipe' || util.isNumber(stdio) && stdio < 0) {
  718. var a = {
  719. type: 'pipe',
  720. readable: i === 0,
  721. writable: i !== 0
  722. };
  723. if (!sync)
  724. a.handle = createPipe();
  725. acc.push(a);
  726. } else if (stdio === 'ipc') {
  727. if (sync || !util.isUndefined(ipc)) {
  728. // Cleanup previously created pipes
  729. cleanup();
  730. if (!sync)
  731. throw Error('Child process can have only one IPC pipe');
  732. else
  733. throw Error('You cannot use IPC with synchronous forks');
  734. }
  735. ipc = createPipe(true);
  736. ipcFd = i;
  737. acc.push({
  738. type: 'pipe',
  739. handle: ipc,
  740. ipc: true
  741. });
  742. } else if (stdio === 'inherit') {
  743. acc.push({
  744. type: 'inherit',
  745. fd: i
  746. });
  747. } else if (util.isNumber(stdio) || util.isNumber(stdio.fd)) {
  748. acc.push({
  749. type: 'fd',
  750. fd: stdio.fd || stdio
  751. });
  752. } else if (getHandleWrapType(stdio) || getHandleWrapType(stdio.handle) ||
  753. getHandleWrapType(stdio._handle)) {
  754. var handle = getHandleWrapType(stdio) ?
  755. stdio :
  756. getHandleWrapType(stdio.handle) ? stdio.handle : stdio._handle;
  757. acc.push({
  758. type: 'wrap',
  759. wrapType: getHandleWrapType(handle),
  760. handle: handle
  761. });
  762. } else if (util.isBuffer(stdio) || util.isString(stdio)) {
  763. if (!sync) {
  764. cleanup();
  765. throw new TypeError('Asynchronous forks do not support Buffer input: ' +
  766. util.inspect(stdio));
  767. }
  768. } else {
  769. // Cleanup
  770. cleanup();
  771. throw new TypeError('Incorrect value for stdio stream: ' +
  772. util.inspect(stdio));
  773. }
  774. return acc;
  775. }, []);
  776. return {stdio: stdio, ipc: ipc, ipcFd: ipcFd};
  777. }
  778. function normalizeSpawnArguments(file /*, args, options*/) {
  779. var args, options;
  780. if (Array.isArray(arguments[1])) {
  781. args = arguments[1].slice(0);
  782. options = arguments[2];
  783. } else if (arguments[1] !== undefined && !util.isObject(arguments[1])) {
  784. throw new TypeError('Incorrect value of args option');
  785. } else {
  786. args = [];
  787. options = arguments[1];
  788. }
  789. if (options === undefined)
  790. options = {};
  791. else if (!util.isObject(options))
  792. throw new TypeError('options argument must be an object');
  793. args.unshift(file);
  794. var env = options.env || process.env;
  795. var envPairs = [];
  796. for (var key in env) {
  797. envPairs.push(key + '=' + env[key]);
  798. }
  799. _convertCustomFds(options);
  800. return {
  801. file: file,
  802. args: args,
  803. options: options,
  804. envPairs: envPairs
  805. };
  806. }
  807. var spawn = exports.spawn = function(/*file, args, options*/) {
  808. var opts = normalizeSpawnArguments.apply(null, arguments);
  809. var options = opts.options;
  810. var child = new ChildProcess();
  811. child.spawn({
  812. file: opts.file,
  813. args: opts.args,
  814. cwd: options.cwd,
  815. windowsVerbatimArguments: !!options.windowsVerbatimArguments,
  816. detached: !!options.detached,
  817. envPairs: opts.envPairs,
  818. stdio: options.stdio,
  819. uid: options.uid,
  820. gid: options.gid
  821. });
  822. return child;
  823. };
  824. function maybeClose(subprocess) {
  825. subprocess._closesGot++;
  826. if (subprocess._closesGot == subprocess._closesNeeded) {
  827. subprocess.emit('close', subprocess.exitCode, subprocess.signalCode);
  828. }
  829. }
  830. function ChildProcess() {
  831. EventEmitter.call(this);
  832. // Initialize TCPWrap and PipeWrap
  833. process.binding('tcp_wrap');
  834. process.binding('pipe_wrap');
  835. var self = this;
  836. this._closesNeeded = 1;
  837. this._closesGot = 0;
  838. this.connected = false;
  839. this.signalCode = null;
  840. this.exitCode = null;
  841. this.killed = false;
  842. this.spawnfile = null;
  843. this._handle = new Process();
  844. this._handle.owner = this;
  845. this._handle.onexit = function(exitCode, signalCode) {
  846. //
  847. // follow 0.4.x behaviour:
  848. //
  849. // - normally terminated processes don't touch this.signalCode
  850. // - signaled processes don't touch this.exitCode
  851. //
  852. // new in 0.9.x:
  853. //
  854. // - spawn failures are reported with exitCode < 0
  855. //
  856. var syscall = self.spawnfile ? 'spawn ' + self.spawnfile : 'spawn';
  857. var err = (exitCode < 0) ? errnoException(exitCode, syscall) : null;
  858. if (signalCode) {
  859. self.signalCode = signalCode;
  860. } else {
  861. self.exitCode = exitCode;
  862. }
  863. if (self.stdin) {
  864. self.stdin.destroy();
  865. }
  866. self._handle.close();
  867. self._handle = null;
  868. if (exitCode < 0) {
  869. if (self.spawnfile)
  870. err.path = self.spawnfile;
  871. self.emit('error', err);
  872. } else {
  873. self.emit('exit', self.exitCode, self.signalCode);
  874. }
  875. // if any of the stdio streams have not been touched,
  876. // then pull all the data through so that it can get the
  877. // eof and emit a 'close' event.
  878. // Do it on nextTick so that the user has one last chance
  879. // to consume the output, if for example they only want to
  880. // start reading the data once the process exits.
  881. process.nextTick(function() {
  882. flushStdio(self);
  883. });
  884. maybeClose(self);
  885. };
  886. }
  887. util.inherits(ChildProcess, EventEmitter);
  888. function flushStdio(subprocess) {
  889. if (subprocess.stdio == null) return;
  890. subprocess.stdio.forEach(function(stream, fd, stdio) {
  891. if (!stream || !stream.readable || stream._consuming ||
  892. stream._readableState.flowing)
  893. return;
  894. stream.resume();
  895. });
  896. }
  897. function getHandleWrapType(stream) {
  898. if (stream instanceof handleWraps.Pipe) return 'pipe';
  899. if (stream instanceof handleWraps.TTY) return 'tty';
  900. if (stream instanceof handleWraps.TCP) return 'tcp';
  901. if (stream instanceof handleWraps.UDP) return 'udp';
  902. return false;
  903. }
  904. ChildProcess.prototype.spawn = function(options) {
  905. var self = this,
  906. ipc,
  907. ipcFd,
  908. // If no `stdio` option was given - use default
  909. stdio = options.stdio || 'pipe';
  910. stdio = _validateStdio(stdio, false);
  911. ipc = stdio.ipc;
  912. ipcFd = stdio.ipcFd;
  913. stdio = options.stdio = stdio.stdio;
  914. if (!util.isUndefined(ipc)) {
  915. // Let child process know about opened IPC channel
  916. options.envPairs = options.envPairs || [];
  917. options.envPairs.push('NODE_CHANNEL_FD=' + ipcFd);
  918. }
  919. this.spawnfile = options.file;
  920. var err = this._handle.spawn(options);
  921. // Run-time errors should emit an error, not throw an exception.
  922. if (err === uv.UV_EAGAIN ||
  923. err === uv.UV_EMFILE ||
  924. err === uv.UV_ENFILE ||
  925. err === uv.UV_ENOENT) {
  926. process.nextTick(function() {
  927. self._handle.onexit(err);
  928. });
  929. // There is no point in continuing when we've hit EMFILE or ENFILE
  930. // because we won't be able to set up the stdio file descriptors.
  931. // It's kind of silly that the de facto spec for ENOENT (the test suite)
  932. // mandates that stdio _is_ set up, even if there is no process on the
  933. // receiving end, but it is what it is.
  934. if (err !== uv.UV_ENOENT) return err;
  935. } else if (err) {
  936. // Close all opened fds on error
  937. stdio.forEach(function(stdio) {
  938. if (stdio.type === 'pipe') {
  939. stdio.handle.close();
  940. }
  941. });
  942. this._handle.close();
  943. this._handle = null;
  944. throw errnoException(err, 'spawn');
  945. }
  946. this.pid = this._handle.pid;
  947. stdio.forEach(function(stdio, i) {
  948. if (stdio.type === 'ignore') return;
  949. if (stdio.ipc) {
  950. self._closesNeeded++;
  951. return;
  952. }
  953. if (stdio.handle) {
  954. // when i === 0 - we're dealing with stdin
  955. // (which is the only one writable pipe)
  956. stdio.socket = createSocket(self.pid !== 0 ? stdio.handle : null, i > 0);
  957. if (i > 0 && self.pid !== 0) {
  958. self._closesNeeded++;
  959. stdio.socket.on('close', function() {
  960. maybeClose(self);
  961. });
  962. }
  963. }
  964. });
  965. this.stdin = stdio.length >= 1 && !util.isUndefined(stdio[0].socket) ?
  966. stdio[0].socket : null;
  967. this.stdout = stdio.length >= 2 && !util.isUndefined(stdio[1].socket) ?
  968. stdio[1].socket : null;
  969. this.stderr = stdio.length >= 3 && !util.isUndefined(stdio[2].socket) ?
  970. stdio[2].socket : null;
  971. this.stdio = stdio.map(function(stdio) {
  972. return util.isUndefined(stdio.socket) ? null : stdio.socket;
  973. });
  974. // Add .send() method and start listening for IPC data
  975. if (!util.isUndefined(ipc)) setupChannel(this, ipc);
  976. return err;
  977. };
  978. ChildProcess.prototype.kill = function(sig) {
  979. var signal;
  980. if (!constants) {
  981. constants = process.binding('constants');
  982. }
  983. if (sig === 0) {
  984. signal = 0;
  985. } else if (!sig) {
  986. signal = constants['SIGTERM'];
  987. } else {
  988. signal = constants[sig];
  989. }
  990. if (util.isUndefined(signal)) {
  991. throw new Error('Unknown signal: ' + sig);
  992. }
  993. if (this._handle) {
  994. var err = this._handle.kill(signal);
  995. if (err === 0) {
  996. /* Success. */
  997. this.killed = true;
  998. return true;
  999. }
  1000. if (err === uv.UV_ESRCH) {
  1001. /* Already dead. */
  1002. } else if (err === uv.UV_EINVAL || err === uv.UV_ENOSYS) {
  1003. /* The underlying platform doesn't support this signal. */
  1004. throw errnoException(err, 'kill');
  1005. } else {
  1006. /* Other error, almost certainly EPERM. */
  1007. this.emit('error', errnoException(err, 'kill'));
  1008. }
  1009. }
  1010. /* Kill didn't succeed. */
  1011. return false;
  1012. };
  1013. ChildProcess.prototype.ref = function() {
  1014. if (this._handle) this._handle.ref();
  1015. };
  1016. ChildProcess.prototype.unref = function() {
  1017. if (this._handle) this._handle.unref();
  1018. };
  1019. function lookupSignal(signal) {
  1020. if (typeof signal === 'number')
  1021. return signal;
  1022. if (!constants)
  1023. constants = process.binding('constants');
  1024. if (!(signal in constants))
  1025. throw new Error('Unknown signal: ' + signal);
  1026. return constants[signal];
  1027. }
  1028. function spawnSync(/*file, args, options*/) {
  1029. var opts = normalizeSpawnArguments.apply(null, arguments);
  1030. var options = opts.options;
  1031. var i;
  1032. options.file = opts.file;
  1033. options.args = opts.args;
  1034. options.envPairs = opts.envPairs;
  1035. if (options.killSignal)
  1036. options.killSignal = lookupSignal(options.killSignal);
  1037. options.stdio = _validateStdio(options.stdio || 'pipe', true).stdio;
  1038. if (options.input) {
  1039. var stdin = options.stdio[0] = util._extend({}, options.stdio[0]);
  1040. stdin.input = options.input;
  1041. }
  1042. // We may want to pass data in on any given fd, ensure it is a valid buffer
  1043. for (i = 0; i < options.stdio.length; i++) {
  1044. var input = options.stdio[i] && options.stdio[i].input;
  1045. if (input != null) {
  1046. var pipe = options.stdio[i] = util._extend({}, options.stdio[i]);
  1047. if (Buffer.isBuffer(input))
  1048. pipe.input = input;
  1049. else if (util.isString(input))
  1050. pipe.input = new Buffer(input, options.encoding);
  1051. else
  1052. throw new TypeError(util.format(
  1053. 'stdio[%d] should be Buffer or string not %s',
  1054. i,
  1055. typeof input));
  1056. }
  1057. }
  1058. if (!spawn_sync)
  1059. spawn_sync = process.binding('spawn_sync');
  1060. var result = spawn_sync.spawn(options);
  1061. if (result.output && options.encoding) {
  1062. for (i = 0; i < result.output.length; i++) {
  1063. if (!result.output[i])
  1064. continue;
  1065. result.output[i] = result.output[i].toString(options.encoding);
  1066. }
  1067. }
  1068. result.stdout = result.output && result.output[1];
  1069. result.stderr = result.output && result.output[2];
  1070. if (result.error)
  1071. result.error = errnoException(result.error, 'spawnSync');
  1072. util._extend(result, opts);
  1073. return result;
  1074. }
  1075. exports.spawnSync = spawnSync;
  1076. function checkExecSyncError(ret) {
  1077. if (ret.error || ret.status !== 0) {
  1078. var err = ret.error;
  1079. ret.error = null;
  1080. if (!err) {
  1081. var msg = 'Command failed: ' +
  1082. (ret.cmd ? ret.cmd : ret.args.join(' ')) +
  1083. (ret.stderr ? '\n' + ret.stderr.toString() : '');
  1084. err = new Error(msg);
  1085. }
  1086. util._extend(err, ret);
  1087. return err;
  1088. }
  1089. return false;
  1090. }
  1091. function execFileSync(/*command, options*/) {
  1092. var opts = normalizeSpawnArguments.apply(null, arguments);
  1093. var inheritStderr = !opts.options.stdio;
  1094. var ret = spawnSync(opts.file, opts.args.slice(1), opts.options);
  1095. if (inheritStderr)
  1096. process.stderr.write(ret.stderr);
  1097. var err = checkExecSyncError(ret);
  1098. if (err)
  1099. throw err;
  1100. else
  1101. return ret.stdout;
  1102. }
  1103. exports.execFileSync = execFileSync;
  1104. function execSync(/*comand, options*/) {
  1105. var opts = normalizeExecArgs.apply(null, arguments);
  1106. var inheritStderr = opts.options ? !opts.options.stdio : true;
  1107. var ret = spawnSync(opts.file, opts.args, opts.options);
  1108. ret.cmd = opts.cmd;
  1109. if (inheritStderr)
  1110. process.stderr.write(ret.stderr);
  1111. var err = checkExecSyncError(ret);
  1112. if (err)
  1113. throw err;
  1114. else
  1115. return ret.stdout;
  1116. }
  1117. exports.execSync = execSync;