Kernel/LibC: Add SYS_KILL/kill()

This commit is contained in:
Bananymous 2023-07-21 19:27:38 +03:00
parent b78596dcf4
commit 10169d773d
7 changed files with 61 additions and 3 deletions

View File

@ -101,7 +101,7 @@ namespace Kernel
BAN::ErrorOr<long> sys_free(void*);
BAN::ErrorOr<long> sys_signal(int, void (*)(int));
BAN::ErrorOr<long> sys_kill(pid_t pid, int signal);
BAN::ErrorOr<long> sys_kill(pid_t pid, int signal, uintptr_t& return_rsp, uintptr_t& return_rip);
BAN::ErrorOr<long> sys_raise(int signal, uintptr_t& return_rsp, uintptr_t& return_rip);
BAN::ErrorOr<long> sys_termid(char*) const;

View File

@ -789,6 +789,31 @@ namespace Kernel
return 0;
}
BAN::ErrorOr<long> 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<long> Process::sys_raise(int signal, uintptr_t& return_rsp, uintptr_t& return_rip)
{
if (signal < _SIGMIN || signal > _SIGMAX)

View File

@ -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;

View File

@ -42,6 +42,7 @@ __BEGIN_DECLS
#define SYS_PIPE 35
#define SYS_DUP2 36
#define SYS_RAISE 37
#define SYS_KILL 38
__END_DECLS

View File

@ -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);
}

View File

@ -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;

View File

@ -260,9 +260,26 @@ int execute_command(BAN::Vector<BAN::String>& 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)
{