From 10169d773dbca24595c7ff028543e4ce6f0191fe Mon Sep 17 00:00:00 2001 From: Bananymous Date: Fri, 21 Jul 2023 19:27:38 +0300 Subject: [PATCH] Kernel/LibC: Add SYS_KILL/kill() --- kernel/include/kernel/Process.h | 2 +- kernel/kernel/Process.cpp | 25 +++++++++++++++++++++++++ kernel/kernel/Syscall.cpp | 3 +++ libc/include/sys/syscall.h | 1 + libc/signal.cpp | 5 +++++ libc/unistd.cpp | 7 +++++++ userspace/Shell/main.cpp | 21 +++++++++++++++++++-- 7 files changed, 61 insertions(+), 3 deletions(-) diff --git a/kernel/include/kernel/Process.h b/kernel/include/kernel/Process.h index 2dd291a757..9f2ae3a8aa 100644 --- a/kernel/include/kernel/Process.h +++ b/kernel/include/kernel/Process.h @@ -101,7 +101,7 @@ namespace Kernel BAN::ErrorOr sys_free(void*); BAN::ErrorOr sys_signal(int, void (*)(int)); - BAN::ErrorOr sys_kill(pid_t pid, int signal); + BAN::ErrorOr sys_kill(pid_t pid, int signal, uintptr_t& return_rsp, uintptr_t& return_rip); BAN::ErrorOr sys_raise(int signal, uintptr_t& return_rsp, uintptr_t& return_rip); BAN::ErrorOr sys_termid(char*) const; diff --git a/kernel/kernel/Process.cpp b/kernel/kernel/Process.cpp index ace5a20c46..b346ca7eb4 100644 --- a/kernel/kernel/Process.cpp +++ b/kernel/kernel/Process.cpp @@ -789,6 +789,31 @@ namespace Kernel return 0; } + BAN::ErrorOr Process::sys_kill(pid_t pid, int signal, uintptr_t& return_rsp, uintptr_t& return_rip) + { + if (pid <= 0) + return BAN::Error::from_errno(ENOTSUP); + if (signal != 0 && (signal < _SIGMIN || signal > _SIGMAX)) + return BAN::Error::from_errno(EINVAL); + + if (pid == this->pid()) + return sys_raise(signal, return_rsp, return_rip); + + LockGuard process_guard(s_process_lock); + CriticalScope _; + for (auto* process : s_processes) + { + if (process->pid() == pid) + { + if (signal) + process->m_threads.front()->m_signal_queue.push(signal); + return 0; + } + } + + return BAN::Error::from_errno(ESRCH); + } + BAN::ErrorOr Process::sys_raise(int signal, uintptr_t& return_rsp, uintptr_t& return_rip) { if (signal < _SIGMIN || signal > _SIGMAX) diff --git a/kernel/kernel/Syscall.cpp b/kernel/kernel/Syscall.cpp index 1171a87d08..9b4db9b60a 100644 --- a/kernel/kernel/Syscall.cpp +++ b/kernel/kernel/Syscall.cpp @@ -143,6 +143,9 @@ namespace Kernel case SYS_RAISE: ret = Process::current().sys_raise((int)arg1, interrupt_stack.rsp, interrupt_stack.rip); break; + case SYS_KILL: + ret = Process::current().sys_kill((pid_t)arg1, (int)arg2, interrupt_stack.rsp, interrupt_stack.rip); + break; default: dwarnln("Unknown syscall {}", syscall); break; diff --git a/libc/include/sys/syscall.h b/libc/include/sys/syscall.h index 677f694e6c..3ce74f298d 100644 --- a/libc/include/sys/syscall.h +++ b/libc/include/sys/syscall.h @@ -42,6 +42,7 @@ __BEGIN_DECLS #define SYS_PIPE 35 #define SYS_DUP2 36 #define SYS_RAISE 37 +#define SYS_KILL 38 __END_DECLS diff --git a/libc/signal.cpp b/libc/signal.cpp index b15dfad6fc..93ce5e728e 100644 --- a/libc/signal.cpp +++ b/libc/signal.cpp @@ -5,4 +5,9 @@ int raise(int sig) { return syscall(SYS_RAISE, sig); +} + +int kill(pid_t pid, int sig) +{ + return syscall(SYS_KILL, pid, sig); } \ No newline at end of file diff --git a/libc/unistd.cpp b/libc/unistd.cpp index 1b15a22505..f4c2e8c1a2 100644 --- a/libc/unistd.cpp +++ b/libc/unistd.cpp @@ -272,6 +272,13 @@ long syscall(long syscall, ...) ret = Kernel::syscall(SYS_RAISE, signal); break; } + case SYS_KILL: + { + pid_t pid = va_arg(args, pid_t); + int signal = va_arg(args, int); + ret = Kernel::syscall(SYS_KILL, pid, signal); + break; + } default: puts("LibC: Unhandeled syscall"); ret = -ENOSYS; diff --git a/userspace/Shell/main.cpp b/userspace/Shell/main.cpp index 4d119d550a..5309c29123 100644 --- a/userspace/Shell/main.cpp +++ b/userspace/Shell/main.cpp @@ -260,9 +260,26 @@ int execute_command(BAN::Vector& args) while (*current) printf("%s\n", *current++); } - else if (args.front() == "raise"sv) + else if (args.front() == "kill-test"sv) { - raise(SIGSEGV); + pid_t pid = fork(); + if (pid == 0) + { + printf("child\n"); + for (;;); + } + if (pid == -1) + { + perror("fork"); + return 1; + } + + sleep(1); + if (kill(pid, SIGSEGV) == -1) + { + perror("kill"); + return 1; + } } else if (args.front() == "cd"sv) {