2
0
Эх сурвалжийг харах

sigemptyset and sigaddset

jD91mZM2 6 жил өмнө
parent
commit
892ce75eb4

+ 1 - 0
Cargo.lock

@@ -424,6 +424,7 @@ name = "signal"
 version = "0.1.0"
 dependencies = [
  "cbindgen 0.5.2",
+ "errno 0.1.0",
  "platform 0.1.0",
 ]
 

+ 1 - 2
src/platform/src/types.rs

@@ -172,8 +172,7 @@ pub struct sigaction {
     pub sa_mask: sigset_t
 }
 
-const NSIG: usize = 64;
-
+pub const NSIG: usize = 64;
 pub type sigset_t = [c_ulong; NSIG / (8 * mem::size_of::<c_ulong>())];
 
 const UTSLENGTH: usize = 65;

+ 1 - 0
src/signal/Cargo.toml

@@ -8,4 +8,5 @@ build = "build.rs"
 cbindgen = { path = "../../cbindgen" }
 
 [dependencies]
+errno = { path = "../errno" }
 platform = { path = "../platform" }

+ 18 - 8
src/signal/src/lib.rs

@@ -3,7 +3,7 @@
 #![no_std]
 #![feature(asm, const_fn, core_intrinsics, global_asm)]
 
-#[macro_use]
+extern crate errno;
 extern crate platform;
 
 #[cfg(target_os = "linux")]
@@ -30,8 +30,6 @@ pub struct sigaction {
     pub sa_mask: sigset_t
 }
 
-const NSIG: usize = 64;
-
 pub use sys::*;
 
 use core::{mem, ptr};
@@ -65,9 +63,18 @@ pub unsafe extern "C" fn sigaction(sig: c_int, act: *const sigaction, oact: *mut
     platform::sigaction(sig, ptr, oact as *mut platform::types::sigaction)
 }
 
-// #[no_mangle]
-pub extern "C" fn sigaddset(set: *mut sigset_t, signo: c_int) -> c_int {
-    unimplemented!();
+#[no_mangle]
+pub extern "C" fn sigaddset(set: *mut sigset_t, mut signo: c_int) -> c_int {
+    if signo <= 0 || signo as usize > NSIG {
+        platform::errno = errno::EINVAL;
+        return -1;
+    }
+
+    let signo = signo as usize - 1; // 0-indexed usize, please!
+
+    let bits_each = 8 * mem::size_of::<sigset_t>();
+    (unsafe { *set })[signo / bits_each] = 1 << (signo & (bits_each - 1));
+    0
 }
 
 // #[no_mangle]
@@ -75,9 +82,12 @@ pub extern "C" fn sigdelset(set: *mut sigset_t, signo: c_int) -> c_int {
     unimplemented!();
 }
 
-// #[no_mangle]
+#[no_mangle]
 pub extern "C" fn sigemptyset(set: *mut sigset_t) -> c_int {
-    unimplemented!();
+    for i in unsafe { &mut (*set) } {
+        *i = 0;
+    }
+    0
 }
 
 #[no_mangle]