]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/unix/mod.rs
Add test for MIR range matching.
[rust.git] / src / libstd / sys / unix / 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
14 use io::{self, ErrorKind};
15 use libc;
16 use num::One;
17 use ops::Neg;
18
19 #[cfg(target_os = "android")]   pub use os::android as platform;
20 #[cfg(target_os = "bitrig")]    pub use os::bitrig as platform;
21 #[cfg(target_os = "dragonfly")] pub use os::dragonfly as platform;
22 #[cfg(target_os = "freebsd")]   pub use os::freebsd as platform;
23 #[cfg(target_os = "ios")]       pub use os::ios as platform;
24 #[cfg(target_os = "linux")]     pub use os::linux as platform;
25 #[cfg(target_os = "macos")]     pub use os::macos as platform;
26 #[cfg(target_os = "nacl")]      pub use os::nacl as platform;
27 #[cfg(target_os = "netbsd")]    pub use os::netbsd as platform;
28 #[cfg(target_os = "openbsd")]   pub use os::openbsd as platform;
29
30 pub mod backtrace;
31 pub mod condvar;
32 pub mod ext;
33 pub mod fd;
34 pub mod fs;
35 pub mod mutex;
36 pub mod net;
37 pub mod os;
38 pub mod os_str;
39 pub mod pipe;
40 pub mod process;
41 pub mod rwlock;
42 pub mod stack_overflow;
43 pub mod thread;
44 pub mod thread_local;
45 pub mod time;
46 pub mod stdio;
47
48 #[cfg(not(target_os = "nacl"))]
49 pub fn init() {
50     use libc::signal;
51     // By default, some platforms will send a *signal* when an EPIPE error
52     // would otherwise be delivered. This runtime doesn't install a SIGPIPE
53     // handler, causing it to kill the program, which isn't exactly what we
54     // want!
55     //
56     // Hence, we set SIGPIPE to ignore when the program starts up in order
57     // to prevent this problem.
58     unsafe {
59         assert!(signal(libc::SIGPIPE, libc::SIG_IGN) != !0);
60     }
61 }
62 #[cfg(target_os = "nacl")]
63 pub fn init() { }
64
65 pub fn decode_error_kind(errno: i32) -> ErrorKind {
66     match errno as libc::c_int {
67         libc::ECONNREFUSED => ErrorKind::ConnectionRefused,
68         libc::ECONNRESET => ErrorKind::ConnectionReset,
69         libc::EPERM | libc::EACCES => ErrorKind::PermissionDenied,
70         libc::EPIPE => ErrorKind::BrokenPipe,
71         libc::ENOTCONN => ErrorKind::NotConnected,
72         libc::ECONNABORTED => ErrorKind::ConnectionAborted,
73         libc::EADDRNOTAVAIL => ErrorKind::AddrNotAvailable,
74         libc::EADDRINUSE => ErrorKind::AddrInUse,
75         libc::ENOENT => ErrorKind::NotFound,
76         libc::EINTR => ErrorKind::Interrupted,
77         libc::EINVAL => ErrorKind::InvalidInput,
78         libc::ETIMEDOUT => ErrorKind::TimedOut,
79         libc::EEXIST => ErrorKind::AlreadyExists,
80
81         // These two constants can have the same value on some systems,
82         // but different values on others, so we can't use a match
83         // clause
84         x if x == libc::EAGAIN || x == libc::EWOULDBLOCK =>
85             ErrorKind::WouldBlock,
86
87         _ => ErrorKind::Other,
88     }
89 }
90
91 pub fn cvt<T: One + PartialEq + Neg<Output=T>>(t: T) -> io::Result<T> {
92     let one: T = T::one();
93     if t == -one {
94         Err(io::Error::last_os_error())
95     } else {
96         Ok(t)
97     }
98 }
99
100 pub fn cvt_r<T, F>(mut f: F) -> io::Result<T>
101     where T: One + PartialEq + Neg<Output=T>, F: FnMut() -> T
102 {
103     loop {
104         match cvt(f()) {
105             Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
106             other => return other,
107         }
108     }
109 }