diff options
author | Nathan Whitaker <17734409+nathanwhit@users.noreply.github.com> | 2024-09-27 12:35:37 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-27 12:35:37 -0700 |
commit | fbddd5a2ebfb11dd376a751e9fc4cf09a6286ada (patch) | |
tree | 75c13ee9f26f61fe8c1d6f80df2580a523177c1b /ext/io | |
parent | a8d1ab52761516b7f9b6069d6e433254794ed48c (diff) |
fix(node): Pass NPM_PROCESS_STATE to subprocesses via temp file instead of env var (#25896)
Fixes https://github.com/denoland/deno/issues/25401. Fixes
https://github.com/denoland/deno/issues/25841. Fixes
https://github.com/denoland/deno/issues/25891.
Diffstat (limited to 'ext/io')
-rw-r--r-- | ext/io/bi_pipe.rs | 6 | ||||
-rw-r--r-- | ext/io/lib.rs | 107 | ||||
-rw-r--r-- | ext/io/pipe.rs | 2 |
3 files changed, 110 insertions, 5 deletions
diff --git a/ext/io/bi_pipe.rs b/ext/io/bi_pipe.rs index 04fff7b00..402e383ac 100644 --- a/ext/io/bi_pipe.rs +++ b/ext/io/bi_pipe.rs @@ -11,11 +11,7 @@ use deno_core::RcRef; use tokio::io::AsyncReadExt; use tokio::io::AsyncWriteExt; -#[cfg(unix)] -pub type RawBiPipeHandle = std::os::fd::RawFd; - -#[cfg(windows)] -pub type RawBiPipeHandle = std::os::windows::io::RawHandle; +pub type RawBiPipeHandle = super::RawIoHandle; /// One end of a bidirectional pipe. This implements the /// `Resource` trait. diff --git a/ext/io/lib.rs b/ext/io/lib.rs index 47921bcee..a07d64ae3 100644 --- a/ext/io/lib.rs +++ b/ext/io/lib.rs @@ -67,6 +67,7 @@ pub use pipe::AsyncPipeRead; pub use pipe::AsyncPipeWrite; pub use pipe::PipeRead; pub use pipe::PipeWrite; +pub use pipe::RawPipeHandle; pub use bi_pipe::bi_pipe_pair_raw; pub use bi_pipe::BiPipe; @@ -75,6 +76,112 @@ pub use bi_pipe::BiPipeResource; pub use bi_pipe::BiPipeWrite; pub use bi_pipe::RawBiPipeHandle; +/// Abstraction over `AsRawFd` (unix) and `AsRawHandle` (windows) +pub trait AsRawIoHandle { + fn as_raw_io_handle(&self) -> RawIoHandle; +} + +#[cfg(unix)] +impl<T> AsRawIoHandle for T +where + T: std::os::unix::io::AsRawFd, +{ + fn as_raw_io_handle(&self) -> RawIoHandle { + self.as_raw_fd() + } +} + +#[cfg(windows)] +impl<T> AsRawIoHandle for T +where + T: std::os::windows::io::AsRawHandle, +{ + fn as_raw_io_handle(&self) -> RawIoHandle { + self.as_raw_handle() + } +} + +/// Abstraction over `IntoRawFd` (unix) and `IntoRawHandle` (windows) +pub trait IntoRawIoHandle { + fn into_raw_io_handle(self) -> RawIoHandle; +} + +#[cfg(unix)] +impl<T> IntoRawIoHandle for T +where + T: std::os::unix::io::IntoRawFd, +{ + fn into_raw_io_handle(self) -> RawIoHandle { + self.into_raw_fd() + } +} + +#[cfg(windows)] +impl<T> IntoRawIoHandle for T +where + T: std::os::windows::io::IntoRawHandle, +{ + fn into_raw_io_handle(self) -> RawIoHandle { + self.into_raw_handle() + } +} + +/// Abstraction over `FromRawFd` (unix) and `FromRawHandle` (windows) +pub trait FromRawIoHandle: Sized { + /// Constructs a type from a raw io handle (fd/HANDLE). + /// + /// # Safety + /// + /// Refer to the standard library docs ([unix](https://doc.rust-lang.org/stable/std/os/windows/io/trait.FromRawHandle.html#tymethod.from_raw_handle)) ([windows](https://doc.rust-lang.org/stable/std/os/fd/trait.FromRawFd.html#tymethod.from_raw_fd)) + /// + unsafe fn from_raw_io_handle(handle: RawIoHandle) -> Self; +} + +#[cfg(unix)] +impl<T> FromRawIoHandle for T +where + T: std::os::unix::io::FromRawFd, +{ + unsafe fn from_raw_io_handle(fd: RawIoHandle) -> T { + // SAFETY: upheld by caller + unsafe { T::from_raw_fd(fd) } + } +} + +#[cfg(windows)] +impl<T> FromRawIoHandle for T +where + T: std::os::windows::io::FromRawHandle, +{ + unsafe fn from_raw_io_handle(fd: RawIoHandle) -> T { + // SAFETY: upheld by caller + unsafe { T::from_raw_handle(fd) } + } +} + +#[cfg(unix)] +pub type RawIoHandle = std::os::fd::RawFd; + +#[cfg(windows)] +pub type RawIoHandle = std::os::windows::io::RawHandle; + +pub fn close_raw_handle(handle: RawIoHandle) { + #[cfg(unix)] + { + // SAFETY: libc call + unsafe { + libc::close(handle); + } + } + #[cfg(windows)] + { + // SAFETY: win32 call + unsafe { + windows_sys::Win32::Foundation::CloseHandle(handle as _); + } + } +} + // Store the stdio fd/handles in global statics in order to keep them // alive for the duration of the application since the last handle/fd // being dropped will close the corresponding pipe. diff --git a/ext/io/pipe.rs b/ext/io/pipe.rs index 70788f752..e0e019e27 100644 --- a/ext/io/pipe.rs +++ b/ext/io/pipe.rs @@ -3,6 +3,8 @@ use std::io; use std::pin::Pin; use std::process::Stdio; +pub type RawPipeHandle = super::RawIoHandle; + // The synchronous read end of a unidirectional pipe. pub struct PipeRead { file: std::fs::File, |