2023-04-18 16:39:37 +00:00
|
|
|
#include <errno.h>
|
2023-05-02 08:50:39 +00:00
|
|
|
#include <os/Process.h>
|
2023-04-18 16:16:24 +00:00
|
|
|
#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);
|
|
|
|
}
|
2023-04-18 16:39:37 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2023-04-22 11:54:47 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2023-04-18 16:16:24 +00:00
|
|
|
}
|