2016-05-03 12:41:17 +08: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 23:11:39 +08:00
|
|
|
#ifndef FISH_PROC_H
|
|
|
|
#define FISH_PROC_H
|
2016-04-21 14:00:54 +08:00
|
|
|
#include "config.h" // IWYU pragma: keep
|
2005-10-04 23:11:39 +08:00
|
|
|
|
2016-04-21 14:00:54 +08:00
|
|
|
#include <assert.h>
|
2016-05-03 12:41:17 +08:00
|
|
|
#include <signal.h>
|
2016-04-21 14:00:54 +08:00
|
|
|
#include <stddef.h>
|
2016-05-03 12:41:17 +08:00
|
|
|
#include <sys/time.h> // IWYU pragma: keep
|
2016-04-21 14:00:54 +08:00
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
2016-05-03 12:41:17 +08:00
|
|
|
#include <list>
|
2005-10-04 23:11:39 +08:00
|
|
|
|
2012-01-30 14:06:58 +08:00
|
|
|
#include "common.h"
|
2016-05-03 12:41:17 +08:00
|
|
|
#include "io.h"
|
2013-12-27 04:24:00 +08:00
|
|
|
#include "parse_tree.h"
|
2005-10-04 23:11:39 +08:00
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
/// The status code use when a command was not found.
|
2006-10-09 07:46:50 +08:00
|
|
|
#define STATUS_UNKNOWN_COMMAND 127
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
/// The status code use when an unknown error occured during execution of a command.
|
2008-01-09 03:31:45 +08:00
|
|
|
#define STATUS_NOT_EXECUTABLE 126
|
2006-10-09 09:21:02 +08:00
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
/// The status code use when an unknown error occured during execution of a command.
|
2006-10-09 09:21:02 +08:00
|
|
|
#define STATUS_EXEC_FAIL 125
|
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
/// The status code use when a wildcard had no matches.
|
2008-01-09 03:31:45 +08:00
|
|
|
#define STATUS_UNMATCHED_WILDCARD 124
|
|
|
|
|
2016-12-04 00:15:29 +08:00
|
|
|
/// The status code use when illegal command name is encountered.
|
|
|
|
#define STATUS_ILLEGAL_CMD 123
|
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
/// The status code used for normal exit in a builtin.
|
2006-12-13 22:34:31 +08:00
|
|
|
#define STATUS_BUILTIN_OK 0
|
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
/// The status code used for erroneous argument combinations in a builtin.
|
2006-10-09 09:21:02 +08:00
|
|
|
#define STATUS_BUILTIN_ERROR 1
|
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
/// Types of processes.
|
|
|
|
enum process_type_t {
|
|
|
|
/// A regular external command.
|
2012-11-19 08:30:30 +08:00
|
|
|
EXTERNAL,
|
2016-05-03 12:41:17 +08:00
|
|
|
/// A builtin command.
|
2012-11-19 08:30:30 +08:00
|
|
|
INTERNAL_BUILTIN,
|
2016-05-03 12:41:17 +08:00
|
|
|
/// A shellscript function.
|
2012-11-19 08:30:30 +08:00
|
|
|
INTERNAL_FUNCTION,
|
2016-05-03 12:41:17 +08:00
|
|
|
/// A block of commands, represented as a node.
|
2013-12-27 17:38:43 +08:00
|
|
|
INTERNAL_BLOCK_NODE,
|
2016-05-03 12:41:17 +08:00
|
|
|
/// The exec builtin.
|
2014-09-23 01:16:16 +08:00
|
|
|
INTERNAL_EXEC
|
2013-12-21 06:37:40 +08:00
|
|
|
};
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
enum {
|
2012-11-19 08:30:30 +08:00
|
|
|
JOB_CONTROL_ALL,
|
|
|
|
JOB_CONTROL_INTERACTIVE,
|
|
|
|
JOB_CONTROL_NONE,
|
2016-05-03 12:41:17 +08:00
|
|
|
};
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 12:41:17 +08: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-29 07:11:46 +08:00
|
|
|
null_terminated_array_t<wchar_t> argv_array;
|
2012-11-18 18:23:22 +08: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-20 07:16:41 +08:00
|
|
|
io_chain_t process_io_chain;
|
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
// No copying.
|
2012-08-06 04:24:33 +08:00
|
|
|
process_t(const process_t &rhs);
|
|
|
|
void operator=(const process_t &rhs);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
public:
|
2012-08-15 15:57:56 +08:00
|
|
|
process_t();
|
|
|
|
~process_t();
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
/// Type of process. Can be one of \c EXTERNAL, \c INTERNAL_BUILTIN, \c INTERNAL_FUNCTION, \c
|
|
|
|
/// INTERNAL_EXEC.
|
2013-12-21 06:37:40 +08:00
|
|
|
enum process_type_t type;
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
/// For internal block processes only, the node offset of the block.
|
2013-12-27 04:24:00 +08:00
|
|
|
node_offset_t internal_block_node;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
/// Sets argv.
|
|
|
|
void set_argv(const wcstring_list_t &argv) { argv_array.set(argv); }
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
/// Returns argv.
|
|
|
|
const wchar_t *const *get_argv(void) const { return argv_array.get(); }
|
|
|
|
const null_terminated_array_t<wchar_t> &get_argv_array(void) const { return argv_array; }
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
/// Returns argv[idx].
|
|
|
|
const wchar_t *argv(size_t idx) const {
|
|
|
|
const wchar_t *const *argv = argv_array.get();
|
2012-03-09 15:21:07 +08:00
|
|
|
assert(argv != NULL);
|
|
|
|
return argv[idx];
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
/// Returns argv[0], or NULL.
|
|
|
|
const wchar_t *argv0() const {
|
|
|
|
const wchar_t *const *argv = argv_array.get();
|
2012-03-09 15:21:07 +08:00
|
|
|
return argv ? argv[0] : NULL;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 12:41:17 +08: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-20 07:16:41 +08:00
|
|
|
|
2016-05-03 12:41:17 +08: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-20 07:16:41 +08:00
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
/// Actual command to pass to exec in case of EXTERNAL or INTERNAL_EXEC.
|
2012-11-19 08:30:30 +08:00
|
|
|
wcstring actual_cmd;
|
2016-05-03 12:41:17 +08:00
|
|
|
/// Process ID
|
2012-11-19 08:30:30 +08:00
|
|
|
pid_t pid;
|
2016-05-03 12:41:17 +08:00
|
|
|
/// File descriptor that pipe output should bind to.
|
2012-11-19 08:30:30 +08:00
|
|
|
int pipe_write_fd;
|
2016-05-03 12:41:17 +08:00
|
|
|
/// File descriptor that the _next_ process pipe input should bind to.
|
2012-11-19 08:30:30 +08:00
|
|
|
int pipe_read_fd;
|
2016-05-03 12:41:17 +08:00
|
|
|
/// True if process has completed.
|
2012-11-19 08:30:30 +08:00
|
|
|
volatile int completed;
|
2016-05-03 12:41:17 +08:00
|
|
|
/// True if process has stopped.
|
2012-11-19 08:30:30 +08:00
|
|
|
volatile int stopped;
|
2016-05-03 12:41:17 +08:00
|
|
|
/// Reported status value.
|
2012-11-19 08:30:30 +08:00
|
|
|
volatile int status;
|
2016-05-03 12:41:17 +08:00
|
|
|
/// Special flag to tell the evaluation function for count to print the help information.
|
2012-11-19 08:30:30 +08:00
|
|
|
int count_help_magic;
|
2016-05-03 12:41:17 +08:00
|
|
|
/// Next process in pipeline. We own this and we are responsible for deleting it.
|
2012-11-19 08:30:30 +08:00
|
|
|
process_t *next;
|
2005-09-20 21:26:39 +08:00
|
|
|
#ifdef HAVE__PROC_SELF_STAT
|
2016-05-03 12:41:17 +08:00
|
|
|
/// Last time of cpu time check.
|
2012-11-19 08:30:30 +08:00
|
|
|
struct timeval last_time;
|
2016-05-03 12:41:17 +08:00
|
|
|
/// Number of jiffies spent in process at last cpu time check.
|
2012-11-19 08:30:30 +08:00
|
|
|
unsigned long last_jiffies;
|
2005-09-20 21:26:39 +08:00
|
|
|
#endif
|
2012-01-30 10:25:54 +08:00
|
|
|
};
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
/// Constants for the flag variable in the job struct.
|
|
|
|
enum {
|
|
|
|
/// Whether the user has been told about stopped job.
|
2012-09-01 16:46:14 +08:00
|
|
|
JOB_NOTIFIED = 1 << 0,
|
2016-05-03 12:41:17 +08:00
|
|
|
/// Whether this job is in the foreground.
|
2012-09-01 16:46:14 +08:00
|
|
|
JOB_FOREGROUND = 1 << 1,
|
2016-05-03 12:41:17 +08:00
|
|
|
/// Whether the specified job is completely constructed, i.e. completely parsed, and every
|
|
|
|
/// process in the job has been forked, etc.
|
2012-09-01 16:46:14 +08:00
|
|
|
JOB_CONSTRUCTED = 1 << 2,
|
2016-05-03 12:41:17 +08: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.
|
2012-09-01 16:46:14 +08:00
|
|
|
JOB_SKIP_NOTIFICATION = 1 << 3,
|
2016-05-03 12:41:17 +08:00
|
|
|
/// Whether the exit status should be negated. This flag can only be set by the not builtin.
|
2012-09-01 16:46:14 +08:00
|
|
|
JOB_NEGATE = 1 << 4,
|
2016-05-03 12:41:17 +08:00
|
|
|
/// Whether the job is under job control.
|
2014-10-14 09:33:55 +08:00
|
|
|
JOB_CONTROL = 1 << 5,
|
2016-05-03 12:41:17 +08:00
|
|
|
/// Whether the job wants to own the terminal when in the foreground.
|
2014-10-14 09:33:55 +08:00
|
|
|
JOB_TERMINAL = 1 << 6
|
2012-09-01 16:46:14 +08:00
|
|
|
};
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2012-02-28 10:43:24 +08:00
|
|
|
typedef int job_id_t;
|
|
|
|
job_id_t acquire_job_id(void);
|
|
|
|
void release_job_id(job_id_t jobid);
|
|
|
|
|
2016-05-03 12:41:17 +08: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 08:30:30 +08:00
|
|
|
wcstring command_str;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 12:41:17 +08: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-20 07:16:41 +08:00
|
|
|
const io_chain_t block_io;
|
2013-08-19 07:55:01 +08:00
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
// No copying.
|
2012-08-06 04:24:33 +08:00
|
|
|
job_t(const job_t &rhs);
|
|
|
|
void operator=(const job_t &);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
public:
|
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-20 07:16:41 +08:00
|
|
|
job_t(job_id_t jobid, const io_chain_t &bio);
|
2012-08-15 15:57:56 +08:00
|
|
|
~job_t();
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
/// Returns whether the command is empty.
|
|
|
|
bool command_is_empty() const { return command_str.empty(); }
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
/// Returns the command as a wchar_t *. */
|
|
|
|
const wchar_t *command_wcstr() const { return command_str.c_str(); }
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
/// Returns the command.
|
|
|
|
const wcstring &command() const { return command_str; }
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
/// Sets the command.
|
|
|
|
void set_command(const wcstring &cmd) { command_str = cmd; }
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
/// A linked list of all the processes in this job. We are responsible for deleting this when we
|
|
|
|
/// are deallocated.
|
2012-11-19 08:30:30 +08:00
|
|
|
process_t *first_process;
|
2016-05-03 12:41:17 +08:00
|
|
|
/// Process group ID for the process group that this job is running in.
|
2012-11-19 08:30:30 +08:00
|
|
|
pid_t pgid;
|
2016-05-03 12:41:17 +08: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 08:30:30 +08:00
|
|
|
struct termios tmodes;
|
2016-05-03 12:41:17 +08: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 08:30:30 +08:00
|
|
|
const job_id_t job_id;
|
2016-05-03 12:41:17 +08:00
|
|
|
/// Bitset containing information about the job. A combination of the JOB_* constants.
|
2012-11-19 08:30:30 +08:00
|
|
|
unsigned int flags;
|
2013-08-19 07:55:01 +08:00
|
|
|
|
2016-05-03 12:41:17 +08: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-19 07:55:01 +08:00
|
|
|
|
2016-05-03 12:41:17 +08: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-20 07:16:41 +08:00
|
|
|
io_chain_t all_io_redirections() const;
|
2012-01-30 08:36:21 +08:00
|
|
|
};
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
/// Whether we are running a subshell command.
|
2005-09-20 21:26:39 +08:00
|
|
|
extern int is_subshell;
|
2005-12-15 21:59:02 +08:00
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
/// Whether we are running a block of commands.
|
2005-09-20 21:26:39 +08:00
|
|
|
extern int is_block;
|
2005-12-15 21:59:02 +08:00
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
/// Whether we are reading from the keyboard right now.
|
2016-05-15 11:35:54 +08:00
|
|
|
bool shell_is_interactive(void);
|
2005-12-15 21:59:02 +08:00
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
/// Whether this shell is attached to the keyboard at all.
|
2005-09-20 21:26:39 +08:00
|
|
|
extern int is_interactive_session;
|
2005-12-15 21:59:02 +08:00
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
/// Whether we are a login shell.
|
2005-09-20 21:26:39 +08:00
|
|
|
extern int is_login;
|
2005-12-15 21:59:02 +08:00
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
/// Whether we are running an event handler.
|
2005-10-06 06:37:08 +08:00
|
|
|
extern int is_event;
|
2005-12-15 21:59:02 +08:00
|
|
|
|
2012-01-30 08:36:21 +08:00
|
|
|
typedef std::list<job_t *> job_list_t;
|
2012-02-28 10:43:24 +08:00
|
|
|
|
|
|
|
bool job_list_is_empty(void);
|
2012-01-30 08:36:21 +08:00
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
/// A class to aid iteration over jobs list. Note this is used from a signal handler, so it must be
|
|
|
|
/// careful to not allocate memory.
|
|
|
|
class job_iterator_t {
|
|
|
|
job_list_t *const job_list;
|
2012-01-30 08:36:21 +08:00
|
|
|
job_list_t::iterator current, end;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
public:
|
2012-02-28 10:43:24 +08:00
|
|
|
void reset(void);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
job_t *next() {
|
2012-01-30 08:36:21 +08:00
|
|
|
job_t *job = NULL;
|
2016-05-03 12:41:17 +08:00
|
|
|
if (current != end) {
|
2012-01-30 08:36:21 +08:00
|
|
|
job = *current;
|
|
|
|
++current;
|
|
|
|
}
|
|
|
|
return job;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-02-28 11:38:15 +08:00
|
|
|
explicit job_iterator_t(job_list_t &jobs);
|
2012-02-28 10:43:24 +08:00
|
|
|
job_iterator_t();
|
2014-11-04 02:56:16 +08:00
|
|
|
size_t count() const;
|
2012-01-30 08:36:21 +08:00
|
|
|
};
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-03 12:41:17 +08: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-04-01 06:33:34 +08:00
|
|
|
bool get_proc_had_barrier();
|
|
|
|
void set_proc_had_barrier(bool flag);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
/// Pid of last process started in the background.
|
2005-09-20 21:26:39 +08:00
|
|
|
extern pid_t proc_last_bg_pid;
|
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
/// The current job control mode.
|
|
|
|
///
|
|
|
|
/// Must be one of JOB_CONTROL_ALL, JOB_CONTROL_INTERACTIVE and JOB_CONTROL_NONE.
|
2006-01-31 01:54:26 +08:00
|
|
|
extern int job_control_mode;
|
|
|
|
|
2016-05-03 12:41:17 +08: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 09:04:59 +08:00
|
|
|
extern int no_exec;
|
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
/// Add the specified flag to the bitset of flags for the specified job.
|
2013-01-30 18:22:38 +08:00
|
|
|
void job_set_flag(job_t *j, unsigned int flag, int set);
|
2006-10-26 04:47:59 +08:00
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
/// Returns one if the specified flag is set in the specified job, 0 otherwise.
|
2013-01-30 18:22:38 +08:00
|
|
|
int job_get_flag(const job_t *j, unsigned int flag);
|
2006-03-18 09:04:59 +08:00
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
/// Sets the status of the last process to exit.
|
2012-11-19 08:30:30 +08:00
|
|
|
void proc_set_last_status(int s);
|
2005-12-15 21:59:02 +08:00
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
/// Returns the status of the last process to exit.
|
2005-09-20 21:26:39 +08:00
|
|
|
int proc_get_last_status();
|
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
/// Remove the specified job.
|
|
|
|
void job_free(job_t *j);
|
2005-12-15 21:59:02 +08:00
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
/// Promotes a job to the front of the job list.
|
2012-01-30 08:36:21 +08:00
|
|
|
void job_promote(job_t *job);
|
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
/// Return the job with the specified job id. If id is 0 or less, return the last job used.
|
2012-02-28 10:43:24 +08:00
|
|
|
job_t *job_get(job_id_t id);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
/// Return the job with the specified pid.
|
2005-09-20 21:26:39 +08:00
|
|
|
job_t *job_get_from_pid(int pid);
|
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
/// Tests if the job is stopped.
|
2012-11-19 08:30:30 +08:00
|
|
|
int job_is_stopped(const job_t *j);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
/// Tests if the job has completed, i.e. if the last process of the pipeline has ended.
|
2013-06-16 17:53:14 +08:00
|
|
|
bool job_is_completed(const job_t *j);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
/// Reassume a (possibly) stopped job. Put job j 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.
|
|
|
|
///
|
|
|
|
/// \param j The job
|
|
|
|
/// \param cont Whether the function should wait for the job to complete before returning
|
2013-01-30 18:22:38 +08:00
|
|
|
void job_continue(job_t *j, bool cont);
|
2005-12-15 21:59:02 +08:00
|
|
|
|
2016-05-03 12:41:17 +08: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
|
2012-11-19 08:30:30 +08:00
|
|
|
int job_reap(bool interactive);
|
2005-12-15 21:59:02 +08:00
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
/// Signal handler for SIGCHLD. Mark any processes with relevant information.
|
2012-11-19 08:30:30 +08:00
|
|
|
void job_handle_signal(int signal, siginfo_t *info, void *con);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
/// Send the specified signal to all processes in the specified job.
|
2012-11-19 08:30:30 +08:00
|
|
|
int job_signal(job_t *j, int signal);
|
2006-11-20 21:12:24 +08:00
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
/// Mark a process as failed to execute (and therefore completed).
|
2012-11-19 08:30:30 +08:00
|
|
|
void job_mark_process_as_failed(const job_t *job, process_t *p);
|
2012-10-29 16:45:51 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
#ifdef HAVE__PROC_SELF_STAT
|
2016-05-03 12:41:17 +08: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 08:30:30 +08:00
|
|
|
unsigned long proc_get_jiffies(process_t *p);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
/// Update process time usage for all processes by calling the proc_get_jiffies function for every
|
|
|
|
/// process of every job.
|
2005-09-20 21:26:39 +08:00
|
|
|
void proc_update_jiffies();
|
|
|
|
#endif
|
|
|
|
|
2016-05-03 12:41:17 +08: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 21:26:39 +08:00
|
|
|
void proc_sanity_check();
|
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
/// Send a process/job exit event notification. This function is a convenience wrapper around
|
|
|
|
/// event_fire().
|
2012-11-19 08:30:30 +08:00
|
|
|
void proc_fire_event(const wchar_t *msg, int type, pid_t pid, int status);
|
2005-12-04 03:46:18 +08:00
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
/// Initializations.
|
2005-10-14 19:40:33 +08:00
|
|
|
void proc_init();
|
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
/// Clean up before exiting.
|
2005-10-14 19:40:33 +08:00
|
|
|
void proc_destroy();
|
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
/// Set new value for is_interactive flag, saving previous value. If needed, update signal handlers.
|
2012-11-19 08:30:30 +08:00
|
|
|
void proc_push_interactive(int value);
|
2006-02-16 21:36:32 +08:00
|
|
|
|
2016-05-03 12:41:17 +08:00
|
|
|
/// Set is_interactive flag to the previous value. If needed, update signal handlers.
|
2006-02-16 21:36:32 +08:00
|
|
|
void proc_pop_interactive();
|
|
|
|
|
2016-05-03 12:41:17 +08: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 09:33:15 +08:00
|
|
|
int proc_format_status(int status);
|
2009-02-22 00:46:56 +08:00
|
|
|
|
2005-10-04 23:11:39 +08:00
|
|
|
#endif
|