Debian Codegraph @

src/windows.rs

raw symbols SCIP
1 use std::env;
2 use std::io::{self, Read, Write};
3 use std::mem;
4 use std::net::{Shutdown, SocketAddr, TcpListener, TcpStream, UdpSocket};
5
6 use uuid::Uuid;
7
8 pub use std::os::windows::io::{FromRawSocket, RawSocket};
9 use winapi::ctypes::{c_uint, c_ulong, c_void};
10 use winapi::shared::ntdef::{HANDLE, NTSTATUS};
11 use winapi::um::processthreadsapi::GetCurrentProcessId;
12 use winapi::um::winsock2::{
13 WSASocketW, FROM_PROTOCOL_INFO, WSAPROTOCOL_INFOW, WSA_FLAG_OVERLAPPED,
14 };
15
16 pub use self::RawSocket as FdType;
17
18 pub fn make_tcp_listener(fd: FdType) -> io::Result<TcpListener> {
19 Ok(unsafe { FromRawSocket::from_raw_socket(fd) })
20 }
21
22 pub fn make_udp_socket(fd: FdType) -> io::Result<UdpSocket> {
23 Ok(unsafe { FromRawSocket::from_raw_socket(fd) })
24 }
25
26 #[repr(C)]
27 pub struct IO_STATUS_BLOCK {
28 status: *const c_void,
29 information: *const c_ulong,
30 }
31
32 #[repr(C)]
33 pub struct FILE_INFORMATION {
34 port: HANDLE,
35 key: *const c_void,
36 }
37
38 #[link(name = "ntdll")]
39 extern "system" {
40 #[link_name = "NtSetInformationFile"]
41 fn NtSetInformationFile(
42 file_handle: HANDLE,
43 block: *mut IO_STATUS_BLOCK,
44 file_info: *mut FILE_INFORMATION,
45 len: c_ulong,
46 cls: c_uint,
47 ) -> NTSTATUS;
48 }
49
50 /// This detaches a socket from IOCP.
51 ///
52 /// This only works on windows 8.1 and later. In ealier windows
53 /// versions it's not possible to detach the socket until shutdown
54 /// which means that stuff that uses IOCP (like tokio) cannot be
55 /// used for reloading type scenarios.
56 unsafe fn detach_from_iocp(sock: FdType) {
57 let mut status_block: IO_STATUS_BLOCK = mem::zeroed();
58 let mut file_info: FILE_INFORMATION = mem::zeroed();
59
60 // FileReplaceCompletionInformation
61 NtSetInformationFile(
62 sock as HANDLE,
63 &mut status_block,
64 &mut file_info,
65 mem::size_of::<FILE_INFORMATION>() as c_ulong,
66 61,
67 );
68 }
69
70 pub fn get_fds() -> Option<Vec<FdType>> {
71 let addr: SocketAddr = env::var("SYSTEMFD_SOCKET_SERVER")
72 .ok()
73 .and_then(|x| x.parse().ok())?;
74 let secret: Uuid = env::var("SYSTEMFD_SOCKET_SECRET")
75 .ok()
76 .and_then(|x| x.parse().ok())?;
77
78 env::remove_var("SYSTEMFD_SOCKET_SERVER");
79 env::remove_var("SYSTEMFD_SOCKET_SECRET");
80
81 let mut data = Vec::new();
82 let proto_len = mem::size_of::<WSAPROTOCOL_INFOW>();
83
84 let mut listener = TcpStream::connect(addr).ok()?;
85 let pid = unsafe { GetCurrentProcessId() };
86 listener
87 .write_all(format!("{}|{}", secret, pid).as_bytes())
88 .ok()?;
89 listener.shutdown(Shutdown::Write).ok()?;
90 listener.read_to_end(&mut data).ok()?;
91
92 let items = data.len() / proto_len;
93 let mut rv = Vec::new();
94
95 for idx in 0..items {
96 let offset = idx * proto_len;
97 let proto_info: &mut WSAPROTOCOL_INFOW =
98 unsafe { mem::transmute(data[offset..offset + proto_len].as_ptr()) };
99 unsafe {
100 let sock = WSASocketW(
101 FROM_PROTOCOL_INFO,
102 FROM_PROTOCOL_INFO,
103 FROM_PROTOCOL_INFO,
104 proto_info,
105 0,
106 WSA_FLAG_OVERLAPPED,
107 ) as FdType;
108 if sock == 0 {
109 return None;
110 }
111 detach_from_iocp(sock);
112 rv.push(sock);
113 }
114 }
115
116 Some(rv)
117 }