Skip to content

Commit

Permalink
improve code of Linux signal
Browse files Browse the repository at this point in the history
  • Loading branch information
wangrunji0408 committed Aug 16, 2020
1 parent f181c71 commit cbff620
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 194 deletions.
4 changes: 1 addition & 3 deletions linux-object/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ spin = "0.5"
xmas-elf = "0.7"
bitflags = "1.2"
hashbrown = "0.7"
numeric-enum-macro = "0.2"
zircon-object = { path = "../zircon-object", features = ["elf"] }
kernel-hal = { path = "../kernel-hal" }
downcast-rs = { git = "https://github.com/rcore-os/downcast-rs", rev = "a632ce1", default-features = false }
Expand All @@ -23,6 +24,3 @@ rcore-fs-sfs = { git = "https://github.com/rcore-os/rcore-fs", rev = "517af47" }
rcore-fs-ramfs = { git = "https://github.com/rcore-os/rcore-fs", rev = "517af47" }
rcore-fs-mountfs = { git = "https://github.com/rcore-os/rcore-fs", rev = "517af47" }
rcore-fs-devfs = { git = "https://github.com/rcore-os/rcore-fs", rev = "517af47" }
num = { version = "0.2.1", default-features = false }
num-traits = { version = "0.2.11", default-features = false }
num-derive = "0.3"
11 changes: 1 addition & 10 deletions linux-object/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
//! Linux kernel objects
#![no_std]
#![deny(
warnings,
unsafe_code,
unused_must_use,
unreachable_patterns,
missing_docs
)]
#![deny(warnings, unsafe_code)]
#![feature(bool_to_option)]

extern crate alloc;

#[macro_use]
extern crate log;

#[macro_use]
extern crate num_derive;

// layer 0
pub mod error;

Expand Down
42 changes: 24 additions & 18 deletions linux-object/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use crate::error::*;
use crate::fs::*;
use crate::signal::{Signal as LSignal, SignalAction};
use crate::signal::{Signal as LinuxSignal, SignalAction};
use alloc::vec::Vec;
use alloc::{
boxed::Box,
Expand Down Expand Up @@ -47,19 +47,16 @@ impl ProcessExt for Process {
fn fork_from(parent: &Arc<Self>, vfork: bool) -> ZxResult<Arc<Self>> {
let linux_parent = parent.linux();
let mut linux_parent_inner = linux_parent.inner.lock();
let linux_parent_sinner = linux_parent.sinner.lock();
let new_linux_proc = LinuxProcess {
root_inode: linux_parent.root_inode.clone(),
parent: Arc::downgrade(parent),
inner: Mutex::new(LinuxProcessInner {
execute_path: linux_parent_inner.execute_path.clone(),
current_working_directory: linux_parent_inner.current_working_directory.clone(),
files: linux_parent_inner.files.clone(),
signal_actions: linux_parent_inner.signal_actions.clone(),
..Default::default()
}),
sinner: Mutex::new(LinuxProcessSignalInner {
dispositions: linux_parent_sinner.dispositions,
}),
};
let new_proc = Process::create_with_ext(&parent.job(), "", new_linux_proc)?;
linux_parent_inner
Expand Down Expand Up @@ -137,8 +134,6 @@ pub struct LinuxProcess {
parent: Weak<Process>,
/// Inner
inner: Mutex<LinuxProcessInner>,
/// Signal inner
sinner: Mutex<LinuxProcessSignalInner>,
}

/// Linux process mut inner data
Expand All @@ -158,12 +153,21 @@ struct LinuxProcessInner {
futexes: HashMap<VirtAddr, Arc<Futex>>,
/// Child processes
children: HashMap<KoID, Arc<Process>>,
/// Signal actions
signal_actions: SignalActions,
}

/// Linux process signal inner data
pub struct LinuxProcessSignalInner {
/// signal actions
pub dispositions: [SignalAction; LSignal::RTMAX + 1],
#[derive(Clone)]
struct SignalActions {
table: [SignalAction; LinuxSignal::RTMAX + 1],
}

impl Default for SignalActions {
fn default() -> Self {
Self {
table: [SignalAction::default(); LinuxSignal::RTMAX + 1],
}
}
}

/// resource limit
Expand All @@ -185,7 +189,7 @@ impl Default for RLimit {
}
}

/// process exit code defination
/// The type of process exit code.
pub type ExitCode = i32;

impl LinuxProcess {
Expand Down Expand Up @@ -223,9 +227,6 @@ impl LinuxProcess {
files,
..Default::default()
}),
sinner: Mutex::new(LinuxProcessSignalInner {
dispositions: [SignalAction::default(); LSignal::RTMAX + 1],
}),
}
}

Expand Down Expand Up @@ -351,9 +352,14 @@ impl LinuxProcess {
self.inner.lock().execute_path = String::from(path);
}

/// Get signal inner
pub fn signal_inner(&self) -> MutexGuard<LinuxProcessSignalInner> {
self.sinner.lock()
/// Get signal action.
pub fn signal_action(&self, signal: LinuxSignal) -> SignalAction {
self.inner.lock().signal_actions.table[signal as u8 as usize]
}

/// Set signal action.
pub fn set_signal_action(&self, signal: LinuxSignal, action: SignalAction) {
self.inner.lock().signal_actions.table[signal as u8 as usize] = action;
}
}

Expand Down
57 changes: 24 additions & 33 deletions linux-object/src/signal/action.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,10 @@
use crate::signal::Signal;
use bitflags::_core::fmt::Debug;
use bitflags::*;
use core::fmt::Formatter;

pub const SIG_ERR: usize = usize::max_value() - 1;
pub const SIG_DFL: usize = 0;
pub const SIG_IGN: usize = 1;

pub const SI_ASYNCNL: i32 = -60;
pub const SI_TKILL: i32 = -6;
pub const SI_SIGIO: i32 = -5;
pub const SI_ASYNCIO: i32 = -4;
pub const SI_MESGQ: i32 = -3;
pub const SI_TIMER: i32 = -2;
pub const SI_QUEUE: i32 = -1;
pub const SI_USER: i32 = 0;
/// from user
pub const SI_KERNEL: i32 = 128;
/// from kernel
// yet there's a bug because of mismatching bits: https://sourceware.org/bugzilla/show_bug.cgi?id=25657
// just support 64bits size sigset
/// Linux struct sigset_t
Expand All @@ -30,15 +16,13 @@ impl Sigset {
pub fn empty() -> Self {
Sigset(0)
}

pub fn contains(&self, sig: Signal) -> bool {
(self.0 >> sig as u64 & 1) != 0
}

pub fn add(&mut self, sig: Signal) {
pub fn insert(&mut self, sig: Signal) {
self.0 |= 1 << sig as u64;
}
pub fn add_set(&mut self, sigset: &Sigset) {
pub fn insert_set(&mut self, sigset: &Sigset) {
self.0 |= sigset.0;
}
pub fn remove(&mut self, sig: Signal) {
Expand All @@ -51,25 +35,14 @@ impl Sigset {

/// Linux struct sigaction
#[repr(C)]
#[derive(Clone, Copy, Default)]
#[derive(Debug, Clone, Copy, Default)]
pub struct SignalAction {
pub handler: usize, // this field may be an union
pub flags: usize,
pub flags: SignalActionFlags,
pub restorer: usize,
pub mask: Sigset,
}

impl Debug for SignalAction {
fn fmt(&self, f: &mut Formatter) -> Result<(), core::fmt::Error> {
f.debug_struct("signal action")
.field("handler", &self.handler)
.field("mask", &self.mask)
.field("flags", &SignalActionFlags::from_bits_truncate(self.flags))
.field("restorer", &self.restorer)
.finish()
}
}

#[repr(C)]
#[derive(Copy, Clone)]
pub union SiginfoFields {
Expand All @@ -91,14 +64,32 @@ impl Default for SiginfoFields {

#[repr(C)]
#[derive(Copy, Clone)]
pub struct Siginfo {
pub struct SigInfo {
pub signo: i32,
pub errno: i32,
pub code: i32,
pub code: SignalCode,
pub field: SiginfoFields,
}

/// A code identifying the cause of the signal.
#[repr(i32)]
#[derive(Debug, Copy, Clone)]
pub enum SignalCode {
ASYNCNL = -60,
TKILL = -6,
SIGIO = -5,
ASYNCIO = -4,
MESGQ = -3,
TIMER = -2,
QUEUE = -1,
/// from user
USER = 0,
/// from kernel
KERNEL = 128,
}

bitflags! {
#[derive(Default)]
pub struct SignalActionFlags : usize {
const NOCLDSTOP = 1;
const NOCLDWAIT = 2;
Expand Down
18 changes: 8 additions & 10 deletions linux-object/src/signal/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use bitflags::*;
use numeric_enum_macro::numeric_enum;

mod action;

pub use self::action::*;

/*
* from rCore/kernel/src/arch/x86_64/signal.rs
*/
/// struct mcontext
#[repr(C)]
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -44,11 +42,10 @@ pub struct MachineContext {
// reserved
pub _reserved1: [usize; 8],
}
/*
* end
*/

#[derive(Eq, PartialEq, FromPrimitive, Debug, Copy, Clone)]
numeric_enum! {
#[repr(u8)]
#[derive(Eq, PartialEq, Debug, Copy, Clone)]
pub enum Signal {
SIGHUP = 1,
SIGINT = 2,
Expand Down Expand Up @@ -116,6 +113,7 @@ pub enum Signal {
SIGRT63 = 63,
SIGRT64 = 64,
}
}

impl Signal {
pub const RTMIN: usize = 32;
Expand All @@ -142,7 +140,7 @@ pub struct SignalUserContext {
#[derive(Clone)]
pub struct SignalFrame {
pub ret_code_addr: usize, // point to ret_code
pub info: Siginfo,
pub info: SigInfo,
pub ucontext: SignalUserContext, // adapt interface, a little bit waste
pub ret_code: [u8; 7], // call sys_sigreturn
}
Expand All @@ -160,7 +158,7 @@ bitflags! {
#[derive(Copy, Clone, Debug)]
pub struct SignalStack {
pub sp: usize,
pub flags: u32,
pub flags: SignalStackFlags,
pub size: usize,
}

Expand All @@ -169,7 +167,7 @@ impl Default for SignalStack {
// default to disabled
SignalStack {
sp: 0,
flags: SignalStackFlags::DISABLE.bits,
flags: SignalStackFlags::DISABLE,
size: 0,
}
}
Expand Down
17 changes: 3 additions & 14 deletions linux-object/src/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ impl ThreadExt for Thread {
fn create_linux(proc: &Arc<Process>) -> ZxResult<Arc<Self>> {
let linux_thread = Mutex::new(LinuxThread {
clear_child_tid: 0.into(),
sinner: Default::default(),
signal_mask: Sigset::default(),
signal_alternate_stack: SignalStack::default(),
});
Thread::create_with_ext(proc, "", linux_thread)
}
Expand Down Expand Up @@ -70,20 +71,8 @@ pub struct LinuxThread {
/// Kernel performs futex wake when thread exits.
/// Ref: [http://man7.org/linux/man-pages/man2/set_tid_address.2.html]
clear_child_tid: UserOutPtr<i32>,
/// Signal inner
sinner: Mutex<LinuxThreadSignalInner>,
}

impl LinuxThread {
pub fn signal_inner(&self) -> MutexGuard<LinuxThreadSignalInner> {
self.sinner.lock()
}
}

#[derive(Default)]
pub struct LinuxThreadSignalInner {
/// Signal mask
pub sig_mask: Sigset,
pub signal_mask: Sigset,
/// signal alternate stack
pub signal_alternate_stack: SignalStack,
}
3 changes: 0 additions & 3 deletions linux-syscall/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,3 @@ linux-object = { path = "../linux-object" }
kernel-hal = { path = "../kernel-hal" }
rcore-fs = { git = "https://github.com/rcore-os/rcore-fs", rev = "517af47" }
lazy_static = { version = "1.4", features = ["spin_no_std"] }
num = { version = "0.2.1", default-features = false }
num-traits = { version = "0.2.11", default-features = false }
num-derive = "0.3"
12 changes: 2 additions & 10 deletions linux-syscall/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,7 @@
//!
#![no_std]
#![deny(
warnings,
unsafe_code,
unused_must_use,
unreachable_patterns,
missing_docs
)]
#![deny(warnings, unsafe_code, missing_docs)]
#![feature(bool_to_option)]

#[macro_use]
Expand All @@ -34,8 +28,6 @@ extern crate alloc;
#[macro_use]
extern crate log;

extern crate num_derive;

use {
self::consts::SyscallType as Sys,
alloc::sync::Arc,
Expand Down Expand Up @@ -145,7 +137,7 @@ impl Syscall<'_> {

// signal
Sys::RT_SIGACTION => self.sys_rt_sigaction(a0, a1.into(), a2.into(), a3),
Sys::RT_SIGPROCMASK => self.sys_rt_sigprocmask(a0, a1.into(), a2.into(), a3),
Sys::RT_SIGPROCMASK => self.sys_rt_sigprocmask(a0 as _, a1.into(), a2.into(), a3),
// Sys::RT_SIGRETURN => self.sys_rt_sigreturn(),
Sys::SIGALTSTACK => self.sys_sigaltstack(a0.into(), a1.into()),
// Sys::KILL => self.sys_kill(a0, a1),
Expand Down
Loading

0 comments on commit cbff620

Please sign in to comment.