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