Skip to content

Commit 0214d3c

Browse files
committed
libstd: win: Remove calls to SetHandleInformation
The WSA_FLAG_NO_HANDLE_INHERIT is available starting from Windows 7 (sp1) which only matches rustc's target platform for Windows, but not the runtime target platforms. This function isn't available when targeting UWP. Instead: - Create all sockets with the WSA_FLAG_NO_HANDLE_INHERIT flag The socket created by accept() need not to explicitely disable handle inheritance, since the documentation states «The newly created socket is the socket that will handle the actual connection; it has the same properties as socket s, including the asynchronous events registered with the WSAAsyncSelect or WSAEventSelect functions.» - Pass a boolean flag to allow the 'their' end of pipes to be inheritable directly upon creating it, instead of a later call to SetHandleInformation
1 parent 4cd98b6 commit 0214d3c

File tree

4 files changed

+28
-10
lines changed

4 files changed

+28
-10
lines changed

src/libstd/sys/windows/c.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ impl Clone for WIN32_FIND_DATAW {
132132
}
133133

134134
pub const WSA_FLAG_OVERLAPPED: DWORD = 0x01;
135+
#[cfg(target_os = "uwp")]
136+
pub const WSA_FLAG_NO_HANDLE_INHERIT: DWORD = 0x80;
135137

136138
pub const WSADESCRIPTION_LEN: usize = 256;
137139
pub const WSASYS_STATUS_LEN: usize = 128;
@@ -168,6 +170,7 @@ pub const STD_INPUT_HANDLE: DWORD = -10i32 as DWORD;
168170
pub const STD_OUTPUT_HANDLE: DWORD = -11i32 as DWORD;
169171
pub const STD_ERROR_HANDLE: DWORD = -12i32 as DWORD;
170172

173+
#[cfg(not(target_os = "uwp"))]
171174
pub const HANDLE_FLAG_INHERIT: DWORD = 0x00000001;
172175

173176
pub const PROGRESS_CONTINUE: DWORD = 0;
@@ -1083,6 +1086,7 @@ extern "system" {
10831086
pub fn GetUserProfileDirectoryW(hToken: HANDLE,
10841087
lpProfileDir: LPWSTR,
10851088
lpcchSize: *mut DWORD) -> BOOL;
1089+
#[cfg(not(target_os = "uwp"))]
10861090
pub fn SetHandleInformation(hObject: HANDLE,
10871091
dwMask: DWORD,
10881092
dwFlags: DWORD) -> BOOL;

src/libstd/sys/windows/net.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,16 @@ impl Socket {
9696
SocketAddr::V6(..) => c::AF_INET6,
9797
};
9898
let socket = unsafe {
99-
match c::WSASocketW(fam, ty, 0, ptr::null_mut(), 0,
100-
c::WSA_FLAG_OVERLAPPED) {
99+
#[cfg(not(target_os = "uwp"))]
100+
let socket_flags = c::WSA_FLAG_OVERLAPPED;
101+
#[cfg(target_os = "uwp")]
102+
let socket_flags = c::WSA_FLAG_OVERLAPPED | c::WSA_FLAG_NO_HANDLE_INHERIT;
103+
match c::WSASocketW(fam, ty, 0, ptr::null_mut(), 0, socket_flags) {
101104
c::INVALID_SOCKET => Err(last_error()),
102105
n => Ok(Socket(n)),
103106
}
104107
}?;
108+
#[cfg(not(target_os = "uwp"))]
105109
socket.set_no_inherit()?;
106110
Ok(socket)
107111
}
@@ -168,6 +172,7 @@ impl Socket {
168172
n => Ok(Socket(n)),
169173
}
170174
}?;
175+
#[cfg(not(target_os = "uwp"))]
171176
socket.set_no_inherit()?;
172177
Ok(socket)
173178
}
@@ -178,15 +183,21 @@ impl Socket {
178183
cvt(c::WSADuplicateSocketW(self.0,
179184
c::GetCurrentProcessId(),
180185
&mut info))?;
186+
#[cfg(not(target_os = "uwp"))]
187+
let socket_flags = c::WSA_FLAG_OVERLAPPED;
188+
#[cfg(target_os = "uwp")]
189+
let socket_flags = c::WSA_FLAG_OVERLAPPED | c::WSA_FLAG_NO_HANDLE_INHERIT;
190+
181191
match c::WSASocketW(info.iAddressFamily,
182192
info.iSocketType,
183193
info.iProtocol,
184194
&mut info, 0,
185-
c::WSA_FLAG_OVERLAPPED) {
195+
socket_flags) {
186196
c::INVALID_SOCKET => Err(last_error()),
187197
n => Ok(Socket(n)),
188198
}
189199
}?;
200+
#[cfg(not(target_os = "uwp"))]
190201
socket.set_no_inherit()?;
191202
Ok(socket)
192203
}
@@ -312,6 +323,7 @@ impl Socket {
312323
}
313324
}
314325

326+
#[cfg(not(target_os = "uwp"))]
315327
fn set_no_inherit(&self) -> io::Result<()> {
316328
sys::cvt(unsafe {
317329
c::SetHandleInformation(self.0 as c::HANDLE,

src/libstd/sys/windows/pipe.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub struct Pipes {
4545
/// mode. This means that technically speaking it should only ever be used
4646
/// with `OVERLAPPED` instances, but also works out ok if it's only ever used
4747
/// once at a time (which we do indeed guarantee).
48-
pub fn anon_pipe(ours_readable: bool) -> io::Result<Pipes> {
48+
pub fn anon_pipe(ours_readable: bool, their_handle_inheritable: bool) -> io::Result<Pipes> {
4949
// Note that we specifically do *not* use `CreatePipe` here because
5050
// unfortunately the anonymous pipes returned do not support overlapped
5151
// operations. Instead, we create a "hopefully unique" name and create a
@@ -137,6 +137,13 @@ pub fn anon_pipe(ours_readable: bool) -> io::Result<Pipes> {
137137
opts.write(ours_readable);
138138
opts.read(!ours_readable);
139139
opts.share_mode(0);
140+
let size = mem::size_of::<c::SECURITY_ATTRIBUTES>();
141+
let mut sa = c::SECURITY_ATTRIBUTES {
142+
nLength: size as c::DWORD,
143+
lpSecurityDescriptor: ptr::null_mut(),
144+
bInheritHandle: their_handle_inheritable as i32,
145+
};
146+
opts.security_attributes(&mut sa);
140147
let theirs = File::open(Path::new(&name), &opts)?;
141148
let theirs = AnonPipe { inner: theirs.into_handle() };
142149

src/libstd/sys/windows/process.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -267,13 +267,8 @@ impl Stdio {
267267

268268
Stdio::MakePipe => {
269269
let ours_readable = stdio_id != c::STD_INPUT_HANDLE;
270-
let pipes = pipe::anon_pipe(ours_readable)?;
270+
let pipes = pipe::anon_pipe(ours_readable, true)?;
271271
*pipe = Some(pipes.ours);
272-
cvt(unsafe {
273-
c::SetHandleInformation(pipes.theirs.handle().raw(),
274-
c::HANDLE_FLAG_INHERIT,
275-
c::HANDLE_FLAG_INHERIT)
276-
})?;
277272
Ok(pipes.theirs.into_handle())
278273
}
279274

0 commit comments

Comments
 (0)