Files
linux/tools/testing/selftests/fchmodat2/fchmodat2_test.c
Bala-Vignesh-Reddy 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

143 lines
3.3 KiB
C

// SPDX-License-Identifier: GPL-2.0-or-later
#define _GNU_SOURCE
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <syscall.h>
#include <unistd.h>
#include "kselftest.h"
int sys_fchmodat2(int dfd, const char *filename, mode_t mode, int flags)
{
int ret = syscall(__NR_fchmodat2, dfd, filename, mode, flags);
return ret >= 0 ? ret : -errno;
}
int setup_testdir(void)
{
int dfd, ret;
char dirname[] = "/tmp/ksft-fchmodat2.XXXXXX";
/* Make the top-level directory. */
if (!mkdtemp(dirname))
ksft_exit_fail_msg("%s: failed to create tmpdir\n", __func__);
dfd = open(dirname, O_PATH | O_DIRECTORY);
if (dfd < 0)
ksft_exit_fail_msg("%s: failed to open tmpdir\n", __func__);
ret = openat(dfd, "regfile", O_CREAT | O_WRONLY | O_TRUNC, 0644);
if (ret < 0)
ksft_exit_fail_msg("%s: failed to create file in tmpdir\n",
__func__);
close(ret);
ret = symlinkat("regfile", dfd, "symlink");
if (ret < 0)
ksft_exit_fail_msg("%s: failed to create symlink in tmpdir\n",
__func__);
return dfd;
}
int expect_mode(int dfd, const char *filename, mode_t expect_mode)
{
struct stat st;
int ret = fstatat(dfd, filename, &st, AT_SYMLINK_NOFOLLOW);
if (ret)
ksft_exit_fail_msg("%s: %s: fstatat failed\n",
__func__, filename);
return (st.st_mode == expect_mode);
}
void test_regfile(void)
{
int dfd, ret;
dfd = setup_testdir();
ret = sys_fchmodat2(dfd, "regfile", 0640, 0);
if (ret < 0)
ksft_exit_fail_msg("%s: fchmodat2(noflag) failed\n", __func__);
if (!expect_mode(dfd, "regfile", 0100640))
ksft_exit_fail_msg("%s: wrong file mode bits after fchmodat2\n",
__func__);
ret = sys_fchmodat2(dfd, "regfile", 0600, AT_SYMLINK_NOFOLLOW);
if (ret < 0)
ksft_exit_fail_msg("%s: fchmodat2(AT_SYMLINK_NOFOLLOW) failed\n",
__func__);
if (!expect_mode(dfd, "regfile", 0100600))
ksft_exit_fail_msg("%s: wrong file mode bits after fchmodat2 with nofollow\n",
__func__);
ksft_test_result_pass("fchmodat2(regfile)\n");
}
void test_symlink(void)
{
int dfd, ret;
dfd = setup_testdir();
ret = sys_fchmodat2(dfd, "symlink", 0640, 0);
if (ret < 0)
ksft_exit_fail_msg("%s: fchmodat2(noflag) failed\n", __func__);
if (!expect_mode(dfd, "regfile", 0100640))
ksft_exit_fail_msg("%s: wrong file mode bits after fchmodat2\n",
__func__);
if (!expect_mode(dfd, "symlink", 0120777))
ksft_exit_fail_msg("%s: wrong symlink mode bits after fchmodat2\n",
__func__);
ret = sys_fchmodat2(dfd, "symlink", 0600, AT_SYMLINK_NOFOLLOW);
/*
* On certain filesystems (xfs or btrfs), chmod operation fails. So we
* first check the symlink target but if the operation fails we mark the
* test as skipped.
*
* https://sourceware.org/legacy-ml/libc-alpha/2020-02/msg00467.html
*/
if (ret == 0 && !expect_mode(dfd, "symlink", 0120600))
ksft_exit_fail_msg("%s: wrong symlink mode bits after fchmodat2 with nofollow\n",
__func__);
if (!expect_mode(dfd, "regfile", 0100640))
ksft_exit_fail_msg("%s: wrong file mode bits after fchmodat2 with nofollow\n",
__func__);
if (ret != 0)
ksft_test_result_skip("fchmodat2(symlink)\n");
else
ksft_test_result_pass("fchmodat2(symlink)\n");
}
#define NUM_TESTS 2
int main(int argc, char **argv)
{
ksft_print_header();
ksft_set_plan(NUM_TESTS);
test_regfile();
test_symlink();
if (ksft_get_fail_cnt() + ksft_get_error_cnt() > 0)
ksft_exit_fail();
else
ksft_exit_pass();
}