From patchwork Thu Mar 12 14:54:17 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Andryuk X-Patchwork-Id: 11434505 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7105A92A for ; Thu, 12 Mar 2020 14:57:03 +0000 (UTC) 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 mail.kernel.org (Postfix) with ESMTPS id 4DA2220663 for ; Thu, 12 Mar 2020 14:57:03 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="kVDNXjPf" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4DA2220663 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1jCPFg-0001lz-L4; Thu, 12 Mar 2020 14:56:00 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1jCPFg-0001lk-2a for xen-devel@lists.xenproject.org; Thu, 12 Mar 2020 14:56:00 +0000 X-Inumbo-ID: 94b5b976-6471-11ea-bec1-bc764e2007e4 Received: from mail-qv1-xf42.google.com (unknown [2607:f8b0:4864:20::f42]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 94b5b976-6471-11ea-bec1-bc764e2007e4; Thu, 12 Mar 2020 14:55:57 +0000 (UTC) Received: by mail-qv1-xf42.google.com with SMTP id du17so2725429qvb.12 for ; Thu, 12 Mar 2020 07:55:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=tiMMbdlplWbQkqKBrkqBCfz0n+qZkFIQLjvGcjRlus0=; b=kVDNXjPf7KOui5qsaux0T5RiwyA/hueF/+XMUrc2aQRs7XW6lOmFsmP7doHyyiIml6 bqdPpdAeEhOibjgyrL5AGllXZZ2zI6wbKLcsundujYDotmHHF5JY4H4vKRLBJjYzPvcX U1QGVcQIdTIXLmT4Ov7btcFD/k65KSCeteeSuN8uDHkCmSBa0UcgrQwRSJ5S20eOQlLe TuVQgUifbr6vvrAkB5tXDs2eyZ+tMV7I4B0I8dpWeE5cq/+UYODLDiF/MwMKhoamSQye pjEKHa+KjMo8+Znq+NWk9LuMoHEnqo/Xoz+SDY8hOV+OfovlA2uX7y1h93LxtGXUqEZN 2hLA== 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=tiMMbdlplWbQkqKBrkqBCfz0n+qZkFIQLjvGcjRlus0=; b=EM+rA4qnrpvNEtZs8aEUYn6Pamt1uBCikkanXHMelzmS8AhePOSu1tu93pgVgn3o+8 sFZA8yLYGU4C3cfi+isxYUARBlAQLPerYaedru4GkqI0FI1zH+Hkh3yq6IVp7iOzDDVj veIYrUzY+/EjunE5+OoIs4MDcSiR4i2oFTxbHkX3bPR+CI57ZZq3WVRlXtPNRxdRbUMv gzTXawciXk//mPrAxFmNen6x1WdKERUNOX92z/0RDHcx46D6QNeQKY+6dBxcxnwbefZY ycF/HHYtrVkpx+WndBgU3NUkC5dC+5MDHABQoQA07ADjSilRkzjuYP/S1sAaS/RaPkEK HYaQ== X-Gm-Message-State: ANhLgQ3TzLGshQx4Ian77G3tyQ+N9iRECh4sGnV6zt5frLPAp7kbYgax oRv2TY66ikBK8xvT+t/E7ZEjBYlJ X-Google-Smtp-Source: ADFU+vtFQtHbmq/6X7Z3fr6MmHwbkRdXuL6OzKGLC7jG8n+bZb9ddH7qGn3h23QsMR6W3MB8RDzaog== X-Received: by 2002:a05:6214:1703:: with SMTP id db3mr1686198qvb.28.1584024957023; Thu, 12 Mar 2020 07:55:57 -0700 (PDT) Received: from pm2-ws13.praxislan02.com ([2001:470:8:67e:ba27:ebff:fee8:ce27]) by smtp.gmail.com with ESMTPSA id q1sm19756442qtp.81.2020.03.12.07.55.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Mar 2020 07:55:56 -0700 (PDT) From: Jason Andryuk To: xen-devel@lists.xenproject.org Date: Thu, 12 Mar 2020 10:54:17 -0400 Message-Id: <20200312145417.106812-3-jandryuk@gmail.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200312145417.106812-1-jandryuk@gmail.com> References: <20200312145417.106812-1-jandryuk@gmail.com> MIME-Version: 1.0 Subject: [Xen-devel] [PATCH 2/2] scripts: Use stat to check lock claim X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Ian Jackson , Ian Jackson , Wei Liu , Jason Andryuk Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" Replace the perl locking check with stat(1). Stat is able to fstat stdin (file descriptor 0) when passed '-' as an argument. This is now used to check $_lockfd. stat(1) support for '-' was introduced to coreutils in 2009. After A releases its lock, script B will return from flock and execute stat. Since the lockfile has been removed by A, stat prints an error to stderr and exits non-zero. Redirect stderr to /dev/null to avoid filling /var/log/xen/xen-hotplug.log with "No such file or directory" messages. Placing the stat call inside the "if" condition ensures we only check the stat output when the command completed successfully. This change removes the only runtime dependency of the xen toolstack on perl. Suggested-by: Ian Jackson Signed-off-by: Jason Andryuk Reviewed-by: Ian Jackson --- v2: Replace sed with bash string manipulation. Signed-off-by: Jason Andryuk --- tools/hotplug/Linux/locking.sh | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/tools/hotplug/Linux/locking.sh b/tools/hotplug/Linux/locking.sh index baaaf73508..69aefae6f6 100644 --- a/tools/hotplug/Linux/locking.sh +++ b/tools/hotplug/Linux/locking.sh @@ -41,7 +41,7 @@ claim_lock() # from chiark-utils, except using flock. It has the benefit of # it being possible to safely remove the lockfile when done. # See below for a correctness proof. - local rightfile + local stat while true; do eval "exec $_lockfd<>$_lockfile" flock -x $_lockfd || return $? @@ -50,14 +50,24 @@ claim_lock() # actually a synthetic symlink in /proc and we aren't # guaranteed that our stat(2) won't lose the race with an # rm(1) between reading the synthetic link and traversing the - # file system to find the inum. Perl is very fast so use that. - rightfile=$( perl -e ' - open STDIN, "<&'$_lockfd'" or die $!; - my $fd_inum = (stat STDIN)[1]; die $! unless defined $fd_inum; - my $file_inum = (stat $ARGV[0])[1]; - print "y\n" if $fd_inum eq $file_inum; - ' "$_lockfile" ) - if [ x$rightfile = xy ]; then break; fi + # file system to find the inum. stat(1) translates '-' into an + # fstat(2) of FD 0. So we just need to arrange the FDs properly + # to get the fstat(2) we need. stat will output two lines like: + # WW.XXX + # YY.ZZZ + # which need to be separated and compared. + if stat=$( stat -L -c '%D.%i' - $_lockfile 0<&$_lockfd 2>/dev/null ) + then + local file_stat + local fd_stat + + # match on literal newline + fd_stat=${stat% +*} + file_stat=${stat#* +} + if [ "$fd_stat" = "$file_stat" ] ; then break; fi + fi # Some versions of bash appear to be buggy if the same # $_lockfile is opened repeatedly. Close the current fd here. eval "exec $_lockfd<&-"