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 #define DATA "conftest.fcntl"
31 /* lock a byte range in a open file */
32 int main(int argc, char *argv[])
35 int fd, ret, status=1;
39 testdir = getenv("TESTDIR");
40 if (testdir) chdir(testdir);
46 fd = open(DATA, O_RDONLY);
49 fprintf(stderr,"ERROR: failed to open %s (errno=%d)\n",
54 lock.l_type = F_WRLCK;
55 lock.l_whence = SEEK_SET;
58 lock.l_pid = getpid();
60 lock.l_type = F_WRLCK;
62 /* check if a lock applies */
63 ret = fcntl(fd,F_GETLK,&lock);
66 (lock.l_type == F_UNLCK)) {
67 fprintf(stderr,"ERROR: lock test failed (ret=%d errno=%d)\n", ret, (int)errno);
75 fd = open(DATA, O_RDWR|O_CREAT|O_EXCL, 0600);
78 fprintf(stderr,"ERROR: failed to open %s (errno=%d)\n",
83 lock.l_type = F_WRLCK;
84 lock.l_whence = SEEK_SET;
87 lock.l_pid = getpid();
89 /* set a 4 byte write lock */
90 fcntl(fd,F_SETLK,&lock);
92 sys_waitpid(pid, &status, 0);
96 #if defined(WIFEXITED) && defined(WEXITSTATUS)
97 if(WIFEXITED(status)) {
98 status = WEXITSTATUS(status);
102 #else /* defined(WIFEXITED) && defined(WEXITSTATUS) */
103 status = (status == 0) ? 0 : 1;
104 #endif /* defined(WIFEXITED) && defined(WEXITSTATUS) */
107 fprintf(stderr,"ERROR: lock test failed with status=%d\n",