Coverage Report

Created: 2026-09-14 07:15

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
#include <termios.h>
20
21
namespace Shell {
22
23
struct LocalFrame;
24
25
class Job : public RefCounted<Job> {
26
public:
27
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))); }
28
29
    ~Job()
30
0
    {
31
        if constexpr (SHELL_JOB_DEBUG) {
32
            if (m_active) {
33
                auto elapsed = m_command_timer.elapsed();
34
                // Don't mistake this for the command!
35
                dbgln("Job entry '{}' deleted in {} ms", m_cmd, elapsed);
36
            }
37
        }
38
0
    }
39
40
    Function<void(RefPtr<Job>)> on_exit;
41
42
0
    pid_t pgid() const { return m_pgid; }
43
0
    pid_t pid() const { return m_pid; }
44
0
    ByteString const& cmd() const { return m_cmd; }
45
0
    const AST::Command& command() const { return *m_command; }
46
0
    AST::Command* command_ptr() { return m_command; }
47
0
    u64 job_id() const { return m_job_id; }
48
0
    bool exited() const { return m_exited; }
49
0
    bool signaled() const { return m_term_sig != -1; }
50
    int exit_code() const
51
0
    {
52
0
        VERIFY(exited());
53
0
        return m_exit_code;
54
0
    }
55
    int termination_signal() const
56
0
    {
57
0
        VERIFY(signaled());
58
0
        return m_term_sig;
59
0
    }
60
0
    bool should_be_disowned() const { return m_should_be_disowned; }
61
0
    void disown() { m_should_be_disowned = true; }
62
0
    bool is_running_in_background() const { return m_running_in_background; }
63
0
    bool should_announce_exit() const { return m_should_announce_exit; }
64
0
    bool should_announce_signal() const { return m_should_announce_signal; }
65
0
    bool is_suspended() const { return m_is_suspended; }
66
0
    bool shell_did_continue() const { return m_shell_did_continue; }
67
    void unblock();
68
69
0
    Core::ElapsedTimer& timer() { return m_command_timer; }
70
71
    void set_has_exit(int exit_code);
72
    void set_signalled(int sig);
73
74
0
    void set_is_suspended(bool value) const { m_is_suspended = value; }
75
0
    void set_shell_did_continue(bool value) const { m_shell_did_continue = value; }
76
77
0
    Optional<struct termios> const& saved_termios() const { return m_saved_termios; }
78
0
    void set_saved_termios(struct termios const& value) const { m_saved_termios = value; }
79
80
    void set_running_in_background(bool running_in_background)
81
0
    {
82
0
        m_running_in_background = running_in_background;
83
0
    }
84
85
0
    void set_should_announce_exit(bool value) { m_should_announce_exit = value; }
86
0
    void set_should_announce_signal(bool value) { m_should_announce_signal = value; }
87
88
0
    void deactivate() const { m_active = false; }
89
90
    enum class PrintStatusMode {
91
        Basic,
92
        OnlyPID,
93
        ListAll,
94
    };
95
96
    bool print_status(PrintStatusMode);
97
98
private:
99
    Job(pid_t pid, unsigned pgid, ByteString cmd, u64 job_id, AST::Command&& command);
100
101
    pid_t m_pgid { 0 };
102
    pid_t m_pid { 0 };
103
    u64 m_job_id { 0 };
104
    ByteString m_cmd;
105
    bool m_exited { false };
106
    bool m_running_in_background { false };
107
    bool m_should_announce_exit { false };
108
    bool m_should_announce_signal { true };
109
    int m_exit_code { -1 };
110
    int m_term_sig { -1 };
111
    Core::ElapsedTimer m_command_timer;
112
    mutable bool m_active { true };
113
    mutable bool m_is_suspended { false };
114
    mutable bool m_shell_did_continue { false };
115
    mutable Optional<struct termios> m_saved_termios;
116
    bool m_should_be_disowned { false };
117
    OwnPtr<AST::Command> m_command;
118
};
119
120
}