1 // Copyright 2013-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.
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.
11 //! Native thread-blocking I/O implementation
13 //! This module contains the implementation of native thread-blocking
14 //! implementations of I/O on all platforms. This module is not intended to be
15 //! used directly, but rather the rust runtime will fall back to using it if
18 //! Rust code normally runs inside of green tasks with a local scheduler using
19 //! asynchronous I/O to cooperate among tasks. This model is not always
20 //! available, however, and that's where these native implementations come into
21 //! play. The only dependencies of these modules are the normal system libraries
22 //! that you would find on the respective platform.
24 #![allow(non_snake_case)]
26 use libc::{mod, c_int};
27 use std::c_str::CString;
29 use std::rt::rtio::{mod, IoResult, IoError};
33 pub use self::file::FileDesc;
34 pub use self::process::Process;
38 // Native I/O implementations
45 #[path = "file_unix.rs"]
48 #[path = "file_windows.rs"]
51 #[cfg(target_os = "macos")]
52 #[cfg(target_os = "ios")]
53 #[cfg(target_os = "freebsd")]
54 #[cfg(target_os = "dragonfly")]
55 #[cfg(target_os = "android")]
56 #[cfg(target_os = "linux")]
57 #[path = "timer_unix.rs"]
60 #[cfg(target_os = "windows")]
61 #[path = "timer_windows.rs"]
65 #[path = "pipe_unix.rs"]
69 #[path = "pipe_windows.rs"]
73 #[path = "tty_windows.rs"]
76 #[cfg(unix)] #[path = "c_unix.rs"] mod c;
77 #[cfg(windows)] #[path = "c_windows.rs"] mod c;
79 fn unimpl() -> IoError {
80 #[cfg(unix)] use libc::ENOSYS as ERROR;
81 #[cfg(windows)] use libc::ERROR_CALL_NOT_IMPLEMENTED as ERROR;
85 detail: Some("not yet supported by the `native` runtime, maybe try `green`.".to_string()),
89 fn last_error() -> IoError {
90 let errno = os::errno() as uint;
92 code: os::errno() as uint,
94 detail: Some(os::error_string(errno)),
98 // unix has nonzero values as errors
99 fn mkerr_libc <Int: num::Zero>(ret: Int) -> IoResult<()> {
107 // windows has zero values as errors
109 fn mkerr_winbool(ret: libc::c_int) -> IoResult<()> {
119 fn retry<I> (f: || -> I) -> I { f() } // PR rust-lang/rust/#17020
123 fn retry<I: PartialEq + num::One + Neg<I>> (f: || -> I) -> I {
124 let minus_one = -num::one::<I>();
127 if n == minus_one && os::errno() == libc::EINTR as int { }
133 fn keep_going(data: &[u8], f: |*const u8, uint| -> i64) -> i64 {
134 let origamt = data.len();
135 let mut data = data.as_ptr();
136 let mut amt = origamt;
138 let ret = retry(|| f(data, amt));
141 } else if ret != -1 {
143 data = unsafe { data.offset(ret as int) };
148 return (origamt - amt) as i64;
151 /// Implementation of rt::rtio's IoFactory trait to generate handles to the
152 /// native I/O functionality.
153 pub struct IoFactory {
154 _cannot_construct_outside_of_this_module: ()
158 pub fn new() -> IoFactory {
160 IoFactory { _cannot_construct_outside_of_this_module: () }
164 impl rtio::IoFactory for IoFactory {
166 fn tcp_connect(&mut self, addr: rtio::SocketAddr,
167 timeout: Option<u64>)
168 -> IoResult<Box<rtio::RtioTcpStream + Send>>
170 net::TcpStream::connect(addr, timeout).map(|s| {
171 box s as Box<rtio::RtioTcpStream + Send>
174 fn tcp_bind(&mut self, addr: rtio::SocketAddr)
175 -> IoResult<Box<rtio::RtioTcpListener + Send>> {
176 net::TcpListener::bind(addr).map(|s| {
177 box s as Box<rtio::RtioTcpListener + Send>
180 fn udp_bind(&mut self, addr: rtio::SocketAddr)
181 -> IoResult<Box<rtio::RtioUdpSocket + Send>> {
182 net::UdpSocket::bind(addr).map(|u| {
183 box u as Box<rtio::RtioUdpSocket + Send>
186 fn unix_bind(&mut self, path: &CString)
187 -> IoResult<Box<rtio::RtioUnixListener + Send>> {
188 pipe::UnixListener::bind(path).map(|s| {
189 box s as Box<rtio::RtioUnixListener + Send>
192 fn unix_connect(&mut self, path: &CString,
193 timeout: Option<u64>) -> IoResult<Box<rtio::RtioPipe + Send>> {
194 pipe::UnixStream::connect(path, timeout).map(|s| {
195 box s as Box<rtio::RtioPipe + Send>
198 fn get_host_addresses(&mut self, host: Option<&str>, servname: Option<&str>,
199 hint: Option<rtio::AddrinfoHint>)
200 -> IoResult<Vec<rtio::AddrinfoInfo>>
202 addrinfo::GetAddrInfoRequest::run(host, servname, hint)
205 // filesystem operations
206 fn fs_from_raw_fd(&mut self, fd: c_int, close: rtio::CloseBehavior)
207 -> Box<rtio::RtioFileStream + Send> {
208 let close = match close {
209 rtio::CloseSynchronously | rtio::CloseAsynchronously => true,
210 rtio::DontClose => false
212 box file::FileDesc::new(fd, close) as Box<rtio::RtioFileStream + Send>
214 fn fs_open(&mut self, path: &CString, fm: rtio::FileMode,
215 fa: rtio::FileAccess)
216 -> IoResult<Box<rtio::RtioFileStream + Send>>
218 file::open(path, fm, fa).map(|fd| box fd as Box<rtio::RtioFileStream + Send>)
220 fn fs_unlink(&mut self, path: &CString) -> IoResult<()> {
223 fn fs_stat(&mut self, path: &CString) -> IoResult<rtio::FileStat> {
226 fn fs_mkdir(&mut self, path: &CString, mode: uint) -> IoResult<()> {
227 file::mkdir(path, mode)
229 fn fs_chmod(&mut self, path: &CString, mode: uint) -> IoResult<()> {
230 file::chmod(path, mode)
232 fn fs_rmdir(&mut self, path: &CString) -> IoResult<()> {
235 fn fs_rename(&mut self, path: &CString, to: &CString) -> IoResult<()> {
236 file::rename(path, to)
238 fn fs_readdir(&mut self, path: &CString, _flags: c_int) -> IoResult<Vec<CString>> {
241 fn fs_lstat(&mut self, path: &CString) -> IoResult<rtio::FileStat> {
244 fn fs_chown(&mut self, path: &CString, uid: int, gid: int) -> IoResult<()> {
245 file::chown(path, uid, gid)
247 fn fs_readlink(&mut self, path: &CString) -> IoResult<CString> {
250 fn fs_symlink(&mut self, src: &CString, dst: &CString) -> IoResult<()> {
251 file::symlink(src, dst)
253 fn fs_link(&mut self, src: &CString, dst: &CString) -> IoResult<()> {
256 fn fs_utime(&mut self, src: &CString, atime: u64,
257 mtime: u64) -> IoResult<()> {
258 file::utime(src, atime, mtime)
262 fn timer_init(&mut self) -> IoResult<Box<rtio::RtioTimer + Send>> {
263 timer::Timer::new().map(|t| box t as Box<rtio::RtioTimer + Send>)
265 fn spawn(&mut self, cfg: rtio::ProcessConfig)
266 -> IoResult<(Box<rtio::RtioProcess + Send>,
267 Vec<Option<Box<rtio::RtioPipe + Send>>>)> {
268 process::Process::spawn(cfg).map(|(p, io)| {
269 (box p as Box<rtio::RtioProcess + Send>,
270 io.move_iter().map(|p| p.map(|p| {
271 box p as Box<rtio::RtioPipe + Send>
275 fn kill(&mut self, pid: libc::pid_t, signum: int) -> IoResult<()> {
276 process::Process::kill(pid, signum)
278 fn pipe_open(&mut self, fd: c_int) -> IoResult<Box<rtio::RtioPipe + Send>> {
279 Ok(box file::FileDesc::new(fd, true) as Box<rtio::RtioPipe + Send>)
282 fn tty_open(&mut self, fd: c_int, _readable: bool)
283 -> IoResult<Box<rtio::RtioTTY + Send>> {
284 if unsafe { libc::isatty(fd) } != 0 {
285 Ok(box file::FileDesc::new(fd, true) as Box<rtio::RtioTTY + Send>)
288 code: libc::ENOTTY as uint,
295 fn tty_open(&mut self, fd: c_int, _readable: bool)
296 -> IoResult<Box<rtio::RtioTTY + Send>> {
298 Ok(box tty::WindowsTTY::new(fd) as Box<rtio::RtioTTY + Send>)
301 code: libc::ERROR_INVALID_HANDLE as uint,
307 fn signal(&mut self, _signal: int, _cb: Box<rtio::Callback>)
308 -> IoResult<Box<rtio::RtioSignal + Send>> {