mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 19:28:12 +00:00
LibCore: Return ErrorOr<pid_t> and support arguments in Process::spawn
This makes the wrapper more like the rest in LibCore, and also removes the annoying limitation of not supporting arguments. There are three overloads one for String, char const *, and StringView argument lists. As long as there are <= 10 arguments the argv list will be allocated inline, otherwise on the heap.
This commit is contained in:
parent
0295d79339
commit
3fc0350caf
11 changed files with 77 additions and 28 deletions
|
@ -1,11 +1,14 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
||||
* Copyright (c) 2022, MacDue <macdue@dueutil.tech>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/String.h>
|
||||
#include <AK/Vector.h>
|
||||
#include <LibCore/Process.h>
|
||||
#include <LibCore/System.h>
|
||||
#include <errno.h>
|
||||
#include <spawn.h>
|
||||
|
||||
|
@ -17,21 +20,65 @@ extern char** environ;
|
|||
|
||||
namespace Core {
|
||||
|
||||
pid_t Process::spawn(StringView path)
|
||||
{
|
||||
String path_string = path;
|
||||
struct ArgvList {
|
||||
String m_path;
|
||||
Vector<char const*, 10> m_argv;
|
||||
|
||||
pid_t pid;
|
||||
char const* argv[] = { path_string.characters(), nullptr };
|
||||
if ((errno = posix_spawn(&pid, path_string.characters(), nullptr, nullptr, const_cast<char**>(argv), environ))) {
|
||||
perror("Process::spawn posix_spawn");
|
||||
} else {
|
||||
#ifdef __serenity__
|
||||
if (disown(pid) < 0)
|
||||
perror("Process::spawn disown");
|
||||
#endif
|
||||
ArgvList(String path, size_t size)
|
||||
: m_path { path }
|
||||
{
|
||||
m_argv.ensure_capacity(size + 2);
|
||||
m_argv.append(m_path.characters());
|
||||
}
|
||||
return pid;
|
||||
|
||||
void append(char const* arg)
|
||||
{
|
||||
m_argv.append(arg);
|
||||
}
|
||||
|
||||
Span<char const*> get()
|
||||
{
|
||||
if (m_argv.is_empty() || m_argv.last() != nullptr)
|
||||
m_argv.append(nullptr);
|
||||
return m_argv;
|
||||
}
|
||||
|
||||
ErrorOr<pid_t> spawn()
|
||||
{
|
||||
auto pid = TRY(System::posix_spawn(m_path.characters(), nullptr, nullptr, const_cast<char**>(get().data()), environ));
|
||||
#ifdef __serenity__
|
||||
TRY(System::disown(pid));
|
||||
#endif
|
||||
return pid;
|
||||
}
|
||||
};
|
||||
|
||||
ErrorOr<pid_t> Process::spawn(StringView path, Span<String const> arguments)
|
||||
{
|
||||
ArgvList argv { path, arguments.size() };
|
||||
for (auto const& arg : arguments)
|
||||
argv.append(arg.characters());
|
||||
return argv.spawn();
|
||||
}
|
||||
|
||||
ErrorOr<pid_t> Process::spawn(StringView path, Span<StringView const> arguments)
|
||||
{
|
||||
Vector<String> backing_strings;
|
||||
backing_strings.ensure_capacity(arguments.size());
|
||||
ArgvList argv { path, arguments.size() };
|
||||
for (auto const& arg : arguments) {
|
||||
backing_strings.append(arg);
|
||||
argv.append(backing_strings.last().characters());
|
||||
}
|
||||
return argv.spawn();
|
||||
}
|
||||
|
||||
ErrorOr<pid_t> Process::spawn(StringView path, Span<char const* const> arguments)
|
||||
{
|
||||
ArgvList argv { path, arguments.size() };
|
||||
for (auto arg : arguments)
|
||||
argv.append(arg);
|
||||
return argv.spawn();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue