1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2024 Wei Gao <wegao@suse.com>
4 */
5
6 /*\
7 * [Description]
8 *
9 * Test for kernel commit
10 * 5d1f903f75a8 ("attr: block mode changes of symlinks")
11 */
12
13 #include "lapi/fcntl.h"
14 #include "tst_test.h"
15
16 #define MODE 0644
17 #define TESTFILE "testfile"
18 #define TESTFILE_SYMLINK "testfile_symlink"
19
run(void)20 static void run(void)
21 {
22 struct stat stat_file, stat_sym;
23 int mode = 0;
24 char fd_path[100];
25
26 int fd = SAFE_OPEN(TESTFILE_SYMLINK, O_PATH | O_NOFOLLOW);
27
28 sprintf(fd_path, "/proc/self/fd/%d", fd);
29
30 TST_EXP_FAIL(chmod(fd_path, mode), ENOTSUP, "chmod(%s, %04o)",
31 TESTFILE_SYMLINK, mode);
32
33 SAFE_STAT(TESTFILE, &stat_file);
34 SAFE_LSTAT(TESTFILE_SYMLINK, &stat_sym);
35
36 stat_file.st_mode &= ~S_IFREG;
37 stat_sym.st_mode &= ~S_IFLNK;
38
39 TST_EXP_EXPR(stat_file.st_mode != (unsigned int)mode,
40 "stat(%s) mode=%04o", TESTFILE, stat_file.st_mode);
41
42 TST_EXP_EXPR(stat_sym.st_mode != (unsigned int)mode,
43 "stat(%s) mode=%04o", TESTFILE, stat_sym.st_mode);
44
45 SAFE_CLOSE(fd);
46 }
47
setup(void)48 static void setup(void)
49 {
50 SAFE_TOUCH(TESTFILE, MODE, NULL);
51 SAFE_SYMLINK(TESTFILE, TESTFILE_SYMLINK);
52 }
53
cleanup(void)54 static void cleanup(void)
55 {
56 remove(TESTFILE);
57 remove(TESTFILE_SYMLINK);
58 }
59
60 static struct tst_test test = {
61 .setup = setup,
62 .cleanup = cleanup,
63 .test_all = run,
64 .min_kver = "6.6",
65 .mntpoint = "mntpoint",
66 .all_filesystems = 1,
67 .tags = (const struct tst_tag[]) {
68 {"linux-git", "5d1f903f75a8"},
69 {}
70 },
71 };
72