913580cd24121e22148d95e192e3be5c78ac042b
[gd/samba/.git] / source4 / build / 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 #include <errno.h>
20
21 #define DATA "conftest.fcntl64"
22
23 /* lock a byte range in a open file */
24 int main(int argc, char *argv[])
25 {
26         struct flock64 lock;
27         int fd, ret, status=1;
28         pid_t pid;
29
30         if (!(pid=fork())) {
31                 sleep(2);
32                 fd = open64(DATA, O_RDONLY);
33
34                 if (fd == -1) exit(1);
35
36                 lock.l_type = F_WRLCK;
37                 lock.l_whence = SEEK_SET;
38                 lock.l_start = 0;
39                 lock.l_len = 4;
40                 lock.l_pid = getpid();
41                 
42                 lock.l_type = F_WRLCK;
43                 
44                 /* check if a lock applies */
45                 ret = fcntl(fd,F_GETLK64,&lock);
46
47                 if ((ret == -1) ||
48                     (lock.l_type == F_UNLCK)) {
49 /*            printf("No lock conflict\n"); */
50                         exit(1);
51                 } else {
52 /*            printf("lock conflict\n"); */
53                         exit(0);
54                 }
55         }
56
57         fd = open64(DATA, O_RDWR|O_CREAT|O_TRUNC, 0600);
58
59         lock.l_type = F_WRLCK;
60         lock.l_whence = SEEK_SET;
61 #if defined(COMPILER_SUPPORTS_LL)
62         lock.l_start = 0x100000000LL;
63 #else
64         lock.l_start = 0x100000000;
65 #endif
66         lock.l_len = 4;
67         lock.l_pid = getpid();
68
69         /* set a 4 byte write lock */
70         fcntl(fd,F_SETLK64,&lock);
71
72         sys_waitpid(pid, &status, 0);
73
74 #if defined(WIFEXITED) && defined(WEXITSTATUS)
75         if(WIFEXITED(status)) {
76                 status = WEXITSTATUS(status);
77         } else {
78                 status = 1;
79         }
80 #else /* defined(WIFEXITED) && defined(WEXITSTATUS) */
81         status = (status == 0) ? 0 : 1;
82 #endif /* defined(WIFEXITED) && defined(WEXITSTATUS) */
83
84         unlink(DATA);
85
86         exit(status);
87 }