[Gluster-devel] locking race condition

Emmanuel Dreyfus manu at netbsd.org
Mon Aug 20 04:35:49 UTC 2012

Emmanuel Dreyfus <manu at netbsd.org> wrote:

> # echo "xxx"|cat -l > test9 
> cat: stdout: No such file or directory

I realize the -l flag for cat is a NetBSD extension. Here is a test case in C
that exhibit the problem. As shown in the transcript below, it does not fails
all the time, sometime I have to run it a few times to get the failure:

$ cat lockrace.c                                                               
/* cc -o lockrace lockrace.c */
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <err.h>
#include <sysexits.h>
#include <sys/param.h>

        char filename[MAXPATHLEN + 1];
        int fd;
        struct flock fl;

        (void)snprintf(filename, MAXPATHLEN, "test-%d", getpid());
        if ((fd = open(filename, O_CREAT|O_EXCL|O_RDWR, 0644)) == -1)
                err(EX_OSERR, "open \"%s\" failed", filename);

        fl.l_len = 0;
        fl.l_start = 0;
        fl.l_type = F_WRLCK;
        fl.l_whence = SEEK_SET;

        if (fcntl(fd, F_SETLKW, &fl) == -1)
                err(EX_OSERR, "fcntl F_SETLKW on \"%s\" failed", filename);

        return EX_OK;
$ cc -o lockrace lockrace.c                                                    
$ ./lockrace                                                                   
$ ./lockrace 
$ ./lockrace 
$ ./lockrace 
$ ./lockrace 
lockrace: fcntl F_SETLKW on "test-19894" failed: No such file or directory

Emmanuel Dreyfus
manu at netbsd.org

More information about the Gluster-devel mailing list