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