5 * Created by Ryan Branche on 2/17/08.
6 * Copyright 2008 Apple Inc. All rights reserved.
11 * Explicitely turn off 64-bit inodes because we are testing the 32-bit inode
12 * versions of statfs functions and getdirentries doesn't support 64-bit inodes.
14 #define _DARWIN_NO_64_BIT_INODE 1
17 #include <mach/mach.h>
20 extern char g_target_path
[ PATH_MAX
];
21 extern int g_skip_setuid_tests
;
22 extern int g_is_single_user
;
24 /* **************************************************************************************************************
25 * Test getdirentries system call.
26 * **************************************************************************************************************
28 struct test_attr_buf
{
30 fsobj_type_t obj_type
;
32 struct timespec backup_time
;
35 typedef struct test_attr_buf test_attr_buf
;
37 int getdirentries_test( void * the_argp
)
39 int my_err
, done
, found_it
, i
;
42 char * my_pathp
= NULL
;
43 char * my_bufp
= NULL
;
46 unsigned long my_count
;
47 unsigned long my_new_state
;
49 struct timespec my_new_backup_time
;
50 struct attrlist my_attrlist
;
51 test_attr_buf my_attr_buf
[4];
52 struct statfs my_statfs_buf
;
55 /* need to know type of file system */
56 my_err
= statfs( &g_target_path
[0], &my_statfs_buf
);
58 printf( "statfs call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
59 goto test_failed_exit
;
61 if ( memcmp( &my_statfs_buf
.f_fstypename
[0], "ufs", 3 ) == 0 ) {
65 my_kr
= vm_allocate((vm_map_t
) mach_task_self(), (vm_address_t
*)&my_bufp
, (1024 * 5), VM_FLAGS_ANYWHERE
);
66 if(my_kr
!= KERN_SUCCESS
){
67 printf( "vm_allocate failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
68 goto test_failed_exit
;
71 my_kr
= vm_allocate((vm_map_t
) mach_task_self(), (vm_address_t
*)&my_pathp
, PATH_MAX
, VM_FLAGS_ANYWHERE
);
72 if(my_kr
!= KERN_SUCCESS
){
73 printf( "vm_allocate failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
74 goto test_failed_exit
;
78 strcat( my_pathp
, &g_target_path
[0] );
79 strcat( my_pathp
, "/" );
81 /* create a test file */
82 my_err
= create_random_name( my_pathp
, 1 );
84 goto test_failed_exit
;
87 /* get pointer to just the file name */
88 my_file_namep
= strrchr( my_pathp
, '/' );
91 /* check out the test directory */
92 my_fd
= open( &g_target_path
[0], (O_RDONLY
), 0 );
94 printf( "open failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
95 goto test_failed_exit
;
101 struct dirent
* my_dirent_p
;
103 /* This call requires that 64-bit inodes are disabled */
104 my_result
= getdirentries( my_fd
, my_bufp
, (1024 * 5), &my_base
);
105 if ( my_result
<= 0 )
107 for ( i
= 0; i
< my_result
; ) {
108 my_dirent_p
= (struct dirent
*) (my_bufp
+ i
);
110 printf( "d_ino %d d_reclen %d d_type %d d_namlen %d \"%s\" \n",
111 my_dirent_p
->d_ino
, my_dirent_p
->d_reclen
, my_dirent_p
->d_type
,
112 my_dirent_p
->d_namlen
, &my_dirent_p
->d_name
[0] );
115 i
+= my_dirent_p
->d_reclen
;
116 /* validate results by looking for our test file */
117 if ( my_dirent_p
->d_type
== DT_REG
&& my_dirent_p
->d_ino
!= 0 &&
118 strlen( my_file_namep
) == my_dirent_p
->d_namlen
&&
119 memcmp( &my_dirent_p
->d_name
[0], my_file_namep
, my_dirent_p
->d_namlen
) == 0 ) {
125 if ( found_it
== 0 ) {
126 printf( "getdirentries failed to find test file. \n" );
127 goto test_failed_exit
;
137 if ( my_pathp
!= NULL
) {
139 vm_deallocate(mach_task_self(), (vm_address_t
)my_pathp
, PATH_MAX
);
141 if ( my_bufp
!= NULL
) {
142 vm_deallocate(mach_task_self(), (vm_address_t
)my_bufp
, (1024 * 5));
149 /* **************************************************************************************************************
150 * Test 32-bit inode versions of statfs, fstatfs, and getfsstat system calls.
151 * **************************************************************************************************************
155 struct vol_attr_buf
{
158 u_int32_t io_blksize
;
161 typedef struct vol_attr_buf vol_attr_buf
;
163 int statfs_32bit_inode_tests( void * the_argp
)
165 int my_err
, my_count
, i
;
169 void * my_bufferp
= NULL
;
170 struct statfs
* my_statfsp
;
173 struct attrlist my_attrlist
;
174 vol_attr_buf my_attr_buf
;
177 my_buffer_size
= (sizeof(struct statfs
) * 10);
178 my_kr
= vm_allocate((vm_map_t
) mach_task_self(), (vm_address_t
*)&my_bufferp
, my_buffer_size
, VM_FLAGS_ANYWHERE
);
179 if(my_kr
!= KERN_SUCCESS
){
180 printf( "vm_allocate failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
181 goto test_failed_exit
;
184 my_statfsp
= (struct statfs
*) my_bufferp
;
185 my_err
= statfs( "/", my_statfsp
);
186 if ( my_err
== -1 ) {
187 printf( "statfs call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
188 goto test_failed_exit
;
190 if ( memcmp( &my_statfsp
->f_fstypename
[0], "ufs", 3 ) == 0 ) {
194 my_count
= getfsstat( (struct statfs
*)my_bufferp
, my_buffer_size
, MNT_NOWAIT
);
195 if ( my_count
== -1 ) {
196 printf( "getfsstat call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
197 goto test_failed_exit
;
200 /* validate results */
201 my_statfsp
= (struct statfs
*) my_bufferp
;
202 for ( i
= 0; i
< my_count
; i
++, my_statfsp
++ ) {
203 if ( memcmp( &my_statfsp
->f_fstypename
[0], "hfs", 3 ) == 0 ||
204 memcmp( &my_statfsp
->f_fstypename
[0], "ufs", 3 ) == 0 ||
205 memcmp( &my_statfsp
->f_fstypename
[0], "devfs", 5 ) == 0 ||
206 memcmp( &my_statfsp
->f_fstypename
[0], "volfs", 5 ) == 0 ) {
207 /* found a valid entry */
211 if ( i
>= my_count
) {
212 printf( "getfsstat call failed. could not find valid f_fstypename! \n" );
213 goto test_failed_exit
;
216 /* set up to validate results via multiple sources. we use getattrlist to get volume
217 * related attributes to verify against results from fstatfs and statfs - but only if
218 * we are not targeting ufs volume since it doesn't support getattr calls
221 memset( &my_attrlist
, 0, sizeof(my_attrlist
) );
222 my_attrlist
.bitmapcount
= ATTR_BIT_MAP_COUNT
;
223 my_attrlist
.volattr
= (ATTR_VOL_SIZE
| ATTR_VOL_IOBLOCKSIZE
);
224 my_err
= getattrlist( "/", &my_attrlist
, &my_attr_buf
, sizeof(my_attr_buf
), 0 );
226 printf( "getattrlist call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
227 goto test_failed_exit
;
231 /* open kernel to use as test file for fstatfs */
232 my_fd
= open( "/System/Library/Kernels/kernel", O_RDONLY
, 0 );
234 printf( "open call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
235 goto test_failed_exit
;
238 /* testing fstatfs */
239 my_statfsp
= (struct statfs
*) my_bufferp
;
240 my_err
= fstatfs( my_fd
, my_statfsp
);
241 if ( my_err
== -1 ) {
242 printf( "fstatfs call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
243 goto test_failed_exit
;
246 /* validate results */
247 if ( !(memcmp( &my_statfsp
->f_fstypename
[0], "hfs", 3 ) == 0 ||
248 memcmp( &my_statfsp
->f_fstypename
[0], "ufs", 3 ) == 0) ) {
249 printf( "fstatfs call failed. could not find valid f_fstypename! \n" );
250 goto test_failed_exit
;
252 my_io_size
= my_statfsp
->f_iosize
;
253 my_fsid
= my_statfsp
->f_fsid
;
254 if ( is_ufs
== 0 && my_statfsp
->f_iosize
!= my_attr_buf
.io_blksize
) {
255 printf( "fstatfs and getattrlist results do not match for volume block size \n" );
256 goto test_failed_exit
;
259 /* try again with statfs */
260 my_err
= statfs( "/System/Library/Kernels/kernel", my_statfsp
);
261 if ( my_err
== -1 ) {
262 printf( "statfs call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
263 goto test_failed_exit
;
266 /* validate resutls */
267 if ( my_io_size
!= my_statfsp
->f_iosize
|| my_fsid
.val
[0] != my_statfsp
->f_fsid
.val
[0] ||
268 my_fsid
.val
[1] != my_statfsp
->f_fsid
.val
[1] ) {
269 printf( "statfs call failed. wrong f_iosize or f_fsid! \n" );
270 goto test_failed_exit
;
272 if ( is_ufs
== 0 && my_statfsp
->f_iosize
!= my_attr_buf
.io_blksize
) {
273 printf( "statfs and getattrlist results do not match for volume block size \n" );
274 goto test_failed_exit
;
277 /* We passed the test */
287 if ( my_bufferp
!= NULL
) {
288 vm_deallocate(mach_task_self(), (vm_address_t
)my_bufferp
, my_buffer_size
);