From 5e74d173abe41b3d4c5fbcc9218e4a8173e7b67c Mon Sep 17 00:00:00 2001
From: 4lDO2 <4lDO2@protonmail.com>
Date: Fri, 12 Aug 2022 14:55:05 +0200
Subject: [PATCH] Fix ld.so access function on Redox.

---
 src/ld_so/access.rs         |  3 ++-
 src/ld_so/start.rs          |  3 +++
 src/platform/mod.rs         |  4 ++--
 src/sync/reentrant_mutex.rs | 27 +++++++++++++++++++++++++++
 4 files changed, 34 insertions(+), 3 deletions(-)
 create mode 100644 src/sync/reentrant_mutex.rs

diff --git a/src/ld_so/access.rs b/src/ld_so/access.rs
index 15bbd074..c29f7f34 100644
--- a/src/ld_so/access.rs
+++ b/src/ld_so/access.rs
@@ -29,7 +29,7 @@ unsafe fn access(path: *const c_char, mode: c_int) -> c_int {
         Ok(ok) => ok,
         Err(_) => return -1,
     };
-    let fd = match syscall::open(path, syscall::O_CLOEXEC) {
+    let fd = match crate::platform::sys::path::open(path, syscall::O_CLOEXEC) {
         Ok(fd) => fd,
         _ => return -1,
     };
@@ -40,6 +40,7 @@ unsafe fn access(path: *const c_char, mode: c_int) -> c_int {
     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,
diff --git a/src/ld_so/start.rs b/src/ld_so/start.rs
index 56bc0546..4a395ffe 100644
--- a/src/ld_so/start.rs
+++ b/src/ld_so/start.rs
@@ -181,6 +181,9 @@ pub extern "C" fn relibc_ld_so_start(sp: &'static mut Stack, ld_entry: usize) ->
         _r_debug.r_ldbase = ld_entry;
     }
 
+    // TODO: Fix memory leak, although minimal.
+    crate::platform::init(auxv.clone());
+
     // Some variables that will be overridden by environment and auxiliary vectors
     let ld_library_path = envs.get("LD_LIBRARY_PATH").map(|s| s.to_owned());
 
diff --git a/src/platform/mod.rs b/src/platform/mod.rs
index 8e55eeb7..09d4e798 100644
--- a/src/platform/mod.rs
+++ b/src/platform/mod.rs
@@ -21,11 +21,11 @@ pub use self::sys::{e, Sys};
 
 #[cfg(all(not(feature = "no_std"), target_os = "linux"))]
 #[path = "linux/mod.rs"]
-mod sys;
+pub(crate) mod sys;
 
 #[cfg(all(not(feature = "no_std"), target_os = "redox"))]
 #[path = "redox/mod.rs"]
-mod sys;
+pub(crate) mod sys;
 
 #[cfg(test)]
 mod test;
diff --git a/src/sync/reentrant_mutex.rs b/src/sync/reentrant_mutex.rs
new file mode 100644
index 00000000..43e60772
--- /dev/null
+++ b/src/sync/reentrant_mutex.rs
@@ -0,0 +1,27 @@
+use super::{AtomicLock, AttemptStatus};
+
+const WAITING_BIT: u32 = 1 << 31;
+const UNLOCKED: u32 = 0;
+// We now have 2^32 - 1 possible thread ID values
+
+pub struct ReentrantMutex<T> {
+    lock: AtomicLock,
+    content: UnsafeCell<T>,
+}
+unsafe impl<T: Send> Send for ReentrantMutex {}
+unsafe impl<T: Send> Sync for ReentrantMutex {}
+
+impl<T> ReentrantMutex<T> {
+    pub const fn new(context: T) -> Self {
+        Self {
+            lock: AtomicLock::new(UNLOCKED),
+            content: UnsafeCell::new(content),
+        }
+    }
+}
+pub struct ReentrantMutexGuard<'a, T: 'a> {
+    mutex: &'a ReentrantMutex<T>,
+    content: &'a T,
+}
+impl<'a, T> Deref for MutexGuard {
+}
-- 
GitLab