From 593d402da41135be31ce9fd1e11480ebe2b7a8de Mon Sep 17 00:00:00 2001 From: Thanos Makatos Date: Wed, 25 Mar 2020 07:50:04 -0400 Subject: trap SIGINT in gpio sample This allows system calls to return if a signal is sent (e.g. SIGINT) in order to clean uo properly. Signed-off-by: Thanos Makatos --- samples/gpio-pci-idio-16.c | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) (limited to 'samples/gpio-pci-idio-16.c') diff --git a/samples/gpio-pci-idio-16.c b/samples/gpio-pci-idio-16.c index bf35e49..04079af 100644 --- a/samples/gpio-pci-idio-16.c +++ b/samples/gpio-pci-idio-16.c @@ -37,6 +37,9 @@ #include #include #include +#include +#include +#include #include "../lib/muser.h" @@ -58,11 +61,17 @@ bar2_access(void *pvt, char * const buf, size_t count, loff_t offset, return count; } +static void _sa_handler(int signum __attribute__((unused))) +{ +} + int main(int argc, char *argv[]) { int ret; bool trans_sock = false, verbose = false; char opt; + struct sigaction act = {.sa_handler = _sa_handler}; + lm_ctx_t *lm_ctx; while ((opt = getopt(argc, argv, "sv")) != -1) { switch (opt) { @@ -98,10 +107,25 @@ int main(int argc, char *argv[]) .uuid = argv[optind], }; - ret = lm_ctx_run(&dev_info); - if (ret != 0) { + sigemptyset(&act.sa_mask); + if (sigaction(SIGINT, &act, NULL) == -1) { + fprintf(stderr, "warning: failed to register signal handler: %m\n"); + } + + lm_ctx = lm_ctx_create(&dev_info); + if (lm_ctx == NULL) { + if (errno == EINTR) { + goto out; + } + fprintf(stderr, "failed to initialize device emulation: %m\n"); + return -1; + } + ret = lm_ctx_drive(lm_ctx); + if (ret != 0 && errno != EINTR) { fprintf(stderr, "failed to realize device emulation: %m\n"); } +out: + lm_ctx_destroy(lm_ctx); return ret; } -- cgit v1.1