Skip to contentSkip to conversation

syscalls: Add tests for name_to_handle_at()

PATCH New Viresh Kumar 2 participants 6 messages Nov 24, 2020 – Dec 8, 2020

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/2 syscalls: Add tests for name_to_handle_at()

This adds basic success and failure tests for name_to_handle_at() syscall.

---
V2->V3:
- Implement name_to_handle_at() and open_by_handle_at() in the header file with tst_syscall().

- Add test for dirfd referring to a file with AT_EMPTY_PATH flag.

 include/lapi/name_to_handle_at.h              |  63 ++++++++++
 runtest/syscalls                              |   3 +
 .../syscalls/name_to_handle_at/.gitignore     |   2 +
 .../syscalls/name_to_handle_at/Makefile       |   7 ++
 .../name_to_handle_at/name_to_handle_at01.c   | 116 ++++++++++++++++++
 .../name_to_handle_at/name_to_handle_at02.c   |  82 +++++++++++++
 6 files changed, 273 insertions(+)
 create mode 100644 include/lapi/name_to_handle_at.h
 create mode 100644 testcases/kernel/syscalls/name_to_handle_at/.gitignore
 create mode 100644 testcases/kernel/syscalls/name_to_handle_at/Makefile
 create mode 100644 testcases/kernel/syscalls/name_to_handle_at/name_to_handle_at01.c
 create mode 100644 testcases/kernel/syscalls/name_to_handle_at/name_to_handle_at02.c
include/lapi/name_to_handle_at.h new file +63 -0
new file mode 100644
index 000000000000..16cba239d769
@@ -0,0 +1,63 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2020 Linaro Limited. All rights reserved.
+ * Author: Viresh Kumar <[email protected]>
+ */
+
+#ifndef NAME_TO_HANDLE_AT_H__
+#define NAME_TO_HANDLE_AT_H__
+
+#include <sys/syscall.h>
+#include "config.h"
+#include "lapi/syscalls.h"
+#include "tst_buffers.h"
+#include "tst_test.h"
+
+#ifndef HAVE_NAME_TO_HANDLE_AT
+struct file_handle {
+	unsigned int handle_bytes;
+	int handle_type;
+	/* File identifier.  */
+	unsigned char f_handle[0];
+};
+
+int name_to_handle_at(int dfd, const char *pathname, struct file_handle *handle,
+		      int *mount_id, int flags)
+{
+	return tst_syscall(__NR_name_to_handle_at, dfd, pathname, handle,
+			   mount_id, flags);
+}
+
+int open_by_handle_at(int mount_fd, struct file_handle *handle, int flags)
+{
+	return tst_syscall(__NR_open_by_handle_at, mount_fd, handle, flags);
+}
+#endif
+
+/* Returns a valid pointer on success, NULL on errors */
+static inline struct file_handle *
+allocate_file_handle(int dfd, const char *pathname)
+{
+	struct file_handle fh = {}, *fhp;
+	int mount_id;
+
+	/*
+	 * Make an initial call to name_to_handle_at() to discover the size
+	 * required for the file handle.
+	 */
+	TEST(name_to_handle_at(dfd, pathname, &fh, &mount_id, 0));
+	if (TST_RET != -1 || TST_ERR != EOVERFLOW) {
+		tst_res(TFAIL | TTERRNO,
+			"name_to_handle_at() should fail with EOVERFLOW");
+		return NULL;
+	}
+
+	/* Valid file handle */
+	fhp = tst_alloc(sizeof(*fhp) + fh.handle_bytes);
+	fhp->handle_type = fh.handle_type;
+	fhp->handle_bytes = fh.handle_bytes;
+
+	return fhp;
+}
+
+#endif /* NAME_TO_HANDLE_AT_H__ */
runtest/syscalls +3 -0
index adf410ed11a4..e9299bf3fe2a 100644
@@ -843,6 +843,9 @@ nanosleep01 nanosleep01
 nanosleep02 nanosleep02
 nanosleep04 nanosleep04
 
+name_to_handle_at01 name_to_handle_at01
+name_to_handle_at02 name_to_handle_at02
+
 nftw01 nftw01
 nftw6401 nftw6401
 
testcases/kernel/syscalls/name_to_handle_at/.gitignore new file +2 -0
new file mode 100644
index 000000000000..268a8a34f85e
@@ -0,0 +1,2 @@
+name_to_handle_at01
+name_to_handle_at02
testcases/kernel/syscalls/name_to_handle_at/Makefile new file +7 -0
new file mode 100644
index 000000000000..18896b6f28c0
@@ -0,0 +1,7 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+top_srcdir		?= ../../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/kernel/syscalls/name_to_handle_at/name_to_handle_at01.c new file +116 -0

116 lines not on this page · raw message

testcases/kernel/syscalls/name_to_handle_at/name_to_handle_at02.c new file +82 -0

82 lines not on this page · raw message

Signature · 1 line
2.25.0.rc1.19.g042ed3e048af
#1 lore raw

PATCH v3 2/2 syscalls: Add tests for open_by_handle_at()

This adds basic success and failure tests for open_by_handle_at() syscall.

---
 runtest/syscalls                              |   3 +
 .../syscalls/open_by_handle_at/.gitignore     |   2 +
 .../syscalls/open_by_handle_at/Makefile       |   7 ++
 .../open_by_handle_at/open_by_handle_at01.c   | 115 ++++++++++++++++++
 .../open_by_handle_at/open_by_handle_at02.c   | 109 +++++++++++++++++
 5 files changed, 236 insertions(+)
 create mode 100644 testcases/kernel/syscalls/open_by_handle_at/.gitignore
 create mode 100644 testcases/kernel/syscalls/open_by_handle_at/Makefile
 create mode 100644 testcases/kernel/syscalls/open_by_handle_at/open_by_handle_at01.c
 create mode 100644 testcases/kernel/syscalls/open_by_handle_at/open_by_handle_at02.c
runtest/syscalls +3 -0
index e9299bf3fe2a..25179b74ecdf 100644
@@ -879,6 +879,9 @@ openat201 openat201
 openat202 openat202
 openat203 openat203
 
+open_by_handle_at01 open_by_handle_at01
+open_by_handle_at02 open_by_handle_at02
+
 open_tree01 open_tree01
 open_tree02 open_tree02
 
testcases/kernel/syscalls/open_by_handle_at/.gitignore new file +2 -0
new file mode 100644
index 000000000000..799eba82a90b
@@ -0,0 +1,2 @@
+open_by_handle_at01
+open_by_handle_at02
testcases/kernel/syscalls/open_by_handle_at/Makefile new file +7 -0
new file mode 100644
index 000000000000..18896b6f28c0
@@ -0,0 +1,7 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+top_srcdir		?= ../../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/kernel/syscalls/open_by_handle_at/open_by_handle_at01.c new file +115 -0

115 lines not on this page · raw message

testcases/kernel/syscalls/open_by_handle_at/open_by_handle_at02.c new file +109 -0

109 lines not on this page · raw message

Signature · 1 line
2.25.0.rc1.19.g042ed3e048af
#2 lore raw

PATCH v3 2/2 syscalls: Add tests for open_by_handle_at()

Hi!

Quoted 36 lines
testcases/kernel/syscalls/open_by_handle_at/open_by_handle_at01.c new file +36 -0
+#define _GNU_SOURCE
+#include <fcntl.h>
+#include <sys/stat.h>
+#include "lapi/name_to_handle_at.h"
+
+#define TEST_FILE "test_file"
+#define TEST_DIR "test_dir"
+
+static int dir_fd, fd_atcwd = AT_FDCWD, file_fd;
+static struct file_handle *fhp;
+
+static struct tcase {
+	int *dfd;
+	const char *pathname;
+	int name_flags;
+	int flags;
+} tcases[] = {
+	{&dir_fd, TEST_FILE, 0, O_RDWR},
+	{&dir_fd, TEST_FILE, 0, O_RDONLY},
+	{&dir_fd, TEST_FILE, 0, O_WRONLY},
+	{&dir_fd, TEST_FILE, AT_EMPTY_PATH, O_RDWR},
+	{&dir_fd, TEST_FILE, AT_EMPTY_PATH, O_RDONLY},
+	{&dir_fd, TEST_FILE, AT_EMPTY_PATH, O_WRONLY},
+	{&dir_fd, TEST_FILE, AT_SYMLINK_FOLLOW, O_RDWR},
+	{&dir_fd, TEST_FILE, AT_SYMLINK_FOLLOW, O_RDONLY},
+	{&dir_fd, TEST_FILE, AT_SYMLINK_FOLLOW, O_WRONLY},
+	{&dir_fd, TEST_FILE, AT_EMPTY_PATH | AT_SYMLINK_FOLLOW, O_RDWR},
+	{&dir_fd, TEST_FILE, AT_EMPTY_PATH | AT_SYMLINK_FOLLOW, O_RDONLY},
+	{&dir_fd, TEST_FILE, AT_EMPTY_PATH | AT_SYMLINK_FOLLOW, O_WRONLY},
+	{&dir_fd, "", AT_EMPTY_PATH, O_RDONLY},
+	{&file_fd, "", AT_EMPTY_PATH, O_RDONLY},
+
+	{&fd_atcwd, TEST_FILE, 0, O_RDWR},
+	{&fd_atcwd, TEST_FILE, 0, O_RDONLY},
+	{&fd_atcwd, TEST_FILE, 0, O_WRONLY},
+	{&fd_atcwd, TEST_FILE, AT_EMPTY_PATH, O_RDWR},

testcases/kernel/syscalls/open_by_handle_at/open_by_handle_at01.c line 60

Quoted 10 lines
testcases/kernel/syscalls/open_by_handle_at/open_by_handle_at01.c new file +10 -0
+	{&fd_atcwd, TEST_FILE, AT_EMPTY_PATH, O_RDONLY},
+	{&fd_atcwd, TEST_FILE, AT_EMPTY_PATH, O_WRONLY},
+	{&fd_atcwd, TEST_FILE, AT_SYMLINK_FOLLOW, O_RDWR},
+	{&fd_atcwd, TEST_FILE, AT_SYMLINK_FOLLOW, O_RDONLY},
+	{&fd_atcwd, TEST_FILE, AT_SYMLINK_FOLLOW, O_WRONLY},
+	{&fd_atcwd, TEST_FILE, AT_EMPTY_PATH | AT_SYMLINK_FOLLOW, O_RDWR},
+	{&fd_atcwd, TEST_FILE, AT_EMPTY_PATH | AT_SYMLINK_FOLLOW, O_RDONLY},
+	{&fd_atcwd, TEST_FILE, AT_EMPTY_PATH | AT_SYMLINK_FOLLOW, O_WRONLY},
+	{&fd_atcwd, "", AT_EMPTY_PATH, O_RDONLY},
+};

I guess that we can do better than exact copy of the name_to_handle_at01.c.

We can as well create two handles in the test setup one for directory and one for file and then loop over different flags for open_by_handle_at() instead of getting the handle in each run() iteration.

Signature · 2 lines
Cyril Hrubis
[email protected]
#3 lore raw

PATCH 1/1 syscalls: Allocate handles at setup time only

Move the allocation of handles to setup time only, so the run() helper can focus on the syscall to test.

---
Cyril, I think you merged the patch for open_by_handle_at by mistake, and that too without your signed-off.

commit 81e0821e1fc1 ("syscalls: Add tests for open_by_handle_at()")

Here is what you asked for in your earlier email.

 .../open_by_handle_at/open_by_handle_at01.c   | 83 +++++++++----------
 1 file changed, 41 insertions(+), 42 deletions(-)
testcases/kernel/syscalls/open_by_handle_at/open_by_handle_at01.c +41 -42
index 35597a13c182..2583bc3a186d 100644
@@ -21,42 +21,23 @@
 #define TEST_DIR "test_dir"
 
 static int dir_fd, fd_atcwd = AT_FDCWD, file_fd;
-static struct file_handle *fhp;
+static struct file_handle *f_fhp, *d_fhp, *at_fhp;
+static struct file_handle *f_fhp, *d_fhp, *at_fhp;
 
 static struct tcase {
 	int *dfd;
-	const char *pathname;
-	int name_flags;
+	struct file_handle **fhp;
 	int flags;
 } tcases[] = {
-	{&dir_fd, TEST_FILE, 0, O_RDWR},
-	{&dir_fd, TEST_FILE, 0, O_RDONLY},
-	{&dir_fd, TEST_FILE, 0, O_WRONLY},
-	{&dir_fd, TEST_FILE, AT_EMPTY_PATH, O_RDWR},
-	{&dir_fd, TEST_FILE, AT_EMPTY_PATH, O_RDONLY},
-	{&dir_fd, TEST_FILE, AT_EMPTY_PATH, O_WRONLY},
-	{&dir_fd, TEST_FILE, AT_SYMLINK_FOLLOW, O_RDWR},
-	{&dir_fd, TEST_FILE, AT_SYMLINK_FOLLOW, O_RDONLY},
-	{&dir_fd, TEST_FILE, AT_SYMLINK_FOLLOW, O_WRONLY},
-	{&dir_fd, TEST_FILE, AT_EMPTY_PATH | AT_SYMLINK_FOLLOW, O_RDWR},
-	{&dir_fd, TEST_FILE, AT_EMPTY_PATH | AT_SYMLINK_FOLLOW, O_RDONLY},
-	{&dir_fd, TEST_FILE, AT_EMPTY_PATH | AT_SYMLINK_FOLLOW, O_WRONLY},
-	{&dir_fd, "", AT_EMPTY_PATH, O_RDONLY},
-	{&file_fd, "", AT_EMPTY_PATH, O_RDONLY},
-
-	{&fd_atcwd, TEST_FILE, 0, O_RDWR},
-	{&fd_atcwd, TEST_FILE, 0, O_RDONLY},
-	{&fd_atcwd, TEST_FILE, 0, O_WRONLY},
-	{&fd_atcwd, TEST_FILE, AT_EMPTY_PATH, O_RDWR},
-	{&fd_atcwd, TEST_FILE, AT_EMPTY_PATH, O_RDONLY},
-	{&fd_atcwd, TEST_FILE, AT_EMPTY_PATH, O_WRONLY},
-	{&fd_atcwd, TEST_FILE, AT_SYMLINK_FOLLOW, O_RDWR},
-	{&fd_atcwd, TEST_FILE, AT_SYMLINK_FOLLOW, O_RDONLY},
-	{&fd_atcwd, TEST_FILE, AT_SYMLINK_FOLLOW, O_WRONLY},
-	{&fd_atcwd, TEST_FILE, AT_EMPTY_PATH | AT_SYMLINK_FOLLOW, O_RDWR},
-	{&fd_atcwd, TEST_FILE, AT_EMPTY_PATH | AT_SYMLINK_FOLLOW, O_RDONLY},
-	{&fd_atcwd, TEST_FILE, AT_EMPTY_PATH | AT_SYMLINK_FOLLOW, O_WRONLY},
-	{&fd_atcwd, "", AT_EMPTY_PATH, O_RDONLY},
+	{&dir_fd, &d_fhp, O_RDWR},
+	{&dir_fd, &d_fhp, O_RDONLY},
+	{&dir_fd, &d_fhp, O_WRONLY},
+	{&file_fd, &f_fhp, O_RDWR},
+	{&file_fd, &f_fhp, O_RDONLY},
+	{&file_fd, &f_fhp, O_WRONLY},
+	{&fd_atcwd, &at_fhp, O_RDWR},
+	{&fd_atcwd, &at_fhp, O_RDONLY},
+	{&fd_atcwd, &at_fhp, O_WRONLY},
 };
 
 static void cleanup(void)
@@ -67,28 +48,46 @@ static void cleanup(void)
 
 static void setup(void)
 {
+	int mount_id;
+
 	SAFE_MKDIR(TEST_DIR, 0700);
 	dir_fd = SAFE_OPEN(TEST_DIR, O_DIRECTORY);
 	SAFE_CHDIR(TEST_DIR);
 	SAFE_TOUCH(TEST_FILE, 0600, NULL);
 	file_fd = SAFE_OPEN("foo_file", O_RDWR | O_CREAT);
-	fhp = allocate_file_handle(AT_FDCWD, TEST_FILE);
+
+	f_fhp = allocate_file_handle(AT_FDCWD, TEST_FILE);
+	d_fhp = allocate_file_handle(AT_FDCWD, TEST_FILE);
+	at_fhp = allocate_file_handle(AT_FDCWD, TEST_FILE);
+
+	TEST(name_to_handle_at(file_fd, "", f_fhp, &mount_id, AT_EMPTY_PATH));
+	if (TST_RET) {
+		tst_res(TFAIL | TTERRNO, "name_to_handle_at() failed");
+		return;
+	}
+
+	TEST(name_to_handle_at(dir_fd, TEST_FILE, d_fhp, &mount_id,
+			       AT_EMPTY_PATH | AT_SYMLINK_FOLLOW));
+	if (TST_RET) {
+		tst_res(TFAIL | TTERRNO, "name_to_handle_at() failed");
+		return;
+	}
+
+	TEST(name_to_handle_at(AT_FDCWD, TEST_FILE, at_fhp, &mount_id,
+			       AT_EMPTY_PATH | AT_SYMLINK_FOLLOW));
+	if (TST_RET) {
+		tst_res(TFAIL | TTERRNO, "name_to_handle_at() failed");
+		return;
+	}
 }
 
 static void run(unsigned int n)
 {
 	struct tcase *tc = &tcases[n];
 	struct stat file_stat;
-	int fd, mount_id;
-
-	TEST(name_to_handle_at(*tc->dfd, tc->pathname, fhp, &mount_id,
-			       tc->name_flags));
-	if (TST_RET) {
-		tst_res(TFAIL | TTERRNO, "name_to_handle_at() failed (%d)", n);
-		return;
-	}
+	int fd;
 
-	TEST(fd = open_by_handle_at(*tc->dfd, fhp, tc->flags));
+	TEST(fd = open_by_handle_at(*tc->dfd, *tc->fhp, tc->flags));
 	if (fd < 0) {
 		tst_res(TFAIL | TTERRNO, "open_by_handle_at() failed (%d)", n);
 		return;
@@ -97,7 +96,7 @@ static void run(unsigned int n)
 	SAFE_FSTAT(fd, &file_stat);
 
 	/* Don't check stats when pathname is empty */
-	if (file_stat.st_size == 0 || !tc->pathname[0])
+	if (file_stat.st_size == 0 || (tc->fhp == &f_fhp))
 		tst_res(TPASS, "open_by_handle_at() passed (%d)", n);
 	else
 		tst_res(TFAIL, "fstat() didn't work as expected (%d)", n);
Signature · 1 line
2.25.0.rc1.19.g042ed3e048af
#4 lore raw

PATCH 1/1 syscalls: Allocate handles at setup time only

Hi!

Quoted 2 lines

Cyril, I think you merged the patch for open_by_handle_at by mistake, and that too without your signed-off.

Sorry, looks like I had messed up again. I will revert that one and apply it again with these changes on the top. Thanks a lot.

Signature · 2 lines
Cyril Hrubis
[email protected]