From patchwork Wed Mar 27 18:13:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leigh Brown X-Patchwork-Id: 13607249 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 077CECD1288 for ; Wed, 27 Mar 2024 18:14:59 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.698690.1090715 (Exim 4.92) (envelope-from ) id 1rpXnS-0005Fc-Dx; Wed, 27 Mar 2024 18:14:46 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 698690.1090715; Wed, 27 Mar 2024 18:14:46 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rpXnS-0005EH-5N; Wed, 27 Mar 2024 18:14:46 +0000 Received: by outflank-mailman (input) for mailman id 698690; Wed, 27 Mar 2024 18:14:14 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rpXmw-0004q7-BP for xen-devel@lists.xenproject.org; Wed, 27 Mar 2024 18:14:14 +0000 Received: from doppler.solinno.uk (8.1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.9.a.d.0.6.f.d.1.0.b.8.0.1.0.0.2.ip6.arpa [2001:8b0:1df6:da9::18]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id cfdbe840-ec65-11ee-afe3-a90da7624cb6; Wed, 27 Mar 2024 19:14:12 +0100 (CET) Received: from folly.solinno.co.uk (folly.dyn.solinno.co.uk [192.168.2.135]) by doppler.solinno.uk (Postfix) with ESMTPSA id ADF358009E; Wed, 27 Mar 2024 18:14:11 +0000 (GMT) Received: by folly.solinno.co.uk (Postfix, from userid 1000) id 822C5202A6; Wed, 27 Mar 2024 18:14:11 +0000 (GMT) X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: cfdbe840-ec65-11ee-afe3-a90da7624cb6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=solinno.co.uk; s=mail; t=1711563251; bh=MMPqjL3hBPQ8fBBa6Taj4NhI2UexT0xGooHYTZT9+Zo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OD29/j6CgXPuqSW/8SMhaMdl//ep46BSk4GQ2rbKH8yxefhIuV+Z6BTmYaHYc3mLd 96BJiG0dRXZZZJTVG0alKblewuibIn4PTde+kWAmZA73MvEdt47rcKuVmiP/qmoPC3 CmNCLwxS75HZ3BAgxscgbH7RU89ROVQM1KqutUkk= From: leigh@solinno.co.uk To: xen-devel@lists.xenproject.org Cc: andrew.cooper3@citrix.com, anthony.perard@citrix.com, slack@rabbit.lu, Leigh Brown Subject: [PATCH 1/6] tools/misc: xenwatchdogd: use EXIT_* constants Date: Wed, 27 Mar 2024 18:13:48 +0000 Message-Id: <20240327181353.10951-2-leigh@solinno.co.uk> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240327181353.10951-1-leigh@solinno.co.uk> References: <55416d60-cae7-4e79-8bde-bc07ee9e3830@suse.com> <20240327181353.10951-1-leigh@solinno.co.uk> MIME-Version: 1.0 From: Leigh Brown Use EXIT_SUCCESS/EXIT_FAILURE constants instead of magic numbers. --- tools/misc/xenwatchdogd.c | 40 +++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/tools/misc/xenwatchdogd.c b/tools/misc/xenwatchdogd.c index 254117b554..2f7c822d61 100644 --- a/tools/misc/xenwatchdogd.c +++ b/tools/misc/xenwatchdogd.c @@ -17,37 +17,37 @@ void daemonize(void) { switch (fork()) { case -1: - err(1, "fork"); + err(EXIT_FAILURE, "fork"); case 0: break; default: - exit(0); + exit(EXIT_SUCCESS); } umask(0); if (setsid() < 0) - err(1, "setsid"); + err(EXIT_FAILURE, "setsid"); if (chdir("/") < 0) - err(1, "chdir /"); + err(EXIT_FAILURE, "chdir /"); if (freopen("/dev/null", "r", stdin) == NULL) - err(1, "reopen stdin"); + err(EXIT_FAILURE, "reopen stdin"); if(freopen("/dev/null", "w", stdout) == NULL) - err(1, "reopen stdout"); + err(EXIT_FAILURE, "reopen stdout"); if(freopen("/dev/null", "w", stderr) == NULL) - err(1, "reopen stderr"); + err(EXIT_FAILURE, "reopen stderr"); } void catch_exit(int sig) { if (id) xc_watchdog(h, id, 300); - exit(0); + exit(EXIT_SUCCESS); } void catch_usr1(int sig) { if (id) xc_watchdog(h, id, 0); - exit(0); + exit(EXIT_SUCCESS); } int main(int argc, char **argv) @@ -56,44 +56,44 @@ int main(int argc, char **argv) int ret; if (argc < 2) - errx(1, "usage: %s ", argv[0]); + errx(EXIT_FAILURE, "usage: %s ", argv[0]); daemonize(); h = xc_interface_open(NULL, NULL, 0); if (h == NULL) - err(1, "xc_interface_open"); + err(EXIT_FAILURE, "xc_interface_open"); t = strtoul(argv[1], NULL, 0); if (t == ULONG_MAX) - err(1, "strtoul"); + err(EXIT_FAILURE, "strtoul"); s = t / 2; if (argc == 3) { s = strtoul(argv[2], NULL, 0); if (s == ULONG_MAX) - err(1, "strtoul"); + err(EXIT_FAILURE, "strtoul"); } if (signal(SIGHUP, &catch_exit) == SIG_ERR) - err(1, "signal"); + err(EXIT_FAILURE, "signal"); if (signal(SIGINT, &catch_exit) == SIG_ERR) - err(1, "signal"); + err(EXIT_FAILURE, "signal"); if (signal(SIGQUIT, &catch_exit) == SIG_ERR) - err(1, "signal"); + err(EXIT_FAILURE, "signal"); if (signal(SIGTERM, &catch_exit) == SIG_ERR) - err(1, "signal"); + err(EXIT_FAILURE, "signal"); if (signal(SIGUSR1, &catch_usr1) == SIG_ERR) - err(1, "signal"); + err(EXIT_FAILURE, "signal"); id = xc_watchdog(h, 0, t); if (id <= 0) - err(1, "xc_watchdog setup"); + err(EXIT_FAILURE, "xc_watchdog setup"); for (;;) { sleep(s); ret = xc_watchdog(h, id, t); if (ret != 0) - err(1, "xc_watchdog"); + err(EXIT_FAILURE, "xc_watchdog"); } } From patchwork Wed Mar 27 18:13:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leigh Brown X-Patchwork-Id: 13607252 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 14D28CD1289 for ; Wed, 27 Mar 2024 18:14:59 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.698692.1090719 (Exim 4.92) (envelope-from ) id 1rpXnS-0005Oh-QK; Wed, 27 Mar 2024 18:14:46 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 698692.1090719; Wed, 27 Mar 2024 18:14:46 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rpXnS-0005Ju-Gj; Wed, 27 Mar 2024 18:14:46 +0000 Received: by outflank-mailman (input) for mailman id 698692; Wed, 27 Mar 2024 18:14:15 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rpXmx-0004q7-BP for xen-devel@lists.xenproject.org; Wed, 27 Mar 2024 18:14:15 +0000 Received: from doppler.solinno.uk (doppler.solinno.uk [81.2.106.178]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id cfd4837f-ec65-11ee-afe3-a90da7624cb6; Wed, 27 Mar 2024 19:14:12 +0100 (CET) Received: from folly.solinno.co.uk (folly.dyn.solinno.co.uk [192.168.2.135]) by doppler.solinno.uk (Postfix) with ESMTPSA id A7AFC80063; Wed, 27 Mar 2024 18:14:11 +0000 (GMT) Received: by folly.solinno.co.uk (Postfix, from userid 1000) id 84509202A7; Wed, 27 Mar 2024 18:14:11 +0000 (GMT) X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: cfd4837f-ec65-11ee-afe3-a90da7624cb6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=solinno.co.uk; s=mail; t=1711563251; bh=ay3hKImaotvpX5ABfW+1h5b91XRSJf6dwN7llSE5zkA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=r6tUrtESc3+Vw4pNkGzCM1cWS7YHKPfPMGCxCvBt2Z2VmfN0FrapUAwBdBSjIaKpt QZ1C3QAoQTQz52XERPlOnU8gVRlqJtAOdoJCyQPL90nE8cvFwqxPljyynvDpguRQ3c k5jUJnPs1S1FHqneNpI1v7V7x0C8CYotjI0OxCPA= From: leigh@solinno.co.uk To: xen-devel@lists.xenproject.org Cc: andrew.cooper3@citrix.com, anthony.perard@citrix.com, slack@rabbit.lu, Leigh Brown Subject: [PATCH 2/6] tools/misc: rework xenwatchdogd signal handling Date: Wed, 27 Mar 2024 18:13:49 +0000 Message-Id: <20240327181353.10951-3-leigh@solinno.co.uk> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240327181353.10951-1-leigh@solinno.co.uk> References: <55416d60-cae7-4e79-8bde-bc07ee9e3830@suse.com> <20240327181353.10951-1-leigh@solinno.co.uk> MIME-Version: 1.0 From: Leigh Brown Rework xenwatchdogd signal handling to do the minimum in the signal handler. This is a very minor enhancement. --- tools/misc/xenwatchdogd.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/tools/misc/xenwatchdogd.c b/tools/misc/xenwatchdogd.c index 2f7c822d61..d4da0ad0b6 100644 --- a/tools/misc/xenwatchdogd.c +++ b/tools/misc/xenwatchdogd.c @@ -9,9 +9,11 @@ #include #include #include +#include xc_interface *h; -int id = 0; +bool safeexit = false; +bool done = false; void daemonize(void) { @@ -38,20 +40,18 @@ void daemonize(void) void catch_exit(int sig) { - if (id) - xc_watchdog(h, id, 300); - exit(EXIT_SUCCESS); + done = true; } void catch_usr1(int sig) { - if (id) - xc_watchdog(h, id, 0); - exit(EXIT_SUCCESS); + safeexit = true; + done = true; } int main(int argc, char **argv) { + int id; int t, s; int ret; @@ -90,10 +90,14 @@ int main(int argc, char **argv) if (id <= 0) err(EXIT_FAILURE, "xc_watchdog setup"); - for (;;) { + while (!done) { sleep(s); ret = xc_watchdog(h, id, t); if (ret != 0) err(EXIT_FAILURE, "xc_watchdog"); } + + // Zero seconds timeout will disarm the watchdog timer + xc_watchdog(h, id, safeexit ? 0 : 300); + return 0; } From patchwork Wed Mar 27 18:13:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leigh Brown X-Patchwork-Id: 13607247 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 8DFF8C47DD9 for ; Wed, 27 Mar 2024 18:14:57 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.698686.1090692 (Exim 4.92) (envelope-from ) id 1rpXnR-0004wk-FX; Wed, 27 Mar 2024 18:14:45 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 698686.1090692; Wed, 27 Mar 2024 18:14:45 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rpXnR-0004vn-BG; Wed, 27 Mar 2024 18:14:45 +0000 Received: by outflank-mailman (input) for mailman id 698686; Wed, 27 Mar 2024 18:14:13 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rpXmv-0004pv-Ek for xen-devel@lists.xenproject.org; Wed, 27 Mar 2024 18:14:13 +0000 Received: from doppler.solinno.uk (8.1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.9.a.d.0.6.f.d.1.0.b.8.0.1.0.0.2.ip6.arpa [2001:8b0:1df6:da9::18]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id cfe395f7-ec65-11ee-a1ef-f123f15fe8a2; Wed, 27 Mar 2024 19:14:12 +0100 (CET) Received: from folly.solinno.co.uk (folly.dyn.solinno.co.uk [192.168.2.135]) by doppler.solinno.uk (Postfix) with ESMTPSA id B0F4B8009F; Wed, 27 Mar 2024 18:14:11 +0000 (GMT) Received: by folly.solinno.co.uk (Postfix, from userid 1000) id 86739202A8; Wed, 27 Mar 2024 18:14:11 +0000 (GMT) X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: cfe395f7-ec65-11ee-a1ef-f123f15fe8a2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=solinno.co.uk; s=mail; t=1711563251; bh=JDIuy3KhnVNygJWUuL86c9BvLnjCSC0gyIjiqhIaOR4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cwUuo4NJyABfEk6Zpckz7ZK35hiSzNFLUrnCwKZmyF6SYyGvbeQDYuJKy9/1jKrNg UbljzUuah9nj65NmAqNlxA3UBGPBPx4ZMFNFGWdBQBEbDxYXcLMjV+uTp34AH1+Pfp JDiHNMSniZw3roF9pEZJtJz2WZLZZ5FVq48wKz4M= From: leigh@solinno.co.uk To: xen-devel@lists.xenproject.org Cc: andrew.cooper3@citrix.com, anthony.perard@citrix.com, slack@rabbit.lu, Leigh Brown Subject: [PATCH 3/6] tools/misc: xenwatchdogd: make functions static Date: Wed, 27 Mar 2024 18:13:50 +0000 Message-Id: <20240327181353.10951-4-leigh@solinno.co.uk> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240327181353.10951-1-leigh@solinno.co.uk> References: <55416d60-cae7-4e79-8bde-bc07ee9e3830@suse.com> <20240327181353.10951-1-leigh@solinno.co.uk> MIME-Version: 1.0 From: Leigh Brown Make all functions except main() static in xenwatchdogd.c. --- tools/misc/xenwatchdogd.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/misc/xenwatchdogd.c b/tools/misc/xenwatchdogd.c index d4da0ad0b6..224753e824 100644 --- a/tools/misc/xenwatchdogd.c +++ b/tools/misc/xenwatchdogd.c @@ -15,7 +15,7 @@ xc_interface *h; bool safeexit = false; bool done = false; -void daemonize(void) +static void daemonize(void) { switch (fork()) { case -1: @@ -38,12 +38,12 @@ void daemonize(void) err(EXIT_FAILURE, "reopen stderr"); } -void catch_exit(int sig) +static void catch_exit(int sig) { done = true; } -void catch_usr1(int sig) +static void catch_usr1(int sig) { safeexit = true; done = true; From patchwork Wed Mar 27 18:13:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leigh Brown X-Patchwork-Id: 13607248 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id CF7E4CD1283 for ; Wed, 27 Mar 2024 18:14:57 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.698688.1090706 (Exim 4.92) (envelope-from ) id 1rpXnS-0005BO-4G; Wed, 27 Mar 2024 18:14:46 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 698688.1090706; Wed, 27 Mar 2024 18:14:46 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rpXnR-00059d-Tk; Wed, 27 Mar 2024 18:14:45 +0000 Received: by outflank-mailman (input) for mailman id 698688; Wed, 27 Mar 2024 18:14:13 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rpXmv-0004q7-Mp for xen-devel@lists.xenproject.org; Wed, 27 Mar 2024 18:14:13 +0000 Received: from doppler.solinno.uk (8.1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.9.a.d.0.6.f.d.1.0.b.8.0.1.0.0.2.ip6.arpa [2001:8b0:1df6:da9::18]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id cfe5eb1d-ec65-11ee-afe3-a90da7624cb6; Wed, 27 Mar 2024 19:14:12 +0100 (CET) Received: from folly.solinno.co.uk (folly.dyn.solinno.co.uk [192.168.2.135]) by doppler.solinno.uk (Postfix) with ESMTPSA id B2139800AB; Wed, 27 Mar 2024 18:14:11 +0000 (GMT) Received: by folly.solinno.co.uk (Postfix, from userid 1000) id 88DF6202A9; Wed, 27 Mar 2024 18:14:11 +0000 (GMT) X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: cfe5eb1d-ec65-11ee-afe3-a90da7624cb6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=solinno.co.uk; s=mail; t=1711563251; bh=rQZPf46rdnhj9rP0ET/XSW2dcaf6Q8RXOc1FXyeFv8A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XUgZomaWSaC7XAON5YMh+zH32wgSPtqAYJD/l4Z3N7J58cAPaBap+XE6LUmFnTywm NYgYgiG2hoMmPy/aVOWbaLQoLKkNWARuDQhll9vfW0xcFRo7SUiak9wwdiHMEgyyZg oWomPnYl7ieMUi1lYhXPNlJ5rNJmDkV0zAGwuels= From: leigh@solinno.co.uk To: xen-devel@lists.xenproject.org Cc: andrew.cooper3@citrix.com, anthony.perard@citrix.com, slack@rabbit.lu, Leigh Brown Subject: [PATCH 4/6] tools/misc: xenwatchdogd: add parse_secs() Date: Wed, 27 Mar 2024 18:13:51 +0000 Message-Id: <20240327181353.10951-5-leigh@solinno.co.uk> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240327181353.10951-1-leigh@solinno.co.uk> References: <55416d60-cae7-4e79-8bde-bc07ee9e3830@suse.com> <20240327181353.10951-1-leigh@solinno.co.uk> MIME-Version: 1.0 From: Leigh Brown Create a new parse_secs() function to parse the timeout and sleep parameters. This ensures that non-numeric parameters are not accidentally treated as numbers. --- tools/misc/xenwatchdogd.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/tools/misc/xenwatchdogd.c b/tools/misc/xenwatchdogd.c index 224753e824..26bfdef3db 100644 --- a/tools/misc/xenwatchdogd.c +++ b/tools/misc/xenwatchdogd.c @@ -49,6 +49,18 @@ static void catch_usr1(int sig) done = true; } +static int parse_secs(const char *arg, const char *what) +{ + char *endptr; + unsigned long val; + + val = strtoul(arg, &endptr, 0); + if (val > INT_MAX || *endptr != 0) + errx(EXIT_FAILURE, "invalid %s: '%s'", what, arg); + + return val; +} + int main(int argc, char **argv) { int id; @@ -64,16 +76,11 @@ int main(int argc, char **argv) if (h == NULL) err(EXIT_FAILURE, "xc_interface_open"); - t = strtoul(argv[1], NULL, 0); - if (t == ULONG_MAX) - err(EXIT_FAILURE, "strtoul"); + t = parse_secs(argv[optind], "timeout"); s = t / 2; - if (argc == 3) { - s = strtoul(argv[2], NULL, 0); - if (s == ULONG_MAX) - err(EXIT_FAILURE, "strtoul"); - } + if (argc == 3) + s = parse_secs(argv[optind], "sleep"); if (signal(SIGHUP, &catch_exit) == SIG_ERR) err(EXIT_FAILURE, "signal"); From patchwork Wed Mar 27 18:13:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leigh Brown X-Patchwork-Id: 13607250 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E9C28C54E67 for ; Wed, 27 Mar 2024 18:14:57 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.698687.1090697 (Exim 4.92) (envelope-from ) id 1rpXnR-00053a-O4; Wed, 27 Mar 2024 18:14:45 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 698687.1090697; Wed, 27 Mar 2024 18:14:45 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rpXnR-00050c-JP; Wed, 27 Mar 2024 18:14:45 +0000 Received: by outflank-mailman (input) for mailman id 698687; Wed, 27 Mar 2024 18:14:13 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rpXmv-0004pv-LY for xen-devel@lists.xenproject.org; Wed, 27 Mar 2024 18:14:13 +0000 Received: from doppler.solinno.uk (8.1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.9.a.d.0.6.f.d.1.0.b.8.0.1.0.0.2.ip6.arpa [2001:8b0:1df6:da9::18]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id cfeaa987-ec65-11ee-a1ef-f123f15fe8a2; Wed, 27 Mar 2024 19:14:12 +0100 (CET) Received: from folly.solinno.co.uk (folly.dyn.solinno.co.uk [192.168.2.135]) by doppler.solinno.uk (Postfix) with ESMTPSA id B4C7B800AC; Wed, 27 Mar 2024 18:14:11 +0000 (GMT) Received: by folly.solinno.co.uk (Postfix, from userid 1000) id 8B12E202AA; Wed, 27 Mar 2024 18:14:11 +0000 (GMT) X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: cfeaa987-ec65-11ee-a1ef-f123f15fe8a2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=solinno.co.uk; s=mail; t=1711563251; bh=hpLEOTagI61CXps0+cy2rWLbakF8//wcNhtpIOFZpR0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nVV3jfw3vsYUmrK7E+spsBFdLLUgaDzBy+GT+8ynXbWE7QimLvAO8PKzckIesdlKb I135XlGL475dZ0gU9XtPg6WrcxEuFMKpdjFYwOtpPat0i52OxGN9g2tlGaMk9kV4+F UJu1R9mvkr+BEWn4WCP6xrYooEFtqLUlqOhGVMKk= From: leigh@solinno.co.uk To: xen-devel@lists.xenproject.org Cc: andrew.cooper3@citrix.com, anthony.perard@citrix.com, slack@rabbit.lu, Leigh Brown Subject: [PATCH 5/6] tools/misc: xenwatchdogd enhancements Date: Wed, 27 Mar 2024 18:13:52 +0000 Message-Id: <20240327181353.10951-6-leigh@solinno.co.uk> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240327181353.10951-1-leigh@solinno.co.uk> References: <55416d60-cae7-4e79-8bde-bc07ee9e3830@suse.com> <20240327181353.10951-1-leigh@solinno.co.uk> MIME-Version: 1.0 From: Leigh Brown Add enhanced parameter parsing and validation, making use of getopt_long(). Adds usage() function, ability to run in the foreground, and the ability to disarm the watchdog timer when exiting. Now checks the number of parameters are correct, that timeout is at least two seconds (to allow a minimum sleep time of one second), and that the sleep time is at least one and less than the watchdog timeout. After these changes, the daemon will no longer instantly reboot the domain if you enter a zero timeout (or non-numeric parameter), and prevent the daemon consuming 100% of a CPU. Add a copyright message. This is based on the previous commits which were from Citrix email addresses. --- tools/misc/xenwatchdogd.c | 111 ++++++++++++++++++++++++++++++++++---- 1 file changed, 101 insertions(+), 10 deletions(-) diff --git a/tools/misc/xenwatchdogd.c b/tools/misc/xenwatchdogd.c index 26bfdef3db..77638a4309 100644 --- a/tools/misc/xenwatchdogd.c +++ b/tools/misc/xenwatchdogd.c @@ -1,3 +1,20 @@ +/* + * xenwatchdogd.c + * + * Watchdog based on Xen hypercall watchdog interface + * + * Copyright 2010-2024 Citrix Ltd and other contributors + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation; version 2.1 only. with the special + * exception on linking described in file LICENSE. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + */ #include #include @@ -10,6 +27,11 @@ #include #include #include +#include + +#define WDOG_MIN_TIMEOUT 2 +#define WDOG_MIN_SLEEP 1 +#define WDOG_EXIT_TIMEOUT 300 xc_interface *h; bool safeexit = false; @@ -49,6 +71,26 @@ static void catch_usr1(int sig) done = true; } +static void __attribute__((noreturn)) usage(int exit_code) +{ + FILE *out = exit_code ? stderr : stdout; + + fprintf(out, + "Usage: xenwatchdog [OPTION]... []\n" + " -h, --help\t\tDisplay this help text and exit.\n" + " -F, --foreground\tRun in foreground.\n" + " -x, --safe-exit\tDisable watchdog on orderly exit.\n" + "\t\t\tNote: default is to set a %d second timeout on exit.\n\n" + " timeout\t\tInteger seconds to arm the watchdog each time.\n" + "\t\t\tNote: minimum timeout is %d seconds.\n\n" + " sleep\t\t\tInteger seconds to sleep between arming the watchdog.\n" + "\t\t\tNote: sleep must be at least %d and less than timeout.\n" + "\t\t\tIf not specified then set to half the timeout.\n", + WDOG_EXIT_TIMEOUT, WDOG_MIN_TIMEOUT, WDOG_MIN_SLEEP + ); + exit(exit_code); +} + static int parse_secs(const char *arg, const char *what) { char *endptr; @@ -66,21 +108,70 @@ int main(int argc, char **argv) int id; int t, s; int ret; + bool daemon = true; + + for ( ;; ) + { + int option_index = 0, c; + static const struct option long_options[] = + { + { "help", no_argument, NULL, 'h' }, + { "foreground", no_argument, NULL, 'F' }, + { "safe-exit", no_argument, NULL, 'x' }, + { NULL, 0, NULL, 0 }, + }; + + c = getopt_long(argc, argv, "hFxD", long_options, &option_index); + if (c == -1) + break; + + switch (c) + { + case 'h': + usage(EXIT_SUCCESS); + + case 'F': + daemon = false; + break; + + case 'x': + safeexit = true; + break; + + default: + usage(EXIT_FAILURE); + } + } - if (argc < 2) - errx(EXIT_FAILURE, "usage: %s ", argv[0]); + if (argc - optind < 1) + errx(EXIT_FAILURE, "timeout must be specified"); - daemonize(); - - h = xc_interface_open(NULL, NULL, 0); - if (h == NULL) - err(EXIT_FAILURE, "xc_interface_open"); + if (argc - optind > 2) + errx(EXIT_FAILURE, "too many arguments"); t = parse_secs(argv[optind], "timeout"); + if (t < WDOG_MIN_TIMEOUT) + errx(EXIT_FAILURE, "Error: timeout must be at least %d seconds", + WDOG_MIN_TIMEOUT); - s = t / 2; - if (argc == 3) + ++optind; + if (optind < argc) { s = parse_secs(argv[optind], "sleep"); + if (s < WDOG_MIN_SLEEP) + errx(EXIT_FAILURE, "Error: sleep must be no less than %d", + WDOG_MIN_SLEEP); + if (s >= t) + errx(EXIT_FAILURE, "Error: sleep must be less than timeout"); + } + else + s = t / 2; + + if (daemon) + daemonize(); + + h = xc_interface_open(NULL, NULL, 0); + if (h == NULL) + err(EXIT_FAILURE, "xc_interface_open"); if (signal(SIGHUP, &catch_exit) == SIG_ERR) err(EXIT_FAILURE, "signal"); @@ -105,6 +196,6 @@ int main(int argc, char **argv) } // Zero seconds timeout will disarm the watchdog timer - xc_watchdog(h, id, safeexit ? 0 : 300); + xc_watchdog(h, id, safeexit ? 0 : WDOG_EXIT_TIMEOUT); return 0; } From patchwork Wed Mar 27 18:13:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leigh Brown X-Patchwork-Id: 13607251 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id CF846CD1284 for ; Wed, 27 Mar 2024 18:14:57 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.698693.1090728 (Exim 4.92) (envelope-from ) id 1rpXnT-0005Xh-7c; Wed, 27 Mar 2024 18:14:47 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 698693.1090728; Wed, 27 Mar 2024 18:14:47 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rpXnS-0005Wt-Sw; Wed, 27 Mar 2024 18:14:46 +0000 Received: by outflank-mailman (input) for mailman id 698693; Wed, 27 Mar 2024 18:14:16 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rpXmy-0004q7-BT for xen-devel@lists.xenproject.org; Wed, 27 Mar 2024 18:14:16 +0000 Received: from doppler.solinno.uk (doppler.solinno.uk [81.2.106.178]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id d0a6128d-ec65-11ee-afe3-a90da7624cb6; Wed, 27 Mar 2024 19:14:13 +0100 (CET) Received: from folly.solinno.co.uk (folly.dyn.solinno.co.uk [192.168.2.135]) by doppler.solinno.uk (Postfix) with ESMTPSA id D3E75800AD; Wed, 27 Mar 2024 18:14:11 +0000 (GMT) Received: by folly.solinno.co.uk (Postfix, from userid 1000) id 8D260202AB; Wed, 27 Mar 2024 18:14:11 +0000 (GMT) X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: d0a6128d-ec65-11ee-afe3-a90da7624cb6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=solinno.co.uk; s=mail; t=1711563251; bh=/l5XcylGh8QpFXW26aAq5ArxFrGUV6w08D0TT0l/waM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YQVXgIPNx1yNAVVZ0+hBNkgAh6nKA1yuxNbnj77qJTP6kpI2gEdvtD2Hc2/KrMCSn xW7Hl6ZnZ6H+/x8fps40L1Q0di+SsyJHwYdLpoGCrDQ3RfBS3YFXejIcKv2+aJk24h NBiH9vxDf8xfMpvccQ54vLaCavCtdiI8Eu90pp00= From: leigh@solinno.co.uk To: xen-devel@lists.xenproject.org Cc: andrew.cooper3@citrix.com, anthony.perard@citrix.com, slack@rabbit.lu, Leigh Brown Subject: [PATCH 6/6] docs/man: Add xenwatchdog manual page Date: Wed, 27 Mar 2024 18:13:53 +0000 Message-Id: <20240327181353.10951-7-leigh@solinno.co.uk> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240327181353.10951-1-leigh@solinno.co.uk> References: <55416d60-cae7-4e79-8bde-bc07ee9e3830@suse.com> <20240327181353.10951-1-leigh@solinno.co.uk> MIME-Version: 1.0 From: Leigh Brown Add a manual page for xenwatchdogd. --- docs/man/xenwatchdogd.8.pod | 54 +++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 docs/man/xenwatchdogd.8.pod diff --git a/docs/man/xenwatchdogd.8.pod b/docs/man/xenwatchdogd.8.pod new file mode 100644 index 0000000000..2f6454f183 --- /dev/null +++ b/docs/man/xenwatchdogd.8.pod @@ -0,0 +1,54 @@ +=head1 NAME + +xenwatchdogd - Xen hypercall based watchdog daemon + +=head1 SYNOPSIS + +B [ I ] > [ > ] + +=head1 DESCRIPTION + +B arms the Xen watchdog timer to I every I +seconds. If the xenwatchdogd process dies or is delayed for more than +I seconds, then Xen will reboot the domain. If the domain being +rebooted is domain 0, the whole system will reboot. + +=head1 OPTIONS + +=over 4 + +=item B<-h>, B<--help> + +Display a help message. + +=item B<-F>, B<--foreground> + +Run in the foreground. The default behaviour is to daemonize. + +=item B<-x>, B<--safe-exit> + +Disable watchdog on orderly exit. The default behaviour is to arm the +watchdog to 300 seconds to allow time for the domain to shutdown. See +also the B section. + +=item B + +The number of seconds to arm the Xen watchdog timer. This must be set to +a minimum of two. + +=item B + +The number of seconds to sleep in between calls to arm the Xen watchdog +timer. This must be at least one second, and less than the I +value. If not specified, it defaults to half the I value. + +=back + +=head1 SIGNALS + +B Will cause the program to disarm the watchdog timer and exit, +regardless of whether the safe exit option was passed. + +=head1 AUTHOR + +Citrix Ltd and other contributors.