]> git.lizzy.rs Git - rust.git/blob - src/libnative/io/c_win32.rs
Rolling up PRs in the queue
[rust.git] / src / libnative / io / c_win32.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! C definitions used by libnative that don't belong in liblibc
12
13 #![allow(type_overflow)]
14
15 use libc;
16
17 pub static WSADESCRIPTION_LEN: uint = 256;
18 pub static WSASYS_STATUS_LEN: uint = 128;
19 pub static FIONBIO: libc::c_long = 0x8004667e;
20 static FD_SETSIZE: uint = 64;
21 pub static MSG_DONTWAIT: libc::c_int = 0;
22
23 #[repr(C)]
24 pub struct WSADATA {
25     pub wVersion: libc::WORD,
26     pub wHighVersion: libc::WORD,
27     pub szDescription: [u8, ..WSADESCRIPTION_LEN + 1],
28     pub szSystemStatus: [u8, ..WSASYS_STATUS_LEN + 1],
29     pub iMaxSockets: u16,
30     pub iMaxUdpDg: u16,
31     pub lpVendorInfo: *u8,
32 }
33
34 pub type LPWSADATA = *mut WSADATA;
35
36 #[repr(C)]
37 pub struct fd_set {
38     fd_count: libc::c_uint,
39     fd_array: [libc::SOCKET, ..FD_SETSIZE],
40 }
41
42 pub fn fd_set(set: &mut fd_set, s: libc::SOCKET) {
43     set.fd_array[set.fd_count as uint] = s;
44     set.fd_count += 1;
45 }
46
47 #[link(name = "ws2_32")]
48 extern "system" {
49     pub fn WSAStartup(wVersionRequested: libc::WORD,
50                       lpWSAData: LPWSADATA) -> libc::c_int;
51     pub fn WSAGetLastError() -> libc::c_int;
52
53     pub fn ioctlsocket(s: libc::SOCKET, cmd: libc::c_long,
54                        argp: *mut libc::c_ulong) -> libc::c_int;
55     pub fn select(nfds: libc::c_int,
56                   readfds: *fd_set,
57                   writefds: *fd_set,
58                   exceptfds: *fd_set,
59                   timeout: *libc::timeval) -> libc::c_int;
60     pub fn getsockopt(sockfd: libc::SOCKET,
61                       level: libc::c_int,
62                       optname: libc::c_int,
63                       optval: *mut libc::c_char,
64                       optlen: *mut libc::c_int) -> libc::c_int;
65
66     pub fn CancelIo(hFile: libc::HANDLE) -> libc::BOOL;
67     pub fn CancelIoEx(hFile: libc::HANDLE,
68                       lpOverlapped: libc::LPOVERLAPPED) -> libc::BOOL;
69 }
70
71 pub mod compat {
72     use std::intrinsics::{atomic_store_relaxed, transmute};
73     use libc::types::os::arch::extra::{LPCWSTR, HMODULE, LPCSTR, LPVOID};
74
75     extern "system" {
76         fn GetModuleHandleW(lpModuleName: LPCWSTR) -> HMODULE;
77         fn GetProcAddress(hModule: HMODULE, lpProcName: LPCSTR) -> LPVOID;
78     }
79
80     // store_func() is idempotent, so using relaxed ordering for the atomics
81     // should be enough.  This way, calling a function in this compatibility
82     // layer (after it's loaded) shouldn't be any slower than a regular DLL
83     // call.
84     unsafe fn store_func(ptr: *mut uint, module: &str, symbol: &str, fallback: uint) {
85         let module = module.to_utf16().append_one(0);
86         symbol.with_c_str(|symbol| {
87             let handle = GetModuleHandleW(module.as_ptr());
88             let func: uint = transmute(GetProcAddress(handle, symbol));
89             atomic_store_relaxed(ptr, if func == 0 {
90                 fallback
91             } else {
92                 func
93             })
94         })
95     }
96
97     /// Macro for creating a compatibility fallback for a Windows function
98     ///
99     /// # Example
100     /// ```
101     /// compat_fn!(adll32::SomeFunctionW(_arg: LPCWSTR) {
102     ///     // Fallback implementation
103     /// })
104     /// ```
105     ///
106     /// Note that arguments unused by the fallback implementation should not be called `_` as
107     /// they are used to be passed to the real function if available.
108     macro_rules! compat_fn(
109         ($module:ident::$symbol:ident($($argname:ident: $argtype:ty),*)
110                                       -> $rettype:ty $fallback:block) => (
111             #[inline(always)]
112             pub unsafe fn $symbol($($argname: $argtype),*) -> $rettype {
113                 static mut ptr: extern "system" fn($($argname: $argtype),*) -> $rettype = thunk;
114
115                 extern "system" fn thunk($($argname: $argtype),*) -> $rettype {
116                     unsafe {
117                         ::io::c::compat::store_func(&mut ptr as *mut _ as *mut uint,
118                                                     stringify!($module),
119                                                     stringify!($symbol),
120                                                     fallback as uint);
121                         ::std::intrinsics::atomic_load_relaxed(&ptr)($($argname),*)
122                     }
123                 }
124
125                 extern "system" fn fallback($($argname: $argtype),*) -> $rettype $fallback
126
127                 ::std::intrinsics::atomic_load_relaxed(&ptr)($($argname),*)
128             }
129         );
130
131         ($module:ident::$symbol:ident($($argname:ident: $argtype:ty),*) $fallback:block) => (
132             compat_fn!($module::$symbol($($argname: $argtype),*) -> () $fallback)
133         )
134     )
135
136     /// Compatibility layer for functions in `kernel32.dll`
137     ///
138     /// Latest versions of Windows this is needed for:
139     ///
140     /// * `CreateSymbolicLinkW`: Windows XP, Windows Server 2003
141     /// * `GetFinalPathNameByHandleW`: Windows XP, Windows Server 2003
142     pub mod kernel32 {
143         use libc::types::os::arch::extra::{DWORD, LPCWSTR, BOOLEAN, HANDLE};
144         use libc::consts::os::extra::ERROR_CALL_NOT_IMPLEMENTED;
145
146         extern "system" {
147             fn SetLastError(dwErrCode: DWORD);
148         }
149
150         compat_fn!(kernel32::CreateSymbolicLinkW(_lpSymlinkFileName: LPCWSTR,
151                                                  _lpTargetFileName: LPCWSTR,
152                                                  _dwFlags: DWORD) -> BOOLEAN {
153             unsafe { SetLastError(ERROR_CALL_NOT_IMPLEMENTED as DWORD); }
154             0
155         })
156
157         compat_fn!(kernel32::GetFinalPathNameByHandleW(_hFile: HANDLE,
158                                                        _lpszFilePath: LPCWSTR,
159                                                        _cchFilePath: DWORD,
160                                                        _dwFlags: DWORD) -> DWORD {
161             unsafe { SetLastError(ERROR_CALL_NOT_IMPLEMENTED as DWORD); }
162             0
163         })
164     }
165 }