diff options
| author | Jeremy Soller <jackpot51@gmail.com> | 2016-11-28 18:07:19 -0700 |
|---|---|---|
| committer | Jeremy Soller <jackpot51@gmail.com> | 2016-11-28 18:07:19 -0700 |
| commit | 746222fd9d6c16d3dc3c44c797dcc868297c133b (patch) | |
| tree | 3d5247c2bcfbe2e42438e38dd54133ee55af484e | |
| parent | d73d32f58d477ca1562e3fc0e966efc88e81409e (diff) | |
| download | rust-746222fd9d6c16d3dc3c44c797dcc868297c133b.tar.gz rust-746222fd9d6c16d3dc3c44c797dcc868297c133b.zip | |
Switch to using syscall crate directly - without import
| -rw-r--r-- | src/libstd/sys/redox/condvar.rs | 5 | ||||
| -rw-r--r-- | src/libstd/sys/redox/ext/fs.rs | 10 | ||||
| -rw-r--r-- | src/libstd/sys/redox/ext/mod.rs | 2 | ||||
| -rw-r--r-- | src/libstd/sys/redox/ext/process.rs | 4 | ||||
| -rw-r--r-- | src/libstd/sys/redox/fd.rs | 29 | ||||
| -rw-r--r-- | src/libstd/sys/redox/fs.rs | 125 | ||||
| -rw-r--r-- | src/libstd/sys/redox/mod.rs | 39 | ||||
| -rw-r--r-- | src/libstd/sys/redox/mutex.rs | 2 | ||||
| -rw-r--r-- | src/libstd/sys/redox/net/mod.rs | 2 | ||||
| -rw-r--r-- | src/libstd/sys/redox/os.rs | 12 | ||||
| -rw-r--r-- | src/libstd/sys/redox/pipe.rs | 4 | ||||
| -rw-r--r-- | src/libstd/sys/redox/process.rs | 53 | ||||
| -rw-r--r-- | src/libstd/sys/redox/stdio.rs | 19 | ||||
| -rw-r--r-- | src/libstd/sys/redox/thread.rs | 26 | ||||
| -rw-r--r-- | src/libstd/sys/redox/time.rs | 27 |
15 files changed, 171 insertions, 188 deletions
diff --git a/src/libstd/sys/redox/condvar.rs b/src/libstd/sys/redox/condvar.rs index f6c8fec545b..7e26162efbc 100644 --- a/src/libstd/sys/redox/condvar.rs +++ b/src/libstd/sys/redox/condvar.rs @@ -3,9 +3,8 @@ use intrinsics::{atomic_cxchg, atomic_xadd, atomic_xchg}; use ptr; use time::Duration; -use super::mutex::{mutex_lock, mutex_unlock, Mutex}; - -use libc::{futex, FUTEX_WAIT, FUTEX_WAKE, FUTEX_REQUEUE}; +use sys::mutex::{mutex_lock, mutex_unlock, Mutex}; +use sys::syscall::{futex, FUTEX_WAIT, FUTEX_WAKE, FUTEX_REQUEUE}; pub struct Condvar { lock: UnsafeCell<*mut i32>, diff --git a/src/libstd/sys/redox/ext/fs.rs b/src/libstd/sys/redox/ext/fs.rs index 2914fafa94c..d292b438724 100644 --- a/src/libstd/sys/redox/ext/fs.rs +++ b/src/libstd/sys/redox/ext/fs.rs @@ -216,7 +216,6 @@ impl MetadataExt for fs::Metadata { } } -/* TODO /// Add special unix types (block/char device, fifo and socket) #[stable(feature = "file_type_ext", since = "1.5.0")] pub trait FileTypeExt { @@ -236,12 +235,11 @@ pub trait FileTypeExt { #[stable(feature = "file_type_ext", since = "1.5.0")] impl FileTypeExt for fs::FileType { - fn is_block_device(&self) -> bool { self.as_inner().is(libc::S_IFBLK) } - fn is_char_device(&self) -> bool { self.as_inner().is(libc::S_IFCHR) } - fn is_fifo(&self) -> bool { self.as_inner().is(libc::S_IFIFO) } - fn is_socket(&self) -> bool { self.as_inner().is(libc::S_IFSOCK) } + fn is_block_device(&self) -> bool { false /*TODO*/ } + fn is_char_device(&self) -> bool { false /*TODO*/ } + fn is_fifo(&self) -> bool { false /*TODO*/ } + fn is_socket(&self) -> bool { false /*TODO*/ } } -*/ /// Creates a new symbolic link on the filesystem. /// diff --git a/src/libstd/sys/redox/ext/mod.rs b/src/libstd/sys/redox/ext/mod.rs index 7ba166e8932..02edfa84aa0 100644 --- a/src/libstd/sys/redox/ext/mod.rs +++ b/src/libstd/sys/redox/ext/mod.rs @@ -44,7 +44,7 @@ pub mod prelude { #[doc(no_inline)] #[stable(feature = "rust1", since = "1.0.0")] pub use super::ffi::{OsStrExt, OsStringExt}; #[doc(no_inline)] #[stable(feature = "rust1", since = "1.0.0")] - pub use super::fs::{PermissionsExt, OpenOptionsExt, MetadataExt}; + pub use super::fs::{FileTypeExt, PermissionsExt, OpenOptionsExt, MetadataExt}; #[doc(no_inline)] #[stable(feature = "rust1", since = "1.0.0")] pub use super::process::{CommandExt, ExitStatusExt}; } diff --git a/src/libstd/sys/redox/ext/process.rs b/src/libstd/sys/redox/ext/process.rs index f8e6b2cf470..3a7c59d4e6d 100644 --- a/src/libstd/sys/redox/ext/process.rs +++ b/src/libstd/sys/redox/ext/process.rs @@ -86,12 +86,12 @@ pub trait CommandExt { #[stable(feature = "rust1", since = "1.0.0")] impl CommandExt for process::Command { fn uid(&mut self, id: u32) -> &mut process::Command { - self.as_inner_mut().uid(id as usize); + self.as_inner_mut().uid(id); self } fn gid(&mut self, id: u32) -> &mut process::Command { - self.as_inner_mut().gid(id as usize); + self.as_inner_mut().gid(id); self } diff --git a/src/libstd/sys/redox/fd.rs b/src/libstd/sys/redox/fd.rs index b716965860e..4c8e62d1863 100644 --- a/src/libstd/sys/redox/fd.rs +++ b/src/libstd/sys/redox/fd.rs @@ -11,9 +11,8 @@ #![unstable(reason = "not public", issue = "0", feature = "fd")] use io::{self, Read}; -use libc; use mem; -use sys::cvt; +use sys::{cvt, syscall}; use sys_common::AsInner; use sys_common::io::read_to_end_uninitialized; @@ -36,7 +35,7 @@ impl FileDesc { } pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> { - cvt(libc::read(self.fd, buf)) + cvt(syscall::read(self.fd, buf)) } pub fn read_to_end(&self, buf: &mut Vec<u8>) -> io::Result<usize> { @@ -45,33 +44,33 @@ impl FileDesc { } pub fn write(&self, buf: &[u8]) -> io::Result<usize> { - cvt(libc::write(self.fd, buf)) + cvt(syscall::write(self.fd, buf)) } pub fn duplicate(&self) -> io::Result<FileDesc> { - let new_fd = cvt(libc::dup(self.fd, &[]))?; + let new_fd = cvt(syscall::dup(self.fd, &[]))?; Ok(FileDesc::new(new_fd)) } pub fn nonblocking(&self) -> io::Result<bool> { - let flags = cvt(libc::fcntl(self.fd, libc::F_GETFL, 0))?; - Ok(flags & libc::O_NONBLOCK == libc::O_NONBLOCK) + let flags = cvt(syscall::fcntl(self.fd, syscall::F_GETFL, 0))?; + Ok(flags & syscall::O_NONBLOCK == syscall::O_NONBLOCK) } pub fn set_cloexec(&self) -> io::Result<()> { - let mut flags = cvt(libc::fcntl(self.fd, libc::F_GETFL, 0))?; - flags |= libc::O_CLOEXEC; - cvt(libc::fcntl(self.fd, libc::F_SETFL, flags)).and(Ok(())) + let mut flags = cvt(syscall::fcntl(self.fd, syscall::F_GETFL, 0))?; + flags |= syscall::O_CLOEXEC; + cvt(syscall::fcntl(self.fd, syscall::F_SETFL, flags)).and(Ok(())) } pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> { - let mut flags = cvt(libc::fcntl(self.fd, libc::F_GETFL, 0))?; + let mut flags = cvt(syscall::fcntl(self.fd, syscall::F_GETFL, 0))?; if nonblocking { - flags |= libc::O_NONBLOCK; + flags |= syscall::O_NONBLOCK; } else { - flags &= !libc::O_NONBLOCK; + flags &= !syscall::O_NONBLOCK; } - cvt(libc::fcntl(self.fd, libc::F_SETFL, flags)).and(Ok(())) + cvt(syscall::fcntl(self.fd, syscall::F_SETFL, flags)).and(Ok(())) } } @@ -96,6 +95,6 @@ impl Drop for FileDesc { // 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. - let _ = libc::close(self.fd); + let _ = syscall::close(self.fd); } } diff --git a/src/libstd/sys/redox/fs.rs b/src/libstd/sys/redox/fs.rs index 12aa17becd8..80aec2e978d 100644 --- a/src/libstd/sys/redox/fs.rs +++ b/src/libstd/sys/redox/fs.rs @@ -13,21 +13,18 @@ use os::unix::prelude::*; use ffi::{OsString, OsStr}; use fmt; use io::{self, Error, ErrorKind, SeekFrom}; -use libc::{self, c_int, mode_t}; use path::{Path, PathBuf}; use sync::Arc; use sys::fd::FileDesc; use sys::time::SystemTime; -use sys::cvt; +use sys::{cvt, syscall}; use sys_common::{AsInner, FromInner}; -use libc::{stat, fstat, fsync, ftruncate, lseek, open}; - pub struct File(FileDesc); #[derive(Clone)] pub struct FileAttr { - stat: stat, + stat: syscall::Stat, } pub struct ReadDir { @@ -57,53 +54,53 @@ pub struct OpenOptions { create_new: bool, // system-specific custom_flags: i32, - mode: mode_t, + mode: u16, } #[derive(Clone, PartialEq, Eq, Debug)] -pub struct FilePermissions { mode: mode_t } +pub struct FilePermissions { mode: u16 } #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)] -pub struct FileType { mode: mode_t } +pub struct FileType { mode: u16 } -pub struct DirBuilder { mode: mode_t } +pub struct DirBuilder { mode: u16 } impl FileAttr { pub fn size(&self) -> u64 { self.stat.st_size as u64 } pub fn perm(&self) -> FilePermissions { - FilePermissions { mode: (self.stat.st_mode as mode_t) & 0o777 } + FilePermissions { mode: (self.stat.st_mode as u16) & 0o777 } } pub fn file_type(&self) -> FileType { - FileType { mode: self.stat.st_mode as mode_t } + FileType { mode: self.stat.st_mode as u16 } } } impl FileAttr { pub fn modified(&self) -> io::Result<SystemTime> { - Ok(SystemTime::from(libc::timespec { - tv_sec: self.stat.st_mtime as libc::time_t, + Ok(SystemTime::from(syscall::TimeSpec { + tv_sec: self.stat.st_mtime as i64, tv_nsec: self.stat.st_mtime_nsec as i32, })) } pub fn accessed(&self) -> io::Result<SystemTime> { - Ok(SystemTime::from(libc::timespec { - tv_sec: self.stat.st_atime as libc::time_t, + Ok(SystemTime::from(syscall::TimeSpec { + tv_sec: self.stat.st_atime as i64, tv_nsec: self.stat.st_atime_nsec as i32, })) } pub fn created(&self) -> io::Result<SystemTime> { - Ok(SystemTime::from(libc::timespec { - tv_sec: self.stat.st_ctime as libc::time_t, + Ok(SystemTime::from(syscall::TimeSpec { + tv_sec: self.stat.st_ctime as i64, tv_nsec: self.stat.st_ctime_nsec as i32, })) } } -impl AsInner<stat> for FileAttr { - fn as_inner(&self) -> &stat { &self.stat } +impl AsInner<syscall::Stat> for FileAttr { + fn as_inner(&self) -> &syscall::Stat { &self.stat } } impl FilePermissions { @@ -119,16 +116,16 @@ impl FilePermissions { } impl FileType { - pub fn is_dir(&self) -> bool { self.is(libc::MODE_DIR) } - pub fn is_file(&self) -> bool { self.is(libc::MODE_FILE) } + pub fn is_dir(&self) -> bool { self.is(syscall::MODE_DIR) } + pub fn is_file(&self) -> bool { self.is(syscall::MODE_FILE) } pub fn is_symlink(&self) -> bool { false } - pub fn is(&self, mode: mode_t) -> bool { self.mode & (libc::MODE_DIR | libc::MODE_FILE) == mode } + pub fn is(&self, mode: u16) -> bool { self.mode & (syscall::MODE_DIR | syscall::MODE_FILE) == mode } } impl FromInner<u32> for FilePermissions { fn from_inner(mode: u32) -> FilePermissions { - FilePermissions { mode: mode as mode_t } + FilePermissions { mode: mode as u16 } } } @@ -215,60 +212,60 @@ impl OpenOptions { pub fn create_new(&mut self, create_new: bool) { self.create_new = create_new; } pub fn custom_flags(&mut self, flags: i32) { self.custom_flags = flags; } - pub fn mode(&mut self, mode: u32) { self.mode = mode as mode_t; } + pub fn mode(&mut self, mode: u32) { self.mode = mode as u16; } - fn get_access_mode(&self) -> io::Result<c_int> { + fn get_access_mode(&self) -> io::Result<usize> { match (self.read, self.write, self.append) { - (true, false, false) => Ok(libc::O_RDONLY as c_int), - (false, true, false) => Ok(libc::O_WRONLY as c_int), - (true, true, false) => Ok(libc::O_RDWR as c_int), - (false, _, true) => Ok(libc::O_WRONLY as c_int | libc::O_APPEND as c_int), - (true, _, true) => Ok(libc::O_RDWR as c_int | libc::O_APPEND as c_int), - (false, false, false) => Err(Error::from_raw_os_error(libc::EINVAL)), + (true, false, false) => Ok(syscall::O_RDONLY), + (false, true, false) => Ok(syscall::O_WRONLY), + (true, true, false) => Ok(syscall::O_RDWR), + (false, _, true) => Ok(syscall::O_WRONLY | syscall::O_APPEND), + (true, _, true) => Ok(syscall::O_RDWR | syscall::O_APPEND), + (false, false, false) => Err(Error::from_raw_os_error(syscall::EINVAL)), } } - fn get_creation_mode(&self) -> io::Result<c_int> { + fn get_creation_mode(&self) -> io::Result<usize> { match (self.write, self.append) { (true, false) => {} (false, false) => if self.truncate || self.create || self.create_new { - return Err(Error::from_raw_os_error(libc::EINVAL)); + return Err(Error::from_raw_os_error(syscall::EINVAL)); }, (_, true) => if self.truncate && !self.create_new { - return Err(Error::from_raw_os_error(libc::EINVAL)); + return Err(Error::from_raw_os_error(syscall::EINVAL)); }, } Ok(match (self.create, self.truncate, self.create_new) { (false, false, false) => 0, - (true, false, false) => libc::O_CREAT as c_int, - (false, true, false) => libc::O_TRUNC as c_int, - (true, true, false) => libc::O_CREAT as c_int | libc::O_TRUNC as c_int, - (_, _, true) => libc::O_CREAT as c_int | libc::O_EXCL as c_int, + (true, false, false) => syscall::O_CREAT, + (false, true, false) => syscall::O_TRUNC, + (true, true, false) => syscall::O_CREAT | syscall::O_TRUNC, + (_, _, true) => syscall::O_CREAT | syscall::O_EXCL, }) } } impl File { pub fn open(path: &Path, opts: &OpenOptions) -> io::Result<File> { - let flags = libc::O_CLOEXEC | + let flags = syscall::O_CLOEXEC | opts.get_access_mode()? as usize | opts.get_creation_mode()? as usize | - (opts.custom_flags as usize & !libc::O_ACCMODE); - let fd = cvt(open(path.to_str().unwrap(), flags | opts.mode as usize))?; + (opts.custom_flags as usize & !syscall::O_ACCMODE); + let fd = cvt(syscall::open(path.to_str().unwrap(), flags | opts.mode as usize))?; Ok(File(FileDesc::new(fd))) } pub fn file_attr(&self) -> io::Result<FileAttr> { - let mut stat: stat = stat::default(); - cvt(fstat(self.0.raw(), &mut stat))?; + let mut stat = syscall::Stat::default(); + cvt(syscall::fstat(self.0.raw(), &mut stat))?; Ok(FileAttr { stat: stat }) } pub fn fsync(&self) -> io::Result<()> { - cvt(fsync(self.0.raw()))?; + cvt(syscall::fsync(self.0.raw()))?; Ok(()) } @@ -277,7 +274,7 @@ impl File { } pub fn truncate(&self, size: u64) -> io::Result<()> { - cvt(ftruncate(self.0.raw(), size as usize))?; + cvt(syscall::ftruncate(self.0.raw(), size as usize))?; Ok(()) } @@ -299,11 +296,11 @@ impl File { let (whence, pos) = match pos { // Casting to `i64` is fine, too large values will end up as // negative which will cause an error in `lseek64`. - SeekFrom::Start(off) => (libc::SEEK_SET, off as i64), - SeekFrom::End(off) => (libc::SEEK_END, off), - SeekFrom::Current(off) => (libc::SEEK_CUR, off), + SeekFrom::Start(off) => (syscall::SEEK_SET, off as i64), + SeekFrom::End(off) => (syscall::SEEK_END, off), + SeekFrom::Current(off) => (syscall::SEEK_CUR, off), }; - let n = cvt(lseek(self.0.raw(), pos as isize, whence))?; + let n = cvt(syscall::lseek(self.0.raw(), pos as isize, whence))?; Ok(n as u64) } @@ -312,7 +309,7 @@ impl File { } pub fn dup(&self, buf: &[u8]) -> io::Result<File> { - let fd = cvt(libc::dup(*self.fd().as_inner() as usize, buf))?; + let fd = cvt(syscall::dup(*self.fd().as_inner() as usize, buf))?; Ok(File(FileDesc::new(fd))) } @@ -322,7 +319,7 @@ impl File { pub fn path(&self) -> io::Result<PathBuf> { let mut buf: [u8; 4096] = [0; 4096]; - match libc::fpath(*self.fd().as_inner() as usize, &mut buf) { + match syscall::fpath(*self.fd().as_inner() as usize, &mut buf) { Ok(count) => Ok(PathBuf::from(unsafe { String::from_utf8_unchecked(Vec::from(&buf[0..count])) })), Err(err) => Err(Error::from_raw_os_error(err.errno)), } @@ -339,13 +336,13 @@ impl DirBuilder { } pub fn mkdir(&self, p: &Path) -> io::Result<()> { - let fd = cvt(libc::open(p.to_str().unwrap(), libc::O_CREAT | libc::O_DIRECTORY | libc::O_EXCL | (self.mode as usize & 0o777)))?; - let _ = libc::close(fd); + let fd = cvt(syscall::open(p.to_str().unwrap(), syscall::O_CREAT | syscall::O_DIRECTORY | syscall::O_EXCL | (self.mode as usize & 0o777)))?; + let _ = syscall::close(fd); Ok(()) } pub fn set_mode(&mut self, mode: u32) { - self.mode = mode as mode_t; + self.mode = mode as u16; } } @@ -374,7 +371,7 @@ impl fmt::Debug for File { pub fn readdir(p: &Path) -> io::Result<ReadDir> { let root = Arc::new(p.to_path_buf()); - let fd = cvt(open(p.to_str().unwrap(), libc::O_CLOEXEC | libc::O_RDONLY | libc::O_DIRECTORY))?; + let fd = cvt(syscall::open(p.to_str().unwrap(), syscall::O_CLOEXEC | syscall::O_RDONLY | syscall::O_DIRECTORY))?; let file = FileDesc::new(fd); let mut data = Vec::new(); file.read_to_end(&mut data)?; @@ -383,7 +380,7 @@ pub fn readdir(p: &Path) -> io::Result<ReadDir> { } pub fn unlink(p: &Path) -> io::Result<()> { - cvt(libc::unlink(p.to_str().unwrap()))?; + cvt(syscall::unlink(p.to_str().unwrap()))?; Ok(()) } @@ -393,12 +390,12 @@ pub fn rename(_old: &Path, _new: &Path) -> io::Result<()> { } pub fn set_perm(p: &Path, perm: FilePermissions) -> io::Result<()> { - cvt(libc::chmod(p.to_str().unwrap(), perm.mode as usize))?; + cvt(syscall::chmod(p.to_str().unwrap(), perm.mode as usize))?; Ok(()) } pub fn rmdir(p: &Path) -> io::Result<()> { - cvt(libc::rmdir(p.to_str().unwrap()))?; + cvt(syscall::rmdir(p.to_str().unwrap()))?; Ok(()) } @@ -438,13 +435,9 @@ pub fn link(_src: &Path, _dst: &Path) -> io::Result<()> { } pub fn stat(p: &Path) -> io::Result<FileAttr> { - let mut stat: stat = stat::default(); - - let fd = cvt(open(p.to_str().unwrap(), libc::O_CLOEXEC | libc::O_STAT))?; - cvt(fstat(fd, &mut stat))?; - let _ = libc::close(fd); - - Ok(FileAttr { stat: stat }) + let fd = cvt(syscall::open(p.to_str().unwrap(), syscall::O_CLOEXEC | syscall::O_STAT))?; + let file = File(FileDesc::new(fd)); + file.file_attr() } pub fn lstat(p: &Path) -> io::Result<FileAttr> { @@ -452,7 +445,7 @@ pub fn lstat(p: &Path) -> io::Result<FileAttr> { } pub fn canonicalize(p: &Path) -> io::Result<PathBuf> { - let fd = cvt(open(p.to_str().unwrap(), libc::O_CLOEXEC | libc::O_STAT))?; + let fd = cvt(syscall::open(p.to_str().unwrap(), syscall::O_CLOEXEC | syscall::O_STAT))?; let file = File(FileDesc::new(fd)); file.path() } diff --git a/src/libstd/sys/redox/mod.rs b/src/libstd/sys/redox/mod.rs index 3f3d8f2c4f4..07ead22b7a8 100644 --- a/src/libstd/sys/redox/mod.rs +++ b/src/libstd/sys/redox/mod.rs @@ -1,7 +1,8 @@ #![allow(dead_code, missing_docs, bad_style)] +pub extern crate syscall; + use io::{self, ErrorKind}; -use libc; pub mod args; pub mod backtrace; @@ -42,40 +43,40 @@ pub fn init() { use intrinsics; let msg = "fatal runtime error: out of memory\n"; unsafe { - let _ = libc::write(libc::STDERR_FILENO, msg.as_bytes()); + let _ = syscall::write(2, msg.as_bytes()); intrinsics::abort(); } } } pub fn decode_error_kind(errno: i32) -> ErrorKind { - match errno as libc::c_int { - libc::ECONNREFUSED => ErrorKind::ConnectionRefused, - libc::ECONNRESET => ErrorKind::ConnectionReset, - libc::EPERM | libc::EACCES => ErrorKind::PermissionDenied, - libc::EPIPE => ErrorKind::BrokenPipe, - libc::ENOTCONN => ErrorKind::NotConnected, - libc::ECONNABORTED => ErrorKind::ConnectionAborted, - libc::EADDRNOTAVAIL => ErrorKind::AddrNotAvailable, - libc::EADDRINUSE => ErrorKind::AddrInUse, - libc::ENOENT => ErrorKind::NotFound, - libc::EINTR => ErrorKind::Interrupted, - libc::EINVAL => ErrorKind::InvalidInput, - libc::ETIMEDOUT => ErrorKind::TimedOut, - libc::EEXIST => ErrorKind::AlreadyExists, + match errno { + syscall::ECONNREFUSED => ErrorKind::ConnectionRefused, + syscall::ECONNRESET => ErrorKind::ConnectionReset, + syscall::EPERM | syscall::EACCES => ErrorKind::PermissionDenied, + syscall::EPIPE => ErrorKind::BrokenPipe, + syscall::ENOTCONN => ErrorKind::NotConnected, + syscall::ECONNABORTED => ErrorKind::ConnectionAborted, + syscall::EADDRNOTAVAIL => ErrorKind::AddrNotAvailable, + syscall::EADDRINUSE => ErrorKind::AddrInUse, + syscall::ENOENT => ErrorKind::NotFound, + syscall::EINTR => ErrorKind::Interrupted, + syscall::EINVAL => ErrorKind::InvalidInput, + syscall::ETIMEDOUT => ErrorKind::TimedOut, + syscall::EEXIST => ErrorKind::AlreadyExists, // 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 => + x if x == syscall::EAGAIN || x == syscall::EWOULDBLOCK => ErrorKind::WouldBlock, _ => ErrorKind::Other, } } -pub fn cvt(result: Result<usize, libc::Error>) -> io::Result<usize> { - result.map_err(|err| io::Error::from_raw_os_error(err.errno as i32)) +pub fn cvt(result: Result<usize, syscall::Error>) -> io::Result<usize> { + result.map_err(|err| io::Error::from_raw_os_error(err.errno)) } /// On Redox, use an illegal instruction to abort diff --git a/src/libstd/sys/redox/mutex.rs b/src/libstd/sys/redox/mutex.rs index 4c2b0de8bd9..42424da858f 100644 --- a/src/libstd/sys/redox/mutex.rs +++ b/src/libstd/sys/redox/mutex.rs @@ -2,7 +2,7 @@ use cell::UnsafeCell; use intrinsics::{atomic_cxchg, atomic_xchg}; use ptr; -use libc::{futex, getpid, FUTEX_WAIT, FUTEX_WAKE}; +use sys::syscall::{futex, getpid, FUTEX_WAIT, FUTEX_WAKE}; pub unsafe fn mutex_try_lock(m: *mut i32) -> bool { atomic_cxchg(m, 0, 1).0 == 0 diff --git a/src/libstd/sys/redox/net/mod.rs b/src/libstd/sys/redox/net/mod.rs index 1c8fde546ef..92c7d72887b 100644 --- a/src/libstd/sys/redox/net/mod.rs +++ b/src/libstd/sys/redox/net/mod.rs @@ -4,7 +4,7 @@ use iter::Iterator; use net::{Ipv4Addr, SocketAddr, SocketAddrV4}; use str::FromStr; use string::{String, ToString}; -use libc::EINVAL; +use sys::syscall::EINVAL; use time; use vec::{IntoIter, Vec}; diff --git a/src/libstd/sys/redox/os.rs b/src/libstd/sys/redox/os.rs index e007e33258f..9ebbae4199b 100644 --- a/src/libstd/sys/redox/os.rs +++ b/src/libstd/sys/redox/os.rs @@ -19,7 +19,6 @@ use ffi::{OsString, OsStr}; use fmt; use io::{self, Read, Write}; use iter; -use libc::{self, c_int, c_char, c_void}; use marker::PhantomData; use mem; use memchr; @@ -28,8 +27,7 @@ use ptr; use slice; use str; use sys_common::mutex::Mutex; -use sys::cvt; -use sys::fd; +use sys::{cvt, fd, syscall}; use vec; const TMPBUF_SZ: usize = 128; @@ -42,7 +40,7 @@ pub fn errno() -> i32 { /// Gets a detailed string description for the given error number. pub fn error_string(errno: i32) -> String { - if let Some(string) = libc::STR_ERROR.get(errno as usize) { + if let Some(string) = syscall::STR_ERROR.get(errno as usize) { string.to_string() } else { "unknown error".to_string() @@ -51,12 +49,12 @@ pub fn error_string(errno: i32) -> String { pub fn getcwd() -> io::Result<PathBuf> { let mut buf = [0; 4096]; - let count = cvt(libc::getcwd(&mut buf))?; + let count = cvt(syscall::getcwd(&mut buf))?; Ok(PathBuf::from(OsString::from_vec(buf[.. count].to_vec()))) } pub fn chdir(p: &path::Path) -> io::Result<()> { - cvt(libc::chdir(p.to_str().unwrap())).and(Ok(())) + cvt(syscall::chdir(p.to_str().unwrap())).and(Ok(())) } pub struct SplitPaths<'a> { @@ -200,6 +198,6 @@ pub fn home_dir() -> Option<PathBuf> { } pub fn exit(code: i32) -> ! { - let _ = libc::exit(code as usize); + let _ = syscall::exit(code as usize); unreachable!(); } diff --git a/src/libstd/sys/redox/pipe.rs b/src/libstd/sys/redox/pipe.rs index fff4a10f913..7f192bef495 100644 --- a/src/libstd/sys/redox/pipe.rs +++ b/src/libstd/sys/redox/pipe.rs @@ -9,7 +9,7 @@ // except according to those terms. use io; -use libc; +use sys::syscall; use sys::fd::FileDesc; //////////////////////////////////////////////////////////////////////////////// @@ -21,7 +21,7 @@ pub struct AnonPipe(FileDesc); pub fn anon_pipe() -> io::Result<(AnonPipe, AnonPipe)> { let mut fds = [0; 2]; - libc::pipe2(&mut fds, libc::O_CLOEXEC).map_err(|err| io::Error::from_raw_os_error(err.errno))?; + syscall::pipe2(&mut fds, syscall::O_CLOEXEC).map_err(|err| io::Error::from_raw_os_error(err.errno))?; Ok((AnonPipe(FileDesc::new(fds[0])), AnonPipe(FileDesc::new(fds[1])))) } diff --git a/src/libstd/sys/redox/process.rs b/src/libstd/sys/redox/process.rs index 269aa6bedac..3c0d9691328 100644 --- a/src/libstd/sys/redox/process.rs +++ b/src/libstd/sys/redox/process.rs @@ -13,12 +13,11 @@ use env; use ffi::OsStr; use fmt; use io::{self, Error, ErrorKind}; -use libc::{self, pid_t, gid_t, uid_t}; use path::Path; use sys::fd::FileDesc; use sys::fs::{File, OpenOptions}; use sys::pipe::{self, AnonPipe}; -use sys::cvt; +use sys::{cvt, syscall}; //////////////////////////////////////////////////////////////////////////////// // Command @@ -47,8 +46,8 @@ pub struct Command { env: HashMap<String, String>, cwd: Option<String>, - uid: Option<uid_t>, - gid: Option<gid_t>, + uid: Option<u32>, + gid: Option<u32>, saw_nul: bool, closures: Vec<Box<FnMut() -> io::Result<()> + Send + Sync>>, stdin: Option<Stdio>, @@ -121,10 +120,10 @@ impl Command { pub fn cwd(&mut self, dir: &OsStr) { self.cwd = Some(dir.to_str().unwrap().to_owned()); } - pub fn uid(&mut self, id: uid_t) { + pub fn uid(&mut self, id: u32) { self.uid = Some(id); } - pub fn gid(&mut self, id: gid_t) { + pub fn gid(&mut self, id: u32) { self.gid = Some(id); } @@ -156,11 +155,11 @@ impl Command { let (input, output) = pipe::anon_pipe()?; let pid = unsafe { - match cvt(libc::clone(0))? { + match cvt(syscall::clone(0))? { 0 => { drop(input); let err = self.do_exec(theirs); - let errno = err.raw_os_error().unwrap_or(libc::EINVAL) as u32; + let errno = err.raw_os_error().unwrap_or(syscall::EINVAL) as u32; let bytes = [ (errno >> 24) as u8, (errno >> 16) as u8, @@ -173,7 +172,7 @@ impl Command { // we want to be sure we *don't* run at_exit destructors as // we're being torn down regardless assert!(output.write(&bytes).is_ok()); - let _ = libc::exit(1); + let _ = syscall::exit(1); panic!("failed to exit"); } n => n, @@ -261,7 +260,7 @@ impl Command { // 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) + // child will either exec() or invoke syscall::exit) unsafe fn do_exec(&mut self, stdio: ChildPipes) -> io::Error { macro_rules! t { ($e:expr) => (match $e { @@ -271,29 +270,29 @@ impl Command { } if let Some(fd) = stdio.stderr.fd() { - let _ = libc::close(libc::STDERR_FILENO); - t!(cvt(libc::dup(fd, &[]))); - let _ = libc::close(fd); + let _ = syscall::close(2); + t!(cvt(syscall::dup(fd, &[]))); + let _ = syscall::close(fd); } if let Some(fd) = stdio.stdout.fd() { - let _ = libc::close(libc::STDOUT_FILENO); - t!(cvt(libc::dup(fd, &[]))); - let _ = libc::close(fd); + let _ = syscall::close(1); + t!(cvt(syscall::dup(fd, &[]))); + let _ = syscall::close(fd); } if let Some(fd) = stdio.stdin.fd() { - let _ = libc::close(libc::STDIN_FILENO); - t!(cvt(libc::dup(fd, &[]))); - let _ = libc::close(fd); + let _ = syscall::close(0); + t!(cvt(syscall::dup(fd, &[]))); + let _ = syscall::close(fd); } if let Some(g) = self.gid { - t!(cvt(libc::setregid(g, g))); + t!(cvt(syscall::setregid(g as usize, g as usize))); } if let Some(u) = self.uid { - t!(cvt(libc::setreuid(u, u))); + t!(cvt(syscall::setreuid(u as usize, u as usize))); } if let Some(ref cwd) = self.cwd { - t!(cvt(libc::chdir(cwd))); + t!(cvt(syscall::chdir(cwd))); } for callback in self.closures.iter_mut() { @@ -324,7 +323,7 @@ impl Command { path_env }; - if let Err(err) = libc::exec(&program, &args) { + if let Err(err) = syscall::execve(&program, &args) { io::Error::from_raw_os_error(err.errno as i32) } else { panic!("return from exec without err"); @@ -370,7 +369,7 @@ impl Stdio { // stderr. No matter which we dup first, the second will get // overwritten prematurely. Stdio::Fd(ref fd) => { - if fd.raw() <= libc::STDERR_FILENO { + if fd.raw() <= 2 { Ok((ChildStdio::Owned(fd.duplicate()?), None)) } else { Ok((ChildStdio::Explicit(fd.raw()), None)) @@ -471,7 +470,7 @@ impl fmt::Display for ExitStatus { /// The unique id of the process (this should never be negative). pub struct Process { - pid: pid_t, + pid: usize, status: Option<ExitStatus>, } @@ -488,7 +487,7 @@ impl Process { Err(Error::new(ErrorKind::InvalidInput, "invalid argument: can't kill an exited process")) } else { - cvt(libc::kill(self.pid, libc::SIGKILL))?; + cvt(syscall::kill(self.pid, syscall::SIGKILL))?; Ok(()) } } @@ -498,7 +497,7 @@ impl Process { return Ok(status) } let mut status = 0; - cvt(libc::waitpid(self.pid, &mut status, 0))?; + cvt(syscall::waitpid(self.pid, &mut status, 0))?; self.status = Some(ExitStatus(status as i32)); Ok(ExitStatus(status as i32)) } diff --git a/src/libstd/sys/redox/stdio.rs b/src/libstd/sys/redox/stdio.rs index 50062186903..1fe7e33a35e 100644 --- a/src/libstd/sys/redox/stdio.rs +++ b/src/libstd/sys/redox/stdio.rs @@ -9,8 +9,7 @@ // except according to those terms. use io; -use libc; -use sys::cvt; +use sys::{cvt, syscall}; use sys::fd::FileDesc; pub struct Stdin(()); @@ -21,14 +20,14 @@ impl Stdin { pub fn new() -> io::Result<Stdin> { Ok(Stdin(())) } pub fn read(&self, data: &mut [u8]) -> io::Result<usize> { - let fd = FileDesc::new(libc::STDIN_FILENO); + let fd = FileDesc::new(0); let ret = fd.read(data); fd.into_raw(); ret } pub fn read_to_end(&self, buf: &mut Vec<u8>) -> io::Result<usize> { - let fd = FileDesc::new(libc::STDIN_FILENO); + let fd = FileDesc::new(0); let ret = fd.read_to_end(buf); fd.into_raw(); ret @@ -39,14 +38,14 @@ impl Stdout { pub fn new() -> io::Result<Stdout> { Ok(Stdout(())) } pub fn write(&self, data: &[u8]) -> io::Result<usize> { - let fd = FileDesc::new(libc::STDOUT_FILENO); + let fd = FileDesc::new(1); let ret = fd.write(data); fd.into_raw(); ret } pub fn flush(&self) -> io::Result<()> { - cvt(libc::fsync(libc::STDOUT_FILENO)).and(Ok(())) + cvt(syscall::fsync(1)).and(Ok(())) } } @@ -54,14 +53,14 @@ impl Stderr { pub fn new() -> io::Result<Stderr> { Ok(Stderr(())) } pub fn write(&self, data: &[u8]) -> io::Result<usize> { - let fd = FileDesc::new(libc::STDERR_FILENO); + let fd = FileDesc::new(2); let ret = fd.write(data); fd.into_raw(); ret } pub fn flush(&self) -> io::Result<()> { - cvt(libc::fsync(libc::STDERR_FILENO)).and(Ok(())) + cvt(syscall::fsync(2)).and(Ok(())) } } @@ -74,9 +73,9 @@ impl io::Write for Stderr { } fn flush(&mut self) -> io::Result<()> { - cvt(libc::fsync(libc::STDERR_FILENO)).and(Ok(())) + cvt(syscall::fsync(2)).and(Ok(())) } } -pub const EBADF_ERR: i32 = ::libc::EBADF; +pub const EBADF_ERR: i32 = ::sys::syscall::EBADF; pub const STDIN_BUF_SIZE: usize = ::sys_common::io::DEFAULT_BUF_SIZE; diff --git a/src/libstd/sys/redox/thread.rs b/src/libstd/sys/redox/thread.rs index 46bc6346a6a..0e7b27d3961 100644 --- a/src/libstd/sys/redox/thread.rs +++ b/src/libstd/sys/redox/thread.rs @@ -9,17 +9,15 @@ // except according to those terms. use alloc::boxed::FnBox; -use cmp; use ffi::CStr; use io; -use libc; use mem; use sys_common::thread::start_thread; -use sys::cvt; +use sys::{cvt, syscall}; use time::Duration; pub struct Thread { - id: libc::pid_t, + id: usize, } // Some platforms may have pthread_t as a pointer in which case we still want @@ -31,10 +29,10 @@ impl Thread { pub unsafe fn new<'a>(_stack: usize, p: Box<FnBox() + 'a>) -> io::Result<Thread> { let p = box p; - let id = cvt(libc::clone(libc::CLONE_VM | libc::CLONE_FS | libc::CLONE_FILES))?; + let id = cvt(syscall::clone(syscall::CLONE_VM | syscall::CLONE_FS | syscall::CLONE_FILES))?; if id == 0 { start_thread(&*p as *const _ as *mut _); - let _ = libc::exit(0); + let _ = syscall::exit(0); panic!("thread failed to exit"); } else { mem::forget(p); @@ -43,7 +41,7 @@ impl Thread { } pub fn yield_now() { - let ret = unsafe { libc::sched_yield() }; + let ret = syscall::sched_yield().expect("failed to sched_yield"); debug_assert_eq!(ret, 0); } @@ -58,13 +56,13 @@ impl Thread { // If we're awoken with a signal then the return value will be -1 and // nanosleep will fill in `ts` with the remaining time. while secs > 0 || nsecs > 0 { - let req = libc::timespec { - tv_sec: cmp::min(libc::time_t::max_value() as u64, secs) as libc::time_t, + let req = syscall::TimeSpec { + tv_sec: secs as i64, tv_nsec: nsecs, }; secs -= req.tv_sec as u64; - let mut rem = libc::timespec::default(); - if libc::nanosleep(&req, &mut rem).is_err() { + let mut rem = syscall::TimeSpec::default(); + if syscall::nanosleep(&req, &mut rem).is_err() { secs += rem.tv_sec as u64; nsecs = rem.tv_nsec; } else { @@ -75,12 +73,12 @@ impl Thread { pub fn join(self) { let mut status = 0; - libc::waitpid(self.id, &mut status, 0).unwrap(); + syscall::waitpid(self.id, &mut status, 0).unwrap(); } - pub fn id(&self) -> libc::pid_t { self.id } + pub fn id(&self) -> usize { self.id } - pub fn into_id(self) -> libc::pid_t { + pub fn into_id(self) -> usize { let id = self.id; mem::forget(self); id diff --git a/src/libstd/sys/redox/time.rs b/src/libstd/sys/redox/time.rs index 4e1a82bcc9a..8f05e3bcfe7 100644 --- a/src/libstd/sys/redox/time.rs +++ b/src/libstd/sys/redox/time.rs @@ -10,15 +10,14 @@ use cmp::Ordering; use fmt; -use libc; -use sys::cvt; +use sys::{cvt, syscall}; use time::Duration; const NSEC_PER_SEC: u64 = 1_000_000_000; #[derive(Copy, Clone)] struct Timespec { - t: libc::timespec, + t: syscall::TimeSpec, } impl Timespec { @@ -53,8 +52,8 @@ impl Timespec { duration to time"); } Timespec { - t: libc::timespec { - tv_sec: secs as libc::time_t, + t: syscall::TimeSpec { + tv_sec: secs as i64, tv_nsec: nsec as i32, }, } @@ -73,8 +72,8 @@ impl Timespec { duration from time"); } Timespec { - t: libc::timespec { - tv_sec: secs as libc::time_t, + t: syscall::TimeSpec { + tv_sec: secs as i64, tv_nsec: nsec as i32, }, } @@ -115,7 +114,7 @@ pub struct SystemTime { pub const UNIX_EPOCH: SystemTime = SystemTime { t: Timespec { - t: libc::timespec { + t: syscall::TimeSpec { tv_sec: 0, tv_nsec: 0, }, @@ -124,7 +123,7 @@ pub const UNIX_EPOCH: SystemTime = SystemTime { impl Instant { pub fn now() -> Instant { - Instant { t: now(libc::CLOCK_MONOTONIC) } + Instant { t: now(syscall::CLOCK_MONOTONIC) } } pub fn sub_instant(&self, other: &Instant) -> Duration { @@ -153,7 +152,7 @@ impl fmt::Debug for Instant { impl SystemTime { pub fn now() -> SystemTime { - SystemTime { t: now(libc::CLOCK_REALTIME) } + SystemTime { t: now(syscall::CLOCK_REALTIME) } } pub fn sub_time(&self, other: &SystemTime) @@ -170,8 +169,8 @@ impl SystemTime { } } -impl From<libc::timespec> for SystemTime { - fn from(t: libc::timespec) -> SystemTime { +impl From<syscall::TimeSpec> for SystemTime { + fn from(t: syscall::TimeSpec) -> SystemTime { SystemTime { t: Timespec { t: t } } } } @@ -189,11 +188,11 @@ pub type clock_t = usize; fn now(clock: clock_t) -> Timespec { let mut t = Timespec { - t: libc::timespec { + t: syscall::TimeSpec { tv_sec: 0, tv_nsec: 0, } }; - cvt(libc::clock_gettime(clock, &mut t.t)).unwrap(); + cvt(syscall::clock_gettime(clock, &mut t.t)).unwrap(); t } |
