diff --git a/Cargo.lock b/Cargo.lock index 880db7f100..c194a25be8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,15 +2,6 @@ # It is not intended for manual editing. version = 4 -[[package]] -name = "aho-corasick" -version = "1.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" -dependencies = [ - "memchr", -] - [[package]] name = "argon2" version = "0.5.3" @@ -25,15 +16,15 @@ dependencies = [ [[package]] name = "autocfg" -version = "1.4.0" +version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" +checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" [[package]] name = "base64ct" -version = "1.7.3" +version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89e25b6adfb930f02d1981565a6e5d9c547ac15a96606256d3b59040e5cd4ca3" +checksum = "55248b47b0caf0546f7988906588779981c43bb1bc9d0c44087278f80cdb44ba" [[package]] name = "bcrypt-pbkdf" @@ -48,9 +39,9 @@ dependencies = [ [[package]] name = "bitflags" -version = "2.9.0" +version = "2.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c8214115b7bf84099f1309324e63141d4c5d7cc26862f97a0a857dbefe165bd" +checksum = "812e12b5285cc515a9c72a5c1d3b6d46a19dac5acfef5265968c166106e31dd3" [[package]] name = "blake2" @@ -105,40 +96,29 @@ dependencies = [ [[package]] name = "cfg-if" -version = "1.0.0" +version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" [[package]] name = "chrono" -version = "0.4.41" +version = "0.4.42" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c469d952047f47f91b68d1cba3f10d63c11d73e4636f24f08daf0278abf01c4d" +checksum = "145052bdd345b87320e369255277e3fb5152762ad123a901ef5c262dd38fe8d2" dependencies = [ "num-traits", ] [[package]] name = "chrono-tz" -version = "0.10.3" +version = "0.10.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "efdce149c370f133a071ca8ef6ea340b7b88748ab0810097a9e2976eaa34b4f3" +checksum = "a6139a8597ed92cf816dfb33f5dd6cf0bb93a6adc938f11039f371bc5bcd26c3" dependencies = [ "chrono", - "chrono-tz-build", "phf", ] -[[package]] -name = "chrono-tz-build" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f10f8c9340e31fc120ff885fcdb54a0b48e474bbd77cab557f0c30a3e569402" -dependencies = [ - "parse-zoneinfo", - "phf_codegen", -] - [[package]] name = "cipher" version = "0.4.4" @@ -172,9 +152,9 @@ version = "0.1.0" [[package]] name = "crypto-common" -version = "0.1.6" +version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a" dependencies = [ "generic-array", "typenum", @@ -248,21 +228,21 @@ version = "0.1.0" [[package]] name = "libc" -version = "0.2.172" +version = "0.2.177" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d750af042f7ef4f724306de029d18836c26c1765a54a6a3f094cbd23a7267ffa" +checksum = "2874a2af47a2325c2001a6e6fad9b16a53b802102b528163885171cf92b15976" [[package]] name = "libm" -version = "0.2.13" +version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c9627da5196e5d8ed0b0495e61e518847578da83483c37288316d9b2e03a7f72" +checksum = "f9fbbcab51052fe104eb5e5d351cf728d30a5be1fe14d9be8a3b097481fb97de" [[package]] name = "libredox" -version = "0.1.3" +version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c0ff37bd590ca25063e35af745c343cb7a0271906fb7b37e4813e8f79f00268d" +checksum = "416f7e718bdb06000964960ffa43b4335ad4012ae8b99060261aa4a8088d5ccb" dependencies = [ "bitflags", "libc", @@ -270,19 +250,18 @@ dependencies = [ [[package]] name = "lock_api" -version = "0.4.12" +version = "0.4.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17" +checksum = "224399e74b87b5f3557511d98dff8b14089b3dadafcab6bb93eab67d3aace965" dependencies = [ - "autocfg", "scopeguard", ] [[package]] name = "log" -version = "0.4.27" +version = "0.4.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "13dc2df351e3202783a1fe0d44375f7295ffb4049267b0f3018346dc122a1d94" +checksum = "34080505efa8e45a4b816c349525ebe327ceaa8559756f0356cba97ef3bf7432" [[package]] name = "md-5" @@ -296,9 +275,9 @@ dependencies = [ [[package]] name = "memchr" -version = "2.7.4" +version = "2.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" +checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273" [[package]] name = "num-traits" @@ -317,15 +296,6 @@ dependencies = [ "memchr", ] -[[package]] -name = "parse-zoneinfo" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1f2a05b18d44e2957b88f96ba460715e295bc1d7510468a2f3d3b44535d26c24" -dependencies = [ - "regex", -] - [[package]] name = "password-hash" version = "0.5.0" @@ -350,38 +320,18 @@ dependencies = [ [[package]] name = "phf" -version = "0.11.3" +version = "0.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1fd6780a80ae0c52cc120a26a1a42c1ae51b247a253e4e06113d23d2c2edd078" +checksum = "913273894cec178f401a31ec4b656318d95473527be05c0752cc41cdc32be8b7" dependencies = [ "phf_shared", ] -[[package]] -name = "phf_codegen" -version = "0.11.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aef8048c789fa5e851558d709946d6d79a8ff88c0440c587967f8e94bfb1216a" -dependencies = [ - "phf_generator", - "phf_shared", -] - -[[package]] -name = "phf_generator" -version = "0.11.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3c80231409c20246a13fddb31776fb942c38553c51e871f8cbd687a4cfb5843d" -dependencies = [ - "phf_shared", - "rand", -] - [[package]] name = "phf_shared" -version = "0.11.3" +version = "0.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "67eabc2ef2a60eb7faa00097bd1ffdb5bd28e62bf39990626a582201b7a754e5" +checksum = "06005508882fb681fd97892ecff4b7fd0fee13ef1aa569f8695dae7ab9099981" dependencies = [ "siphasher", ] @@ -400,18 +350,18 @@ checksum = "66df580334caab2f744839ab1be85493d7ec731a92d6cf928008ab0b212bf3bc" [[package]] name = "proc-macro2" -version = "1.0.95" +version = "1.0.103" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778" +checksum = "5ee95bc4ef87b8d5ba32e8b7714ccc834865276eab0aed5c9958d00ec45f49e8" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.40" +version = "1.0.42" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d" +checksum = "a338cc41d27e6cc6dce6cefc13a0729dfbb81c262b1f519331575dd80ef3067f" dependencies = [ "proc-macro2", ] @@ -480,41 +430,12 @@ dependencies = [ [[package]] name = "redox_syscall" -version = "0.5.13" -source = "git+https://gitlab.redox-os.org/redox-os/syscall.git?branch=master#f821e2ceb7bf2af0ece0334e06ec89a99404cf59" +version = "0.5.18" +source = "git+https://gitlab.redox-os.org/redox-os/syscall.git?branch=master#15aa8e32ef0f3328dd9d48cc8ca02e148a362069" dependencies = [ "bitflags", ] -[[package]] -name = "regex" -version = "1.11.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191" -dependencies = [ - "aho-corasick", - "memchr", - "regex-automata", - "regex-syntax", -] - -[[package]] -name = "regex-automata" -version = "0.4.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908" -dependencies = [ - "aho-corasick", - "memchr", - "regex-syntax", -] - -[[package]] -name = "regex-syntax" -version = "0.8.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" - [[package]] name = "relibc" version = "0.2.5" @@ -655,9 +576,9 @@ checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" [[package]] name = "syn" -version = "2.0.101" +version = "2.0.110" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ce2b7fc941b3a24138a0a7cf8e858bfc6a992e7978a068a5c760deb0ed43caf" +checksum = "a99801b5bd34ede4cf3fc688c5919368fea4e4814a4664359503e6015b280aea" dependencies = [ "proc-macro2", "quote", @@ -666,15 +587,15 @@ dependencies = [ [[package]] name = "typenum" -version = "1.18.0" +version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1dccffe3ce07af9386bfd29e80c0ab1a8205a2fc34e4bcd40364df902cfa8f3f" +checksum = "562d481066bde0658276a35467c4af00bdc6ee726305698a55b86e61d7ad82bb" [[package]] name = "unicode-ident" -version = "1.0.18" +version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" +checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5" [[package]] name = "unicode-width" diff --git a/redox-rt/src/arch/aarch64.rs b/redox-rt/src/arch/aarch64.rs index f38f446c2a..b34a2a74a3 100644 --- a/redox-rt/src/arch/aarch64.rs +++ b/redox-rt/src/arch/aarch64.rs @@ -4,7 +4,7 @@ use syscall::{data::*, error::*}; use crate::{ RtTcb, Tcb, - proc::{FdGuard, ForkArgs, fork_inner}, + proc::{FdGuard, FdGuardUpper, ForkArgs, fork_inner}, protocol::{ProcCall, RtSigInfo}, signal::{PROC_CONTROL_STRUCT, PosixStackt, RtSigarea, SigStack, inner_c}, }; @@ -74,28 +74,14 @@ pub struct ArchIntRegs { /// Deactive TLS, used before exec() on Redox to not trick target executable into thinking TLS /// is already initialized as if it was a thread. -pub unsafe fn deactivate_tcb(open_via_dup: usize) -> Result<()> { +pub unsafe fn deactivate_tcb(open_via_dup: &FdGuardUpper) -> Result<()> { let mut env = syscall::EnvRegisters::default(); - let file = FdGuard::new(syscall::dup(open_via_dup, b"regs/env")?); + let file = open_via_dup.dup(b"regs/env")?; env.tpidr_el0 = 0; - let _ = syscall::write(*file, &mut env)?; - Ok(()) -} - -pub fn copy_env_regs(cur_pid_fd: usize, new_pid_fd: usize) -> Result<()> { - // Copy environment registers. - { - let cur_env_regs_fd = FdGuard::new(syscall::dup(cur_pid_fd, b"regs/env")?); - let new_env_regs_fd = FdGuard::new(syscall::dup(new_pid_fd, b"regs/env")?); - - let mut env_regs = syscall::EnvRegisters::default(); - let _ = syscall::read(*cur_env_regs_fd, &mut env_regs)?; - let _ = syscall::write(*new_env_regs_fd, &env_regs)?; - } - + file.write(&mut env)?; Ok(()) } diff --git a/redox-rt/src/arch/i686.rs b/redox-rt/src/arch/i686.rs index 67f67d060e..3733f4a87a 100644 --- a/redox-rt/src/arch/i686.rs +++ b/redox-rt/src/arch/i686.rs @@ -4,7 +4,7 @@ use syscall::*; use crate::{ RtTcb, - proc::{FdGuard, ForkArgs, fork_inner}, + proc::{FdGuard, FdGuardUpper, ForkArgs, fork_inner}, protocol::{ProcCall, RtSigInfo}, signal::{PROC_CONTROL_STRUCT, PosixStackt, RtSigarea, SigStack, inner_fastcall}, }; @@ -57,29 +57,15 @@ pub struct ArchIntRegs { /// Deactive TLS, used before exec() on Redox to not trick target executable into thinking TLS /// is already initialized as if it was a thread. -pub unsafe fn deactivate_tcb(open_via_dup: usize) -> Result<()> { +pub unsafe fn deactivate_tcb(open_via_dup: &FdGuardUpper) -> Result<()> { let mut env = syscall::EnvRegisters::default(); - let file = FdGuard::new(syscall::dup(open_via_dup, b"regs/env")?); + let file = open_via_dup.dup(b"regs/env")?; env.fsbase = 0; env.gsbase = 0; - let _ = syscall::write(*file, &mut env)?; - Ok(()) -} - -pub fn copy_env_regs(cur_pid_fd: usize, new_pid_fd: usize) -> Result<()> { - // Copy environment registers. - { - let cur_env_regs_fd = FdGuard::new(syscall::dup(cur_pid_fd, b"regs/env")?); - let new_env_regs_fd = FdGuard::new(syscall::dup(new_pid_fd, b"regs/env")?); - - let mut env_regs = syscall::EnvRegisters::default(); - let _ = syscall::read(*cur_env_regs_fd, &mut env_regs)?; - let _ = syscall::write(*new_env_regs_fd, &env_regs)?; - } - + file.write(&mut env)?; Ok(()) } diff --git a/redox-rt/src/arch/riscv64.rs b/redox-rt/src/arch/riscv64.rs index 0191b3530e..d729a03e1b 100644 --- a/redox-rt/src/arch/riscv64.rs +++ b/redox-rt/src/arch/riscv64.rs @@ -2,7 +2,7 @@ use core::cell::SyncUnsafeCell; use crate::{ RtTcb, Tcb, - proc::{FdGuard, ForkArgs, fork_inner}, + proc::{FdGuard, FdGuardUpper, ForkArgs, fork_inner}, protocol::{ProcCall, RtSigInfo}, signal::{PosixStackt, RtSigarea, SigStack, get_sigaltstack, inner_c}, }; @@ -50,28 +50,14 @@ pub struct ArchIntRegs { /// Deactive TLS, used before exec() on Redox to not trick target executable into thinking TLS /// is already initialized as if it was a thread. -pub unsafe fn deactivate_tcb(open_via_dup: usize) -> Result<()> { +pub unsafe fn deactivate_tcb(open_via_dup: &FdGuardUpper) -> Result<()> { let mut env = syscall::EnvRegisters::default(); - let file = FdGuard::new(syscall::dup(open_via_dup, b"regs/env")?); + let file = open_via_dup.dup(b"regs/env")?; env.tp = 0; - let _ = syscall::write(*file, &mut env)?; - Ok(()) -} - -pub fn copy_env_regs(cur_pid_fd: usize, new_pid_fd: usize) -> Result<()> { - // Copy environment registers. - { - let cur_env_regs_fd = FdGuard::new(syscall::dup(cur_pid_fd, b"regs/env")?); - let new_env_regs_fd = FdGuard::new(syscall::dup(new_pid_fd, b"regs/env")?); - - let mut env_regs = syscall::EnvRegisters::default(); - let _ = syscall::read(*cur_env_regs_fd, &mut env_regs)?; - let _ = syscall::write(*new_env_regs_fd, &env_regs)?; - } - + file.write(&mut env)?; Ok(()) } diff --git a/redox-rt/src/arch/x86_64.rs b/redox-rt/src/arch/x86_64.rs index 3f485e1ec1..2c92aec9d7 100644 --- a/redox-rt/src/arch/x86_64.rs +++ b/redox-rt/src/arch/x86_64.rs @@ -12,7 +12,7 @@ use syscall::{ use crate::{ Tcb, - proc::{FdGuard, ForkArgs, fork_inner}, + proc::{FdGuard, FdGuardUpper, ForkArgs, fork_inner}, protocol::{ProcCall, RtSigInfo}, signal::{PROC_CONTROL_STRUCT, PosixStackt, RtSigarea, SigStack, get_sigaltstack, inner_c}, }; @@ -70,29 +70,15 @@ pub struct ArchIntRegs { /// Deactive TLS, used before exec() on Redox to not trick target executable into thinking TLS /// is already initialized as if it was a thread. -pub unsafe fn deactivate_tcb(open_via_dup: usize) -> Result<()> { +pub unsafe fn deactivate_tcb(open_via_dup: &FdGuardUpper) -> Result<()> { let mut env = syscall::EnvRegisters::default(); - let file = FdGuard::new(syscall::dup(open_via_dup, b"regs/env")?); + let file = open_via_dup.dup(b"regs/env")?; env.fsbase = 0; env.gsbase = 0; - let _ = syscall::write(*file, &mut env)?; - Ok(()) -} - -pub fn copy_env_regs(cur_pid_fd: usize, new_pid_fd: usize) -> Result<()> { - // Copy environment registers. - { - let cur_env_regs_fd = FdGuard::new(syscall::dup(cur_pid_fd, b"regs/env")?); - let new_env_regs_fd = FdGuard::new(syscall::dup(new_pid_fd, b"regs/env")?); - - let mut env_regs = syscall::EnvRegisters::default(); - let _ = syscall::read(*cur_env_regs_fd, &mut env_regs)?; - let _ = syscall::write(*new_env_regs_fd, &env_regs)?; - } - + file.write(&mut env)?; Ok(()) } diff --git a/redox-rt/src/lib.rs b/redox-rt/src/lib.rs index b0ef058d5e..2697646628 100644 --- a/redox-rt/src/lib.rs +++ b/redox-rt/src/lib.rs @@ -12,7 +12,7 @@ use generic_rt::{ExpectTlsFree, GenericTcb}; use syscall::Sigcontrol; use self::{ - proc::{FdGuard, STATIC_PROC_INFO}, + proc::{FdGuard, FdGuardUpper, STATIC_PROC_INFO}, protocol::ProcMeta, sync::Mutex, }; @@ -55,13 +55,13 @@ pub mod thread; pub struct RtTcb { pub control: Sigcontrol, pub arch: UnsafeCell, - pub thr_fd: UnsafeCell>, + pub thr_fd: UnsafeCell>, } impl RtTcb { pub fn current() -> &'static Self { unsafe { &Tcb::current().unwrap().os_specific } } - pub fn thread_fd(&self) -> &FdGuard { + pub fn thread_fd(&self) -> &FdGuardUpper { unsafe { (&*self.thr_fd.get()).as_ref().unwrap() } } } @@ -87,16 +87,16 @@ pub unsafe fn tcb_activate(_tcb: &RtTcb, tls_end: usize, tls_len: usize) { pub unsafe fn tcb_activate(tcb: &RtTcb, tls_end: usize, _tls_len: usize) { let mut env = syscall::EnvRegisters::default(); - let file = FdGuard::new( - syscall::dup(**tcb.thread_fd(), b"regs/env") - .expect_notls("failed to open handle for process registers"), - ); + let file = tcb + .thread_fd() + .dup(b"regs/env") + .expect_notls("failed to open handle for process registers"); - let _ = syscall::read(*file, &mut env).expect_notls("failed to read gsbase"); + file.read(&mut env).expect_notls("failed to read gsbase"); env.gsbase = tls_end as u32; - let _ = syscall::write(*file, &env).expect_notls("failed to write gsbase"); + file.write(&env).expect_notls("failed to write gsbase"); } /// OS and architecture specific code to activate TLS - Redox x86_64 @@ -105,16 +105,16 @@ pub unsafe fn tcb_activate(tcb: &RtTcb, tls_end: usize, _tls_len: usize) { pub unsafe fn tcb_activate(tcb: &RtTcb, tls_end_and_tcb_start: usize, _tls_len: usize) { let mut env = syscall::EnvRegisters::default(); - let file = FdGuard::new( - syscall::dup(**tcb.thread_fd(), b"regs/env") - .expect_notls("failed to open handle for process registers"), - ); + let file = tcb + .thread_fd() + .dup(b"regs/env") + .expect_notls("failed to open handle for process registers"); - let _ = syscall::read(*file, &mut env).expect_notls("failed to read fsbase"); + file.read(&mut env).expect_notls("failed to read fsbase"); env.fsbase = tls_end_and_tcb_start as u64; - let _ = syscall::write(*file, &env).expect_notls("failed to write fsbase"); + file.write(&env).expect_notls("failed to write fsbase"); } /// OS and architecture specific code to activate TLS - Redox riscv64 @@ -135,7 +135,7 @@ pub unsafe fn tcb_activate(_tcb: &RtTcb, tls_end: usize, tls_len: usize) { /// Initialize redox-rt in situations where relibc is not used #[allow(unsafe_op_in_unsafe_fn)] #[cfg(not(feature = "proc"))] -pub unsafe fn initialize_freestanding(this_thr_fd: FdGuard) -> &'static FdGuard { +pub unsafe fn initialize_freestanding(this_thr_fd: FdGuardUpper) -> &'static FdGuardUpper { // TODO: This code is a hack! Integrate the ld_so TCB code into generic-rt, and then use that // (this function will need pointers to the ELF structs normally passed in auxvs), so the TCB // is initialized properly. @@ -181,12 +181,12 @@ pub unsafe fn initialize_freestanding(this_thr_fd: FdGuard) -> &'static FdGuard (*page.os_specific.thr_fd.get()).as_ref().unwrap() } -pub(crate) fn read_proc_meta(proc: &FdGuard) -> syscall::Result { +pub(crate) fn read_proc_meta(proc: &FdGuardUpper) -> syscall::Result { let mut bytes = [0_u8; size_of::()]; - let _ = syscall::read(**proc, &mut bytes)?; + proc.read(&mut bytes)?; Ok(*plain::from_bytes::(&bytes).unwrap()) } -pub unsafe fn initialize(#[cfg(feature = "proc")] proc_fd: FdGuard) { +pub unsafe fn initialize(#[cfg(feature = "proc")] proc_fd: FdGuardUpper) { #[cfg(feature = "proc")] let metadata = read_proc_meta(&proc_fd).unwrap(); @@ -196,7 +196,7 @@ pub unsafe fn initialize(#[cfg(feature = "proc")] proc_fd: FdGuard) { #[cfg(feature = "proc")] { - unsafe { crate::arch::PROC_FD.get().write(*proc_fd) }; + unsafe { crate::arch::PROC_FD.get().write(proc_fd.as_raw_fd()) }; } unsafe { @@ -230,7 +230,7 @@ pub unsafe fn initialize(#[cfg(feature = "proc")] proc_fd: FdGuard) { #[repr(C)] // TODO: is repr(C) required? pub(crate) struct StaticProcInfo { pid: u32, - proc_fd: MaybeUninit, + proc_fd: MaybeUninit, has_proc_fd: bool, } pub struct DynamicProcInfo { @@ -258,7 +258,7 @@ pub(crate) fn static_proc_info() -> &'static StaticProcInfo { unsafe { &*STATIC_PROC_INFO.get() } } #[inline] -pub fn current_proc_fd() -> &'static FdGuard { +pub fn current_proc_fd() -> &'static FdGuardUpper { let info = static_proc_info(); assert!(info.has_proc_fd); unsafe { info.proc_fd.assume_init_ref() } @@ -270,10 +270,13 @@ struct ChildHookCommonArgs { } unsafe fn child_hook_common(args: ChildHookCommonArgs) { + let new_thr_fd = args.new_thr_fd.to_upper().unwrap(); + let new_proc_fd = args.new_proc_fd.map(|x| x.to_upper().unwrap()); + // TODO: just pass PID to child rather than obtaining it via IPC? #[cfg(feature = "proc")] let metadata = read_proc_meta( - args.new_proc_fd + new_proc_fd .as_ref() .expect("must be present with proc feature"), ) @@ -282,8 +285,8 @@ unsafe fn child_hook_common(args: ChildHookCommonArgs) { #[cfg(not(feature = "proc"))] let metadata = ProcMeta::default(); - if let Some(proc_fd) = &args.new_proc_fd { - unsafe { crate::arch::PROC_FD.get().write(**proc_fd) }; + if let Some(proc_fd) = &new_proc_fd { + unsafe { crate::arch::PROC_FD.get().write(proc_fd.as_raw_fd()) }; } let old_proc_fd = unsafe { @@ -291,15 +294,13 @@ unsafe fn child_hook_common(args: ChildHookCommonArgs) { .get() .replace(StaticProcInfo { pid: metadata.pid, - has_proc_fd: args.new_proc_fd.is_some(), - proc_fd: args - .new_proc_fd - .map_or_else(MaybeUninit::uninit, MaybeUninit::new), + has_proc_fd: new_proc_fd.is_some(), + proc_fd: new_proc_fd.map_or_else(MaybeUninit::uninit, MaybeUninit::new), }) .proc_fd }; drop(old_proc_fd); - let old_thr_fd = unsafe { RtTcb::current().thr_fd.get().replace(Some(args.new_thr_fd)) }; + let old_thr_fd = unsafe { RtTcb::current().thr_fd.get().replace(Some(new_thr_fd)) }; drop(old_thr_fd); } diff --git a/redox-rt/src/proc.rs b/redox-rt/src/proc.rs index e3867c1826..de222d1bb3 100644 --- a/redox-rt/src/proc.rs +++ b/redox-rt/src/proc.rs @@ -36,11 +36,11 @@ use syscall::{ pub enum FexecResult { Normal { - addrspace_handle: FdGuard, + addrspace_handle: FdGuardUpper, }, Interp { path: Box<[u8]>, - image_file: FdGuard, + image_file: FdGuardUpper, interp_override: InterpOverride, }, } @@ -70,10 +70,10 @@ pub struct ExtraInfo<'a> { } pub fn fexec_impl( - image_file: FdGuard, - thread_fd: &FdGuard, - proc_fd: &FdGuard, - memory_scheme_fd: &FdGuard, + image_file: FdGuardUpper, + thread_fd: &FdGuardUpper, + proc_fd: &FdGuardUpper, + memory_scheme_fd: &FdGuardUpper, path: &[u8], args: A, envs: E, @@ -92,12 +92,12 @@ where // some misalignments, and then switch address space. let mut header_bytes = [0_u8; size_of::
()]; - pread_all(*image_file, 0, &mut header_bytes)?; + pread_all(&image_file, 0, &mut header_bytes)?; let header = Header::from_bytes(&header_bytes); let grants_fd = { - let current_addrspace_fd = FdGuard::new(syscall::dup(**thread_fd, b"addrspace")?); - FdGuard::new(syscall::dup(*current_addrspace_fd, b"empty")?) + let current_addrspace_fd = thread_fd.dup(b"addrspace")?; + current_addrspace_fd.dup(b"empty")?.to_upper()? }; // Never allow more than 1 MiB of program headers. @@ -121,7 +121,7 @@ where |o| core::mem::take(&mut o.tree), ); - pread_all(*image_file as usize, u64::from(header.e_phoff), phs).map_err(|_| Error::new(EIO))?; + pread_all(&image_file, u64::from(header.e_phoff), phs).map_err(|_| Error::new(EIO))?; for ph_idx in 0..phnum { let ph_bytes = &phs[ph_idx * phentsize..(ph_idx + 1) * phentsize]; @@ -144,11 +144,7 @@ where // PT_INTERP must come before any PT_LOAD, so we don't have to iterate twice. PT_INTERP => { let mut interp = vec![0_u8; segment.p_filesz as usize]; - pread_all( - *image_file as usize, - u64::from(segment.p_offset), - &mut interp, - )?; + pread_all(&image_file, u64::from(segment.p_offset), &mut interp)?; return Ok(FexecResult::Interp { path: interp.into_boxed_slice(), @@ -190,13 +186,13 @@ where if filesz > 0 { let (_guard, dst_memory) = unsafe { MmapGuard::map_mut_anywhere( - *grants_fd, + &grants_fd, vaddr, // offset (voff + filesz).next_multiple_of(PAGE_SIZE), // size )? }; pread_all( - *image_file, + &image_file, u64::from(segment.p_offset), &mut dst_memory[voff..voff + filesz], )?; @@ -245,7 +241,7 @@ where stack_page } else { let new = MmapGuard::map( - *grants_fd, + &grants_fd, &Map { offset: new_page_no * PAGE_SIZE, size: PAGE_SIZE, @@ -285,7 +281,7 @@ where )?; unsafe { let (_guard, memory) = - MmapGuard::map_mut_anywhere(*grants_fd, pheaders, pheaders_size_aligned)?; + MmapGuard::map_mut_anywhere(&grants_fd, pheaders, pheaders_size_aligned)?; memory[..pheaders_to_convey.len()].copy_from_slice(pheaders_to_convey); } @@ -350,7 +346,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); } @@ -412,21 +408,18 @@ where push(argc)?; - if let Ok(sighandler_fd) = syscall::dup(**thread_fd, b"sighandler").map(FdGuard::new) { - let _ = syscall::write( - *sighandler_fd, - &SetSighandlerData { - user_handler: 0, - excp_handler: 0, - thread_control_addr: 0, - proc_control_addr: 0, - }, - ); + if let Ok(sighandler_fd) = thread_fd.dup(b"sighandler") { + let _ = sighandler_fd.write(&SetSighandlerData { + user_handler: 0, + excp_handler: 0, + thread_control_addr: 0, + proc_control_addr: 0, + }); // TODO: sync with procmgr } unsafe { - deactivate_tcb(**thread_fd)?; + deactivate_tcb(&thread_fd)?; } // TODO: Restore old name if exec failed? @@ -438,7 +431,7 @@ where // XXX: takes &mut [] since it can mutate, but we could unsafe{}ly pass it directly // otherwise let _ = proc_call( - **proc_fd, + proc_fd.as_raw_fd(), &mut buf, CallFlags::empty(), &[ProcCall::Rename as u64], @@ -447,37 +440,38 @@ where // TODO: Error handling let _ = proc_call( - **proc_fd, + proc_fd.as_raw_fd(), &mut [], CallFlags::empty(), &[ProcCall::DisableSetpgid as u64], ); if interp_override.is_some() { - let mmap_min_fd = FdGuard::new(syscall::dup(*grants_fd, b"mmap-min-addr")?); + let mmap_min_fd = grants_fd.dup(b"mmap-min-addr")?; let last_addr = tree.iter().rev().nth(1).map_or(0, |(off, len)| *off + *len); let aligned_last_addr = last_addr.next_multiple_of(PAGE_SIZE); - let _ = syscall::write(*mmap_min_fd, &usize::to_ne_bytes(aligned_last_addr)); + let _ = mmap_min_fd.write(&usize::to_ne_bytes(aligned_last_addr)); } - let addrspace_selection_fd = FdGuard::new(syscall::dup(**thread_fd, b"current-addrspace")?); + let addrspace_selection_fd = thread_fd.dup(b"current-addrspace")?.to_upper()?; - let _ = syscall::write( - *addrspace_selection_fd, - &create_set_addr_space_buf(*grants_fd, header.e_entry as usize, sp), - ); + let _ = addrspace_selection_fd.write(&create_set_addr_space_buf( + grants_fd.as_raw_fd(), + header.e_entry as usize, + sp, + )); Ok(FexecResult::Normal { addrspace_handle: addrspace_selection_fd, }) } -fn write_usizes(fd: &FdGuard, usizes: [usize; N]) -> Result<()> { - let _ = syscall::write(**fd, unsafe { plain::as_bytes(&usizes) }); +fn write_usizes(fd: &FdGuardUpper, usizes: [usize; N]) -> Result<()> { + fd.write(unsafe { plain::as_bytes(&usizes) })?; Ok(()) } fn allocate_remote( - addrspace_fd: &FdGuard, - memory_scheme_fd: &FdGuard, + addrspace_fd: &FdGuardUpper, + memory_scheme_fd: &FdGuardUpper, dst_addr: usize, len: usize, flags: MapFlags, @@ -485,8 +479,8 @@ fn allocate_remote( mmap_remote(addrspace_fd, memory_scheme_fd, 0, dst_addr, len, flags) } pub fn mmap_remote( - addrspace_fd: &FdGuard, - fd: &FdGuard, + addrspace_fd: &FdGuardUpper, + fd: &FdGuardUpper, offset: usize, dst_addr: usize, len: usize, @@ -498,7 +492,7 @@ pub fn mmap_remote( // op syscall::flag::ADDRSPACE_OP_MMAP, // fd - **fd, + fd.as_raw_fd(), // "offset" offset, // address @@ -511,7 +505,7 @@ pub fn mmap_remote( ) } pub fn mprotect_remote( - addrspace_fd: &FdGuard, + addrspace_fd: &FdGuardUpper, addr: usize, len: usize, flags: MapFlags, @@ -530,7 +524,7 @@ pub fn mprotect_remote( ], ) } -pub fn munmap_remote(addrspace_fd: &FdGuard, addr: usize, len: usize) -> Result<()> { +pub fn munmap_remote(addrspace_fd: &FdGuardUpper, addr: usize, len: usize) -> Result<()> { write_usizes( addrspace_fd, [ @@ -544,8 +538,8 @@ pub fn munmap_remote(addrspace_fd: &FdGuard, addr: usize, len: usize) -> Result< ) } pub fn munmap_transfer( - src: &FdGuard, - dst: &FdGuard, + src: &FdGuardUpper, + dst: &FdGuardUpper, src_addr: usize, dst_addr: usize, len: usize, @@ -557,7 +551,7 @@ pub fn munmap_transfer( // op syscall::flag::ADDRSPACE_OP_TRANSFER, // fd - **src, + src.as_raw_fd(), // "offset" (source address) src_addr, // address @@ -569,13 +563,13 @@ pub fn munmap_transfer( ], ) } -fn pread_all(fd: usize, offset: u64, buf: &mut [u8]) -> Result<()> { - syscall::lseek(fd, offset as isize, SEEK_SET)?; +fn pread_all(fd: &FdGuardUpper, offset: u64, buf: &mut [u8]) -> Result<()> { + fd.lseek(offset as isize, SEEK_SET)?; let mut total_bytes_read = 0; while total_bytes_read < buf.len() { - total_bytes_read += match syscall::read(fd, &mut buf[total_bytes_read..])? { + total_bytes_read += match fd.read(&mut buf[total_bytes_read..])? { 0 => return Err(Error::new(ENOEXEC)), bytes_read => bytes_read, } @@ -604,17 +598,18 @@ fn find_free_target_addr(tree: &BTreeMap, size: usize) -> Option { + fd: &'a FdGuardUpper, base: usize, size: usize, } -impl MmapGuard { - pub fn map(fd: usize, map: &Map) -> Result { +impl<'a> MmapGuard<'a> { + pub fn map(fd: &'a FdGuardUpper, map: &Map) -> Result { + let base = unsafe { syscall::fmap(fd.as_raw_fd(), map)? }; Ok(Self { fd, size: map.size, - base: unsafe { syscall::fmap(fd, map)? }, + base, }) } pub fn remap(&mut self, offset: usize, mut flags: MapFlags) -> Result<()> { @@ -623,7 +618,7 @@ impl MmapGuard { let _new_base = unsafe { syscall::fmap( - self.fd, + self.fd.as_raw_fd(), &Map { offset, size: self.size, @@ -635,8 +630,8 @@ impl MmapGuard { Ok(()) } - pub unsafe fn map_mut_anywhere<'a>( - fd: usize, + pub unsafe fn map_mut_anywhere( + fd: &'a FdGuardUpper, offset: usize, size: usize, ) -> Result<(Self, &'a mut [u8])> { @@ -666,7 +661,7 @@ impl MmapGuard { self.size = 0; } } -impl Drop for MmapGuard { +impl<'a> Drop for MmapGuard<'a> { fn drop(&mut self) { if self.size != 0 { let _ = unsafe { syscall::funmap(self.base, self.size) }; @@ -675,14 +670,76 @@ impl Drop for MmapGuard { } #[repr(transparent)] -pub struct FdGuard { +pub struct FdGuard { fd: usize, } -impl FdGuard { +pub type FdGuardUpper = FdGuard; +impl FdGuard { #[inline] - pub const fn new(fd: usize) -> Self { + pub fn new(fd: usize) -> Self { Self { fd } } + + #[inline] + pub fn open>(path: T, flags: usize) -> Result { + syscall::open(path, flags).map(Self::new) + } + + #[inline] + pub fn to_upper(self) -> Result { + // Move to upper table if necessary + let fd = if self.fd & syscall::UPPER_FDTBL_TAG == 0 { + let fd = syscall::fcntl(self.fd, syscall::F_DUPFD, syscall::UPPER_FDTBL_TAG)?; + drop(self); + fd + } else { + self.take() + }; + Ok(FdGuard:: { fd }) + } + + // Not implemented for UPPER to prevent misuse + #[inline] + pub fn as_c_fd(&self) -> Option { + i32::try_from(self.fd).ok() + } +} +impl FdGuard { + #[inline] + pub fn dup(&self, buf: &[u8]) -> Result> { + syscall::dup(self.fd, buf).map(FdGuard::new) + } + + #[inline] + pub fn fcntl(&self, cmd: usize, arg: usize) -> Result { + syscall::fcntl(self.fd, cmd, arg) + } + + #[inline] + pub fn fstat(&self, stat: &mut syscall::Stat) -> Result { + syscall::fstat(self.fd, stat) + } + + #[inline] + pub fn lseek(&self, offset: isize, whence: usize) -> Result { + syscall::lseek(self.fd, offset, whence) + } + + #[inline] + pub fn read(&self, buf: &mut [u8]) -> Result { + syscall::read(self.fd, buf) + } + + #[inline] + pub fn write(&self, buf: &[u8]) -> Result { + syscall::write(self.fd, buf) + } + + #[inline] + pub fn as_raw_fd(&self) -> usize { + self.fd + } + #[inline] pub fn take(self) -> usize { let fd = self.fd; @@ -690,26 +747,22 @@ impl FdGuard { fd } } -impl core::ops::Deref for FdGuard { - type Target = usize; - - #[inline] - fn deref(&self) -> &Self::Target { - &self.fd - } -} - -impl Drop for FdGuard { +impl Drop for FdGuard { #[inline] fn drop(&mut self) { let _ = syscall::close(self.fd); } } -impl Debug for FdGuard { +impl Debug for FdGuard { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { write!(f, "[fd {}]", self.fd) } } +impl Debug for FdGuardUpper { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + write!(f, "[fd upper {}]", self.fd & !syscall::UPPER_FDTBL_TAG) + } +} pub fn create_set_addr_space_buf( space: usize, ip: usize, @@ -741,7 +794,7 @@ pub fn fork_impl(args: &ForkArgs<'_>) -> Result { pub enum ForkArgs<'a> { Init { - this_thr_fd: &'a FdGuard, + this_thr_fd: &'a FdGuardUpper, auth: &'a FdGuard, }, Managed, @@ -767,25 +820,24 @@ pub fn fork_inner(initial_rsp: *mut usize, args: &ForkArgs) -> Result { // Copy existing files into new file table, but do not reuse the same file table (i.e. new // parent FDs will not show up for the child). { - cur_filetable_fd = FdGuard::new(syscall::dup(**cur_thr_fd, b"filetable")?); + cur_filetable_fd = cur_thr_fd.dup(b"filetable")?; // This must be done before the address space is copied. unsafe { - let proc_fd = new_proc_fd.as_ref().map_or(usize::MAX, |p| **p); + let proc_fd = new_proc_fd.as_ref().map_or(usize::MAX, |p| p.as_raw_fd()); //let _ = syscall::write(1, alloc::format!("FDTBL{}PROC{}THR{}\n", *cur_filetable_fd, proc_fd, *new_thr_fd).as_bytes()); - initial_rsp.write(*cur_filetable_fd); + initial_rsp.write(cur_filetable_fd.as_raw_fd()); initial_rsp.add(1).write(proc_fd); - initial_rsp.add(2).write(*new_thr_fd); + initial_rsp.add(2).write(new_thr_fd.as_raw_fd()); } } // CoW-duplicate address space. { - let new_addr_space_sel_fd = - FdGuard::new(syscall::dup(*new_thr_fd, b"current-addrspace")?); + let new_addr_space_sel_fd = new_thr_fd.dup(b"current-addrspace")?; - let cur_addr_space_fd = FdGuard::new(syscall::dup(**cur_thr_fd, b"addrspace")?); - let new_addr_space_fd = FdGuard::new(syscall::dup(*cur_addr_space_fd, b"exclusive")?); + let cur_addr_space_fd = cur_thr_fd.dup(b"addrspace")?.to_upper()?; + let new_addr_space_fd = cur_addr_space_fd.dup(b"exclusive")?.to_upper()?; let mut grant_desc_buf = [GrantDesc::default(); 16]; loop { @@ -796,7 +848,7 @@ pub fn fork_inner(initial_rsp: *mut usize, args: &ForkArgs) -> Result { grant_desc_buf.len() * size_of::(), ) }; - syscall::read(*cur_addr_space_fd, buf)? + cur_addr_space_fd.read(buf)? }; if bytes_read == 0 { break; @@ -828,7 +880,7 @@ pub fn fork_inner(initial_rsp: *mut usize, args: &ForkArgs) -> Result { buf = alloc::format!("grant-fd-{:>08x}", grant.base).into_bytes(); } - let grant_fd = FdGuard::new(syscall::dup(*cur_addr_space_fd, &buf)?); + let grant_fd = cur_addr_space_fd.dup(&buf)?.to_upper()?; let mut flags = MAP_SHARED | MAP_FIXED_NOREPLACE; @@ -848,11 +900,11 @@ pub fn fork_inner(initial_rsp: *mut usize, args: &ForkArgs) -> Result { } let buf = create_set_addr_space_buf( - *new_addr_space_fd, + new_addr_space_fd.as_raw_fd(), __relibc_internal_fork_ret as usize, initial_rsp as usize, ); - let _ = syscall::write(*new_addr_space_sel_fd, &buf)?; + new_addr_space_sel_fd.write(&buf)?; } { // Reuse the same sigaltstack and signal entry (all memory will be re-mapped CoW later). @@ -860,28 +912,33 @@ pub fn fork_inner(initial_rsp: *mut usize, args: &ForkArgs) -> Result { // Do this after the address space is cloned, since the kernel will get a shared // reference to the TCB and whatever pages stores the signal proc control struct. { - let new_sighandler_fd = FdGuard::new(syscall::dup(*new_thr_fd, b"sighandler")?); - let _ = syscall::write( - *new_sighandler_fd, - &crate::signal::current_setsighandler_struct(), - )?; + let new_sighandler_fd = new_thr_fd.dup(b"sighandler")?; + new_sighandler_fd.write(&crate::signal::current_setsighandler_struct())?; } if let Some(ref proc_fd) = new_proc_fd { proc_call( - **proc_fd, + proc_fd.as_raw_fd(), &mut [], CallFlags::empty(), &[ProcCall::SyncSigPctl as u64], )?; thread_call( - *new_thr_fd, + new_thr_fd.as_raw_fd(), &mut [], CallFlags::empty(), &[ThreadCall::SyncSigTctl as u64], )?; } } - copy_env_regs(**cur_thr_fd, *new_thr_fd)?; + { + // Copy environment registers. + let cur_env_regs_fd = cur_thr_fd.dup(b"regs/env")?; + let new_env_regs_fd = new_thr_fd.dup(b"regs/env")?; + + let mut env_regs = syscall::EnvRegisters::default(); + cur_env_regs_fd.read(&mut env_regs)?; + new_env_regs_fd.write(&env_regs)?; + } } // Copy the file table. We do this last to ensure that all previously used file descriptors are // closed. The only exception -- the filetable selection fd and the current filetable fd -- @@ -889,23 +946,20 @@ pub fn fork_inner(initial_rsp: *mut usize, args: &ForkArgs) -> Result { { // TODO: Use file descriptor forwarding or something similar to avoid copying the file // table in the kernel. - let new_filetable_fd = FdGuard::new(syscall::dup(*cur_filetable_fd, b"copy")?); - let new_filetable_sel_fd = FdGuard::new(syscall::dup(*new_thr_fd, b"current-filetable")?); - let _ = syscall::write( - *new_filetable_sel_fd, - &usize::to_ne_bytes(*new_filetable_fd), - )?; + let new_filetable_fd = cur_filetable_fd.dup(b"copy")?; + let new_filetable_sel_fd = new_thr_fd.dup(b"current-filetable")?; + new_filetable_sel_fd.write(&usize::to_ne_bytes(new_filetable_fd.as_raw_fd()))?; } - let start_fd = FdGuard::new(syscall::dup(*new_thr_fd, b"start")?); - let _ = syscall::write(*start_fd, &[0])?; + let start_fd = new_thr_fd.dup(b"start")?; + start_fd.write(&[0])?; Ok(new_pid) } pub struct NewChildProc { - proc_fd: Option, + proc_fd: Option, - thr_fd: FdGuard, + thr_fd: FdGuardUpper, pid: usize, } @@ -918,8 +972,8 @@ pub fn new_child_process(args: &ForkArgs<'_>) -> Result { "cannot use ForkArgs::Managed without an existing proc info" ); let this_proc_fd = unsafe { proc_info.proc_fd.assume_init_ref() }; - let child_proc_fd = FdGuard::new(syscall::dup(**this_proc_fd, b"fork")?); - let only_thread_fd = FdGuard::new(syscall::dup(*child_proc_fd, b"thread-0")?); + let child_proc_fd = this_proc_fd.dup(b"fork")?.to_upper()?; + let only_thread_fd = child_proc_fd.dup(b"thread-0")?.to_upper()?; let meta = read_proc_meta(&child_proc_fd)?; Ok(NewChildProc { proc_fd: Some(child_proc_fd), @@ -932,7 +986,7 @@ pub fn new_child_process(args: &ForkArgs<'_>) -> Result { #[cfg(not(feature = "proc"))] ForkArgs::Init { this_thr_fd, auth } => { - let thr_fd = FdGuard::new(syscall::dup(**auth, b"new-context")?); + let thr_fd = auth.dup(b"new-context")?.to_upper()?; let buf = syscall::ProcSchemeAttrs { pid: 0, euid: 0, @@ -945,11 +999,9 @@ pub fn new_child_process(args: &ForkArgs<'_>) -> Result { buf }, }; - let attr_fd = FdGuard::new(syscall::dup( - *thr_fd, - alloc::format!("auth-{}-attrs", **auth).as_bytes(), - )?); - let _ = syscall::write(*attr_fd, &buf)?; + let attr_fd = + thr_fd.dup(alloc::format!("auth-{}-attrs", auth.as_raw_fd()).as_bytes())?; + attr_fd.write(&buf)?; Ok(NewChildProc { thr_fd, pid: 1, // dummy fd to distinguish child from parent @@ -959,21 +1011,25 @@ pub fn new_child_process(args: &ForkArgs<'_>) -> Result { } } -pub unsafe fn make_init() -> [&'static FdGuard; 2] { +pub unsafe fn make_init() -> (&'static FdGuardUpper, &'static FdGuardUpper) { let proc_fd = FdGuard::new( syscall::open("/scheme/proc/init", syscall::O_CLOEXEC).expect("failed to create init"), - ); + ) + .to_upper() + .unwrap(); syscall::sendfd( - *proc_fd, - syscall::dup(**RtTcb::current().thread_fd(), &[]).unwrap(), + proc_fd.as_raw_fd(), + RtTcb::current().thread_fd().dup(&[]).unwrap().take(), 0, 0, ) .expect("failed to assign current thread to init process"); - let managed_thr_fd = FdGuard::new( - syscall::dup(*proc_fd, b"thread-0").expect("failed to get managed thread for init"), - ); + let managed_thr_fd = proc_fd + .dup(b"thread-0") + .expect("failed to get managed thread for init") + .to_upper() + .unwrap(); let managed_thr_fd = unsafe { (*RtTcb::current().thr_fd.get()).insert(managed_thr_fd) }; @@ -993,10 +1049,10 @@ pub unsafe fn make_init() -> [&'static FdGuard; 2] { egid: 0, sgid: 0, }; - [ + ( unsafe { (*STATIC_PROC_INFO.get()).proc_fd.assume_init_ref() }, managed_thr_fd, - ] + ) } pub(crate) static STATIC_PROC_INFO: SyncUnsafeCell = SyncUnsafeCell::new(StaticProcInfo { diff --git a/redox-rt/src/signal.rs b/redox-rt/src/signal.rs index 853206e045..026ba119b8 100644 --- a/redox-rt/src/signal.rs +++ b/redox-rt/src/signal.rs @@ -9,7 +9,6 @@ use crate::{ RtTcb, Tcb, arch::*, current_proc_fd, - proc::FdGuard, protocol::{ ProcCall, RtSigInfo, SIGCHLD, SIGCONT, SIGKILL, SIGSTOP, SIGTSTP, SIGTTIN, SIGTTOU, SIGURG, SIGWINCH, ThreadCall, @@ -179,7 +178,7 @@ unsafe fn inner(stack: &mut SigStack) { SigactionKind::Default if usize::from(sig) == SIGCONT => SignalHandler { handler: None }, SigactionKind::Default => { let _ = proc_call( - **current_proc_fd(), + current_proc_fd().as_raw_fd(), &mut [], CallFlags::empty(), &[ProcCall::Exit as u64, u64::from(sig) << 8], @@ -647,10 +646,11 @@ pub fn setup_sighandler(tcb: &RtTcb, first_thread: bool) { let data = current_setsighandler_struct(); - let fd = FdGuard::new( - syscall::dup(**tcb.thread_fd(), b"sighandler").expect("failed to open sighandler fd"), - ); - let _ = syscall::write(*fd, &data).expect("failed to write to sighandler fd"); + let fd = tcb + .thread_fd() + .dup(b"sighandler") + .expect("failed to open sighandler fd"); + fd.write(&data).expect("failed to write to sighandler fd"); this_thread_call( &mut [], CallFlags::empty(), @@ -862,7 +862,7 @@ fn try_claim_single(sig_idx: u32, thread_control: Option<&Sigcontrol>) -> Option let mut buf = [0_u8; size_of::()]; buf[..4].copy_from_slice(&(sig_idx - 32).to_ne_bytes()); proc_call( - **static_proc_info().proc_fd.assume_init_ref(), + static_proc_info().proc_fd.assume_init_ref().as_raw_fd(), &mut buf, CallFlags::empty(), &[ProcCall::Sigdeq as u64], diff --git a/redox-rt/src/sys.rs b/redox-rt/src/sys.rs index b1d70a7b91..065080e0c6 100644 --- a/redox-rt/src/sys.rs +++ b/redox-rt/src/sys.rs @@ -146,7 +146,12 @@ pub fn sys_call( } } pub fn this_proc_call(payload: &mut [u8], flags: CallFlags, metadata: &[u64]) -> Result { - proc_call(**crate::current_proc_fd(), payload, flags, metadata) + proc_call( + crate::current_proc_fd().as_raw_fd(), + payload, + flags, + metadata, + ) } pub fn proc_call( proc_fd: usize, @@ -165,7 +170,12 @@ pub fn thread_call( sys_call(thread_fd, payload, flags, metadata) } pub fn this_thread_call(payload: &mut [u8], flags: CallFlags, metadata: &[u64]) -> Result { - thread_call(**RtTcb::current().thread_fd(), payload, flags, metadata) + thread_call( + RtTcb::current().thread_fd().as_raw_fd(), + payload, + flags, + metadata, + ) } #[derive(Clone, Copy, Debug)] diff --git a/redox-rt/src/thread.rs b/redox-rt/src/thread.rs index 0211bb29bd..81d46c31f9 100644 --- a/redox-rt/src/thread.rs +++ b/redox-rt/src/thread.rs @@ -5,36 +5,33 @@ use syscall::Result; use crate::{RtTcb, arch::*, proc::*, signal::tmp_disable_signals, static_proc_info}; /// Spawns a new context sharing the same address space as the current one (i.e. a new thread). -pub unsafe fn rlct_clone_impl(stack: *mut usize) -> Result { +pub unsafe fn rlct_clone_impl(stack: *mut usize) -> Result { let proc_info = static_proc_info(); assert!(proc_info.has_proc_fd); let cur_proc_fd = unsafe { proc_info.proc_fd.assume_init_ref() }; let cur_thr_fd = RtTcb::current().thread_fd(); - let new_thr_fd = FdGuard::new(syscall::dup(**cur_proc_fd, b"new-thread")?); + let new_thr_fd = cur_proc_fd.dup(b"new-thread")?.to_upper().unwrap(); // Inherit existing address space { - let cur_addr_space_fd = FdGuard::new(syscall::dup(**cur_thr_fd, b"addrspace")?); - let new_addr_space_sel_fd = FdGuard::new(syscall::dup(*new_thr_fd, b"current-addrspace")?); + let cur_addr_space_fd = cur_thr_fd.dup(b"addrspace")?; + let new_addr_space_sel_fd = new_thr_fd.dup(b"current-addrspace")?; let buf = create_set_addr_space_buf( - *cur_addr_space_fd, + cur_addr_space_fd.as_raw_fd(), __relibc_internal_rlct_clone_ret as usize, stack as usize, ); - let _ = syscall::write(*new_addr_space_sel_fd, &buf)?; + new_addr_space_sel_fd.write(&buf)?; } // Inherit reference to file table { - let cur_filetable_fd = FdGuard::new(syscall::dup(**cur_thr_fd, b"filetable")?); - let new_filetable_sel_fd = FdGuard::new(syscall::dup(*new_thr_fd, b"current-filetable")?); + let cur_filetable_fd = cur_thr_fd.dup(b"filetable")?; + let new_filetable_sel_fd = new_thr_fd.dup(b"current-filetable")?; - let _ = syscall::write( - *new_filetable_sel_fd, - &usize::to_ne_bytes(*cur_filetable_fd), - )?; + new_filetable_sel_fd.write(&usize::to_ne_bytes(cur_filetable_fd.as_raw_fd()))?; } // Since the signal handler is not yet initialized, signals specifically targeting the thread @@ -43,8 +40,8 @@ pub unsafe fn rlct_clone_impl(stack: *mut usize) -> Result { // until it has initialized its own signal handler. // Unblock context. - let start_fd = FdGuard::new(syscall::dup(*new_thr_fd, b"start")?); - let _ = syscall::write(*start_fd, &[0])?; + let start_fd = new_thr_fd.dup(b"start")?; + start_fd.write(&[0])?; Ok(new_thr_fd) } @@ -54,7 +51,7 @@ pub unsafe fn exit_this_thread(stack_base: *mut (), stack_size: usize) -> ! { let tcb = RtTcb::current(); // TODO: modify interface so it writes directly to the thread fd? - let status_fd = syscall::dup(**tcb.thread_fd(), b"status").unwrap(); + let status_fd = tcb.thread_fd().dup(b"status").unwrap(); let _ = unsafe { syscall::funmap(tcb as *const RtTcb as usize, syscall::PAGE_SIZE) }; @@ -63,6 +60,6 @@ pub unsafe fn exit_this_thread(stack_base: *mut (), stack_size: usize) -> ! { .unwrap() .copy_from_slice(&[usize::MAX, stack_base as usize, stack_size]); // TODO: SYS_CALL w/CONSUME - syscall::write(status_fd, &buf).unwrap(); + status_fd.write(&buf).unwrap(); unreachable!() } diff --git a/src/header/stdlib/mod.rs b/src/header/stdlib/mod.rs index 7ea5a9fd59..3a4dbb1cb7 100644 --- a/src/header/stdlib/mod.rs +++ b/src/header/stdlib/mod.rs @@ -419,7 +419,10 @@ unsafe fn find_env(search: *const c_char) -> Option<(usize, *mut c_char)> { let mut search = search; loop { let end_of_query = *search == 0 || *search == b'=' as c_char; - assert_ne!(*item, 0, "environ has an item without value"); + if *item == 0 { + //TODO: environ has an item without value, is this a problem? + break; + } if *item == b'=' as c_char || end_of_query { if *item == b'=' as c_char && end_of_query { // Both keys env here diff --git a/src/header/sys_ioctl/redox.rs b/src/header/sys_ioctl/redox.rs index a259206011..5cf69b4c5b 100644 --- a/src/header/sys_ioctl/redox.rs +++ b/src/header/sys_ioctl/redox.rs @@ -41,17 +41,13 @@ pub const SIOCATMARK: c_ulong = 0x8905; // TODO: some of the structs passed as T have padding bytes, so casting to a byte slice is UB fn dup_read(fd: c_int, name: &str, t: &mut T) -> syscall::Result { - let dup = syscall::dup(fd as usize, name.as_bytes())?; + let dup = FdGuard::new(syscall::dup(fd as usize, name.as_bytes())?); let size = mem::size_of::(); - let res = syscall::read(dup, unsafe { - slice::from_raw_parts_mut(t as *mut T as *mut u8, size) - }); + let bytes = dup.read(unsafe { slice::from_raw_parts_mut(t as *mut T as *mut u8, size) })?; - let _ = syscall::close(dup); - - res.map(|bytes| bytes / size) + Ok(bytes / size) } // FIXME: unsound @@ -60,11 +56,9 @@ fn dup_write(fd: c_int, name: &str, t: &T) -> Result { let size = mem::size_of::(); - let bytes_written = syscall::write(*dup, unsafe { - slice::from_raw_parts(t as *const T as *const u8, size) - })?; + let bytes = dup.write(unsafe { slice::from_raw_parts(t as *const T as *const u8, size) })?; - Ok(bytes_written / size) + Ok(bytes / size) } unsafe fn ioctl_inner(fd: c_int, request: c_ulong, out: *mut c_void) -> Result { diff --git a/src/ld_so/dso.rs b/src/ld_so/dso.rs index 17fa8b32ad..bb6e4d2ee0 100644 --- a/src/ld_so/dso.rs +++ b/src/ld_so/dso.rs @@ -579,14 +579,14 @@ impl DSO { } } }; - let voff = ph.p_vaddr(endian) % ph.p_align(endian); - let vsize = ((ph.p_memsz(endian) + voff) as usize) + let _voff = ph.p_vaddr(endian) % ph.p_align(endian); + let _vsize = ((ph.p_memsz(endian) + _voff) as usize) .next_multiple_of(ph.p_align(endian) as usize); trace!( " copy {:#x}, {:#x}: {:#x}, {:#x}", - ph.p_vaddr(endian) - voff, - vsize, - voff, + ph.p_vaddr(endian) - _voff, + _vsize, + _voff, obj_data.len() ); mmap_data.copy_from_slice(obj_data); diff --git a/src/ld_so/mod.rs b/src/ld_so/mod.rs index 36665ea9ac..5b20a70798 100644 --- a/src/ld_so/mod.rs +++ b/src/ld_so/mod.rs @@ -40,7 +40,7 @@ static mut STATIC_TCB_MASTER: Master = Master { #[inline(never)] pub fn static_init( sp: &'static Stack, - #[cfg(target_os = "redox")] thr_fd: redox_rt::proc::FdGuard, + #[cfg(target_os = "redox")] thr_fd: redox_rt::proc::FdGuardUpper, ) { const SIZEOF_PHDR64: usize = mem::size_of::>(); const SIZEOF_PHDR32: usize = mem::size_of::>(); @@ -135,7 +135,7 @@ pub fn static_init( #[cfg(any(target_os = "linux", target_os = "redox"))] pub unsafe fn init( sp: &'static Stack, - #[cfg(target_os = "redox")] thr_fd: redox_rt::proc::FdGuard, + #[cfg(target_os = "redox")] thr_fd: redox_rt::proc::FdGuardUpper, ) { let tp: usize; @@ -157,12 +157,13 @@ pub unsafe fn init( { let mut env = syscall::EnvRegisters::default(); - let file = syscall::dup(*thr_fd, b"regs/env") - .expect_notls("failed to open handle for process registers"); + { + let file = thr_fd + .dup(b"regs/env") + .expect_notls("failed to open handle for process registers"); - let _ = syscall::read(file, &mut env).expect_notls("failed to read gsbase"); - - let _ = syscall::close(file); + file.read(&mut env).expect_notls("failed to read gsbase"); + } tp = env.gsbase as usize; } @@ -170,12 +171,13 @@ pub unsafe fn init( { let mut env = syscall::EnvRegisters::default(); - let file = syscall::dup(*thr_fd, b"regs/env") - .expect_notls("failed to open handle for process registers"); + { + let file = thr_fd + .dup(b"regs/env") + .expect_notls("failed to open handle for process registers"); - let _ = syscall::read(file, &mut env).expect_notls("failed to read fsbase"); - - let _ = syscall::close(file); + file.read(&mut env).expect_notls("failed to read fsbase"); + } tp = env.fsbase as usize; } diff --git a/src/ld_so/start.rs b/src/ld_so/start.rs index cb8c4e487f..1cd8f1ea12 100644 --- a/src/ld_so/start.rs +++ b/src/ld_so/start.rs @@ -163,7 +163,9 @@ pub unsafe extern "C" fn relibc_ld_so_start(sp: &'static mut Stack, ld_entry: us let tcb = Tcb::new(0).expect_notls("ld.so: failed to allocate bootstrap TCB"); tcb.activate( #[cfg(target_os = "redox")] - redox_rt::proc::FdGuard::new(thr_fd), + redox_rt::proc::FdGuard::new(thr_fd) + .to_upper() + .expect_notls("failed to move thread fd to upper table"), ); #[cfg(target_os = "redox")] { @@ -173,7 +175,11 @@ pub unsafe extern "C" fn relibc_ld_so_start(sp: &'static mut Stack, ld_entry: us ) .expect_notls("no proc fd present"); - redox_rt::initialize(redox_rt::proc::FdGuard::new(proc_fd)); + redox_rt::initialize( + redox_rt::proc::FdGuard::new(proc_fd) + .to_upper() + .expect_notls("failed to move proc fd to upper table"), + ); redox_rt::signal::setup_sighandler(&tcb.os_specific, true); } } diff --git a/src/ld_so/tcb.rs b/src/ld_so/tcb.rs index f2b537529e..6dbf4cbb39 100644 --- a/src/ld_so/tcb.rs +++ b/src/ld_so/tcb.rs @@ -205,7 +205,10 @@ impl Tcb { } /// Activate TLS - pub unsafe fn activate(&mut self, #[cfg(target_os = "redox")] thr_fd: redox_rt::proc::FdGuard) { + pub unsafe fn activate( + &mut self, + #[cfg(target_os = "redox")] thr_fd: redox_rt::proc::FdGuardUpper, + ) { unsafe { Self::os_arch_activate( &self.os_specific, @@ -345,7 +348,7 @@ impl Tcb { os: &OsSpecific, tls_end: usize, tls_len: usize, - thr_fd: redox_rt::proc::FdGuard, + thr_fd: redox_rt::proc::FdGuardUpper, ) { unsafe { os.thr_fd.get().write(Some(thr_fd)); diff --git a/src/platform/mod.rs b/src/platform/mod.rs index 2a6ed96305..f9f09ea3a1 100644 --- a/src/platform/mod.rs +++ b/src/platform/mod.rs @@ -334,7 +334,7 @@ pub unsafe fn init(auxvs: Box<[[usize; 2]]>) { let Some(proc_fd) = get_auxv(&auxvs, AT_REDOX_PROC_FD) else { panic!("Missing proc and thread fd!"); }; - redox_rt::initialize(FdGuard::new(proc_fd)); + redox_rt::initialize(FdGuard::new(proc_fd).to_upper().unwrap()); // TODO: Is it safe to assume setup_sighandler has been called at this point? redox_rt::sys::this_proc_call( diff --git a/src/platform/redox/exec.rs b/src/platform/redox/exec.rs index a230d760fe..15190ca51a 100644 --- a/src/platform/redox/exec.rs +++ b/src/platform/redox/exec.rs @@ -16,13 +16,13 @@ use crate::{ use redox_rt::{ RtTcb, - proc::{ExtraInfo, FdGuard, FexecResult, InterpOverride}, + proc::{ExtraInfo, FdGuard, FdGuardUpper, FexecResult, InterpOverride}, sys::Resugid, }; use syscall::{data::Stat, error::*, flag::*}; fn fexec_impl( - exec_file: FdGuard, + exec_file: FdGuardUpper, path: &[u8], args: &[&[u8]], envs: &[&[u8]], @@ -30,7 +30,7 @@ fn fexec_impl( extrainfo: &ExtraInfo, interp_override: Option, ) -> Result { - let memory = FdGuard::new(syscall::open("/scheme/memory", 0)?); + let memory = FdGuard::open("/scheme/memory", 0)?.to_upper()?; let addrspace_selection_fd = match redox_rt::proc::fexec_impl( exec_file, @@ -231,8 +231,13 @@ pub fn execve( // scenarios. While execve() is undefined according to POSIX if there exist sibling // threads, it could still be allowed by keeping certain file descriptors and instead // set the active file table. - let files_fd = - File::new(syscall::dup(**RtTcb::current().thread_fd(), b"filetable")? as c_int); + let files_fd = File::new( + c_int::try_from(syscall::dup( + RtTcb::current().thread_fd().as_raw_fd(), + b"filetable", + )?) + .unwrap(), + ); for line in BufReader::new(files_fd).lines() { let line = match line { Ok(l) => l, @@ -252,7 +257,7 @@ pub fn execve( } // TODO: Convert image_file to FdGuard earlier? - let exec_fd_guard = FdGuard::new(image_file.fd as usize); + let exec_fd_guard = FdGuard::new(image_file.fd as usize).to_upper().unwrap(); core::mem::forget(image_file); let sigprocmask = redox_rt::signal::get_sigmask().unwrap(); @@ -263,8 +268,8 @@ pub fn execve( sigignmask: redox_rt::signal::get_sigignmask_to_inherit(), sigprocmask, umask: redox_rt::sys::get_umask(), - thr_fd: **RtTcb::current().thread_fd(), - proc_fd: **redox_rt::current_proc_fd(), + thr_fd: RtTcb::current().thread_fd().as_raw_fd(), + proc_fd: redox_rt::current_proc_fd().as_raw_fd(), }; fexec_impl( exec_fd_guard, diff --git a/src/platform/redox/extra.rs b/src/platform/redox/extra.rs index 977a8170ae..35c74d40aa 100644 --- a/src/platform/redox/extra.rs +++ b/src/platform/redox/extra.rs @@ -22,14 +22,14 @@ pub unsafe extern "C" fn redox_fpath(fd: c_int, buf: *mut c_void, count: size_t) pub fn pipe2(flags: usize) -> syscall::error::Result<[c_int; 2]> { let read_flags = flags | O_RDONLY; let write_flags = flags | O_WRONLY; - let mut read_fd = FdGuard::new(syscall::open("/scheme/pipe", read_flags)?); - let mut write_fd = FdGuard::new(syscall::dup(*read_fd, b"write")?); - syscall::fcntl(*write_fd, F_SETFL, write_flags)?; - syscall::fcntl(*write_fd, F_SETFD, write_flags)?; + let mut read_fd = FdGuard::open("/scheme/pipe", read_flags)?; + let mut write_fd = read_fd.dup(b"write")?; + write_fd.fcntl(F_SETFL, write_flags)?; + write_fd.fcntl(F_SETFD, write_flags)?; let fds = [ - c_int::try_from(*read_fd).map_err(|_| Error::new(EMFILE))?, - c_int::try_from(*write_fd).map_err(|_| Error::new(EMFILE))?, + c_int::try_from(read_fd.as_raw_fd()).map_err(|_| Error::new(EMFILE))?, + c_int::try_from(write_fd.as_raw_fd()).map_err(|_| Error::new(EMFILE))?, ]; read_fd.take(); diff --git a/src/platform/redox/libredox.rs b/src/platform/redox/libredox.rs index f6aebb4795..8e496a2b70 100644 --- a/src/platform/redox/libredox.rs +++ b/src/platform/redox/libredox.rs @@ -146,6 +146,7 @@ pub unsafe extern "C" fn redox_openat_v1( fd, str::from_utf8_unchecked(slice::from_raw_parts(path_base, path_len)), flags as usize, + 0, //TODO: openat fcntl_flags )) } #[unsafe(no_mangle)] @@ -386,12 +387,12 @@ pub unsafe extern "C" fn redox_mkns_v1( // ABI-UNSTABLE #[unsafe(no_mangle)] pub unsafe extern "C" fn redox_cur_procfd_v0() -> usize { - **redox_rt::current_proc_fd() + redox_rt::current_proc_fd().as_raw_fd() } #[unsafe(no_mangle)] pub unsafe extern "C" fn redox_cur_thrfd_v0() -> usize { - **redox_rt::RtTcb::current().thread_fd() + redox_rt::RtTcb::current().thread_fd().as_raw_fd() } #[unsafe(no_mangle)] diff --git a/src/platform/redox/mod.rs b/src/platform/redox/mod.rs index 493a6aa492..63d4eff81c 100644 --- a/src/platform/redox/mod.rs +++ b/src/platform/redox/mod.rs @@ -112,7 +112,7 @@ impl Pal for Sys { let mut stat = syscall::Stat::default(); - syscall::fstat(*fd as usize, &mut stat)?; + fd.fstat(&mut stat)?; let Resugid { ruid, rgid, .. } = redox_rt::sys::posix_getresugid(); @@ -500,8 +500,8 @@ impl Pal for Sys { } //TODO: store fd internally - let fd = FdGuard::new(syscall::open(path, open_flags)?); - Ok(syscall::read(*fd, buf)?) + let fd = FdGuard::open(path, open_flags)?; + Ok(fd.read(buf)?) } fn getresgid( @@ -578,8 +578,8 @@ impl Pal for Sys { fn gettid() -> pid_t { // This is used by pthread mutexes for reentrant checks and must be nonzero // and unique for each thread in the same process (but not cross-process) - Self::current_os_tid() - .thread_fd + let thread_fd = Self::current_os_tid().thread_fd; + (thread_fd & !syscall::UPPER_FDTBL_TAG) .checked_add(1) .unwrap() .try_into() @@ -861,7 +861,7 @@ impl Pal for Sys { } fn current_os_tid() -> crate::pthread::OsTid { crate::pthread::OsTid { - thread_fd: **RtTcb::current().thread_fd(), + thread_fd: RtTcb::current().thread_fd().as_raw_fd(), } } @@ -1070,7 +1070,7 @@ impl Pal for Sys { } let path = format!("/scheme/time/{clock_id}"); - let timerfd = FdGuard::new(libredox::open(&path, O_RDWR, 0)?); + let timerfd = FdGuard::open(&path, syscall::O_RDWR)?; let eventfd = FdGuard::new(Error::demux(unsafe { event::redox_event_queue_create_v1(0) })?); diff --git a/src/platform/redox/path.rs b/src/platform/redox/path.rs index 1cd198f677..2f4d76f92e 100644 --- a/src/platform/redox/path.rs +++ b/src/platform/redox/path.rs @@ -156,7 +156,7 @@ pub fn open(path: &str, flags: usize) -> Result { let resolve_flags = O_CLOEXEC | O_SYMLINK | O_RDONLY; let resolve_fd = FdGuard::new(syscall::open(&*canon, resolve_flags)?); - let bytes_read = syscall::read(*resolve_fd, &mut resolve_buf)?; + let bytes_read = resolve_fd.read(&mut resolve_buf)?; // TODO: make resolve_buf PATH_MAX + 1 bytes? if bytes_read == resolve_buf.len() { return Err(Error::new(ENAMETOOLONG)); diff --git a/src/platform/redox/socket.rs b/src/platform/redox/socket.rs index e5f942e770..ae792d068d 100644 --- a/src/platform/redox/socket.rs +++ b/src/platform/redox/socket.rs @@ -281,7 +281,7 @@ unsafe fn serialize_ancillary_data_to_stream( let fds_slice = slice::from_raw_parts(fds_ptr, fd_count); for &fd in fds_slice.iter() { let fd_to_send = FdGuard::new(syscall::dup(fd as usize, b"")?); - syscall::sendfd(socket as usize, *fd_to_send as usize, 0, 0)?; + syscall::sendfd(socket as usize, fd_to_send.as_raw_fd(), 0, 0)?; } } @@ -562,12 +562,12 @@ impl PalSocket for Sys { )?; let fs_bind_result = (|| -> Result<()> { - let dirfd = FdGuard::new(syscall::open( + let dirfd = FdGuard::open( &dir_path, syscall::O_RDONLY | syscall::O_DIRECTORY | syscall::O_CLOEXEC, - )?); + )?; let fd_to_send = FdGuard::new(syscall::dup(socket as usize, &[])?); - let _ = syscall::sendfd(*dirfd, *fd_to_send, 0, 0)?; + syscall::sendfd(dirfd.as_raw_fd(), fd_to_send.as_raw_fd(), 0, 0)?; Ok(()) })(); @@ -628,14 +628,14 @@ impl PalSocket for Sys { let (_, fd_path) = dir_path_and_fd_path(&path)?; let target_path = format!("/{fd_path}"); - let socket_file_fd = FdGuard::new(syscall::open(&target_path, syscall::O_RDWR)?); + let socket_file_fd = FdGuard::open(&target_path, syscall::O_RDWR)?; const TOKEN_BUF_SIZE: usize = 16; let mut token_buf = [0u8; TOKEN_BUF_SIZE]; redox_rt::sys::sys_call( - *socket_file_fd, + socket_file_fd.as_raw_fd(), &mut token_buf, CallFlags::empty(), &[FsCall::Connect as u64], @@ -809,9 +809,11 @@ impl PalSocket for Sys { Self::read(socket, slice::from_raw_parts_mut(buf as *mut u8, len)) } else { let fd = FdGuard::new(syscall::dup(socket as usize, b"listen")?); - Self::getpeername(*fd as c_int, address, address_len)?; - - Self::read(*fd as c_int, slice::from_raw_parts_mut(buf as *mut u8, len)) + Self::getpeername(fd.as_c_fd().unwrap(), address, address_len)?; + Self::read( + fd.as_c_fd().unwrap(), + slice::from_raw_parts_mut(buf as *mut u8, len), + ) } } @@ -976,7 +978,10 @@ impl PalSocket for Sys { Self::write(socket, slice::from_raw_parts(buf as *const u8, len)) } else { let fd = FdGuard::new(bind_or_connect!(connect copy, socket, dest_addr, dest_len)?); - Self::write(*fd as c_int, slice::from_raw_parts(buf as *const u8, len)) + Self::write( + fd.as_c_fd().unwrap(), + slice::from_raw_parts(buf as *const u8, len), + ) } } @@ -1009,7 +1014,7 @@ impl PalSocket for Sys { tv_nsec, }; - Self::write(*fd as c_int, ×pec)?; + Self::write(fd.as_c_fd().unwrap(), ×pec)?; Ok(()) }; @@ -1078,28 +1083,27 @@ impl PalSocket for Sys { match (domain, kind) { (AF_UNIX, SOCK_STREAM) => { - let listener = FdGuard::new(syscall::open("/scheme/uds_stream", flags | O_CREAT)?); + let listener = FdGuard::open("/scheme/uds_stream", flags | O_CREAT)?; // For now, uds_stream: lets connects be instant, and instead blocks // on any I/O performed. So we don't need to mark this as // nonblocking. - let mut fd0 = FdGuard::new(syscall::dup(*listener, b"connect")?); - - let mut fd1 = FdGuard::new(syscall::dup(*listener, b"listen")?); + let mut fd0 = listener.dup(b"connect")?; + let mut fd1 = listener.dup(b"listen")?; sv[0] = fd0.take() as c_int; sv[1] = fd1.take() as c_int; Ok(()) } (AF_UNIX, SOCK_DGRAM) => { - let listener = FdGuard::new(syscall::open("/scheme/uds_dgram", flags | O_CREAT)?); + let listener = FdGuard::open("/scheme/uds_dgram", flags | O_CREAT)?; // For now, uds_dgram: lets connects be instant, and instead blocks // on any I/O performed. So we don't need to mark this as // nonblocking. - let mut fd0 = FdGuard::new(syscall::dup(*listener, b"connect")?); + let mut fd0 = listener.dup(b"connect")?; sv[0] = fd0.take() as c_int; sv[1] = listener.take() as c_int; diff --git a/src/pthread/mod.rs b/src/pthread/mod.rs index 25e7def7c9..f918a166db 100644 --- a/src/pthread/mod.rs +++ b/src/pthread/mod.rs @@ -229,7 +229,11 @@ unsafe extern "C" fn new_thread_shim( } #[cfg(target_os = "redox")] { - tcb.activate(redox_rt::proc::FdGuard::new(tid.thread_fd)); + tcb.activate( + redox_rt::proc::FdGuard::new(tid.thread_fd) + .to_upper() + .unwrap(), + ); redox_rt::signal::setup_sighandler(&tcb.os_specific, false); } } diff --git a/src/start.rs b/src/start.rs index b42f7f74e4..e576cfab3b 100644 --- a/src/start.rs +++ b/src/start.rs @@ -153,7 +153,9 @@ pub unsafe extern "C" fn relibc_start_v1( let thr_fd = redox_rt::proc::FdGuard::new( crate::platform::get_auxv_raw(sp.auxv().cast(), redox_rt::auxv_defs::AT_REDOX_THR_FD) .expect_notls("no thread fd present"), - ); + ) + .to_upper() + .expect_notls("failed to move thread fd to upper table"); // Initialize TLS, if necessary ld_so::init(