libos: Add Process::wait()
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
apio 2023-06-09 23:12:31 +02:00
parent a1bf4dafbe
commit e4e501ecfe
Signed by: apio
GPG Key ID: B8A7D06E42258954
5 changed files with 28 additions and 16 deletions

View File

@ -14,7 +14,6 @@
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/sysmacros.h>
#include <sys/wait.h>
#include <unistd.h>
FILE* g_init_log;
@ -97,8 +96,7 @@ static Result<void> 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)
{

View File

@ -11,7 +11,6 @@
#include <stdlib.h>
#include <string.h>
#include <sys/utsname.h>
#include <sys/wait.h>
#include <unistd.h>
using os::File;
@ -118,11 +117,7 @@ Result<int> 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;

View File

@ -3,7 +3,6 @@
#include <os/Process.h>
#include <stdio.h>
#include <sys/resource.h>
#include <sys/wait.h>
Result<int> luna_main(int argc, char** argv)
{
@ -23,11 +22,7 @@ Result<int> 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)

View File

@ -11,6 +11,7 @@
#include <luna/Result.h>
#include <luna/String.h>
#include <sys/types.h>
#include <sys/wait.h>
namespace os
{
@ -57,5 +58,19 @@ namespace os
* @return Result<void> Always an error, as this function does not return on success.
*/
static Result<void> exec(StringView path, Slice<String> args, Slice<String> 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<pid_t> 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<pid_t> wait(pid_t child, int* status, int options = 0);
};
}

View File

@ -62,4 +62,10 @@ namespace os
return err(errno);
}
Result<pid_t> Process::wait(pid_t child, int* status, int options)
{
long rc = syscall(SYS_waitpid, child, status, options);
return Result<pid_t>::from_syscall(rc);
}
}