Skip to content

Commit 05bae6d

Browse files
committed
Add Type::no_inherit
Sets the WSA_FLAG_NO_HANDLE_INHERIT flag on socket creation on Windows.
1 parent e887d43 commit 05bae6d

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

src/socket.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ use crate::{Domain, Protocol, SockAddr, Type};
5858
/// #[cfg(any(target_os = "android", target_os = "dragonfly", target_os = "freebsd", target_os = "linux", target_os = "netbsd", target_os = "openbsd"))]
5959
/// let ty = ty.cloexec();
6060
///
61+
/// // On windows set `WSA_FLAG_NO_HANDLE_INHERIT`.
62+
/// #[cfg(windows)]
63+
/// let ty = ty.no_inherit();
64+
///
6165
/// let socket = Socket::new(domain, ty, Some(protocol))?;
6266
///
6367
/// // On platforms that don't support `SOCK_CLOEXEC`, use `FD_CLOEXEC`.
@@ -68,9 +72,6 @@ use crate::{Domain, Protocol, SockAddr, Type};
6872
/// #[cfg(target_vendor = "apple")]
6973
/// socket.set_nosigpipe()?;
7074
///
71-
/// // On windows set `HANDLE_FLAG_INHERIT`.
72-
/// #[cfg(windows)]
73-
/// socket.set_no_inherit()?;
7475
/// # drop(socket);
7576
/// # Ok(())
7677
/// # }

src/sys/windows.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::cmp;
1010
use std::fmt;
1111
use std::io;
1212
use std::io::{IoSlice, IoSliceMut, Read, Write};
13-
use std::mem::{self, size_of_val, MaybeUninit};
13+
use std::mem::{self, size_of, size_of_val, MaybeUninit};
1414
use std::net::Shutdown;
1515
use std::net::{self, Ipv4Addr, Ipv6Addr};
1616
use std::os::windows::prelude::*;
@@ -34,7 +34,7 @@ use winapi::um::winbase;
3434
use winapi::um::winbase::INFINITE;
3535
use winapi::um::winsock2 as sock;
3636

37-
use crate::{RecvFlags, SockAddr};
37+
use crate::{RecvFlags, SockAddr, Type};
3838

3939
const MSG_PEEK: c_int = 0x2;
4040
const SD_BOTH: c_int = 2;
@@ -89,6 +89,19 @@ impl_debug!(
8989
ws2def::AF_UNSPEC, // = 0.
9090
);
9191

92+
/// Windows only API.
93+
impl Type {
94+
/// Our custom flag to set `WSA_FLAG_NO_HANDLE_INHERIT` on socket creation.
95+
/// Trying to mimic `Type::cloexec` on windows.
96+
const NO_INHERIT: c_int = 1 << (size_of::<c_int>());
97+
98+
/// Set `WSA_FLAG_NO_HANDLE_INHERIT` on the socket.
99+
#[cfg(feature = "all")]
100+
pub const fn no_inherit(self) -> Type {
101+
Type(self.0 | Type::NO_INHERIT)
102+
}
103+
}
104+
92105
impl_debug!(
93106
crate::Type,
94107
ws2def::SOCK_STREAM,
@@ -139,17 +152,25 @@ fn last_error() -> io::Error {
139152
// TODO: rename to `Socket` once the struct `Socket` is no longer used.
140153
pub(crate) type SysSocket = sock::SOCKET;
141154

142-
pub(crate) fn socket(family: c_int, ty: c_int, protocol: c_int) -> io::Result<SysSocket> {
155+
pub(crate) fn socket(family: c_int, mut ty: c_int, protocol: c_int) -> io::Result<SysSocket> {
143156
init();
144157

158+
// Check if we set our custom flag.
159+
let flags = if ty & Type::NO_INHERIT != 0 {
160+
ty = ty & !Type::NO_INHERIT;
161+
sock::WSA_FLAG_NO_HANDLE_INHERIT
162+
} else {
163+
0
164+
};
165+
145166
syscall!(
146167
WSASocketW(
147168
family,
148169
ty,
149170
protocol,
150171
ptr::null_mut(),
151172
0,
152-
sock::WSA_FLAG_OVERLAPPED,
173+
sock::WSA_FLAG_OVERLAPPED | flags,
153174
),
154175
PartialEq::eq,
155176
sock::INVALID_SOCKET

0 commit comments

Comments
 (0)