From nobody Wed May 1 08:35:34 2024 Delivered-To: importer@patchew.org Received-SPF: none (zohomail.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=none (zohomail.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1583847046; cv=none; d=zohomail.com; s=zohoarc; b=hCOm+mvo8S1aXQ6OIUtiGroGmS5j31i2ybkCJX/mP2+qXZFYstEtrAW/bYsqofUeSzyB4vDAe8uT3Ez35Rq20I5jsCP2cfQgq3S/WKRW92h7H12iqWRrz3SqmFEjn8BXVoNJC4xhRGNZlANbvOJcNanYkZgy1vqEtYvUgXuqphg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1583847046; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=Y4GuRR/xfEWfNgsC1cuJ7aB44pMqbvdNZvlZWfbM9xE=; b=iLGeOF87IW245OaTUVAa4nloOgvnwlgg6+VRRqfJuaJX67HmZhkhcW4pPgzYZ3RCiAZSfPPQQz7eYDAbs9ZBx1riOXzD9b6R+XtLlYGra3tflSaA/AUln9n03fbSKtwCURoUpfLSjiNaAELEFL5hMmy+hdS/z0Rq4bHO11B8Dbs= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=none (zohomail.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 15838470462211002.2517672048884; Tue, 10 Mar 2020 06:30:46 -0700 (PDT) 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 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) 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) X-Inumbo-ID: 3056aad4-62d3-11ea-92cf-bc764e2007e4 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) 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 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) 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=3D$( perl -e ' - open STDIN, "<&'$_lockfd'" or die $!; - my $fd_inum =3D (stat STDIN)[1]; die $! unless defined $fd_inu= m; - my $file_inum =3D (stat $ARGV[0])[1]; - print "y\n" if $fd_inum eq $file_inum; - ' "$_lockfile" ) - if [ x$rightfile =3D 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=3D$( stat -L -c '%D.%i' - $_lockfile 0<&$_lockfd 2>/dev/null = || : ) + if [ -n "$stat" ]; then + fd_stat=3D$( echo "$stat" | sed -n '1p' ) + file_stat=3D$( echo "$stat" | sed -n '2p' ) + if [ "$fd_stat" =3D "$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 } --=20 2.24.1 _______________________________________________ Xen-devel mailing list Xen-devel@lists.xenproject.org https://lists.xenproject.org/mailman/listinfo/xen-devel