Skip to content

Instantly share code, notes, and snippets.

@glasser
Created September 19, 2012 19:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glasser/3751746 to your computer and use it in GitHub Desktop.
Save glasser/3751746 to your computer and use it in GitHub Desktop.
Node 0.8 child_processes can't use /dev/stdin or /proc/self/fd/N
var spawn = require('child_process').spawn;
var cp = spawn('cat', ['/proc/self/fd/0']);
cp.stdout.setEncoding('utf8');
cp.stdout.on('data', function (chunk) {
console.log("child stdout: ", chunk);
});
cp.stderr.setEncoding('utf8');
cp.stderr.on('data', function (chunk) {
console.log("child stderr: ", chunk);
});
cp.on('exit', function (code) {
console.log("exit code", code);
});
cp.stdin.end();
$ node cat_test.js
exit code 0
$ node spidertest.js
exit code 1
child stderr: cat: /proc/self/fd/0: No such device or address
#include <sys/types.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <stdio.h>
#include <unistd.h>
int main(int argc, char** argv) {
int pipefd[2];
int fd;
/*if (socketpair(AF_UNIX, SOCK_STREAM, 0, pipefd) != 0) {
perror("socketpair"); return 1;
}*/
if (pipe2(pipefd, O_CLOEXEC) != 0) {
perror("pipe2"); return 1;
}
if (dup2(pipefd[0], 0) == -1) {
perror("dup2"); return 1;
}
fd = open("/proc/self/fd/0", O_RDONLY);
if (fd == -1) {
perror("open"); return 1;
} else {
printf("opened %d\n", fd);
return 0;
}
}
Node 0.8 changes libuv to use socketpair instead of pipe2 to create the pipe
between parent and child.
See https://github.com/joyent/libuv/commit/c0081f0e6675131721dbb5138fd398792a8c2163
But when you use socketpair, this breaks the use of /dev/stdin or /proc/self/fd/0 (etc).
See even just the C file below: if you replace the pipe2 call with the socketpair call, the open fails.
Node 0.8 changes libuv to use socketpair instead of pipe2 to create the pipe between parent and child. See https://github.com/joyent/libuv/commit/c0081f0e6675131721dbb5138fd398792a8c2163
But when you use socketpair, this breaks the use of /dev/stdin or /proc/self/fd/0 (etc).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment