2020-05-15 22:33:50 +00:00
|
|
|
/*
|
2021-04-28 20:46:44 +00:00
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
2020-05-15 22:33:50 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-05-15 22:33:50 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-05-16 19:05:13 +00:00
|
|
|
#include "Execution.h"
|
2020-08-11 11:24:46 +00:00
|
|
|
#include "Forward.h"
|
2023-12-16 14:19:34 +00:00
|
|
|
#include <AK/ByteString.h>
|
2021-04-26 01:28:37 +00:00
|
|
|
#include <AK/Debug.h>
|
2020-06-17 13:35:06 +00:00
|
|
|
#include <AK/Function.h>
|
2020-05-16 19:05:13 +00:00
|
|
|
#include <AK/JsonObject.h>
|
|
|
|
#include <AK/JsonValue.h>
|
|
|
|
#include <AK/OwnPtr.h>
|
|
|
|
#include <LibCore/ElapsedTimer.h>
|
2023-08-06 16:09:39 +00:00
|
|
|
#include <LibCore/EventReceiver.h>
|
2020-05-15 22:33:50 +00:00
|
|
|
|
2020-10-01 14:43:01 +00:00
|
|
|
namespace Shell {
|
|
|
|
|
2020-09-01 04:12:16 +00:00
|
|
|
struct LocalFrame;
|
|
|
|
|
2020-06-17 13:35:06 +00:00
|
|
|
class Job : public RefCounted<Job> {
|
2020-05-15 22:33:50 +00:00
|
|
|
public:
|
2023-12-16 14:19:34 +00:00
|
|
|
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))); }
|
2020-05-15 22:33:50 +00:00
|
|
|
|
2020-05-16 19:05:13 +00:00
|
|
|
~Job()
|
|
|
|
{
|
2021-04-26 01:28:37 +00:00
|
|
|
if constexpr (SHELL_JOB_DEBUG) {
|
|
|
|
if (m_active) {
|
|
|
|
auto elapsed = m_command_timer.elapsed();
|
|
|
|
// Don't mistake this for the command!
|
|
|
|
dbgln("Job entry '{}' deleted in {} ms", m_cmd, elapsed);
|
|
|
|
}
|
2020-05-24 16:31:39 +00:00
|
|
|
}
|
2020-05-16 19:05:13 +00:00
|
|
|
}
|
|
|
|
|
2020-09-01 04:12:16 +00:00
|
|
|
Function<void(RefPtr<Job>)> on_exit;
|
|
|
|
|
2020-08-06 11:48:45 +00:00
|
|
|
pid_t pgid() const { return m_pgid; }
|
2020-05-15 22:33:50 +00:00
|
|
|
pid_t pid() const { return m_pid; }
|
2023-12-16 14:19:34 +00:00
|
|
|
ByteString const& cmd() const { return m_cmd; }
|
2020-09-01 04:12:16 +00:00
|
|
|
const AST::Command& command() const { return *m_command; }
|
|
|
|
AST::Command* command_ptr() { return m_command; }
|
2020-05-15 22:33:50 +00:00
|
|
|
u64 job_id() const { return m_job_id; }
|
2020-05-16 19:05:13 +00:00
|
|
|
bool exited() const { return m_exited; }
|
2020-08-08 09:18:07 +00:00
|
|
|
bool signaled() const { return m_term_sig != -1; }
|
|
|
|
int exit_code() const
|
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(exited());
|
2020-08-08 09:18:07 +00:00
|
|
|
return m_exit_code;
|
|
|
|
}
|
|
|
|
int termination_signal() const
|
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(signaled());
|
2020-08-08 09:18:07 +00:00
|
|
|
return m_term_sig;
|
|
|
|
}
|
2020-06-17 13:35:06 +00:00
|
|
|
bool should_be_disowned() const { return m_should_be_disowned; }
|
|
|
|
void disown() { m_should_be_disowned = true; }
|
2020-05-16 19:05:13 +00:00
|
|
|
bool is_running_in_background() const { return m_running_in_background; }
|
2020-08-15 18:50:39 +00:00
|
|
|
bool should_announce_exit() const { return m_should_announce_exit; }
|
2020-12-06 17:22:43 +00:00
|
|
|
bool should_announce_signal() const { return m_should_announce_signal; }
|
2020-07-13 15:59:52 +00:00
|
|
|
bool is_suspended() const { return m_is_suspended; }
|
2021-03-29 21:49:02 +00:00
|
|
|
bool shell_did_continue() const { return m_shell_did_continue; }
|
2023-02-20 17:26:54 +00:00
|
|
|
void unblock();
|
2020-05-16 19:05:13 +00:00
|
|
|
|
|
|
|
Core::ElapsedTimer& timer() { return m_command_timer; }
|
|
|
|
|
2020-09-01 04:12:16 +00:00
|
|
|
void set_has_exit(int exit_code);
|
|
|
|
void set_signalled(int sig);
|
2020-05-16 19:05:13 +00:00
|
|
|
|
2020-07-13 15:59:52 +00:00
|
|
|
void set_is_suspended(bool value) const { m_is_suspended = value; }
|
2021-03-29 21:49:02 +00:00
|
|
|
void set_shell_did_continue(bool value) const { m_shell_did_continue = value; }
|
2020-07-13 15:59:52 +00:00
|
|
|
|
2020-05-16 19:05:13 +00:00
|
|
|
void set_running_in_background(bool running_in_background)
|
|
|
|
{
|
|
|
|
m_running_in_background = running_in_background;
|
|
|
|
}
|
2020-05-15 22:33:50 +00:00
|
|
|
|
2020-08-15 18:50:39 +00:00
|
|
|
void set_should_announce_exit(bool value) { m_should_announce_exit = value; }
|
2020-12-06 17:22:43 +00:00
|
|
|
void set_should_announce_signal(bool value) { m_should_announce_signal = value; }
|
2020-08-15 18:50:39 +00:00
|
|
|
|
2020-05-24 16:31:39 +00:00
|
|
|
void deactivate() const { m_active = false; }
|
|
|
|
|
2020-08-06 13:09:25 +00:00
|
|
|
enum class PrintStatusMode {
|
|
|
|
Basic,
|
|
|
|
OnlyPID,
|
|
|
|
ListAll,
|
|
|
|
};
|
|
|
|
|
|
|
|
bool print_status(PrintStatusMode);
|
|
|
|
|
2020-05-15 22:33:50 +00:00
|
|
|
private:
|
2023-12-16 14:19:34 +00:00
|
|
|
Job(pid_t pid, unsigned pgid, ByteString cmd, u64 job_id, AST::Command&& command);
|
2020-08-06 11:48:45 +00:00
|
|
|
|
|
|
|
pid_t m_pgid { 0 };
|
2020-05-15 22:33:50 +00:00
|
|
|
pid_t m_pid { 0 };
|
|
|
|
u64 m_job_id { 0 };
|
2023-12-16 14:19:34 +00:00
|
|
|
ByteString m_cmd;
|
2020-05-16 19:05:13 +00:00
|
|
|
bool m_exited { false };
|
|
|
|
bool m_running_in_background { false };
|
2020-08-15 18:50:39 +00:00
|
|
|
bool m_should_announce_exit { false };
|
2020-12-06 17:22:43 +00:00
|
|
|
bool m_should_announce_signal { true };
|
2020-05-16 19:05:13 +00:00
|
|
|
int m_exit_code { -1 };
|
2020-08-08 09:18:07 +00:00
|
|
|
int m_term_sig { -1 };
|
2020-05-16 19:05:13 +00:00
|
|
|
Core::ElapsedTimer m_command_timer;
|
2020-05-24 16:31:39 +00:00
|
|
|
mutable bool m_active { true };
|
2020-07-13 15:59:52 +00:00
|
|
|
mutable bool m_is_suspended { false };
|
2021-03-29 21:49:02 +00:00
|
|
|
mutable bool m_shell_did_continue { false };
|
2020-06-17 13:35:06 +00:00
|
|
|
bool m_should_be_disowned { false };
|
2020-09-01 04:12:16 +00:00
|
|
|
OwnPtr<AST::Command> m_command;
|
2020-05-15 22:33:50 +00:00
|
|
|
};
|
2020-10-01 14:43:01 +00:00
|
|
|
|
|
|
|
}
|