Coverage Report

Created: 2025-11-02 07:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/serenity/Userland/Libraries/LibShell/Job.h
Line
Count
Source
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/ByteString.h>
12
#include <AK/Debug.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/EventReceiver.h>
19
20
namespace Shell {
21
22
struct LocalFrame;
23
24
class Job : public RefCounted<Job> {
25
public:
26
0
    static NonnullRefPtr<Job> create(pid_t pid, pid_t pgid, ByteString cmd, u64 job_id, AST::Command&& command) { return adopt_ref(*new Job(pid, pgid, move(cmd), job_id, move(command))); }
27
28
    ~Job()
29
0
    {
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
0
    }
38
39
    Function<void(RefPtr<Job>)> on_exit;
40
41
0
    pid_t pgid() const { return m_pgid; }
42
0
    pid_t pid() const { return m_pid; }
43
0
    ByteString const& cmd() const { return m_cmd; }
44
0
    const AST::Command& command() const { return *m_command; }
45
0
    AST::Command* command_ptr() { return m_command; }
46
0
    u64 job_id() const { return m_job_id; }
47
0
    bool exited() const { return m_exited; }
48
0
    bool signaled() const { return m_term_sig != -1; }
49
    int exit_code() const
50
0
    {
51
0
        VERIFY(exited());
52
0
        return m_exit_code;
53
0
    }
54
    int termination_signal() const
55
0
    {
56
0
        VERIFY(signaled());
57
0
        return m_term_sig;
58
0
    }
59
0
    bool should_be_disowned() const { return m_should_be_disowned; }
60
0
    void disown() { m_should_be_disowned = true; }
61
0
    bool is_running_in_background() const { return m_running_in_background; }
62
0
    bool should_announce_exit() const { return m_should_announce_exit; }
63
0
    bool should_announce_signal() const { return m_should_announce_signal; }
64
0
    bool is_suspended() const { return m_is_suspended; }
65
0
    bool shell_did_continue() const { return m_shell_did_continue; }
66
    void unblock();
67
68
0
    Core::ElapsedTimer& timer() { return m_command_timer; }
69
70
    void set_has_exit(int exit_code);
71
    void set_signalled(int sig);
72
73
0
    void set_is_suspended(bool value) const { m_is_suspended = value; }
74
0
    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
0
    {
78
0
        m_running_in_background = running_in_background;
79
0
    }
80
81
0
    void set_should_announce_exit(bool value) { m_should_announce_exit = value; }
82
0
    void set_should_announce_signal(bool value) { m_should_announce_signal = value; }
83
84
0
    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
94
private:
95
    Job(pid_t pid, unsigned pgid, ByteString 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
    ByteString 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
}