Path: blob/master/tools/testing/selftests/fchmodat2/fchmodat2_test.c
26285 views
// SPDX-License-Identifier: GPL-2.0-or-later12#define _GNU_SOURCE3#include <fcntl.h>4#include <sys/stat.h>5#include <sys/types.h>6#include <syscall.h>7#include <unistd.h>89#include "../kselftest.h"1011int sys_fchmodat2(int dfd, const char *filename, mode_t mode, int flags)12{13int ret = syscall(__NR_fchmodat2, dfd, filename, mode, flags);1415return ret >= 0 ? ret : -errno;16}1718int setup_testdir(void)19{20int dfd, ret;21char dirname[] = "/tmp/ksft-fchmodat2.XXXXXX";2223/* Make the top-level directory. */24if (!mkdtemp(dirname))25ksft_exit_fail_msg("%s: failed to create tmpdir\n", __func__);2627dfd = open(dirname, O_PATH | O_DIRECTORY);28if (dfd < 0)29ksft_exit_fail_msg("%s: failed to open tmpdir\n", __func__);3031ret = openat(dfd, "regfile", O_CREAT | O_WRONLY | O_TRUNC, 0644);32if (ret < 0)33ksft_exit_fail_msg("%s: failed to create file in tmpdir\n",34__func__);35close(ret);3637ret = symlinkat("regfile", dfd, "symlink");38if (ret < 0)39ksft_exit_fail_msg("%s: failed to create symlink in tmpdir\n",40__func__);4142return dfd;43}4445int expect_mode(int dfd, const char *filename, mode_t expect_mode)46{47struct stat st;48int ret = fstatat(dfd, filename, &st, AT_SYMLINK_NOFOLLOW);4950if (ret)51ksft_exit_fail_msg("%s: %s: fstatat failed\n",52__func__, filename);5354return (st.st_mode == expect_mode);55}5657void test_regfile(void)58{59int dfd, ret;6061dfd = setup_testdir();6263ret = sys_fchmodat2(dfd, "regfile", 0640, 0);6465if (ret < 0)66ksft_exit_fail_msg("%s: fchmodat2(noflag) failed\n", __func__);6768if (!expect_mode(dfd, "regfile", 0100640))69ksft_exit_fail_msg("%s: wrong file mode bits after fchmodat2\n",70__func__);7172ret = sys_fchmodat2(dfd, "regfile", 0600, AT_SYMLINK_NOFOLLOW);7374if (ret < 0)75ksft_exit_fail_msg("%s: fchmodat2(AT_SYMLINK_NOFOLLOW) failed\n",76__func__);7778if (!expect_mode(dfd, "regfile", 0100600))79ksft_exit_fail_msg("%s: wrong file mode bits after fchmodat2 with nofollow\n",80__func__);8182ksft_test_result_pass("fchmodat2(regfile)\n");83}8485void test_symlink(void)86{87int dfd, ret;8889dfd = setup_testdir();9091ret = sys_fchmodat2(dfd, "symlink", 0640, 0);9293if (ret < 0)94ksft_exit_fail_msg("%s: fchmodat2(noflag) failed\n", __func__);9596if (!expect_mode(dfd, "regfile", 0100640))97ksft_exit_fail_msg("%s: wrong file mode bits after fchmodat2\n",98__func__);99100if (!expect_mode(dfd, "symlink", 0120777))101ksft_exit_fail_msg("%s: wrong symlink mode bits after fchmodat2\n",102__func__);103104ret = sys_fchmodat2(dfd, "symlink", 0600, AT_SYMLINK_NOFOLLOW);105106/*107* On certain filesystems (xfs or btrfs), chmod operation fails. So we108* first check the symlink target but if the operation fails we mark the109* test as skipped.110*111* https://sourceware.org/legacy-ml/libc-alpha/2020-02/msg00467.html112*/113if (ret == 0 && !expect_mode(dfd, "symlink", 0120600))114ksft_exit_fail_msg("%s: wrong symlink mode bits after fchmodat2 with nofollow\n",115__func__);116117if (!expect_mode(dfd, "regfile", 0100640))118ksft_exit_fail_msg("%s: wrong file mode bits after fchmodat2 with nofollow\n",119__func__);120121if (ret != 0)122ksft_test_result_skip("fchmodat2(symlink)\n");123else124ksft_test_result_pass("fchmodat2(symlink)\n");125}126127#define NUM_TESTS 2128129int main(int argc, char **argv)130{131ksft_print_header();132ksft_set_plan(NUM_TESTS);133134test_regfile();135test_symlink();136137if (ksft_get_fail_cnt() + ksft_get_error_cnt() > 0)138ksft_exit_fail();139else140ksft_exit_pass();141}142143144