1 // Copyright 2013 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.
26 use std::c_str::CString;
29 use std::io::net::ip::SocketAddr;
30 use std::io::process::ProcessConfig;
31 use std::io::signal::Signum;
34 use std::rt::rtio::{RtioTcpStream, RtioTcpListener, RtioUdpSocket};
35 use std::rt::rtio::{RtioUnixListener, RtioPipe, RtioFileStream, RtioProcess};
36 use std::rt::rtio::{RtioSignal, RtioTTY, CloseBehavior, RtioTimer};
37 use ai = std::io::net::addrinfo;
40 pub use self::file::FileDesc;
41 pub use self::process::Process;
45 // Native I/O implementations
52 #[path = "file_unix.rs"]
55 #[path = "file_win32.rs"]
58 #[cfg(target_os = "macos")]
59 #[cfg(target_os = "freebsd")]
60 #[cfg(target_os = "android")]
61 #[cfg(target_os = "linux")]
62 #[path = "timer_unix.rs"]
65 #[cfg(target_os = "win32")]
66 #[path = "timer_win32.rs"]
70 #[path = "pipe_unix.rs"]
74 #[path = "pipe_win32.rs"]
77 #[cfg(unix)] #[path = "c_unix.rs"] mod c;
78 #[cfg(windows)] #[path = "c_win32.rs"] mod c;
80 pub type IoResult<T> = Result<T, IoError>;
82 fn unimpl() -> IoError {
84 kind: io::IoUnavailable,
85 desc: "unimplemented I/O interface",
90 fn last_error() -> IoError {
94 // unix has nonzero values as errors
95 fn mkerr_libc(ret: libc::c_int) -> IoResult<()> {
103 // windows has zero values as errors
105 fn mkerr_winbool(ret: libc::c_int) -> IoResult<()> {
115 fn retry(f: || -> libc::c_int) -> libc::c_int {
118 -1 if os::errno() as int == libc::WSAEINTR as int => {}
126 fn retry(f: || -> libc::c_int) -> libc::c_int {
129 -1 if os::errno() as int == libc::EINTR as int => {}
135 fn keep_going(data: &[u8], f: |*u8, uint| -> i64) -> i64 {
136 let origamt = data.len();
137 let mut data = data.as_ptr();
138 let mut amt = origamt;
140 let ret = retry(|| f(data, amt) as libc::c_int);
143 } else if ret != -1 {
145 data = unsafe { data.offset(ret as int) };
150 return (origamt - amt) as i64;
153 /// Implementation of rt::rtio's IoFactory trait to generate handles to the
154 /// native I/O functionality.
155 pub struct IoFactory {
156 cannot_construct_outside_of_this_module: ()
160 pub fn new() -> IoFactory {
162 IoFactory { cannot_construct_outside_of_this_module: () }
166 impl rtio::IoFactory for IoFactory {
168 fn tcp_connect(&mut self, addr: SocketAddr,
169 timeout: Option<u64>) -> IoResult<Box<RtioTcpStream:Send>> {
170 net::TcpStream::connect(addr, timeout).map(|s| {
171 box s as Box<RtioTcpStream:Send>
174 fn tcp_bind(&mut self, addr: SocketAddr)
175 -> IoResult<Box<RtioTcpListener:Send>> {
176 net::TcpListener::bind(addr).map(|s| {
177 box s as Box<RtioTcpListener:Send>
180 fn udp_bind(&mut self, addr: SocketAddr)
181 -> IoResult<Box<RtioUdpSocket:Send>> {
182 net::UdpSocket::bind(addr).map(|u| box u as Box<RtioUdpSocket:Send>)
184 fn unix_bind(&mut self, path: &CString)
185 -> IoResult<Box<RtioUnixListener:Send>> {
186 pipe::UnixListener::bind(path).map(|s| {
187 box s as Box<RtioUnixListener:Send>
190 fn unix_connect(&mut self, path: &CString,
191 timeout: Option<u64>) -> IoResult<Box<RtioPipe:Send>> {
192 pipe::UnixStream::connect(path, timeout).map(|s| {
193 box s as Box<RtioPipe:Send>
196 fn get_host_addresses(&mut self, host: Option<&str>, servname: Option<&str>,
197 hint: Option<ai::Hint>) -> IoResult<Vec<ai::Info>> {
198 addrinfo::GetAddrInfoRequest::run(host, servname, hint)
201 // filesystem operations
202 fn fs_from_raw_fd(&mut self, fd: c_int, close: CloseBehavior)
203 -> Box<RtioFileStream:Send> {
204 let close = match close {
205 rtio::CloseSynchronously | rtio::CloseAsynchronously => true,
206 rtio::DontClose => false
208 box file::FileDesc::new(fd, close) as Box<RtioFileStream:Send>
210 fn fs_open(&mut self, path: &CString, fm: io::FileMode, fa: io::FileAccess)
211 -> IoResult<Box<RtioFileStream:Send>> {
212 file::open(path, fm, fa).map(|fd| box fd as Box<RtioFileStream:Send>)
214 fn fs_unlink(&mut self, path: &CString) -> IoResult<()> {
217 fn fs_stat(&mut self, path: &CString) -> IoResult<io::FileStat> {
220 fn fs_mkdir(&mut self, path: &CString,
221 mode: io::FilePermission) -> IoResult<()> {
222 file::mkdir(path, mode)
224 fn fs_chmod(&mut self, path: &CString,
225 mode: io::FilePermission) -> IoResult<()> {
226 file::chmod(path, mode)
228 fn fs_rmdir(&mut self, path: &CString) -> IoResult<()> {
231 fn fs_rename(&mut self, path: &CString, to: &CString) -> IoResult<()> {
232 file::rename(path, to)
234 fn fs_readdir(&mut self, path: &CString, _flags: c_int) -> IoResult<Vec<Path>> {
237 fn fs_lstat(&mut self, path: &CString) -> IoResult<io::FileStat> {
240 fn fs_chown(&mut self, path: &CString, uid: int, gid: int) -> IoResult<()> {
241 file::chown(path, uid, gid)
243 fn fs_readlink(&mut self, path: &CString) -> IoResult<Path> {
246 fn fs_symlink(&mut self, src: &CString, dst: &CString) -> IoResult<()> {
247 file::symlink(src, dst)
249 fn fs_link(&mut self, src: &CString, dst: &CString) -> IoResult<()> {
252 fn fs_utime(&mut self, src: &CString, atime: u64,
253 mtime: u64) -> IoResult<()> {
254 file::utime(src, atime, mtime)
258 fn timer_init(&mut self) -> IoResult<Box<RtioTimer:Send>> {
259 timer::Timer::new().map(|t| box t as Box<RtioTimer:Send>)
261 fn spawn(&mut self, config: ProcessConfig)
262 -> IoResult<(Box<RtioProcess:Send>,
263 Vec<Option<Box<RtioPipe:Send>>>)> {
264 process::Process::spawn(config).map(|(p, io)| {
265 (box p as Box<RtioProcess:Send>,
266 io.move_iter().map(|p| p.map(|p| {
267 box p as Box<RtioPipe:Send>
271 fn kill(&mut self, pid: libc::pid_t, signum: int) -> IoResult<()> {
272 process::Process::kill(pid, signum)
274 fn pipe_open(&mut self, fd: c_int) -> IoResult<Box<RtioPipe:Send>> {
275 Ok(box file::FileDesc::new(fd, true) as Box<RtioPipe:Send>)
277 fn tty_open(&mut self, fd: c_int, _readable: bool)
278 -> IoResult<Box<RtioTTY:Send>> {
279 if unsafe { libc::isatty(fd) } != 0 {
280 Ok(box file::FileDesc::new(fd, true) as Box<RtioTTY:Send>)
283 kind: io::MismatchedFileTypeForOperation,
284 desc: "file descriptor is not a TTY",
289 fn signal(&mut self, _signal: Signum, _channel: Sender<Signum>)
290 -> IoResult<Box<RtioSignal:Send>> {