From 84fa0102c6906252999967925f32098ab6d5a259 Mon Sep 17 00:00:00 2001 From: dzwdz Date: Sun, 11 Jun 2023 19:17:39 +0200 Subject: kernel: replace await with wait2, roughly compatible with POSIX dash works now :^))) --- src/kernel/proc.c | 13 ++++++++++++- src/kernel/proc.h | 4 ++++ src/kernel/syscalls.c | 28 ++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) (limited to 'src/kernel') diff --git a/src/kernel/proc.c b/src/kernel/proc.c index ecbd839..3ef35c0 100644 --- a/src/kernel/proc.c +++ b/src/kernel/proc.c @@ -349,7 +349,18 @@ void proc_tryreap(Proc *dead) { if (parent->state != PS_WAITS4CHILDDEATH) { return; /* don't reap yet */ } - regs_savereturn(&parent->regs, dead->death_msg); + if (parent->awaited_death.legacy) { + regs_savereturn(&parent->regs, dead->death_msg); + } else { + regs_savereturn(&parent->regs, proc_ns_id(parent->pns, dead)); + if (parent->awaited_death.out) { + struct sys_wait2 __user *out = parent->awaited_death.out; + struct sys_wait2 data; + memset(&data, 0, sizeof data); + data.status = dead->death_msg; + pcpy_to(parent, out, &data, sizeof data); + } + } proc_setstate(parent, PS_RUNNING); } /* can't be reaped anymore */ diff --git a/src/kernel/proc.h b/src/kernel/proc.h index f473124..a1820dc 100644 --- a/src/kernel/proc.h +++ b/src/kernel/proc.h @@ -44,6 +44,10 @@ struct Proc { enum proc_state state; union { /* saved value, meaning depends on .state */ int death_msg; // PS_DEAD + struct { + bool legacy; /* false = wait2, true = await */ + struct sys_wait2 __user *out; + } awaited_death; struct { char __user *buf; size_t max_len; diff --git a/src/kernel/syscalls.c b/src/kernel/syscalls.c index 1e75f9d..66f8757 100644 --- a/src/kernel/syscalls.c +++ b/src/kernel/syscalls.c @@ -25,6 +25,7 @@ _Noreturn void _sys_exit(long ret) { long _sys_await(void) { bool has_children = false; proc_setstate(proc_cur, PS_WAITS4CHILDDEATH); + proc_cur->awaited_death.legacy = true; for (Proc *iter = proc_cur->child; iter; iter = iter->sibling) @@ -401,6 +402,32 @@ uint32_t _sys_getppid(void) { } } +int _sys_wait2(int pid, int flags, struct sys_wait2 __user *out) { + if (pid != -1 || flags != 0) { + SYSCALL_RETURN(-ENOSYS); + } + + bool has_children = false; + proc_setstate(proc_cur, PS_WAITS4CHILDDEATH); + proc_cur->awaited_death.legacy = false; + proc_cur->awaited_death.out = out; + + for (Proc *iter = proc_cur->child; iter; iter = iter->sibling) { + if (iter->noreap) continue; + has_children = true; + if (iter->state == PS_TOREAP) { + proc_tryreap(iter); + return 0; // dummy + } + } + + if (!has_children) { + proc_setstate(proc_cur, PS_RUNNING); + SYSCALL_RETURN(-ECHILD); + } + return 0; // dummy +} + long _sys_execbuf(void __user *ubuf, size_t len) { if (len == 0) SYSCALL_RETURN(0); if (len > EXECBUF_MAX_LEN) @@ -455,6 +482,7 @@ long _syscall(long num, long a, long b, long c, long d, long e) { break; case _SYS_INTR_SET: _sys_intr_set((userptr_t)a); break; case _SYS_GETPID: _sys_getpid(); break; case _SYS_GETPPID: _sys_getppid(); + break; case _SYS_WAIT2: _sys_wait2(a, b, (userptr_t)c); break; case _SYS_EXECBUF: _sys_execbuf((userptr_t)a, b); break; case _SYS_DEBUG_KLOG: _sys_debug_klog((userptr_t)a, b); break; -- cgit v1.2.3