diff options
author | MacDue <macdue@dueutil.tech> | 2023-03-15 16:49:13 +0000 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2023-03-24 22:06:38 +0000 |
commit | d27a513dc534ecb39c9d8f64c6bf4f1fea274218 (patch) | |
tree | 5a0ade083e603b2279690244d9d8dface87b070c | |
parent | b3edd83e0a5c1f32509706ae2e33d6a73f29b903 (diff) | |
download | serenity-d27a513dc534ecb39c9d8f64c6bf4f1fea274218.zip |
LibCore: Add KeepAsChild option to Core::Process::spawn()
This now allows spawning a process without disowning.
-rw-r--r-- | Userland/Libraries/LibCore/Process.cpp | 19 | ||||
-rw-r--r-- | Userland/Libraries/LibCore/Process.h | 11 |
2 files changed, 19 insertions, 11 deletions
diff --git a/Userland/Libraries/LibCore/Process.cpp b/Userland/Libraries/LibCore/Process.cpp index bc77908ad1..32e43ba711 100644 --- a/Userland/Libraries/LibCore/Process.cpp +++ b/Userland/Libraries/LibCore/Process.cpp @@ -54,7 +54,7 @@ struct ArgvList { m_working_directory = working_directory; } - ErrorOr<pid_t> spawn() + ErrorOr<pid_t> spawn(Process::KeepAsChild keep_as_child) { #ifdef AK_OS_SERENITY posix_spawn_file_actions_t spawn_actions; @@ -66,24 +66,27 @@ struct ArgvList { posix_spawn_file_actions_addchdir(&spawn_actions, m_working_directory.characters()); auto pid = TRY(System::posix_spawn(m_path.view(), &spawn_actions, nullptr, const_cast<char**>(get().data()), environ)); - TRY(System::disown(pid)); + if (keep_as_child == Process::KeepAsChild::No) + TRY(System::disown(pid)); #else auto pid = TRY(System::posix_spawn(m_path.view(), nullptr, nullptr, const_cast<char**>(get().data()), environ)); + // FIXME: Support keep_as_child outside Serenity. + (void)keep_as_child; #endif return pid; } }; -ErrorOr<pid_t> Process::spawn(StringView path, ReadonlySpan<DeprecatedString> arguments, DeprecatedString working_directory) +ErrorOr<pid_t> Process::spawn(StringView path, ReadonlySpan<DeprecatedString> arguments, DeprecatedString working_directory, KeepAsChild keep_as_child) { ArgvList argv { path, arguments.size() }; for (auto const& arg : arguments) argv.append(arg.characters()); argv.set_working_directory(working_directory); - return argv.spawn(); + return argv.spawn(keep_as_child); } -ErrorOr<pid_t> Process::spawn(StringView path, ReadonlySpan<StringView> arguments, DeprecatedString working_directory) +ErrorOr<pid_t> Process::spawn(StringView path, ReadonlySpan<StringView> arguments, DeprecatedString working_directory, KeepAsChild keep_as_child) { Vector<DeprecatedString> backing_strings; backing_strings.ensure_capacity(arguments.size()); @@ -93,16 +96,16 @@ ErrorOr<pid_t> Process::spawn(StringView path, ReadonlySpan<StringView> argument argv.append(backing_strings.last().characters()); } argv.set_working_directory(working_directory); - return argv.spawn(); + return argv.spawn(keep_as_child); } -ErrorOr<pid_t> Process::spawn(StringView path, ReadonlySpan<char const*> arguments, DeprecatedString working_directory) +ErrorOr<pid_t> Process::spawn(StringView path, ReadonlySpan<char const*> arguments, DeprecatedString working_directory, KeepAsChild keep_as_child) { ArgvList argv { path, arguments.size() }; for (auto arg : arguments) argv.append(arg); argv.set_working_directory(working_directory); - return argv.spawn(); + return argv.spawn(keep_as_child); } ErrorOr<String> Process::get_name() diff --git a/Userland/Libraries/LibCore/Process.h b/Userland/Libraries/LibCore/Process.h index 9931445a43..3e0891afba 100644 --- a/Userland/Libraries/LibCore/Process.h +++ b/Userland/Libraries/LibCore/Process.h @@ -15,9 +15,14 @@ namespace Core { class Process { public: - static ErrorOr<pid_t> spawn(StringView path, ReadonlySpan<DeprecatedString> arguments, DeprecatedString working_directory = {}); - static ErrorOr<pid_t> spawn(StringView path, ReadonlySpan<StringView> arguments, DeprecatedString working_directory = {}); - static ErrorOr<pid_t> spawn(StringView path, ReadonlySpan<char const*> arguments = {}, DeprecatedString working_directory = {}); + enum class KeepAsChild { + Yes, + No + }; + + static ErrorOr<pid_t> spawn(StringView path, ReadonlySpan<DeprecatedString> arguments, DeprecatedString working_directory = {}, KeepAsChild keep_as_child = KeepAsChild::No); + static ErrorOr<pid_t> spawn(StringView path, ReadonlySpan<StringView> arguments, DeprecatedString working_directory = {}, KeepAsChild keep_as_child = KeepAsChild::No); + static ErrorOr<pid_t> spawn(StringView path, ReadonlySpan<char const*> arguments = {}, DeprecatedString working_directory = {}, KeepAsChild keep_as_child = KeepAsChild::No); static ErrorOr<String> get_name(); enum class SetThreadName { |