Kernel: Implement static Process::kill()

This allows killing processes even when there does not exist a current
thread.
This commit is contained in:
Bananymous 2024-09-24 13:16:43 +03:00
parent 1b0086217c
commit 348d04f7f5
3 changed files with 22 additions and 10 deletions

View File

@ -171,6 +171,7 @@ namespace Kernel
BAN::ErrorOr<long> sys_tty_ctrl(int fildes, int command, int flags); BAN::ErrorOr<long> sys_tty_ctrl(int fildes, int command, int flags);
static BAN::ErrorOr<void> kill(pid_t pid, int signal);
BAN::ErrorOr<long> sys_kill(pid_t pid, int signal); BAN::ErrorOr<long> sys_kill(pid_t pid, int signal);
BAN::ErrorOr<long> sys_sigaction(int signal, const struct sigaction* act, struct sigaction* oact); BAN::ErrorOr<long> sys_sigaction(int signal, const struct sigaction* act, struct sigaction* oact);
BAN::ErrorOr<long> sys_sigpending(sigset_t* set); BAN::ErrorOr<long> sys_sigpending(sigset_t* set);

View File

@ -1847,20 +1847,13 @@ namespace Kernel
return 0; return 0;
} }
BAN::ErrorOr<long> Process::sys_kill(pid_t pid, int signal) BAN::ErrorOr<void> Process::kill(pid_t pid, int signal)
{ {
if (pid == 0 || pid == -1) if (pid == 0 || pid == -1)
return BAN::Error::from_errno(ENOTSUP); return BAN::Error::from_errno(ENOTSUP);
if (signal != 0 && (signal < _SIGMIN || signal > _SIGMAX)) if (signal != 0 && (signal < _SIGMIN || signal > _SIGMAX))
return BAN::Error::from_errno(EINVAL); return BAN::Error::from_errno(EINVAL);
if (pid == m_pid)
{
if (signal)
add_pending_signal(signal);
return 0;
}
bool found = false; bool found = false;
for_each_process( for_each_process(
[&](Process& process) [&](Process& process)
@ -1880,10 +1873,28 @@ namespace Kernel
); );
if (found) if (found)
return 0; return {};
return BAN::Error::from_errno(ESRCH); return BAN::Error::from_errno(ESRCH);
} }
BAN::ErrorOr<long> 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<long> Process::sys_sigaction(int signal, const struct sigaction* act, struct sigaction* oact) BAN::ErrorOr<long> Process::sys_sigaction(int signal, const struct sigaction* act, struct sigaction* oact)
{ {
if (signal < _SIGMIN || signal > _SIGMAX) if (signal < _SIGMIN || signal > _SIGMAX)

View File

@ -273,7 +273,7 @@ namespace Kernel
// ^C // ^C
if (ch == '\x03') 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()); dwarnln("TTY: {}", ret.error());
return; return;
} }