Skip to content

Commit 5e180a5

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 364da5d commit 5e180a5

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
@@ -365,6 +365,17 @@ 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 process is inherited by the new process.
370+
/// If the flag is `false`, the handles are not inherited.
371+
///
372+
/// The default value for this flag is `true`.
373+
///
374+
/// **Note** that inherited handles have the same value and access rights as the original handles. For additional discussion of inheritable handles, see [Remarks][1].
375+
///
376+
/// [1]: <https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw#remarks>
377+
#[unstable(feature = "windows_process_extensions_inherit_handles", issue = "146407")]
378+
fn inherit_handles(&mut self, inherit_handles: bool) -> &mut process::Command;
368379
}
369380

370381
#[stable(feature = "windows_process_extensions", since = "1.16.0")]
@@ -421,6 +432,11 @@ impl CommandExt for process::Command {
421432
self.as_inner_mut().startupinfo_force_feedback(enabled);
422433
self
423434
}
435+
436+
fn inherit_handles(&mut self, inherit_handles: bool) -> &mut process::Command {
437+
self.as_inner_mut().inherit_handles(inherit_handles);
438+
self
439+
}
424440
}
425441

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

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

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

163164
pub enum Stdio {
@@ -192,6 +193,7 @@ impl Command {
192193
startupinfo_fullscreen: false,
193194
startupinfo_untrusted_source: false,
194195
startupinfo_force_feedback: None,
196+
inherit_handles: true,
195197
}
196198
}
197199

@@ -257,6 +259,10 @@ impl Command {
257259
self.cwd.as_ref().map(Path::new)
258260
}
259261

262+
pub fn inherit_handles(&mut self, inherit_handles: bool) {
263+
self.inherit_handles = inherit_handles;
264+
}
265+
260266
pub fn spawn(
261267
&mut self,
262268
default: Stdio,
@@ -315,6 +321,7 @@ impl Command {
315321
flags |= c::DETACHED_PROCESS | c::CREATE_NEW_PROCESS_GROUP;
316322
}
317323

324+
let inherit_handles = self.inherit_handles as c::BOOL;
318325
let (envp, _data) = make_envp(maybe_env)?;
319326
let (dirp, _data) = make_dirp(self.cwd.as_ref())?;
320327
let mut pi = zeroed_process_information();
@@ -406,7 +413,7 @@ impl Command {
406413
cmd_str.as_mut_ptr(),
407414
ptr::null_mut(),
408415
ptr::null_mut(),
409-
c::TRUE,
416+
inherit_handles,
410417
flags,
411418
envp,
412419
dirp,

0 commit comments

Comments
 (0)