Add silent option to child_process.fork
authorAndreas Madsen <amwebdk@gmail.com>
Sat, 17 Dec 2011 10:52:40 +0000 (11:52 +0100)
committerRyan Dahl <ry@tinyclouds.org>
Sat, 17 Dec 2011 20:53:44 +0000 (12:53 -0800)
Fixes #2354.

doc/api/child_processes.markdown
lib/child_process.js
test/simple/test-child-process-silent.js [new file with mode: 0644]

index 8efcede..5580e5b 100644 (file)
@@ -229,8 +229,9 @@ And then the child script, `'sub.js'` might look like this:
 In the child the `process` object will have a `send()` method, and `process`
 will emit objects each time it receives a message on its channel.
 
-By default the spawned Node process will have the stdin, stdout, stderr
-associated with the parent's.
+By default the spawned Node process will have the stdout, stderr associated
+with the parent's. To change this behavior set the `silent` property in the
+`options` object to `true`.
 
 These child Nodes are still whole new instances of V8. Assume at least 30ms
 startup and 10mb memory for each new Node. That is, you cannot create many
index a34313f..8bcfdf7 100644 (file)
@@ -158,8 +158,8 @@ exports.fork = function(modulePath, args, options) {
   }
 
   // Leave stdin open for the IPC channel. stdout and stderr should be the
-  // same as the parent's.
-  options.customFds = [-1, 1, 2];
+  // same as the parent's if silent isn't set.
+  options.customFds = (options.silent ? [-1, -1, -1] : [-1, 1, 2]);
 
   // Just need to set this - child process won't actually use the fd.
   // For backwards compat - this can be changed to 'NODE_CHANNEL' before v0.6.
diff --git a/test/simple/test-child-process-silent.js b/test/simple/test-child-process-silent.js
new file mode 100644 (file)
index 0000000..3e6e013
--- /dev/null
@@ -0,0 +1,77 @@
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+var common = require('../common');
+var assert = require('assert');
+var fork = require('child_process').fork;
+
+var isChild = process.argv[2] === 'child';
+
+if (isChild) {
+  console.log('LOG: a stdout message');
+  console.error('LOG: a stderr message');
+
+  process.send('message from child');
+  process.once('message', function () {
+    process.send('got message from master');
+  });
+
+} else {
+
+  var checks = {
+    stdoutNotPiped: false,
+    stderrNotPiped: false,
+    childSending: false,
+    childReciveing: false
+  };
+
+  var child = fork(process.argv[1], ['child'], { silent: true });
+  child.on('message', function (message) {
+    if (checks.childSending === false) {
+      checks.childSending = (message === 'message from child');
+    }
+
+    if (checks.childReciveing === false) {
+      checks.childReciveing = (message === 'got message from master');
+    }
+
+    if (checks.childReciveing === true) {
+      child.kill();
+    }
+  });
+
+  checks.stdoutNotPiped = (child.stdout && child.stdout.readable === true);
+  checks.stderrNotPiped = (child.stderr && child.stderr.readable === true);
+
+  child.send('message to child');
+
+  //Check all values
+  process.once('exit', function() {
+
+    //Check message system
+    assert.ok(checks.childSending, 'The child was able to send a message');
+    assert.ok(checks.childReciveing, 'The child was able to receive a message');
+
+    //Check std(out|err) pipes
+    assert.ok(checks.stdoutNotPiped, 'The stdout socket was piped to parent');
+    assert.ok(checks.stdoutNotPiped, 'The stderr socket was piped to parent');
+  });
+}