|
1 | 1 | /*
|
2 | 2 | * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
3 | 3 | * Copyright (c) 2022, MacDue <macdue@dueutil.tech>
|
| 4 | + * Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org> |
4 | 5 | *
|
5 | 6 | * SPDX-License-Identifier: BSD-2-Clause
|
6 | 7 | */
|
7 | 8 |
|
8 | 9 | #include <AK/DeprecatedString.h>
|
| 10 | +#include <AK/String.h> |
9 | 11 | #include <AK/Vector.h>
|
10 | 12 | #include <LibCore/Process.h>
|
11 | 13 | #include <LibCore/System.h>
|
12 | 14 | #include <errno.h>
|
13 | 15 | #include <spawn.h>
|
| 16 | +#include <unistd.h> |
14 | 17 |
|
15 | 18 | #ifdef AK_OS_SERENITY
|
16 | 19 | # include <serenity.h>
|
| 20 | +# include <syscall.h> |
17 | 21 | #endif
|
18 | 22 |
|
19 | 23 | extern char** environ;
|
@@ -97,4 +101,37 @@ ErrorOr<pid_t> Process::spawn(StringView path, Span<char const* const> arguments
|
97 | 101 | return argv.spawn();
|
98 | 102 | }
|
99 | 103 |
|
| 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 | + |
100 | 137 | }
|
0 commit comments