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