Skip to content

Commit

Permalink
child_process: set stdin properties when execed
Browse files Browse the repository at this point in the history
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
  • Loading branch information
thefourtheye committed Aug 9, 2015
1 parent 2db57bd commit 5d70af8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,12 @@
stdin._handle.readStop();
});

stdin.isTTY = true;
stdin.isRaw = false;
stdin.setRawMode = function setRawMode() {
throw new Error('Not a raw device');
};

return stdin;
});

Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/child-process-stdin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
console.log(process.stdin.isTTY);
console.log(process.stdin.isRaw);
try {
process.stdin.setRawMode();
} catch (ex) {
console.error(ex);
}
19 changes: 18 additions & 1 deletion test/parallel/test-child-process-stdin.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
'use strict';
var common = require('../common');
var assert = require('assert');

const os = require('os');
const path = require('path');
var spawn = require('child_process').spawn;
const exec = require('child_process').exec;

var cat = spawn(common.isWindows ? 'more' : 'cat');
cat.stdin.write('hello');
Expand Down Expand Up @@ -66,3 +68,18 @@ process.on('exit', function() {
assert.equal('hello world', response);
}
});

// Regression test for https://github.com/nodejs/io.js/issues/2333
const cpFile = path.join(common.fixturesDir, 'child-process-stdin.js');
const nodeBinary = process.argv[0];

exec(`${nodeBinary} ${cpFile}`, function(err, stdout, stderr) {
const stdoutLines = stdout.split(os.EOL);
assert.strictEqual(stdoutLines[0], 'true');
assert.strictEqual(stdoutLines[1], 'false');
assert.strictEqual(stdoutLines.length, 3);

const stderrLines = stderr.split(os.EOL);
assert.strictEqual(stderrLines[0], '[Error: Not a raw device]');
assert.strictEqual(stderrLines.length, 2);
});

0 comments on commit 5d70af8

Please sign in to comment.