From b3013e9de93b31d94fb58ea65c4b19b7738a1547 Mon Sep 17 00:00:00 2001 From: Bart Van Assche Date: Tue, 24 Jun 2008 09:54:05 +0000 Subject: [PATCH] Reworked test such that file locking is now triggered from two different processes. git-svn-id: svn://svn.valgrind.org/valgrind/trunk@8277 --- memcheck/tests/file_locking.c | 104 +++++++++++++++++-------- memcheck/tests/file_locking.stderr.exp | 9 ++- memcheck/tests/file_locking.vgtest | 2 +- 3 files changed, 79 insertions(+), 36 deletions(-) diff --git a/memcheck/tests/file_locking.c b/memcheck/tests/file_locking.c index 9d833377bd..4e60bea9e8 100644 --- a/memcheck/tests/file_locking.c +++ b/memcheck/tests/file_locking.c @@ -4,14 +4,16 @@ */ -#include -#include -#include -#include +#include +#include #include #include -#include #include +#include +#include +#include +#include +#include #include @@ -31,6 +33,46 @@ static int lock_file(const int fd) return fcntl(fd, F_SETLK, &fl) >= 0; } +static int open_lock_and_map(const char* const process_name, + const char* const filename) +{ + int fd; + int flags; + + fd = open(filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); + if (fd < 0) + { + perror("open"); + goto err1; + } + + flags = fcntl(fd, F_GETFD); + assert(flags >= 0); + if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0) + assert(0); + + fprintf(stderr, "%s: about to lock file for writing.\n", process_name); + if (! lock_file(fd)) + { + perror("fcntl"); + goto err2; + } + + fprintf(stderr, "%s: file locking attempt succeeded.\n", process_name); + if (mmap(NULL, 1, PROT_WRITE, MAP_SHARED, fd, 0) == 0) + { + perror("mmap"); + goto err2; + } + + goto out; + +err2: + close(fd); +err1: +out: + return fd; +} int main(int argc, char *argv[]) { @@ -44,44 +86,44 @@ int main(int argc, char *argv[]) unlink(filename); - if ((fd1 = open(filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)) >= 0) + if ((fd1 = open_lock_and_map("parent", filename)) >= 0) { - fprintf(stderr, "About to lock file for writing.\n"); - if (lock_file(fd1)) + pid_t fork_result; + + fork_result = fork(); + switch (fork_result) { - fprintf(stderr, "First locking attempt succeeded.\n"); - if ((fd2 = open(filename, O_RDWR)) >= 0) + case -1: + perror("fork"); + break; + + case 0: + /* child */ + fd2 = open_lock_and_map("child", filename); + if (fd2 >= 0) { - if (! lock_file(fd2)) - { - fprintf(stderr, "Second locking attempt failed.\n"); - exitcode = 0; - } - else - { - fprintf(stderr, "Second locking attempt succeeded.\n"); - } close(fd2); } - else + exit(0); + break; + + default: + /* parent */ { - perror("second open call"); + int child_status; + int wait_result; + + wait_result = wait4(fork_result, &child_status, 0, 0); + assert(wait_result >= 0); } } - else - { - perror("first locking attempt"); - } - close(fd1); - } - else - { - perror("first open call"); } + close(fd1); + unlink(filename); - fprintf(stderr, "Test finished successfully.\n"); + fprintf(stderr, "Test finished.\n"); return exitcode; } diff --git a/memcheck/tests/file_locking.stderr.exp b/memcheck/tests/file_locking.stderr.exp index df5bcfa923..f1b870af47 100644 --- a/memcheck/tests/file_locking.stderr.exp +++ b/memcheck/tests/file_locking.stderr.exp @@ -1,4 +1,5 @@ -About to lock file for writing. -First locking attempt succeeded. -Second locking attempt succeeded. -Test finished successfully. +parent: about to lock file for writing. +parent: file locking attempt succeeded. +child: about to lock file for writing. +fcntl: Resource temporarily unavailable +Test finished. diff --git a/memcheck/tests/file_locking.vgtest b/memcheck/tests/file_locking.vgtest index 48b9a305c1..887c127dac 100644 --- a/memcheck/tests/file_locking.vgtest +++ b/memcheck/tests/file_locking.vgtest @@ -1,2 +1,2 @@ prog: file_locking -vgopts: -q +vgopts: -q --trace-children=yes -- 2.47.2