1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2014 Fujitsu Ltd.
4 * Copyright (c) Linux Test Project, 2014-2023
5 * Author: Zeng Linggang <[email protected]>
6 */
7
8 /*\
9 * [Description]
10 *
11 * Verify that:
12 *
13 * - link() fails with EPERM if the old path is a directory.
14 * - link() fails with EXDEV if the old path and the new path
15 * are not on the same mounted file system(Linux permits
16 * a file system to be mounted at multiple points, but link()
17 * does not work across different mount points, even if the same
18 * file system is mounted on both).
19 * - link() fails with EROFS if the file is on a read-only file system.
20 * - link() fails with ELOOP if too many symbolic links were encountered
21 * in resolving path.
22 */
23
24 #include <errno.h>
25 #include "tst_test.h"
26
27 #define DIR_MODE (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP| \
28 S_IXGRP|S_IROTH|S_IXOTH)
29 #define MNT_POINT "mntpoint"
30 #define TEST_FILE "testfile"
31 #define TEST_FILE1 "testfile1"
32 #define TEST_FILE2 "mntpoint/file"
33 #define TEST_FILE3 "mntpoint/testfile4"
34
35 static char test_file4[PATH_MAX] = ".";
36 static void setup(void);
37
38 static struct tcase {
39 char *oldpath;
40 char *newpath;
41 int exp_errno;
42 } tcases[] = {
43 {TEST_FILE1, TEST_FILE, EPERM},
44 {TEST_FILE2, TEST_FILE, EXDEV},
45 {TEST_FILE2, TEST_FILE3, EROFS},
46 {test_file4, TEST_FILE, ELOOP},
47 };
48
link_verify(unsigned int i)49 static void link_verify(unsigned int i)
50 {
51 struct tcase *tc = &tcases[i];
52
53 TEST(link(tc->oldpath, tc->newpath));
54
55 if (TST_RET != -1) {
56 tst_res(TFAIL, "link() succeeded unexpectedly (%li)",
57 TST_RET);
58 return;
59 }
60
61 if (TST_ERR == tc->exp_errno) {
62 tst_res(TPASS | TTERRNO, "link() failed as expected");
63 return;
64 }
65
66 tst_res(TFAIL | TTERRNO,
67 "link() failed unexpectedly; expected: %d - %s",
68 tc->exp_errno, tst_strerrno(tc->exp_errno));
69 }
70
setup(void)71 static void setup(void)
72 {
73 int i;
74
75 SAFE_MKDIR(TEST_FILE1, DIR_MODE);
76
77 SAFE_MKDIR("test_eloop", DIR_MODE);
78 SAFE_SYMLINK("../test_eloop", "test_eloop/test_eloop");
79 for (i = 0; i < 43; i++)
80 strcat(test_file4, "/test_eloop");
81 }
82
83 static struct tst_test test = {
84 .setup = setup,
85 .test = link_verify,
86 .tcnt = ARRAY_SIZE(tcases),
87 .needs_root = 1,
88 .needs_rofs = 1,
89 .mntpoint = MNT_POINT,
90 };
91