Skip to content

Commit 8db45eb

Browse files
Added new inherit_handles flag to CommandExt trait
1 parent 9229b1e commit 8db45eb

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

library/std/src/os/windows/process.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,17 @@ pub trait CommandExt: Sealed {
252252
attribute: usize,
253253
value: T,
254254
) -> &mut process::Command;
255+
256+
/// If this flag is set to `true`, each inheritable handle in the calling process is inherited by the new process.
257+
/// If the flag is `false`, the handles are not inherited.
258+
///
259+
/// The default value for this flag is `true`.
260+
///
261+
/// **Note** that inherited handles have the same value and access rights as the original handles. For additional discussion of inheritable handles, see [Remarks][1].
262+
///
263+
/// [1]: <https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw#remarks>
264+
#[unstable(feature = "windows_process_extensions_inherit_handles", issue = "")]
265+
fn inherit_handles(&mut self, inherit_handles: bool) -> &mut process::Command;
255266
}
256267

257268
#[stable(feature = "windows_process_extensions", since = "1.16.0")]
@@ -288,6 +299,11 @@ impl CommandExt for process::Command {
288299
self.as_inner_mut().raw_attribute(attribute, value);
289300
self
290301
}
302+
303+
fn inherit_handles(&mut self, inherit_handles: bool) -> &mut process::Command {
304+
self.as_inner_mut().inherit_handles(inherit_handles);
305+
self
306+
}
291307
}
292308

293309
#[unstable(feature = "windows_process_extensions_main_thread_handle", issue = "96723")]

library/std/src/sys/windows/process.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ pub struct Command {
168168
stderr: Option<Stdio>,
169169
force_quotes_enabled: bool,
170170
proc_thread_attributes: BTreeMap<usize, ProcThreadAttributeValue>,
171+
inherit_handles: bool,
171172
}
172173

173174
pub enum Stdio {
@@ -198,6 +199,7 @@ impl Command {
198199
stderr: None,
199200
force_quotes_enabled: false,
200201
proc_thread_attributes: Default::default(),
202+
inherit_handles: true,
201203
}
202204
}
203205

@@ -259,6 +261,10 @@ impl Command {
259261
);
260262
}
261263

264+
pub fn inherit_handles(&mut self, inherit_handles: bool) {
265+
self.inherit_handles = inherit_handles;
266+
}
267+
262268
pub fn spawn(
263269
&mut self,
264270
default: Stdio,
@@ -294,6 +300,7 @@ impl Command {
294300
flags |= c::DETACHED_PROCESS | c::CREATE_NEW_PROCESS_GROUP;
295301
}
296302

303+
let inherit_handles = self.inherit_handles as c::BOOL;
297304
let (envp, _data) = make_envp(maybe_env)?;
298305
let (dirp, _data) = make_dirp(self.cwd.as_ref())?;
299306
let mut pi = zeroed_process_information();
@@ -362,7 +369,7 @@ impl Command {
362369
cmd_str.as_mut_ptr(),
363370
ptr::null_mut(),
364371
ptr::null_mut(),
365-
c::TRUE,
372+
inherit_handles,
366373
flags,
367374
envp,
368375
dirp,

0 commit comments

Comments
 (0)