@@ -10,7 +10,7 @@ use std::cmp;
10
10
use std:: fmt;
11
11
use std:: io;
12
12
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 } ;
14
14
use std:: net:: Shutdown ;
15
15
use std:: net:: { self , Ipv4Addr , Ipv6Addr } ;
16
16
use std:: os:: windows:: prelude:: * ;
@@ -34,7 +34,7 @@ use winapi::um::winbase;
34
34
use winapi:: um:: winbase:: INFINITE ;
35
35
use winapi:: um:: winsock2 as sock;
36
36
37
- use crate :: { RecvFlags , SockAddr } ;
37
+ use crate :: { RecvFlags , SockAddr , Type } ;
38
38
39
39
const MSG_PEEK : c_int = 0x2 ;
40
40
const SD_BOTH : c_int = 2 ;
@@ -89,6 +89,19 @@ impl_debug!(
89
89
ws2def:: AF_UNSPEC , // = 0.
90
90
) ;
91
91
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
+
92
105
impl_debug ! (
93
106
crate :: Type ,
94
107
ws2def:: SOCK_STREAM ,
@@ -139,17 +152,25 @@ fn last_error() -> io::Error {
139
152
// TODO: rename to `Socket` once the struct `Socket` is no longer used.
140
153
pub ( crate ) type SysSocket = sock:: SOCKET ;
141
154
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 > {
143
156
init ( ) ;
144
157
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
+
145
166
syscall ! (
146
167
WSASocketW (
147
168
family,
148
169
ty,
149
170
protocol,
150
171
ptr:: null_mut( ) ,
151
172
0 ,
152
- sock:: WSA_FLAG_OVERLAPPED ,
173
+ sock:: WSA_FLAG_OVERLAPPED | flags ,
153
174
) ,
154
175
PartialEq :: eq,
155
176
sock:: INVALID_SOCKET
0 commit comments