From patchwork Fri Mar 29 11:10: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: 13610554 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 3B959CD128E for ; Fri, 29 Mar 2024 11:11:40 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.699355.1092158 (Exim 4.92) (envelope-from ) id 1rqA8s-0006tS-W6; Fri, 29 Mar 2024 11:11:27 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 699355.1092158; Fri, 29 Mar 2024 11:11:26 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rqA8s-0006sl-R2; Fri, 29 Mar 2024 11:11:26 +0000 Received: by outflank-mailman (input) for mailman id 699355; Fri, 29 Mar 2024 11:11:25 +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 1rqA8r-0006qp-RR for xen-devel@lists.xenproject.org; Fri, 29 Mar 2024 11:11:25 +0000 Received: from doppler.solinno.uk (doppler.solinno.uk [81.2.106.178]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 13de8cef-edbd-11ee-afe3-a90da7624cb6; Fri, 29 Mar 2024 12:11:23 +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 EBE878009E; Fri, 29 Mar 2024 11:11:22 +0000 (GMT) Received: by folly.solinno.co.uk (Postfix, from userid 1000) id B714F20183; Fri, 29 Mar 2024 11:11:22 +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: 13de8cef-edbd-11ee-afe3-a90da7624cb6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=solinno.co.uk; s=mail; t=1711710682; bh=4RuAZZ2FiEx1YmWEem8TR0isA44tccZW5bEUQSzrF7E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cuKTzJmiwdHU1qAZR56fQjOc62NRLS9VTp6MXkfEJVBv+FZx3j7+KtNy5KBixreyp r3iUm73I97pm+ZL1JJ9+dmEvvURfe3AVe3FYfCYnoue0U8ICQZPfAqhpFYH97rdlYi pytqAYGc398Dqij8FFzGPPmloTZo9IL6Xq8WU+Mg= 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 v2 1/6] tools/misc: xenwatchdogd: use EXIT_* constants Date: Fri, 29 Mar 2024 11:10:51 +0000 Message-Id: <20240329111056.6118-2-leigh@solinno.co.uk> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240329111056.6118-1-leigh@solinno.co.uk> References: <20240329111056.6118-1-leigh@solinno.co.uk> MIME-Version: 1.0 From: Leigh Brown Use EXIT_SUCCESS/EXIT_FAILURE constants instead of magic numbers. Signed-off-by: Leigh Brown Reviewed-by: Anthony PERARD --- 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 Fri Mar 29 11:10: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: 13610552 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 EB42ACD1290 for ; Fri, 29 Mar 2024 11:11:38 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.699358.1092188 (Exim 4.92) (envelope-from ) id 1rqA8u-0007bY-QS; Fri, 29 Mar 2024 11:11:28 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 699358.1092188; Fri, 29 Mar 2024 11:11:28 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rqA8u-0007ar-L0; Fri, 29 Mar 2024 11:11:28 +0000 Received: by outflank-mailman (input) for mailman id 699358; Fri, 29 Mar 2024 11:11:26 +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 1rqA8s-0006qp-RU for xen-devel@lists.xenproject.org; Fri, 29 Mar 2024 11:11:26 +0000 Received: from doppler.solinno.uk (doppler.solinno.uk [81.2.106.178]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 13de8d67-edbd-11ee-afe3-a90da7624cb6; Fri, 29 Mar 2024 12:11:23 +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 EE1E18009F; Fri, 29 Mar 2024 11:11:22 +0000 (GMT) Received: by folly.solinno.co.uk (Postfix, from userid 1000) id B9288202A8; Fri, 29 Mar 2024 11:11:22 +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: 13de8d67-edbd-11ee-afe3-a90da7624cb6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=solinno.co.uk; s=mail; t=1711710682; bh=I1KRv77SpIHtiaVfa6jI9oqQE3FOnPOPsxPAoW9qC9A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MFz1C8QqkX4a00kAlbl931eb2FFXJ6Ibpk8EmslqrcQrxIzAMW8EJrabcoqrepG7X Xe3XCgDyWezFivAs3sNPYJVaP6IjqFYvMcVfKS4ybDOkXPbfvXK3152rNsOdC0OrhC Fgm2DFyUr9PW3AE+VnZuKTrW0YimWT0TPIkY6lkg= 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 v2 2/6] tools/misc: rework xenwatchdogd signal handling Date: Fri, 29 Mar 2024 11:10:52 +0000 Message-Id: <20240329111056.6118-3-leigh@solinno.co.uk> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240329111056.6118-1-leigh@solinno.co.uk> References: <20240329111056.6118-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. Signed-off-by: Leigh Brown Reviewed-by: Anthony PERARD --- 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..35a0df655a 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; +static bool safeexit = false; +static 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 Fri Mar 29 11:10: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: 13610549 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 B88FFCD11DD for ; Fri, 29 Mar 2024 11:11:38 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.699357.1092184 (Exim 4.92) (envelope-from ) id 1rqA8u-0007Xt-I0; Fri, 29 Mar 2024 11:11:28 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 699357.1092184; Fri, 29 Mar 2024 11:11:28 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rqA8u-0007WX-CT; Fri, 29 Mar 2024 11:11:28 +0000 Received: by outflank-mailman (input) for mailman id 699357; Fri, 29 Mar 2024 11:11:26 +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 1rqA8s-0006qu-KZ for xen-devel@lists.xenproject.org; Fri, 29 Mar 2024 11:11:26 +0000 Received: from doppler.solinno.uk (doppler.solinno.uk [81.2.106.178]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 13de8de8-edbd-11ee-a1ef-f123f15fe8a2; Fri, 29 Mar 2024 12:11:23 +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 F080A800AB; Fri, 29 Mar 2024 11:11:22 +0000 (GMT) Received: by folly.solinno.co.uk (Postfix, from userid 1000) id BB6D8202A9; Fri, 29 Mar 2024 11:11:22 +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: 13de8de8-edbd-11ee-a1ef-f123f15fe8a2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=solinno.co.uk; s=mail; t=1711710682; bh=gQdWCDqh5zFcK/ZMx9JSLjpUrWRQkvbUNQcXRiak9pQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=f4eP/5IlAfpJBFxFvkLT/sK9fXPQCirqm/Qd0hf0AdY30Pejr5275U/JC0zCluNY6 KhNSPODwnILmcRaPLLpCYe3wcUImIaffWXJupx2tLynnhohTvBMnnWBuW2qrVlNX1t 0JfgTNK2mBdsroKfAlPVyQ8jcGth7L44GP5xpkCk= 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 v2 3/6] tools/misc: xenwatchdogd: add static qualifier Date: Fri, 29 Mar 2024 11:10:53 +0000 Message-Id: <20240329111056.6118-4-leigh@solinno.co.uk> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240329111056.6118-1-leigh@solinno.co.uk> References: <20240329111056.6118-1-leigh@solinno.co.uk> MIME-Version: 1.0 From: Leigh Brown Make all functions except main() static in xenwatchdogd.c. Also make the remaining global variable static. Signed-off-by: Leigh Brown Reviewed-by: Anthony PERARD --- tools/misc/xenwatchdogd.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tools/misc/xenwatchdogd.c b/tools/misc/xenwatchdogd.c index 35a0df655a..2e7f9f51c5 100644 --- a/tools/misc/xenwatchdogd.c +++ b/tools/misc/xenwatchdogd.c @@ -11,11 +11,11 @@ #include #include -xc_interface *h; +static xc_interface *h; static bool safeexit = false; static 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 Fri Mar 29 11:10:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leigh Brown X-Patchwork-Id: 13610550 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 F2CEBCD1291 for ; Fri, 29 Mar 2024 11:11:38 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.699354.1092153 (Exim 4.92) (envelope-from ) id 1rqA8s-0006rD-Ma; Fri, 29 Mar 2024 11:11:26 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 699354.1092153; Fri, 29 Mar 2024 11:11:26 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rqA8s-0006r6-Jc; Fri, 29 Mar 2024 11:11:26 +0000 Received: by outflank-mailman (input) for mailman id 699354; Fri, 29 Mar 2024 11:11:25 +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 1rqA8r-0006qp-5b for xen-devel@lists.xenproject.org; Fri, 29 Mar 2024 11:11:25 +0000 Received: from doppler.solinno.uk (doppler.solinno.uk [81.2.106.178]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 13de8d71-edbd-11ee-afe3-a90da7624cb6; Fri, 29 Mar 2024 12:11:23 +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 F107A800AC; Fri, 29 Mar 2024 11:11:22 +0000 (GMT) Received: by folly.solinno.co.uk (Postfix, from userid 1000) id BD8BC202AA; Fri, 29 Mar 2024 11:11:22 +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: 13de8d71-edbd-11ee-afe3-a90da7624cb6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=solinno.co.uk; s=mail; t=1711710682; bh=Q+rKJ2Q7LqFNdLGMkfelDfN5qg5la8JE5UlDaV5w0rc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pjf8roxX7fv1QHDjU6GvhVSOfwSdOioSDPeeUXSTkpiDomnS4uzgymF2clq2cTfpA RMRHocYs8nY6hwM1K3Y4cg/8/lPEawRzHF2swPZf4QUWzdo2ugrcKpOusDd6rWfLFh lar4vsFg/gmIOZOXqfi3AgxHJH9REx/ysia6S9tA= 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 v2 4/6] tools/misc: xenwatchdogd: add parse_secs() Date: Fri, 29 Mar 2024 11:10:54 +0000 Message-Id: <20240329111056.6118-5-leigh@solinno.co.uk> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240329111056.6118-1-leigh@solinno.co.uk> References: <20240329111056.6118-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. Signed-off-by: Leigh Brown --- 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 2e7f9f51c5..19ec4c5359 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 Fri Mar 29 11:10:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leigh Brown X-Patchwork-Id: 13610553 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 E84A3CD1292 for ; Fri, 29 Mar 2024 11:11:39 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.699360.1092201 (Exim 4.92) (envelope-from ) id 1rqA8v-0007of-Jx; Fri, 29 Mar 2024 11:11:29 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 699360.1092201; Fri, 29 Mar 2024 11:11:29 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rqA8v-0007mA-Ah; Fri, 29 Mar 2024 11:11:29 +0000 Received: by outflank-mailman (input) for mailman id 699360; Fri, 29 Mar 2024 11:11:27 +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 1rqA8t-0006qp-RW for xen-devel@lists.xenproject.org; Fri, 29 Mar 2024 11:11:27 +0000 Received: from doppler.solinno.uk (doppler.solinno.uk [81.2.106.178]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 14bc7654-edbd-11ee-afe3-a90da7624cb6; Fri, 29 Mar 2024 12:11:25 +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 25133800AE; Fri, 29 Mar 2024 11:11:23 +0000 (GMT) Received: by folly.solinno.co.uk (Postfix, from userid 1000) id BFBD8202AB; Fri, 29 Mar 2024 11:11:22 +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: 14bc7654-edbd-11ee-afe3-a90da7624cb6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=solinno.co.uk; s=mail; t=1711710683; bh=FFT0JKZLTG2RafX6HaY0v4ymDYgP25/NOMi/BqeNaFo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=IeYrDIc9gMTrE93pBdwcmZBGD+FsYwOd1s3dZPRCBY9TdX4FmqQ1uLyXQw9tfYPLf Mm4QmRfG5rb2VefjPyyeP0nEF9NOawu6eeM4KEKcYZAr+tckKTHEKRwqNdpZyAmzgZ TamkqEtwCFNOPPu35aIROD7sN30g6jaVLUeH/GJ0= 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 v2 5/6] tools/misc: xenwatchdogd enhancements Date: Fri, 29 Mar 2024 11:10:55 +0000 Message-Id: <20240329111056.6118-6-leigh@solinno.co.uk> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240329111056.6118-1-leigh@solinno.co.uk> References: <20240329111056.6118-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. Signed-off-by: Leigh Brown --- 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 19ec4c5359..b78320f86d 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 static xc_interface *h; static 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 Fri Mar 29 11:10:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leigh Brown X-Patchwork-Id: 13610551 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 E6B94CD128A for ; Fri, 29 Mar 2024 11:11:38 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.699359.1092195 (Exim 4.92) (envelope-from ) id 1rqA8v-0007i2-85; Fri, 29 Mar 2024 11:11:29 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 699359.1092195; Fri, 29 Mar 2024 11:11:29 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rqA8u-0007fq-W8; Fri, 29 Mar 2024 11:11:28 +0000 Received: by outflank-mailman (input) for mailman id 699359; Fri, 29 Mar 2024 11:11:27 +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 1rqA8t-0006qu-Kd for xen-devel@lists.xenproject.org; Fri, 29 Mar 2024 11:11:27 +0000 Received: from doppler.solinno.uk (doppler.solinno.uk [81.2.106.178]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 14bca0d2-edbd-11ee-a1ef-f123f15fe8a2; Fri, 29 Mar 2024 12:11:24 +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 2415A800AD; Fri, 29 Mar 2024 11:11:23 +0000 (GMT) Received: by folly.solinno.co.uk (Postfix, from userid 1000) id C21C2202AC; Fri, 29 Mar 2024 11:11:22 +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: 14bca0d2-edbd-11ee-a1ef-f123f15fe8a2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=solinno.co.uk; s=mail; t=1711710683; bh=vvCdYJp3hmq98C5XpPhX29JIJPhg6FKX4zHYexemcSA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Na54nphOlyvcT6fmlR+2XS4n7fZOIZY6aNNyhFg131DLIZ3EO1CvFh9WxdPDl4/wS 4KnLfdc2ZsMcYc2U1qJyccrHyi/DEZYaDDdMaWrUgl4Ir71LV19mv4BOn/XDv0sA7m 5D8QfiW3LT0iNPduaQMyQgnTTnrw/QmK7xKKMS58= 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 v2 6/6] docs/man: Add xenwatchdog manual page Date: Fri, 29 Mar 2024 11:10:56 +0000 Message-Id: <20240329111056.6118-7-leigh@solinno.co.uk> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240329111056.6118-1-leigh@solinno.co.uk> References: <20240329111056.6118-1-leigh@solinno.co.uk> MIME-Version: 1.0 From: Leigh Brown Add a manual page for xenwatchdogd. Signed-off-by: Leigh Brown --- 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.