When I call pstat_getpathname, the fcntl lock I had on the file descriptor is released. Can anyone confirm this behavior and/or point me toward something I am doing wrong?
Example output of the program follows. The "waiting" pid should not say "got lock" until the holding pid says "releasing lock". Commenting out the call to pstat_getpathname fixes this behavior.
6869: got lock on 3
6869: sleeping...
6868: waiting for lock on 3...
6869: calling pstat_getpathname
6868: got lock on 3
Code:
#define _PSTAT64
#include
#include
#include
#include
#include
#include
int main(int argc, char *argv[])
{
struct flock lock;
pid_t pid;
int result;
int fd;
pid = fork();
fd = open("/tmp/locktest",O_RDWR|O_CREAT,0666);
if (fd == -1) return EXIT_FAILURE;
printf("%5d: waiting for lock on %d...\n",getpid(),fd);
lock.l_type = F_WRLCK;
lock.l_start = 0;
lock.l_len = 0;
lock.l_whence = SEEK_SET;
do {
errno = 0;
result = fcntl(fd,F_SETLKW,&lock);
} while (result == -1 && errno == EINTR);
if (result == -1) return EXIT_FAILURE;
printf("%5d: got lock on %d\n",getpid(),fd);
printf("%5d: sleeping...\n",getpid());
sleep(1);
printf("%5d: calling pstat_getpathname\n",getpid());
{
char filename[PATH_MAX];
struct pst_fileinfo2 psf;
pstat_getfile2(&psf,sizeof(psf),0,fd,getpid());
pstat_getpathname(filename,sizeof(filename),&psf.psf_fid);
}
printf("%5d: sleeping...\n",getpid());
sleep(2);
printf("%5d: releasing lock on %d\n",getpid(),fd);
close(fd);
if (pid != 0) waitpid(pid,NULL,0);
return EXIT_SUCCESS;
}