Luna/libos/src/Process.cpp
apio 82411789e8
All checks were successful
continuous-integration/drone/push Build is passing
libos+apps: Add kill
2023-07-10 22:17:05 +02:00

78 lines
2.3 KiB
C++

/**
* @file Process.cpp
* @author apio (cloudapio.eu)
* @brief Functions to manipulate processes.
*
* @copyright Copyright (c) 2023, the Luna authors.
*
*/
#include <errno.h>
#include <os/Process.h>
#include <sys/syscall.h>
#include <unistd.h>
namespace os
{
Result<pid_t> Process::fork()
{
long rc = syscall(SYS_fork);
return Result<pid_t>::from_syscall(rc);
}
Result<void> Process::exec(StringView path, Slice<String> args, bool search_in_path)
{
Vector<const char*> argv;
for (const auto& arg : args) { TRY(argv.try_append(arg.chars())); }
TRY(argv.try_append(nullptr));
if (search_in_path) execvp(path.chars(), const_cast<char**>(argv.data()));
else
execv(path.chars(), const_cast<char**>(argv.data()));
return err(errno);
}
Result<void> Process::exec(StringView path, Slice<StringView> args, bool search_in_path)
{
Vector<const char*> argv;
for (const auto& arg : args) { TRY(argv.try_append(arg.chars())); }
TRY(argv.try_append(nullptr));
if (search_in_path) execvp(path.chars(), const_cast<char**>(argv.data()));
else
execv(path.chars(), const_cast<char**>(argv.data()));
return err(errno);
}
Result<void> Process::exec(StringView path, Slice<String> args, Slice<String> env, bool search_in_path)
{
Vector<const char*> argv;
for (const auto& arg : args) { TRY(argv.try_append(arg.chars())); }
TRY(argv.try_append(nullptr));
Vector<const char*> envp;
for (const auto& arg : env) { TRY(envp.try_append(arg.chars())); }
TRY(envp.try_append(nullptr));
if (search_in_path) execvpe(path.chars(), const_cast<char**>(argv.data()), const_cast<char**>(envp.data()));
else
execve(path.chars(), const_cast<char**>(argv.data()), const_cast<char**>(envp.data()));
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);
}
Result<void> Process::kill(pid_t pid, int signo)
{
long rc = syscall(SYS_kill, pid, signo);
return Result<void>::from_syscall(rc);
}
}