Run rustfmt

This commit is contained in:
4lDO2
2023-10-26 15:51:04 +02:00
parent 3c1107f21a
commit ae46e37ca7
9 changed files with 275 additions and 164 deletions
+4 -1
View File
@@ -93,7 +93,10 @@ pub fn execve(
// NOTE: We must omit O_CLOEXEC and close manually, otherwise it will be closed before we
// have even read it!
let (mut image_file, arg0) = match exec {
Executable::AtPath(path) => (File::open(path, O_RDONLY as c_int).map_err(|_| Error::new(ENOENT))?, path.to_bytes()),
Executable::AtPath(path) => (
File::open(path, O_RDONLY as c_int).map_err(|_| Error::new(ENOENT))?,
path.to_bytes(),
),
Executable::InFd { file, arg0 } => (file, arg0),
};
+61 -17
View File
@@ -1,9 +1,11 @@
use core::{slice, str};
use syscall::{Error, Result, EMFILE, WaitFlags};
use syscall::{Error, Result, WaitFlags, EMFILE};
use crate::header::signal::sigaction;
use crate::platform::types::{c_int, mode_t};
use crate::{
header::signal::sigaction,
platform::types::{c_int, mode_t},
};
pub type RawResult = usize;
@@ -13,15 +15,26 @@ pub fn open(path: &str, oflag: c_int, mode: mode_t) -> Result<usize> {
((oflag as usize) & 0xFFFF_0000) | ((mode as usize) & 0xFFFF),
)?;
c_int::try_from(usize_fd).map_err(|_| {
let _ = syscall::close(usize_fd);
Error::new(EMFILE)
}).map(|f| f as usize)
c_int::try_from(usize_fd)
.map_err(|_| {
let _ = syscall::close(usize_fd);
Error::new(EMFILE)
})
.map(|f| f as usize)
}
#[no_mangle]
pub unsafe extern "C" fn redox_open_v1(path_base: *const u8, path_len: usize, flags: u32, mode: u16) -> RawResult {
Error::mux(open(str::from_utf8_unchecked(slice::from_raw_parts(path_base, path_len)), flags as c_int, mode as mode_t))
pub unsafe extern "C" fn redox_open_v1(
path_base: *const u8,
path_len: usize,
flags: u32,
mode: u16,
) -> RawResult {
Error::mux(open(
str::from_utf8_unchecked(slice::from_raw_parts(path_base, path_len)),
flags as c_int,
mode as mode_t,
))
}
#[no_mangle]
@@ -29,15 +42,31 @@ pub unsafe extern "C" fn redox_dup_v1(fd: usize, buf: *const u8, len: usize) ->
Error::mux(syscall::dup(fd, core::slice::from_raw_parts(buf, len)))
}
#[no_mangle]
pub unsafe extern "C" fn redox_dup2_v1(old_fd: usize, new_fd: usize, buf: *const u8, len: usize) -> RawResult {
Error::mux(syscall::dup2(old_fd, new_fd, core::slice::from_raw_parts(buf, len)))
pub unsafe extern "C" fn redox_dup2_v1(
old_fd: usize,
new_fd: usize,
buf: *const u8,
len: usize,
) -> RawResult {
Error::mux(syscall::dup2(
old_fd,
new_fd,
core::slice::from_raw_parts(buf, len),
))
}
#[no_mangle]
pub unsafe extern "C" fn redox_read_v1(fd: usize, dst_base: *mut u8, dst_len: usize) -> RawResult {
Error::mux(syscall::read(fd, slice::from_raw_parts_mut(dst_base, dst_len)))
Error::mux(syscall::read(
fd,
slice::from_raw_parts_mut(dst_base, dst_len),
))
}
#[no_mangle]
pub unsafe extern "C" fn redox_write_v1(fd: usize, src_base: *const u8, src_len: usize) -> RawResult {
pub unsafe extern "C" fn redox_write_v1(
fd: usize,
src_base: *const u8,
src_len: usize,
) -> RawResult {
Error::mux(syscall::write(fd, slice::from_raw_parts(src_base, src_len)))
}
#[no_mangle]
@@ -59,7 +88,10 @@ pub unsafe extern "C" fn redox_fchown_v1(fd: usize, new_uid: u32, new_gid: u32)
}
#[no_mangle]
pub unsafe extern "C" fn redox_fpath_v1(fd: usize, dst_base: *mut u8, dst_len: usize) -> RawResult {
Error::mux(syscall::fpath(fd, core::slice::from_raw_parts_mut(dst_base, dst_len)))
Error::mux(syscall::fpath(
fd,
core::slice::from_raw_parts_mut(dst_base, dst_len),
))
}
#[no_mangle]
pub unsafe extern "C" fn redox_close_v1(fd: usize) -> RawResult {
@@ -94,7 +126,11 @@ pub unsafe extern "C" fn redox_setrens_v1(rns: usize, ens: usize) -> RawResult {
#[no_mangle]
pub unsafe extern "C" fn redox_waitpid_v1(pid: usize, status: *mut i32, options: u32) -> RawResult {
let mut sts = 0_usize;
let res = Error::mux(syscall::waitpid(pid, &mut sts, WaitFlags::from_bits_truncate(options as usize)));
let res = Error::mux(syscall::waitpid(
pid,
&mut sts,
WaitFlags::from_bits_truncate(options as usize),
));
status.write(sts as i32);
res
}
@@ -105,11 +141,19 @@ pub unsafe extern "C" fn redox_kill_v1(pid: usize, signal: u32) -> RawResult {
}
#[no_mangle]
pub unsafe extern "C" fn redox_sigaction_v1(signal: u32, new: *const sigaction, old: *mut sigaction) -> RawResult {
pub unsafe extern "C" fn redox_sigaction_v1(
signal: u32,
new: *const sigaction,
old: *mut sigaction,
) -> RawResult {
Error::mux(super::signal::sigaction_impl(signal as i32, new.as_ref(), old.as_mut()).map(|()| 0))
}
#[no_mangle]
pub unsafe extern "C" fn redox_sigprocmask_v1(how: u32, new: *const u64, old: *mut u64) -> RawResult {
pub unsafe extern "C" fn redox_sigprocmask_v1(
how: u32,
new: *const u64,
old: *mut u64,
) -> RawResult {
Error::mux(super::signal::sigprocmask_impl(how as i32, new, old).map(|()| 0))
}
+29 -8
View File
@@ -63,7 +63,7 @@ macro_rules! path_from_c_str {
}};
}
use self::{path::canonicalize, exec::Executable};
use self::{exec::Executable, path::canonicalize};
pub fn e(sys: Result<usize>) -> usize {
match sys {
@@ -207,7 +207,10 @@ impl Pal for Sys {
fn clock_settime(clk_id: clockid_t, tp: *const timespec) -> c_int {
// TODO
eprintln!("relibc clock_settime({}, {:p}): not implemented", clk_id, tp);
eprintln!(
"relibc clock_settime({}, {:p}): not implemented",
clk_id, tp
);
unsafe { errno = ENOSYS };
-1
}
@@ -238,7 +241,10 @@ impl Pal for Sys {
}
unsafe fn fexecve(fildes: c_int, argv: *const *mut c_char, envp: *const *mut c_char) -> c_int {
e(self::exec::execve(
Executable::InFd { file: File::new(fildes), arg0: CStr::from_ptr(argv.read()).to_bytes() },
Executable::InFd {
file: File::new(fildes),
arg0: CStr::from_ptr(argv.read()).to_bytes(),
},
self::exec::ArgEnv::C { argv, envp },
None,
)) as c_int
@@ -573,7 +579,10 @@ impl Pal for Sys {
unsafe fn getrlimit(resource: c_int, rlim: *mut rlimit) -> c_int {
//TODO
eprintln!("relibc getrlimit({}, {:p}): not implemented", resource, rlim);
eprintln!(
"relibc getrlimit({}, {:p}): not implemented",
resource, rlim
);
if !rlim.is_null() {
(*rlim).rlim_cur = RLIM_INFINITY;
(*rlim).rlim_max = RLIM_INFINITY;
@@ -583,7 +592,10 @@ impl Pal for Sys {
unsafe fn setrlimit(resource: c_int, rlim: *const rlimit) -> c_int {
//TOOD
eprintln!("relibc setrlimit({}, {:p}): not implemented", resource, rlim);
eprintln!(
"relibc setrlimit({}, {:p}): not implemented",
resource, rlim
);
unsafe { errno = EPERM };
-1
}
@@ -715,7 +727,10 @@ impl Pal for Sys {
}
unsafe fn msync(addr: *mut c_void, len: usize, flags: c_int) -> c_int {
eprintln!("relibc msync({:p}, 0x{:x}, 0x{:x}): not implemented", addr, len, flags);
eprintln!(
"relibc msync({:p}, 0x{:x}, 0x{:x}): not implemented",
addr, len, flags
);
e(Err(syscall::Error::new(syscall::ENOSYS))) as c_int
/* TODO
e(syscall::msync(
@@ -744,7 +759,10 @@ impl Pal for Sys {
}
unsafe fn madvise(addr: *mut c_void, len: usize, flags: c_int) -> c_int {
eprintln!("relibc madvise({:p}, 0x{:x}, 0x{:x}): not implemented", addr, len, flags);
eprintln!(
"relibc madvise({:p}, 0x{:x}, 0x{:x}): not implemented",
addr, len, flags
);
e(Err(syscall::Error::new(syscall::ENOSYS))) as c_int
}
@@ -850,7 +868,10 @@ impl Pal for Sys {
fn setpriority(which: c_int, who: id_t, prio: c_int) -> c_int {
// TODO
eprintln!("relibc setpriority({}, {}, {}): not implemented", which, who, prio);
eprintln!(
"relibc setpriority({}, {}, {}): not implemented",
which, who, prio
);
unsafe { errno = ENOSYS };
-1
}
+54 -46
View File
@@ -23,8 +23,8 @@ use goblin::elf64::{
use syscall::{
error::*,
flag::{MapFlags, SEEK_SET},
PAGE_SIZE, Map, PROT_WRITE, O_CLOEXEC,
GrantDesc, GrantFlags, PROT_READ, PROT_EXEC, MAP_SHARED, MAP_FIXED_NOREPLACE,
GrantDesc, GrantFlags, Map, MAP_FIXED_NOREPLACE, MAP_SHARED, O_CLOEXEC, PAGE_SIZE, PROT_EXEC,
PROT_READ, PROT_WRITE,
};
pub use self::arch::*;
@@ -174,13 +174,8 @@ where
let (first_aligned_page, remaining_filesz) = if voff > 0 {
let bytes_to_next_page = PAGE_SIZE - voff;
let (_guard, dst_page) = unsafe {
MmapGuard::map_mut_anywhere(
*grants_fd,
vaddr,
PAGE_SIZE,
)?
};
let (_guard, dst_page) =
unsafe { MmapGuard::map_mut_anywhere(*grants_fd, vaddr, PAGE_SIZE)? };
let length = core::cmp::min(bytes_to_next_page, filesz);
@@ -209,16 +204,19 @@ where
// Use commented out lines to trigger kernel bug (FIXME).
//let pages_in_this_group = core::cmp::min(PAGES_PER_ITER, file_page_count - page_idx * PAGES_PER_ITER);
let pages_in_this_group = core::cmp::min(PAGES_PER_ITER, remaining_page_count - page_idx);
let pages_in_this_group =
core::cmp::min(PAGES_PER_ITER, remaining_page_count - page_idx);
if pages_in_this_group == 0 { break }
if pages_in_this_group == 0 {
break;
}
// TODO: MAP_FIXED to optimize away funmap?
let (_guard, dst_memory) = unsafe {
MmapGuard::map_mut_anywhere(
*grants_fd,
first_aligned_page + page_idx * PAGE_SIZE, // offset
pages_in_this_group * PAGE_SIZE, // size
pages_in_this_group * PAGE_SIZE, // size
)?
};
@@ -290,7 +288,11 @@ where
};
unsafe {
page.as_mut_ptr_slice().as_mut_ptr().add(new_page_off).cast::<usize>().write(word);
page.as_mut_ptr_slice()
.as_mut_ptr()
.add(new_page_off)
.cast::<usize>()
.write(word);
}
Ok(())
@@ -312,11 +314,8 @@ where
MapFlags::PROT_READ | MapFlags::PROT_WRITE,
)?;
unsafe {
let (_guard, memory) = MmapGuard::map_mut_anywhere(
*grants_fd,
pheaders,
pheaders_size_aligned,
)?;
let (_guard, memory) =
MmapGuard::map_mut_anywhere(*grants_fd, pheaders, pheaders_size_aligned)?;
memory[..pheaders_to_convey.len()].copy_from_slice(pheaders_to_convey);
}
@@ -380,11 +379,7 @@ where
let aligned_size = size.next_multiple_of(PAGE_SIZE);
let (_guard, memory) = unsafe {
MmapGuard::map_mut_anywhere(
*grants_fd,
containing_page,
aligned_size,
)?
MmapGuard::map_mut_anywhere(*grants_fd, containing_page, aligned_size)?
};
memory[displacement..][..source_slice.len()].copy_from_slice(source_slice);
}
@@ -606,22 +601,34 @@ impl MmapGuard {
flags.remove(MapFlags::MAP_FIXED_NOREPLACE);
flags.insert(MapFlags::MAP_FIXED);
let _new_base = unsafe { syscall::fmap(self.fd, &Map {
offset,
size: self.size,
flags,
address: self.base,
})? };
let _new_base = unsafe {
syscall::fmap(
self.fd,
&Map {
offset,
size: self.size,
flags,
address: self.base,
},
)?
};
Ok(())
}
pub unsafe fn map_mut_anywhere<'a>(fd: usize, offset: usize, size: usize) -> Result<(Self, &'a mut [u8])> {
let mut this = Self::map(fd, &Map {
size,
offset,
address: 0,
flags: PROT_WRITE,
})?;
pub unsafe fn map_mut_anywhere<'a>(
fd: usize,
offset: usize,
size: usize,
) -> Result<(Self, &'a mut [u8])> {
let mut this = Self::map(
fd,
&Map {
size,
offset,
address: 0,
flags: PROT_WRITE,
},
)?;
let slice = &mut *this.as_mut_ptr_slice();
Ok((this, slice))
@@ -704,10 +711,7 @@ fn fork_inner(initial_rsp: *mut usize) -> Result<usize> {
let (cur_filetable_fd, new_pid_fd, new_pid);
{
let cur_pid_fd = FdGuard::new(syscall::open(
"thisproc:current/open_via_dup",
O_CLOEXEC,
)?);
let cur_pid_fd = FdGuard::new(syscall::open("thisproc:current/open_via_dup", O_CLOEXEC)?);
(new_pid_fd, new_pid) = new_context()?;
// Do not allocate new signal stack, but copy existing address (all memory will be re-mapped
@@ -759,7 +763,12 @@ fn fork_inner(initial_rsp: *mut usize) -> Result<usize> {
let mut grant_desc_buf = [GrantDesc::default(); 16];
loop {
let bytes_read = {
let buf = unsafe { core::slice::from_raw_parts_mut(grant_desc_buf.as_mut_ptr().cast(), grant_desc_buf.len() * size_of::<GrantDesc>()) };
let buf = unsafe {
core::slice::from_raw_parts_mut(
grant_desc_buf.as_mut_ptr().cast(),
grant_desc_buf.len() * size_of::<GrantDesc>(),
)
};
syscall::read(*cur_addr_space_fd, buf)?
};
if bytes_read == 0 {
@@ -769,7 +778,9 @@ fn fork_inner(initial_rsp: *mut usize) -> Result<usize> {
let grants = &grant_desc_buf[..bytes_read / size_of::<GrantDesc>()];
for grant in grants {
if !grant.flags.contains(GrantFlags::GRANT_SCHEME) || !grant.flags.contains(GrantFlags::GRANT_SHARED) {
if !grant.flags.contains(GrantFlags::GRANT_SCHEME)
|| !grant.flags.contains(GrantFlags::GRANT_SHARED)
{
continue;
}
@@ -846,10 +857,7 @@ fn fork_inner(initial_rsp: *mut usize) -> Result<usize> {
pub fn new_context() -> Result<(FdGuard, usize)> {
// Create a new context (fields such as uid/gid will be inherited from the current context).
let fd = FdGuard::new(syscall::open(
"thisproc:new/open_via_dup",
O_CLOEXEC,
)?);
let fd = FdGuard::new(syscall::open("thisproc:new/open_via_dup", O_CLOEXEC)?);
// Extract pid.
let mut buffer = [0_u8; 64];
+7 -11
View File
@@ -140,7 +140,11 @@ impl PalSignal for Sys {
}
}
pub(crate) fn sigaction_impl(sig: i32, act: Option<&sigaction>, oact: Option<&mut sigaction>) -> Result<()> {
pub(crate) fn sigaction_impl(
sig: i32,
act: Option<&sigaction>,
oact: Option<&mut sigaction>,
) -> Result<()> {
let new_opt = act.map(|act| {
let m = act.sa_mask;
let sa_handler = unsafe { mem::transmute(act.sa_handler) };
@@ -152,11 +156,7 @@ pub(crate) fn sigaction_impl(sig: i32, act: Option<&sigaction>, oact: Option<&mu
}
});
let mut old_opt = oact.as_ref().map(|_| syscall::SigAction::default());
syscall::sigaction(
sig as usize,
new_opt.as_ref(),
old_opt.as_mut(),
)?;
syscall::sigaction(sig as usize, new_opt.as_ref(), old_opt.as_mut())?;
if let (Some(old), Some(oact)) = (old_opt, oact) {
oact.sa_handler = unsafe { mem::transmute(old.sa_handler) };
let m = old.sa_mask;
@@ -172,11 +172,7 @@ pub(crate) fn sigprocmask_impl(how: i32, set: *const sigset_t, oset: *mut sigset
Some([unsafe { *set as u64 }, 0])
};
let mut old_opt = if oset.is_null() { None } else { Some([0, 0]) };
syscall::sigprocmask(
how as usize,
new_opt.as_ref(),
old_opt.as_mut(),
)?;
syscall::sigprocmask(how as usize, new_opt.as_ref(), old_opt.as_mut())?;
if let Some(old) = old_opt {
unsafe { *oset = old[0] as sigset_t };
}