tech-kern archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

flock(2): locking against itself?



I ran into an issue, which turned out to be, loosely put, that an open
file table entry cannot lock against itself.  Here's a small test
program (most error checking omitted for brevity):

#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/wait.h>

static int fd;

int main(void);
int main(void)
{
 fd = open(".lock",O_RDWR|O_CREAT,0666);
 if (fork() == 0)
  { if (flock(fd,LOCK_EX|LOCK_NB) < 0)
    { printf("lock child: %s\n",strerror(errno));
      exit(1);
    }
    sleep(5);
    exit(0);
  }
 if (flock(fd,LOCK_EX|LOCK_NB) < 0)
  { printf("lock parent: %s\n",strerror(errno));
    exit(1);
  }
 sleep(5);
 wait(0);
 return(0);
}

An earlier version skipped the fork, doing the two flock calls in
succession from the same process, without the sleeps.  Neither version
produces EWOULDBLOCK from either flock call on any of the systems I
tried it on (my mutant 1.4T, my mutant 5.2, and a guest account on a
stock (as far as I know) 9.0).

This is not what I was expecting.  On examination, the manpages
available to me (including the one at http://man.netbsd.org/flock.2)
turn out to say nothing to clarify this.  Moving the open after the
fork, so the parent and child open separately, I do, of course, get the
expected EWOULDBLOCK from one process.

Is this expected behaviour, or is it a bug?

/~\ The ASCII				  Mouse
\ / Ribbon Campaign
 X  Against HTML		mouse%rodents-montreal.org@localhost
/ \ Email!	     7D C8 61 52 5D E7 2D 39  4E F1 31 3E E8 B3 27 4B


Home | Main Index | Thread Index | Old Index