aboutsummaryrefslogtreecommitdiff
path: root/samples/gpio-pci-idio-16.c
diff options
context:
space:
mode:
authorThanos Makatos <thanos.makatos@nutanix.com>2020-03-25 07:50:04 -0400
committerThanos Makatos <thanos.makatos@nutanix.com>2020-03-25 10:36:29 -0400
commit593d402da41135be31ce9fd1e11480ebe2b7a8de (patch)
tree3297a673894d74219b2bc0114a3fc86a8b31afb3 /samples/gpio-pci-idio-16.c
parent403c0be53cfd0ad42464a38b5b28496ba0479e0e (diff)
downloadlibvfio-user-593d402da41135be31ce9fd1e11480ebe2b7a8de.zip
libvfio-user-593d402da41135be31ce9fd1e11480ebe2b7a8de.tar.gz
libvfio-user-593d402da41135be31ce9fd1e11480ebe2b7a8de.tar.bz2
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 <thanos.makatos@nutanix.com>
Diffstat (limited to 'samples/gpio-pci-idio-16.c')
-rw-r--r--samples/gpio-pci-idio-16.c28
1 files changed, 26 insertions, 2 deletions
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 <err.h>
#include <stdlib.h>
#include <unistd.h>
+#include <string.h>
+#include <signal.h>
+#include <errno.h>
#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;
}