b218fa9282e8043e2e0326a1021230e743b1a5ae
[samba.git] / source / tests / fcntl_lock64.c
1 /* test whether 64 bit fcntl locking really 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.fcntl64"
35
36 /* lock a byte range in a open file */
37 int main(int argc, char *argv[])
38 {
39         struct flock64 lock;
40         int fd, ret, status=1;
41         pid_t pid;
42
43         if (!(pid=fork())) {
44                 sleep(2);
45                 fd = open64(DATA, O_RDONLY);
46
47                 if (fd == -1) return 1;
48
49                 lock.l_type = F_WRLCK;
50                 lock.l_whence = SEEK_SET;
51                 lock.l_start = 0;
52                 lock.l_len = 4;
53                 lock.l_pid = getpid();
54                 
55                 lock.l_type = F_WRLCK;
56                 
57                 /* check if a lock applies */
58                 ret = fcntl(fd,F_GETLK64,&lock);
59
60                 if ((ret == -1) ||
61                     (lock.l_type == F_UNLCK)) {
62 /*            printf("No lock conflict\n"); */
63                         return 1;
64                 } else {
65 /*            printf("lock conflict\n"); */
66                         return 0;
67                 }
68         }
69
70         fd = open64(DATA, O_RDWR|O_CREAT|O_TRUNC, 0600);
71
72         lock.l_type = F_WRLCK;
73         lock.l_whence = SEEK_SET;
74 #if defined(COMPILER_SUPPORTS_LL)
75         lock.l_start = 0x100000000LL;
76 #else
77         lock.l_start = 0x100000000;
78 #endif
79         lock.l_len = 4;
80         lock.l_pid = getpid();
81
82         /* set a 4 byte write lock */
83         fcntl(fd,F_SETLK64,&lock);
84
85         sys_waitpid(pid, &status, 0);
86
87 #if defined(WIFEXITED) && defined(WEXITSTATUS)
88         if(WIFEXITED(status)) {
89                 status = WEXITSTATUS(status);
90         } else {
91                 status = 1;
92         }
93 #else /* defined(WIFEXITED) && defined(WEXITSTATUS) */
94         status = (status == 0) ? 0 : 1;
95 #endif /* defined(WIFEXITED) && defined(WEXITSTATUS) */
96
97         unlink(DATA);
98
99         return status;
100 }