-
Notifications
You must be signed in to change notification settings - Fork 7.3k
child_process: describe how to open fd in child #8624
Conversation
That's wrong. You can open a socket on an arbitrary fd, but then you have TWO streams open on the same fd, which is not a good idea. I'm assuming you found this necessary, but what was the problem you tried to solve by doing this? |
I don't completely understand your point. I'm trying to read data from the pipe in the child. What is the recommended way of doing this? The docs only state how to read/write from the parent side (using stdio), but not how to read/write from the child side of the pipe. I would like to send over some BSON data in a privilege separated server I'm building. |
I see. fd 0 is stdin, fd 1 is stdout, fd 2 is stderr. In the child, that is process.stdin, process.stdout, process.stderr, respectively. In the parent it is the equivalent properties on the ChildProcess object, as you saw documented. |
Yes, well I knew about stdin/out/err, but I'm talking about pipes that are requested by the user. So for example in my server I have something like:
So my question was, how would I open fd 4 in the child (to receive some data written by the parent). According to @bnoordhuis this should practically be done with net.Socket. See also: https://groups.google.com/forum/#!topic/nodejs/FPHhTo9ywE4 |
As to your docs, specifically, they are misleading, they need to make it abundantly clear that doing this for fds 0-2 is unnecessary and almost certainly harmful. Also, if http://nodejs.org/api/net.html#net_new_net_socket_options is to be believed, your example shows a socket being created that is neither readable nor writeable. I suggest you amend your text to acknowledge the specialness of stdio, and merely link to the net.Socket documentation as a possible way of working with non-stdio fds. Btw, your question:
was unanswered. The answer is 'yes' if you set the 'silent' option to true, but not if you silent to false. Its not clear from your post why you wouldn't send BSON using |
"Its not clear from your post why you wouldn't send BSON using ChildProcess.send() or ChildProcess.stdin.write(), which are conveniently set up for you by the ChildProcess for just that kind of use, but your IPC arrangement is up to you, I guess." You're right, I think ChildProcess.stdin.write() would be a good contender in my specific case. Apart from that I didn't realize I could mix ChildProcess.send() with non-JSON messages. I'm not sure if that would affect performance but I think from a code readability point of view splitting my JSON "command" channel from the BSON "data" channel makes things more clear and easier to maintain. Still, documenting how to open fds > 2 is a good thing imho. I've used your advice and amended the commit. |
ChildProcess.stderr, respectively. The parent end of all pipes is exposed to | ||
the parent as a property on the `child_process` object as | ||
`ChildProcess.stdio[fd]`. Fd's greater than 2 can be opened in the child | ||
using a new [net.Socket][]. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You have typos in the above, link markdown that doesn't work, use of ChildProcess.stdin when you mean child's process.stdin, and describing a fd as being "opened", which is not correct.
But your essential point is valid, and the docs are terrible, I've addressed your specific concern and others in #8639
tnx |
Its common knowledge on unix, but node documentation depends on knowing this, as it exposes both streams named after stdio, and the fd numbers, so make this explicit. Fixes: #8624 PR-URL: #8454 Reviewed-by: Trevor Norris <[email protected]>
Its common knowledge on unix, but node documentation depends on knowing this, as it exposes both streams named after stdio, and the fd numbers, so make this explicit. Fixes: nodejs#8624 PR-URL: nodejs#8454 Reviewed-by: Trevor Norris <[email protected]>
Its common knowledge on unix, but node documentation depends on knowing this, as it exposes both streams named after stdio, and the fd numbers, so make this explicit. Fixes: nodejs/node-v0.x-archive#8624 PR-URL: nodejs/node-v0.x-archive#8454 Reviewed-by: Trevor Norris <[email protected]> Cherry-picked-from: nodejs/node-v0.x-archive@13a992b
clarify how the child end of a pipe should be opened.
discussion: https://groups.google.com/forum/#!topic/nodejs/F4m1K8MIqkw