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