From cbb9f47ee52a866e986ce954a2ceead49d0a046b Mon Sep 17 00:00:00 2001 From: Bananymous Date: Sun, 4 Jun 2023 17:59:13 +0300 Subject: [PATCH] LibC: add wait and waitpid Note that wait() doesn't work since only waiting for specified pid is supported. wait() will just return -1 and set errno to ECHILD. --- libc/CMakeLists.txt | 1 + libc/include/sys/syscall.h | 1 + libc/include/sys/wait.h | 5 +++-- libc/sys/wait.cpp | 13 +++++++++++++ libc/unistd.cpp | 8 ++++++++ 5 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 libc/sys/wait.cpp diff --git a/libc/CMakeLists.txt b/libc/CMakeLists.txt index c42271df..14eee1c2 100644 --- a/libc/CMakeLists.txt +++ b/libc/CMakeLists.txt @@ -10,6 +10,7 @@ set(LIBC_SOURCES stdio.cpp stdlib.cpp string.cpp + sys/wait.cpp termios.cpp unistd.cpp math.S diff --git a/libc/include/sys/syscall.h b/libc/include/sys/syscall.h index 3cb1af42..6a04fb2d 100644 --- a/libc/include/sys/syscall.h +++ b/libc/include/sys/syscall.h @@ -21,6 +21,7 @@ __BEGIN_DECLS #define SYS_SLEEP 14 #define SYS_EXEC 15 #define SYS_REALLOC 16 +#define SYS_WAIT 17 __END_DECLS diff --git a/libc/include/sys/wait.h b/libc/include/sys/wait.h index 0a9a82ff..c5d95f19 100644 --- a/libc/include/sys/wait.h +++ b/libc/include/sys/wait.h @@ -20,11 +20,12 @@ __BEGIN_DECLS #define WNOWAIT 0x10 #define WSTOPPED 0x20 +#define WEXITSTATUS(status) (status & 0xFF) +#define WIFEXITED(status) (WEXITSTATUS(status) != 0) + // FIXME #if 0 -#define WEXITSTATUS #define WIFCONTINUED -#define WIFEXITED #define WIFSIGNALED #define WIFSTOPPED #define WSTOPSIG diff --git a/libc/sys/wait.cpp b/libc/sys/wait.cpp new file mode 100644 index 00000000..7e85bd50 --- /dev/null +++ b/libc/sys/wait.cpp @@ -0,0 +1,13 @@ +#include +#include +#include + +pid_t wait(int* stat_loc) +{ + return waitpid(-1, stat_loc, 0); +} + +pid_t waitpid(pid_t pid, int* stat_loc, int options) +{ + return (pid_t)syscall(SYS_WAIT, pid, stat_loc, options); +} \ No newline at end of file diff --git a/libc/unistd.cpp b/libc/unistd.cpp index 4818b6d3..81df889c 100644 --- a/libc/unistd.cpp +++ b/libc/unistd.cpp @@ -137,6 +137,14 @@ long syscall(long syscall, ...) ret = Kernel::syscall(SYS_REALLOC, (uintptr_t)ptr, size); break; } + case SYS_WAIT: + { + pid_t pid = va_arg(args, pid_t); + int* stat_loc = va_arg(args, int*); + int options = va_arg(args, int); + ret = Kernel::syscall(SYS_WAIT, pid, (uintptr_t)stat_loc, options); + break; + } default: puts("LibC: Unhandeled syscall"); ret = -ENOSYS;