diff --git a/redox-rt/Cargo.toml b/redox-rt/Cargo.toml index a69851747e..9210ba259a 100644 --- a/redox-rt/Cargo.toml +++ b/redox-rt/Cargo.toml @@ -15,3 +15,7 @@ plain = "0.2" redox_syscall = "0.5.8" generic-rt = { path = "../generic-rt" } + +[features] +proc = [] +default = ["proc"] diff --git a/redox-rt/src/lib.rs b/redox-rt/src/lib.rs index 1d0906a057..7931d978c5 100644 --- a/redox-rt/src/lib.rs +++ b/redox-rt/src/lib.rs @@ -63,7 +63,7 @@ impl RtTcb { unsafe { if (&*self.thr_fd.get()).is_none() { self.thr_fd.get().write(Some(FdGuard::new( - syscall::open("/scheme/thisproc/current/open_via_dup", O_CLOEXEC).unwrap(), + syscall::open("/scheme/thisproc/current", O_CLOEXEC).unwrap(), ))); } (&*self.thr_fd.get()).as_ref().unwrap() @@ -178,17 +178,31 @@ pub unsafe fn initialize_freestanding() { initialize(); } pub unsafe fn initialize() { + #[cfg(feature = "proc")] + // Find the PID attached to this process + let pid = todo!("getpid"); + + #[cfg(not(feature = "proc"))] + // Bootstrap mode, don't associate proc fds with PIDs + let pid = 0; + THIS_PID .get() - .write(Some(syscall::getpid().unwrap().try_into().unwrap()).unwrap()); + .write(Some(pid).unwrap()); } static THIS_PID: SyncUnsafeCell = SyncUnsafeCell::new(0); unsafe fn child_hook_common(new_pid_fd: FdGuard) { + // TODO: just pass PID to child rather than obtaining it via IPC? + #[cfg(feature = "proc")] + let pid = todo!("getpid"); + #[cfg(not(feature = "proc"))] + let pid = 0; + // TODO: Currently pidfd == threadfd, but this will not be the case later. RtTcb::current().thr_fd.get().write(Some(new_pid_fd)); THIS_PID .get() - .write(Some(syscall::getpid().unwrap().try_into().unwrap()).unwrap()); + .write(Some(pid).unwrap()); } diff --git a/redox-rt/src/proc.rs b/redox-rt/src/proc.rs index a6a4d67e60..abb2fe0a4d 100644 --- a/redox-rt/src/proc.rs +++ b/redox-rt/src/proc.rs @@ -74,8 +74,7 @@ where { // Here, we do the minimum part of loading an application, which is what the kernel used to do. // We load the executable into memory (albeit at different offsets in this executable), fix - // some misalignments, and then execute the SYS_EXEC syscall to replace the program memory - // entirely. + // some misalignments, and then switch address space. let mut header_bytes = [0_u8; size_of::
()]; pread_all(*image_file, 0, &mut header_bytes)?; @@ -695,10 +694,10 @@ pub fn fork_inner(initial_rsp: *mut usize) -> Result { { let cur_pid_fd = FdGuard::new(syscall::open( - "/scheme/thisproc/current/open_via_dup", + "/scheme/thisproc/current", O_CLOEXEC, )?); - (new_pid_fd, new_pid) = new_child_process()?; + (new_pid_fd, new_pid) = new_child_process(*cur_pid_fd)?; copy_str(*cur_pid_fd, *new_pid_fd, "name")?; @@ -823,10 +822,10 @@ pub fn fork_inner(initial_rsp: *mut usize) -> Result { Ok(new_pid) } -pub fn new_child_process() -> Result<(FdGuard, usize)> { +pub fn new_child_process(_cur_pid_fd: usize) -> 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( - "/scheme/thisproc/new/open_via_dup", + "/scheme/thisproc/new", O_CLOEXEC, )?); diff --git a/redox-rt/src/signal.rs b/redox-rt/src/signal.rs index f303383957..f252ba5418 100644 --- a/redox-rt/src/signal.rs +++ b/redox-rt/src/signal.rs @@ -152,7 +152,8 @@ unsafe fn inner(stack: &mut SigStack) { panic!("ctl {:x?} signal {}", os.control, stack.sig_num) } SigactionKind::Default => { - syscall::exit(stack.sig_num as usize); + //syscall::exit(stack.sig_num as usize); + todo!("exit"); unreachable!(); } SigactionKind::Handled { handler } => handler, @@ -497,7 +498,8 @@ bitflags::bitflags! { const STORED_FLAGS: u32 = 0xfe00_0000; fn default_handler(sig: c_int) { - syscall::exit(sig as usize); + //syscall::exit(sig as usize); + todo!("exit") } #[derive(Clone, Copy)] diff --git a/redox-rt/src/sys.rs b/redox-rt/src/sys.rs index 2f9bd82c30..1fffae6e20 100644 --- a/redox-rt/src/sys.rs +++ b/redox-rt/src/sys.rs @@ -43,7 +43,7 @@ pub fn posix_write(fd: usize, buf: &[u8]) -> Result { } #[inline] pub fn posix_kill(pid: usize, sig: usize) -> Result<()> { - match wrapper(false, || syscall::kill(pid, sig)) { + match wrapper(false, || Ok(todo!("kill"))) { Ok(_) | Err(Error { errno: EINTR }) => Ok(()), Err(error) => Err(error), } @@ -57,7 +57,8 @@ pub fn posix_sigqueue(pid: usize, sig: usize, arg: usize) -> Result<()> { pid: posix_getpid(), }; match wrapper(false, || unsafe { - syscall::syscall3(syscall::SYS_SIGENQUEUE, pid, sig, addr_of!(siginf) as usize) + //syscall::syscall3(syscall::SYS_SIGENQUEUE, pid, sig, addr_of!(siginf) as usize) + Ok(todo!("sigenqueue")) }) { Ok(_) | Err(Error { errno: EINTR }) => Ok(()), Err(error) => Err(error), @@ -70,7 +71,10 @@ pub fn posix_getpid() -> u32 { } #[inline] pub fn posix_killpg(pgrp: usize, sig: usize) -> Result<()> { - match wrapper(false, || syscall::kill(usize::wrapping_neg(pgrp), sig)) { + match wrapper(false, || + //syscall::kill(usize::wrapping_neg(pgrp), sig) + Ok(todo!("killpg")) + ) { Ok(_) | Err(Error { errno: EINTR }) => Ok(()), Err(error) => Err(error), } @@ -103,11 +107,12 @@ pub unsafe fn sys_futex_wake(addr: *mut u32, num: u32) -> Result { } pub fn sys_waitpid(pid: usize, status: &mut usize, flags: usize) -> Result { wrapper(true, || { - syscall::waitpid( + /*syscall::waitpid( pid, status, syscall::WaitFlags::from_bits(flags).expect("waitpid: invalid bit pattern"), - ) + )*/ + todo!("waitpid") }) } pub fn posix_kill_thread(thread_fd: usize, signal: u32) -> Result<()> { @@ -134,3 +139,40 @@ pub fn swap_umask(mask: u32) -> u32 { pub fn get_umask() -> u32 { UMASK.load(Ordering::Acquire) } + +pub fn posix_setresuid(ruid: Option, euid: Option, suid: Option) -> Result<()> { + todo!("posix_setresuid") +} +pub fn posix_setresgid(rgid: Option, egid: Option, sgid: Option) -> Result<()> { + todo!("posix_setresgid") +} +pub fn posix_getruid() -> u32 { + todo!("posix_getruid") +} +pub fn posix_getrgid() -> u32 { + todo!("posix_getrgid") +} +pub fn posix_geteuid() -> u32 { + todo!("posix_geteuid") +} +pub fn posix_getegid() -> u32 { + todo!("posix_getegid") +} +pub fn posix_getppid() -> usize { + todo!("posix_getppid") +} +pub fn posix_exit(status: i32) -> ! { + todo!("posix_exit") +} +pub fn setrens(rns: usize, ens: usize) -> Result<()> { + todo!("setrens") +} +pub fn posix_getpgid(pid: usize) -> Result { + todo!("posix_getpgid") +} +pub fn posix_setpgid(pid: usize, pgid: usize) -> Result<()> { + todo!("posix_setpgid") +} +pub fn posix_getsid(pid: usize) -> Result { + todo!("posix_getsid") +} diff --git a/src/platform/redox/exec.rs b/src/platform/redox/exec.rs index 10e09eccc1..2e7923d83e 100644 --- a/src/platform/redox/exec.rs +++ b/src/platform/redox/exec.rs @@ -119,8 +119,8 @@ pub fn execve( let mut stat = Stat::default(); syscall::fstat(*image_file as usize, &mut stat)?; - let uid = syscall::getuid()?; - let gid = syscall::getuid()?; + let uid = redox_rt::sys::posix_getruid() as usize; + let gid = redox_rt::sys::posix_getrgid() as usize; let mode = if uid == stat.st_uid as usize { (stat.st_mode >> 3 * 2) & 0o7 diff --git a/src/platform/redox/libredox.rs b/src/platform/redox/libredox.rs index 5447db0436..62506a4b08 100644 --- a/src/platform/redox/libredox.rs +++ b/src/platform/redox/libredox.rs @@ -219,36 +219,36 @@ pub unsafe extern "C" fn redox_close_v1(fd: usize) -> RawResult { #[no_mangle] pub unsafe extern "C" fn redox_get_pid_v1() -> RawResult { - Error::mux(syscall::getpid()) + redox_rt::sys::posix_getpid() as _ } #[no_mangle] pub unsafe extern "C" fn redox_get_euid_v1() -> RawResult { - Error::mux(syscall::geteuid()) + redox_rt::sys::posix_geteuid() as _ } #[no_mangle] pub unsafe extern "C" fn redox_get_ruid_v1() -> RawResult { - Error::mux(syscall::getuid()) + redox_rt::sys::posix_getruid() as _ } #[no_mangle] pub unsafe extern "C" fn redox_get_egid_v1() -> RawResult { - Error::mux(syscall::getegid()) + redox_rt::sys::posix_getegid() as _ } #[no_mangle] pub unsafe extern "C" fn redox_get_rgid_v1() -> RawResult { - Error::mux(syscall::getgid()) + redox_rt::sys::posix_getrgid() as _ } #[no_mangle] pub unsafe extern "C" fn redox_setrens_v1(rns: usize, ens: usize) -> RawResult { - Error::mux(syscall::setrens(rns, ens)) + Error::mux(redox_rt::sys::setrens(rns, ens).map(|()| 0)) } #[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( + let res = Error::mux(redox_rt::sys::sys_waitpid( pid, &mut sts, - WaitFlags::from_bits_truncate(options as usize), + WaitFlags::from_bits_truncate(options as usize).bits(), )); status.write(sts as i32); res @@ -256,7 +256,7 @@ pub unsafe extern "C" fn redox_waitpid_v1(pid: usize, status: *mut i32, options: #[no_mangle] pub unsafe extern "C" fn redox_kill_v1(pid: usize, signal: u32) -> RawResult { - Error::mux(syscall::kill(pid, signal as usize)) + Error::mux(redox_rt::sys::posix_kill(pid, signal as usize).map(|()| 0)) } #[no_mangle] diff --git a/src/platform/redox/mod.rs b/src/platform/redox/mod.rs index f3cfa0b137..48986b2539 100644 --- a/src/platform/redox/mod.rs +++ b/src/platform/redox/mod.rs @@ -50,6 +50,13 @@ fn round_up_to_page_size(val: usize) -> Option { .map(|val| (val - 1) / PAGE_SIZE * PAGE_SIZE) } +fn cvt_uid(id: c_int) -> Result> { + if id == -1 { + return Ok(None); + } + Ok(Some(id.try_into().map_err(|_| Errno(EINVAL))?)) +} + mod clone; mod epoll; mod event; @@ -93,8 +100,8 @@ impl Pal for Sys { syscall::fstat(*fd as usize, &mut stat)?; - let uid = syscall::getuid()?; - let gid = syscall::getgid()?; + let uid = redox_rt::sys::posix_getruid() as usize; + let gid = redox_rt::sys::posix_getrgid() as usize; let perms = if stat.st_uid as usize == uid { stat.st_mode >> (3 * 2 & 0o7) @@ -199,7 +206,7 @@ impl Pal for Sys { } fn exit(status: c_int) -> ! { - let _ = syscall::exit((status as usize) << 8); + let _ = redox_rt::sys::posix_exit(status); loop {} } @@ -401,15 +408,15 @@ impl Pal for Sys { } fn getegid() -> gid_t { - syscall::getegid().unwrap() as gid_t + redox_rt::sys::posix_getegid() as gid_t } fn geteuid() -> uid_t { - syscall::geteuid().unwrap() as uid_t + redox_rt::sys::posix_geteuid() as uid_t } fn getgid() -> gid_t { - syscall::getgid().unwrap() as gid_t + redox_rt::sys::posix_getrgid() as gid_t } unsafe fn getgroups(size: c_int, list: *mut gid_t) -> Result { @@ -423,7 +430,7 @@ impl Pal for Sys { } fn getpgid(pid: pid_t) -> Result { - Ok(syscall::getpgid(pid as usize)? as pid_t) + Ok(redox_rt::sys::posix_getpgid(pid as usize)? as pid_t) } fn getpid() -> pid_t { @@ -431,7 +438,7 @@ impl Pal for Sys { } fn getppid() -> pid_t { - syscall::getppid().unwrap() as pid_t + redox_rt::sys::posix_getppid() as pid_t } fn getpriority(which: c_int, who: id_t) -> Result { @@ -487,17 +494,7 @@ impl Pal for Sys { } fn getsid(pid: pid_t) -> Result { - let mut buf = [0; mem::size_of::()]; - let path = if pid == 0 { - format!("/scheme/thisproc/current/session_id") - } else { - format!("/scheme/proc/{}/session_id", pid) - }; - let path_c = CString::new(path).unwrap(); - 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()) + Ok(redox_rt::sys::posix_getsid(pid as usize)? as _) } fn gettid() -> pid_t { @@ -527,7 +524,7 @@ impl Pal for Sys { } fn getuid() -> uid_t { - syscall::getuid().unwrap() as pid_t + redox_rt::sys::posix_getruid() as uid_t } fn lchown(path: CStr, owner: uid_t, group: gid_t) -> Result<()> { @@ -838,8 +835,7 @@ impl Pal for Sys { } fn setpgid(pid: pid_t, pgid: pid_t) -> Result<()> { - syscall::setpgid(pid as usize, pgid as usize)?; - Ok(()) + Ok(redox_rt::sys::posix_setpgid(pid as usize, pgid as usize)?) } fn setpriority(which: c_int, who: id_t, prio: c_int) -> Result<()> { @@ -864,19 +860,11 @@ impl Pal for Sys { } fn setresgid(rgid: gid_t, egid: gid_t, sgid: gid_t) -> Result<()> { - if sgid != -1 { - println!("TODO: suid"); - } - syscall::setregid(rgid as usize, egid as usize)?; - Ok(()) + Ok(redox_rt::sys::posix_setresgid(cvt_uid(rgid)?, cvt_uid(egid)?, cvt_uid(sgid)?)?) } fn setresuid(ruid: uid_t, euid: uid_t, suid: uid_t) -> Result<()> { - if suid != -1 { - println!("TODO: suid"); - } - syscall::setreuid(ruid as usize, euid as usize)?; - Ok(()) + Ok(redox_rt::sys::posix_setresuid(cvt_uid(ruid)?, cvt_uid(euid)?, cvt_uid(suid)?)?) } fn symlink(path1: CStr, path2: CStr) -> Result<()> { @@ -1057,8 +1045,8 @@ impl Pal for Sys { } fn verify() -> bool { - // GETPID on Redox is 20, which is WRITEV on Linux - (unsafe { syscall::syscall5(syscall::number::SYS_GETPID, !0, !0, !0, !0, !0) }).is_ok() + // YIELD on Redox is 20, which is SYS_ARCH_PRCTL on Linux + (unsafe { syscall::syscall5(syscall::number::SYS_YIELD, !0, !0, !0, !0, !0) }).is_ok() } unsafe fn exit_thread(stack_base: *mut (), stack_size: usize) -> ! {