]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
Added regression test for POSIX advisory locking (fcntl(..., F_SETFL, ...)).
authorBart Van Assche <bvanassche@acm.org>
Mon, 23 Jun 2008 11:43:28 +0000 (11:43 +0000)
committerBart Van Assche <bvanassche@acm.org>
Mon, 23 Jun 2008 11:43:28 +0000 (11:43 +0000)
git-svn-id: svn://svn.valgrind.org/valgrind/trunk@8269

memcheck/tests/Makefile.am
memcheck/tests/file_locking.c [new file with mode: 0644]
memcheck/tests/file_locking.stderr.exp [new file with mode: 0644]
memcheck/tests/file_locking.vgtest [new file with mode: 0644]

index 92217066b1f0122588519f6d8ef377d59eea97c0..737a439ccfd4b2ee97c6b9819c0c261cd1272e0a 100644 (file)
@@ -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 (file)
index 0000000..fe5bff4
--- /dev/null
@@ -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 (file)
index 0000000..e12d434
--- /dev/null
@@ -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 (file)
index 0000000..48b9a30
--- /dev/null
@@ -0,0 +1,2 @@
+prog: file_locking
+vgopts: -q