• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2015-2016 Oracle and/or its affiliates. All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of
7  * the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  *
17  * Author: Alexey Kodanev <alexey.kodanev@oracle.com>
18  *
19  */
20 
21 #define _GNU_SOURCE
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <unistd.h>
25 #include <errno.h>
26 
27 #include "test.h"
28 #include "safe_macros.h"
29 #include "lapi/fcntl.h"
30 
31 char *TCID = "open14";
32 int TST_TOTAL = 3;
33 static ssize_t size;
34 static char buf[1024];
35 static const ssize_t blocks_num = 4;
36 static struct stat st;
37 
cleanup(void)38 static void cleanup(void)
39 {
40 	tst_rmdir();
41 }
42 
setup(void)43 static void setup(void)
44 {
45 	tst_tmpdir();
46 
47 	size = sizeof(buf);
48 
49 	memset(buf, 1, size);
50 
51 	int fd = open(".", O_TMPFILE | O_RDWR, 0600);
52 
53 	if (fd == -1) {
54 		if (errno == EISDIR || errno == ENOTSUP)
55 			tst_brkm(TCONF, cleanup, "O_TMPFILE not supported");
56 
57 		tst_brkm(TBROK | TERRNO, cleanup, "open() failed");
58 	}
59 
60 	SAFE_CLOSE(cleanup, fd);
61 }
62 
write_file(int fd)63 static void write_file(int fd)
64 {
65 	int i;
66 
67 	for (i = 0; i < blocks_num; ++i)
68 		SAFE_WRITE(cleanup, 1, fd, buf, size);
69 }
70 
test01(void)71 void test01(void)
72 {
73 	int fd;
74 	char path[PATH_MAX], tmp[PATH_MAX];
75 
76 	tst_resm(TINFO, "creating a file with O_TMPFILE flag");
77 	fd = SAFE_OPEN(cleanup, ".", O_TMPFILE | O_RDWR, 0600);
78 
79 	tst_resm(TINFO, "writing data to the file");
80 	write_file(fd);
81 
82 	SAFE_FSTAT(cleanup, fd, &st);
83 	tst_resm(TINFO, "file size is '%li'", (long)st.st_size);
84 
85 	if (st.st_size != blocks_num * size) {
86 		tst_resm(TFAIL, "not expected size: '%li' != '%zu'",
87 			 (long)st.st_size, blocks_num * size);
88 		SAFE_CLOSE(cleanup, fd);
89 		return;
90 	}
91 
92 	tst_resm(TINFO, "looking for the file in '.'");
93 	if (!tst_dir_is_empty(cleanup, ".", 1))
94 		tst_brkm(TFAIL, cleanup, "found a file, this is not expected");
95 	tst_resm(TINFO, "file not found, OK");
96 
97 	snprintf(path, PATH_MAX,  "/proc/self/fd/%d", fd);
98 	SAFE_READLINK(cleanup, path, tmp, PATH_MAX);
99 
100 	tst_resm(TINFO, "renaming '%s' -> 'tmpfile'", tmp);
101 	SAFE_LINKAT(cleanup, AT_FDCWD, path, AT_FDCWD, "tmpfile",
102 		    AT_SYMLINK_FOLLOW);
103 
104 	if (tst_dir_is_empty(cleanup, ".", 1))
105 		tst_brkm(TFAIL, cleanup, "file not found");
106 
107 	SAFE_UNLINK(cleanup, "tmpfile");
108 	SAFE_CLOSE(cleanup, fd);
109 
110 	tst_resm(TPASS, "single file tests passed");
111 }
112 
read_file(int fd)113 static void read_file(int fd)
114 {
115 	int i;
116 	char tmp[size];
117 
118 	SAFE_LSEEK(cleanup, fd, 0, SEEK_SET);
119 
120 	for (i = 0; i < blocks_num; ++i) {
121 		SAFE_READ(cleanup, 0, fd, tmp, size);
122 		if (memcmp(buf, tmp, size))
123 			tst_brkm(TFAIL, cleanup, "got unexepected data");
124 	}
125 }
126 
test02(void)127 static void test02(void)
128 {
129 	const int files_num = 100;
130 	int i, fd[files_num];
131 	char path[PATH_MAX];
132 
133 	tst_resm(TINFO, "create files in multiple directories");
134 	for (i = 0; i < files_num; ++i) {
135 		snprintf(path, PATH_MAX, "tst02_%d", i);
136 		SAFE_MKDIR(cleanup, path, 0700);
137 		SAFE_CHDIR(cleanup, path);
138 
139 		fd[i] = SAFE_OPEN(cleanup, ".", O_TMPFILE | O_RDWR, 0600);
140 	}
141 
142 	tst_resm(TINFO, "removing test directories");
143 	for (i = files_num - 1; i >= 0; --i) {
144 		SAFE_CHDIR(cleanup, "..");
145 		snprintf(path, PATH_MAX, "tst02_%d", i);
146 		SAFE_RMDIR(cleanup, path);
147 	}
148 
149 	tst_resm(TINFO, "writing/reading temporary files");
150 	for (i = 0; i < files_num; ++i) {
151 		write_file(fd[i]);
152 		read_file(fd[i]);
153 	}
154 
155 	tst_resm(TINFO, "closing temporary files");
156 	for (i = 0; i < files_num; ++i)
157 		SAFE_CLOSE(cleanup, fd[i]);
158 
159 	tst_resm(TPASS, "multiple files tests passed");
160 }
161 
link_tmp_file(int fd)162 static void link_tmp_file(int fd)
163 {
164 	char path1[PATH_MAX], path2[PATH_MAX];
165 
166 	snprintf(path1, PATH_MAX,  "/proc/self/fd/%d", fd);
167 	snprintf(path2, PATH_MAX,  "tmpfile_%d", fd);
168 
169 	SAFE_LINKAT(cleanup, AT_FDCWD, path1, AT_FDCWD, path2,
170 		    AT_SYMLINK_FOLLOW);
171 }
172 
test03(void)173 static void test03(void)
174 {
175 	const int files_num = 100;
176 	const mode_t test_perms[] = { 0, 07777, 001, 0755, 0644, 0440 };
177 
178 	int i, fd[files_num];
179 	char path[PATH_MAX];
180 	struct stat st;
181 	mode_t mask = umask(0), perm;
182 
183 	umask(mask);
184 
185 	tst_resm(TINFO, "create multiple directories, link files into them");
186 	tst_resm(TINFO, "and check file permissions");
187 	for (i = 0; i < files_num; ++i) {
188 
189 		snprintf(path, PATH_MAX, "tst03_%d", i);
190 		SAFE_MKDIR(cleanup, path, 0700);
191 		SAFE_CHDIR(cleanup, path);
192 
193 		perm = test_perms[i % ARRAY_SIZE(test_perms)];
194 
195 		fd[i] = SAFE_OPEN(cleanup, ".", O_TMPFILE | O_RDWR, perm);
196 
197 		write_file(fd[i]);
198 		read_file(fd[i]);
199 
200 		link_tmp_file(fd[i]);
201 
202 		snprintf(path, PATH_MAX, "tmpfile_%d", fd[i]);
203 
204 		SAFE_LSTAT(cleanup, path, &st);
205 
206 		mode_t exp_mode = perm & ~mask;
207 
208 		if ((st.st_mode & ~S_IFMT) != exp_mode) {
209 			tst_brkm(TFAIL, cleanup,
210 				"file mode read %o, but expected %o",
211 				st.st_mode & ~S_IFMT, exp_mode);
212 		}
213 	}
214 
215 	tst_resm(TINFO, "remove files, directories");
216 	for (i = files_num - 1; i >= 0; --i) {
217 		snprintf(path, PATH_MAX, "tmpfile_%d", fd[i]);
218 		SAFE_UNLINK(cleanup, path);
219 		SAFE_CLOSE(cleanup, fd[i]);
220 
221 		SAFE_CHDIR(cleanup, "..");
222 
223 		snprintf(path, PATH_MAX, "tst03_%d", i);
224 		SAFE_RMDIR(cleanup, path);
225 	}
226 
227 	tst_resm(TPASS, "file permission tests passed");
228 }
229 
main(int ac,char * av[])230 int main(int ac, char *av[])
231 {
232 	int lc;
233 
234 	tst_parse_opts(ac, av, NULL, NULL);
235 
236 	setup();
237 
238 	for (lc = 0; TEST_LOOPING(lc); ++lc) {
239 		tst_count = 0;
240 		test01();
241 		test02();
242 		test03();
243 	}
244 
245 	cleanup();
246 	tst_exit();
247 }
248