Switch almost all of Pal to Rusty error handling.

This commit is contained in:
4lDO2
2024-09-27 10:19:44 +02:00
parent 549bdc5c72
commit 313b32c815
20 changed files with 338 additions and 301 deletions
+1 -1
View File
@@ -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]
+1 -1
View File
@@ -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;
}
+1 -1
View File
@@ -1320,7 +1320,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;
}
+5 -2
View File
@@ -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()
}
+5 -2
View File
@@ -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;
+29 -10
View File
@@ -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")]
+5 -1
View File
@@ -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,5 @@ 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,
)
.or_minus_one_errno()
}
+3 -1
View File
@@ -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()
}
+9 -7
View File
@@ -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);
+3 -3
View File
@@ -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);
+4 -1
View File
@@ -2,6 +2,7 @@
use crate::{
c_str::CStr,
error::ResultExt,
header::time::timespec,
platform::{types::*, Pal, PalSignal, Sys},
};
@@ -52,7 +53,7 @@ pub unsafe extern "C" fn setitimer(
#[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 +78,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()
}
+2 -1
View File
@@ -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()
}
+2 -2
View File
@@ -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]
+14 -10
View File
@@ -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();
@@ -442,7 +444,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 +508,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 +528,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]
@@ -775,7 +777,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
}
@@ -902,7 +904,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]
+3
View File
@@ -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()
}