Files
Bala-Vignesh-ReddyandAndrew Morton e6fbd1759c selftests: complete kselftest include centralization
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>
2025-11-27 14:24:31 -08:00

95 lines
1.9 KiB
C

// SPDX-License-Identifier: GPL-2.0
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <linux/kernel.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syscall.h>
#include <unistd.h>
#include <sys/resource.h>
#include <linux/close_range.h>
#include "kselftest_harness.h"
#include "../clone3/clone3_selftests.h"
TEST(unshare_EMFILE)
{
pid_t pid;
int status;
struct __clone_args args = {
.flags = CLONE_FILES,
.exit_signal = SIGCHLD,
};
int fd;
ssize_t n, n2;
static char buf[512], buf2[512];
struct rlimit rlimit;
int nr_open;
fd = open("/proc/sys/fs/nr_open", O_RDWR);
ASSERT_GE(fd, 0);
n = read(fd, buf, sizeof(buf));
ASSERT_GT(n, 0);
ASSERT_EQ(buf[n - 1], '\n');
ASSERT_EQ(sscanf(buf, "%d", &nr_open), 1);
ASSERT_EQ(0, getrlimit(RLIMIT_NOFILE, &rlimit));
/* bump fs.nr_open */
n2 = sprintf(buf2, "%d\n", nr_open + 1024);
lseek(fd, 0, SEEK_SET);
write(fd, buf2, n2);
/* bump ulimit -n */
rlimit.rlim_cur = nr_open + 1024;
rlimit.rlim_max = nr_open + 1024;
EXPECT_EQ(0, setrlimit(RLIMIT_NOFILE, &rlimit)) {
lseek(fd, 0, SEEK_SET);
write(fd, buf, n);
exit(EXIT_FAILURE);
}
/* get a descriptor past the old fs.nr_open */
EXPECT_GE(dup2(2, nr_open + 64), 0) {
lseek(fd, 0, SEEK_SET);
write(fd, buf, n);
exit(EXIT_FAILURE);
}
/* get descriptor table shared */
pid = sys_clone3(&args, sizeof(args));
EXPECT_GE(pid, 0) {
lseek(fd, 0, SEEK_SET);
write(fd, buf, n);
exit(EXIT_FAILURE);
}
if (pid == 0) {
int err;
/* restore fs.nr_open */
lseek(fd, 0, SEEK_SET);
write(fd, buf, n);
/* ... and now unshare(CLONE_FILES) must fail with EMFILE */
err = unshare(CLONE_FILES);
EXPECT_EQ(err, -1)
exit(EXIT_FAILURE);
EXPECT_EQ(errno, EMFILE)
exit(EXIT_FAILURE);
exit(EXIT_SUCCESS);
}
EXPECT_EQ(waitpid(pid, &status, 0), pid);
EXPECT_EQ(true, WIFEXITED(status));
EXPECT_EQ(0, WEXITSTATUS(status));
}
TEST_HARNESS_MAIN