/* * Copyright (c) 2021, Andreas Kling * Copyright (c) 2022, MacDue * Copyright (c) 2023, Sam Atkins * Copyright (c) 2024, Tim Flynn * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include namespace Core { namespace FileAction { struct OpenFile { ByteString path; File::OpenMode mode = File::OpenMode::NotOpen; int fd = -1; mode_t permissions = 0600; }; struct CloseFile { int fd { -1 }; }; struct DupFd { int write_fd { -1 }; int fd { -1 }; }; } struct ProcessSpawnOptions { StringView name {}; ByteString executable {}; bool search_for_executable_in_path { false }; Vector const& arguments {}; Optional working_directory {}; using FileActionType = Variant; Vector file_actions {}; }; class Process { AK_MAKE_NONCOPYABLE(Process); public: enum class KeepAsChild { Yes, No }; Process(Process&& other); Process& operator=(Process&& other); ~Process(); static ErrorOr spawn(ProcessSpawnOptions const& options); static Process current(); static ErrorOr spawn(StringView path, ReadonlySpan arguments, ByteString working_directory = {}, KeepAsChild keep_as_child = KeepAsChild::No); static ErrorOr spawn(StringView path, ReadonlySpan arguments, ByteString working_directory = {}, KeepAsChild keep_as_child = KeepAsChild::No); static ErrorOr get_name(); enum class SetThreadName { No, Yes, }; static ErrorOr set_name(StringView, SetThreadName = SetThreadName::No); static void wait_for_debugger_and_break(); static ErrorOr is_being_debugged(); pid_t pid() const; #ifndef AK_OS_WINDOWS ErrorOr disown(); #endif ErrorOr wait_for_termination(); private: #ifndef AK_OS_WINDOWS Process(pid_t pid = -1) : m_pid(pid) , m_should_disown(true) { } pid_t m_pid; bool m_should_disown; #else Process(void* handle = 0) : m_handle(handle) { } void* m_handle; #endif }; }