Job.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. class Job : public RefCounted<Job> {
  41. public:
  42. static NonnullRefPtr<Job> create(pid_t pid, pid_t pgid, String command, u64 job_id, AST::Pipeline* pipeline = nullptr) { return adopt(*new Job(pid, pgid, move(command), job_id, pipeline)); }
  43. ~Job()
  44. {
  45. #ifdef JOB_TIME_INFO
  46. if (m_active) {
  47. auto elapsed = m_command_timer.elapsed();
  48. dbg() << "Command \"" << m_cmd << "\" finished in " << elapsed << " ms";
  49. }
  50. #endif
  51. }
  52. pid_t pgid() const { return m_pgid; }
  53. pid_t pid() const { return m_pid; }
  54. const String& cmd() const { return m_cmd; }
  55. u64 job_id() const { return m_job_id; }
  56. bool exited() const { return m_exited; }
  57. bool signaled() const { return m_term_sig != -1; }
  58. int exit_code() const
  59. {
  60. ASSERT(exited());
  61. return m_exit_code;
  62. }
  63. int termination_signal() const
  64. {
  65. ASSERT(signaled());
  66. return m_term_sig;
  67. }
  68. bool should_be_disowned() const { return m_should_be_disowned; }
  69. void disown() { m_should_be_disowned = true; }
  70. bool is_running_in_background() const { return m_running_in_background; }
  71. bool is_suspended() const { return m_is_suspended; }
  72. void unblock() const
  73. {
  74. if (!m_exited && on_exit)
  75. on_exit(*this);
  76. }
  77. Function<void(RefPtr<Job>)> on_exit;
  78. Core::ElapsedTimer& timer() { return m_command_timer; }
  79. void set_has_exit(int exit_code)
  80. {
  81. if (m_exited)
  82. return;
  83. m_exit_code = exit_code;
  84. m_exited = true;
  85. if (on_exit)
  86. on_exit(*this);
  87. }
  88. void set_signalled(int sig)
  89. {
  90. if (m_exited)
  91. return;
  92. m_exited = true;
  93. m_exit_code = 126;
  94. m_term_sig = sig;
  95. if (on_exit)
  96. on_exit(*this);
  97. }
  98. void set_is_suspended(bool value) const { m_is_suspended = value; }
  99. void set_running_in_background(bool running_in_background)
  100. {
  101. m_running_in_background = running_in_background;
  102. }
  103. void deactivate() const { m_active = false; }
  104. enum class PrintStatusMode {
  105. Basic,
  106. OnlyPID,
  107. ListAll,
  108. };
  109. bool print_status(PrintStatusMode);
  110. private:
  111. Job(pid_t pid, unsigned pgid, String cmd, u64 job_id, AST::Pipeline* pipeline)
  112. : m_pgid(pgid)
  113. , m_pid(pid)
  114. , m_job_id(job_id)
  115. , m_cmd(move(cmd))
  116. {
  117. if (pipeline)
  118. m_pipeline = *pipeline;
  119. set_running_in_background(false);
  120. m_command_timer.start();
  121. }
  122. pid_t m_pgid { 0 };
  123. pid_t m_pid { 0 };
  124. u64 m_job_id { 0 };
  125. String m_cmd;
  126. bool m_exited { false };
  127. bool m_running_in_background { false };
  128. int m_exit_code { -1 };
  129. int m_term_sig { -1 };
  130. Core::ElapsedTimer m_command_timer;
  131. mutable bool m_active { true };
  132. mutable bool m_is_suspended { false };
  133. bool m_should_be_disowned { false };
  134. RefPtr<AST::Pipeline> m_pipeline;
  135. };