1 /** Test program for POSIX advisory record locking. See also #164669
2 * (http://bugs.kde.org/show_bug.cgi?id=164669).
3 * See also http://www.opengroup.org/onlinepubs/007908799/xsh/fcntl.html.
12 #include "tests/sys_mman.h"
14 #include <sys/types.h>
20 /** Lock an entire file exclusively.
22 * @return 1 upon success, 0 upon failure.
24 static int lock_file(const int fd
)
28 fl
.l_type
= F_WRLCK
; /* exclusive lock */
29 fl
.l_whence
= SEEK_SET
;
31 fl
.l_len
= 0; /* lock entire file */
33 return fcntl(fd
, F_SETLK
, &fl
) >= 0;
36 static int open_lock_and_map(const char* const process_name
,
37 const char* const filename
)
42 fd
= open(filename
, O_RDWR
| O_CREAT
, S_IRUSR
| S_IWUSR
);
49 flags
= fcntl(fd
, F_GETFD
);
51 if (fcntl(fd
, F_SETFD
, flags
| FD_CLOEXEC
) < 0)
54 fprintf(stderr
, "%s: about to lock file for writing.\n", process_name
);
61 fprintf(stderr
, "%s: file locking attempt succeeded.\n", process_name
);
62 if (mmap(NULL
, 1, PROT_WRITE
, MAP_SHARED
, fd
, 0) == 0)
77 int main(int argc
, char *argv
[])
84 snprintf(filename
, sizeof(filename
), "/tmp/valgrind-file-locking-test.%d",
89 if ((fd1
= open_lock_and_map("parent", filename
)) >= 0)
102 fd2
= open_lock_and_map("child", filename
);
116 wait_result
= wait4(fork_result
, &child_status
, 0, 0);
117 assert(wait_result
>= 0);
126 fprintf(stderr
, "Test finished.\n");