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 25, 2024
1 parent 3be0dc6 commit e69756a
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 14 deletions.
2 changes: 1 addition & 1 deletion api/ruxos_posix_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ multitask = ["ruxfeat/multitask", "ruxtask/multitask", "dep:ruxfutex"]
fd = ["alloc"]
fs = ["dep:ruxfs", "ruxfeat/fs", "fd"]
net = ["dep:ruxnet", "ruxfeat/net", "fd"]
signal = ["ruxruntime/signal"]
signal = ["ruxruntime/signal", "ruxhal/signal"]
pipe = ["fd"]
select = ["fd"]
epoll = ["fd"]
Expand Down
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
36 changes: 31 additions & 5 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,
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
}
}
18 changes: 15 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,20 @@ 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
/// TODO: send to the specified thread.
pub fn sys_tkill(tid: pid_t, sig: c_int) -> c_int {
debug!("sys_tkill <= tid {} sig {}", tid, sig);
sys_kill(tid, sig)
}
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
1 change: 1 addition & 0 deletions modules/ruxhal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ rtc = []
tls = ["alloc"]
default = []
musl = []
signal = []

[dependencies]
log = "0.4"
Expand Down
4 changes: 4 additions & 0 deletions modules/ruxhal/src/arch/aarch64/trap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ fn handle_sync_exception(tf: &mut TrapFrame) {
);
}
}
#[cfg(feature = "signal")]
{
crate::trap::handle_signal();
}
}

#[no_mangle]
Expand Down
11 changes: 11 additions & 0 deletions modules/ruxhal/src/trap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ pub trait TrapHandler {
fn handle_page_fault(_vaddr: usize, _caus: PageFaultCause) -> bool {
panic!("No handle_page_fault implement");
}
/// Handles signal for every trap.
#[cfg(feature = "signal")]
fn handle_signal() {
panic!("No handle_page_fault implement");
}
}

/// Call the external IRQ handler.
Expand All @@ -65,3 +70,9 @@ pub(crate) fn handle_syscall(syscall_id: usize, args: [usize; 6]) -> isize {
pub(crate) fn handle_page_fault(vaddr: usize, cause: PageFaultCause) -> bool {
call_interface!(TrapHandler::handle_page_fault, vaddr, cause)
}

#[allow(dead_code)]
#[cfg(feature = "signal")]
pub(crate) fn handle_signal() {
call_interface!(TrapHandler::handle_signal)
}
2 changes: 1 addition & 1 deletion modules/ruxruntime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ virtio-9p = ["fs", "rux9p"]
net-9p = ["fs", "rux9p"]
net = ["ruxdriver", "ruxnet"]
display = ["ruxdriver", "ruxdisplay"]
signal = []
signal = ["ruxhal/signal"]

musl = ["dep:ruxfutex"]

Expand Down
2 changes: 1 addition & 1 deletion modules/ruxruntime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,8 +438,8 @@ fn init_interrupt() {
if signal & (1 << signum) != 0
/* TODO: && support mask */
{
Signal::sigaction(signum as u8, None, None);
Signal::signal(signum as i8, false);
Signal::sigaction(signum as u8, None, None);
}
}
}
Expand Down
22 changes: 22 additions & 0 deletions modules/ruxruntime/src/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ use core::{
time::Duration,
};

use crate_interface::impl_interface;
use ruxhal::trap::TrapHandler;

/// sigaction in kernel
#[allow(non_camel_case_types)]
#[allow(dead_code)]
Expand Down Expand Up @@ -62,6 +65,25 @@ static mut SIGNAL_IF: Signal = Signal {
timer_interval: [Duration::from_nanos(0); 3],
};

#[cfg(feature = "signal")]
struct SignalHandler;

#[impl_interface]
impl TrapHandler for SignalHandler {
#[cfg(feature = "signal")]
fn handle_signal() {
let signal = Signal::signal(-1, true).unwrap();
for signum in 0..32 {
if signal & (1 << signum) != 0
/* TODO: && support mask */
{
Signal::signal(signum as i8, false);
Signal::sigaction(signum as u8, None, None);
}
}
}
}

impl Signal {
/// Set signal
/// signum: signal number, if signum < 0, just return current signal
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 e69756a

Please sign in to comment.