diff --git a/library/std/src/os/windows/process.rs b/library/std/src/os/windows/process.rs index 5d87216ff1b4c..e02df2d95df2d 100644 --- a/library/std/src/os/windows/process.rs +++ b/library/std/src/os/windows/process.rs @@ -252,6 +252,17 @@ pub trait CommandExt: Sealed { attribute: usize, value: T, ) -> &mut process::Command; + + /// If this flag is set to `true`, each inheritable handle in the calling process is inherited by the new process. + /// If the flag is `false`, the handles are not inherited. + /// + /// The default value for this flag is `true`. + /// + /// **Note** that inherited handles have the same value and access rights as the original handles. For additional discussion of inheritable handles, see [Remarks][1]. + /// + /// [1]: + #[unstable(feature = "windows_process_extensions_inherit_handles", issue = "")] + fn inherit_handles(&mut self, inherit_handles: bool) -> &mut process::Command; } #[stable(feature = "windows_process_extensions", since = "1.16.0")] @@ -288,6 +299,11 @@ impl CommandExt for process::Command { self.as_inner_mut().raw_attribute(attribute, value); self } + + fn inherit_handles(&mut self, inherit_handles: bool) -> &mut process::Command { + self.as_inner_mut().inherit_handles(inherit_handles); + self + } } #[unstable(feature = "windows_process_extensions_main_thread_handle", issue = "96723")] diff --git a/library/std/src/sys/windows/process.rs b/library/std/src/sys/windows/process.rs index 6f46da1748a70..afae45b0c64c5 100644 --- a/library/std/src/sys/windows/process.rs +++ b/library/std/src/sys/windows/process.rs @@ -168,6 +168,7 @@ pub struct Command { stderr: Option, force_quotes_enabled: bool, proc_thread_attributes: BTreeMap, + inherit_handles: bool, } pub enum Stdio { @@ -198,6 +199,7 @@ impl Command { stderr: None, force_quotes_enabled: false, proc_thread_attributes: Default::default(), + inherit_handles: true, } } @@ -259,6 +261,10 @@ impl Command { ); } + pub fn inherit_handles(&mut self, inherit_handles: bool) { + self.inherit_handles = inherit_handles; + } + pub fn spawn( &mut self, default: Stdio, @@ -294,6 +300,7 @@ impl Command { flags |= c::DETACHED_PROCESS | c::CREATE_NEW_PROCESS_GROUP; } + let inherit_handles = self.inherit_handles as c::BOOL; let (envp, _data) = make_envp(maybe_env)?; let (dirp, _data) = make_dirp(self.cwd.as_ref())?; let mut pi = zeroed_process_information(); @@ -362,7 +369,7 @@ impl Command { cmd_str.as_mut_ptr(), ptr::null_mut(), ptr::null_mut(), - c::TRUE, + inherit_handles, flags, envp, dirp,