Skip to contentSkip to conversation

Fix locking in tools/hotplug/Linux/locking.sh

PATCH Applied Jacek Konieczny 4 participants 11 messages Nov 9, 2012 – Nov 14, 2012

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix locking in tools/hotplug/Linux/locking.sh

The claim_lock() function would fail in the perl code with:

Invalid argument at -e line 2.

because the Perl snippet opens for reading the file descriptor, which was earlier opened for write (append).

---
 tools/hotplug/Linux/locking.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/hotplug/Linux/locking.sh +1 -1
index e34f155..122bcfb 100644
@@ -44,7 +44,7 @@ claim_lock()
     # See below for a correctness proof.
     local rightfile
     while true; do
-        eval "exec $_lockfd>>$_lockfile"
+        eval "exec $_lockfd<>$_lockfile"
         flock -x $_lockfd || return $?
         # 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
Signature · 1 line
1.7.12.4
#1 lore raw
Quoted 6 lines · Jacek Konieczny

The claim_lock() function would fail in the perl code with:

Invalid argument at -e line 2.

because the Perl snippet opens for reading the file descriptor, which was earlier opened for write (append).

Looks plausible to me.

How does this interact with the

        eval "exec $_lockfd<&-"

which Olaf added in 26079:b3b03536789a ? I suppose close is close and <&- and >&- or even <>&- are equivalent?

Quoted 19 lines
---
 tools/hotplug/Linux/locking.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/hotplug/Linux/locking.sh +1 -1
index e34f155..122bcfb 100644
@@ -44,7 +44,7 @@ claim_lock()
     # See below for a correctness proof.
     local rightfile
     while true; do
-        eval "exec $_lockfd>>$_lockfile"
+        eval "exec $_lockfd<>$_lockfile"
         flock -x $_lockfd || return $?
         # 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
#2 lore raw
Quoted 8 lines · Ian Campbell
Quoted 6 lines · Jacek Konieczny

The claim_lock() function would fail in the perl code with:

Invalid argument at -e line 2.

because the Perl snippet opens for reading the file descriptor, which was earlier opened for write (append).

Quoted 6 lines

Looks plausible to me.

How does this interact with the

        eval "exec $_lockfd<&-"

which Olaf added in 26079:b3b03536789a ? I suppose close is close and <&- and >&- or even <>&- are equivalent?

It seems <&- and >&- are indeed the same and there is no such thing as '<>&-'.

I have added the Olaf's change to my test setup and it works properly. So no conflict here.

Quoted 19 lines
Quoted 19 lines
---
 tools/hotplug/Linux/locking.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/hotplug/Linux/locking.sh +1 -1
index e34f155..122bcfb 100644
@@ -44,7 +44,7 @@ claim_lock()
     # See below for a correctness proof.
     local rightfile
     while true; do
-        eval "exec $_lockfd>>$_lockfile"
+        eval "exec $_lockfd<>$_lockfile"
         flock -x $_lockfd || return $?
         # 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
#3 lore raw
In reply to Ian Campbell
Quoted 8 lines · Ian Campbell
Quoted 6 lines · Jacek Konieczny

The claim_lock() function would fail in the perl code with:

Invalid argument at -e line 2.

because the Perl snippet opens for reading the file descriptor, which was earlier opened for write (append).

Quoted 6 lines

Looks plausible to me.

How does this interact with the

        eval "exec $_lockfd<&-"

which Olaf added in 26079:b3b03536789a ? I suppose close is close and <&- and >&- or even <>&- are equivalent?

I did not see the perl error, in what environment did you notice this error?

Olaf

#4 lore raw
Quoted 12 lines · Olaf Hering
Quoted 10 lines · Ian Campbell
Quoted 6 lines · Jacek Konieczny

The claim_lock() function would fail in the perl code with:

Invalid argument at -e line 2.

because the Perl snippet opens for reading the file descriptor, which was earlier opened for write (append).

Looks plausible to me.

Quoted 7 lines
Quoted 4 lines

How does this interact with the

        eval "exec $_lockfd<&-"

which Olaf added in 26079:b3b03536789a ? I suppose close is close and <&- and >&- or even <>&- are equivalent?

I did not see the perl error, in what environment did you notice this error?

Xen 4.2.0

# uname -rm
3.6.6-aos4 x86_64
# perl --version
This is perl, v5.8.8 built for x86_64-pld-linux-thread-multi

I find it a bit strange that it seems to work for everybody else that me, but the source of my error seems obvious (opening write-only file descriptor for reading).

I guess my perl, for some reason, does something more when using open() on an existing file descriptor.

Greets,
Jacek

#5 lore raw
Quoted 2 lines · Jacek Konieczny
# perl --version
This is perl, v5.8.8 built for x86_64-pld-linux-thread-multi

sles11sp2 has perl 5.10.0, maybe there is different behaviour in different perl versions.

Olaf

#6 lore raw
Quoted 7 lines · Olaf Hering
Quoted 2 lines · Jacek Konieczny
# perl --version
This is perl, v5.8.8 built for x86_64-pld-linux-thread-multi

sles11sp2 has perl 5.10.0, maybe there is different behaviour in different perl versions.

More likely to be the kernel, since Perl would have to jump through hoops to figure out if a random fd which it got passed was r/o or r/w, I think.

Perhaps its a security thing (e.g. selinux or something like that) enforcing something extra?

Might be interesting to run things under strace ? Per the attached t.sh. Which for me produces the below without complaint:

Ian.

+ _lockfd=42
+ _lockfile=/tmp/lockfile
+ eval 'exec 42>>/tmp/lockfile'
++ exec
+ flock -x 42
++ strace perl -e '

            open STDIN, "<&42" 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;
                             ' /tmp/lockfile

execve("/usr/bin/perl", ["perl", "-e", "\n open STDIN, \"<&42\" "..., "/tmp/lockfile"], [/* 35 vars */]) = 0
brk(0) = 0xea0000
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f08728d7000
access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=68197, ...}) = 0
mmap(NULL, 68197, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f08728c6000
close(3) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
open("/usr/lib/libperl.so.5.10", O_RDONLY) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\220\222\3\0\0\0\0\0"..., 832) = 832 fstat(3, {st_mode=S_IFREG|0644, st_size=1495208, ...}) = 0
mmap(NULL, 3590856, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f087234f000 mprotect(0x7f08724b4000, 2093056, PROT_NONE) = 0
mmap(0x7f08726b3000, 36864, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x164000) = 0x7f08726b3000

close(3)                                = 0
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
open("/lib/libdl.so.2", O_RDONLY)       = 3

read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\340\r\0\0\0\0\0\0"..., 832) = 832 fstat(3, {st_mode=S_IFREG|0644, st_size=14696, ...}) = 0
mmap(NULL, 2109696, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f087214b000 mprotect(0x7f087214d000, 2097152, PROT_NONE) = 0
mmap(0x7f087234d000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x2000) = 0x7f087234d000

close(3)                                = 0
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
open("/lib/libm.so.6", O_RDONLY)        = 3

read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\360>\0\0\0\0\0\0"..., 832) = 832 fstat(3, {st_mode=S_IFREG|0644, st_size=530736, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f08728c5000 mmap(NULL, 2625768, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f0871ec9000 mprotect(0x7f0871f49000, 2097152, PROT_NONE) = 0
mmap(0x7f0872149000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x80000) = 0x7f0872149000

close(3)                                = 0
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
open("/lib/libpthread.so.0", O_RDONLY)  = 3

read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\360Y\0\0\0\0\0\0"..., 832) = 832 fstat(3, {st_mode=S_IFREG|0755, st_size=131258, ...}) = 0
mmap(NULL, 2208640, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f0871cad000 mprotect(0x7f0871cc4000, 2093056, PROT_NONE) = 0
mmap(0x7f0871ec3000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x16000) = 0x7f0871ec3000 mmap(0x7f0871ec5000, 13184, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f0871ec5000

close(3)                                = 0
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
open("/lib/libc.so.6", O_RDONLY)        = 3

read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\240\355\1\0\0\0\0\0"..., 832) = 832 fstat(3, {st_mode=S_IFREG|0755, st_size=1437064, ...}) = 0
mmap(NULL, 3545160, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f087194b000 mprotect(0x7f0871aa4000, 2093056, PROT_NONE) = 0

mmap(0x7f0871ca3000, 20480, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x158000) = 0x7f0871ca3000
mmap(0x7f0871ca8000, 18504, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f0871ca8000
close(3)                                = 0
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
open("/lib/libcrypt.so.1", O_RDONLY)    = 3

read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0P\n\0\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0644, st_size=35104, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f08728c4000 mmap(NULL, 2318784, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f0871714000 mprotect(0x7f087171c000, 2093056, PROT_NONE) = 0
mmap(0x7f087191b000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x7000) = 0x7f087191b000 mmap(0x7f087191d000, 184768, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f087191d000
close(3) = 0

mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f08728c3000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f08728c2000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f08728c1000

arch_prctl(ARCH_SET_FS, 0x7f08728c2700) = 0
mprotect(0x7f087191b000, 4096, PROT_READ) = 0
mprotect(0x7f0871ca3000, 16384, PROT_READ) = 0
mprotect(0x7f0871ec3000, 4096, PROT_READ) = 0
mprotect(0x7f0872149000, 4096, PROT_READ) = 0
mprotect(0x7f087234d000, 4096, PROT_READ) = 0
mprotect(0x7f08728d9000, 4096, PROT_READ) = 0

munmap(0x7f08728c6000, 68197)           = 0
set_tid_address(0x7f08728c29d0)         = 17761
set_robust_list(0x7f08728c29e0, 0x18)   = 0

futex(0x7fffbb03b91c, FUTEX_WAKE_PRIVATE, 1) = 0
futex(0x7fffbb03b91c, FUTEX_WAIT_BITSET_PRIVATE|FUTEX_CLOCK_REALTIME, 1, NULL, 7f08728c2700) = -1 EAGAIN (Resource temporarily unavailable) rt_sigaction(SIGRTMIN, {0x7f0871cb2870, [], SA_RESTORER|SA_SIGINFO, 0x7f0871cbbff0}, NULL, 8) = 0 rt_sigaction(SIGRT_1, {0x7f0871cb2900, [], SA_RESTORER|SA_RESTART|SA_SIGINFO, 0x7f0871cbbff0}, NULL, 8) = 0 rt_sigprocmask(SIG_UNBLOCK, [RTMIN RT_1], NULL, 8) = 0
getrlimit(RLIMIT_STACK, {rlim_cur=8192*1024, rlim_max=RLIM_INFINITY}) = 0
rt_sigaction(SIGFPE, {SIG_IGN, [FPE], SA_RESTORER|SA_RESTART, 0x7f087197d230}, {SIG_DFL, [], 0}, 8) = 0

brk(0)                                  = 0xea0000
brk(0xec1000)                           = 0xec1000
getuid()                                = 1161
geteuid()                               = 1161
getgid()                                = 10020
getegid()                               = 10020
open("/usr/lib/locale/locale-archive", O_RDONLY) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=2018512, ...}) = 0
mmap(NULL, 2018512, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f0871527000
close(3)                                = 0
open("/dev/urandom", O_RDONLY)          = 3
read(3, "\346\340\354H", 4)             = 4
close(3)                                = 0

readlink("/proc/self/exe", "/usr/bin/perl", 4095) = 13 stat("/usr/local/lib/site_perl/5.10.1/x86_64-linux-gnu-thread-multi", 0x7fffbb03b390) = -1 ENOENT (No such file or directory) stat("/usr/local/lib/site_perl/5.10.1", 0x7fffbb03b390) = -1 ENOENT (No such file or directory) stat("/usr/local/lib/site_perl/x86_64-linux-gnu-thread-multi", 0x7fffbb03b390) = -1 ENOENT (No such file or directory) stat("/usr/local/lib/perl/5.10.0", 0x7fffbb03b4d0) = -1 ENOENT (No such file or directory) stat("/usr/local/share/perl/5.10.0", 0x7fffbb03b4d0) = -1 ENOENT (No such file or directory)
ioctl(0, SNDCTL_TMR_TIMEBASE or TCGETS, {B38400 opost isig icanon echo ...}) = 0
lseek(0, 0, SEEK_CUR) = -1 ESPIPE (Illegal seek)
ioctl(1, SNDCTL_TMR_TIMEBASE or TCGETS, 0x7fffbb03b260) = -1 EINVAL (Invalid argument)
lseek(1, 0, SEEK_CUR) = -1 ESPIPE (Illegal seek)
ioctl(2, SNDCTL_TMR_TIMEBASE or TCGETS, 0x7fffbb03b270) = -1 ENOTTY (Inappropriate ioctl for device)
lseek(2, 0, SEEK_CUR) = 7954
open("/dev/null", O_RDONLY) = 3
ioctl(3, SNDCTL_TMR_TIMEBASE or TCGETS, 0x7fffbb03b390) = -1 ENOTTY (Inappropriate ioctl for device)
lseek(3, 0, SEEK_CUR) = 0
fcntl(3, F_SETFD, FD_CLOEXEC) = 0
fstat(3, {st_mode=S_IFCHR|0666, st_rdev=makedev(1, 3), ...}) = 0
rt_sigaction(SIGCHLD, NULL, {SIG_DFL, [], 0}, 8) = 0

brk(0xee2000)                           = 0xee2000
close(3)                                = 0
dup(42)                                 = 3
ioctl(3, SNDCTL_TMR_TIMEBASE or TCGETS, 0x7fffbb03b320) = -1 ENOTTY (Inappropriate ioctl for device)
lseek(3, 0, SEEK_CUR)                   = 0
fstat(3, {st_mode=S_IFREG|0644, st_size=0, ...}) = 0
dup2(3, 0)                              = 0
close(3)                                = 0
fcntl(0, F_SETFD, 0)                    = 0

fstat(0, {st_mode=S_IFREG|0644, st_size=0, ...}) = 0
stat("/tmp/lockfile", {st_mode=S_IFREG|0644, st_size=0, ...}) = 0
rt_sigaction(SIG_0, NULL, {0x7fffbb03b810, ~[HUP INT QUIT TRAP BUS FPE USR1 SEGV USR2 TERM CONT TTIN TTOU URG XFSZ PROF WINCH RTMIN RT_1 RT_2 RT_3 RT_5 RT_6 RT_7 RT_8 RT_16 RT_17 RT_18 RT_19 RT_20 RT_21 RT_22 RT_23 RT_24 RT_25 RT_26 RT_27 RT_28 RT_29 RT_30 RT_31], 0}, 8) = -1 EINVAL (Invalid argument) rt_sigaction(SIGHUP, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGINT, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGQUIT, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGILL, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGTRAP, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGABRT, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGBUS, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGFPE, NULL, {SIG_IGN, [FPE], SA_RESTORER|SA_RESTART, 0x7f087197d230}, 8) = 0
rt_sigaction(SIGKILL, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGUSR1, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGSEGV, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGUSR2, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGPIPE, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGALRM, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGTERM, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGSTKFLT, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGCHLD, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGCONT, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGSTOP, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGTSTP, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGTTIN, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGTTOU, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGURG, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGXCPU, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGXFSZ, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGVTALRM, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGPROF, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGWINCH, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGIO, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGPWR, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGSYS, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGRT_2, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGRT_3, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGRT_4, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGRT_5, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGRT_6, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGRT_7, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGRT_8, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGRT_9, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGRT_10, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGRT_11, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGRT_12, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGRT_13, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGRT_14, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGRT_15, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGRT_16, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGRT_17, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGRT_18, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGRT_19, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGRT_20, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGRT_21, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGRT_22, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGRT_23, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGRT_24, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGRT_25, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGRT_26, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGRT_27, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGRT_28, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGRT_29, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGRT_30, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGRT_31, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGRT_32, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGABRT, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGCHLD, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGIO, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGSYS, NULL, {SIG_DFL, [], 0}, 8) = 0
write(1, "y\n", 2) = 2
exit_group(0) = ?
+ rightfile=y
+ echo y

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#7 lore raw
Quoted 6 lines · Ian Campbell
Quoted 5 lines · Olaf Hering
Quoted 2 lines · Jacek Konieczny
# perl --version
This is perl, v5.8.8 built for x86_64-pld-linux-thread-multi
Quoted 6 lines
Quoted 2 lines

sles11sp2 has perl 5.10.0, maybe there is different behaviour in different perl versions.

More likely to be the kernel, since Perl would have to jump through hoops to figure out if a random fd which it got passed was r/o or r/w, I think.

I suspected my perl just checks the file descriptor which is to be opened for reading, and it seems to be the case.

…however, after looking into Perl source code it seems it might be glibc and its fdopen() implementation, as Perl calls fdopen() (unless 'sfio' is used instead of 'stdio').

And from the fdopen(3) manual:

Quoted 2 lines

The mode of the stream (one of the values "r", "r+", "w", "w+", "a", "a+") must be compatible with the mode of the file descriptor.

# rpm -q glibc
glibc-2.15-10.aos1.x86_64
Quoted 2 lines

Perhaps its a security thing (e.g. selinux or something like that) enforcing something extra?

Nothing like that here.

Quoted 2 lines

Might be interesting to run things under strace ?

Here you are, (notice the fcntl(3, F_GETFL) call before the error):

+ _lockfd=42
+ _lockfile=/tmp/lockfile
+ eval 'exec 42>>/tmp/lockfile'
++ exec
+ flock -x 42
++ strace perl -e '

            open STDIN, "<&42" 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;
                             ' /tmp/lockfile

execve("/usr/bin/perl", ["perl", "-e", "\n open STDIN, \"<&42\" "..., "/tmp/lockfile"], [/* 28 vars */]) = 0
brk(0) = 0x1c01000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f217ae6c000
access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
open("/usr/lib64/perl5/5.8.8/x86_64-pld-linux-thread-multi/CORE/tls/x86_64/libperl.so.5.8.0", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) stat("/usr/lib64/perl5/5.8.8/x86_64-pld-linux-thread-multi/CORE/tls/x86_64", 0x7fff5242d820) = -1 ENOENT (No such file or directory) open("/usr/lib64/perl5/5.8.8/x86_64-pld-linux-thread-multi/CORE/tls/libperl.so.5.8.0", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) stat("/usr/lib64/perl5/5.8.8/x86_64-pld-linux-thread-multi/CORE/tls", 0x7fff5242d820) = -1 ENOENT (No such file or directory) open("/usr/lib64/perl5/5.8.8/x86_64-pld-linux-thread-multi/CORE/x86_64/libperl.so.5.8.0", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) stat("/usr/lib64/perl5/5.8.8/x86_64-pld-linux-thread-multi/CORE/x86_64", 0x7fff5242d820) = -1 ENOENT (No such file or directory) open("/usr/lib64/perl5/5.8.8/x86_64-pld-linux-thread-multi/CORE/libperl.so.5.8.0", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0@Y\3\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=1425032, ...}) = 0
mmap(NULL, 3528768, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f217a8ee000
mprotect(0x7f217aa40000, 2093056, PROT_NONE) = 0
mmap(0x7f217ac3f000, 45056, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x151000) = 0x7f217ac3f000 mmap(0x7f217ac4a000, 6208, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f217ac4a000
close(3) = 0
open("/usr/lib64/perl5/5.8.8/x86_64-pld-linux-thread-multi/CORE/libdl.so.2", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=24104, ...}) = 0
mmap(NULL, 24104, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f217ae66000
close(3) = 0
open("/lib64/libdl.so.2", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\20\16\0\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=14672, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f217ae65000
mmap(NULL, 2109688, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f217a6ea000
mprotect(0x7f217a6ec000, 2097152, PROT_NONE) = 0
mmap(0x7f217a8ec000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x2000) = 0x7f217a8ec000
close(3) = 0
open("/usr/lib64/perl5/5.8.8/x86_64-pld-linux-thread-multi/CORE/libm.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) open("/lib64/libm.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\260U\0\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=997640, ...}) = 0
mmap(NULL, 3092776, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f217a3f6000
mprotect(0x7f217a4e9000, 2093056, PROT_NONE) = 0
mmap(0x7f217a6e8000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0xf2000) = 0x7f217a6e8000
close(3) = 0
open("/usr/lib64/perl5/5.8.8/x86_64-pld-linux-thread-multi/CORE/libpthread.so.0", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) open("/lib64/libpthread.so.0", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0`m\0\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=135433, ...}) = 0
mmap(NULL, 2212784, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f217a1d9000
mprotect(0x7f217a1f1000, 2093056, PROT_NONE) = 0
mmap(0x7f217a3f0000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x17000) = 0x7f217a3f0000 mmap(0x7f217a3f2000, 13232, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f217a3f2000
close(3) = 0
open("/usr/lib64/perl5/5.8.8/x86_64-pld-linux-thread-multi/CORE/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) open("/lib64/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\320\25\2\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=1728816, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f217ae64000
mmap(NULL, 3841120, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f2179e2f000
mprotect(0x7f2179fcf000, 2097152, PROT_NONE) = 0

mmap(0x7f217a1cf000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1a0000) = 0x7f217a1cf000
mmap(0x7f217a1d5000, 15456, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f217a1d5000

close(3) = 0
open("/usr/lib64/perl5/5.8.8/x86_64-pld-linux-thread-multi/CORE/libcrypt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) open("/lib64/libcrypt.so.1", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\260\n\0\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=47280, ...}) = 0
mmap(NULL, 2331072, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f2179bf5000
mprotect(0x7f2179c00000, 2093056, PROT_NONE) = 0
mmap(0x7f2179dff000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0xa000) = 0x7f2179dff000 mmap(0x7f2179e01000, 184768, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f2179e01000
close(3) = 0

mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f217ae63000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f217ae62000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f217ae61000

arch_prctl(ARCH_SET_FS, 0x7f217ae62700) = 0
mprotect(0x7f217a1cf000, 16384, PROT_READ) = 0
mprotect(0x7f2179dff000, 4096, PROT_READ) = 0
mprotect(0x7f217a3f0000, 4096, PROT_READ) = 0
mprotect(0x7f217a6e8000, 4096, PROT_READ) = 0
mprotect(0x7f217a8ec000, 4096, PROT_READ) = 0
mprotect(0x7f217ac3f000, 12288, PROT_READ) = 0
mprotect(0x603000, 4096, PROT_READ) = 0
mprotect(0x7f217ae6d000, 4096, PROT_READ) = 0

munmap(0x7f217ae66000, 24104)           = 0
set_tid_address(0x7f217ae629d0)         = 30903
set_robust_list(0x7f217ae629e0, 0x18)   = 0

futex(0x7fff5242e07c, FUTEX_WAKE_PRIVATE, 1) = 0
futex(0x7fff5242e07c, FUTEX_WAIT_BITSET_PRIVATE|FUTEX_CLOCK_REALTIME, 1, NULL, 7f217ae62700) = -1 EAGAIN (Resource temporarily unavailable) rt_sigaction(SIGRTMIN, {0x7f217a1df7c0, [], SA_RESTORER|SA_SIGINFO, 0x7f217a1e8de0}, NULL, 8) = 0 rt_sigaction(SIGRT_1, {0x7f217a1df850, [], SA_RESTORER|SA_RESTART|SA_SIGINFO, 0x7f217a1e8de0}, NULL, 8) = 0 rt_sigprocmask(SIG_UNBLOCK, [RTMIN RT_1], NULL, 8) = 0
getrlimit(RLIMIT_STACK, {rlim_cur=8192*1024, rlim_max=RLIM_INFINITY}) = 0
rt_sigaction(SIGFPE, {SIG_IGN, [FPE], SA_RESTORER|SA_RESTART, 0x7f2179e63d00}, {SIG_DFL, [], 0}, 8) = 0

brk(0)                                  = 0x1c01000
brk(0x1c23000)                          = 0x1c23000
getuid()                                = 0
geteuid()                               = 0
getgid()                                = 0
getegid()                               = 0

open("/usr/lib64/locale/locale-archive", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=2223712, ...}) = 0
mmap(NULL, 2223712, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f21799d6000
close(3) = 0
mmap(NULL, 135168, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f217ae40000

open("/dev/urandom", O_RDONLY)          = 3
read(3, "^nbc", 4)                      = 4
close(3)                                = 0
open("/dev/null", O_RDONLY)             = 3
fcntl(3, F_SETFD, FD_CLOEXEC)           = 0

fstat(3, {st_mode=S_IFCHR|0666, st_rdev=makedev(1, 3), ...}) = 0
rt_sigaction(SIGCHLD, NULL, {SIG_DFL, [], 0}, 8) = 0
readlink("/proc/self/exe", "/usr/bin/perl5.8.8", 4095) = 18
brk(0x1c44000) = 0x1c44000
getppid() = 30902
fstat(3, {st_mode=S_IFCHR|0666, st_rdev=makedev(1, 3), ...}) = 0
ioctl(3, SNDCTL_TMR_TIMEBASE or SNDRV_TIMER_IOCTL_NEXT_DEVICE or TCGETS, 0x7fff5242d618) = -1 ENOTTY (Inappropriate ioctl for device) mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f217ae6b000

lseek(3, 0, SEEK_CUR)                   = 0
lseek(3, 0, SEEK_SET)                   = 0
close(3)                                = 0
munmap(0x7f217ae6b000, 4096)            = 0
dup(42)                                 = 3
fcntl(3, F_GETFL)                       = 0x8401 (flags O_WRONLY|O_APPEND|O_LARGEFILE)
close(3)                                = 0

write(2, "Invalid argument at -e line 2.\n", 31Invalid argument at -e line 2. ) = 31
exit_group(22) = ?
+ rightfile=

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

Acked-by
#8 lore raw

Jacek Konieczny writes ("Re: [Xen-devel] [PATCH] Fix locking in tools/hotplug/Linux/locking.sh"):

Quoted 1 line

fcntl(3, F_GETFL) = 0x8401 (flags O_WRONLY|O_APPEND|O_LARGEFILE)

So it seems perl is going out of its way to spot this earlier. That explains the mystery. Anyway the patch is fine.

Acked-by
#9 lore raw
Quoted 5 lines · Ian Jackson

Jacek Konieczny writes ("Re: [Xen-devel] [PATCH] Fix locking in tools/hotplug/Linux/locking.sh"):

Quoted 1 line

fcntl(3, F_GETFL) = 0x8401 (flags O_WRONLY|O_APPEND|O_LARGEFILE)

So it seems perl is going out of its way to spot this earlier. That explains the mystery.

Looking at glibc it seems like it has had this test since the mid-nineties, so quite why we aren't seeing it elsewhere I can't explain (maybe bash buggily opens the fd RDWR?)

Quoted 3 lines

Anyway the patch is fine.

Yes, irrespective of why this patch does seem to be correct: