mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2026-02-15 15:36:31 -05:00
This follow-up patch completes centralization of kselftest.h and ksefltest_harness.h includes in remaining seltests files, replacing all relative paths with a non-relative paths using shared -I include path in lib.mk Tested with gcc-13.3 and clang-18.1, and cross-compiled successfully on riscv, arm64, x86_64 and powerpc arch. [reddybalavignesh9979@gmail.com: add selftests include path for kselftest.h] Link: https://lkml.kernel.org/r/20251017090201.317521-1-reddybalavignesh9979@gmail.com Link: https://lkml.kernel.org/r/20251016104409.68985-1-reddybalavignesh9979@gmail.com Signed-off-by: Bala-Vignesh-Reddy <reddybalavignesh9979@gmail.com> Suggested-by: Andrew Morton <akpm@linux-foundation.org> Link: https://lore.kernel.org/lkml/20250820143954.33d95635e504e94df01930d0@linux-foundation.org/ Reviewed-by: Wei Yang <richard.weiyang@gmail.com> Cc: David Hildenbrand <david@redhat.com> Cc: David S. Miller <davem@davemloft.net> Cc: Eric Dumazet <edumazet@google.com> Cc: Günther Noack <gnoack@google.com> Cc: Jakub Kacinski <kuba@kernel.org> Cc: Liam Howlett <liam.howlett@oracle.com> Cc: Lorenzo Stoakes <lorenzo.stoakes@oracle.com> Cc: Michal Hocko <mhocko@suse.com> Cc: Mickael Salaun <mic@digikod.net> Cc: Ming Lei <ming.lei@redhat.com> Cc: Paolo Abeni <pabeni@redhat.com> Cc: Shuah Khan <shuah@kernel.org> Cc: Simon Horman <horms@kernel.org> Cc: Suren Baghdasaryan <surenb@google.com> Cc: Vlastimil Babka <vbabka@suse.cz> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
117 lines
3.1 KiB
C
117 lines
3.1 KiB
C
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
// Copyright (c) 2024 Christian Brauner <brauner@kernel.org>
|
|
|
|
#define _GNU_SOURCE
|
|
#include <fcntl.h>
|
|
#include <limits.h>
|
|
#include <sched.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <linux/fs.h>
|
|
#include <sys/ioctl.h>
|
|
#include <sys/stat.h>
|
|
#include <sys/mount.h>
|
|
#include <unistd.h>
|
|
|
|
#include "pidfd.h"
|
|
#include "kselftest_harness.h"
|
|
#include "../filesystems/wrappers.h"
|
|
|
|
FIXTURE(pidfd_bind_mount) {
|
|
char template[PATH_MAX];
|
|
int fd_tmp;
|
|
int pidfd;
|
|
struct stat st1;
|
|
struct stat st2;
|
|
__u32 gen1;
|
|
__u32 gen2;
|
|
bool must_unmount;
|
|
};
|
|
|
|
FIXTURE_SETUP(pidfd_bind_mount)
|
|
{
|
|
self->fd_tmp = -EBADF;
|
|
self->must_unmount = false;
|
|
ASSERT_EQ(unshare(CLONE_NEWNS), 0);
|
|
ASSERT_LE(snprintf(self->template, PATH_MAX, "%s", P_tmpdir "/pidfd_bind_mount_XXXXXX"), PATH_MAX);
|
|
self->fd_tmp = mkstemp(self->template);
|
|
ASSERT_GE(self->fd_tmp, 0);
|
|
self->pidfd = sys_pidfd_open(getpid(), 0);
|
|
ASSERT_GE(self->pidfd, 0);
|
|
ASSERT_GE(fstat(self->pidfd, &self->st1), 0);
|
|
ASSERT_EQ(ioctl(self->pidfd, FS_IOC_GETVERSION, &self->gen1), 0);
|
|
}
|
|
|
|
FIXTURE_TEARDOWN(pidfd_bind_mount)
|
|
{
|
|
ASSERT_EQ(close(self->fd_tmp), 0);
|
|
if (self->must_unmount)
|
|
ASSERT_EQ(umount2(self->template, 0), 0);
|
|
ASSERT_EQ(unlink(self->template), 0);
|
|
}
|
|
|
|
/*
|
|
* Test that a detached mount can be created for a pidfd and then
|
|
* attached to the filesystem hierarchy.
|
|
*/
|
|
TEST_F(pidfd_bind_mount, bind_mount)
|
|
{
|
|
int fd_tree;
|
|
|
|
fd_tree = sys_open_tree(self->pidfd, "", OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC | AT_EMPTY_PATH);
|
|
ASSERT_GE(fd_tree, 0);
|
|
|
|
ASSERT_EQ(move_mount(fd_tree, "", self->fd_tmp, "", MOVE_MOUNT_F_EMPTY_PATH | MOVE_MOUNT_T_EMPTY_PATH), 0);
|
|
self->must_unmount = true;
|
|
|
|
ASSERT_EQ(close(fd_tree), 0);
|
|
}
|
|
|
|
/* Test that a pidfd can be reopened through procfs. */
|
|
TEST_F(pidfd_bind_mount, reopen)
|
|
{
|
|
int pidfd;
|
|
char proc_path[PATH_MAX];
|
|
|
|
sprintf(proc_path, "/proc/self/fd/%d", self->pidfd);
|
|
pidfd = open(proc_path, O_RDONLY | O_NOCTTY | O_CLOEXEC);
|
|
ASSERT_GE(pidfd, 0);
|
|
|
|
ASSERT_GE(fstat(self->pidfd, &self->st2), 0);
|
|
ASSERT_EQ(ioctl(self->pidfd, FS_IOC_GETVERSION, &self->gen2), 0);
|
|
|
|
ASSERT_TRUE(self->st1.st_dev == self->st2.st_dev && self->st1.st_ino == self->st2.st_ino);
|
|
ASSERT_TRUE(self->gen1 == self->gen2);
|
|
|
|
ASSERT_EQ(close(pidfd), 0);
|
|
}
|
|
|
|
/*
|
|
* Test that a detached mount can be created for a pidfd and then
|
|
* attached to the filesystem hierarchy and reopened.
|
|
*/
|
|
TEST_F(pidfd_bind_mount, bind_mount_reopen)
|
|
{
|
|
int fd_tree, fd_pidfd_mnt;
|
|
|
|
fd_tree = sys_open_tree(self->pidfd, "", OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC | AT_EMPTY_PATH);
|
|
ASSERT_GE(fd_tree, 0);
|
|
|
|
ASSERT_EQ(move_mount(fd_tree, "", self->fd_tmp, "", MOVE_MOUNT_F_EMPTY_PATH | MOVE_MOUNT_T_EMPTY_PATH), 0);
|
|
self->must_unmount = true;
|
|
|
|
fd_pidfd_mnt = openat(-EBADF, self->template, O_RDONLY | O_NOCTTY | O_CLOEXEC);
|
|
ASSERT_GE(fd_pidfd_mnt, 0);
|
|
|
|
ASSERT_GE(fstat(fd_tree, &self->st2), 0);
|
|
ASSERT_EQ(ioctl(fd_pidfd_mnt, FS_IOC_GETVERSION, &self->gen2), 0);
|
|
|
|
ASSERT_TRUE(self->st1.st_dev == self->st2.st_dev && self->st1.st_ino == self->st2.st_ino);
|
|
ASSERT_TRUE(self->gen1 == self->gen2);
|
|
|
|
ASSERT_EQ(close(fd_tree), 0);
|
|
ASSERT_EQ(close(fd_pidfd_mnt), 0);
|
|
}
|
|
|
|
TEST_HARNESS_MAIN
|