]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/windows/mod.rs
Auto merge of #22541 - Manishearth:rollup, r=Gankro
[rust.git] / src / libstd / sys / windows / mod.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 #![allow(missing_docs)]
12 #![allow(non_camel_case_types)]
13 #![allow(non_snake_case)]
14
15 use prelude::v1::*;
16
17 use ffi::OsStr;
18 use io::{self, ErrorKind};
19 use libc;
20 use mem;
21 use old_io::{self, IoResult, IoError};
22 use num::Int;
23 use os::windows::OsStrExt;
24 use sync::{Once, ONCE_INIT};
25
26 macro_rules! helper_init { (static $name:ident: Helper<$m:ty>) => (
27     static $name: Helper<$m> = Helper {
28         lock: ::sync::MUTEX_INIT,
29         cond: ::sync::CONDVAR_INIT,
30         chan: ::cell::UnsafeCell { value: 0 as *mut ::sync::mpsc::Sender<$m> },
31         signal: ::cell::UnsafeCell { value: 0 },
32         initialized: ::cell::UnsafeCell { value: false },
33         shutdown: ::cell::UnsafeCell { value: false },
34     };
35 ) }
36
37 pub mod backtrace;
38 pub mod c;
39 pub mod condvar;
40 pub mod ext;
41 pub mod fs;
42 pub mod fs2;
43 pub mod handle;
44 pub mod helper_signal;
45 pub mod mutex;
46 pub mod net;
47 pub mod os;
48 pub mod os_str;
49 pub mod pipe;
50 pub mod pipe2;
51 pub mod process;
52 pub mod process2;
53 pub mod rwlock;
54 pub mod stack_overflow;
55 pub mod sync;
56 pub mod tcp;
57 pub mod thread;
58 pub mod thread_local;
59 pub mod time;
60 pub mod timer;
61 pub mod tty;
62 pub mod udp;
63
64 pub mod addrinfo {
65     pub use sys_common::net::get_host_addresses;
66     pub use sys_common::net::get_address_name;
67 }
68
69 // FIXME: move these to c module
70 pub type sock_t = libc::SOCKET;
71 pub type wrlen = libc::c_int;
72 pub type msglen_t = libc::c_int;
73 pub unsafe fn close_sock(sock: sock_t) { let _ = libc::closesocket(sock); }
74
75 // windows has zero values as errors
76 fn mkerr_winbool(ret: libc::c_int) -> IoResult<()> {
77     if ret == 0 {
78         Err(last_error())
79     } else {
80         Ok(())
81     }
82 }
83
84 pub fn last_error() -> IoError {
85     let errno = os::errno() as i32;
86     let mut err = decode_error(errno);
87     err.detail = Some(os::error_string(errno));
88     err
89 }
90
91 pub fn last_net_error() -> IoError {
92     let errno = unsafe { c::WSAGetLastError() as i32 };
93     let mut err = decode_error(errno);
94     err.detail = Some(os::error_string(errno));
95     err
96 }
97
98 pub fn last_gai_error(_errno: i32) -> IoError {
99     last_net_error()
100 }
101
102 /// Convert an `errno` value into a high-level error variant and description.
103 pub fn decode_error(errno: i32) -> IoError {
104     let (kind, desc) = match errno {
105         libc::EOF => (old_io::EndOfFile, "end of file"),
106         libc::ERROR_NO_DATA => (old_io::BrokenPipe, "the pipe is being closed"),
107         libc::ERROR_FILE_NOT_FOUND => (old_io::FileNotFound, "file not found"),
108         libc::ERROR_INVALID_NAME => (old_io::InvalidInput, "invalid file name"),
109         libc::WSAECONNREFUSED => (old_io::ConnectionRefused, "connection refused"),
110         libc::WSAECONNRESET => (old_io::ConnectionReset, "connection reset"),
111         libc::ERROR_ACCESS_DENIED | libc::WSAEACCES =>
112             (old_io::PermissionDenied, "permission denied"),
113         libc::WSAEWOULDBLOCK => {
114             (old_io::ResourceUnavailable, "resource temporarily unavailable")
115         }
116         libc::WSAENOTCONN => (old_io::NotConnected, "not connected"),
117         libc::WSAECONNABORTED => (old_io::ConnectionAborted, "connection aborted"),
118         libc::WSAEADDRNOTAVAIL => (old_io::ConnectionRefused, "address not available"),
119         libc::WSAEADDRINUSE => (old_io::ConnectionRefused, "address in use"),
120         libc::ERROR_BROKEN_PIPE => (old_io::EndOfFile, "the pipe has ended"),
121         libc::ERROR_OPERATION_ABORTED =>
122             (old_io::TimedOut, "operation timed out"),
123         libc::WSAEINVAL => (old_io::InvalidInput, "invalid argument"),
124         libc::ERROR_CALL_NOT_IMPLEMENTED =>
125             (old_io::IoUnavailable, "function not implemented"),
126         libc::ERROR_INVALID_HANDLE =>
127             (old_io::MismatchedFileTypeForOperation,
128              "invalid handle provided to function"),
129         libc::ERROR_NOTHING_TO_TERMINATE =>
130             (old_io::InvalidInput, "no process to kill"),
131         libc::ERROR_ALREADY_EXISTS =>
132             (old_io::PathAlreadyExists, "path already exists"),
133
134         // libuv maps this error code to EISDIR. we do too. if it is found
135         // to be incorrect, we can add in some more machinery to only
136         // return this message when ERROR_INVALID_FUNCTION after certain
137         // Windows calls.
138         libc::ERROR_INVALID_FUNCTION => (old_io::InvalidInput,
139                                          "illegal operation on a directory"),
140
141         _ => (old_io::OtherIoError, "unknown error")
142     };
143     IoError { kind: kind, desc: desc, detail: None }
144 }
145
146 pub fn decode_error_detailed(errno: i32) -> IoError {
147     let mut err = decode_error(errno);
148     err.detail = Some(os::error_string(errno));
149     err
150 }
151
152 pub fn decode_error_kind(errno: i32) -> ErrorKind {
153     match errno as libc::c_int {
154         libc::ERROR_ACCESS_DENIED => ErrorKind::PermissionDenied,
155         libc::ERROR_ALREADY_EXISTS => ErrorKind::PathAlreadyExists,
156         libc::ERROR_BROKEN_PIPE => ErrorKind::BrokenPipe,
157         libc::ERROR_FILE_NOT_FOUND => ErrorKind::FileNotFound,
158         libc::ERROR_INVALID_FUNCTION => ErrorKind::InvalidInput,
159         libc::ERROR_INVALID_HANDLE => ErrorKind::MismatchedFileTypeForOperation,
160         libc::ERROR_INVALID_NAME => ErrorKind::InvalidInput,
161         libc::ERROR_NOTHING_TO_TERMINATE => ErrorKind::InvalidInput,
162         libc::ERROR_NO_DATA => ErrorKind::BrokenPipe,
163         libc::ERROR_OPERATION_ABORTED => ErrorKind::TimedOut,
164
165         libc::WSAEACCES => ErrorKind::PermissionDenied,
166         libc::WSAEADDRINUSE => ErrorKind::ConnectionRefused,
167         libc::WSAEADDRNOTAVAIL => ErrorKind::ConnectionRefused,
168         libc::WSAECONNABORTED => ErrorKind::ConnectionAborted,
169         libc::WSAECONNREFUSED => ErrorKind::ConnectionRefused,
170         libc::WSAECONNRESET => ErrorKind::ConnectionReset,
171         libc::WSAEINVAL => ErrorKind::InvalidInput,
172         libc::WSAENOTCONN => ErrorKind::NotConnected,
173         libc::WSAEWOULDBLOCK => ErrorKind::ResourceUnavailable,
174
175         _ => ErrorKind::Other,
176     }
177 }
178
179
180 #[inline]
181 pub fn retry<I, F>(f: F) -> I where F: FnOnce() -> I { f() } // PR rust-lang/rust/#17020
182
183 pub fn ms_to_timeval(ms: u64) -> libc::timeval {
184     libc::timeval {
185         tv_sec: (ms / 1000) as libc::c_long,
186         tv_usec: ((ms % 1000) * 1000) as libc::c_long,
187     }
188 }
189
190 pub fn wouldblock() -> bool {
191     let err = os::errno();
192     err == libc::WSAEWOULDBLOCK as i32
193 }
194
195 pub fn set_nonblocking(fd: sock_t, nb: bool) -> IoResult<()> {
196     let mut set = nb as libc::c_ulong;
197     if unsafe { c::ioctlsocket(fd, c::FIONBIO, &mut set) != 0 } {
198         Err(last_error())
199     } else {
200         Ok(())
201     }
202 }
203
204 pub fn init_net() {
205     unsafe {
206         static START: Once = ONCE_INIT;
207
208         START.call_once(|| {
209             let mut data: c::WSADATA = mem::zeroed();
210             let ret = c::WSAStartup(0x202, // version 2.2
211                                     &mut data);
212             assert_eq!(ret, 0);
213         });
214     }
215 }
216
217 pub fn unimpl() -> IoError {
218     IoError {
219         kind: old_io::IoUnavailable,
220         desc: "operation is not implemented",
221         detail: None,
222     }
223 }
224
225 fn to_utf16(s: Option<&str>) -> IoResult<Vec<u16>> {
226     match s {
227         Some(s) => Ok(to_utf16_os(OsStr::from_str(s))),
228         None => Err(IoError {
229             kind: old_io::InvalidInput,
230             desc: "valid unicode input required",
231             detail: None,
232         }),
233     }
234 }
235
236 fn to_utf16_os(s: &OsStr) -> Vec<u16> {
237     let mut v: Vec<_> = s.encode_wide().collect();
238     v.push(0);
239     v
240 }
241
242 // Many Windows APIs follow a pattern of where we hand the a buffer and then
243 // they will report back to us how large the buffer should be or how many bytes
244 // currently reside in the buffer. This function is an abstraction over these
245 // functions by making them easier to call.
246 //
247 // The first callback, `f1`, is yielded a (pointer, len) pair which can be
248 // passed to a syscall. The `ptr` is valid for `len` items (u16 in this case).
249 // The closure is expected to return what the syscall returns which will be
250 // interpreted by this function to determine if the syscall needs to be invoked
251 // again (with more buffer space).
252 //
253 // Once the syscall has completed (errors bail out early) the second closure is
254 // yielded the data which has been read from the syscall. The return value
255 // from this closure is then the return value of the function.
256 fn fill_utf16_buf_base<F1, F2, T>(mut f1: F1, f2: F2) -> Result<T, ()>
257     where F1: FnMut(*mut u16, libc::DWORD) -> libc::DWORD,
258           F2: FnOnce(&[u16]) -> T
259 {
260     // Start off with a stack buf but then spill over to the heap if we end up
261     // needing more space.
262     let mut stack_buf = [0u16; 512];
263     let mut heap_buf = Vec::new();
264     unsafe {
265         let mut n = stack_buf.len();
266         loop {
267             let buf = if n <= stack_buf.len() {
268                 &mut stack_buf[..]
269             } else {
270                 let extra = n - heap_buf.len();
271                 heap_buf.reserve(extra);
272                 heap_buf.set_len(n);
273                 &mut heap_buf[..]
274             };
275
276             // This function is typically called on windows API functions which
277             // will return the correct length of the string, but these functions
278             // also return the `0` on error. In some cases, however, the
279             // returned "correct length" may actually be 0!
280             //
281             // To handle this case we call `SetLastError` to reset it to 0 and
282             // then check it again if we get the "0 error value". If the "last
283             // error" is still 0 then we interpret it as a 0 length buffer and
284             // not an actual error.
285             c::SetLastError(0);
286             let k = match f1(buf.as_mut_ptr(), n as libc::DWORD) {
287                 0 if libc::GetLastError() == 0 => 0,
288                 0 => return Err(()),
289                 n => n,
290             } as usize;
291             if k == n && libc::GetLastError() ==
292                             libc::ERROR_INSUFFICIENT_BUFFER as libc::DWORD {
293                 n *= 2;
294             } else if k >= n {
295                 n = k;
296             } else {
297                 return Ok(f2(&buf[..k]))
298             }
299         }
300     }
301 }
302
303 fn fill_utf16_buf<F1, F2, T>(f1: F1, f2: F2) -> IoResult<T>
304     where F1: FnMut(*mut u16, libc::DWORD) -> libc::DWORD,
305           F2: FnOnce(&[u16]) -> T
306 {
307     fill_utf16_buf_base(f1, f2).map_err(|()| IoError::last_error())
308 }
309
310 fn fill_utf16_buf_new<F1, F2, T>(f1: F1, f2: F2) -> io::Result<T>
311     where F1: FnMut(*mut u16, libc::DWORD) -> libc::DWORD,
312           F2: FnOnce(&[u16]) -> T
313 {
314     fill_utf16_buf_base(f1, f2).map_err(|()| io::Error::last_os_error())
315 }
316
317 fn os2path(s: &[u16]) -> Path {
318     // FIXME: this should not be a panicking conversion (aka path reform)
319     Path::new(String::from_utf16(s).unwrap())
320 }
321
322 pub fn truncate_utf16_at_nul<'a>(v: &'a [u16]) -> &'a [u16] {
323     match v.iter().position(|c| *c == 0) {
324         // don't include the 0
325         Some(i) => &v[..i],
326         None => v
327     }
328 }
329
330 fn cvt<I: Int>(i: I) -> io::Result<I> {
331     if i == Int::zero() {
332         Err(io::Error::last_os_error())
333     } else {
334         Ok(i)
335     }
336 }
337
338 fn ms_to_filetime(ms: u64) -> libc::FILETIME {
339     // A FILETIME is a count of 100 nanosecond intervals, so we multiply by
340     // 10000 b/c there are 10000 intervals in 1 ms
341     let ms = ms * 10000;
342     libc::FILETIME {
343         dwLowDateTime: ms as u32,
344         dwHighDateTime: (ms >> 32) as u32,
345     }
346 }