1 | /* |
---|
2 | * COPYRIGHT (c) 1989-2011. |
---|
3 | * On-Line Applications Research Corporation (OAR). |
---|
4 | * |
---|
5 | * The license and distribution terms for this file may be |
---|
6 | * found in the file LICENSE in this distribution or at |
---|
7 | * http://www.rtems.com/license/LICENSE. |
---|
8 | * |
---|
9 | * $Id$ |
---|
10 | */ |
---|
11 | |
---|
12 | #ifdef HAVE_CONFIG_H |
---|
13 | #include "config.h" |
---|
14 | #endif |
---|
15 | |
---|
16 | #include <stdio.h> |
---|
17 | #include <errno.h> |
---|
18 | #include <fcntl.h> |
---|
19 | #include <string.h> |
---|
20 | #include <unistd.h> |
---|
21 | #include <stdlib.h> /* exit */ |
---|
22 | #include <sys/stat.h> |
---|
23 | #include <sys/types.h> |
---|
24 | |
---|
25 | #include "rtems.h" |
---|
26 | |
---|
27 | #include "fstest_support.h" |
---|
28 | #include "fs_config.h" |
---|
29 | |
---|
30 | #include "fstest.h" |
---|
31 | #include "pmacros.h" |
---|
32 | |
---|
33 | #define TEMP_DIR "waterbuffalo" |
---|
34 | |
---|
35 | |
---|
36 | /* Break out of a chroot() environment in C */ |
---|
37 | void break_out_of_chroot(void) |
---|
38 | { |
---|
39 | |
---|
40 | int dir_fd; /* File descriptor to directory */ |
---|
41 | struct stat sbuf; /* The stat() buffer */ |
---|
42 | chdir("/"); |
---|
43 | |
---|
44 | if (stat(TEMP_DIR,&sbuf)<0) { |
---|
45 | if (errno==ENOENT) { |
---|
46 | if (mkdir(TEMP_DIR,0755)<0) { |
---|
47 | fprintf(stderr,"Failed to create %s - %s\n", TEMP_DIR, |
---|
48 | strerror(errno)); |
---|
49 | exit(1); |
---|
50 | } |
---|
51 | } else { |
---|
52 | fprintf(stderr,"Failed to stat %s - %s\n", TEMP_DIR, |
---|
53 | strerror(errno)); |
---|
54 | exit(1); |
---|
55 | } |
---|
56 | } else if (!S_ISDIR(sbuf.st_mode)) { |
---|
57 | fprintf(stderr,"Error - %s is not a directory!\n",TEMP_DIR); |
---|
58 | exit(1); |
---|
59 | } |
---|
60 | |
---|
61 | if ((dir_fd=open(".",O_RDONLY))<0) { |
---|
62 | fprintf(stderr,"Failed to open ""." |
---|
63 | " for reading - %s\n", strerror(errno)); |
---|
64 | exit(1); |
---|
65 | } |
---|
66 | |
---|
67 | if (chroot(TEMP_DIR)<0) { |
---|
68 | fprintf(stderr,"Failed to chroot to %s - %s\n",TEMP_DIR, |
---|
69 | strerror(errno)); |
---|
70 | exit(1); |
---|
71 | } |
---|
72 | |
---|
73 | if (fchdir(dir_fd)<0) { |
---|
74 | fprintf(stderr,"Failed to fchdir - %s\n", |
---|
75 | strerror(errno)); |
---|
76 | exit(1); |
---|
77 | } |
---|
78 | close(dir_fd); |
---|
79 | chdir(".."); |
---|
80 | chroot("."); |
---|
81 | |
---|
82 | } |
---|
83 | |
---|
84 | /* |
---|
85 | * Main entry point of every filesystem test |
---|
86 | */ |
---|
87 | |
---|
88 | rtems_task Init( |
---|
89 | rtems_task_argument ignored) |
---|
90 | { |
---|
91 | int rc=0; |
---|
92 | puts( "\n\n*** FILE SYSTEM TEST ( " FILESYSTEM " ) ***" ); |
---|
93 | |
---|
94 | puts( "Initializing filesystem " FILESYSTEM ); |
---|
95 | test_initialize_filesystem(); |
---|
96 | rc=chdir(BASE_FOR_TEST); |
---|
97 | rtems_test_assert(rc==0); |
---|
98 | |
---|
99 | rc=chroot(BASE_FOR_TEST); |
---|
100 | rtems_test_assert(rc==0); |
---|
101 | |
---|
102 | test(); |
---|
103 | |
---|
104 | break_out_of_chroot(); |
---|
105 | chdir("/"); |
---|
106 | |
---|
107 | puts( "\n\nShutting down filesystem " FILESYSTEM ); |
---|
108 | test_shutdown_filesystem(); |
---|
109 | |
---|
110 | puts( "*** END OF FILE SYSTEM TEST ( " FILESYSTEM " ) ***" ); |
---|
111 | rtems_test_exit(0); |
---|
112 | } |
---|