pub mod call {
use core::mem::MaybeUninit;
use super::*;
/// flags and mode are binary compatible with libc
#[inline]
pub fn open(path: impl AsRef<str>, flags: i32, mode: u16) -> Result<usize> {
let path = path.as_ref();
Ok(Error::demux(unsafe {
redox_open_v1(path.as_ptr(), path.len(), flags as u32, mode)
})?)
}
#[inline]
pub fn openat(
fd: usize,
path: impl AsRef<[u8]>,
flags: i32,
fcntl_flags: u32,
) -> Result<usize> {
let path = path.as_ref();
Ok(Error::demux(unsafe {
redox_openat_v1(fd, path.as_ptr(), path.len(), flags as u32, fcntl_flags)
})?)
}
#[inline]
pub fn dup(fd: usize, buf: impl AsRef<[u8]>) -> Result<usize> {
let buf = buf.as_ref();
Ok(Error::demux(unsafe {
redox_dup_v1(fd, buf.as_ptr(), buf.len())
})?)
}
#[inline]
pub fn dup2(old_fd: usize, new_fd: usize, buf: impl AsRef<[u8]>) -> Result<usize> {
let buf = buf.as_ref();
Ok(Error::demux(unsafe {
redox_dup2_v1(old_fd, new_fd, buf.as_ptr(), buf.len())
})?)
}
#[inline]
pub fn read(raw_fd: usize, buf: &mut [u8]) -> Result<usize> {
Ok(Error::demux(unsafe {
redox_read_v1(raw_fd, buf.as_mut_ptr(), buf.len())
})?)
}
#[inline]
pub fn write(raw_fd: usize, buf: &[u8]) -> Result<usize> {
Error::demux(unsafe { redox_write_v1(raw_fd, buf.as_ptr(), buf.len()) })
}
#[inline]
pub fn fchmod(raw_fd: usize, new_mode: u16) -> Result<()> {
Error::demux(unsafe { redox_fchmod_v1(raw_fd, new_mode) })?;
Ok(())
}
#[inline]
pub fn fchown(raw_fd: usize, new_uid: u32, new_gid: u32) -> Result<()> {
Error::demux(unsafe { redox_fchown_v1(raw_fd, new_uid, new_gid) })?;
Ok(())
}
#[inline]
pub fn getdents(fd: usize, buf: &mut [u8], opaque: u64) -> Result<usize> {
Error::demux(unsafe { redox_getdents_v0(fd, buf.as_mut_ptr(), buf.len(), opaque) })
}
#[inline]
pub fn fstat(raw_fd: usize) -> Result<data::Stat> {
unsafe {
let mut ret = MaybeUninit::uninit();
Error::demux(redox_fstat_v1(raw_fd, ret.as_mut_ptr()))?;
Ok(ret.assume_init())
}
}
#[inline]
pub fn fstatvfs(raw_fd: usize) -> Result<data::StatVfs> {
unsafe {
let mut ret = MaybeUninit::uninit();
Error::demux(redox_fstatvfs_v1(raw_fd, ret.as_mut_ptr()))?;
Ok(ret.assume_init())
}
}
#[inline]
pub fn fsync(raw_fd: usize) -> Result<()> {
Error::demux(unsafe { redox_fsync_v1(raw_fd) }).map(|_| ())
}
#[inline]
pub fn fdatasync(raw_fd: usize) -> Result<()> {
Error::demux(unsafe { redox_fdatasync_v1(raw_fd) }).map(|_| ())
}
#[inline]
pub fn ftruncate(raw_fd: usize, new_size: usize) -> Result<()> {
Error::demux(unsafe { redox_ftruncate_v0(raw_fd, new_size) }).map(|_| ())
}
#[inline]
pub fn futimens(raw_fd: usize, times: &[data::TimeSpec; 2]) -> Result<()> {
Error::demux(unsafe { redox_futimens_v1(raw_fd, times.as_ptr()) })?;
Ok(())
}
/* TODO: Support unlinkat using std_fs_call
#[inline]
pub fn unlinkat(fd: usize, path: impl AsRef<[u8]>, flags: i32) -> Result<()> {
let path = path.as_ref();
Error::demux(unsafe { redox_unlinkat_v0(fd, path.as_ptr(), path.len(), flags as u32) })
.map(|_| ())
}
*/
#[inline]
pub fn fpath(raw_fd: usize, buf: &mut [u8]) -> Result<usize> {
Error::demux(unsafe { redox_fpath_v1(raw_fd, buf.as_mut_ptr(), buf.len()) })
}
#[inline]
pub fn relpathat(raw_fd: usize, fd: usize, buf: &mut [u8]) -> Result<usize> {
Error::demux(unsafe { redox_relpathat_v0(raw_fd, fd, buf.as_mut_ptr(), buf.len()) })
}
#[inline]
pub fn close(raw_fd: usize) -> Result<()> {
Error::demux(unsafe { redox_close_v1(raw_fd) })?;
Ok(())
}
#[cfg(feature = "redox_syscall")]
#[inline]
pub fn call_ro(
fd: usize,
payload: &mut [u8],
flags: syscall::CallFlags,
metadata: &[u64],
) -> Result<usize> {
Ok(Error::demux(unsafe {
redox_sys_call_v0(
fd,
payload.as_mut_ptr(),
payload.len(),
(flags | syscall::CallFlags::READ).bits(),
metadata.as_ptr(),
metadata.len(),
)
})?)
}
#[cfg(feature = "redox_syscall")]
#[inline]
pub fn call_wo(
fd: usize,
payload: &[u8],
flags: syscall::CallFlags,
metadata: &[u64],
) -> Result<usize> {
Ok(Error::demux(unsafe {
redox_sys_call_v0(
fd,
payload.as_ptr() as *mut u8,
payload.len(),
(flags | syscall::CallFlags::WRITE).bits(),
metadata.as_ptr(),
metadata.len(),
)
})?)
}
#[cfg(feature = "redox_syscall")]
#[inline]
pub fn call_rw(
fd: usize,
payload: &mut [u8],
flags: syscall::CallFlags,
metadata: &[u64],
) -> Result<usize> {
Ok(Error::demux(unsafe {
redox_sys_call_v0(
fd,
payload.as_mut_ptr(),
payload.len(),
(flags | syscall::CallFlags::READ | syscall::CallFlags::WRITE).bits(),
metadata.as_ptr(),
metadata.len(),
)
})?)
}
#[inline]
pub fn geteuid() -> Result<usize> {
Error::demux(unsafe { redox_get_euid_v1() })
}
#[inline]
pub fn getruid() -> Result<usize> {
Error::demux(unsafe { redox_get_ruid_v1() })
}
#[inline]
pub fn getegid() -> Result<usize> {
Error::demux(unsafe { redox_get_egid_v1() })
}
#[inline]
pub fn getrgid() -> Result<usize> {
Error::demux(unsafe { redox_get_rgid_v1() })
}
#[inline]
pub fn getpid() -> Result<usize> {
Error::demux(unsafe { redox_get_pid_v1() })
}
#[inline]
pub fn getens() -> Result<usize> {
Error::demux(unsafe { redox_get_ens_v0() })
}
#[inline]
// [u8; size_of::<crate::protocol::ProcMeta>()]
pub fn get_proc_credentials(cap_fd: usize, target_pid: usize, buf: &mut [u8]) -> Result<usize> {
Error::demux(unsafe { redox_get_proc_credentials_v1(cap_fd, target_pid, buf) })
}
#[inline]
pub fn setrens(rns: usize, ens: usize) -> Result<usize> {
Error::demux(unsafe { redox_setrens_v1(rns, ens) })
}
#[inline]
pub fn waitpid(pid: usize, status: &mut i32, options: i32) -> Result<usize> {
Error::demux(unsafe { redox_waitpid_v1(pid, status as *mut i32, options as u32) })
}
#[inline]
pub fn kill(pid: usize, signal: u32) -> Result<()> {
Error::demux(unsafe { redox_kill_v1(pid, signal) }).map(|_| ())
}
#[inline]
pub fn clock_gettime(clock: i32) -> Result<data::TimeSpec> {
unsafe {
let mut ret = MaybeUninit::uninit();
Error::demux(redox_clock_gettime_v1(clock as usize, ret.as_mut_ptr()))?;
Ok(ret.assume_init())
}
}
#[inline]
pub fn sigprocmask(
how: i32,
newmask: Option<&data::SigSet>,
oldmask: Option<&mut data::SigSet>,
) -> Result<()> {
Error::demux(unsafe {
redox_sigprocmask_v1(
how as u32,
newmask.map_or(core::ptr::null(), |m| m),
oldmask.map_or(core::ptr::null_mut(), |m| m),
)
})
.map(|_| ())
}
#[inline]
pub fn sigaction(
signal: i32,
newact: Option<&data::SigAction>,
oldact: Option<&mut data::SigAction>,
) -> Result<()> {
Error::demux(unsafe {
redox_sigaction_v1(
signal as u32,
newact.map_or(core::ptr::null(), |m| m),
oldact.map_or(core::ptr::null_mut(), |m| m),
)
})
.map(|_| ())
}
#[derive(Clone, Copy, Debug)]
pub struct MmapArgs {
pub addr: *mut (),
pub length: usize,
pub prot: u32,
pub flags: u32,
pub fd: usize,
pub offset: u64,
}
#[inline]
pub unsafe fn mmap(args: MmapArgs) -> Result<*mut ()> {
Error::demux(redox_mmap_v1(
args.addr,
args.length,
args.prot,
args.flags,
args.fd,
args.offset,
))
.map(|addr| addr as *mut ())
}
#[inline]
pub unsafe fn munmap(addr: *mut (), length: usize) -> Result<()> {
Error::demux(redox_munmap_v1(addr, length)).map(|_| ())
}
#[inline]
pub fn strerror(error: u16, desc: &mut [u8]) -> Option<(&str, usize)> {
unsafe {
let mut len_inout = desc.len();
let copied_len = Error::demux(redox_strerror_v1(
desc.as_mut_ptr(),
&mut len_inout,
error.into(),
))
.ok()?;
Some((
core::str::from_utf8_unchecked(&desc[..copied_len]),
len_inout,
))
}
}
#[inline]
#[cfg(feature = "mkns")]
pub fn mkns(names: &[ioslice::IoSlice]) -> Result<usize> {
// no-op
let iovecs = ioslice::IoSlice::cast_to_raw_iovecs(names);
unsafe { Error::demux(redox_mkns_v1(iovecs.as_ptr(), iovecs.len(), 0)) }
}
#[inline]
pub fn get_socket_token(fd: usize, buf: &mut [u8]) -> Result<usize> {
Error::demux(unsafe { redox_get_socket_token_v0(fd, buf.as_mut_ptr(), buf.len()) })
}
#[inline]
pub fn setns(fd: usize) -> Result<usize> {
Error::demux(unsafe { redox_setns_v0(fd) })
}
#[inline]
pub fn getns() -> Result<usize> {
Error::demux(unsafe { redox_get_ns_v0() })
}
#[inline]
pub fn register_scheme_to_ns(ns_fd: usize, name: impl AsRef<str>, cap_fd: usize) -> Result<()> {
let name = name.as_ref();
Error::demux(unsafe {
redox_register_scheme_to_ns_v0(ns_fd, name.as_ptr(), name.len(), cap_fd)
})
.map(|_| ())
}
}