1f08c3bdfSopenharmony_ci/*
2f08c3bdfSopenharmony_ci * Copyright (c) 2014 Fujitsu Ltd.
3f08c3bdfSopenharmony_ci * Author: Xiaoguang Wang <wangxg.fnst@cn.fujitsu.com>
4f08c3bdfSopenharmony_ci *
5f08c3bdfSopenharmony_ci * This program is free software; you can redistribute it and/or modify it
6f08c3bdfSopenharmony_ci * under the terms of version 2 of the GNU General Public License as
7f08c3bdfSopenharmony_ci * published by the Free Software Foundation.
8f08c3bdfSopenharmony_ci *
9f08c3bdfSopenharmony_ci * This program is distributed in the hope that it would be useful, but
10f08c3bdfSopenharmony_ci * WITHOUT ANY WARRANTY; without even the implied warranty of
11f08c3bdfSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12f08c3bdfSopenharmony_ci *
13f08c3bdfSopenharmony_ci * You should have received a copy of the GNU General Public License along
14f08c3bdfSopenharmony_ci * with this program; if not, write the Free Software Foundation, Inc.,
15f08c3bdfSopenharmony_ci * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16f08c3bdfSopenharmony_ci */
17f08c3bdfSopenharmony_ci
18f08c3bdfSopenharmony_ci/*
19f08c3bdfSopenharmony_ci * Description:
20f08c3bdfSopenharmony_ci * Verify that,
21f08c3bdfSopenharmony_ci *  1) mknod(2) returns -1 and sets errno to EROFS if pathname refers to
22f08c3bdfSopenharmony_ci *     a file on a read-only file system.
23f08c3bdfSopenharmony_ci *  2) mknod(2) returns -1 and sets errno to ELOOP if Too many symbolic
24f08c3bdfSopenharmony_ci *     links were encountered in resolving pathname.
25f08c3bdfSopenharmony_ci */
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_ci#define _GNU_SOURCE
28f08c3bdfSopenharmony_ci
29f08c3bdfSopenharmony_ci#include <sys/types.h>
30f08c3bdfSopenharmony_ci#include <sys/stat.h>
31f08c3bdfSopenharmony_ci#include <stdlib.h>
32f08c3bdfSopenharmony_ci#include <errno.h>
33f08c3bdfSopenharmony_ci#include <string.h>
34f08c3bdfSopenharmony_ci#include <signal.h>
35f08c3bdfSopenharmony_ci#include <sys/mount.h>
36f08c3bdfSopenharmony_ci
37f08c3bdfSopenharmony_ci#include "test.h"
38f08c3bdfSopenharmony_ci#include "safe_macros.h"
39f08c3bdfSopenharmony_ci#include "lapi/fcntl.h"
40f08c3bdfSopenharmony_ci#include "mknodat.h"
41f08c3bdfSopenharmony_ci
42f08c3bdfSopenharmony_cistatic void setup(void);
43f08c3bdfSopenharmony_cistatic void cleanup(void);
44f08c3bdfSopenharmony_ci
45f08c3bdfSopenharmony_ci#define DIR_MODE	(S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP| \
46f08c3bdfSopenharmony_ci			 S_IXGRP|S_IROTH|S_IXOTH)
47f08c3bdfSopenharmony_ci#define MNT_POINT	"mntpoint"
48f08c3bdfSopenharmony_ci
49f08c3bdfSopenharmony_ci#define FIFOMODE	(S_IFIFO | S_IRUSR | S_IRGRP | S_IROTH)
50f08c3bdfSopenharmony_ci#define FREGMODE	(S_IFREG | S_IRUSR | S_IRGRP | S_IROTH)
51f08c3bdfSopenharmony_ci#define SOCKMODE	(S_IFSOCK | S_IRUSR | S_IRGRP | S_IROTH)
52f08c3bdfSopenharmony_ci
53f08c3bdfSopenharmony_cistatic const char *device;
54f08c3bdfSopenharmony_cistatic int mount_flag;
55f08c3bdfSopenharmony_ci
56f08c3bdfSopenharmony_cistatic int dir_fd;
57f08c3bdfSopenharmony_cistatic int curfd = AT_FDCWD;
58f08c3bdfSopenharmony_ci
59f08c3bdfSopenharmony_ci#define ELOPFILE	"/test_eloop"
60f08c3bdfSopenharmony_cistatic char elooppathname[sizeof(ELOPFILE) * 43] = ".";
61f08c3bdfSopenharmony_ci
62f08c3bdfSopenharmony_cistatic struct test_case_t {
63f08c3bdfSopenharmony_ci	int *dir_fd;
64f08c3bdfSopenharmony_ci	char *pathname;
65f08c3bdfSopenharmony_ci	mode_t mode;
66f08c3bdfSopenharmony_ci	int exp_errno;
67f08c3bdfSopenharmony_ci} test_cases[] = {
68f08c3bdfSopenharmony_ci	{ &curfd, "tnode1", FIFOMODE, 0 },
69f08c3bdfSopenharmony_ci	{ &curfd, "tnode2", FREGMODE, 0 },
70f08c3bdfSopenharmony_ci	{ &curfd, "tnode3", SOCKMODE, 0 },
71f08c3bdfSopenharmony_ci	{ &dir_fd, "tnode4", FIFOMODE, EROFS },
72f08c3bdfSopenharmony_ci	{ &dir_fd, "tnode5", FREGMODE, EROFS },
73f08c3bdfSopenharmony_ci	{ &dir_fd, "tnode6", SOCKMODE, EROFS },
74f08c3bdfSopenharmony_ci	{ &curfd, elooppathname, FIFOMODE, ELOOP },
75f08c3bdfSopenharmony_ci	{ &curfd, elooppathname, FREGMODE, ELOOP },
76f08c3bdfSopenharmony_ci	{ &curfd, elooppathname, SOCKMODE, ELOOP },
77f08c3bdfSopenharmony_ci};
78f08c3bdfSopenharmony_ci
79f08c3bdfSopenharmony_cistatic void mknodat_verify(struct test_case_t *tc);
80f08c3bdfSopenharmony_ci
81f08c3bdfSopenharmony_cichar *TCID = "mknodat";
82f08c3bdfSopenharmony_ciint TST_TOTAL = ARRAY_SIZE(test_cases);
83f08c3bdfSopenharmony_ci
84f08c3bdfSopenharmony_ciint main(int ac, char **av)
85f08c3bdfSopenharmony_ci{
86f08c3bdfSopenharmony_ci	int lc, i;
87f08c3bdfSopenharmony_ci
88f08c3bdfSopenharmony_ci	tst_parse_opts(ac, av, NULL, NULL);
89f08c3bdfSopenharmony_ci
90f08c3bdfSopenharmony_ci	setup();
91f08c3bdfSopenharmony_ci
92f08c3bdfSopenharmony_ci	for (lc = 0; TEST_LOOPING(lc); lc++) {
93f08c3bdfSopenharmony_ci		tst_count = 0;
94f08c3bdfSopenharmony_ci
95f08c3bdfSopenharmony_ci		for (i = 0; i < TST_TOTAL; i++)
96f08c3bdfSopenharmony_ci			mknodat_verify(&test_cases[i]);
97f08c3bdfSopenharmony_ci	}
98f08c3bdfSopenharmony_ci
99f08c3bdfSopenharmony_ci	cleanup();
100f08c3bdfSopenharmony_ci	tst_exit();
101f08c3bdfSopenharmony_ci}
102f08c3bdfSopenharmony_ci
103f08c3bdfSopenharmony_cistatic void setup(void)
104f08c3bdfSopenharmony_ci{
105f08c3bdfSopenharmony_ci	int i;
106f08c3bdfSopenharmony_ci	const char *fs_type;
107f08c3bdfSopenharmony_ci
108f08c3bdfSopenharmony_ci	tst_require_root();
109f08c3bdfSopenharmony_ci
110f08c3bdfSopenharmony_ci	tst_sig(NOFORK, DEF_HANDLER, cleanup);
111f08c3bdfSopenharmony_ci
112f08c3bdfSopenharmony_ci	tst_tmpdir();
113f08c3bdfSopenharmony_ci
114f08c3bdfSopenharmony_ci	fs_type = tst_dev_fs_type();
115f08c3bdfSopenharmony_ci	device = tst_acquire_device(cleanup);
116f08c3bdfSopenharmony_ci
117f08c3bdfSopenharmony_ci	if (!device)
118f08c3bdfSopenharmony_ci		tst_brkm(TCONF, cleanup, "Failed to acquire device");
119f08c3bdfSopenharmony_ci
120f08c3bdfSopenharmony_ci	tst_mkfs(cleanup, device, fs_type, NULL, NULL);
121f08c3bdfSopenharmony_ci
122f08c3bdfSopenharmony_ci	TEST_PAUSE;
123f08c3bdfSopenharmony_ci
124f08c3bdfSopenharmony_ci	/*
125f08c3bdfSopenharmony_ci	 * mount a read-only file system for EROFS test
126f08c3bdfSopenharmony_ci	 */
127f08c3bdfSopenharmony_ci	SAFE_MKDIR(cleanup, MNT_POINT, DIR_MODE);
128f08c3bdfSopenharmony_ci	SAFE_MOUNT(cleanup, device, MNT_POINT, fs_type, MS_RDONLY, NULL);
129f08c3bdfSopenharmony_ci	mount_flag = 1;
130f08c3bdfSopenharmony_ci	dir_fd = SAFE_OPEN(cleanup, MNT_POINT, O_DIRECTORY);
131f08c3bdfSopenharmony_ci
132f08c3bdfSopenharmony_ci	/*
133f08c3bdfSopenharmony_ci	 * NOTE: the ELOOP test is written based on that the consecutive
134f08c3bdfSopenharmony_ci	 * symlinks limits in kernel is hardwired to 40.
135f08c3bdfSopenharmony_ci	 */
136f08c3bdfSopenharmony_ci	SAFE_MKDIR(cleanup, "test_eloop", DIR_MODE);
137f08c3bdfSopenharmony_ci	SAFE_SYMLINK(cleanup, "../test_eloop", "test_eloop/test_eloop");
138f08c3bdfSopenharmony_ci	for (i = 0; i < 43; i++)
139f08c3bdfSopenharmony_ci		strcat(elooppathname, ELOPFILE);
140f08c3bdfSopenharmony_ci}
141f08c3bdfSopenharmony_ci
142f08c3bdfSopenharmony_cistatic void mknodat_verify(struct test_case_t *tc)
143f08c3bdfSopenharmony_ci{
144f08c3bdfSopenharmony_ci	int fd = *(tc->dir_fd);
145f08c3bdfSopenharmony_ci	char *pathname = tc->pathname;
146f08c3bdfSopenharmony_ci	mode_t mode = tc->mode;
147f08c3bdfSopenharmony_ci
148f08c3bdfSopenharmony_ci	TEST(mknodat(fd, pathname, mode, 0));
149f08c3bdfSopenharmony_ci
150f08c3bdfSopenharmony_ci	if (TEST_ERRNO == tc->exp_errno) {
151f08c3bdfSopenharmony_ci		tst_resm(TPASS | TTERRNO,
152f08c3bdfSopenharmony_ci			 "mknodat() returned the expected value");
153f08c3bdfSopenharmony_ci	} else {
154f08c3bdfSopenharmony_ci		tst_resm(TFAIL | TTERRNO,
155f08c3bdfSopenharmony_ci			 "mknodat() got unexpected return value; expected: "
156f08c3bdfSopenharmony_ci			 "%d - %s", tc->exp_errno,
157f08c3bdfSopenharmony_ci			 strerror(tc->exp_errno));
158f08c3bdfSopenharmony_ci	}
159f08c3bdfSopenharmony_ci
160f08c3bdfSopenharmony_ci	if (TEST_ERRNO == 0 &&
161f08c3bdfSopenharmony_ci	    tst_syscall(__NR_unlinkat, fd, pathname, 0) < 0) {
162f08c3bdfSopenharmony_ci		tst_brkm(TBROK | TERRNO, cleanup, "unlinkat(%d, %s) "
163f08c3bdfSopenharmony_ci			 "failed.", fd, pathname);
164f08c3bdfSopenharmony_ci	}
165f08c3bdfSopenharmony_ci}
166f08c3bdfSopenharmony_ci
167f08c3bdfSopenharmony_cistatic void cleanup(void)
168f08c3bdfSopenharmony_ci{
169f08c3bdfSopenharmony_ci	if (dir_fd > 0 && close(dir_fd) < 0)
170f08c3bdfSopenharmony_ci		tst_resm(TWARN | TERRNO, "close(%d) failed", dir_fd);
171f08c3bdfSopenharmony_ci	if (mount_flag && tst_umount(MNT_POINT) < 0)
172f08c3bdfSopenharmony_ci		tst_resm(TWARN | TERRNO, "umount device:%s failed", device);
173f08c3bdfSopenharmony_ci
174f08c3bdfSopenharmony_ci	if (device)
175f08c3bdfSopenharmony_ci		tst_release_device(device);
176f08c3bdfSopenharmony_ci
177f08c3bdfSopenharmony_ci	tst_rmdir();
178f08c3bdfSopenharmony_ci}
179