Skip to content

Commit 5d70af8

Browse files
committed
child_process: set stdin properties when execed
When a child process is created, the `stdin` will not have `isTTY`, `isRaw` and `setRawMode` properties. Because, `uv_guess_handle` in `guessHandleType` call returns `PIPE` for fd 0. So, we create a `net.Socket` and return. But normally it will return `TTY` and we create `tty.ReadStream` and return where all those properties are properly set. This path explicitly sets the above mentioned properties on the returned socket object. Fixes: nodejs#2333 PR-URL: nodejs#2336
1 parent 2db57bd commit 5d70af8

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

src/node.js

+6
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,12 @@
717717
stdin._handle.readStop();
718718
});
719719

720+
stdin.isTTY = true;
721+
stdin.isRaw = false;
722+
stdin.setRawMode = function setRawMode() {
723+
throw new Error('Not a raw device');
724+
};
725+
720726
return stdin;
721727
});
722728

test/fixtures/child-process-stdin.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
console.log(process.stdin.isTTY);
2+
console.log(process.stdin.isRaw);
3+
try {
4+
process.stdin.setRawMode();
5+
} catch (ex) {
6+
console.error(ex);
7+
}

test/parallel/test-child-process-stdin.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
'use strict';
22
var common = require('../common');
33
var assert = require('assert');
4-
4+
const os = require('os');
5+
const path = require('path');
56
var spawn = require('child_process').spawn;
7+
const exec = require('child_process').exec;
68

79
var cat = spawn(common.isWindows ? 'more' : 'cat');
810
cat.stdin.write('hello');
@@ -66,3 +68,18 @@ process.on('exit', function() {
6668
assert.equal('hello world', response);
6769
}
6870
});
71+
72+
// Regression test for https://github.com/nodejs/io.js/issues/2333
73+
const cpFile = path.join(common.fixturesDir, 'child-process-stdin.js');
74+
const nodeBinary = process.argv[0];
75+
76+
exec(`${nodeBinary} ${cpFile}`, function(err, stdout, stderr) {
77+
const stdoutLines = stdout.split(os.EOL);
78+
assert.strictEqual(stdoutLines[0], 'true');
79+
assert.strictEqual(stdoutLines[1], 'false');
80+
assert.strictEqual(stdoutLines.length, 3);
81+
82+
const stderrLines = stderr.split(os.EOL);
83+
assert.strictEqual(stderrLines[0], '[Error: Not a raw device]');
84+
assert.strictEqual(stderrLines.length, 2);
85+
});

0 commit comments

Comments
 (0)