added sys/wait.h to fcntl_lock test
[samba.git] / source / tests / fcntl_lock.c
1 /* test whether fcntl locking works on this system */
2
3 #if defined(HAVE_UNISTD_H)
4 #include <unistd.h>
5 #endif
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <sys/types.h>
10
11 #ifdef HAVE_FCNTL_H
12 #include <fcntl.h>
13 #endif
14
15 #ifdef HAVE_SYS_FCNTL_H
16 #include <sys/fcntl.h>
17 #endif
18
19 #ifdef HAVE_SYS_WAIT_H
20 #include <sys/wait.h>
21 #endif
22
23 #include <errno.h>
24
25 static int sys_waitpid(pid_t pid,int *status,int options)
26 {
27 #ifdef HAVE_WAITPID
28   return waitpid(pid,status,options);
29 #else /* USE_WAITPID */
30   return wait4(pid, status, options, NULL);
31 #endif /* USE_WAITPID */
32 }
33
34 #define DATA "conftest.fcntl"
35
36 #ifndef SEEK_SET
37 #define SEEK_SET 0
38 #endif
39
40 /* lock a byte range in a open file */
41 int main(int argc, char *argv[])
42 {
43         struct flock lock;
44         int fd, ret, status=1;
45         pid_t pid;
46
47         if (!(pid=fork())) {
48                 sleep(2);
49                 fd = open(DATA, O_RDONLY);
50
51                 if (fd == -1) {
52                         fprintf(stderr,"ERROR: failed to open %s (errno=%d)\n", 
53                                 DATA, (int)errno);
54                         exit(1);
55                 }
56
57                 lock.l_type = F_WRLCK;
58                 lock.l_whence = SEEK_SET;
59                 lock.l_start = 0;
60                 lock.l_len = 4;
61                 lock.l_pid = getpid();
62                 
63                 lock.l_type = F_WRLCK;
64                 
65                 /* check if a lock applies */
66                 ret = fcntl(fd,F_GETLK,&lock);
67
68                 if ((ret == -1) ||
69                     (lock.l_type == F_UNLCK)) {
70                         fprintf(stderr,"ERROR: lock test failed (ret=%d errno=%d)\n", ret, (int)errno);
71                         exit(1);
72                 } else {
73                         exit(0);
74                 }
75         }
76
77         fd = open(DATA, O_RDWR|O_CREAT|O_TRUNC, 0600);
78
79         if (fd == -1) {
80                 fprintf(stderr,"ERROR: failed to open %s (errno=%d)\n", 
81                         DATA, (int)errno);
82                 exit(1);
83         }
84
85         lock.l_type = F_WRLCK;
86         lock.l_whence = SEEK_SET;
87         lock.l_start = 0;
88         lock.l_len = 4;
89         lock.l_pid = getpid();
90
91         /* set a 4 byte write lock */
92         fcntl(fd,F_SETLK,&lock);
93
94         sys_waitpid(pid, &status, 0);
95
96         unlink(DATA);
97
98 #if defined(WIFEXITED) && defined(WEXITSTATUS)
99     if(WIFEXITED(status)) {
100         status = WEXITSTATUS(status);
101     } else {
102         status = 1;
103     }
104 #else /* defined(WIFEXITED) && defined(WEXITSTATUS) */
105         status = (status == 0) ? 0 : 1;
106 #endif /* defined(WIFEXITED) && defined(WEXITSTATUS) */
107
108         if (status) {
109                 fprintf(stderr,"ERROR: lock test failed with status=%d\n", 
110                         status);
111         }
112
113         exit(status);
114 }