1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
/*
* 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>
#ifdef __serenity__
# include <serenity.h>
#endif
extern char** environ;
namespace Core {
struct ArgvList {
String m_path;
Vector<char const*, 10> m_argv;
ArgvList(String path, size_t size)
: m_path { path }
{
m_argv.ensure_capacity(size + 2);
m_argv.append(m_path.characters());
}
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.view(), 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();
}
}
|