Jack O'Connor
Jack O'Connor

Reputation: 10876

Any way to give a file descriptor to a child process, without closing it?

The Stdio type implements FromRawFd, which lets me build one out of any file descriptor. (In my case, I want to use pipes.) That's exactly what I need, but my problem is that the stdin()/stdout()/stderr() methods take their Stdio argument by value. That means that when the Command object goes out of scope, all its fd's get closed. Is there any way to give an fd to a child process sort of by reference, so that it's still available in the parent process after the child is done? Right now I've settled for just calling libc::dup() for each child, which doesn't seem great.

Upvotes: 4

Views: 647

Answers (1)

llogiq
llogiq

Reputation: 14511

There is currently no better solution, alas. However, the correct solution would be a Command::into_io(self) -> (Option<StdIo>, Option<StdIo>, Option<StdIo>) method that deconstructs the Command to return stdin, stdout and stderr, if available.

I've filed an issue to add that function.

Upvotes: 2

Related Questions