2016-05-03 04:41:17 +00:00
|
|
|
// 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.
|
2016-04-21 06:00:54 +00:00
|
|
|
// IWYU pragma: no_include <__bit_reference>
|
2005-09-20 13:26:39 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <signal.h>
|
2005-09-20 13:26:39 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <termios.h>
|
2016-05-03 04:41:17 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <wchar.h>
|
2015-07-25 15:14:25 +00:00
|
|
|
#include <wctype.h>
|
2017-02-14 04:37:27 +00:00
|
|
|
|
2006-01-19 12:22:07 +00:00
|
|
|
#if HAVE_TERM_H
|
2005-09-20 13:26:39 +00:00
|
|
|
#include <term.h>
|
2006-01-19 12:22:07 +00:00
|
|
|
#elif HAVE_NCURSES_TERM_H
|
|
|
|
#include <ncurses/term.h>
|
|
|
|
#endif
|
2006-07-30 20:26:59 +00:00
|
|
|
#ifdef HAVE_SIGINFO_H
|
|
|
|
#include <siginfo.h>
|
|
|
|
#endif
|
2006-08-09 22:26:05 +00:00
|
|
|
#ifdef HAVE_SYS_SELECT_H
|
|
|
|
#include <sys/select.h>
|
|
|
|
#endif
|
2016-04-21 06:00:54 +00:00
|
|
|
#include <sys/time.h> // IWYU pragma: keep
|
2016-05-03 04:41:17 +00:00
|
|
|
#include <sys/types.h>
|
2017-02-14 04:37:27 +00:00
|
|
|
|
2016-04-21 06:00:54 +00:00
|
|
|
#include <algorithm> // IWYU pragma: keep
|
2017-02-14 04:37:27 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <vector>
|
2006-08-09 22:26:05 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
#include "common.h"
|
2005-10-11 19:23:43 +00:00
|
|
|
#include "event.h"
|
2016-05-03 04:41:17 +00:00
|
|
|
#include "fallback.h" // IWYU pragma: keep
|
2016-04-21 06:00:54 +00:00
|
|
|
#include "io.h"
|
2016-05-03 04:41:17 +00:00
|
|
|
#include "output.h"
|
2016-04-21 06:00:54 +00:00
|
|
|
#include "parse_tree.h"
|
2016-05-03 04:41:17 +00:00
|
|
|
#include "parser.h"
|
|
|
|
#include "proc.h"
|
|
|
|
#include "reader.h"
|
|
|
|
#include "sanity.h"
|
|
|
|
#include "signal.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "wutil.h" // IWYU pragma: keep
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Size of buffer for reading buffered output.
|
2005-09-20 13:26:39 +00:00
|
|
|
#define BUFFER_SIZE 4096
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Status of last process to exit.
|
|
|
|
static int last_status = 0;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
bool job_list_is_empty(void) {
|
2012-02-28 02:43:24 +00:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
|
|
|
return parser_t::principal_parser().job_list().empty();
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
void job_iterator_t::reset() {
|
2012-02-28 02:43:24 +00:00
|
|
|
this->current = job_list->begin();
|
|
|
|
this->end = job_list->end();
|
|
|
|
}
|
2012-01-30 00:36:21 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
job_iterator_t::job_iterator_t(job_list_t &jobs) : job_list(&jobs) { this->reset(); }
|
2012-01-30 00:36:21 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
job_iterator_t::job_iterator_t() : job_list(&parser_t::principal_parser().job_list()) {
|
2013-11-29 21:31:18 +00:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
2012-02-28 02:43:24 +00:00
|
|
|
this->reset();
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
size_t job_iterator_t::count() const { return this->job_list->size(); }
|
2014-11-03 18:56:16 +00:00
|
|
|
|
2016-04-04 02:02:46 +00:00
|
|
|
#if 0
|
|
|
|
// This isn't used so the lint tools were complaining about its presence. I'm keeping it in the
|
|
|
|
// source because it could be useful for debugging. However, it would probably be better to add a
|
|
|
|
// verbose or debug option to the builtin `jobs` command.
|
2013-01-30 10:22:38 +00:00
|
|
|
void print_jobs(void)
|
|
|
|
{
|
|
|
|
job_iterator_t jobs;
|
|
|
|
job_t *j;
|
2016-04-04 02:02:46 +00:00
|
|
|
while (j = jobs.next()) {
|
2017-01-14 04:34:15 +00:00
|
|
|
fwprintf(stdout, L"%p -> %ls -> (foreground %d, complete %d, stopped %d, constructed %d)\n",
|
2017-01-26 23:06:58 +00:00
|
|
|
j, j->command_wcstr(), j->get_flag(JOB_FOREGROUND), job_is_completed(j),
|
|
|
|
job_is_stopped(j), j->get_flag(JOB_CONSTRUCTED));
|
2013-01-30 10:22:38 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-04 02:02:46 +00:00
|
|
|
#endif
|
2012-02-28 02:43:24 +00:00
|
|
|
|
2017-06-20 04:05:34 +00:00
|
|
|
bool is_interactive_session = false;
|
|
|
|
bool is_subshell = false;
|
|
|
|
bool is_block = false;
|
|
|
|
bool is_breakpoint = false;
|
|
|
|
bool is_login = false;
|
|
|
|
int is_event = false;
|
2005-09-20 13:26:39 +00:00
|
|
|
pid_t proc_last_bg_pid = 0;
|
2006-01-30 17:54:26 +00:00
|
|
|
int job_control_mode = JOB_CONTROL_INTERACTIVE;
|
2016-05-03 04:41:17 +00:00
|
|
|
int no_exec = 0;
|
2006-03-18 01:04:59 +00:00
|
|
|
|
2012-02-26 02:54:49 +00:00
|
|
|
static int is_interactive = -1;
|
|
|
|
|
2012-03-31 22:33:34 +00:00
|
|
|
static bool proc_had_barrier = false;
|
|
|
|
|
2016-05-15 03:35:54 +00:00
|
|
|
bool shell_is_interactive(void) {
|
2012-02-26 02:54:49 +00:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
2016-05-15 03:35:54 +00:00
|
|
|
// is_interactive is statically initialized to -1. Ensure it has been dynamically set
|
|
|
|
// before we're called.
|
|
|
|
assert(is_interactive != -1);
|
2014-01-01 23:29:56 +00:00
|
|
|
return is_interactive > 0;
|
2012-02-26 02:54:49 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
bool get_proc_had_barrier() {
|
2012-03-31 22:33:34 +00:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
|
|
|
return proc_had_barrier;
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
void set_proc_had_barrier(bool flag) {
|
2012-03-31 22:33:34 +00:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
|
|
|
proc_had_barrier = flag;
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// The event variable used to send all process event.
|
2012-02-09 03:02:25 +00:00
|
|
|
static event_t event(0);
|
2005-12-11 22:21:01 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// A stack containing the values of is_interactive. Used by proc_push_interactive and
|
|
|
|
/// proc_pop_interactive.
|
2012-07-17 19:47:01 +00:00
|
|
|
static std::vector<int> interactive_stack;
|
2005-10-14 11:40:33 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
void proc_init() { proc_push_interactive(0); }
|
2005-10-14 11:40:33 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Remove job from list of jobs.
|
|
|
|
static int job_remove(job_t *j) {
|
2012-02-28 02:43:24 +00:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
|
|
|
return parser_t::principal_parser().job_remove(j);
|
2012-01-30 00:36:21 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
void job_promote(job_t *job) {
|
2012-02-28 02:43:24 +00:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
|
|
|
parser_t::principal_parser().job_promote(job);
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
void proc_destroy() {
|
2012-02-28 02:43:24 +00:00
|
|
|
job_list_t &jobs = parser_t::principal_parser().job_list();
|
2016-05-03 04:41:17 +00:00
|
|
|
while (!jobs.empty()) {
|
2017-01-26 22:47:32 +00:00
|
|
|
job_t *job = jobs.front().get();
|
2012-11-19 00:30:30 +00:00
|
|
|
debug(2, L"freeing leaked job %ls", job->command_wcstr());
|
2017-01-26 22:47:32 +00:00
|
|
|
job_remove(job);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
void proc_set_last_status(int s) {
|
2013-04-07 21:38:57 +00:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
2012-11-19 00:30:30 +00:00
|
|
|
last_status = s;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
int proc_get_last_status() { return last_status; }
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
// 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.
|
2017-01-29 21:03:54 +00:00
|
|
|
static owning_lock<std::vector<bool>> locked_consumed_job_ids;
|
2012-02-28 02:43:24 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
job_id_t acquire_job_id(void) {
|
2017-01-29 21:03:54 +00:00
|
|
|
auto locker = locked_consumed_job_ids.acquire();
|
|
|
|
std::vector<bool> &consumed_job_ids = locker.value;
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
// 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.
|
2012-02-28 02:43:24 +00:00
|
|
|
*slot = true;
|
2012-07-29 00:49:46 +00:00
|
|
|
return (job_id_t)(slot - consumed_job_ids.begin() + 1);
|
2012-02-28 02:43:24 +00:00
|
|
|
}
|
2016-05-04 22:19:47 +00:00
|
|
|
|
|
|
|
// 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();
|
2012-02-28 02:43:24 +00:00
|
|
|
}
|
2006-03-10 13:38:09 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
void release_job_id(job_id_t jid) {
|
2012-02-28 02:43:24 +00:00
|
|
|
assert(jid > 0);
|
2017-01-29 21:03:54 +00:00
|
|
|
auto locker = locked_consumed_job_ids.acquire();
|
|
|
|
std::vector<bool> &consumed_job_ids = locker.value;
|
2012-02-28 02:43:24 +00:00
|
|
|
size_t slot = (size_t)(jid - 1), count = consumed_job_ids.size();
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
// Make sure this slot is within our vector and is currently set to consumed.
|
2012-02-28 02:43:24 +00:00
|
|
|
assert(slot < count);
|
|
|
|
assert(consumed_job_ids.at(slot) == true);
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
// Clear it and then resize the vector to eliminate unused trailing job IDs.
|
2012-02-28 02:43:24 +00:00
|
|
|
consumed_job_ids.at(slot) = false;
|
2016-05-03 04:41:17 +00:00
|
|
|
while (count--) {
|
|
|
|
if (consumed_job_ids.at(count)) break;
|
2012-02-28 02:43:24 +00:00
|
|
|
}
|
|
|
|
consumed_job_ids.resize(count + 1);
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
job_t *job_get(job_id_t id) {
|
2012-02-28 02:43:24 +00:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
|
|
|
return parser_t::principal_parser().job_get(id);
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
job_t *job_get_from_pid(int pid) {
|
2012-02-28 02:43:24 +00:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
|
|
|
return parser_t::principal_parser().job_get_from_pid(pid);
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// 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) {
|
2017-01-23 17:28:34 +00:00
|
|
|
for (const process_ptr_t &p : j->processes) {
|
2016-05-03 04:41:17 +00:00
|
|
|
if (!p->completed && !p->stopped) {
|
2012-11-19 00:30:30 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// 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) {
|
2017-01-27 04:00:43 +00:00
|
|
|
assert(!j->processes.empty());
|
2013-06-16 09:53:14 +00:00
|
|
|
bool result = true;
|
2017-01-23 17:28:34 +00:00
|
|
|
for (const process_ptr_t &p : j->processes) {
|
2016-05-03 04:41:17 +00:00
|
|
|
if (!p->completed) {
|
2013-06-16 09:53:14 +00:00
|
|
|
result = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2017-01-26 23:06:58 +00:00
|
|
|
void job_t::set_flag(job_flag_t flag, bool set) {
|
2016-05-03 04:41:17 +00:00
|
|
|
if (set) {
|
2017-01-26 23:06:58 +00:00
|
|
|
this->flags |= flag;
|
2016-05-03 04:41:17 +00:00
|
|
|
} else {
|
2017-01-26 23:06:58 +00:00
|
|
|
this->flags &= ~flag;
|
2013-01-30 10:22:38 +00:00
|
|
|
}
|
2006-10-25 20:47:59 +00:00
|
|
|
}
|
|
|
|
|
2017-05-05 05:42:42 +00:00
|
|
|
bool job_t::get_flag(job_flag_t flag) const { return (this->flags & flag) == flag; }
|
2006-10-25 20:47:59 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
int job_signal(job_t *j, int signal) {
|
2017-04-23 14:51:01 +00:00
|
|
|
pid_t my_pgid = getpgrp();
|
2012-11-19 00:30:30 +00:00
|
|
|
int res = 0;
|
|
|
|
|
2017-04-23 14:51:01 +00:00
|
|
|
if (j->pgid != my_pgid) {
|
2016-10-09 21:38:26 +00:00
|
|
|
res = killpg(j->pgid, signal);
|
2016-05-03 04:41:17 +00:00
|
|
|
} else {
|
2017-01-23 17:28:34 +00:00
|
|
|
for (const process_ptr_t &p : j->processes) {
|
2016-10-22 18:21:13 +00:00
|
|
|
if (!p->completed && p->pid && kill(p->pid, signal)) {
|
|
|
|
res = -1;
|
|
|
|
break;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
2006-11-20 13:12:24 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Store the status of the process pid that was returned by waitpid.
|
2016-10-09 21:38:26 +00:00
|
|
|
static void mark_process_status(process_t *p, int status) {
|
2016-05-03 04:41:17 +00:00
|
|
|
// 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")) );
|
2012-11-19 00:30:30 +00:00
|
|
|
p->status = status;
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
if (WIFSTOPPED(status)) {
|
2012-11-19 00:30:30 +00:00
|
|
|
p->stopped = 1;
|
2016-05-03 04:41:17 +00:00
|
|
|
} else if (WIFSIGNALED(status) || WIFEXITED(status)) {
|
2012-11-19 00:30:30 +00:00
|
|
|
p->completed = 1;
|
2016-05-03 04:41:17 +00:00
|
|
|
} else {
|
|
|
|
// This should never be reached.
|
2012-11-19 00:30:30 +00:00
|
|
|
p->completed = 1;
|
2017-01-03 05:11:53 +00:00
|
|
|
debug(1, "Process %ld exited abnormally", (long)p->pid);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2017-01-23 17:28:34 +00:00
|
|
|
void job_mark_process_as_failed(job_t *job, const process_t *failed_proc) {
|
2016-05-03 04:41:17 +00:00
|
|
|
// The given process failed to even lift off (e.g. posix_spawn failed) and so doesn't have a
|
2017-01-23 17:28:34 +00:00
|
|
|
// valid pid. Mark it and everything after it as dead.
|
|
|
|
bool found = false;
|
|
|
|
for (process_ptr_t &p : job->processes) {
|
|
|
|
found = found || (p.get() == failed_proc);
|
|
|
|
if (found) {
|
|
|
|
p->completed = true;
|
|
|
|
}
|
2013-01-30 10:22:38 +00:00
|
|
|
}
|
2012-10-29 08:45:51 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// 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) {
|
2017-01-23 17:28:34 +00:00
|
|
|
job_t *j = NULL;
|
2017-01-26 21:39:19 +00:00
|
|
|
const process_t *found_proc = NULL;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-01-30 00:36:21 +00:00
|
|
|
job_iterator_t jobs;
|
2016-05-03 04:41:17 +00:00
|
|
|
while (!found_proc && (j = jobs.next())) {
|
2017-01-23 17:28:34 +00:00
|
|
|
process_t *prev = NULL;
|
|
|
|
for (process_ptr_t &p : j->processes) {
|
2016-05-03 04:41:17 +00:00
|
|
|
if (pid == p->pid) {
|
2017-01-23 17:28:34 +00:00
|
|
|
mark_process_status(p.get(), status);
|
2017-01-26 21:39:19 +00:00
|
|
|
found_proc = p.get();
|
2012-11-19 00:30:30 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-01-23 17:28:34 +00:00
|
|
|
prev = p.get();
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-04 02:42:25 +00:00
|
|
|
// If the child process was not killed by a signal or other than SIGINT or SIGQUIT we're done.
|
|
|
|
if (!WIFSIGNALED(status) || (WTERMSIG(status) != SIGINT && WTERMSIG(status) != SIGQUIT)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_interactive_session) {
|
|
|
|
// In an interactive session, tell the principal parser to skip all blocks we're executing
|
|
|
|
// so control-C returns control to the user.
|
2017-01-26 21:39:19 +00:00
|
|
|
if (found_proc) parser_t::skip_all_blocks();
|
2016-11-04 02:42:25 +00:00
|
|
|
} else {
|
|
|
|
// Deliver the SIGINT or SIGQUIT signal to ourself since we're not interactive.
|
|
|
|
struct sigaction act;
|
|
|
|
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));
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
|
2016-11-02 03:42:02 +00:00
|
|
|
#if 0
|
2016-11-04 02:42:25 +00:00
|
|
|
// TODO: Decide whether to eliminate this block or have it emit a warning message.
|
|
|
|
// WARNING: See the special short-circuit logic above vis-a-vis signals.
|
2016-05-03 04:41:17 +00:00
|
|
|
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...
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2016-11-02 03:42:02 +00:00
|
|
|
#endif
|
2012-11-19 00:30:30 +00:00
|
|
|
return;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
process_t::process_t()
|
2017-01-23 17:28:34 +00:00
|
|
|
: is_first_in_job(),
|
|
|
|
is_last_in_job(),
|
|
|
|
type(), // gets set later
|
2016-05-03 04:41:17 +00:00
|
|
|
internal_block_node(NODE_OFFSET_INVALID),
|
|
|
|
pid(0),
|
|
|
|
pipe_write_fd(0),
|
|
|
|
pipe_read_fd(0),
|
|
|
|
completed(0),
|
|
|
|
stopped(0),
|
|
|
|
status(0),
|
2017-01-23 17:28:34 +00:00
|
|
|
count_help_magic(0)
|
2012-08-15 07:57:56 +00:00
|
|
|
#ifdef HAVE__PROC_SELF_STAT
|
2016-05-03 04:41:17 +00:00
|
|
|
,
|
|
|
|
last_time(),
|
|
|
|
last_jiffies(0)
|
2012-08-15 07:57:56 +00:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2017-04-23 14:56:33 +00:00
|
|
|
/// The constructor sets the pgid to -2 as a sentinel value
|
|
|
|
/// 0 should not be used; although it is not a valid PGID in userspace,
|
|
|
|
/// the Linux kernel will use it for kernel processes.
|
|
|
|
/// -1 should not be used; it is a possible return value of the getpgid()
|
|
|
|
/// function
|
2016-05-03 04:41:17 +00:00
|
|
|
job_t::job_t(job_id_t jobid, const io_chain_t &bio)
|
2017-04-23 14:56:33 +00:00
|
|
|
: block_io(bio), pgid(-2), tmodes(), job_id(jobid), flags(0) {}
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2017-01-27 04:00:43 +00:00
|
|
|
job_t::~job_t() { release_job_id(job_id); }
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Return all the IO redirections. Start with the block IO, then walk over the processes.
|
|
|
|
io_chain_t job_t::all_io_redirections() const {
|
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-19 23:16:41 +00:00
|
|
|
io_chain_t result = this->block_io;
|
2017-01-23 17:28:34 +00:00
|
|
|
for (const process_ptr_t &p : this->processes) {
|
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-19 23:16:41 +00:00
|
|
|
result.append(p->io_chain());
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-10-25 23:51:25 +00:00
|
|
|
typedef unsigned int process_generation_count_t;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// 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.
|
2016-11-03 04:54:57 +00:00
|
|
|
static volatile process_generation_count_t s_sigchld_generation_cnt = 0;
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// 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) {
|
2014-10-25 23:51:25 +00:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
2016-05-03 04:41:17 +00:00
|
|
|
|
|
|
|
// A static value tracking the SIGCHLD gen count at the time we last processed it. When this is
|
2016-11-03 04:54:57 +00:00
|
|
|
// different from s_sigchld_generation_cnt, it indicates there may be unreaped processes.
|
2016-05-03 04:41:17 +00:00
|
|
|
// 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.
|
2016-11-03 04:54:57 +00:00
|
|
|
static process_generation_count_t s_last_sigchld_generation_cnt = 0;
|
2016-05-03 04:41:17 +00:00
|
|
|
|
2014-10-25 23:51:25 +00:00
|
|
|
int processed_count = 0;
|
|
|
|
bool got_error = false;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
// 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.
|
2016-11-03 04:54:57 +00:00
|
|
|
const process_generation_count_t local_count = s_sigchld_generation_cnt;
|
2016-05-03 04:41:17 +00:00
|
|
|
|
|
|
|
// 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.
|
2016-11-03 04:54:57 +00:00
|
|
|
bool wants_waitpid = wants_await || local_count != s_last_sigchld_generation_cnt;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
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.
|
2014-10-25 23:51:25 +00:00
|
|
|
int options = WUNTRACED;
|
2016-05-03 04:41:17 +00:00
|
|
|
if (!(wants_await && processed_count == 0)) {
|
2014-10-25 23:51:25 +00:00
|
|
|
options |= WNOHANG;
|
2012-11-19 08:31:03 +00:00
|
|
|
}
|
2016-05-03 04:41:17 +00:00
|
|
|
|
2014-10-25 23:51:25 +00:00
|
|
|
int status = -1;
|
|
|
|
pid_t pid = waitpid(-1, &status, options);
|
2016-05-03 04:41:17 +00:00
|
|
|
if (pid > 0) {
|
|
|
|
// We got a valid pid.
|
2012-11-19 08:31:03 +00:00
|
|
|
handle_child_status(pid, status);
|
2014-10-25 23:51:25 +00:00
|
|
|
processed_count += 1;
|
2016-05-03 04:41:17 +00:00
|
|
|
} else if (pid == 0) {
|
|
|
|
// No ready-and-waiting children, we're done.
|
2014-10-25 23:51:25 +00:00
|
|
|
break;
|
2016-05-03 04:41:17 +00:00
|
|
|
} 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.
|
2014-10-25 23:51:25 +00:00
|
|
|
got_error = (errno != ECHILD);
|
2012-11-19 08:31:03 +00:00
|
|
|
break;
|
2014-10-25 23:51:25 +00:00
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
}
|
2014-10-25 23:51:25 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
if (got_error) {
|
2014-10-25 23:51:25 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2016-11-03 04:54:57 +00:00
|
|
|
s_last_sigchld_generation_cnt = local_count;
|
2016-05-04 22:19:47 +00:00
|
|
|
return processed_count;
|
2014-10-25 23:51:25 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// This is called from a signal handler. The signal is always SIGCHLD.
|
2016-10-09 21:38:26 +00:00
|
|
|
void job_handle_signal(int signal, siginfo_t *info, void *context) {
|
|
|
|
UNUSED(signal);
|
|
|
|
UNUSED(info);
|
|
|
|
UNUSED(context);
|
2016-05-03 04:41:17 +00:00
|
|
|
// This is the only place that this generation count is modified. It's OK if it overflows.
|
2016-11-03 04:54:57 +00:00
|
|
|
s_sigchld_generation_cnt += 1;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Given a command like "cat file", truncate it to a reasonable length.
|
|
|
|
static wcstring truncate_command(const wcstring &cmd) {
|
2014-11-03 18:56:16 +00:00
|
|
|
const size_t max_len = 32;
|
2016-05-03 04:41:17 +00:00
|
|
|
if (cmd.size() <= max_len) {
|
|
|
|
// No truncation necessary.
|
2014-11-03 18:56:16 +00:00
|
|
|
return cmd;
|
|
|
|
}
|
2016-05-03 04:41:17 +00:00
|
|
|
|
|
|
|
// Truncation required.
|
2014-11-03 18:56:16 +00:00
|
|
|
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;
|
2016-05-03 04:41:17 +00:00
|
|
|
// Eat trailing whitespace.
|
|
|
|
while (trunc_length > 0 && iswspace(cmd.at(trunc_length - 1))) {
|
2014-11-03 18:56:16 +00:00
|
|
|
trunc_length -= 1;
|
|
|
|
}
|
|
|
|
wcstring result = wcstring(cmd, 0, trunc_length);
|
2016-05-03 04:41:17 +00:00
|
|
|
// Append ellipsis.
|
|
|
|
if (ellipsis_is_unicode) {
|
2014-11-03 18:56:16 +00:00
|
|
|
result.push_back(ellipsis_char);
|
2016-05-03 04:41:17 +00:00
|
|
|
} else {
|
2014-11-03 18:56:16 +00:00
|
|
|
result.append(L"...");
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Format information about job status for the user to look at.
|
2017-05-27 22:41:22 +00:00
|
|
|
typedef enum { JOB_STOPPED, JOB_ENDED } job_status_t;
|
|
|
|
static void format_job_info(const job_t *j, job_status_t status) {
|
|
|
|
const wchar_t *msg = L"Job %d, '%ls' has ended"; // this is the most common status msg
|
|
|
|
if (status == JOB_STOPPED) msg = L"Job %d, '%ls' has stopped";
|
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
fwprintf(stdout, L"\r");
|
2017-05-27 22:41:22 +00:00
|
|
|
fwprintf(stdout, _(msg), j->job_id, truncate_command(j->command()).c_str());
|
2012-11-19 00:30:30 +00:00
|
|
|
fflush(stdout);
|
2017-05-27 22:41:22 +00:00
|
|
|
if (cur_term) {
|
2016-10-17 21:37:20 +00:00
|
|
|
tputs(clr_eol, 1, &writeb);
|
2017-05-27 22:41:22 +00:00
|
|
|
} else {
|
2016-12-24 00:37:00 +00:00
|
|
|
fwprintf(stdout, L"\e[K");
|
2017-05-27 22:41:22 +00:00
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
fwprintf(stdout, L"\n");
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
void proc_fire_event(const wchar_t *msg, int type, pid_t pid, int status) {
|
|
|
|
event.type = type;
|
2012-11-19 00:30:30 +00:00
|
|
|
event.param1.pid = pid;
|
|
|
|
|
2012-12-20 09:52:44 +00:00
|
|
|
event.arguments.push_back(msg);
|
|
|
|
event.arguments.push_back(to_string<int>(pid));
|
|
|
|
event.arguments.push_back(to_string<int>(status));
|
2012-11-19 00:30:30 +00:00
|
|
|
event_fire(&event);
|
2012-12-20 09:52:44 +00:00
|
|
|
event.arguments.resize(0);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2005-10-11 19:23:43 +00:00
|
|
|
|
2016-09-09 20:13:45 +00:00
|
|
|
int job_reap(bool allow_interactive) {
|
2012-02-16 08:24:27 +00:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
2012-11-19 00:30:30 +00:00
|
|
|
job_t *jnext;
|
2016-05-03 04:41:17 +00:00
|
|
|
int found = 0;
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
// job_reap may fire an event handler, we do not want to call ourselves recursively (to avoid
|
|
|
|
// infinite recursion).
|
2014-10-25 23:51:25 +00:00
|
|
|
static bool locked = false;
|
2016-05-03 04:41:17 +00:00
|
|
|
if (locked) {
|
2014-10-25 23:51:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
locked = true;
|
2016-05-03 04:41:17 +00:00
|
|
|
|
2016-09-09 20:13:45 +00:00
|
|
|
// this may be invoked in an exit handler, after the TERM has been torn down
|
|
|
|
// don't try to print in that case (#3222)
|
|
|
|
const bool interactive = allow_interactive && cur_term != NULL;
|
|
|
|
|
2014-10-25 23:51:25 +00:00
|
|
|
process_mark_finished_children(false);
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
// Preserve the exit status.
|
2013-12-31 22:37:37 +00:00
|
|
|
const int saved_status = proc_get_last_status();
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2012-01-30 00:36:21 +00:00
|
|
|
job_iterator_t jobs;
|
2014-11-03 18:56:16 +00:00
|
|
|
const size_t job_count = jobs.count();
|
2012-01-30 00:36:21 +00:00
|
|
|
jnext = jobs.next();
|
2016-05-03 04:41:17 +00:00
|
|
|
while (jnext) {
|
2012-01-30 00:36:21 +00:00
|
|
|
job_t *j = jnext;
|
|
|
|
jnext = jobs.next();
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
// 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.
|
2017-01-26 23:06:58 +00:00
|
|
|
if ((!j->get_flag(JOB_SKIP_NOTIFICATION)) && (!interactive) &&
|
|
|
|
(!j->get_flag(JOB_FOREGROUND))) {
|
2012-11-19 00:30:30 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-01-23 17:28:34 +00:00
|
|
|
for (const process_ptr_t &p : j->processes) {
|
2012-11-19 00:30:30 +00:00
|
|
|
int s;
|
2016-05-03 04:41:17 +00:00
|
|
|
if (!p->completed) continue;
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
if (!p->pid) continue;
|
2012-11-19 00:30:30 +00:00
|
|
|
|
|
|
|
s = p->status;
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
proc_fire_event(L"PROCESS_EXIT", EVENT_EXIT, p->pid,
|
|
|
|
(WIFSIGNALED(s) ? -1 : WEXITSTATUS(s)));
|
|
|
|
|
2016-10-30 21:37:54 +00:00
|
|
|
// Ignore signal SIGPIPE.We issue it ourselves to the pipe writer when the pipe reader
|
|
|
|
// dies.
|
|
|
|
if (!WIFSIGNALED(s) || WTERMSIG(s) == SIGPIPE) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle signals other than SIGPIPE.
|
2017-01-23 17:28:34 +00:00
|
|
|
int proc_is_job = (p->is_first_in_job && p->is_last_in_job);
|
2017-01-26 23:06:58 +00:00
|
|
|
if (proc_is_job) j->set_flag(JOB_NOTIFIED, true);
|
|
|
|
if (j->get_flag(JOB_SKIP_NOTIFICATION)) {
|
2016-10-30 21:37:54 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2017-01-26 23:06:58 +00:00
|
|
|
if (WTERMSIG(p->status) != SIGINT || !j->get_flag(JOB_FOREGROUND)) {
|
2016-10-30 21:37:54 +00:00
|
|
|
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 =
|
2017-02-09 21:32:30 +00:00
|
|
|
(job_count == 1) ? wcstring() : format_string(_(L"Job %d, "), j->job_id);
|
2016-10-30 21:37:54 +00:00
|
|
|
fwprintf(stdout, _(L"%ls: %ls\'%ls\' terminated by signal %ls (%ls)"),
|
2016-11-03 04:54:57 +00:00
|
|
|
program_name, job_number_desc.c_str(),
|
|
|
|
truncate_command(j->command()).c_str(), sig2wcs(WTERMSIG(p->status)),
|
|
|
|
signal_get_desc(WTERMSIG(p->status)));
|
2016-10-30 21:37:54 +00:00
|
|
|
} else {
|
|
|
|
const wcstring job_number_desc =
|
2016-11-03 04:54:57 +00:00
|
|
|
(job_count == 1) ? wcstring() : format_string(L"from job %d, ", j->job_id);
|
2017-05-02 04:44:30 +00:00
|
|
|
const wchar_t *fmt =
|
|
|
|
_(L"%ls: Process %d, \'%ls\' %ls\'%ls\' terminated by signal %ls (%ls)");
|
|
|
|
fwprintf(stdout, fmt, program_name, p->pid, p->argv0(), job_number_desc.c_str(),
|
2016-11-03 04:54:57 +00:00
|
|
|
truncate_command(j->command()).c_str(), sig2wcs(WTERMSIG(p->status)),
|
|
|
|
signal_get_desc(WTERMSIG(p->status)));
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2016-10-22 18:21:13 +00:00
|
|
|
|
2016-10-30 21:37:54 +00:00
|
|
|
if (cur_term != NULL) {
|
|
|
|
tputs(clr_eol, 1, &writeb);
|
|
|
|
} else {
|
2016-12-24 00:37:00 +00:00
|
|
|
fwprintf(stdout, L"\e[K"); // no term set up - do clr_eol manually
|
2016-10-30 21:37:54 +00:00
|
|
|
}
|
|
|
|
fwprintf(stdout, L"\n");
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2016-10-30 21:37:54 +00:00
|
|
|
found = 1;
|
|
|
|
p->status = 0; // clear status so it is not reported more than once
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
// 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)) {
|
2017-01-26 23:06:58 +00:00
|
|
|
if (!j->get_flag(JOB_FOREGROUND) && !j->get_flag(JOB_NOTIFIED) &&
|
|
|
|
!j->get_flag(JOB_SKIP_NOTIFICATION)) {
|
2017-05-27 22:41:22 +00:00
|
|
|
format_job_info(j, JOB_ENDED);
|
2016-05-03 04:41:17 +00:00
|
|
|
found = 1;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
proc_fire_event(L"JOB_EXIT", EVENT_EXIT, -j->pgid, 0);
|
|
|
|
proc_fire_event(L"JOB_EXIT", EVENT_JOB_ID, j->job_id, 0);
|
|
|
|
|
2017-01-26 22:47:32 +00:00
|
|
|
job_remove(j);
|
2017-01-26 23:06:58 +00:00
|
|
|
} else if (job_is_stopped(j) && !j->get_flag(JOB_NOTIFIED)) {
|
2016-05-03 04:41:17 +00:00
|
|
|
// Notify the user about newly stopped jobs.
|
2017-01-26 23:06:58 +00:00
|
|
|
if (!j->get_flag(JOB_SKIP_NOTIFICATION)) {
|
2017-05-27 22:41:22 +00:00
|
|
|
format_job_info(j, JOB_STOPPED);
|
2016-05-03 04:41:17 +00:00
|
|
|
found = 1;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2017-01-26 23:06:58 +00:00
|
|
|
j->set_flag(JOB_NOTIFIED, true);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
if (found) fflush(stdout);
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
// Restore the exit status.
|
2013-12-31 22:37:37 +00:00
|
|
|
proc_set_last_status(saved_status);
|
|
|
|
|
2014-10-25 23:51:25 +00:00
|
|
|
locked = false;
|
2012-11-19 00:30:30 +00:00
|
|
|
|
|
|
|
return found;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE__PROC_SELF_STAT
|
2006-01-20 14:27:21 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Maximum length of a /proc/[PID]/stat filename.
|
2006-01-20 14:27:21 +00:00
|
|
|
#define FN_SIZE 256
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Get the CPU time for the specified process.
|
|
|
|
unsigned long proc_get_jiffies(process_t *p) {
|
2016-05-29 05:28:26 +00:00
|
|
|
if (p->pid <= 0) return 0;
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2016-05-29 05:28:26 +00:00
|
|
|
wchar_t fn[FN_SIZE];
|
2012-11-19 00:30:30 +00:00
|
|
|
char state;
|
2016-05-03 04:41:17 +00:00
|
|
|
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;
|
2012-11-19 00:30:30 +00:00
|
|
|
char comm[1024];
|
|
|
|
|
|
|
|
swprintf(fn, FN_SIZE, L"/proc/%d/stat", p->pid);
|
|
|
|
FILE *f = wfopen(fn, "r");
|
2016-05-03 04:41:17 +00:00
|
|
|
if (!f) return 0;
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2016-05-29 05:28:26 +00:00
|
|
|
// TODO: replace the use of fscanf() as it is brittle and should never be used.
|
|
|
|
int count = fscanf(f,
|
|
|
|
"%9d %1023s %c %9d %9d %9d %9d %9d %9lu "
|
|
|
|
"%9lu %9lu %9lu %9lu %9lu %9lu %9ld %9ld %9ld "
|
|
|
|
"%9ld %9ld %9ld %9lu %9lu %9ld %9lu %9lu %9lu "
|
|
|
|
"%9lu %9lu %9lu %9lu %9lu %9lu %9lu %9lu %9lu "
|
|
|
|
"%9lu %9d %9d ",
|
|
|
|
&pid, comm, &state, &ppid, &pgrp, &session, &tty_nr, &tpgid, &flags, &minflt,
|
|
|
|
&cminflt, &majflt, &cmajflt, &utime, &stime, &cutime, &cstime, &priority,
|
|
|
|
&nice, &placeholder, &itrealvalue, &starttime, &vsize, &rss, &rlim,
|
|
|
|
&startcode, &endcode, &startstack, &kstkesp, &kstkeip, &signal, &blocked,
|
|
|
|
&sigignore, &sigcatch, &wchan, &nswap, &cnswap, &exit_signal, &processor);
|
2014-04-26 15:36:20 +00:00
|
|
|
fclose(f);
|
2016-05-29 05:28:26 +00:00
|
|
|
if (count < 17) return 0;
|
2016-05-03 04:41:17 +00:00
|
|
|
return utime + stime + cutime + cstime;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Update the CPU time for all jobs.
|
|
|
|
void proc_update_jiffies() {
|
|
|
|
job_t *job;
|
2012-11-19 00:30:30 +00:00
|
|
|
job_iterator_t j;
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
for (job = j.next(); job; job = j.next()) {
|
2017-01-23 18:38:55 +00:00
|
|
|
for (process_ptr_t &p : job->processes) {
|
2012-11-19 00:30:30 +00:00
|
|
|
gettimeofday(&p->last_time, 0);
|
2017-01-23 18:39:53 +00:00
|
|
|
p->last_jiffies = proc_get_jiffies(p.get());
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// 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) {
|
2012-11-19 00:30:30 +00:00
|
|
|
fd_set fds;
|
2016-05-03 04:41:17 +00:00
|
|
|
int maxfd = -1;
|
2012-11-19 00:30:30 +00:00
|
|
|
|
|
|
|
FD_ZERO(&fds);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-19 23:16:41 +00:00
|
|
|
const io_chain_t chain = j->all_io_redirections();
|
2016-05-03 04:41:17 +00:00
|
|
|
for (size_t idx = 0; idx < chain.size(); idx++) {
|
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-19 23:16:41 +00:00
|
|
|
const io_data_t *io = chain.at(idx).get();
|
2016-05-03 04:41:17 +00:00
|
|
|
if (io->io_mode == IO_BUFFER) {
|
2016-07-30 19:01:37 +00:00
|
|
|
const io_pipe_t *io_pipe = static_cast<const io_pipe_t *>(io);
|
2013-01-15 09:31:36 +00:00
|
|
|
int fd = io_pipe->pipe_fd[0];
|
2016-05-03 04:41:17 +00:00
|
|
|
// fwprintf( stderr, L"fd %d on job %ls\n", fd, j->command );
|
2012-11-19 00:30:30 +00:00
|
|
|
FD_SET(fd, &fds);
|
|
|
|
maxfd = maxi(maxfd, fd);
|
2017-06-19 05:07:48 +00:00
|
|
|
debug(3, L"select_try on %d", fd);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
if (maxfd >= 0) {
|
2012-11-19 00:30:30 +00:00
|
|
|
int retval;
|
|
|
|
struct timeval tv;
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 10000;
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
retval = select(maxfd + 1, &fds, 0, 0, &tv);
|
2015-01-13 06:26:07 +00:00
|
|
|
if (retval == 0) {
|
2017-06-19 05:07:48 +00:00
|
|
|
debug(3, L"select_try hit timeout");
|
2015-01-13 06:26:07 +00:00
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
return retval > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// Read from descriptors until they are empty.
|
|
|
|
///
|
|
|
|
/// \param j the job to test
|
|
|
|
static void read_try(job_t *j) {
|
2013-01-30 10:22:38 +00:00
|
|
|
io_buffer_t *buff = NULL;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
// Find the last buffer, which is the one we want to read from.
|
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-19 23:16:41 +00:00
|
|
|
const io_chain_t chain = j->all_io_redirections();
|
2016-05-03 04:41:17 +00:00
|
|
|
for (size_t idx = 0; idx < chain.size(); idx++) {
|
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-19 23:16:41 +00:00
|
|
|
io_data_t *d = chain.at(idx).get();
|
2016-05-03 04:41:17 +00:00
|
|
|
if (d->io_mode == IO_BUFFER) {
|
2013-01-15 08:44:31 +00:00
|
|
|
buff = static_cast<io_buffer_t *>(d);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
if (buff) {
|
2017-06-19 05:07:48 +00:00
|
|
|
debug(3, L"proc::read_try('%ls')", j->command_wcstr());
|
2016-05-03 04:41:17 +00:00
|
|
|
while (1) {
|
2012-11-19 00:30:30 +00:00
|
|
|
char b[BUFFER_SIZE];
|
|
|
|
long l;
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
l = read_blocked(buff->pipe_fd[0], b, BUFFER_SIZE);
|
|
|
|
if (l == 0) {
|
2012-11-19 00:30:30 +00:00
|
|
|
break;
|
2016-05-03 04:41:17 +00:00
|
|
|
} else if (l < 0) {
|
|
|
|
if (errno != EAGAIN) {
|
|
|
|
debug(1, _(L"An error occured while reading output from code block"));
|
2012-11-19 00:30:30 +00:00
|
|
|
wperror(L"read_try");
|
|
|
|
}
|
|
|
|
break;
|
2016-05-03 04:41:17 +00:00
|
|
|
} else {
|
2012-03-04 10:35:30 +00:00
|
|
|
buff->out_buffer_append(b, l);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// 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.
|
2017-07-27 02:45:22 +00:00
|
|
|
bool terminal_give_to_job(job_t *j, int cont) {
|
2017-02-27 05:46:15 +00:00
|
|
|
errno = 0;
|
|
|
|
if (j->pgid == 0) {
|
|
|
|
debug(2, "terminal_give_to_job() returning early due to no process group");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-07-27 21:23:36 +00:00
|
|
|
//Previously, terminal_give_to_job was being called for each process in a job, hence all the comments
|
|
|
|
//and warnings below. It is now only called for the first process in a job.t d
|
|
|
|
|
2017-07-26 19:25:21 +00:00
|
|
|
//it may not be safe to call tcsetpgrp if we've already done so, as at that point we are no longer
|
|
|
|
//the controlling process group for the terminal and no longer have permission to set the process
|
|
|
|
//group that is in control, causing tcsetpgrp to return EPERM, even though that's not the documented
|
|
|
|
//behavior in tcsetpgrp(3), which instead says other bad things will happen (it says SIGTTOU will be
|
|
|
|
//sent to all members of the background *calling* process group, but it's more complicated than that,
|
|
|
|
//SIGTTOU may or may not be sent depending on the TTY configuration and whether or not signal handlers
|
|
|
|
//for SIGTTOU are installed. Read: http://curiousthing.org/sigttin-sigttou-deep-dive-linux
|
|
|
|
//In all cases, our goal here was just to hand over control of the terminal to this process group,
|
|
|
|
//which is a no-op if it's already been done.
|
2017-07-27 21:23:36 +00:00
|
|
|
auto previous_owner = tcgetpgrp(STDIN_FILENO);
|
|
|
|
if (previous_owner == j->pgid) {
|
2017-07-26 19:25:21 +00:00
|
|
|
debug(2, L"Process group %d already has control of terminal\n", j->pgid);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
debug(4, L"Attempting bring process group to foreground via tcsetpgrp for job->pgid %d\n", j->pgid);
|
|
|
|
debug(4, L"caller session id: %d, pgid %d has session id: %d\n", getsid(0), j->pgid, getsid(j->pgid));
|
|
|
|
|
2017-07-27 21:23:36 +00:00
|
|
|
//the tcsetpgrp(2) man page says that EPERM is thrown if "pgrp has a supported value, but is not the
|
|
|
|
//process group ID of a process in the same session as the calling process."
|
|
|
|
//Since we _guarantee_ that this isn't the case (the child calls setpgid before it calls SIGSTOP, and
|
|
|
|
//the child was created in the same session as us), it seems that EPERM is being thrown because of an
|
|
|
|
//caching issue - the call to tcsetpgrp isn't seeing the newly-created process group just yet. On this
|
|
|
|
//developer's test machine (WSL running Linux 4.4.0), EPERM does indeed disappear on retry. The important
|
|
|
|
//thing is that we can guarantee the process isn't going to exit while we wait (which would cause us to
|
|
|
|
//possibly block indefinitely).
|
|
|
|
|
2017-08-06 21:38:25 +00:00
|
|
|
signal_block(true);
|
2016-12-29 02:52:33 +00:00
|
|
|
int result = -1;
|
|
|
|
errno = EINTR;
|
2017-07-27 21:23:36 +00:00
|
|
|
while (result == -1 && (errno == EINTR || errno == EPERM)) {
|
2016-12-29 02:52:33 +00:00
|
|
|
result = tcsetpgrp(STDIN_FILENO, j->pgid);
|
|
|
|
}
|
|
|
|
if (result == -1) {
|
2016-12-15 03:21:36 +00:00
|
|
|
if (errno == ENOTTY) redirect_tty_output();
|
2017-07-26 19:25:21 +00:00
|
|
|
debug(1, _(L"terminal_give_to_job(): Could not send job %d ('%ls') with pgid %d to foreground"), j->job_id, j->command_wcstr(), j->pgid);
|
2012-11-19 00:30:30 +00:00
|
|
|
wperror(L"tcsetpgrp");
|
2017-08-06 21:38:25 +00:00
|
|
|
signal_unblock(true);
|
2013-04-07 19:40:08 +00:00
|
|
|
return false;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
|
2016-12-29 02:52:33 +00:00
|
|
|
if (cont) {
|
2017-01-27 04:00:43 +00:00
|
|
|
int result = -1;
|
2017-02-27 05:46:15 +00:00
|
|
|
// TODO: Remove this EINTR loop since we have blocked all signals and thus cannot be
|
|
|
|
// interrupted. I'm leaving it in place because all of the logic involving controlling
|
|
|
|
// terminal management is more than a little opaque and smacks of voodoo programming.
|
2017-01-27 04:00:43 +00:00
|
|
|
errno = EINTR;
|
|
|
|
while (result == -1 && errno == EINTR) {
|
|
|
|
result = tcsetattr(STDIN_FILENO, TCSADRAIN, &j->tmodes);
|
|
|
|
}
|
|
|
|
if (result == -1) {
|
|
|
|
if (errno == ENOTTY) redirect_tty_output();
|
|
|
|
debug(1, _(L"terminal_give_to_job(): Could not send job %d ('%ls') to foreground"),
|
|
|
|
j->job_id, j->command_wcstr());
|
|
|
|
wperror(L"tcsetattr");
|
2017-08-06 21:38:25 +00:00
|
|
|
signal_unblock(true);
|
2017-01-27 04:00:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-12-29 02:52:33 +00:00
|
|
|
}
|
|
|
|
|
2017-08-06 21:38:25 +00:00
|
|
|
signal_unblock(true);
|
2013-04-07 19:40:08 +00:00
|
|
|
return true;
|
2006-11-11 10:48:40 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
/// 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.
|
2017-02-27 05:46:15 +00:00
|
|
|
static bool terminal_return_from_job(job_t *j) {
|
|
|
|
errno = 0;
|
|
|
|
if (j->pgid == 0) {
|
|
|
|
debug(2, "terminal_return_from_job() returning early due to no process group");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-08-06 21:38:25 +00:00
|
|
|
signal_block(true);
|
2016-12-15 03:21:36 +00:00
|
|
|
if (tcsetpgrp(STDIN_FILENO, getpgrp()) == -1) {
|
|
|
|
if (errno == ENOTTY) redirect_tty_output();
|
2012-11-19 00:30:30 +00:00
|
|
|
debug(1, _(L"Could not return shell to foreground"));
|
|
|
|
wperror(L"tcsetpgrp");
|
2017-08-06 21:38:25 +00:00
|
|
|
signal_unblock(true);
|
2017-02-27 05:46:15 +00:00
|
|
|
return false;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
|
2016-12-15 03:21:36 +00:00
|
|
|
// Save jobs terminal modes.
|
|
|
|
if (tcgetattr(STDIN_FILENO, &j->tmodes)) {
|
2017-01-11 05:52:10 +00:00
|
|
|
if (errno == EIO) redirect_tty_output();
|
2012-11-19 00:30:30 +00:00
|
|
|
debug(1, _(L"Could not return shell to foreground"));
|
|
|
|
wperror(L"tcgetattr");
|
2017-08-06 21:38:25 +00:00
|
|
|
signal_unblock(true);
|
2017-02-27 05:46:15 +00:00
|
|
|
return false;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
// 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
|
2012-10-06 01:23:38 +00:00
|
|
|
#if 0
|
2016-05-03 04:41:17 +00:00
|
|
|
// Restore the shell's terminal modes.
|
2017-01-11 05:52:10 +00:00
|
|
|
if (tcsetattr(STDIN_FILENO, TCSADRAIN, &shell_modes) == -1) {
|
|
|
|
if (errno == EIO) redirect_tty_output();
|
2012-11-19 00:30:30 +00:00
|
|
|
debug(1, _(L"Could not return shell to foreground"));
|
|
|
|
wperror(L"tcsetattr");
|
2017-02-27 05:46:15 +00:00
|
|
|
return false;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-10-06 01:23:38 +00:00
|
|
|
#endif
|
2006-11-11 10:48:40 +00:00
|
|
|
|
2017-08-06 21:38:25 +00:00
|
|
|
signal_unblock(true);
|
2017-02-27 05:46:15 +00:00
|
|
|
return true;
|
2006-11-11 10:48:40 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
void job_continue(job_t *j, bool cont) {
|
|
|
|
// Put job first in the job list.
|
2012-01-30 00:36:21 +00:00
|
|
|
job_promote(j);
|
2017-01-26 23:06:58 +00:00
|
|
|
j->set_flag(JOB_NOTIFIED, false);
|
2012-11-19 00:30:30 +00:00
|
|
|
|
|
|
|
CHECK_BLOCK();
|
2016-05-03 04:41:17 +00:00
|
|
|
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)) {
|
2017-01-26 23:06:58 +00:00
|
|
|
if (j->get_flag(JOB_TERMINAL) && j->get_flag(JOB_FOREGROUND)) {
|
2016-05-03 04:41:17 +00:00
|
|
|
// Put the job into the foreground. Hack: ensure that stdin is marked as blocking first
|
|
|
|
// (issue #176).
|
2013-04-07 19:40:08 +00:00
|
|
|
make_fd_blocking(STDIN_FILENO);
|
2017-02-27 05:46:15 +00:00
|
|
|
if (!terminal_give_to_job(j, cont)) return;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
// Send the job a continue signal, if necessary.
|
|
|
|
if (cont) {
|
2017-01-23 17:28:34 +00:00
|
|
|
for (process_ptr_t &p : j->processes) p->stopped = false;
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2017-01-26 23:06:58 +00:00
|
|
|
if (j->get_flag(JOB_CONTROL)) {
|
2016-05-03 04:41:17 +00:00
|
|
|
if (killpg(j->pgid, SIGCONT)) {
|
2012-11-19 00:30:30 +00:00
|
|
|
wperror(L"killpg (SIGCONT)");
|
|
|
|
return;
|
|
|
|
}
|
2016-05-03 04:41:17 +00:00
|
|
|
} else {
|
2017-01-23 17:28:34 +00:00
|
|
|
for (const process_ptr_t &p : j->processes) {
|
2016-05-03 04:41:17 +00:00
|
|
|
if (kill(p->pid, SIGCONT) < 0) {
|
2012-11-19 00:30:30 +00:00
|
|
|
wperror(L"kill (SIGCONT)");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-26 23:06:58 +00:00
|
|
|
if (j->get_flag(JOB_FOREGROUND)) {
|
2016-05-03 04:41:17 +00:00
|
|
|
// Look for finished processes first, to avoid select() if it's already done.
|
2015-01-13 06:26:07 +00:00
|
|
|
process_mark_finished_children(false);
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
// 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: {
|
2014-12-29 09:04:13 +00:00
|
|
|
read_try(j);
|
|
|
|
process_mark_finished_children(false);
|
|
|
|
break;
|
|
|
|
}
|
2016-05-03 04:41:17 +00:00
|
|
|
case 0: {
|
|
|
|
// No FDs are ready. Look for finished processes.
|
2014-12-29 09:04:13 +00:00
|
|
|
process_mark_finished_children(false);
|
|
|
|
break;
|
|
|
|
}
|
2016-05-03 04:41:17 +00:00
|
|
|
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.
|
2015-02-27 17:53:57 +00:00
|
|
|
process_mark_finished_children(true);
|
2014-12-29 09:04:13 +00:00
|
|
|
break;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2016-11-03 01:29:14 +00:00
|
|
|
default: {
|
|
|
|
DIE("unexpected return value from select_try()");
|
|
|
|
break;
|
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-26 23:06:58 +00:00
|
|
|
if (j->get_flag(JOB_FOREGROUND)) {
|
2016-05-03 04:41:17 +00:00
|
|
|
if (job_is_completed(j)) {
|
|
|
|
// It's possible that the job will produce output and exit before we've even read from
|
|
|
|
// it.
|
|
|
|
//
|
2012-11-19 00:30:30 +00:00
|
|
|
// 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
|
2016-05-03 04:41:17 +00:00
|
|
|
// were sometimes having their output combined with the set_color calls in the wrong
|
|
|
|
// order!
|
2012-11-19 00:30:30 +00:00
|
|
|
read_try(j);
|
|
|
|
|
2017-01-23 17:28:34 +00:00
|
|
|
const std::unique_ptr<process_t> &p = j->processes.back();
|
2016-05-03 04:41:17 +00:00
|
|
|
|
2016-10-22 18:21:13 +00:00
|
|
|
// 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 ((WIFEXITED(p->status) || WIFSIGNALED(p->status)) && p->pid) {
|
|
|
|
int status = proc_format_status(p->status);
|
2017-01-14 04:34:15 +00:00
|
|
|
// fwprintf(stdout, L"setting status %d for %ls\n", job_get_flag( j, JOB_NEGATE
|
2016-10-22 18:21:13 +00:00
|
|
|
// )?!status:status, j->command);
|
2017-01-26 23:06:58 +00:00
|
|
|
proc_set_last_status(j->get_flag(JOB_NEGATE) ? !status : status);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-05 09:33:17 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
// Put the shell back in the foreground.
|
2017-01-26 23:06:58 +00:00
|
|
|
if (j->get_flag(JOB_TERMINAL) && j->get_flag(JOB_FOREGROUND)) {
|
2017-02-27 05:46:15 +00:00
|
|
|
terminal_return_from_job(j);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
int proc_format_status(int status) {
|
|
|
|
if (WIFSIGNALED(status)) {
|
|
|
|
return 128 + WTERMSIG(status);
|
|
|
|
} else if (WIFEXITED(status)) {
|
2012-11-19 00:30:30 +00:00
|
|
|
return WEXITSTATUS(status);
|
|
|
|
}
|
|
|
|
return status;
|
2009-02-21 16:46:56 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
void proc_sanity_check() {
|
2017-01-26 22:47:32 +00:00
|
|
|
const job_t *fg_job = NULL;
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2012-01-30 00:36:21 +00:00
|
|
|
job_iterator_t jobs;
|
2017-01-26 22:47:32 +00:00
|
|
|
while (const job_t *j = jobs.next()) {
|
2017-01-26 23:06:58 +00:00
|
|
|
if (!j->get_flag(JOB_CONSTRUCTED)) continue;
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
// More than one foreground job?
|
2017-01-26 23:06:58 +00:00
|
|
|
if (j->get_flag(JOB_FOREGROUND) && !(job_is_stopped(j) || job_is_completed(j))) {
|
2017-01-24 23:14:56 +00:00
|
|
|
if (fg_job) {
|
2016-05-03 04:41:17 +00:00
|
|
|
debug(0, _(L"More than one job in foreground: job 1: '%ls' job 2: '%ls'"),
|
|
|
|
fg_job->command_wcstr(), j->command_wcstr());
|
2012-11-19 00:30:30 +00:00
|
|
|
sanity_lose();
|
|
|
|
}
|
|
|
|
fg_job = j;
|
|
|
|
}
|
|
|
|
|
2017-01-23 17:28:34 +00:00
|
|
|
for (const process_ptr_t &p : j->processes) {
|
2016-05-03 04:41:17 +00:00
|
|
|
// Internal block nodes do not have argv - see issue #1545.
|
2014-07-12 18:01:00 +00:00
|
|
|
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);
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
if ((p->stopped & (~0x00000001)) != 0) {
|
|
|
|
debug(0, _(L"Job '%ls', process '%ls' has inconsistent state \'stopped\'=%d"),
|
|
|
|
j->command_wcstr(), p->argv0(), p->stopped);
|
2012-11-19 00:30:30 +00:00
|
|
|
sanity_lose();
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
if ((p->completed & (~0x00000001)) != 0) {
|
|
|
|
debug(0, _(L"Job '%ls', process '%ls' has inconsistent state \'completed\'=%d"),
|
|
|
|
j->command_wcstr(), p->argv0(), p->completed);
|
2012-11-19 00:30:30 +00:00
|
|
|
sanity_lose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
void proc_push_interactive(int value) {
|
2012-02-26 02:54:49 +00:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
2012-11-19 00:30:30 +00:00
|
|
|
int old = is_interactive;
|
2012-02-10 03:26:44 +00:00
|
|
|
interactive_stack.push_back(is_interactive);
|
2012-11-19 00:30:30 +00:00
|
|
|
is_interactive = value;
|
2016-05-03 04:41:17 +00:00
|
|
|
if (old != value) signal_set_handlers();
|
2006-02-16 13:36:32 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 04:41:17 +00:00
|
|
|
void proc_pop_interactive() {
|
2012-02-26 02:54:49 +00:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
2012-11-19 00:30:30 +00:00
|
|
|
int old = is_interactive;
|
2016-05-03 04:41:17 +00:00
|
|
|
is_interactive = interactive_stack.back();
|
2012-02-10 03:26:44 +00:00
|
|
|
interactive_stack.pop_back();
|
2016-05-03 04:41:17 +00:00
|
|
|
if (is_interactive != old) signal_set_handlers();
|
2006-02-16 13:36:32 +00:00
|
|
|
}
|