Fork & vfork in rust

How to share file descriptors between parent and child processes. I looked thru the std::process and std::os::unix::process, but failed to find any methods. Should I call libc::fork() or any methods can achieve that in rust ? Why std::process doesn't provide fork() calls ?

thanks,
-minskey

You can get the file descriptor of your streams with std::os::unix::io::AsRawFd, call libc::fcntl to turn off FD_CLOEXEC, and then create a stream in your child process with std::os::unix::io::AsRawFd.

thank u for the info.

In c programming, to share fd between processes, we could via fork() or sendmsg() with deep copy. I tried, libc and nix crate can do that, but not sure if it is the right way in RUST. Besides, it seems that unix/linux signals would have to be handled via libc or libc-based nix crate. Or is there rust native way ?

First of all a little high-level view. Rust doesn't have much of a runtime therefore it doesn't much care for what you do with forks and file descriptors. It provides some utility functions for the common use cases, like the stuff in std::process, but if you need something special, it won't stand in your way.

Second, I'd try looking at this extension for Command (available only on unix systems) https://doc.rust-lang.org/std/os/unix/process/trait.CommandExt.html. You could maybe do the dance from @jethrogb in the pre_exec hook (prepare them there and then first thing off put them into the right places by some dup2 call in the child). Then you can reuse the whole infrastructure around Command to wait for the child, etc.

If you do want to touch signals, I wrote the signal-hook crate to make them a little bit easier to work with. It tries to have more rusty interface, but I don't think there's anything directly in std.

Got it, thank u very much !

-minskey