fish-shell/src/event.cpp

451 lines
16 KiB
C++
Raw Normal View History

// Functions for handling event triggers.
#include "config.h" // IWYU pragma: keep
#include "event.h"
#include <signal.h>
#include <stddef.h>
#include <unistd.h>
2017-02-11 02:47:02 +00:00
#include <algorithm>
#include <atomic>
2017-02-11 02:47:02 +00:00
#include <functional>
#include <memory>
2015-07-25 15:14:25 +00:00
#include <string>
2017-02-11 02:47:02 +00:00
#include <type_traits>
#include "common.h"
#include "fallback.h" // IWYU pragma: keep
#include "input_common.h"
2015-07-25 15:14:25 +00:00
#include "io.h"
#include "parser.h"
#include "proc.h"
#include "signal.h"
#include "wutil.h" // IWYU pragma: keep
class pending_signals_t {
static constexpr size_t SIGNAL_COUNT = NSIG;
/// A counter that is incremented each time a pending signal is received.
std::atomic<uint32_t> counter_{0};
/// List of pending signals.
std::array<std::atomic<bool>, SIGNAL_COUNT> received_{};
/// The last counter visible in acquire_pending().
/// This is not accessed from a signal handler.
owning_lock<uint32_t> last_counter_{0};
public:
pending_signals_t() = default;
/// No copying.
pending_signals_t(const pending_signals_t &);
void operator=(const pending_signals_t &);
/// Mark a signal as pending. This may be called from a signal handler.
/// We expect only one signal handler to execute at once.
/// Also note that these may be coalesced.
void mark(int which) {
if (which >= 0 && static_cast<size_t>(which) < received_.size()) {
// Must mark our received first, then pending.
received_[which].store(true, std::memory_order_relaxed);
uint32_t count = counter_.load(std::memory_order_relaxed);
counter_.store(1 + count, std::memory_order_release);
}
}
/// \return the list of signals that were set, clearing them.
std::bitset<SIGNAL_COUNT> acquire_pending() {
auto current = last_counter_.acquire();
// Check the counter first. If it hasn't changed, no signals have been received.
uint32_t count = counter_.load(std::memory_order_acquire);
if (count == *current) {
return {};
}
// The signal count has changed. Store the new counter and fetch all the signals that are
// set.
*current = count;
std::bitset<SIGNAL_COUNT> result{};
uint32_t bit = 0;
for (auto &signal : received_) {
bool val = signal.load(std::memory_order_relaxed);
if (val) {
result.set(bit);
signal.store(false, std::memory_order_relaxed);
}
bit++;
}
return result;
}
};
static pending_signals_t s_pending_signals;
/// List of event handlers.
static owning_lock<event_handler_list_t> s_event_handlers;
/// Variables (one per signal) set when a signal is observed. This is inspected by a signal handler.
2019-06-03 07:02:22 +00:00
static volatile sig_atomic_t s_observed_signals[NSIG] = {};
static void set_signal_observed(int sig, bool val) {
if (sig >= 0 &&
static_cast<size_t>(sig) < sizeof s_observed_signals / sizeof *s_observed_signals) {
s_observed_signals[sig] = val;
}
}
/// Tests if one event instance matches the definition of a event class.
static bool handler_matches(const event_handler_t &classv, const event_t &instance) {
if (classv.desc.type == event_type_t::any) return true;
if (classv.desc.type != instance.desc.type) return false;
switch (classv.desc.type) {
2019-02-21 06:42:58 +00:00
case event_type_t::signal: {
return classv.desc.param1.signal == instance.desc.param1.signal;
}
2019-02-21 06:42:58 +00:00
case event_type_t::variable: {
return instance.desc.str_param1 == classv.desc.str_param1;
}
2019-02-21 06:42:58 +00:00
case event_type_t::exit: {
if (classv.desc.param1.pid == EVENT_ANY_PID) return true;
return classv.desc.param1.pid == instance.desc.param1.pid;
}
case event_type_t::caller_exit: {
return classv.desc.param1.caller_id == instance.desc.param1.caller_id;
}
2019-02-21 06:42:58 +00:00
case event_type_t::generic: {
return classv.desc.str_param1 == instance.desc.str_param1;
}
case event_type_t::any:
default: {
DIE("unexpected classv.type");
return false;
}
}
}
/// Test if specified event is blocked.
static int event_is_blocked(parser_t &parser, const event_t &e) {
2019-05-13 01:23:00 +00:00
(void)e;
const block_t *block;
size_t idx = 0;
while ((block = parser.block_at_index(idx++))) {
2019-02-21 06:42:58 +00:00
if (event_block_list_blocks_type(block->event_blocks)) return true;
}
2019-02-21 06:42:58 +00:00
return event_block_list_blocks_type(parser.global_event_blocks);
}
wcstring event_get_desc(const parser_t &parser, const event_t &evt) {
const event_description_t &ed = evt.desc;
switch (ed.type) {
2019-02-21 06:42:58 +00:00
case event_type_t::signal: {
return format_string(_(L"signal handler for %ls (%ls)"), sig2wcs(ed.param1.signal),
signal_get_desc(ed.param1.signal));
}
2019-02-21 06:42:58 +00:00
case event_type_t::variable: {
return format_string(_(L"handler for variable '%ls'"), ed.str_param1.c_str());
}
2019-02-21 06:42:58 +00:00
case event_type_t::exit: {
if (ed.param1.pid > 0) {
return format_string(_(L"exit handler for process %d"), ed.param1.pid);
} else {
// In events, PGIDs are stored as negative PIDs
job_t *j = parser.job_get_from_pid(-ed.param1.pid);
if (j) {
Introduce the internal jobs for functions This PR is aimed at improving how job ids are assigned. In particular, previous to this commit, a job id would be consumed by functions (and thus aliases). Since it's usual to use functions as command wrappers this results in awkward job id assignments. For example if the user is like me and just made the jump from vim -> neovim then the user might create the following alias: ``` alias vim=nvim ``` Previous to this commit if the user ran `vim` after setting up this alias, backgrounded (^Z) and ran `jobs` then the output might be: ``` Job Group State Command 2 60267 stopped nvim $argv ``` If the user subsequently opened another vim (nvim) session, backgrounded and ran jobs then they might see what follows: ``` Job Group State Command 4 70542 stopped nvim $argv 2 60267 stopped nvim $argv ``` These job ids feel unnatural, especially when transitioning away from e.g. bash where job ids are sequentially incremented (and aliases/functions don't consume a job id). See #6053 for more details. As @ridiculousfish pointed out in https://github.com/fish-shell/fish-shell/issues/6053#issuecomment-559899400, we want to elide a job's job id if it corresponds to a single function in the foreground. This translates to the following prerequisites: - A job must correspond to a single process (i.e. the job continuation must be empty) - A job must be in the foreground (i.e. `&` wasn't appended) - The job's single process must resolve to a function invocation If all of these conditions are true then we should mark a job as "internal" and somehow remove it from consideration when any infrastructure tries to interact with jobs / job ids. I saw two paths to implement these requirements: - At the time of job creation calculate whether or not a job is "internal" and use a separate list of job ids to track their ids. Additionally introduce a new flag denoting that a job is internal so that e.g. `jobs` doesn't list internal jobs - I started implementing this route but quickly realized I was computing the same information that would be computed later on (e.g. "is this job a single process" and "is this jobs statement a function"). Specifically I was computing data that populate_job_process would end up computing later anyway. Additionally this added some weird complexities to the job system (after the change there were two job id lists AND an additional flag that had to be taken into consideration) - Once a function is about to be executed we release the current jobs job id if the prerequisites are satisfied (which at this point have been fully computed). - I opted for this solution since it seems cleaner. In this implementation "releasing a job id" is done by both calling `release_job_id` and by marking the internal job_id member variable to -1. The former operation allows subsequent child jobs to reuse that same job id (so e.g. the situation described in Motivation doesn't occur), and the latter ensures that no other job / job id infrastructure will interact with these jobs because valid jobs have positive job ids. The second operation causes job_id to become non-const which leads to the list of code changes outside of `exec.c` (i.e. a codemod from `job_t::job_id` -> `job_t::job_id()` and moving the old member variable to a non-const private `job_t::job_id_`) Note: Its very possible I missed something and setting the job id to -1 will break some other infrastructure, please let me know if so! I tried to run `make/ninja lint`, but a bunch of non-relevant issues appeared (e.g. `fatal error: 'config.h' file not found`). I did successfully clang-format (`git clang-format -f`) and run tests, though. This PR closes #6053.
2019-12-29 15:46:07 +00:00
return format_string(_(L"exit handler for job %d, '%ls'"), j->job_id(),
j->command_wcstr());
} else {
return format_string(_(L"exit handler for job with process group %d"),
-ed.param1.pid);
}
2012-11-19 08:31:03 +00:00
}
DIE("Unreachable");
}
case event_type_t::caller_exit: {
return _(L"exit handler for command substitution caller");
2012-11-19 08:31:03 +00:00
}
2019-02-21 06:42:58 +00:00
case event_type_t::generic: {
return format_string(_(L"handler for generic event '%ls'"), ed.str_param1.c_str());
}
case event_type_t::any: {
DIE("Unreachable");
}
default:
DIE("Unknown event type");
}
}
void event_add_handler(std::shared_ptr<event_handler_t> eh) {
if (eh->desc.type == event_type_t::signal) {
signal_handle(eh->desc.param1.signal);
set_signal_observed(eh->desc.param1.signal, true);
}
s_event_handlers.acquire()->push_back(std::move(eh));
}
void event_remove_function_handlers(const wcstring &name) {
auto handlers = s_event_handlers.acquire();
auto begin = handlers->begin(), end = handlers->end();
handlers->erase(std::remove_if(begin, end,
[&](const shared_ptr<event_handler_t> &eh) {
return eh->function_name == name;
}),
end);
}
event_handler_list_t event_get_function_handlers(const wcstring &name) {
auto handlers = s_event_handlers.acquire();
event_handler_list_t result;
for (const shared_ptr<event_handler_t> &eh : *handlers) {
if (eh->function_name == name) {
result.push_back(eh);
}
}
return result;
}
bool event_is_signal_observed(int sig) {
// We are in a signal handler! Don't allocate memory, etc.
bool result = false;
if (sig >= 0 && static_cast<unsigned long>(sig) <
sizeof(s_observed_signals) / sizeof(*s_observed_signals)) {
result = s_observed_signals[sig];
}
return result;
}
/// Perform the specified event. Since almost all event firings will not be matched by even a single
/// event handler, we make sure to optimize the 'no matches' path. This means that nothing is
/// allocated/initialized unless needed.
static void event_fire_internal(parser_t &parser, const event_t &event) {
auto &ld = parser.libdata();
assert(ld.is_event >= 0 && "is_event should not be negative");
scoped_push<decltype(ld.is_event)> inc_event{&ld.is_event, ld.is_event + 1};
// Suppress fish_trace during events.
scoped_push<bool> suppress_trace{&ld.suppress_fish_trace, true};
// Capture the event handlers that match this event.
event_handler_list_t fire;
for (const auto &handler : *s_event_handlers.acquire()) {
// Check if this event is a match.
if (handler_matches(*handler, event)) {
fire.push_back(handler);
}
}
// Iterate over our list of matching events. Fire the ones that are still present.
for (const shared_ptr<event_handler_t> &handler : fire) {
// Only fire if this event is still present.
// TODO: this is kind of crazy. We want to support removing (and thereby suppressing) an
// event handler from another, but we also don't want to hold the lock across callouts. How
// can we make this less silly?
if (!contains(*s_event_handlers.acquire(), handler)) {
continue;
}
// Construct a buffer to evaluate, starting with the function name and then all the
// arguments.
wcstring buffer = handler->function_name;
for (const wcstring &arg : event.arguments) {
buffer.push_back(L' ');
buffer.append(escape_string(arg, ESCAPE_ALL));
2011-12-27 08:06:07 +00:00
}
// Event handlers are not part of the main flow of code, so they are marked as
// non-interactive.
scoped_push<bool> interactive{&ld.is_interactive, false};
auto prev_statuses = parser.get_last_statuses();
FLOGF(event, L"Firing event '%ls'", event.desc.str_param1.c_str());
block_t *b = parser.push_block(block_t::event_block(event));
parser.eval(buffer, io_chain_t());
parser.pop_block(b);
parser.set_last_statuses(std::move(prev_statuses));
}
}
/// Handle all pending signal events.
void event_fire_delayed(parser_t &parser) {
auto &ld = parser.libdata();
// Do not invoke new event handlers from within event handlers.
if (ld.is_event) return;
// Do not invoke new event handlers if we are unwinding (#6649).
if (signal_check_cancel()) return;
std::vector<shared_ptr<const event_t>> to_send;
to_send.swap(ld.blocked_events);
assert(ld.blocked_events.empty());
// Append all signal events to to_send.
auto signals = s_pending_signals.acquire_pending();
if (signals.any()) {
for (uint32_t sig = 0; sig < signals.size(); sig++) {
if (signals.test(sig)) {
2019-02-21 06:42:58 +00:00
auto e = std::make_shared<event_t>(event_type_t::signal);
e->desc.param1.signal = sig;
e->arguments.push_back(sig2wcs(sig));
to_send.push_back(std::move(e));
}
}
}
// Fire or re-block all events.
for (const auto &evt : to_send) {
if (event_is_blocked(parser, *evt)) {
ld.blocked_events.push_back(evt);
} else {
event_fire_internal(parser, *evt);
}
}
}
void event_enqueue_signal(int signal) {
// Beware, we are in a signal handler
s_pending_signals.mark(signal);
}
void event_fire(parser_t &parser, const event_t &event) {
// Fire events triggered by signals.
event_fire_delayed(parser);
if (event_is_blocked(parser, event)) {
parser.libdata().blocked_events.push_back(std::make_shared<event_t>(event));
} else {
event_fire_internal(parser, event);
}
}
/// Mapping between event type to name.
/// Note we don't bother to sort this.
struct event_type_name_t {
event_type_t type;
const wchar_t *name;
};
2019-02-21 06:42:58 +00:00
static const event_type_name_t events_mapping[] = {{event_type_t::signal, L"signal"},
{event_type_t::variable, L"variable"},
{event_type_t::exit, L"exit"},
{event_type_t::caller_exit, L"caller-exit"},
2019-02-21 06:42:58 +00:00
{event_type_t::generic, L"generic"}};
maybe_t<event_type_t> event_type_for_name(const wcstring &name) {
for (const auto &em : events_mapping) {
if (name == em.name) {
return em.type;
}
}
return none();
}
static const wchar_t *event_name_for_type(event_type_t type) {
for (const auto &em : events_mapping) {
if (type == em.type) {
return em.name;
}
}
return L"";
}
void event_print(io_streams_t &streams, maybe_t<event_type_t> type_filter) {
event_handler_list_t tmp = *s_event_handlers.acquire();
std::sort(tmp.begin(), tmp.end(),
[](const shared_ptr<event_handler_t> &e1, const shared_ptr<event_handler_t> &e2) {
const event_description_t &d1 = e1->desc;
const event_description_t &d2 = e2->desc;
if (d1.type != d2.type) {
return d1.type < d2.type;
}
switch (d1.type) {
case event_type_t::signal:
return d1.signal < d2.signal;
case event_type_t::exit:
return d1.param1.pid < d2.param1.pid;
case event_type_t::caller_exit:
return d1.param1.caller_id < d2.param1.caller_id;
case event_type_t::variable:
case event_type_t::any:
case event_type_t::generic:
return d1.str_param1 < d2.str_param1;
}
2019-02-23 22:09:17 +00:00
DIE("Unreachable");
});
maybe_t<event_type_t> last_type{};
for (const shared_ptr<event_handler_t> &evt : tmp) {
// If we have a filter, skip events that don't match.
if (type_filter && *type_filter != evt->desc.type) {
continue;
}
if (!last_type || *last_type != evt->desc.type) {
if (last_type) streams.out.append(L"\n");
last_type = evt->desc.type;
streams.out.append_format(L"Event %ls\n", event_name_for_type(*last_type));
}
switch (evt->desc.type) {
2019-02-21 06:42:58 +00:00
case event_type_t::signal:
streams.out.append_format(L"%ls %ls\n", sig2wcs(evt->desc.param1.signal),
evt->function_name.c_str());
break;
case event_type_t::exit:
break;
case event_type_t::caller_exit:
streams.out.append_format(L"caller-exit %ls\n", evt->function_name.c_str());
break;
2019-02-21 06:42:58 +00:00
case event_type_t::variable:
case event_type_t::generic:
streams.out.append_format(L"%ls %ls\n", evt->desc.str_param1.c_str(),
evt->function_name.c_str());
break;
case event_type_t::any:
DIE("Unreachable");
default:
streams.out.append_format(L"%ls\n", evt->function_name.c_str());
break;
}
}
}
void event_fire_generic(parser_t &parser, const wchar_t *name, const wcstring_list_t *args) {
assert(name && "Null name");
2012-02-09 03:02:25 +00:00
2019-02-21 06:42:58 +00:00
event_t ev(event_type_t::generic);
ev.desc.str_param1 = name;
if (args) ev.arguments = *args;
event_fire(parser, ev);
}
event_description_t event_description_t::signal(int sig) {
event_description_t event(event_type_t::signal);
2012-02-09 03:02:25 +00:00
event.param1.signal = sig;
return event;
}
event_description_t event_description_t::variable(wcstring str) {
event_description_t event(event_type_t::variable);
event.str_param1 = std::move(str);
2012-02-09 03:02:25 +00:00
return event;
}
event_description_t event_description_t::generic(wcstring str) {
event_description_t event(event_type_t::generic);
event.str_param1 = std::move(str);
2012-02-09 03:02:25 +00:00
return event;
}
event_t event_t::variable(wcstring name, wcstring_list_t args) {
event_t evt{event_type_t::variable};
evt.desc.str_param1 = std::move(name);
evt.arguments = std::move(args);
return evt;
}