From e1bcee5ff5f8e472f87ab61691bc89bf084f9854 Mon Sep 17 00:00:00 2001 From: Baye Dieng Date: Sat, 12 Feb 2022 01:01:20 +0000 Subject: [PATCH] Implement kill system call --- Documentation/compatibility.md | 2 +- kernel/syscalls/kill.rs | 25 +++++++++++++++++++++++++ kernel/syscalls/mod.rs | 3 +++ libs/kerla_utils/bitmap_allocator.rs | 2 +- 4 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 kernel/syscalls/kill.rs diff --git a/Documentation/compatibility.md b/Documentation/compatibility.md index 219b3171..721edf73 100644 --- a/Documentation/compatibility.md +++ b/Documentation/compatibility.md @@ -82,7 +82,7 @@ Not supported. | 59 | execve | Partially | `v0.0.1` | | | 60 | exit | Partially | `v0.0.1` | | | 61 | wait4 | Partially | `v0.0.1` | | -| 62 | kill | Unimplemented | | | +| 62 | kill | Partially | next release | | | 63 | uname | Partially | `v0.0.1` | | | 64 | semget | Unimplemented | | | | 65 | semop | Unimplemented | | | diff --git a/kernel/syscalls/kill.rs b/kernel/syscalls/kill.rs new file mode 100644 index 00000000..dc62a9b5 --- /dev/null +++ b/kernel/syscalls/kill.rs @@ -0,0 +1,25 @@ +use crate::process::{current_process, signal::Signal, PId, Process}; +use crate::result::Errno; +use crate::result::Result; +use crate::syscalls::SyscallHandler; + +impl<'a> SyscallHandler<'a> { + pub fn sys_kill(&self, pid: PId, sig: Signal) -> Result { + let pid_int = pid.as_i32(); + match pid_int { + pid_int if pid_int > 0 => match Process::find_by_pid(pid) { + Some(proc) => proc.send_signal(sig), + None => return Err(Errno::ESRCH.into()), + }, + 0 => current_process().process_group().lock().signal(sig), + -1 => { + // TODO: check for permissions once linux capabilities is implemented + current_process().send_signal(sig); + } + pid_int if pid_int < -1 => current_process().process_group().lock().signal(sig), + _ => (), + } + + Ok(0) + } +} diff --git a/kernel/syscalls/mod.rs b/kernel/syscalls/mod.rs index a5b78629..9cf019bb 100644 --- a/kernel/syscalls/mod.rs +++ b/kernel/syscalls/mod.rs @@ -44,6 +44,7 @@ pub(self) mod getsockname; pub(self) mod getsockopt; pub(self) mod gettid; pub(self) mod ioctl; +pub(self) mod kill; pub(self) mod link; pub(self) mod linkat; pub(self) mod listen; @@ -138,6 +139,7 @@ const SYS_FORK: usize = 57; const SYS_EXECVE: usize = 59; const SYS_EXIT: usize = 60; const SYS_WAIT4: usize = 61; +const SYS_KILL: usize = 62; const SYS_UNAME: usize = 63; const SYS_FCNTL: usize = 72; const SYS_FSYNC: usize = 74; @@ -319,6 +321,7 @@ impl<'a> SyscallHandler<'a> { bitflags_from_user!(WaitOptions, a3 as c_int)?, UserVAddr::new(a4), ), + SYS_KILL => self.sys_kill(PId::new(a1 as i32), a2 as c_int), SYS_EXIT => self.sys_exit(a1 as i32), SYS_EXIT_GROUP => self.sys_exit_group(a1 as i32), SYS_SOCKET => self.sys_socket(a1 as i32, a2 as i32, a3 as i32), diff --git a/libs/kerla_utils/bitmap_allocator.rs b/libs/kerla_utils/bitmap_allocator.rs index a026582f..08ec2cd8 100644 --- a/libs/kerla_utils/bitmap_allocator.rs +++ b/libs/kerla_utils/bitmap_allocator.rs @@ -1,4 +1,4 @@ -use bitvec::{prelude::*, index::BitMask}; +use bitvec::{index::BitMask, prelude::*}; use crate::alignment::align_up;