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
|
2016-04-21 06:00:54 +00:00
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
2017-02-11 02:47:02 +00:00
|
|
|
|
2018-11-12 00:57:30 +00:00
|
|
|
#include <list>
|
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"
|
2018-10-02 17:30:23 +00:00
|
|
|
#include "enum_set.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.
|
|
|
|
enum process_type_t {
|
|
|
|
/// A regular external command.
|
2012-11-19 00:30:30 +00:00
|
|
|
EXTERNAL,
|
2016-05-03 04:41:17 +00:00
|
|
|
/// A builtin command.
|
2012-11-19 00:30:30 +00:00
|
|
|
INTERNAL_BUILTIN,
|
2016-05-03 04:41:17 +00:00
|
|
|
/// A shellscript function.
|
2012-11-19 00:30:30 +00:00
|
|
|
INTERNAL_FUNCTION,
|
2016-05-03 04:41:17 +00:00
|
|
|
/// A block of commands, represented as a node.
|
2013-12-27 09:38:43 +00:00
|
|
|
INTERNAL_BLOCK_NODE,
|
2016-05-03 04:41:17 +00:00
|
|
|
/// The exec builtin.
|
2014-09-22 17:16:16 +00:00
|
|
|
INTERNAL_EXEC
|
2013-12-20 22:37:40 +00:00
|
|
|
};
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
enum {
|
2012-11-19 00:30:30 +00:00
|
|
|
JOB_CONTROL_ALL,
|
|
|
|
JOB_CONTROL_INTERACTIVE,
|
|
|
|
JOB_CONTROL_NONE,
|
2016-05-03 04:41:17 +00:00
|
|
|
};
|
2012-11-18 10:23:22 +00:00
|
|
|
|
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
|
|
|
|
/// type field, which can be one of EXTERNAL, INTERNAL_BUILTIN, INTERNAL_FUNCTION, INTERNAL_EXEC.
|
|
|
|
///
|
|
|
|
/// 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.
|
|
|
|
///
|
|
|
|
/// If the process is of type EXTERNAL or INTERNAL_EXEC, argv is the argument array and actual_cmd
|
|
|
|
/// is the absolute path of the command to execute.
|
|
|
|
///
|
|
|
|
/// If the process is of type INTERNAL_BUILTIN, argv is the argument vector, and argv[0] is the name
|
|
|
|
/// of the builtin command.
|
|
|
|
///
|
|
|
|
/// If the process is of type INTERNAL_FUNCTION, argv is the argument vector, and argv[0] is the
|
|
|
|
/// name of the shellscript function.
|
|
|
|
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
|
|
|
|
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
|
|
|
io_chain_t process_io_chain;
|
|
|
|
|
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
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Type of process. Can be one of \c EXTERNAL, \c INTERNAL_BUILTIN, \c INTERNAL_FUNCTION, \c
|
|
|
|
/// INTERNAL_EXEC.
|
2018-02-11 03:16:35 +00:00
|
|
|
enum process_type_t type { 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
|
|
|
|
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(); }
|
|
|
|
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();
|
2012-03-09 07:21:07 +00:00
|
|
|
assert(argv != NULL);
|
|
|
|
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();
|
2012-03-09 07:21:07 +00:00
|
|
|
return argv ? argv[0] : NULL;
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// IO chain getter and setter.
|
|
|
|
const io_chain_t &io_chain() const { return process_io_chain; }
|
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
|
|
|
void set_io_chain(const io_chain_t &chain) { this->process_io_chain = chain; }
|
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();
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Actual command to pass to exec in case of EXTERNAL or INTERNAL_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};
|
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-02-16 09:20:08 +00:00
|
|
|
volatile bool completed{false};
|
2016-05-03 04:41:17 +00:00
|
|
|
/// True if process has stopped.
|
2019-02-16 09:20:08 +00:00
|
|
|
volatile bool stopped{false};
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Reported status value.
|
2018-02-11 03:16:35 +00:00
|
|
|
volatile int status{0};
|
2005-09-20 13:26:39 +00:00
|
|
|
#ifdef HAVE__PROC_SELF_STAT
|
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};
|
2005-09-20 13:26:39 +00:00
|
|
|
#endif
|
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;
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Constants for the flag variable in the job struct.
|
2018-10-02 17:30:23 +00:00
|
|
|
enum class job_flag_t {
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Whether the user has been told about stopped job.
|
2018-10-02 17:30:23 +00:00
|
|
|
NOTIFIED,
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Whether this job is in the foreground.
|
2018-10-02 17:30:23 +00:00
|
|
|
FOREGROUND,
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Whether the specified job is completely constructed, i.e. completely parsed, and every
|
|
|
|
/// process in the job has been forked, etc.
|
2018-10-02 17:30:23 +00:00
|
|
|
CONSTRUCTED,
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Whether the specified job is a part of a subshell, event handler or some other form of
|
|
|
|
/// special job that should not be reported.
|
2018-10-02 17:30:23 +00:00
|
|
|
SKIP_NOTIFICATION,
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Whether the exit status should be negated. This flag can only be set by the not builtin.
|
2018-10-02 17:30:23 +00:00
|
|
|
NEGATE,
|
2018-10-08 17:44:47 +00:00
|
|
|
/// Whether the job is under job control, i.e. has its own pgrp.
|
2018-10-02 17:30:23 +00:00
|
|
|
JOB_CONTROL,
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Whether the job wants to own the terminal when in the foreground.
|
2018-10-02 17:30:23 +00:00
|
|
|
TERMINAL,
|
2019-02-16 22:19:59 +00:00
|
|
|
|
|
|
|
JOB_FLAG_COUNT
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct enum_info_t<job_flag_t> {
|
|
|
|
static constexpr auto count = job_flag_t::JOB_FLAG_COUNT;
|
2012-09-01 08:46:14 +00:00
|
|
|
};
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-02-28 02:43:24 +00:00
|
|
|
typedef int job_id_t;
|
|
|
|
job_id_t acquire_job_id(void);
|
|
|
|
void release_job_id(job_id_t jobid);
|
|
|
|
|
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 {
|
|
|
|
/// 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
|
|
|
// The IO chain associated with the block.
|
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
|
|
|
const io_chain_t block_io;
|
2013-08-18 23:55:01 +00:00
|
|
|
|
2018-11-04 07:58:44 +00:00
|
|
|
// The parent job. If we were created as a nested job due to execution of a block or function in
|
|
|
|
// a pipeline, then this refers to the job corresponding to that pipeline. Otherwise it is null.
|
|
|
|
const std::shared_ptr<job_t> parent_job;
|
|
|
|
|
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:
|
2018-11-04 07:58:44 +00:00
|
|
|
job_t(job_id_t jobid, io_chain_t bio, std::shared_ptr<job_t> parent);
|
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 whether the command is empty.
|
|
|
|
bool command_is_empty() const { return command_str.empty(); }
|
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.
|
|
|
|
void set_command(const wcstring &cmd) { command_str = 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 {
|
|
|
|
if (p->pid <= 0) {
|
|
|
|
// Can't reap without a pid.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!is_constructed() && pgid > 0 && p->pid == pgid) {
|
|
|
|
// p is the the group leader in an under-construction job.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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 {
|
2018-12-02 22:53:26 +00:00
|
|
|
if (processes.empty())
|
|
|
|
return L"";
|
|
|
|
// 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
|
2012-11-19 00:30:30 +00:00
|
|
|
pid_t pgid;
|
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.
|
2012-11-19 00:30:30 +00:00
|
|
|
struct termios tmodes;
|
2016-05-03 04:41:17 +00:00
|
|
|
/// The job id of the job. This is a small integer that is a unique identifier of the job within
|
|
|
|
/// this shell, and is used e.g. in process expansion.
|
2012-11-19 00:30:30 +00:00
|
|
|
const job_id_t job_id;
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Bitset containing information about the job. A combination of the JOB_* constants.
|
2018-10-02 17:30:23 +00:00
|
|
|
enum_set_t<job_flag_t> flags;
|
2013-08-18 23:55:01 +00:00
|
|
|
|
2017-01-26 23:06:58 +00:00
|
|
|
// Get and set flags
|
|
|
|
bool get_flag(job_flag_t flag) const;
|
|
|
|
void set_flag(job_flag_t flag, bool set);
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Returns the block IO redirections associated with the job. These are things like the IO
|
|
|
|
/// redirections associated with the begin...end statement.
|
|
|
|
const io_chain_t &block_io_chain() const { return this->block_io; }
|
2013-08-18 23:55:01 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Fetch all the IO redirections associated with the job.
|
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
|
|
|
io_chain_t all_io_redirections() const;
|
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
|
|
|
|
bool is_constructed() const { return get_flag(job_flag_t::CONSTRUCTED); };
|
|
|
|
/// The job was launched in the foreground and has control of the terminal
|
|
|
|
bool is_foreground() const { return get_flag(job_flag_t::FOREGROUND); };
|
|
|
|
/// 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;
|
|
|
|
|
2018-11-04 09:11:12 +00:00
|
|
|
/// \return the parent job, or nullptr.
|
|
|
|
const std::shared_ptr<job_t> get_parent() const { return parent_job; }
|
|
|
|
|
|
|
|
/// \return whether this job and its parent chain are fully constructed.
|
|
|
|
bool job_chain_is_fully_constructed() const;
|
|
|
|
|
2018-10-02 20:10:42 +00:00
|
|
|
// (This function would just be called `continue` but that's obviously a reserved keyword)
|
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.
|
|
|
|
///
|
2018-10-02 20:10:42 +00:00
|
|
|
/// \param send_sigcont Whether SIGCONT should be sent to the job if it is in the foreground.
|
|
|
|
void continue_job(bool send_sigcont);
|
2018-10-02 17:30:23 +00:00
|
|
|
|
|
|
|
/// Promotes the job to the front of the job list.
|
|
|
|
void promote();
|
|
|
|
|
|
|
|
/// 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
|
|
|
|
|
|
|
/// 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
|
|
|
|
2017-06-20 04:05:34 +00:00
|
|
|
/// Whether we are reading from the keyboard right now.
|
|
|
|
bool shell_is_interactive(void);
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Whether we are running a subshell command.
|
2017-06-20 04:05:34 +00:00
|
|
|
extern bool is_subshell;
|
2005-12-15 13:59:02 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Whether we are running a block of commands.
|
2017-06-20 04:05:34 +00:00
|
|
|
extern bool is_block;
|
2005-12-15 13:59:02 +00:00
|
|
|
|
2017-06-20 04:05:34 +00:00
|
|
|
/// Whether we are running due to a `breakpoint` command.
|
|
|
|
extern bool is_breakpoint;
|
2005-12-15 13:59:02 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Whether this shell is attached to the keyboard at all.
|
2017-06-20 04:05:34 +00:00
|
|
|
extern bool is_interactive_session;
|
2005-12-15 13:59:02 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Whether we are a login shell.
|
2017-06-20 04:05:34 +00:00
|
|
|
extern bool is_login;
|
2005-12-15 13:59:02 +00:00
|
|
|
|
2017-06-20 04:05:34 +00:00
|
|
|
/// Whether we are running an event handler. This is not a bool because we keep count of the event
|
|
|
|
/// nesting level.
|
2005-10-05 22:37:08 +00:00
|
|
|
extern int is_event;
|
2005-12-15 13:59:02 +00:00
|
|
|
|
2018-11-12 00:56:49 +00:00
|
|
|
// List of jobs. We sometimes mutate this while iterating - hence it must be a list, not a vector
|
2018-11-12 00:57:30 +00:00
|
|
|
typedef std::list<shared_ptr<job_t>> job_list_t;
|
2012-02-28 02:43:24 +00:00
|
|
|
|
|
|
|
bool job_list_is_empty(void);
|
2012-01-30 00:36:21 +00:00
|
|
|
|
2017-01-22 08:59:50 +00:00
|
|
|
/// A class to aid iteration over jobs list
|
2016-05-03 04:41:17 +00:00
|
|
|
class job_iterator_t {
|
|
|
|
job_list_t *const job_list;
|
2012-01-30 00:36:21 +00:00
|
|
|
job_list_t::iterator current, end;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
public:
|
2012-02-28 02:43:24 +00:00
|
|
|
void reset(void);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
job_t *next() {
|
2012-01-30 00:36:21 +00:00
|
|
|
job_t *job = NULL;
|
2016-05-03 04:41:17 +00:00
|
|
|
if (current != end) {
|
2017-01-26 22:47:32 +00:00
|
|
|
job = current->get();
|
2012-01-30 00:36:21 +00:00
|
|
|
++current;
|
|
|
|
}
|
|
|
|
return job;
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-02-28 03:38:15 +00:00
|
|
|
explicit job_iterator_t(job_list_t &jobs);
|
2012-02-28 02:43:24 +00:00
|
|
|
job_iterator_t();
|
2014-11-03 18:56:16 +00:00
|
|
|
size_t count() const;
|
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 a universal variable barrier roundtrip has already been made for the currently executing
|
|
|
|
/// command. Such a roundtrip only needs to be done once on a given command, unless a universal
|
|
|
|
/// variable value is changed. Once this has been done, this variable is set to 1, so that no more
|
|
|
|
/// roundtrips need to be done.
|
|
|
|
///
|
|
|
|
/// Both setting it to one when it should be zero and the opposite may cause concurrency bugs.
|
2012-03-31 22:33:34 +00:00
|
|
|
bool get_proc_had_barrier();
|
|
|
|
void set_proc_had_barrier(bool flag);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// The current job control mode.
|
|
|
|
///
|
|
|
|
/// Must be one of JOB_CONTROL_ALL, JOB_CONTROL_INTERACTIVE and JOB_CONTROL_NONE.
|
2006-01-30 17:54:26 +00:00
|
|
|
extern int job_control_mode;
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// 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.
|
2006-03-18 01:04:59 +00:00
|
|
|
extern int no_exec;
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Sets the status of the last process to exit.
|
2012-11-19 00:30:30 +00:00
|
|
|
void proc_set_last_status(int s);
|
2005-12-15 13:59:02 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Returns the status of the last process to exit.
|
2005-09-20 13:26:39 +00:00
|
|
|
int proc_get_last_status();
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Notify the user about stopped or terminated jobs. Delete terminated jobs from the job list.
|
|
|
|
///
|
|
|
|
/// \param interactive whether interactive jobs should be reaped as well
|
2018-12-31 06:43:26 +00:00
|
|
|
bool job_reap(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).
|
2018-11-04 07:58:44 +00:00
|
|
|
void job_mark_process_as_failed(const std::shared_ptr<job_t> &job, const process_t *p);
|
2012-10-29 08:45:51 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
#ifdef HAVE__PROC_SELF_STAT
|
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.
|
2005-09-20 13:26:39 +00:00
|
|
|
void proc_update_jiffies();
|
|
|
|
#endif
|
|
|
|
|
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.
|
2005-09-20 13:26:39 +00:00
|
|
|
void proc_sanity_check();
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Send a process/job exit event notification. This function is a convenience wrapper around
|
|
|
|
/// event_fire().
|
2012-11-19 00:30:30 +00:00
|
|
|
void proc_fire_event(const wchar_t *msg, int 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();
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Clean up before exiting.
|
2005-10-14 11:40:33 +00:00
|
|
|
void proc_destroy();
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Set new value for is_interactive flag, saving previous value. If needed, update signal handlers.
|
2012-11-19 00:30:30 +00:00
|
|
|
void proc_push_interactive(int value);
|
2006-02-16 13:36:32 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Set is_interactive flag to the previous value. If needed, update signal handlers.
|
2006-02-16 13:36:32 +00:00
|
|
|
void proc_pop_interactive();
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Format an exit status code as returned by e.g. wait into a fish exit code number as accepted by
|
|
|
|
/// proc_set_last_status.
|
2013-02-03 01:33:15 +00:00
|
|
|
int proc_format_status(int status);
|
2009-02-21 16:46:56 +00:00
|
|
|
|
2017-10-22 07:10:23 +00:00
|
|
|
/// Wait for any process finishing.
|
|
|
|
pid_t proc_wait_any();
|
|
|
|
|
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
|
|
|
|
void hup_background_jobs();
|
|
|
|
|
2018-10-02 18:24:05 +00:00
|
|
|
/// Give ownership of the terminal to the specified job.
|
|
|
|
///
|
|
|
|
/// \param j The job to give the terminal to.
|
|
|
|
/// \param restore_attrs 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.
|
|
|
|
bool terminal_give_to_job(const job_t *j, bool restore_attrs);
|
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
|
|
|
|
|
|
|
/// 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 };
|
|
|
|
|
|
|
|
#endif
|