[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[RFC PATCH 10/19] tools/vhost-user-rpmb: handle shutdown and SIGINT/SIGH
From: |
Alex Bennée |
Subject: |
[RFC PATCH 10/19] tools/vhost-user-rpmb: handle shutdown and SIGINT/SIGHUP cleanly |
Date: |
Fri, 25 Sep 2020 13:51:38 +0100 |
The libvhost-user library will just exit if it handles the
VHOST_USER_NONE message and we want to ensure we have tidied up after
ourselves. As we need to signal the shutdown of the main loop we need
to move the information into the VuRmb state structure.
We also want to do the same if we catch a SIGINT/SIGHUP termination
signal. While we are at it add some instrumentation so we can follow
the program flow.
Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
---
tools/vhost-user-rpmb/main.c | 49 +++++++++++++++++++++++++++++++-----
1 file changed, 43 insertions(+), 6 deletions(-)
diff --git a/tools/vhost-user-rpmb/main.c b/tools/vhost-user-rpmb/main.c
index 1be0d4b8a567..7b3b29ccfc5b 100644
--- a/tools/vhost-user-rpmb/main.c
+++ b/tools/vhost-user-rpmb/main.c
@@ -12,6 +12,7 @@
#include <glib.h>
#include <gio/gio.h>
#include <gio/gunixsocketaddress.h>
+#include <glib-unix.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
@@ -73,6 +74,7 @@ struct virtio_rpmb_frame {
typedef struct VuRpmb {
VugDev dev;
struct virtio_rpmb_config virtio_config;
+ GMainLoop *loop;
} VuRpmb;
struct virtio_rpmb_ctrl_command {
@@ -158,10 +160,22 @@ vrpmb_queue_set_started(VuDev *dev, int qidx, bool
started)
}
}
-static int
-vrpmb_process_msg(VuDev *dev, VhostUserMsg *msg, int *do_reply)
+/*
+ * vrpmb_process_msg: process messages of vhost-user interface
+ *
+ * Any that are not handled here are processed by the libvhost library
+ * itself.
+ */
+static int vrpmb_process_msg(VuDev *dev, VhostUserMsg *msg, int *do_reply)
{
+ VuRpmb *r = container_of(dev, VuRpmb, dev.parent);
+
+ g_info("%s: msg %d", __func__, msg->request);
+
switch (msg->request) {
+ case VHOST_USER_NONE:
+ g_main_loop_quit(r->loop);
+ return 1;
default:
return 0;
}
@@ -181,6 +195,9 @@ static const VuDevIface vuiface = {
static void vrpmb_destroy(VuRpmb *r)
{
vug_deinit(&r->dev);
+ if (socket_path) {
+ unlink(socket_path);
+ }
}
/* Print vhost-user.json backend program capabilities */
@@ -191,11 +208,18 @@ static void print_capabilities(void)
printf("}\n");
}
+static gboolean hangup(gpointer user_data)
+{
+ GMainLoop *loop = (GMainLoop *) user_data;
+ g_info("%s: caught hangup/quit signal, quitting main loop", __func__);
+ g_main_loop_quit(loop);
+ return true;
+}
+
int main (int argc, char *argv[])
{
GError *error = NULL;
GOptionContext *context;
- g_autoptr(GMainLoop) loop = NULL;
g_autoptr(GSocket) socket = NULL;
VuRpmb rpmb = { };
@@ -262,15 +286,28 @@ int main (int argc, char *argv[])
}
}
+ /*
+ * Create the main loop first so all the various sources can be
+ * added. As well as catching signals we need to ensure vug_init
+ * can add it's GSource watches.
+ */
+
+ rpmb.loop = g_main_loop_new(NULL, FALSE);
+ /* catch exit signals */
+ g_unix_signal_add(SIGHUP, hangup, rpmb.loop);
+ g_unix_signal_add(SIGINT, hangup, rpmb.loop);
+
if (!vug_init(&rpmb.dev, VHOST_USER_RPMB_MAX_QUEUES,
g_socket_get_fd(socket),
vrpmb_panic, &vuiface)) {
g_printerr("Failed to initialize libvhost-user-glib.\n");
exit(EXIT_FAILURE);
}
- loop = g_main_loop_new(NULL, FALSE);
- g_main_loop_run(loop);
- g_main_loop_unref(loop);
+ g_message("entering main loop, awaiting messages");
+ g_main_loop_run(rpmb.loop);
+ g_message("finished main loop, cleaning up");
+
+ g_main_loop_unref(rpmb.loop);
vrpmb_destroy(&rpmb);
}
--
2.20.1
- [RFC PATCH 04/19] hw/block: add vhost-user-rpmb-pci boilerplate, (continued)
- [RFC PATCH 04/19] hw/block: add vhost-user-rpmb-pci boilerplate, Alex Bennée, 2020/09/25
- [RFC PATCH 05/19] virtio-pci: add notification trace points, Alex Bennée, 2020/09/25
- [RFC PATCH 03/19] hw/virtio: move virtio-pci.h into shared include space, Alex Bennée, 2020/09/25
- [RFC PATCH 06/19] tools/vhost-user-rpmb: add boilerplate and initial main, Alex Bennée, 2020/09/25
- [RFC PATCH 07/19] tools/vhost-user-rpmb: implement --print-capabilities, Alex Bennée, 2020/09/25
- [RFC PATCH 08/19] tools/vhost-user-rpmb: connect to fd and instantiate basic run loop, Alex Bennée, 2020/09/25
- [RFC PATCH 09/19] tools/vhost-user-rpmb: add a --verbose/debug flags for logging, Alex Bennée, 2020/09/25
- [RFC PATCH 11/19] tools/vhost-user-rpmb: add --flash-path for backing store, Alex Bennée, 2020/09/25
- [RFC PATCH 12/19] tools/vhost-user-rpmb: import hmac_sha256 functions, Alex Bennée, 2020/09/25
- [RFC PATCH 10/19] tools/vhost-user-rpmb: handle shutdown and SIGINT/SIGHUP cleanly,
Alex Bennée <=
- [RFC PATCH 17/19] tools/vhost-user-rpmb: add key persistence, Alex Bennée, 2020/09/25
- [RFC PATCH 15/19] tools/vhost-user-rpmb: implement VIRTIO_RPMB_REQ_DATA_WRITE, Alex Bennée, 2020/09/25
- [RFC PATCH 16/19] tools/vhost-user-rpmb: implement VIRTIO_RPMB_REQ_DATA_READ, Alex Bennée, 2020/09/25
- [RFC PATCH 18/19] tools/vhost-user-rpmb: allow setting of the write_count, Alex Bennée, 2020/09/25
- [RFC PATCH 13/19] tools/vhost-user-rpmb: implement the PROGRAM_KEY handshake, Alex Bennée, 2020/09/25
- [RFC PATCH 19/19] docs: add a man page for vhost-user-rpmb, Alex Bennée, 2020/09/25
- [RFC PATCH 14/19] tools/vhost-user-rpmb: implement VIRTIO_RPMB_REQ_GET_WRITE_COUNTER, Alex Bennée, 2020/09/25