libos: Allow passing environment to Process::exec

This commit is contained in:
apio 2023-04-22 13:54:47 +02:00
parent 5d56638851
commit e654ed6415
Signed by: apio
GPG Key ID: B8A7D06E42258954
2 changed files with 18 additions and 0 deletions

View File

@ -11,5 +11,6 @@ namespace os
static Result<pid_t> fork();
static Result<void> exec(StringView path, Slice<String> args, bool search_in_path = true);
static Result<void> exec(StringView path, Slice<String> args, Slice<String> env, bool search_in_path = true);
};
}

View File

@ -24,4 +24,21 @@ namespace os
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);
}
}