Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/pidfd: decode pidfd file handles withou having to specify an fd

Link: https://lore.kernel.org/20250624-work-pidfs-fhandle-v2-11-d02a04858fe3@kernel.org
Reviewed-by: Jan Kara <jack@suse.cz>
Reviewed-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Christian Brauner <brauner@kernel.org>

+65 -1
+1 -1
tools/testing/selftests/pidfd/Makefile
··· 1 1 # SPDX-License-Identifier: GPL-2.0-only 2 - CFLAGS += -g $(KHDR_INCLUDES) -pthread -Wall 2 + CFLAGS += -g $(KHDR_INCLUDES) $(TOOLS_INCLUDES) -pthread -Wall 3 3 4 4 TEST_GEN_PROGS := pidfd_test pidfd_fdinfo_test pidfd_open_test \ 5 5 pidfd_poll_test pidfd_wait pidfd_getfd_test pidfd_setns_test \
+4
tools/testing/selftests/pidfd/pidfd.h
··· 19 19 #include "../kselftest.h" 20 20 #include "../clone3/clone3_selftests.h" 21 21 22 + #ifndef FD_PIDFS_ROOT 23 + #define FD_PIDFS_ROOT -10002 24 + #endif 25 + 22 26 #ifndef P_PIDFD 23 27 #define P_PIDFD 3 24 28 #endif
+60
tools/testing/selftests/pidfd/pidfd_file_handle_test.c
··· 500 500 ASSERT_EQ(close(pidfd), 0); 501 501 } 502 502 503 + /* 504 + * That we decode a file handle without having to pass a pidfd. 505 + */ 506 + TEST_F(file_handle, decode_purely_based_on_file_handle) 507 + { 508 + int mnt_id; 509 + struct file_handle *fh; 510 + int pidfd = -EBADF; 511 + struct stat st1, st2; 512 + 513 + fh = malloc(sizeof(struct file_handle) + MAX_HANDLE_SZ); 514 + ASSERT_NE(fh, NULL); 515 + memset(fh, 0, sizeof(struct file_handle) + MAX_HANDLE_SZ); 516 + fh->handle_bytes = MAX_HANDLE_SZ; 517 + 518 + ASSERT_EQ(name_to_handle_at(self->child_pidfd1, "", fh, &mnt_id, AT_EMPTY_PATH), 0); 519 + 520 + ASSERT_EQ(fstat(self->child_pidfd1, &st1), 0); 521 + 522 + pidfd = open_by_handle_at(FD_PIDFS_ROOT, fh, 0); 523 + ASSERT_GE(pidfd, 0); 524 + 525 + ASSERT_EQ(fstat(pidfd, &st2), 0); 526 + ASSERT_TRUE(st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino); 527 + 528 + ASSERT_EQ(close(pidfd), 0); 529 + 530 + pidfd = open_by_handle_at(FD_PIDFS_ROOT, fh, O_CLOEXEC); 531 + ASSERT_GE(pidfd, 0); 532 + 533 + ASSERT_EQ(fstat(pidfd, &st2), 0); 534 + ASSERT_TRUE(st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino); 535 + 536 + ASSERT_EQ(close(pidfd), 0); 537 + 538 + pidfd = open_by_handle_at(FD_PIDFS_ROOT, fh, O_NONBLOCK); 539 + ASSERT_GE(pidfd, 0); 540 + 541 + ASSERT_EQ(fstat(pidfd, &st2), 0); 542 + ASSERT_TRUE(st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino); 543 + 544 + ASSERT_EQ(close(pidfd), 0); 545 + 546 + pidfd = open_by_handle_at(self->pidfd, fh, 0); 547 + ASSERT_GE(pidfd, 0); 548 + 549 + ASSERT_EQ(fstat(pidfd, &st2), 0); 550 + ASSERT_TRUE(st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino); 551 + 552 + ASSERT_EQ(close(pidfd), 0); 553 + 554 + pidfd = open_by_handle_at(-EBADF, fh, 0); 555 + ASSERT_LT(pidfd, 0); 556 + 557 + pidfd = open_by_handle_at(AT_FDCWD, fh, 0); 558 + ASSERT_LT(pidfd, 0); 559 + 560 + free(fh); 561 + } 562 + 503 563 TEST_HARNESS_MAIN