2016-04-29 02:19:50 +00:00
|
|
|
// Functions for handling event triggers
|
|
|
|
//
|
|
|
|
// Because most of these functions can be called by signal handler, it is important to make it well
|
|
|
|
// defined when these functions produce output or perform memory allocations, since such functions
|
|
|
|
// may not be safely called by signal handlers.
|
2005-10-05 22:37:08 +00:00
|
|
|
#ifndef FISH_EVENT_H
|
|
|
|
#define FISH_EVENT_H
|
|
|
|
|
2016-04-21 06:00:54 +00:00
|
|
|
#include <unistd.h>
|
2017-02-14 04:37:27 +00:00
|
|
|
|
2022-08-21 06:14:48 +00:00
|
|
|
#include <cstdint>
|
2017-01-22 00:48:07 +00:00
|
|
|
#include <memory>
|
2022-08-21 06:14:48 +00:00
|
|
|
#include <utility>
|
2015-07-25 15:14:25 +00:00
|
|
|
#include <vector>
|
2012-02-10 15:55:06 +00:00
|
|
|
|
2011-12-27 08:06:07 +00:00
|
|
|
#include "common.h"
|
2022-08-21 06:14:48 +00:00
|
|
|
#include "global_safety.h"
|
|
|
|
|
|
|
|
struct io_streams_t;
|
2005-10-05 22:37:08 +00:00
|
|
|
|
2016-04-29 02:19:50 +00:00
|
|
|
/// The process id that is used to match any process id.
|
2022-09-20 18:58:37 +00:00
|
|
|
#define EVENT_ANY_PID 0
|
2005-10-05 22:37:08 +00:00
|
|
|
|
2016-04-29 02:19:50 +00:00
|
|
|
/// Enumeration of event types.
|
2019-02-21 06:42:58 +00:00
|
|
|
enum class event_type_t {
|
2016-04-29 02:19:50 +00:00
|
|
|
/// Matches any event type (Not always any event, as the function name may limit the choice as
|
|
|
|
/// well.
|
2019-02-21 06:42:58 +00:00
|
|
|
any,
|
2016-04-29 02:19:50 +00:00
|
|
|
/// An event triggered by a signal.
|
2019-02-21 06:42:58 +00:00
|
|
|
signal,
|
2016-04-29 02:19:50 +00:00
|
|
|
/// An event triggered by a variable update.
|
2019-02-21 06:42:58 +00:00
|
|
|
variable,
|
2021-05-19 18:29:03 +00:00
|
|
|
/// An event triggered by a process exit.
|
|
|
|
process_exit,
|
|
|
|
/// An event triggered by a job exit.
|
|
|
|
job_exit,
|
2020-02-09 00:08:26 +00:00
|
|
|
/// An event triggered by a job exit, triggering the 'caller'-style events only.
|
|
|
|
caller_exit,
|
2016-04-29 02:19:50 +00:00
|
|
|
/// A generic event.
|
2019-02-21 06:42:58 +00:00
|
|
|
generic,
|
2016-04-29 02:19:50 +00:00
|
|
|
};
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2021-05-19 18:29:03 +00:00
|
|
|
/// Null-terminated list of valid event filter names.
|
|
|
|
/// These are what are valid to pass to 'functions --handlers-type'
|
|
|
|
extern const wchar_t *const event_filter_names[];
|
|
|
|
|
2019-02-23 09:04:05 +00:00
|
|
|
/// Properties of an event.
|
|
|
|
struct event_description_t {
|
2021-05-20 18:25:32 +00:00
|
|
|
/// Helper type for on-job-exit events.
|
|
|
|
struct job_spec_t {
|
|
|
|
// pid requested by the event, or ANY_PID for all.
|
|
|
|
pid_t pid;
|
|
|
|
|
|
|
|
// internal_job_id of the job to match.
|
|
|
|
// If this is 0, we match either all jobs (pid == ANY_PID) or no jobs (otherwise).
|
|
|
|
uint64_t internal_job_id;
|
|
|
|
};
|
|
|
|
|
2019-02-23 09:04:05 +00:00
|
|
|
/// The event type.
|
2019-02-21 06:42:58 +00:00
|
|
|
event_type_t type;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-29 02:19:50 +00:00
|
|
|
/// The type-specific parameter. The int types are one of the following:
|
|
|
|
///
|
|
|
|
/// signal: Signal number for signal-type events.Use EVENT_ANY_SIGNAL to match any signal
|
2021-05-19 18:29:03 +00:00
|
|
|
/// pid: Process id for process-type events. Use EVENT_ANY_PID to match any pid.
|
2021-05-20 18:25:32 +00:00
|
|
|
/// jobspec: Info for on-job-exit events.
|
2020-02-08 23:43:21 +00:00
|
|
|
/// caller_id: Internal job id for caller_exit type events
|
2016-04-29 02:19:50 +00:00
|
|
|
union {
|
2012-02-09 03:02:25 +00:00
|
|
|
int signal;
|
|
|
|
pid_t pid;
|
2021-05-20 18:25:32 +00:00
|
|
|
job_spec_t jobspec;
|
2021-05-19 18:29:03 +00:00
|
|
|
uint64_t caller_id;
|
2019-02-23 09:04:05 +00:00
|
|
|
} param1{};
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-29 02:19:50 +00:00
|
|
|
/// The string types are one of the following:
|
|
|
|
///
|
|
|
|
/// variable: Variable name for variable-type events.
|
|
|
|
/// param: The parameter describing this generic event.
|
2019-02-23 09:04:05 +00:00
|
|
|
wcstring str_param1{};
|
2005-10-05 22:37:08 +00:00
|
|
|
|
2019-02-23 09:04:05 +00:00
|
|
|
explicit event_description_t(event_type_t t) : type(t) {}
|
|
|
|
static event_description_t signal(int sig);
|
|
|
|
static event_description_t variable(wcstring str);
|
|
|
|
static event_description_t generic(wcstring str);
|
|
|
|
};
|
2005-12-11 22:21:01 +00:00
|
|
|
|
2019-02-23 09:04:05 +00:00
|
|
|
/// Represents a handler for an event.
|
|
|
|
struct event_handler_t {
|
|
|
|
/// Properties of the event to match.
|
2022-06-06 19:09:04 +00:00
|
|
|
const event_description_t desc;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2019-02-23 09:04:05 +00:00
|
|
|
/// Name of the function to invoke.
|
2022-06-06 19:09:04 +00:00
|
|
|
const wcstring function_name{};
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2022-05-08 23:42:51 +00:00
|
|
|
/// A flag set when an event handler is removed from the global list.
|
|
|
|
/// Once set, this is never cleared.
|
2022-06-06 19:09:04 +00:00
|
|
|
relaxed_atomic_bool_t removed{false};
|
|
|
|
|
|
|
|
/// A flag set when an event handler is first fired.
|
|
|
|
relaxed_atomic_bool_t fired{false};
|
2022-05-08 23:42:51 +00:00
|
|
|
|
|
|
|
explicit event_handler_t(event_type_t t) : desc(std::move(t)) {}
|
|
|
|
|
2019-02-23 09:04:05 +00:00
|
|
|
event_handler_t(event_description_t d, wcstring name)
|
2020-03-13 20:45:41 +00:00
|
|
|
: desc(std::move(d)), function_name(std::move(name)) {}
|
2019-02-23 09:04:05 +00:00
|
|
|
};
|
|
|
|
using event_handler_list_t = std::vector<std::shared_ptr<event_handler_t>>;
|
|
|
|
|
|
|
|
/// Represents a event that is fired, or capable of being fired.
|
|
|
|
struct event_t {
|
|
|
|
/// Properties of the event.
|
|
|
|
event_description_t desc;
|
|
|
|
|
|
|
|
/// Arguments to any handler.
|
|
|
|
wcstring_list_t arguments{};
|
|
|
|
|
2021-05-17 22:33:33 +00:00
|
|
|
explicit event_t(event_type_t t) : desc(t) {}
|
2019-02-23 09:04:05 +00:00
|
|
|
|
2021-10-26 15:03:45 +00:00
|
|
|
/// Create an event_type_t::variable event with the args for erasing a variable.
|
|
|
|
static event_t variable_erase(wcstring name);
|
|
|
|
/// Create an event_type_t::variable event with the args for setting a variable.
|
|
|
|
static event_t variable_set(wcstring name);
|
2021-05-17 22:33:33 +00:00
|
|
|
|
|
|
|
/// Create a PROCESS_EXIT event.
|
|
|
|
static event_t process_exit(pid_t pid, int status);
|
|
|
|
|
2021-05-19 18:29:03 +00:00
|
|
|
/// Create a JOB_EXIT event. The pgid should be positive.
|
2021-05-17 22:33:33 +00:00
|
|
|
/// The reported status is always 0 for historical reasons.
|
2021-05-20 18:25:32 +00:00
|
|
|
static event_t job_exit(pid_t pgid, internal_job_id_t jid);
|
2021-05-17 22:33:33 +00:00
|
|
|
|
|
|
|
/// Create a caller_exit event.
|
|
|
|
static event_t caller_exit(uint64_t internal_job_id, int job_id);
|
2012-02-08 10:34:31 +00:00
|
|
|
};
|
2005-10-05 22:37:08 +00:00
|
|
|
|
2019-06-03 09:31:13 +00:00
|
|
|
class parser_t;
|
|
|
|
|
2016-04-29 02:19:50 +00:00
|
|
|
/// Add an event handler.
|
2019-02-23 09:04:05 +00:00
|
|
|
void event_add_handler(std::shared_ptr<event_handler_t> eh);
|
|
|
|
|
|
|
|
/// Remove all events for the given function name.
|
|
|
|
void event_remove_function_handlers(const wcstring &name);
|
|
|
|
|
|
|
|
/// Return all event handlers for the given function.
|
|
|
|
event_handler_list_t event_get_function_handlers(const wcstring &name);
|
2005-10-05 22:37:08 +00:00
|
|
|
|
2016-04-29 02:19:50 +00:00
|
|
|
/// Returns whether an event listener is registered for the given signal. This is safe to call from
|
|
|
|
/// a signal handler.
|
2012-06-04 21:20:01 +00:00
|
|
|
bool event_is_signal_observed(int signal);
|
|
|
|
|
2019-06-03 09:31:13 +00:00
|
|
|
/// Fire the specified event \p event, executing it on \p parser.
|
|
|
|
void event_fire(parser_t &parser, const event_t &event);
|
2019-02-23 09:04:05 +00:00
|
|
|
|
2019-06-03 09:31:13 +00:00
|
|
|
/// Fire all delayed events attached to the given parser.
|
|
|
|
void event_fire_delayed(parser_t &parser);
|
2005-10-05 22:37:08 +00:00
|
|
|
|
2019-02-21 03:36:29 +00:00
|
|
|
/// Enqueue a signal event. Invoked from a signal handler.
|
|
|
|
void event_enqueue_signal(int signal);
|
2012-06-04 21:20:01 +00:00
|
|
|
|
2021-05-19 18:29:03 +00:00
|
|
|
/// Print all events. If type_filter is not empty, only output events with that type.
|
|
|
|
void event_print(io_streams_t &streams, const wcstring &type_filter);
|
2018-01-31 17:47:23 +00:00
|
|
|
|
2016-04-29 02:19:50 +00:00
|
|
|
/// Returns a string describing the specified event.
|
2020-02-08 20:39:03 +00:00
|
|
|
wcstring event_get_desc(const parser_t &parser, const event_t &e);
|
2012-12-20 10:48:36 +00:00
|
|
|
|
2016-04-29 02:19:50 +00:00
|
|
|
/// Fire a generic event with the specified name.
|
2022-05-09 00:07:34 +00:00
|
|
|
void event_fire_generic(parser_t &parser, wcstring name, wcstring_list_t args = {});
|
2007-08-19 16:42:30 +00:00
|
|
|
|
2005-10-05 22:37:08 +00:00
|
|
|
#endif
|