2016-05-03 04:41:17 +00:00
|
|
|
// Prototypes for utilities for keeping track of jobs, processes and subshells, as well as signal
|
|
|
|
// handling functions for tracking children. These functions do not themselves launch new processes,
|
|
|
|
// the exec library will call proc to create representations of the running jobs as needed.
|
2005-10-04 15:11:39 +00:00
|
|
|
#ifndef FISH_PROC_H
|
|
|
|
#define FISH_PROC_H
|
2016-04-21 06:00:54 +00:00
|
|
|
#include "config.h" // IWYU pragma: keep
|
2005-10-04 15:11:39 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
#include <signal.h>
|
2016-04-21 06:00:54 +00:00
|
|
|
#include <stddef.h>
|
2016-05-03 04:41:17 +00:00
|
|
|
#include <sys/time.h> // IWYU pragma: keep
|
2019-02-25 21:12:09 +00:00
|
|
|
#include <sys/wait.h>
|
2016-04-21 06:00:54 +00:00
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
2017-02-11 02:47:02 +00:00
|
|
|
|
2018-12-31 05:53:26 +00:00
|
|
|
#include <deque>
|
2017-02-11 02:47:02 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <vector>
|
2005-10-04 15:11:39 +00:00
|
|
|
|
2012-01-30 06:06:58 +00:00
|
|
|
#include "common.h"
|
2019-02-21 06:42:58 +00:00
|
|
|
#include "event.h"
|
2019-12-08 21:03:42 +00:00
|
|
|
#include "global_safety.h"
|
2016-05-03 04:41:17 +00:00
|
|
|
#include "io.h"
|
2013-12-26 20:24:00 +00:00
|
|
|
#include "parse_tree.h"
|
2018-02-11 03:16:35 +00:00
|
|
|
#include "tnode.h"
|
2019-02-17 01:39:14 +00:00
|
|
|
#include "topic_monitor.h"
|
2005-10-04 15:11:39 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Types of processes.
|
2019-03-24 19:29:25 +00:00
|
|
|
enum class process_type_t {
|
2016-05-03 04:41:17 +00:00
|
|
|
/// A regular external command.
|
2019-03-24 19:29:25 +00:00
|
|
|
external,
|
2016-05-03 04:41:17 +00:00
|
|
|
/// A builtin command.
|
2019-03-24 19:29:25 +00:00
|
|
|
builtin,
|
2016-05-03 04:41:17 +00:00
|
|
|
/// A shellscript function.
|
2019-03-24 19:29:25 +00:00
|
|
|
function,
|
2016-05-03 04:41:17 +00:00
|
|
|
/// A block of commands, represented as a node.
|
2019-03-24 19:29:25 +00:00
|
|
|
block_node,
|
2016-05-03 04:41:17 +00:00
|
|
|
/// The exec builtin.
|
2019-03-24 19:29:25 +00:00
|
|
|
exec,
|
2013-12-20 22:37:40 +00:00
|
|
|
};
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2019-03-24 19:12:08 +00:00
|
|
|
enum class job_control_t {
|
|
|
|
all,
|
|
|
|
interactive,
|
|
|
|
none,
|
2016-05-03 04:41:17 +00:00
|
|
|
};
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2019-02-25 18:05:42 +00:00
|
|
|
/// A proc_status_t is a value type that encapsulates logic around exited vs stopped vs signaled,
|
|
|
|
/// etc.
|
|
|
|
class proc_status_t {
|
|
|
|
int status_{};
|
|
|
|
|
|
|
|
explicit proc_status_t(int status) : status_(status) {}
|
|
|
|
|
|
|
|
/// Encode a return value \p ret and signal \p sig into a status value like waitpid() does.
|
|
|
|
static constexpr int w_exitcode(int ret, int sig) {
|
|
|
|
#ifdef W_EXITCODE
|
|
|
|
return W_EXITCODE(ret, sig);
|
|
|
|
#else
|
|
|
|
return ((ret) << 8 | (sig));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
proc_status_t() = default;
|
|
|
|
|
|
|
|
/// Construct from a status returned from a waitpid call.
|
|
|
|
static proc_status_t from_waitpid(int status) { return proc_status_t(status); }
|
|
|
|
|
|
|
|
/// Construct directly from an exit code.
|
|
|
|
static proc_status_t from_exit_code(int ret) {
|
|
|
|
// Some paranoia.
|
|
|
|
constexpr int zerocode = w_exitcode(0, 0);
|
|
|
|
static_assert(WIFEXITED(zerocode), "Synthetic exit status not reported as exited");
|
|
|
|
return proc_status_t(w_exitcode(ret, 0 /* sig */));
|
|
|
|
}
|
|
|
|
|
2019-12-18 02:15:42 +00:00
|
|
|
/// Construct directly from a signal.
|
|
|
|
static proc_status_t from_signal(int sig) {
|
|
|
|
return proc_status_t(w_exitcode(0 /* ret */, sig));
|
|
|
|
}
|
|
|
|
|
2019-02-25 18:05:42 +00:00
|
|
|
/// \return if we are stopped (as in SIGSTOP).
|
|
|
|
bool stopped() const { return WIFSTOPPED(status_); }
|
|
|
|
|
|
|
|
/// \return if we exited normally (not a signal).
|
|
|
|
bool normal_exited() const { return WIFEXITED(status_); }
|
|
|
|
|
|
|
|
/// \return if we exited because of a signal.
|
|
|
|
bool signal_exited() const { return WIFSIGNALED(status_); }
|
|
|
|
|
|
|
|
/// \return the signal code, given that we signal exited.
|
|
|
|
int signal_code() const {
|
|
|
|
assert(signal_exited() && "Process is not signal exited");
|
|
|
|
return WTERMSIG(status_);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \return the exit code, given that we normal exited.
|
|
|
|
int exit_code() const {
|
|
|
|
assert(normal_exited() && "Process is not normal exited");
|
|
|
|
return WEXITSTATUS(status_);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \return if this status represents success.
|
|
|
|
bool is_success() const { return normal_exited() && exit_code() == EXIT_SUCCESS; }
|
|
|
|
|
|
|
|
/// \return the value appropriate to populate $status.
|
|
|
|
int status_value() const {
|
|
|
|
if (signal_exited()) {
|
|
|
|
return 128 + signal_code();
|
|
|
|
} else if (normal_exited()) {
|
|
|
|
return exit_code();
|
|
|
|
} else {
|
|
|
|
DIE("Process is not exited");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-02-13 23:17:07 +00:00
|
|
|
/// A structure representing a "process" internal to fish. This is backed by a pthread instead of a
|
|
|
|
/// separate process.
|
|
|
|
class internal_proc_t {
|
2019-05-28 16:38:45 +00:00
|
|
|
/// An identifier for internal processes.
|
|
|
|
/// This is used for logging purposes only.
|
|
|
|
const uint64_t internal_proc_id_;
|
|
|
|
|
2019-02-13 23:17:07 +00:00
|
|
|
/// Whether the process has exited.
|
|
|
|
std::atomic<bool> exited_{};
|
|
|
|
|
|
|
|
/// If the process has exited, its status code.
|
2019-02-25 18:05:42 +00:00
|
|
|
std::atomic<proc_status_t> status_{};
|
2019-02-13 23:17:07 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
/// \return if this process has exited.
|
2019-02-25 18:05:42 +00:00
|
|
|
bool exited() const { return exited_.load(std::memory_order_acquire); }
|
2019-02-13 23:17:07 +00:00
|
|
|
|
|
|
|
/// Mark this process as exited, with the given status.
|
2019-02-25 18:05:42 +00:00
|
|
|
void mark_exited(proc_status_t status);
|
2019-02-13 23:17:07 +00:00
|
|
|
|
2019-02-25 18:05:42 +00:00
|
|
|
proc_status_t get_status() const {
|
2019-02-13 23:17:07 +00:00
|
|
|
assert(exited() && "Process is not exited");
|
2019-02-25 18:05:42 +00:00
|
|
|
return status_.load(std::memory_order_relaxed);
|
2019-02-13 23:17:07 +00:00
|
|
|
}
|
2019-05-28 16:38:45 +00:00
|
|
|
|
|
|
|
uint64_t get_id() const { return internal_proc_id_; }
|
|
|
|
|
|
|
|
internal_proc_t();
|
2019-02-13 23:17:07 +00:00
|
|
|
};
|
|
|
|
|
2019-06-23 19:39:29 +00:00
|
|
|
/// 0 should not be used; although it is not a valid PGID in userspace,
|
|
|
|
/// the Linux kernel will use it for kernel processes.
|
|
|
|
/// -1 should not be used; it is a possible return value of the getpgid()
|
|
|
|
/// function
|
|
|
|
enum { INVALID_PID = -2 };
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// A structure representing a single fish process. Contains variables for tracking process state
|
|
|
|
/// and the process argument list. Actually, a fish process can be either a regular external
|
|
|
|
/// process, an internal builtin which may or may not spawn a fake IO process during execution, a
|
|
|
|
/// shellscript function or a block of commands to be evaluated by calling eval. Lastly, this
|
|
|
|
/// process can be the result of an exec command. The role of this process_t is determined by the
|
2019-03-24 19:29:25 +00:00
|
|
|
/// type field, which can be one of process_type_t::external, process_type_t::builtin,
|
|
|
|
/// process_type_t::function, process_type_t::exec.
|
2016-05-03 04:41:17 +00:00
|
|
|
///
|
|
|
|
/// The process_t contains information on how the process should be started, such as command name
|
|
|
|
/// and arguments, as well as runtime information on the status of the actual physical process which
|
|
|
|
/// represents it. Shellscript functions, builtins and blocks of code may all need to spawn an
|
|
|
|
/// external process that handles the piping and redirecting of IO for them.
|
|
|
|
///
|
2019-03-24 19:29:25 +00:00
|
|
|
/// If the process is of type process_type_t::external or process_type_t::exec, argv is the argument
|
|
|
|
/// array and actual_cmd is the absolute path of the command to execute.
|
2016-05-03 04:41:17 +00:00
|
|
|
///
|
2019-03-24 19:29:25 +00:00
|
|
|
/// If the process is of type process_type_t::builtin, argv is the argument vector, and argv[0] is
|
|
|
|
/// the name of the builtin command.
|
2016-05-03 04:41:17 +00:00
|
|
|
///
|
2019-03-24 19:29:25 +00:00
|
|
|
/// If the process is of type process_type_t::function, argv is the argument vector, and argv[0] is
|
|
|
|
/// the name of the shellscript function.
|
2019-05-05 05:12:31 +00:00
|
|
|
class parser_t;
|
2016-05-03 04:41:17 +00:00
|
|
|
class process_t {
|
|
|
|
private:
|
2012-02-28 23:11:46 +00:00
|
|
|
null_terminated_array_t<wchar_t> argv_array;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2019-12-13 00:44:24 +00:00
|
|
|
redirection_spec_list_t proc_redirection_specs;
|
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-19 23:16:41 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
// No copying.
|
2019-02-16 09:20:08 +00:00
|
|
|
process_t(const process_t &rhs) = delete;
|
|
|
|
void operator=(const process_t &rhs) = delete;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
public:
|
2012-08-15 07:57:56 +00:00
|
|
|
process_t();
|
2017-01-23 17:28:34 +00:00
|
|
|
|
2019-02-16 09:20:08 +00:00
|
|
|
/// Note whether we are the first and/or last in the job
|
2018-02-11 03:16:35 +00:00
|
|
|
bool is_first_in_job{false};
|
|
|
|
bool is_last_in_job{false};
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2019-03-24 19:29:25 +00:00
|
|
|
/// Type of process.
|
|
|
|
process_type_t type{process_type_t::external};
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2018-02-11 03:16:35 +00:00
|
|
|
/// For internal block processes only, the node offset of the statement.
|
|
|
|
/// This is always either block, ifs, or switchs, never boolean or decorated.
|
2018-02-12 03:34:12 +00:00
|
|
|
parsed_source_ref_t block_node_source{};
|
2018-02-11 03:16:35 +00:00
|
|
|
tnode_t<grammar::statement> internal_block_node{};
|
2012-11-18 10:23:22 +00:00
|
|
|
|
Support FOO=bar syntax for passing variables to individual commands
This adds initial support for statements with prefixed variable assignments.
Statments like this are supported:
a=1 b=$a echo $b # outputs 1
Just like in other shells, the left-hand side of each assignment must
be a valid variable identifier (no quoting/escaping). Array indexing
(PATH[1]=/bin ls $PATH) is *not* yet supported, but can be added fairly
easily.
The right hand side may be any valid string token, like a command
substitution, or a brace expansion.
Since `a=* foo` is equivalent to `begin set -lx a *; foo; end`,
the assignment, like `set`, uses nullglob behavior, e.g. below command
can safely be used to check if a directory is empty.
x=/nothing/{,.}* test (count $x) -eq 0
Generic file completion is done after the equal sign, so for example
pressing tab after something like `HOME=/` completes files in the
root directory
Subcommand completion works, so something like
`GIT_DIR=repo.git and command git ` correctly calls git completions
(but the git completion does not use the variable as of now).
The variable assignment is highlighted like an argument.
Closes #6048
2019-10-23 01:13:29 +00:00
|
|
|
struct concrete_assignment {
|
|
|
|
wcstring variable_name;
|
|
|
|
wcstring_list_t values;
|
|
|
|
};
|
|
|
|
/// The expanded variable assignments for this process, as specified by the `a=b cmd` syntax.
|
|
|
|
std::vector<concrete_assignment> variable_assignments;
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Sets argv.
|
|
|
|
void set_argv(const wcstring_list_t &argv) { argv_array.set(argv); }
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Returns argv.
|
2018-08-04 23:15:06 +00:00
|
|
|
wchar_t **get_argv() { return argv_array.get(); }
|
2019-10-19 01:08:22 +00:00
|
|
|
const wchar_t *const *get_argv() const { return argv_array.get(); }
|
2018-08-04 23:15:06 +00:00
|
|
|
const null_terminated_array_t<wchar_t> &get_argv_array() const { return argv_array; }
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Returns argv[idx].
|
|
|
|
const wchar_t *argv(size_t idx) const {
|
|
|
|
const wchar_t *const *argv = argv_array.get();
|
2019-11-19 02:34:50 +00:00
|
|
|
assert(argv != nullptr);
|
2012-03-09 07:21:07 +00:00
|
|
|
return argv[idx];
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Returns argv[0], or NULL.
|
|
|
|
const wchar_t *argv0() const {
|
|
|
|
const wchar_t *const *argv = argv_array.get();
|
2019-11-19 02:34:50 +00:00
|
|
|
return argv ? argv[0] : nullptr;
|
2012-03-09 07:21:07 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2019-12-13 00:44:24 +00:00
|
|
|
/// Redirection list getter and setter.
|
|
|
|
const redirection_spec_list_t &redirection_specs() const { return proc_redirection_specs; }
|
|
|
|
void set_redirection_specs(redirection_spec_list_t specs) {
|
|
|
|
this->proc_redirection_specs = std::move(specs);
|
|
|
|
}
|
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-19 23:16:41 +00:00
|
|
|
|
2019-02-17 01:35:16 +00:00
|
|
|
/// Store the current topic generations. That is, right before the process is launched, record
|
|
|
|
/// the generations of all topics; then we can tell which generation values have changed after
|
|
|
|
/// launch. This helps us avoid spurious waitpid calls.
|
|
|
|
void check_generations_before_launch();
|
|
|
|
|
2019-03-24 19:29:25 +00:00
|
|
|
/// Actual command to pass to exec in case of process_type_t::external or process_type_t::exec.
|
2012-11-19 00:30:30 +00:00
|
|
|
wcstring actual_cmd;
|
2019-02-17 01:39:14 +00:00
|
|
|
|
|
|
|
/// Generation counts for reaping.
|
|
|
|
generation_list_t gens_{};
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Process ID
|
2018-02-11 03:16:35 +00:00
|
|
|
pid_t pid{0};
|
2019-02-13 23:17:07 +00:00
|
|
|
|
|
|
|
/// If we are an "internal process," that process.
|
|
|
|
std::shared_ptr<internal_proc_t> internal_proc_{};
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// File descriptor that pipe output should bind to.
|
2018-02-11 03:16:35 +00:00
|
|
|
int pipe_write_fd{0};
|
2016-05-03 04:41:17 +00:00
|
|
|
/// True if process has completed.
|
2019-03-03 19:45:05 +00:00
|
|
|
bool completed{false};
|
2016-05-03 04:41:17 +00:00
|
|
|
/// True if process has stopped.
|
2019-03-03 19:45:05 +00:00
|
|
|
bool stopped{false};
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Reported status value.
|
2019-02-25 18:05:42 +00:00
|
|
|
proc_status_t status{};
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Last time of cpu time check.
|
2018-02-11 03:16:35 +00:00
|
|
|
struct timeval last_time {};
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Number of jiffies spent in process at last cpu time check.
|
2018-02-11 03:16:35 +00:00
|
|
|
unsigned long last_jiffies{0};
|
2012-01-30 02:25:54 +00:00
|
|
|
};
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2017-01-23 17:28:34 +00:00
|
|
|
typedef std::unique_ptr<process_t> process_ptr_t;
|
|
|
|
typedef std::vector<process_ptr_t> process_list_t;
|
|
|
|
|
2012-02-28 02:43:24 +00:00
|
|
|
typedef int job_id_t;
|
|
|
|
job_id_t acquire_job_id(void);
|
2019-11-19 00:54:36 +00:00
|
|
|
void release_job_id(job_id_t jid);
|
2012-02-28 02:43:24 +00:00
|
|
|
|
2019-12-08 21:03:42 +00:00
|
|
|
/// Information about where a job comes from.
|
|
|
|
/// This should be safe to copy across threads; in particular that means this cannot contain a
|
2019-12-12 00:34:20 +00:00
|
|
|
/// job_t. It is also important that job_t not contain this: because it stores block IO, it will
|
|
|
|
/// extend the life of the IO which may prevent pipes from closing in a timely manner. See #6397.
|
2019-12-08 21:03:42 +00:00
|
|
|
struct job_lineage_t {
|
|
|
|
/// The pgid of the parental job.
|
|
|
|
/// If our job is "nested" as part of a function or block execution, and that function or block
|
|
|
|
/// is part of a pipeline, then this may be set.
|
|
|
|
maybe_t<pid_t> parent_pgid{};
|
|
|
|
|
|
|
|
/// The IO chain associated with any block containing this job.
|
|
|
|
/// For example, in `begin; foo ; end < file.txt` this would have the 'file.txt' IO.
|
|
|
|
io_chain_t block_io{};
|
|
|
|
|
|
|
|
/// A shared pointer indicating that the entire tree of jobs is safe to disown.
|
2019-12-11 02:32:56 +00:00
|
|
|
/// This is set to true by the "root" job after it is constructed.
|
|
|
|
std::shared_ptr<relaxed_atomic_bool_t> root_constructed{};
|
2019-12-08 21:03:42 +00:00
|
|
|
};
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// A struct represeting a job. A job is basically a pipeline of one or more processes and a couple
|
|
|
|
/// of flags.
|
|
|
|
class job_t {
|
2019-06-23 19:39:29 +00:00
|
|
|
public:
|
|
|
|
/// A set of jobs properties. These are immutable: they do not change for the lifetime of the
|
|
|
|
/// job.
|
|
|
|
struct properties_t {
|
|
|
|
/// Whether the specified job is a part of a subshell, event handler or some other form of
|
|
|
|
/// special job that should not be reported.
|
|
|
|
bool skip_notification{};
|
|
|
|
|
|
|
|
/// Whether the job wants to own the terminal when in the foreground.
|
|
|
|
bool wants_terminal{};
|
2019-06-26 18:28:27 +00:00
|
|
|
|
|
|
|
/// Whether this job was created as part of an event handler.
|
|
|
|
bool from_event_handler{};
|
2019-06-23 19:39:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
/// Set of immutable job properties.
|
|
|
|
const properties_t properties;
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// The original command which led to the creation of this job. It is used for displaying
|
|
|
|
/// messages about job status on the terminal.
|
2012-11-19 00:30:30 +00:00
|
|
|
wcstring command_str;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
// No copying.
|
2017-01-26 22:47:32 +00:00
|
|
|
job_t(const job_t &rhs) = delete;
|
|
|
|
void operator=(const job_t &) = delete;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
public:
|
2019-12-08 21:03:42 +00:00
|
|
|
job_t(job_id_t job_id, const properties_t &props, job_lineage_t lineage);
|
2012-08-15 07:57:56 +00:00
|
|
|
~job_t();
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Returns the command as a wchar_t *. */
|
|
|
|
const wchar_t *command_wcstr() const { return command_str.c_str(); }
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Returns the command.
|
|
|
|
const wcstring &command() const { return command_str; }
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Sets the command.
|
2019-09-09 16:07:25 +00:00
|
|
|
void set_command(wcstring cmd) { command_str = std::move(cmd); }
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2019-02-17 01:39:14 +00:00
|
|
|
/// \return whether it is OK to reap a given process. Sometimes we want to defer reaping a
|
|
|
|
/// process if it is the group leader and the job is not yet constructed, because then we might
|
|
|
|
/// also reap the process group and then we cannot add new processes to the group.
|
|
|
|
bool can_reap(const process_t *p) const {
|
2019-02-13 23:17:07 +00:00
|
|
|
// Internal processes can always be reaped.
|
|
|
|
if (p->internal_proc_) {
|
|
|
|
return true;
|
|
|
|
} else if (p->pid <= 0) {
|
2019-02-17 01:39:14 +00:00
|
|
|
// Can't reap without a pid.
|
|
|
|
return false;
|
2019-02-13 23:17:07 +00:00
|
|
|
} else if (!is_constructed() && pgid > 0 && p->pid == pgid) {
|
2019-02-17 01:39:14 +00:00
|
|
|
// p is the the group leader in an under-construction job.
|
|
|
|
return false;
|
2019-02-13 23:17:07 +00:00
|
|
|
} else {
|
|
|
|
return true;
|
2019-02-17 01:39:14 +00:00
|
|
|
}
|
2019-02-13 23:17:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \returns the reap topic for a process, which describes the manner in which we are reaped. A
|
|
|
|
/// none returns means don't reap, or perhaps defer reaping.
|
|
|
|
maybe_t<topic_t> reap_topic_for_process(const process_t *p) const {
|
|
|
|
if (p->completed || !can_reap(p)) return none();
|
|
|
|
return p->internal_proc_ ? topic_t::internal_exit : topic_t::sigchld;
|
2019-02-17 01:39:14 +00:00
|
|
|
}
|
|
|
|
|
2018-10-02 16:19:56 +00:00
|
|
|
/// Returns a truncated version of the job string. Used when a message has already been emitted
|
|
|
|
/// containing the full job string and job id, but using the job id alone would be confusing
|
|
|
|
/// due to reuse of freed job ids. Prevents overloading the debug comments with the full,
|
|
|
|
/// untruncated job string when we don't care what the job is, only which of the currently
|
|
|
|
/// running jobs it is.
|
|
|
|
wcstring preview() const {
|
2019-05-05 10:09:25 +00:00
|
|
|
if (processes.empty()) return L"";
|
2018-12-02 22:53:26 +00:00
|
|
|
// Note argv0 may be empty in e.g. a block process.
|
|
|
|
const wchar_t *argv0 = processes.front()->argv0();
|
|
|
|
wcstring result = argv0 ? argv0 : L"null";
|
|
|
|
return result + L" ...";
|
2018-10-02 16:19:56 +00:00
|
|
|
}
|
|
|
|
|
2017-01-23 17:28:34 +00:00
|
|
|
/// All the processes in this job.
|
|
|
|
process_list_t processes;
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Process group ID for the process group that this job is running in.
|
2017-04-23 14:56:33 +00:00
|
|
|
/// Set to a nonexistent, non-return-value of getpgid() integer by the constructor
|
2019-06-23 19:39:29 +00:00
|
|
|
pid_t pgid{INVALID_PID};
|
|
|
|
|
|
|
|
/// The id of this job.
|
|
|
|
const job_id_t job_id;
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// The saved terminal modes of this job. This needs to be saved so that we can restore the
|
|
|
|
/// terminal to the same state after temporarily taking control over the terminal when a job
|
|
|
|
/// stops.
|
2019-06-23 19:39:29 +00:00
|
|
|
struct termios tmodes {};
|
|
|
|
|
2019-12-11 02:32:56 +00:00
|
|
|
/// Whether the specified job is completely constructed, i.e. completely parsed, and every
|
|
|
|
/// process in the job has been forked, etc.
|
|
|
|
/// This is a shared_ptr because it may be passed to child jobs through the lineage.
|
|
|
|
const std::shared_ptr<relaxed_atomic_bool_t> constructed =
|
|
|
|
std::make_shared<relaxed_atomic_bool_t>(false);
|
|
|
|
|
2019-12-12 00:34:20 +00:00
|
|
|
/// Whether the root job is constructed; this may share a reference with 'constructed'.
|
|
|
|
const std::shared_ptr<relaxed_atomic_bool_t> root_constructed;
|
|
|
|
|
2019-10-15 21:37:10 +00:00
|
|
|
/// Flags associated with the job.
|
|
|
|
struct flags_t {
|
|
|
|
/// Whether the user has been told about stopped job.
|
|
|
|
bool notified{false};
|
|
|
|
|
|
|
|
/// Whether this job is in the foreground.
|
|
|
|
bool foreground{false};
|
|
|
|
|
|
|
|
/// Whether the exit status should be negated. This flag can only be set by the not builtin.
|
|
|
|
bool negate{false};
|
|
|
|
|
|
|
|
/// Whether the job is under job control, i.e. has its own pgrp.
|
|
|
|
bool job_control{false};
|
|
|
|
|
|
|
|
/// This job is disowned, and should be removed from the active jobs list.
|
|
|
|
bool disown_requested{false};
|
|
|
|
} job_flags{};
|
|
|
|
|
|
|
|
/// Access the job flags.
|
|
|
|
const flags_t &flags() const { return job_flags; }
|
2013-08-18 23:55:01 +00:00
|
|
|
|
2019-10-15 21:37:10 +00:00
|
|
|
/// Access mutable job flags.
|
|
|
|
flags_t &mut_flags() { return job_flags; }
|
2017-01-26 23:06:58 +00:00
|
|
|
|
2019-06-23 19:39:29 +00:00
|
|
|
/// \return if we want job control.
|
2019-10-15 21:37:10 +00:00
|
|
|
bool wants_job_control() const { return flags().job_control; }
|
2019-06-23 19:39:29 +00:00
|
|
|
|
2019-07-12 20:31:56 +00:00
|
|
|
/// \return if this job should own the terminal when it runs.
|
|
|
|
bool should_claim_terminal() const { return properties.wants_terminal && is_foreground(); }
|
2019-06-23 19:39:29 +00:00
|
|
|
|
2019-12-11 02:32:56 +00:00
|
|
|
/// Mark this job as constructed. The job must not have previously been marked as constructed.
|
|
|
|
void mark_constructed();
|
|
|
|
|
2018-10-02 17:30:23 +00:00
|
|
|
// Helper functions to check presence of flags on instances of jobs
|
|
|
|
/// The job has been fully constructed, i.e. all its member processes have been launched
|
2019-12-11 02:32:56 +00:00
|
|
|
bool is_constructed() const { return *constructed; }
|
2018-10-02 17:30:23 +00:00
|
|
|
/// The job was launched in the foreground and has control of the terminal
|
2019-10-15 21:37:10 +00:00
|
|
|
bool is_foreground() const { return flags().foreground; }
|
2018-10-02 17:30:23 +00:00
|
|
|
/// The job is complete, i.e. all its member processes have been reaped
|
|
|
|
bool is_completed() const;
|
|
|
|
/// The job is in a stopped state
|
|
|
|
bool is_stopped() const;
|
2019-04-10 04:29:58 +00:00
|
|
|
/// The job is OK to be externally visible, e.g. to the user via `jobs`
|
2019-04-29 05:33:29 +00:00
|
|
|
bool is_visible() const {
|
2019-10-15 21:37:10 +00:00
|
|
|
return !is_completed() && is_constructed() && !flags().disown_requested;
|
2019-11-20 06:17:30 +00:00
|
|
|
}
|
2019-06-23 19:39:29 +00:00
|
|
|
bool skip_notification() const { return properties.skip_notification; }
|
2019-06-26 18:28:27 +00:00
|
|
|
bool from_event_handler() const { return properties.from_event_handler; }
|
2018-10-02 17:30:23 +00:00
|
|
|
|
2019-07-28 21:05:51 +00:00
|
|
|
/// \return whether we should report process exit events.
|
|
|
|
/// This implements some historical behavior which has not been justified.
|
|
|
|
bool should_report_process_exits() const;
|
|
|
|
|
2018-11-04 09:11:12 +00:00
|
|
|
/// \return whether this job and its parent chain are fully constructed.
|
|
|
|
bool job_chain_is_fully_constructed() const;
|
|
|
|
|
2018-10-02 17:30:23 +00:00
|
|
|
/// Resume a (possibly) stopped job. Puts job in the foreground. If cont is true, restore the
|
|
|
|
/// saved terminal modes and send the process group a SIGCONT signal to wake it up before we
|
|
|
|
/// block.
|
|
|
|
///
|
2019-04-07 03:00:52 +00:00
|
|
|
/// \param reclaim_foreground_pgrp whether, when the job finishes or stops, to reclaim the
|
|
|
|
/// foreground pgrp (via tcsetpgrp). \param send_sigcont Whether SIGCONT should be sent to the
|
|
|
|
/// job if it is in the foreground.
|
2019-05-05 05:12:31 +00:00
|
|
|
void continue_job(parser_t &parser, bool reclaim_foreground_pgrp, bool send_sigcont);
|
2018-10-02 17:30:23 +00:00
|
|
|
|
|
|
|
/// Send the specified signal to all processes in this job.
|
2018-10-02 20:10:42 +00:00
|
|
|
/// \return true on success, false on failure.
|
|
|
|
bool signal(int signal);
|
2018-10-02 17:30:23 +00:00
|
|
|
|
2019-02-25 09:21:32 +00:00
|
|
|
/// \returns the statuses for this job.
|
|
|
|
statuses_t get_statuses() const;
|
|
|
|
|
2018-10-02 17:30:23 +00:00
|
|
|
/// Return the job instance matching this unique job id.
|
|
|
|
/// If id is 0 or less, return the last job used.
|
|
|
|
static job_t *from_job_id(job_id_t id);
|
|
|
|
|
|
|
|
/// Return the job containing the process identified by the unique pid provided.
|
|
|
|
static job_t *from_pid(pid_t pid);
|
2012-01-30 00:36:21 +00:00
|
|
|
};
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Whether this shell is attached to the keyboard at all.
|
2019-05-12 22:48:00 +00:00
|
|
|
bool is_interactive_session();
|
|
|
|
void set_interactive_session(bool flag);
|
2005-12-15 13:59:02 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Whether we are a login shell.
|
2019-05-12 22:48:00 +00:00
|
|
|
bool get_login();
|
|
|
|
void mark_login();
|
|
|
|
|
|
|
|
/// If this flag is set, fish will never fork or run execve. It is used to put fish into a syntax
|
|
|
|
/// verifier mode where fish tries to validate the syntax of a file but doesn't actually do
|
|
|
|
/// anything.
|
|
|
|
bool no_exec();
|
|
|
|
void mark_no_exec();
|
2005-12-15 13:59:02 +00:00
|
|
|
|
2018-12-31 05:53:26 +00:00
|
|
|
// List of jobs.
|
|
|
|
typedef std::deque<shared_ptr<job_t>> job_list_t;
|
2012-02-28 02:43:24 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// The current job control mode.
|
|
|
|
///
|
2019-03-24 19:12:08 +00:00
|
|
|
/// Must be one of job_control_t::all, job_control_t::interactive and job_control_t::none.
|
2019-04-30 06:08:01 +00:00
|
|
|
job_control_t get_job_control_mode();
|
|
|
|
void set_job_control_mode(job_control_t mode);
|
2006-01-30 17:54:26 +00:00
|
|
|
|
2019-04-29 16:05:00 +00:00
|
|
|
/// Notify the user about stopped or terminated jobs, and delete completed jobs from the job list.
|
2019-04-30 03:58:58 +00:00
|
|
|
/// If \p interactive is set, allow removing interactive jobs; otherwise skip them.
|
2019-04-29 16:05:00 +00:00
|
|
|
/// \return whether text was printed to stdout.
|
2019-04-30 03:58:58 +00:00
|
|
|
class parser_t;
|
|
|
|
bool job_reap(parser_t &parser, bool interactive);
|
2005-12-15 13:59:02 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Mark a process as failed to execute (and therefore completed).
|
2019-11-19 00:54:36 +00:00
|
|
|
void job_mark_process_as_failed(const std::shared_ptr<job_t> &job, const process_t *failed_proc);
|
2012-10-29 08:45:51 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Use the procfs filesystem to look up how many jiffies of cpu time was used by this process. This
|
|
|
|
/// function is only available on systems with the procfs file entry 'stat', i.e. Linux.
|
2012-11-19 00:30:30 +00:00
|
|
|
unsigned long proc_get_jiffies(process_t *p);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Update process time usage for all processes by calling the proc_get_jiffies function for every
|
|
|
|
/// process of every job.
|
2019-05-05 05:12:31 +00:00
|
|
|
void proc_update_jiffies(parser_t &parser);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Perform a set of simple sanity checks on the job list. This includes making sure that only one
|
|
|
|
/// job is in the foreground, that every process is in a valid state, etc.
|
2019-05-05 05:12:31 +00:00
|
|
|
void proc_sanity_check(const parser_t &parser);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2019-05-01 23:17:23 +00:00
|
|
|
/// Create a process/job exit event notification.
|
|
|
|
event_t proc_create_event(const wchar_t *msg, event_type_t type, pid_t pid, int status);
|
2005-12-03 19:46:18 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Initializations.
|
2005-10-14 11:40:33 +00:00
|
|
|
void proc_init();
|
|
|
|
|
2019-03-03 00:45:15 +00:00
|
|
|
/// Wait for any process finishing, or receipt of a signal.
|
2019-04-30 03:58:58 +00:00
|
|
|
void proc_wait_any(parser_t &parser);
|
2017-10-22 07:10:23 +00:00
|
|
|
|
2018-09-09 08:36:21 +00:00
|
|
|
/// Set and get whether we are in initialization.
|
|
|
|
// Hackish. In order to correctly report the origin of code with no associated file, we need to
|
|
|
|
// know whether it's run during initialization or not.
|
|
|
|
void set_is_within_fish_initialization(bool flag);
|
|
|
|
bool is_within_fish_initialization();
|
|
|
|
|
2018-10-20 18:58:51 +00:00
|
|
|
/// Terminate all background jobs
|
2019-05-05 05:12:31 +00:00
|
|
|
void hup_background_jobs(const parser_t &parser);
|
2018-10-20 18:58:51 +00:00
|
|
|
|
2019-06-29 22:58:36 +00:00
|
|
|
/// Give ownership of the terminal to the specified job, if it wants it.
|
2018-10-02 18:24:05 +00:00
|
|
|
///
|
|
|
|
/// \param j The job to give the terminal to.
|
2019-06-29 22:58:36 +00:00
|
|
|
/// \param continuing_from_stopped If this variable is set, we are giving back control to a job that
|
|
|
|
/// was previously stopped. In that case, we need to set the terminal attributes to those saved in
|
|
|
|
/// the job.
|
|
|
|
/// \return 1 if transferred, 0 if no transfer was necessary, -1 on error.
|
|
|
|
int terminal_maybe_give_to_job(const job_t *j, bool continuing_from_stopped);
|
2018-08-05 00:32:04 +00:00
|
|
|
|
2018-08-18 23:56:01 +00:00
|
|
|
/// Given that we are about to run a builtin, acquire the terminal if it is owned by the given job.
|
|
|
|
/// Returns the pid to restore after running the builtin, or -1 if there is no pid to restore.
|
|
|
|
pid_t terminal_acquire_before_builtin(int job_pgid);
|
2018-10-02 03:55:18 +00:00
|
|
|
|
2018-11-18 21:18:18 +00:00
|
|
|
/// Add a pid to the list of pids we wait on even though they are not associated with any jobs.
|
|
|
|
/// Used to avoid zombie processes after disown.
|
|
|
|
void add_disowned_pgid(pid_t pgid);
|
2018-10-02 03:55:18 +00:00
|
|
|
|
2019-05-12 21:59:30 +00:00
|
|
|
bool have_proc_stat();
|
2019-04-29 13:29:21 +00:00
|
|
|
|
2018-10-02 03:55:18 +00:00
|
|
|
#endif
|