Skip to content
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

Add new inherit_handles flag to CommandExt trait #115501

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions library/std/src/os/windows/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]: <https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw#remarks>
#[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")]
Expand Down Expand Up @@ -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")]
Expand Down
9 changes: 8 additions & 1 deletion library/std/src/sys/windows/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ pub struct Command {
stderr: Option<Stdio>,
force_quotes_enabled: bool,
proc_thread_attributes: BTreeMap<usize, ProcThreadAttributeValue>,
inherit_handles: bool,
}

pub enum Stdio {
Expand Down Expand Up @@ -198,6 +199,7 @@ impl Command {
stderr: None,
force_quotes_enabled: false,
proc_thread_attributes: Default::default(),
inherit_handles: true,
}
}

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -362,7 +369,7 @@ impl Command {
cmd_str.as_mut_ptr(),
ptr::null_mut(),
ptr::null_mut(),
c::TRUE,
inherit_handles,
flags,
envp,
dirp,
Expand Down
Loading