Has fcntl(2) left the building?

Jules Colding colding at 42tools.com
Tue Nov 4 10:45:40 UTC 2008


Hi,

I have a simple daemon which locks a file descriptor using fcntl(2) to  
ensure that it is alone in the air. Specifically it does:


static int
lock_fd(const int fd)
{
	struct flock lock = {
		.l_type = F_WRLCK,
		.l_start = 0,
		.l_whence = SEEK_SET,
		.l_len = 0,
	};

	if (-1 == fd)
		return -1;

	return fcntl(fd, F_SETLK, &lock);
}

The file descriptor "fd" has been successfully opened with:

    g_open(file_path, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);


The problem is that it returns -1 and sets errno to EDEADLK. That  
shouldn't be possible as far as I know. EDEADLK should only be set if  
cmd was F_SETLKW but I'm using F_SETLK. Obviously my daemon fails to  
lock the file.

I would very grateful if someone could tell me how this could happen  
and how to avoid it.


The complete source is here:

    http://trac.42tools.net/evolution-brutus/browser/trunk/session/ 
main.c


Best regards and thanks a lot in advance,
   jules





More information about the ubuntu-users mailing list