diff options
Diffstat (limited to 'src/libstd/sys/unix/ext/process.rs')
| -rw-r--r-- | src/libstd/sys/unix/ext/process.rs | 70 |
1 files changed, 68 insertions, 2 deletions
diff --git a/src/libstd/sys/unix/ext/process.rs b/src/libstd/sys/unix/ext/process.rs index 212aeb0406e..fa19a2620ba 100644 --- a/src/libstd/sys/unix/ext/process.rs +++ b/src/libstd/sys/unix/ext/process.rs @@ -12,8 +12,11 @@ #![stable(feature = "rust1", since = "1.0.0")] -use os::unix::raw::{uid_t, gid_t}; +use prelude::v1::*; + +use io; use os::unix::io::{FromRawFd, RawFd, AsRawFd, IntoRawFd}; +use os::unix::raw::{uid_t, gid_t}; use process; use sys; use sys_common::{AsInnerMut, AsInner, FromInner, IntoInner}; @@ -44,6 +47,56 @@ pub trait CommandExt { #[unstable(feature = "process_session_leader", reason = "recently added", issue = "27811")] fn session_leader(&mut self, on: bool) -> &mut process::Command; + + /// Schedules a closure to be run just before the `exec` function is + /// invoked. + /// + /// The closure is allowed to return an I/O error whose OS error code will + /// be communicated back to the parent and returned as an error from when + /// the spawn was requested. + /// + /// Multiple closures can be registered and they will be called in order of + /// their registration. If a closure returns `Err` then no further closures + /// will be called and the spawn operation will immediately return with a + /// failure. + /// + /// # Notes + /// + /// This closure will be run in the context of the child process after a + /// `fork`. This primarily means that any modificatons made to memory on + /// behalf of this closure will **not** be visible to the parent process. + /// This is often a very constrained environment where normal operations + /// like `malloc` or acquiring a mutex are not guaranteed to work (due to + /// other threads perhaps still running when the `fork` was run). + /// + /// When this closure is run, aspects such as the stdio file descriptors and + /// working directory have successfully been changed, so output to these + /// locations may not appear where intended. + #[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")] @@ -62,6 +115,17 @@ impl CommandExt for process::Command { self.as_inner_mut().session_leader(on); self } + + fn before_exec<F>(&mut self, f: F) -> &mut process::Command + where F: FnMut() -> io::Result<()> + Send + Sync + 'static + { + 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` @@ -82,7 +146,9 @@ impl ExitStatusExt for process::ExitStatus { #[stable(feature = "process_extensions", since = "1.2.0")] impl FromRawFd for process::Stdio { unsafe fn from_raw_fd(fd: RawFd) -> process::Stdio { - process::Stdio::from_inner(sys::fd::FileDesc::new(fd)) + let fd = sys::fd::FileDesc::new(fd); + let io = sys::process::Stdio::Fd(fd); + process::Stdio::from_inner(io) } } |
