Skip to contentSkip to conversation

scripts: Use stat to check lock claim

PATCH In review Jason Andryuk 3 participants 6 messages Mar 12, 2020 – Mar 30, 2020

lore
Share & tools
lore
#0 lore raw

v2 cover scripts: Use stat to check lock claim

This patch series replaces perl with stat to check lock ownership in the Linux hotplug scripts. This removes Xen's runtime dependency on perl.

An additional patch is a tabs to space whitespace cleanup.

Jason Andryuk (2):
scripts: Replace tabs in locking.sh
scripts: Use stat to check lock claim

 tools/hotplug/Linux/locking.sh | 32 +++++++++++++++++++++-----------
 1 file changed, 21 insertions(+), 11 deletions(-)
Signature · 7 lines
2.24.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw

PATCH v2 1/2 scripts: Replace tabs in locking.sh

Replace two stray tabs with spaces to make the file whitespace consistent.

---
 tools/hotplug/Linux/locking.sh | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/hotplug/Linux/locking.sh +2 -2
index c6a7e96ff9..baaaf73508 100644
@@ -58,8 +58,8 @@ claim_lock()
             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.
+        # Some versions of bash appear to be buggy if the same
+        # $_lockfile is opened repeatedly. Close the current fd here.
         eval "exec $_lockfd<&-"
     done
 }
Signature · 7 lines
2.24.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#2 lore raw

PATCH v2 2/2 scripts: Use stat to check lock claim

In reply to Jason Andryuk

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.

---
v2:
Replace sed with bash string manipulation.

---
 tools/hotplug/Linux/locking.sh | 28 +++++++++++++++++++---------
 1 file changed, 19 insertions(+), 9 deletions(-)
tools/hotplug/Linux/locking.sh +19 -9
index baaaf73508..69aefae6f6 100644
@@ -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<&-"
Signature · 7 lines
2.24.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#3 lore raw
Quoted 11 lines · Jason Andryuk

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.

Quoted 8 lines

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.

Ian, ping?

I already applied patch 1. This patch looks to be good as well.

Wei.

Reviewed-by
#4 lore raw

Wei Liu writes ("Re: [PATCH 2/2] scripts: Use stat to check lock claim"):

Quoted 18 lines · Jason Andryuk
Quoted 17 lines

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.

Quoted 6 lines
Quoted 2 lines

Ian, ping?

I already applied patch 1. This patch looks to be good as well.

Sorry. Yes.

#5 lore raw
Quoted 22 lines · Ian Jackson

Wei Liu writes ("Re: [PATCH 2/2] scripts: Use stat to check lock claim"):

Quoted 21 lines · Jason Andryuk
Quoted 19 lines

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.

Quoted 7 lines
Quoted 3 lines

Ian, ping?

I already applied patch 1. This patch looks to be good as well.

Sorry. Yes.

Thanks, I will apply this patch shortly.

Wei.