std/os/windows/process.rs
1//! Windows-specific extensions to primitives in the [`std::process`] module.
2//!
3//! [`std::process`]: crate::process
4
5#![stable(feature = "process_extensions", since = "1.2.0")]
6
7use crate::ffi::{OsStr, c_void};
8use crate::mem::MaybeUninit;
9use crate::os::windows::io::{
10 AsHandle, AsRawHandle, BorrowedHandle, FromRawHandle, IntoRawHandle, OwnedHandle, RawHandle,
11};
12use crate::sys::{AsInner, AsInnerMut, FromInner, IntoInner};
13use crate::{io, marker, process, ptr, sys};
14
15#[stable(feature = "process_extensions", since = "1.2.0")]
16impl FromRawHandle for process::Stdio {
17 unsafe fn from_raw_handle(handle: RawHandle) -> process::Stdio {
18 let handle = unsafe { sys::handle::Handle::from_raw_handle(handle as *mut _) };
19 let io = sys::process::Stdio::Handle(handle);
20 process::Stdio::from_inner(io)
21 }
22}
23
24#[stable(feature = "io_safety", since = "1.63.0")]
25impl From<OwnedHandle> for process::Stdio {
26 /// Takes ownership of a handle and returns a [`Stdio`](process::Stdio)
27 /// that can attach a stream to it.
28 fn from(handle: OwnedHandle) -> process::Stdio {
29 let handle = sys::handle::Handle::from_inner(handle);
30 let io = sys::process::Stdio::Handle(handle);
31 process::Stdio::from_inner(io)
32 }
33}
34
35#[stable(feature = "process_extensions", since = "1.2.0")]
36impl AsRawHandle for process::Child {
37 #[inline]
38 fn as_raw_handle(&self) -> RawHandle {
39 self.as_inner().handle().as_raw_handle() as *mut _
40 }
41}
42
43#[stable(feature = "io_safety", since = "1.63.0")]
44impl AsHandle for process::Child {
45 #[inline]
46 fn as_handle(&self) -> BorrowedHandle<'_> {
47 self.as_inner().handle().as_handle()
48 }
49}
50
51#[stable(feature = "into_raw_os", since = "1.4.0")]
52impl IntoRawHandle for process::Child {
53 fn into_raw_handle(self) -> RawHandle {
54 self.into_inner().into_handle().into_raw_handle() as *mut _
55 }
56}
57
58#[stable(feature = "io_safety", since = "1.63.0")]
59impl From<process::Child> for OwnedHandle {
60 /// Takes ownership of a [`Child`](process::Child)'s process handle.
61 fn from(child: process::Child) -> OwnedHandle {
62 child.into_inner().into_handle().into_inner()
63 }
64}
65
66#[stable(feature = "process_extensions", since = "1.2.0")]
67impl AsRawHandle for process::ChildStdin {
68 #[inline]
69 fn as_raw_handle(&self) -> RawHandle {
70 self.as_inner().handle().as_raw_handle() as *mut _
71 }
72}
73
74#[stable(feature = "process_extensions", since = "1.2.0")]
75impl AsRawHandle for process::ChildStdout {
76 #[inline]
77 fn as_raw_handle(&self) -> RawHandle {
78 self.as_inner().handle().as_raw_handle() as *mut _
79 }
80}
81
82#[stable(feature = "process_extensions", since = "1.2.0")]
83impl AsRawHandle for process::ChildStderr {
84 #[inline]
85 fn as_raw_handle(&self) -> RawHandle {
86 self.as_inner().handle().as_raw_handle() as *mut _
87 }
88}
89
90#[stable(feature = "into_raw_os", since = "1.4.0")]
91impl IntoRawHandle for process::ChildStdin {
92 fn into_raw_handle(self) -> RawHandle {
93 self.into_inner().into_handle().into_raw_handle() as *mut _
94 }
95}
96
97#[stable(feature = "into_raw_os", since = "1.4.0")]
98impl IntoRawHandle for process::ChildStdout {
99 fn into_raw_handle(self) -> RawHandle {
100 self.into_inner().into_handle().into_raw_handle() as *mut _
101 }
102}
103
104#[stable(feature = "into_raw_os", since = "1.4.0")]
105impl IntoRawHandle for process::ChildStderr {
106 fn into_raw_handle(self) -> RawHandle {
107 self.into_inner().into_handle().into_raw_handle() as *mut _
108 }
109}
110
111/// Creates a `ChildStdin` from the provided `OwnedHandle`.
112///
113/// The provided handle must be asynchronous, as reading and
114/// writing from and to it is implemented using asynchronous APIs.
115#[stable(feature = "child_stream_from_fd", since = "1.74.0")]
116impl From<OwnedHandle> for process::ChildStdin {
117 fn from(handle: OwnedHandle) -> process::ChildStdin {
118 let handle = sys::handle::Handle::from_inner(handle);
119 let pipe = sys::process::ChildPipe::from_inner(handle);
120 process::ChildStdin::from_inner(pipe)
121 }
122}
123
124/// Creates a `ChildStdout` from the provided `OwnedHandle`.
125///
126/// The provided handle must be asynchronous, as reading and
127/// writing from and to it is implemented using asynchronous APIs.
128#[stable(feature = "child_stream_from_fd", since = "1.74.0")]
129impl From<OwnedHandle> for process::ChildStdout {
130 fn from(handle: OwnedHandle) -> process::ChildStdout {
131 let handle = sys::handle::Handle::from_inner(handle);
132 let pipe = sys::process::ChildPipe::from_inner(handle);
133 process::ChildStdout::from_inner(pipe)
134 }
135}
136
137/// Creates a `ChildStderr` from the provided `OwnedHandle`.
138///
139/// The provided handle must be asynchronous, as reading and
140/// writing from and to it is implemented using asynchronous APIs.
141#[stable(feature = "child_stream_from_fd", since = "1.74.0")]
142impl From<OwnedHandle> for process::ChildStderr {
143 fn from(handle: OwnedHandle) -> process::ChildStderr {
144 let handle = sys::handle::Handle::from_inner(handle);
145 let pipe = sys::process::ChildPipe::from_inner(handle);
146 process::ChildStderr::from_inner(pipe)
147 }
148}
149
150/// Windows-specific extensions to [`process::ExitStatus`].
151#[stable(feature = "exit_status_from", since = "1.12.0")]
152pub impl(self) trait ExitStatusExt {
153 /// Creates a new `ExitStatus` from the raw underlying `u32` return value of
154 /// a process.
155 #[stable(feature = "exit_status_from", since = "1.12.0")]
156 fn from_raw(raw: u32) -> Self;
157}
158
159#[stable(feature = "exit_status_from", since = "1.12.0")]
160impl ExitStatusExt for process::ExitStatus {
161 fn from_raw(raw: u32) -> Self {
162 process::ExitStatus::from_inner(From::from(raw))
163 }
164}
165
166/// Windows-specific extensions to the [`process::Command`] builder.
167#[stable(feature = "windows_process_extensions", since = "1.16.0")]
168pub impl(self) trait CommandExt {
169 /// Sets the [process creation flags][1] to be passed to `CreateProcess`.
170 ///
171 /// These will always be ORed with `CREATE_UNICODE_ENVIRONMENT`.
172 ///
173 /// [1]: https://docs.microsoft.com/en-us/windows/win32/procthread/process-creation-flags
174 #[stable(feature = "windows_process_extensions", since = "1.16.0")]
175 fn creation_flags(&mut self, flags: u32) -> &mut process::Command;
176
177 /// Sets the field `wShowWindow` of [STARTUPINFO][1] that is passed to `CreateProcess`.
178 /// Allowed values are the ones listed in
179 /// <https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow>
180 ///
181 /// [1]: <https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/ns-processthreadsapi-startupinfow>
182 #[unstable(feature = "windows_process_extensions_show_window", issue = "127544")]
183 fn show_window(&mut self, cmd_show: u16) -> &mut process::Command;
184
185 /// Forces all arguments to be wrapped in quote (`"`) characters.
186 ///
187 /// This is useful for passing arguments to [MSYS2/Cygwin][1] based
188 /// executables: these programs will expand unquoted arguments containing
189 /// wildcard characters (`?` and `*`) by searching for any file paths
190 /// matching the wildcard pattern.
191 ///
192 /// Adding quotes has no effect when passing arguments to programs
193 /// that use [msvcrt][2]. This includes programs built with both
194 /// MinGW and MSVC.
195 ///
196 /// [1]: <https://github.com/msys2/MSYS2-packages/issues/2176>
197 /// [2]: <https://msdn.microsoft.com/en-us/library/17w5ykft.aspx>
198 #[unstable(feature = "windows_process_extensions_force_quotes", issue = "82227")]
199 fn force_quotes(&mut self, enabled: bool) -> &mut process::Command;
200
201 /// Append literal text to the command line without any quoting or escaping.
202 ///
203 /// This is useful for passing arguments to applications that don't follow
204 /// the standard C run-time escaping rules, such as `cmd.exe /c`.
205 ///
206 /// # Batch files
207 ///
208 /// Note the `cmd /c` command line has slightly different escaping rules than batch files
209 /// themselves. If possible, it may be better to write complex arguments to a temporary
210 /// `.bat` file, with appropriate escaping, and simply run that using:
211 ///
212 /// ```no_run
213 /// # use std::process::Command;
214 /// # let temp_bat_file = "";
215 /// # #[allow(unused)]
216 /// let output = Command::new("cmd").args(["/c", &format!("\"{temp_bat_file}\"")]).output();
217 /// ```
218 ///
219 /// # Example
220 ///
221 /// Run a batch script using both trusted and untrusted arguments.
222 ///
223 /// ```no_run
224 /// #[cfg(windows)]
225 /// // `my_script_path` is a path to known bat file.
226 /// // `user_name` is an untrusted name given by the user.
227 /// fn run_script(
228 /// my_script_path: &str,
229 /// user_name: &str,
230 /// ) -> Result<std::process::Output, std::io::Error> {
231 /// use std::io::{Error, ErrorKind};
232 /// use std::os::windows::process::CommandExt;
233 /// use std::process::Command;
234 ///
235 /// // Create the command line, making sure to quote the script path.
236 /// // This assumes the fixed arguments have been tested to work with the script we're using.
237 /// let mut cmd_args = format!(r#""{my_script_path}" "--features=[a,b,c]""#);
238 ///
239 /// // Make sure the user name is safe. In particular we need to be
240 /// // cautious of ascii symbols that cmd may interpret specially.
241 /// // Here we only allow alphanumeric characters.
242 /// if !user_name.chars().all(|c| c.is_alphanumeric()) {
243 /// return Err(Error::new(ErrorKind::InvalidInput, "invalid user name"));
244 /// }
245 ///
246 /// // now we have validated the user name, let's add that too.
247 /// cmd_args.push_str(" --user ");
248 /// cmd_args.push_str(user_name);
249 ///
250 /// // call cmd.exe and return the output
251 /// Command::new("cmd.exe")
252 /// .arg("/c")
253 /// // surround the entire command in an extra pair of quotes, as required by cmd.exe.
254 /// .raw_arg(&format!("\"{cmd_args}\""))
255 /// .output()
256 /// }
257 /// ````
258 #[stable(feature = "windows_process_extensions_raw_arg", since = "1.62.0")]
259 fn raw_arg<S: AsRef<OsStr>>(&mut self, text_to_append_as_is: S) -> &mut process::Command;
260
261 /// When [`process::Command`] creates pipes, request that our side is always async.
262 ///
263 /// By default [`process::Command`] may choose to use pipes where both ends
264 /// are opened for synchronous read or write operations. By using
265 /// `async_pipes(true)`, this behavior is overridden so that our side is
266 /// always async.
267 ///
268 /// This is important because if doing async I/O a pipe or a file has to be
269 /// opened for async access.
270 ///
271 /// The end of the pipe sent to the child process will always be synchronous
272 /// regardless of this option.
273 ///
274 /// # Example
275 ///
276 /// ```
277 /// #![feature(windows_process_extensions_async_pipes)]
278 /// use std::os::windows::process::CommandExt;
279 /// use std::process::{Command, Stdio};
280 ///
281 /// # let program = "";
282 ///
283 /// Command::new(program)
284 /// .async_pipes(true)
285 /// .stdin(Stdio::piped())
286 /// .stdout(Stdio::piped())
287 /// .stderr(Stdio::piped());
288 /// ```
289 #[unstable(feature = "windows_process_extensions_async_pipes", issue = "98289")]
290 fn async_pipes(&mut self, always_async: bool) -> &mut process::Command;
291
292 /// Executes the command as a child process with the given
293 /// [`ProcThreadAttributeList`], returning a handle to it.
294 ///
295 /// This method enables the customization of attributes for the spawned
296 /// child process on Windows systems.
297 /// Attributes offer extended configurability for process creation,
298 /// but their usage can be intricate and potentially unsafe.
299 ///
300 /// # Note
301 ///
302 /// By default, stdin, stdout, and stderr are inherited from the parent
303 /// process.
304 ///
305 /// # Example
306 ///
307 /// ```
308 /// #![feature(windows_process_extensions_raw_attribute)]
309 /// use std::os::windows::io::AsRawHandle;
310 /// use std::os::windows::process::{CommandExt, ProcThreadAttributeList};
311 /// use std::process::Command;
312 ///
313 /// # struct ProcessDropGuard(std::process::Child);
314 /// # impl Drop for ProcessDropGuard {
315 /// # fn drop(&mut self) {
316 /// # let _ = self.0.kill();
317 /// # }
318 /// # }
319 /// #
320 /// let parent = Command::new("cmd").spawn()?;
321 /// let parent_process_handle = parent.as_raw_handle();
322 /// # let parent = ProcessDropGuard(parent);
323 ///
324 /// const PROC_THREAD_ATTRIBUTE_PARENT_PROCESS: usize = 0x00020000;
325 /// let mut attribute_list = ProcThreadAttributeList::build()
326 /// .attribute(PROC_THREAD_ATTRIBUTE_PARENT_PROCESS, &parent_process_handle)
327 /// .finish()
328 /// .unwrap();
329 ///
330 /// let mut child = Command::new("cmd").spawn_with_attributes(&attribute_list)?;
331 /// #
332 /// # child.kill()?;
333 /// # Ok::<(), std::io::Error>(())
334 /// ```
335 #[unstable(feature = "windows_process_extensions_raw_attribute", issue = "114854")]
336 fn spawn_with_attributes(
337 &mut self,
338 attribute_list: &ProcThreadAttributeList<'_>,
339 ) -> io::Result<process::Child>;
340
341 /// When true, sets the `STARTF_RUNFULLSCREEN` flag on the [STARTUPINFO][1] struct before passing it to `CreateProcess`.
342 ///
343 /// [1]: https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/ns-processthreadsapi-startupinfoa
344 #[unstable(feature = "windows_process_extensions_startupinfo", issue = "141010")]
345 fn startupinfo_fullscreen(&mut self, enabled: bool) -> &mut process::Command;
346
347 /// When true, sets the `STARTF_UNTRUSTEDSOURCE` flag on the [STARTUPINFO][1] struct before passing it to `CreateProcess`.
348 ///
349 /// [1]: https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/ns-processthreadsapi-startupinfoa
350 #[unstable(feature = "windows_process_extensions_startupinfo", issue = "141010")]
351 fn startupinfo_untrusted_source(&mut self, enabled: bool) -> &mut process::Command;
352
353 /// When specified, sets the following flags on the [STARTUPINFO][1] struct before passing it to `CreateProcess`:
354 /// - If `Some(true)`, sets `STARTF_FORCEONFEEDBACK`
355 /// - If `Some(false)`, sets `STARTF_FORCEOFFFEEDBACK`
356 /// - If `None`, does not set any flags
357 ///
358 /// [1]: https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/ns-processthreadsapi-startupinfoa
359 #[unstable(feature = "windows_process_extensions_startupinfo", issue = "141010")]
360 fn startupinfo_force_feedback(&mut self, enabled: Option<bool>) -> &mut process::Command;
361
362 /// If this flag is set to `true`, each inheritable handle in the calling
363 /// process is inherited by the new process. If the flag is `false`, the
364 /// handles are not inherited.
365 ///
366 /// The default value for this flag is `true`.
367 ///
368 /// **Note** that inherited handles have the same value and access rights
369 /// as the original handles. For additional discussion of inheritable handles,
370 /// see the [Remarks][1] section of the `CreateProcessW` documentation.
371 ///
372 /// [1]: https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw#remarks
373 #[unstable(feature = "windows_process_extensions_inherit_handles", issue = "146407")]
374 fn inherit_handles(&mut self, inherit_handles: bool) -> &mut process::Command;
375}
376
377#[stable(feature = "windows_process_extensions", since = "1.16.0")]
378impl CommandExt for process::Command {
379 fn creation_flags(&mut self, flags: u32) -> &mut process::Command {
380 self.as_inner_mut().creation_flags(flags);
381 self
382 }
383
384 fn show_window(&mut self, cmd_show: u16) -> &mut process::Command {
385 self.as_inner_mut().show_window(Some(cmd_show));
386 self
387 }
388
389 fn force_quotes(&mut self, enabled: bool) -> &mut process::Command {
390 self.as_inner_mut().force_quotes(enabled);
391 self
392 }
393
394 fn raw_arg<S: AsRef<OsStr>>(&mut self, raw_text: S) -> &mut process::Command {
395 self.as_inner_mut().raw_arg(raw_text.as_ref());
396 self
397 }
398
399 fn async_pipes(&mut self, always_async: bool) -> &mut process::Command {
400 // FIXME: This currently has an intentional no-op implementation.
401 // For the time being our side of the pipes will always be async.
402 // Once the ecosystem has adjusted, we may then be able to start making
403 // use of synchronous pipes within the standard library.
404 let _ = always_async;
405 self
406 }
407
408 fn spawn_with_attributes(
409 &mut self,
410 attribute_list: &ProcThreadAttributeList<'_>,
411 ) -> io::Result<process::Child> {
412 self.as_inner_mut()
413 .spawn_with_attributes(sys::process::Stdio::Inherit, true, Some(attribute_list))
414 .map(process::Child::from_inner)
415 }
416
417 fn startupinfo_fullscreen(&mut self, enabled: bool) -> &mut process::Command {
418 self.as_inner_mut().startupinfo_fullscreen(enabled);
419 self
420 }
421
422 fn startupinfo_untrusted_source(&mut self, enabled: bool) -> &mut process::Command {
423 self.as_inner_mut().startupinfo_untrusted_source(enabled);
424 self
425 }
426
427 fn startupinfo_force_feedback(&mut self, enabled: Option<bool>) -> &mut process::Command {
428 self.as_inner_mut().startupinfo_force_feedback(enabled);
429 self
430 }
431
432 fn inherit_handles(&mut self, inherit_handles: bool) -> &mut process::Command {
433 self.as_inner_mut().inherit_handles(inherit_handles);
434 self
435 }
436}
437
438#[unstable(feature = "windows_process_extensions_main_thread_handle", issue = "96723")]
439pub impl(self) trait ChildExt {
440 /// Extracts the main thread raw handle, without taking ownership
441 #[unstable(feature = "windows_process_extensions_main_thread_handle", issue = "96723")]
442 fn main_thread_handle(&self) -> BorrowedHandle<'_>;
443}
444
445#[unstable(feature = "windows_process_extensions_main_thread_handle", issue = "96723")]
446impl ChildExt for process::Child {
447 fn main_thread_handle(&self) -> BorrowedHandle<'_> {
448 self.handle.main_thread_handle()
449 }
450}
451
452/// Windows-specific extensions to [`process::ExitCode`].
453#[unstable(feature = "windows_process_exit_code_from", issue = "111688")]
454pub impl(self) trait ExitCodeExt {
455 /// Creates a new `ExitCode` from the raw underlying `u32` return value of
456 /// a process.
457 ///
458 /// The exit code should not be 259, as this conflicts with the `STILL_ACTIVE`
459 /// macro returned from the `GetExitCodeProcess` function to signal that the
460 /// process has yet to run to completion.
461 #[unstable(feature = "windows_process_exit_code_from", issue = "111688")]
462 fn from_raw(raw: u32) -> Self;
463}
464
465#[unstable(feature = "windows_process_exit_code_from", issue = "111688")]
466impl ExitCodeExt for process::ExitCode {
467 fn from_raw(raw: u32) -> Self {
468 process::ExitCode::from_inner(From::from(raw))
469 }
470}
471
472/// A wrapper around windows [`ProcThreadAttributeList`][1].
473///
474/// [1]: <https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-initializeprocthreadattributelist>
475#[derive(Debug)]
476#[unstable(feature = "windows_process_extensions_raw_attribute", issue = "114854")]
477pub struct ProcThreadAttributeList<'a> {
478 attribute_list: Box<[MaybeUninit<u8>]>,
479 _lifetime_marker: marker::PhantomData<&'a ()>,
480}
481
482#[unstable(feature = "windows_process_extensions_raw_attribute", issue = "114854")]
483impl<'a> ProcThreadAttributeList<'a> {
484 /// Creates a new builder for constructing a [`ProcThreadAttributeList`].
485 pub fn build() -> ProcThreadAttributeListBuilder<'a> {
486 ProcThreadAttributeListBuilder::new()
487 }
488
489 /// Returns a pointer to the underling attribute list.
490 #[doc(hidden)]
491 pub fn as_ptr(&self) -> *const MaybeUninit<u8> {
492 self.attribute_list.as_ptr()
493 }
494}
495
496#[unstable(feature = "windows_process_extensions_raw_attribute", issue = "114854")]
497impl<'a> Drop for ProcThreadAttributeList<'a> {
498 /// Deletes the attribute list.
499 ///
500 /// This method calls [`DeleteProcThreadAttributeList`][1] to delete the
501 /// underlying attribute list.
502 ///
503 /// [1]: <https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-deleteprocthreadattributelist>
504 fn drop(&mut self) {
505 let lp_attribute_list = self.attribute_list.as_mut_ptr().cast::<c_void>();
506 unsafe { sys::c::DeleteProcThreadAttributeList(lp_attribute_list) }
507 }
508}
509
510/// Builder for constructing a [`ProcThreadAttributeList`].
511#[derive(Clone, Debug)]
512#[unstable(feature = "windows_process_extensions_raw_attribute", issue = "114854")]
513pub struct ProcThreadAttributeListBuilder<'a> {
514 attributes: alloc::collections::BTreeMap<usize, ProcThreadAttributeValue>,
515 _lifetime_marker: marker::PhantomData<&'a ()>,
516}
517
518#[unstable(feature = "windows_process_extensions_raw_attribute", issue = "114854")]
519impl<'a> ProcThreadAttributeListBuilder<'a> {
520 fn new() -> Self {
521 ProcThreadAttributeListBuilder {
522 attributes: alloc::collections::BTreeMap::new(),
523 _lifetime_marker: marker::PhantomData,
524 }
525 }
526
527 /// Sets an attribute on the attribute list.
528 ///
529 /// The `attribute` parameter specifies the raw attribute to be set, while
530 /// the `value` parameter holds the value associated with that attribute.
531 /// Please refer to the [Windows documentation][1] for a list of valid attributes.
532 ///
533 /// # Note
534 ///
535 /// The maximum number of attributes is the value of [`u32::MAX`]. If this
536 /// limit is exceeded, the call to [`Self::finish`] will return an `Error`
537 /// indicating that the maximum number of attributes has been exceeded.
538 ///
539 /// # Safety Note
540 ///
541 /// Remember that improper use of attributes can lead to undefined behavior
542 /// or security vulnerabilities. Always consult the documentation and ensure
543 /// proper attribute values are used.
544 ///
545 /// [1]: <https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-updateprocthreadattribute#parameters>
546 pub fn attribute<T>(self, attribute: usize, value: &'a T) -> Self {
547 unsafe {
548 self.raw_attribute(attribute, ptr::addr_of!(*value).cast::<c_void>(), size_of::<T>())
549 }
550 }
551
552 /// Sets a raw attribute on the attribute list.
553 ///
554 /// This function is useful for setting attributes with pointers or sizes
555 /// that cannot be derived directly from their values.
556 ///
557 /// # Safety
558 ///
559 /// This function is marked as `unsafe` because it deals with raw pointers
560 /// and sizes. It is the responsibility of the caller to ensure the value
561 /// lives longer than the resulting [`ProcThreadAttributeList`] as well as
562 /// the validity of the size parameter.
563 ///
564 /// # Example
565 ///
566 #[cfg_attr(target_vendor = "win7", doc = "```no_run")]
567 #[cfg_attr(not(target_vendor = "win7"), doc = "```")]
568 /// #![feature(windows_process_extensions_raw_attribute)]
569 /// use std::ffi::c_void;
570 /// use std::os::windows::process::{CommandExt, ProcThreadAttributeList};
571 /// use std::os::windows::raw::HANDLE;
572 /// use std::process::Command;
573 ///
574 /// #[repr(C)]
575 /// pub struct COORD {
576 /// pub X: i16,
577 /// pub Y: i16,
578 /// }
579 ///
580 /// unsafe extern "system" {
581 /// fn CreatePipe(
582 /// hreadpipe: *mut HANDLE,
583 /// hwritepipe: *mut HANDLE,
584 /// lppipeattributes: *const c_void,
585 /// nsize: u32,
586 /// ) -> i32;
587 /// fn CreatePseudoConsole(
588 /// size: COORD,
589 /// hinput: HANDLE,
590 /// houtput: HANDLE,
591 /// dwflags: u32,
592 /// phpc: *mut isize,
593 /// ) -> i32;
594 /// fn CloseHandle(hobject: HANDLE) -> i32;
595 /// }
596 ///
597 /// let [mut input_read_side, mut output_write_side, mut output_read_side, mut input_write_side] =
598 /// [unsafe { std::mem::zeroed::<HANDLE>() }; 4];
599 ///
600 /// unsafe {
601 /// CreatePipe(&mut input_read_side, &mut input_write_side, std::ptr::null(), 0);
602 /// CreatePipe(&mut output_read_side, &mut output_write_side, std::ptr::null(), 0);
603 /// }
604 ///
605 /// let size = COORD { X: 60, Y: 40 };
606 /// let mut h_pc = unsafe { std::mem::zeroed() };
607 /// unsafe { CreatePseudoConsole(size, input_read_side, output_write_side, 0, &mut h_pc) };
608 ///
609 /// unsafe { CloseHandle(input_read_side) };
610 /// unsafe { CloseHandle(output_write_side) };
611 ///
612 /// const PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE: usize = 131094;
613 ///
614 /// let attribute_list = unsafe {
615 /// ProcThreadAttributeList::build()
616 /// .raw_attribute(
617 /// PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE,
618 /// h_pc as *const c_void,
619 /// size_of::<isize>(),
620 /// )
621 /// .finish()?
622 /// };
623 ///
624 /// let mut child = Command::new("cmd").spawn_with_attributes(&attribute_list)?;
625 /// #
626 /// # child.kill()?;
627 /// # Ok::<(), std::io::Error>(())
628 /// ```
629 pub unsafe fn raw_attribute<T>(
630 mut self,
631 attribute: usize,
632 value_ptr: *const T,
633 value_size: usize,
634 ) -> Self {
635 self.attributes.insert(
636 attribute,
637 ProcThreadAttributeValue { ptr: value_ptr.cast::<c_void>(), size: value_size },
638 );
639 self
640 }
641
642 /// Finalizes the construction of the `ProcThreadAttributeList`.
643 ///
644 /// # Errors
645 ///
646 /// Returns an error if the maximum number of attributes is exceeded
647 /// or if there is an I/O error during initialization.
648 pub fn finish(&self) -> io::Result<ProcThreadAttributeList<'a>> {
649 // To initialize our ProcThreadAttributeList, we need to determine
650 // how many bytes to allocate for it. The Windows API simplifies this
651 // process by allowing us to call `InitializeProcThreadAttributeList`
652 // with a null pointer to retrieve the required size.
653 let mut required_size = 0;
654 let Ok(attribute_count) = self.attributes.len().try_into() else {
655 return Err(io::const_error!(
656 io::ErrorKind::InvalidInput,
657 "maximum number of ProcThreadAttributes exceeded",
658 ));
659 };
660 unsafe {
661 sys::c::InitializeProcThreadAttributeList(
662 ptr::null_mut(),
663 attribute_count,
664 0,
665 &mut required_size,
666 )
667 };
668
669 let mut attribute_list = vec![MaybeUninit::uninit(); required_size].into_boxed_slice();
670
671 // Once we've allocated the necessary memory, it's safe to invoke
672 // `InitializeProcThreadAttributeList` to properly initialize the list.
673 sys::cvt(unsafe {
674 sys::c::InitializeProcThreadAttributeList(
675 attribute_list.as_mut_ptr().cast::<c_void>(),
676 attribute_count,
677 0,
678 &mut required_size,
679 )
680 })?;
681
682 // # Add our attributes to the buffer.
683 // It's theoretically possible for the attribute count to exceed a u32
684 // value. Therefore, we ensure that we don't add more attributes than
685 // the buffer was initialized for.
686 for (&attribute, value) in self.attributes.iter().take(attribute_count as usize) {
687 sys::cvt(unsafe {
688 sys::c::UpdateProcThreadAttribute(
689 attribute_list.as_mut_ptr().cast::<c_void>(),
690 0,
691 attribute,
692 value.ptr,
693 value.size,
694 ptr::null_mut(),
695 ptr::null_mut(),
696 )
697 })?;
698 }
699
700 Ok(ProcThreadAttributeList { attribute_list, _lifetime_marker: marker::PhantomData })
701 }
702}
703
704/// Wrapper around the value data to be used as a Process Thread Attribute.
705#[derive(Clone, Debug)]
706struct ProcThreadAttributeValue {
707 ptr: *const c_void,
708 size: usize,
709}