Skip to content

Commit a6cd121

Browse files
Merge #1462
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 <[email protected]>
2 parents 5dd14c3 + b410397 commit a6cd121

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/unistd.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -200,14 +200,19 @@ impl ForkResult {
200200
/// be created that are identical with the exception of their pid and the
201201
/// return value of this function. As an example:
202202
///
203-
/// ```no_run
204-
/// use nix::unistd::{fork, ForkResult};
203+
/// ```
204+
/// use nix::{sys::wait::waitpid,unistd::{fork, ForkResult, write}};
205205
///
206206
/// match unsafe{fork()} {
207207
/// Ok(ForkResult::Parent { child, .. }) => {
208208
/// println!("Continuing execution in parent process, new child has pid: {}", child);
209+
/// waitpid(child, None).unwrap();
210+
/// }
211+
/// Ok(ForkResult::Child) => {
212+
/// // Unsafe to use `println!` (or `unwrap`) here. See Safety.
213+
/// write(libc::STDOUT_FILENO, "I'm a new child process\n".as_bytes()).ok();
214+
/// unsafe { libc::_exit(0) };
209215
/// }
210-
/// Ok(ForkResult::Child) => println!("I'm a new child process"),
211216
/// Err(_) => println!("Fork failed"),
212217
/// }
213218
/// ```

0 commit comments

Comments
 (0)