[Samba] O_EXCL unreliable?

Urban Widmark urban at teststation.com
Tue Jun 18 10:42:05 GMT 2002


On Tue, 18 Jun 2002, Egon Eckert wrote:

> Thanks.  We will have to find out something (we are trying to
> synchronize Linux vs. Windows application).  "lock directory"
> seems a bit strange/bizarre to me...  Lock regions (in files)
> make me scared as well -- I see many problems/questions about
> them here in the list (mostly unanswered :-) ).

Each dir is one lock.

You try to create the directory, if it succeeds then you've got the lock.
When you are done you remove the dir.

While the dir exists no one else can create it, because it exists and 
their requests will fail with EEXIST. If they want to wait they can loop 
over the mkdir.

The example code below shows the idea. Releasing is just an rmdir, with 
handling of the case that someone has the dir open (eg is looking at it in 
explorer ...)

This assumes that the server won't answer two simultaneous mkdir requests
positively.

A problem is that the lockdir won't go away automatically when your
program crashes.

/Urban


/*
 * lockdir.c - Example of locks using directories
 */

#include <errno.h>

static char *lockdir = "/mnt/smb/.lock";

int main(int argc, char **argv)
{
    int res;

    res = mkdir(lockdir, 0700);
    while (res == -1 && errno == EEXIST) {
	sleep(1);
	res = mkdir(lockdir, 0700);
    }

    /* return 0 if we got the lock */
    return res;
}





More information about the samba mailing list