From patchwork Tue Mar 10 13:29:04 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Andryuk X-Patchwork-Id: 11429355 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 BB572139A for ; Tue, 10 Mar 2020 13:31:16 +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 93BA12467D for ; Tue, 10 Mar 2020 13:31:16 +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="DQBYGlcf" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 93BA12467D 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 1jBex2-0003FS-Nm; Tue, 10 Mar 2020 13:29:40 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1jBex0-0003FL-Ni for xen-devel@lists.xenproject.org; Tue, 10 Mar 2020 13:29:38 +0000 X-Inumbo-ID: 3056aad4-62d3-11ea-92cf-bc764e2007e4 Received: from mail-qk1-x743.google.com (unknown [2607:f8b0:4864:20::743]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 3056aad4-62d3-11ea-92cf-bc764e2007e4; Tue, 10 Mar 2020 13:29:37 +0000 (UTC) Received: by mail-qk1-x743.google.com with SMTP id y126so12654792qke.4 for ; Tue, 10 Mar 2020 06:29:37 -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:mime-version :content-transfer-encoding; bh=6HRNq9BAI9Jo3/+xbo/eArOFRBqLWGQURpBV+0mu0Zc=; b=DQBYGlcff3Fedmq1nMdO938TGb2kx5N8NH4qkl9my52UdhBo29amCQOzyXrt8hCXiU U1wbO69Iz3caI373YLibXTevBw2zgpLpmXaKL/H3X3BrzzZuBjwBW2xrOnw/bLKJhNP+ CgswgdqhT+wyhVQZdvP7um0+yPSsbyubh9+nrtKAQ46fXaHgothPEF8cGuyhxampehat UuPoGOUHncp43o1RGUYfftbUOpqPlvudLg0d3oZPnNSE6lQBRsSzw2cDHNQo0cMqzhXW gLPAnsLNlByFJlPtfKX6gMCR/Gs/KWpRYZeoJ1tND1O21WgipDrbKIz4mFJ7pdNSZhAf vAEA== 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:mime-version :content-transfer-encoding; bh=6HRNq9BAI9Jo3/+xbo/eArOFRBqLWGQURpBV+0mu0Zc=; b=Oa5WuC9uIlsE+1vU1x3nrnDLa6X3V8EIOhUYf/ZgT+I/9DC1TqfEX9NOAjn0jjsbSp H9flo0YgTudrsxIIPNA1N+LLRjv+bwgzzo0ItfzksHTQLfs9yJBNlc3+5oKnrGsK0bzg xl6WiRtvFZrsH0i6aDJQuxkvD+CjeTDgpqf5lohj8n4inn/bDELks2AV64/G7DlKFBFo wF0RVLFOjUIizSCvll6msmxJZHBBekegfc+v2sM2RDEUmvwmNnfivursGHmHc8i3ffZD QvvxKLIwQMvOmf3DtmH6lrNZLUV1pmQxbOtA/u7HwXr0/l4PA4SRh7VVmGmubhlvGKKJ BXYA== X-Gm-Message-State: ANhLgQ04Cij+vUm/ExRWGb0q0gczKFpNNm548T4V3O5fXgcAf0aMf/Oo KkFCOHCU6EZHWisqi7UAbn5z0vlw X-Google-Smtp-Source: ADFU+vtiGe7f7iOL7cVA1xMfQZWT4467b0V9dWMjN2DnlPRMx+wQpdkGQiie3u+qzuvl3eMWLAcG1Q== X-Received: by 2002:a37:9f42:: with SMTP id i63mr19791871qke.192.1583846976955; Tue, 10 Mar 2020 06:29:36 -0700 (PDT) Received: from pm2-ws13.praxislan02.com ([2001:470:8:67e:ba27:ebff:fee8:ce27]) by smtp.gmail.com with ESMTPSA id 206sm11184689qkn.36.2020.03.10.06.29.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Mar 2020 06:29:36 -0700 (PDT) From: Jason Andryuk To: xen-devel@lists.xenproject.org Date: Tue, 10 Mar 2020 09:29:04 -0400 Message-Id: <20200310132904.16992-1-jandryuk@gmail.com> X-Mailer: git-send-email 2.24.1 MIME-Version: 1.0 Subject: [Xen-devel] [PATCH] 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 from A, stat prints an error to stderr and exits non-zero. '|| :' is needed to squash the non-zero exit status - otherwise the script terminates since `set -e` is enabled. stderr needs to be redirected to /dev/null otherwise /var/log/xen/xen-hotplug.log will get filled with "No such file or directory" messages. This change removes the only runtime dependency of the xen toolstack on perl. While here, replace some tabs with spaces to match the rest of the file. Suggested-by: Ian Jackson Signed-off-by: Jason Andryuk Acked-by: Wei Liu --- tools/hotplug/Linux/locking.sh | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/tools/hotplug/Linux/locking.sh b/tools/hotplug/Linux/locking.sh index c6a7e96ff9..03361f405f 100644 --- a/tools/hotplug/Linux/locking.sh +++ b/tools/hotplug/Linux/locking.sh @@ -41,7 +41,9 @@ 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 + local fd_stat + local file_stat while true; do eval "exec $_lockfd<>$_lockfile" flock -x $_lockfd || return $? @@ -50,16 +52,20 @@ 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 - # Some versions of bash appear to be buggy if the same - # $_lockfile is opened repeatedly. Close the current fd here. + # 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. + stat=$( stat -L -c '%D.%i' - $_lockfile 0<&$_lockfd 2>/dev/null || : ) + if [ -n "$stat" ]; then + fd_stat=$( echo "$stat" | sed -n '1p' ) + file_stat=$( echo "$stat" | sed -n '2p' ) + 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<&-" done }