From 57f098931ac0439cbb95b0cc7004b923ed1ccb4d Mon Sep 17 00:00:00 2001
From: 4lDO2 <4lDO2@protonmail.com>
Date: Mon, 30 Sep 2024 16:55:27 +0200
Subject: [PATCH] Remove ld.so's now unnecessary access() wrapper.

---
 src/ld_so/access.rs       | 61 +++------------------------------------
 src/ld_so/linker.rs       |  4 +--
 src/ld_so/start.rs        |  4 +--
 src/platform/redox/mod.rs |  2 +-
 4 files changed, 9 insertions(+), 62 deletions(-)

diff --git a/src/ld_so/access.rs b/src/ld_so/access.rs
index c29f7f341..1d083fe89 100644
--- a/src/ld_so/access.rs
+++ b/src/ld_so/access.rs
@@ -1,67 +1,14 @@
-// Wrapper over the access syscall that doesn't touch errno variable,
-// Do not use outside of ld_so
-
 #[cfg(target_os = "redox")]
 use crate::header::unistd::{F_OK, R_OK, W_OK, X_OK};
 use crate::{
     c_str::{CStr, CString},
-    platform::types::*,
+    error::Errno,
+    platform::{types::*, Pal, Sys},
 };
 
-pub fn accessible(path: &str, mode: c_int) -> c_int {
+pub fn accessible(path: &str, mode: c_int) -> Result<(), Errno> {
     let path_c = CString::new(path.as_bytes()).unwrap(); /*.map_err(|err| {
                                                              Error::Malformed(format!("invalid path '{}': {}", path, err))
                                                          })?;*/
-    unsafe { access(path_c.as_ptr(), mode) }
-}
-
-#[cfg(target_os = "linux")]
-unsafe fn access(path: *const c_char, mode: c_int) -> c_int {
-    let path = CStr::from_ptr(path);
-    syscall!(ACCESS, (path).as_ptr(), mode) as c_int
-}
-
-// Wrapper over the systemcall, Do not use outside of ld_so
-#[cfg(target_os = "redox")]
-unsafe fn access(path: *const c_char, mode: c_int) -> c_int {
-    use core::str;
-    let path = match str::from_utf8(CStr::from_ptr(path).to_bytes()) {
-        Ok(ok) => ok,
-        Err(_) => return -1,
-    };
-    let fd = match crate::platform::sys::path::open(path, syscall::O_CLOEXEC) {
-        Ok(fd) => fd,
-        _ => return -1,
-    };
-    if mode == F_OK {
-        return 0;
-    }
-    let mut stat = syscall::Stat::default();
-    if syscall::fstat(fd, &mut stat).is_err() {
-        return -1;
-    }
-    let _ = syscall::close(fd);
-    let uid = match syscall::getuid() {
-        Ok(uid) => uid,
-        Err(_) => return -1,
-    };
-    let gid = match syscall::getgid() {
-        Ok(gid) => gid,
-        Err(_) => return -1,
-    };
-
-    let perms = if stat.st_uid as usize == uid {
-        stat.st_mode >> (3 * 2 & 0o7)
-    } else if stat.st_gid as usize == gid {
-        stat.st_mode >> (3 * 1 & 0o7)
-    } else {
-        stat.st_mode & 0o7
-    };
-    if (mode & R_OK == R_OK && perms & 0o4 != 0o4)
-        || (mode & W_OK == W_OK && perms & 0o2 != 0o2)
-        || (mode & X_OK == X_OK && perms & 0o1 != 0o1)
-    {
-        return -1;
-    }
-    0
+    unsafe { Sys::access(CStr::from_ptr(path_c.as_ptr()), mode) }
 }
diff --git a/src/ld_so/linker.rs b/src/ld_so/linker.rs
index 983715cbf..b6a90dd08 100644
--- a/src/ld_so/linker.rs
+++ b/src/ld_so/linker.rs
@@ -266,7 +266,7 @@ impl Linker {
         parent_runpath: &Option<String>,
     ) -> Result<String> {
         let mut full_path = name.to_string();
-        if accessible(&full_path, F_OK) == 0 {
+        if accessible(&full_path, F_OK).is_ok() {
             return Ok(full_path);
         } else {
             let mut search_paths = Vec::new();
@@ -280,7 +280,7 @@ impl Linker {
             for part in search_paths.iter() {
                 full_path = format!("{}/{}", part, name);
                 trace!("trying path {}", full_path);
-                if accessible(&full_path, F_OK) == 0 {
+                if accessible(&full_path, F_OK).is_ok() {
                     return Ok(full_path);
                 }
             }
diff --git a/src/ld_so/start.rs b/src/ld_so/start.rs
index d0cf2c942..0d0b0265d 100644
--- a/src/ld_so/start.rs
+++ b/src/ld_so/start.rs
@@ -113,7 +113,7 @@ fn resolve_path_name(
     name_or_path: &str,
     envs: &BTreeMap<String, String>,
 ) -> Option<(String, String)> {
-    if accessible(name_or_path, unistd::F_OK) == 0 {
+    if accessible(name_or_path, unistd::F_OK).is_ok() {
         return Some((
             name_or_path.to_string(),
             name_or_path
@@ -135,7 +135,7 @@ fn resolve_path_name(
         } else {
             format!("{}/{}", part, name_or_path)
         };
-        if accessible(&path, unistd::F_OK) == 0 {
+        if accessible(&path, unistd::F_OK).is_ok() {
             return Some((path.to_string(), name_or_path.to_string()));
         }
     }
diff --git a/src/platform/redox/mod.rs b/src/platform/redox/mod.rs
index bda186275..e186b5de9 100644
--- a/src/platform/redox/mod.rs
+++ b/src/platform/redox/mod.rs
@@ -78,7 +78,7 @@ pub struct Sys;
 
 impl Pal for Sys {
     fn access(path: CStr, mode: c_int) -> Result<()> {
-        let fd = File::open(path, fcntl::O_PATH | fcntl::O_CLOEXEC)?;
+        let fd = FdGuard::new(Sys::open(path, fcntl::O_PATH | fcntl::O_CLOEXEC, 0)? as usize);
 
         if mode == F_OK {
             return Ok(());
-- 
GitLab