diff options
author | bart <bart@a5019735-40e9-0310-863c-91ae7b9d1cf9> | 2008-06-23 11:43:28 +0000 |
---|---|---|
committer | bart <bart@a5019735-40e9-0310-863c-91ae7b9d1cf9> | 2008-06-23 11:43:28 +0000 |
commit | ad60eeb3336aea0f14de01fd74d219c664e6d7d0 (patch) | |
tree | f276ac9ef3a1f7bf0c38f3494736e7c7f8499cdf /memcheck | |
parent | fa1b293c9fdac5503a08e3a242c884458501d38f (diff) |
Added regression test for POSIX advisory locking (fcntl(..., F_SETFL, ...)).
git-svn-id: svn://svn.valgrind.org/valgrind/trunk@8269 a5019735-40e9-0310-863c-91ae7b9d1cf9
Diffstat (limited to 'memcheck')
-rw-r--r-- | memcheck/tests/Makefile.am | 2 | ||||
-rw-r--r-- | memcheck/tests/file_locking.c | 87 | ||||
-rw-r--r-- | memcheck/tests/file_locking.stderr.exp | 4 | ||||
-rw-r--r-- | memcheck/tests/file_locking.vgtest | 2 |
4 files changed, 95 insertions, 0 deletions
diff --git a/memcheck/tests/Makefile.am b/memcheck/tests/Makefile.am index 92217066..737a439c 100644 --- a/memcheck/tests/Makefile.am +++ b/memcheck/tests/Makefile.am @@ -49,6 +49,7 @@ EXTRA_DIST = $(noinst_SCRIPTS) \ exitprog.stderr.exp exitprog.vgtest \ execve.stderr.exp execve.stderr.exp2 execve.vgtest \ execve2.stderr.exp execve2.stderr.exp2 execve2.vgtest \ + file_locking.stderr.exp file_locking.vgtest \ fprw.stderr.exp fprw.vgtest \ fwrite.stderr.exp fwrite.stderr.exp2 fwrite.vgtest \ inits.stderr.exp inits.vgtest \ @@ -187,6 +188,7 @@ check_PROGRAMS = \ deep_templates \ describe-block \ doublefree error_counts errs1 exitprog execve execve2 erringfds \ + file_locking \ fprw fwrite hello inits inline \ leak-0 leak-cycle leak-pool leak-tree leak-regroot leakotron \ linux-syslog-syscall \ diff --git a/memcheck/tests/file_locking.c b/memcheck/tests/file_locking.c new file mode 100644 index 00000000..fe5bff48 --- /dev/null +++ b/memcheck/tests/file_locking.c @@ -0,0 +1,87 @@ +/** Test program for POSIX advisory record locking. See also #164669 + * (http://bugs.kde.org/show_bug.cgi?id=164669). + * See also http://www.opengroup.org/onlinepubs/007908799/xsh/fcntl.html. + */ + + +#include <sys/mman.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <unistd.h> +#include <fcntl.h> +#include <stdio.h> +#include <errno.h> +#include <stdlib.h> +#include <unistd.h> + + +/** Lock an entire file exclusively. + * + * @return 1 upon success, 0 upon failure. + */ +static int lock_file(const int fd) +{ + struct flock fl; + + fl.l_type = F_WRLCK; /* exclusive lock */ + fl.l_whence = SEEK_SET; + fl.l_start = 0; + fl.l_len = 0; /* lock entire file */ + fl.l_pid = 0; + return fcntl(fd, F_SETLK, &fl) >= 0; +} + + +int main(int argc, char *argv[]) +{ + int fd1; + int fd2; + int exitcode = 1; + char filename[256]; + + snprintf(filename, sizeof(filename), "/tmp/valgrind-file-locking-test.%d", + getpid()); + + unlink(filename); + + if ((fd1 = open(filename, O_RDWR | O_CREAT)) >= 0) + { + fprintf(stderr, "About to lock file for writing.\n"); + if (lock_file(fd1)) + { + fprintf(stderr, "First locking attempt succeeded.\n"); + if ((fd2 = open(filename, O_RDWR)) >= 0) + { + if (! lock_file(fd2)) + { + fprintf(stderr, "Second locking attempt failed.\n"); + exitcode = 0; + } + else + { + fprintf(stderr, "ERROR: second lock attempt succeeded !\n"); + } + close(fd2); + } + else + { + perror("second open call"); + } + } + else + { + perror("first locking attempt"); + } + close(fd1); + } + else + { + perror("first open call"); + } + + unlink(filename); + + fprintf(stderr, "Test finished successfully.\n"); + + return exitcode; +} diff --git a/memcheck/tests/file_locking.stderr.exp b/memcheck/tests/file_locking.stderr.exp new file mode 100644 index 00000000..e12d4346 --- /dev/null +++ b/memcheck/tests/file_locking.stderr.exp @@ -0,0 +1,4 @@ +About to lock file for writing. +First locking attempt succeeded. +second open call: Permission denied +Test finished successfully. diff --git a/memcheck/tests/file_locking.vgtest b/memcheck/tests/file_locking.vgtest new file mode 100644 index 00000000..48b9a305 --- /dev/null +++ b/memcheck/tests/file_locking.vgtest @@ -0,0 +1,2 @@ +prog: file_locking +vgopts: -q |