Marc Slemko wrote:
>State-Changed-Why:
>I am unable to duplicate this problem on FreeBSD. Are
>you mounting things via NFS?
No, I didn't use NFS.
I don't know why you couldn't duplicate it.
The problem is neither caused by NFS, nor FreeBSD.
The Apache's programmers seem to suppose that flock() is compatible
with fcntl().
However, the supposition is wrong.
The following program will tell you the proper usage of flock().
--------
#include <sys/file.h>
#include <sys/errno.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int
main(int argc, char **argv)
{
int fd;
int bad = argc >= 2 && strcmp(argv[1], "bad") == 0;
if (bad)
fd = open("lockfile", O_WRONLY | O_CREAT, 0666);
if (fork() == 0) {
/* child */
if (!bad)
fd = open("lockfile", O_WRONLY | O_CREAT, 0666);
flock(fd, LOCK_EX);
sleep(2);
exit(0);
}
/* parent */
sleep(1);
if (!bad)
fd = open("lockfile", O_WRONLY);
if (flock(fd, LOCK_EX | LOCK_NB) == -1) {
if (errno == EWOULDBLOCK)
printf("flock works fine.\n");
else
printf("flock fails: %s\n", strerror(errno));
} else
printf("flock doesn't work.\n");
return 0;
}
--------
The platforms, the command lines and the corresponding outputs are below.
FreeBSD | ./a.out | locking works file.
FreeBSD | ./a.out bad | locking doesn't work.
Solaris | ./a.out | locking works file.
Solaris | ./a.out bad | locking works file.
Note: Solaris's flock() seems to be using fcntl() internally,
so it often has the funny behavior.
--
Tetsuya FURUKAWA, Tokyo, Japan.