about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2016-02-04 11:16:32 -0800
committerAlex Crichton <alex@alexcrichton.com>2016-02-10 09:28:49 -0800
commitb37477c03e7683cc67273ddc5506496a7b03971c (patch)
tree703095c3ab93f0de9be7ee7d40898a7221f21c40 /src/libstd
parentd15db1d392c9126ed5cc766753f08540c08a3626 (diff)
downloadrust-b37477c03e7683cc67273ddc5506496a7b03971c.tar.gz
rust-b37477c03e7683cc67273ddc5506496a7b03971c.zip
std: Implement CommandExt::exec
This commit implements the `exec` function proposed in [RFC 1359][rfc] which is
a function on the `CommandExt` trait to execute all parts of a `Command::spawn`
without the `fork` on Unix. More details on the function itself can be found in
the comments in the commit.

[rfc]: https://github.com/rust-lang/rfcs/pull/1359

cc #31398
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/sys/unix/ext/process.rs26
-rw-r--r--src/libstd/sys/unix/process.rs16
2 files changed, 40 insertions, 2 deletions
diff --git a/src/libstd/sys/unix/ext/process.rs b/src/libstd/sys/unix/ext/process.rs
index cf72cfd7e50..fa19a2620ba 100644
--- a/src/libstd/sys/unix/ext/process.rs
+++ b/src/libstd/sys/unix/ext/process.rs
@@ -75,6 +75,28 @@ pub trait CommandExt {
     #[unstable(feature = "process_exec", issue = "31398")]
     fn before_exec<F>(&mut self, f: F) -> &mut process::Command
         where F: FnMut() -> io::Result<()> + Send + Sync + 'static;
+
+    /// Performs all the required setup by this `Command`, followed by calling
+    /// the `execvp` syscall.
+    ///
+    /// On success this function will not return, and otherwise it will return
+    /// an error indicating why the exec (or another part of the setup of the
+    /// `Command`) failed.
+    ///
+    /// 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.
+    ///
+    /// # Notes
+    ///
+    /// The process may be in a "broken state" if this function returns in
+    /// error. For example the working directory, environment variables, signal
+    /// handling settings, various user/group information, or aspects of stdio
+    /// file descriptors may have changed. If a "transactional spawn" is
+    /// required to gracefully handle errors it is recommended to use the
+    /// cross-platform `spawn` instead.
+    #[unstable(feature = "process_exec", issue = "31398")]
+    fn exec(&mut self) -> io::Error;
 }
 
 #[stable(feature = "rust1", since = "1.0.0")]
@@ -100,6 +122,10 @@ impl CommandExt for process::Command {
         self.as_inner_mut().before_exec(Box::new(f));
         self
     }
+
+    fn exec(&mut self) -> io::Error {
+        self.as_inner_mut().exec(sys::process::Stdio::Inherit)
+    }
 }
 
 /// Unix-specific extensions to `std::process::ExitStatus`
diff --git a/src/libstd/sys/unix/process.rs b/src/libstd/sys/unix/process.rs
index 4716d25c0b2..60785f37642 100644
--- a/src/libstd/sys/unix/process.rs
+++ b/src/libstd/sys/unix/process.rs
@@ -230,7 +230,7 @@ impl Command {
             match try!(cvt(libc::fork())) {
                 0 => {
                     drop(input);
-                    let err = self.exec(theirs);
+                    let err = self.do_exec(theirs);
                     let errno = err.raw_os_error().unwrap_or(libc::EINVAL) as u32;
                     let bytes = [
                         (errno >> 24) as u8,
@@ -290,6 +290,18 @@ impl Command {
         }
     }
 
+    pub fn exec(&mut self, default: Stdio) -> io::Error {
+        if self.saw_nul {
+            return io::Error::new(ErrorKind::InvalidInput,
+                                  "nul byte found in provided data")
+        }
+
+        match self.setup_io(default) {
+            Ok((_, theirs)) => unsafe { self.do_exec(theirs) },
+            Err(e) => e,
+        }
+    }
+
     // And at this point we've reached a special time in the life of the
     // child. The child must now be considered hamstrung and unable to
     // do anything other than syscalls really. Consider the following
@@ -320,7 +332,7 @@ impl Command {
     // allocation). Instead we just close it manually. This will never
     // have the drop glue anyway because this code never returns (the
     // child will either exec() or invoke libc::exit)
-    unsafe fn exec(&mut self, stdio: ChildPipes) -> io::Error {
+    unsafe fn do_exec(&mut self, stdio: ChildPipes) -> io::Error {
         macro_rules! try {
             ($e:expr) => (match $e {
                 Ok(e) => e,