diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2015-04-09 17:42:22 -0700 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2015-04-14 10:14:11 -0700 |
| commit | bf4e77d4b543632ca4df8fdd7092850dffc3954b (patch) | |
| tree | c4b56d2a5974e1b3bf4bfc8b7ca1a62d64c2c341 /src/libstd/sys/unix | |
| parent | dabf0c6371d3b193664f58746fa27c1835a010f3 (diff) | |
| download | rust-bf4e77d4b543632ca4df8fdd7092850dffc3954b.tar.gz rust-bf4e77d4b543632ca4df8fdd7092850dffc3954b.zip | |
std: Remove old_io/old_path/rand modules
This commit entirely removes the old I/O, path, and rand modules. All functionality has been deprecated and unstable for quite some time now!
Diffstat (limited to 'src/libstd/sys/unix')
| -rw-r--r-- | src/libstd/sys/unix/ext.rs | 81 | ||||
| -rw-r--r-- | src/libstd/sys/unix/fs.rs | 409 | ||||
| -rw-r--r-- | src/libstd/sys/unix/mod.rs | 111 | ||||
| -rw-r--r-- | src/libstd/sys/unix/os.rs | 12 | ||||
| -rw-r--r-- | src/libstd/sys/unix/pipe.rs | 328 | ||||
| -rw-r--r-- | src/libstd/sys/unix/process.rs | 627 | ||||
| -rw-r--r-- | src/libstd/sys/unix/process2.rs | 4 | ||||
| -rw-r--r-- | src/libstd/sys/unix/tcp.rs | 164 | ||||
| -rw-r--r-- | src/libstd/sys/unix/timer.rs | 294 | ||||
| -rw-r--r-- | src/libstd/sys/unix/tty.rs | 81 | ||||
| -rw-r--r-- | src/libstd/sys/unix/udp.rs | 11 |
11 files changed, 6 insertions, 2116 deletions
diff --git a/src/libstd/sys/unix/ext.rs b/src/libstd/sys/unix/ext.rs index fa0f14b4807..0fbcf3aee61 100644 --- a/src/libstd/sys/unix/ext.rs +++ b/src/libstd/sys/unix/ext.rs @@ -15,14 +15,12 @@ //! //! # Example //! -//! ```rust,ignore -//! #![feature(globs)] -//! -//! use std::old_io::fs::File; +//! ```no_run +//! use std::fs::File; //! use std::os::unix::prelude::*; //! //! fn main() { -//! let f = File::create(&Path::new("foo.txt")).unwrap(); +//! let f = File::create("foo.txt").unwrap(); //! let fd = f.as_raw_fd(); //! //! // use fd with native unix bindings @@ -34,7 +32,6 @@ /// Unix-specific extensions to general I/O primitives #[stable(feature = "rust1", since = "1.0.0")] pub mod io { - #[allow(deprecated)] use old_io; use fs; use libc; use net; @@ -82,14 +79,6 @@ pub mod io { unsafe fn from_raw_fd(fd: RawFd) -> Self; } - #[allow(deprecated)] - #[stable(feature = "rust1", since = "1.0.0")] - impl AsRawFd for old_io::fs::File { - fn as_raw_fd(&self) -> RawFd { - self.as_inner().fd() - } - } - #[stable(feature = "rust1", since = "1.0.0")] impl AsRawFd for fs::File { fn as_raw_fd(&self) -> RawFd { @@ -103,70 +92,6 @@ pub mod io { } } - #[allow(deprecated)] - #[stable(feature = "rust1", since = "1.0.0")] - impl AsRawFd for old_io::pipe::PipeStream { - fn as_raw_fd(&self) -> RawFd { - self.as_inner().fd() - } - } - - #[allow(deprecated)] - #[stable(feature = "rust1", since = "1.0.0")] - impl AsRawFd for old_io::net::pipe::UnixStream { - fn as_raw_fd(&self) -> RawFd { - self.as_inner().fd() - } - } - - #[allow(deprecated)] - #[stable(feature = "rust1", since = "1.0.0")] - impl AsRawFd for old_io::net::pipe::UnixListener { - fn as_raw_fd(&self) -> RawFd { - self.as_inner().fd() - } - } - - #[allow(deprecated)] - #[stable(feature = "rust1", since = "1.0.0")] - impl AsRawFd for old_io::net::pipe::UnixAcceptor { - fn as_raw_fd(&self) -> RawFd { - self.as_inner().fd() - } - } - - #[stable(feature = "rust1", since = "1.0.0")] - #[allow(deprecated)] - impl AsRawFd for old_io::net::tcp::TcpStream { - fn as_raw_fd(&self) -> RawFd { - self.as_inner().fd() - } - } - - #[stable(feature = "rust1", since = "1.0.0")] - #[allow(deprecated)] - impl AsRawFd for old_io::net::tcp::TcpListener { - fn as_raw_fd(&self) -> RawFd { - self.as_inner().fd() - } - } - - #[stable(feature = "rust1", since = "1.0.0")] - #[allow(deprecated)] - impl AsRawFd for old_io::net::tcp::TcpAcceptor { - fn as_raw_fd(&self) -> RawFd { - self.as_inner().fd() - } - } - - #[allow(deprecated)] - #[stable(feature = "rust1", since = "1.0.0")] - impl AsRawFd for old_io::net::udp::UdpSocket { - fn as_raw_fd(&self) -> RawFd { - self.as_inner().fd() - } - } - #[stable(feature = "rust1", since = "1.0.0")] impl AsRawFd for net::TcpStream { fn as_raw_fd(&self) -> RawFd { *self.as_inner().socket().as_inner() } diff --git a/src/libstd/sys/unix/fs.rs b/src/libstd/sys/unix/fs.rs deleted file mode 100644 index 6121105f10b..00000000000 --- a/src/libstd/sys/unix/fs.rs +++ /dev/null @@ -1,409 +0,0 @@ -// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or -// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license -// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! Blocking posix-based file I/O -#![allow(deprecated)] - -#![allow(deprecated)] // this module itself is essentially deprecated - -use prelude::v1::*; - -use ffi::{CString, CStr}; -use old_io::{FilePermission, Write, UnstableFileStat, Open, FileAccess, FileMode}; -use old_io::{IoResult, FileStat, SeekStyle}; -use old_io::{Read, Truncate, SeekCur, SeekSet, ReadWrite, SeekEnd, Append}; -use old_io; -use old_path::{Path, GenericPath}; -use libc::{self, c_int, c_void}; -use mem; -use ptr; -use sys::retry; -use sys_common::{keep_going, eof, mkerr_libc}; - -pub type fd_t = libc::c_int; - -pub struct FileDesc { - /// The underlying C file descriptor. - fd: fd_t, - - /// Whether to close the file descriptor on drop. - close_on_drop: bool, -} - -impl FileDesc { - pub fn new(fd: fd_t, close_on_drop: bool) -> FileDesc { - FileDesc { fd: fd, close_on_drop: close_on_drop } - } - - pub fn read(&self, buf: &mut [u8]) -> IoResult<usize> { - let ret = retry(|| unsafe { - libc::read(self.fd(), - buf.as_mut_ptr() as *mut libc::c_void, - buf.len() as libc::size_t) - }); - if ret == 0 { - Err(eof()) - } else if ret < 0 { - Err(super::last_error()) - } else { - Ok(ret as usize) - } - } - pub fn write(&self, buf: &[u8]) -> IoResult<()> { - let ret = keep_going(buf, |buf, len| { - unsafe { - libc::write(self.fd(), buf as *const libc::c_void, - len as libc::size_t) as i64 - } - }); - if ret < 0 { - Err(super::last_error()) - } else { - Ok(()) - } - } - - pub fn fd(&self) -> fd_t { self.fd } - - pub fn seek(&self, pos: i64, whence: SeekStyle) -> IoResult<u64> { - let whence = match whence { - SeekSet => libc::SEEK_SET, - SeekEnd => libc::SEEK_END, - SeekCur => libc::SEEK_CUR, - }; - let n = unsafe { libc::lseek(self.fd(), pos as libc::off_t, whence) }; - if n < 0 { - Err(super::last_error()) - } else { - Ok(n as u64) - } - } - - pub fn tell(&self) -> IoResult<u64> { - let n = unsafe { libc::lseek(self.fd(), 0, libc::SEEK_CUR) }; - if n < 0 { - Err(super::last_error()) - } else { - Ok(n as u64) - } - } - - pub fn fsync(&self) -> IoResult<()> { - mkerr_libc(retry(|| unsafe { libc::fsync(self.fd()) })) - } - - pub fn datasync(&self) -> IoResult<()> { - return mkerr_libc(os_datasync(self.fd())); - - #[cfg(any(target_os = "macos", target_os = "ios"))] - fn os_datasync(fd: c_int) -> c_int { - unsafe { libc::fcntl(fd, libc::F_FULLFSYNC) } - } - #[cfg(target_os = "linux")] - fn os_datasync(fd: c_int) -> c_int { - retry(|| unsafe { libc::fdatasync(fd) }) - } - #[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "linux")))] - fn os_datasync(fd: c_int) -> c_int { - retry(|| unsafe { libc::fsync(fd) }) - } - } - - pub fn truncate(&self, offset: i64) -> IoResult<()> { - mkerr_libc(retry(|| unsafe { - libc::ftruncate(self.fd(), offset as libc::off_t) - })) - } - - pub fn fstat(&self) -> IoResult<FileStat> { - let mut stat: libc::stat = unsafe { mem::zeroed() }; - match unsafe { libc::fstat(self.fd(), &mut stat) } { - 0 => Ok(mkstat(&stat)), - _ => Err(super::last_error()), - } - } - - /// Extract the actual filedescriptor without closing it. - pub fn unwrap(self) -> fd_t { - let fd = self.fd; - unsafe { mem::forget(self) }; - fd - } -} - -impl Drop for FileDesc { - fn drop(&mut self) { - // closing stdio file handles makes no sense, so never do it. Also, note - // that errors are ignored when closing a file descriptor. The reason - // for this is that if an error occurs we don't actually know if the - // file descriptor was closed or not, and if we retried (for something - // like EINTR), we might close another valid file descriptor (opened - // after we closed ours. - if self.close_on_drop && self.fd > libc::STDERR_FILENO { - let n = unsafe { libc::close(self.fd) }; - if n != 0 { - println!("error {} when closing file descriptor {}", n, self.fd); - } - } - } -} - -fn cstr(path: &Path) -> IoResult<CString> { - Ok(try!(CString::new(path.as_vec()))) -} - -pub fn open(path: &Path, fm: FileMode, fa: FileAccess) -> IoResult<FileDesc> { - let flags = match fm { - Open => 0, - Append => libc::O_APPEND, - Truncate => libc::O_TRUNC, - }; - // Opening with a write permission must silently create the file. - let (flags, mode) = match fa { - Read => (flags | libc::O_RDONLY, 0), - Write => (flags | libc::O_WRONLY | libc::O_CREAT, - libc::S_IRUSR | libc::S_IWUSR), - ReadWrite => (flags | libc::O_RDWR | libc::O_CREAT, - libc::S_IRUSR | libc::S_IWUSR), - }; - - let path = try!(cstr(path)); - match retry(|| unsafe { libc::open(path.as_ptr(), flags, mode) }) { - -1 => Err(super::last_error()), - fd => Ok(FileDesc::new(fd, true)), - } -} - -pub fn mkdir(p: &Path, mode: usize) -> IoResult<()> { - let p = try!(cstr(p)); - mkerr_libc(unsafe { libc::mkdir(p.as_ptr(), mode as libc::mode_t) }) -} - -pub fn readdir(p: &Path) -> IoResult<Vec<Path>> { - use libc::{dirent_t}; - use libc::{opendir, readdir_r, closedir}; - - fn prune(root: &CString, dirs: Vec<Path>) -> Vec<Path> { - let root = Path::new(root); - - dirs.into_iter().filter(|path| { - path.as_vec() != b"." && path.as_vec() != b".." - }).map(|path| root.join(path)).collect() - } - - extern { - fn rust_dirent_t_size() -> libc::c_int; - fn rust_list_dir_val(ptr: *mut dirent_t) -> *const libc::c_char; - } - - let size = unsafe { rust_dirent_t_size() }; - let mut buf = Vec::<u8>::with_capacity(size as usize); - let ptr = buf.as_mut_ptr() as *mut dirent_t; - - let p = try!(CString::new(p.as_vec())); - let dir_ptr = unsafe {opendir(p.as_ptr())}; - - if dir_ptr as usize != 0 { - let mut paths = vec!(); - let mut entry_ptr = ptr::null_mut(); - while unsafe { readdir_r(dir_ptr, ptr, &mut entry_ptr) == 0 } { - if entry_ptr.is_null() { break } - paths.push(unsafe { - Path::new(CStr::from_ptr(rust_list_dir_val(entry_ptr)).to_bytes()) - }); - } - assert_eq!(unsafe { closedir(dir_ptr) }, 0); - Ok(prune(&p, paths)) - } else { - Err(super::last_error()) - } -} - -pub fn unlink(p: &Path) -> IoResult<()> { - let p = try!(cstr(p)); - mkerr_libc(unsafe { libc::unlink(p.as_ptr()) }) -} - -pub fn rename(old: &Path, new: &Path) -> IoResult<()> { - let old = try!(cstr(old)); - let new = try!(cstr(new)); - mkerr_libc(unsafe { - libc::rename(old.as_ptr(), new.as_ptr()) - }) -} - -pub fn chmod(p: &Path, mode: usize) -> IoResult<()> { - let p = try!(cstr(p)); - mkerr_libc(retry(|| unsafe { - libc::chmod(p.as_ptr(), mode as libc::mode_t) - })) -} - -pub fn rmdir(p: &Path) -> IoResult<()> { - let p = try!(cstr(p)); - mkerr_libc(unsafe { libc::rmdir(p.as_ptr()) }) -} - -pub fn chown(p: &Path, uid: isize, gid: isize) -> IoResult<()> { - let p = try!(cstr(p)); - mkerr_libc(retry(|| unsafe { - libc::chown(p.as_ptr(), uid as libc::uid_t, gid as libc::gid_t) - })) -} - -pub fn readlink(p: &Path) -> IoResult<Path> { - let c_path = try!(cstr(p)); - let p = c_path.as_ptr(); - let mut len = unsafe { libc::pathconf(p as *mut _, libc::_PC_NAME_MAX) }; - if len == -1 { - len = 1024; // FIXME: read PATH_MAX from C ffi? - } - let mut buf: Vec<u8> = Vec::with_capacity(len as usize); - match unsafe { - libc::readlink(p, buf.as_ptr() as *mut libc::c_char, - len as libc::size_t) as libc::c_int - } { - -1 => Err(super::last_error()), - n => { - assert!(n > 0); - unsafe { buf.set_len(n as usize); } - Ok(Path::new(buf)) - } - } -} - -pub fn symlink(src: &Path, dst: &Path) -> IoResult<()> { - let src = try!(cstr(src)); - let dst = try!(cstr(dst)); - mkerr_libc(unsafe { libc::symlink(src.as_ptr(), dst.as_ptr()) }) -} - -pub fn link(src: &Path, dst: &Path) -> IoResult<()> { - let src = try!(cstr(src)); - let dst = try!(cstr(dst)); - mkerr_libc(unsafe { libc::link(src.as_ptr(), dst.as_ptr()) }) -} - -fn mkstat(stat: &libc::stat) -> FileStat { - // FileStat times are in milliseconds - fn mktime(secs: u64, nsecs: u64) -> u64 { secs * 1000 + nsecs / 1000000 } - - fn ctime(stat: &libc::stat) -> u64 { - mktime(stat.st_ctime as u64, stat.st_ctime_nsec as u64) - } - - fn atime(stat: &libc::stat) -> u64 { - mktime(stat.st_atime as u64, stat.st_atime_nsec as u64) - } - - fn mtime(stat: &libc::stat) -> u64 { - mktime(stat.st_mtime as u64, stat.st_mtime_nsec as u64) - } - - #[cfg(not(any(target_os = "linux", target_os = "android")))] - fn flags(stat: &libc::stat) -> u64 { stat.st_flags as u64 } - #[cfg(any(target_os = "linux", target_os = "android"))] - fn flags(_stat: &libc::stat) -> u64 { 0 } - - #[cfg(not(any(target_os = "linux", target_os = "android")))] - fn gen(stat: &libc::stat) -> u64 { stat.st_gen as u64 } - #[cfg(any(target_os = "linux", target_os = "android"))] - fn gen(_stat: &libc::stat) -> u64 { 0 } - - FileStat { - size: stat.st_size as u64, - kind: match (stat.st_mode as libc::mode_t) & libc::S_IFMT { - libc::S_IFREG => old_io::FileType::RegularFile, - libc::S_IFDIR => old_io::FileType::Directory, - libc::S_IFIFO => old_io::FileType::NamedPipe, - libc::S_IFBLK => old_io::FileType::BlockSpecial, - libc::S_IFLNK => old_io::FileType::Symlink, - _ => old_io::FileType::Unknown, - }, - perm: FilePermission::from_bits_truncate(stat.st_mode as u32), - created: ctime(stat), - modified: mtime(stat), - accessed: atime(stat), - unstable: UnstableFileStat { - device: stat.st_dev as u64, - inode: stat.st_ino as u64, - rdev: stat.st_rdev as u64, - nlink: stat.st_nlink as u64, - uid: stat.st_uid as u64, - gid: stat.st_gid as u64, - blksize: stat.st_blksize as u64, - blocks: stat.st_blocks as u64, - flags: flags(stat), - gen: gen(stat), - }, - } -} - -pub fn stat(p: &Path) -> IoResult<FileStat> { - let p = try!(cstr(p)); - let mut stat: libc::stat = unsafe { mem::zeroed() }; - match unsafe { libc::stat(p.as_ptr(), &mut stat) } { - 0 => Ok(mkstat(&stat)), - _ => Err(super::last_error()), - } -} - -pub fn lstat(p: &Path) -> IoResult<FileStat> { - let p = try!(cstr(p)); - let mut stat: libc::stat = unsafe { mem::zeroed() }; - match unsafe { libc::lstat(p.as_ptr(), &mut stat) } { - 0 => Ok(mkstat(&stat)), - _ => Err(super::last_error()), - } -} - -pub fn utime(p: &Path, atime: u64, mtime: u64) -> IoResult<()> { - let p = try!(cstr(p)); - let buf = libc::utimbuf { - actime: (atime / 1000) as libc::time_t, - modtime: (mtime / 1000) as libc::time_t, - }; - mkerr_libc(unsafe { libc::utime(p.as_ptr(), &buf) }) -} - -#[cfg(test)] -mod tests { - use super::FileDesc; - use libc; - use os; - use prelude::v1::*; - - #[cfg_attr(any(target_os = "freebsd", - target_os = "openbsd", - target_os = "bitrig"), - ignore)] - // under some system, pipe(2) will return a bidrectionnal pipe - #[test] - fn test_file_desc() { - // Run this test with some pipes so we don't have to mess around with - // opening or closing files. - let (mut reader, mut writer) = unsafe { ::sys::os::pipe().unwrap() }; - - writer.write(b"test").unwrap(); - let mut buf = [0; 4]; - match reader.read(&mut buf) { - Ok(4) => { - assert_eq!(buf[0], 't' as u8); - assert_eq!(buf[1], 'e' as u8); - assert_eq!(buf[2], 's' as u8); - assert_eq!(buf[3], 't' as u8); - } - r => panic!("invalid read: {:?}", r), - } - - assert!(writer.read(&mut buf).is_err()); - assert!(reader.write(&buf).is_err()); - } -} diff --git a/src/libstd/sys/unix/mod.rs b/src/libstd/sys/unix/mod.rs index e8409bb4fd4..a8a6219f398 100644 --- a/src/libstd/sys/unix/mod.rs +++ b/src/libstd/sys/unix/mod.rs @@ -13,124 +13,30 @@ use prelude::v1::*; -use ffi::CStr; use io::{self, ErrorKind}; use libc; use num::{Int, SignedInt}; -use old_io::{self, IoError}; -use str; -use sys_common::mkerr_libc; pub mod backtrace; pub mod c; pub mod condvar; pub mod ext; pub mod fd; -pub mod fs; // support for std::old_io -pub mod fs2; // support for std::fs -pub mod helper_signal; +pub mod fs2; pub mod mutex; pub mod net; pub mod os; pub mod os_str; -pub mod pipe; pub mod pipe2; -pub mod process; pub mod process2; pub mod rwlock; pub mod stack_overflow; pub mod sync; -pub mod tcp; pub mod thread; pub mod thread_local; pub mod time; -pub mod timer; -pub mod tty; -pub mod udp; pub mod stdio; -pub mod addrinfo { - pub use sys_common::net::get_host_addresses; - pub use sys_common::net::get_address_name; -} - -// FIXME: move these to c module -pub type sock_t = self::fs::fd_t; -pub type wrlen = libc::size_t; -pub type msglen_t = libc::size_t; -pub unsafe fn close_sock(sock: sock_t) { let _ = libc::close(sock); } - -#[allow(deprecated)] -pub fn last_error() -> IoError { - decode_error_detailed(os::errno() as i32) -} - -#[allow(deprecated)] -pub fn last_net_error() -> IoError { - last_error() -} - -extern "system" { - fn gai_strerror(errcode: libc::c_int) -> *const libc::c_char; -} - -#[allow(deprecated)] -pub fn last_gai_error(s: libc::c_int) -> IoError { - - let mut err = decode_error(s); - err.detail = Some(unsafe { - let data = CStr::from_ptr(gai_strerror(s)); - str::from_utf8(data.to_bytes()).unwrap().to_string() - }); - err -} - -/// Convert an `errno` value into a high-level error variant and description. -#[allow(deprecated)] -pub fn decode_error(errno: i32) -> IoError { - // FIXME: this should probably be a bit more descriptive... - let (kind, desc) = match errno { - libc::EOF => (old_io::EndOfFile, "end of file"), - libc::ECONNREFUSED => (old_io::ConnectionRefused, "connection refused"), - libc::ECONNRESET => (old_io::ConnectionReset, "connection reset"), - libc::EPERM | libc::EACCES => - (old_io::PermissionDenied, "permission denied"), - libc::EPIPE => (old_io::BrokenPipe, "broken pipe"), - libc::ENOTCONN => (old_io::NotConnected, "not connected"), - libc::ECONNABORTED => (old_io::ConnectionAborted, "connection aborted"), - libc::EADDRNOTAVAIL => (old_io::ConnectionRefused, "address not available"), - libc::EADDRINUSE => (old_io::ConnectionRefused, "address in use"), - libc::ENOENT => (old_io::FileNotFound, "no such file or directory"), - libc::EISDIR => (old_io::InvalidInput, "illegal operation on a directory"), - libc::ENOSYS => (old_io::IoUnavailable, "function not implemented"), - libc::EINVAL => (old_io::InvalidInput, "invalid argument"), - libc::ENOTTY => - (old_io::MismatchedFileTypeForOperation, - "file descriptor is not a TTY"), - libc::ETIMEDOUT => (old_io::TimedOut, "operation timed out"), - libc::ECANCELED => (old_io::TimedOut, "operation aborted"), - libc::consts::os::posix88::EEXIST => - (old_io::PathAlreadyExists, "path already exists"), - - // These two constants can have the same value on some systems, - // but different values on others, so we can't use a match - // clause - x if x == libc::EAGAIN || x == libc::EWOULDBLOCK => - (old_io::ResourceUnavailable, "resource temporarily unavailable"), - - _ => (old_io::OtherIoError, "unknown error") - }; - IoError { kind: kind, desc: desc, detail: None } -} - -#[allow(deprecated)] -pub fn decode_error_detailed(errno: i32) -> IoError { - let mut err = decode_error(errno); - err.detail = Some(os::error_string(errno)); - err -} - -#[allow(deprecated)] pub fn decode_error_kind(errno: i32) -> ErrorKind { match errno as libc::c_int { libc::ECONNREFUSED => ErrorKind::ConnectionRefused, @@ -199,18 +105,3 @@ pub fn ms_to_timeval(ms: u64) -> libc::timeval { tv_usec: ((ms % 1000) * 1000) as libc::suseconds_t, } } - -#[allow(deprecated)] -pub fn wouldblock() -> bool { - let err = os::errno(); - err == libc::EWOULDBLOCK as i32 || err == libc::EAGAIN as i32 -} - -#[allow(deprecated)] -pub fn set_nonblocking(fd: sock_t, nb: bool) { - let set = nb as libc::c_int; - mkerr_libc(retry(|| unsafe { c::ioctl(fd, c::FIONBIO, &set) })).unwrap(); -} - -// nothing needed on unix platforms -pub fn init_net() {} diff --git a/src/libstd/sys/unix/os.rs b/src/libstd/sys/unix/os.rs index d2220bdec32..1c6a13352ff 100644 --- a/src/libstd/sys/unix/os.rs +++ b/src/libstd/sys/unix/os.rs @@ -22,14 +22,12 @@ use io; use iter; use libc::{self, c_int, c_char, c_void}; use mem; -#[allow(deprecated)] use old_io::{IoError, IoResult}; use ptr; use path::{self, PathBuf}; use slice; use str; use sys::c; use sys::fd; -use sys::fs::FileDesc; use vec; const BUF_BYTES: usize = 2048; @@ -448,16 +446,6 @@ pub fn unsetenv(n: &OsStr) { } } -#[allow(deprecated)] -pub unsafe fn pipe() -> IoResult<(FileDesc, FileDesc)> { - let mut fds = [0; 2]; - if libc::pipe(fds.as_mut_ptr()) == 0 { - Ok((FileDesc::new(fds[0], true), FileDesc::new(fds[1], true))) - } else { - Err(IoError::last_error()) - } -} - pub fn page_size() -> usize { unsafe { libc::sysconf(libc::_SC_PAGESIZE) as usize diff --git a/src/libstd/sys/unix/pipe.rs b/src/libstd/sys/unix/pipe.rs deleted file mode 100644 index f0071295bf2..00000000000 --- a/src/libstd/sys/unix/pipe.rs +++ /dev/null @@ -1,328 +0,0 @@ -// Copyright 2014 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or -// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license -// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#![allow(deprecated)] - -use prelude::v1::*; - -use ffi::CString; -use libc; -use mem; -use sync::{Arc, Mutex}; -use sync::atomic::{AtomicBool, Ordering}; -use old_io::{self, IoResult, IoError}; - -use sys::{self, timer, retry, c, set_nonblocking, wouldblock}; -use sys::fs::{fd_t, FileDesc}; -use sys_common::net::*; -use sys_common::net::SocketStatus::*; -use sys_common::{eof, mkerr_libc}; - -fn unix_socket(ty: libc::c_int) -> IoResult<fd_t> { - match unsafe { libc::socket(libc::AF_UNIX, ty, 0) } { - -1 => Err(super::last_error()), - fd => Ok(fd) - } -} - -fn addr_to_sockaddr_un(addr: &CString, - storage: &mut libc::sockaddr_storage) - -> IoResult<libc::socklen_t> { - // the sun_path length is limited to SUN_LEN (with null) - assert!(mem::size_of::<libc::sockaddr_storage>() >= - mem::size_of::<libc::sockaddr_un>()); - let s = unsafe { &mut *(storage as *mut _ as *mut libc::sockaddr_un) }; - - let len = addr.as_bytes().len(); - if len > s.sun_path.len() - 1 { - return Err(IoError { - kind: old_io::InvalidInput, - desc: "invalid argument: path must be smaller than SUN_LEN", - detail: None, - }) - } - s.sun_family = libc::AF_UNIX as libc::sa_family_t; - for (slot, value) in s.sun_path.iter_mut().zip(addr.as_bytes().iter()) { - *slot = *value as libc::c_char; - } - - // count the null terminator - let len = mem::size_of::<libc::sa_family_t>() + len + 1; - return Ok(len as libc::socklen_t); -} - -struct Inner { - fd: fd_t, - - // Unused on Linux, where this lock is not necessary. - #[allow(dead_code)] - lock: Mutex<()>, -} - -impl Inner { - fn new(fd: fd_t) -> Inner { - Inner { fd: fd, lock: Mutex::new(()) } - } -} - -impl Drop for Inner { - fn drop(&mut self) { unsafe { let _ = libc::close(self.fd); } } -} - -fn connect(addr: &CString, ty: libc::c_int, - timeout: Option<u64>) -> IoResult<Inner> { - let mut storage = unsafe { mem::zeroed() }; - let len = try!(addr_to_sockaddr_un(addr, &mut storage)); - let inner = Inner::new(try!(unix_socket(ty))); - let addrp = &storage as *const _ as *const libc::sockaddr; - - match timeout { - None => { - match retry(|| unsafe { libc::connect(inner.fd, addrp, len) }) { - -1 => Err(super::last_error()), - _ => Ok(inner) - } - } - Some(timeout_ms) => { - try!(connect_timeout(inner.fd, addrp, len, timeout_ms)); - Ok(inner) - } - } -} - -fn bind(addr: &CString, ty: libc::c_int) -> IoResult<Inner> { - let mut storage = unsafe { mem::zeroed() }; - let len = try!(addr_to_sockaddr_un(addr, &mut storage)); - let inner = Inner::new(try!(unix_socket(ty))); - let addrp = &storage as *const _ as *const libc::sockaddr; - match unsafe { - libc::bind(inner.fd, addrp, len) - } { - -1 => Err(super::last_error()), - _ => Ok(inner) - } -} - -//////////////////////////////////////////////////////////////////////////////// -// Unix Streams -//////////////////////////////////////////////////////////////////////////////// - -pub struct UnixStream { - inner: Arc<Inner>, - read_deadline: u64, - write_deadline: u64, -} - -impl UnixStream { - pub fn connect(addr: &CString, - timeout: Option<u64>) -> IoResult<UnixStream> { - connect(addr, libc::SOCK_STREAM, timeout).map(|inner| { - UnixStream::new(Arc::new(inner)) - }) - } - - fn new(inner: Arc<Inner>) -> UnixStream { - UnixStream { - inner: inner, - read_deadline: 0, - write_deadline: 0, - } - } - - pub fn fd(&self) -> fd_t { self.inner.fd } - - #[cfg(target_os = "linux")] - fn lock_nonblocking(&self) {} - - #[cfg(not(target_os = "linux"))] - fn lock_nonblocking<'a>(&'a self) -> Guard<'a> { - let ret = Guard { - fd: self.fd(), - guard: self.inner.lock.lock().unwrap(), - }; - set_nonblocking(self.fd(), true); - ret - } - - pub fn read(&mut self, buf: &mut [u8]) -> IoResult<usize> { - let fd = self.fd(); - let dolock = || self.lock_nonblocking(); - let doread = |nb| unsafe { - let flags = if nb {c::MSG_DONTWAIT} else {0}; - libc::recv(fd, - buf.as_mut_ptr() as *mut libc::c_void, - buf.len() as libc::size_t, - flags) as libc::c_int - }; - read(fd, self.read_deadline, dolock, doread) - } - - pub fn write(&mut self, buf: &[u8]) -> IoResult<()> { - let fd = self.fd(); - let dolock = || self.lock_nonblocking(); - let dowrite = |nb: bool, buf: *const u8, len: usize| unsafe { - let flags = if nb {c::MSG_DONTWAIT} else {0}; - libc::send(fd, - buf as *const _, - len as libc::size_t, - flags) as i64 - }; - match write(fd, self.write_deadline, buf, true, dolock, dowrite) { - Ok(_) => Ok(()), - Err(e) => Err(e) - } - } - - pub fn close_write(&mut self) -> IoResult<()> { - mkerr_libc(unsafe { libc::shutdown(self.fd(), libc::SHUT_WR) }) - } - - pub fn close_read(&mut self) -> IoResult<()> { - mkerr_libc(unsafe { libc::shutdown(self.fd(), libc::SHUT_RD) }) - } - - pub fn set_timeout(&mut self, timeout: Option<u64>) { - let deadline = timeout.map(|a| timer::now() + a).unwrap_or(0); - self.read_deadline = deadline; - self.write_deadline = deadline; - } - - pub fn set_read_timeout(&mut self, timeout: Option<u64>) { - self.read_deadline = timeout.map(|a| timer::now() + a).unwrap_or(0); - } - - pub fn set_write_timeout(&mut self, timeout: Option<u64>) { - self.write_deadline = timeout.map(|a| timer::now() + a).unwrap_or(0); - } -} - -impl Clone for UnixStream { - fn clone(&self) -> UnixStream { - UnixStream::new(self.inner.clone()) - } -} - -//////////////////////////////////////////////////////////////////////////////// -// Unix Listener -//////////////////////////////////////////////////////////////////////////////// - -pub struct UnixListener { - inner: Inner, - path: CString, -} - -// we currently own the CString, so these impls should be safe -unsafe impl Send for UnixListener {} -unsafe impl Sync for UnixListener {} - -impl UnixListener { - pub fn bind(addr: &CString) -> IoResult<UnixListener> { - bind(addr, libc::SOCK_STREAM).map(|fd| { - UnixListener { inner: fd, path: addr.clone() } - }) - } - - pub fn fd(&self) -> fd_t { self.inner.fd } - - pub fn listen(self) -> IoResult<UnixAcceptor> { - match unsafe { libc::listen(self.fd(), 128) } { - -1 => Err(super::last_error()), - - _ => { - let (reader, writer) = try!(unsafe { sys::os::pipe() }); - set_nonblocking(reader.fd(), true); - set_nonblocking(writer.fd(), true); - set_nonblocking(self.fd(), true); - Ok(UnixAcceptor { - inner: Arc::new(AcceptorInner { - listener: self, - reader: reader, - writer: writer, - closed: AtomicBool::new(false), - }), - deadline: 0, - }) - } - } - } -} - -pub struct UnixAcceptor { - inner: Arc<AcceptorInner>, - deadline: u64, -} - -struct AcceptorInner { - listener: UnixListener, - reader: FileDesc, - writer: FileDesc, - closed: AtomicBool, -} - -impl UnixAcceptor { - pub fn fd(&self) -> fd_t { self.inner.listener.fd() } - - pub fn accept(&mut self) -> IoResult<UnixStream> { - let deadline = if self.deadline == 0 {None} else {Some(self.deadline)}; - - while !self.inner.closed.load(Ordering::SeqCst) { - unsafe { - let mut storage: libc::sockaddr_storage = mem::zeroed(); - let storagep = &mut storage as *mut libc::sockaddr_storage; - let size = mem::size_of::<libc::sockaddr_storage>(); - let mut size = size as libc::socklen_t; - match retry(|| { - libc::accept(self.fd(), - storagep as *mut libc::sockaddr, - &mut size as *mut libc::socklen_t) as libc::c_int - }) { - -1 if wouldblock() => {} - -1 => return Err(super::last_error()), - fd => return Ok(UnixStream::new(Arc::new(Inner::new(fd)))), - } - } - try!(await(&[self.fd(), self.inner.reader.fd()], - deadline, Readable)); - } - - Err(eof()) - } - - pub fn set_timeout(&mut self, timeout: Option<u64>) { - self.deadline = timeout.map(|a| timer::now() + a).unwrap_or(0); - } - - pub fn close_accept(&mut self) -> IoResult<()> { - self.inner.closed.store(true, Ordering::SeqCst); - let fd = FileDesc::new(self.inner.writer.fd(), false); - match fd.write(&[0]) { - Ok(..) => Ok(()), - Err(..) if wouldblock() => Ok(()), - Err(e) => Err(e), - } - } -} - -impl Clone for UnixAcceptor { - fn clone(&self) -> UnixAcceptor { - UnixAcceptor { inner: self.inner.clone(), deadline: 0 } - } -} - -impl Drop for UnixListener { - fn drop(&mut self) { - // Unlink the path to the socket to ensure that it doesn't linger. We're - // careful to unlink the path before we close the file descriptor to - // prevent races where we unlink someone else's path. - unsafe { - let _ = libc::unlink(self.path.as_ptr()); - } - } -} diff --git a/src/libstd/sys/unix/process.rs b/src/libstd/sys/unix/process.rs deleted file mode 100644 index 8095325f83d..00000000000 --- a/src/libstd/sys/unix/process.rs +++ /dev/null @@ -1,627 +0,0 @@ -// Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or -// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license -// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#![allow(deprecated)] // this module itself is essentially deprecated - -use prelude::v1::*; -use self::Req::*; - -use collections::HashMap; -use ffi::CString; -use hash::Hash; -use old_io::process::{ProcessExit, ExitStatus, ExitSignal}; -use old_io::{IoResult, EndOfFile}; -use libc::{self, pid_t, c_void, c_int}; -use io; -use mem; -use sys::os; -use old_path::BytesContainer; -use ptr; -use sync::mpsc::{channel, Sender, Receiver}; -use sys::fs::FileDesc; -use sys::{self, retry, c, wouldblock, set_nonblocking, ms_to_timeval}; -use sys_common::helper_thread::Helper; -use sys_common::{AsInner, mkerr_libc, timeout}; - -pub use sys_common::ProcessConfig; - -helper_init! { static HELPER: Helper<Req> } - -/// The unique id of the process (this should never be negative). -pub struct Process { - pub pid: pid_t -} - -enum Req { - NewChild(libc::pid_t, Sender<ProcessExit>, u64), -} - -const CLOEXEC_MSG_FOOTER: &'static [u8] = b"NOEX"; - -impl Process { - pub fn id(&self) -> pid_t { - self.pid - } - - pub unsafe fn kill(&self, signal: isize) -> IoResult<()> { - Process::killpid(self.pid, signal) - } - - pub unsafe fn killpid(pid: pid_t, signal: isize) -> IoResult<()> { - let r = libc::funcs::posix88::signal::kill(pid, signal as c_int); - mkerr_libc(r) - } - - pub fn spawn<K, V, C, P>(cfg: &C, in_fd: Option<P>, - out_fd: Option<P>, err_fd: Option<P>) - -> IoResult<Process> - where C: ProcessConfig<K, V>, P: AsInner<FileDesc>, - K: BytesContainer + Eq + Hash, V: BytesContainer - { - use libc::funcs::posix88::unistd::{fork, dup2, close, chdir, execvp}; - - mod rustrt { - extern { - pub fn rust_unset_sigprocmask(); - } - } - - unsafe fn set_cloexec(fd: c_int) { - let ret = c::ioctl(fd, c::FIOCLEX); - assert_eq!(ret, 0); - } - - #[cfg(all(target_os = "android", target_arch = "aarch64"))] - unsafe fn getdtablesize() -> c_int { - libc::sysconf(libc::consts::os::sysconf::_SC_OPEN_MAX) as c_int - } - #[cfg(not(all(target_os = "android", target_arch = "aarch64")))] - unsafe fn getdtablesize() -> c_int { - libc::funcs::bsd44::getdtablesize() - } - - let dirp = cfg.cwd().map(|c| c.as_ptr()).unwrap_or(ptr::null()); - - // temporary until unboxed closures land - let cfg = unsafe { - mem::transmute::<&ProcessConfig<K,V>,&'static ProcessConfig<K,V>>(cfg) - }; - - with_envp(cfg.env(), move|envp: *const c_void| { - with_argv(cfg.program(), cfg.args(), move|argv: *const *const libc::c_char| unsafe { - let (input, mut output) = try!(sys::os::pipe()); - - // We may use this in the child, so perform allocations before the - // fork - let devnull = b"/dev/null\0"; - - set_cloexec(output.fd()); - - let pid = fork(); - if pid < 0 { - return Err(super::last_error()) - } else if pid > 0 { - #[inline] - fn combine(arr: &[u8]) -> i32 { - let a = arr[0] as u32; - let b = arr[1] as u32; - let c = arr[2] as u32; - let d = arr[3] as u32; - - ((a << 24) | (b << 16) | (c << 8) | (d << 0)) as i32 - } - - let p = Process{ pid: pid }; - drop(output); - let mut bytes = [0; 8]; - return match input.read(&mut bytes) { - Ok(8) => { - assert!(combine(CLOEXEC_MSG_FOOTER) == combine(&bytes[4.. 8]), - "Validation on the CLOEXEC pipe failed: {:?}", bytes); - let errno = combine(&bytes[0.. 4]); - assert!(p.wait(0).is_ok(), "wait(0) should either return Ok or panic"); - Err(super::decode_error(errno)) - } - Err(ref e) if e.kind == EndOfFile => Ok(p), - Err(e) => { - assert!(p.wait(0).is_ok(), "wait(0) should either return Ok or panic"); - panic!("the CLOEXEC pipe failed: {:?}", e) - }, - Ok(..) => { // pipe I/O up to PIPE_BUF bytes should be atomic - assert!(p.wait(0).is_ok(), "wait(0) should either return Ok or panic"); - panic!("short read on the CLOEXEC pipe") - } - }; - } - - // And at this point we've reached a special time in the life of the - // child. The child must now be considered hamstrung and unable to - // do anything other than syscalls really. Consider the following - // scenario: - // - // 1. Thread A of process 1 grabs the malloc() mutex - // 2. Thread B of process 1 forks(), creating thread C - // 3. Thread C of process 2 then attempts to malloc() - // 4. The memory of process 2 is the same as the memory of - // process 1, so the mutex is locked. - // - // This situation looks a lot like deadlock, right? It turns out - // that this is what pthread_atfork() takes care of, which is - // presumably implemented across platforms. The first thing that - // threads to *before* forking is to do things like grab the malloc - // mutex, and then after the fork they unlock it. - // - // Despite this information, libnative's spawn has been witnessed to - // deadlock on both OSX and FreeBSD. I'm not entirely sure why, but - // all collected backtraces point at malloc/free traffic in the - // child spawned process. - // - // For this reason, the block of code below should contain 0 - // invocations of either malloc of free (or their related friends). - // - // As an example of not having malloc/free traffic, we don't close - // this file descriptor by dropping the FileDesc (which contains an - // allocation). Instead we just close it manually. This will never - // have the drop glue anyway because this code never returns (the - // child will either exec() or invoke libc::exit) - let _ = libc::close(input.fd()); - - fn fail(output: &mut FileDesc) -> ! { - let errno = sys::os::errno() as u32; - let bytes = [ - (errno >> 24) as u8, - (errno >> 16) as u8, - (errno >> 8) as u8, - (errno >> 0) as u8, - CLOEXEC_MSG_FOOTER[0], CLOEXEC_MSG_FOOTER[1], - CLOEXEC_MSG_FOOTER[2], CLOEXEC_MSG_FOOTER[3] - ]; - // pipe I/O up to PIPE_BUF bytes should be atomic - assert!(output.write(&bytes).is_ok()); - unsafe { libc::_exit(1) } - } - - rustrt::rust_unset_sigprocmask(); - - // If a stdio file descriptor is set to be ignored (via a -1 file - // descriptor), then we don't actually close it, but rather open - // up /dev/null into that file descriptor. Otherwise, the first file - // descriptor opened up in the child would be numbered as one of the - // stdio file descriptors, which is likely to wreak havoc. - let setup = |src: Option<P>, dst: c_int| { - let src = match src { - None => { - let flags = if dst == libc::STDIN_FILENO { - libc::O_RDONLY - } else { - libc::O_RDWR - }; - libc::open(devnull.as_ptr() as *const _, flags, 0) - } - Some(obj) => { - let fd = obj.as_inner().fd(); - // Leak the memory and the file descriptor. We're in the - // child now an all our resources are going to be - // cleaned up very soon - mem::forget(obj); - fd - } - }; - src != -1 && retry(|| dup2(src, dst)) != -1 - }; - - if !setup(in_fd, libc::STDIN_FILENO) { fail(&mut output) } - if !setup(out_fd, libc::STDOUT_FILENO) { fail(&mut output) } - if !setup(err_fd, libc::STDERR_FILENO) { fail(&mut output) } - - // close all other fds - for fd in (3..getdtablesize()).rev() { - if fd != output.fd() { - let _ = close(fd as c_int); - } - } - - match cfg.gid() { - Some(u) => { - if libc::setgid(u as libc::gid_t) != 0 { - fail(&mut output); - } - } - None => {} - } - match cfg.uid() { - Some(u) => { - // When dropping privileges from root, the `setgroups` call - // will remove any extraneous groups. If we don't call this, - // then even though our uid has dropped, we may still have - // groups that enable us to do super-user things. This will - // fail if we aren't root, so don't bother checking the - // return value, this is just done as an optimistic - // privilege dropping function. - extern { - fn setgroups(ngroups: libc::c_int, - ptr: *const libc::c_void) -> libc::c_int; - } - let _ = setgroups(0, ptr::null()); - - if libc::setuid(u as libc::uid_t) != 0 { - fail(&mut output); - } - } - None => {} - } - if cfg.detach() { - // Don't check the error of setsid because it fails if we're the - // process leader already. We just forked so it shouldn't return - // error, but ignore it anyway. - let _ = libc::setsid(); - } - if !dirp.is_null() && chdir(dirp) == -1 { - fail(&mut output); - } - if !envp.is_null() { - *sys::os::environ() = envp as *const _; - } - let _ = execvp(*argv, argv as *mut _); - fail(&mut output); - }) - }) - } - - pub fn wait(&self, deadline: u64) -> IoResult<ProcessExit> { - use cmp; - use sync::mpsc::TryRecvError; - - static mut WRITE_FD: libc::c_int = 0; - - let mut status = 0 as c_int; - if deadline == 0 { - return match retry(|| unsafe { c::waitpid(self.pid, &mut status, 0) }) { - -1 => panic!("unknown waitpid error: {:?}", super::last_error()), - _ => Ok(translate_status(status)), - } - } - - // On unix, wait() and its friends have no timeout parameters, so there is - // no way to time out a thread in wait(). From some googling and some - // thinking, it appears that there are a few ways to handle timeouts in - // wait(), but the only real reasonable one for a multi-threaded program is - // to listen for SIGCHLD. - // - // With this in mind, the waiting mechanism with a timeout barely uses - // waitpid() at all. There are a few times that waitpid() is invoked with - // WNOHANG, but otherwise all the necessary blocking is done by waiting for - // a SIGCHLD to arrive (and that blocking has a timeout). Note, however, - // that waitpid() is still used to actually reap the child. - // - // Signal handling is super tricky in general, and this is no exception. Due - // to the async nature of SIGCHLD, we use the self-pipe trick to transmit - // data out of the signal handler to the rest of the application. The first - // idea would be to have each thread waiting with a timeout to read this - // output file descriptor, but a write() is akin to a signal(), not a - // broadcast(), so it would only wake up one thread, and possibly the wrong - // thread. Hence a helper thread is used. - // - // The helper thread here is responsible for farming requests for a - // waitpid() with a timeout, and then processing all of the wait requests. - // By guaranteeing that only this helper thread is reading half of the - // self-pipe, we're sure that we'll never lose a SIGCHLD. This helper thread - // is also responsible for select() to wait for incoming messages or - // incoming SIGCHLD messages, along with passing an appropriate timeout to - // select() to wake things up as necessary. - // - // The ordering of the following statements is also very purposeful. First, - // we must be guaranteed that the helper thread is booted and available to - // receive SIGCHLD signals, and then we must also ensure that we do a - // nonblocking waitpid() at least once before we go ask the sigchld helper. - // This prevents the race where the child exits, we boot the helper, and - // then we ask for the child's exit status (never seeing a sigchld). - // - // The actual communication between the helper thread and this thread is - // quite simple, just a channel moving data around. - - HELPER.boot(register_sigchld, waitpid_helper); - - match self.try_wait() { - Some(ret) => return Ok(ret), - None => {} - } - - let (tx, rx) = channel(); - HELPER.send(NewChild(self.pid, tx, deadline)); - return match rx.recv() { - Ok(e) => Ok(e), - Err(..) => Err(timeout("wait timed out")), - }; - - // Register a new SIGCHLD handler, returning the reading half of the - // self-pipe plus the old handler registered (return value of sigaction). - // - // Be sure to set up the self-pipe first because as soon as we register a - // handler we're going to start receiving signals. - fn register_sigchld() -> (libc::c_int, c::sigaction) { - unsafe { - let mut pipes = [0; 2]; - assert_eq!(libc::pipe(pipes.as_mut_ptr()), 0); - set_nonblocking(pipes[0], true); - set_nonblocking(pipes[1], true); - WRITE_FD = pipes[1]; - - let mut old: c::sigaction = mem::zeroed(); - let mut new: c::sigaction = mem::zeroed(); - new.sa_handler = sigchld_handler; - new.sa_flags = c::SA_NOCLDSTOP; - assert_eq!(c::sigaction(c::SIGCHLD, &new, &mut old), 0); - (pipes[0], old) - } - } - - // Helper thread for processing SIGCHLD messages - fn waitpid_helper(input: libc::c_int, - messages: Receiver<Req>, - (read_fd, old): (libc::c_int, c::sigaction)) { - set_nonblocking(input, true); - let mut set: c::fd_set = unsafe { mem::zeroed() }; - let mut tv: libc::timeval; - let mut active = Vec::<(libc::pid_t, Sender<ProcessExit>, u64)>::new(); - let max = cmp::max(input, read_fd) + 1; - - 'outer: loop { - // Figure out the timeout of our syscall-to-happen. If we're waiting - // for some processes, then they'll have a timeout, otherwise we - // wait indefinitely for a message to arrive. - // - // FIXME: sure would be nice to not have to scan the entire array - let min = active.iter().map(|a| a.2).enumerate().min_by(|p| { - p.1 - }); - let (p, idx) = match min { - Some((idx, deadline)) => { - let now = sys::timer::now(); - let ms = if now < deadline {deadline - now} else {0}; - tv = ms_to_timeval(ms); - (&mut tv as *mut _, idx) - } - None => (ptr::null_mut(), -1), - }; - - // Wait for something to happen - c::fd_set(&mut set, input); - c::fd_set(&mut set, read_fd); - match unsafe { c::select(max, &mut set, ptr::null_mut(), - ptr::null_mut(), p) } { - // interrupted, retry - -1 if os::errno() == libc::EINTR as i32 => continue, - - // We read something, break out and process - 1 | 2 => {} - - // Timeout, the pending request is removed - 0 => { - drop(active.remove(idx)); - continue - } - - n => panic!("error in select {:?} ({:?})", os::errno(), n), - } - - // Process any pending messages - if drain(input) { - loop { - match messages.try_recv() { - Ok(NewChild(pid, tx, deadline)) => { - active.push((pid, tx, deadline)); - } - // Once we've been disconnected it means the main - // thread is exiting (at_exit has run). We could - // still have active waiter for other threads, so - // we're just going to drop them all on the floor. - // This means that they won't receive a "you're - // done" message in which case they'll be considered - // as timed out, but more generally errors will - // start propagating. - Err(TryRecvError::Disconnected) => { - break 'outer; - } - Err(TryRecvError::Empty) => break, - } - } - } - - // If a child exited (somehow received SIGCHLD), then poll all - // children to see if any of them exited. - // - // We also attempt to be responsible netizens when dealing with - // SIGCHLD by invoking any previous SIGCHLD handler instead of just - // ignoring any previous SIGCHLD handler. Note that we don't provide - // a 1:1 mapping of our handler invocations to the previous handler - // invocations because we drain the `read_fd` entirely. This is - // probably OK because the kernel is already allowed to coalesce - // simultaneous signals, we're just doing some extra coalescing. - // - // Another point of note is that this likely runs the signal handler - // on a different thread than the one that received the signal. I - // *think* this is ok at this time. - // - // The main reason for doing this is to allow stdtest to run native - // tests as well. Both libgreen and libnative are running around - // with process timeouts, but libgreen should get there first - // (currently libuv doesn't handle old signal handlers). - if drain(read_fd) { - let i: usize = unsafe { mem::transmute(old.sa_handler) }; - if i != 0 { - assert!(old.sa_flags & c::SA_SIGINFO == 0); - (old.sa_handler)(c::SIGCHLD); - } - - // FIXME: sure would be nice to not have to scan the entire - // array... - active.retain(|&(pid, ref tx, _)| { - let pr = Process { pid: pid }; - match pr.try_wait() { - Some(msg) => { tx.send(msg).unwrap(); false } - None => true, - } - }); - } - } - - // Once this helper thread is done, we re-register the old sigchld - // handler and close our intermediate file descriptors. - unsafe { - assert_eq!(c::sigaction(c::SIGCHLD, &old, ptr::null_mut()), 0); - let _ = libc::close(read_fd); - let _ = libc::close(WRITE_FD); - WRITE_FD = -1; - } - } - - // Drain all pending data from the file descriptor, returning if any data - // could be drained. This requires that the file descriptor is in - // nonblocking mode. - fn drain(fd: libc::c_int) -> bool { - let mut ret = false; - loop { - let mut buf = [0u8; 1]; - match unsafe { - libc::read(fd, buf.as_mut_ptr() as *mut libc::c_void, - buf.len() as libc::size_t) - } { - n if n > 0 => { ret = true; } - 0 => return true, - -1 if wouldblock() => return ret, - n => panic!("bad read {} ({})", - io::Error::last_os_error(), n), - } - } - } - - // Signal handler for SIGCHLD signals, must be async-signal-safe! - // - // This function will write to the writing half of the "self pipe" to wake - // up the helper thread if it's waiting. Note that this write must be - // nonblocking because if it blocks and the reader is the thread we - // interrupted, then we'll deadlock. - // - // When writing, if the write returns EWOULDBLOCK then we choose to ignore - // it. At that point we're guaranteed that there's something in the pipe - // which will wake up the other end at some point, so we just allow this - // signal to be coalesced with the pending signals on the pipe. - extern fn sigchld_handler(_signum: libc::c_int) { - let msg = 1; - match unsafe { - libc::write(WRITE_FD, &msg as *const _ as *const libc::c_void, 1) - } { - 1 => {} - -1 if wouldblock() => {} // see above comments - n => panic!("bad error on write fd: {:?} {:?}", n, os::errno()), - } - } - } - - pub fn try_wait(&self) -> Option<ProcessExit> { - let mut status = 0 as c_int; - match retry(|| unsafe { - c::waitpid(self.pid, &mut status, c::WNOHANG) - }) { - n if n == self.pid => Some(translate_status(status)), - 0 => None, - n => panic!("unknown waitpid error `{:?}`: {:?}", n, - super::last_error()), - } - } -} - -fn with_argv<T,F>(prog: &CString, args: &[CString], - cb: F) - -> T - where F : FnOnce(*const *const libc::c_char) -> T -{ - let mut ptrs: Vec<*const libc::c_char> = Vec::with_capacity(args.len()+1); - - // Convert the CStrings into an array of pointers. Note: the - // lifetime of the various CStrings involved is guaranteed to be - // larger than the lifetime of our invocation of cb, but this is - // technically unsafe as the callback could leak these pointers - // out of our scope. - ptrs.push(prog.as_ptr()); - ptrs.extend(args.iter().map(|tmp| tmp.as_ptr())); - - // Add a terminating null pointer (required by libc). - ptrs.push(ptr::null()); - - cb(ptrs.as_ptr()) -} - -fn with_envp<K,V,T,F>(env: Option<&HashMap<K, V>>, - cb: F) - -> T - where F : FnOnce(*const c_void) -> T, - K : BytesContainer + Eq + Hash, - V : BytesContainer -{ - // On posixy systems we can pass a char** for envp, which is a - // null-terminated array of "k=v\0" strings. Since we must create - // these strings locally, yet expose a raw pointer to them, we - // create a temporary vector to own the CStrings that outlives the - // call to cb. - match env { - Some(env) => { - let mut tmps = Vec::with_capacity(env.len()); - - for pair in env { - let mut kv = Vec::new(); - kv.push_all(pair.0.container_as_bytes()); - kv.push('=' as u8); - kv.push_all(pair.1.container_as_bytes()); - kv.push(0); // terminating null - tmps.push(kv); - } - - // As with `with_argv`, this is unsafe, since cb could leak the pointers. - let mut ptrs: Vec<*const libc::c_char> = - tmps.iter() - .map(|tmp| tmp.as_ptr() as *const libc::c_char) - .collect(); - ptrs.push(ptr::null()); - - cb(ptrs.as_ptr() as *const c_void) - } - _ => cb(ptr::null()) - } -} - -fn translate_status(status: c_int) -> ProcessExit { - #![allow(non_snake_case)] - #[cfg(any(target_os = "linux", target_os = "android"))] - mod imp { - pub fn WIFEXITED(status: i32) -> bool { (status & 0xff) == 0 } - pub fn WEXITSTATUS(status: i32) -> i32 { (status >> 8) & 0xff } - pub fn WTERMSIG(status: i32) -> i32 { status & 0x7f } - } - - #[cfg(any(target_os = "macos", - target_os = "ios", - target_os = "freebsd", - target_os = "dragonfly", - target_os = "bitrig", - target_os = "openbsd"))] - mod imp { - pub fn WIFEXITED(status: i32) -> bool { (status & 0x7f) == 0 } - pub fn WEXITSTATUS(status: i32) -> i32 { status >> 8 } - pub fn WTERMSIG(status: i32) -> i32 { status & 0o177 } - } - - if imp::WIFEXITED(status) { - ExitStatus(imp::WEXITSTATUS(status) as isize) - } else { - ExitSignal(imp::WTERMSIG(status) as isize) - } -} diff --git a/src/libstd/sys/unix/process2.rs b/src/libstd/sys/unix/process2.rs index 0b4e871454d..caa7b4eb29c 100644 --- a/src/libstd/sys/unix/process2.rs +++ b/src/libstd/sys/unix/process2.rs @@ -328,8 +328,8 @@ impl Process { }) { n if n == self.pid => Some(translate_status(status)), 0 => None, - n => panic!("unknown waitpid error `{:?}`: {:?}", n, - super::last_error()), + n => panic!("unknown waitpid error `{}`: {}", n, + io::Error::last_os_error()), } } } diff --git a/src/libstd/sys/unix/tcp.rs b/src/libstd/sys/unix/tcp.rs deleted file mode 100644 index a9f2198208b..00000000000 --- a/src/libstd/sys/unix/tcp.rs +++ /dev/null @@ -1,164 +0,0 @@ -// Copyright 2014 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or -// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license -// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#![allow(deprecated)] - -use prelude::v1::*; - -use old_io::net::ip; -use old_io::IoResult; -use libc; -use mem; -use ptr; -use super::{last_error, last_net_error, retry, sock_t}; -use sync::Arc; -use sync::atomic::{AtomicBool, Ordering}; -use sys::fs::FileDesc; -use sys::{set_nonblocking, wouldblock}; -use sys; -use sys_common; -use sys_common::net; -use sys_common::net::SocketStatus::Readable; - -pub use sys_common::net::TcpStream; - -//////////////////////////////////////////////////////////////////////////////// -// TCP listeners -//////////////////////////////////////////////////////////////////////////////// - -pub struct TcpListener { - pub inner: FileDesc, -} - -unsafe impl Sync for TcpListener {} - -impl TcpListener { - pub fn bind(addr: ip::SocketAddr) -> IoResult<TcpListener> { - let fd = try!(net::socket(addr, libc::SOCK_STREAM)); - let ret = TcpListener { inner: FileDesc::new(fd, true) }; - - let mut storage = unsafe { mem::zeroed() }; - let len = net::addr_to_sockaddr(addr, &mut storage); - let addrp = &storage as *const _ as *const libc::sockaddr; - - // On platforms with Berkeley-derived sockets, this allows - // to quickly rebind a socket, without needing to wait for - // the OS to clean up the previous one. - try!(net::setsockopt(fd, libc::SOL_SOCKET, - libc::SO_REUSEADDR, - 1 as libc::c_int)); - - - match unsafe { libc::bind(fd, addrp, len) } { - -1 => Err(last_error()), - _ => Ok(ret), - } - } - - pub fn fd(&self) -> sock_t { self.inner.fd() } - - pub fn listen(self, backlog: isize) -> IoResult<TcpAcceptor> { - match unsafe { libc::listen(self.fd(), backlog as libc::c_int) } { - -1 => Err(last_net_error()), - _ => { - let (reader, writer) = try!(unsafe { sys::os::pipe() }); - set_nonblocking(reader.fd(), true); - set_nonblocking(writer.fd(), true); - set_nonblocking(self.fd(), true); - Ok(TcpAcceptor { - inner: Arc::new(AcceptorInner { - listener: self, - reader: reader, - writer: writer, - closed: AtomicBool::new(false), - }), - deadline: 0, - }) - } - } - } - - pub fn socket_name(&mut self) -> IoResult<ip::SocketAddr> { - net::sockname(self.fd(), libc::getsockname) - } -} - -pub struct TcpAcceptor { - inner: Arc<AcceptorInner>, - deadline: u64, -} - -struct AcceptorInner { - listener: TcpListener, - reader: FileDesc, - writer: FileDesc, - closed: AtomicBool, -} - -unsafe impl Sync for AcceptorInner {} - -impl TcpAcceptor { - pub fn fd(&self) -> sock_t { self.inner.listener.fd() } - - pub fn accept(&mut self) -> IoResult<TcpStream> { - // In implementing accept, the two main concerns are dealing with - // close_accept() and timeouts. The unix implementation is based on a - // nonblocking accept plus a call to select(). Windows ends up having - // an entirely separate implementation than unix, which is explained - // below. - // - // To implement timeouts, all blocking is done via select() instead of - // accept() by putting the socket in non-blocking mode. Because - // select() takes a timeout argument, we just pass through the timeout - // to select(). - // - // To implement close_accept(), we have a self-pipe to ourselves which - // is passed to select() along with the socket being accepted on. The - // self-pipe is never written to unless close_accept() is called. - let deadline = if self.deadline == 0 {None} else {Some(self.deadline)}; - - while !self.inner.closed.load(Ordering::SeqCst) { - match retry(|| unsafe { - libc::accept(self.fd(), ptr::null_mut(), ptr::null_mut()) - }) { - -1 if wouldblock() => {} - -1 => return Err(last_net_error()), - fd => return Ok(TcpStream::new(fd as sock_t)), - } - try!(net::await(&[self.fd(), self.inner.reader.fd()], - deadline, Readable)); - } - - Err(sys_common::eof()) - } - - pub fn set_timeout(&mut self, timeout: Option<u64>) { - self.deadline = timeout.map(|a| sys::timer::now() + a).unwrap_or(0); - } - - pub fn close_accept(&mut self) -> IoResult<()> { - self.inner.closed.store(true, Ordering::SeqCst); - let fd = FileDesc::new(self.inner.writer.fd(), false); - match fd.write(&[0]) { - Ok(..) => Ok(()), - Err(..) if wouldblock() => Ok(()), - Err(e) => Err(e), - } - } -} - -impl Clone for TcpAcceptor { - fn clone(&self) -> TcpAcceptor { - TcpAcceptor { - inner: self.inner.clone(), - deadline: 0, - } - } -} diff --git a/src/libstd/sys/unix/timer.rs b/src/libstd/sys/unix/timer.rs deleted file mode 100644 index 9309147b15c..00000000000 --- a/src/libstd/sys/unix/timer.rs +++ /dev/null @@ -1,294 +0,0 @@ -// Copyright 2013 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or -// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license -// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! Timers for non-Linux/non-Windows OSes -//! -//! This module implements timers with a worker thread, select(), and a lot of -//! witchcraft that turns out to be horribly inaccurate timers. The unfortunate -//! part is that I'm at a loss of what else to do one these OSes. This is also -//! why Linux has a specialized timerfd implementation and windows has its own -//! implementation (they're more accurate than this one). -//! -//! The basic idea is that there is a worker thread that's communicated to via a -//! channel and a pipe, the pipe is used by the worker thread in a select() -//! syscall with a timeout. The timeout is the "next timer timeout" while the -//! channel is used to send data over to the worker thread. -//! -//! Whenever the call to select() times out, then a channel receives a message. -//! Whenever the call returns that the file descriptor has information, then the -//! channel from timers is drained, enqueuing all incoming requests. -//! -//! The actual implementation of the helper thread is a sorted array of -//! timers in terms of target firing date. The target is the absolute time at -//! which the timer should fire. Timers are then re-enqueued after a firing if -//! the repeat boolean is set. -//! -//! Naturally, all this logic of adding times and keeping track of -//! relative/absolute time is a little lossy and not quite exact. I've done the -//! best I could to reduce the amount of calls to 'now()', but there's likely -//! still inaccuracies trickling in here and there. -//! -//! One of the tricky parts of this implementation is that whenever a timer is -//! acted upon, it must cancel whatever the previous action was (if one is -//! active) in order to act like the other implementations of this timer. In -//! order to do this, the timer's inner pointer is transferred to the worker -//! thread. Whenever the timer is modified, it first takes ownership back from -//! the worker thread in order to modify the same data structure. This has the -//! side effect of "cancelling" the previous requests while allowing a -//! re-enqueuing later on. -//! -//! Note that all time units in this file are in *milliseconds*. - -#![allow(deprecated)] - -use prelude::v1::*; -use self::Req::*; - -use old_io::IoResult; -use libc; -use mem; -use sys::os; -use io; -use ptr; -use sync::atomic::{self, Ordering}; -use sync::mpsc::{channel, Sender, Receiver, TryRecvError}; -use sys::c; -use sys::fs::FileDesc; -use sys_common::helper_thread::Helper; - -helper_init! { static HELPER: Helper<Req> } - -pub trait Callback { - fn call(&mut self); -} - -pub struct Timer { - id: usize, - inner: Option<Box<Inner>>, -} - -pub struct Inner { - cb: Option<Box<Callback + Send>>, - interval: u64, - repeat: bool, - target: u64, - id: usize, -} - -pub enum Req { - // Add a new timer to the helper thread. - NewTimer(Box<Inner>), - - // Remove a timer based on its id and then send it back on the channel - // provided - RemoveTimer(usize, Sender<Box<Inner>>), -} - -// returns the current time (in milliseconds) -pub fn now() -> u64 { - unsafe { - let mut now: libc::timeval = mem::zeroed(); - assert_eq!(c::gettimeofday(&mut now, ptr::null_mut()), 0); - return (now.tv_sec as u64) * 1000 + (now.tv_usec as u64) / 1000; - } -} - -fn helper(input: libc::c_int, messages: Receiver<Req>, _: ()) { - let mut set: c::fd_set = unsafe { mem::zeroed() }; - - let fd = FileDesc::new(input, true); - let mut timeout: libc::timeval = unsafe { mem::zeroed() }; - - // active timers are those which are able to be selected upon (and it's a - // sorted list, and dead timers are those which have expired, but ownership - // hasn't yet been transferred back to the timer itself. - let mut active: Vec<Box<Inner>> = vec![]; - let mut dead = vec![]; - - // inserts a timer into an array of timers (sorted by firing time) - fn insert(t: Box<Inner>, active: &mut Vec<Box<Inner>>) { - match active.iter().position(|tm| tm.target > t.target) { - Some(pos) => { active.insert(pos, t); } - None => { active.push(t); } - } - } - - // signals the first requests in the queue, possible re-enqueueing it. - fn signal(active: &mut Vec<Box<Inner>>, - dead: &mut Vec<(usize, Box<Inner>)>) { - if active.is_empty() { return } - - let mut timer = active.remove(0); - let mut cb = timer.cb.take().unwrap(); - cb.call(); - if timer.repeat { - timer.cb = Some(cb); - timer.target += timer.interval; - insert(timer, active); - } else { - dead.push((timer.id, timer)); - } - } - - 'outer: loop { - let timeout = if active.len() == 0 { - // Empty array? no timeout (wait forever for the next request) - ptr::null_mut() - } else { - let now = now(); - // If this request has already expired, then signal it and go - // through another iteration - if active[0].target <= now { - signal(&mut active, &mut dead); - continue; - } - - // The actual timeout listed in the requests array is an - // absolute date, so here we translate the absolute time to a - // relative time. - let tm = active[0].target - now; - timeout.tv_sec = (tm / 1000) as libc::time_t; - timeout.tv_usec = ((tm % 1000) * 1000) as libc::suseconds_t; - &mut timeout as *mut libc::timeval - }; - - c::fd_set(&mut set, input); - match unsafe { - c::select(input + 1, &mut set, ptr::null_mut(), - ptr::null_mut(), timeout) - } { - // timed out - 0 => signal(&mut active, &mut dead), - - // file descriptor write woke us up, we've got some new requests - 1 => { - loop { - match messages.try_recv() { - // Once we've been disconnected it means the main thread - // is exiting (at_exit has run). We could still have - // active timers for other threads, so we're just going - // to drop them all on the floor. This is all we can - // really do, however, to prevent resource leakage. The - // remaining timers will likely start panicking quickly - // as they attempt to re-use this thread but are - // disallowed to do so. - Err(TryRecvError::Disconnected) => { - break 'outer; - } - - Ok(NewTimer(timer)) => insert(timer, &mut active), - - Ok(RemoveTimer(id, ack)) => { - match dead.iter().position(|&(i, _)| id == i) { - Some(i) => { - let (_, i) = dead.remove(i); - ack.send(i).unwrap(); - continue - } - None => {} - } - let i = active.iter().position(|i| i.id == id); - let i = i.expect("no timer found"); - let t = active.remove(i); - ack.send(t).unwrap(); - } - Err(..) => break - } - } - - // drain the file descriptor - let mut buf = [0]; - assert_eq!(fd.read(&mut buf).unwrap(), 1); - } - - -1 if os::errno() == libc::EINTR as i32 => {} - n => panic!("helper thread failed in select() with error: {} ({})", - n, io::Error::last_os_error()) - } - } -} - -impl Timer { - pub fn new() -> IoResult<Timer> { - // See notes above regarding using isize return value - // instead of () - HELPER.boot(|| {}, helper); - - static ID: atomic::AtomicUsize = atomic::ATOMIC_USIZE_INIT; - let id = ID.fetch_add(1, Ordering::Relaxed); - Ok(Timer { - id: id, - inner: Some(box Inner { - cb: None, - interval: 0, - target: 0, - repeat: false, - id: id, - }) - }) - } - - pub fn sleep(&mut self, ms: u64) { - let mut inner = self.inner(); - inner.cb = None; // cancel any previous request - self.inner = Some(inner); - - let mut to_sleep = libc::timespec { - tv_sec: (ms / 1000) as libc::time_t, - tv_nsec: ((ms % 1000) * 1000000) as libc::c_long, - }; - while unsafe { libc::nanosleep(&to_sleep, &mut to_sleep) } != 0 { - if os::errno() as isize != libc::EINTR as isize { - panic!("failed to sleep, but not because of EINTR?"); - } - } - } - - pub fn oneshot(&mut self, msecs: u64, cb: Box<Callback + Send>) { - let now = now(); - let mut inner = self.inner(); - - inner.repeat = false; - inner.cb = Some(cb); - inner.interval = msecs; - inner.target = now + msecs; - - HELPER.send(NewTimer(inner)); - } - - pub fn period(&mut self, msecs: u64, cb: Box<Callback + Send>) { - let now = now(); - let mut inner = self.inner(); - - inner.repeat = true; - inner.cb = Some(cb); - inner.interval = msecs; - inner.target = now + msecs; - - HELPER.send(NewTimer(inner)); - } - - fn inner(&mut self) -> Box<Inner> { - match self.inner.take() { - Some(i) => i, - None => { - let (tx, rx) = channel(); - HELPER.send(RemoveTimer(self.id, tx)); - rx.recv().unwrap() - } - } - } -} - -impl Drop for Timer { - fn drop(&mut self) { - self.inner = Some(self.inner()); - } -} diff --git a/src/libstd/sys/unix/tty.rs b/src/libstd/sys/unix/tty.rs deleted file mode 100644 index 2f6fd713bfb..00000000000 --- a/src/libstd/sys/unix/tty.rs +++ /dev/null @@ -1,81 +0,0 @@ -// Copyright 2014 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or -// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license -// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#![allow(deprecated)] - -use prelude::v1::*; - -use sys::fs::FileDesc; -use libc::{self, c_int, c_ulong}; -use old_io::{self, IoResult, IoError}; -use sys::c; -use sys_common; - -pub struct TTY { - pub fd: FileDesc, -} - -#[cfg(any(target_os = "macos", - target_os = "ios", - target_os = "dragonfly", - target_os = "freebsd", - target_os = "bitrig", - target_os = "openbsd"))] -const TIOCGWINSZ: c_ulong = 0x40087468; - -#[cfg(any(target_os = "linux", target_os = "android"))] -const TIOCGWINSZ: c_ulong = 0x00005413; - -impl TTY { - pub fn new(fd: c_int) -> IoResult<TTY> { - if unsafe { libc::isatty(fd) } != 0 { - Ok(TTY { fd: FileDesc::new(fd, true) }) - } else { - Err(IoError { - kind: old_io::MismatchedFileTypeForOperation, - desc: "file descriptor is not a TTY", - detail: None, - }) - } - } - - pub fn read(&mut self, buf: &mut [u8]) -> IoResult<usize> { - self.fd.read(buf) - } - pub fn write(&mut self, buf: &[u8]) -> IoResult<()> { - self.fd.write(buf) - } - pub fn set_raw(&mut self, _raw: bool) -> IoResult<()> { - Err(sys_common::unimpl()) - } - - pub fn get_winsize(&mut self) -> IoResult<(isize, isize)> { - unsafe { - #[repr(C)] - struct winsize { - ws_row: u16, - ws_col: u16, - ws_xpixel: u16, - ws_ypixel: u16 - } - - let mut size = winsize { ws_row: 0, ws_col: 0, ws_xpixel: 0, ws_ypixel: 0 }; - if c::ioctl(self.fd.fd(), TIOCGWINSZ, &mut size) == -1 { - Err(IoError { - kind: old_io::OtherIoError, - desc: "Size of terminal could not be determined", - detail: None, - }) - } else { - Ok((size.ws_col as isize, size.ws_row as isize)) - } - } - } -} diff --git a/src/libstd/sys/unix/udp.rs b/src/libstd/sys/unix/udp.rs deleted file mode 100644 index 50f8fb828ad..00000000000 --- a/src/libstd/sys/unix/udp.rs +++ /dev/null @@ -1,11 +0,0 @@ -// Copyright 2014 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or -// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license -// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -pub use sys_common::net::UdpSocket; |
