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()`.
This commit is contained in:
Sam Atkins 2023-01-22 19:55:20 +00:00 committed by Andreas Kling
parent 2095e2529f
commit 00b897af8f
2 changed files with 45 additions and 0 deletions

View file

@ -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
}
}

View file

@ -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);
};
}