diff --git a/src/fs.rs b/src/fs.rs index 76b31c307e2b424921d913e96b6d5f5d725294a6..674649b544b4ef3e6c6582a022a966fa63f3e7e1 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -62,7 +62,7 @@ impl File { impl io::Read for &File { fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { - match Sys::read(self.fd, buf).or_minus_one_errno() /* TODO */ { + match Sys::read(self.fd, buf).map(|read| read as ssize_t).or_minus_one_errno() /* TODO */ { -1 => Err(io::last_os_error()), ok => Ok(ok as usize), } @@ -71,7 +71,10 @@ impl io::Read for &File { impl io::Write for &File { fn write(&mut self, buf: &[u8]) -> io::Result<usize> { - match Sys::write(self.fd, buf).or_minus_one_errno() { + match Sys::write(self.fd, buf) + .map(|read| read as ssize_t) + .or_minus_one_errno() + { -1 => Err(io::last_os_error()), ok => Ok(ok as usize), } diff --git a/src/header/fcntl/mod.rs b/src/header/fcntl/mod.rs index 6774e6b49e344288635ffc9f1972dba3fb8c260e..c21b279287a3f0235e005341d9f1cf382d20e0c2 100644 --- a/src/header/fcntl/mod.rs +++ b/src/header/fcntl/mod.rs @@ -55,7 +55,7 @@ pub unsafe extern "C" fn fcntl(fildes: c_int, cmd: c_int, mut __valist: ...) -> _ => 0, }; - Sys::fcntl(fildes, cmd, arg) + Sys::fcntl(fildes, cmd, arg).or_minus_one_errno() } #[no_mangle] diff --git a/src/header/poll/mod.rs b/src/header/poll/mod.rs index cbf4e9defbc2238964292966d07f28cb83720ed8..cd9979d6cbf88e46d852bc4384e94391117372da 100644 --- a/src/header/poll/mod.rs +++ b/src/header/poll/mod.rs @@ -3,6 +3,7 @@ use core::{mem, slice}; use crate::{ + error::ResultExt, fs::File, header::sys_epoll::{ epoll_create1, epoll_ctl, epoll_data, epoll_event, epoll_wait, EPOLLERR, EPOLLHUP, EPOLLIN, @@ -77,13 +78,13 @@ pub fn poll_epoll(fds: &mut [pollfd], timeout: c_int) -> c_int { pfd.revents = 0; - if epoll_ctl(*ep, EPOLL_CTL_ADD, pfd.fd, &mut event) < 0 { + if unsafe { epoll_ctl(*ep, EPOLL_CTL_ADD, pfd.fd, &mut event) } < 0 { return -1; } } let mut events: [epoll_event; 32] = unsafe { mem::zeroed() }; - let res = epoll_wait(*ep, events.as_mut_ptr(), events.len() as c_int, timeout); + let res = unsafe { epoll_wait(*ep, events.as_mut_ptr(), events.len() as c_int, timeout) }; if res < 0 { return -1; } diff --git a/src/header/pty/redox.rs b/src/header/pty/redox.rs index f22dfc43edaca9b00088482283aef0b0142fd844..f99f882a0fda0a39225cf12c2e60d1c3ac2aebba 100644 --- a/src/header/pty/redox.rs +++ b/src/header/pty/redox.rs @@ -12,7 +12,9 @@ pub(super) unsafe fn openpty(name: &mut [u8]) -> Result<(c_int, c_int), ()> { } // TODO: better error handling - let count = Sys::fpath(master, name).or_minus_one_errno(); + let count = Sys::fpath(master, name) + .map(|u| u as ssize_t) + .or_minus_one_errno(); if count < 0 { unistd::close(master); return Err(()); diff --git a/src/header/signal/mod.rs b/src/header/signal/mod.rs index 61e1780f5b32afc2279c17302fa0d044c6239853..1457a7337d156064da974136b9bb8e5da9314449 100644 --- a/src/header/signal/mod.rs +++ b/src/header/signal/mod.rs @@ -88,7 +88,7 @@ pub type stack_t = sigaltstack; #[no_mangle] pub extern "C" fn kill(pid: pid_t, sig: c_int) -> c_int { - Sys::kill(pid, sig) + Sys::kill(pid, sig).map(|()| 0).or_minus_one_errno() } #[no_mangle] pub extern "C" fn sigqueue(pid: pid_t, sig: c_int, val: sigval) -> c_int { @@ -99,7 +99,7 @@ pub extern "C" fn sigqueue(pid: pid_t, sig: c_int, val: sigval) -> c_int { #[no_mangle] pub extern "C" fn killpg(pgrp: pid_t, sig: c_int) -> c_int { - Sys::killpg(pgrp, sig) + Sys::killpg(pgrp, sig).map(|()| 0).or_minus_one_errno() } #[no_mangle] diff --git a/src/header/stdio/mod.rs b/src/header/stdio/mod.rs index e36a182db69fde296dfb3c048d1380ff5c9c000f..6719aed47a87610be9c95868f839a32e22b23bcf 100644 --- a/src/header/stdio/mod.rs +++ b/src/header/stdio/mod.rs @@ -837,7 +837,7 @@ pub unsafe extern "C" fn pclose(stream: *mut FILE) -> c_int { fclose(stream); let mut wstatus = 0; - if Sys::waitpid(pid, &mut wstatus, 0) < 0 { + if Sys::waitpid(pid, &mut wstatus, 0).or_minus_one_errno() == -1 { return -1; } diff --git a/src/header/stdlib/mod.rs b/src/header/stdlib/mod.rs index 47dd99e5eb3c8ba75925a3270441b841e33d8b8e..9a290a9ed33d87903c97094039c34e0a6449fa3c 100644 --- a/src/header/stdlib/mod.rs +++ b/src/header/stdlib/mod.rs @@ -986,7 +986,9 @@ pub unsafe extern "C" fn realpath(pathname: *const c_char, resolved: *mut c_char let len = out.len(); // TODO: better error handling - let read = Sys::fpath(*file, &mut out[..len - 1]).or_minus_one_errno(); + let read = Sys::fpath(*file, &mut out[..len - 1]) + .map(|read| read as ssize_t) + .or_minus_one_errno(); if read < 0 { return ptr::null_mut(); } @@ -1320,7 +1322,7 @@ pub unsafe extern "C" fn system(command: *const c_char) -> c_int { unreachable!(); } else if child_pid > 0 { let mut wstatus = 0; - if Sys::waitpid(child_pid, &mut wstatus, 0) == !0 { + if Sys::waitpid(child_pid, &mut wstatus, 0).or_minus_one_errno() == -1 { return -1; } diff --git a/src/header/sys_epoll/mod.rs b/src/header/sys_epoll/mod.rs index 831d6dca03addf51b194c1134e721b9a289c783e..8089642ae16a7a1d61ec53d2b8753a100ad5b059 100644 --- a/src/header/sys_epoll/mod.rs +++ b/src/header/sys_epoll/mod.rs @@ -3,6 +3,7 @@ use core::ptr; use crate::{ + error::ResultExt, header::signal::sigset_t, platform::{types::*, PalEpoll, Sys}, }; @@ -64,13 +65,24 @@ pub extern "C" fn epoll_create(_size: c_int) -> c_int { #[no_mangle] pub extern "C" fn epoll_create1(flags: c_int) -> c_int { - trace_expr!(Sys::epoll_create1(flags), "epoll_create1({:#x})", flags) + trace_expr!( + Sys::epoll_create1(flags).or_minus_one_errno(), + "epoll_create1({:#x})", + flags + ) } #[no_mangle] -pub extern "C" fn epoll_ctl(epfd: c_int, op: c_int, fd: c_int, event: *mut epoll_event) -> c_int { +pub unsafe extern "C" fn epoll_ctl( + epfd: c_int, + op: c_int, + fd: c_int, + event: *mut epoll_event, +) -> c_int { trace_expr!( - Sys::epoll_ctl(epfd, op, fd, event), + Sys::epoll_ctl(epfd, op, fd, event) + .map(|()| 0) + .or_minus_one_errno(), "epoll_ctl({}, {}, {}, {:p})", epfd, op, @@ -80,7 +92,7 @@ pub extern "C" fn epoll_ctl(epfd: c_int, op: c_int, fd: c_int, event: *mut epoll } #[no_mangle] -pub extern "C" fn epoll_wait( +pub unsafe extern "C" fn epoll_wait( epfd: c_int, events: *mut epoll_event, maxevents: c_int, @@ -90,7 +102,7 @@ pub extern "C" fn epoll_wait( } #[no_mangle] -pub extern "C" fn epoll_pwait( +pub unsafe extern "C" fn epoll_pwait( epfd: c_int, events: *mut epoll_event, maxevents: c_int, @@ -98,7 +110,9 @@ pub extern "C" fn epoll_pwait( sigmask: *const sigset_t, ) -> c_int { trace_expr!( - Sys::epoll_pwait(epfd, events, maxevents, timeout, sigmask), + Sys::epoll_pwait(epfd, events, maxevents, timeout, sigmask) + .map(|e| e as c_int) + .or_minus_one_errno(), "epoll_pwait({}, {:p}, {}, {}, {:p})", epfd, events, diff --git a/src/header/sys_file/mod.rs b/src/header/sys_file/mod.rs index 7d8e6324035c00ac9bd3e1a6470c3bca2d35c392..15e0f81ec1e868c22816362ac1cca1877c026534 100644 --- a/src/header/sys_file/mod.rs +++ b/src/header/sys_file/mod.rs @@ -1,6 +1,9 @@ //! sys/file.h implementation -use crate::platform::{types::*, Pal, Sys}; +use crate::{ + error::ResultExt, + platform::{types::*, Pal, Sys}, +}; pub const LOCK_SH: usize = 1; pub const LOCK_EX: usize = 2; @@ -13,5 +16,5 @@ pub const L_XTND: usize = 2; #[no_mangle] pub extern "C" fn flock(fd: c_int, operation: c_int) -> c_int { - Sys::flock(fd, operation) + Sys::flock(fd, operation).map(|()| 0).or_minus_one_errno() } diff --git a/src/header/sys_ioctl/linux.rs b/src/header/sys_ioctl/linux.rs index 949052dbaeaa1fcaff8f3502c0e27a421849e324..e6b95848e0e64bc0a9726f48110aafa4c240dc66 100644 --- a/src/header/sys_ioctl/linux.rs +++ b/src/header/sys_ioctl/linux.rs @@ -1,9 +1,12 @@ -use crate::platform::{types::*, Sys}; +use crate::{ + error::ResultExt, + platform::{types::*, Sys}, +}; #[no_mangle] pub unsafe extern "C" fn ioctl(fd: c_int, request: c_ulong, out: *mut c_void) -> c_int { // TODO: Somehow support varargs to syscall?? - Sys::ioctl(fd, request, out) + Sys::ioctl(fd, request, out).or_minus_one_errno() } pub const TCGETS: c_ulong = 0x5401; diff --git a/src/header/sys_mman/mod.rs b/src/header/sys_mman/mod.rs index 29f639a7a44ce6ea02bf5ff501d31f1d6ae225d3..3133a74159a01a962c09de1c70d04b3513d00ee2 100644 --- a/src/header/sys_mman/mod.rs +++ b/src/header/sys_mman/mod.rs @@ -1,7 +1,8 @@ use crate::{ c_str::{CStr, CString}, + error::{Errno, ResultExt}, header::{fcntl, unistd}, - platform::{types::*, Pal, Sys}, + platform::{types::*, Pal, Sys, ERRNO}, }; pub use self::sys::*; @@ -45,12 +46,12 @@ pub const POSIX_MADV_WONTNEED: c_int = 4; #[no_mangle] pub unsafe extern "C" fn mlock(addr: *const c_void, len: usize) -> c_int { - Sys::mlock(addr, len) + Sys::mlock(addr, len).map(|()| 0).or_minus_one_errno() } #[no_mangle] -pub extern "C" fn mlockall(flags: c_int) -> c_int { - Sys::mlockall(flags) +pub unsafe extern "C" fn mlockall(flags: c_int) -> c_int { + Sys::mlockall(flags).map(|()| 0).or_minus_one_errno() } #[no_mangle] @@ -62,7 +63,13 @@ pub unsafe extern "C" fn mmap( fildes: c_int, off: off_t, ) -> *mut c_void { - Sys::mmap(addr, len, prot, flags, fildes, off) + match Sys::mmap(addr, len, prot, flags, fildes, off) { + Ok(ptr) => ptr, + Err(Errno(errno)) => { + ERRNO.set(errno); + MAP_FAILED + } + } } #[no_mangle] @@ -74,37 +81,49 @@ unsafe extern "C" fn mremap( mut __valist: ... ) -> *mut c_void { let new_address = __valist.arg::<*mut c_void>(); - Sys::mremap(old_address, old_size, new_size, flags, new_address) + match Sys::mremap(old_address, old_size, new_size, flags, new_address) { + Ok(ptr) => ptr, + Err(Errno(errno)) => { + ERRNO.set(errno); + MAP_FAILED + } + } } #[no_mangle] pub unsafe extern "C" fn mprotect(addr: *mut c_void, len: size_t, prot: c_int) -> c_int { Sys::mprotect(addr, len, prot) + .map(|()| 0) + .or_minus_one_errno() } #[no_mangle] pub unsafe extern "C" fn msync(addr: *mut c_void, len: size_t, flags: c_int) -> c_int { Sys::msync(addr, len, flags) + .map(|()| 0) + .or_minus_one_errno() } #[no_mangle] pub unsafe extern "C" fn munlock(addr: *const c_void, len: usize) -> c_int { - Sys::munlock(addr, len) + Sys::munlock(addr, len).map(|()| 0).or_minus_one_errno() } #[no_mangle] -pub extern "C" fn munlockall() -> c_int { - Sys::munlockall() +pub unsafe extern "C" fn munlockall() -> c_int { + Sys::munlockall().map(|()| 0).or_minus_one_errno() } #[no_mangle] pub unsafe extern "C" fn munmap(addr: *mut c_void, len: size_t) -> c_int { - Sys::munmap(addr, len) + Sys::munmap(addr, len).map(|()| 0).or_minus_one_errno() } #[no_mangle] pub unsafe extern "C" fn madvise(addr: *mut c_void, len: size_t, flags: c_int) -> c_int { Sys::madvise(addr, len, flags) + .map(|()| 0) + .or_minus_one_errno() } #[cfg(target_os = "linux")] diff --git a/src/header/sys_random/mod.rs b/src/header/sys_random/mod.rs index 957f8c7753eb374471cf095f37b8956799180716..36dfc3b408a74ee40adeade48b8ade6a5ee30dc7 100644 --- a/src/header/sys_random/mod.rs +++ b/src/header/sys_random/mod.rs @@ -1,6 +1,9 @@ use core::slice; -use crate::platform::{types::*, Pal, Sys}; +use crate::{ + error::ResultExt, + platform::{types::*, Pal, Sys}, +}; pub const GRND_NONBLOCK: c_uint = 1; pub const GRND_RANDOM: c_uint = 2; @@ -11,4 +14,6 @@ pub unsafe extern "C" fn getrandom(buf: *mut c_void, buflen: size_t, flags: c_ui slice::from_raw_parts_mut(buf as *mut u8, buflen as usize), flags, ) + .map(|read| read as ssize_t) + .or_minus_one_errno() } diff --git a/src/header/sys_resource/mod.rs b/src/header/sys_resource/mod.rs index d8a44b968651f075ed4fb5c328e7fecc9a0326c6..4e54febacbfcc7caa314049ce9fa15f0e8373798 100644 --- a/src/header/sys_resource/mod.rs +++ b/src/header/sys_resource/mod.rs @@ -68,7 +68,7 @@ pub const PRIO_USER: c_int = 2; #[no_mangle] pub unsafe extern "C" fn getpriority(which: c_int, who: id_t) -> c_int { - let r = Sys::getpriority(which, who); + let r = Sys::getpriority(which, who).or_minus_one_errno(); if r < 0 { return r; } @@ -99,4 +99,6 @@ pub unsafe extern "C" fn setrlimit(resource: c_int, rlp: *const rlimit) -> c_int #[no_mangle] pub unsafe extern "C" fn getrusage(who: c_int, r_usage: *mut rusage) -> c_int { Sys::getrusage(who, &mut *r_usage) + .map(|()| 0) + .or_minus_one_errno() } diff --git a/src/header/sys_select/mod.rs b/src/header/sys_select/mod.rs index 6208ab831e1fb212ef6e5d728ce0ff7a2acdda49..22a981d19642259d38d77bed7ea548e4e74c3301 100644 --- a/src/header/sys_select/mod.rs +++ b/src/header/sys_select/mod.rs @@ -80,7 +80,7 @@ pub fn select_epoll( data: epoll_data { fd }, ..Default::default() }; - if epoll_ctl(*ep, EPOLL_CTL_ADD, fd, &mut event) < 0 { + if unsafe { epoll_ctl(*ep, EPOLL_CTL_ADD, fd, &mut event) } < 0 { if platform::ERRNO.get() == errno::EPERM { not_epoll += 1; } else { @@ -121,12 +121,14 @@ pub fn select_epoll( None => -1, } }; - let res = epoll_wait( - *ep, - events.as_mut_ptr(), - events.len() as c_int, - epoll_timeout, - ); + let res = unsafe { + epoll_wait( + *ep, + events.as_mut_ptr(), + events.len() as c_int, + epoll_timeout, + ) + }; if res < 0 { return -1; } diff --git a/src/header/sys_stat/mod.rs b/src/header/sys_stat/mod.rs index 17dfe6bcd339630422fce1e2b4cb433a548dc69a..5b75c31a30733d1592b7d51ce2c20f5d74fb929e 100644 --- a/src/header/sys_stat/mod.rs +++ b/src/header/sys_stat/mod.rs @@ -77,18 +77,18 @@ pub extern "C" fn fchmod(fildes: c_int, mode: mode_t) -> c_int { } #[no_mangle] -pub extern "C" fn fstat(fildes: c_int, buf: *mut stat) -> c_int { - Sys::fstat(fildes, buf) +pub unsafe extern "C" fn fstat(fildes: c_int, buf: *mut stat) -> c_int { + Sys::fstat(fildes, buf).map(|()| 0).or_minus_one_errno() } #[no_mangle] -pub extern "C" fn __fxstat(_ver: c_int, fildes: c_int, buf: *mut stat) -> c_int { +pub unsafe extern "C" fn __fxstat(_ver: c_int, fildes: c_int, buf: *mut stat) -> c_int { fstat(fildes, buf) } #[no_mangle] -pub extern "C" fn futimens(fd: c_int, times: *const timespec) -> c_int { - Sys::futimens(fd, times) +pub unsafe extern "C" fn futimens(fd: c_int, times: *const timespec) -> c_int { + Sys::futimens(fd, times).map(|()| 0).or_minus_one_errno() } #[no_mangle] @@ -100,7 +100,8 @@ pub unsafe extern "C" fn lstat(path: *const c_char, buf: *mut stat) -> c_int { return -1; } - let res = Sys::fstat(fd, buf); + // TODO: Rustify + let res = Sys::fstat(fd, buf).map(|()| 0).or_minus_one_errno(); Sys::close(fd); @@ -147,7 +148,8 @@ pub unsafe extern "C" fn stat(file: *const c_char, buf: *mut stat) -> c_int { return -1; } - let res = Sys::fstat(fd, buf); + // TODO: Rustify + let res = Sys::fstat(fd, buf).map(|()| 0).or_minus_one_errno(); Sys::close(fd); diff --git a/src/header/sys_statvfs/mod.rs b/src/header/sys_statvfs/mod.rs index 6bfde24f0cd1c90487944ae24745be2386ba3a0c..306e0436bf60adc890bf5c20b00dcd8585b72393 100644 --- a/src/header/sys_statvfs/mod.rs +++ b/src/header/sys_statvfs/mod.rs @@ -27,8 +27,8 @@ pub struct statvfs { } #[no_mangle] -pub extern "C" fn fstatvfs(fildes: c_int, buf: *mut statvfs) -> c_int { - Sys::fstatvfs(fildes, buf) +pub unsafe extern "C" fn fstatvfs(fildes: c_int, buf: *mut statvfs) -> c_int { + Sys::fstatvfs(fildes, buf).map(|()| 0).or_minus_one_errno() } #[no_mangle] @@ -40,7 +40,7 @@ pub unsafe extern "C" fn statvfs(file: *const c_char, buf: *mut statvfs) -> c_in return -1; } - let res = Sys::fstatvfs(fd, buf); + let res = Sys::fstatvfs(fd, buf).map(|()| 0).or_minus_one_errno(); Sys::close(fd); diff --git a/src/header/sys_time/mod.rs b/src/header/sys_time/mod.rs index e9641493ecffab1d7b118d42a98a9f1c3222e205..af5e895d03499ae781f906e7f0c67b83d2f58420 100644 --- a/src/header/sys_time/mod.rs +++ b/src/header/sys_time/mod.rs @@ -2,6 +2,7 @@ use crate::{ c_str::CStr, + error::ResultExt, header::time::timespec, platform::{types::*, Pal, PalSignal, Sys}, }; @@ -39,6 +40,8 @@ pub struct fd_set { #[no_mangle] pub unsafe extern "C" fn getitimer(which: c_int, value: *mut itimerval) -> c_int { Sys::getitimer(which, value) + .map(|()| 0) + .or_minus_one_errno() } #[no_mangle] @@ -48,11 +51,13 @@ pub unsafe extern "C" fn setitimer( ovalue: *mut itimerval, ) -> c_int { Sys::setitimer(which, value, ovalue) + .map(|()| 0) + .or_minus_one_errno() } #[no_mangle] pub unsafe extern "C" fn gettimeofday(tp: *mut timeval, tzp: *mut timezone) -> c_int { - Sys::gettimeofday(tp, tzp) + Sys::gettimeofday(tp, tzp).map(|()| 0).or_minus_one_errno() } #[no_mangle] @@ -77,4 +82,6 @@ pub unsafe extern "C" fn utimes(path: *const c_char, times: *const timeval) -> c .as_ptr() }; Sys::utimens(path, times_spec) + .map(|()| 0) + .or_minus_one_errno() } diff --git a/src/header/sys_wait/mod.rs b/src/header/sys_wait/mod.rs index 3628353977a5e398687e784402529b2657a55a90..e482bb3211e3f84e153b17d5bc47505c8000188b 100644 --- a/src/header/sys_wait/mod.rs +++ b/src/header/sys_wait/mod.rs @@ -1,6 +1,7 @@ //! sys/wait.h implementation for Redox, following //! http://pubs.opengroup.org/onlinepubs/7908799/xsh/syswait.h.html +use crate::error::ResultExt; //use header::sys_resource::rusage; use crate::platform::{types::*, Pal, Sys}; @@ -47,5 +48,5 @@ pub unsafe extern "C" fn wait(stat_loc: *mut c_int) -> pid_t { #[no_mangle] pub unsafe extern "C" fn waitpid(pid: pid_t, stat_loc: *mut c_int, options: c_int) -> pid_t { - Sys::waitpid(pid, stat_loc, options) + Sys::waitpid(pid, stat_loc, options).or_minus_one_errno() } diff --git a/src/header/time/mod.rs b/src/header/time/mod.rs index 0ea67045389cc9a6f20a1681f75aa326dfcedf88..3942ecb2069253206842766c8c188dfdc28b43f6 100644 --- a/src/header/time/mod.rs +++ b/src/header/time/mod.rs @@ -488,8 +488,8 @@ pub unsafe extern "C" fn mktime(timeptr: *mut tm) -> time_t { } #[no_mangle] -pub extern "C" fn nanosleep(rqtp: *const timespec, rmtp: *mut timespec) -> c_int { - Sys::nanosleep(rqtp, rmtp) +pub unsafe extern "C" fn nanosleep(rqtp: *const timespec, rmtp: *mut timespec) -> c_int { + Sys::nanosleep(rqtp, rmtp).map(|()| 0).or_minus_one_errno() } #[no_mangle] diff --git a/src/header/unistd/brk.rs b/src/header/unistd/brk.rs index 7efa5c2be349baa816208309960a148745258d10..47907758cf23ae9dd93a300afe341e1308de7b76 100644 --- a/src/header/unistd/brk.rs +++ b/src/header/unistd/brk.rs @@ -1,6 +1,7 @@ use core::ptr; use crate::{ + error::ResultExtPtrMut, header::errno::ENOMEM, platform::{self, types::*, Pal, Sys}, }; @@ -9,7 +10,7 @@ static mut BRK: *mut c_void = ptr::null_mut(); #[no_mangle] pub unsafe extern "C" fn brk(addr: *mut c_void) -> c_int { - BRK = Sys::brk(addr); + BRK = Sys::brk(addr).or_errno_null_mut(); if BRK < addr { platform::ERRNO.set(ENOMEM); @@ -22,7 +23,7 @@ pub unsafe extern "C" fn brk(addr: *mut c_void) -> c_int { #[no_mangle] pub unsafe extern "C" fn sbrk(incr: intptr_t) -> *mut c_void { if BRK.is_null() { - BRK = Sys::brk(ptr::null_mut()); + BRK = Sys::brk(ptr::null_mut()).or_errno_null_mut(); } let old_brk = BRK; @@ -30,7 +31,7 @@ pub unsafe extern "C" fn sbrk(incr: intptr_t) -> *mut c_void { if incr != 0 { let addr = old_brk.offset(incr); - BRK = Sys::brk(addr); + BRK = Sys::brk(addr).or_errno_null_mut(); if BRK < addr { platform::ERRNO.set(ENOMEM); diff --git a/src/header/unistd/mod.rs b/src/header/unistd/mod.rs index 03ef809cbbed04d34049495d7c1763c38533a963..df8f8c6fa612211b2f844a05c5ba4f4508b1f065 100644 --- a/src/header/unistd/mod.rs +++ b/src/header/unistd/mod.rs @@ -9,7 +9,7 @@ use core::{ use crate::{ c_str::CStr, - error::ResultExt, + error::{Errno, ResultExt}, header::{ crypt::{crypt_data, crypt_r}, errno, fcntl, limits, @@ -17,7 +17,7 @@ use crate::{ sys_ioctl, sys_resource, sys_time, sys_utsname, termios, time::timespec, }, - platform::{self, types::*, Pal, Sys}, + platform::{self, types::*, Pal, Sys, ERRNO}, }; use alloc::collections::LinkedList; @@ -163,7 +163,7 @@ pub extern "C" fn daemon(nochdir: c_int, noclose: c_int) -> c_int { } } - match fork() { + match unsafe { fork() } { 0 => {} -1 => return -1, _ => _exit(0), @@ -283,6 +283,8 @@ pub unsafe extern "C" fn execve( ) -> c_int { let path = CStr::from_ptr(path); Sys::execve(path, argv, envp) + .map(|()| unreachable!()) + .or_minus_one_errno() } #[cfg(target_os = "linux")] @@ -344,12 +346,12 @@ pub extern "C" fn fdatasync(fildes: c_int) -> c_int { } #[no_mangle] -pub extern "C" fn fork() -> pid_t { - let fork_hooks = unsafe { init_fork_hooks() }; +pub unsafe extern "C" fn fork() -> pid_t { + let fork_hooks = init_fork_hooks(); for prepare in &fork_hooks[0] { prepare(); } - let pid = Sys::fork(); + let pid = Sys::fork().or_minus_one_errno(); if pid == 0 { for child in &fork_hooks[2] { child(); @@ -375,7 +377,7 @@ pub extern "C" fn ftruncate(fildes: c_int, length: off_t) -> c_int { } #[no_mangle] -pub extern "C" fn getcwd(mut buf: *mut c_char, mut size: size_t) -> *mut c_char { +pub unsafe extern "C" fn getcwd(mut buf: *mut c_char, mut size: size_t) -> *mut c_char { let alloc = buf.is_null(); let mut stack_buf = [0; limits::PATH_MAX]; if alloc { @@ -383,10 +385,13 @@ pub extern "C" fn getcwd(mut buf: *mut c_char, mut size: size_t) -> *mut c_char size = stack_buf.len(); } - let ret = Sys::getcwd(buf, size); - if ret.is_null() { - return ptr::null_mut(); - } + let ret = match Sys::getcwd(buf, size) { + Ok(()) => buf, + Err(Errno(errno)) => { + ERRNO.set(errno); + return ptr::null_mut(); + } + }; if alloc { let len = stack_buf @@ -442,7 +447,7 @@ pub extern "C" fn getgid() -> gid_t { #[no_mangle] pub unsafe extern "C" fn getgroups(size: c_int, list: *mut gid_t) -> c_int { - Sys::getgroups(size, list) + Sys::getgroups(size, list).or_minus_one_errno() } // #[no_mangle] @@ -506,12 +511,12 @@ pub extern "C" fn getpagesize() -> c_int { #[no_mangle] pub extern "C" fn getpgid(pid: pid_t) -> pid_t { - Sys::getpgid(pid) + Sys::getpgid(pid).or_minus_one_errno() } #[no_mangle] pub extern "C" fn getpgrp() -> pid_t { - Sys::getpgid(Sys::getpid()) + Sys::getpgid(Sys::getpid()).or_minus_one_errno() } #[no_mangle] @@ -526,7 +531,7 @@ pub extern "C" fn getppid() -> pid_t { #[no_mangle] pub extern "C" fn getsid(pid: pid_t) -> pid_t { - Sys::getsid(pid) + Sys::getsid(pid).or_minus_one_errno() } #[no_mangle] @@ -535,8 +540,8 @@ pub extern "C" fn getuid() -> uid_t { } #[no_mangle] -pub extern "C" fn getwd(path_name: *mut c_char) -> *mut c_char { - getcwd(path_name, limits::PATH_MAX) +pub unsafe extern "C" fn getwd(path_name: *mut c_char) -> *mut c_char { + unsafe { getcwd(path_name, limits::PATH_MAX) } } #[no_mangle] @@ -642,6 +647,7 @@ pub unsafe extern "C" fn pread( slice::from_raw_parts_mut(buf.cast::<u8>(), nbyte), offset, ) + .map(|read| read as ssize_t) .or_minus_one_errno() } @@ -676,6 +682,7 @@ pub unsafe extern "C" fn pwrite( slice::from_raw_parts(buf.cast::<u8>(), nbyte), offset, ) + .map(|read| read as ssize_t) .or_minus_one_errno() } @@ -683,7 +690,9 @@ pub unsafe extern "C" fn pwrite( pub unsafe extern "C" fn read(fildes: c_int, buf: *const c_void, nbyte: size_t) -> ssize_t { let buf = unsafe { slice::from_raw_parts_mut(buf as *mut u8, nbyte as usize) }; trace_expr!( - Sys::read(fildes, buf).or_minus_one_errno(), + Sys::read(fildes, buf) + .map(|read| read as ssize_t) + .or_minus_one_errno(), "read({}, {:p}, {})", fildes, buf, @@ -699,7 +708,9 @@ pub unsafe extern "C" fn readlink( ) -> ssize_t { let path = CStr::from_ptr(path); let buf = slice::from_raw_parts_mut(buf as *mut u8, bufsize as usize); - Sys::readlink(path, buf).or_minus_one_errno() + Sys::readlink(path, buf) + .map(|read| read as ssize_t) + .or_minus_one_errno() } #[no_mangle] @@ -775,7 +786,7 @@ pub extern "C" fn sleep(seconds: c_uint) -> c_uint { tv_nsec: 0, }; let rmtp = ptr::null_mut(); - Sys::nanosleep(&rqtp, rmtp); + unsafe { Sys::nanosleep(&rqtp, rmtp).map(|()| 0).or_minus_one_errno() }; 0 } @@ -857,7 +868,9 @@ pub extern "C" fn ttyname_r(fildes: c_int, name: *mut c_char, namesize: size_t) return errno::ERANGE; } - let len = Sys::fpath(fildes, &mut name[..namesize - 1]).or_minus_one_errno(); + let len = Sys::fpath(fildes, &mut name[..namesize - 1]) + .map(|read| read as ssize_t) + .or_minus_one_errno(); if len < 0 { return -platform::ERRNO.get(); } @@ -902,7 +915,9 @@ pub extern "C" fn usleep(useconds: useconds_t) -> c_int { tv_nsec: ((useconds % 1_000_000) * 1000) as c_long, }; let rmtp = ptr::null_mut(); - Sys::nanosleep(&rqtp, rmtp) + unsafe { Sys::nanosleep(&rqtp, rmtp) } + .map(|()| 0) + .or_minus_one_errno() } // #[no_mangle] @@ -913,5 +928,7 @@ pub extern "C" fn vfork() -> pid_t { #[no_mangle] pub unsafe extern "C" fn write(fildes: c_int, buf: *const c_void, nbyte: size_t) -> ssize_t { let buf = slice::from_raw_parts(buf as *const u8, nbyte as usize); - Sys::write(fildes, buf).or_minus_one_errno() + Sys::write(fildes, buf) + .map(|bytes| bytes as ssize_t) + .or_minus_one_errno() } diff --git a/src/header/utime/mod.rs b/src/header/utime/mod.rs index 5098cec7f576586b33e1a1c1e22c8c9cc391deab..bb17488d23c1d73971f453251bc7769a595cd158 100644 --- a/src/header/utime/mod.rs +++ b/src/header/utime/mod.rs @@ -2,6 +2,7 @@ use crate::{ c_str::CStr, + error::ResultExt, header::time::timespec, platform::{types::*, Pal, Sys}, }; @@ -27,4 +28,6 @@ pub unsafe extern "C" fn utime(filename: *const c_char, times: *const utimbuf) - }, ]; Sys::utimens(filename, times_spec.as_ptr()) + .map(|()| 0) + .or_minus_one_errno() } diff --git a/src/lib.rs b/src/lib.rs index 8ab4e35145c794f252916c2f2c768e82034d4dd5..4ae044e0dc1544bcae2112cf207f86b11aee2907 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,5 @@ #![no_std] +#![allow(warnings)] #![allow(non_camel_case_types)] #![allow(non_upper_case_globals)] #![allow(unused_variables)] diff --git a/src/platform/allocator/sys.rs b/src/platform/allocator/sys.rs index a2e6543fc19ff5392a1848989eb61cfa4c96effa..8241907c1df708a8649c08ee572db5b4dd642e38 100644 --- a/src/platform/allocator/sys.rs +++ b/src/platform/allocator/sys.rs @@ -25,7 +25,7 @@ static LOCK: Mutex<()> = Mutex::new(()); unsafe impl Allocator for System { fn alloc(&self, size: usize) -> (*mut u8, usize, u32) { - let addr = unsafe { + let Ok(addr) = (unsafe { Sys::mmap( 0 as *mut _, size, @@ -34,36 +34,33 @@ unsafe impl Allocator for System { -1, 0, ) + }) else { + return (ptr::null_mut(), 0, 0); }; - if addr as *mut c_void == MAP_FAILED { - (ptr::null_mut(), 0, 0) - } else { - (addr as *mut u8, size, 0) - } + (addr as *mut u8, size, 0) } fn remap(&self, ptr: *mut u8, oldsize: usize, newsize: usize, can_move: bool) -> *mut u8 { let flags = if can_move { MREMAP_MAYMOVE } else { 0 }; - let ptr = unsafe { Sys::mremap(ptr as *mut _, oldsize, newsize, flags, ptr::null_mut()) }; - if ptr as *mut c_void == MAP_FAILED { - ptr::null_mut() - } else { - ptr as *mut u8 - } + let Ok(ptr) = + (unsafe { Sys::mremap(ptr as *mut _, oldsize, newsize, flags, ptr::null_mut()) }) + else { + return ptr::null_mut(); + }; + ptr as *mut u8 } fn free_part(&self, ptr: *mut u8, oldsize: usize, newsize: usize) -> bool { unsafe { - let rc = Sys::mremap(ptr as *mut _, oldsize, newsize, 0, ptr::null_mut()); - if rc as *mut c_void != MAP_FAILED { + if Sys::mremap(ptr as *mut _, oldsize, newsize, 0, ptr::null_mut()).is_ok() { return true; } - Sys::munmap(ptr.offset(newsize as isize) as *mut _, oldsize - newsize) == 0 + Sys::munmap(ptr.add(newsize) as *mut _, oldsize - newsize).is_ok() } } fn free(&self, ptr: *mut u8, size: usize) -> bool { - unsafe { Sys::munmap(ptr as *mut _, size) == 0 } + unsafe { Sys::munmap(ptr as *mut _, size).is_ok() } } fn can_release_part(&self, _flags: u32) -> bool { diff --git a/src/platform/linux/epoll.rs b/src/platform/linux/epoll.rs index b92dc08d6b2923262a9167fa3464fcfb47415e5b..17b60959ad2695623d4262f0b8b5d594c4987423 100644 --- a/src/platform/linux/epoll.rs +++ b/src/platform/linux/epoll.rs @@ -1,34 +1,38 @@ -use super::{ - super::{types::*, PalEpoll}, - e, Sys, +use super::{e_raw, Sys}; +use crate::{ + error::Result, + header::{signal::sigset_t, sys_epoll::epoll_event}, + platform::{types::*, PalEpoll}, }; -use crate::header::{signal::sigset_t, sys_epoll::epoll_event}; impl PalEpoll for Sys { - fn epoll_create1(flags: c_int) -> c_int { - unsafe { e(syscall!(EPOLL_CREATE1, flags)) as c_int } + fn epoll_create1(flags: c_int) -> Result<c_int> { + Ok(unsafe { e_raw(syscall!(EPOLL_CREATE1, flags))? as c_int }) } - fn epoll_ctl(epfd: c_int, op: c_int, fd: c_int, event: *mut epoll_event) -> c_int { - unsafe { e(syscall!(EPOLL_CTL, epfd, op, fd, event)) as c_int } + unsafe fn epoll_ctl(epfd: c_int, op: c_int, fd: c_int, event: *mut epoll_event) -> Result<()> { + unsafe { + e_raw(syscall!(EPOLL_CTL, epfd, op, fd, event))?; + } + Ok(()) } - fn epoll_pwait( + unsafe fn epoll_pwait( epfd: c_int, events: *mut epoll_event, maxevents: c_int, timeout: c_int, sigmask: *const sigset_t, - ) -> c_int { + ) -> Result<usize> { unsafe { - e(syscall!( + e_raw(syscall!( EPOLL_PWAIT, epfd, events, maxevents, timeout, sigmask - )) as c_int + )) } } } diff --git a/src/platform/linux/mod.rs b/src/platform/linux/mod.rs index 3258a900a205b3bcd7a1cd3286b5e8e9340d4158..039f5a4b5d12b777e433f4a2d7e1be22c607a5e4 100644 --- a/src/platform/linux/mod.rs +++ b/src/platform/linux/mod.rs @@ -77,9 +77,9 @@ pub fn e(sys: usize) -> usize { pub struct Sys; impl Sys { - pub unsafe fn ioctl(fd: c_int, request: c_ulong, out: *mut c_void) -> c_int { + pub unsafe fn ioctl(fd: c_int, request: c_ulong, out: *mut c_void) -> Result<c_int> { // TODO: Somehow support varargs to syscall?? - e(syscall!(IOCTL, fd, request, out)) as c_int + Ok(e_raw(syscall!(IOCTL, fd, request, out))? as c_int) } // fn times(out: *mut tms) -> clock_t { @@ -92,8 +92,8 @@ impl Pal for Sys { e_raw(unsafe { syscall!(ACCESS, path.as_ptr(), mode) }).map(|_| ()) } - unsafe fn brk(addr: *mut c_void) -> *mut c_void { - unsafe { syscall!(BRK, addr) as *mut c_void } + unsafe fn brk(addr: *mut c_void) -> Result<*mut c_void> { + Ok(e_raw(unsafe { syscall!(BRK, addr) })? as *mut c_void) } fn chdir(path: CStr) -> Result<()> { @@ -144,10 +144,15 @@ impl Pal for Sys { e_raw(unsafe { syscall!(DUP3, fildes, fildes2, 0) }).map(|f| f as c_int) } - unsafe fn execve(path: CStr, argv: *const *mut c_char, envp: *const *mut c_char) -> c_int { - e(syscall!(EXECVE, path.as_ptr(), argv, envp)) as c_int + unsafe fn execve(path: CStr, argv: *const *mut c_char, envp: *const *mut c_char) -> Result<()> { + e_raw(syscall!(EXECVE, path.as_ptr(), argv, envp))?; + unreachable!() } - unsafe fn fexecve(fildes: c_int, argv: *const *mut c_char, envp: *const *mut c_char) -> c_int { + unsafe fn fexecve( + fildes: c_int, + argv: *const *mut c_char, + envp: *const *mut c_char, + ) -> Result<()> { todo!("not yet used by relibc") } @@ -178,53 +183,50 @@ impl Pal for Sys { e_raw(unsafe { syscall!(FDATASYNC, fildes) }).map(|_| ()) } - fn flock(fd: c_int, operation: c_int) -> c_int { - e(unsafe { syscall!(FLOCK, fd, operation) }) as c_int + fn flock(fd: c_int, operation: c_int) -> Result<()> { + e_raw(unsafe { syscall!(FLOCK, fd, operation) }).map(|_| ()) } - fn fstat(fildes: c_int, buf: *mut stat) -> c_int { + unsafe fn fstat(fildes: c_int, buf: *mut stat) -> Result<()> { let empty = b"\0"; let empty_ptr = empty.as_ptr() as *const c_char; - e(unsafe { syscall!(NEWFSTATAT, fildes, empty_ptr, buf, AT_EMPTY_PATH) }) as c_int + e_raw(unsafe { syscall!(NEWFSTATAT, fildes, empty_ptr, buf, AT_EMPTY_PATH) }).map(|_| ()) } - fn fstatvfs(fildes: c_int, buf: *mut statvfs) -> c_int { + unsafe fn fstatvfs(fildes: c_int, buf: *mut statvfs) -> Result<()> { let mut kbuf = linux_statfs::default(); let kbuf_ptr = &mut kbuf as *mut linux_statfs; - let res = e(unsafe { syscall!(FSTATFS, fildes, kbuf_ptr) }) as c_int; - if res == 0 { - unsafe { - if !buf.is_null() { - (*buf).f_bsize = kbuf.f_bsize as c_ulong; - (*buf).f_frsize = if kbuf.f_frsize != 0 { - kbuf.f_frsize - } else { - kbuf.f_bsize - } as c_ulong; - (*buf).f_blocks = kbuf.f_blocks; - (*buf).f_bfree = kbuf.f_bfree; - (*buf).f_bavail = kbuf.f_bavail; - (*buf).f_files = kbuf.f_files; - (*buf).f_ffree = kbuf.f_ffree; - (*buf).f_favail = kbuf.f_ffree; - (*buf).f_fsid = kbuf.f_fsid as c_ulong; - (*buf).f_flag = kbuf.f_flags as c_ulong; - (*buf).f_namemax = kbuf.f_namelen as c_ulong; - } - } + e_raw(syscall!(FSTATFS, fildes, kbuf_ptr))?; + + if !buf.is_null() { + (*buf).f_bsize = kbuf.f_bsize as c_ulong; + (*buf).f_frsize = if kbuf.f_frsize != 0 { + kbuf.f_frsize + } else { + kbuf.f_bsize + } as c_ulong; + (*buf).f_blocks = kbuf.f_blocks; + (*buf).f_bfree = kbuf.f_bfree; + (*buf).f_bavail = kbuf.f_bavail; + (*buf).f_files = kbuf.f_files; + (*buf).f_ffree = kbuf.f_ffree; + (*buf).f_favail = kbuf.f_ffree; + (*buf).f_fsid = kbuf.f_fsid as c_ulong; + (*buf).f_flag = kbuf.f_flags as c_ulong; + (*buf).f_namemax = kbuf.f_namelen as c_ulong; } - res + Ok(()) } - fn fcntl(fildes: c_int, cmd: c_int, arg: c_ulonglong) -> c_int { - e(unsafe { syscall!(FCNTL, fildes, cmd, arg) }) as c_int + fn fcntl(fildes: c_int, cmd: c_int, arg: c_ulonglong) -> Result<c_int> { + Ok(e_raw(unsafe { syscall!(FCNTL, fildes, cmd, arg) })? as c_int) } - fn fork() -> pid_t { - e(unsafe { syscall!(CLONE, SIGCHLD, 0, 0, 0, 0) }) as pid_t + unsafe fn fork() -> Result<pid_t> { + Ok(e_raw(unsafe { syscall!(CLONE, SIGCHLD, 0, 0, 0, 0) })? as pid_t) } - fn fpath(fildes: c_int, out: &mut [u8]) -> Result<ssize_t> { + fn fpath(fildes: c_int, out: &mut [u8]) -> Result<usize> { let mut proc_path = b"/proc/self/fd/".to_vec(); write!(proc_path, "{}", fildes).unwrap(); proc_path.push(0); @@ -263,20 +265,17 @@ impl Pal for Sys { .map(|n| n as u32) } - fn futimens(fd: c_int, times: *const timespec) -> c_int { - e(unsafe { syscall!(UTIMENSAT, fd, ptr::null::<c_char>(), times, 0) }) as c_int + unsafe fn futimens(fd: c_int, times: *const timespec) -> Result<()> { + e_raw(unsafe { syscall!(UTIMENSAT, fd, ptr::null::<c_char>(), times, 0) }).map(|_| ()) } - fn utimens(path: CStr, times: *const timespec) -> c_int { - e(unsafe { syscall!(UTIMENSAT, AT_FDCWD, path.as_ptr(), times, 0) }) as c_int + unsafe fn utimens(path: CStr, times: *const timespec) -> Result<()> { + e_raw(unsafe { syscall!(UTIMENSAT, AT_FDCWD, path.as_ptr(), times, 0) }).map(|_| ()) } - fn getcwd(buf: *mut c_char, size: size_t) -> *mut c_char { - if e(unsafe { syscall!(GETCWD, buf, size) }) == !0 { - ptr::null_mut() - } else { - buf - } + unsafe fn getcwd(buf: *mut c_char, size: size_t) -> Result<()> { + e_raw(unsafe { syscall!(GETCWD, buf, size) })?; + Ok(()) } fn getdents(fd: c_int, buf: &mut [u8], _off: u64) -> Result<usize> { @@ -292,43 +291,48 @@ impl Pal for Sys { } fn getegid() -> gid_t { - e(unsafe { syscall!(GETEGID) }) as gid_t + // Always successful + unsafe { syscall!(GETEGID) as gid_t } } fn geteuid() -> uid_t { - e(unsafe { syscall!(GETEUID) }) as uid_t + // Always successful + unsafe { syscall!(GETEUID) as uid_t } } fn getgid() -> gid_t { - e(unsafe { syscall!(GETGID) }) as gid_t + // Always successful + unsafe { syscall!(GETGID) as gid_t } } - unsafe fn getgroups(size: c_int, list: *mut gid_t) -> c_int { - e(unsafe { syscall!(GETGROUPS, size, list) }) as c_int + unsafe fn getgroups(size: c_int, list: *mut gid_t) -> Result<c_int> { + Ok(e_raw(unsafe { syscall!(GETGROUPS, size, list) })? as c_int) } fn getpagesize() -> usize { 4096 } - fn getpgid(pid: pid_t) -> pid_t { - e(unsafe { syscall!(GETPGID, pid) }) as pid_t + fn getpgid(pid: pid_t) -> Result<pid_t> { + Ok(e_raw(unsafe { syscall!(GETPGID, pid) })? as pid_t) } fn getpid() -> pid_t { - e(unsafe { syscall!(GETPID) }) as pid_t + // Always successful + unsafe { syscall!(GETPID) as pid_t } } fn getppid() -> pid_t { - e(unsafe { syscall!(GETPPID) }) as pid_t + // Always successful + unsafe { syscall!(GETPPID) as pid_t } } - fn getpriority(which: c_int, who: id_t) -> c_int { - e(unsafe { syscall!(GETPRIORITY, which, who) }) as c_int + fn getpriority(which: c_int, who: id_t) -> Result<c_int> { + Ok(e_raw(unsafe { syscall!(GETPRIORITY, which, who) })? as c_int) } - fn getrandom(buf: &mut [u8], flags: c_uint) -> ssize_t { - e(unsafe { syscall!(GETRANDOM, buf.as_mut_ptr(), buf.len(), flags) }) as ssize_t + fn getrandom(buf: &mut [u8], flags: c_uint) -> Result<usize> { + e_raw(unsafe { syscall!(GETRANDOM, buf.as_mut_ptr(), buf.len(), flags) }) } unsafe fn getrlimit(resource: c_int, rlim: *mut rlimit) -> Result<()> { @@ -339,24 +343,26 @@ impl Pal for Sys { e_raw(syscall!(SETRLIMIT, resource, rlimit)).map(|_| ()) } - fn getrusage(who: c_int, r_usage: &mut rusage) -> c_int { - e(unsafe { syscall!(GETRUSAGE, who, r_usage as *mut rusage) }) as c_int + fn getrusage(who: c_int, r_usage: &mut rusage) -> Result<()> { + e_raw(unsafe { syscall!(GETRUSAGE, who, r_usage as *mut rusage) })?; + Ok(()) } - fn getsid(pid: pid_t) -> pid_t { - e(unsafe { syscall!(GETSID, pid) }) as pid_t + fn getsid(pid: pid_t) -> Result<pid_t> { + Ok(e_raw(unsafe { syscall!(GETSID, pid) })? as pid_t) } fn gettid() -> pid_t { - e(unsafe { syscall!(GETTID) }) as pid_t + // Always successful + unsafe { syscall!(GETTID) as pid_t } } - fn gettimeofday(tp: *mut timeval, tzp: *mut timezone) -> c_int { - e(unsafe { syscall!(GETTIMEOFDAY, tp, tzp) }) as c_int + unsafe fn gettimeofday(tp: *mut timeval, tzp: *mut timezone) -> Result<()> { + e_raw(unsafe { syscall!(GETTIMEOFDAY, tp, tzp) }).map(|_| ()) } fn getuid() -> uid_t { - e(unsafe { syscall!(GETUID) }) as uid_t + unsafe { syscall!(GETUID) as uid_t } } fn lchown(path: CStr, owner: uid_t, group: gid_t) -> Result<()> { @@ -404,12 +410,12 @@ impl Pal for Sys { Sys::mknod(path, mode | S_IFIFO, 0) } - unsafe fn mlock(addr: *const c_void, len: usize) -> c_int { - e(syscall!(MLOCK, addr, len)) as c_int + unsafe fn mlock(addr: *const c_void, len: usize) -> Result<()> { + e_raw(unsafe { syscall!(MLOCK, addr, len) }).map(|_| ()) } - fn mlockall(flags: c_int) -> c_int { - e(unsafe { syscall!(MLOCKALL, flags) }) as c_int + unsafe fn mlockall(flags: c_int) -> Result<()> { + e_raw(unsafe { syscall!(MLOCKALL, flags) }).map(|_| ()) } unsafe fn mmap( @@ -419,8 +425,8 @@ impl Pal for Sys { flags: c_int, fildes: c_int, off: off_t, - ) -> *mut c_void { - e(syscall!(MMAP, addr, len, prot, flags, fildes, off)) as *mut c_void + ) -> Result<*mut c_void> { + Ok(e_raw(syscall!(MMAP, addr, len, prot, flags, fildes, off))? as *mut c_void) } unsafe fn mremap( @@ -429,36 +435,36 @@ impl Pal for Sys { new_len: usize, flags: c_int, args: *mut c_void, - ) -> *mut c_void { - e(syscall!(MREMAP, addr, len, new_len, flags, args)) as *mut c_void + ) -> Result<*mut c_void> { + Ok(e_raw(syscall!(MREMAP, addr, len, new_len, flags, args))? as *mut c_void) } - unsafe fn mprotect(addr: *mut c_void, len: usize, prot: c_int) -> c_int { - e(syscall!(MPROTECT, addr, len, prot)) as c_int + unsafe fn mprotect(addr: *mut c_void, len: usize, prot: c_int) -> Result<()> { + e_raw(syscall!(MPROTECT, addr, len, prot)).map(|_| ()) } - unsafe fn msync(addr: *mut c_void, len: usize, flags: c_int) -> c_int { - e(syscall!(MSYNC, addr, len, flags)) as c_int + unsafe fn msync(addr: *mut c_void, len: usize, flags: c_int) -> Result<()> { + e_raw(syscall!(MSYNC, addr, len, flags)).map(|_| ()) } - unsafe fn munlock(addr: *const c_void, len: usize) -> c_int { - e(syscall!(MUNLOCK, addr, len)) as c_int + unsafe fn munlock(addr: *const c_void, len: usize) -> Result<()> { + e_raw(syscall!(MUNLOCK, addr, len)).map(|_| ()) } - fn munlockall() -> c_int { - e(unsafe { syscall!(MUNLOCKALL) }) as c_int + unsafe fn munlockall() -> Result<()> { + e_raw(unsafe { syscall!(MUNLOCKALL) }).map(|_| ()) } - unsafe fn munmap(addr: *mut c_void, len: usize) -> c_int { - e(syscall!(MUNMAP, addr, len)) as c_int + unsafe fn munmap(addr: *mut c_void, len: usize) -> Result<()> { + e_raw(syscall!(MUNMAP, addr, len)).map(|_| ()) } - unsafe fn madvise(addr: *mut c_void, len: usize, flags: c_int) -> c_int { - e(syscall!(MADVISE, addr, len, flags)) as c_int + unsafe fn madvise(addr: *mut c_void, len: usize, flags: c_int) -> Result<()> { + e_raw(syscall!(MADVISE, addr, len, flags)).map(|_| ()) } - fn nanosleep(rqtp: *const timespec, rmtp: *mut timespec) -> c_int { - e(unsafe { syscall!(NANOSLEEP, rqtp, rmtp) }) as c_int + unsafe fn nanosleep(rqtp: *const timespec, rmtp: *mut timespec) -> Result<()> { + e_raw(unsafe { syscall!(NANOSLEEP, rqtp, rmtp) }).map(|_| ()) } fn open(path: CStr, oflag: c_int, mode: mode_t) -> Result<c_int> { @@ -534,17 +540,14 @@ impl Pal for Sys { } } - fn read(fildes: c_int, buf: &mut [u8]) -> Result<ssize_t> { - Ok(e_raw(unsafe { syscall!(READ, fildes, buf.as_mut_ptr(), buf.len()) })? as ssize_t) + fn read(fildes: c_int, buf: &mut [u8]) -> Result<usize> { + e_raw(unsafe { syscall!(READ, fildes, buf.as_mut_ptr(), buf.len()) }) } - fn pread(fildes: c_int, buf: &mut [u8], off: off_t) -> Result<ssize_t> { - Ok( - e_raw(unsafe { syscall!(PREAD64, fildes, buf.as_mut_ptr(), buf.len(), off) })? - as ssize_t, - ) + fn pread(fildes: c_int, buf: &mut [u8], off: off_t) -> Result<usize> { + e_raw(unsafe { syscall!(PREAD64, fildes, buf.as_mut_ptr(), buf.len(), off) }) } - fn readlink(pathname: CStr, out: &mut [u8]) -> Result<ssize_t> { + fn readlink(pathname: CStr, out: &mut [u8]) -> Result<usize> { e_raw(unsafe { syscall!( READLINKAT, @@ -554,7 +557,6 @@ impl Pal for Sys { out.len() ) }) - .map(|b| b as ssize_t) } fn rename(old: CStr, new: CStr) -> Result<()> { @@ -614,19 +616,19 @@ impl Pal for Sys { e_raw(unsafe { syscall!(UNLINKAT, AT_FDCWD, path.as_ptr(), 0) }).map(|_| ()) } - fn waitpid(pid: pid_t, stat_loc: *mut c_int, options: c_int) -> pid_t { - e(unsafe { syscall!(WAIT4, pid, stat_loc, options, 0) }) as pid_t + unsafe fn waitpid(pid: pid_t, stat_loc: *mut c_int, options: c_int) -> Result<pid_t> { + e_raw(unsafe { syscall!(WAIT4, pid, stat_loc, options, 0) }).map(|p| p as pid_t) } - fn write(fildes: c_int, buf: &[u8]) -> Result<ssize_t> { - Ok(e_raw(unsafe { syscall!(WRITE, fildes, buf.as_ptr(), buf.len()) })? as ssize_t) + fn write(fildes: c_int, buf: &[u8]) -> Result<usize> { + e_raw(unsafe { syscall!(WRITE, fildes, buf.as_ptr(), buf.len()) }) } - fn pwrite(fildes: c_int, buf: &[u8], off: off_t) -> Result<ssize_t> { - Ok(e_raw(unsafe { syscall!(PWRITE64, fildes, buf.as_ptr(), buf.len(), off) })? as ssize_t) + fn pwrite(fildes: c_int, buf: &[u8], off: off_t) -> Result<usize> { + e_raw(unsafe { syscall!(PWRITE64, fildes, buf.as_ptr(), buf.len(), off) }) } fn verify() -> bool { // GETPID on Linux is 39, which does not exist on Redox - e(unsafe { sc::syscall5(sc::nr::GETPID, !0, !0, !0, !0, !0) }) != !0 + e_raw(unsafe { sc::syscall5(sc::nr::GETPID, !0, !0, !0, !0, !0) }).is_ok() } } diff --git a/src/platform/linux/signal.rs b/src/platform/linux/signal.rs index 7a379e06af65d94d7ee6a88daa5c46a34273bb81..10cefc820000249e6c1309f338f65dd8bcd90c43 100644 --- a/src/platform/linux/signal.rs +++ b/src/platform/linux/signal.rs @@ -6,7 +6,7 @@ use super::{ e, e_raw, Sys, }; use crate::{ - error::Errno, + error::{Errno, Result}, header::{ signal::{sigaction, siginfo_t, sigset_t, stack_t, NSIG, SA_RESTORER, SI_QUEUE}, sys_time::itimerval, @@ -57,14 +57,16 @@ pub struct mcontext_t { } impl PalSignal for Sys { - unsafe fn getitimer(which: c_int, out: *mut itimerval) -> c_int { - e(syscall!(GETITIMER, which, out)) as c_int + unsafe fn getitimer(which: c_int, out: *mut itimerval) -> Result<()> { + e_raw(syscall!(GETITIMER, which, out))?; + Ok(()) } - fn kill(pid: pid_t, sig: c_int) -> c_int { - e(unsafe { syscall!(KILL, pid, sig) }) as c_int + fn kill(pid: pid_t, sig: c_int) -> Result<()> { + e_raw(unsafe { syscall!(KILL, pid, sig) })?; + Ok(()) } - fn sigqueue(pid: pid_t, sig: c_int, val: sigval) -> Result<(), Errno> { + fn sigqueue(pid: pid_t, sig: c_int, val: sigval) -> Result<()> { let info = siginfo_t { si_addr: core::ptr::null_mut(), si_code: SI_QUEUE, @@ -78,18 +80,20 @@ impl PalSignal for Sys { e_raw(unsafe { syscall!(RT_SIGQUEUEINFO, pid, sig, addr_of!(info)) }).map(|_| ()) } - fn killpg(pgrp: pid_t, sig: c_int) -> c_int { - e(unsafe { syscall!(KILL, -(pgrp as isize) as pid_t, sig) }) as c_int + fn killpg(pgrp: pid_t, sig: c_int) -> Result<()> { + e_raw(unsafe { syscall!(KILL, -(pgrp as isize) as pid_t, sig) })?; + Ok(()) } - fn raise(sig: c_int) -> Result<(), Errno> { + fn raise(sig: c_int) -> Result<()> { let tid = e_raw(unsafe { syscall!(GETTID) })? as pid_t; e_raw(unsafe { syscall!(TKILL, tid, sig) })?; Ok(()) } - unsafe fn setitimer(which: c_int, new: *const itimerval, old: *mut itimerval) -> c_int { - e(syscall!(SETITIMER, which, new, old)) as c_int + unsafe fn setitimer(which: c_int, new: *const itimerval, old: *mut itimerval) -> Result<()> { + e_raw(unsafe { syscall!(SETITIMER, which, new, old) })?; + Ok(()) } fn sigaction( @@ -118,7 +122,7 @@ impl PalSignal for Sys { .map(|_| ()) } - unsafe fn sigaltstack(ss: Option<&stack_t>, old_ss: Option<&mut stack_t>) -> Result<(), Errno> { + unsafe fn sigaltstack(ss: Option<&stack_t>, old_ss: Option<&mut stack_t>) -> Result<()> { e_raw(syscall!( SIGALTSTACK, ss.map_or_else(core::ptr::null, |x| x as *const _), @@ -127,7 +131,7 @@ impl PalSignal for Sys { .map(|_| ()) } - fn sigpending(set: &mut sigset_t) -> Result<(), Errno> { + fn sigpending(set: &mut sigset_t) -> Result<()> { e_raw(unsafe { syscall!( RT_SIGPENDING, @@ -138,11 +142,7 @@ impl PalSignal for Sys { .map(|_| ()) } - fn sigprocmask( - how: c_int, - set: Option<&sigset_t>, - oset: Option<&mut sigset_t>, - ) -> Result<(), Errno> { + fn sigprocmask(how: c_int, set: Option<&sigset_t>, oset: Option<&mut sigset_t>) -> Result<()> { e_raw(unsafe { syscall!( RT_SIGPROCMASK, @@ -166,7 +166,7 @@ impl PalSignal for Sys { set: &sigset_t, sig: Option<&mut siginfo_t>, tp: Option<×pec>, - ) -> Result<(), Errno> { + ) -> Result<()> { unsafe { e_raw(syscall!( RT_SIGTIMEDWAIT, diff --git a/src/platform/mod.rs b/src/platform/mod.rs index 9b51834582fb2997ae606854237405abcd49ae12..d94cdb7e46df0f7d76913fe326fb54f2387aa5ef 100644 --- a/src/platform/mod.rs +++ b/src/platform/mod.rs @@ -86,7 +86,9 @@ pub struct FileWriter(pub c_int); impl FileWriter { pub fn write(&mut self, buf: &[u8]) -> isize { - Sys::write(self.0, buf).or_minus_one_errno() + Sys::write(self.0, buf) + .map(|u| u as isize) + .or_minus_one_errno() } } @@ -107,14 +109,19 @@ impl WriteByte for FileWriter { pub struct FileReader(pub c_int); impl FileReader { + // TODO: This is a bad interface. Rustify pub fn read(&mut self, buf: &mut [u8]) -> isize { - Sys::read(self.0, buf).or_minus_one_errno() + Sys::read(self.0, buf) + .map(|u| u as isize) + .or_minus_one_errno() } } impl Read for FileReader { fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { - let i = Sys::read(self.0, buf).or_minus_one_errno(); // TODO + let i = Sys::read(self.0, buf) + .map(|u| u as isize) + .or_minus_one_errno(); // TODO if i >= 0 { Ok(i as usize) } else { diff --git a/src/platform/pal/epoll.rs b/src/platform/pal/epoll.rs index 6162368b384f481cf9fdffb7688dfd9d47cbc0a5..3d72f566ce083e522508e64f9f41d02356a92768 100644 --- a/src/platform/pal/epoll.rs +++ b/src/platform/pal/epoll.rs @@ -1,14 +1,17 @@ -use super::super::{types::*, Pal}; -use crate::header::{signal::sigset_t, sys_epoll::epoll_event}; +use crate::{ + error::{Errno, Result}, + header::{signal::sigset_t, sys_epoll::epoll_event}, + platform::{types::*, Pal}, +}; pub trait PalEpoll: Pal { - fn epoll_create1(flags: c_int) -> c_int; - fn epoll_ctl(epfd: c_int, op: c_int, fd: c_int, event: *mut epoll_event) -> c_int; - fn epoll_pwait( + fn epoll_create1(flags: c_int) -> Result<c_int>; + unsafe fn epoll_ctl(epfd: c_int, op: c_int, fd: c_int, event: *mut epoll_event) -> Result<()>; + unsafe fn epoll_pwait( epfd: c_int, events: *mut epoll_event, maxevents: c_int, timeout: c_int, sigmask: *const sigset_t, - ) -> c_int; + ) -> Result<usize>; } diff --git a/src/platform/pal/mod.rs b/src/platform/pal/mod.rs index e8716defdea9e06d38a38c1147286add0b8ee2c7..c80069001e1038bb4af63ea7ab25df9d095d18be 100644 --- a/src/platform/pal/mod.rs +++ b/src/platform/pal/mod.rs @@ -28,7 +28,7 @@ mod socket; pub trait Pal { fn access(path: CStr, mode: c_int) -> Result<()>; - unsafe fn brk(addr: *mut c_void) -> *mut c_void; + unsafe fn brk(addr: *mut c_void) -> Result<*mut c_void>; fn chdir(path: CStr) -> Result<()>; @@ -50,8 +50,12 @@ pub trait Pal { fn dup2(fildes: c_int, fildes2: c_int) -> Result<c_int>; - unsafe fn execve(path: CStr, argv: *const *mut c_char, envp: *const *mut c_char) -> c_int; - unsafe fn fexecve(fildes: c_int, argv: *const *mut c_char, envp: *const *mut c_char) -> c_int; + unsafe fn execve(path: CStr, argv: *const *mut c_char, envp: *const *mut c_char) -> Result<()>; + unsafe fn fexecve( + fildes: c_int, + argv: *const *mut c_char, + envp: *const *mut c_char, + ) -> Result<()>; fn exit(status: c_int) -> !; @@ -65,17 +69,17 @@ pub trait Pal { fn fdatasync(fildes: c_int) -> Result<()>; - fn flock(fd: c_int, operation: c_int) -> c_int; + fn flock(fd: c_int, operation: c_int) -> Result<()>; - fn fstat(fildes: c_int, buf: *mut stat) -> c_int; + unsafe fn fstat(fildes: c_int, buf: *mut stat) -> Result<()>; - fn fstatvfs(fildes: c_int, buf: *mut statvfs) -> c_int; + unsafe fn fstatvfs(fildes: c_int, buf: *mut statvfs) -> Result<()>; - fn fcntl(fildes: c_int, cmd: c_int, arg: c_ulonglong) -> c_int; + fn fcntl(fildes: c_int, cmd: c_int, arg: c_ulonglong) -> Result<c_int>; - fn fork() -> pid_t; + unsafe fn fork() -> Result<pid_t>; - fn fpath(fildes: c_int, out: &mut [u8]) -> Result<ssize_t>; + fn fpath(fildes: c_int, out: &mut [u8]) -> Result<usize>; fn fsync(fildes: c_int) -> Result<()>; @@ -84,11 +88,11 @@ pub trait Pal { unsafe fn futex_wait(addr: *mut u32, val: u32, deadline: Option<×pec>) -> Result<()>; unsafe fn futex_wake(addr: *mut u32, num: u32) -> Result<u32>; - fn futimens(fd: c_int, times: *const timespec) -> c_int; + unsafe fn futimens(fd: c_int, times: *const timespec) -> Result<()>; - fn utimens(path: CStr, times: *const timespec) -> c_int; + unsafe fn utimens(path: CStr, times: *const timespec) -> Result<()>; - fn getcwd(buf: *mut c_char, size: size_t) -> *mut c_char; + unsafe fn getcwd(buf: *mut c_char, size: size_t) -> Result<()>; fn getdents(fd: c_int, buf: &mut [u8], opaque_offset: u64) -> Result<usize>; fn dir_seek(fd: c_int, opaque_offset: u64) -> Result<()>; @@ -98,40 +102,46 @@ pub trait Pal { // possible this_dent slice is safe (and will be validated). unsafe fn dent_reclen_offset(this_dent: &[u8], offset: usize) -> Option<(u16, u64)>; + // Always successful fn getegid() -> gid_t; + // Always successful fn geteuid() -> uid_t; + // Always successful fn getgid() -> gid_t; - unsafe fn getgroups(size: c_int, list: *mut gid_t) -> c_int; + unsafe fn getgroups(size: c_int, list: *mut gid_t) -> Result<c_int>; /* Note that this is distinct from the legacy POSIX function * getpagesize(), which returns a c_int. On some Linux platforms, * page size may be determined through a syscall ("getpagesize"). */ fn getpagesize() -> usize; - fn getpgid(pid: pid_t) -> pid_t; + fn getpgid(pid: pid_t) -> Result<pid_t>; + // Always successful fn getpid() -> pid_t; + // Always successful fn getppid() -> pid_t; - fn getpriority(which: c_int, who: id_t) -> c_int; + fn getpriority(which: c_int, who: id_t) -> Result<c_int>; - fn getrandom(buf: &mut [u8], flags: c_uint) -> ssize_t; + fn getrandom(buf: &mut [u8], flags: c_uint) -> Result<usize>; unsafe fn getrlimit(resource: c_int, rlim: *mut rlimit) -> Result<()>; unsafe fn setrlimit(resource: c_int, rlim: *const rlimit) -> Result<()>; - fn getrusage(who: c_int, r_usage: &mut rusage) -> c_int; + fn getrusage(who: c_int, r_usage: &mut rusage) -> Result<()>; - fn getsid(pid: pid_t) -> pid_t; + fn getsid(pid: pid_t) -> Result<pid_t>; + // Always successful fn gettid() -> pid_t; - fn gettimeofday(tp: *mut timeval, tzp: *mut timezone) -> c_int; + unsafe fn gettimeofday(tp: *mut timeval, tzp: *mut timezone) -> Result<()>; fn getuid() -> uid_t; @@ -149,9 +159,9 @@ pub trait Pal { fn mknod(path: CStr, mode: mode_t, dev: dev_t) -> Result<()>; - unsafe fn mlock(addr: *const c_void, len: usize) -> c_int; + unsafe fn mlock(addr: *const c_void, len: usize) -> Result<()>; - fn mlockall(flags: c_int) -> c_int; + unsafe fn mlockall(flags: c_int) -> Result<()>; unsafe fn mmap( addr: *mut c_void, @@ -160,7 +170,7 @@ pub trait Pal { flags: c_int, fildes: c_int, off: off_t, - ) -> *mut c_void; + ) -> Result<*mut c_void>; unsafe fn mremap( addr: *mut c_void, @@ -168,21 +178,21 @@ pub trait Pal { new_len: usize, flags: c_int, args: *mut c_void, - ) -> *mut c_void; + ) -> Result<*mut c_void>; - unsafe fn mprotect(addr: *mut c_void, len: usize, prot: c_int) -> c_int; + unsafe fn mprotect(addr: *mut c_void, len: usize, prot: c_int) -> Result<()>; - unsafe fn msync(addr: *mut c_void, len: usize, flags: c_int) -> c_int; + unsafe fn msync(addr: *mut c_void, len: usize, flags: c_int) -> Result<()>; - unsafe fn munlock(addr: *const c_void, len: usize) -> c_int; + unsafe fn munlock(addr: *const c_void, len: usize) -> Result<()>; - unsafe fn madvise(addr: *mut c_void, len: usize, flags: c_int) -> c_int; + unsafe fn madvise(addr: *mut c_void, len: usize, flags: c_int) -> Result<()>; - fn munlockall() -> c_int; + unsafe fn munlockall() -> Result<()>; - unsafe fn munmap(addr: *mut c_void, len: usize) -> c_int; + unsafe fn munmap(addr: *mut c_void, len: usize) -> Result<()>; - fn nanosleep(rqtp: *const timespec, rmtp: *mut timespec) -> c_int; + unsafe fn nanosleep(rqtp: *const timespec, rmtp: *mut timespec) -> Result<()>; fn open(path: CStr, oflag: c_int, mode: mode_t) -> Result<c_int>; @@ -193,10 +203,10 @@ pub trait Pal { fn current_os_tid() -> pthread::OsTid; - fn read(fildes: c_int, buf: &mut [u8]) -> Result<ssize_t>; - fn pread(fildes: c_int, buf: &mut [u8], offset: off_t) -> Result<ssize_t>; + fn read(fildes: c_int, buf: &mut [u8]) -> Result<usize>; + fn pread(fildes: c_int, buf: &mut [u8], offset: off_t) -> Result<usize>; - fn readlink(pathname: CStr, out: &mut [u8]) -> Result<ssize_t>; + fn readlink(pathname: CStr, out: &mut [u8]) -> Result<usize>; fn rename(old: CStr, new: CStr) -> Result<()>; @@ -220,16 +230,17 @@ pub trait Pal { fn sync() -> Result<()>; + // Always successful fn umask(mask: mode_t) -> mode_t; unsafe fn uname(utsname: *mut utsname) -> Result<()>; fn unlink(path: CStr) -> Result<()>; - fn waitpid(pid: pid_t, stat_loc: *mut c_int, options: c_int) -> pid_t; + unsafe fn waitpid(pid: pid_t, stat_loc: *mut c_int, options: c_int) -> Result<pid_t>; - fn write(fildes: c_int, buf: &[u8]) -> Result<ssize_t>; - fn pwrite(fildes: c_int, buf: &[u8], offset: off_t) -> Result<ssize_t>; + fn write(fildes: c_int, buf: &[u8]) -> Result<usize>; + fn pwrite(fildes: c_int, buf: &[u8], offset: off_t) -> Result<usize>; fn verify() -> bool; } diff --git a/src/platform/pal/signal.rs b/src/platform/pal/signal.rs index 7a01780ac15301d57028803ce22ace6c870f2714..695dcb65d97680865714815988e7d3c946fc5f3e 100644 --- a/src/platform/pal/signal.rs +++ b/src/platform/pal/signal.rs @@ -1,6 +1,6 @@ use super::super::{types::*, Pal}; use crate::{ - error::Errno, + error::{Errno, Result}, header::{ signal::{sigaction, siginfo_t, sigset_t, sigval, stack_t}, sys_time::itimerval, @@ -9,33 +9,25 @@ use crate::{ }; pub trait PalSignal: Pal { - unsafe fn getitimer(which: c_int, out: *mut itimerval) -> c_int; + unsafe fn getitimer(which: c_int, out: *mut itimerval) -> Result<()>; - fn kill(pid: pid_t, sig: c_int) -> c_int; + fn kill(pid: pid_t, sig: c_int) -> Result<()>; - fn sigqueue(pid: pid_t, sig: c_int, val: sigval) -> Result<(), Errno>; + fn sigqueue(pid: pid_t, sig: c_int, val: sigval) -> Result<()>; - fn killpg(pgrp: pid_t, sig: c_int) -> c_int; + fn killpg(pgrp: pid_t, sig: c_int) -> Result<()>; - fn raise(sig: c_int) -> Result<(), Errno>; + fn raise(sig: c_int) -> Result<()>; - unsafe fn setitimer(which: c_int, new: *const itimerval, old: *mut itimerval) -> c_int; + unsafe fn setitimer(which: c_int, new: *const itimerval, old: *mut itimerval) -> Result<()>; - fn sigaction( - sig: c_int, - act: Option<&sigaction>, - oact: Option<&mut sigaction>, - ) -> Result<(), Errno>; + fn sigaction(sig: c_int, act: Option<&sigaction>, oact: Option<&mut sigaction>) -> Result<()>; - unsafe fn sigaltstack(ss: Option<&stack_t>, old_ss: Option<&mut stack_t>) -> Result<(), Errno>; + unsafe fn sigaltstack(ss: Option<&stack_t>, old_ss: Option<&mut stack_t>) -> Result<()>; - fn sigpending(set: &mut sigset_t) -> Result<(), Errno>; + fn sigpending(set: &mut sigset_t) -> Result<()>; - fn sigprocmask( - how: c_int, - set: Option<&sigset_t>, - oset: Option<&mut sigset_t>, - ) -> Result<(), Errno>; + fn sigprocmask(how: c_int, set: Option<&sigset_t>, oset: Option<&mut sigset_t>) -> Result<()>; fn sigsuspend(mask: &sigset_t) -> Errno; // always fails @@ -43,5 +35,5 @@ pub trait PalSignal: Pal { set: &sigset_t, sig: Option<&mut siginfo_t>, tp: Option<×pec>, - ) -> Result<(), Errno>; + ) -> Result<()>; } diff --git a/src/platform/redox/epoll.rs b/src/platform/redox/epoll.rs index 25521854d62ced4dc7e1ea6860e019b46c64249c..89adbbf8f6cd21aa9242536c340d136fe7666cfc 100644 --- a/src/platform/redox/epoll.rs +++ b/src/platform/redox/epoll.rs @@ -4,7 +4,7 @@ use super::{ }; use crate::{ - error::ResultExt, + error::{Errno, ResultExt}, fs::File, header::{errno::*, fcntl::*, signal::sigset_t, sys_epoll::*}, io::prelude::*, @@ -52,14 +52,19 @@ fn event_flags_to_epoll(flags: syscall::EventFlags) -> c_uint { } impl PalEpoll for Sys { - fn epoll_create1(flags: c_int) -> c_int { - Sys::open(c_str!("/scheme/event"), O_RDWR | flags, 0).or_minus_one_errno() + fn epoll_create1(flags: c_int) -> Result<c_int, Errno> { + Sys::open(c_str!("/scheme/event"), O_RDWR | flags, 0) } - fn epoll_ctl(epfd: c_int, op: c_int, fd: c_int, event: *mut epoll_event) -> c_int { + unsafe fn epoll_ctl( + epfd: c_int, + op: c_int, + fd: c_int, + event: *mut epoll_event, + ) -> Result<(), Errno> { match op { EPOLL_CTL_ADD | EPOLL_CTL_MOD => { - if Sys::write( + Sys::write( epfd, &Event { id: fd as usize, @@ -68,17 +73,10 @@ impl PalEpoll for Sys { // systems. If this is needed, use a box or something data: unsafe { (*event).data.u64 as usize }, }, - ) - .or_minus_one_errno() - < 0 - { - -1 - } else { - 0 - } + )?; } EPOLL_CTL_DEL => { - if Sys::write( + Sys::write( epfd, &Event { id: fd as usize, @@ -86,68 +84,49 @@ impl PalEpoll for Sys { //TODO: Is data required? data: 0, }, - ) - .or_minus_one_errno() - < 0 - { - -1 - } else { - 0 - } - } - _ => { - platform::ERRNO.set(EINVAL); - -1 + )?; } + _ => return Err(Errno(EINVAL)), } + Ok(()) } - fn epoll_pwait( + unsafe fn epoll_pwait( epfd: c_int, events: *mut epoll_event, maxevents: c_int, timeout: c_int, _sigset: *const sigset_t, - ) -> c_int { + ) -> Result<usize, Errno> { // TODO: sigset assert_eq!(mem::size_of::<epoll_event>(), mem::size_of::<Event>()); if maxevents <= 0 { - platform::ERRNO.set(EINVAL); - return -1; + return Err(Errno(EINVAL)); } let timer_opt = if timeout != -1 { - match File::open(c_str!("/scheme/time/4"), O_RDWR) { - Err(_) => return -1, - Ok(mut timer) => { - if Sys::write( - epfd, - &Event { - id: timer.fd as usize, - flags: EVENT_READ, - data: 0, - }, - ) - .or_minus_one_errno() - == -1 - { - return -1; - } - - let mut time = TimeSpec::default(); - if let Err(err) = timer.read(&mut time) { - return -1; - } - time.tv_sec += (timeout as i64) / 1000; - time.tv_nsec += (timeout % 1000) * 1000000; - if let Err(err) = timer.write(&time) { - return -1; - } - - Some(timer) - } - } + let mut timer = File::open(c_str!("/scheme/time/4"), O_RDWR)?; + Sys::write( + epfd, + &Event { + id: timer.fd as usize, + flags: EVENT_READ, + data: 0, + }, + )?; + + let mut time = TimeSpec::default(); + let _ = timer + .read(&mut time) + .map_err(|err| Errno(err.raw_os_error().unwrap_or(EIO)))?; + time.tv_sec += (timeout as i64) / 1000; + time.tv_nsec += (timeout % 1000) * 1000000; + let _ = timer + .write(&time) + .map_err(|err| Errno(err.raw_os_error().unwrap_or(EIO)))?; + + Some(timer) } else { None }; @@ -157,11 +136,7 @@ impl PalEpoll for Sys { events as *mut u8, maxevents as usize * mem::size_of::<syscall::Event>(), ) - }) - .or_minus_one_errno(); // TODO - if bytes_read == -1 { - return -1; - } + })?; let read = bytes_read as usize / mem::size_of::<syscall::Event>(); let mut count = 0; @@ -187,6 +162,6 @@ impl PalEpoll for Sys { } } - count as c_int + Ok(count) } } diff --git a/src/platform/redox/mod.rs b/src/platform/redox/mod.rs index 62cfab1ac94e36a6d9d5a3dbecbab982109e24b3..94fcfac5fb7b807049969cec1763b1276c01e7e7 100644 --- a/src/platform/redox/mod.rs +++ b/src/platform/redox/mod.rs @@ -74,6 +74,7 @@ macro_rules! path_from_c_str { use self::{exec::Executable, path::canonicalize}; +// TODO: Remove pub fn e(sys: syscall::error::Result<usize>) -> usize { match sys { Ok(ok) => ok, @@ -118,7 +119,7 @@ impl Pal for Sys { Ok(()) } - unsafe fn brk(addr: *mut c_void) -> *mut c_void { + unsafe fn brk(addr: *mut c_void) -> Result<*mut c_void> { // On first invocation, allocate a buffer for brk if BRK_CUR.is_null() { // 4 megabytes of RAM ought to be enough for anybody @@ -131,12 +132,7 @@ impl Pal for Sys { MAP_ANONYMOUS, 0, 0, - ); - if allocated == !0 as *mut c_void - /* MAP_FAILED */ - { - return !0 as *mut c_void; - } + )?; BRK_CUR = allocated; BRK_END = (allocated as *mut u8).add(BRK_MAX_SIZE) as *mut c_void; @@ -144,15 +140,14 @@ impl Pal for Sys { if addr.is_null() { // Lookup what previous brk() invocations have set the address to - BRK_CUR + Ok(BRK_CUR) } else if BRK_CUR <= addr && addr < BRK_END { // It's inside buffer, return BRK_CUR = addr; - addr + Ok(addr) } else { // It was outside of valid range - ERRNO.set(ENOMEM); - ptr::null_mut() + Err(Errno(ENOMEM)) } } @@ -214,22 +209,28 @@ impl Pal for Sys { loop {} } - unsafe fn execve(path: CStr, argv: *const *mut c_char, envp: *const *mut c_char) -> c_int { - e(self::exec::execve( + unsafe fn execve(path: CStr, argv: *const *mut c_char, envp: *const *mut c_char) -> Result<()> { + self::exec::execve( Executable::AtPath(path), self::exec::ArgEnv::C { argv, envp }, None, - )) as c_int + )?; + unreachable!() } - unsafe fn fexecve(fildes: c_int, argv: *const *mut c_char, envp: *const *mut c_char) -> c_int { - e(self::exec::execve( + unsafe fn fexecve( + fildes: c_int, + argv: *const *mut c_char, + envp: *const *mut c_char, + ) -> Result<()> { + self::exec::execve( Executable::InFd { file: File::new(fildes), arg0: CStr::from_ptr(argv.read()).to_bytes(), }, self::exec::ArgEnv::C { argv, envp }, None, - )) as c_int + )?; + unreachable!() } fn fchdir(fd: c_int) -> Result<()> { @@ -251,8 +252,8 @@ impl Pal for Sys { Ok(()) } - fn fcntl(fd: c_int, cmd: c_int, args: c_ulonglong) -> c_int { - e(syscall::fcntl(fd as usize, cmd as usize, args as usize)) as c_int + fn fcntl(fd: c_int, cmd: c_int, args: c_ulonglong) -> Result<c_int> { + Ok(syscall::fcntl(fd as usize, cmd as usize, args as usize)? as c_int) } fn fdatasync(fd: c_int) -> Result<()> { @@ -261,25 +262,26 @@ impl Pal for Sys { Ok(()) } - fn flock(_fd: c_int, _operation: c_int) -> c_int { + fn flock(_fd: c_int, _operation: c_int) -> Result<()> { // TODO: Redox does not have file locking yet - 0 + Ok(()) } - fn fork() -> pid_t { + unsafe fn fork() -> Result<pid_t> { + // TODO: Find way to avoid lock. let _guard = clone::wrlock(); - let res = clone::fork_impl(); - e(res) as pid_t + + Ok(clone::fork_impl()? as pid_t) } - // FIXME: unsound - fn fstat(fildes: c_int, buf: *mut stat) -> c_int { - unsafe { e(libredox::fstat(fildes as usize, buf).map(|()| 0)) as c_int } + unsafe fn fstat(fildes: c_int, buf: *mut stat) -> Result<()> { + libredox::fstat(fildes as usize, buf)?; + Ok(()) } - // FIXME: unsound - fn fstatvfs(fildes: c_int, buf: *mut statvfs) -> c_int { - unsafe { e(libredox::fstatvfs(fildes as usize, buf).map(|()| 0)) as c_int } + unsafe fn fstatvfs(fildes: c_int, buf: *mut statvfs) -> Result<()> { + libredox::fstatvfs(fildes as usize, buf)?; + Ok(()) } fn fsync(fd: c_int) -> Result<()> { @@ -306,35 +308,26 @@ impl Pal for Sys { Ok(redox_rt::sys::sys_futex_wake(addr, num)?) } - // FIXME: unsound - fn futimens(fd: c_int, times: *const timespec) -> c_int { - unsafe { e(libredox::futimens(fd as usize, times).map(|()| 0)) as c_int } + unsafe fn futimens(fd: c_int, times: *const timespec) -> Result<()> { + libredox::futimens(fd as usize, times)?; + Ok(()) } - // FIXME: unsound - fn utimens(path: CStr, times: *const timespec) -> c_int { - match File::open(path, fcntl::O_PATH | fcntl::O_CLOEXEC) { - Ok(file) => Self::futimens(*file, times), - Err(_) => -1, - } + unsafe fn utimens(path: CStr, times: *const timespec) -> Result<()> { + let file = File::open(path, fcntl::O_PATH | fcntl::O_CLOEXEC)?; + Self::futimens(*file, times) } - // FIXME: unsound - fn getcwd(buf: *mut c_char, size: size_t) -> *mut c_char { + unsafe fn getcwd(buf: *mut c_char, size: size_t) -> Result<()> { // TODO: Not using MaybeUninit seems a little unsafe let buf_slice = unsafe { slice::from_raw_parts_mut(buf as *mut u8, size as usize) }; if buf_slice.is_empty() { - ERRNO.set(EINVAL); - return ptr::null_mut(); - } - - if path::getcwd(buf_slice).is_none() { - ERRNO.set(ERANGE); - return ptr::null_mut(); + return Err(Errno(EINVAL)); } - buf + path::getcwd(buf_slice).ok_or(Errno(ERANGE))?; + Ok(()) } fn getdents(fd: c_int, buf: &mut [u8], opaque: u64) -> Result<usize> { @@ -414,30 +407,29 @@ impl Pal for Sys { } fn getegid() -> gid_t { - e(syscall::getegid()) as gid_t + syscall::getegid().unwrap() as gid_t } fn geteuid() -> uid_t { - e(syscall::geteuid()) as uid_t + syscall::geteuid().unwrap() as uid_t } fn getgid() -> gid_t { - e(syscall::getgid()) as gid_t + syscall::getgid().unwrap() as gid_t } - unsafe fn getgroups(size: c_int, list: *mut gid_t) -> c_int { + unsafe fn getgroups(size: c_int, list: *mut gid_t) -> Result<c_int> { // TODO eprintln!("relibc getgroups({}, {:p}): not implemented", size, list); - ERRNO.set(ENOSYS); - -1 + Err(Errno(ENOSYS)) } fn getpagesize() -> usize { PAGE_SIZE } - fn getpgid(pid: pid_t) -> pid_t { - e(syscall::getpgid(pid as usize)) as pid_t + fn getpgid(pid: pid_t) -> Result<pid_t> { + Ok(syscall::getpgid(pid as usize)? as pid_t) } fn getpid() -> pid_t { @@ -445,19 +437,16 @@ impl Pal for Sys { } fn getppid() -> pid_t { - e(syscall::getppid()) as pid_t + syscall::getppid().unwrap() as pid_t } - fn getpriority(which: c_int, who: id_t) -> c_int { + fn getpriority(which: c_int, who: id_t) -> Result<c_int> { // TODO eprintln!("getpriority({}, {}): not implemented", which, who); - ERRNO.set(ENOSYS); - -1 + Err(Errno(ENOSYS)) } - fn getrandom(buf: &mut [u8], flags: c_uint) -> ssize_t { - //TODO: make this a system call? - + fn getrandom(buf: &mut [u8], flags: c_uint) -> Result<usize> { let path = if flags & sys_random::GRND_RANDOM != 0 { //TODO: /dev/random equivalent "/scheme/rand" @@ -470,16 +459,9 @@ impl Pal for Sys { open_flags |= syscall::O_NONBLOCK; } - let fd = e(syscall::open(path, open_flags)); - if fd == !0 { - return -1; - } - - let res = e(syscall::read(fd, buf)) as ssize_t; - - let _ = syscall::close(fd); - - res + //TODO: store fd internally + let fd = FdGuard::new(syscall::open(path, open_flags)?); + Ok(syscall::read(*fd, buf)?) } unsafe fn getrlimit(resource: c_int, rlim: *mut rlimit) -> Result<()> { @@ -504,13 +486,13 @@ impl Pal for Sys { Err(Errno(EPERM)) } - fn getrusage(who: c_int, r_usage: &mut rusage) -> c_int { + fn getrusage(who: c_int, r_usage: &mut rusage) -> Result<()> { //TODO eprintln!("relibc getrusage({}, {:p}): not implemented", who, r_usage); - 0 + Ok(()) } - fn getsid(pid: pid_t) -> pid_t { + fn getsid(pid: pid_t) -> Result<pid_t> { let mut buf = [0; mem::size_of::<usize>()]; let path = if pid == 0 { format!("/scheme/thisproc/current/session_id") @@ -518,29 +500,21 @@ impl Pal for Sys { format!("/scheme/proc/{}/session_id", pid) }; let path_c = CString::new(path).unwrap(); - match File::open(CStr::borrow(&path_c), fcntl::O_RDONLY | fcntl::O_CLOEXEC) { - Ok(mut file) => match file.read(&mut buf) { - Ok(_) => usize::from_ne_bytes(buf).try_into().unwrap(), - Err(_) => -1, - }, - Err(_) => -1, - } + let mut file = File::open(CStr::borrow(&path_c), fcntl::O_RDONLY | fcntl::O_CLOEXEC)?; + file.read(&mut buf) + .map_err(|err| Errno(err.raw_os_error().unwrap_or(EIO)))?; + Ok(usize::from_ne_bytes(buf).try_into().unwrap()) } fn gettid() -> pid_t { - //TODO + // TODO: TIDs do not exist on Redox, at least in a form where they can be sent to other + // processes and used in various syscalls from there. Should the fd be returned here? Self::getpid() } - fn gettimeofday(tp: *mut timeval, tzp: *mut timezone) -> c_int { + unsafe fn gettimeofday(tp: *mut timeval, tzp: *mut timezone) -> Result<()> { let mut redox_tp = redox_timespec::default(); - let err = e(syscall::clock_gettime( - syscall::CLOCK_REALTIME, - &mut redox_tp, - )) as c_int; - if err < 0 { - return err; - } + syscall::clock_gettime(syscall::CLOCK_REALTIME, &mut redox_tp)?; unsafe { (*tp).tv_sec = redox_tp.tv_sec as time_t; (*tp).tv_usec = (redox_tp.tv_nsec / 1000) as suseconds_t; @@ -550,11 +524,11 @@ impl Pal for Sys { (*tzp).tz_dsttime = 0; } } - 0 + Ok(()) } fn getuid() -> uid_t { - e(syscall::getuid()) as pid_t + syscall::getuid().unwrap() as pid_t } fn lchown(path: CStr, owner: uid_t, group: gid_t) -> Result<()> { @@ -612,14 +586,14 @@ impl Pal for Sys { Ok(()) } - unsafe fn mlock(addr: *const c_void, len: usize) -> c_int { + unsafe fn mlock(addr: *const c_void, len: usize) -> Result<()> { // Redox never swaps - 0 + Ok(()) } - fn mlockall(flags: c_int) -> c_int { + unsafe fn mlockall(flags: c_int) -> Result<()> { // Redox never swaps - 0 + Ok(()) } unsafe fn mmap( @@ -629,7 +603,7 @@ impl Pal for Sys { flags: c_int, fildes: c_int, off: off_t, - ) -> *mut c_void { + ) -> Result<*mut c_void> { let map = Map { offset: off as usize, size: round_up_to_page_size(len), @@ -639,11 +613,11 @@ impl Pal for Sys { address: addr as usize, }; - if flags & MAP_ANONYMOUS == MAP_ANONYMOUS { - e(syscall::fmap(!0, &map)) as *mut c_void + Ok(if flags & MAP_ANONYMOUS == MAP_ANONYMOUS { + syscall::fmap(!0, &map)? } else { - e(syscall::fmap(fildes as usize, &map)) as *mut c_void - } + syscall::fmap(fildes as usize, &map)? + } as *mut c_void) } unsafe fn mremap( @@ -652,60 +626,59 @@ impl Pal for Sys { new_len: usize, flags: c_int, args: *mut c_void, - ) -> *mut c_void { - MAP_FAILED + ) -> Result<*mut c_void> { + Err(Errno(ENOSYS)) } - unsafe fn mprotect(addr: *mut c_void, len: usize, prot: c_int) -> c_int { - e(syscall::mprotect( + unsafe fn mprotect(addr: *mut c_void, len: usize, prot: c_int) -> Result<()> { + syscall::mprotect( addr as usize, round_up_to_page_size(len), syscall::MapFlags::from_bits((prot as usize) << 16) .expect("mprotect: invalid bit pattern"), - )) as c_int + )?; + Ok(()) } - unsafe fn msync(addr: *mut c_void, len: usize, flags: c_int) -> c_int { + unsafe fn msync(addr: *mut c_void, len: usize, flags: c_int) -> Result<()> { eprintln!( "relibc msync({:p}, 0x{:x}, 0x{:x}): not implemented", addr, len, flags ); - e(Err(syscall::Error::new(syscall::ENOSYS))) as c_int + Err(Errno(ENOSYS)) /* TODO - e(syscall::msync( + syscall::msync( addr as usize, round_up_to_page_size(len), flags - )) as c_int + )?; */ } - unsafe fn munlock(addr: *const c_void, len: usize) -> c_int { + unsafe fn munlock(addr: *const c_void, len: usize) -> Result<()> { // Redox never swaps - 0 + Ok(()) } - fn munlockall() -> c_int { + unsafe fn munlockall() -> Result<()> { // Redox never swaps - 0 + Ok(()) } - unsafe fn munmap(addr: *mut c_void, len: usize) -> c_int { - if e(syscall::funmap(addr as usize, round_up_to_page_size(len))) == !0 { - return !0; - } - 0 + unsafe fn munmap(addr: *mut c_void, len: usize) -> Result<()> { + syscall::funmap(addr as usize, round_up_to_page_size(len))?; + Ok(()) } - unsafe fn madvise(addr: *mut c_void, len: usize, flags: c_int) -> c_int { + unsafe fn madvise(addr: *mut c_void, len: usize, flags: c_int) -> Result<()> { eprintln!( "relibc madvise({:p}, 0x{:x}, 0x{:x}): not implemented", addr, len, flags ); - e(Err(syscall::Error::new(syscall::ENOSYS))) as c_int + Err(Errno(ENOSYS)) } - fn nanosleep(rqtp: *const timespec, rmtp: *mut timespec) -> c_int { + unsafe fn nanosleep(rqtp: *const timespec, rmtp: *mut timespec) -> Result<()> { let redox_rqtp = unsafe { redox_timespec::from(&*rqtp) }; let mut redox_rmtp: redox_timespec; if rmtp.is_null() { @@ -713,18 +686,14 @@ impl Pal for Sys { } else { redox_rmtp = unsafe { redox_timespec::from(&*rmtp) }; } - match e(syscall::nanosleep(&redox_rqtp, &mut redox_rmtp)) as c_int { - -1 => -1, - _ => { - unsafe { - if !rmtp.is_null() { - (*rmtp).tv_sec = redox_rmtp.tv_sec as time_t; - (*rmtp).tv_nsec = redox_rmtp.tv_nsec as c_long; - } - } - 0 + syscall::nanosleep(&redox_rqtp, &mut redox_rmtp)?; + unsafe { + if !rmtp.is_null() { + (*rmtp).tv_sec = redox_rmtp.tv_sec as time_t; + (*rmtp).tv_nsec = redox_rmtp.tv_nsec as c_long; } } + Ok(()) } fn open(path: CStr, oflag: c_int, mode: mode_t) -> Result<c_int> { @@ -766,11 +735,11 @@ impl Pal for Sys { } } - fn read(fd: c_int, buf: &mut [u8]) -> Result<ssize_t> { + fn read(fd: c_int, buf: &mut [u8]) -> Result<usize> { let fd = usize::try_from(fd).map_err(|_| Errno(EBADF))?; - Ok(redox_rt::sys::posix_read(fd, buf)? as ssize_t) + Ok(redox_rt::sys::posix_read(fd, buf)?) } - fn pread(fd: c_int, buf: &mut [u8], offset: off_t) -> Result<ssize_t> { + fn pread(fd: c_int, buf: &mut [u8], offset: off_t) -> Result<usize> { unsafe { Ok(syscall::syscall5( syscall::SYS_READ2, @@ -779,11 +748,11 @@ impl Pal for Sys { buf.len(), offset as usize, !0, - )? as ssize_t) + )?) } } - fn fpath(fildes: c_int, out: &mut [u8]) -> Result<ssize_t> { + fn fpath(fildes: c_int, out: &mut [u8]) -> Result<usize> { // Since this is used by realpath, it converts from the old format to the new one for // compatibility reasons let mut buf = [0; limits::PATH_MAX]; @@ -807,12 +776,12 @@ impl Pal for Sys { ), }; match res { - Ok(()) => Ok(cursor.position() as ssize_t), + Ok(()) => Ok(cursor.position() as usize), Err(_err) => Err(Errno(ENAMETOOLONG)), } } - fn readlink(pathname: CStr, out: &mut [u8]) -> Result<ssize_t> { + fn readlink(pathname: CStr, out: &mut [u8]) -> Result<usize> { let file = File::open( pathname, fcntl::O_RDONLY | fcntl::O_SYMLINK | fcntl::O_CLOEXEC, @@ -989,7 +958,7 @@ impl Pal for Sys { Ok(()) } - fn waitpid(mut pid: pid_t, stat_loc: *mut c_int, options: c_int) -> pid_t { + unsafe fn waitpid(mut pid: pid_t, stat_loc: *mut c_int, options: c_int) -> Result<pid_t> { if pid == !0 { pid = 0; } @@ -1009,16 +978,16 @@ impl Pal for Sys { let mut _event = PtraceEvent::default(); let _ = (&mut &session.tracer).read(&mut _event); - res = Some(e(inner( + res = Some(inner( &mut status, options | sys_wait::WNOHANG | sys_wait::WUNTRACED, - ))); - if res == Some(0) { + )); + if res == Some(Ok(0)) { // WNOHANG, just pretend ptrace SIGSTOP:ped this status = (syscall::SIGSTOP << 8) | 0x7f; assert!(syscall::wifstopped(status)); assert_eq!(syscall::wstopsig(status), syscall::SIGSTOP); - res = Some(pid as usize); + res = Some(Ok(pid as usize)); } } } @@ -1028,7 +997,7 @@ impl Pal for Sys { // it if (and only if) a ptrace traceme was activated during // the wait. let res = res.unwrap_or_else(|| loop { - let res = e(inner(&mut status, options | sys_wait::WUNTRACED)); + let res = inner(&mut status, options | sys_wait::WUNTRACED); // TODO: Also handle special PIDs here if !syscall::wifstopped(status) @@ -1045,14 +1014,14 @@ impl Pal for Sys { *stat_loc = status as c_int; } } - res as pid_t + Ok(res? as pid_t) } - fn write(fd: c_int, buf: &[u8]) -> Result<ssize_t> { + fn write(fd: c_int, buf: &[u8]) -> Result<usize> { let fd = usize::try_from(fd).map_err(|_| Errno(EBADFD))?; - Ok(redox_rt::sys::posix_write(fd, buf)? as ssize_t) + Ok(redox_rt::sys::posix_write(fd, buf)?) } - fn pwrite(fd: c_int, buf: &[u8], offset: off_t) -> Result<ssize_t> { + fn pwrite(fd: c_int, buf: &[u8], offset: off_t) -> Result<usize> { unsafe { Ok(syscall::syscall5( syscall::SYS_WRITE2, @@ -1061,7 +1030,7 @@ impl Pal for Sys { buf.len(), offset as usize, !0, - )? as ssize_t) + )?) } } diff --git a/src/platform/redox/signal.rs b/src/platform/redox/signal.rs index 0ae511bbec63407e17a7f85a5f4bc7e657955cac..e8104b18678c70f8a868c88174194b9c7890f67c 100644 --- a/src/platform/redox/signal.rs +++ b/src/platform/redox/signal.rs @@ -1,15 +1,9 @@ -use core::mem::{self, offset_of}; -use redox_rt::signal::{ - PosixStackt, SigStack, Sigaction, SigactionFlags, SigactionKind, Sigaltstack, SignalHandler, -}; -use syscall::{self, Result}; - use super::{ super::{types::*, Pal, PalSignal}, e, Sys, }; use crate::{ - error::Errno, + error::{Errno, Result}, header::{ errno::{EINVAL, ENOSYS}, signal::{ @@ -21,6 +15,13 @@ use crate::{ }, platform::ERRNO, }; +use core::mem::{self, offset_of}; +use redox_rt::{ + proc::FdGuard, + signal::{ + PosixStackt, SigStack, Sigaction, SigactionFlags, SigactionKind, Sigaltstack, SignalHandler, + }, +}; const _: () = { #[track_caller] @@ -45,41 +46,30 @@ const _: () = { }; impl PalSignal for Sys { - unsafe fn getitimer(which: c_int, out: *mut itimerval) -> c_int { + unsafe fn getitimer(which: c_int, out: *mut itimerval) -> Result<()> { let path = match which { ITIMER_REAL => "/scheme/itimer/1", - _ => { - ERRNO.set(EINVAL); - return -1; - } + _ => return Err(Errno(EINVAL)), }; - let fd = e(syscall::open(path, syscall::O_RDONLY | syscall::O_CLOEXEC)); - if fd == !0 { - return -1; - } + let fd = FdGuard::new(syscall::open(path, syscall::O_RDONLY | syscall::O_CLOEXEC)?); let mut spec = syscall::ITimerSpec::default(); - let count = e(syscall::read(fd, &mut spec)); - - let _ = syscall::close(fd); - - if count == !0 { - return -1; - } + let count = syscall::read(*fd, &mut spec)?; (*out).it_interval.tv_sec = spec.it_interval.tv_sec as time_t; (*out).it_interval.tv_usec = spec.it_interval.tv_nsec / 1000; (*out).it_value.tv_sec = spec.it_value.tv_sec as time_t; (*out).it_value.tv_usec = spec.it_value.tv_nsec / 1000; - 0 + Ok(()) } - fn kill(pid: pid_t, sig: c_int) -> c_int { - e(redox_rt::sys::posix_kill(pid as usize, sig as usize).map(|()| 0)) as c_int + fn kill(pid: pid_t, sig: c_int) -> Result<()> { + redox_rt::sys::posix_kill(pid as usize, sig as usize)?; + Ok(()) } - fn sigqueue(pid: pid_t, sig: c_int, val: sigval) -> Result<(), Errno> { + fn sigqueue(pid: pid_t, sig: c_int, val: sigval) -> Result<()> { Ok(redox_rt::sys::posix_sigqueue( pid as usize, sig as usize, @@ -87,58 +77,44 @@ impl PalSignal for Sys { )?) } - fn killpg(pgrp: pid_t, sig: c_int) -> c_int { - e(redox_rt::sys::posix_killpg(pgrp as usize, sig as usize).map(|()| 0)) as c_int + fn killpg(pgrp: pid_t, sig: c_int) -> Result<()> { + redox_rt::sys::posix_killpg(pgrp as usize, sig as usize)?; + Ok(()) } - fn raise(sig: c_int) -> Result<(), Errno> { + fn raise(sig: c_int) -> Result<()> { // TODO: Bypass kernel? unsafe { Self::rlct_kill(Self::current_os_tid(), sig as _) } } - unsafe fn setitimer(which: c_int, new: *const itimerval, old: *mut itimerval) -> c_int { + unsafe fn setitimer(which: c_int, new: *const itimerval, old: *mut itimerval) -> Result<()> { let path = match which { ITIMER_REAL => "/scheme/itimer/1", - _ => { - ERRNO.set(EINVAL); - return -1; - } + _ => return Err(Errno(EINVAL)), }; - let fd = e(syscall::open(path, syscall::O_RDWR | syscall::O_CLOEXEC)); - if fd == !0 { - return -1; - } + let fd = FdGuard::new(syscall::open(path, syscall::O_RDWR | syscall::O_CLOEXEC)?); let mut spec = syscall::ITimerSpec::default(); - let mut count = e(syscall::read(fd, &mut spec)); + let _ = syscall::read(*fd, &mut spec)?; - if count != !0 { - unsafe { - if !old.is_null() { - (*old).it_interval.tv_sec = spec.it_interval.tv_sec as time_t; - (*old).it_interval.tv_usec = spec.it_interval.tv_nsec / 1000; - (*old).it_value.tv_sec = spec.it_value.tv_sec as time_t; - (*old).it_value.tv_usec = spec.it_value.tv_nsec / 1000; - } - - spec.it_interval.tv_sec = (*new).it_interval.tv_sec as i64; - spec.it_interval.tv_nsec = (*new).it_interval.tv_usec * 1000; - spec.it_value.tv_sec = (*new).it_value.tv_sec as i64; - spec.it_value.tv_nsec = (*new).it_value.tv_usec * 1000; + unsafe { + if !old.is_null() { + (*old).it_interval.tv_sec = spec.it_interval.tv_sec as time_t; + (*old).it_interval.tv_usec = spec.it_interval.tv_nsec / 1000; + (*old).it_value.tv_sec = spec.it_value.tv_sec as time_t; + (*old).it_value.tv_usec = spec.it_value.tv_nsec / 1000; } - count = e(syscall::write(fd, &spec)); - } - - let _ = syscall::close(fd); - - if count == !0 { - return -1; + spec.it_interval.tv_sec = (*new).it_interval.tv_sec as i64; + spec.it_interval.tv_nsec = (*new).it_interval.tv_usec * 1000; + spec.it_value.tv_sec = (*new).it_value.tv_sec as i64; + spec.it_value.tv_nsec = (*new).it_value.tv_usec * 1000; } - 0 + let _ = syscall::write(*fd, &spec)?; + Ok(()) } fn sigaction( diff --git a/src/platform/redox/socket.rs b/src/platform/redox/socket.rs index 6d91c53991406ca2ff2d3db3c7c3f055e2246407..4f5448005288af4777866d1a7d3a707038cdc6ea 100644 --- a/src/platform/redox/socket.rs +++ b/src/platform/redox/socket.rs @@ -306,7 +306,9 @@ impl PalSocket for Sys { return -1; } if address == ptr::null_mut() || address_len == ptr::null_mut() { - Self::read(socket, slice::from_raw_parts_mut(buf as *mut u8, len)).or_minus_one_errno() + Self::read(socket, slice::from_raw_parts_mut(buf as *mut u8, len)) + .map(|u| u as ssize_t) + .or_minus_one_errno() } else { let fd = e(syscall::dup(socket as usize, b"listen")); if fd == !0 { @@ -318,6 +320,7 @@ impl PalSocket for Sys { } let ret = Self::read(fd as c_int, slice::from_raw_parts_mut(buf as *mut u8, len)) + .map(|u| u as ssize_t) .or_minus_one_errno(); let _ = syscall::close(fd); ret @@ -351,10 +354,13 @@ impl PalSocket for Sys { return -1; } if dest_addr == ptr::null() || dest_len == 0 { - Self::write(socket, slice::from_raw_parts(buf as *const u8, len)).or_minus_one_errno() + Self::write(socket, slice::from_raw_parts(buf as *const u8, len)) + .map(|u| u as ssize_t) + .or_minus_one_errno() } else { let fd = bind_or_connect!(connect copy, socket, dest_addr, dest_len); let ret = Self::write(fd as c_int, slice::from_raw_parts(buf as *const u8, len)) + .map(|u| u as ssize_t) .or_minus_one_errno(); let _ = syscall::close(fd); ret @@ -389,7 +395,9 @@ impl PalSocket for Sys { tv_nsec: timeval.tv_usec * 1000, }; - let ret = Self::write(fd as c_int, ×pec).or_minus_one_errno(); + let ret = Self::write(fd as c_int, ×pec) + .map(|u| u as ssize_t) + .or_minus_one_errno(); let _ = syscall::close(fd); diff --git a/src/platform/rlb.rs b/src/platform/rlb.rs index ec9ed09892e1c19adb26d64694273d8177a21a6c..ade9555892b92a65f5fe498e0bb9d8a00fd06dfb 100644 --- a/src/platform/rlb.rs +++ b/src/platform/rlb.rs @@ -61,7 +61,9 @@ impl RawLineBuffer { self.buf.set_len(capacity); } - let read = Sys::read(self.fd, &mut self.buf[len..]).or_minus_one_errno(); + let read = Sys::read(self.fd, &mut self.buf[len..]) + .map(|u| u as isize) + .or_minus_one_errno(); let read_usize = read.max(0) as usize;