From: Bart Van Assche Date: Mon, 23 Jun 2008 11:43:28 +0000 (+0000) Subject: Added regression test for POSIX advisory locking (fcntl(..., F_SETFL, ...)). X-Git-Tag: svn/VALGRIND_3_4_0~443 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=857eaf6fdbae6688c3729a7338bfb3179960356e;p=thirdparty%2Fvalgrind.git Added regression test for POSIX advisory locking (fcntl(..., F_SETFL, ...)). git-svn-id: svn://svn.valgrind.org/valgrind/trunk@8269 --- diff --git a/memcheck/tests/Makefile.am b/memcheck/tests/Makefile.am index 92217066b1..737a439ccf 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 0000000000..fe5bff489e --- /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 +#include +#include +#include +#include +#include +#include +#include +#include + + +/** 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 0000000000..e12d4346a8 --- /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 0000000000..48b9a305c1 --- /dev/null +++ b/memcheck/tests/file_locking.vgtest @@ -0,0 +1,2 @@ +prog: file_locking +vgopts: -q