2016-04-29 02:19:50 +00:00
|
|
|
// Functions for handling event triggers.
|
2016-05-18 22:30:21 +00:00
|
|
|
#include "config.h" // IWYU pragma: keep
|
|
|
|
|
2019-10-13 22:50:48 +00:00
|
|
|
#include "event.h"
|
|
|
|
|
2016-04-29 02:19:50 +00:00
|
|
|
#include <signal.h>
|
2017-02-13 04:24:22 +00:00
|
|
|
#include <stddef.h>
|
2016-04-21 06:00:54 +00:00
|
|
|
#include <unistd.h>
|
2017-02-11 02:47:02 +00:00
|
|
|
|
2016-04-21 06:00:54 +00:00
|
|
|
#include <algorithm>
|
2019-02-21 03:36:29 +00:00
|
|
|
#include <atomic>
|
2017-02-11 02:47:02 +00:00
|
|
|
#include <functional>
|
2016-04-29 02:19:50 +00:00
|
|
|
#include <memory>
|
2015-07-25 15:14:25 +00:00
|
|
|
#include <string>
|
2017-02-11 02:47:02 +00:00
|
|
|
#include <type_traits>
|
2005-10-05 22:37:08 +00:00
|
|
|
|
|
|
|
#include "common.h"
|
2016-04-29 02:19:50 +00:00
|
|
|
#include "fallback.h" // IWYU pragma: keep
|
|
|
|
#include "input_common.h"
|
2015-07-25 15:14:25 +00:00
|
|
|
#include "io.h"
|
2016-04-29 02:19:50 +00:00
|
|
|
#include "parser.h"
|
|
|
|
#include "proc.h"
|
|
|
|
#include "signal.h"
|
2021-04-14 15:17:22 +00:00
|
|
|
#include "termsize.h"
|
2021-05-17 22:33:33 +00:00
|
|
|
#include "wcstringutil.h"
|
2016-04-29 02:19:50 +00:00
|
|
|
#include "wutil.h" // IWYU pragma: keep
|
2006-07-19 22:55:49 +00:00
|
|
|
|
2019-02-21 03:36:29 +00:00
|
|
|
class pending_signals_t {
|
|
|
|
static constexpr size_t SIGNAL_COUNT = NSIG;
|
2005-10-05 22:37:08 +00:00
|
|
|
|
2019-02-21 03:36:29 +00:00
|
|
|
/// A counter that is incremented each time a pending signal is received.
|
|
|
|
std::atomic<uint32_t> counter_{0};
|
2005-10-05 22:37:08 +00:00
|
|
|
|
2019-02-21 03:36:29 +00:00
|
|
|
/// List of pending signals.
|
|
|
|
std::array<std::atomic<bool>, SIGNAL_COUNT> received_{};
|
2005-10-05 22:37:08 +00:00
|
|
|
|
2019-02-21 03:36:29 +00:00
|
|
|
/// The last counter visible in acquire_pending().
|
|
|
|
/// This is not accessed from a signal handler.
|
|
|
|
owning_lock<uint32_t> last_counter_{0};
|
|
|
|
|
2019-05-05 10:09:25 +00:00
|
|
|
public:
|
2019-02-21 03:36:29 +00:00
|
|
|
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 {};
|
|
|
|
}
|
|
|
|
|
2019-05-05 10:09:25 +00:00
|
|
|
// The signal count has changed. Store the new counter and fetch all the signals that are
|
|
|
|
// set.
|
2019-02-21 03:36:29 +00:00
|
|
|
*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;
|
2005-10-05 22:37:08 +00:00
|
|
|
|
2016-04-29 02:19:50 +00:00
|
|
|
/// List of event handlers.
|
2019-06-03 19:33:10 +00:00
|
|
|
static owning_lock<event_handler_list_t> s_event_handlers;
|
2005-10-05 22:37:08 +00:00
|
|
|
|
2016-04-29 02:19:50 +00:00
|
|
|
/// 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] = {};
|
|
|
|
|
2016-04-29 02:19:50 +00:00
|
|
|
static void set_signal_observed(int sig, bool val) {
|
2019-11-19 01:08:16 +00:00
|
|
|
if (sig >= 0 &&
|
|
|
|
static_cast<size_t>(sig) < sizeof s_observed_signals / sizeof *s_observed_signals) {
|
2014-12-29 08:34:36 +00:00
|
|
|
s_observed_signals[sig] = val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-06 04:32:57 +00:00
|
|
|
/// Tests if one event instance matches the definition of an event class.
|
|
|
|
/// In case of a match, \p only_once indicates that the event cannot match again by nature.
|
|
|
|
static bool handler_matches(const event_handler_t &classv, const event_t &instance,
|
|
|
|
bool &only_once) {
|
|
|
|
only_once = false;
|
2019-02-23 09:04:05 +00:00
|
|
|
if (classv.desc.type == event_type_t::any) return true;
|
|
|
|
if (classv.desc.type != instance.desc.type) return false;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2019-02-23 09:04:05 +00:00
|
|
|
switch (classv.desc.type) {
|
2019-02-21 06:42:58 +00:00
|
|
|
case event_type_t::signal: {
|
2019-02-23 09:04:05 +00:00
|
|
|
return classv.desc.param1.signal == instance.desc.param1.signal;
|
2016-04-29 02:19:50 +00:00
|
|
|
}
|
2019-02-21 06:42:58 +00:00
|
|
|
case event_type_t::variable: {
|
2019-02-23 09:04:05 +00:00
|
|
|
return instance.desc.str_param1 == classv.desc.str_param1;
|
2016-04-29 02:19:50 +00:00
|
|
|
}
|
2021-05-19 18:29:03 +00:00
|
|
|
case event_type_t::process_exit: {
|
2019-02-23 09:04:05 +00:00
|
|
|
if (classv.desc.param1.pid == EVENT_ANY_PID) return true;
|
2021-03-06 04:32:57 +00:00
|
|
|
only_once = true;
|
2019-02-23 09:04:05 +00:00
|
|
|
return classv.desc.param1.pid == instance.desc.param1.pid;
|
2016-04-29 02:19:50 +00:00
|
|
|
}
|
2021-05-19 18:29:03 +00:00
|
|
|
case event_type_t::job_exit: {
|
2021-05-20 18:25:32 +00:00
|
|
|
const auto &jobspec = classv.desc.param1.jobspec;
|
|
|
|
if (jobspec.pid == EVENT_ANY_PID) return true;
|
2021-05-19 18:29:03 +00:00
|
|
|
only_once = true;
|
2021-05-20 18:25:32 +00:00
|
|
|
return jobspec.internal_job_id == instance.desc.param1.jobspec.internal_job_id;
|
2021-05-19 18:29:03 +00:00
|
|
|
}
|
2020-02-09 00:08:26 +00:00
|
|
|
case event_type_t::caller_exit: {
|
2021-03-06 04:32:57 +00:00
|
|
|
only_once = true;
|
2020-02-08 23:43:21 +00:00
|
|
|
return classv.desc.param1.caller_id == instance.desc.param1.caller_id;
|
2016-04-29 02:19:50 +00:00
|
|
|
}
|
2019-02-21 06:42:58 +00:00
|
|
|
case event_type_t::generic: {
|
2019-02-23 09:04:05 +00:00
|
|
|
return classv.desc.str_param1 == instance.desc.str_param1;
|
2016-04-29 02:19:50 +00:00
|
|
|
}
|
2019-03-12 22:27:13 +00:00
|
|
|
case event_type_t::any:
|
|
|
|
default: {
|
2016-10-30 00:25:48 +00:00
|
|
|
DIE("unexpected classv.type");
|
2019-02-23 09:04:05 +00:00
|
|
|
return false;
|
2016-10-30 00:25:48 +00:00
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2005-10-05 22:37:08 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 02:19:50 +00:00
|
|
|
/// Test if specified event is blocked.
|
2019-06-03 09:31:13 +00:00
|
|
|
static int event_is_blocked(parser_t &parser, const event_t &e) {
|
2019-05-13 01:23:00 +00:00
|
|
|
(void)e;
|
2013-12-21 01:41:21 +00:00
|
|
|
const block_t *block;
|
|
|
|
size_t idx = 0;
|
2016-04-29 02:19:50 +00:00
|
|
|
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;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2019-02-21 06:42:58 +00:00
|
|
|
return event_block_list_blocks_type(parser.global_event_blocks);
|
2005-12-11 22:21:01 +00:00
|
|
|
}
|
|
|
|
|
2020-02-08 20:39:03 +00:00
|
|
|
wcstring event_get_desc(const parser_t &parser, const event_t &evt) {
|
2019-02-23 09:04:05 +00:00
|
|
|
const event_description_t &ed = evt.desc;
|
|
|
|
switch (ed.type) {
|
2019-02-21 06:42:58 +00:00
|
|
|
case event_type_t::signal: {
|
2019-02-23 09:04:05 +00:00
|
|
|
return format_string(_(L"signal handler for %ls (%ls)"), sig2wcs(ed.param1.signal),
|
|
|
|
signal_get_desc(ed.param1.signal));
|
2016-04-29 02:19:50 +00:00
|
|
|
}
|
2019-02-23 09:04:05 +00:00
|
|
|
|
2019-02-21 06:42:58 +00:00
|
|
|
case event_type_t::variable: {
|
2019-02-23 09:04:05 +00:00
|
|
|
return format_string(_(L"handler for variable '%ls'"), ed.str_param1.c_str());
|
2016-04-29 02:19:50 +00:00
|
|
|
}
|
2019-02-23 09:04:05 +00:00
|
|
|
|
2021-05-19 18:29:03 +00:00
|
|
|
case event_type_t::process_exit: {
|
|
|
|
return format_string(_(L"exit handler for process %d"), ed.param1.pid);
|
|
|
|
}
|
|
|
|
|
|
|
|
case event_type_t::job_exit: {
|
2021-05-20 18:25:32 +00:00
|
|
|
const auto &jobspec = ed.param1.jobspec;
|
|
|
|
if (const job_t *j = parser.job_get_from_pid(jobspec.pid)) {
|
2021-05-19 18:29:03 +00:00
|
|
|
return format_string(_(L"exit handler for job %d, '%ls'"), j->job_id(),
|
|
|
|
j->command_wcstr());
|
2016-04-29 02:19:50 +00:00
|
|
|
} else {
|
2021-05-20 18:25:32 +00:00
|
|
|
return format_string(_(L"exit handler for job with pid %d"), jobspec.pid);
|
2012-11-19 08:31:03 +00:00
|
|
|
}
|
2016-04-29 02:19:50 +00:00
|
|
|
}
|
2019-02-23 09:04:05 +00:00
|
|
|
|
2020-02-09 00:08:26 +00:00
|
|
|
case event_type_t::caller_exit: {
|
|
|
|
return _(L"exit handler for command substitution caller");
|
2012-11-19 08:31:03 +00:00
|
|
|
}
|
2019-02-23 09:04:05 +00:00
|
|
|
|
2019-02-21 06:42:58 +00:00
|
|
|
case event_type_t::generic: {
|
2019-02-23 09:04:05 +00:00
|
|
|
return format_string(_(L"handler for generic event '%ls'"), ed.str_param1.c_str());
|
2016-04-29 02:19:50 +00:00
|
|
|
}
|
2019-05-05 10:09:25 +00:00
|
|
|
case event_type_t::any: {
|
|
|
|
DIE("Unreachable");
|
|
|
|
}
|
2019-02-23 09:04:05 +00:00
|
|
|
default:
|
2019-03-04 23:27:56 +00:00
|
|
|
DIE("Unknown event type");
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2006-02-01 15:49:11 +00:00
|
|
|
}
|
|
|
|
|
2019-02-23 09:04:05 +00:00
|
|
|
void event_add_handler(std::shared_ptr<event_handler_t> eh) {
|
|
|
|
if (eh->desc.type == event_type_t::signal) {
|
2019-06-28 17:33:03 +00:00
|
|
|
signal_handle(eh->desc.param1.signal);
|
2019-02-23 09:04:05 +00:00
|
|
|
set_signal_observed(eh->desc.param1.signal, true);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2005-10-05 22:37:08 +00:00
|
|
|
|
2019-06-03 19:33:10 +00:00
|
|
|
s_event_handlers.acquire()->push_back(std::move(eh));
|
2005-10-05 22:37:08 +00:00
|
|
|
}
|
|
|
|
|
2019-02-23 09:04:05 +00:00
|
|
|
void event_remove_function_handlers(const wcstring &name) {
|
2019-06-03 19:33:10 +00:00
|
|
|
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);
|
2005-10-05 22:37:08 +00:00
|
|
|
}
|
|
|
|
|
2019-02-23 09:04:05 +00:00
|
|
|
event_handler_list_t event_get_function_handlers(const wcstring &name) {
|
2019-06-03 19:33:10 +00:00
|
|
|
auto handlers = s_event_handlers.acquire();
|
2019-02-23 09:04:05 +00:00
|
|
|
event_handler_list_t result;
|
2019-06-03 19:33:10 +00:00
|
|
|
for (const shared_ptr<event_handler_t> &eh : *handlers) {
|
2019-02-23 09:04:05 +00:00
|
|
|
if (eh->function_name == name) {
|
|
|
|
result.push_back(eh);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
2019-02-23 09:04:05 +00:00
|
|
|
return result;
|
2005-10-05 22:37:08 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 02:19:50 +00:00
|
|
|
bool event_is_signal_observed(int sig) {
|
|
|
|
// We are in a signal handler! Don't allocate memory, etc.
|
2014-12-29 08:34:36 +00:00
|
|
|
bool result = false;
|
2019-11-19 01:08:16 +00:00
|
|
|
if (sig >= 0 && static_cast<unsigned long>(sig) <
|
|
|
|
sizeof(s_observed_signals) / sizeof(*s_observed_signals)) {
|
2014-12-29 08:34:36 +00:00
|
|
|
result = s_observed_signals[sig];
|
2012-06-04 21:20:01 +00:00
|
|
|
}
|
2014-12-29 08:34:36 +00:00
|
|
|
return result;
|
2012-06-04 21:20:01 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 02:19:50 +00:00
|
|
|
/// 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.
|
2019-06-03 09:31:13 +00:00
|
|
|
static void event_fire_internal(parser_t &parser, const event_t &event) {
|
|
|
|
auto &ld = parser.libdata();
|
2019-05-13 01:02:57 +00:00
|
|
|
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};
|
2019-02-21 03:36:29 +00:00
|
|
|
|
2019-10-19 01:08:22 +00:00
|
|
|
// Suppress fish_trace during events.
|
|
|
|
scoped_push<bool> suppress_trace{&ld.suppress_fish_trace, true};
|
|
|
|
|
2019-02-23 09:04:05 +00:00
|
|
|
// Capture the event handlers that match this event.
|
2021-03-06 04:32:57 +00:00
|
|
|
struct firing_handler_t {
|
|
|
|
std::shared_ptr<event_handler_t> handler;
|
|
|
|
bool delete_after_call;
|
|
|
|
};
|
|
|
|
std::vector<firing_handler_t> fire;
|
|
|
|
{
|
|
|
|
for (const auto &handler : *s_event_handlers.acquire()) {
|
|
|
|
// Check if this event is a match.
|
|
|
|
bool only_once = false;
|
|
|
|
if (!handler_matches(*handler, event, only_once)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the nature of the event means it can't be fired more than once, deregister the
|
|
|
|
// event. This also works around a bug where jobs run without job control (no separate
|
|
|
|
// pgrp) cause handlers to run for each subsequent job started without job control
|
|
|
|
// (#7721). We can't erase it here because we check if the event is still extant before
|
|
|
|
// actually calling it below, so we instead push it along with its "delete after
|
|
|
|
// calling" value.
|
|
|
|
fire.push_back(firing_handler_t{handler, only_once});
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
|
|
|
|
2019-02-23 09:04:05 +00:00
|
|
|
// Iterate over our list of matching events. Fire the ones that are still present.
|
2021-03-06 04:32:57 +00:00
|
|
|
for (const auto &firing_event : fire) {
|
|
|
|
auto &handler = firing_event.handler;
|
2019-06-03 19:33:10 +00:00
|
|
|
// 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?
|
2021-03-06 04:40:06 +00:00
|
|
|
{
|
|
|
|
auto event_handlers = s_event_handlers.acquire();
|
|
|
|
if (!contains(*event_handlers, handler)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the event before firing it so we don't have to lock and unlock the event
|
|
|
|
// handlers list when handing control off to the handler.
|
|
|
|
if (firing_event.delete_after_call) {
|
|
|
|
FLOGF(event, L"Pruning handler '%ls' before firing", event.desc.str_param1.c_str());
|
|
|
|
for (auto event_handler = event_handlers->begin();
|
|
|
|
event_handler != event_handlers->end(); ++event_handler) {
|
|
|
|
if (event_handler->get() == firing_event.handler.get()) {
|
|
|
|
event_handlers->erase(event_handler);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-22 00:48:07 +00:00
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2019-02-23 09:04:05 +00:00
|
|
|
// 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
|
|
|
}
|
2005-10-11 19:23:43 +00:00
|
|
|
|
2016-04-29 02:19:50 +00:00
|
|
|
// Event handlers are not part of the main flow of code, so they are marked as
|
|
|
|
// non-interactive.
|
2019-05-27 21:52:48 +00:00
|
|
|
scoped_push<bool> interactive{&ld.is_interactive, false};
|
2019-05-12 21:00:44 +00:00
|
|
|
auto prev_statuses = parser.get_last_statuses();
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2020-10-06 14:53:25 +00:00
|
|
|
FLOGF(event, L"Firing event '%ls'", event.desc.str_param1.c_str());
|
2019-05-19 21:40:06 +00:00
|
|
|
block_t *b = parser.push_block(block_t::event_block(event));
|
2019-12-23 00:27:03 +00:00
|
|
|
parser.eval(buffer, io_chain_t());
|
2017-01-21 23:35:35 +00:00
|
|
|
parser.pop_block(b);
|
2019-05-12 21:00:44 +00:00
|
|
|
parser.set_last_statuses(std::move(prev_statuses));
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2005-10-05 22:37:08 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 02:19:50 +00:00
|
|
|
/// Handle all pending signal events.
|
2019-06-03 09:31:13 +00:00
|
|
|
void event_fire_delayed(parser_t &parser) {
|
|
|
|
auto &ld = parser.libdata();
|
2019-02-21 03:36:29 +00:00
|
|
|
// Do not invoke new event handlers from within event handlers.
|
2019-06-03 09:31:13 +00:00
|
|
|
if (ld.is_event) return;
|
2020-03-01 21:27:34 +00:00
|
|
|
// Do not invoke new event handlers if we are unwinding (#6649).
|
2020-07-12 18:35:27 +00:00
|
|
|
if (signal_check_cancel()) return;
|
2019-02-21 03:36:29 +00:00
|
|
|
|
2020-07-19 19:03:10 +00:00
|
|
|
std::vector<shared_ptr<const event_t>> to_send;
|
2019-06-03 09:31:13 +00:00
|
|
|
to_send.swap(ld.blocked_events);
|
|
|
|
assert(ld.blocked_events.empty());
|
2019-02-21 03:36:29 +00:00
|
|
|
|
|
|
|
// Append all signal events to to_send.
|
|
|
|
auto signals = s_pending_signals.acquire_pending();
|
|
|
|
if (signals.any()) {
|
2019-05-05 10:09:25 +00:00
|
|
|
for (uint32_t sig = 0; sig < signals.size(); sig++) {
|
2019-02-21 03:36:29 +00:00
|
|
|
if (signals.test(sig)) {
|
2021-04-14 15:17:22 +00:00
|
|
|
// HACK: The only variables we change in response to a *signal*
|
|
|
|
// are $COLUMNS and $LINES.
|
|
|
|
// Do that now.
|
|
|
|
if (sig == SIGWINCH) {
|
|
|
|
(void)termsize_container_t::shared().updating(parser);
|
|
|
|
}
|
2019-02-21 06:42:58 +00:00
|
|
|
auto e = std::make_shared<event_t>(event_type_t::signal);
|
2019-02-23 09:04:05 +00:00
|
|
|
e->desc.param1.signal = sig;
|
2019-02-21 03:36:29 +00:00
|
|
|
e->arguments.push_back(sig2wcs(sig));
|
|
|
|
to_send.push_back(std::move(e));
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2019-02-21 03:36:29 +00:00
|
|
|
// Fire or re-block all events.
|
|
|
|
for (const auto &evt : to_send) {
|
2019-06-03 09:31:13 +00:00
|
|
|
if (event_is_blocked(parser, *evt)) {
|
|
|
|
ld.blocked_events.push_back(evt);
|
2019-02-21 03:36:29 +00:00
|
|
|
} else {
|
2019-06-03 09:31:13 +00:00
|
|
|
event_fire_internal(parser, *evt);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
}
|
2005-10-05 22:37:08 +00:00
|
|
|
}
|
|
|
|
|
2019-02-21 03:36:29 +00:00
|
|
|
void event_enqueue_signal(int signal) {
|
|
|
|
// Beware, we are in a signal handler
|
|
|
|
s_pending_signals.mark(signal);
|
2012-06-04 21:20:01 +00:00
|
|
|
}
|
|
|
|
|
2019-06-03 09:31:13 +00:00
|
|
|
void event_fire(parser_t &parser, const event_t &event) {
|
2019-02-21 03:36:29 +00:00
|
|
|
// Fire events triggered by signals.
|
2019-06-03 09:31:13 +00:00
|
|
|
event_fire_delayed(parser);
|
2019-02-21 03:36:29 +00:00
|
|
|
|
2019-06-03 09:31:13 +00:00
|
|
|
if (event_is_blocked(parser, event)) {
|
|
|
|
parser.libdata().blocked_events.push_back(std::make_shared<event_t>(event));
|
2019-02-23 09:04:05 +00:00
|
|
|
} else {
|
2019-06-03 09:31:13 +00:00
|
|
|
event_fire_internal(parser, event);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2005-10-05 22:37:08 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 18:29:03 +00:00
|
|
|
static const wchar_t *event_name_for_type(event_type_t type) {
|
|
|
|
switch (type) {
|
|
|
|
case event_type_t::any:
|
|
|
|
return L"any";
|
|
|
|
case event_type_t::signal:
|
|
|
|
return L"signal";
|
|
|
|
case event_type_t::variable:
|
|
|
|
return L"variable";
|
|
|
|
case event_type_t::process_exit:
|
|
|
|
return L"process-exit";
|
|
|
|
case event_type_t::job_exit:
|
|
|
|
return L"job-exit";
|
|
|
|
case event_type_t::caller_exit:
|
|
|
|
return L"caller-exit";
|
|
|
|
case event_type_t::generic:
|
|
|
|
return L"generic";
|
2018-03-10 10:08:33 +00:00
|
|
|
}
|
2021-05-19 18:29:03 +00:00
|
|
|
return L"";
|
2018-01-31 17:47:23 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 18:29:03 +00:00
|
|
|
const wchar_t *const event_filter_names[] = {L"signal", L"variable", L"exit",
|
|
|
|
L"process-exit", L"job-exit", L"caller-exit",
|
|
|
|
L"generic", nullptr};
|
|
|
|
|
|
|
|
static bool filter_matches_event(const wcstring &filter, event_type_t type) {
|
|
|
|
if (filter.empty()) return true;
|
|
|
|
switch (type) {
|
|
|
|
case event_type_t::any:
|
|
|
|
return false;
|
|
|
|
case event_type_t::signal:
|
|
|
|
return filter == L"signal";
|
|
|
|
case event_type_t::variable:
|
|
|
|
return filter == L"variable";
|
|
|
|
case event_type_t::process_exit:
|
|
|
|
return filter == L"process-exit" || filter == L"exit";
|
|
|
|
case event_type_t::job_exit:
|
|
|
|
return filter == L"job-exit" || filter == L"exit";
|
|
|
|
case event_type_t::caller_exit:
|
|
|
|
return filter == L"process-exit" || filter == L"exit";
|
|
|
|
case event_type_t::generic:
|
|
|
|
return filter == L"generic";
|
2018-01-31 17:47:23 +00:00
|
|
|
}
|
2021-05-19 18:29:03 +00:00
|
|
|
DIE("Unreachable");
|
2018-01-31 17:47:23 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 18:29:03 +00:00
|
|
|
void event_print(io_streams_t &streams, const wcstring &type_filter) {
|
2019-06-03 19:33:10 +00:00
|
|
|
event_handler_list_t tmp = *s_event_handlers.acquire();
|
2018-01-31 17:47:23 +00:00
|
|
|
std::sort(tmp.begin(), tmp.end(),
|
2019-02-23 09:04:05 +00:00
|
|
|
[](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;
|
2021-05-19 18:29:03 +00:00
|
|
|
case event_type_t::process_exit:
|
2019-02-23 09:04:05 +00:00
|
|
|
return d1.param1.pid < d2.param1.pid;
|
2021-05-19 18:29:03 +00:00
|
|
|
case event_type_t::job_exit:
|
2021-05-20 18:25:32 +00:00
|
|
|
return d1.param1.jobspec.pid < d2.param1.jobspec.pid;
|
2020-02-09 00:08:26 +00:00
|
|
|
case event_type_t::caller_exit:
|
2020-02-08 23:43:21 +00:00
|
|
|
return d1.param1.caller_id < d2.param1.caller_id;
|
2019-02-23 09:04:05 +00:00
|
|
|
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");
|
2019-02-23 09:04:05 +00:00
|
|
|
});
|
2018-01-31 17:47:23 +00:00
|
|
|
|
2018-03-10 10:08:33 +00:00
|
|
|
maybe_t<event_type_t> last_type{};
|
2019-02-23 09:04:05 +00:00
|
|
|
for (const shared_ptr<event_handler_t> &evt : tmp) {
|
2018-03-10 10:08:33 +00:00
|
|
|
// If we have a filter, skip events that don't match.
|
2021-05-19 18:29:03 +00:00
|
|
|
if (!filter_matches_event(type_filter, evt->desc.type)) {
|
2018-03-10 10:08:33 +00:00
|
|
|
continue;
|
|
|
|
}
|
2018-01-31 17:47:23 +00:00
|
|
|
|
2019-02-23 09:04:05 +00:00
|
|
|
if (!last_type || *last_type != evt->desc.type) {
|
2019-05-05 10:09:25 +00:00
|
|
|
if (last_type) streams.out.append(L"\n");
|
2020-09-08 20:33:44 +00:00
|
|
|
last_type = evt->desc.type;
|
2018-03-10 10:08:33 +00:00
|
|
|
streams.out.append_format(L"Event %ls\n", event_name_for_type(*last_type));
|
|
|
|
}
|
2019-02-23 09:04:05 +00:00
|
|
|
switch (evt->desc.type) {
|
2019-02-21 06:42:58 +00:00
|
|
|
case event_type_t::signal:
|
2019-02-23 09:04:05 +00:00
|
|
|
streams.out.append_format(L"%ls %ls\n", sig2wcs(evt->desc.param1.signal),
|
|
|
|
evt->function_name.c_str());
|
2018-03-10 10:08:33 +00:00
|
|
|
break;
|
2021-05-19 18:29:03 +00:00
|
|
|
case event_type_t::process_exit:
|
|
|
|
case event_type_t::job_exit:
|
2020-02-09 00:08:26 +00:00
|
|
|
break;
|
|
|
|
case event_type_t::caller_exit:
|
|
|
|
streams.out.append_format(L"caller-exit %ls\n", evt->function_name.c_str());
|
2018-03-10 10:08:33 +00:00
|
|
|
break;
|
2019-02-21 06:42:58 +00:00
|
|
|
case event_type_t::variable:
|
|
|
|
case event_type_t::generic:
|
2019-02-23 09:04:05 +00:00
|
|
|
streams.out.append_format(L"%ls %ls\n", evt->desc.str_param1.c_str(),
|
|
|
|
evt->function_name.c_str());
|
2018-03-10 10:08:33 +00:00
|
|
|
break;
|
2019-05-05 10:09:25 +00:00
|
|
|
case event_type_t::any:
|
|
|
|
DIE("Unreachable");
|
2018-03-10 10:08:33 +00:00
|
|
|
default:
|
|
|
|
streams.out.append_format(L"%ls\n", evt->function_name.c_str());
|
|
|
|
break;
|
2018-01-31 17:47:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-03 09:31:13 +00:00
|
|
|
void event_fire_generic(parser_t &parser, const wchar_t *name, const wcstring_list_t *args) {
|
2019-05-28 00:24:19 +00:00
|
|
|
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);
|
2019-02-23 09:04:05 +00:00
|
|
|
ev.desc.str_param1 = name;
|
2016-04-29 02:19:50 +00:00
|
|
|
if (args) ev.arguments = *args;
|
2019-06-03 09:31:13 +00:00
|
|
|
event_fire(parser, ev);
|
2007-08-19 16:42:30 +00:00
|
|
|
}
|
|
|
|
|
2019-02-23 09:04:05 +00:00
|
|
|
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;
|
|
|
|
}
|
2007-08-19 16:42:30 +00:00
|
|
|
|
2019-02-23 09:04:05 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-02-23 09:04:05 +00:00
|
|
|
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;
|
|
|
|
}
|
2019-02-23 09:04:05 +00:00
|
|
|
|
2021-05-17 22:33:33 +00:00
|
|
|
// static
|
2019-02-23 09:04:05 +00:00
|
|
|
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;
|
|
|
|
}
|
2021-05-17 22:33:33 +00:00
|
|
|
|
|
|
|
// static
|
|
|
|
event_t event_t::process_exit(pid_t pid, int status) {
|
2021-05-19 18:29:03 +00:00
|
|
|
event_t evt{event_type_t::process_exit};
|
2021-05-17 22:33:33 +00:00
|
|
|
evt.desc.param1.pid = pid;
|
|
|
|
evt.arguments.reserve(3);
|
|
|
|
evt.arguments.push_back(L"PROCESS_EXIT");
|
|
|
|
evt.arguments.push_back(to_string(pid));
|
|
|
|
evt.arguments.push_back(to_string(status));
|
|
|
|
return evt;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2021-05-20 18:25:32 +00:00
|
|
|
event_t event_t::job_exit(pid_t pid, internal_job_id_t jid) {
|
2021-05-19 18:29:03 +00:00
|
|
|
event_t evt{event_type_t::job_exit};
|
2021-05-20 18:25:32 +00:00
|
|
|
evt.desc.param1.jobspec = {pid, jid};
|
2021-05-17 22:33:33 +00:00
|
|
|
evt.arguments.reserve(3);
|
|
|
|
evt.arguments.push_back(L"JOB_EXIT");
|
2021-05-20 18:25:32 +00:00
|
|
|
evt.arguments.push_back(to_string(pid));
|
2021-05-17 22:33:33 +00:00
|
|
|
evt.arguments.push_back(L"0"); // historical
|
|
|
|
return evt;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
event_t event_t::caller_exit(uint64_t caller_id, int job_id) {
|
|
|
|
event_t evt{event_type_t::caller_exit};
|
|
|
|
evt.desc.param1.caller_id = caller_id;
|
|
|
|
evt.arguments.reserve(3);
|
|
|
|
evt.arguments.push_back(L"JOB_EXIT");
|
|
|
|
evt.arguments.push_back(to_string(job_id));
|
|
|
|
evt.arguments.push_back(L"0"); // historical
|
|
|
|
return evt;
|
|
|
|
}
|