linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Christian Brauner <brauner@kernel.org>
To: Jeff Layton <jlayton@kernel.org>,
	Chuck Lever <chuck.lever@oracle.com>,  Jan Kara <jack@suse.cz>,
	Amir Goldstein <amir73il@gmail.com>,
	 Simona Vetter <simona@ffwll.ch>
Cc: linux-fsdevel@vger.kernel.org, linux-nfs@vger.kernel.org,
	 Christian Brauner <brauner@kernel.org>
Subject: [PATCH v2 11/11] selftests/pidfd: decode pidfd file handles withou having to specify an fd
Date: Tue, 24 Jun 2025 10:29:14 +0200	[thread overview]
Message-ID: <20250624-work-pidfs-fhandle-v2-11-d02a04858fe3@kernel.org> (raw)
In-Reply-To: <20250624-work-pidfs-fhandle-v2-0-d02a04858fe3@kernel.org>

Signed-off-by: Christian Brauner <brauner@kernel.org>
---
 tools/testing/selftests/pidfd/Makefile             |  2 +-
 tools/testing/selftests/pidfd/pidfd.h              |  4 ++
 .../selftests/pidfd/pidfd_file_handle_test.c       | 60 ++++++++++++++++++++++
 3 files changed, 65 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/pidfd/Makefile b/tools/testing/selftests/pidfd/Makefile
index 03a6eede9c9e..764a8f9ecefa 100644
--- a/tools/testing/selftests/pidfd/Makefile
+++ b/tools/testing/selftests/pidfd/Makefile
@@ -1,5 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0-only
-CFLAGS += -g $(KHDR_INCLUDES) -pthread -Wall
+CFLAGS += -g $(KHDR_INCLUDES) $(TOOLS_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 \
diff --git a/tools/testing/selftests/pidfd/pidfd.h b/tools/testing/selftests/pidfd/pidfd.h
index 5dfeb1bdf399..b427a2636402 100644
--- a/tools/testing/selftests/pidfd/pidfd.h
+++ b/tools/testing/selftests/pidfd/pidfd.h
@@ -19,6 +19,10 @@
 #include "../kselftest.h"
 #include "../clone3/clone3_selftests.h"
 
+#ifndef FD_INVALID
+#define FD_INVALID -10009 /* Invalid file descriptor. */
+#endif
+
 #ifndef P_PIDFD
 #define P_PIDFD 3
 #endif
diff --git a/tools/testing/selftests/pidfd/pidfd_file_handle_test.c b/tools/testing/selftests/pidfd/pidfd_file_handle_test.c
index 439b9c6c0457..ff1bf51bca5e 100644
--- a/tools/testing/selftests/pidfd/pidfd_file_handle_test.c
+++ b/tools/testing/selftests/pidfd/pidfd_file_handle_test.c
@@ -500,4 +500,64 @@ TEST_F(file_handle, valid_name_to_handle_at_flags)
 	ASSERT_EQ(close(pidfd), 0);
 }
 
+/*
+ * That we decode a file handle without having to pass a pidfd.
+ */
+TEST_F(file_handle, decode_purely_based_on_file_handle)
+{
+	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_pidfd1, "", fh, &mnt_id, AT_EMPTY_PATH), 0);
+
+	ASSERT_EQ(fstat(self->child_pidfd1, &st1), 0);
+
+	pidfd = open_by_handle_at(FD_INVALID, fh, 0);
+	ASSERT_GE(pidfd, 0);
+
+	ASSERT_EQ(fstat(pidfd, &st2), 0);
+	ASSERT_TRUE(st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino);
+
+	ASSERT_EQ(close(pidfd), 0);
+
+	pidfd = open_by_handle_at(FD_INVALID, fh, O_CLOEXEC);
+	ASSERT_GE(pidfd, 0);
+
+	ASSERT_EQ(fstat(pidfd, &st2), 0);
+	ASSERT_TRUE(st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino);
+
+	ASSERT_EQ(close(pidfd), 0);
+
+	pidfd = open_by_handle_at(FD_INVALID, fh, O_NONBLOCK);
+	ASSERT_GE(pidfd, 0);
+
+	ASSERT_EQ(fstat(pidfd, &st2), 0);
+	ASSERT_TRUE(st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino);
+
+	ASSERT_EQ(close(pidfd), 0);
+
+	pidfd = open_by_handle_at(self->pidfd, fh, 0);
+	ASSERT_GE(pidfd, 0);
+
+	ASSERT_EQ(fstat(pidfd, &st2), 0);
+	ASSERT_TRUE(st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino);
+
+	ASSERT_EQ(close(pidfd), 0);
+
+	pidfd = open_by_handle_at(-EBADF, fh, 0);
+	ASSERT_LT(pidfd, 0);
+
+	pidfd = open_by_handle_at(AT_FDCWD, fh, 0);
+	ASSERT_LT(pidfd, 0);
+
+	free(fh);
+}
+
 TEST_HARNESS_MAIN

-- 
2.47.2


  parent reply	other threads:[~2025-06-24  8:29 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-24  8:29 [PATCH v2 00/11] fhandle, pidfs: allow open_by_handle_at() purely based on file handle Christian Brauner
2025-06-24  8:29 ` [PATCH v2 01/11] fhandle: raise FILEID_IS_DIR in handle_type Christian Brauner
2025-06-24  9:31   ` Jan Kara
2025-06-24  8:29 ` [PATCH v2 02/11] fhandle: hoist copy_from_user() above get_path_from_fd() Christian Brauner
2025-06-24  9:31   ` Jan Kara
2025-06-24  8:29 ` [PATCH v2 03/11] fhandle: rename to get_path_anchor() Christian Brauner
2025-06-24  9:31   ` Jan Kara
2025-06-24  8:29 ` [PATCH v2 04/11] pidfs: add pidfs_root_path() helper Christian Brauner
2025-06-24  9:31   ` Jan Kara
2025-06-24  8:29 ` [PATCH v2 05/11] fhandle: reflow get_path_anchor() Christian Brauner
2025-06-24  9:16   ` Jan Kara
2025-06-24 10:16     ` Christian Brauner
2025-06-24  8:29 ` [PATCH v2 06/11] uapi/fcntl: mark range as reserved Christian Brauner
2025-06-24  9:16   ` Jan Kara
2025-06-24 10:57   ` Amir Goldstein
2025-06-24 13:47     ` Christian Brauner
2025-06-24  8:29 ` [PATCH v2 07/11] uapi/fcntl: add FD_INVALID Christian Brauner
2025-06-24  9:17   ` Jan Kara
2025-06-24  8:29 ` [PATCH v2 08/11] exportfs: add FILEID_PIDFS Christian Brauner
2025-06-24  9:17   ` Jan Kara
2025-06-24 13:15   ` Amir Goldstein
2025-06-24 13:43     ` Christian Brauner
2025-06-24 14:20       ` Amir Goldstein
2025-06-24  8:29 ` [PATCH v2 09/11] fhandle: add EXPORT_OP_AUTONOMOUS_HANDLES marker Christian Brauner
2025-06-24  9:18   ` Jan Kara
2025-06-24  9:20   ` Jan Kara
2025-06-24 10:16     ` Christian Brauner
2025-06-24  8:29 ` [PATCH v2 10/11] fhandle, pidfs: support open_by_handle_at() purely based on file handle Christian Brauner
2025-06-24  9:30   ` Jan Kara
2025-06-24 10:15     ` Christian Brauner
2025-06-24 10:53     ` Amir Goldstein
2025-06-24 14:28       ` Amir Goldstein
2025-06-24 14:51         ` Christian Brauner
2025-06-24 15:07           ` Amir Goldstein
2025-06-24 15:23             ` Christian Brauner
2025-06-24 17:45               ` Jan Kara
2025-06-24 19:23               ` Amir Goldstein
2025-06-25  7:52                 ` Christian Brauner
2025-06-24 23:07   ` Al Viro
2025-06-25  7:52     ` Christian Brauner
2025-06-24  8:29 ` Christian Brauner [this message]
2025-06-24  9:39   ` [PATCH v2 11/11] selftests/pidfd: decode pidfd file handles withou having to specify an fd Jan Kara
2025-06-24 10:58 ` [PATCH v2 00/11] fhandle, pidfs: allow open_by_handle_at() purely based on file handle Amir Goldstein
2025-06-24 10:59 ` Christian Brauner
2025-06-24 14:15   ` Jan Kara
2025-06-24 14:34     ` Amir Goldstein
2025-06-24 14:39     ` Christian Brauner

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=20250624-work-pidfs-fhandle-v2-11-d02a04858fe3@kernel.org \
    --to=brauner@kernel.org \
    --cc=amir73il@gmail.com \
    --cc=chuck.lever@oracle.com \
    --cc=jack@suse.cz \
    --cc=jlayton@kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-nfs@vger.kernel.org \
    --cc=simona@ffwll.ch \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).