summaryrefslogtreecommitdiffstats
path: root/library/std/src/os/windows
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 18:31:44 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 18:31:44 +0000
commitc23a457e72abe608715ac76f076f47dc42af07a5 (patch)
tree2772049aaf84b5c9d0ed12ec8d86812f7a7904b6 /library/std/src/os/windows
parentReleasing progress-linux version 1.73.0+dfsg1-1~progress7.99u1. (diff)
downloadrustc-c23a457e72abe608715ac76f076f47dc42af07a5.tar.xz
rustc-c23a457e72abe608715ac76f076f47dc42af07a5.zip
Merging upstream version 1.74.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'library/std/src/os/windows')
-rw-r--r--library/std/src/os/windows/io/mod.rs10
-rw-r--r--library/std/src/os/windows/io/raw.rs6
-rw-r--r--library/std/src/os/windows/io/socket.rs8
-rw-r--r--library/std/src/os/windows/process.rs108
4 files changed, 123 insertions, 9 deletions
diff --git a/library/std/src/os/windows/io/mod.rs b/library/std/src/os/windows/io/mod.rs
index e2a401fb6..3d3ae3878 100644
--- a/library/std/src/os/windows/io/mod.rs
+++ b/library/std/src/os/windows/io/mod.rs
@@ -6,7 +6,8 @@
//!
//! This module provides three types for representing raw handles and sockets
//! with different ownership properties: raw, borrowed, and owned, which are
-//! analogous to types used for representing pointers:
+//! analogous to types used for representing pointers. These types reflect concepts of [I/O
+//! safety][io-safety] on Windows.
//!
//! | Type | Analogous to |
//! | ---------------------- | ------------ |
@@ -23,8 +24,8 @@
//! And in new code, they should be considered unsafe to do I/O on (analogous
//! to dereferencing them). Rust did not always provide this guidance, so
//! existing code in the Rust ecosystem often doesn't mark `RawHandle` and
-//! `RawSocket` usage as unsafe. Once the `io_safety` feature is stable,
-//! libraries will be encouraged to migrate, either by adding `unsafe` to APIs
+//! `RawSocket` usage as unsafe.
+//! Libraries are encouraged to migrate, either by adding `unsafe` to APIs
//! that dereference `RawHandle` and `RawSocket` values, or by using to
//! `BorrowedHandle`, `BorrowedSocket`, `OwnedHandle`, or `OwnedSocket`.
//!
@@ -45,8 +46,11 @@
//! Like boxes, `OwnedHandle` and `OwnedSocket` values conceptually own the
//! resource they point to, and free (close) it when they are dropped.
//!
+//! See the [`io` module docs][io-safety] for a general explanation of I/O safety.
+//!
//! [`BorrowedHandle<'a>`]: crate::os::windows::io::BorrowedHandle
//! [`BorrowedSocket<'a>`]: crate::os::windows::io::BorrowedSocket
+//! [io-safety]: crate::io#io-safety
#![stable(feature = "rust1", since = "1.0.0")]
diff --git a/library/std/src/os/windows/io/raw.rs b/library/std/src/os/windows/io/raw.rs
index 1759e2e7f..770583a9c 100644
--- a/library/std/src/os/windows/io/raw.rs
+++ b/library/std/src/os/windows/io/raw.rs
@@ -62,7 +62,7 @@ pub trait FromRawHandle {
/// # Safety
///
/// The `handle` passed in must:
- /// - be a valid an open handle,
+ /// - be an [owned handle][io-safety]; in particular, it must be open.
/// - be a handle for a resource that may be freed via [`CloseHandle`]
/// (as opposed to `RegCloseKey` or other close functions).
///
@@ -71,6 +71,7 @@ pub trait FromRawHandle {
///
/// [`CloseHandle`]: https://docs.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-closehandle
/// [here]: https://devblogs.microsoft.com/oldnewthing/20040302-00/?p=40443
+ /// [io-safety]: io#io-safety
#[stable(feature = "from_raw_os", since = "1.1.0")]
unsafe fn from_raw_handle(handle: RawHandle) -> Self;
}
@@ -207,10 +208,11 @@ pub trait FromRawSocket {
/// # Safety
///
/// The `socket` passed in must:
- /// - be a valid an open socket,
+ /// - be an [owned socket][io-safety]; in particular, it must be open.
/// - be a socket that may be freed via [`closesocket`].
///
/// [`closesocket`]: https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-closesocket
+ /// [io-safety]: io#io-safety
#[stable(feature = "from_raw_os", since = "1.1.0")]
unsafe fn from_raw_socket(sock: RawSocket) -> Self;
}
diff --git a/library/std/src/os/windows/io/socket.rs b/library/std/src/os/windows/io/socket.rs
index 6359835ca..c80b9e284 100644
--- a/library/std/src/os/windows/io/socket.rs
+++ b/library/std/src/os/windows/io/socket.rs
@@ -116,7 +116,7 @@ impl BorrowedSocket<'_> {
let mut info = unsafe { mem::zeroed::<sys::c::WSAPROTOCOL_INFOW>() };
let result = unsafe {
sys::c::WSADuplicateSocketW(
- self.as_raw_socket(),
+ self.as_raw_socket() as sys::c::SOCKET,
sys::c::GetCurrentProcessId(),
&mut info,
)
@@ -134,7 +134,7 @@ impl BorrowedSocket<'_> {
};
if socket != sys::c::INVALID_SOCKET {
- unsafe { Ok(OwnedSocket::from_raw_socket(socket)) }
+ unsafe { Ok(OwnedSocket::from_raw_socket(socket as RawSocket)) }
} else {
let error = unsafe { sys::c::WSAGetLastError() };
@@ -158,7 +158,7 @@ impl BorrowedSocket<'_> {
}
unsafe {
- let socket = OwnedSocket::from_raw_socket(socket);
+ let socket = OwnedSocket::from_raw_socket(socket as RawSocket);
socket.set_no_inherit()?;
Ok(socket)
}
@@ -211,7 +211,7 @@ impl Drop for OwnedSocket {
#[inline]
fn drop(&mut self) {
unsafe {
- let _ = sys::c::closesocket(self.socket);
+ let _ = sys::c::closesocket(self.socket as sys::c::SOCKET);
}
}
}
diff --git a/library/std/src/os/windows/process.rs b/library/std/src/os/windows/process.rs
index 073168cf2..d00e79476 100644
--- a/library/std/src/os/windows/process.rs
+++ b/library/std/src/os/windows/process.rs
@@ -106,6 +106,45 @@ impl IntoRawHandle for process::ChildStderr {
}
}
+/// Create a `ChildStdin` from the provided `OwnedHandle`.
+///
+/// The provided handle must be asynchronous, as reading and
+/// writing from and to it is implemented using asynchronous APIs.
+#[stable(feature = "child_stream_from_fd", since = "1.74.0")]
+impl From<OwnedHandle> for process::ChildStdin {
+ fn from(handle: OwnedHandle) -> process::ChildStdin {
+ let handle = sys::handle::Handle::from_inner(handle);
+ let pipe = sys::pipe::AnonPipe::from_inner(handle);
+ process::ChildStdin::from_inner(pipe)
+ }
+}
+
+/// Create a `ChildStdout` from the provided `OwnedHandle`.
+///
+/// The provided handle must be asynchronous, as reading and
+/// writing from and to it is implemented using asynchronous APIs.
+#[stable(feature = "child_stream_from_fd", since = "1.74.0")]
+impl From<OwnedHandle> for process::ChildStdout {
+ fn from(handle: OwnedHandle) -> process::ChildStdout {
+ let handle = sys::handle::Handle::from_inner(handle);
+ let pipe = sys::pipe::AnonPipe::from_inner(handle);
+ process::ChildStdout::from_inner(pipe)
+ }
+}
+
+/// Create a `ChildStderr` from the provided `OwnedHandle`.
+///
+/// The provided handle must be asynchronous, as reading and
+/// writing from and to it is implemented using asynchronous APIs.
+#[stable(feature = "child_stream_from_fd", since = "1.74.0")]
+impl From<OwnedHandle> for process::ChildStderr {
+ fn from(handle: OwnedHandle) -> process::ChildStderr {
+ let handle = sys::handle::Handle::from_inner(handle);
+ let pipe = sys::pipe::AnonPipe::from_inner(handle);
+ process::ChildStderr::from_inner(pipe)
+ }
+}
+
/// Windows-specific extensions to [`process::ExitStatus`].
///
/// This trait is sealed: it cannot be implemented outside the standard library.
@@ -192,6 +231,66 @@ pub trait CommandExt: Sealed {
/// ```
#[unstable(feature = "windows_process_extensions_async_pipes", issue = "98289")]
fn async_pipes(&mut self, always_async: bool) -> &mut process::Command;
+
+ /// Sets a raw attribute on the command, providing extended configuration options for Windows processes.
+ ///
+ /// This method allows you to specify custom attributes for a child process on Windows systems using raw attribute values.
+ /// Raw attributes provide extended configurability for process creation, but their usage can be complex and potentially unsafe.
+ ///
+ /// The `attribute` parameter specifies the raw attribute to be set, while the `value` parameter holds the value associated with that attribute.
+ /// Please refer to the [`windows-rs`](https://microsoft.github.io/windows-docs-rs/doc/windows/) documentation or the [`Win32 API documentation`](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-updateprocthreadattribute) for detailed information about available attributes and their meanings.
+ ///
+ /// # Note
+ ///
+ /// The maximum number of raw attributes is the value of [`u32::MAX`].
+ /// If this limit is exceeded, the call to [`process::Command::spawn`] will return an `Error` indicating that the maximum number of attributes has been exceeded.
+ /// # Safety
+ ///
+ /// The usage of raw attributes is potentially unsafe and should be done with caution. Incorrect attribute values or improper configuration can lead to unexpected behavior or errors.
+ ///
+ /// # Example
+ ///
+ /// The following example demonstrates how to create a child process with a specific parent process ID using a raw attribute.
+ ///
+ /// ```rust
+ /// #![feature(windows_process_extensions_raw_attribute)]
+ /// use std::os::windows::{process::CommandExt, io::AsRawHandle};
+ /// use std::process::Command;
+ ///
+ /// # struct ProcessDropGuard(std::process::Child);
+ /// # impl Drop for ProcessDropGuard {
+ /// # fn drop(&mut self) {
+ /// # let _ = self.0.kill();
+ /// # }
+ /// # }
+ ///
+ /// let parent = Command::new("cmd").spawn()?;
+ ///
+ /// let mut child_cmd = Command::new("cmd");
+ ///
+ /// const PROC_THREAD_ATTRIBUTE_PARENT_PROCESS: usize = 0x00020000;
+ ///
+ /// unsafe {
+ /// child_cmd.raw_attribute(PROC_THREAD_ATTRIBUTE_PARENT_PROCESS, parent.as_raw_handle() as isize);
+ /// }
+ /// #
+ /// # let parent = ProcessDropGuard(parent);
+ ///
+ /// let mut child = child_cmd.spawn()?;
+ ///
+ /// # child.kill()?;
+ /// # Ok::<(), std::io::Error>(())
+ /// ```
+ ///
+ /// # Safety Note
+ ///
+ /// Remember that improper use of raw attributes can lead to undefined behavior or security vulnerabilities. Always consult the documentation and ensure proper attribute values are used.
+ #[unstable(feature = "windows_process_extensions_raw_attribute", issue = "114854")]
+ unsafe fn raw_attribute<T: Copy + Send + Sync + 'static>(
+ &mut self,
+ attribute: usize,
+ value: T,
+ ) -> &mut process::Command;
}
#[stable(feature = "windows_process_extensions", since = "1.16.0")]
@@ -219,6 +318,15 @@ impl CommandExt for process::Command {
let _ = always_async;
self
}
+
+ unsafe fn raw_attribute<T: Copy + Send + Sync + 'static>(
+ &mut self,
+ attribute: usize,
+ value: T,
+ ) -> &mut process::Command {
+ self.as_inner_mut().raw_attribute(attribute, value);
+ self
+ }
}
#[unstable(feature = "windows_process_extensions_main_thread_handle", issue = "96723")]