diff options
author | Jo-Philipp Wich <jo@mein.io> | 2023-07-11 13:30:51 +0200 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2023-07-27 13:59:32 +0200 |
commit | 29b1c0deba8487d1d7ad86242a1a5e90aa775218 (patch) | |
tree | 984a28dea6f2fd88a112f7d49709c32c0a8fa858 /include | |
parent | bb5eba4db8895d862038825b84d5b7a94ee5cbb0 (diff) |
vm: introduce basic signal handling infrastructure
Introduce the basic facilities to implement UNIX process signal handling
in ucode. The VM structure is extended by a bitmap keeping track of
received signal numbers, a sigaction structure which holds a generic
signal handler to update the bitmap, a pipe where the generic signal
handler will send received signal numbers to and an array of managed
signal handler functions, indexed by signal number.
Additionally, three new C API functions are added to control signal
delivery in the VM instance:
- `uc_vm_signal_dispatch()`
This function invokes signal handler callbacks for each received
signal number, clears the bitmap and empties the pipe. The VM itself
will invoke this function after each executed bytecode instruction.
In some cases however it is useful for C code driving the VM to
force immediate signal delivery, e.g. from within long running C
functions that do not return to ucode (to the next bytecode
instruction) in a timely manner.
- `uc_vm_signal_raise()`
This function will deliver the given signal number, so that it is
picked up by the next call to `uc_vm_signal_dispatch()`. It is used
by the generic C signal handler function to forward received signals
to the VM instance.
- `uc_vm_signal_notifyfd()`
This functions returns the read end of the internal signal pipe.
It is mainly useful for integration into select or poll based event
loops, in order to be notified when there's pending signals for
delivery into the VM instance.
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'include')
-rw-r--r-- | include/ucode/types.h | 8 | ||||
-rw-r--r-- | include/ucode/vm.h | 4 |
2 files changed, 12 insertions, 0 deletions
diff --git a/include/ucode/types.h b/include/ucode/types.h index 9827db5..641c469 100644 --- a/include/ucode/types.h +++ b/include/ucode/types.h @@ -20,6 +20,7 @@ #include <stdbool.h> #include <stdint.h> #include <regex.h> +#include <signal.h> #include <json-c/json.h> #include "util.h" @@ -230,6 +231,7 @@ typedef struct { bool raw_mode; uc_search_path_t module_search_path; uc_search_path_t force_dynlink_list; + bool setup_signal_handlers; } uc_parse_config_t; extern uc_parse_config_t uc_default_parse_config; @@ -311,6 +313,12 @@ struct uc_vm { uc_stringbuf_t *strbuf; uc_exception_handler_t *exhandler; FILE *output; + struct { + uint64_t raised[((NSIG + 63) & ~63) / 64]; + uc_value_t *handler; + struct sigaction sa; + int sigpipe[2]; + } signal; }; diff --git a/include/ucode/vm.h b/include/ucode/vm.h index 161f1ee..3cb6dc0 100644 --- a/include/ucode/vm.h +++ b/include/ucode/vm.h @@ -156,4 +156,8 @@ uc_vm_raise_exception(uc_vm_t *vm, uc_exception_type_t type, const char *fmt, .. uc_vm_status_t uc_vm_execute(uc_vm_t *vm, uc_program_t *fn, uc_value_t **retval); uc_value_t *uc_vm_invoke(uc_vm_t *vm, const char *fname, size_t nargs, ...); +uc_exception_type_t uc_vm_signal_dispatch(uc_vm_t *vm); +void uc_vm_signal_raise(uc_vm_t *vm, int signo); +int uc_vm_signal_notifyfd(uc_vm_t *vm); + #endif /* UCODE_VM_H */ |