summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorSam Atkins <atkinssj@serenityos.org>2023-01-22 19:55:20 +0000
committerAndreas Kling <kling@serenityos.org>2023-01-25 14:27:45 +0100
commit00b897af8f362c360c9e116f314d593155874478 (patch)
treed94aa2bddf2245459be71fc26b39564864fd38be /Userland
parent2095e2529f3310f6ad0a59f1b6c5bd54c47b10d6 (diff)
downloadserenity-00b897af8f362c360c9e116f314d593155874478.zip
LibCore: Add nice get/set_process_name() wrappers in Core::Process
`Process::get_name()` and `Process::set_name()` are basically the same as `get_process_name()` and `set_process_name()`, except making use of convenient Serenity standard types and returning ErrorOr, instead of char* and errno shenanigans. `Process::set_name()` has an optional `SetThreadName` parameter, for when you also want to set the thread's name to the same thing. That's true for the two places that use `set_process_name()`.
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibCore/Process.cpp37
-rw-r--r--Userland/Libraries/LibCore/Process.h8
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);
};
}