From 348d04f7f57dbbb4e759724ed9fc3ddd90ad2f69 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Tue, 24 Sep 2024 13:16:43 +0300 Subject: [PATCH] Kernel: Implement static Process::kill() This allows killing processes even when there does not exist a current thread. --- kernel/include/kernel/Process.h | 1 + kernel/kernel/Process.cpp | 29 ++++++++++++++++++++--------- kernel/kernel/Terminal/TTY.cpp | 2 +- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/kernel/include/kernel/Process.h b/kernel/include/kernel/Process.h index d3a9b3d992..ecab369335 100644 --- a/kernel/include/kernel/Process.h +++ b/kernel/include/kernel/Process.h @@ -171,6 +171,7 @@ namespace Kernel BAN::ErrorOr sys_tty_ctrl(int fildes, int command, int flags); + static BAN::ErrorOr kill(pid_t pid, int signal); BAN::ErrorOr sys_kill(pid_t pid, int signal); BAN::ErrorOr sys_sigaction(int signal, const struct sigaction* act, struct sigaction* oact); BAN::ErrorOr sys_sigpending(sigset_t* set); diff --git a/kernel/kernel/Process.cpp b/kernel/kernel/Process.cpp index 9e61f08907..b9e72c9b56 100644 --- a/kernel/kernel/Process.cpp +++ b/kernel/kernel/Process.cpp @@ -1847,20 +1847,13 @@ namespace Kernel return 0; } - BAN::ErrorOr Process::sys_kill(pid_t pid, int signal) + BAN::ErrorOr Process::kill(pid_t pid, int signal) { if (pid == 0 || pid == -1) return BAN::Error::from_errno(ENOTSUP); if (signal != 0 && (signal < _SIGMIN || signal > _SIGMAX)) return BAN::Error::from_errno(EINVAL); - if (pid == m_pid) - { - if (signal) - add_pending_signal(signal); - return 0; - } - bool found = false; for_each_process( [&](Process& process) @@ -1880,10 +1873,28 @@ namespace Kernel ); if (found) - return 0; + return {}; return BAN::Error::from_errno(ESRCH); } + BAN::ErrorOr Process::sys_kill(pid_t pid, int signal) + { + if (pid == 0 || pid == -1) + return BAN::Error::from_errno(ENOTSUP); + if (signal != 0 && (signal < _SIGMIN || signal > _SIGMAX)) + return BAN::Error::from_errno(EINVAL); + + if (pid == m_pid) + { + if (signal) + add_pending_signal(signal); + return 0; + } + + TRY(kill(pid, signal)); + return 0; + } + BAN::ErrorOr Process::sys_sigaction(int signal, const struct sigaction* act, struct sigaction* oact) { if (signal < _SIGMIN || signal > _SIGMAX) diff --git a/kernel/kernel/Terminal/TTY.cpp b/kernel/kernel/Terminal/TTY.cpp index f2d339da96..8cf7e18d4f 100644 --- a/kernel/kernel/Terminal/TTY.cpp +++ b/kernel/kernel/Terminal/TTY.cpp @@ -273,7 +273,7 @@ namespace Kernel // ^C if (ch == '\x03') { - if (auto ret = Process::current().sys_kill(-m_foreground_pgrp, SIGINT); ret.is_error()) + if (auto ret = Process::kill(-m_foreground_pgrp, SIGINT); ret.is_error()) dwarnln("TTY: {}", ret.error()); return; }