Skip to content

Commit 00b897a

Browse files
AtkinsSJawesomekling
authored andcommitted
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()`.
1 parent 2095e25 commit 00b897a

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

Userland/Libraries/LibCore/Process.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
/*
22
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
33
* Copyright (c) 2022, MacDue <macdue@dueutil.tech>
4+
* Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
45
*
56
* SPDX-License-Identifier: BSD-2-Clause
67
*/
78

89
#include <AK/DeprecatedString.h>
10+
#include <AK/String.h>
911
#include <AK/Vector.h>
1012
#include <LibCore/Process.h>
1113
#include <LibCore/System.h>
1214
#include <errno.h>
1315
#include <spawn.h>
16+
#include <unistd.h>
1417

1518
#ifdef AK_OS_SERENITY
1619
# include <serenity.h>
20+
# include <syscall.h>
1721
#endif
1822

1923
extern char** environ;
@@ -97,4 +101,37 @@ ErrorOr<pid_t> Process::spawn(StringView path, Span<char const* const> arguments
97101
return argv.spawn();
98102
}
99103

104+
ErrorOr<String> Process::get_name()
105+
{
106+
#if defined(AK_OS_SERENITY)
107+
char buffer[BUFSIZ];
108+
int rc = get_process_name(buffer, BUFSIZ);
109+
if (rc != 0)
110+
return Error::from_syscall("get_process_name"sv, -rc);
111+
return String::from_utf8(StringView { buffer, strlen(buffer) });
112+
#else
113+
// FIXME: Implement Process::get_name() for other platforms.
114+
return String::from_utf8_short_string("???"sv);
115+
#endif
116+
}
117+
118+
ErrorOr<void> Process::set_name([[maybe_unused]] StringView name, [[maybe_unused]] SetThreadName set_thread_name)
119+
{
120+
#if defined(AK_OS_SERENITY)
121+
int rc = set_process_name(name.characters_without_null_termination(), name.length());
122+
if (rc != 0)
123+
return Error::from_syscall("set_process_name"sv, -rc);
124+
if (set_thread_name == SetThreadName::No)
125+
return {};
126+
127+
rc = syscall(SC_set_thread_name, gettid(), name.characters_without_null_termination(), name.length());
128+
if (rc != 0)
129+
return Error::from_syscall("set_thread_name"sv, -rc);
130+
return {};
131+
#else
132+
// FIXME: Implement Process::set_name() for other platforms.
133+
return {};
134+
#endif
135+
}
136+
100137
}

Userland/Libraries/LibCore/Process.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
22
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
33
* Copyright (c) 2022, MacDue <macdue@dueutil.tech>
4+
* Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
45
*
56
* SPDX-License-Identifier: BSD-2-Clause
67
*/
@@ -17,6 +18,13 @@ class Process {
1718
static ErrorOr<pid_t> spawn(StringView path, Span<DeprecatedString const> arguments, DeprecatedString working_directory = {});
1819
static ErrorOr<pid_t> spawn(StringView path, Span<StringView const> arguments, DeprecatedString working_directory = {});
1920
static ErrorOr<pid_t> spawn(StringView path, Span<char const* const> arguments = {}, DeprecatedString working_directory = {});
21+
22+
static ErrorOr<String> get_name();
23+
enum class SetThreadName {
24+
No,
25+
Yes,
26+
};
27+
static ErrorOr<void> set_name(StringView, SetThreadName = SetThreadName::No);
2028
};
2129

2230
}

0 commit comments

Comments
 (0)