Skip to content

Commit

Permalink
Merge #1462
Browse files Browse the repository at this point in the history
1462: Fix fork test and enable doc test r=asomers a=sporksmith

Replaces `println!`  with raw `libc::write`. `println!` isn't guaranteed
to be async-signal-safe, and almost certainly *isn't* due to internal
buffering and locking.

Adds a call to `libc::_exit` in the child arm, so that it doesn't fall
through and start executing the parent code.

Adds a call to `waitpid` in the parent arm, to clean up the child
process.

Removes the `no_run` directive, so that it's run in the doc tests.

Co-authored-by: Jim Newsome <jnewsome@torproject.org>
  • Loading branch information
bors[bot] and sporksmith authored Jul 10, 2021
2 parents 5dd14c3 + b410397 commit a6cd121
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,19 @@ impl ForkResult {
/// be created that are identical with the exception of their pid and the
/// return value of this function. As an example:
///
/// ```no_run
/// use nix::unistd::{fork, ForkResult};
/// ```
/// use nix::{sys::wait::waitpid,unistd::{fork, ForkResult, write}};
///
/// match unsafe{fork()} {
/// Ok(ForkResult::Parent { child, .. }) => {
/// println!("Continuing execution in parent process, new child has pid: {}", child);
/// waitpid(child, None).unwrap();
/// }
/// Ok(ForkResult::Child) => {
/// // Unsafe to use `println!` (or `unwrap`) here. See Safety.
/// write(libc::STDOUT_FILENO, "I'm a new child process\n".as_bytes()).ok();
/// unsafe { libc::_exit(0) };
/// }
/// Ok(ForkResult::Child) => println!("I'm a new child process"),
/// Err(_) => println!("Fork failed"),
/// }
/// ```
Expand Down

0 comments on commit a6cd121

Please sign in to comment.