about summary refs log tree commit diff
diff options
context:
space:
mode:
authorSimonas Kazlauskas <git@kazlauskas.me>2016-12-21 21:42:07 +0200
committerSimonas Kazlauskas <git@kazlauskas.me>2017-01-19 21:11:32 +0200
commitc2eab73788a066384f3d1facca1ca7b9fc214962 (patch)
treebeb3b3684a0d68360fe8e017537ba4f6996acbbb
parent3dcb28842048ad51394f05473d1f9fb9ed8d143a (diff)
downloadrust-c2eab73788a066384f3d1facca1ca7b9fc214962.tar.gz
rust-c2eab73788a066384f3d1facca1ca7b9fc214962.zip
Expand documentation of process::exit and exec
Show a conventional way to use process::exit when destructors are considered important and also
mention that the same caveats wrt destructors apply to exec as well.
-rw-r--r--src/libstd/process.rs21
-rw-r--r--src/libstd/sys/unix/ext/process.rs10
2 files changed, 29 insertions, 2 deletions
diff --git a/src/libstd/process.rs b/src/libstd/process.rs
index aa76b792535..011d4d81b89 100644
--- a/src/libstd/process.rs
+++ b/src/libstd/process.rs
@@ -924,10 +924,27 @@ impl Child {
 ///
 /// # Examples
 ///
+/// Due to this function’s behavior regarding destructors, a conventional way
+/// to use the function is to extract the actual computation to another
+/// function and compute the exit code from its return value:
+///
 /// ```
-/// use std::process;
+/// use std::io::{self, Write};
+///
+/// fn run_app() -> Result<(), ()> {
+///     // Application logic here
+///     Ok(())
+/// }
 ///
-/// process::exit(0);
+/// fn main() {
+///     ::std::process::exit(match run_app() {
+///        Ok(_) => 0,
+///        Err(err) => {
+///            writeln!(io::stderr(), "error: {:?}", err).unwrap();
+///            1
+///        }
+///     });
+/// }
 /// ```
 ///
 /// Due to [platform-specific behavior], the exit code for this example will be
diff --git a/src/libstd/sys/unix/ext/process.rs b/src/libstd/sys/unix/ext/process.rs
index 585dcbb9a34..2961c4ec582 100644
--- a/src/libstd/sys/unix/ext/process.rs
+++ b/src/libstd/sys/unix/ext/process.rs
@@ -67,10 +67,20 @@ pub trait CommandExt {
     /// an error indicating why the exec (or another part of the setup of the
     /// `Command`) failed.
     ///
+    /// `exec` not returning has the same implications as calling
+    /// [`process::exit`] – no destructors on the current stack or any other
+    /// thread’s stack will be run. Therefore, it is recommended to only call
+    /// `exec` at a point where it is fine to not run any destructors. Note,
+    /// that the `execvp` syscall independently guarantees that all memory is
+    /// freed and all file descriptors with the `CLOEXEC` option (set by default
+    /// on all file descriptors opened by the standard library) are closed.
+    ///
     /// This function, unlike `spawn`, will **not** `fork` the process to create
     /// a new child. Like spawn, however, the default behavior for the stdio
     /// descriptors will be to inherited from the current process.
     ///
+    /// [`process::exit`]: ../../../process/fn.exit.html
+    ///
     /// # Notes
     ///
     /// The process may be in a "broken state" if this function returns in