All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Daniel P. Berrange" <berrange@redhat.com>
To: Ian Jackson <Ian.Jackson@eu.citrix.com>
Cc: "xen-devel@lists.xensource.com" <xen-devel@lists.xensource.com>,
	Zhigang Wang <zhigang.x.wang@oracle.com>,
	Ian Campbell <Ian.Campbell@citrix.com>
Subject: Re: [xen-unstable test] 13461: regressions - FAIL
Date: Thu, 5 Jul 2012 11:46:11 +0100	[thread overview]
Message-ID: <20120705104611.GG13884@redhat.com> (raw)
In-Reply-To: <20469.28420.49684.132247@mariner.uk.xensource.com>

On Thu, Jul 05, 2012 at 11:40:04AM +0100, Ian Jackson wrote:
> Daniel P. Berrange writes ("Re: [Xen-devel] [xen-unstable test] 13461: regressions - FAIL"):
> > Yes, as you say flock() operates on the inode, so if something deletes
> > and recreates the file, future flocks will operate differently. Ideally
> > you should just never rm the files at all.
> > 
> > If you need to 'rm' them, then to avoid this, you must do two things
> > 
> >  - Only 'rm /foo' while holding the lock on /foo
> >  - Record the inode before acquiring the lock. After acquiring the
> >    lock check whether the inode on disk is the same. If not,
> >    release the lock & repeat.
> 
> It seems more logical to me to check the inum of the open fd against
> the file.  Something like this perhaps (untested):

Yes, using the open fd is actually what I should have said.

> 
> diff -r ad08cd8e7097 tools/hotplug/Linux/locking.sh
> --- a/tools/hotplug/Linux/locking.sh	Thu Jul 05 11:00:28 2012 +0100
> +++ b/tools/hotplug/Linux/locking.sh	Thu Jul 05 11:39:59 2012 +0100
> @@ -30,6 +30,7 @@ _setlockfd()
>      done
>      _lockdict[$i]="$1"
>      let _lockfd=200+i
> +    let _lockfile="$LOCK_BASEDIR/$1"
>  }
>  
>  
> @@ -37,13 +38,32 @@ claim_lock()
>  {
>      mkdir -p "$LOCK_BASEDIR"
>      _setlockfd $1
> -    eval "exec $_lockfd>>$LOCK_BASEDIR/$1"
> -    flock -x $_lockfd
> +    # The locking strategy is identical to that from with-lock-ex(1)
> +    # from chiark-utils, except using flock.  It has the benefit of
> +    # it being possible to safely remove the lockfile when done.
> +    local rightfile
> +    while true; do
> +        eval "exec $_lockfd>>$lockfile"
> +        flock -x $_lockfd
> +        # 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.
> +        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
> +    done
>  }
>  
>  
>  release_lock()
>  {
>      _setlockfd $1
> -    flock -u $_lockfd
> +    rm "$_lockfile"

I think you still want the 'flock' line here, but have it after the
'rm' line. Otherwise you leave the $_lockfd filehandle open. Yes,
I know the calling script will probably just exit, but it doesn't
hurt to be careful here.

Regards,
Danie.
-- 
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|

  reply	other threads:[~2012-07-05 10:46 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-07-05  4:23 [xen-unstable test] 13461: regressions - FAIL xen.org
2012-07-05  5:49 ` Ian Campbell
2012-07-05  9:28   ` Daniel P. Berrange
2012-07-05  9:31     ` Ian Campbell
2012-07-05 10:40     ` Ian Jackson
2012-07-05 10:46       ` Daniel P. Berrange [this message]
2012-07-05 10:54         ` Ian Jackson
2012-07-05 10:57           ` Daniel P. Berrange
2012-07-05 10:57       ` Ian Campbell
2012-07-05 11:00         ` Roger Pau Monne
2012-07-05 11:03           ` Ian Campbell
2012-07-05 11:21         ` Ian Jackson
2012-07-05 11:32           ` Ian Campbell
2012-07-05 11:34           ` Ian Campbell
2012-07-05 11:43             ` [xen-unstable test] 13461: regressions - FAIL [and 1 more messages] Ian Jackson
2012-07-05 11:53           ` [xen-unstable test] 13461: regressions - FAIL Roger Pau Monne
2012-07-05 11:57             ` Ian Campbell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20120705104611.GG13884@redhat.com \
    --to=berrange@redhat.com \
    --cc=Ian.Campbell@citrix.com \
    --cc=Ian.Jackson@eu.citrix.com \
    --cc=xen-devel@lists.xensource.com \
    --cc=zhigang.x.wang@oracle.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.