Process.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <andreas@ladybird.org>
  3. * Copyright (c) 2022, MacDue <macdue@dueutil.tech>
  4. * Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
  5. * Copyright (c) 2024, Tim Flynn <trflynn89@serenityos.org>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #pragma once
  10. #include <AK/ByteString.h>
  11. #include <AK/Forward.h>
  12. #include <AK/Span.h>
  13. #include <LibCore/File.h>
  14. namespace Core {
  15. namespace FileAction {
  16. struct OpenFile {
  17. ByteString path;
  18. File::OpenMode mode = File::OpenMode::NotOpen;
  19. int fd = -1;
  20. mode_t permissions = 0600;
  21. };
  22. struct CloseFile {
  23. int fd { -1 };
  24. };
  25. // FIXME: Implement other file actions
  26. }
  27. struct ProcessSpawnOptions {
  28. StringView name {};
  29. ByteString executable {};
  30. bool search_for_executable_in_path { false };
  31. Vector<ByteString> const& arguments {};
  32. Optional<ByteString> working_directory {};
  33. using FileActionType = Variant<FileAction::OpenFile, FileAction::CloseFile>;
  34. Vector<FileActionType> file_actions {};
  35. };
  36. class Process {
  37. AK_MAKE_NONCOPYABLE(Process);
  38. public:
  39. enum class KeepAsChild {
  40. Yes,
  41. No
  42. };
  43. Process(Process&& other)
  44. : m_pid(exchange(other.m_pid, 0))
  45. , m_should_disown(exchange(other.m_should_disown, false))
  46. {
  47. }
  48. Process& operator=(Process&& other)
  49. {
  50. m_pid = exchange(other.m_pid, 0);
  51. m_should_disown = exchange(other.m_should_disown, false);
  52. return *this;
  53. }
  54. ~Process()
  55. {
  56. (void)disown();
  57. }
  58. static ErrorOr<Process> spawn(ProcessSpawnOptions const& options);
  59. static Process current();
  60. // FIXME: Make the following 2 functions return Process instance or delete them.
  61. static ErrorOr<pid_t> spawn(StringView path, ReadonlySpan<ByteString> arguments, ByteString working_directory = {}, KeepAsChild keep_as_child = KeepAsChild::No);
  62. static ErrorOr<pid_t> spawn(StringView path, ReadonlySpan<StringView> arguments, ByteString working_directory = {}, KeepAsChild keep_as_child = KeepAsChild::No);
  63. static ErrorOr<String> get_name();
  64. enum class SetThreadName {
  65. No,
  66. Yes,
  67. };
  68. static ErrorOr<void> set_name(StringView, SetThreadName = SetThreadName::No);
  69. static void wait_for_debugger_and_break();
  70. static ErrorOr<bool> is_being_debugged();
  71. pid_t pid() const { return m_pid; }
  72. ErrorOr<void> disown();
  73. // FIXME: Make it return an exit code.
  74. ErrorOr<bool> wait_for_termination();
  75. private:
  76. Process(pid_t pid)
  77. : m_pid(pid)
  78. , m_should_disown(true)
  79. {
  80. }
  81. pid_t m_pid;
  82. bool m_should_disown;
  83. };
  84. }