summaryrefslogtreecommitdiff
path: root/src/unistd.rs
diff options
context:
space:
mode:
authorJim Newsome <jnewsome@torproject.org>2021-07-09 10:29:52 -0500
committerJim Newsome <jnewsome@torproject.org>2021-07-09 10:37:12 -0500
commitb4103976d19da366391a78b93a6453e7117671e6 (patch)
tree043d7ec9ef021322af26d6cc78986d064876b23e /src/unistd.rs
parent5dd14c39b88fb6eb25ad670435d1a79ba294b21e (diff)
downloadnix-b4103976d19da366391a78b93a6453e7117671e6.zip
Fix fork test and enable doc test
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.
Diffstat (limited to 'src/unistd.rs')
-rw-r--r--src/unistd.rs11
1 files changed, 8 insertions, 3 deletions
diff --git a/src/unistd.rs b/src/unistd.rs
index de3b0490..268c9994 100644
--- a/src/unistd.rs
+++ b/src/unistd.rs
@@ -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"),
/// }
/// ```