Skip to contentSkip to conversation

Remove locking.sh dependency on perl

PATCH In review Jason Andryuk 3 participants 8 messages Feb 26, 2020 – Mar 6, 2020

lore
Share & tools
lore
#0 lore raw

cover Remove locking.sh dependency on perl

Perl is a large dependency for locking.sh's single use of comparing a file descriptor's and a file's inodes. Many systems don't otherwise require perl, so dropping this use eliminates the dependency.

Replace the open-coded perl with an equivalent C implementation.

Jason Andryuk (2):
tools/helpers: Introduce cmp-fd-file-inode utility Linux/locking.sh: Use cmp-fd-file-inode for lock check

 .gitignore                        |  1 +
 tools/helpers/Makefile            |  3 +++
 tools/helpers/cmp-fd-file-inode.c | 43 +++++++++++++++++++++++++++++++
 tools/hotplug/Linux/locking.sh    | 10 ++-----
 4 files changed, 49 insertions(+), 8 deletions(-)
 create mode 100644 tools/helpers/cmp-fd-file-inode.c
Signature · 7 lines
2.24.1


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

PATCH 1/2 tools/helpers: Introduce cmp-fd-file-inode utility

This is a C implementation of the perl code inside of locking.sh to check that the locked file descriptor and lock file share the same inode and therefore match. One change from the perl version is replacing printing "y" on success with exit values of 0 (shell True) and 1 (shell False).

Requiring perl is a large dependency for the single use, so a dedicated utility removes that dependency for systems that otherwise would not install perl.

---
 .gitignore                        |  1 +
 tools/helpers/Makefile            |  3 +++
 tools/helpers/cmp-fd-file-inode.c | 43 +++++++++++++++++++++++++++++++
 3 files changed, 47 insertions(+)
 create mode 100644 tools/helpers/cmp-fd-file-inode.c
.gitignore +1 -0
index 4ca679ddbc..897f878eef 100644
@@ -164,6 +164,7 @@ tools/fuzz/x86_instruction_emulator/x86-emulate.[ch]
 tools/helpers/_paths.h
 tools/helpers/init-xenstore-domain
 tools/helpers/xen-init-dom0
+tools/helpers/cmp-fd-file-inode
 tools/hotplug/common/hotplugpath.sh
 tools/hotplug/FreeBSD/rc.d/xencommons
 tools/hotplug/FreeBSD/rc.d/xendriverdomain
tools/helpers/Makefile +3 -0
index f759528322..7daf5c46ca 100644
@@ -8,6 +8,7 @@ include $(XEN_ROOT)/tools/Rules.mk
 PROGS += xen-init-dom0
 ifeq ($(CONFIG_Linux),y)
 PROGS += init-xenstore-domain
+PROGS += cmp-fd-file-inode
 endif
 
 XEN_INIT_DOM0_OBJS = xen-init-dom0.o init-dom-json.o
@@ -40,12 +41,14 @@ install: all
 	$(INSTALL_PROG) xen-init-dom0 $(DESTDIR)$(LIBEXEC_BIN)
 ifeq ($(CONFIG_Linux),y)
 	$(INSTALL_PROG) init-xenstore-domain $(DESTDIR)$(LIBEXEC_BIN)
+	$(INSTALL_PROG) cmp-fd-file-inode $(DESTDIR)$(LIBEXEC_BIN)
 endif
 
 .PHONY: uninstall
 uninstall:
 ifeq ($(CONFIG_Linux),y)
 	rm -f $(DESTDIR)$(LIBEXEC_BIN)/init-xenstore-domain
+	rm -f $(DESTDIR)$(LIBEXEC_BIN)/cmp-fd-file-inode
 endif
 	rm -f $(DESTDIR)$(LIBEXEC_BIN)/xen-init-dom0
 
tools/helpers/cmp-fd-file-inode.c new file +43 -0

43 lines not on this page · raw message

Signature · 7 lines
2.24.1


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

Re: [Xen-devel] [PATCH 1/2] tools/helpers: Introduce cmp-fd-file-inode utility

Jason Andryuk writes ("[PATCH 1/2] tools/helpers: Introduce cmp-fd-file-inode utility"):

Quoted 5 lines

This is a C implementation of the perl code inside of locking.sh to check that the locked file descriptor and lock file share the same inode and therefore match. One change from the perl version is replacing printing "y" on success with exit values of 0 (shell True) and 1 (shell False).

Maybe it would be better to use stat(1) ? On Linux
stat -L -c%D.%i /dev/stdin blah.lock
or some such, and then compare the two numbers.

I'm reluctant to host a general-purpose shell utility in xen.git, no matter how useful...

Thanks,
Ian.

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

#3 lore raw

Re: [Xen-devel] [PATCH 1/2] tools/helpers: Introduce cmp-fd-file-inode utility

Quoted 8 lines · Ian Jackson

Jason Andryuk writes ("[PATCH 1/2] tools/helpers: Introduce cmp-fd-file-inode utility"):

Quoted 5 lines

This is a C implementation of the perl code inside of locking.sh to check that the locked file descriptor and lock file share the same inode and therefore match. One change from the perl version is replacing printing "y" on success with exit values of 0 (shell True) and 1 (shell False).

Quoted 6 lines

Maybe it would be better to use stat(1) ? On Linux
stat -L -c%D.%i /dev/stdin blah.lock
or some such, and then compare the two numbers.

I'm reluctant to host a general-purpose shell utility in xen.git, no matter how useful...

Thanks for taking a look.

I'd be happy to use stat if it works. The comment in locking.sh above the usage is:
# We can't just stat /dev/stdin or /proc/self/fd/$_lockfd or # use bash's test -ef because those all go through what is # 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.

...which I thought ruled out stat.

Regards,
Jason

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

#4 lore raw

Re: [Xen-devel] [PATCH 1/2] tools/helpers: Introduce cmp-fd-file-inode utility

Jason Andryuk writes ("Re: [PATCH 1/2] tools/helpers: Introduce cmp-fd-file-inode utility"):

Quoted 10 lines

I'd be happy to use stat if it works. The comment in locking.sh above the usage is:
# We can't just stat /dev/stdin or /proc/self/fd/$_lockfd or # use bash's test -ef because those all go through what is # 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.

...which I thought ruled out stat.

Well read.

I have done some more testing and in my tests (on Debian stretch)
stat -L - <some-file
does this

    fstat64(0, {st_mode=S_IFREG|0664, st_size=117844, ...}) = 0

(according to strace) which is precisely what is needed.

Oddly, it also does this

    fstat64(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 171), ...}) = 0

but it doesn't seem to do anything with the results, so I think that's harmless.

I wrote that comment in 2012. Presumably `stat -L -' has appeared in the meantime.

The synthetic symlink may be a red herring anyway; nowadays at least, I am told by someone who read the Linux kernel source that
the name comes from the readlink' method on the link inode, but a different method entirely -- get_link' -- is used by `namei' to actually resolve the link to a destination inode.

But using `-' is clearly fine, like this I think:

mariner:~> stat -c%D.%i -L - t <t
fe04.844307
fe04.844307
mariner:~>

Sorry to muddy the waters.

Ian.

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

#5 lore raw

Re: [Xen-devel] [PATCH 1/2] tools/helpers: Introduce cmp-fd-file-inode utility

Quoted 18 lines · Ian Jackson

Jason Andryuk writes ("Re: [PATCH 1/2] tools/helpers: Introduce cmp-fd-file-inode utility"):

Quoted 10 lines

I'd be happy to use stat if it works. The comment in locking.sh above the usage is:
# We can't just stat /dev/stdin or /proc/self/fd/$_lockfd or # use bash's test -ef because those all go through what is # 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.

...which I thought ruled out stat.

Well read.

I have done some more testing and in my tests (on Debian stretch)
stat -L - <some-file
does this

Quoted 7 lines
    fstat64(0, {st_mode=S_IFREG|0664, st_size=117844, ...}) = 0

(according to strace) which is precisely what is needed.

Oddly, it also does this

    fstat64(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 171), ...}) = 0

but it doesn't seem to do anything with the results, so I think that's harmless.

I think this stat is from glibc before printing. stat file - re-orders the fstat(1) before fstat(0).

Quoted 2 lines

I wrote that comment in 2012. Presumably `stat -L -' has appeared in the meantime.

I peaked at coreutils git, and - was added in 2009. But I had no idea of the magic '-'. Thank you for finding it.

Quoted 6 lines

The synthetic symlink may be a red herring anyway; nowadays at least, I am told by someone who read the Linux kernel source that
the name comes from the readlink' method on the link inode, but a different method entirely -- get_link' -- is used by `namei' to actually resolve the link to a destination inode.

Quoted 8 lines

But using `-' is clearly fine, like this I think:

mariner:~> stat -c%D.%i -L - t <t
fe04.844307
fe04.844307
mariner:~>

Sorry to muddy the waters.

Thanks again for finding the solution.

Regards,
Jason

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

#6 lore raw

Re: [Xen-devel] [PATCH 1/2] tools/helpers: Introduce cmp-fd-file-inode utility

In reply to Ian Jackson
Quoted 8 lines · Ian Jackson

Jason Andryuk writes ("[PATCH 1/2] tools/helpers: Introduce cmp-fd-file-inode utility"):

Quoted 5 lines

This is a C implementation of the perl code inside of locking.sh to check that the locked file descriptor and lock file share the same inode and therefore match. One change from the perl version is replacing printing "y" on success with exit values of 0 (shell True) and 1 (shell False).

Quoted 7 lines

Maybe it would be better to use stat(1) ? On Linux
stat -L -c%D.%i /dev/stdin blah.lock
or some such, and then compare the two numbers.

I'm reluctant to host a general-purpose shell utility in xen.git, no matter how useful...

Do you have any other suggestions?

I agree it's not great to have loads of little helper programs lying around. But it's a lot better than pulling in a full perl installation for a single line.

I sort of feel like part of the issue is that this is written in shell at all. The necessity to fall back to perl seems to me to indicate that bash is the wrong language for what needs to happen here. If locking.sh were locking.c instead, this entire series probably wouldn't be necessary.

If no better options are forthcoming, I think we should accept something like this until something better comes along.

-George

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

#7 lore raw

PATCH 2/2 Linux/locking.sh: Use cmp-fd-file-inode for lock check

In reply to Jason Andryuk

Replace perl with cmp-fd-file-inode when checking that the lock file descriptor and lockfile inodes match.

---
 tools/hotplug/Linux/locking.sh | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)
tools/hotplug/Linux/locking.sh +2 -8
index c6a7e96ff9..de468c4bb5 100644
@@ -50,14 +50,8 @@ 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.
+        if cmp-fd-file-inode $_lockfd $_lockfile ; then break; 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