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