Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

child_process: describe how to open fd in child #8624

Closed
wants to merge 1 commit into from
Closed

child_process: describe how to open fd in child #8624

wants to merge 1 commit into from

Conversation

timkuijsten
Copy link

clarify how the child end of a pipe should be opened.

discussion: https://groups.google.com/forum/#!topic/nodejs/F4m1K8MIqkw

@sam-github
Copy link

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?

@timkuijsten
Copy link
Author

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.

@sam-github
Copy link

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.

@timkuijsten
Copy link
Author

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:

var child = spawn('node', ['lib/oplog_handler'], {
  stdio: ['pipe', 'pipe', 'pipe', 'ipc', 'pipe']
});

child.stdio[4].write(/* some Buffers */);

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

@sam-github
Copy link

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:

1. is the following equivalent to `fork('lib/foo')`? 

     spawn('node', ['lib/foo'], { 
       stdio: ['pipe', 'pipe', 'pipe', 'ipc'] 
     }); 

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 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.

@timkuijsten
Copy link
Author

"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][].

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

@timkuijsten
Copy link
Author

tnx

trevnorris pushed a commit that referenced this pull request Nov 19, 2014
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]>
mscdex pushed a commit to mscdex/node that referenced this pull request Dec 25, 2014
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]>
piscisaureus pushed a commit to piscisaureus/node2 that referenced this pull request Jan 10, 2015
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
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants