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