-
Notifications
You must be signed in to change notification settings - Fork 681
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
mark fork as unsafe #1047
mark fork as unsafe #1047
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -185,14 +185,19 @@ impl ForkResult { | |
/// Create a new child process duplicating the parent process ([see | ||
/// fork(2)](http://pubs.opengroup.org/onlinepubs/9699919799/functions/fork.html)). | ||
/// | ||
/// After calling the fork system call (successfully) two processes will | ||
/// be created that are identical with the exception of their pid and the | ||
/// return value of this function. As an example: | ||
/// After calling the fork system call (successfully) a new process (child) will | ||
/// be created that is a copy of the calling process (parent). If the parent process is | ||
/// multithreaded, only the calling thread will be cloned. | ||
/// The child process differs in a few aspects from the parent process, most notably the pid and the | ||
/// return value of this function. For a list of all differences in the child and parent | ||
/// see the [man page]. | ||
/// | ||
/// As an example: | ||
/// | ||
/// ```no_run | ||
/// use nix::unistd::{fork, ForkResult}; | ||
/// | ||
/// match fork() { | ||
/// match unsafe {fork()} { | ||
/// Ok(ForkResult::Parent { child, .. }) => { | ||
/// println!("Continuing execution in parent process, new child has pid: {}", child); | ||
/// } | ||
|
@@ -210,21 +215,26 @@ impl ForkResult { | |
/// I'm a new child process | ||
/// ``` | ||
/// | ||
/// | ||
/// # Safety | ||
/// Make sure to read the [man page]. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I consider that RTFM goes without saying. You can remove this sentence. |
||
/// Fork has some properties that can lead to unintended behavior. | ||
/// Especially the handling of mutexes and open files can be tricky. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sentence fragment. |
||
/// | ||
/// In a multithreaded program, only [async-signal-safe] functions like `pause` | ||
/// and `_exit` may be called by the child (the parent isn't restricted). Note | ||
/// that memory allocation may **not** be async-signal-safe and thus must be | ||
/// and `_exit` are safe to call by the child until it calls `execve`(the parent isn't restricted). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Technically, there are other functions that can take the place of |
||
/// Note that memory allocation may **not** be async-signal-safe and thus must be | ||
/// prevented. | ||
/// | ||
/// Those functions are only a small subset of your operating system's API, so | ||
/// special care must be taken to only invoke code you can control and audit. | ||
/// | ||
/// [async-signal-safe]: http://man7.org/linux/man-pages/man7/signal-safety.7.html | ||
/// [man page]: http://man7.org/linux/man-pages/man2/fork.2.html | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For standardized functions, it's better to use the open group's man page: http://pubs.opengroup.org/onlinepubs/9699919799/functions/fork.html |
||
#[inline] | ||
pub fn fork() -> Result<ForkResult> { | ||
pub unsafe fn fork() -> Result<ForkResult> { | ||
use self::ForkResult::*; | ||
let res = unsafe { libc::fork() }; | ||
let res = libc::fork(); | ||
|
||
Errno::result(res).map(|res| match res { | ||
0 => Child, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should add a section to the docs describing how to use
fork
safely, like this: