Skip to content

Commit 94c934a

Browse files
Add new inherit_handles flag to CommandExt trait
This patch adds a new flag to the [`CommandExt`](1) trait to set whether to inherit the handles of the calling process (2) on Windows systems. ACP: rust-lang/libs-team#264 [1]: https://doc.rust-lang.org/stable/std/os/windows/process/trait.CommandExt.html [2]: https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw
1 parent 030ec9a commit 94c934a

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

std/src/os/windows/process.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,20 @@ pub trait CommandExt: Sealed {
365365
/// [1]: https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/ns-processthreadsapi-startupinfoa
366366
#[unstable(feature = "windows_process_extensions_startupinfo", issue = "141010")]
367367
fn startupinfo_force_feedback(&mut self, enabled: Option<bool>) -> &mut process::Command;
368+
369+
/// If this flag is set to `true`, each inheritable handle in the calling
370+
/// process is inherited by the new process. If the flag is `false`, the
371+
/// handles are not inherited.
372+
///
373+
/// The default value for this flag is `true`.
374+
///
375+
/// **Note** that inherited handles have the same value and access rights
376+
/// as the original handles. For additional discussion of inheritable handles,
377+
/// see the [Remarks][1] section of the `CreateProcessW` documentation.
378+
///
379+
/// [1]: https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw#remarks
380+
#[unstable(feature = "windows_process_extensions_inherit_handles", issue = "146407")]
381+
fn inherit_handles(&mut self, inherit_handles: bool) -> &mut process::Command;
368382
}
369383

370384
#[stable(feature = "windows_process_extensions", since = "1.16.0")]
@@ -421,6 +435,11 @@ impl CommandExt for process::Command {
421435
self.as_inner_mut().startupinfo_force_feedback(enabled);
422436
self
423437
}
438+
439+
fn inherit_handles(&mut self, inherit_handles: bool) -> &mut process::Command {
440+
self.as_inner_mut().inherit_handles(inherit_handles);
441+
self
442+
}
424443
}
425444

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

std/src/sys/process/windows.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ pub struct Command {
159159
startupinfo_fullscreen: bool,
160160
startupinfo_untrusted_source: bool,
161161
startupinfo_force_feedback: Option<bool>,
162+
inherit_handles: bool,
162163
}
163164

164165
pub enum Stdio {
@@ -187,6 +188,7 @@ impl Command {
187188
startupinfo_fullscreen: false,
188189
startupinfo_untrusted_source: false,
189190
startupinfo_force_feedback: None,
191+
inherit_handles: true,
190192
}
191193
}
192194

@@ -252,6 +254,10 @@ impl Command {
252254
self.cwd.as_ref().map(Path::new)
253255
}
254256

257+
pub fn inherit_handles(&mut self, inherit_handles: bool) {
258+
self.inherit_handles = inherit_handles;
259+
}
260+
255261
pub fn spawn(
256262
&mut self,
257263
default: Stdio,
@@ -310,6 +316,7 @@ impl Command {
310316
flags |= c::DETACHED_PROCESS | c::CREATE_NEW_PROCESS_GROUP;
311317
}
312318

319+
let inherit_handles = self.inherit_handles as c::BOOL;
313320
let (envp, _data) = make_envp(maybe_env)?;
314321
let (dirp, _data) = make_dirp(self.cwd.as_ref())?;
315322
let mut pi = zeroed_process_information();
@@ -401,7 +408,7 @@ impl Command {
401408
cmd_str.as_mut_ptr(),
402409
ptr::null_mut(),
403410
ptr::null_mut(),
404-
c::TRUE,
411+
inherit_handles,
405412
flags,
406413
envp,
407414
dirp,

0 commit comments

Comments
 (0)