Skip to content

Commit

Permalink
add implementation for rt_sigaction, sys_kill and sys_tkill
Browse files Browse the repository at this point in the history
  • Loading branch information
thesayol committed Jul 3, 2024
1 parent 910aa53 commit 430c44a
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 9 deletions.
3 changes: 2 additions & 1 deletion api/ruxos_posix_api/src/imp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ pub mod getrandom;
pub mod io;
pub mod prctl;
pub mod resources;
pub mod rt_sig;
pub mod stat;
pub mod sys;
pub mod task;
Expand All @@ -39,6 +38,8 @@ pub mod pipe;
#[cfg(feature = "multitask")]
pub mod pthread;
#[cfg(feature = "signal")]
pub mod rt_sig;
#[cfg(feature = "signal")]
pub mod signal;

/// Invalid syscall
Expand Down
32 changes: 29 additions & 3 deletions api/ruxos_posix_api/src/imp/rt_sig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
use axerrno::LinuxError;

use crate::ctypes;
use crate::{
ctypes::{self, k_sigaction},
sys_sigaction,
};
use core::{
ffi::c_int,
sync::atomic::{AtomicUsize, Ordering},
Expand Down Expand Up @@ -84,12 +87,35 @@ pub fn sys_rt_sigprocmask(
}

/// sigaction syscall for A64 musl
pub fn sys_rt_sigaction(
pub unsafe fn sys_rt_sigaction(
sig: c_int,
_sa: *const ctypes::sigaction,
_old: *mut ctypes::sigaction,
_sigsetsize: ctypes::size_t,
) -> c_int {
debug!("sys_rt_sigaction <= sig: {}", sig);
syscall_body!(sys_rt_sigaction, Ok(0))
syscall_body!(sys_rt_sigaction, {
let _sa = unsafe { *_sa };
let _old = unsafe { *_old };
let sa = k_sigaction::from(_sa);
let mut old_sa = k_sigaction::from(_old);
sys_sigaction(sig as _, Some(&sa), Some(&mut old_sa));
Ok(0)
})
}

impl From<ctypes::sigaction> for k_sigaction {
fn from(sa: ctypes::sigaction) -> Self {
let mut ret = Self {
..Default::default()
};
ret.flags = sa.sa_flags as _;
let mask = sa.sa_mask.__bits[0]; // only get the first 64 signals
ret.mask[0] = mask as _;
ret.mask[1] = (mask >> 32) as _;

ret.handler = unsafe { sa.__sa_handler.sa_handler };
ret.restorer = sa.sa_restorer;
ret
}
}
22 changes: 19 additions & 3 deletions api/ruxos_posix_api/src/imp/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,24 @@ pub unsafe fn sys_sigaltstack(
syscall_body!(sys_sigaltstack, Ok(0))
}

/// TODO: send a signal to a process
pub unsafe fn sys_kill(pid: pid_t, sig: c_int) -> c_int {
/// send a signal to a process
pub fn sys_kill(pid: pid_t, sig: c_int) -> c_int {
debug!("sys_kill <= pid {} sig {}", pid, sig);
syscall_body!(sys_kill, Ok(0))
syscall_body!(sys_kill, {
match Signal::signal(sig as _, true) {
None => Err(LinuxError::EINVAL),
Some(_) => Ok(0),
}
})
}

/// send a signal to a thread
pub fn sys_tkill(tid: pid_t, sig: c_int) -> c_int {
debug!("sys_tkill <= tid {} sig {}", tid, sig);
syscall_body!(sys_kill, {
match Signal::signal(sig as _, true) {
None => Err(LinuxError::EINVAL),
Some(_) => Ok(0),
}
})
}
7 changes: 5 additions & 2 deletions api/ruxos_posix_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ pub use imp::getrandom::{sys_getrandom, sys_rand, sys_random, sys_srand};
pub use imp::io::{sys_read, sys_readv, sys_write, sys_writev};
pub use imp::prctl::{sys_arch_prctl, sys_prctl};
pub use imp::resources::{sys_getrlimit, sys_prlimit64, sys_setrlimit};
pub use imp::rt_sig::{sys_rt_sigaction, sys_rt_sigprocmask};
pub use imp::stat::{
sys_getegid, sys_geteuid, sys_getgid, sys_getpgid, sys_getuid, sys_setgid, sys_setpgid,
sys_setuid, sys_umask,
Expand Down Expand Up @@ -107,7 +106,11 @@ pub use imp::pthread::{
sys_pthread_setspecific,
};
#[cfg(feature = "signal")]
pub use imp::signal::{sys_getitimer, sys_kill, sys_setitimer, sys_sigaction, sys_sigaltstack};
pub use imp::rt_sig::{sys_rt_sigaction, sys_rt_sigprocmask};
#[cfg(feature = "signal")]
pub use imp::signal::{
sys_getitimer, sys_kill, sys_setitimer, sys_sigaction, sys_sigaltstack, sys_tkill,
};

#[cfg(feature = "multitask")]
pub use imp::pthread::futex::sys_futex;
Expand Down
2 changes: 2 additions & 0 deletions ulib/ruxmusl/src/aarch64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ pub fn syscall(syscall_id: SyscallId, args: [usize; 6]) -> isize {
#[cfg(feature = "signal")]
SyscallId::KILL => ruxos_posix_api::sys_kill(args[0] as pid_t, args[1] as c_int) as _,
#[cfg(feature = "signal")]
SyscallId::TKILL => ruxos_posix_api::sys_tkill(args[0] as pid_t, args[1] as c_int) as _,
#[cfg(feature = "signal")]
SyscallId::SIGALTSTACK => ruxos_posix_api::sys_sigaltstack(
args[0] as *const core::ffi::c_void,
args[1] as *mut core::ffi::c_void,
Expand Down
2 changes: 2 additions & 0 deletions ulib/ruxmusl/src/aarch64/syscall_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ pub enum SyscallId {
#[cfg(feature = "signal")]
KILL = 129,
#[cfg(feature = "signal")]
TKILL = 130,
#[cfg(feature = "signal")]
SIGALTSTACK = 132,
#[cfg(feature = "signal")]
RT_SIGACTION = 134,
Expand Down

0 comments on commit 430c44a

Please sign in to comment.