1 /* test whether fcntl locking works on this system */
3 #if defined(HAVE_UNISTD_H)
15 #ifdef HAVE_SYS_FCNTL_H
16 #include <sys/fcntl.h>
21 static int sys_waitpid(pid_t pid,int *status,int options)
24 return waitpid(pid,status,options);
25 #else /* USE_WAITPID */
26 return wait4(pid, status, options, NULL);
27 #endif /* USE_WAITPID */
30 #define DATA "conftest.fcntl"
36 /* lock a byte range in a open file */
37 int main(int argc, char *argv[])
40 int fd, ret, status=1;
45 fd = open(DATA, O_RDONLY);
48 fprintf(stderr,"ERROR: failed to open %s (errno=%d)\n",
53 lock.l_type = F_WRLCK;
54 lock.l_whence = SEEK_SET;
57 lock.l_pid = getpid();
59 lock.l_type = F_WRLCK;
61 /* check if a lock applies */
62 ret = fcntl(fd,F_GETLK,&lock);
65 (lock.l_type == F_UNLCK)) {
66 fprintf(stderr,"ERROR: lock test failed (ret=%d errno=%d)\n", ret, (int)errno);
73 fd = open(DATA, O_RDWR|O_CREAT|O_TRUNC, 0600);
76 fprintf(stderr,"ERROR: failed to open %s (errno=%d)\n",
81 lock.l_type = F_WRLCK;
82 lock.l_whence = SEEK_SET;
85 lock.l_pid = getpid();
87 /* set a 4 byte write lock */
88 fcntl(fd,F_SETLK,&lock);
90 sys_waitpid(pid, &status, 0);
94 #if defined(WIFEXITED) && defined(WEXITSTATUS)
95 if(WIFEXITED(status)) {
96 status = WEXITSTATUS(status);
100 #else /* defined(WIFEXITED) && defined(WEXITSTATUS) */
101 status = (status == 0) ? 0 : 1;
102 #endif /* defined(WIFEXITED) && defined(WEXITSTATUS) */
105 fprintf(stderr,"ERROR: lock test failed with status=%d\n",