From patchwork Fri Sep 25 12:51:38 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alex_Benn=C3=A9e?= X-Patchwork-Id: 1371277 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=gDSOYgSF; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4ByX633k61z9sWF for ; Fri, 25 Sep 2020 22:58:35 +1000 (AEST) Received: from localhost ([::1]:58078 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kLnJ3-0000SG-G8 for incoming@patchwork.ozlabs.org; Fri, 25 Sep 2020 08:58:33 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:48348) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kLnCu-0007qm-Eh for qemu-devel@nongnu.org; Fri, 25 Sep 2020 08:52:13 -0400 Received: from mail-wr1-x444.google.com ([2a00:1450:4864:20::444]:34820) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kLnCs-0001R5-EM for qemu-devel@nongnu.org; Fri, 25 Sep 2020 08:52:12 -0400 Received: by mail-wr1-x444.google.com with SMTP id e16so3518664wrm.2 for ; Fri, 25 Sep 2020 05:52:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=La3wf0jgdLLaxy3qyiZ5nLFWQDulF/WBD+D4LgHv5vo=; b=gDSOYgSFewYCKCeiBthgQfO9lDdu251R/GVGuV69QTfKwahNlWXaTyMUnKiZcCLRwZ J3We7jodc6NQ7jHC86IJpHHURJc45O+bvnnqI/MehiicHpvvWUHzonrCKxc14M1htnM/ ieM+uPWUdkfYbyHoEGQ0NqHWnjG2MmAr6amBossVDArsXaThqA69zcMqZjL29V/xRPkG kQHSWh0RGM5Ot/o7V6ISt5Uk//+IctUG0nsjkBfnJ8HUfN9mJvW0hGc0oHrppjAB3Gyr 4qJxETzaACuIVtVWHItKkcHuifPd/KG7nyqc8SFS3PCAQDKiG1gnlCzGoa6FpimLafIb 6DlQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=La3wf0jgdLLaxy3qyiZ5nLFWQDulF/WBD+D4LgHv5vo=; b=ebZ5YeV1lRoi66VlnEZ71ngjmuGPxV3rKcY7L9JYfagjjasow/bX/jANw85LfaVUmp sE4AkQnCzOSpGWOtknIC/GoreIXhU6b/SWwz8NrTBgxd680jX59T56v7voJ3GiPpKScB wCTrDrGmTTVFjm9uaDGk0bOyHTCdbH65I3GtOAM/fq8AWJBPLs5ehxGrTREQnzHvoIqf Rrh7hQNGmVWD0Hrlnp+mP+KGdHKg2Xjck6FzZmNYqCxzOcgrPrPj5CIVkG/W6cdlEZs7 tvAL7sdDHJzOIJtzFNpZZIKJiR3xNyo5WyROYQpIue+oRYArXS2a0xx5Oej+Y8PgYwsY FtDQ== X-Gm-Message-State: AOAM530WE1A75FXBMUm1cgdQqXnNXRA9HSp6TtkwAVJdrCF0wbPba5bu zRV+/3b0FfhrvSlK/75XYZDiYg== X-Google-Smtp-Source: ABdhPJw+FnAq0jAiugV2Cg4Tl3jIA4VQwRsQf9Hp7qNWdz8GCxcD2vXGq4F6xrTrZ0uReMSF0qA0HQ== X-Received: by 2002:adf:e292:: with SMTP id v18mr4351820wri.256.1601038329141; Fri, 25 Sep 2020 05:52:09 -0700 (PDT) Received: from zen.linaroharston ([51.148.130.216]) by smtp.gmail.com with ESMTPSA id i83sm2877331wma.22.2020.09.25.05.51.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Sep 2020 05:52:00 -0700 (PDT) Received: from zen.lan (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id 2C74D1FF99; Fri, 25 Sep 2020 13:51:49 +0100 (BST) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: qemu-devel@nongnu.org, maxim.uvarov@linaro.org, joakim.bech@linaro.org, ilias.apalodimas@linaro.org, tomas.winkler@intel.com, yang.huang@intel.com, bing.zhu@intel.com, Matti.Moell@opensynergy.com, hmo@opensynergy.com Subject: [RFC PATCH 10/19] tools/vhost-user-rpmb: handle shutdown and SIGINT/SIGHUP cleanly Date: Fri, 25 Sep 2020 13:51:38 +0100 Message-Id: <20200925125147.26943-11-alex.bennee@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200925125147.26943-1-alex.bennee@linaro.org> References: <20200925125147.26943-1-alex.bennee@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::444; envelope-from=alex.bennee@linaro.org; helo=mail-wr1-x444.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: jean-philippe@linaro.org, takahiro.akashi@linaro.org, virtualization@lists.linuxfoundation.org, =?utf-8?q?Alex_Benn=C3=A9e?= , arnd@linaro.org, stratos-dev@op-lists.linaro.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" 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 --- 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 #include #include +#include #include #include #include @@ -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); }