Job.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (c) 2020, The SerenityOS developers.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include "Execution.h"
  28. #include "Forward.h"
  29. #include <AK/Function.h>
  30. #include <AK/JsonObject.h>
  31. #include <AK/JsonValue.h>
  32. #include <AK/OwnPtr.h>
  33. #include <AK/String.h>
  34. #include <LibCore/ElapsedTimer.h>
  35. #include <LibCore/Object.h>
  36. #define JOB_TIME_INFO
  37. #ifndef __serenity__
  38. # undef JOB_TIME_INFO
  39. #endif
  40. struct LocalFrame;
  41. class Job : public RefCounted<Job> {
  42. public:
  43. static NonnullRefPtr<Job> create(pid_t pid, pid_t pgid, String cmd, u64 job_id, AST::Command&& command) { return adopt(*new Job(pid, pgid, move(cmd), job_id, move(command))); }
  44. ~Job()
  45. {
  46. #ifdef JOB_TIME_INFO
  47. if (m_active) {
  48. auto elapsed = m_command_timer.elapsed();
  49. // Don't mistake this for the command!
  50. dbg() << "Job entry \"" << m_cmd << "\" deleted in " << elapsed << " ms";
  51. }
  52. #endif
  53. }
  54. Function<void(RefPtr<Job>)> on_exit;
  55. pid_t pgid() const { return m_pgid; }
  56. pid_t pid() const { return m_pid; }
  57. const String& cmd() const { return m_cmd; }
  58. const AST::Command& command() const { return *m_command; }
  59. AST::Command* command_ptr() { return m_command; }
  60. u64 job_id() const { return m_job_id; }
  61. bool exited() const { return m_exited; }
  62. bool signaled() const { return m_term_sig != -1; }
  63. int exit_code() const
  64. {
  65. ASSERT(exited());
  66. return m_exit_code;
  67. }
  68. int termination_signal() const
  69. {
  70. ASSERT(signaled());
  71. return m_term_sig;
  72. }
  73. bool should_be_disowned() const { return m_should_be_disowned; }
  74. void disown() { m_should_be_disowned = true; }
  75. bool is_running_in_background() const { return m_running_in_background; }
  76. bool should_announce_exit() const { return m_should_announce_exit; }
  77. bool is_suspended() const { return m_is_suspended; }
  78. void unblock() const;
  79. Core::ElapsedTimer& timer() { return m_command_timer; }
  80. void set_has_exit(int exit_code);
  81. void set_signalled(int sig);
  82. void set_is_suspended(bool value) const { m_is_suspended = value; }
  83. void set_running_in_background(bool running_in_background)
  84. {
  85. m_running_in_background = running_in_background;
  86. }
  87. void set_should_announce_exit(bool value) { m_should_announce_exit = value; }
  88. void deactivate() const { m_active = false; }
  89. enum class PrintStatusMode {
  90. Basic,
  91. OnlyPID,
  92. ListAll,
  93. };
  94. bool print_status(PrintStatusMode);
  95. private:
  96. Job(pid_t pid, unsigned pgid, String cmd, u64 job_id, AST::Command&& command);
  97. pid_t m_pgid { 0 };
  98. pid_t m_pid { 0 };
  99. u64 m_job_id { 0 };
  100. String m_cmd;
  101. bool m_exited { false };
  102. bool m_running_in_background { false };
  103. bool m_should_announce_exit { false };
  104. int m_exit_code { -1 };
  105. int m_term_sig { -1 };
  106. Core::ElapsedTimer m_command_timer;
  107. mutable bool m_active { true };
  108. mutable bool m_is_suspended { false };
  109. bool m_should_be_disowned { false };
  110. OwnPtr<AST::Command> m_command;
  111. };