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>,
}
}
-/* TODO
/// Add special unix types (block/char device, fifo and socket)
#[stable(feature = "file_type_ext", since = "1.5.0")]
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.
///
#[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};
}
#[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
}
#![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;
}
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> {
}
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(()))
}
}
// 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);
}
}
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 {
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 {
}
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 }
}
}
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(())
}
}
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(())
}
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)
}
}
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)))
}
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)),
}
}
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;
}
}
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)?;
}
pub fn unlink(p: &Path) -> io::Result<()> {
- cvt(libc::unlink(p.to_str().unwrap()))?;
+ cvt(syscall::unlink(p.to_str().unwrap()))?;
Ok(())
}
}
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(())
}
}
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> {
}
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()
}
#![allow(dead_code, missing_docs, bad_style)]
+pub extern crate syscall;
+
use io::{self, ErrorKind};
-use libc;
pub mod args;
pub mod backtrace;
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
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
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};
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;
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;
/// 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()
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> {
}
pub fn exit(code: i32) -> ! {
- let _ = libc::exit(code as usize);
+ let _ = syscall::exit(code as usize);
unreachable!();
}
// except according to those terms.
use io;
-use libc;
+use sys::syscall;
use sys::fd::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]))))
}
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
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>,
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);
}
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,
// 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,
// 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 {
}
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() {
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");
// 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))
/// The unique id of the process (this should never be negative).
pub struct Process {
- pid: pid_t,
+ pid: usize,
status: Option<ExitStatus>,
}
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(())
}
}
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))
}
// except according to those terms.
use io;
-use libc;
-use sys::cvt;
+use sys::{cvt, syscall};
use sys::fd::FileDesc;
pub struct 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
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(()))
}
}
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(()))
}
}
}
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;
// 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
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);
}
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);
}
// 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 {
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
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 {
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,
},
}
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,
},
}
pub const UNIX_EPOCH: SystemTime = SystemTime {
t: Timespec {
- t: libc::timespec {
+ t: syscall::TimeSpec {
tv_sec: 0,
tv_nsec: 0,
},
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 {
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)
}
}
-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 } }
}
}
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
}