Job.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include "Execution.h"
  8. #include "Forward.h"
  9. #include <AK/Debug.h>
  10. #include <AK/Function.h>
  11. #include <AK/JsonObject.h>
  12. #include <AK/JsonValue.h>
  13. #include <AK/OwnPtr.h>
  14. #include <AK/String.h>
  15. #include <LibCore/ElapsedTimer.h>
  16. #include <LibCore/Object.h>
  17. namespace Shell {
  18. struct LocalFrame;
  19. class Job : public RefCounted<Job> {
  20. public:
  21. static NonnullRefPtr<Job> create(pid_t pid, pid_t pgid, String cmd, u64 job_id, AST::Command&& command) { return adopt_ref(*new Job(pid, pgid, move(cmd), job_id, move(command))); }
  22. ~Job()
  23. {
  24. if constexpr (SHELL_JOB_DEBUG) {
  25. if (m_active) {
  26. auto elapsed = m_command_timer.elapsed();
  27. // Don't mistake this for the command!
  28. dbgln("Job entry '{}' deleted in {} ms", m_cmd, elapsed);
  29. }
  30. }
  31. }
  32. Function<void(RefPtr<Job>)> on_exit;
  33. pid_t pgid() const { return m_pgid; }
  34. pid_t pid() const { return m_pid; }
  35. const String& cmd() const { return m_cmd; }
  36. const AST::Command& command() const { return *m_command; }
  37. AST::Command* command_ptr() { return m_command; }
  38. u64 job_id() const { return m_job_id; }
  39. bool exited() const { return m_exited; }
  40. bool signaled() const { return m_term_sig != -1; }
  41. int exit_code() const
  42. {
  43. VERIFY(exited());
  44. return m_exit_code;
  45. }
  46. int termination_signal() const
  47. {
  48. VERIFY(signaled());
  49. return m_term_sig;
  50. }
  51. bool should_be_disowned() const { return m_should_be_disowned; }
  52. void disown() { m_should_be_disowned = true; }
  53. bool is_running_in_background() const { return m_running_in_background; }
  54. bool should_announce_exit() const { return m_should_announce_exit; }
  55. bool should_announce_signal() const { return m_should_announce_signal; }
  56. bool is_suspended() const { return m_is_suspended; }
  57. bool shell_did_continue() const { return m_shell_did_continue; }
  58. void unblock() const;
  59. Core::ElapsedTimer& timer() { return m_command_timer; }
  60. void set_has_exit(int exit_code);
  61. void set_signalled(int sig);
  62. void set_is_suspended(bool value) const { m_is_suspended = value; }
  63. void set_shell_did_continue(bool value) const { m_shell_did_continue = value; }
  64. void set_running_in_background(bool running_in_background)
  65. {
  66. m_running_in_background = running_in_background;
  67. }
  68. void set_should_announce_exit(bool value) { m_should_announce_exit = value; }
  69. void set_should_announce_signal(bool value) { m_should_announce_signal = value; }
  70. void deactivate() const { m_active = false; }
  71. enum class PrintStatusMode {
  72. Basic,
  73. OnlyPID,
  74. ListAll,
  75. };
  76. bool print_status(PrintStatusMode);
  77. private:
  78. Job(pid_t pid, unsigned pgid, String cmd, u64 job_id, AST::Command&& command);
  79. pid_t m_pgid { 0 };
  80. pid_t m_pid { 0 };
  81. u64 m_job_id { 0 };
  82. String m_cmd;
  83. bool m_exited { false };
  84. bool m_running_in_background { false };
  85. bool m_should_announce_exit { false };
  86. bool m_should_announce_signal { true };
  87. int m_exit_code { -1 };
  88. int m_term_sig { -1 };
  89. Core::ElapsedTimer m_command_timer;
  90. mutable bool m_active { true };
  91. mutable bool m_is_suspended { false };
  92. mutable bool m_shell_did_continue { false };
  93. bool m_should_be_disowned { false };
  94. OwnPtr<AST::Command> m_command;
  95. };
  96. }