Skip to content

Commit 41bd4c8

Browse files
seritoolsmbilker
authored andcommitted
Stub SetHandleInformation/Socket::set_no_inherit
SetHandleInformation is exported by kernel32 on Win9X/ME, but is just stubbed, returning `ERROR_CALL_NOT_IMPLEMENTED`. Sockets are non-inheritable on these systems anyways, so we "fail successfully". https://www.betaarchive.com/wiki/index.php/Microsoft_KB_Archive/150523#MORE_INFORMATION SetHandleInformation is also unavailable on WinNT before 3.51. This is fine, however, because MS did not supply WinSock 2 for Windows NT before 4.0, so this function is not called.
1 parent 61cf125 commit 41bd4c8

File tree

4 files changed

+26
-7
lines changed

4 files changed

+26
-7
lines changed

library/std/src/os/windows/io/socket.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,27 @@ impl OwnedSocket {
9292
#[allow(fuzzy_provenance_casts)]
9393
#[cfg(not(target_vendor = "uwp"))]
9494
pub(crate) fn set_no_inherit(&self) -> io::Result<()> {
95-
cvt(unsafe {
95+
let res = cvt(unsafe {
9696
sys::c::SetHandleInformation(
9797
self.as_raw_socket() as sys::c::HANDLE,
9898
sys::c::HANDLE_FLAG_INHERIT,
9999
0,
100100
)
101101
})
102-
.map(drop)
102+
.map(drop);
103+
104+
match res {
105+
// SetHandleInformation is exported by kernel32 on Win9X/ME, but is just stubbed,
106+
// returning `ERROR_CALL_NOT_IMPLEMENTED`. Sockets are non-inheritable on these
107+
// systems anyways, so we "fail successfully" here.
108+
// https://www.betaarchive.com/wiki/index.php/Microsoft_KB_Archive/150523#MORE_INFORMATION
109+
110+
// SetHandleInformation is also unavailable on WinNT before 3.51. This is fine,
111+
// however, because MS did not supply WinSock 2 for Windows NT before 4.0, so this
112+
// function is not called.
113+
Err(e) if e.raw_os_error() == Some(sys::c::ERROR_CALL_NOT_IMPLEMENTED as i32) => Ok(()),
114+
res => res,
115+
}
103116
}
104117

105118
#[cfg(target_vendor = "uwp")]

library/std/src/sys/windows/c.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,17 @@ compat_fn_with_fallback! {
493493
SetLastError(ERROR_CALL_NOT_IMPLEMENTED as DWORD);
494494
0
495495
}
496+
497+
// >= NT 3.51+
498+
// https://docs.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-sethandleinformation
499+
pub fn SetHandleInformation(
500+
hObject: HANDLE,
501+
dwMask: DWORD,
502+
dwFlags: DWORD
503+
) -> BOOL {
504+
SetLastError(ERROR_CALL_NOT_IMPLEMENTED as DWORD);
505+
FALSE
506+
}
496507
}
497508

498509
compat_fn_optional! {

library/std/src/sys/windows/c/windows_sys.lst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1925,7 +1925,6 @@ Windows.Win32.Foundation.MAX_PATH
19251925
Windows.Win32.Foundation.NO_ERROR
19261926
Windows.Win32.Foundation.NTSTATUS
19271927
Windows.Win32.Foundation.RtlNtStatusToDosError
1928-
Windows.Win32.Foundation.SetHandleInformation
19291928
Windows.Win32.Foundation.SetLastError
19301929
Windows.Win32.Foundation.STATUS_DELETE_PENDING
19311930
Windows.Win32.Foundation.STATUS_END_OF_FILE

library/std/src/sys/windows/c/windows_sys.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -404,10 +404,6 @@ extern "system" {
404404
) -> BOOL;
405405
}
406406
#[link(name = "kernel32")]
407-
extern "system" {
408-
pub fn SetHandleInformation(hobject: HANDLE, dwmask: u32, dwflags: HANDLE_FLAGS) -> BOOL;
409-
}
410-
#[link(name = "kernel32")]
411407
extern "system" {
412408
pub fn SetLastError(dwerrcode: WIN32_ERROR) -> ();
413409
}

0 commit comments

Comments
 (0)