Created
September 19, 2012 19:37
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ node cat_test.js | |
exit code 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ node spidertest.js | |
exit code 1 | |
child stderr: cat: /proc/self/fd/0: No such device or address |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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