diff options
-rw-r--r-- | Userland/Libraries/LibCore/Process.cpp | 37 | ||||
-rw-r--r-- | Userland/Libraries/LibCore/Process.h | 8 |
2 files changed, 45 insertions, 0 deletions
diff --git a/Userland/Libraries/LibCore/Process.cpp b/Userland/Libraries/LibCore/Process.cpp index 8ec332a465..6553f192ed 100644 --- a/Userland/Libraries/LibCore/Process.cpp +++ b/Userland/Libraries/LibCore/Process.cpp @@ -1,19 +1,23 @@ /* * Copyright (c) 2021, Andreas Kling <kling@serenityos.org> * Copyright (c) 2022, MacDue <macdue@dueutil.tech> + * Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org> * * SPDX-License-Identifier: BSD-2-Clause */ #include <AK/DeprecatedString.h> +#include <AK/String.h> #include <AK/Vector.h> #include <LibCore/Process.h> #include <LibCore/System.h> #include <errno.h> #include <spawn.h> +#include <unistd.h> #ifdef AK_OS_SERENITY # include <serenity.h> +# include <syscall.h> #endif extern char** environ; @@ -97,4 +101,37 @@ ErrorOr<pid_t> Process::spawn(StringView path, Span<char const* const> arguments return argv.spawn(); } +ErrorOr<String> Process::get_name() +{ +#if defined(AK_OS_SERENITY) + char buffer[BUFSIZ]; + int rc = get_process_name(buffer, BUFSIZ); + if (rc != 0) + return Error::from_syscall("get_process_name"sv, -rc); + return String::from_utf8(StringView { buffer, strlen(buffer) }); +#else + // FIXME: Implement Process::get_name() for other platforms. + return String::from_utf8_short_string("???"sv); +#endif +} + +ErrorOr<void> Process::set_name([[maybe_unused]] StringView name, [[maybe_unused]] SetThreadName set_thread_name) +{ +#if defined(AK_OS_SERENITY) + int rc = set_process_name(name.characters_without_null_termination(), name.length()); + if (rc != 0) + return Error::from_syscall("set_process_name"sv, -rc); + if (set_thread_name == SetThreadName::No) + return {}; + + rc = syscall(SC_set_thread_name, gettid(), name.characters_without_null_termination(), name.length()); + if (rc != 0) + return Error::from_syscall("set_thread_name"sv, -rc); + return {}; +#else + // FIXME: Implement Process::set_name() for other platforms. + return {}; +#endif +} + } diff --git a/Userland/Libraries/LibCore/Process.h b/Userland/Libraries/LibCore/Process.h index 54cd6a52e8..9041f0ee0d 100644 --- a/Userland/Libraries/LibCore/Process.h +++ b/Userland/Libraries/LibCore/Process.h @@ -1,6 +1,7 @@ /* * Copyright (c) 2021, Andreas Kling <kling@serenityos.org> * Copyright (c) 2022, MacDue <macdue@dueutil.tech> + * Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org> * * SPDX-License-Identifier: BSD-2-Clause */ @@ -17,6 +18,13 @@ public: static ErrorOr<pid_t> spawn(StringView path, Span<DeprecatedString const> arguments, DeprecatedString working_directory = {}); static ErrorOr<pid_t> spawn(StringView path, Span<StringView const> arguments, DeprecatedString working_directory = {}); static ErrorOr<pid_t> spawn(StringView path, Span<char const* const> arguments = {}, DeprecatedString working_directory = {}); + + static ErrorOr<String> get_name(); + enum class SetThreadName { + No, + Yes, + }; + static ErrorOr<void> set_name(StringView, SetThreadName = SetThreadName::No); }; } |