2021-08-06 01:04:11 +02:00
|
|
|
/*
|
2024-10-04 13:19:50 +02:00
|
|
|
* Copyright (c) 2021, Andreas Kling <andreas@ladybird.org>
|
2022-05-10 00:24:15 +01:00
|
|
|
* Copyright (c) 2022, MacDue <macdue@dueutil.tech>
|
2023-01-22 19:55:20 +00:00
|
|
|
* Copyright (c) 2023, 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
#include <AK/ByteString.h>
|
2021-08-06 01:04:11 +02:00
|
|
|
#include <AK/Forward.h>
|
2025-02-13 10:43:55 +05:00
|
|
|
#include <AK/String.h>
|
2023-11-06 23:26:04 -05:00
|
|
|
#include <LibCore/File.h>
|
2021-08-06 01:04:11 +02:00
|
|
|
|
|
|
|
namespace Core {
|
|
|
|
|
2023-11-06 23:26:04 -05:00
|
|
|
namespace FileAction {
|
|
|
|
|
|
|
|
struct OpenFile {
|
2023-12-16 17:49:34 +03:30
|
|
|
ByteString path;
|
2023-11-06 23:26:04 -05:00
|
|
|
File::OpenMode mode = File::OpenMode::NotOpen;
|
|
|
|
int fd = -1;
|
|
|
|
mode_t permissions = 0600;
|
|
|
|
};
|
|
|
|
|
2024-04-23 15:31:07 -04:00
|
|
|
struct CloseFile {
|
|
|
|
int fd { -1 };
|
|
|
|
};
|
|
|
|
|
2024-11-27 15:43:12 -06:00
|
|
|
struct DupFd {
|
|
|
|
int write_fd { -1 };
|
|
|
|
int fd { -1 };
|
|
|
|
};
|
2023-11-06 23:26:04 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ProcessSpawnOptions {
|
2024-04-23 16:35:36 -04:00
|
|
|
StringView name {};
|
|
|
|
ByteString executable {};
|
2024-01-16 13:42:30 +00:00
|
|
|
bool search_for_executable_in_path { false };
|
2024-04-23 15:31:07 -04:00
|
|
|
Vector<ByteString> const& arguments {};
|
|
|
|
Optional<ByteString> working_directory {};
|
2024-04-23 16:35:36 -04:00
|
|
|
|
2024-11-27 15:43:12 -06:00
|
|
|
using FileActionType = Variant<FileAction::OpenFile, FileAction::CloseFile, FileAction::DupFd>;
|
2024-04-27 08:39:41 -04:00
|
|
|
Vector<FileActionType> file_actions {};
|
2023-11-06 23:26:04 -05:00
|
|
|
};
|
|
|
|
|
2021-08-06 01:04:11 +02:00
|
|
|
class Process {
|
2023-11-06 23:26:04 -05:00
|
|
|
AK_MAKE_NONCOPYABLE(Process);
|
|
|
|
|
2021-08-06 01:04:11 +02:00
|
|
|
public:
|
2023-03-15 16:49:13 +00:00
|
|
|
enum class KeepAsChild {
|
|
|
|
Yes,
|
|
|
|
No
|
|
|
|
};
|
|
|
|
|
2024-11-17 22:07:43 +05:00
|
|
|
Process(Process&& other);
|
|
|
|
Process& operator=(Process&& other);
|
|
|
|
~Process();
|
2023-11-06 23:26:04 -05:00
|
|
|
|
|
|
|
static ErrorOr<Process> spawn(ProcessSpawnOptions const& options);
|
2024-06-29 22:24:01 -06:00
|
|
|
static Process current();
|
2023-11-06 23:26:04 -05:00
|
|
|
|
2024-11-17 20:11:13 +05:00
|
|
|
static ErrorOr<Process> spawn(StringView path, ReadonlySpan<ByteString> arguments, ByteString working_directory = {}, KeepAsChild keep_as_child = KeepAsChild::No);
|
|
|
|
static ErrorOr<Process> spawn(StringView path, ReadonlySpan<StringView> arguments, ByteString working_directory = {}, KeepAsChild keep_as_child = KeepAsChild::No);
|
2023-11-06 23:26:04 -05:00
|
|
|
|
2023-01-22 19:55:20 +00:00
|
|
|
static ErrorOr<String> get_name();
|
|
|
|
enum class SetThreadName {
|
|
|
|
No,
|
|
|
|
Yes,
|
|
|
|
};
|
|
|
|
static ErrorOr<void> set_name(StringView, SetThreadName = SetThreadName::No);
|
2023-08-02 10:27:58 +02:00
|
|
|
|
2023-08-02 14:09:19 +02:00
|
|
|
static void wait_for_debugger_and_break();
|
2023-08-02 10:27:58 +02:00
|
|
|
static ErrorOr<bool> is_being_debugged();
|
2023-11-06 23:26:04 -05:00
|
|
|
|
2024-11-17 22:07:43 +05:00
|
|
|
pid_t pid() const;
|
2023-11-06 23:26:04 -05:00
|
|
|
|
2024-11-17 22:07:43 +05:00
|
|
|
#ifndef AK_OS_WINDOWS
|
2023-11-06 23:26:04 -05:00
|
|
|
ErrorOr<void> disown();
|
2024-11-17 22:07:43 +05:00
|
|
|
#endif
|
2023-11-06 23:26:04 -05:00
|
|
|
|
2024-11-18 06:18:40 +05:00
|
|
|
ErrorOr<int> wait_for_termination();
|
2023-11-06 23:26:04 -05:00
|
|
|
|
|
|
|
private:
|
2024-11-17 22:07:43 +05:00
|
|
|
#ifndef AK_OS_WINDOWS
|
2024-11-17 20:11:13 +05:00
|
|
|
Process(pid_t pid = -1)
|
2023-11-06 23:26:04 -05:00
|
|
|
: m_pid(pid)
|
|
|
|
, m_should_disown(true)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
pid_t m_pid;
|
|
|
|
bool m_should_disown;
|
2024-11-17 22:07:43 +05:00
|
|
|
#else
|
|
|
|
Process(void* handle = 0)
|
|
|
|
: m_handle(handle)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void* m_handle;
|
|
|
|
#endif
|
2021-08-06 01:04:11 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|