Skip to content

Commit

Permalink
Rollup merge of #97060 - bdbai:fix/uwphandle, r=ChrisDenton
Browse files Browse the repository at this point in the history
Fix use of SetHandleInformation on UWP

The use of `SetHandleInformation` (introduced in #96441 to make `HANDLE` inheritable) breaks UWP builds because it is not available for UWP targets.

Proposed workaround: duplicate the `HANDLE` with `inherit = true` and immediately close the old one. Traditional Windows Desktop programs are not affected.

cc `@ChrisDenton`
  • Loading branch information
Dylan-DPC authored May 15, 2022
2 parents ede4f0e + 4f637ee commit d56c59e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions library/std/src/os/windows/io/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ impl OwnedHandle {
}

/// Allow child processes to inherit the handle.
#[cfg(not(target_vendor = "uwp"))]
pub(crate) fn set_inheritable(&self) -> io::Result<()> {
cvt(unsafe {
c::SetHandleInformation(
Expand Down
1 change: 1 addition & 0 deletions library/std/src/os/windows/io/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::mem;
use crate::mem::forget;
use crate::sys;
use crate::sys::c;
#[cfg(not(target_vendor = "uwp"))]
use crate::sys::cvt;

/// A borrowed socket.
Expand Down
1 change: 1 addition & 0 deletions library/std/src/sys/windows/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ impl Handle {
Ok(Self(self.0.duplicate(access, inherit, options)?))
}

#[cfg(not(target_vendor = "uwp"))]
pub(crate) fn set_inheritable(&self) -> io::Result<()> {
self.0.set_inheritable()
}
Expand Down
13 changes: 12 additions & 1 deletion library/std/src/sys/windows/pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,21 @@ impl Pipes {
} else {
let (ours, theirs) = if ours_readable { (read, write) } else { (write, read) };
let ours = Handle::from_raw_handle(ours);
#[cfg(not(target_vendor = "uwp"))]
let theirs = Handle::from_raw_handle(theirs);
#[cfg(target_vendor = "uwp")]
let mut theirs = Handle::from_raw_handle(theirs);

if their_handle_inheritable {
theirs.set_inheritable()?;
#[cfg(not(target_vendor = "uwp"))]
{
theirs.set_inheritable()?;
}

#[cfg(target_vendor = "uwp")]
{
theirs = theirs.duplicate(0, true, c::DUPLICATE_SAME_ACCESS)?;
}
}

Ok(Pipes { ours: AnonPipe::Sync(ours), theirs: AnonPipe::Sync(theirs) })
Expand Down

0 comments on commit d56c59e

Please sign in to comment.