blob: 477702a3bcee48e17ee44143ccc1a45c551f5338 [file] [log] [blame]
Zeng Linggang63147202014-02-18 22:00:18 +08001/*
2 * Copyright (c) 2014 Fujitsu Ltd.
3 * Author: Zeng Linggang <zenglg.jy@cn.fujitsu.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 *
19 */
20/*
21 * Test Description:
22 * Verify that,
23 * 1. link() fails with -1 return value and sets errno to EPERM
24 * if oldpath is a directory.
25 * 2. link() fails with -1 return value and sets errno to EXDEV
26 * if oldpath and newpath are not on the same mounted file system( Linux
27 * permits a file system to be mounted at multiple points, but link()
28 * does not work across different mount points, even if the same
29 * file system is mounted on both. ).
30 * 3. link() fails with -1 return value and sets errno to EROFS
31 * if the file is on a read-only file system.
32 * 4. link() fails with -1 return value and sets errno to ELOOP
33 * if too many symbolic links were encountered in resolving path.
34 */
35
36#include <stdio.h>
37#include <stdlib.h>
38#include <unistd.h>
39#include <fcntl.h>
40#include <errno.h>
41#include <string.h>
42#include <signal.h>
43#include <sys/types.h>
44#include <sys/stat.h>
45#include <pwd.h>
46#include <sys/mount.h>
47
48#include "test.h"
Zeng Linggang63147202014-02-18 22:00:18 +080049#include "safe_macros.h"
50
51#define DIR_MODE (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP| \
52 S_IXGRP|S_IROTH|S_IXOTH)
53#define MNT_POINT "mntpoint"
54#define TEST_FILE "testfile"
55#define TEST_FILE1 "testfile1"
56#define TEST_FILE2 "mntpoint/testfile3"
57#define TEST_FILE3 "mntpoint/testfile4"
58
59static char test_file4[PATH_MAX] = ".";
60static void setup(void);
61static void cleanup(void);
Zeng Linggang63147202014-02-18 22:00:18 +080062
Cyril Hrubis6383fd92014-06-19 17:06:46 +020063static const char *device;
Zeng Linggang63147202014-02-18 22:00:18 +080064static int mount_flag;
65
Zeng Linggang63147202014-02-18 22:00:18 +080066static struct test_case_t {
67 char *oldpath;
68 char *newpath;
69 int exp_errno;
70} test_cases[] = {
71 {TEST_FILE1, TEST_FILE, EPERM},
72 {TEST_FILE2, TEST_FILE, EXDEV},
73 {TEST_FILE2, TEST_FILE3, EROFS},
74 {test_file4, TEST_FILE, ELOOP},
75};
76
77static void link_verify(const struct test_case_t *);
78
79char *TCID = "link08";
80int TST_TOTAL = ARRAY_SIZE(test_cases);
Zeng Linggang63147202014-02-18 22:00:18 +080081
82int main(int ac, char **av)
83{
84 int i, lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020085 const char *msg;
Zeng Linggang63147202014-02-18 22:00:18 +080086
Cyril Hrubis6383fd92014-06-19 17:06:46 +020087 msg = parse_opts(ac, av, NULL, NULL);
Zeng Linggang63147202014-02-18 22:00:18 +080088 if (msg != NULL)
89 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
90
Zeng Linggang63147202014-02-18 22:00:18 +080091 setup();
92
93 for (lc = 0; TEST_LOOPING(lc); lc++) {
94 tst_count = 0;
95 for (i = 0; i < TST_TOTAL; i++)
96 link_verify(&test_cases[i]);
97 }
98
99 cleanup();
100 tst_exit();
101
102}
103
104static void link_verify(const struct test_case_t *tc)
105{
106 TEST(link(tc->oldpath, tc->newpath));
107
108 if (TEST_RETURN != -1) {
109 tst_resm(TFAIL, "link succeeded unexpectedly");
110 return;
111 }
112
113 if (TEST_ERRNO == tc->exp_errno) {
114 tst_resm(TPASS | TTERRNO, "link failed as expected");
115 } else {
116 tst_resm(TFAIL | TTERRNO,
117 "link failed unexpectedly; expected: %d - %s",
118 tc->exp_errno, strerror(tc->exp_errno));
119 }
120}
121
122
123static void setup(void)
124{
125 int i;
Cyril Hrubis6383fd92014-06-19 17:06:46 +0200126 const char *fs_type;
Zeng Linggang63147202014-02-18 22:00:18 +0800127
128 tst_require_root(NULL);
129
130 tst_sig(NOFORK, DEF_HANDLER, cleanup);
131
Zeng Linggang63147202014-02-18 22:00:18 +0800132 TEST_PAUSE;
133
134 tst_tmpdir();
135
Cyril Hrubis6383fd92014-06-19 17:06:46 +0200136 fs_type = tst_dev_fs_type();
137 device = tst_acquire_device(cleanup);
138
139 if (!device)
140 tst_brkm(TCONF, cleanup, "Failed to acquire device");
141
Zeng Linggang63147202014-02-18 22:00:18 +0800142 SAFE_MKDIR(cleanup, TEST_FILE1, DIR_MODE);
143
144 SAFE_MKDIR(cleanup, "test_eloop", DIR_MODE);
145 SAFE_SYMLINK(cleanup, "../test_eloop", "test_eloop/test_eloop");
146 for (i = 0; i < 43; i++)
147 strcat(test_file4, "/test_eloop");
148
Cyril Hrubis6383fd92014-06-19 17:06:46 +0200149 tst_mkfs(cleanup, device, fs_type, NULL);
Zeng Linggang63147202014-02-18 22:00:18 +0800150 SAFE_MKDIR(cleanup, MNT_POINT, DIR_MODE);
Cyril Hrubis6383fd92014-06-19 17:06:46 +0200151 if (mount(device, MNT_POINT, fs_type, 0, NULL) < 0) {
Zeng Linggang63147202014-02-18 22:00:18 +0800152 tst_brkm(TBROK | TERRNO, cleanup,
153 "mount device:%s failed", device);
154 }
155 mount_flag = 1;
156
157 SAFE_TOUCH(cleanup, TEST_FILE2, 0644, NULL);
Cyril Hrubis6383fd92014-06-19 17:06:46 +0200158 if (mount(device, MNT_POINT, fs_type,
Zeng Linggang63147202014-02-18 22:00:18 +0800159 MS_REMOUNT | MS_RDONLY, NULL) < 0) {
160 tst_brkm(TBROK | TERRNO, cleanup,
161 "mount device:%s failed", device);
162 }
163}
164
165static void cleanup(void)
166{
Cyril Hrubisc60a2302015-01-29 16:28:24 +0100167 if (mount_flag && tst_umount(MNT_POINT) < 0)
Zeng Linggang63147202014-02-18 22:00:18 +0800168 tst_resm(TWARN | TERRNO, "umount device:%s failed", device);
169
Cyril Hrubis6383fd92014-06-19 17:06:46 +0200170 if (device)
171 tst_release_device(NULL, device);
Zeng Linggang63147202014-02-18 22:00:18 +0800172
Cyril Hrubis6383fd92014-06-19 17:06:46 +0200173 tst_rmdir();
Zeng Linggang63147202014-02-18 22:00:18 +0800174}