1 | /* |
---|
2 | * COPYRIGHT (c) 1989-2012. |
---|
3 | * On-Line Applications Research Corporation (OAR). |
---|
4 | * |
---|
5 | * Modifications to support reference counting in the file system are |
---|
6 | * Copyright (c) 2012 embedded brains GmbH. |
---|
7 | * |
---|
8 | * The license and distribution terms for this file may be |
---|
9 | * found in the file LICENSE in this distribution or at |
---|
10 | * http://www.rtems.com/license/LICENSE. |
---|
11 | */ |
---|
12 | |
---|
13 | #ifdef HAVE_CONFIG_H |
---|
14 | #include "config.h" |
---|
15 | #endif |
---|
16 | |
---|
17 | #include <stdio.h> |
---|
18 | #include <sys/types.h> |
---|
19 | #include <sys/stat.h> |
---|
20 | #include <fcntl.h> |
---|
21 | #include <dirent.h> |
---|
22 | #include <string.h> |
---|
23 | #include <unistd.h> |
---|
24 | #include <errno.h> |
---|
25 | #include <rtems.h> |
---|
26 | #include <rtems/libio.h> |
---|
27 | #include <pmacros.h> |
---|
28 | |
---|
29 | /* forward declarations to avoid warnings */ |
---|
30 | int test_main(void); |
---|
31 | |
---|
32 | DIR *directory; |
---|
33 | DIR *directory2; |
---|
34 | DIR *directory3; |
---|
35 | DIR *directory_not; |
---|
36 | |
---|
37 | char *dnames[] = { |
---|
38 | "a", |
---|
39 | "b", |
---|
40 | "c", |
---|
41 | "d", |
---|
42 | "e", |
---|
43 | "f", |
---|
44 | "c/y", |
---|
45 | "c/z", |
---|
46 | "c/x", |
---|
47 | "c/y/a3333", |
---|
48 | "c/y/j123", |
---|
49 | "c/y/my_mount_point", |
---|
50 | "c/y/my_mount_point/my_dir", |
---|
51 | "c/z/my_mount_point", |
---|
52 | "END" |
---|
53 | }; |
---|
54 | |
---|
55 | char *fnames[] = { |
---|
56 | "a", |
---|
57 | "b", |
---|
58 | "c", |
---|
59 | "d", |
---|
60 | "e", |
---|
61 | "f", |
---|
62 | "c/y", |
---|
63 | "c/z", |
---|
64 | "c/x", |
---|
65 | "c/y/a3333", |
---|
66 | "c/y/j123", |
---|
67 | "c/y/my_mount_point", |
---|
68 | "c/y/my_mount_point/my_dir", |
---|
69 | "c/y/my_mount_point/my_dir/d", |
---|
70 | "c/z/my_mount_point", |
---|
71 | "/c/z/my_mount_point/a/../../my_mount_point/a/g", |
---|
72 | "END" |
---|
73 | }; |
---|
74 | |
---|
75 | #if defined(__rtems__) |
---|
76 | int test_main(void) |
---|
77 | #else |
---|
78 | int main( |
---|
79 | int argc, |
---|
80 | char **argv |
---|
81 | ) |
---|
82 | #endif |
---|
83 | { |
---|
84 | int i; |
---|
85 | int fd; |
---|
86 | int status; |
---|
87 | struct stat statbuf; |
---|
88 | static char mount_point_string[25] = { "/c/z/my_mount_point" }; |
---|
89 | static const char my_sub_fs_dir [] = "/c/y/my_mount_point/my_sub_fs_dir"; |
---|
90 | static const char my_link [] = "/c/y/my_link"; |
---|
91 | static const char mount_point [] = "/c/y/my_mount_point"; |
---|
92 | |
---|
93 | printf( "\n\n*** MOUNT/UNMOUNT TEST ***\n" ); |
---|
94 | |
---|
95 | /* |
---|
96 | * Change directory to the root and create files under |
---|
97 | * the base file system. |
---|
98 | */ |
---|
99 | |
---|
100 | printf( "\nchdir to the root directory\n" ); |
---|
101 | status = chdir( "/" ); |
---|
102 | printf( "chdir() status : %d\n\n", status ); |
---|
103 | |
---|
104 | printf( "\nCreating a series of directories under /\n" ); |
---|
105 | i=0; |
---|
106 | while ( strcmp(dnames[i], "END") != 0 ) |
---|
107 | { |
---|
108 | status = mkdir( dnames[i], 0777 ); |
---|
109 | printf("Creating : %25s %d %d ", dnames[i], status, errno ); |
---|
110 | if ( status == 0 ) |
---|
111 | printf(" Success\n"); |
---|
112 | else |
---|
113 | printf(" Failure\n"); |
---|
114 | |
---|
115 | i++; |
---|
116 | } |
---|
117 | |
---|
118 | /* |
---|
119 | * Create a Files with all rwx for others group and user. Verify |
---|
120 | * the created file. |
---|
121 | */ |
---|
122 | |
---|
123 | printf("create /b/my_file\n"); |
---|
124 | fd = open ("/b/my_file", O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO); |
---|
125 | rtems_test_assert( fd != 0 ); |
---|
126 | close (fd); |
---|
127 | |
---|
128 | printf("Verify /b/my_file\n"); |
---|
129 | fd = open("/b/my_file", S_IRWXU|S_IRWXG|S_IRWXO); |
---|
130 | rtems_test_assert( fd != 0 ); |
---|
131 | close( fd ); |
---|
132 | |
---|
133 | |
---|
134 | printf("create c/y/my_mount_point/my_dir/d\n"); |
---|
135 | fd = open ("c/y/my_mount_point/my_dir/d", O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO); |
---|
136 | rtems_test_assert( fd != 0 ); |
---|
137 | close (fd); |
---|
138 | |
---|
139 | printf("Verify c/y/my_mount_point/my_dir/d\n"); |
---|
140 | fd = open("c/y/my_mount_point/my_dir/d", S_IRWXU|S_IRWXG|S_IRWXO); |
---|
141 | rtems_test_assert( fd != 0 ); |
---|
142 | close( fd ); |
---|
143 | |
---|
144 | /* |
---|
145 | * Mount an the IMFS file system on the base file system. |
---|
146 | */ |
---|
147 | |
---|
148 | printf("Attempting to mount IMFS file system at /c/z/my_mount_point \n"); |
---|
149 | status = mount( |
---|
150 | "null", |
---|
151 | mount_point_string, |
---|
152 | "imfs", |
---|
153 | RTEMS_FILESYSTEM_READ_WRITE, |
---|
154 | NULL ); |
---|
155 | rtems_test_assert( status == 0 ); |
---|
156 | printf("2nd file system successfully mounted at /c/z/my_mount_point \n"); |
---|
157 | |
---|
158 | /* |
---|
159 | * Change directory to the mount point and create a group of files under |
---|
160 | * the mounted file system. |
---|
161 | */ |
---|
162 | |
---|
163 | printf( "\nchdir to /c/z/my_mount_point.\n" ); |
---|
164 | status = chdir( "/c/z/my_mount_point" ); |
---|
165 | printf( "chdir() status : %d\n\n", status ); |
---|
166 | |
---|
167 | printf( "\nCreating a series of directories under /c/z/my_mount_point\n" ); |
---|
168 | i=0; |
---|
169 | while ( strcmp(fnames[i], "END") != 0 ) |
---|
170 | { |
---|
171 | status = mkdir( fnames[i], 0777 ); |
---|
172 | printf("Creating: %46s %d %d ", fnames[i], status, errno ); |
---|
173 | if ( status == 0 ) |
---|
174 | printf(" Success\n"); |
---|
175 | else { |
---|
176 | printf(" Failure\n"); |
---|
177 | perror("errno"); |
---|
178 | } |
---|
179 | |
---|
180 | status = stat( fnames[i], &statbuf ); |
---|
181 | if ( status == -1 ) |
---|
182 | printf( ": %s\n", strerror( errno ) ); |
---|
183 | |
---|
184 | i++; |
---|
185 | } |
---|
186 | |
---|
187 | printf( "\nchdir to /\n" ); |
---|
188 | status = chdir( "/" ); |
---|
189 | printf( "chdir() status : %d\n\n", status ); |
---|
190 | |
---|
191 | /* |
---|
192 | * Unmount the first file system we mounted |
---|
193 | */ |
---|
194 | |
---|
195 | printf( "Unmount status:"); |
---|
196 | status = unmount( "/c/z/my_mount_point" ); |
---|
197 | printf( " %d\n", status ); |
---|
198 | |
---|
199 | |
---|
200 | /* |
---|
201 | * Mount a NULL filesystem and verify it fails. |
---|
202 | */ |
---|
203 | |
---|
204 | printf("Mount a NULL file system and verify EINVAL\n"); |
---|
205 | status = mount( |
---|
206 | "null", |
---|
207 | mount_point_string, |
---|
208 | "nofound", |
---|
209 | RTEMS_FILESYSTEM_READ_WRITE, |
---|
210 | NULL ); |
---|
211 | rtems_test_assert( status == -1 ); |
---|
212 | rtems_test_assert( errno == EINVAL ); |
---|
213 | |
---|
214 | /* |
---|
215 | * Verify mount with option of -62 fails with EINVAL |
---|
216 | */ |
---|
217 | |
---|
218 | printf("mount with option of -62 should fail with EINVAL\n"); |
---|
219 | status = mount( |
---|
220 | "null", |
---|
221 | "/c/y/my_mount_point", |
---|
222 | "imfs", |
---|
223 | -62, |
---|
224 | NULL ); |
---|
225 | rtems_test_assert( status == -1 ); |
---|
226 | rtems_test_assert( errno == EINVAL ); |
---|
227 | |
---|
228 | /* |
---|
229 | * Mount a Read Only File system. |
---|
230 | */ |
---|
231 | |
---|
232 | printf("Mount a Read Only filesystem at /c/y/my_mount_point \n"); |
---|
233 | status = mount( |
---|
234 | "null", |
---|
235 | "/c/y/my_mount_point", |
---|
236 | "imfs", |
---|
237 | RTEMS_FILESYSTEM_READ_ONLY, |
---|
238 | NULL ); |
---|
239 | rtems_test_assert( status == 0 ); |
---|
240 | printf("Read only file system successfully mounted at /c/y/my_mount_point \n"); |
---|
241 | |
---|
242 | /* |
---|
243 | * Create a directory that passes through the read only file system. |
---|
244 | */ |
---|
245 | |
---|
246 | printf("create c/y/my_mount_point/../../y/new_dir\n"); |
---|
247 | status = mkdir("c/y/my_mount_point/../../y/new_dir",S_IRWXU ); |
---|
248 | rtems_test_assert( status == 0 ); |
---|
249 | status = stat("c/y/my_mount_point/../../y/new_dir",&statbuf ); |
---|
250 | rtems_test_assert( status == 0 ); |
---|
251 | status = stat("c/y/new_dir", &statbuf ); |
---|
252 | rtems_test_assert( status == 0 ); |
---|
253 | |
---|
254 | /* |
---|
255 | * Attempt to mount a second file system at a used mount point. |
---|
256 | */ |
---|
257 | |
---|
258 | printf("Verify a mount point returns EROFS for another mount\n"); |
---|
259 | status = mount( |
---|
260 | "null", |
---|
261 | "/c/y/my_mount_point", |
---|
262 | "imfs", |
---|
263 | RTEMS_FILESYSTEM_READ_ONLY, |
---|
264 | NULL ); |
---|
265 | rtems_test_assert( status == -1 ); |
---|
266 | rtems_test_assert( errno == EROFS); |
---|
267 | |
---|
268 | printf("Unmount /c/y/my_mount_point\n"); |
---|
269 | status = unmount( "/c/y/my_mount_point" ); |
---|
270 | rtems_test_assert( status == 0 ); |
---|
271 | |
---|
272 | printf("Mount a read-write file system at /c/y/my_mount_point\n"); |
---|
273 | status = mount( |
---|
274 | "null", |
---|
275 | "/c/y/my_mount_point", |
---|
276 | "imfs", |
---|
277 | RTEMS_FILESYSTEM_READ_WRITE, |
---|
278 | NULL ); |
---|
279 | rtems_test_assert( status == 0 ); |
---|
280 | |
---|
281 | printf("Verify a mount point returns EBUSY for another mount\n"); |
---|
282 | status = mount( |
---|
283 | "null", |
---|
284 | "/c/y/my_mount_point", |
---|
285 | "imfs", |
---|
286 | RTEMS_FILESYSTEM_READ_ONLY, |
---|
287 | NULL ); |
---|
288 | rtems_test_assert( status == -1 ); |
---|
289 | rtems_test_assert( errno == EBUSY); |
---|
290 | |
---|
291 | /* |
---|
292 | * Attempt to mount at a file. |
---|
293 | */ |
---|
294 | |
---|
295 | printf("Mount on a file should fail with ENOTDIR\n"); |
---|
296 | status = mount( |
---|
297 | "null", |
---|
298 | "/b/my_file", |
---|
299 | "imfs", |
---|
300 | RTEMS_FILESYSTEM_READ_ONLY, |
---|
301 | NULL ); |
---|
302 | rtems_test_assert( status == -1 ); |
---|
303 | rtems_test_assert( errno == ENOTDIR ); |
---|
304 | |
---|
305 | |
---|
306 | /* |
---|
307 | * Verify we can unmount a file system while we are in it. |
---|
308 | */ |
---|
309 | |
---|
310 | printf("Create and chdir to /c/y/my_mount_point/mydir\n"); |
---|
311 | status = mkdir( "/c/y/my_mount_point/mydir", 0777); |
---|
312 | rtems_test_assert( status == 0 ); |
---|
313 | |
---|
314 | status = chdir( "/c/y/my_mount_point/mydir" ); |
---|
315 | rtems_test_assert( status == 0 ); |
---|
316 | |
---|
317 | printf("unmount of /c/y/my_mount_point\n"); |
---|
318 | status = unmount( "/c/y/my_mount_point" ); |
---|
319 | rtems_test_assert( status == 0 ); |
---|
320 | |
---|
321 | printf("chdir to .. should fail with ENXIO\n"); |
---|
322 | status = chdir( ".." ); |
---|
323 | rtems_test_assert( status == -1 ); |
---|
324 | rtems_test_assert( errno == ENXIO ); |
---|
325 | |
---|
326 | /* |
---|
327 | * Chdir to root and verify we unmounted the file system now. |
---|
328 | */ |
---|
329 | |
---|
330 | printf("chdir to / and verify we can unmount /c/y/my_mount_point\n"); |
---|
331 | status = chdir( "/" ); |
---|
332 | rtems_test_assert( status == 0 ); |
---|
333 | |
---|
334 | printf("chdir to /c/y/my_mount_point/my_dir should fail with ENOENT\n"); |
---|
335 | status = chdir( "/c/y/my_mount_point/mydir" ); |
---|
336 | rtems_test_assert( status == -1 ); |
---|
337 | rtems_test_assert( errno == ENOENT ); |
---|
338 | |
---|
339 | /* |
---|
340 | * Attempt to unmount a directory that does not exist. |
---|
341 | */ |
---|
342 | |
---|
343 | printf("unmount /b/mount_point should fail with EINVAL\n"); |
---|
344 | status = unmount( "/b/mount_point" ); |
---|
345 | rtems_test_assert( status == -1 ); |
---|
346 | rtems_test_assert( errno == ENOENT ); |
---|
347 | |
---|
348 | /* |
---|
349 | * Remount the filesystem. |
---|
350 | */ |
---|
351 | |
---|
352 | printf("Mount /c/y/my_mount_point\n"); |
---|
353 | status = mount( |
---|
354 | "null", |
---|
355 | "/c/y/my_mount_point", |
---|
356 | "imfs", |
---|
357 | RTEMS_FILESYSTEM_READ_WRITE, |
---|
358 | NULL ); |
---|
359 | rtems_test_assert( status == 0 ); |
---|
360 | |
---|
361 | /* |
---|
362 | * Create a file and directory then open the directory. |
---|
363 | * Verify unmount will return successful while directory is open. |
---|
364 | */ |
---|
365 | |
---|
366 | printf("Create and open /c/y/my_mount_point/my_file\n"); |
---|
367 | fd = open( "/c/y/my_mount_point/my_file", O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO); |
---|
368 | rtems_test_assert( fd != -1 ); |
---|
369 | status = close( fd ); |
---|
370 | rtems_test_assert( status == 0 ); |
---|
371 | |
---|
372 | printf("\nmkdir %s\n", my_sub_fs_dir ); |
---|
373 | status = mkdir( my_sub_fs_dir, S_IRWXU ); |
---|
374 | |
---|
375 | printf("open %s\n", my_sub_fs_dir ); |
---|
376 | directory = opendir( my_sub_fs_dir ); |
---|
377 | rtems_test_assert( directory ); |
---|
378 | |
---|
379 | printf("mkdir %s should fail with EEXIST\n", my_sub_fs_dir ); |
---|
380 | status = mkdir( my_sub_fs_dir, S_IRWXU ); |
---|
381 | rtems_test_assert( status == -1 ); |
---|
382 | rtems_test_assert( errno == EEXIST ); |
---|
383 | |
---|
384 | printf("unmount %s\n", mount_point ); |
---|
385 | status = unmount( mount_point ); |
---|
386 | rtems_test_assert( status == 0 ); |
---|
387 | |
---|
388 | printf("close %s\n", my_sub_fs_dir ); |
---|
389 | status = closedir( directory ); |
---|
390 | rtems_test_assert( status == 0 ); |
---|
391 | |
---|
392 | printf("mkdir %s\n", my_sub_fs_dir ); |
---|
393 | status = mkdir( my_sub_fs_dir, S_IRWXU ); |
---|
394 | rtems_test_assert( status == 0 ); |
---|
395 | |
---|
396 | printf("rmdir %s\n", my_sub_fs_dir ); |
---|
397 | status = rmdir( my_sub_fs_dir ); |
---|
398 | rtems_test_assert( status == 0 ); |
---|
399 | |
---|
400 | /* |
---|
401 | * Attempt to unmount a directory that is not a mount point. |
---|
402 | */ |
---|
403 | |
---|
404 | printf("Unmount /c/y/my_mount_point/my_dir should fail with EACCES\n"); |
---|
405 | status = unmount( "/c/y/my_mount_point/my_dir" ); |
---|
406 | rtems_test_assert( status == -1 ); |
---|
407 | rtems_test_assert( errno == EACCES ); |
---|
408 | |
---|
409 | /* |
---|
410 | * Mount file system |
---|
411 | */ |
---|
412 | |
---|
413 | printf("Mount a file system at %s\n", mount_point); |
---|
414 | status = mount( |
---|
415 | "null", |
---|
416 | mount_point, |
---|
417 | "imfs", |
---|
418 | RTEMS_FILESYSTEM_READ_WRITE, |
---|
419 | NULL ); |
---|
420 | rtems_test_assert( status == 0 ); |
---|
421 | |
---|
422 | /* |
---|
423 | * Verify you cannot create a hard link across mounted file systems. |
---|
424 | */ |
---|
425 | |
---|
426 | printf("Verify a hard link across filesystems fails with EXDEV\n"); |
---|
427 | status = mkdir( my_sub_fs_dir, S_IRWXU ); |
---|
428 | rtems_test_assert( status == 0 ); |
---|
429 | |
---|
430 | status = link( my_sub_fs_dir, my_link ); |
---|
431 | rtems_test_assert( status == -1 ); |
---|
432 | rtems_test_assert( errno == EXDEV ); |
---|
433 | |
---|
434 | /* |
---|
435 | * Create a symbolic link across mountpoints. |
---|
436 | */ |
---|
437 | |
---|
438 | printf("Verify a symbolic link across file systems works\n"); |
---|
439 | status = symlink( my_sub_fs_dir, my_link ); |
---|
440 | rtems_test_assert( status == 0 ); |
---|
441 | status = stat( my_link, &statbuf ); |
---|
442 | rtems_test_assert( status == 0 ); |
---|
443 | |
---|
444 | printf("unmount %s\n", mount_point); |
---|
445 | status = unmount( mount_point ); |
---|
446 | rtems_test_assert( status == 0 ); |
---|
447 | |
---|
448 | /* |
---|
449 | * Verify symblic link no longer works. |
---|
450 | */ |
---|
451 | |
---|
452 | printf("Verify the symbolic link now fails\n"); |
---|
453 | status = stat( my_link, &statbuf ); |
---|
454 | rtems_test_assert( status == -1 ); |
---|
455 | rtems_test_assert( errno == ENOENT ); |
---|
456 | |
---|
457 | printf( "\n\n*** END OF MOUNT/UNMOUNT TEST ***\n" ); |
---|
458 | rtems_test_exit(0); |
---|
459 | } |
---|