From 3db425fef6e527394b0ffec94320fb0835df11a1 Mon Sep 17 00:00:00 2001
From: 4lDO2 <4lDO2@protonmail.com>
Date: Mon, 24 Jun 2024 10:29:51 +0200
Subject: [PATCH] Correctly find signal number and check action.

---
 redox-rt/src/arch/x86_64.rs | 14 ++++++++------
 redox-rt/src/signal.rs      | 25 +++++++++++++++++++++----
 2 files changed, 29 insertions(+), 10 deletions(-)

diff --git a/redox-rt/src/arch/x86_64.rs b/redox-rt/src/arch/x86_64.rs
index 7349809f..2b54b5c5 100644
--- a/redox-rt/src/arch/x86_64.rs
+++ b/redox-rt/src/arch/x86_64.rs
@@ -136,18 +136,20 @@ asmfunction!(__relibc_internal_sigentry: ["
 
     // Read first signal word
     mov rax, fs:[{tcb_sc_off} + {sc_word}]
-    mov rcx, rax
-    shr rcx, 32
-    and eax, ecx
+    mov rdx, rax
+    shr rdx, 32
+    not edx
+    and eax, edx
     and eax, {SIGW0_PENDING_MASK}
     bsf eax, eax
     jnz 2f
 
     // Read second signal word
     mov rax, fs:[{tcb_sc_off} + {sc_word} + 8]
-    mov rcx, rax
-    shr rcx, 32
-    and eax, ecx
+    mov rdx, rax
+    shr rdx, 32
+    not edx
+    and eax, edx
     and eax, {SIGW1_PENDING_MASK}
     bsf eax, eax
     jnz 7f
diff --git a/redox-rt/src/signal.rs b/redox-rt/src/signal.rs
index 2ec1e76c..368620ce 100644
--- a/redox-rt/src/signal.rs
+++ b/redox-rt/src/signal.rs
@@ -43,15 +43,30 @@ pub struct SigStack {
 unsafe fn inner(stack: &mut SigStack) {
     // TODO: Set procmask based on SA_NODEFER, SA_RESETHAND, and sa_mask.
 
+    // asm counts from 0
+    stack.sig_num += 1;
+
+    let sigaction = SIGACTIONS.lock()[stack.sig_num];
+
+    let handler = match sigaction.kind {
+        SigactionKind::Ignore => unreachable!(),
+        SigactionKind::Default => {
+            syscall::exit(stack.sig_num << 8);
+            unreachable!();
+        }
+        SigactionKind::Handled { handler } => handler,
+    };
+
     // Re-enable signals again.
     let control_flags = &Tcb::current().unwrap().os_specific.control.control_flags;
     control_flags.store(control_flags.load(Ordering::Relaxed) & !SigcontrolFlags::INHIBIT_DELIVERY.bits(), Ordering::Release);
     core::sync::atomic::compiler_fence(Ordering::Acquire);
 
-    let _ = syscall::write(1, b"INNER SIGNAL HANDLER\n");
-    /*loop {}
-    let handler: extern "C" fn(c_int) = core::mem::transmute(stack.sa_handler);
-    handler(stack.sig_num as c_int)*/
+    if sigaction.flags.contains(SigactionFlags::SIGINFO) && let Some(sigaction) = handler.sigaction {
+        sigaction(stack.sig_num as c_int, core::ptr::null_mut(), core::ptr::null_mut());
+    } else if let Some(handler) = handler.handler {
+        handler(stack.sig_num as c_int);
+    }
 }
 #[cfg(not(target_arch = "x86"))]
 pub(crate) unsafe extern "C" fn inner_c(stack: usize) {
@@ -265,7 +280,9 @@ struct TheDefault {
     ignmask: u64,
 }
 
+// indexed directly by signal number
 static SIGACTIONS: Mutex<[Sigaction; 64]> = Mutex::new([Sigaction { flags: SigactionFlags::empty(), mask: 0, kind: SigactionKind::Default }; 64]);
+
 static IGNMASK: AtomicU64 = AtomicU64::new(sig_bit(SIGCHLD) | sig_bit(SIGURG) | sig_bit(SIGWINCH));
 
 static PROC_CONTROL_STRUCT: SigProcControl = SigProcControl {
-- 
GitLab