r23543: Adjust comments.
[Samba.git] / source / tests / fcntl_lock64.c
blobb218fa9282e8043e2e0326a1021230e743b1a5ae
1 /* test whether 64 bit fcntl locking really works on this system */
3 #if defined(HAVE_UNISTD_H)
4 #include <unistd.h>
5 #endif
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <sys/types.h>
11 #ifdef HAVE_FCNTL_H
12 #include <fcntl.h>
13 #endif
15 #ifdef HAVE_SYS_FCNTL_H
16 #include <sys/fcntl.h>
17 #endif
19 #ifdef HAVE_SYS_WAIT_H
20 #include <sys/wait.h>
21 #endif
23 #include <errno.h>
25 static int sys_waitpid(pid_t pid,int *status,int options)
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 */
34 #define DATA "conftest.fcntl64"
36 /* lock a byte range in a open file */
37 int main(int argc, char *argv[])
39 struct flock64 lock;
40 int fd, ret, status=1;
41 pid_t pid;
43 if (!(pid=fork())) {
44 sleep(2);
45 fd = open64(DATA, O_RDONLY);
47 if (fd == -1) return 1;
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();
55 lock.l_type = F_WRLCK;
57 /* check if a lock applies */
58 ret = fcntl(fd,F_GETLK64,&lock);
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;
70 fd = open64(DATA, O_RDWR|O_CREAT|O_TRUNC, 0600);
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();
82 /* set a 4 byte write lock */
83 fcntl(fd,F_SETLK64,&lock);
85 sys_waitpid(pid, &status, 0);
87 #if defined(WIFEXITED) && defined(WEXITSTATUS)
88 if(WIFEXITED(status)) {
89 status = WEXITSTATUS(status);
90 } else {
91 status = 1;
93 #else /* defined(WIFEXITED) && defined(WEXITSTATUS) */
94 status = (status == 0) ? 0 : 1;
95 #endif /* defined(WIFEXITED) && defined(WEXITSTATUS) */
97 unlink(DATA);
99 return status;