diff mbox series

[1/6] syscalls/fcntl: New test for DN_RENAME (dnotify)

Message ID 20220414145357.3184012-2-amir73il@gmail.com
State Accepted
Headers show
Series Fanotify tests for v5.17 features | expand

Commit Message

Amir Goldstein April 14, 2022, 2:53 p.m. UTC
Check that signal is delivered for directory watching for DN_RENAME
only when rename is within the same directory.
Even if both old and new directories are watches, DN_RENAME is not
generated on cross parent rename.

Signed-off-by: Amir Goldstein <amir73il@gmail.com>
---
 runtest/syscalls                           |   2 +
 testcases/kernel/syscalls/fcntl/.gitignore |   2 +
 testcases/kernel/syscalls/fcntl/fcntl39.c  | 114 +++++++++++++++++++++
 3 files changed, 118 insertions(+)
 create mode 100644 testcases/kernel/syscalls/fcntl/fcntl39.c

Comments

Petr Vorel April 14, 2022, 3:12 p.m. UTC | #1
Hi Amir,

could you please fix fcntl39.c to be running for more iterations?

./fcntl39_64 -i2
tst_kconfig.c:82: TINFO: Parsing kernel config '/proc/config.gz'
tst_test.c:1459: TINFO: Timeout per run is 0h 05m 00s
fcntl39.c:68: TINFO: Testing no DN_RENAME on rename from parent to subdir
fcntl39.c:73: TPASS: No event on parent as expected
fcntl39.c:77: TPASS: No event on subdir as expected
fcntl39.c:79: TINFO: Testing DN_RENAME on rename of subdir itself
fcntl39.c:82: TPASS: Got event on parent as expected
fcntl39.c:88: TPASS: No event on subdir as expected
fcntl39.c:62: TBROK: open(test_dir,0,20254540) failed: ENOENT (2)

Kind regards,
Petr
Amir Goldstein April 14, 2022, 3:31 p.m. UTC | #2
On Thu, Apr 14, 2022 at 6:12 PM Petr Vorel <pvorel@suse.cz> wrote:
>
> Hi Amir,
>
> could you please fix fcntl39.c to be running for more iterations?
>
> ./fcntl39_64 -i2
> tst_kconfig.c:82: TINFO: Parsing kernel config '/proc/config.gz'
> tst_test.c:1459: TINFO: Timeout per run is 0h 05m 00s
> fcntl39.c:68: TINFO: Testing no DN_RENAME on rename from parent to subdir
> fcntl39.c:73: TPASS: No event on parent as expected
> fcntl39.c:77: TPASS: No event on subdir as expected
> fcntl39.c:79: TINFO: Testing DN_RENAME on rename of subdir itself
> fcntl39.c:82: TPASS: Got event on parent as expected
> fcntl39.c:88: TPASS: No event on subdir as expected
> fcntl39.c:62: TBROK: open(test_dir,0,20254540) failed: ENOENT (2)
>
> Kind regards,
> Petr

Sorry forgot to run it with -i

Here is the fix if you want to apply it yourself:

--- a/testcases/kernel/syscalls/fcntl/fcntl39.c
+++ b/testcases/kernel/syscalls/fcntl/fcntl39.c
@@ -89,6 +89,12 @@ static void verify_dnotify(void)

        SAFE_CLOSE(parent_fd);
        SAFE_CLOSE(subdir_fd);
+
+       /* Cleanup before rerun */
+       SAFE_RENAME(TEST_DIR2 "/" TEST_FILE, TEST_FILE);
+       SAFE_RENAME(TEST_DIR2, TEST_DIR);
+       got_parent_event = 0;
+       got_subdir_event = 0;
 }

Thanks,
Amir.
Petr Vorel April 14, 2022, 7:03 p.m. UTC | #3
Hi Amir,

> Sorry forgot to run it with -i

> Here is the fix if you want to apply it yourself:

> --- a/testcases/kernel/syscalls/fcntl/fcntl39.c
> +++ b/testcases/kernel/syscalls/fcntl/fcntl39.c
> @@ -89,6 +89,12 @@ static void verify_dnotify(void)

>         SAFE_CLOSE(parent_fd);
>         SAFE_CLOSE(subdir_fd);
> +
> +       /* Cleanup before rerun */
> +       SAFE_RENAME(TEST_DIR2 "/" TEST_FILE, TEST_FILE);
> +       SAFE_RENAME(TEST_DIR2, TEST_DIR);
> +       got_parent_event = 0;
> +       got_subdir_event = 0;
>  }

Thanks for a quick fix, patch merged (with minor changes to keep checkpatch happy).

Kind regards,
Petr

> Thanks,
> Amir.
diff mbox series

Patch

diff --git a/runtest/syscalls b/runtest/syscalls
index c30383ee5..d43d6983b 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -334,6 +334,8 @@  fcntl37 fcntl37
 fcntl37_64 fcntl37_64
 fcntl38 fcntl38
 fcntl38_64 fcntl38_64
+fcntl39 fcntl39
+fcntl39_64 fcntl39_64
 
 fdatasync01 fdatasync01
 fdatasync02 fdatasync02
diff --git a/testcases/kernel/syscalls/fcntl/.gitignore b/testcases/kernel/syscalls/fcntl/.gitignore
index ede0c97b8..48b36ec34 100644
--- a/testcases/kernel/syscalls/fcntl/.gitignore
+++ b/testcases/kernel/syscalls/fcntl/.gitignore
@@ -74,3 +74,5 @@ 
 /fcntl37_64
 /fcntl38
 /fcntl38_64
+/fcntl39
+/fcntl39_64
diff --git a/testcases/kernel/syscalls/fcntl/fcntl39.c b/testcases/kernel/syscalls/fcntl/fcntl39.c
new file mode 100644
index 000000000..0fef20764
--- /dev/null
+++ b/testcases/kernel/syscalls/fcntl/fcntl39.c
@@ -0,0 +1,114 @@ 
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2021 CTERA Networks. All Rights Reserved.
+ *
+ * Started by Amir Goldstein <amir73il@gmail.com>
+ *
+ * DESCRIPTION
+ *     Check that dnotify DN_RENAME event is reported only on rename
+ *     inside same parent
+ */
+
+#include <fcntl.h>
+#include <signal.h>
+#include <stdio.h>
+#include <unistd.h>
+#include "tst_test.h"
+#include "lapi/fcntl.h"
+
+#define	TEST_DIR	"test_dir"
+#define	TEST_DIR2	"test_dir2"
+#define	TEST_FILE	"test_file"
+
+#define TEST_SIG SIGRTMIN+1
+
+static int parent_fd, subdir_fd;
+static int got_parent_event, got_subdir_event;
+
+static void dnotify_handler(int sig, siginfo_t *si, void *data __attribute__((unused)))
+{
+	if (si->si_fd == parent_fd)
+		got_parent_event = 1;
+	else if (si->si_fd == subdir_fd)
+		got_subdir_event = 1;
+	else
+		tst_brk(TBROK, "Got unexpected signal %d with si_fd %d", sig, si->si_fd);
+}
+
+static void setup_dnotify(int fd)
+{
+	struct sigaction act;
+
+	act.sa_sigaction = dnotify_handler;
+	sigemptyset(&act.sa_mask);
+	act.sa_flags = SA_SIGINFO;
+	sigaction(TEST_SIG, &act, NULL);
+
+	TEST(fcntl(fd, F_SETSIG, TEST_SIG));
+	if (TST_RET != 0) {
+		tst_brk(TBROK, "F_SETSIG failed errno = %d : %s",
+			TST_ERR, strerror(TST_ERR));
+	}
+	TEST(fcntl(fd, F_NOTIFY, DN_RENAME|DN_MULTISHOT));
+	if (TST_RET != 0) {
+		tst_brk(TBROK, "F_NOTIFY failed errno = %d : %s",
+			TST_ERR, strerror(TST_ERR));
+	}
+}
+
+static void verify_dnotify(void)
+{
+	parent_fd = SAFE_OPEN(".", O_RDONLY);
+	subdir_fd = SAFE_OPEN(TEST_DIR, O_RDONLY);
+	/* Watch renames inside ".", but not in and out of "." */
+	setup_dnotify(parent_fd);
+	/* Also watch for renames inside subdir, but not in and out of subdir */
+	setup_dnotify(subdir_fd);
+	/* Rename file from "." to subdir should not generate DN_RENAME on either */
+	tst_res(TINFO, "Testing no DN_RENAME on rename from parent to subdir");
+	SAFE_RENAME(TEST_FILE, TEST_DIR "/" TEST_FILE);
+	if (got_parent_event)
+		tst_res(TFAIL, "Got unexpected event on parent");
+	else
+		tst_res(TPASS, "No event on parent as expected");
+	if (got_subdir_event)
+		tst_res(TFAIL, "Got unexpected event on subdir");
+	else
+		tst_res(TPASS, "No event on subdir as expected");
+	/* Rename subdir itself should generate DN_RENAME on ".", but not on itself */
+	tst_res(TINFO, "Testing DN_RENAME on rename of subdir itself");
+	SAFE_RENAME(TEST_DIR, TEST_DIR2);
+	if (got_parent_event)
+		tst_res(TPASS, "Got event on parent as expected");
+	else
+		tst_res(TFAIL, "Missing event on parent");
+	if (got_subdir_event)
+		tst_res(TFAIL, "Got unexpected event on subdir");
+	else
+		tst_res(TPASS, "No event on subdir as expected");
+
+	SAFE_CLOSE(parent_fd);
+	SAFE_CLOSE(subdir_fd);
+}
+
+static void setup(void)
+{
+	SAFE_MKDIR(TEST_DIR, 00700);
+	SAFE_TOUCH(TEST_FILE, 0666, NULL);
+}
+
+static void cleanup(void)
+{
+	if (parent_fd > 0)
+		SAFE_CLOSE(parent_fd);
+	if (subdir_fd > 0)
+		SAFE_CLOSE(subdir_fd);
+}
+
+static struct tst_test test = {
+	.needs_tmpdir = 1,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = verify_dnotify,
+	.needs_kconfigs = (const char *[]) { "CONFIG_DNOTIFY=y", NULL },
+};