From f629b061fa07a0a7122c92b4aace0c5129da35f7 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Fri, 14 Nov 2025 13:28:56 -0700 Subject: [PATCH] use upper fd table --- Cargo.lock | 100 +++--------------- bootstrap/Cargo.lock | 32 +++--- bootstrap/src/exec.rs | 44 +++++--- bootstrap/src/initfs.rs | 4 +- bootstrap/src/procmgr.rs | 223 ++++++++++++++++++--------------------- 5 files changed, 158 insertions(+), 245 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 26ff4a5400..964df27276 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -52,22 +52,22 @@ dependencies = [ [[package]] name = "anstyle-query" -version = "1.1.4" +version = "1.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e231f6134f61b71076a3eab506c379d4f36122f2af15a9ff04415ea4c3339e2" +checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" dependencies = [ - "windows-sys 0.60.2", + "windows-sys", ] [[package]] name = "anstyle-wincon" -version = "3.0.10" +version = "3.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3e0633414522a32ffaac8ac6cc8f748e090c5717661fddeea04219e2344f5f2a" +checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" dependencies = [ "anstyle", "once_cell_polyfill", - "windows-sys 0.60.2", + "windows-sys", ] [[package]] @@ -170,9 +170,9 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" [[package]] name = "cc" -version = "1.2.45" +version = "1.2.46" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "35900b6c8d709fb1d854671ae27aeaa9eec2f8b01b364e1619a40da3e6fe2afe" +checksum = "b97463e1064cb1b1c1384ad0a0b9c8abd0988e2a91f52606c80ef14aadb63e36" dependencies = [ "find-msvc-tools", "shlex", @@ -313,9 +313,9 @@ checksum = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed" [[package]] name = "find-msvc-tools" -version = "0.1.4" +version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "52051878f80a721bb68ebfbc930e07b65ba72f2da88968ea5c06fd6ca3d3a127" +checksum = "3a3076410a55c90011c298b04d0cfa770b00fa04e1e3c97d3f6c9de105a03844" [[package]] name = "generic-array" @@ -329,7 +329,7 @@ dependencies = [ [[package]] name = "generic-rt" version = "0.1.0" -source = "git+https://gitlab.redox-os.org/redox-os/relibc.git#1c6bbf24af2dee5300481d12ae93e88976be4727" +source = "git+https://gitlab.redox-os.org/redox-os/relibc.git#099c35f3caa1d8c289d14423607c494c59ad8a51" [[package]] name = "getrandom" @@ -757,7 +757,7 @@ dependencies = [ [[package]] name = "redox-rt" version = "0.1.0" -source = "git+https://gitlab.redox-os.org/redox-os/relibc.git#1c6bbf24af2dee5300481d12ae93e88976be4727" +source = "git+https://gitlab.redox-os.org/redox-os/relibc.git#099c35f3caa1d8c289d14423607c494c59ad8a51" dependencies = [ "bitflags 2.10.0", "generic-rt", @@ -1127,7 +1127,7 @@ version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" dependencies = [ - "windows-sys 0.61.2", + "windows-sys", ] [[package]] @@ -1195,15 +1195,6 @@ dependencies = [ "windows-link", ] -[[package]] -name = "windows-sys" -version = "0.60.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f2f500e4d28234f72040990ec9d39e3a6b950f9f22d3dba18416c35882612bcb" -dependencies = [ - "windows-targets", -] - [[package]] name = "windows-sys" version = "0.61.2" @@ -1213,71 +1204,6 @@ dependencies = [ "windows-link", ] -[[package]] -name = "windows-targets" -version = "0.53.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4945f9f551b88e0d65f3db0bc25c33b8acea4d9e41163edf90dcd0b19f9069f3" -dependencies = [ - "windows-link", - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_gnullvm", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", -] - -[[package]] -name = "windows_aarch64_gnullvm" -version = "0.53.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a9d8416fa8b42f5c947f8482c43e7d89e73a173cead56d044f6a56104a6d1b53" - -[[package]] -name = "windows_aarch64_msvc" -version = "0.53.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b9d782e804c2f632e395708e99a94275910eb9100b2114651e04744e9b125006" - -[[package]] -name = "windows_i686_gnu" -version = "0.53.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "960e6da069d81e09becb0ca57a65220ddff016ff2d6af6a223cf372a506593a3" - -[[package]] -name = "windows_i686_gnullvm" -version = "0.53.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa7359d10048f68ab8b09fa71c3daccfb0e9b559aed648a8f95469c27057180c" - -[[package]] -name = "windows_i686_msvc" -version = "0.53.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e7ac75179f18232fe9c285163565a57ef8d3c89254a30685b57d83a38d326c2" - -[[package]] -name = "windows_x86_64_gnu" -version = "0.53.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c3842cdd74a865a8066ab39c8a7a473c0778a3f29370b5fd6b4b9aa7df4a499" - -[[package]] -name = "windows_x86_64_gnullvm" -version = "0.53.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ffa179e2d07eee8ad8f57493436566c7cc30ac536a3379fdf008f47f6bb7ae1" - -[[package]] -name = "windows_x86_64_msvc" -version = "0.53.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d6bbff5f0aada427a1e5a6da5f1f98158182f26556f345ac9e04d36d0ebed650" - [[package]] name = "zerocopy" version = "0.8.27" diff --git a/bootstrap/Cargo.lock b/bootstrap/Cargo.lock index 14ed059010..3cef880b45 100644 --- a/bootstrap/Cargo.lock +++ b/bootstrap/Cargo.lock @@ -10,9 +10,9 @@ checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50" [[package]] name = "bitflags" -version = "2.9.4" +version = "2.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2261d10cca569e4643e526d8dc2e62e433cc8aba21ab764233731f8d369bf394" +checksum = "812e12b5285cc515a9c72a5c1d3b6d46a19dac5acfef5265968c166106e31dd3" [[package]] name = "bootstrap" @@ -40,7 +40,7 @@ checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2" [[package]] name = "generic-rt" version = "0.1.0" -source = "git+https://gitlab.redox-os.org/redox-os/relibc.git#7ed934a01d2ac8ab603b6dc24e918d8197192068" +source = "git+https://gitlab.redox-os.org/redox-os/relibc.git#099c35f3caa1d8c289d14423607c494c59ad8a51" [[package]] name = "goblin" @@ -64,9 +64,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.176" +version = "0.2.177" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "58f929b4d672ea937a23a1ab494143d968337a5f47e56d0815df1e0890ddf174" +checksum = "2874a2af47a2325c2001a6e6fad9b16a53b802102b528163885171cf92b15976" [[package]] name = "libredox" @@ -111,18 +111,18 @@ checksum = "b4596b6d070b27117e987119b4dac604f3c58cfb0b191112e24771b2faeac1a6" [[package]] name = "proc-macro2" -version = "1.0.101" +version = "1.0.103" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89ae43fd86e4158d6db51ad8e2b80f313af9cc74f5c0e03ccb87de09998732de" +checksum = "5ee95bc4ef87b8d5ba32e8b7714ccc834865276eab0aed5c9958d00ec45f49e8" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.41" +version = "1.0.42" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ce25767e7b499d1b604768e7cde645d14cc8584231ea6b295e9c9eb22c02e1d1" +checksum = "a338cc41d27e6cc6dce6cefc13a0729dfbb81c262b1f519331575dd80ef3067f" dependencies = [ "proc-macro2", ] @@ -143,7 +143,7 @@ checksum = "436d45c2b6a5b159d43da708e62b25be3a4a3d5550d654b72216ade4c4bfd717" [[package]] name = "redox-rt" version = "0.1.0" -source = "git+https://gitlab.redox-os.org/redox-os/relibc.git#7ed934a01d2ac8ab603b6dc24e918d8197192068" +source = "git+https://gitlab.redox-os.org/redox-os/relibc.git#099c35f3caa1d8c289d14423607c494c59ad8a51" dependencies = [ "bitflags", "generic-rt", @@ -164,8 +164,8 @@ dependencies = [ [[package]] name = "redox_syscall" -version = "0.5.17" -source = "git+https://gitlab.redox-os.org/redox-os/syscall.git?branch=master#aedadf4d5cb915e0c6a22a5c7358fa853354e21d" +version = "0.5.18" +source = "git+https://gitlab.redox-os.org/redox-os/syscall.git?branch=master#15aa8e32ef0f3328dd9d48cc8ca02e148a362069" dependencies = [ "bitflags", ] @@ -213,9 +213,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.106" +version = "2.0.110" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ede7c438028d4436d71104916910f5bb611972c5cfd7f89b8300a8186e6fada6" +checksum = "a99801b5bd34ede4cf3fc688c5919368fea4e4814a4664359503e6015b280aea" dependencies = [ "proc-macro2", "quote", @@ -224,6 +224,6 @@ dependencies = [ [[package]] name = "unicode-ident" -version = "1.0.19" +version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f63a545481291138910575129486daeaf8ac54aee4387fe7906919f7830c7d9d" +checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5" diff --git a/bootstrap/src/exec.rs b/bootstrap/src/exec.rs index bf89cf775a..9f5a32bdec 100644 --- a/bootstrap/src/exec.rs +++ b/bootstrap/src/exec.rs @@ -2,7 +2,7 @@ use alloc::borrow::ToOwned; use alloc::vec::Vec; use syscall::flag::{O_CLOEXEC, O_RDONLY}; -use syscall::{Error, EINTR}; +use syscall::{EINTR, Error}; use redox_rt::proc::*; @@ -26,12 +26,13 @@ impl log::Log for Logger { } pub fn main() -> ! { - let auth = FdGuard::new( - syscall::open("/scheme/kernel.proc/authority", O_CLOEXEC) - .expect("failed to get proc authority"), - ); - let this_thr_fd = - FdGuard::new(syscall::dup(*auth, b"cur-context").expect("failed to open open_via_dup")); + let auth = FdGuard::open("/scheme/kernel.proc/authority", O_CLOEXEC) + .expect("failed to get proc authority"); + let this_thr_fd = auth + .dup(b"cur-context") + .expect("failed to open open_via_dup") + .to_upper() + .unwrap(); let this_thr_fd = unsafe { redox_rt::initialize_freestanding(this_thr_fd) }; log::set_max_level(log::LevelFilter::Warn); @@ -40,10 +41,8 @@ pub fn main() -> ! { let envs = { let mut env = [0_u8; 4096]; - let fd = FdGuard::new( - syscall::open("/scheme/sys/env", O_RDONLY).expect("bootstrap: failed to open env"), - ); - let bytes_read = syscall::read(*fd, &mut env).expect("bootstrap: failed to read env"); + let fd = FdGuard::open("/scheme/sys/env", O_RDONLY).expect("bootstrap: failed to open env"); + let bytes_read = fd.read(&mut env).expect("bootstrap: failed to read env"); if bytes_read >= env.len() { // TODO: Handle this, we can allocate as much as we want in theory. @@ -87,7 +86,7 @@ pub fn main() -> ! { spawn("process manager", &auth, &this_thr_fd, |write_fd| { crate::procmgr::run(write_fd, &auth) }); - let [init_proc_fd, init_thr_fd] = unsafe { redox_rt::proc::make_init() }; + let (init_proc_fd, init_thr_fd) = unsafe { redox_rt::proc::make_init() }; // from this point, this_thr_fd is no longer valid const CWD: &[u8] = b"/scheme/initfs"; @@ -98,8 +97,8 @@ pub fn main() -> ! { sigprocmask: 0, sigignmask: 0, umask: redox_rt::sys::get_umask(), - thr_fd: **init_thr_fd, - proc_fd: **init_proc_fd, + thr_fd: init_thr_fd.as_raw_fd(), + proc_fd: init_proc_fd.as_raw_fd(), }; let path = "/scheme/initfs/bin/init"; @@ -112,8 +111,14 @@ pub fn main() -> ! { + DEFAULT_SCHEME.len() + 1; - let image_file = FdGuard::new(syscall::open(path, O_RDONLY).expect("failed to open init")); - let memory = FdGuard::new(syscall::open("/scheme/memory", 0).expect("failed to open memory")); + let image_file = FdGuard::open(path, O_RDONLY) + .expect("failed to open init") + .to_upper() + .unwrap(); + let memory = FdGuard::open("/scheme/memory", 0) + .expect("failed to open memory") + .to_upper() + .unwrap(); fexec_impl( image_file, @@ -132,7 +137,12 @@ pub fn main() -> ! { unreachable!() } -pub(crate) fn spawn(name: &str, auth: &FdGuard, this_thr_fd: &FdGuard, inner: impl FnOnce(usize)) { +pub(crate) fn spawn( + name: &str, + auth: &FdGuard, + this_thr_fd: &FdGuardUpper, + inner: impl FnOnce(usize), +) { let read = syscall::open("/scheme/pipe", O_CLOEXEC).expect("failed to open sync read pipe"); // The write pipe will not inherit O_CLOEXEC, but is closed by the daemon later. diff --git a/bootstrap/src/initfs.rs b/bootstrap/src/initfs.rs index 5f88f05a04..062068d313 100644 --- a/bootstrap/src/initfs.rs +++ b/bootstrap/src/initfs.rs @@ -6,10 +6,10 @@ use core::str; use alloc::string::String; use hashbrown::HashMap; -use redox_initfs::{types::Timespec, InitFs, Inode, InodeDir, InodeKind, InodeStruct}; +use redox_initfs::{InitFs, Inode, InodeDir, InodeKind, InodeStruct, types::Timespec}; use redox_path::canonicalize_to_standard; -use redox_scheme::{scheme::SchemeSync, CallerCtx, OpenResult, RequestKind}; +use redox_scheme::{CallerCtx, OpenResult, RequestKind, scheme::SchemeSync}; use redox_scheme::{SignalBehavior, Socket}; use syscall::data::Stat; diff --git a/bootstrap/src/procmgr.rs b/bootstrap/src/procmgr.rs index 45aac827b2..2bec5e1870 100644 --- a/bootstrap/src/procmgr.rs +++ b/bootstrap/src/procmgr.rs @@ -8,8 +8,8 @@ use core::str::FromStr; use core::sync::atomic::Ordering; use core::task::Poll::{self, *}; -use alloc::collections::btree_map::BTreeMap; use alloc::collections::VecDeque; +use alloc::collections::btree_map::BTreeMap; use alloc::rc::{Rc, Weak}; use alloc::vec; use alloc::vec::Vec; @@ -20,8 +20,8 @@ use hashbrown::{DefaultHashBuilder, HashMap, HashSet}; use redox_rt::proc::FdGuard; use redox_rt::protocol::{ - ProcCall, ProcKillTarget, ProcMeta, RtSigInfo, ThreadCall, WaitFlags, SIGCHLD, SIGCONT, SIGHUP, - SIGKILL, SIGSTOP, SIGTSTP, SIGTTIN, SIGTTOU, + ProcCall, ProcKillTarget, ProcMeta, RtSigInfo, SIGCHLD, SIGCONT, SIGHUP, SIGKILL, SIGSTOP, + SIGTSTP, SIGTTIN, SIGTTOU, ThreadCall, WaitFlags, }; use redox_scheme::scheme::{IntoTag, Op, OpCall}; use redox_scheme::{ @@ -31,11 +31,11 @@ use redox_scheme::{ use slab::Slab; use syscall::schemev2::NewFdFlags; use syscall::{ - sig_bit, ContextStatus, ContextVerb, CtxtStsBuf, Error, Event, EventFlags, FobtainFdFlags, - MapFlags, ProcSchemeAttrs, Result, SenderInfo, SetSighandlerData, SigProcControl, Sigcontrol, - EACCES, EAGAIN, EBADF, EBADFD, ECANCELED, ECHILD, EEXIST, EINTR, EINVAL, ENOENT, ENOSYS, - EOPNOTSUPP, EOWNERDEAD, EPERM, ERESTART, ESRCH, EWOULDBLOCK, O_ACCMODE, O_CREAT, O_RDONLY, - PAGE_SIZE, + ContextStatus, ContextVerb, CtxtStsBuf, EACCES, EAGAIN, EBADF, EBADFD, ECANCELED, ECHILD, + EEXIST, EINTR, EINVAL, ENOENT, ENOSYS, EOPNOTSUPP, EOWNERDEAD, EPERM, ERESTART, ESRCH, + EWOULDBLOCK, Error, Event, EventFlags, FobtainFdFlags, MapFlags, O_ACCMODE, O_CREAT, O_RDONLY, + PAGE_SIZE, ProcSchemeAttrs, Result, SenderInfo, SetSighandlerData, SigProcControl, Sigcontrol, + sig_bit, }; #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] @@ -77,18 +77,18 @@ pub fn run(write_fd: usize, auth: &FdGuard) { continue; }; match scheme.work_on(state, &mut new_awoken) { - Ready(resp) => { - loop { - match socket.write_response(resp, SignalBehavior::Interrupt) { - Ok(false) => break 'outer, - Ok(_) => break, - Err(err) if err.errno == EINTR => continue, - Err(err) => { - panic!("bootstrap: failed to write scheme response to kernel: {err}") - } + Ready(resp) => loop { + match socket.write_response(resp, SignalBehavior::Interrupt) { + Ok(false) => break 'outer, + Ok(_) => break, + Err(err) if err.errno == EINTR => continue, + Err(err) => { + panic!( + "bootstrap: failed to write scheme response to kernel: {err}" + ) } } - } + }, Pending => continue, } } @@ -141,7 +141,7 @@ pub fn run(write_fd: usize, auth: &FdGuard) { let mut proc = proc_rc.borrow_mut(); log::trace!("THREAD EVENT FROM {}, {}", event.data, thread.pid.0); let mut sts_buf = CtxtStsBuf::default(); - let _ = syscall::read(*thread.status_hndl, &mut sts_buf).unwrap(); + thread.status_hndl.read(&mut sts_buf).unwrap(); let status = if sts_buf.status == ContextStatus::Dead as usize { // dont-care, already called explicit exit() @@ -333,7 +333,7 @@ impl Page { off: displacement, ptr: NonNull::new(unsafe { syscall::fmap( - **fd, + fd.as_raw_fd(), &syscall::Map { offset: req_offset, size: PAGE_SIZE, @@ -537,19 +537,14 @@ enum WaitpidTarget { struct RawEventQueue(FdGuard); impl RawEventQueue { pub fn new() -> Result { - syscall::open("/scheme/event", O_CREAT) - .map(FdGuard::new) - .map(Self) + FdGuard::open("/scheme/event", O_CREAT).map(Self) } pub fn subscribe(&self, fd: usize, ident: usize, flags: EventFlags) -> Result<()> { - let _ = syscall::write( - *self.0, - &Event { - id: fd, - data: ident, - flags, - }, - )?; + self.0.write(&Event { + id: fd, + data: ident, + flags, + })?; Ok(()) } pub fn unsubscribe(&self, fd: usize, ident: usize) -> Result<()> { @@ -557,7 +552,7 @@ impl RawEventQueue { } pub fn next_event(&self) -> Result { let mut event = Event::default(); - let read = syscall::read(*self.0, &mut event)?; + let read = self.0.read(&mut event)?; assert_eq!( read, size_of::(), @@ -598,15 +593,11 @@ impl<'a> ProcScheme<'a> { // TODO: Use global thread id etc. rather than reusing fd for identifier? self.queue - .subscribe(*fd, fd_out, EventFlags::EVENT_READ) + .subscribe(fd_out, fd_out, EventFlags::EVENT_READ) + .expect("TODO"); + let status_hndl = fd + .dup(alloc::format!("auth-{}-status", self.auth.as_raw_fd()).as_bytes()) .expect("TODO"); - let status_hndl = FdGuard::new( - syscall::dup( - *fd, - alloc::format!("auth-{}-status", **self.auth).as_bytes(), - ) - .expect("TODO"), - ); let thread = Rc::new(RefCell::new(Thread { fd, @@ -677,17 +668,15 @@ impl<'a> ProcScheme<'a> { .. } = *proc_guard.borrow(); - let new_ctxt_fd = FdGuard::new(syscall::dup(**self.auth, b"new-context")?); - let status_fd = FdGuard::new(syscall::dup( - *new_ctxt_fd, - alloc::format!("auth-{}-status", **self.auth).as_bytes(), - )?); + let new_ctxt_fd = self.auth.dup(b"new-context")?; + let status_fd = + new_ctxt_fd.dup(alloc::format!("auth-{}-status", self.auth.as_raw_fd()).as_bytes())?; + let thread_ident = new_ctxt_fd.as_raw_fd(); self.queue - .subscribe(*new_ctxt_fd, *new_ctxt_fd, EventFlags::EVENT_READ) + .subscribe(thread_ident, thread_ident, EventFlags::EVENT_READ) .expect("TODO"); - let thread_ident = *new_ctxt_fd; let thread = Rc::new(RefCell::new(Thread { fd: new_ctxt_fd, status_hndl: status_fd, @@ -744,34 +733,27 @@ impl<'a> ProcScheme<'a> { let proc_rc = self.processes.get_mut(&pid).ok_or(Error::new(EBADFD))?; let mut proc = proc_rc.borrow_mut(); - let ctxt_fd = FdGuard::new(syscall::dup(**self.auth, b"new-context")?); + let ctxt_fd = self.auth.dup(b"new-context")?; // TODO: sync_kernel_attrs? - let attr_fd = FdGuard::new(syscall::dup( - *ctxt_fd, - alloc::format!("auth-{}-attrs", **self.auth).as_bytes(), - )?); - let _ = syscall::write( - *attr_fd, - &ProcSchemeAttrs { - pid: pid.0 as u32, - euid: proc.euid, - egid: proc.egid, - ens: proc.ens, - debug_name: arraystring_to_bytes(proc.name), - }, - )?; + let attr_fd = + ctxt_fd.dup(alloc::format!("auth-{}-attrs", self.auth.as_raw_fd()).as_bytes())?; + attr_fd.write(&ProcSchemeAttrs { + pid: pid.0 as u32, + euid: proc.euid, + egid: proc.egid, + ens: proc.ens, + debug_name: arraystring_to_bytes(proc.name), + })?; - let status_hndl = FdGuard::new(syscall::dup( - *ctxt_fd, - alloc::format!("auth-{}-status", **self.auth).as_bytes(), - )?); + let status_hndl = + ctxt_fd.dup(alloc::format!("auth-{}-status", self.auth.as_raw_fd()).as_bytes())?; + let ident = ctxt_fd.as_raw_fd(); self.queue - .subscribe(*ctxt_fd, *ctxt_fd, EventFlags::EVENT_READ) + .subscribe(ident, ident, EventFlags::EVENT_READ) .expect("TODO"); - let ident = *ctxt_fd; let thread = Rc::new(RefCell::new(Thread { fd: ctxt_fd, status_hndl, @@ -852,7 +834,7 @@ impl<'a> ProcScheme<'a> { Ok(len) } Handle::Init | Handle::Thread(_) | Handle::ProcCredsCapability => { - return Err(Error::new(EBADF)) + return Err(Error::new(EBADF)); } } } @@ -897,7 +879,7 @@ impl<'a> ProcScheme<'a> { // By forwarding all dup calls to the kernel, this fd is now effectively the same // as the underlying fd since that fd can't do anything itself. Ok(OpenResult::OtherScheme { - fd: syscall::dup(*thread.fd, buf)?, + fd: thread.fd.dup(buf)?.take(), }) } Handle::Init | Handle::Ps(_) | Handle::ProcCredsCapability => Err(Error::new(EBADF)), @@ -938,11 +920,7 @@ impl<'a> ProcScheme<'a> { return Response::ready_err(EINVAL, op); }; fn cvt_u32(u: u32) -> Option { - if u == u32::MAX { - None - } else { - Some(u) - } + if u == u32::MAX { None } else { Some(u) } } match verb { ProcCall::Setrens => Ready(Response::new( @@ -1305,14 +1283,14 @@ impl<'a> ProcScheme<'a> { Response::ready_err(EAGAIN, tag) } else { Pending - } + }; } ProcessStatus::Exited { .. } => { return if let Some(tag) = tag { Response::ready_err(ESRCH, tag) } else { Pending - } + }; } } @@ -1344,7 +1322,7 @@ impl<'a> ProcScheme<'a> { for thread in &process.threads { let thread = thread.borrow_mut(); // TODO: cancel all threads anyway on error? - if let Err(err) = syscall::write(*thread.status_hndl, &usize::MAX.to_ne_bytes()) { + if let Err(err) = thread.status_hndl.write(&usize::MAX.to_ne_bytes()) { if let Some(tag) = tag { return Response::ready_err(err.errno, tag); } @@ -1714,7 +1692,7 @@ impl<'a> ProcScheme<'a> { } else { state_entry.remove(); Pending - } + }; } _ => { return if let Some(tag) = tag { @@ -1797,7 +1775,9 @@ impl<'a> ProcScheme<'a> { false, awoken, ) { - log::warn!("Failed to send newly-orphaned-pgid SIGHUP to PID {sighup_pid:?}: {err}"); + log::warn!( + "Failed to send newly-orphaned-pgid SIGHUP to PID {sighup_pid:?}: {err}" + ); } log::trace!("SENDING SIGHUP TO {sighup_pid:?}"); if let Err(err) = self.on_send_sig( @@ -1809,7 +1789,9 @@ impl<'a> ProcScheme<'a> { false, awoken, ) { - log::warn!("Failed to send newly-orphaned-pgid SIGHUP to PID {sighup_pid:?}: {err}"); + log::warn!( + "Failed to send newly-orphaned-pgid SIGHUP to PID {sighup_pid:?}: {err}" + ); } } } @@ -2115,11 +2097,10 @@ impl<'a> ProcScheme<'a> { Ordering::Relaxed, ); } - let _ = syscall::write( - *thread.status_hndl, - &(ContextVerb::Unstop as usize).to_ne_bytes(), - ) - .expect("TODO"); + thread + .status_hndl + .write(&(ContextVerb::Unstop as usize).to_ne_bytes()) + .expect("TODO"); } // POSIX XSI allows but does not reqiure SIGCHLD to be sent when SIGCONT occurs. return SendResult::SucceededSigcont { @@ -2154,10 +2135,10 @@ impl<'a> ProcScheme<'a> { for thread in &target_proc.threads { let thread = thread.borrow(); - match syscall::write( - *thread.status_hndl, - &(ContextVerb::Stop as usize).to_ne_bytes(), - ) { + match thread + .status_hndl + .write(&(ContextVerb::Stop as usize).to_ne_bytes()) + { Ok(_) => (), // TODO: Write a test that this actually results in the thread eventually // being removed from `threads`. A "dead thread" event should already have @@ -2167,7 +2148,10 @@ impl<'a> ProcScheme<'a> { // Thread has state Dead, so ignore. Err(Error { errno: EOWNERDEAD }) => continue, Err(other) => { - log::error!("Unexpected error when stopping context: {other}, pid {target_pid:?} thread fd {}", *thread.status_hndl); + log::error!( + "Unexpected error when stopping context: {other}, pid {target_pid:?} thread fd {}", + thread.status_hndl.as_raw_fd() + ); continue; } } @@ -2191,11 +2175,10 @@ impl<'a> ProcScheme<'a> { if sig == SIGKILL { for thread in &target_proc.threads { let thread = thread.borrow(); - let _ = syscall::write( - *thread.status_hndl, - &(ContextVerb::ForceKill as usize).to_ne_bytes(), - ) - .expect("TODO"); + thread + .status_hndl + .write(&(ContextVerb::ForceKill as usize).to_ne_bytes()) + .expect("TODO"); } *killed_self |= is_self; @@ -2218,11 +2201,10 @@ impl<'a> ProcScheme<'a> { let _was_new = tctl.word[sig_group].fetch_or(bit, Ordering::Release); if (tctl.word[sig_group].load(Ordering::Relaxed) >> 32) & bit != 0 { *killed_self |= is_self; - let _ = syscall::write( - *thread.status_hndl, - &(ContextVerb::Interrupt as usize).to_ne_bytes(), - ) - .expect("TODO"); + thread + .status_hndl + .write(&(ContextVerb::Interrupt as usize).to_ne_bytes()) + .expect("TODO"); } } KillTarget::Proc(proc) => { @@ -2279,11 +2261,10 @@ impl<'a> ProcScheme<'a> { if (tctl.word[sig_group].load(Ordering::Relaxed) >> 32) & sig_bit(sig) != 0 { - let _ = syscall::write( - *thread.status_hndl, - &(ContextVerb::Interrupt as usize).to_ne_bytes(), - ) - .expect("TODO"); + thread + .status_hndl + .write(&(ContextVerb::Interrupt as usize).to_ne_bytes()) + .expect("TODO"); *killed_self |= is_self; break; } @@ -2390,7 +2371,7 @@ impl<'a> ProcScheme<'a> { } fn real_tctl_pctl_intra_page_offsets(fd: &FdGuard) -> Result<[u16; 2]> { let mut buf = SetSighandlerData::default(); - let _ = syscall::read(**fd, &mut buf)?; + fd.read(&mut buf)?; Ok([ (buf.thread_control_addr % PAGE_SIZE) as u16, (buf.proc_control_addr % PAGE_SIZE) as u16, @@ -2418,7 +2399,7 @@ impl<'a> ProcScheme<'a> { } fn on_sync_sigtctl(thread: &mut Thread) -> Result<()> { log::trace!("Sync tctl {:?}", thread.pid); - let sigcontrol_fd = FdGuard::new(syscall::dup(*thread.fd, b"sighandler")?); + let sigcontrol_fd = thread.fd.dup(b"sighandler")?; let [tctl_off, _] = Self::real_tctl_pctl_intra_page_offsets(&sigcontrol_fd)?; log::trace!("read intra offsets"); thread @@ -2434,7 +2415,7 @@ impl<'a> ProcScheme<'a> { .ok_or(Error::new(ESRCH))? .borrow_mut(); let any_thread = proc.threads.first().ok_or(Error::new(EINVAL))?; - let sigcontrol_fd = FdGuard::new(syscall::dup(*any_thread.borrow().fd, b"sighandler")?); + let sigcontrol_fd = any_thread.borrow().fd.dup(b"sighandler")?; let [_, pctl_off] = Self::real_tctl_pctl_intra_page_offsets(&sigcontrol_fd)?; proc.sig_pctl .replace(Page::map(&sigcontrol_fd, PAGE_SIZE, pctl_off)?); @@ -2597,20 +2578,16 @@ impl Process { // TODO: continue with other threads if one fails? for thread_rc in &self.threads { let thread = thread_rc.borrow(); - let attr_fd = FdGuard::new(syscall::dup( - *thread.fd, - alloc::format!("auth-{}-attrs", **auth).as_bytes(), - )?); - let _ = syscall::write( - *attr_fd, - &ProcSchemeAttrs { - pid: my_pid.0 as u32, - euid: self.euid, - egid: self.egid, - ens: self.ens, - debug_name: arraystring_to_bytes(self.name), - }, - )?; + let attr_fd = thread + .fd + .dup(alloc::format!("auth-{}-attrs", auth.as_raw_fd()).as_bytes())?; + attr_fd.write(&ProcSchemeAttrs { + pid: my_pid.0 as u32, + euid: self.euid, + egid: self.egid, + ens: self.ens, + debug_name: arraystring_to_bytes(self.name), + })?; } Ok(()) }