'posix_spawn and non-standard pipe setup for ipc between parent and child process
The file actions of posix_spawn describe the setup code for the child process before it might run execve (which deletes stack etc).
The intended use case is to have one or more additional pipes from the child process to the parent process as separate channel, which does not get potentially polluted from stderr or stdout logs by the child process.
The user code in the child process should be able to use the separate pipe handles as dedicated communication channels to the parent, even with exec
(and not only functions getting executed).
We want to close one pipe end to prevent unbounded reads waiting for input and thus must dup()
it without providing the file descriptor, because any chosen file descriptor might be used for another operations between parent and child (no CLOEXEC
and co set).
As I understand it, this leaves only 2 options during child process setup (after clone before execve
):
- store environment variables during child process setup
- add file descriptors to its own stdin for later parsing/using in user code
Does posix_spawn specify actions to 1. modify environment variables or 2. apply write operations during child process setup? If the answer to both is no: Is there a portable way to hack around the problem ie with function pointers? Do you see any flawed assumptions or workarounds how to simplify the solution?
Solution 1:[1]
posix_spawn
takes a pointer to the desired child environment as an argument. If you want to use the parent's environment you can just passenviron
here, but there's no obligation to do so. If you want to pass a modified version of it, you have to construct that in the parent before callingposix_spawn
and pass a pointer to that.There is no such thing as "adding file descriptors to stdin" regardless of whether you're using
posix_spawn
or any other mechanisms. stdin is a single file descriptor (0) which refers to a single open file description. I don't understand what you're trying to achieve exactly, but the normal usage pattern is that you setup a pipe in the parent, then use dup2 file actions (passed as an argument toposix_spawn
) in order to put the read end of that pipe on the child's fd 0. The write end can then be written from the parent, or passed off to one or more other processes to let them write to it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | R.. GitHub STOP HELPING ICE |