All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alexander Aring <aahringo@redhat.com>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH 3/5] fcntl42: test for F_SETLKW interruption case
Date: Tue, 30 May 2023 16:37:05 -0400	[thread overview]
Message-ID: <20230530203707.2965684-4-aahringo@redhat.com> (raw)
In-Reply-To: <20230530203707.2965684-1-aahringo@redhat.com>

This patch adds fcntl42 testcase to test on side effects, e.g. unlock
all acquired locks, when a lock request was interrupted by a signal.

Signed-off-by: Alexander Aring <aahringo@redhat.com>
---
 testcases/kernel/syscalls/fcntl/.gitignore |   2 +
 testcases/kernel/syscalls/fcntl/fcntl42.c  | 153 +++++++++++++++++++++
 2 files changed, 155 insertions(+)
 create mode 100644 testcases/kernel/syscalls/fcntl/fcntl42.c

diff --git a/testcases/kernel/syscalls/fcntl/.gitignore b/testcases/kernel/syscalls/fcntl/.gitignore
index 4bdae1a22..abffa2967 100644
--- a/testcases/kernel/syscalls/fcntl/.gitignore
+++ b/testcases/kernel/syscalls/fcntl/.gitignore
@@ -78,3 +78,5 @@
 /fcntl40_64
 /fcntl41
 /fcntl41_64
+/fcntl42
+/fcntl42_64
diff --git a/testcases/kernel/syscalls/fcntl/fcntl42.c b/testcases/kernel/syscalls/fcntl/fcntl42.c
new file mode 100644
index 000000000..4d66568fd
--- /dev/null
+++ b/testcases/kernel/syscalls/fcntl/fcntl42.c
@@ -0,0 +1,153 @@
+/*
+ * [Description]
+ * This test confirms that DLM posix locking has problems when a posix lock
+ * got interrupted every lock gets unlocked.
+ *
+ * man fcntl:
+ *
+ * F_SETLKW (struct flock *)
+ *  As for F_SETLK, but if a conflicting lock is held on  the  file,
+ *  then  wait  for that lock to be released.  If a signal is caught
+ *  while waiting, then the call is interrupted and (after the  signal
+ *  handler has returned) returns immediately (with return value -1 and
+ *  errno set to EINTR; see signal(7)).
+ *
+ * The above quote of the man page of fcntl() is what this testcase tests.
+ * particulary if it has side-effects of previously acquired locks.
+ *
+ * [How to use it]
+ * Call it with TMPDIR=/mnt ./fcntl42 where TMPDIR is a gfs2 mountpoint.
+ * Try it on other filesystems to compare results.
+ *
+ * [What's it doing]
+ *
+ * What this test does is (using dlm_controld interval range interpretation):
+ *
+ * parent:
+ *
+ * 1. lock[0-0]
+ *
+ * child:
+ *
+ * 2. lock[1-1]
+ * 3. lockw[0-0] - should block (see 1. parent), but we get interrupted by SIGALRM
+ *
+ * parent:
+ *
+ * 4. trylock[1-1] - should return -1 and errno -EAGAIN because the child
+ *                   should still have lock[1-1] acuired and this is what
+ *                   the child thinks to have. If it's successful the child
+ *                   wrongly assumes it has the lock[1-1] still acquired and
+ *                   the child process is still alive.
+ *
+ */
+
+#include <sys/wait.h>
+
+#include "tst_test.h"
+
+static int fd;
+
+static void catch_alarm(int num)
+{
+	(void)num;
+
+	tst_res(TINFO, "catch alarm");
+}
+
+void do_child1(void)
+{
+	struct flock fl = {
+		.l_type = F_WRLCK,
+		.l_whence = SEEK_SET,
+		.l_start = 1L,
+		.l_len = 1L,
+	};
+	struct sigaction act;
+	int rv;
+
+	SAFE_FCNTL(fd, F_SETLK, &fl);
+	tst_res(TINFO, "child locked lock 1-1");
+
+	fl.l_type = F_WRLCK;
+	fl.l_start = 0;
+	fl.l_len = 1;
+
+	memset(&act, 0, sizeof(act));
+	act.sa_handler = catch_alarm;
+	sigemptyset(&act.sa_mask);
+	sigaddset(&act.sa_mask, SIGALRM);
+	sigaction(SIGALRM, &act, NULL);
+
+	/* interrupt SETLKW by signal in 3 secs */
+	alarm(3);
+	rv = fcntl(fd, F_SETLKW, &fl);
+	if (rv == -1 && errno == EINTR)
+		tst_res(TPASS, "Child1 interrupted 0-0");
+
+	TST_CHECKPOINT_WAKE(1);
+	/* keep child alive until parent check region */
+	TST_CHECKPOINT_WAIT(2);
+}
+
+static void fcntl40_test(void)
+{
+	struct flock fl = {
+		.l_type = F_WRLCK,
+		.l_whence = SEEK_SET,
+		.l_start = 0L,
+		.l_len = 1L,
+	};
+	pid_t pid;
+	int rv;
+
+	SAFE_FCNTL(fd, F_SETLK, &fl);
+	tst_res(TINFO, "parent lock 0-0");
+
+	pid = SAFE_FORK();
+	if (pid == 0) {
+		do_child1();
+		tst_res(TINFO, "childs exits");
+		return;
+	}
+
+	TST_CHECKPOINT_WAIT(1);
+
+	fl.l_type = F_WRLCK;
+	fl.l_start = 1;
+	fl.l_len = 1;
+	rv = fcntl(fd, F_SETLK, &fl);
+	/* parent testing childs region, the child will think
+	 * it has region 1-1 locked because it was interrupted
+	 * by region 0-0. Due bugs the interruption also unlocked
+	 * region 1-1.
+	 */
+	if (rv == -1 && errno == EAGAIN)
+		tst_res(TPASS, "region 1-1 locked");
+	else
+		tst_res(TFAIL, "region 1-1 unlocked");
+
+	TST_CHECKPOINT_WAKE(2);
+
+	wait(NULL);
+	tst_res(TINFO, "parent exits");
+}
+
+static void setup(void)
+{
+	fd = SAFE_OPEN("filename", O_RDWR | O_CREAT, 0700);
+}
+
+static void cleanup(void)
+{
+	if (fd > -1)
+		SAFE_CLOSE(fd);
+}
+
+static struct tst_test test = {
+	.forks_child = 1,
+	.needs_checkpoints = 1,
+	.test_all = fcntl40_test,
+	.setup = setup,
+	.cleanup = cleanup,
+};
-- 
2.31.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

  parent reply	other threads:[~2023-05-30 20:37 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-30 20:37 [LTP] [PATCH 0/5] fcntl: add more testcases Alexander Aring
2023-05-30 20:37 ` [LTP] [PATCH 1/5] fcntl40: test for owner values on classic posix lock Alexander Aring
2023-06-21  9:03   ` Petr Vorel
2023-06-30 19:59     ` Alexander Aring
2023-07-02 19:18       ` Petr Vorel
2023-07-05 13:23         ` Alexander Aring
2023-07-07  8:14           ` Petr Vorel
2023-07-07 12:50             ` Alexander Aring
2023-07-07 13:17               ` Petr Vorel
2023-07-02 19:19       ` Petr Vorel
2023-05-30 20:37 ` [LTP] [PATCH 2/5] fcntl41: test for owner values on OFD posix locks Alexander Aring
2023-06-21  9:38   ` Petr Vorel
2023-06-30 20:00     ` Alexander Aring
2023-05-30 20:37 ` Alexander Aring [this message]
2023-05-30 20:37 ` [LTP] [PATCH 4/5] fcntl43: test for identical F_SETLKW lock requests Alexander Aring
2023-05-30 20:37 ` [LTP] [PATCH 5/5] fcntl44: test for kill child while others waiting Alexander Aring

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230530203707.2965684-4-aahringo@redhat.com \
    --to=aahringo@redhat.com \
    --cc=ltp@lists.linux.it \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.