提交 ad28f5f6 编写于 作者: chyyuu1972's avatar chyyuu1972

update signal impl and apps

上级 a6b278fe
...@@ -34,8 +34,8 @@ pub fn syscall(syscall_id: usize, args: [usize; 3]) -> isize { ...@@ -34,8 +34,8 @@ pub fn syscall(syscall_id: usize, args: [usize; 3]) -> isize {
SYSCALL_WRITE => sys_write(args[0], args[1] as *const u8, args[2]), SYSCALL_WRITE => sys_write(args[0], args[1] as *const u8, args[2]),
SYSCALL_EXIT => sys_exit(args[0] as i32), SYSCALL_EXIT => sys_exit(args[0] as i32),
SYSCALL_YIELD => sys_yield(), SYSCALL_YIELD => sys_yield(),
SYSCALL_KILL => sys_kill(args[0], args[1] as u32), SYSCALL_KILL => sys_kill(args[0], args[1] as i32),
SYSCALL_SIGACTION => sys_sigaction(args[0], args[1] as *const SignalAction, args[2] as *mut SignalAction), SYSCALL_SIGACTION => sys_sigaction(args[0] as i32, args[1] as *const SignalAction, args[2] as *mut SignalAction),
SYSCALL_SIGPROCMASK => sys_sigprocmask(args[0] as u32), SYSCALL_SIGPROCMASK => sys_sigprocmask(args[0] as u32),
SYSCALL_SIGRETURN => sys_sigretrun(), SYSCALL_SIGRETURN => sys_sigretrun(),
SYSCALL_GET_TIME => sys_get_time(), SYSCALL_GET_TIME => sys_get_time(),
......
...@@ -104,9 +104,9 @@ pub fn sys_waitpid(pid: isize, exit_code_ptr: *mut i32) -> isize { ...@@ -104,9 +104,9 @@ pub fn sys_waitpid(pid: isize, exit_code_ptr: *mut i32) -> isize {
// ---- release current PCB automatically // ---- release current PCB automatically
} }
pub fn sys_kill(pid: usize, signal: u32) -> isize { pub fn sys_kill(pid: usize, signum: i32) -> isize {
if let Some(task) = pid2task(pid) { if let Some(task) = pid2task(pid) {
if let Some(flag) = SignalFlags::from_bits(signal) { if let Some(flag) = SignalFlags::from_bits(1 << signum) {
// insert the signal if legal // insert the signal if legal
let mut task_ref = task.inner_exclusive_access(); let mut task_ref = task.inner_exclusive_access();
if task_ref.signals.contains(flag) { if task_ref.signals.contains(flag) {
...@@ -159,7 +159,7 @@ fn check_sigaction_error(signal: SignalFlags, action: usize, old_action: usize) ...@@ -159,7 +159,7 @@ fn check_sigaction_error(signal: SignalFlags, action: usize, old_action: usize)
} }
} }
pub fn sys_sigaction(signum: usize, action: *const SignalAction, old_action: *mut SignalAction) -> isize { pub fn sys_sigaction(signum: i32, action: *const SignalAction, old_action: *mut SignalAction) -> isize {
let token = current_user_token(); let token = current_user_token();
if let Some(task) = current_task() { if let Some(task) = current_task() {
let mut inner = task.inner_exclusive_access(); let mut inner = task.inner_exclusive_access();
...@@ -170,7 +170,7 @@ pub fn sys_sigaction(signum: usize, action: *const SignalAction, old_action: *mu ...@@ -170,7 +170,7 @@ pub fn sys_sigaction(signum: usize, action: *const SignalAction, old_action: *mu
if check_sigaction_error(flag, action as usize, old_action as usize) { if check_sigaction_error(flag, action as usize, old_action as usize) {
return -1; return -1;
} }
let old_kernel_action = inner.signal_actions.table[signum]; let old_kernel_action = inner.signal_actions.table[signum as usize];
if old_kernel_action.mask != SignalFlags::from_bits(40).unwrap() { if old_kernel_action.mask != SignalFlags::from_bits(40).unwrap() {
*translated_refmut(token, old_action) = old_kernel_action; *translated_refmut(token, old_action) = old_kernel_action;
} else { } else {
...@@ -178,7 +178,7 @@ pub fn sys_sigaction(signum: usize, action: *const SignalAction, old_action: *mu ...@@ -178,7 +178,7 @@ pub fn sys_sigaction(signum: usize, action: *const SignalAction, old_action: *mu
ref_old_action.handler = old_kernel_action.handler; ref_old_action.handler = old_kernel_action.handler;
} }
let ref_action = translated_ref(token, action); let ref_action = translated_ref(token, action);
inner.signal_actions.table[signum] = *ref_action; inner.signal_actions.table[signum as usize] = *ref_action;
return 0; return 0;
} }
} }
......
#![no_std]
#![no_main]
#[macro_use]
extern crate user_lib;
// use user_lib::{sigaction, sigprocmask, SignalAction, SignalFlags, fork, exit, wait, kill, getpid, sleep, sigreturn};
use user_lib::*;
fn func() {
println!("user_sig_test succsess");
sigreturn();
}
#[no_mangle]
pub fn main() -> i32 {
let mut new = SignalAction::default();
let old = SignalAction::default();
new.handler = func as usize;
println!("signal_simple: sigaction");
if sigaction(SIGUSR1, &new, &old) < 0 {
panic!("Sigaction failed!");
}
println!("signal_simple: kill");
if kill(getpid() as usize, SIGUSR1) < 0 {
println!("Kill failed!");
exit(1);
}
println!("signal_simple: Done");
0
}
#![no_std]
#![no_main]
#[macro_use]
extern crate user_lib;
use user_lib::{sigaction, sigprocmask, SignalAction, SignalFlags, fork, exit, waitpid, kill, getpid, sleep, sigreturn};
fn func() {
println!("user_sig_test succsess");
sigreturn();
}
#[no_mangle]
pub fn main() -> i32 {
let pid = fork();
if pid==0{
let mut new = SignalAction::default();
let old = SignalAction::default();
new.handler = func as usize;
println!("signal_simple2: child sigaction");
if sigaction(10, &new, &old) < 0 {
panic!("Sigaction failed!");
}
sleep(1000);
println!("signal_simple2: child done");
exit(0);
} else if pid >0 {
println!("signal_simple2: parent kill child");
sleep(500);
if kill(pid as usize, 1<<10) < 0 {
println!("Kill failed!");
exit(1);
}
println!("signal_simple2: parent wait child");
let mut exit_code = 0;
waitpid(pid as usize, &mut exit_code);
println!("signal_simple2: parent Done");
exit(0);
}
0
}
...@@ -4,7 +4,8 @@ ...@@ -4,7 +4,8 @@
#[macro_use] #[macro_use]
extern crate user_lib; extern crate user_lib;
use user_lib::{sigaction, sigprocmask, SignalAction, SignalFlags, fork, exit, wait, kill, getpid, sleep, sigreturn}; // use user_lib::{sigaction, sigprocmask, SignalAction, SignalFlags, fork, exit, wait, kill, getpid, sleep, sigreturn};
use user_lib::*;
fn func() { fn func() {
println!("user_sig_test succsess"); println!("user_sig_test succsess");
...@@ -36,10 +37,10 @@ fn user_sig_test_kill() { ...@@ -36,10 +37,10 @@ fn user_sig_test_kill() {
let old = SignalAction::default(); let old = SignalAction::default();
new.handler = func as usize; new.handler = func as usize;
if sigaction(10, &new, &old) < 0 { if sigaction(SIGUSR1, &new, &old) < 0 {
panic!("Sigaction failed!"); panic!("Sigaction failed!");
} }
if kill(getpid() as usize, 1 << 10) < 0 { if kill(getpid() as usize, SIGUSR1) < 0 {
println!("Kill failed!"); println!("Kill failed!");
exit(1); exit(1);
} }
...@@ -51,11 +52,11 @@ fn user_sig_test_multiprocsignals() { ...@@ -51,11 +52,11 @@ fn user_sig_test_multiprocsignals() {
let mut new = SignalAction::default(); let mut new = SignalAction::default();
let old = SignalAction::default(); let old = SignalAction::default();
new.handler = func as usize; new.handler = func as usize;
if sigaction(10, &new, &old) < 0 { if sigaction(SIGUSR1, &new, &old) < 0 {
panic!("Sigaction failed!"); panic!("Sigaction failed!");
} }
} else { } else {
if kill(pid as usize, 1 << 10) < 0 { if kill(pid as usize, SIGUSR1) < 0 {
println!("Kill failed!"); println!("Kill failed!");
exit(1); exit(1);
} }
...@@ -70,11 +71,11 @@ fn user_sig_test_restore() { ...@@ -70,11 +71,11 @@ fn user_sig_test_restore() {
let old2 = SignalAction::default(); let old2 = SignalAction::default();
new.handler = func as usize; new.handler = func as usize;
if sigaction(10, &new, &old) < 0 { if sigaction(SIGUSR1, &new, &old) < 0 {
panic!("Sigaction failed!"); panic!("Sigaction failed!");
} }
if sigaction(10, &old, &old2) < 0 { if sigaction(SIGUSR1, &old, &old2) < 0 {
panic!("Sigaction failed!"); panic!("Sigaction failed!");
} }
...@@ -135,13 +136,13 @@ fn final_sig_test() { ...@@ -135,13 +136,13 @@ fn final_sig_test() {
let pid= fork(); let pid= fork();
if pid == 0{ if pid == 0{
if sigaction(10, &new, &old) < 0 { if sigaction(SIGUSR1, &new, &old) < 0 {
panic!("Sigaction failed!"); panic!("Sigaction failed!");
} }
if sigaction(14, &new2, &old2) < 0 { if sigaction(14, &new2, &old2) < 0 {
panic!("Sigaction failed!"); panic!("Sigaction failed!");
} }
if kill(getpid() as usize, 1 << 10) < 0 { if kill(getpid() as usize, SIGUSR1) < 0 {
println!("Kill failed!"); println!("Kill failed!");
exit(-1); exit(-1);
} }
......
...@@ -156,6 +156,39 @@ impl Default for SignalAction { ...@@ -156,6 +156,39 @@ impl Default for SignalAction {
} }
} }
pub const SIGDEF :i32 = 1; // Default signal handling
pub const SIGHUP :i32 = 1;
pub const SIGINT :i32 = 2;
pub const SIGQUIT :i32 = 3;
pub const SIGILL :i32 = 4;
pub const SIGTRAP :i32 = 5;
pub const SIGABRT :i32 = 6;
pub const SIGBUS :i32 = 7;
pub const SIGFPE :i32 = 8;
pub const SIGKILL :i32 = 9;
pub const SIGUSR1 :i32 = 10;
pub const SIGSEGV :i32 = 11;
pub const SIGUSR2 :i32 = 12;
pub const SIGPIPE :i32 = 13;
pub const SIGALRM :i32 = 14;
pub const SIGTERM :i32 = 15;
pub const SIGSTKFLT :i32 = 16;
pub const SIGCHLD :i32 = 17;
pub const SIGCONT :i32 = 18;
pub const SIGSTOP :i32 = 19;
pub const SIGTSTP :i32 = 20;
pub const SIGTTIN :i32 = 21;
pub const SIGTTOU :i32 = 22;
pub const SIGURG :i32 = 23;
pub const SIGXCPU :i32 = 24;
pub const SIGXFSZ :i32 = 25;
pub const SIGVTALRM :i32 = 26;
pub const SIGPROF :i32 = 27;
pub const SIGWINCH :i32 = 28;
pub const SIGIO :i32 = 29;
pub const SIGPWR :i32 = 30;
pub const SIGSYS :i32 = 31;
bitflags! { bitflags! {
pub struct SignalFlags: i32 { pub struct SignalFlags: i32 {
const SIGDEF = 1; // Default signal handling const SIGDEF = 1; // Default signal handling
...@@ -197,7 +230,7 @@ pub fn kill(pid: usize, signal: i32) -> isize { ...@@ -197,7 +230,7 @@ pub fn kill(pid: usize, signal: i32) -> isize {
sys_kill(pid, signal) sys_kill(pid, signal)
} }
pub fn sigaction(signum: usize, action: *const SignalAction, old_action: *const SignalAction) -> isize { pub fn sigaction(signum: i32, action: *const SignalAction, old_action: *const SignalAction) -> isize {
sys_sigaction(signum, action, old_action) sys_sigaction(signum, action, old_action)
} }
......
...@@ -97,8 +97,8 @@ pub fn sys_waitpid(pid: isize, exit_code: *mut i32) -> isize { ...@@ -97,8 +97,8 @@ pub fn sys_waitpid(pid: isize, exit_code: *mut i32) -> isize {
syscall(SYSCALL_WAITPID, [pid as usize, exit_code as usize, 0]) syscall(SYSCALL_WAITPID, [pid as usize, exit_code as usize, 0])
} }
pub fn sys_sigaction(signum: usize, action: *const SignalAction, old_action: *const SignalAction) -> isize { pub fn sys_sigaction(signum: i32, action: *const SignalAction, old_action: *const SignalAction) -> isize {
syscall(SYSCALL_SIGACTION, [signum, action as usize, old_action as usize]) syscall(SYSCALL_SIGACTION, [signum as usize , action as usize, old_action as usize])
} }
pub fn sys_sigprocmask(mask: u32) -> isize { pub fn sys_sigprocmask(mask: u32) -> isize {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册