aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorGravatar Kurtis Rader <krader@skepticism.us>2016-05-02 21:41:17 -0700
committerGravatar Kurtis Rader <krader@skepticism.us>2016-05-02 22:07:58 -0700
commit9d742a4fa18d3bdab0ab2b68f8f57e334ea3c0e1 (patch)
treec10a76c3c8813f74d2e3b814e91d7dc6475ad0fb /src
parentdc8d31a12abc2dbd2aebd7a9c9b34207110c1233 (diff)
restyle proc module to match project style
Reduces lint errors from 134 to 101 (-25%). Line count from 1994 to 1466 (-26%). Another step in resolving issue #2902.
Diffstat (limited to 'src')
-rw-r--r--src/print_help.cpp25
-rw-r--r--src/print_help.h11
-rw-r--r--src/proc.cpp1210
-rw-r--r--src/proc.h558
4 files changed, 638 insertions, 1166 deletions
diff --git a/src/print_help.cpp b/src/print_help.cpp
index 856b87d5..ad479023 100644
--- a/src/print_help.cpp
+++ b/src/print_help.cpp
@@ -1,33 +1,22 @@
-/** \file print_help.c
- Print help message for the specified command
-*/
-#include <stdlib.h>
+// Print help message for the specified command.
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
-#include <sys/types.h>
+#include "common.h"
#include "print_help.h"
#define CMD_LEN 1024
#define HELP_ERR "Could not show help message\n"
-/* defined in common.h */
-ssize_t write_loop(int fd, const char *buff, size_t count);
-
-
-void print_help(const char *c, int fd)
-{
- char cmd[ CMD_LEN];
+void print_help(const char *c, int fd) {
+ char cmd[CMD_LEN];
int printed = snprintf(cmd, CMD_LEN, "fish -c '__fish_print_help %s >&%d'", c, fd);
- if (printed < CMD_LEN)
- {
- if ((system(cmd) == -1))
- {
+ if (printed < CMD_LEN) {
+ if ((system(cmd) == -1)) {
write_loop(2, HELP_ERR, strlen(HELP_ERR));
}
-
}
-
}
diff --git a/src/print_help.h b/src/print_help.h
index 005800b1..9c5a6229 100644
--- a/src/print_help.h
+++ b/src/print_help.h
@@ -1,15 +1,8 @@
-
-/** \file print_help.h
- Print help message for the specified command
-*/
-
+// Print help message for the specified command.
#ifndef FISH_PRINT_HELP_H
#define FISH_PRINT_HELP_H
-/**
- Print help message for the specified command
-*/
-
+/// Print help message for the specified command.
void print_help(const char *cmd, int fd);
#endif
diff --git a/src/proc.cpp b/src/proc.cpp
index f4e47cc9..9bec1811 100644
--- a/src/proc.cpp
+++ b/src/proc.cpp
@@ -1,27 +1,22 @@
-/** \file proc.c
-
-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.
-
-Some of the code in this file is based on code from the Glibc manual.
-*/
+// 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.
+//
+// Some of the code in this file is based on code from the Glibc manual.
// IWYU pragma: no_include <__bit_reference>
#include "config.h"
+#include <errno.h>
+#include <pthread.h>
+#include <signal.h>
#include <stdio.h>
#include <sys/wait.h>
-#include <wchar.h>
-#include <errno.h>
#include <termios.h>
-#include <pthread.h>
+#include <unistd.h>
+#include <wchar.h>
#include <wctype.h>
#include <memory>
#include <vector>
-#include <unistd.h>
-#include <signal.h>
#if HAVE_TERM_H
#include <term.h>
#elif HAVE_NCURSES_TERM_H
@@ -33,62 +28,49 @@ Some of the code in this file is based on code from the Glibc manual.
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
-#include <sys/types.h>
#include <stdbool.h>
#include <sys/time.h> // IWYU pragma: keep
+#include <sys/types.h>
#include <algorithm> // IWYU pragma: keep
-#include "fallback.h" // IWYU pragma: keep
-#include "util.h"
-#include "wutil.h" // IWYU pragma: keep
-#include "proc.h"
#include "common.h"
-#include "reader.h"
-#include "sanity.h"
-#include "parser.h"
-#include "signal.h"
#include "event.h"
-#include "output.h"
+#include "fallback.h" // IWYU pragma: keep
#include "io.h"
+#include "output.h"
#include "parse_tree.h"
+#include "parser.h"
+#include "proc.h"
+#include "reader.h"
+#include "sanity.h"
+#include "signal.h"
+#include "util.h"
+#include "wutil.h" // IWYU pragma: keep
-/**
- Size of buffer for reading buffered output
-*/
+/// Size of buffer for reading buffered output.
#define BUFFER_SIZE 4096
-/**
- Status of last process to exit
-*/
-static int last_status=0;
+/// Status of last process to exit.
+static int last_status = 0;
-bool job_list_is_empty(void)
-{
+bool job_list_is_empty(void) {
ASSERT_IS_MAIN_THREAD();
return parser_t::principal_parser().job_list().empty();
}
-void job_iterator_t::reset()
-{
+void job_iterator_t::reset() {
this->current = job_list->begin();
this->end = job_list->end();
}
-job_iterator_t::job_iterator_t(job_list_t &jobs) : job_list(&jobs)
-{
- this->reset();
-}
+job_iterator_t::job_iterator_t(job_list_t &jobs) : job_list(&jobs) { this->reset(); }
-job_iterator_t::job_iterator_t() : job_list(&parser_t::principal_parser().job_list())
-{
+job_iterator_t::job_iterator_t() : job_list(&parser_t::principal_parser().job_list()) {
ASSERT_IS_MAIN_THREAD();
this->reset();
}
-size_t job_iterator_t::count() const
-{
- return this->job_list->size();
-}
+size_t job_iterator_t::count() const { return this->job_list->size(); }
#if 0
// This isn't used so the lint tools were complaining about its presence. I'm keeping it in the
@@ -106,193 +88,150 @@ void print_jobs(void)
}
#endif
-int is_interactive_session=0;
-int is_subshell=0;
-int is_block=0;
-int is_login=0;
-int is_event=0;
+int is_interactive_session = 0;
+int is_subshell = 0;
+int is_block = 0;
+int is_login = 0;
+int is_event = 0;
pid_t proc_last_bg_pid = 0;
int job_control_mode = JOB_CONTROL_INTERACTIVE;
-int no_exec=0;
+int no_exec = 0;
static int is_interactive = -1;
static bool proc_had_barrier = false;
-int get_is_interactive(void)
-{
+int get_is_interactive(void) {
ASSERT_IS_MAIN_THREAD();
- /* is_interactive is initialized to -1; ensure someone has popped/pushed it before then */
+ // is_interactive is initialized to -1; ensure someone has popped/pushed it before then.
assert(is_interactive >= 0);
return is_interactive > 0;
}
-bool get_proc_had_barrier()
-{
+bool get_proc_had_barrier() {
ASSERT_IS_MAIN_THREAD();
return proc_had_barrier;
}
-void set_proc_had_barrier(bool flag)
-{
+void set_proc_had_barrier(bool flag) {
ASSERT_IS_MAIN_THREAD();
proc_had_barrier = flag;
}
-/**
- The event variable used to send all process event
-*/
+/// The event variable used to send all process event.
static event_t event(0);
-/**
- A stack containing the values of is_interactive. Used by proc_push_interactive and proc_pop_interactive.
-*/
+/// A stack containing the values of is_interactive. Used by proc_push_interactive and
+/// proc_pop_interactive.
static std::vector<int> interactive_stack;
-void proc_init()
-{
- proc_push_interactive(0);
-}
+void proc_init() { proc_push_interactive(0); }
-
-/**
- Remove job from list of jobs
-*/
-static int job_remove(job_t *j)
-{
+/// Remove job from list of jobs.
+static int job_remove(job_t *j) {
ASSERT_IS_MAIN_THREAD();
return parser_t::principal_parser().job_remove(j);
}
-void job_promote(job_t *job)
-{
+void job_promote(job_t *job) {
ASSERT_IS_MAIN_THREAD();
parser_t::principal_parser().job_promote(job);
}
-
-/*
- Remove job from the job list and free all memory associated with
- it.
-*/
-void job_free(job_t * j)
-{
+/// Remove job from the job list and free all memory associated with it.
+void job_free(job_t *j) {
job_remove(j);
delete j;
}
-void proc_destroy()
-{
+void proc_destroy() {
job_list_t &jobs = parser_t::principal_parser().job_list();
- while (! jobs.empty())
- {
+ while (!jobs.empty()) {
job_t *job = jobs.front();
debug(2, L"freeing leaked job %ls", job->command_wcstr());
job_free(job);
}
}
-void proc_set_last_status(int s)
-{
+void proc_set_last_status(int s) {
ASSERT_IS_MAIN_THREAD();
last_status = s;
}
-int proc_get_last_status()
-{
- return last_status;
-}
+int proc_get_last_status() { return last_status; }
-/* Basic thread safe job IDs. The vector consumed_job_ids has a true value wherever the job ID corresponding to that slot is in use. The job ID corresponding to slot 0 is 1. */
+// Basic thread safe job IDs. The vector consumed_job_ids has a true value wherever the job ID
+// corresponding to that slot is in use. The job ID corresponding to slot 0 is 1.
static pthread_mutex_t job_id_lock = PTHREAD_MUTEX_INITIALIZER;
static std::vector<bool> consumed_job_ids;
-job_id_t acquire_job_id(void)
-{
+job_id_t acquire_job_id(void) {
scoped_lock lock(job_id_lock);
- /* Find the index of the first 0 slot */
- std::vector<bool>::iterator slot = std::find(consumed_job_ids.begin(), consumed_job_ids.end(), false);
- if (slot != consumed_job_ids.end())
- {
- /* We found a slot. Note that slot 0 corresponds to job ID 1. */
+ // Find the index of the first 0 slot.
+ std::vector<bool>::iterator slot =
+ std::find(consumed_job_ids.begin(), consumed_job_ids.end(), false);
+ if (slot != consumed_job_ids.end()) {
+ // We found a slot. Note that slot 0 corresponds to job ID 1.
*slot = true;
return (job_id_t)(slot - consumed_job_ids.begin() + 1);
- }
- else
- {
- /* We did not find a slot; create a new slot. The size of the vector is now the job ID (since it is one larger than the slot). */
+ } else {
+ // We did not find a slot; create a new slot. The size of the vector is now the job ID
+ // (since it is one larger than the slot).
consumed_job_ids.push_back(true);
return (job_id_t)consumed_job_ids.size();
}
}
-void release_job_id(job_id_t jid)
-{
+void release_job_id(job_id_t jid) {
assert(jid > 0);
scoped_lock lock(job_id_lock);
size_t slot = (size_t)(jid - 1), count = consumed_job_ids.size();
- /* Make sure this slot is within our vector and is currently set to consumed */
+ // Make sure this slot is within our vector and is currently set to consumed.
assert(slot < count);
assert(consumed_job_ids.at(slot) == true);
- /* Clear it and then resize the vector to eliminate unused trailing job IDs */
+ // Clear it and then resize the vector to eliminate unused trailing job IDs.
consumed_job_ids.at(slot) = false;
- while (count--)
- {
- if (consumed_job_ids.at(count))
- break;
+ while (count--) {
+ if (consumed_job_ids.at(count)) break;
}
consumed_job_ids.resize(count + 1);
}
-job_t *job_get(job_id_t id)
-{
+job_t *job_get(job_id_t id) {
ASSERT_IS_MAIN_THREAD();
return parser_t::principal_parser().job_get(id);
}
-job_t *job_get_from_pid(int pid)
-{
+job_t *job_get_from_pid(int pid) {
ASSERT_IS_MAIN_THREAD();
return parser_t::principal_parser().job_get_from_pid(pid);
}
-
-/*
- Return true if all processes in the job have stopped or completed.
-
- \param j the job to test
-*/
-int job_is_stopped(const job_t *j)
-{
+/// Return true if all processes in the job have stopped or completed.
+///
+/// \param j the job to test
+int job_is_stopped(const job_t *j) {
process_t *p;
- for (p = j->first_process; p; p = p->next)
- {
- if (!p->completed && !p->stopped)
- {
+ for (p = j->first_process; p; p = p->next) {
+ if (!p->completed && !p->stopped) {
return 0;
}
}
return 1;
}
-
-/*
- Return true if the last processes in the job has completed.
-
- \param j the job to test
-*/
-bool job_is_completed(const job_t *j)
-{
+/// Return true if the last processes in the job has completed.
+///
+/// \param j the job to test
+bool job_is_completed(const job_t *j) {
assert(j->first_process != NULL);
bool result = true;
- for (process_t *p = j->first_process; p != NULL; p = p->next)
- {
- if (! p->completed)
- {
+ for (process_t *p = j->first_process; p != NULL; p = p->next) {
+ if (!p->completed) {
result = false;
break;
}
@@ -300,114 +239,81 @@ bool job_is_completed(const job_t *j)
return result;
}
-void job_set_flag(job_t *j, unsigned int flag, int set)
-{
- if (set)
- {
+void job_set_flag(job_t *j, unsigned int flag, int set) {
+ if (set) {
j->flags |= flag;
- }
- else
- {
+ } else {
j->flags &= ~flag;
}
}
-int job_get_flag(const job_t *j, unsigned int flag)
-{
- return !!(j->flags & flag);
-}
+int job_get_flag(const job_t *j, unsigned int flag) { return !!(j->flags & flag); }
-int job_signal(job_t *j, int signal)
-{
+int job_signal(job_t *j, int signal) {
pid_t my_pid = getpid();
int res = 0;
- if (j->pgid != my_pid)
- {
+ if (j->pgid != my_pid) {
res = killpg(j->pgid, SIGHUP);
- }
- else
- {
- for (process_t *p = j->first_process; p; p=p->next)
- {
- if (! p->completed)
- {
- if (p->pid)
- {
- if (kill(p->pid, SIGHUP))
- {
+ } else {
+ for (process_t *p = j->first_process; p; p = p->next) {
+ if (!p->completed) {
+ if (p->pid) {
+ if (kill(p->pid, SIGHUP)) {
res = -1;
break;
}
}
}
}
-
}
return res;
}
-
-/**
- Store the status of the process pid that was returned by waitpid.
-*/
-static void mark_process_status(const job_t *j, process_t *p, int status)
-{
-// debug( 0, L"Process %ls %ls", p->argv[0], WIFSTOPPED (status)?L"stopped":(WIFEXITED( status )?L"exited":(WIFSIGNALED( status )?L"signaled to exit":L"BLARGH")) );
+/// Store the status of the process pid that was returned by waitpid.
+static void mark_process_status(const job_t *j, process_t *p, int status) {
+ // debug( 0, L"Process %ls %ls", p->argv[0], WIFSTOPPED (status)?L"stopped":(WIFEXITED( status
+ // )?L"exited":(WIFSIGNALED( status )?L"signaled to exit":L"BLARGH")) );
p->status = status;
- if (WIFSTOPPED(status))
- {
+ if (WIFSTOPPED(status)) {
p->stopped = 1;
- }
- else if (WIFSIGNALED(status) || WIFEXITED(status))
- {
+ } else if (WIFSIGNALED(status) || WIFEXITED(status)) {
p->completed = 1;
- }
- else
- {
- /* This should never be reached */
+ } else {
+ // This should never be reached.
p->completed = 1;
fprintf(stderr, "Process %ld exited abnormally\n", (long)p->pid);
}
}
-void job_mark_process_as_failed(const job_t *job, process_t *p)
-{
- /* The given process failed to even lift off (e.g. posix_spawn failed) and so doesn't have a valid pid. Mark it as dead. */
- for (process_t *cursor = p; cursor != NULL; cursor = cursor->next)
- {
+void job_mark_process_as_failed(const job_t *job, process_t *p) {
+ // The given process failed to even lift off (e.g. posix_spawn failed) and so doesn't have a
+ // valid pid. Mark it as dead.
+ for (process_t *cursor = p; cursor != NULL; cursor = cursor->next) {
cursor->completed = 1;
}
}
-/**
- Handle status update for child \c pid.
-
- \param pid the pid of the process whose status changes
- \param status the status as returned by wait
-*/
-static void handle_child_status(pid_t pid, int status)
-{
+/// Handle status update for child \c pid.
+///
+/// \param pid the pid of the process whose status changes
+/// \param status the status as returned by wait
+static void handle_child_status(pid_t pid, int status) {
bool found_proc = false;
const job_t *j = NULL;
process_t *p = NULL;
job_iterator_t jobs;
- while (! found_proc && (j = jobs.next()))
- {
- process_t *prev=0;
- for (p=j->first_process; p; p=p->next)
- {
- if (pid == p->pid)
- {
+ while (!found_proc && (j = jobs.next())) {
+ process_t *prev = 0;
+ for (p = j->first_process; p; p = p->next) {
+ if (pid == p->pid) {
mark_process_status(j, p, status);
- if (p->completed && prev != 0)
- {
- if (!prev->completed && prev->pid)
- {
- kill(prev->pid,SIGPIPE);
+ if (p->completed && prev != 0) {
+ if (!prev->completed && prev->pid) {
+ kill(prev->pid, SIGPIPE);
}
}
found_proc = true;
@@ -417,90 +323,66 @@ static void handle_child_status(pid_t pid, int status)
}
}
-
- if (WIFSIGNALED(status) &&
- (WTERMSIG(status)==SIGINT ||
- WTERMSIG(status)==SIGQUIT))
- {
- if (!is_interactive_session)
- {
+ if (WIFSIGNALED(status) && (WTERMSIG(status) == SIGINT || WTERMSIG(status) == SIGQUIT)) {
+ if (!is_interactive_session) {
struct sigaction act;
- sigemptyset(& act.sa_mask);
- act.sa_flags=0;
- act.sa_handler=SIG_DFL;
+ sigemptyset(&act.sa_mask);
+ act.sa_flags = 0;
+ act.sa_handler = SIG_DFL;
sigaction(SIGINT, &act, 0);
sigaction(SIGQUIT, &act, 0);
kill(getpid(), WTERMSIG(status));
- }
- else
- {
- /* In an interactive session, tell the principal parser to skip all blocks we're executing so control-C returns control to the user. */
- if (p && found_proc)
- {
+ } else {
+ // In an interactive session, tell the principal parser to skip all blocks we're
+ // executing so control-C returns control to the user.
+ if (p && found_proc) {
parser_t::skip_all_blocks();
}
}
}
- if (!found_proc)
- {
- /*
- A child we lost track of?
-
- There have been bugs in both subshell handling and in
- builtin handling that have caused this previously...
- */
+ if (!found_proc) {
+ // A child we lost track of? There have been bugs in both subshell handling and in builtin
+ // handling that have caused this previously...
}
return;
}
-process_t::process_t() :
- type(), // gets set later
- internal_block_node(NODE_OFFSET_INVALID),
- pid(0),
- pipe_write_fd(0),
- pipe_read_fd(0),
- completed(0),
- stopped(0),
- status(0),
- count_help_magic(0),
- next(NULL)
+process_t::process_t()
+ : type(), // gets set later
+ internal_block_node(NODE_OFFSET_INVALID),
+ pid(0),
+ pipe_write_fd(0),
+ pipe_read_fd(0),
+ completed(0),
+ stopped(0),
+ status(0),
+ count_help_magic(0),
+ next(NULL)
#ifdef HAVE__PROC_SELF_STAT
- ,last_time(),
- last_jiffies(0)
+ ,
+ last_time(),
+ last_jiffies(0)
#endif
{
}
-process_t::~process_t()
-{
- if (this->next != NULL)
- delete this->next;
+process_t::~process_t() {
+ if (this->next != NULL) delete this->next;
}
-job_t::job_t(job_id_t jobid, const io_chain_t &bio) :
- block_io(bio),
- first_process(NULL),
- pgid(0),
- tmodes(),
- job_id(jobid),
- flags(0)
-{
-}
+job_t::job_t(job_id_t jobid, const io_chain_t &bio)
+ : block_io(bio), first_process(NULL), pgid(0), tmodes(), job_id(jobid), flags(0) {}
-job_t::~job_t()
-{
- if (first_process != NULL)
- delete first_process;
+job_t::~job_t() {
+ if (first_process != NULL) delete first_process;
release_job_id(job_id);
}
-/* Return all the IO redirections. Start with the block IO, then walk over the processes */
-io_chain_t job_t::all_io_redirections() const
-{
+/// Return all the IO redirections. Start with the block IO, then walk over the processes.
+io_chain_t job_t::all_io_redirections() const {
io_chain_t result = this->block_io;
- for (process_t *p = this->first_process; p != NULL; p = p->next)
- {
+ for (process_t *p = this->first_process; p != NULL; p = p->next) {
result.append(p->io_chain());
}
return result;
@@ -508,136 +390,124 @@ io_chain_t job_t::all_io_redirections() const
typedef unsigned int process_generation_count_t;
-/* A static value tracking how many SIGCHLDs we have seen. This is only ever modified from within the SIGCHLD signal handler, and therefore does not need atomics or locks */
+/// A static value tracking how many SIGCHLDs we have seen. This is only ever modified from within
+/// the SIGCHLD signal handler, and therefore does not need atomics or locks.
static volatile process_generation_count_t s_sigchld_generation_count = 0;
-/* If we have received a SIGCHLD signal, process any children. If await is false, this returns immediately if no SIGCHLD has been received. If await is true, this waits for one. Returns true if something was processed. This returns the number of children processed, or -1 on error. */
-static int process_mark_finished_children(bool wants_await)
-{
+/// If we have received a SIGCHLD signal, process any children. If await is false, this returns
+/// immediately if no SIGCHLD has been received. If await is true, this waits for one. Returns true
+/// if something was processed. This returns the number of children processed, or -1 on error.
+static int process_mark_finished_children(bool wants_await) {
ASSERT_IS_MAIN_THREAD();
-
- /* A static value tracking the SIGCHLD gen count at the time we last processed it. When this is different from s_sigchld_generation_count, it indicates there may be unreaped processes. There may not be if we reaped them via the other waitpid path. This is only ever modified from the main thread, and not from a signal handler. */
+
+ // A static value tracking the SIGCHLD gen count at the time we last processed it. When this is
+ // different from s_sigchld_generation_count, it indicates there may be unreaped processes.
+ // There may not be if we reaped them via the other waitpid path. This is only ever modified
+ // from the main thread, and not from a signal handler.
static process_generation_count_t s_last_processed_sigchld_generation_count = 0;
-
+
int processed_count = 0;
bool got_error = false;
- /* The critical read. This fetches a value which is only written in the signal handler. This needs to be an atomic read (we'd use sig_atomic_t, if we knew that were unsigned - fortunately aligned unsigned int is atomic on pretty much any modern chip.) It also needs to occur before we start reaping, since the signal handler can be invoked at any point. */
+ // The critical read. This fetches a value which is only written in the signal handler. This
+ // needs to be an atomic read (we'd use sig_atomic_t, if we knew that were unsigned -
+ // fortunately aligned unsigned int is atomic on pretty much any modern chip.) It also needs to
+ // occur before we start reaping, since the signal handler can be invoked at any point.
const process_generation_count_t local_count = s_sigchld_generation_count;
-
- /* Determine whether we have children to process. Note that we can't reliably use the difference because a single SIGCHLD may be delivered for multiple children - see #1768. Also if we are awaiting, we always process. */
+
+ // Determine whether we have children to process. Note that we can't reliably use the difference
+ // because a single SIGCHLD may be delivered for multiple children - see #1768. Also if we are
+ // awaiting, we always process.
bool wants_waitpid = wants_await || local_count != s_last_processed_sigchld_generation_count;
- if (wants_waitpid)
- {
- for (;;)
- {
- /* Call waitpid until we get 0/ECHILD. If we wait, it's only on the first iteration. So we want to set NOHANG (don't wait) unless wants_await is true and this is the first iteration. */
+ if (wants_waitpid) {
+ for (;;) {
+ // Call waitpid until we get 0/ECHILD. If we wait, it's only on the first iteration. So
+ // we want to set NOHANG (don't wait) unless wants_await is true and this is the first
+ // iteration.
int options = WUNTRACED;
- if (! (wants_await && processed_count == 0))
- {
+ if (!(wants_await && processed_count == 0)) {
options |= WNOHANG;
}
-
+
int status = -1;
pid_t pid = waitpid(-1, &status, options);
- if (pid > 0)
- {
- /* We got a valid pid */
+ if (pid > 0) {
+ // We got a valid pid.
handle_child_status(pid, status);
processed_count += 1;
- }
- else if (pid == 0)
- {
- /* No ready-and-waiting children, we're done */
+ } else if (pid == 0) {
+ // No ready-and-waiting children, we're done.
break;
- }
- else
- {
- /* This indicates an error. One likely failure is ECHILD (no children), which we break on, and is not considered an error. The other likely failure is EINTR, which means we got a signal, which is considered an error. */
+ } else {
+ // This indicates an error. One likely failure is ECHILD (no children), which we
+ // break on, and is not considered an error. The other likely failure is EINTR,
+ // which means we got a signal, which is considered an error.
got_error = (errno != ECHILD);
break;
}
}
}
- if (got_error)
- {
+ if (got_error) {
return -1;
- }
- else
- {
+ } else {
s_last_processed_sigchld_generation_count = local_count;
return processed_count;
}
}
-
-/* This is called from a signal handler. The signal is always SIGCHLD. */
-void job_handle_signal(int signal, siginfo_t *info, void *con)
-{
- /* This is the only place that this generation count is modified. It's OK if it overflows. */
+/// This is called from a signal handler. The signal is always SIGCHLD.
+void job_handle_signal(int signal, siginfo_t *info, void *con) {
+ // This is the only place that this generation count is modified. It's OK if it overflows.
s_sigchld_generation_count += 1;
}
-/* Given a command like "cat file", truncate it to a reasonable length */
-static wcstring truncate_command(const wcstring &cmd)
-{
+/// Given a command like "cat file", truncate it to a reasonable length.
+static wcstring truncate_command(const wcstring &cmd) {
const size_t max_len = 32;
- if (cmd.size() <= max_len)
- {
- // No truncation necessary
+ if (cmd.size() <= max_len) {
+ // No truncation necessary.
return cmd;
}
-
- // Truncation required
+
+ // Truncation required.
const bool ellipsis_is_unicode = (ellipsis_char == L'\x2026');
const size_t ellipsis_length = ellipsis_is_unicode ? 1 : 3;
size_t trunc_length = max_len - ellipsis_length;
- // Eat trailing whitespace
- while (trunc_length > 0 && iswspace(cmd.at(trunc_length - 1)))
- {
+ // Eat trailing whitespace.
+ while (trunc_length > 0 && iswspace(cmd.at(trunc_length - 1))) {
trunc_length -= 1;
}
wcstring result = wcstring(cmd, 0, trunc_length);
- // Append ellipsis
- if (ellipsis_is_unicode)
- {
+ // Append ellipsis.
+ if (ellipsis_is_unicode) {
result.push_back(ellipsis_char);
- }
- else
- {
+ } else {
result.append(L"...");
}
return result;
}
-/**
- Format information about job status for the user to look at.
-
- \param j the job to test
- \param status a string description of the job exit type
-*/
-static void format_job_info(const job_t *j, const wchar_t *status, size_t job_count)
-{
+/// Format information about job status for the user to look at.
+///
+/// \param j the job to test
+/// \param status a string description of the job exit type
+static void format_job_info(const job_t *j, const wchar_t *status, size_t job_count) {
fwprintf(stdout, L"\r");
- if (job_count == 1)
- {
+ if (job_count == 1) {
fwprintf(stdout, _(L"\'%ls\' has %ls"), truncate_command(j->command()).c_str(), status);
- }
- else
- {
- fwprintf(stdout, _(L"Job %d, \'%ls\' has %ls"), j->job_id, truncate_command(j->command()).c_str(), status);
+ } else {
+ fwprintf(stdout, _(L"Job %d, \'%ls\' has %ls"), j->job_id,
+ truncate_command(j->command()).c_str(), status);
}
fflush(stdout);
- tputs(clr_eol,1,&writeb);
+ tputs(clr_eol, 1, &writeb);
fwprintf(stdout, L"\n");
}
-void proc_fire_event(const wchar_t *msg, int type, pid_t pid, int status)
-{
-
- event.type=type;
+void proc_fire_event(const wchar_t *msg, int type, pid_t pid, int status) {
+ event.type = type;
event.param1.pid = pid;
event.arguments.push_back(msg);
@@ -647,146 +517,126 @@ void proc_fire_event(const wchar_t *msg, int type, pid_t pid, int status)
event.arguments.resize(0);
}
-int job_reap(bool interactive)
-{
+int job_reap(bool interactive) {
ASSERT_IS_MAIN_THREAD();
job_t *jnext;
- int found=0;
+ int found = 0;
- /* job_reap may fire an event handler, we do not want to call ourselves recursively (to avoid infinite recursion). */
+ // job_reap may fire an event handler, we do not want to call ourselves recursively (to avoid
+ // infinite recursion).
static bool locked = false;
- if (locked)
- {
+ if (locked) {
return 0;
}
locked = true;
-
+
process_mark_finished_children(false);
- /* Preserve the exit status */
+ // Preserve the exit status.
const int saved_status = proc_get_last_status();
job_iterator_t jobs;
const size_t job_count = jobs.count();
jnext = jobs.next();
- while (jnext)
- {
+ while (jnext) {
job_t *j = jnext;
jnext = jobs.next();
- /*
- If we are reaping only jobs who do not need status messages
- sent to the console, do not consider reaping jobs that need
- status messages
- */
- if ((!job_get_flag(j, JOB_SKIP_NOTIFICATION)) && (!interactive) && (!job_get_flag(j, JOB_FOREGROUND)))
- {
+ // If we are reaping only jobs who do not need status messages sent to the console, do not
+ // consider reaping jobs that need status messages.
+ if ((!job_get_flag(j, JOB_SKIP_NOTIFICATION)) && (!interactive) &&
+ (!job_get_flag(j, JOB_FOREGROUND))) {
continue;
}
- for (process_t *p = j->first_process; p; p=p->next)
- {
+ for (process_t *p = j->first_process; p; p = p->next) {
int s;
- if (!p->completed)
- continue;
+ if (!p->completed) continue;
- if (!p->pid)
- continue;
+ if (!p->pid) continue;
s = p->status;
- proc_fire_event(L"PROCESS_EXIT", EVENT_EXIT, p->pid, (WIFSIGNALED(s)?-1:WEXITSTATUS(s)));
-
- if (WIFSIGNALED(s))
- {
- /*
- Ignore signal SIGPIPE.We issue it ourselves to the pipe
- writer when the pipe reader dies.
- */
- if (WTERMSIG(s) != SIGPIPE)
- {
- int proc_is_job = ((p==j->first_process) && (p->next == 0));
- if (proc_is_job)
- job_set_flag(j, JOB_NOTIFIED, 1);
- if (!job_get_flag(j, JOB_SKIP_NOTIFICATION))
- {
- /* Print nothing if we get SIGINT in the foreground process group, to avoid spamming obvious stuff on the console (#1119). If we get SIGINT for the foreground process, assume the user typed ^C and can see it working. It's possible they didn't, and the signal was delivered via pkill, etc., but the SIGINT/SIGTERM distinction is precisely to allow INT to be from a UI and TERM to be programmatic, so this assumption is keeping with the design of signals.
- If echoctl is on, then the terminal will have written ^C to the console. If off, it won't have. We don't echo ^C either way, so as to respect the user's preference. */
- if (WTERMSIG(p->status) != SIGINT || ! job_get_flag(j, JOB_FOREGROUND))
- {
- if (proc_is_job)
- {
- // We want to report the job number, unless it's the only job, in which case we don't need to
- const wcstring job_number_desc = (job_count == 1) ? wcstring() : format_string(L"Job %d, ", j->job_id);
+ proc_fire_event(L"PROCESS_EXIT", EVENT_EXIT, p->pid,
+ (WIFSIGNALED(s) ? -1 : WEXITSTATUS(s)));
+
+ if (WIFSIGNALED(s)) {
+ // Ignore signal SIGPIPE.We issue it ourselves to the pipe writer when the pipe
+ // reader dies.
+ if (WTERMSIG(s) != SIGPIPE) {
+ int proc_is_job = ((p == j->first_process) && (p->next == 0));
+ if (proc_is_job) job_set_flag(j, JOB_NOTIFIED, 1);
+ if (!job_get_flag(j, JOB_SKIP_NOTIFICATION)) {
+ // Print nothing if we get SIGINT in the foreground process group, to avoid
+ // spamming obvious stuff on the console (#1119). If we get SIGINT for the
+ // foreground process, assume the user typed ^C and can see it working. It's
+ // possible they didn't, and the signal was delivered via pkill, etc., but
+ // the SIGINT/SIGTERM distinction is precisely to allow INT to be from a UI
+ // and TERM to be programmatic, so this assumption is keeping with the
+ // design of signals. If echoctl is on, then the terminal will have written
+ // ^C to the console. If off, it won't have. We don't echo ^C either way, so
+ // as to respect the user's preference.
+ if (WTERMSIG(p->status) != SIGINT || !job_get_flag(j, JOB_FOREGROUND)) {
+ if (proc_is_job) {
+ // We want to report the job number, unless it's the only job, in
+ // which case we don't need to.
+ const wcstring job_number_desc =
+ (job_count == 1) ? wcstring()
+ : format_string(L"Job %d, ", j->job_id);
fwprintf(stdout,
_(L"%ls: %ls\'%ls\' terminated by signal %ls (%ls)"),
- program_name,
- job_number_desc.c_str(),
+ program_name, job_number_desc.c_str(),
truncate_command(j->command()).c_str(),
sig2wcs(WTERMSIG(p->status)),
signal_get_desc(WTERMSIG(p->status)));
- }
- else
- {
- const wcstring job_number_desc = (job_count == 1) ? wcstring() : format_string(L"from job %d, ", j->job_id);
- fwprintf(stdout,
- _(L"%ls: Process %d, \'%ls\' %ls\'%ls\' terminated by signal %ls (%ls)"),
- program_name,
- p->pid,
- p->argv0(),
- job_number_desc.c_str(),
+ } else {
+ const wcstring job_number_desc =
+ (job_count == 1) ? wcstring()
+ : format_string(L"from job %d, ", j->job_id);
+ fwprintf(stdout, _(L"%ls: Process %d, \'%ls\' %ls\'%ls\' "
+ L"terminated by signal %ls (%ls)"),
+ program_name, p->pid, p->argv0(), job_number_desc.c_str(),
truncate_command(j->command()).c_str(),
sig2wcs(WTERMSIG(p->status)),
signal_get_desc(WTERMSIG(p->status)));
}
- tputs(clr_eol,1,&writeb);
+ tputs(clr_eol, 1, &writeb);
fwprintf(stdout, L"\n");
}
- found=1;
+ found = 1;
}
- /*
- Clear status so it is not reported more than once
- */
+ // Clear status so it is not reported more than once.
p->status = 0;
}
}
}
- /*
- If all processes have completed, tell the user the job has
- completed and delete it from the active job list.
- */
- if (job_is_completed(j))
- {
- if (!job_get_flag(j, JOB_FOREGROUND) && !job_get_flag(j, JOB_NOTIFIED) && !job_get_flag(j, JOB_SKIP_NOTIFICATION))
- {
+ // If all processes have completed, tell the user the job has completed and delete it from
+ // the active job list.
+ if (job_is_completed(j)) {
+ if (!job_get_flag(j, JOB_FOREGROUND) && !job_get_flag(j, JOB_NOTIFIED) &&
+ !job_get_flag(j, JOB_SKIP_NOTIFICATION)) {
format_job_info(j, _(L"ended"), job_count);
- found=1;
+ found = 1;
}
proc_fire_event(L"JOB_EXIT", EVENT_EXIT, -j->pgid, 0);
proc_fire_event(L"JOB_EXIT", EVENT_JOB_ID, j->job_id, 0);
job_free(j);
- }
- else if (job_is_stopped(j) && !job_get_flag(j, JOB_NOTIFIED))
- {
- /*
- Notify the user about newly stopped jobs.
- */
- if (!job_get_flag(j, JOB_SKIP_NOTIFICATION))
- {
+ } else if (job_is_stopped(j) && !job_get_flag(j, JOB_NOTIFIED)) {
+ // Notify the user about newly stopped jobs.
+ if (!job_get_flag(j, JOB_SKIP_NOTIFICATION)) {
format_job_info(j, _(L"stopped"), job_count);
- found=1;
+ found = 1;
}
job_set_flag(j, JOB_NOTIFIED, 1);
}
}
- if (found)
- fflush(stdout);
+ if (found) fflush(stdout);
- /* Restore the exit status. */
+ // Restore the exit status.
proc_set_last_status(saved_status);
locked = false;
@@ -794,160 +644,119 @@ int job_reap(bool interactive)
return found;
}
-
#ifdef HAVE__PROC_SELF_STAT
-/**
- Maximum length of a /proc/[PID]/stat filename
-*/
+/// Maximum length of a /proc/[PID]/stat filename.
#define FN_SIZE 256
-/**
- Get the CPU time for the specified process
-*/
-unsigned long proc_get_jiffies(process_t *p)
-{
+/// Get the CPU time for the specified process.
+unsigned long proc_get_jiffies(process_t *p) {
wchar_t fn[FN_SIZE];
char state;
- int pid, ppid, pgrp,
- session, tty_nr, tpgid,
- exit_signal, processor;
-
- long int cutime, cstime, priority,
- nice, placeholder, itrealvalue,
- rss;
- unsigned long int flags, minflt, cminflt,
- majflt, cmajflt, utime,
- stime, starttime, vsize,
- rlim, startcode, endcode,
- startstack, kstkesp, kstkeip,
- signal, blocked, sigignore,
- sigcatch, wchan, nswap, cnswap;
+ int pid, ppid, pgrp, session, tty_nr, tpgid, exit_signal, processor;
+
+ long int cutime, cstime, priority, nice, placeholder, itrealvalue, rss;
+ unsigned long int flags, minflt, cminflt, majflt, cmajflt, utime, stime, starttime, vsize, rlim,
+ startcode, endcode, startstack, kstkesp, kstkeip, signal, blocked, sigignore, sigcatch,
+ wchan, nswap, cnswap;
char comm[1024];
- if (p->pid <= 0)
- return 0;
+ if (p->pid <= 0) return 0;
swprintf(fn, FN_SIZE, L"/proc/%d/stat", p->pid);
FILE *f = wfopen(fn, "r");
- if (!f)
- return 0;
+ if (!f) return 0;
- int count = fscanf(f,
- "%d %s %c "
- "%d %d %d "
- "%d %d %lu "
+ int count = fscanf(
+ f,
+ "%d %s %c "
+ "%d %d %d "
+ "%d %d %lu "
- "%lu %lu %lu "
- "%lu %lu %lu "
- "%ld %ld %ld "
+ "%lu %lu %lu "
+ "%lu %lu %lu "
+ "%ld %ld %ld "
- "%ld %ld %ld "
- "%lu %lu %ld "
- "%lu %lu %lu "
+ "%ld %ld %ld "
+ "%lu %lu %ld "
+ "%lu %lu %lu "
- "%lu %lu %lu "
- "%lu %lu %lu "
- "%lu %lu %lu "
+ "%lu %lu %lu "
+ "%lu %lu %lu "
+ "%lu %lu %lu "
- "%lu %d %d ",
+ "%lu %d %d ",
- &pid, comm, &state,
- &ppid, &pgrp, &session,
- &tty_nr, &tpgid, &flags,
+ &pid, comm, &state, &ppid, &pgrp, &session, &tty_nr, &tpgid, &flags,
- &minflt, &cminflt, &majflt,
- &cmajflt, &utime, &stime,
- &cutime, &cstime, &priority,
+ &minflt, &cminflt, &majflt, &cmajflt, &utime, &stime, &cutime, &cstime, &priority,
- &nice, &placeholder, &itrealvalue,
- &starttime, &vsize, &rss,
- &rlim, &startcode, &endcode,
+ &nice, &placeholder, &itrealvalue, &starttime, &vsize, &rss, &rlim, &startcode, &endcode,
- &startstack, &kstkesp, &kstkeip,
- &signal, &blocked, &sigignore,
- &sigcatch, &wchan, &nswap,
+ &startstack, &kstkesp, &kstkeip, &signal, &blocked, &sigignore, &sigcatch, &wchan, &nswap,
- &cnswap, &exit_signal, &processor
- );
+ &cnswap, &exit_signal, &processor);
- /*
- Don't need to check exit status of fclose on read-only streams
- */
+ // Don't need to check exit status of fclose on read-only streams.
fclose(f);
-
- if (count < 17)
- {
+
+ if (count < 17) {
return 0;
}
- return utime+stime+cutime+cstime;
-
+ return utime + stime + cutime + cstime;
}
-/**
- Update the CPU time for all jobs
-*/
-void proc_update_jiffies()
-{
- job_t* job;
+/// Update the CPU time for all jobs.
+void proc_update_jiffies() {
+ job_t *job;
process_t *p;
job_iterator_t j;
- for (job = j.next(); job; job = j.next())
- {
- for (p=job->first_process; p; p=p->next)
- {
+ for (job = j.next(); job; job = j.next()) {
+ for (p = job->first_process; p; p = p->next) {
gettimeofday(&p->last_time, 0);
p->last_jiffies = proc_get_jiffies(p);
}
}
}
-
#endif
-/**
- Check if there are buffers associated with the job, and select on
- them for a while if available.
-
- \param j the job to test
-
- \return 1 if buffers were available, zero otherwise
-*/
-static int select_try(job_t *j)
-{
+/// Check if there are buffers associated with the job, and select on them for a while if available.
+///
+/// \param j the job to test
+///
+/// \return 1 if buffers were available, zero otherwise
+static int select_try(job_t *j) {
fd_set fds;
- int maxfd=-1;
+ int maxfd = -1;
FD_ZERO(&fds);
const io_chain_t chain = j->all_io_redirections();
- for (size_t idx = 0; idx < chain.size(); idx++)
- {
+ for (size_t idx = 0; idx < chain.size(); idx++) {
const io_data_t *io = chain.at(idx).get();
- if (io->io_mode == IO_BUFFER)
- {
+ if (io->io_mode == IO_BUFFER) {
CAST_INIT(const io_pipe_t *, io_pipe, io);
int fd = io_pipe->pipe_fd[0];
-// fwprintf( stderr, L"fd %d on job %ls\n", fd, j->command );
+ // fwprintf( stderr, L"fd %d on job %ls\n", fd, j->command );
FD_SET(fd, &fds);
maxfd = maxi(maxfd, fd);
debug(3, L"select_try on %d\n", fd);
}
}
- if (maxfd >= 0)
- {
+ if (maxfd >= 0) {
int retval;
struct timeval tv;
- tv.tv_sec=0;
- tv.tv_usec=10000;
+ tv.tv_sec = 0;
+ tv.tv_usec = 10000;
- retval =select(maxfd+1, &fds, 0, 0, &tv);
+ retval = select(maxfd + 1, &fds, 0, 0, &tv);
if (retval == 0) {
debug(3, L"select_try hit timeout\n");
}
@@ -957,90 +766,58 @@ static int select_try(job_t *j)
return -1;
}
-/**
- Read from descriptors until they are empty.
-
- \param j the job to test
-*/
-static void read_try(job_t *j)
-{
+/// Read from descriptors until they are empty.
+///
+/// \param j the job to test
+static void read_try(job_t *j) {
io_buffer_t *buff = NULL;
- /*
- Find the last buffer, which is the one we want to read from
- */
+ // Find the last buffer, which is the one we want to read from.
const io_chain_t chain = j->all_io_redirections();
- for (size_t idx = 0; idx < chain.size(); idx++)
- {
+ for (size_t idx = 0; idx < chain.size(); idx++) {
io_data_t *d = chain.at(idx).get();
- if (d->io_mode == IO_BUFFER)
- {
+ if (d->io_mode == IO_BUFFER) {
buff = static_cast<io_buffer_t *>(d);
}
}
- if (buff)
- {
+ if (buff) {
debug(3, L"proc::read_try('%ls')\n", j->command_wcstr());
- while (1)
- {
+ while (1) {
char b[BUFFER_SIZE];
long l;
- l=read_blocked(buff->pipe_fd[0],
- b, BUFFER_SIZE);
- if (l==0)
- {
+ l = read_blocked(buff->pipe_fd[0], b, BUFFER_SIZE);
+ if (l == 0) {
break;
- }
- else if (l<0)
- {
- if (errno != EAGAIN)
- {
- debug(1,
- _(L"An error occured while reading output from code block"));
+ } else if (l < 0) {
+ if (errno != EAGAIN) {
+ debug(1, _(L"An error occured while reading output from code block"));
wperror(L"read_try");
}
break;
- }
- else
- {
+ } else {
buff->out_buffer_append(b, l);
}
}
}
}
-
-/**
- Give ownership of the terminal to the specified job.
-
- \param j The job to give the terminal to.
-
- \param cont If this variable is set, we are giving back control to
- a job that has previously been stopped. In that case, we need to
- set the terminal attributes to those saved in the job.
- */
-static bool terminal_give_to_job(job_t *j, int cont)
-{
-
- if (tcsetpgrp(0, j->pgid))
- {
- debug(1,
- _(L"Could not send job %d ('%ls') to foreground"),
- j->job_id,
- j->command_wcstr());
+/// Give ownership of the terminal to the specified job.
+///
+/// \param j The job to give the terminal to.
+/// \param cont If this variable is set, we are giving back control to a job that has previously
+/// been stopped. In that case, we need to set the terminal attributes to those saved in the job.
+static bool terminal_give_to_job(job_t *j, int cont) {
+ if (tcsetpgrp(0, j->pgid)) {
+ debug(1, _(L"Could not send job %d ('%ls') to foreground"), j->job_id, j->command_wcstr());
wperror(L"tcsetpgrp");
return false;
}
- if (cont)
- {
- if (tcsetattr(0, TCSADRAIN, &j->tmodes))
- {
- debug(1,
- _(L"Could not send job %d ('%ls') to foreground"),
- j->job_id,
+ if (cont) {
+ if (tcsetattr(0, TCSADRAIN, &j->tmodes)) {
+ debug(1, _(L"Could not send job %d ('%ls') to foreground"), j->job_id,
j->command_wcstr());
wperror(L"tcsetattr");
return false;
@@ -1049,16 +826,10 @@ static bool terminal_give_to_job(job_t *j, int cont)
return true;
}
-/**
- Returns control of the terminal to the shell, and saves the terminal
- attribute state to the job, so that we can restore the terminal
- ownership to the job at a later time .
-*/
-static int terminal_return_from_job(job_t *j)
-{
-
- if (tcsetpgrp(0, getpgrp()))
- {
+/// Returns control of the terminal to the shell, and saves the terminal attribute state to the job,
+/// so that we can restore the terminal ownership to the job at a later time.
+static int terminal_return_from_job(job_t *j) {
+ if (tcsetpgrp(0, getpgrp())) {
debug(1, _(L"Could not return shell to foreground"));
wperror(L"tcsetpgrp");
return 0;
@@ -1067,23 +838,19 @@ static int terminal_return_from_job(job_t *j)
/*
Save jobs terminal modes.
*/
- if (tcgetattr(0, &j->tmodes))
- {
+ if (tcgetattr(0, &j->tmodes)) {
debug(1, _(L"Could not return shell to foreground"));
wperror(L"tcgetattr");
return 0;
}
- /* Disabling this per https://github.com/adityagodbole/fish-shell/commit/9d229cd18c3e5c25a8bd37e9ddd3b67ddc2d1b72
- On Linux, 'cd . ; ftp' prevents you from typing into the ftp prompt
- See https://github.com/fish-shell/fish-shell/issues/121
- */
+// Disabling this per
+// https://github.com/adityagodbole/fish-shell/commit/9d229cd18c3e5c25a8bd37e9ddd3b67ddc2d1b72 On
+// Linux, 'cd . ; ftp' prevents you from typing into the ftp prompt. See
+// https://github.com/fish-shell/fish-shell/issues/121
#if 0
- /*
- Restore the shell's terminal modes.
- */
- if (tcsetattr(0, TCSADRAIN, &shell_modes))
- {
+ // Restore the shell's terminal modes.
+ if (tcsetattr(0, TCSADRAIN, &shell_modes)) {
debug(1, _(L"Could not return shell to foreground"));
wperror(L"tcsetattr");
return 0;
@@ -1093,29 +860,21 @@ static int terminal_return_from_job(job_t *j)
return 1;
}
-void job_continue(job_t *j, bool cont)
-{
- /*
- Put job first in the job list
- */
+void job_continue(job_t *j, bool cont) {
+ // Put job first in the job list.
job_promote(j);
job_set_flag(j, JOB_NOTIFIED, 0);
CHECK_BLOCK();
- debug(4,
- L"Continue job %d, gid %d (%ls), %ls, %ls",
- j->job_id,
- j->pgid,
- j->command_wcstr(),
- job_is_completed(j)?L"COMPLETED":L"UNCOMPLETED",
- is_interactive?L"INTERACTIVE":L"NON-INTERACTIVE");
-
- if (!job_is_completed(j))
- {
- if (job_get_flag(j, JOB_TERMINAL) && job_get_flag(j, JOB_FOREGROUND))
- {
- /* Put the job into the foreground. Hack: ensure that stdin is marked as blocking first (#176). */
+ debug(4, L"Continue job %d, gid %d (%ls), %ls, %ls", j->job_id, j->pgid, j->command_wcstr(),
+ job_is_completed(j) ? L"COMPLETED" : L"UNCOMPLETED",
+ is_interactive ? L"INTERACTIVE" : L"NON-INTERACTIVE");
+
+ if (!job_is_completed(j)) {
+ if (job_get_flag(j, JOB_TERMINAL) && job_get_flag(j, JOB_FOREGROUND)) {
+ // Put the job into the foreground. Hack: ensure that stdin is marked as blocking first
+ // (issue #176).
make_fd_blocking(STDIN_FILENO);
signal_block();
@@ -1124,34 +883,23 @@ void job_continue(job_t *j, bool cont)
signal_unblock();
- if (!ok)
- return;
+ if (!ok) return;
}
- /*
- Send the job a continue signal, if necessary.
- */
- if (cont)
- {
+ // Send the job a continue signal, if necessary.
+ if (cont) {
process_t *p;
- for (p=j->first_process; p; p=p->next)
- p->stopped=0;
+ for (p = j->first_process; p; p = p->next) p->stopped = 0;
- if (job_get_flag(j, JOB_CONTROL))
- {
- if (killpg(j->pgid, SIGCONT))
- {
+ if (job_get_flag(j, JOB_CONTROL)) {
+ if (killpg(j->pgid, SIGCONT)) {
wperror(L"killpg (SIGCONT)");
return;
}
- }
- else
- {
- for (p=j->first_process; p; p=p->next)
- {
- if (kill(p->pid, SIGCONT) < 0)
- {
+ } else {
+ for (p = j->first_process; p; p = p->next) {
+ if (kill(p->pid, SIGCONT) < 0) {
wperror(L"kill (SIGCONT)");
return;
}
@@ -1159,46 +907,33 @@ void job_continue(job_t *j, bool cont)
}
}
- if (job_get_flag(j, JOB_FOREGROUND))
- {
- /* Look for finished processes first, to avoid select() if it's already done. */
+ if (job_get_flag(j, JOB_FOREGROUND)) {
+ // Look for finished processes first, to avoid select() if it's already done.
process_mark_finished_children(false);
- /*
- Wait for job to report.
- */
- while (! reader_exit_forced() && ! job_is_stopped(j) && ! job_is_completed(j))
- {
-// debug( 1, L"select_try()" );
- switch (select_try(j))
- {
- case 1:
- {
+ // Wait for job to report.
+ while (!reader_exit_forced() && !job_is_stopped(j) && !job_is_completed(j)) {
+ // debug( 1, L"select_try()" );
+ switch (select_try(j)) {
+ case 1: {
read_try(j);
process_mark_finished_children(false);
break;
}
-
- case 0:
- {
- /* No FDs are ready. Look for finished processes. */
+
+ case 0: {
+ // No FDs are ready. Look for finished processes.
process_mark_finished_children(false);
break;
}
- case -1:
- {
- /*
- If there is no funky IO magic, we can use
- waitpid instead of handling child deaths
- through signals. This gives a rather large
- speed boost (A factor 3 startup time
- improvement on my 300 MHz machine) on
- short-lived jobs.
-
- This will return early if we get a signal,
- like SIGHUP.
- */
+ case -1: {
+ // If there is no funky IO magic, we can use waitpid instead of handling
+ // child deaths through signals. This gives a rather large speed boost (A
+ // factor 3 startup time improvement on my 300 MHz machine) on short-lived
+ // jobs.
+ //
+ // This will return early if we get a signal, like SIGHUP.
process_mark_finished_children(true);
break;
}
@@ -1207,40 +942,34 @@ void job_continue(job_t *j, bool cont)
}
}
- if (job_get_flag(j, JOB_FOREGROUND))
- {
-
- if (job_is_completed(j))
- {
-
- // It's possible that the job will produce output and exit before we've even read from it.
+ if (job_get_flag(j, JOB_FOREGROUND)) {
+ if (job_is_completed(j)) {
+ // It's possible that the job will produce output and exit before we've even read from
+ // it.
+ //
// We'll eventually read the output, but it may be after we've executed subsequent calls
// This is why my prompt colors kept getting screwed up - the builtin echo calls
- // were sometimes having their output combined with the set_color calls in the wrong order!
+ // were sometimes having their output combined with the set_color calls in the wrong
+ // order!
read_try(j);
process_t *p = j->first_process;
- while (p->next)
- p = p->next;
-
- if (WIFEXITED(p->status) || WIFSIGNALED(p->status))
- {
- /*
- Mark process status only if we are in the foreground
- and the last process in a pipe, and it is not a short circuited builtin
- */
- if (p->pid)
- {
+ while (p->next) p = p->next;
+
+ if (WIFEXITED(p->status) || WIFSIGNALED(p->status)) {
+ // Mark process status only if we are in the foreground and the last process in a
+ // pipe, and it is not a short circuited builtin.
+ if (p->pid) {
int status = proc_format_status(p->status);
- //wprintf(L"setting status %d for %ls\n", job_get_flag( j, JOB_NEGATE )?!status:status, j->command);
- proc_set_last_status(job_get_flag(j, JOB_NEGATE)?!status:status);
+ // wprintf(L"setting status %d for %ls\n", job_get_flag( j, JOB_NEGATE
+ // )?!status:status, j->command);
+ proc_set_last_status(job_get_flag(j, JOB_NEGATE) ? !status : status);
}
}
}
- /* Put the shell back in the foreground. */
- if (job_get_flag(j, JOB_TERMINAL) && job_get_flag(j, JOB_FOREGROUND))
- {
+ // Put the shell back in the foreground.
+ if (job_get_flag(j, JOB_TERMINAL) && job_get_flag(j, JOB_FOREGROUND)) {
int ok;
signal_block();
@@ -1249,114 +978,79 @@ void job_continue(job_t *j, bool cont)
signal_unblock();
- if (!ok)
- return;
-
+ if (!ok) return;
}
}
-
}
-int proc_format_status(int status)
-{
- if (WIFSIGNALED(status))
- {
- return 128+WTERMSIG(status);
- }
- else if (WIFEXITED(status))
- {
+int proc_format_status(int status) {
+ if (WIFSIGNALED(status)) {
+ return 128 + WTERMSIG(status);
+ } else if (WIFEXITED(status)) {
return WEXITSTATUS(status);
}
return status;
-
}
-
-void proc_sanity_check()
-{
+void proc_sanity_check() {
job_t *j;
- job_t *fg_job=0;
+ job_t *fg_job = 0;
job_iterator_t jobs;
- while ((j = jobs.next()))
- {
+ while ((j = jobs.next())) {
process_t *p;
- if (!job_get_flag(j, JOB_CONSTRUCTED))
- continue;
+ if (!job_get_flag(j, JOB_CONSTRUCTED)) continue;
+ validate_pointer(j->first_process, _(L"Process list pointer"), 0);
- validate_pointer(j->first_process,
- _(L"Process list pointer"),
- 0);
-
- /*
- More than one foreground job?
- */
- if (job_get_flag(j, JOB_FOREGROUND) && !(job_is_stopped(j) || job_is_completed(j)))
- {
- if (fg_job != 0)
- {
- debug(0,
- _(L"More than one job in foreground: job 1: '%ls' job 2: '%ls'"),
- fg_job->command_wcstr(),
- j->command_wcstr());
+ // More than one foreground job?
+ if (job_get_flag(j, JOB_FOREGROUND) && !(job_is_stopped(j) || job_is_completed(j))) {
+ if (fg_job != 0) {
+ debug(0, _(L"More than one job in foreground: job 1: '%ls' job 2: '%ls'"),
+ fg_job->command_wcstr(), j->command_wcstr());
sanity_lose();
}
fg_job = j;
}
p = j->first_process;
- while (p)
- {
- /* Internal block nodes do not have argv - see #1545 */
+ while (p) {
+ // Internal block nodes do not have argv - see issue #1545.
bool null_ok = (p->type == INTERNAL_BLOCK_NODE);
validate_pointer(p->get_argv(), _(L"Process argument list"), null_ok);
validate_pointer(p->argv0(), _(L"Process name"), null_ok);
validate_pointer(p->next, _(L"Process list pointer"), true);
- if ((p->stopped & (~0x00000001)) != 0)
- {
- debug(0,
- _(L"Job '%ls', process '%ls' has inconsistent state \'stopped\'=%d"),
- j->command_wcstr(),
- p->argv0(),
- p->stopped);
+ if ((p->stopped & (~0x00000001)) != 0) {
+ debug(0, _(L"Job '%ls', process '%ls' has inconsistent state \'stopped\'=%d"),
+ j->command_wcstr(), p->argv0(), p->stopped);
sanity_lose();
}
- if ((p->completed & (~0x00000001)) != 0)
- {
- debug(0,
- _(L"Job '%ls', process '%ls' has inconsistent state \'completed\'=%d"),
- j->command_wcstr(),
- p->argv0(),
- p->completed);
+ if ((p->completed & (~0x00000001)) != 0) {
+ debug(0, _(L"Job '%ls', process '%ls' has inconsistent state \'completed\'=%d"),
+ j->command_wcstr(), p->argv0(), p->completed);
sanity_lose();
}
- p=p->next;
+ p = p->next;
}
-
}
}
-void proc_push_interactive(int value)
-{
+void proc_push_interactive(int value) {
ASSERT_IS_MAIN_THREAD();
int old = is_interactive;
interactive_stack.push_back(is_interactive);
is_interactive = value;
- if (old != value)
- signal_set_handlers();
+ if (old != value) signal_set_handlers();
}
-void proc_pop_interactive()
-{
+void proc_pop_interactive() {
ASSERT_IS_MAIN_THREAD();
int old = is_interactive;
- is_interactive= interactive_stack.back();
+ is_interactive = interactive_stack.back();
interactive_stack.pop_back();
- if (is_interactive != old)
- signal_set_handlers();
+ if (is_interactive != old) signal_set_handlers();
}
diff --git a/src/proc.h b/src/proc.h
index 7fcbe6da..3d24405c 100644
--- a/src/proc.h
+++ b/src/proc.h
@@ -1,254 +1,170 @@
-/** \file proc.h
-
- 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.
-
-*/
+// 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.
#ifndef FISH_PROC_H
#define FISH_PROC_H
#include "config.h" // IWYU pragma: keep
-#include <signal.h>
-#include <list>
#include <assert.h>
+#include <signal.h>
+#include <stdbool.h>
#include <stddef.h>
+#include <sys/time.h> // IWYU pragma: keep
#include <termios.h>
#include <unistd.h>
-#include <stdbool.h>
-#include <sys/time.h> // IWYU pragma: keep
+#include <list>
-#include "io.h"
#include "common.h"
+#include "io.h"
#include "parse_tree.h"
-/**
- The status code use when a command was not found
-*/
+/// The status code use when a command was not found.
#define STATUS_UNKNOWN_COMMAND 127
-/**
- The status code use when an unknown error occured during execution of a command
-*/
+/// The status code use when an unknown error occured during execution of a command.
#define STATUS_NOT_EXECUTABLE 126
-/**
- The status code use when an unknown error occured during execution of a command
-*/
+/// The status code use when an unknown error occured during execution of a command.
#define STATUS_EXEC_FAIL 125
-/**
- The status code use when a wildcard had no matches
-*/
+/// The status code use when a wildcard had no matches.
#define STATUS_UNMATCHED_WILDCARD 124
-/**
- The status code used for normal exit in a builtin
-*/
+/// The status code used for normal exit in a builtin.
#define STATUS_BUILTIN_OK 0
-/**
- The status code used for erroneous argument combinations in a builtin
-*/
+/// The status code used for erroneous argument combinations in a builtin.
#define STATUS_BUILTIN_ERROR 1
-/**
- Types of processes
-*/
-enum process_type_t
-{
- /**
- A regular external command
- */
+/// Types of processes.
+enum process_type_t {
+ /// A regular external command.
EXTERNAL,
- /**
- A builtin command
- */
+ /// A builtin command.
INTERNAL_BUILTIN,
- /**
- A shellscript function
- */
+ /// A shellscript function.
INTERNAL_FUNCTION,
-
- /** A block of commands, represented as a node */
+ /// A block of commands, represented as a node.
INTERNAL_BLOCK_NODE,
-
- /**
- The exec builtin
- */
+ /// The exec builtin.
INTERNAL_EXEC
};
-enum
-{
+enum {
JOB_CONTROL_ALL,
JOB_CONTROL_INTERACTIVE,
JOB_CONTROL_NONE,
-}
-;
-
-/**
- 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:
+};
+/// 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:
null_terminated_array_t<wchar_t> argv_array;
io_chain_t process_io_chain;
- /* No copying */
+ // No copying.
process_t(const process_t &rhs);
void operator=(const process_t &rhs);
-public:
-
+ public:
process_t();
~process_t();
-
- /**
- Type of process. Can be one of \c EXTERNAL, \c
- INTERNAL_BUILTIN, \c INTERNAL_FUNCTION, \c INTERNAL_EXEC
- */
+ /// Type of process. Can be one of \c EXTERNAL, \c INTERNAL_BUILTIN, \c INTERNAL_FUNCTION, \c
+ /// INTERNAL_EXEC.
enum process_type_t type;
- /* For internal block processes only, the node offset of the block */
+ /// For internal block processes only, the node offset of the block.
node_offset_t internal_block_node;
- /** Sets argv */
- void set_argv(const wcstring_list_t &argv)
- {
- argv_array.set(argv);
- }
+ /// Sets argv.
+ void set_argv(const wcstring_list_t &argv) { argv_array.set(argv); }
- /** 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;
- }
+ /// 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; }
- /** Returns argv[idx] */
- const wchar_t *argv(size_t idx) const
- {
- const wchar_t * const *argv = argv_array.get();
+ /// Returns argv[idx].
+ const wchar_t *argv(size_t idx) const {
+ const wchar_t *const *argv = argv_array.get();
assert(argv != NULL);
return argv[idx];
}
- /** Returns argv[0], or NULL */
- const wchar_t *argv0() const
- {
- const wchar_t * const *argv = argv_array.get();
+ /// Returns argv[0], or NULL.
+ const wchar_t *argv0() const {
+ const wchar_t *const *argv = argv_array.get();
return argv ? argv[0] : NULL;
}
- /* IO chain getter and setter */
- const io_chain_t &io_chain() const
- {
- return process_io_chain;
- }
+ /// IO chain getter and setter.
+ const io_chain_t &io_chain() const { return process_io_chain; }
- void set_io_chain(const io_chain_t &chain)
- {
- this->process_io_chain = chain;
- }
+ void set_io_chain(const io_chain_t &chain) { this->process_io_chain = chain; }
- /** actual command to pass to exec in case of EXTERNAL or INTERNAL_EXEC. */
+ /// Actual command to pass to exec in case of EXTERNAL or INTERNAL_EXEC.
wcstring actual_cmd;
-
- /** process ID */
+ /// Process ID
pid_t pid;
-
- /** File descriptor that pipe output should bind to */
+ /// File descriptor that pipe output should bind to.
int pipe_write_fd;
-
- /** File descriptor that the _next_ process pipe input should bind to */
+ /// File descriptor that the _next_ process pipe input should bind to.
int pipe_read_fd;
-
- /** true if process has completed */
+ /// True if process has completed.
volatile int completed;
-
- /** true if process has stopped */
+ /// True if process has stopped.
volatile int stopped;
-
- /** reported status value */
+ /// Reported status value.
volatile int status;
-
- /** Special flag to tell the evaluation function for count to print the help information */
+ /// Special flag to tell the evaluation function for count to print the help information.
int count_help_magic;
-
- /** Next process in pipeline. We own this and we are responsible for deleting it. */
+ /// Next process in pipeline. We own this and we are responsible for deleting it.
process_t *next;
#ifdef HAVE__PROC_SELF_STAT
- /** Last time of cpu time check */
+ /// Last time of cpu time check.
struct timeval last_time;
- /** Number of jiffies spent in process at last cpu time check */
+ /// Number of jiffies spent in process at last cpu time check.
unsigned long last_jiffies;
#endif
};
-/**
- Constants for the flag variable in the job struct
-*/
-enum
-{
- /** Whether the user has been told about stopped job */
+/// Constants for the flag variable in the job struct.
+enum {
+ /// Whether the user has been told about stopped job.
JOB_NOTIFIED = 1 << 0,
-
- /** Whether this job is in the foreground */
+ /// Whether this job is in the foreground.
JOB_FOREGROUND = 1 << 1,
-
- /**
- Whether the specified job is completely constructed,
- i.e. completely parsed, and every process in the job has been
- forked, etc.
- */
+ /// Whether the specified job is completely constructed, i.e. completely parsed, and every
+ /// process in the job has been forked, etc.
JOB_CONSTRUCTED = 1 << 2,
-
- /** Whether the specified job is a part of a subshell, event handler or some other form of special job that should not be reported */
+ /// Whether the specified job is a part of a subshell, event handler or some other form of
+ /// special job that should not be reported.
JOB_SKIP_NOTIFICATION = 1 << 3,
-
- /** Whether the exit status should be negated. This flag can only be set by the not builtin. */
+ /// Whether the exit status should be negated. This flag can only be set by the not builtin.
JOB_NEGATE = 1 << 4,
-
- /** Whether the job is under job control */
+ /// Whether the job is under job control.
JOB_CONTROL = 1 << 5,
-
- /** Whether the job wants to own the terminal when in the foreground */
+ /// Whether the job wants to own the terminal when in the foreground.
JOB_TERMINAL = 1 << 6
};
@@ -256,147 +172,93 @@ typedef int job_id_t;
job_id_t acquire_job_id(void);
void release_job_id(job_id_t jobid);
-/**
- 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.
- */
+/// 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.
wcstring command_str;
- /* The IO chain associated with the block */
+ // The IO chain associated with the block.
const io_chain_t block_io;
- /* No copying */
+ // No copying.
job_t(const job_t &rhs);
void operator=(const job_t &);
-public:
-
+ public:
job_t(job_id_t jobid, const io_chain_t &bio);
~job_t();
- /** Returns whether the command is empty. */
- bool command_is_empty() const
- {
- return command_str.empty();
- }
+ /// Returns whether the command is empty.
+ bool command_is_empty() const { return command_str.empty(); }
- /** Returns the command as a wchar_t *. */
- const wchar_t *command_wcstr() const
- {
- return command_str.c_str();
- }
+ /// Returns the command as a wchar_t *. */
+ const wchar_t *command_wcstr() const { return command_str.c_str(); }
- /** Returns the command */
- const wcstring &command() const
- {
- return command_str;
- }
+ /// Returns the command.
+ const wcstring &command() const { return command_str; }
- /** Sets the command */
- void set_command(const wcstring &cmd)
- {
- command_str = cmd;
- }
+ /// Sets the command.
+ void set_command(const wcstring &cmd) { command_str = cmd; }
- /**
- A linked list of all the processes in this job. We are responsible for deleting this when we are deallocated.
- */
+ /// A linked list of all the processes in this job. We are responsible for deleting this when we
+ /// are deallocated.
process_t *first_process;
-
- /**
- process group ID for the process group that this job is
- running in.
- */
+ /// Process group ID for the process group that this job is running in.
pid_t pgid;
-
- /**
- 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.
- */
+ /// 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.
struct termios tmodes;
-
- /**
- 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.
- */
+ /// 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.
const job_id_t job_id;
-
- /**
- Bitset containing information about the job. A combination of the JOB_* constants.
- */
+ /// Bitset containing information about the job. A combination of the JOB_* constants.
unsigned int flags;
- /* 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;
- }
+ /// 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; }
- /* Fetch all the IO redirections associated with the job */
+ /// Fetch all the IO redirections associated with the job.
io_chain_t all_io_redirections() const;
};
-/**
- Whether we are running a subshell command
-*/
+/// Whether we are running a subshell command.
extern int is_subshell;
-/**
- Whether we are running a block of commands
-*/
+/// Whether we are running a block of commands.
extern int is_block;
-/**
- Whether we are reading from the keyboard right now
-*/
+/// Whether we are reading from the keyboard right now.
int get_is_interactive(void);
-/**
- Whether this shell is attached to the keyboard at all
-*/
+/// Whether this shell is attached to the keyboard at all.
extern int is_interactive_session;
-/**
- Whether we are a login shell
-*/
+/// Whether we are a login shell.
extern int is_login;
-/**
- Whether we are running an event handler
-*/
+/// Whether we are running an event handler.
extern int is_event;
-
typedef std::list<job_t *> job_list_t;
bool job_list_is_empty(void);
-/** 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;
+/// 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;
job_list_t::iterator current, end;
-public:
+ public:
void reset(void);
- job_t *next()
- {
+ job_t *next() {
job_t *job = NULL;
- if (current != end)
- {
+ if (current != end) {
job = *current;
++current;
}
@@ -408,177 +270,111 @@ public:
size_t count() const;
};
-/**
- 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.
-*/
+/// 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.
bool get_proc_had_barrier();
void set_proc_had_barrier(bool flag);
-/**
- Pid of last process started in the background
-*/
+/// Pid of last process started in the background.
extern pid_t proc_last_bg_pid;
-/**
- The current job control mode.
-
- Must be one of JOB_CONTROL_ALL, JOB_CONTROL_INTERACTIVE and JOB_CONTROL_NONE
-*/
+/// The current job control mode.
+///
+/// Must be one of JOB_CONTROL_ALL, JOB_CONTROL_INTERACTIVE and JOB_CONTROL_NONE.
extern int job_control_mode;
-/**
- 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.
- */
+/// 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.
extern int no_exec;
-/**
- Add the specified flag to the bitset of flags for the specified job
- */
+/// Add the specified flag to the bitset of flags for the specified job.
void job_set_flag(job_t *j, unsigned int flag, int set);
-/**
- Returns one if the specified flag is set in the specified job, 0 otherwise.
- */
+/// Returns one if the specified flag is set in the specified job, 0 otherwise.
int job_get_flag(const job_t *j, unsigned int flag);
-/**
- Sets the status of the last process to exit
-*/
+/// Sets the status of the last process to exit.
void proc_set_last_status(int s);
-/**
- Returns the status of the last process to exit
-*/
+/// Returns the status of the last process to exit.
int proc_get_last_status();
-/**
- Remove the specified job
-*/
-void job_free(job_t* j);
+/// Remove the specified job.
+void job_free(job_t *j);
-/**
- Promotes a job to the front of the job list.
-*/
+/// Promotes a job to the front of the job list.
void job_promote(job_t *job);
-/**
- Return the job with the specified job id.
- If id is 0 or less, return the last job used.
-*/
+/// Return the job with the specified job id. If id is 0 or less, return the last job used.
job_t *job_get(job_id_t id);
-/**
- Return the job with the specified pid.
-*/
+/// Return the job with the specified pid.
job_t *job_get_from_pid(int pid);
-/**
- Tests if the job is stopped
-*/
+/// Tests if the job is stopped.
int job_is_stopped(const job_t *j);
-/**
- Tests if the job has completed, i.e. if the last process of the pipeline has ended.
-*/
+/// Tests if the job has completed, i.e. if the last process of the pipeline has ended.
bool job_is_completed(const job_t *j);
-/**
- 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
-*/
+/// 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
void job_continue(job_t *j, bool cont);
-/**
- 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
-*/
+/// 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
int job_reap(bool interactive);
-/**
- Signal handler for SIGCHLD. Mark any processes with relevant
- information.
-*/
+/// Signal handler for SIGCHLD. Mark any processes with relevant information.
void job_handle_signal(int signal, siginfo_t *info, void *con);
-/**
- Send the specified signal to all processes in the specified job.
-*/
+/// Send the specified signal to all processes in the specified job.
int job_signal(job_t *j, int signal);
-/**
- Mark a process as failed to execute (and therefore completed)
-*/
+/// Mark a process as failed to execute (and therefore completed).
void job_mark_process_as_failed(const job_t *job, process_t *p);
#ifdef HAVE__PROC_SELF_STAT
-/**
- 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.
-*/
+/// 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.
unsigned long proc_get_jiffies(process_t *p);
-/**
- Update process time usage for all processes by calling the
- proc_get_jiffies function for every process of every job.
-*/
+/// Update process time usage for all processes by calling the proc_get_jiffies function for every
+/// process of every job.
void proc_update_jiffies();
-
#endif
-/**
- 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.
-*/
+/// 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.
void proc_sanity_check();
-/**
- Send a process/job exit event notification. This function is a
- convenience wrapper around event_fire().
-*/
+/// Send a process/job exit event notification. This function is a convenience wrapper around
+/// event_fire().
void proc_fire_event(const wchar_t *msg, int type, pid_t pid, int status);
-/**
- Initializations
-*/
+/// Initializations.
void proc_init();
-/**
- Clean up before exiting
-*/
+/// Clean up before exiting.
void proc_destroy();
-/**
- Set new value for is_interactive flag, saving previous value. If
- needed, update signal handlers.
-*/
+/// Set new value for is_interactive flag, saving previous value. If needed, update signal handlers.
void proc_push_interactive(int value);
-/**
- Set is_interactive flag to the previous value. If needed, update
- signal handlers.
-*/
+/// Set is_interactive flag to the previous value. If needed, update signal handlers.
void proc_pop_interactive();
-/**
- Format an exit status code as returned by e.g. wait into a fish exit code number as accepted by proc_set_last_status.
- */
+/// Format an exit status code as returned by e.g. wait into a fish exit code number as accepted by
+/// proc_set_last_status.
int proc_format_status(int status);
#endif