From 85cbdc05c1ec6e3e893f087f3f054c8c63bca0b5 Mon Sep 17 00:00:00 2001 From: Matheus Date: Mon, 9 Sep 2024 18:03:49 -0300 Subject: [PATCH] syscalls: killthread --- kernel-src/arch/x86-64/syscall.asm | 4 +- kernel-src/arch/x86-64/syscalllog.c | 8 ++-- kernel-src/sys/syscalls/killthread.c | 58 ++++++++++++++++++++++++++ patches/mlibc/jinx-working-patch.patch | 14 +++++-- 4 files changed, 76 insertions(+), 8 deletions(-) create mode 100644 kernel-src/sys/syscalls/killthread.c diff --git a/kernel-src/arch/x86-64/syscall.asm b/kernel-src/arch/x86-64/syscall.asm index 794c9a9e..e3e0e3f0 100644 --- a/kernel-src/arch/x86-64/syscall.asm +++ b/kernel-src/arch/x86-64/syscall.asm @@ -90,6 +90,7 @@ extern syscall_setegid extern syscall_sigsuspend extern syscall_sigtimedwait extern syscall_sigpending +extern syscall_killthread syscalltab: dq syscall_print dq syscall_mmap @@ -182,7 +183,8 @@ dq syscall_setegid dq syscall_sigsuspend dq syscall_sigtimedwait dq syscall_sigpending -syscallcount equ 91 +dq syscall_killthread +syscallcount equ 92 section .text global arch_syscall_entry ; on entry: diff --git a/kernel-src/arch/x86-64/syscalllog.c b/kernel-src/arch/x86-64/syscalllog.c index d7814836..014590de 100644 --- a/kernel-src/arch/x86-64/syscalllog.c +++ b/kernel-src/arch/x86-64/syscalllog.c @@ -5,7 +5,7 @@ #ifdef SYSCALL_LOGGING -#define SYSCALL_COUNT 91 +#define SYSCALL_COUNT 92 #define LOGSTR(x) arch_e9_puts(x) static char *name[] = { @@ -99,7 +99,8 @@ static char *name[] = { "setegid", "sigsuspend", "sigtimedwait", - "sigpending" + "sigpending", + "killthread" }; static char *args[] = { @@ -193,7 +194,8 @@ static char *args[] = { "egid %d", // setegid "sigset %p", // sigsuspend "sigset %p info %p, timespec %p", // sigtimedwait - "sigset %p\n" // sigpending + "sigset %p\n", // sigpending + "pid %d tid %d signal %d" // killthread }; #endif diff --git a/kernel-src/sys/syscalls/killthread.c b/kernel-src/sys/syscalls/killthread.c new file mode 100644 index 00000000..543ae135 --- /dev/null +++ b/kernel-src/sys/syscalls/killthread.c @@ -0,0 +1,58 @@ +#include +#include +#include +#include +#include + +syscallret_t syscall_killthread(context_t *context, int pid, int tid, int signal) { + syscallret_t ret = { + .ret = -1 + }; + + if (pid < 1 || tid < 1 || signal < 0 || signal > NSIG) { + ret.errno = EINVAL; + return ret; + } + + proc_t *sender = _cpu()->thread->proc; + proc_t *target = sched_getprocfrompid(pid); + if (target == NULL) { + ret.errno = ESRCH; + return ret; + } + + // permission check + if (signal != SIGCONT || jobctl_getpgid(target) != jobctl_getpgid(sender)) { + ret.errno = auth_process_check(&sender->cred, AUTH_ACTIONS_PROCESS_SIGNAL, target); + if (ret.errno) + goto leave; + } + + bool ok = false; + bool intstatus = spinlock_acquireirqclear(&target->threadlistlock); + + thread_t *thread = target->threadlist; + while (thread) { + if (thread->tid == tid) { + signal_signalthread(thread, signal, false); + ok = true; + break; + } + + thread = thread->procnext; + } + + spinlock_releaseirqrestore(&target->threadlistlock, intstatus); + + if (ok == false) { + ret.errno = ESRCH; + ret.ret = -1; + } else { + ret.errno = 0; + ret.ret = 0; + } + + leave: + PROC_RELEASE(target); + return ret; +} diff --git a/patches/mlibc/jinx-working-patch.patch b/patches/mlibc/jinx-working-patch.patch index 065bc873..d20dae72 100644 --- a/patches/mlibc/jinx-working-patch.patch +++ b/patches/mlibc/jinx-working-patch.patch @@ -221,10 +221,10 @@ index 0000000..e5b087a + diff --git mlibc-workdir/sysdeps/astral/generic/generic.cpp mlibc-workdir/sysdeps/astral/generic/generic.cpp new file mode 100644 -index 0000000..1ce2424 +index 0000000..b73c420 --- /dev/null +++ mlibc-workdir/sysdeps/astral/generic/generic.cpp -@@ -0,0 +1,1002 @@ +@@ -0,0 +1,1007 @@ +#include +#include +#include @@ -299,6 +299,11 @@ index 0000000..1ce2424 + return 0; + } + ++ int sys_tgkill(int pid, int tid, int sig) { ++ long ret; ++ return syscall(SYSCALL_KILLTHREAD, &ret, pid, tid, sig); ++ } ++ + int sys_sigpending(sigset_t *set) { + long ret; + return syscall(SYSCALL_SIGPENDING, &ret, (uint64_t)set); @@ -1246,10 +1251,10 @@ index 0000000..990eca0 +#endif diff --git mlibc-workdir/sysdeps/astral/include/astral/syscall.h mlibc-workdir/sysdeps/astral/include/astral/syscall.h new file mode 100644 -index 0000000..058d486 +index 0000000..4d603e8 --- /dev/null +++ mlibc-workdir/sysdeps/astral/include/astral/syscall.h -@@ -0,0 +1,114 @@ +@@ -0,0 +1,115 @@ +#ifndef _SYSCALL_H_INCLUDE +#define _SYSCALL_H_INCLUDE + @@ -1344,6 +1349,7 @@ index 0000000..058d486 +#define SYSCALL_SIGSUSPEND 88 +#define SYSCALL_SIGTIMEDWAIT 89 +#define SYSCALL_SIGPENDING 90 ++#define SYSCALL_KILLTHREAD 91 + +#include +#include