| 
									
										
										
										
											2021-08-06 01:04:11 +02:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2024-10-04 13:19:50 +02:00
										 |  |  |  * Copyright (c) 2021, Andreas Kling <andreas@ladybird.org> | 
					
						
							| 
									
										
										
										
											2023-03-13 20:24:33 +00:00
										 |  |  |  * Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech> | 
					
						
							| 
									
										
										
										
											2024-01-30 18:30:22 +00:00
										 |  |  |  * Copyright (c) 2023-2024, Sam Atkins <atkinssj@serenityos.org> | 
					
						
							| 
									
										
										
										
											2024-04-23 16:35:36 -04:00
										 |  |  |  * Copyright (c) 2024, Tim Flynn <trflynn89@serenityos.org> | 
					
						
							| 
									
										
										
										
											2021-08-06 01:04:11 +02:00
										 |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-16 17:49:34 +03:30
										 |  |  | #include <AK/ByteString.h>
 | 
					
						
							| 
									
										
										
										
											2023-03-13 20:24:33 +00:00
										 |  |  | #include <AK/ScopeGuard.h>
 | 
					
						
							| 
									
										
										
										
											2023-01-22 19:55:20 +00:00
										 |  |  | #include <AK/String.h>
 | 
					
						
							| 
									
										
										
										
											2022-05-10 00:24:15 +01:00
										 |  |  | #include <AK/Vector.h>
 | 
					
						
							| 
									
										
										
										
											2024-01-30 18:30:22 +00:00
										 |  |  | #include <LibCore/Environment.h>
 | 
					
						
							| 
									
										
										
										
											2023-08-02 10:27:58 +02:00
										 |  |  | #include <LibCore/File.h>
 | 
					
						
							| 
									
										
										
										
											2021-08-06 01:04:11 +02:00
										 |  |  | #include <LibCore/Process.h>
 | 
					
						
							| 
									
										
										
										
											2022-05-10 00:24:15 +01:00
										 |  |  | #include <LibCore/System.h>
 | 
					
						
							| 
									
										
										
										
											2021-08-06 01:04:11 +02:00
										 |  |  | #include <errno.h>
 | 
					
						
							| 
									
										
										
										
											2024-04-23 18:53:00 -04:00
										 |  |  | #include <signal.h>
 | 
					
						
							| 
									
										
										
										
											2021-08-06 01:04:11 +02:00
										 |  |  | #include <spawn.h>
 | 
					
						
							| 
									
										
										
										
											2023-01-22 19:55:20 +00:00
										 |  |  | #include <unistd.h>
 | 
					
						
							| 
									
										
										
										
											2021-08-06 01:04:11 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-02 15:11:49 +02:00
										 |  |  | #if defined(AK_OS_SERENITY)
 | 
					
						
							| 
									
										
										
										
											2021-08-06 01:04:11 +02:00
										 |  |  | #    include <serenity.h>
 | 
					
						
							| 
									
										
										
										
											2023-08-19 21:10:25 +03:00
										 |  |  | #    include <sys/prctl.h>
 | 
					
						
							| 
									
										
										
										
											2023-01-22 19:55:20 +00:00
										 |  |  | #    include <syscall.h>
 | 
					
						
							| 
									
										
										
										
											2023-08-24 19:09:09 +02:00
										 |  |  | #elif defined(AK_OS_BSD_GENERIC) && !defined(AK_OS_SOLARIS)
 | 
					
						
							| 
									
										
										
										
											2023-08-02 15:11:49 +02:00
										 |  |  | #    include <sys/sysctl.h>
 | 
					
						
							| 
									
										
										
										
											2023-09-03 22:31:35 +03:00
										 |  |  | #elif defined(AK_OS_GNU_HURD)
 | 
					
						
							|  |  |  | extern "C" { | 
					
						
							|  |  |  | #    include <hurd.h>
 | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-08-06 01:04:11 +02:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2023-08-22 20:33:14 +02:00
										 |  |  | #if defined(AK_OS_FREEBSD)
 | 
					
						
							|  |  |  | #    include <sys/user.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2021-08-06 01:04:11 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace Core { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-10 00:24:15 +01:00
										 |  |  | struct ArgvList { | 
					
						
							| 
									
										
										
										
											2023-12-16 17:49:34 +03:30
										 |  |  |     ByteString m_path; | 
					
						
							| 
									
										
										
										
											2022-05-10 00:24:15 +01:00
										 |  |  |     Vector<char const*, 10> m_argv; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-16 17:49:34 +03:30
										 |  |  |     ArgvList(ByteString path, size_t size) | 
					
						
							| 
									
										
										
										
											2022-05-10 00:24:15 +01:00
										 |  |  |         : m_path { path } | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         m_argv.ensure_capacity(size + 2); | 
					
						
							|  |  |  |         m_argv.append(m_path.characters()); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     void append(char const* arg) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         m_argv.append(arg); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Span<char const*> get() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (m_argv.is_empty() || m_argv.last() != nullptr) | 
					
						
							|  |  |  |             m_argv.append(nullptr); | 
					
						
							|  |  |  |         return m_argv; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-11-06 23:26:04 -05:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2021-08-06 01:04:11 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-29 22:24:01 -06:00
										 |  |  | Process Process::current() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto p = Process { getpid() }; | 
					
						
							|  |  |  |     p.m_should_disown = false; | 
					
						
							|  |  |  |     return p; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-06 23:26:04 -05:00
										 |  |  | ErrorOr<Process> Process::spawn(ProcessSpawnOptions const& options) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | #define CHECK(invocation)                  \
 | 
					
						
							|  |  |  |     if (int returned_errno = (invocation)) \ | 
					
						
							|  |  |  |         return Error::from_errno(returned_errno); | 
					
						
							| 
									
										
										
										
											2022-10-15 19:04:18 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-06 23:26:04 -05:00
										 |  |  |     posix_spawn_file_actions_t spawn_actions; | 
					
						
							|  |  |  |     CHECK(posix_spawn_file_actions_init(&spawn_actions)); | 
					
						
							|  |  |  |     ScopeGuard cleanup_spawn_actions = [&] { | 
					
						
							|  |  |  |         posix_spawn_file_actions_destroy(&spawn_actions); | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (options.working_directory.has_value()) { | 
					
						
							| 
									
										
										
										
											2022-10-09 15:23:23 -06:00
										 |  |  | #ifdef AK_OS_SERENITY
 | 
					
						
							| 
									
										
										
										
											2023-11-06 23:26:04 -05:00
										 |  |  |         CHECK(posix_spawn_file_actions_addchdir(&spawn_actions, options.working_directory->characters())); | 
					
						
							| 
									
										
										
										
											2022-10-15 19:04:18 -06:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2023-11-06 23:26:04 -05:00
										 |  |  |         // FIXME: Support ProcessSpawnOptions::working_directory n platforms that support it.
 | 
					
						
							|  |  |  |         TODO(); | 
					
						
							| 
									
										
										
										
											2021-08-06 01:04:11 +02:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2022-05-10 00:24:15 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-11-06 23:26:04 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for (auto const& file_action : options.file_actions) { | 
					
						
							|  |  |  |         TRY(file_action.visit( | 
					
						
							|  |  |  |             [&](FileAction::OpenFile const& action) -> ErrorOr<void> { | 
					
						
							|  |  |  |                 CHECK(posix_spawn_file_actions_addopen( | 
					
						
							|  |  |  |                     &spawn_actions, | 
					
						
							|  |  |  |                     action.fd, | 
					
						
							|  |  |  |                     action.path.characters(), | 
					
						
							|  |  |  |                     File::open_mode_to_options(action.mode | Core::File::OpenMode::KeepOnExec), | 
					
						
							|  |  |  |                     action.permissions)); | 
					
						
							|  |  |  |                 return {}; | 
					
						
							| 
									
										
										
										
											2024-04-23 15:31:07 -04:00
										 |  |  |             }, | 
					
						
							|  |  |  |             [&](FileAction::CloseFile const& action) -> ErrorOr<void> { | 
					
						
							|  |  |  |                 CHECK(posix_spawn_file_actions_addclose(&spawn_actions, action.fd)); | 
					
						
							|  |  |  |                 return {}; | 
					
						
							| 
									
										
										
										
											2023-11-06 23:26:04 -05:00
										 |  |  |             })); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #undef CHECK
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-16 13:42:30 +00:00
										 |  |  |     ArgvList argv_list(options.executable, options.arguments.size()); | 
					
						
							| 
									
										
										
										
											2023-11-06 23:26:04 -05:00
										 |  |  |     for (auto const& argument : options.arguments) | 
					
						
							|  |  |  |         argv_list.append(argument.characters()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-16 13:42:30 +00:00
										 |  |  |     pid_t pid; | 
					
						
							|  |  |  |     if (options.search_for_executable_in_path) { | 
					
						
							| 
									
										
										
										
											2024-01-30 18:30:22 +00:00
										 |  |  |         pid = TRY(System::posix_spawnp(options.executable.view(), &spawn_actions, nullptr, const_cast<char**>(argv_list.get().data()), Core::Environment::raw_environ())); | 
					
						
							| 
									
										
										
										
											2024-01-16 13:42:30 +00:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2024-01-30 18:30:22 +00:00
										 |  |  |         pid = TRY(System::posix_spawn(options.executable.view(), &spawn_actions, nullptr, const_cast<char**>(argv_list.get().data()), Core::Environment::raw_environ())); | 
					
						
							| 
									
										
										
										
											2024-01-16 13:42:30 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-11-06 23:26:04 -05:00
										 |  |  |     return Process { pid }; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-05-10 00:24:15 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-16 17:49:34 +03:30
										 |  |  | ErrorOr<pid_t> Process::spawn(StringView path, ReadonlySpan<ByteString> arguments, ByteString working_directory, KeepAsChild keep_as_child) | 
					
						
							| 
									
										
										
										
											2022-05-10 00:24:15 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-11-06 23:26:04 -05:00
										 |  |  |     auto process = TRY(spawn({ | 
					
						
							| 
									
										
										
										
											2024-01-16 13:42:30 +00:00
										 |  |  |         .executable = path, | 
					
						
							| 
									
										
										
										
											2023-12-16 17:49:34 +03:30
										 |  |  |         .arguments = Vector<ByteString> { arguments }, | 
					
						
							|  |  |  |         .working_directory = working_directory.is_empty() ? Optional<ByteString> {} : Optional<ByteString> { working_directory }, | 
					
						
							| 
									
										
										
										
											2023-11-06 23:26:04 -05:00
										 |  |  |     })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (keep_as_child == KeepAsChild::No) | 
					
						
							|  |  |  |         TRY(process.disown()); | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         // FIXME: This won't be needed if return value is changed to Process.
 | 
					
						
							|  |  |  |         process.m_should_disown = false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return process.pid(); | 
					
						
							| 
									
										
										
										
											2022-05-10 00:24:15 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-16 17:49:34 +03:30
										 |  |  | ErrorOr<pid_t> Process::spawn(StringView path, ReadonlySpan<StringView> arguments, ByteString working_directory, KeepAsChild keep_as_child) | 
					
						
							| 
									
										
										
										
											2022-05-10 00:24:15 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-12-16 17:49:34 +03:30
										 |  |  |     Vector<ByteString> backing_strings; | 
					
						
							| 
									
										
										
										
											2023-11-06 23:26:04 -05:00
										 |  |  |     backing_strings.ensure_capacity(arguments.size()); | 
					
						
							|  |  |  |     for (auto const& argument : arguments) | 
					
						
							|  |  |  |         backing_strings.append(argument); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto process = TRY(spawn({ | 
					
						
							| 
									
										
										
										
											2024-01-16 13:42:30 +00:00
										 |  |  |         .executable = path, | 
					
						
							| 
									
										
										
										
											2023-11-06 23:26:04 -05:00
										 |  |  |         .arguments = backing_strings, | 
					
						
							| 
									
										
										
										
											2023-12-16 17:49:34 +03:30
										 |  |  |         .working_directory = working_directory.is_empty() ? Optional<ByteString> {} : Optional<ByteString> { working_directory }, | 
					
						
							| 
									
										
										
										
											2023-11-06 23:26:04 -05:00
										 |  |  |     })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (keep_as_child == KeepAsChild::No) | 
					
						
							|  |  |  |         TRY(process.disown()); | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |         process.m_should_disown = false; | 
					
						
							|  |  |  |     return process.pid(); | 
					
						
							| 
									
										
										
										
											2021-08-06 01:04:11 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-22 19:55:20 +00:00
										 |  |  | 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) }); | 
					
						
							| 
									
										
										
										
											2023-09-03 22:07:23 +03:00
										 |  |  | #elif defined(AK_LIBC_GLIBC) || (defined(AK_OS_LINUX) && !defined(AK_OS_ANDROID))
 | 
					
						
							| 
									
										
										
										
											2023-08-02 10:18:16 +02:00
										 |  |  |     return String::from_utf8(StringView { program_invocation_name, strlen(program_invocation_name) }); | 
					
						
							| 
									
										
										
										
											2023-08-27 21:12:46 +02:00
										 |  |  | #elif defined(AK_OS_BSD_GENERIC) || defined(AK_OS_HAIKU)
 | 
					
						
							| 
									
										
										
										
											2023-08-02 10:18:16 +02:00
										 |  |  |     auto const* progname = getprogname(); | 
					
						
							|  |  |  |     return String::from_utf8(StringView { progname, strlen(progname) }); | 
					
						
							| 
									
										
										
										
											2023-01-22 19:55:20 +00:00
										 |  |  | #else
 | 
					
						
							|  |  |  |     // FIXME: Implement Process::get_name() for other platforms.
 | 
					
						
							| 
									
										
										
										
											2023-08-07 22:26:17 -04:00
										 |  |  |     return "???"_string; | 
					
						
							| 
									
										
										
										
											2023-01-22 19:55:20 +00:00
										 |  |  | #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 {}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-19 21:10:25 +03:00
										 |  |  |     rc = prctl(PR_SET_THREAD_NAME, gettid(), name.characters_without_null_termination(), name.length()); | 
					
						
							| 
									
										
										
										
											2023-01-22 19:55:20 +00:00
										 |  |  |     if (rc != 0) | 
					
						
							|  |  |  |         return Error::from_syscall("set_thread_name"sv, -rc); | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  |     // FIXME: Implement Process::set_name() for other platforms.
 | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-02 10:27:58 +02:00
										 |  |  | ErrorOr<bool> Process::is_being_debugged() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-08-02 15:11:49 +02:00
										 |  |  | #if defined(AK_OS_LINUX)
 | 
					
						
							| 
									
										
										
										
											2023-08-02 10:27:58 +02:00
										 |  |  |     auto unbuffered_status_file = TRY(Core::File::open("/proc/self/status"sv, Core::File::OpenMode::Read)); | 
					
						
							|  |  |  |     auto status_file = TRY(Core::InputBufferedFile::create(move(unbuffered_status_file))); | 
					
						
							|  |  |  |     auto buffer = TRY(ByteBuffer::create_uninitialized(4096)); | 
					
						
							|  |  |  |     while (TRY(status_file->can_read_line())) { | 
					
						
							|  |  |  |         auto line = TRY(status_file->read_line(buffer)); | 
					
						
							|  |  |  |         auto const parts = line.split_view(':'); | 
					
						
							|  |  |  |         if (parts.size() < 2 || parts[0] != "TracerPid"sv) | 
					
						
							|  |  |  |             continue; | 
					
						
							| 
									
										
										
										
											2023-12-23 15:59:14 +13:00
										 |  |  |         auto tracer_pid = parts[1].to_number<u32>(); | 
					
						
							| 
									
										
										
										
											2023-08-02 10:27:58 +02:00
										 |  |  |         return (tracer_pid != 0UL); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return false; | 
					
						
							| 
									
										
										
										
											2023-09-03 22:31:35 +03:00
										 |  |  | #elif defined(AK_OS_GNU_HURD)
 | 
					
						
							|  |  |  |     process_t proc = getproc(); | 
					
						
							|  |  |  |     if (!MACH_PORT_VALID(proc)) | 
					
						
							|  |  |  |         return Error::from_syscall("getproc"sv, -errno); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     int flags = PI_FETCH_TASKINFO; | 
					
						
							|  |  |  |     // We're going to ask the proc server for the info about our process,
 | 
					
						
							|  |  |  |     // and it is going to reply, placing the info into a buffer. It can
 | 
					
						
							|  |  |  |     // either fill in (overwrite) the buffer we provide to it (called pi_buffer
 | 
					
						
							|  |  |  |     // below), or allocate (as if with mmap or vm_allocate) a new buffer.
 | 
					
						
							|  |  |  |     // The buffer is really of type struct procinfo[], but it's transferred
 | 
					
						
							|  |  |  |     // over IPC as int[]. We pass in a double pointer (int** pi_array) that
 | 
					
						
							|  |  |  |     // initially points to our pi_buffer, but the call will update it to
 | 
					
						
							|  |  |  |     // point to the newly allocated buffer if it ends up making one.
 | 
					
						
							|  |  |  |     struct procinfo pi_buffer = {}; | 
					
						
							|  |  |  |     int* pi_array = reinterpret_cast<int*>(&pi_buffer); | 
					
						
							|  |  |  |     mach_msg_type_number_t pi_array_len = sizeof(pi_buffer) / sizeof(int); | 
					
						
							|  |  |  |     data_t waits = nullptr; | 
					
						
							|  |  |  |     mach_msg_type_number_t waits_len = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     kern_return_t err = proc_getprocinfo(proc, getpid(), &flags, &pi_array, &pi_array_len, &waits, &waits_len); | 
					
						
							|  |  |  |     mach_port_deallocate(mach_task_self(), proc); | 
					
						
							|  |  |  |     if (err) { | 
					
						
							|  |  |  |         __hurd_fail(static_cast<error_t>(err)); | 
					
						
							|  |  |  |         return Error::from_syscall("proc_getprocinfo"sv, -errno); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Now cast the returned buffer pointer back to struct procinfo, and
 | 
					
						
							|  |  |  |     // read the info we're interested in (the PI_TRACED flag) from there.
 | 
					
						
							|  |  |  |     VERIFY(pi_array_len >= sizeof(struct procinfo)); | 
					
						
							|  |  |  |     struct procinfo* procinfo = reinterpret_cast<struct procinfo*>(pi_array); | 
					
						
							|  |  |  |     bool traced = procinfo->state & PI_TRACED; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // If the returned buffer is not the one we allocated on the stack,
 | 
					
						
							|  |  |  |     // we should unmap it.
 | 
					
						
							|  |  |  |     if (procinfo != &pi_buffer) | 
					
						
							|  |  |  |         (void)System::munmap(pi_array, pi_array_len * sizeof(int)); | 
					
						
							|  |  |  |     if (waits) | 
					
						
							|  |  |  |         (void)System::munmap(waits, waits_len); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return traced; | 
					
						
							| 
									
										
										
										
											2023-08-22 20:33:14 +02:00
										 |  |  | #elif defined(AK_OS_MACOS) || defined(AK_OS_FREEBSD)
 | 
					
						
							| 
									
										
										
										
											2023-08-02 15:11:49 +02:00
										 |  |  |     // https://developer.apple.com/library/archive/qa/qa1361/_index.html
 | 
					
						
							|  |  |  |     int mib[4] = {}; | 
					
						
							|  |  |  |     struct kinfo_proc info = {}; | 
					
						
							|  |  |  |     size_t size = sizeof(info); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Initialize mib, which tells sysctl the info we want, in this case
 | 
					
						
							|  |  |  |     // we're looking for information about a specific process ID.
 | 
					
						
							|  |  |  |     mib[0] = CTL_KERN; | 
					
						
							|  |  |  |     mib[1] = KERN_PROC; | 
					
						
							|  |  |  |     mib[2] = KERN_PROC_PID; | 
					
						
							|  |  |  |     mib[3] = getpid(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, NULL, 0) < 0) | 
					
						
							|  |  |  |         return Error::from_syscall("sysctl"sv, -errno); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-22 20:33:14 +02:00
										 |  |  |         // We're being debugged if the P_TRACED flag is set.
 | 
					
						
							|  |  |  | #    if defined(AK_OS_MACOS)
 | 
					
						
							| 
									
										
										
										
											2023-08-02 15:11:49 +02:00
										 |  |  |     return ((info.kp_proc.p_flag & P_TRACED) != 0); | 
					
						
							| 
									
										
										
										
											2023-08-22 20:33:14 +02:00
										 |  |  | #    elif defined(AK_OS_FREEBSD)
 | 
					
						
							|  |  |  |     return ((info.ki_flag & P_TRACED) != 0); | 
					
						
							|  |  |  | #    endif
 | 
					
						
							| 
									
										
										
										
											2023-08-02 10:27:58 +02:00
										 |  |  | #endif
 | 
					
						
							|  |  |  |     // FIXME: Implement this for more platforms.
 | 
					
						
							| 
									
										
										
										
											2024-09-05 15:06:15 +04:00
										 |  |  |     return Error::from_string_literal("Platform does not support checking for debugger"); | 
					
						
							| 
									
										
										
										
											2023-08-02 10:27:58 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-02 14:09:19 +02:00
										 |  |  | // Forces the process to sleep until a debugger is attached, then breaks.
 | 
					
						
							|  |  |  | void Process::wait_for_debugger_and_break() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     bool should_print_process_info { true }; | 
					
						
							|  |  |  |     for (;;) { | 
					
						
							|  |  |  |         auto check = Process::is_being_debugged(); | 
					
						
							|  |  |  |         if (check.is_error()) { | 
					
						
							|  |  |  |             dbgln("Cannot wait for debugger: {}. Continuing.", check.release_error()); | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (check.value()) { | 
					
						
							|  |  |  |             kill(getpid(), SIGTRAP); | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (should_print_process_info) { | 
					
						
							|  |  |  |             dbgln("Process {} with pid {} is sleeping, waiting for debugger.", Process::get_name(), getpid()); | 
					
						
							|  |  |  |             should_print_process_info = false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         ::usleep(100 * 1000); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-06 23:26:04 -05:00
										 |  |  | ErrorOr<void> Process::disown() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (m_pid != 0 && m_should_disown) { | 
					
						
							|  |  |  | #ifdef AK_OS_SERENITY
 | 
					
						
							|  |  |  |         TRY(System::disown(m_pid)); | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  |         // FIXME: Support disown outside Serenity.
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |         m_should_disown = false; | 
					
						
							|  |  |  |         return {}; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         return Error::from_errno(EINVAL); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ErrorOr<bool> Process::wait_for_termination() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     VERIFY(m_pid > 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     bool exited_with_code_0 = true; | 
					
						
							|  |  |  |     int status; | 
					
						
							|  |  |  |     if (waitpid(m_pid, &status, 0) == -1) | 
					
						
							|  |  |  |         return Error::from_syscall("waitpid"sv, errno); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (WIFEXITED(status)) { | 
					
						
							|  |  |  |         exited_with_code_0 &= WEXITSTATUS(status) == 0; | 
					
						
							|  |  |  |     } else if (WIFSIGNALED(status)) { | 
					
						
							|  |  |  |         exited_with_code_0 = false; | 
					
						
							|  |  |  |     } else if (WIFSTOPPED(status)) { | 
					
						
							|  |  |  |         // This is only possible if the child process is being traced by us.
 | 
					
						
							|  |  |  |         VERIFY_NOT_REACHED(); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         VERIFY_NOT_REACHED(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     m_should_disown = false; | 
					
						
							|  |  |  |     return exited_with_code_0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-06 01:04:11 +02:00
										 |  |  | } |