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>
19 #ifdef HAVE_SYS_WAIT_H
25 static int sys_waitpid(pid_t pid,int *status,int options)
28 return waitpid(pid,status,options);
29 #else /* USE_WAITPID */
30 return wait4(pid, status, options, NULL);
31 #endif /* USE_WAITPID */
34 #define DATA "conftest.fcntl"
40 /* lock a byte range in a open file */
41 int main(int argc, char *argv[])
44 int fd, ret, status=1;
49 fd = open(DATA, O_RDONLY);
52 fprintf(stderr,"ERROR: failed to open %s (errno=%d)\n",
57 lock.l_type = F_WRLCK;
58 lock.l_whence = SEEK_SET;
61 lock.l_pid = getpid();
63 lock.l_type = F_WRLCK;
65 /* check if a lock applies */
66 ret = fcntl(fd,F_GETLK,&lock);
69 (lock.l_type == F_UNLCK)) {
70 fprintf(stderr,"ERROR: lock test failed (ret=%d errno=%d)\n", ret, (int)errno);
77 fd = open(DATA, O_RDWR|O_CREAT|O_TRUNC, 0600);
80 fprintf(stderr,"ERROR: failed to open %s (errno=%d)\n",
85 lock.l_type = F_WRLCK;
86 lock.l_whence = SEEK_SET;
89 lock.l_pid = getpid();
91 /* set a 4 byte write lock */
92 fcntl(fd,F_SETLK,&lock);
94 sys_waitpid(pid, &status, 0);
98 #if defined(WIFEXITED) && defined(WEXITSTATUS)
99 if(WIFEXITED(status)) {
100 status = WEXITSTATUS(status);
104 #else /* defined(WIFEXITED) && defined(WEXITSTATUS) */
105 status = (status == 0) ? 0 : 1;
106 #endif /* defined(WIFEXITED) && defined(WEXITSTATUS) */
109 fprintf(stderr,"ERROR: lock test failed with status=%d\n",