Skip to contentSkip to conversation

selftests/pidfd: add pidfs file handle selftests

PATCH Superseded Christian Brauner 2 participants 4 messages Dec 2, 2024 – Dec 4, 2024

Subsystems (3)
lore
Share & tools
lore

Christian Brauner

author maintainer
#0 lore raw

PATCH 1/1 selftests/pidfd: add pidfs file handle selftests

Add selftests for pidfs file handles.

---
 tools/testing/selftests/pidfd/.gitignore      |   1 +
 tools/testing/selftests/pidfd/Makefile        |   3 +-
 tools/testing/selftests/pidfd/pidfd.h         |  39 ++
 .../selftests/pidfd/pidfd_file_handle_test.c  | 352 ++++++++++++++++++
 .../selftests/pidfd/pidfd_setns_test.c        |  47 +--
 tools/testing/selftests/pidfd/pidfd_wait.c    |  47 +--
 6 files changed, 416 insertions(+), 73 deletions(-)
 create mode 100644 tools/testing/selftests/pidfd/pidfd_file_handle_test.c
tools/testing/selftests/pidfd/.gitignore +1 -0
index 973198a3ec3d..224260e1a4a2 100644
@@ -6,3 +6,4 @@ pidfd_wait
 pidfd_fdinfo_test
 pidfd_getfd_test
 pidfd_setns_test
+pidfd_file_handle_test
tools/testing/selftests/pidfd/Makefile +2 -1
index d731e3e76d5b..3c16d8e77684 100644
@@ -2,7 +2,8 @@
 CFLAGS += -g $(KHDR_INCLUDES) -pthread -Wall
 
 TEST_GEN_PROGS := pidfd_test pidfd_fdinfo_test pidfd_open_test \
-	pidfd_poll_test pidfd_wait pidfd_getfd_test pidfd_setns_test
+	pidfd_poll_test pidfd_wait pidfd_getfd_test pidfd_setns_test \
+	pidfd_file_handle_test
 
 include ../lib.mk
 
tools/testing/selftests/pidfd/pidfd.h +39 -0
index 88d6830ee004..28a471c88c51 100644
@@ -17,6 +17,7 @@
 #include <sys/wait.h>
 
 #include "../kselftest.h"
+#include "../clone3/clone3_selftests.h"
 
 #ifndef P_PIDFD
 #define P_PIDFD 3
@@ -68,6 +69,11 @@
 #define PIDFD_SKIP 3
 #define PIDFD_XFAIL 4
 
+static inline int sys_waitid(int which, pid_t pid, siginfo_t *info, int options)
+{
+	return syscall(__NR_waitid, which, pid, info, options, NULL);
+}
+
 static inline int wait_for_pid(pid_t pid)
 {
 	int status, ret;
@@ -114,4 +120,37 @@ static inline int sys_memfd_create(const char *name, unsigned int flags)
 	return syscall(__NR_memfd_create, name, flags);
 }
 
+static inline pid_t create_child(int *pidfd, unsigned flags)
+{
+	struct __clone_args args = {
+		.flags		= CLONE_PIDFD | flags,
+		.exit_signal	= SIGCHLD,
+		.pidfd		= ptr_to_u64(pidfd),
+	};
+
+	return sys_clone3(&args, sizeof(struct __clone_args));
+}
+
+static inline ssize_t read_nointr(int fd, void *buf, size_t count)
+{
+	ssize_t ret;
+
+	do {
+		ret = read(fd, buf, count);
+	} while (ret < 0 && errno == EINTR);
+
+	return ret;
+}
+
+static inline ssize_t write_nointr(int fd, const void *buf, size_t count)
+{
+	ssize_t ret;
+
+	do {
+		ret = write(fd, buf, count);
+	} while (ret < 0 && errno == EINTR);
+
+	return ret;
+}
+
 #endif /* __PIDFD_H */
tools/testing/selftests/pidfd/pidfd_file_handle_test.c new file +352 -0

352 lines not on this page · raw message

tools/testing/selftests/pidfd/pidfd_setns_test.c +4 -43

77 lines not on this page · raw message

tools/testing/selftests/pidfd/pidfd_wait.c +18 -29

152 lines not on this page · raw message

Signature · 1 line
2.45.2
author maintainer
#1 lore raw

PATCH v2 1/1 selftests/pidfd: add pidfs file handle selftests

Add selftests for pidfs file handles.

---
I've added a bunch more selftests.
Frankly, I'm going to probably be adding more as corner cases come to me. And I'm just going to be amending the patch and stuffing them into the tree unless I hear objections.

---
 tools/testing/selftests/pidfd/.gitignore      |   1 +
 tools/testing/selftests/pidfd/Makefile        |   3 +-
 tools/testing/selftests/pidfd/pidfd.h         |  39 ++
 .../selftests/pidfd/pidfd_file_handle_test.c  | 503 ++++++++++++++++++
 .../selftests/pidfd/pidfd_setns_test.c        |  47 +-
 tools/testing/selftests/pidfd/pidfd_wait.c    |  47 +-
 6 files changed, 567 insertions(+), 73 deletions(-)
 create mode 100644 tools/testing/selftests/pidfd/pidfd_file_handle_test.c
tools/testing/selftests/pidfd/.gitignore +1 -0
index 973198a3ec3d..224260e1a4a2 100644
@@ -6,3 +6,4 @@ pidfd_wait
 pidfd_fdinfo_test
 pidfd_getfd_test
 pidfd_setns_test
+pidfd_file_handle_test
tools/testing/selftests/pidfd/Makefile +2 -1
index d731e3e76d5b..3c16d8e77684 100644
@@ -2,7 +2,8 @@
 CFLAGS += -g $(KHDR_INCLUDES) -pthread -Wall
 
 TEST_GEN_PROGS := pidfd_test pidfd_fdinfo_test pidfd_open_test \
-	pidfd_poll_test pidfd_wait pidfd_getfd_test pidfd_setns_test
+	pidfd_poll_test pidfd_wait pidfd_getfd_test pidfd_setns_test \
+	pidfd_file_handle_test
 
 include ../lib.mk
 
tools/testing/selftests/pidfd/pidfd.h +39 -0
index 88d6830ee004..28a471c88c51 100644
@@ -17,6 +17,7 @@
 #include <sys/wait.h>
 
 #include "../kselftest.h"
+#include "../clone3/clone3_selftests.h"
 
 #ifndef P_PIDFD
 #define P_PIDFD 3
@@ -68,6 +69,11 @@
 #define PIDFD_SKIP 3
 #define PIDFD_XFAIL 4
 
+static inline int sys_waitid(int which, pid_t pid, siginfo_t *info, int options)
+{
+	return syscall(__NR_waitid, which, pid, info, options, NULL);
+}
+
 static inline int wait_for_pid(pid_t pid)
 {
 	int status, ret;
@@ -114,4 +120,37 @@ static inline int sys_memfd_create(const char *name, unsigned int flags)
 	return syscall(__NR_memfd_create, name, flags);
 }
 
+static inline pid_t create_child(int *pidfd, unsigned flags)
+{
+	struct __clone_args args = {
+		.flags		= CLONE_PIDFD | flags,
+		.exit_signal	= SIGCHLD,
+		.pidfd		= ptr_to_u64(pidfd),
+	};
+
+	return sys_clone3(&args, sizeof(struct __clone_args));
+}
+
+static inline ssize_t read_nointr(int fd, void *buf, size_t count)
+{
+	ssize_t ret;
+
+	do {
+		ret = read(fd, buf, count);
+	} while (ret < 0 && errno == EINTR);
+
+	return ret;
+}
+
+static inline ssize_t write_nointr(int fd, const void *buf, size_t count)
+{
+	ssize_t ret;
+
+	do {
+		ret = write(fd, buf, count);
+	} while (ret < 0 && errno == EINTR);
+
+	return ret;
+}
+
 #endif /* __PIDFD_H */
tools/testing/selftests/pidfd/pidfd_file_handle_test.c new file +503 -0

503 lines not on this page · raw message

tools/testing/selftests/pidfd/pidfd_setns_test.c +4 -43

77 lines not on this page · raw message

tools/testing/selftests/pidfd/pidfd_wait.c +18 -29

152 lines not on this page · raw message

Signature · 1 line
2.45.2
Reviewed-by on v2 1/1
#2 lore raw
Quoted 11 lines · Christian Brauner

Add selftests for pidfs file handles.

---
I've added a bunch more selftests.
Frankly, I'm going to probably be adding more as corner cases come to me. And I'm just going to be amending the patch and stuffing them into the tree unless I hear objections.
---

Generally, tests look good and you may add

But I wonder...

[...]

Quoted 21 lines
tools/testing/selftests/pidfd/pidfd_file_handle_test.c new file +21 -0
+/*
+ * Test valid flags to open a pidfd file handle. Note, that
+ * PIDFD_NONBLOCK is defined as O_NONBLOCK and O_NONBLOCK is an alias to
+ * O_NDELAY. Also note that PIDFD_THREAD is an alias for O_EXCL.
+ */
+TEST_F(file_handle, open_by_handle_at_valid_flags)
+{
+       int mnt_id;
+       struct file_handle *fh;
+       int pidfd = -EBADF;
+       struct stat st1, st2;
+
+       fh = malloc(sizeof(struct file_handle) + MAX_HANDLE_SZ);
+       ASSERT_NE(fh, NULL);
+       memset(fh, 0, sizeof(struct file_handle) + MAX_HANDLE_SZ);
+       fh->handle_bytes = MAX_HANDLE_SZ;
+
+       ASSERT_EQ(name_to_handle_at(self->child_pidfd2, "", fh, &mnt_id, AT_EMPTY_PATH), 0);
+
+       ASSERT_EQ(fstat(self->child_pidfd2, &st1), 0);
+

tools/testing/selftests/pidfd/pidfd_file_handle_test.c line 376

Quoted 10 lines
tools/testing/selftests/pidfd/pidfd_file_handle_test.c new file +10 -0
+       pidfd = open_by_handle_at(self->pidfd, fh,
+                                 O_RDONLY |
+                                 O_WRONLY |
+                                 O_RDWR |
+                                 O_NONBLOCK |
+                                 O_NDELAY |
+                                 O_CLOEXEC |
+                                 O_EXCL);
+       ASSERT_GE(pidfd, 0);
+

IIRC, your patch always opens an fd with O_RDWR. Right?

Isn't it confusing to request O_RDONLY and get O_RDWR? Should we only allow requests to request open_by_handle_at() with O_RDWR mode?

Thanks,
Amir.

author maintainer
#3 lore raw
Quoted 55 lines · Amir Goldstein
Quoted 11 lines · Christian Brauner

Add selftests for pidfs file handles.

---
I've added a bunch more selftests.
Frankly, I'm going to probably be adding more as corner cases come to me. And I'm just going to be amending the patch and stuffing them into the tree unless I hear objections.
---

Generally, tests look good and you may add

But I wonder...

[...]

Quoted 21 lines
tools/testing/selftests/pidfd/pidfd_file_handle_test.c new file +21 -0
+/*
+ * Test valid flags to open a pidfd file handle. Note, that
+ * PIDFD_NONBLOCK is defined as O_NONBLOCK and O_NONBLOCK is an alias to
+ * O_NDELAY. Also note that PIDFD_THREAD is an alias for O_EXCL.
+ */
+TEST_F(file_handle, open_by_handle_at_valid_flags)
+{
+       int mnt_id;
+       struct file_handle *fh;
+       int pidfd = -EBADF;
+       struct stat st1, st2;
+
+       fh = malloc(sizeof(struct file_handle) + MAX_HANDLE_SZ);
+       ASSERT_NE(fh, NULL);
+       memset(fh, 0, sizeof(struct file_handle) + MAX_HANDLE_SZ);
+       fh->handle_bytes = MAX_HANDLE_SZ;
+
+       ASSERT_EQ(name_to_handle_at(self->child_pidfd2, "", fh, &mnt_id, AT_EMPTY_PATH), 0);
+
+       ASSERT_EQ(fstat(self->child_pidfd2, &st1), 0);
+

tools/testing/selftests/pidfd/pidfd_file_handle_test.c line 376

Quoted 10 lines
tools/testing/selftests/pidfd/pidfd_file_handle_test.c new file +10 -0
+       pidfd = open_by_handle_at(self->pidfd, fh,
+                                 O_RDONLY |
+                                 O_WRONLY |
+                                 O_RDWR |
+                                 O_NONBLOCK |
+                                 O_NDELAY |
+                                 O_CLOEXEC |
+                                 O_EXCL);
+       ASSERT_GE(pidfd, 0);
+

IIRC, your patch always opens an fd with O_RDWR. Right?

Quoted 3 lines

Isn't it confusing to request O_RDONLY and get O_RDWR? Should we only allow requests to request open_by_handle_at() with O_RDWR mode?

I think it's nicer to allow people to use open_by_handle_at() with a zero flag value as O_RDONLY is 0 and have things just work instead of confusing them by forcing them to specify O_RDWR. Similar to how pidfd_open() always gives you a O_RDWR file.