Skip to content

Commit 914e6b1

Browse files
committed
selftests/pidfd: decode pidfd file handles withou having to specify an fd
Link: https://lore.kernel.org/[email protected] Reviewed-by: Jan Kara <[email protected]> Reviewed-by: Amir Goldstein <[email protected]> Signed-off-by: Christian Brauner <[email protected]>
1 parent b953614 commit 914e6b1

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

tools/testing/selftests/pidfd/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# SPDX-License-Identifier: GPL-2.0-only
2-
CFLAGS += -g $(KHDR_INCLUDES) -pthread -Wall
2+
CFLAGS += -g $(KHDR_INCLUDES) $(TOOLS_INCLUDES) -pthread -Wall
33

44
TEST_GEN_PROGS := pidfd_test pidfd_fdinfo_test pidfd_open_test \
55
pidfd_poll_test pidfd_wait pidfd_getfd_test pidfd_setns_test \

tools/testing/selftests/pidfd/pidfd.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
#include "../kselftest.h"
2020
#include "../clone3/clone3_selftests.h"
2121

22+
#ifndef FD_PIDFS_ROOT
23+
#define FD_PIDFS_ROOT -10002
24+
#endif
25+
2226
#ifndef P_PIDFD
2327
#define P_PIDFD 3
2428
#endif

tools/testing/selftests/pidfd/pidfd_file_handle_test.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,4 +500,64 @@ TEST_F(file_handle, valid_name_to_handle_at_flags)
500500
ASSERT_EQ(close(pidfd), 0);
501501
}
502502

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+
503563
TEST_HARNESS_MAIN

0 commit comments

Comments
 (0)