diff options
author | dzwdz | 2023-06-11 19:17:39 +0200 |
---|---|---|
committer | dzwdz | 2023-06-11 19:17:39 +0200 |
commit | 84fa0102c6906252999967925f32098ab6d5a259 (patch) | |
tree | 00bd8c98a68e4882e2f42510c9c55932ef855ba3 | |
parent | ff154205e1671e11ed8566740c1cefaad79c2512 (diff) |
kernel: replace await with wait2, roughly compatible with POSIX
dash works now :^)))
-rw-r--r-- | src/kernel/proc.c | 13 | ||||
-rw-r--r-- | src/kernel/proc.h | 4 | ||||
-rw-r--r-- | src/kernel/syscalls.c | 28 | ||||
-rw-r--r-- | src/shared/include/camellia/errno.h | 1 | ||||
-rw-r--r-- | src/shared/include/camellia/syscalls.h | 4 | ||||
-rw-r--r-- | src/shared/include/camellia/types.h | 4 | ||||
-rw-r--r-- | src/user/app/testelf/main.c | 2 | ||||
-rw-r--r-- | src/user/app/tests/kernel/miscsyscall.c | 13 | ||||
-rw-r--r-- | src/user/lib/include/__errno.h | 1 | ||||
-rw-r--r-- | src/user/lib/include/sys/wait.h | 4 | ||||
-rw-r--r-- | src/user/lib/signal.c | 3 | ||||
-rw-r--r-- | src/user/lib/syscall.c | 4 | ||||
-rw-r--r-- | src/user/lib/syswait.c | 18 |
13 files changed, 91 insertions, 8 deletions
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 @@ -45,6 +45,10 @@ struct Proc { 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; struct ufs_request __user *res; 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; diff --git a/src/shared/include/camellia/errno.h b/src/shared/include/camellia/errno.h index b56ba91..e29f3d7 100644 --- a/src/shared/include/camellia/errno.h +++ b/src/shared/include/camellia/errno.h @@ -15,6 +15,7 @@ #define EMFILE 11 /* all file descriptors taken */ #define ECONNRESET 12 #define EPIPE 13 +#define ECHILD 14 #define EISDIR 200 #define ENAMETOOLONG 201 diff --git a/src/shared/include/camellia/syscalls.h b/src/shared/include/camellia/syscalls.h index 809cad7..9a8fa94 100644 --- a/src/shared/include/camellia/syscalls.h +++ b/src/shared/include/camellia/syscalls.h @@ -21,6 +21,7 @@ #define _SYS_INTR_SET 18 #define _SYS_GETPID 19 #define _SYS_GETPPID 20 +#define _SYS_WAIT2 21 #define _SYS_EXECBUF 100 #define _SYS_DEBUG_KLOG 101 @@ -84,6 +85,9 @@ void _sys_intr_set(void __user *ip); uint32_t _sys_getpid(void); uint32_t _sys_getppid(void); +// TODO deprecate await +int _sys_wait2(int pid, int flags, struct sys_wait2 __user *out); + /* see shared/execbuf.h */ long _sys_execbuf(void __user *buf, size_t len); diff --git a/src/shared/include/camellia/types.h b/src/shared/include/camellia/types.h index 3b9cfb4..65200d7 100644 --- a/src/shared/include/camellia/types.h +++ b/src/shared/include/camellia/types.h @@ -35,3 +35,7 @@ struct intr_data { void __user *ip; void __user *sp; /* last for pop %rsp */ }; + +struct sys_wait2 { + long status; +}; diff --git a/src/user/app/testelf/main.c b/src/user/app/testelf/main.c index 5608cc7..a2e5932 100644 --- a/src/user/app/testelf/main.c +++ b/src/user/app/testelf/main.c @@ -10,7 +10,7 @@ int main(int argc, char **argv) { printf("argc == %u\n", argc); for (int i = 0; i < argc; i++) printf("argv[%u] == %p == \"%s\"\n", i, argv[i], argv[i]); - if (strcmp(argv[1], "stackexec") == 0) { + if (argv[1] && strcmp(argv[1], "stackexec") == 0) { /* exec something with arguments on the stack */ const char s_d[] = "I am a pretty long string on the stack. Oh my. " \ "I hope I won't get corrupted.\0"; diff --git a/src/user/app/tests/kernel/miscsyscall.c b/src/user/app/tests/kernel/miscsyscall.c index c90560e..459da2a 100644 --- a/src/user/app/tests/kernel/miscsyscall.c +++ b/src/user/app/tests/kernel/miscsyscall.c @@ -40,6 +40,18 @@ static void test_await2(void) { _sys_filicide(); } +static void test_wait2_basic(void) { + struct sys_wait2 data = {0}; + int pid; + pid = fork(); + if (pid == 0) { + exit(420); + } + test(_sys_wait2(-1, 0, &data) == pid); + test(data.status == 420); + test(_sys_wait2(-1, 0, NULL) == -ECHILD); +} + static void test_pipe(void) { const char *pipe_msgs[2] = {"hello", "world"}; hid_t ends[2]; @@ -293,6 +305,7 @@ static void test_badopen(void) { void r_k_miscsyscall(void) { run_test(test_await); run_test(test_await2); + run_test(test_wait2_basic); run_test(test_pipe); run_test(test_memflag); run_test(test_dup); diff --git a/src/user/lib/include/__errno.h b/src/user/lib/include/__errno.h index f72fcf6..0808ac6 100644 --- a/src/user/lib/include/__errno.h +++ b/src/user/lib/include/__errno.h @@ -13,6 +13,7 @@ E( 10, "EACCES") E( 11, "EMFILE all file descriptors taken") E( 12, "ECONNRESET") E( 13, "EPIPE") +E( 14, "ECHILD") E(200, "EISDIR") E(201, "ENAMETOOLONG") E(202, "ENOTDIR") diff --git a/src/user/lib/include/sys/wait.h b/src/user/lib/include/sys/wait.h index c71418c..cff407e 100644 --- a/src/user/lib/include/sys/wait.h +++ b/src/user/lib/include/sys/wait.h @@ -2,8 +2,8 @@ #include <sys/types.h> #define WIFSTOPPED(x) 0 -#define WEXITSTATUS(x) 0 -#define WIFEXITED(x) 0 +#define WEXITSTATUS(x) ((x)&0xFF) +#define WIFEXITED(x) 1 #define WSTOPSIG(x) 0 #define WTERMSIG(x) 0 diff --git a/src/user/lib/signal.c b/src/user/lib/signal.c index d8b673d..41340e7 100644 --- a/src/user/lib/signal.c +++ b/src/user/lib/signal.c @@ -76,8 +76,7 @@ int sigfillset(sigset_t *set) { } int sigprocmask(int how, const sigset_t *set, const sigset_t *oldset) { - (void)how; (void)set; (void)oldset; - __libc_panic("unimplemented"); + return 0; } int sigsuspend(const sigset_t *mask) { diff --git a/src/user/lib/syscall.c b/src/user/lib/syscall.c index 3e8473c..5196683 100644 --- a/src/user/lib/syscall.c +++ b/src/user/lib/syscall.c @@ -90,6 +90,10 @@ uint32_t _sys_getppid(void) { return (uint32_t)_syscall(_SYS_GETPPID, 0, 0, 0, 0, 0); } +int _sys_wait2(int pid, int flags, struct sys_wait2 __user *out) { + return (int)_syscall(_SYS_WAIT2, (long)pid, (long)flags, (long)out, 0, 0); +} + long _sys_execbuf(void __user *buf, size_t len) { return _syscall(_SYS_EXECBUF, (long)buf, (long)len, 0, 0, 0); } diff --git a/src/user/lib/syswait.c b/src/user/lib/syswait.c index 6389cb2..ce80c7b 100644 --- a/src/user/lib/syswait.c +++ b/src/user/lib/syswait.c @@ -1,8 +1,22 @@ #include <bits/panic.h> +#include <camellia.h> +#include <camellia/syscalls.h> +#include <errno.h> #include <sys/resource.h> #include <sys/wait.h> pid_t wait3(int *wstatus, int opts, struct rusage *rusage) { - (void)wstatus; (void)opts; (void)rusage; - __libc_panic("unimplemented"); + struct sys_wait2 res; + if (opts || rusage) { + __libc_panic("unimplemented"); + } + pid_t ret = _sys_wait2(-1, 0, &res); + if (ret < 0) { + errno = -ret; + return -1; + } + if (wstatus) { + *wstatus = res.status & 0xFF; + } + return ret; } |