From e4e501ecfe847d064c26f8e869461f755edea1e9 Mon Sep 17 00:00:00 2001 From: apio Date: Fri, 9 Jun 2023 23:12:31 +0200 Subject: [PATCH] libos: Add Process::wait() --- apps/init.cpp | 9 +++++---- apps/sh.cpp | 7 +------ apps/time.cpp | 7 +------ libos/include/os/Process.h | 15 +++++++++++++++ libos/src/Process.cpp | 6 ++++++ 5 files changed, 28 insertions(+), 16 deletions(-) diff --git a/apps/init.cpp b/apps/init.cpp index 640f0332..afc47547 100644 --- a/apps/init.cpp +++ b/apps/init.cpp @@ -14,7 +14,6 @@ #include #include #include -#include #include FILE* g_init_log; @@ -97,8 +96,7 @@ static Result try_start_service(Service& service) fprintf(g_init_log, "[init] waiting for child process %d to finish\n", pid); int status; - pid_t id = waitpid(pid, &status, 0); - if (id < 0) { return err(errno); } + TRY(os::Process::wait(pid, &status)); fprintf(g_init_log, "[init] child process %d exited with code %d\n", pid, WEXITSTATUS(status)); } @@ -312,7 +310,10 @@ int main() while (1) { int status; - pid_t child = wait(&status); + auto rc = os::Process::wait(os::Process::ANY_CHILD, &status); + if (rc.has_error()) continue; + + pid_t child = rc.release_value(); for (auto& service : g_services) { diff --git a/apps/sh.cpp b/apps/sh.cpp index d998fcd2..2d55e7a2 100644 --- a/apps/sh.cpp +++ b/apps/sh.cpp @@ -11,7 +11,6 @@ #include #include #include -#include #include using os::File; @@ -118,11 +117,7 @@ Result luna_main(int argc, char** argv) if (child == 0) { TRY(execute_command(cmd.view())); } - if (waitpid(child, NULL, 0) < 0) - { - perror("waitpid"); - return 1; - } + TRY(os::Process::wait(child, nullptr)); } return 0; diff --git a/apps/time.cpp b/apps/time.cpp index d3e4dc34..1d57727e 100644 --- a/apps/time.cpp +++ b/apps/time.cpp @@ -3,7 +3,6 @@ #include #include #include -#include Result luna_main(int argc, char** argv) { @@ -23,11 +22,7 @@ Result luna_main(int argc, char** argv) unreachable(); } - if (waitpid(pid, nullptr, 0) < 0) - { - perror("waitpid"); - return 1; - } + TRY(os::Process::wait(pid, nullptr)); struct rusage usage; if (getrusage(RUSAGE_CHILDREN, &usage) < 0) diff --git a/libos/include/os/Process.h b/libos/include/os/Process.h index b2c7a490..8010424e 100644 --- a/libos/include/os/Process.h +++ b/libos/include/os/Process.h @@ -11,6 +11,7 @@ #include #include #include +#include namespace os { @@ -57,5 +58,19 @@ namespace os * @return Result Always an error, as this function does not return on success. */ static Result exec(StringView path, Slice args, Slice env, bool search_in_path = true); + + // To use as the child argument to wait() to wait for any child. + static constexpr pid_t ANY_CHILD = -1; + + /** + * @brief Wait for a child process to finish running. + * + * @param child The process ID of the child process, or ANY_CHILD to wait for any child. + * @param status The child's exit status will be stored here. If you don't want it, use nullptr. + * @param options Options to pass to waitpid(2). + * @return Result The process ID of the child process that was waited for (may not be the same as the + * child argument if ANY_CHILD was passed). + */ + static Result wait(pid_t child, int* status, int options = 0); }; } diff --git a/libos/src/Process.cpp b/libos/src/Process.cpp index 31f3a327..72b72bec 100644 --- a/libos/src/Process.cpp +++ b/libos/src/Process.cpp @@ -62,4 +62,10 @@ namespace os return err(errno); } + + Result Process::wait(pid_t child, int* status, int options) + { + long rc = syscall(SYS_waitpid, child, status, options); + return Result::from_syscall(rc); + } }