5 * Created by Jerry Cottingham on 3/25/05.
6 * Copyright 2008 Apple Inc. All rights reserved.
11 #include <sys/ipc.h> /* for message queue tests */
12 #include <sys/msg.h> /* for message queue tests */
13 #include <sys/syscall.h> /* for get / settid */
14 #include <sys/sysctl.h> /* for determining hw */
15 #include <AvailabilityMacros.h> /* for determination of Mac OS X version (tiger, leopard, etc.) */
16 #include <libkern/OSByteOrder.h> /* for OSSwap32() */
17 #include <mach/mach.h>
19 extern char g_target_path
[ PATH_MAX
];
20 extern int g_skip_setuid_tests
;
21 extern int g_is_under_rosetta
;
22 extern int g_is_single_user
;
25 void print_acct_debug_strings( char * my_ac_comm
);
28 #if TEST_SYSTEM_CALLS /* system calls to do */
29 "reboot", /* 55 = reboot */
30 "revoke", /* 56 = revoke */
31 "sbrk", /* 69 = sbrk */
32 "sstk", /* 70 = sstk */
33 "mount", /* 167 = mount */
34 "unmount", /* 159 = unmount */
35 "undelete", /* 205 = undelete */
36 "watchevent", /* 231 = watchevent */
37 "waitevent", /* 232 = waitevent */
38 "modwatch", /* 233 = modwatch */
39 "fsctl", /* 242 = fsctl */
40 "initgroups", /* 243 = initgroups */
41 "semsys", /* 251 = semsys */
42 "semconfig", /* 257 = semconfig */
43 "msgsys", /* 252 = msgsys */
44 "shmsys", /* 253 = shmsys */
45 "load_shared_file", /* 296 = load_shared_file */
46 "reset_shared_file", /* 297 = reset_shared_file */
47 "new_system_shared_regions", /* 298 = new_system_shared_regions */
48 "shared_region_map_file_np", /* 299 = shared_region_map_file_np */
49 "shared_region_make_private_np", /* 300 = shared_region_make_private_np */
50 "__pthread_kill", /* 328 = __pthread_kill */
51 "pthread_sigmask", /* 329 = pthread_sigmask */
52 "__disable_threadsignal", /* 331 = __disable_threadsignal */
53 "__pthread_markcancel", /* 332 = __pthread_markcancel */
54 "__pthread_canceled", /* 333 = __pthread_canceled */
55 "__semwait_signal", /* 334 = __semwait_signal */
56 "audit", /* 350 = audit */
57 "auditon", /* 351 = auditon */
58 "getaudit", /* 355 = getaudit */
59 "setaudit", /* 356 = setaudit */
60 "getaudit_addr", /* 357 = getaudit_addr */
61 "setaudit_addr", /* 358 = setaudit_addr */
62 "auditctl", /* 359 = auditctl */
65 /* **************************************************************************************************************
66 * Test the syscall system call.
67 * **************************************************************************************************************
69 int syscall_test( void * the_argp
)
76 my_kr
= vm_allocate((vm_map_t
) mach_task_self(), (vm_address_t
*)&my_pathp
, PATH_MAX
, VM_FLAGS_ANYWHERE
);
77 if(my_kr
!= KERN_SUCCESS
){
78 printf( "vm_allocate failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
79 goto test_failed_exit
;
83 strcpy( my_pathp
, &g_target_path
[0] );
84 strcat( my_pathp
, "/" );
86 /* create a test file */
87 my_err
= create_random_name( my_pathp
, 1 );
89 goto test_failed_exit
;
92 /* use an indirect system call to open our test file.
93 * I picked open since it uses a path pointer which grows to 64 bits in an LP64 environment.
95 my_fd
= syscall( SYS_open
, my_pathp
, (O_RDWR
| O_EXCL
), 0 );
97 printf( "open call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
98 printf( "\t file we attempted to open -> \"%s\" \n", my_pathp
);
99 goto test_failed_exit
;
103 goto test_passed_exit
;
111 if ( my_pathp
!= NULL
) {
113 vm_deallocate(mach_task_self(), (vm_address_t
)my_pathp
, PATH_MAX
);
118 /* **************************************************************************************************************
119 * Test fork wait4, and exit system calls.
120 * **************************************************************************************************************
122 int fork_wait4_exit_test( void * the_argp
)
124 int my_err
, my_status
;
125 pid_t my_pid
, my_wait_pid
;
126 struct rusage my_usage
;
128 /* spin off another process */
130 if ( my_pid
== -1 ) {
131 printf( "fork failed with errno %d - %s \n", errno
, strerror( errno
) );
134 else if ( my_pid
== 0 ) {
137 /* child process does very little then exits */
138 my_err
= stat( &g_target_path
[0], &my_sb
);
140 printf( "stat call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
141 printf( "\t path we stated \"%s\" \n", &g_target_path
[0] );
147 /* parent process waits for child to exit */
148 my_wait_pid
= wait4( my_pid
, &my_status
, 0, &my_usage
);
149 if ( my_wait_pid
== -1 ) {
150 printf( "wait4 failed with errno %d - %s \n", errno
, strerror( errno
) );
154 /* wait4 should return our child's pid when it exits */
155 if ( my_wait_pid
!= my_pid
) {
156 printf( "wait4 did not return child pid - returned %d should be %d \n", my_wait_pid
, my_pid
);
160 /* kind of just guessing on these values so if this fails we should take a closer
161 * look at the returned rusage structure.
163 if ( my_usage
.ru_utime
.tv_sec
> 1 || my_usage
.ru_stime
.tv_sec
> 1 ||
164 my_usage
.ru_majflt
> 1000 || my_usage
.ru_msgsnd
> 100 ) {
165 printf( "wait4 returned an odd looking rusage structure \n" );
169 if ( WIFEXITED( my_status
) && WEXITSTATUS( my_status
) == 44 ) {
172 printf( "wait4 returned wrong exit status - 0x%02X \n", my_status
);
179 /* **************************************************************************************************************
180 * Test fsync, ftruncate, lseek, pread, pwrite, read, readv, truncate, write, writev system calls.
181 * **************************************************************************************************************
183 int read_write_test( void * the_argp
)
187 char * my_pathp
= NULL
;
188 char * my_bufp
= NULL
;
190 off_t my_current_offset
;
191 struct iovec my_iovs
[2];
195 my_kr
= vm_allocate((vm_map_t
) mach_task_self(), (vm_address_t
*)&my_pathp
, PATH_MAX
, VM_FLAGS_ANYWHERE
);
196 if(my_kr
!= KERN_SUCCESS
){
197 printf( "vm_allocate failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
198 goto test_failed_exit
;
201 my_kr
= vm_allocate((vm_map_t
) mach_task_self(), (vm_address_t
*)&my_bufp
, MY_BUFFER_SIZE
, VM_FLAGS_ANYWHERE
);
202 if(my_kr
!= KERN_SUCCESS
){
203 printf( "vm_allocate failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
204 goto test_failed_exit
;
208 strcat( my_pathp
, &g_target_path
[0] );
209 strcat( my_pathp
, "/" );
211 /* create a test file */
212 my_err
= create_random_name( my_pathp
, 1 );
214 goto test_failed_exit
;
217 my_fd
= open( my_pathp
, O_RDONLY
, 0 );
219 printf( "open call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
220 printf( "\t file we attempted to open -> \"%s\" \n", my_pathp
);
221 goto test_failed_exit
;
224 /* should get EOF since the file is empty at this point */
225 my_result
= read( my_fd
, my_bufp
, 10);
226 if ( my_result
== -1 ) {
227 printf( "read call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
228 goto test_failed_exit
;
230 if ( my_result
!= 0 ) {
231 if ( sizeof( ssize_t
) > sizeof( int ) ) {
232 printf( "read call failed - should have read 0 bytes on empty file - read %ld \n", (long int) my_result
);
235 printf( "read call failed - should have read 0 bytes on empty file - read %d \n", (int) my_result
);
237 goto test_failed_exit
;
240 /* this write should fail since we opened for read only */
241 my_result
= write( my_fd
, my_bufp
, 10 );
243 if ( my_result
!= -1 ) {
244 if ( sizeof( ssize_t
) > sizeof( int ) ) {
245 printf( "write should have failed for read only fd - %ld \n", (long int) my_result
);
248 printf( "write should have failed for read only fd - %d \n", (int) my_result
);
250 goto test_failed_exit
;
252 if ( my_err
!= EBADF
) {
253 printf( "write call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
254 printf( "should have failed with EBADF error %d \n", EBADF
);
255 goto test_failed_exit
;
258 /* now really write some data */
260 my_fd
= open( my_pathp
, O_RDWR
, 0 );
262 printf( "open call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
263 printf( "\t file we attempted to open -> \"%s\" \n", my_pathp
);
264 goto test_failed_exit
;
267 memset( my_bufp
, 'j', MY_BUFFER_SIZE
);
268 my_result
= write( my_fd
, my_bufp
, MY_BUFFER_SIZE
);
269 if ( my_result
== -1 ) {
270 printf( "write call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
271 goto test_failed_exit
;
273 if ( my_result
!= MY_BUFFER_SIZE
) {
274 printf( "write failed to write out all the data \n" );
275 goto test_failed_exit
;
278 /* push data to disk */
279 my_err
= fsync( my_fd
);
280 if ( my_err
== -1 ) {
281 printf( "fsync failed with errno %d - %s \n", errno
, strerror( errno
) );
282 goto test_failed_exit
;
285 /* now verify the write worked OK using readv */
286 lseek( my_fd
, 0, SEEK_SET
);
287 bzero( (void *)my_bufp
, MY_BUFFER_SIZE
);
288 my_iovs
[0].iov_base
= my_bufp
;
289 my_iovs
[0].iov_len
= 16;
290 my_iovs
[1].iov_base
= (my_bufp
+ MY_BUFFER_SIZE
- 16) ;
291 my_iovs
[1].iov_len
= 16;
293 my_result
= readv( my_fd
, &my_iovs
[0], 2 );
294 if ( my_result
== -1 ) {
295 printf( "readv call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
296 goto test_failed_exit
;
298 if ( my_result
!= 32 ) {
299 printf( "readv failed to get all the data - asked for %d got back %d\n", MY_BUFFER_SIZE
, (int) my_result
);
300 goto test_failed_exit
;
302 if ( *my_bufp
!= 'j' || *(my_bufp
+ (MY_BUFFER_SIZE
- 1)) != 'j' ) {
303 printf( "readv failed to get correct data \n" );
304 goto test_failed_exit
;
308 my_err
= ftruncate( my_fd
, 0 );
309 if ( my_err
== -1 ) {
310 printf( "ftruncate call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
311 goto test_failed_exit
;
314 my_err
= fstat( my_fd
, &my_sb
);
315 if ( my_err
== -1 ) {
316 printf( "fstat call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
317 goto test_failed_exit
;
319 if ( my_sb
.st_size
!= 0 ) {
320 printf( "ftruncate call failed - file size is wrong \n" );
321 goto test_failed_exit
;
325 lseek( my_fd
, 0, SEEK_SET
);
326 memset( my_bufp
, 'z', MY_BUFFER_SIZE
);
327 my_iovs
[0].iov_base
= my_bufp
;
328 my_iovs
[0].iov_len
= 8;
329 my_iovs
[1].iov_base
= (my_bufp
+ MY_BUFFER_SIZE
- 8) ;
330 my_iovs
[1].iov_len
= 8;
331 my_result
= writev( my_fd
, &my_iovs
[0], 2 );
332 if ( my_result
== -1 ) {
333 printf( "writev call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
334 goto test_failed_exit
;
336 if ( my_result
!= 16 ) {
337 printf( "writev failed to get all the data - asked for %d got back %d\n", MY_BUFFER_SIZE
, (int) my_result
);
338 goto test_failed_exit
;
341 /* now verify the writev worked OK */
342 lseek( my_fd
, 0, SEEK_SET
);
343 bzero( (void *)my_bufp
, MY_BUFFER_SIZE
);
344 my_iovs
[0].iov_base
= my_bufp
;
345 my_iovs
[0].iov_len
= 8;
346 my_iovs
[1].iov_base
= (my_bufp
+ MY_BUFFER_SIZE
- 8) ;
347 my_iovs
[1].iov_len
= 8;
349 my_result
= readv( my_fd
, &my_iovs
[0], 2 );
350 if ( my_result
== -1 ) {
351 printf( "readv call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
352 goto test_failed_exit
;
354 if ( my_result
!= 16 ) {
355 printf( "readv failed to get all the data - asked for %d got back %d\n", MY_BUFFER_SIZE
, (int) my_result
);
356 goto test_failed_exit
;
358 if ( *my_bufp
!= 'z' || *(my_bufp
+ (MY_BUFFER_SIZE
- 1)) != 'z' ) {
359 printf( "readv failed to get correct data \n" );
360 goto test_failed_exit
;
363 /* test pread and pwrite */
364 my_current_offset
= lseek( my_fd
, 0, SEEK_CUR
);
365 if ( my_current_offset
== -1 ) {
366 printf( "lseek call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
367 goto test_failed_exit
;
370 my_result
= pwrite( my_fd
, "jer", 3, my_current_offset
);
371 if ( my_result
== -1 ) {
372 printf( "pwrite call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
373 goto test_failed_exit
;
375 if ( my_result
!= 3 ) {
376 printf( "pwrite failed to write all the data \n" );
377 goto test_failed_exit
;
380 /* make sure file position did not advance */
381 if ( my_current_offset
!= lseek( my_fd
, 0, SEEK_CUR
) ) {
382 printf( "pwrite advanced file positiion \n" );
383 goto test_failed_exit
;
386 bzero( (void *)my_bufp
, MY_BUFFER_SIZE
);
387 my_result
= pread( my_fd
, my_bufp
, 3, my_current_offset
);
388 if ( my_result
== -1 ) {
389 printf( "pread call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
390 goto test_failed_exit
;
392 if ( my_result
!= 3 ) {
393 printf( "pread failed to write all the data \n" );
394 goto test_failed_exit
;
397 /* make sure file position did not advance */
398 if ( my_current_offset
!= lseek( my_fd
, 0, SEEK_CUR
) ) {
399 printf( "pread advanced file positiion \n" );
400 goto test_failed_exit
;
403 /* make sure pread and pwrite transferred correct data */
404 if ( strcmp( my_bufp
, "jer" ) != 0 ) {
405 printf( "pread or pwrite failed to read / write correct data \n" );
406 goto test_failed_exit
;
410 my_err
= truncate( my_pathp
, 0 );
411 if ( my_err
== -1 ) {
412 printf( "truncate call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
413 goto test_failed_exit
;
416 my_err
= stat( my_pathp
, &my_sb
);
417 if ( my_err
== -1 ) {
418 printf( "stat call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
419 goto test_failed_exit
;
421 if ( my_sb
.st_size
!= 0 ) {
422 printf( "truncate call failed - file size is wrong \n" );
423 goto test_failed_exit
;
427 goto test_passed_exit
;
435 if ( my_pathp
!= NULL
) {
437 vm_deallocate(mach_task_self(), (vm_address_t
)my_pathp
, PATH_MAX
);
439 if ( my_bufp
!= NULL
)
440 vm_deallocate(mach_task_self(), (vm_address_t
)my_bufp
, MY_BUFFER_SIZE
);
444 /* **************************************************************************************************************
445 * Test close, fpathconf, fstat, open, pathconf system calls.
446 * **************************************************************************************************************
448 int open_close_test( void * the_argp
)
452 char * my_pathp
= NULL
;
454 long my_pconf_result
;
459 my_kr
= vm_allocate((vm_map_t
) mach_task_self(), (vm_address_t
*)&my_pathp
, PATH_MAX
, VM_FLAGS_ANYWHERE
);
460 if(my_kr
!= KERN_SUCCESS
){
461 printf( "vm_allocate failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
462 goto test_failed_exit
;
466 strcat( my_pathp
, &g_target_path
[0] );
467 strcat( my_pathp
, "/" );
469 /* create a test file */
470 my_err
= create_random_name( my_pathp
, 1 );
472 goto test_failed_exit
;
475 /* test O_WRONLY case */
476 my_fd
= open( my_pathp
, O_WRONLY
, 0 );
478 printf( "open call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
479 printf( "\t file we attempted to open -> \"%s\" \n", my_pathp
);
480 goto test_failed_exit
;
483 /* test pathconf and fpathconf */
484 my_pconf_result
= pathconf( my_pathp
, _PC_PATH_MAX
);
485 if ( my_pconf_result
== -1 ) {
486 printf( "pathconf - _PC_PATH_MAX - failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
487 goto test_failed_exit
;
489 // printf( "_PC_PATH_MAX %ld \n", my_pconf_result );
490 /* results look OK? */
491 if ( my_pconf_result
< PATH_MAX
) {
492 printf( "pathconf - _PC_PATH_MAX - looks like wrong results \n" );
493 goto test_failed_exit
;
496 my_pconf_result
= fpathconf( my_fd
, _PC_NAME_MAX
);
497 if ( my_pconf_result
== -1 ) {
498 printf( "fpathconf - _PC_PATH_MAX - failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
499 goto test_failed_exit
;
501 // printf( "_PC_NAME_MAX %ld \n", my_pconf_result );
502 /* results look OK? */
503 if ( my_pconf_result
< 6 ) {
504 printf( "fpathconf - _PC_NAME_MAX - looks like wrong results \n" );
505 goto test_failed_exit
;
508 /* write some data then try to read it */
509 my_result
= write( my_fd
, "kat", 3 );
511 if ( my_result
!= 3 ) {
512 if ( sizeof( ssize_t
) > sizeof( int ) ) {
513 printf( "write failed. should have written 3 bytes actually wrote - %ld \n", (long int) my_result
);
516 printf( "write failed. should have written 3 bytes actually wrote - %d \n", (int) my_result
);
518 goto test_failed_exit
;
521 /* Try to read - this should fail since we opened file with O_WRONLY */
522 my_result
= read( my_fd
, &my_buffer
[0], sizeof(my_buffer
) );
524 if ( my_result
!= -1 ) {
525 printf( "read call should have failed with errno 9 (EBADF) \n" );
526 goto test_failed_exit
;
528 else if ( my_err
!= EBADF
) {
529 printf( "read call should have failed with errno 9 (EBADF). actually failed with %d - \"%s\" \n", my_err
, strerror( my_err
) );
530 goto test_failed_exit
;
535 /* test O_TRUNC and O_APPEND case */
536 my_fd
= open( my_pathp
, (O_RDWR
| O_TRUNC
| O_APPEND
), 0 );
538 printf( "open call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
539 printf( "\t file we attempted to open -> \"%s\" \n", my_pathp
);
540 goto test_failed_exit
;
543 my_result
= read( my_fd
, &my_buffer
[0], sizeof(my_buffer
) );
544 if ( my_result
== -1 ) {
545 printf( "read call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
546 goto test_failed_exit
;
548 if ( my_result
!= 0 ) {
549 printf( "read failed - should have read 0 bytes. \n" );
550 goto test_failed_exit
;
553 my_result
= write( my_fd
, "kat", 3 );
555 if ( my_result
!= 3 ) {
556 if ( sizeof( ssize_t
) > sizeof( int ) ) {
557 printf( "write failed. should have written 3 bytes actually wrote - %ld \n", (long int) my_result
);
560 printf( "write failed. should have written 3 bytes actually wrote - %d \n", (int) my_result
);
562 goto test_failed_exit
;
565 /* add some more data to the test file - this should be appended */
566 lseek( my_fd
, 0, SEEK_SET
);
567 my_result
= write( my_fd
, "zzz", 3 );
569 if ( my_result
!= 3 ) {
570 if ( sizeof( ssize_t
) > sizeof( int ) ) {
571 printf( "write failed. should have written 3 bytes actually wrote - %ld \n", (long int) my_result
);
574 printf( "write failed. should have written 3 bytes actually wrote - %d \n", (int) my_result
);
576 goto test_failed_exit
;
579 /* now verify the writes */
580 bzero( (void *)&my_buffer
[0], sizeof(my_buffer
) );
581 lseek( my_fd
, 0, SEEK_SET
);
582 my_result
= read( my_fd
, &my_buffer
[0], sizeof(my_buffer
) );
583 if ( my_result
== -1 ) {
584 printf( "read call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
585 goto test_failed_exit
;
587 if ( my_buffer
[0] != 'k' || my_buffer
[5] != 'z' ) {
588 printf( "read failed to get correct data \n" );
589 goto test_failed_exit
;
593 my_err
= fstat( my_fd
, &my_sb
);
594 if ( my_err
== -1 ) {
595 printf( "fstat call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
596 goto test_failed_exit
;
598 if ( my_sb
.st_size
!= 6 ) {
599 printf( "fstat call failed - st_size is wrong \n" );
600 goto test_failed_exit
;
602 if ( !S_ISREG( my_sb
.st_mode
) ) {
603 printf( "fstat call failed - st_mode does not indicate regular file \n" );
604 goto test_failed_exit
;
608 goto test_passed_exit
;
616 if ( my_pathp
!= NULL
) {
618 vm_deallocate(mach_task_self(), (vm_address_t
)my_pathp
, PATH_MAX
);
623 /* **************************************************************************************************************
624 * Test link, stat and unlink system calls.
625 * **************************************************************************************************************
627 int link_stat_unlink_test( void * the_argp
)
631 char * my_pathp
= NULL
;
632 char * my_path2p
= NULL
;
633 nlink_t my_link_count
;
638 my_kr
= vm_allocate((vm_map_t
) mach_task_self(), (vm_address_t
*)&my_pathp
, PATH_MAX
, VM_FLAGS_ANYWHERE
);
639 if(my_kr
!= KERN_SUCCESS
){
640 printf( "vm_allocate failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
641 goto test_failed_exit
;
644 my_kr
= vm_allocate((vm_map_t
) mach_task_self(), (vm_address_t
*)&my_path2p
, PATH_MAX
, VM_FLAGS_ANYWHERE
);
645 if(my_kr
!= KERN_SUCCESS
){
646 printf( "vm_allocate failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
647 goto test_failed_exit
;
652 strcat( my_pathp
, &g_target_path
[0] );
653 strcat( my_pathp
, "/" );
655 /* create a test file */
656 my_err
= create_random_name( my_pathp
, 1 );
658 goto test_failed_exit
;
661 /* now create a name for the link file */
662 strcat( my_path2p
, my_pathp
);
663 strcat( my_path2p
, "link" );
665 /* get the current link count */
666 my_err
= stat( my_pathp
, &my_sb
);
668 printf( "stat call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
669 goto test_failed_exit
;
671 my_link_count
= my_sb
.st_nlink
;
673 /* check file size (should be 0) */
674 if ( my_sb
.st_size
!= 0 ) {
675 printf( "stat structure looks bogus for test file \"%s\" \n", my_pathp
);
676 printf( "st_size is not 0 \n" );
677 goto test_failed_exit
;
680 /* change file size */
681 my_fd
= open( my_pathp
, O_RDWR
, 0 );
683 printf( "open call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
684 printf( "\t file we attempted to open -> \"%s\" \n", my_pathp
);
685 goto test_failed_exit
;
687 my_result
= write( my_fd
, "kat", 3 );
689 if ( my_result
!= 3 ) {
690 if ( sizeof( ssize_t
) > sizeof( int ) ) {
691 printf( "write failed. should have written 3 bytes actually wrote - %ld \n", (long int) my_result
);
694 printf( "write failed. should have written 3 bytes actually wrote - %d \n", (int) my_result
);
696 goto test_failed_exit
;
701 /* now link another file to our test file and recheck link count */
702 my_err
= link( my_pathp
, my_path2p
);
704 printf( "link call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
705 goto test_failed_exit
;
707 my_err
= stat( my_pathp
, &my_sb
);
709 printf( "stat call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
710 goto test_failed_exit
;
712 if ( (my_link_count
+ 1) != my_sb
.st_nlink
) {
713 printf( "stat structure looks bogus for test file \"%s\" \n", my_pathp
);
714 printf( "incorrect st_nlink \n" );
715 goto test_failed_exit
;
718 /* check file size (should be 3) */
719 if ( my_sb
.st_size
!= 3 ) {
720 printf( "stat structure looks bogus for test file \"%s\" \n", my_pathp
);
721 printf( "st_size is not 3 \n" );
722 goto test_failed_exit
;
725 /* now make sure unlink works OK */
726 my_err
= unlink( my_path2p
);
728 printf( "unlink call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
729 goto test_failed_exit
;
731 my_err
= stat( my_pathp
, &my_sb
);
733 printf( "stat call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
734 goto test_failed_exit
;
736 if ( my_link_count
!= my_sb
.st_nlink
) {
737 printf( "stat structure looks bogus for test file \"%s\" \n", my_pathp
);
738 printf( "incorrect st_nlink \n" );
739 goto test_failed_exit
;
743 goto test_passed_exit
;
751 if ( my_pathp
!= NULL
) {
753 vm_deallocate(mach_task_self(), (vm_address_t
)my_pathp
, PATH_MAX
);
755 if ( my_path2p
!= NULL
) {
757 vm_deallocate(mach_task_self(), (vm_address_t
)my_path2p
, PATH_MAX
);
762 /* **************************************************************************************************************
763 * Test chdir and fchdir system calls.
764 * **************************************************************************************************************
766 int chdir_fchdir_test( void * the_argp
)
770 char * my_pathp
= NULL
;
771 char * my_file_namep
;
776 char *cwd
= getwd(NULL
); /* Save current working directory so we can restore later */
778 my_kr
= vm_allocate((vm_map_t
) mach_task_self(), (vm_address_t
*)&my_pathp
, PATH_MAX
, VM_FLAGS_ANYWHERE
);
779 if(my_kr
!= KERN_SUCCESS
){
780 printf( "vm_allocate failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
781 goto test_failed_exit
;
785 strcat( my_pathp
, &g_target_path
[0] );
786 strcat( my_pathp
, "/" );
788 /* create a test file */
789 my_err
= create_random_name( my_pathp
, 1 );
791 goto test_failed_exit
;
794 /* test by doing a stat on the test file using a full path and a partial path.
795 * get full path first.
797 my_err
= stat( my_pathp
, &my_sb
);
799 printf( "stat call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
800 goto test_failed_exit
;
803 /* now do the chdir to our test directory and then do the stat relative to that location */
804 my_err
= chdir( &g_target_path
[0] );
806 printf( "chdir call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
807 goto test_failed_exit
;
810 my_file_namep
= strrchr( my_pathp
, '/' );
812 my_err
= stat( my_file_namep
, &my_sb2
);
814 printf( "stat call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
815 goto test_failed_exit
;
818 /* both stat buffers should contain the same data since they should be referencing the same
821 if ( my_sb
.st_ino
!= my_sb2
.st_ino
|| my_sb
.st_size
!= my_sb2
.st_size
||
822 my_sb
.st_mtimespec
.tv_sec
!= my_sb2
.st_mtimespec
.tv_sec
||
823 my_sb
.st_mtimespec
.tv_nsec
!= my_sb2
.st_mtimespec
.tv_nsec
) {
824 printf( "chdir call appears to have failed. stat buffer contents do not match! \n" );
825 goto test_failed_exit
;
828 /* now change our current directory to "/" and use fchdir to get back to our test directory */
829 my_err
= chdir( "/" );
831 printf( "chdir call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
832 goto test_failed_exit
;
835 /* we should not find our test file at the root of the volume */
836 my_err
= stat( my_file_namep
, &my_sb2
);
838 printf( "chdir to root volume has failed \n" );
839 goto test_failed_exit
;
842 /* get a file descriptor to the test directory for use with fchdir */
843 my_fd
= open( &g_target_path
[0], O_RDONLY
, 0 );
845 printf( "open call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
846 printf( "\t we attempted to open -> \"%s\" \n", &g_target_path
[0] );
847 goto test_failed_exit
;
850 my_err
= fchdir( my_fd
);
851 if ( my_err
== -1 ) {
852 printf( "fchdir call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
853 goto test_failed_exit
;
856 my_err
= stat( my_file_namep
, &my_sb2
);
858 printf( "stat call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
859 goto test_failed_exit
;
862 /* both stat buffers should contain the same data since they should be referencing the same
865 if ( my_sb
.st_ino
!= my_sb2
.st_ino
|| my_sb
.st_size
!= my_sb2
.st_size
||
866 my_sb
.st_mtimespec
.tv_sec
!= my_sb2
.st_mtimespec
.tv_sec
||
867 my_sb
.st_mtimespec
.tv_nsec
!= my_sb2
.st_mtimespec
.tv_nsec
) {
868 printf( "chdir call appears to have failed. stat buffer contents do not match! \n" );
869 goto test_failed_exit
;
873 goto test_passed_exit
;
881 if ( my_pathp
!= NULL
) {
883 vm_deallocate(mach_task_self(), (vm_address_t
)my_pathp
, PATH_MAX
);
885 if ( chdir(cwd
) != 0) /* Changes back to original directory, don't screw up the env. */
890 /* **************************************************************************************************************
891 * Test access, chmod and fchmod system calls.
892 * **************************************************************************************************************
894 int access_chmod_fchmod_test( void * the_argp
)
898 char * my_pathp
= NULL
;
902 my_kr
= vm_allocate((vm_map_t
) mach_task_self(), (vm_address_t
*)&my_pathp
, PATH_MAX
, VM_FLAGS_ANYWHERE
);
903 if(my_kr
!= KERN_SUCCESS
){
904 printf( "vm_allocate failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
905 goto test_failed_exit
;
909 strcat( my_pathp
, &g_target_path
[0] );
910 strcat( my_pathp
, "/" );
912 /* create a test file */
913 my_err
= create_random_name( my_pathp
, 1 );
915 goto test_failed_exit
;
919 my_err
= chmod( my_pathp
, S_IRWXU
);
920 if ( my_err
== -1 ) {
921 printf( "chmod call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
922 goto test_failed_exit
;
925 my_err
= chmod( my_pathp
, (S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
) );
926 if ( my_err
== -1 ) {
927 printf( "chmod call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
928 goto test_failed_exit
;
931 /* test access - this should fail */
932 my_err
= access( my_pathp
, (X_OK
) );
934 printf( "access call should have failed, but did not. \n" );
935 goto test_failed_exit
;
937 else if ( my_err
== -1 ) {
941 /* special case when running as root - we get back EPERM when running as root */
943 #if !TARGET_OS_EMBEDDED
944 if ( ( tmp
== 0 && my_err
!= EPERM
) || (tmp
!= 0 && my_err
!= EACCES
) ) {
945 printf( "access failed with errno %d - %s. \n", my_err
, strerror( my_err
) );
946 goto test_failed_exit
;
949 if ( ( tmp
== 0 && my_err
!= EACCES
) || (tmp
!= 0 && my_err
!= EACCES
) ) {
950 printf( "access failed with errno %d - %s. \n", my_err
, strerror( my_err
) );
951 goto test_failed_exit
;
956 /* verify correct modes are set */
957 my_err
= stat( my_pathp
, &my_sb
);
959 printf( "stat call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
960 goto test_failed_exit
;
963 if ( (my_sb
.st_mode
& (S_IRWXO
| S_IXGRP
)) != 0 ||
964 (my_sb
.st_mode
& (S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
)) == 0 ) {
965 printf( "chmod call appears to have failed. stat shows incorrect values in st_mode! \n" );
966 goto test_failed_exit
;
970 my_fd
= open( my_pathp
, O_RDONLY
, 0 );
972 printf( "open call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
973 printf( "\t we attempted to open -> \"%s\" \n", &g_target_path
[0] );
974 goto test_failed_exit
;
977 my_err
= fchmod( my_fd
, S_IRWXU
);
978 if ( my_err
== -1 ) {
979 printf( "fchmod call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
980 goto test_failed_exit
;
983 my_err
= stat( my_pathp
, &my_sb
);
985 printf( "stat call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
986 goto test_failed_exit
;
989 /* verify correct modes are set */
990 if ( (my_sb
.st_mode
& (S_IRWXG
| S_IRWXO
)) != 0 ||
991 (my_sb
.st_mode
& (S_IRWXU
)) == 0 ) {
992 printf( "fchmod call appears to have failed. stat shows incorrect values in st_mode! \n" );
993 goto test_failed_exit
;
997 goto test_passed_exit
;
1005 if ( my_pathp
!= NULL
) {
1007 vm_deallocate(mach_task_self(), (vm_address_t
)my_pathp
, PATH_MAX
);
1012 /* **************************************************************************************************************
1013 * Test chown, fchown, lchown, lstat, readlink, symlink system calls.
1014 * **************************************************************************************************************
1016 int chown_fchown_lchown_lstat_symlink_test( void * the_argp
)
1018 #if !TARGET_OS_EMBEDDED
1019 int my_err
, my_group_count
, i
;
1021 char * my_pathp
= NULL
;
1022 char * my_link_pathp
= NULL
;
1024 gid_t my_orig_gid
, my_new_gid1
= 0, my_new_gid2
= 0;
1027 gid_t my_groups
[ NGROUPS_MAX
];
1028 char my_buffer
[ 64 ];
1029 kern_return_t my_kr
;
1031 my_kr
= vm_allocate((vm_map_t
) mach_task_self(), (vm_address_t
*)&my_pathp
, PATH_MAX
, VM_FLAGS_ANYWHERE
);
1032 if(my_kr
!= KERN_SUCCESS
){
1033 printf( "vm_allocate failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
1034 goto test_failed_exit
;
1038 strcat( my_pathp
, &g_target_path
[0] );
1039 strcat( my_pathp
, "/" );
1041 /* create a test file */
1042 my_err
= create_random_name( my_pathp
, 1 );
1043 if ( my_err
!= 0 ) {
1044 goto test_failed_exit
;
1047 my_kr
= vm_allocate((vm_map_t
) mach_task_self(), (vm_address_t
*)&my_link_pathp
, PATH_MAX
, VM_FLAGS_ANYWHERE
);
1048 if(my_kr
!= KERN_SUCCESS
){
1049 printf( "vm_allocate failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
1050 goto test_failed_exit
;
1053 *my_link_pathp
= 0x00;
1054 strcat( my_link_pathp
, &g_target_path
[0] );
1055 strcat( my_link_pathp
, "/" );
1057 /* get a test file name for the link */
1058 my_err
= create_random_name( my_link_pathp
, 0 );
1059 if ( my_err
!= 0 ) {
1060 goto test_failed_exit
;
1063 /* set up by getting a list of groups */
1064 my_group_count
= getgroups( NGROUPS_MAX
, &my_groups
[0] );
1066 if ( my_group_count
== -1 || my_group_count
< 1 ) {
1067 printf( "getgroups call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
1068 goto test_failed_exit
;
1071 my_err
= stat( my_pathp
, &my_sb
);
1072 if ( my_err
!= 0 ) {
1073 printf( "stat call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
1074 goto test_failed_exit
;
1077 /* now change group owner to something other than current value */
1078 my_orig_gid
= my_sb
.st_gid
;
1079 my_orig_uid
= my_sb
.st_uid
;
1081 for ( i
= 0; i
< my_group_count
; i
++ ) {
1082 if ( my_orig_gid
!= my_groups
[ i
] ) {
1083 if ( my_new_gid1
== 0 ) {
1084 my_new_gid1
= my_groups
[ i
];
1087 my_new_gid2
= my_groups
[ i
];
1092 if ( i
>= my_group_count
) {
1093 printf( "not enough groups to choose from. st_gid is the same as current groups! \n" );
1094 goto test_failed_exit
;
1097 my_err
= chown( my_pathp
, my_orig_uid
, my_new_gid1
);
1098 if ( my_err
!= 0 ) {
1099 printf( "chown call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
1100 goto test_failed_exit
;
1103 /* make sure the group owner was changed */
1104 my_err
= stat( my_pathp
, &my_sb
);
1105 if ( my_err
!= 0 ) {
1106 printf( "stat call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
1107 goto test_failed_exit
;
1109 if ( my_sb
.st_gid
== my_orig_gid
) {
1110 printf( "chown call failed. st_gid is not correct! \n" );
1111 goto test_failed_exit
;
1114 /* change group owner back using fchown */
1115 my_fd
= open( my_pathp
, O_RDWR
, 0 );
1116 if ( my_fd
== -1 ) {
1117 printf( "open call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
1118 printf( "\t we attempted to open -> \"%s\" \n", &g_target_path
[0] );
1119 goto test_failed_exit
;
1122 my_err
= fchown( my_fd
, my_orig_uid
, my_new_gid2
);
1123 if ( my_err
!= 0 ) {
1124 printf( "fchown call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
1125 goto test_failed_exit
;
1128 /* make sure the group owner was changed back to the original value */
1129 my_err
= stat( my_pathp
, &my_sb
);
1130 if ( my_err
!= 0 ) {
1131 printf( "stat call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
1132 goto test_failed_exit
;
1134 if ( my_sb
.st_gid
== my_new_gid1
) {
1135 printf( "fchown call failed. st_gid is not correct! \n" );
1136 goto test_failed_exit
;
1139 /* create a link file and test lchown */
1140 my_err
= symlink( my_pathp
, my_link_pathp
);
1141 if ( my_err
!= 0 ) {
1142 printf( "symlink call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
1143 goto test_failed_exit
;
1146 my_err
= lstat( my_link_pathp
, &my_sb
);
1147 if ( my_err
!= 0 ) {
1148 printf( "lstat call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
1149 goto test_failed_exit
;
1152 /* now change group owner to something other than current value */
1153 my_orig_gid
= my_sb
.st_gid
;
1154 my_orig_uid
= my_sb
.st_uid
;
1155 my_err
= lchown( my_link_pathp
, my_orig_uid
, my_new_gid1
);
1156 if ( my_err
!= 0 ) {
1157 printf( "lchown call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
1158 goto test_failed_exit
;
1161 /* make sure the group owner was changed to new value */
1162 my_err
= lstat( my_link_pathp
, &my_sb
);
1163 if ( my_err
!= 0 ) {
1164 printf( "lstat call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
1165 goto test_failed_exit
;
1167 if ( my_sb
.st_gid
== my_new_gid2
) {
1168 printf( "lchown call failed. st_gid is not correct! \n" );
1169 goto test_failed_exit
;
1172 /* make sure we can read the symlink file */
1173 my_result
= readlink( my_link_pathp
, &my_buffer
[0], sizeof(my_buffer
) );
1174 if ( my_result
== -1 ) {
1175 printf( "readlink call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
1176 goto test_failed_exit
;
1178 /* make sure we read some data */
1179 if ( my_result
< 1 ) {
1180 printf( "readlink failed to read any data. \n" );
1181 goto test_failed_exit
;
1185 goto test_passed_exit
;
1193 if ( my_pathp
!= NULL
) {
1195 vm_deallocate(mach_task_self(), (vm_address_t
)my_pathp
, PATH_MAX
);
1197 if ( my_link_pathp
!= NULL
) {
1198 unlink( my_link_pathp
);
1199 vm_deallocate(mach_task_self(), (vm_address_t
)my_link_pathp
, PATH_MAX
);
1203 printf( "\t--> Test not designed for EMBEDDED TARGET\n" );
1208 /* **************************************************************************************************************
1209 * Test fstatfs, getattrlist, getfsstat, statfs, getfsstat64, statfs64, fstatfs64 system calls.
1210 * **************************************************************************************************************
1214 struct vol_attr_buf
{
1217 u_int32_t io_blksize
;
1220 typedef struct vol_attr_buf vol_attr_buf
;
1222 int fs_stat_tests( void * the_argp
)
1224 int my_err
, my_count
, i
;
1225 int my_buffer_size
, my_buffer64_size
;
1230 struct attrlist my_attrlist
;
1231 vol_attr_buf my_attr_buf
;
1232 void * my_bufferp
= NULL
;
1233 struct statfs
* my_statfsp
;
1234 kern_return_t my_kr
;
1236 #if !TARGET_OS_EMBEDDED
1237 void * my_buffer64p
= NULL
;
1238 struct statfs64
* my_statfs64p
;
1240 my_buffer64_size
= (sizeof(struct statfs64
) * 10);
1242 my_kr
= vm_allocate((vm_map_t
) mach_task_self(),(vm_address_t
*) &my_buffer64p
, my_buffer64_size
, VM_FLAGS_ANYWHERE
);
1243 if(my_kr
!= KERN_SUCCESS
){
1244 printf( "vm_allocate failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
1245 goto test_failed_exit
;
1249 my_buffer_size
= (sizeof(struct statfs
) * 10);
1251 my_kr
= vm_allocate((vm_map_t
) mach_task_self(),(vm_address_t
*) &my_bufferp
, my_buffer_size
, VM_FLAGS_ANYWHERE
);
1252 if(my_kr
!= KERN_SUCCESS
){
1253 printf( "vm_allocate failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
1254 goto test_failed_exit
;
1257 my_statfsp
= (struct statfs
*) my_bufferp
;
1258 my_err
= statfs( "/", my_statfsp
);
1259 if ( my_err
== -1 ) {
1260 printf( "statfs call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
1261 goto test_failed_exit
;
1263 if ( memcmp( &my_statfsp
->f_fstypename
[0], "ufs", 3 ) == 0 ) {
1267 my_count
= getfsstat( (struct statfs
*)my_bufferp
, my_buffer_size
, MNT_NOWAIT
);
1268 if ( my_count
== -1 ) {
1269 printf( "getfsstat call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
1270 goto test_failed_exit
;
1273 /* validate results */
1274 my_statfsp
= (struct statfs
*) my_bufferp
;
1275 for ( i
= 0; i
< my_count
; i
++, my_statfsp
++ ) {
1276 if ( memcmp( &my_statfsp
->f_fstypename
[0], "hfs", 3 ) == 0 ||
1277 memcmp( &my_statfsp
->f_fstypename
[0], "ufs", 3 ) == 0 ||
1278 memcmp( &my_statfsp
->f_fstypename
[0], "devfs", 5 ) == 0 ||
1279 memcmp( &my_statfsp
->f_fstypename
[0], "volfs", 5 ) == 0 ) {
1280 /* found a valid entry */
1284 if ( i
>= my_count
) {
1285 printf( "getfsstat call failed. could not find valid f_fstypename! \n" );
1286 goto test_failed_exit
;
1289 #if !TARGET_OS_EMBEDDED
1290 /* now try statfs64 */
1291 my_statfs64p
= (struct statfs64
*) my_buffer64p
;
1292 my_err
= statfs64( "/", my_statfs64p
);
1293 if ( my_err
== -1 ) {
1294 printf( "statfs64 call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
1295 goto test_failed_exit
;
1297 if ( my_statfs64p
->f_fsid
.val
[0] != my_statfsp
->f_fsid
.val
[0] ||
1298 my_statfs64p
->f_fsid
.val
[1] != my_statfsp
->f_fsid
.val
[1] ) {
1299 printf( "statfs64 call failed. wrong f_fsid! \n" );
1300 goto test_failed_exit
;
1303 my_count
= getfsstat64( (struct statfs64
*)my_buffer64p
, my_buffer64_size
, MNT_NOWAIT
);
1304 if ( my_count
== -1 ) {
1305 printf( "getfsstat64 call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
1306 goto test_failed_exit
;
1309 /* validate results */
1310 my_statfs64p
= (struct statfs64
*) my_buffer64p
;
1311 for ( i
= 0; i
< my_count
; i
++, my_statfs64p
++ ) {
1312 if ( memcmp( &my_statfs64p
->f_fstypename
[0], "hfs", 3 ) == 0 ||
1313 memcmp( &my_statfs64p
->f_fstypename
[0], "ufs", 3 ) == 0 ||
1314 memcmp( &my_statfs64p
->f_fstypename
[0], "devfs", 5 ) == 0 ||
1315 memcmp( &my_statfs64p
->f_fstypename
[0], "volfs", 5 ) == 0 ) {
1316 /* found a valid entry */
1320 if ( i
>= my_count
) {
1321 printf( "getfsstat64 call failed. could not find valid f_fstypename! \n" );
1322 goto test_failed_exit
;
1326 /* set up to validate results via multiple sources. we use getattrlist to get volume
1327 * related attributes to verify against results from fstatfs and statfs - but only if
1328 * we are not targeting ufs volume since it doesn't support getattr calls
1330 if ( is_ufs
== 0 ) {
1331 memset( &my_attrlist
, 0, sizeof(my_attrlist
) );
1332 my_attrlist
.bitmapcount
= ATTR_BIT_MAP_COUNT
;
1333 my_attrlist
.volattr
= (ATTR_VOL_SIZE
| ATTR_VOL_IOBLOCKSIZE
);
1334 my_err
= getattrlist( "/", &my_attrlist
, &my_attr_buf
, sizeof(my_attr_buf
), 0 );
1335 if ( my_err
!= 0 ) {
1336 printf( "getattrlist call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
1337 goto test_failed_exit
;
1341 /* open kernel to use as test file for fstatfs */
1342 my_fd
= open( "/mach_kernel", O_RDONLY
, 0 );
1343 if ( my_fd
== -1 ) {
1344 printf( "open call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
1345 goto test_failed_exit
;
1348 #if !TARGET_OS_EMBEDDED
1349 /* testing fstatfs64 */
1350 my_statfs64p
= (struct statfs64
*) my_buffer64p
;
1351 my_err
= fstatfs64( my_fd
, my_statfs64p
);
1352 if ( my_err
== -1 ) {
1353 printf( "fstatfs64 call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
1354 goto test_failed_exit
;
1357 /* validate results - assumes we only boot from hfs or ufs */
1358 if ( !(memcmp( &my_statfs64p
->f_fstypename
[0], "hfs", 3 ) == 0 ||
1359 memcmp( &my_statfs64p
->f_fstypename
[0], "ufs", 3 ) == 0) ) {
1360 printf( "fstatfs64 call failed. could not find valid f_fstypename! \n" );
1361 goto test_failed_exit
;
1365 /* testing fstatfs */
1366 my_statfsp
= (struct statfs
*) my_bufferp
;
1367 my_err
= fstatfs( my_fd
, my_statfsp
);
1368 if ( my_err
== -1 ) {
1369 printf( "fstatfs call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
1370 goto test_failed_exit
;
1373 /* validate results */
1374 if ( !(memcmp( &my_statfsp
->f_fstypename
[0], "hfs", 3 ) == 0 ||
1375 memcmp( &my_statfsp
->f_fstypename
[0], "ufs", 3 ) == 0) ) {
1376 printf( "fstatfs call failed. could not find valid f_fstypename! \n" );
1377 goto test_failed_exit
;
1379 my_io_size
= my_statfsp
->f_iosize
;
1380 my_fsid
= my_statfsp
->f_fsid
;
1381 if ( is_ufs
== 0 && my_statfsp
->f_iosize
!= my_attr_buf
.io_blksize
) {
1382 printf( "fstatfs and getattrlist results do not match for volume block size \n" );
1383 goto test_failed_exit
;
1386 /* try again with statfs */
1387 my_err
= statfs( "/mach_kernel", my_statfsp
);
1388 if ( my_err
== -1 ) {
1389 printf( "statfs call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
1390 goto test_failed_exit
;
1393 /* validate results */
1394 if ( my_io_size
!= my_statfsp
->f_iosize
|| my_fsid
.val
[0] != my_statfsp
->f_fsid
.val
[0] ||
1395 my_fsid
.val
[1] != my_statfsp
->f_fsid
.val
[1] ) {
1396 printf( "statfs call failed. wrong f_iosize or f_fsid! \n" );
1397 goto test_failed_exit
;
1399 if ( is_ufs
== 0 && my_statfsp
->f_iosize
!= my_attr_buf
.io_blksize
) {
1400 printf( "statfs and getattrlist results do not match for volume block size \n" );
1401 goto test_failed_exit
;
1405 goto test_passed_exit
;
1413 if ( my_bufferp
!= NULL
) {
1414 vm_deallocate(mach_task_self(), (vm_address_t
)my_bufferp
, my_buffer_size
);
1416 #if !TARGET_OS_EMBEDDED
1417 if ( my_buffer64p
!= NULL
) {
1418 vm_deallocate(mach_task_self(), (vm_address_t
)my_buffer64p
, my_buffer64_size
);
1425 /* **************************************************************************************************************
1426 * Test getpid, getppid, and pipe system calls.
1427 * **************************************************************************************************************
1429 int getpid_getppid_pipe_test( void * the_argp
)
1431 int my_err
, my_status
;
1432 pid_t my_pid
, my_wait_pid
;
1434 int my_fildes
[2] = {-1, -1};
1435 off_t my_current_offset
;
1436 char my_pid_string
[64];
1438 my_err
= pipe( &my_fildes
[0] );
1439 if ( my_err
!= 0 ) {
1440 printf( "pipe call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
1441 goto test_failed_exit
;
1444 /* make sure we can't seek on a pipe */
1445 my_current_offset
= lseek( my_fildes
[0], 0, SEEK_CUR
);
1446 if ( my_current_offset
!= -1 ) {
1447 printf( "lseek on pipe should fail but did not \n" );
1448 goto test_failed_exit
;
1451 /* fork here and use pipe to communicate */
1453 if ( my_pid
== -1 ) {
1454 printf( "fork failed with errno %d - %s \n", errno
, strerror( errno
) );
1455 goto test_failed_exit
;
1457 else if ( my_pid
== 0 ) {
1459 unsigned long my_ppid
;
1462 close( my_fildes
[1] ); /* close write end of pipe */
1465 /* get the parent's pid using getppid and from the parent (using getpid in porent) */
1466 my_count
= read( my_fildes
[0], &my_buffer
[0], sizeof(my_buffer
) );
1467 if ( my_count
== -1 ) {
1468 printf( "read from pipe failed. got errno %d - %s. \n", errno
, strerror( errno
) );
1472 /* parent wrote (to our pipe) its pid as character string */
1473 my_ppid
= strtoul( &my_buffer
[0], NULL
, 10 );
1474 if ( my_ppid
== 0 ) {
1475 printf( "strtoul failed. got errno %d - %s. \n", errno
, strerror( errno
) );
1479 if ( getppid( ) != my_ppid
) {
1480 printf( "getppid failed. pid we got from parent does not match getppid result. \n" );
1486 /* parent process - get our pid using getpid and send it to child for verification */
1487 close( my_fildes
[0] ); /* close read end of pipe */
1490 sprintf( &my_pid_string
[0], "%d\n", getpid( ) );
1492 my_count
= write( my_fildes
[1], &my_pid_string
[0], sizeof(my_pid_string
) );
1493 if ( my_count
== -1 ) {
1494 printf( "write to pipe failed. got errno %d - %s. \n", errno
, strerror( errno
) );
1495 goto test_failed_exit
;
1498 /* wait for child to exit */
1499 my_wait_pid
= wait4( my_pid
, &my_status
, 0, NULL
);
1500 if ( my_wait_pid
== -1 ) {
1501 printf( "wait4 failed with errno %d - %s \n", errno
, strerror( errno
) );
1502 goto test_failed_exit
;
1505 /* wait4 should return our child's pid when it exits */
1506 if ( my_wait_pid
!= my_pid
) {
1507 printf( "wait4 did not return child pid - returned %d should be %d \n", my_wait_pid
, my_pid
);
1508 goto test_failed_exit
;
1511 if ( WIFEXITED( my_status
) && WEXITSTATUS( my_status
) != 0 ) {
1512 printf( "wait4 returned wrong exit status - 0x%02X \n", my_status
);
1513 goto test_failed_exit
;
1517 goto test_passed_exit
;
1523 if ( my_fildes
[0] != -1 )
1524 close( my_fildes
[0] );
1525 if ( my_fildes
[1] != -1 )
1526 close( my_fildes
[1] );
1531 /* **************************************************************************************************************
1532 * Test getauid, gettid, getuid, geteuid, issetugid, setaudit_addr, seteuid, settid, settid_with_pid, setuid system calls.
1533 * **************************************************************************************************************
1535 int uid_tests( void * the_argp
)
1537 int my_err
, my_status
;
1538 pid_t my_pid
, my_wait_pid
;
1540 if ( g_skip_setuid_tests
!= 0 ) {
1541 printf("\t skipping this test \n");
1543 goto test_passed_exit
;
1546 /* test issetugid - should return 1 when not root and 0 when root
1547 * Figuring out setugid will not work in single-user mode; skip
1548 * this test in that case.
1550 if (!g_is_single_user
) {
1551 my_err
= issetugid( );
1552 if ( getuid( ) == 0 ) {
1553 if ( my_err
== 1 ) {
1554 printf( "issetugid should return false \n" );
1555 goto test_failed_exit
;
1559 if ( my_err
== 0 ) {
1560 printf( "issetugid should return true \n" );
1561 goto test_failed_exit
;
1567 * fork here and do the setuid work in the child
1570 if ( my_pid
== -1 ) {
1571 printf( "fork failed with errno %d - %s \n", errno
, strerror( errno
) );
1572 goto test_failed_exit
;
1574 else if ( my_pid
== 0 ) {
1578 uid_t my_ruid
, my_euid
;
1579 uid_t my_uid
, my_temp_uid
;
1580 gid_t my_gid
, my_temp_gid
;
1581 auditinfo_addr_t my_aia
;
1583 my_ruid
= getuid( );
1584 my_euid
= geteuid( );
1585 if ( my_ruid
== my_euid
) {
1589 /* Test getauid, gettid, setaudit_addr, settid, settid_with_pid */
1590 /* get our current uid and gid for comparison later */
1594 my_err
= syscall( SYS_settid
, 4444, 5555 );
1595 //my_err = settid( 4444, 5555 );
1597 printf( "settid call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
1601 my_err
= syscall( SYS_gettid
, &my_temp_uid
, &my_temp_gid
);
1602 //my_err = gettid( &my_temp_uid, &my_temp_gid );
1604 printf( "gettid call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
1607 if (my_temp_uid
!= 4444) {
1608 printf("get / settid test failed - wrong uid was set - %d \n", my_temp_uid
);
1611 if (my_temp_gid
!= 5555) {
1612 printf("get / settid test failed - wrong gid was set - %d \n", my_temp_gid
);
1616 /* resume original identity */
1617 my_err
= syscall( SYS_settid
, KAUTH_UID_NONE
, KAUTH_GID_NONE
);
1618 //my_err = settid( KAUTH_UID_NONE, KAUTH_GID_NONE );
1620 printf( "settid revert - failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
1624 /* values should be returned to original settings */
1625 my_temp_uid
= getuid( );
1626 if (my_temp_uid
== 4444) {
1627 printf("test failed - wrong uid was set - %d \n", my_temp_uid
);
1630 my_temp_gid
= getgid( );
1631 if (my_temp_gid
== 5555) {
1632 printf("test failed - wrong gid was set - %d \n", my_temp_gid
);
1637 * Assume the identity of our parent.
1639 my_err
= syscall( SYS_settid_with_pid
, getppid( ), 1 );
1640 //my_err = settid_with_pid, my_target_pid, 1 );
1642 printf( "settid_with_pid assume - failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
1647 * Resume our identity.
1649 my_err
= syscall( SYS_settid_with_pid
, 0, 0 );
1650 //my_err = settid_with_pid( my_target_pid, 0 );
1652 printf( "settid_with_pid resume - failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
1657 * test to make sure setaudit_addr doesn't cause audit info to get lost from
1660 bzero( &my_aia
, sizeof(my_aia
) );
1661 my_aia
.ai_auid
= 442344;
1662 my_aia
.ai_asid
= AU_ASSIGN_ASID
;
1663 my_aia
.ai_termid
.at_type
= AU_IPv4
;
1664 my_err
= setaudit_addr( &my_aia
, sizeof(my_aia
) );
1666 printf( "setaudit_addr - failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
1671 my_err
= getaudit_addr( &my_aia
, sizeof(my_aia
) );
1673 printf( "getaudit_addr - failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
1676 //printf("new audit ID is %d \n", my_aia.ai_auid);
1678 if (my_aia
.ai_auid
!= 442344) {
1679 printf("test failed - wrong audit ID was set - %d \n", my_aia
.ai_auid
);
1683 /* change real uid and effective uid to current euid */
1684 my_err
= setuid( my_euid
);
1685 if ( my_err
== -1 ) {
1686 printf( "setuid call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
1689 if ( getuid( ) != my_euid
) {
1690 printf( "setuid call failed to set the real uid \n" );
1694 /* change effective uid to current euid - really a NOP */
1695 my_err
= seteuid( my_euid
);
1696 if ( my_err
== -1 ) {
1697 printf( "seteuid call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
1700 if ( geteuid( ) != my_euid
) {
1701 printf( "seteuid call failed to set the original euid \n" );
1705 /* change real uid and effective uid to original real uid */
1706 my_err
= setuid( my_ruid
);
1707 if ( my_err
== -1 ) {
1708 printf( "setuid call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
1711 if ( getuid( ) != my_ruid
) {
1712 printf( "setuid call failed to set the real uid \n" );
1721 * wait for child to exit
1723 my_wait_pid
= wait4( my_pid
, &my_status
, 0, NULL
);
1724 if ( my_wait_pid
== -1 ) {
1725 printf( "wait4 failed with errno %d - %s \n", errno
, strerror( errno
) );
1726 goto test_failed_exit
;
1729 /* wait4 should return our child's pid when it exits */
1730 if ( my_wait_pid
!= my_pid
) {
1731 printf( "wait4 did not return child pid - returned %d should be %d \n", my_wait_pid
, my_pid
);
1732 goto test_failed_exit
;
1735 if ( WIFEXITED( my_status
) && WEXITSTATUS( my_status
) != 0 ) {
1736 printf( "wait4 returned wrong exit status - 0x%02X \n", my_status
);
1737 goto test_failed_exit
;
1741 goto test_passed_exit
;
1750 /* **************************************************************************************************************
1751 * Test mknod, sync system calls.
1752 * **************************************************************************************************************
1754 int mknod_sync_test( void * the_argp
)
1757 char * my_pathp
= NULL
;
1758 kern_return_t my_kr
;
1760 if ( g_skip_setuid_tests
!= 0 ) {
1761 printf("\t skipping this test \n");
1763 goto test_passed_exit
;
1766 my_kr
= vm_allocate((vm_map_t
) mach_task_self(), (vm_address_t
*)&my_pathp
, PATH_MAX
, VM_FLAGS_ANYWHERE
);
1767 if(my_kr
!= KERN_SUCCESS
){
1768 printf( "vm_allocate failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
1769 goto test_failed_exit
;
1773 strcat( my_pathp
, "/dev/" );
1775 /* get a unique name for our test file */
1776 my_err
= create_random_name( my_pathp
, 0 );
1777 if ( my_err
!= 0 ) {
1778 goto test_failed_exit
;
1781 my_err
= mknod( my_pathp
, (S_IFCHR
| S_IRWXU
), 0 );
1782 if ( my_err
== -1 ) {
1783 printf( "mknod failed with errno %d - %s \n", errno
, strerror( errno
) );
1784 printf( "path \"%s\" \n", my_pathp
);
1785 goto test_failed_exit
;
1788 /* not really sure what to do with sync call test */
1791 goto test_passed_exit
;
1797 if ( my_pathp
!= NULL
) {
1799 vm_deallocate(mach_task_self(), (vm_address_t
)my_pathp
, PATH_MAX
);
1804 /* **************************************************************************************************************
1805 * Test chflags, fchflags system calls.
1806 * **************************************************************************************************************
1808 int chflags_fchflags_test( void * the_argp
)
1813 char * my_pathp
= NULL
;
1815 kern_return_t my_kr
;
1817 my_kr
= vm_allocate((vm_map_t
) mach_task_self(), (vm_address_t
*)&my_pathp
, PATH_MAX
, VM_FLAGS_ANYWHERE
);
1818 if(my_kr
!= KERN_SUCCESS
){
1819 printf( "vm_allocate failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
1820 goto test_failed_exit
;
1824 strcat( my_pathp
, &g_target_path
[0] );
1825 strcat( my_pathp
, "/" );
1827 /* create a test file */
1828 my_err
= create_random_name( my_pathp
, 1 );
1829 if ( my_err
!= 0 ) {
1830 goto test_failed_exit
;
1833 /* make test file unchangable */
1834 my_err
= stat( my_pathp
, &my_sb
);
1835 if ( my_err
!= 0 ) {
1836 printf( "stat call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
1837 goto test_failed_exit
;
1840 my_flags
= (my_sb
.st_flags
| UF_IMMUTABLE
);
1841 my_err
= chflags( my_pathp
, my_flags
);
1842 if ( my_err
!= 0 ) {
1843 printf( "chflags call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
1844 goto test_failed_exit
;
1847 /* should fail with EPERM since we cannot change the file now */
1848 my_fd
= open( my_pathp
, O_RDWR
, 0 );
1849 if ( my_fd
== -1 && errno
!= EPERM
) {
1850 printf( "open call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
1851 printf( "open failed with wrong error - should be EPERM \n" );
1852 goto test_failed_exit
;
1855 /* this open should work OK */
1856 my_fd
= open( my_pathp
, O_RDONLY
, 0 );
1857 if ( my_fd
== -1 ) {
1858 printf( "open call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
1859 goto test_failed_exit
;
1862 my_err
= stat( my_pathp
, &my_sb
);
1863 if ( my_err
!= 0 ) {
1864 printf( "stat call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
1865 goto test_failed_exit
;
1868 my_flags
= (my_sb
.st_flags
& ~UF_IMMUTABLE
);
1869 my_err
= fchflags( my_fd
, my_flags
);
1870 if ( my_err
!= 0 ) {
1871 printf( "chflags call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
1872 goto test_failed_exit
;
1878 /* should now work */
1879 my_fd
= open( my_pathp
, O_RDWR
, 0 );
1880 if ( my_fd
== -1 ) {
1881 printf( "open call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
1882 goto test_failed_exit
;
1886 goto test_passed_exit
;
1894 if ( my_pathp
!= NULL
) {
1896 vm_deallocate(mach_task_self(), (vm_address_t
)my_pathp
, PATH_MAX
);
1902 /* **************************************************************************************************************
1903 * Test kill, vfork, execve system calls.
1904 * **************************************************************************************************************
1906 /* There are many new exec() situations to test now that 64-bit is in. These extra tests are in response to
1907 * rdar://4606399 and rdar://4607285. It should cover every permutation of the following variables.
1909 * - Current Process "Bitness": 64 or 32
1910 * - exec()'ed process "bitness": 64 or 32
1911 * (if 64 bit, size of page zero:) (4GB or 4KB)
1912 * - Parent Process "Bitness": 64 or 32
1914 * Test to make sure certain inheritance properties of fork()'ed children
1915 * are correctly set.
1916 * 1. 64 bit process forking() 64-bit child, child execing() 64-bit file (4GB pagezero)
1917 * 2. 64 bit process forking() 64-bit child, child execing() 64-bit file (4KB pagezero)
1918 * 3. 64 bit process forking() 64-bit child, child execing() 32-bit file
1919 * 4. 32 bit process forking() 32-bit child, child execing() 32-bit file
1920 * 5. 32 bit process forking() 32-bit child, child execing() 64 bit file (4GB pagezero)
1921 * 6. 32 bit process forking() 32-bit child, child execing() 64 bit file (4KB pagezero)
1926 int execve_kill_vfork_test( void * the_argp
)
1928 int my_err
, my_status
;
1929 pid_t my_pid
, my_wait_pid
;
1930 char * errmsg
= NULL
;
1931 char * argvs
[2] = {"", NULL
};
1932 int bits
= get_bits(); /* Gets actual processor bit-ness. */
1934 if (bits
!= 32 && bits
!= 64) {
1935 printf("Determination of processor bit-ness failed, get_bits() returned %d.\n", get_bits());
1939 if (get_architecture() == -1) {
1940 errmsg
= "get_architecture() could not determine the CPU architecture.\n";
1941 goto test_failed_exit
;
1944 if (get_architecture() == INTEL
) {
1945 int ppc_fail_flag
= 0;
1948 if (stat("/usr/libexec/oah/translate", &sb
))
1951 if (bits
== 64 && sizeof(long) == 8) {
1953 * Running on x86_64 hardware and running in 64-bit mode.
1954 * Check cases 1, 2, 3 and fork a child to check 4, 5, 6.
1956 errmsg
= "execve failed: from x86_64 forking and exec()ing 64-bit x86_64 process w/ 4G pagezero.\n";
1957 argvs
[0] = "sleep-x86_64-4G";
1958 if (do_execve_test("helpers/sleep-x86_64-4G", argvs
, NULL
, 1)) goto test_failed_exit
;
1960 errmsg
= "execve failed: from x86_64 forking and exec()ing 64-bit x86_64 process w/ 4K Pagezero.\n";
1961 argvs
[0] = "sleep-x86_64-4K";
1962 if (do_execve_test("helpers/sleep-x86_64-4K", argvs
, NULL
, 1)) goto test_failed_exit
;
1964 errmsg
= "execve failed: from x64_64 forking and exec()ing 32-bit i386 process.\n";
1965 argvs
[0] = "sleep-i386";
1966 if (do_execve_test("helpers/sleep-i386", argvs
, NULL
, 1)) goto test_failed_exit
;
1968 /* Fork off a helper process and load a 32-bit program in it to test 32->64 bit exec(). */
1969 errmsg
= "execve failed to exec the helper process.\n";
1970 argvs
[0] = "launch-i386";
1971 if (do_execve_test("helpers/launch-i386", argvs
, NULL
, 1) != 0) goto test_failed_exit
;
1973 /* Test posix_spawn for i386, x86_64, and ppc (should succeed) */
1975 if (do_spawn_test(CPU_TYPE_I386
, 0))
1976 goto test_failed_exit
;
1977 if (do_spawn_test(CPU_TYPE_X86_64
, 0))
1978 goto test_failed_exit
;
1980 * Note: rosetta is no go in single-user mode
1982 if (!g_is_single_user
) {
1983 if (do_spawn_test(CPU_TYPE_POWERPC
, ppc_fail_flag
))
1984 goto test_failed_exit
;
1987 else if (bits
== 64 && sizeof(long) == 4) {
1989 * Running on x86_64 hardware, but actually running in 32-bit mode.
1990 * Check cases 4, 5, 6 and fork a child to check 1, 2, 3.
1992 errmsg
= "execve failed: from i386 forking and exec()ing i386 process.\n";
1993 argvs
[0] = "sleep-i386";
1994 if (do_execve_test("helpers/sleep-i386", argvs
, NULL
, 0)) goto test_failed_exit
;
1996 errmsg
= "execve failed: from i386 forking and exec()ing x86_64 process w/ 4G pagezero.\n";
1997 argvs
[0] = "sleep-x86_64-4G";
1998 if (do_execve_test("helpers/sleep-x86_64-4G", argvs
, NULL
, 0)) goto test_failed_exit
;
2000 errmsg
= "execve failed: from i386 forking and exec()ing x86_64 process w/ 4K pagezero.\n";
2001 argvs
[0] = "sleep-x86_64-4K";
2002 if (do_execve_test("helpers/sleep-x86_64-4K", argvs
, NULL
, 0)) goto test_failed_exit
;
2004 /* Fork off a helper process and load a 64-bit program in it to test 64->32 bit exec(). */
2005 errmsg
= "execve failed to exec the helper process.\n";
2006 argvs
[0] = "launch-x86_64";
2007 if (do_execve_test("helpers/launch-x86_64", argvs
, NULL
, 1) != 0) goto test_failed_exit
;
2009 /* Test posix_spawn for i386, x86_64, and ppc (should succeed) */
2011 if (do_spawn_test(CPU_TYPE_I386
, 0))
2012 goto test_failed_exit
;
2013 if (do_spawn_test(CPU_TYPE_X86_64
, 0))
2014 goto test_failed_exit
;
2016 * Note: rosetta is no go in single-user mode
2018 if (!g_is_single_user
) {
2019 if (do_spawn_test(CPU_TYPE_POWERPC
, ppc_fail_flag
))
2020 goto test_failed_exit
;
2023 else if (bits
== 32) {
2024 /* Running on i386 hardware. Check cases 4. */
2025 errmsg
= "execve failed: from i386 forking and exec()ing 32-bit i386 process.\n";
2026 argvs
[0] = "sleep-i386";
2027 if (do_execve_test("helpers/sleep-i386", argvs
, NULL
, 1)) goto test_failed_exit
;
2029 /* Test posix_spawn for x86_64 (should fail), i386, and ppc (should succeed) */
2031 if (do_spawn_test(CPU_TYPE_X86_64
, 1))
2032 goto test_failed_exit
;
2033 if (do_spawn_test(CPU_TYPE_I386
, 0))
2034 goto test_failed_exit
;
2036 * Note: rosetta is no go in single-user mode
2038 if (!g_is_single_user
) {
2039 if (do_spawn_test(CPU_TYPE_POWERPC
, ppc_fail_flag
))
2040 goto test_failed_exit
;
2044 else if (get_architecture() == POWERPC
) {
2045 if (bits
== 64 && sizeof(long) == 8) {
2047 * Running on PPC64 hardware and running in 64-bit mode.
2048 * No longer supported on SnowLeopard.
2050 errmsg
= "runnning ppc64 on snowleopard";
2051 goto test_failed_exit
;
2053 else if (bits
== 64 && sizeof(long) == 4) {
2055 * Running as PPC on PPC64 hardware or under Rosetta on x86_64 hardware.
2056 * Check cases 4, 5, 6 and fork a child to check 1, 2, 3.
2058 errmsg
= "execve failed: from ppc forking and exec()ing ppc process.\n";
2059 argvs
[0] = "sleep-ppc32";
2060 if (do_execve_test("helpers/sleep-ppc32", argvs
, NULL
, 0)) goto test_failed_exit
;
2062 /* Test posix_spawn for i386 and ppc */
2064 if (do_spawn_test(CPU_TYPE_I386
, (g_is_under_rosetta
? 0 : 1)))
2065 goto test_failed_exit
;
2066 if (do_spawn_test(CPU_TYPE_POWERPC
, 0))
2067 goto test_failed_exit
;
2069 else if (bits
== 32) {
2070 /* Running on ppc hardware. Check cases 4. */
2071 errmsg
= "execve failed: from ppc forking and exec()ing 32 bit ppc process.\n";
2072 argvs
[0] = "sleep-ppc32";
2073 if (do_execve_test("helpers/sleep-ppc32", argvs
, NULL
, 1)) goto test_failed_exit
;
2074 /* Test posix_spawn for i386 (should fail) and ppc (should succeed) */
2076 /* when under Rosetta, this process is CPU_TYPE_POWERPC, but the system should be able to run CPU_TYPE_I386 binaries */
2077 if (do_spawn_test(CPU_TYPE_I386
, (g_is_under_rosetta
? 0 : 1)))
2078 goto test_failed_exit
;
2079 if (do_spawn_test(CPU_TYPE_POWERPC
, 0))
2080 goto test_failed_exit
;
2083 else if(get_architecture() == ARM
) {
2086 /* Running on arm hardware. Check cases 2. */
2087 errmsg
= "execve failed: from arm forking and exec()ing 32-bit arm process.\n";
2088 argvs
[0] = "sleep-arm";
2089 if (do_execve_test("helpers/sleep-arm", argvs
, NULL
, 1))
2090 goto test_failed_exit
;
2092 /* Test posix_spawn for arm (should succeed) */
2094 if (do_spawn_test(CPU_TYPE_ARM
, 0))
2095 goto test_failed_exit
;
2099 /* Just in case someone decides we need more architectures in the future */
2100 printf("get_architecture() returned unknown architecture");
2108 printf("%s", errmsg
);
2113 /* **************************************************************************************************************
2114 * Test getegid, getgid, getgroups, setegid, setgid, setgroups system calls.
2115 * **************************************************************************************************************
2117 int groups_test( void * the_argp
)
2119 #if !TARGET_OS_EMBEDDED
2121 int my_group_count
, my_orig_group_count
;
2123 gid_t my_effective_gid
;
2124 gid_t my_removed_gid
;
2126 gid_t my_groups
[ NGROUPS_MAX
];
2128 if ( g_skip_setuid_tests
!= 0 ) {
2129 printf("\t skipping this test \n");
2131 goto test_passed_exit
;
2134 my_real_gid
= getgid( );
2135 my_effective_gid
= getegid( );
2137 /* start by getting list of groups the current user belongs to */
2138 my_orig_group_count
= getgroups( NGROUPS_MAX
, &my_groups
[0] );
2140 if ( my_orig_group_count
== -1 || my_orig_group_count
< 1 ) {
2141 printf( "getgroups call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
2142 goto test_failed_exit
;
2145 /* make sure real and effective gids are correct */
2146 for ( i
= 0; i
< my_orig_group_count
; i
++ ) {
2147 if ( my_groups
[i
] == my_real_gid
)
2150 if ( i
>= my_orig_group_count
) {
2151 printf( "getgid or getgroups call failed. could not find real gid in list of groups. \n" );
2152 goto test_failed_exit
;
2154 for ( i
= 0; i
< my_orig_group_count
; i
++ ) {
2155 if ( my_groups
[i
] == my_effective_gid
)
2158 if ( i
>= my_orig_group_count
) {
2159 printf( "getegid or getgroups call failed. could not find effective gid in list of groups. \n" );
2160 goto test_failed_exit
;
2163 /* remove the last group */
2164 my_removed_gid
= my_groups
[ (my_orig_group_count
- 1) ];
2165 my_err
= setgroups( (my_orig_group_count
- 1), &my_groups
[0] );
2166 if ( my_err
== -1 ) {
2167 printf( "setgroups call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
2168 goto test_failed_exit
;
2171 my_group_count
= getgroups( NGROUPS_MAX
, &my_groups
[0] );
2173 if ( my_group_count
== -1 || my_group_count
< 1 ) {
2174 printf( "getgroups call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
2175 goto test_failed_exit
;
2178 /* make sure setgroups dropped one */
2179 if ( my_orig_group_count
<= my_group_count
) {
2180 printf( "setgroups call failed. current group count is too high. \n" );
2181 goto test_failed_exit
;
2184 /* now put removed gid back */
2185 my_groups
[ (my_orig_group_count
- 1) ] = my_removed_gid
;
2186 my_err
= setgroups( my_orig_group_count
, &my_groups
[0] );
2187 if ( my_err
== -1 ) {
2188 printf( "setgroups call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
2189 goto test_failed_exit
;
2192 /* find a group to change real and effective gid to then do it */
2194 for ( i
= 0; i
< my_orig_group_count
; i
++ ) {
2195 if ( my_groups
[i
] == my_effective_gid
|| my_groups
[i
] == my_real_gid
)
2197 my_new_gid
= my_groups
[i
];
2200 if ( my_new_gid
== -1 ) {
2201 printf( "could not find a gid to switch to. \n" );
2202 goto test_failed_exit
;
2206 my_err
= setegid( my_new_gid
);
2207 if ( my_err
== -1 ) {
2208 printf( "setegid call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
2209 goto test_failed_exit
;
2211 /* verify it changed */
2212 if ( getegid( ) != my_new_gid
) {
2213 printf( "setegid failed to change the effective gid. \n" );
2214 goto test_failed_exit
;
2216 /* change it back to original value */
2217 my_err
= setegid( my_effective_gid
);
2218 if ( my_err
== -1 ) {
2219 printf( "setegid call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
2220 goto test_failed_exit
;
2224 my_err
= setgid( my_new_gid
);
2225 if ( my_err
== -1 ) {
2226 printf( "setgid call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
2227 goto test_failed_exit
;
2229 /* verify it changed */
2230 if ( getgid( ) != my_new_gid
) {
2231 printf( "setgid failed to change the real gid. \n" );
2232 goto test_failed_exit
;
2234 /* change it back to original value */
2235 my_err
= setgid( my_real_gid
);
2236 if ( my_err
== -1 ) {
2237 printf( "setegid call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
2238 goto test_failed_exit
;
2242 goto test_passed_exit
;
2250 printf( "\t--> Test not designed for EMBEDDED TARGET\n" );
2256 /* **************************************************************************************************************
2257 * Test dup, dup2, getdtablesize system calls.
2258 * **************************************************************************************************************
2260 int dup_test( void * the_argp
)
2265 int my_table_size
, my_loop_counter
= 0;
2266 char * my_pathp
= NULL
;
2269 kern_return_t my_kr
;
2271 my_kr
= vm_allocate((vm_map_t
) mach_task_self(), (vm_address_t
*)&my_pathp
, PATH_MAX
, VM_FLAGS_ANYWHERE
);
2272 if(my_kr
!= KERN_SUCCESS
){
2273 printf( "vm_allocate failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
2274 goto test_failed_exit
;
2278 strcat( my_pathp
, &g_target_path
[0] );
2279 strcat( my_pathp
, "/" );
2281 /* create a test file */
2282 my_err
= create_random_name( my_pathp
, 1 );
2283 if ( my_err
!= 0 ) {
2284 goto test_failed_exit
;
2287 /* test dup, dup2, getdtablesize */
2288 my_table_size
= getdtablesize( );
2289 if ( my_table_size
< 20 ) {
2290 printf( "getdtablesize should return at least 20, returned %d \n", my_table_size
);
2291 goto test_failed_exit
;
2294 my_fd
= open( my_pathp
, O_RDWR
, 0 );
2295 if ( my_fd
== -1 ) {
2296 printf( "open call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
2297 goto test_failed_exit
;
2300 my_newfd
= dup( my_fd
);
2301 if ( my_newfd
== -1 ) {
2302 printf( "dup call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
2303 goto test_failed_exit
;
2307 /* now write somne data to the orginal and new fd */
2308 /* make sure test file is empty */
2309 my_err
= ftruncate( my_fd
, 0 );
2310 if ( my_err
== -1 ) {
2311 printf( "ftruncate call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
2312 goto test_failed_exit
;
2315 lseek( my_fd
, 0, SEEK_SET
);
2316 my_count
= write( my_fd
, "aa", 2 );
2317 if ( my_count
== -1 ) {
2318 printf( "write call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
2319 goto test_failed_exit
;
2322 my_count
= write( my_newfd
, "xx", 2 );
2323 if ( my_count
== -1 ) {
2324 printf( "write call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
2325 goto test_failed_exit
;
2328 /* now read it back and make sure data is correct */
2329 lseek( my_fd
, 0, SEEK_SET
);
2330 my_count
= read( my_fd
, &my_buffer
[0], sizeof(my_buffer
) );
2331 if ( my_count
== -1 ) {
2332 printf( "read call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
2333 goto test_failed_exit
;
2335 if ( my_buffer
[0] != 'a' || my_buffer
[1] != 'a' || my_buffer
[2] != 'x' || my_buffer
[3] != 'x' ) {
2336 printf( "wrong data in test file. \n" );
2337 goto test_failed_exit
;
2340 bzero( &my_buffer
[0], sizeof(my_buffer
) );
2341 lseek( my_newfd
, 0, SEEK_SET
);
2342 my_count
= read( my_newfd
, &my_buffer
[0], sizeof(my_buffer
) );
2343 if ( my_count
== -1 ) {
2344 printf( "read call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
2345 goto test_failed_exit
;
2347 if ( my_buffer
[0] != 'a' || my_buffer
[1] != 'a' || my_buffer
[2] != 'x' || my_buffer
[3] != 'x' ) {
2348 printf( "wrong data in test file. \n" );
2349 goto test_failed_exit
;
2352 /* we do the above tests twice - once for dup and once for dup2 */
2353 if ( my_loop_counter
< 1 ) {
2357 my_err
= dup2( my_fd
, my_newfd
);
2358 if ( my_err
== -1 ) {
2359 printf( "dup2 call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
2360 goto test_failed_exit
;
2367 goto test_passed_exit
;
2375 if ( my_newfd
!= -1 )
2377 if ( my_pathp
!= NULL
) {
2379 vm_deallocate(mach_task_self(), (vm_address_t
)my_pathp
, PATH_MAX
);
2385 /* **************************************************************************************************************
2386 * Test getrusage system call.
2387 * **************************************************************************************************************
2389 int getrusage_test( void * the_argp
)
2392 struct rusage my_rusage
;
2394 my_err
= getrusage( RUSAGE_SELF
, &my_rusage
);
2395 if ( my_err
== -1 ) {
2396 printf( "getrusage failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
2397 goto test_failed_exit
;
2400 /* do a sanity check on the getrusage results */
2401 if ( my_rusage
.ru_msgrcv
> 1000 || my_rusage
.ru_msgrcv
< 0 ) {
2402 printf( "getrusage seems to report wrong data - ru_msgrcv looks odd. \n" );
2403 goto test_failed_exit
;
2405 if ( my_rusage
.ru_nsignals
> 1000 || my_rusage
.ru_nsignals
< 0 ) {
2406 printf( "getrusage seems to report wrong data - ru_nsignals looks odd. \n" );
2407 goto test_failed_exit
;
2411 goto test_passed_exit
;
2420 /* **************************************************************************************************************
2421 * Test getitimer, setitimer, sigaction, sigpending, sigprocmask, sigsuspend, sigwait system calls.
2422 * **************************************************************************************************************
2425 int alarm_global
= 0;
2426 void test_alarm_handler( int the_arg
);
2427 void test_alarm_handler( int the_arg
)
2430 //printf( "test_alarm_handler - got here \n" );
2431 if ( the_arg
== 0 ) {
2436 void test_signal_handler( int the_arg
);
2437 void test_signal_handler( int the_arg
)
2439 //printf( "test_signal_handler - got here \n" );
2440 if ( the_arg
== 0 ) {
2445 int signals_test( void * the_argp
)
2447 int my_err
, my_status
;
2449 char * my_pathp
= NULL
;
2450 pid_t my_pid
, my_wait_pid
;
2451 kern_return_t my_kr
;
2453 my_kr
= vm_allocate((vm_map_t
) mach_task_self(), (vm_address_t
*)&my_pathp
, PATH_MAX
, VM_FLAGS_ANYWHERE
);
2454 if(my_kr
!= KERN_SUCCESS
){
2455 printf( "vm_allocate failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
2456 goto test_failed_exit
;
2460 strcat( my_pathp
, &g_target_path
[0] );
2461 strcat( my_pathp
, "/" );
2463 /* create a test file */
2464 my_err
= create_random_name( my_pathp
, 1 );
2465 if ( my_err
!= 0 ) {
2466 goto test_failed_exit
;
2470 * spin off a child process that we will use for signal related testing.
2473 if ( my_pid
== -1 ) {
2474 printf( "fork failed with errno %d - %s \n", errno
, strerror( errno
) );
2475 goto test_failed_exit
;
2477 if ( my_pid
== 0 ) {
2479 * child process - test signal related system calls.
2484 struct sigaction my_sigaction
;
2485 #ifdef MAC_OS_X_VERSION_10_5
2486 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
2487 /* If this is Leopard. To allow compiling for Inca x86_64 this definition cannot
2488 * be included. But it is needed to compile on Leopard.
2490 struct __darwin_sigaltstack my_sigaltstack
;
2493 struct sigaltstack my_sigaltstack
;
2495 struct itimerval my_timer
;
2498 /* test getting the current signal stack context */
2499 my_err
= sigaltstack( NULL
, &my_sigaltstack
);
2500 if ( my_err
== -1 ) {
2501 printf( "sigaction failed with errno %d - %s \n", errno
, strerror( errno
) );
2504 if ( (my_sigaltstack
.ss_flags
& SS_DISABLE
) == 0 ) {
2505 printf( "sigaction must have failed - SS_DISABLE is cleared \n" );
2509 /* set up to catch SIGUSR1 */
2510 my_sigaction
.sa_handler
= test_signal_handler
;
2511 my_sigaction
.sa_flags
= SA_RESTART
;
2512 my_sigaction
.sa_mask
= 0;
2514 my_err
= sigaction( SIGUSR1
, &my_sigaction
, NULL
);
2515 if ( my_err
== -1 ) {
2516 printf( "sigaction failed with errno %d - %s \n", errno
, strerror( errno
) );
2520 /* now suspend until signal SIGUSR1 is sent */
2521 sigemptyset( &my_sigset
);
2522 my_err
= sigsuspend( &my_sigset
);
2523 if ( my_err
== -1 ) {
2524 if ( errno
!= EINTR
) {
2525 printf( "sigsuspend should have returned with errno EINTR \n" );
2531 sigemptyset( &my_sigset
);
2532 sigaddset( &my_sigset
, SIGUSR1
);
2533 if ( sigismember( &my_sigset
, SIGUSR1
) == 0 ) {
2534 printf( "sigaddset call failed to add SIGUSR1 to signal set \n" );
2537 my_err
= sigprocmask( SIG_BLOCK
, &my_sigset
, NULL
);
2538 if ( my_err
== -1 ) {
2539 printf( "sigprocmask failed with errno %d - %s \n", errno
, strerror( errno
) );
2543 /* make sure we are blocking SIGUSR1 */
2544 sigemptyset( &my_sigset
);
2545 my_err
= sigprocmask( 0, NULL
, &my_sigset
);
2546 if ( my_err
== -1 ) {
2547 printf( "sigprocmask failed with errno %d - %s \n", errno
, strerror( errno
) );
2550 if ( sigismember( &my_sigset
, SIGUSR1
) == 0 ) {
2551 printf( "sigaddset call failed to add SIGUSR1 to signal set \n" );
2555 /* our parent will send a 2nd SIGUSR1 signal which we should now see getting
2558 sigemptyset( &my_sigset
);
2559 sigaddset( &my_sigset
, SIGUSR1
);
2560 my_err
= sigwait( &my_sigset
, &my_signal
);
2561 if ( my_err
== -1 ) {
2562 printf( "sigwait failed with errno %d - %s \n", errno
, strerror( errno
) );
2565 //printf( "%s - %d - signal 0x%02X %d \n", __FUNCTION__, __LINE__, my_signal, my_signal );
2566 if ( my_signal
!= SIGUSR1
) {
2567 printf( "sigwait failed to catch a pending SIGUSR1 signal. \n" );
2571 /* now unblock SIGUSR1 */
2572 sigfillset( &my_sigset
);
2573 sigdelset( &my_sigset
, SIGUSR1
);
2574 my_err
= sigprocmask( SIG_UNBLOCK
, &my_sigset
, NULL
);
2575 if ( my_err
== -1 ) {
2576 printf( "sigprocmask failed with errno %d - %s \n", errno
, strerror( errno
) );
2579 if ( sigismember( &my_sigset
, SIGUSR1
) != 0 ) {
2580 printf( "sigprocmask call failed to unblock SIGUSR1 \n" );
2584 /* test get / setitimer */
2585 timerclear( &my_timer
.it_interval
);
2586 timerclear( &my_timer
.it_value
);
2587 my_err
= setitimer( ITIMER_VIRTUAL
, &my_timer
, NULL
);
2588 if ( my_err
== -1 ) {
2589 printf( "setitimer - ITIMER_VIRTUAL - failed with errno %d - %s \n", errno
, strerror( errno
) );
2592 my_err
= setitimer( ITIMER_PROF
, &my_timer
, NULL
);
2593 if ( my_err
== -1 ) {
2594 printf( "setitimer - ITIMER_PROF - failed with errno %d - %s \n", errno
, strerror( errno
) );
2598 /* set up to catch SIGALRM */
2600 my_sigaction
.sa_handler
= test_alarm_handler
;
2601 my_sigaction
.sa_flags
= SA_RESTART
;
2602 my_sigaction
.sa_mask
= 0;
2604 my_err
= sigaction( SIGALRM
, &my_sigaction
, NULL
);
2605 if ( my_err
== -1 ) {
2606 printf( "sigaction - SIGALRM - failed with errno %d - %s \n", errno
, strerror( errno
) );
2610 /* set timer for half a second */
2611 my_timer
.it_value
.tv_usec
= (1000000 / 2);
2612 my_err
= setitimer( ITIMER_REAL
, &my_timer
, NULL
);
2613 if ( my_err
== -1 ) {
2614 printf( "setitimer - ITIMER_REAL - failed with errno %d - %s \n", errno
, strerror( errno
) );
2618 /* now suspend until signal SIGALRM is sent */
2619 sigfillset( &my_sigset
);
2620 sigdelset( &my_sigset
, SIGALRM
);
2621 my_err
= sigsuspend( &my_sigset
);
2622 if ( my_err
== -1 ) {
2623 if ( errno
!= EINTR
) {
2624 printf( "sigsuspend should have returned with errno EINTR \n" );
2628 if ( alarm_global
!= 4 ) {
2629 printf( "setitimer test failed - did not catch SIGALRM \n" );
2633 /* make sure ITIMER_REAL is now clear */
2634 my_timer
.it_value
.tv_sec
= 44;
2635 my_timer
.it_value
.tv_usec
= 44;
2636 my_err
= getitimer( ITIMER_REAL
, &my_timer
);
2637 if ( my_err
== -1 ) {
2638 printf( "getitimer - ITIMER_REAL - failed with errno %d - %s \n", errno
, strerror( errno
) );
2641 if ( timerisset( &my_timer
.it_value
) || timerisset( &my_timer
.it_interval
) ) {
2642 printf( "ITIMER_REAL is set, but should not be \n" );
2650 * parent process - let child set up to suspend then signal it with SIGUSR1
2653 my_err
= kill( my_pid
, SIGUSR1
);
2654 if ( my_err
== -1 ) {
2655 printf( "kill call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
2656 goto test_failed_exit
;
2659 /* send 2nd signal to suspended child - which should be blocking SIGUSR1 signals */
2661 my_err
= kill( my_pid
, SIGUSR1
);
2662 if ( my_err
== -1 ) {
2663 printf( "kill call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
2664 goto test_failed_exit
;
2667 /* wait for child to exit */
2668 my_wait_pid
= wait4( my_pid
, &my_status
, 0, NULL
);
2669 if ( my_wait_pid
== -1 ) {
2670 printf( "wait4 failed with errno %d - %s \n", errno
, strerror( errno
) );
2671 goto test_failed_exit
;
2674 if ( WIFEXITED( my_status
) && WEXITSTATUS( my_status
) != 0 ) {
2675 goto test_failed_exit
;
2679 goto test_passed_exit
;
2687 if ( my_pathp
!= NULL
) {
2689 vm_deallocate(mach_task_self(), (vm_address_t
)my_pathp
, PATH_MAX
);
2694 /* **************************************************************************************************************
2695 * Test getlogin, setlogin system calls.
2696 * **************************************************************************************************************
2698 int getlogin_setlogin_test( void * the_argp
)
2700 int my_err
, my_status
;
2701 pid_t my_pid
, my_wait_pid
;
2702 kern_return_t my_kr
;
2704 if ( g_skip_setuid_tests
!= 0 ) {
2705 printf("\t skipping this test \n");
2707 goto test_passed_exit
;
2711 * spin off a child process that we will use for testing.
2714 if ( my_pid
== -1 ) {
2715 printf( "fork failed with errno %d - %s \n", errno
, strerror( errno
) );
2716 goto test_failed_exit
;
2718 if ( my_pid
== 0 ) {
2720 * child process - do getlogin and setlogin testing.
2722 char * my_namep
= NULL
;
2724 char * my_new_namep
= NULL
;
2726 my_namep
= getlogin( );
2727 if ( my_namep
== NULL
) {
2728 printf( "getlogin returned NULL name pointer \n" );
2733 my_len
= strlen( my_namep
) + 4;
2735 my_kr
= vm_allocate((vm_map_t
) mach_task_self(), (vm_address_t
*)&my_new_namep
, my_len
, VM_FLAGS_ANYWHERE
);
2736 if(my_kr
!= KERN_SUCCESS
){
2737 printf( "vm_allocate failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
2742 bzero( (void *)my_new_namep
, my_len
);
2744 strcat( my_new_namep
, my_namep
);
2745 strcat( my_new_namep
, "2" );
2749 my_err
= setlogin( my_new_namep
);
2750 if ( my_err
== -1 ) {
2751 printf( "When setting new login name, setlogin failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
2756 /* make sure we set new name */
2757 my_namep
= getlogin( );
2758 if ( my_namep
== NULL
) {
2759 printf( "getlogin returned NULL name pointer \n" );
2764 if ( memcmp( my_namep
, my_new_namep
, strlen( my_new_namep
) ) != 0 ) {
2765 printf( "setlogin failed to set the new name \n" );
2770 /* reset to original name */
2771 my_len
= strlen ( my_namep
);
2772 my_namep
[ my_len
- 1 ] = '\0';
2774 my_err
= setlogin( my_namep
);
2775 if ( my_err
== -1 ) {
2776 printf( "When resetting login name, setlogin failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
2784 if ( my_new_namep
!= NULL
) {
2785 vm_deallocate(mach_task_self(), (vm_address_t
)my_new_namep
, my_len
);
2791 * wait for child to exit
2793 my_wait_pid
= wait4( my_pid
, &my_status
, 0, NULL
);
2794 if ( my_wait_pid
== -1 ) {
2795 printf( "wait4 failed with errno %d - %s \n", errno
, strerror( errno
) );
2796 goto test_failed_exit
;
2799 if ( WIFEXITED( my_status
) && WEXITSTATUS( my_status
) != 0 ) {
2800 goto test_failed_exit
;
2803 goto test_passed_exit
;
2812 /* **************************************************************************************************************
2813 * Test acct system call.
2814 * **************************************************************************************************************
2816 int acct_test( void * the_argp
)
2818 int my_err
, my_status
;
2820 char * my_pathp
= NULL
;
2821 struct acct
* my_acctp
;
2822 pid_t my_pid
, my_wait_pid
;
2824 char my_buffer
[ (sizeof(struct acct
) + 32) ];
2825 kern_return_t my_kr
;
2827 if ( g_skip_setuid_tests
!= 0 ) {
2828 printf("\t skipping this test \n");
2830 goto test_passed_exit
;
2833 my_kr
= vm_allocate((vm_map_t
) mach_task_self(), (vm_address_t
*)&my_pathp
, PATH_MAX
, VM_FLAGS_ANYWHERE
);
2834 if(my_kr
!= KERN_SUCCESS
){
2835 printf( "vm_allocate failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
2836 goto test_failed_exit
;
2840 strcat( my_pathp
, &g_target_path
[0] );
2841 strcat( my_pathp
, "/" );
2843 /* create a test file */
2844 my_err
= create_random_name( my_pathp
, 1 );
2845 if ( my_err
!= 0 ) {
2846 goto test_failed_exit
;
2849 /* enable process accounting */
2850 my_err
= acct( my_pathp
);
2851 if ( my_err
== -1 ) {
2852 printf( "acct failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
2853 goto test_failed_exit
;
2857 * spin off a child process that we will use for testing.
2860 if ( my_pid
== -1 ) {
2861 printf( "fork failed with errno %d - %s \n", errno
, strerror( errno
) );
2862 goto test_failed_exit
;
2864 if ( my_pid
== 0 ) {
2865 char *argv
[2]; /* supply valid argv array to execv() */
2866 argv
[0] = "/usr/bin/true";
2870 * child process - do a little work then exit.
2872 my_err
= execv( argv
[0], argv
);
2877 * wait for child to exit
2879 my_wait_pid
= wait4( my_pid
, &my_status
, 0, NULL
);
2880 if ( my_wait_pid
== -1 ) {
2881 printf( "wait4 failed with errno %d - %s \n", errno
, strerror( errno
) );
2882 goto test_failed_exit
;
2885 if ( WIFEXITED( my_status
) && WEXITSTATUS( my_status
) != 0 ) {
2886 printf("unexpected child exit status for accounting test load: %d\n", WEXITSTATUS( my_status
));
2887 goto test_failed_exit
;
2890 /* disable process accounting */
2891 my_err
= acct( NULL
);
2892 if ( my_err
== -1 ) {
2893 printf( "acct failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
2894 goto test_failed_exit
;
2897 /* now verify that there is accounting info in the log file */
2898 my_fd
= open( my_pathp
, O_RDONLY
, 0 );
2899 if ( my_fd
== -1 ) {
2900 printf( "open call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
2901 goto test_failed_exit
;
2904 lseek( my_fd
, 0, SEEK_SET
);
2905 bzero( (void *)&my_buffer
[0], sizeof(my_buffer
) );
2906 my_count
= read( my_fd
, &my_buffer
[0], sizeof(struct acct
) );
2907 if ( my_count
== -1 ) {
2908 printf( "read call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
2909 goto test_failed_exit
;
2912 my_acctp
= (struct acct
*) &my_buffer
[0];
2914 /* first letters in ac_comm should match the name of the executable */
2915 if ( getuid( ) != my_acctp
->ac_uid
|| getgid( ) != my_acctp
->ac_gid
||
2916 my_acctp
->ac_comm
[0] != 't' || my_acctp
->ac_comm
[1] != 'r' ) {
2917 if (g_is_under_rosetta
) {
2918 // on x86 systems, data written by kernel to accounting info file is little endian;
2919 // but Rosetta processes expects it to be big endian; so swap the uid for our test
2920 if ( getuid( ) != OSSwapInt32(my_acctp
->ac_uid
) ||
2921 getgid( ) != OSSwapInt32(my_acctp
->ac_gid
) ||
2922 my_acctp
->ac_comm
[0] != 't' ||
2923 my_acctp
->ac_comm
[1] != 'r' ) {
2924 printf( "accounting data does not look correct under Rosetta:\n" );
2925 printf( "------------------------\n" );
2926 printf( "my_acctp->ac_uid = %lu (should be: %lu)\n",
2927 (unsigned long) OSSwapInt32( my_acctp
->ac_uid
), (unsigned long) getuid() );
2928 printf( "my_acctp->ac_gid = %lu (should be: %lu)\n",
2929 (unsigned long) OSSwapInt32( my_acctp
->ac_gid
), (unsigned long) getgid() );
2931 print_acct_debug_strings(my_acctp
->ac_comm
);
2934 // is cool under Rosetta
2936 goto test_passed_exit
;
2940 printf( "accounting data does not look correct:\n" );
2941 printf( "------------------------\n" );
2942 printf( "my_acctp->ac_uid = %lu (should be: %lu)\n", (unsigned long) my_acctp
->ac_uid
, (unsigned long) getuid() );
2943 printf( "my_acctp->ac_gid = %lu (should be: %lu)\n", (unsigned long) my_acctp
->ac_gid
, (unsigned long) getgid() );
2945 print_acct_debug_strings(my_acctp
->ac_comm
);
2948 goto test_failed_exit
;
2951 goto test_passed_exit
;
2959 if ( my_pathp
!= NULL
) {
2961 vm_deallocate(mach_task_self(), (vm_address_t
)my_pathp
, PATH_MAX
);
2966 void print_acct_debug_strings( char * my_ac_comm
)
2968 char my_cmd_str
[11]; /* sizeof(acct_cmd) + 1 for '\0' if acct_cmd is bogus */
2969 char my_hex_str
[128];
2972 my_hex_str
[0] = '\0';
2973 for(i
= 0; i
< 10; i
++)
2975 sprintf( my_hex_str
, "%s \'0x%x\' ", my_hex_str
, my_ac_comm
[i
]);
2978 memccpy(my_cmd_str
, my_ac_comm
, '\0', 10);
2979 my_cmd_str
[10] = '\0'; /* In case ac_comm was bogus */
2982 printf( "my_acctp->ac_comm = \"%s\" (should begin with: \"tr\")\n", my_cmd_str
);
2983 printf( "my_acctp->ac_comm = \"%s\"\n", my_hex_str
);
2984 printf( "------------------------\n" );
2988 /* **************************************************************************************************************
2989 * Test ioctl system calls.
2990 * **************************************************************************************************************
2992 int ioctl_test( void * the_argp
)
2994 int my_err
, my_result
;
2996 struct statfs
* my_infop
;
2999 long long my_block_count
;
3000 char my_name
[ 128 ];
3002 my_result
= getmntinfo( &my_infop
, MNT_NOWAIT
);
3003 if ( my_result
< 1 ) {
3004 printf( "getmntinfo failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
3005 goto test_failed_exit
;
3008 /* make this a raw device */
3009 strcpy( &my_name
[0], &my_infop
->f_mntfromname
[0] );
3010 if ( (my_ptr
= strrchr( &my_name
[0], '/' )) != 0 ) {
3011 if ( my_ptr
[1] != 'r' ) {
3012 my_ptr
[ strlen( my_ptr
) ] = 0x00;
3013 memmove( &my_ptr
[2], &my_ptr
[1], (strlen( &my_ptr
[1] ) + 1) );
3018 my_fd
= open(&my_name
[0], O_RDONLY
);
3019 if ( my_fd
== -1 ) {
3020 printf( "open call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
3021 goto test_failed_exit
;
3024 /* obtain the size of the media (in blocks) */
3025 my_err
= ioctl( my_fd
, DKIOCGETBLOCKCOUNT
, &my_block_count
);
3026 if ( my_err
== -1 ) {
3027 printf( "ioctl DKIOCGETBLOCKCOUNT failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
3028 goto test_failed_exit
;
3031 /* obtain the block size of the media */
3032 my_err
= ioctl( my_fd
, DKIOCGETBLOCKSIZE
, &my_blksize
);
3033 if ( my_err
== -1 ) {
3034 printf( "ioctl DKIOCGETBLOCKSIZE failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
3035 goto test_failed_exit
;
3037 //printf( "my_block_count %qd my_blksize %d \n", my_block_count, my_blksize );
3039 /* make sure the returned data looks somewhat valid */
3040 if ( my_blksize
< 0 || my_blksize
> (1024 * 1000) ) {
3041 printf( "ioctl appears to have returned incorrect block size data \n" );
3042 goto test_failed_exit
;
3046 goto test_passed_exit
;
3057 /* **************************************************************************************************************
3058 * Test mkdir, rmdir, umask system calls.
3059 * **************************************************************************************************************
3061 int mkdir_rmdir_umask_test( void * the_argp
)
3066 char * my_pathp
= NULL
;
3067 mode_t my_orig_mask
;
3069 kern_return_t my_kr
;
3071 my_kr
= vm_allocate((vm_map_t
) mach_task_self(), (vm_address_t
*)&my_pathp
, PATH_MAX
, VM_FLAGS_ANYWHERE
);
3072 if(my_kr
!= KERN_SUCCESS
){
3073 printf( "vm_allocate failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
3074 goto test_failed_exit
;
3078 strcat( my_pathp
, &g_target_path
[0] );
3079 strcat( my_pathp
, "/" );
3081 /* get a unique name to use with mkdir */
3082 my_err
= create_random_name( my_pathp
, 0 );
3083 if ( my_err
!= 0 ) {
3084 printf( "create_random_name failed with error %d\n", my_err
);
3085 goto test_failed_exit
;
3088 /* set umask to clear WX for other and group and clear X for user */
3089 my_orig_mask
= umask( (S_IXUSR
| S_IWGRP
| S_IXGRP
| S_IWOTH
| S_IXOTH
) );
3092 /* create a directory with RWX for user, group, other (which should be limited by umask) */
3093 my_err
= mkdir( my_pathp
, (S_IRWXU
| S_IRWXG
| S_IRWXO
) );
3094 if ( my_err
== -1 ) {
3095 printf( "mkdir failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
3096 goto test_failed_exit
;
3099 /* verify results - (S_IXUSR | S_IWGRP | S_IXGRP | S_IWOTH | S_IXOTH) should be clear*/
3100 my_err
= stat( my_pathp
, &my_sb
);
3101 if ( my_err
!= 0 ) {
3102 printf( "stat call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
3103 goto test_failed_exit
;
3105 if ( (my_sb
.st_mode
& (S_IXUSR
| S_IWGRP
| S_IXGRP
| S_IWOTH
| S_IXOTH
)) != 0 ) {
3106 printf( "umask did not limit modes as it should have \n" );
3107 goto test_failed_exit
;
3110 /* get rid of our test directory */
3111 my_err
= rmdir( my_pathp
);
3112 if ( my_err
== -1 ) {
3113 printf( "rmdir failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
3114 goto test_failed_exit
;
3117 goto test_passed_exit
;
3125 if ( my_pathp
!= NULL
) {
3127 vm_deallocate(mach_task_self(), (vm_address_t
)my_pathp
, PATH_MAX
);
3129 if ( did_umask
!= 0 ) {
3130 umask( my_orig_mask
);
3136 /* **************************************************************************************************************
3137 * Test chroot system call.
3138 * **************************************************************************************************************
3140 int chroot_test( void * the_argp
)
3142 int my_err
, my_status
;
3143 pid_t my_pid
, my_wait_pid
;
3144 char * my_pathp
= NULL
;
3145 kern_return_t my_kr
;
3147 if ( g_skip_setuid_tests
!= 0 ) {
3148 printf("\t skipping this test \n");
3150 goto test_passed_exit
;
3153 my_kr
= vm_allocate((vm_map_t
) mach_task_self(), (vm_address_t
*)&my_pathp
, PATH_MAX
, VM_FLAGS_ANYWHERE
);
3154 if(my_kr
!= KERN_SUCCESS
){
3155 printf( "vm_allocate failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
3156 goto test_failed_exit
;
3160 strcat( my_pathp
, &g_target_path
[0] );
3161 strcat( my_pathp
, "/" );
3163 /* get a unique name for our test directory */
3164 my_err
= create_random_name( my_pathp
, 0 );
3165 if ( my_err
!= 0 ) {
3166 goto test_failed_exit
;
3169 /* create a test directory */
3170 my_err
= mkdir( my_pathp
, (S_IRWXU
| S_IRWXG
| S_IRWXO
) );
3171 if ( my_err
== -1 ) {
3172 printf( "mkdir failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
3173 goto test_failed_exit
;
3177 * spin off a child process that we will use for testing.
3180 if ( my_pid
== -1 ) {
3181 printf( "fork failed with errno %d - %s \n", errno
, strerror( errno
) );
3182 goto test_failed_exit
;
3184 if ( my_pid
== 0 ) {
3186 * child process - do getlogin and setlogin testing.
3190 /* change our root to our new test directory */
3191 my_err
= chroot( my_pathp
);
3192 if ( my_err
!= 0 ) {
3193 printf( "chroot failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
3197 /* verify root directory is now an empty directory */
3198 my_err
= stat( "/", &my_sb
);
3199 if ( my_err
!= 0 ) {
3200 printf( "stat call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
3203 if ( my_sb
.st_nlink
> 2 ) {
3204 printf( "root dir should be emnpty! \n" );
3211 * wait for child to exit
3213 my_wait_pid
= wait4( my_pid
, &my_status
, 0, NULL
);
3214 if ( my_wait_pid
== -1 ) {
3215 printf( "wait4 failed with errno %d - %s \n", errno
, strerror( errno
) );
3216 goto test_failed_exit
;
3219 if ( WIFEXITED( my_status
) && WEXITSTATUS( my_status
) != 0 ) {
3220 printf( "bad exit status\n" );
3221 goto test_failed_exit
;
3225 goto test_passed_exit
;
3231 if ( my_pathp
!= NULL
) {
3232 my_err
= rmdir( my_pathp
);
3233 if ( my_err
!= 0 ) {
3234 printf( "rmdir failed with error %d - \"%s\" path %p\n", errno
, strerror( errno
), my_pathp
);
3236 vm_deallocate(mach_task_self(), (vm_address_t
)my_pathp
, PATH_MAX
);
3241 /* **************************************************************************************************************
3242 * Test getpgrp, getpgid, getsid, setpgid, setpgrp, setsid system calls.
3243 * **************************************************************************************************************
3245 int process_group_test( void * the_argp
)
3247 int my_err
= 0, i
= 0;
3248 pid_t my_session_id
, my_pid
, my_process_group
;
3250 /* get current session ID, pgid, and pid */
3251 my_session_id
= getsid( 0 );
3252 if ( my_session_id
== -1 ) {
3253 printf( "getsid call failed with error %d - \"%s\" \n",
3254 errno
, strerror( errno
) );
3255 goto test_failed_exit
;
3259 my_process_group
= getpgrp( );
3261 /* test getpgrp and getpgid - they should return the same results when 0 is passed to getpgid */
3262 if ( my_process_group
!= getpgid( 0 ) ) {
3263 printf( "getpgrp and getpgid did not return the same process group ID \n" );
3264 printf( "getpgid: %d, my_process_group: %d\n", getpgid( 0 ), my_process_group
);
3265 goto test_failed_exit
;
3268 if ( my_pid
== my_process_group
) {
3269 /* we are process group leader */
3271 if ( my_err
== 0 || errno
!= EPERM
) {
3272 printf( "setsid call should have failed with EPERM\n" );
3273 goto test_failed_exit
;
3276 /* we are not process group leader: try creating new session */
3278 if ( my_err
== -1 ) {
3279 printf( "setsid call failed with error %d - \"%s\" \n",
3280 errno
, strerror( errno
) );
3281 goto test_failed_exit
;
3284 if ( my_process_group
== getpgid( 0 ) ) {
3285 printf( "process group was not reset \n" );
3286 goto test_failed_exit
;
3290 /* find an unused process group ID */
3291 for ( i
= 10000; i
< 1000000; i
++ ) {
3292 my_process_group
= getpgid( i
);
3293 if ( my_process_group
== -1 ) {
3298 /* this should fail */
3299 my_err
= setpgid( 0, my_process_group
);
3300 if ( my_err
!= -1 ) {
3301 printf( "setpgid should have failed, but did not \n" );
3302 goto test_failed_exit
;
3306 goto test_passed_exit
;
3315 /* **************************************************************************************************************
3316 * Test fcntl system calls.
3317 * **************************************************************************************************************
3319 int fcntl_test( void * the_argp
)
3321 int my_err
, my_result
, my_tmep
;
3323 char * my_pathp
= NULL
;
3324 kern_return_t my_kr
;
3326 my_kr
= vm_allocate((vm_map_t
) mach_task_self(), (vm_address_t
*)&my_pathp
, PATH_MAX
, VM_FLAGS_ANYWHERE
);
3327 if(my_kr
!= KERN_SUCCESS
){
3328 printf( "vm_allocate failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
3329 goto test_failed_exit
;
3333 strcat( my_pathp
, &g_target_path
[0] );
3334 strcat( my_pathp
, "/" );
3336 /* create a test file */
3337 my_err
= create_random_name( my_pathp
, 1 );
3338 if ( my_err
!= 0 ) {
3339 goto test_failed_exit
;
3342 /* open our test file and use fcntl to get / set file descriptor flags */
3343 my_fd
= open( my_pathp
, O_RDONLY
, 0 );
3344 if ( my_fd
== -1 ) {
3345 printf( "open call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
3346 goto test_failed_exit
;
3349 my_result
= fcntl( my_fd
, F_GETFD
, 0 );
3350 if ( my_result
== -1 ) {
3351 printf( "fcntl - F_GETFD - failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
3352 goto test_failed_exit
;
3355 my_tmep
= (my_result
& FD_CLOEXEC
);
3357 /* FD_CLOEXEC is on, let's turn it off */
3358 my_result
= fcntl( my_fd
, F_SETFD
, 0 );
3361 /* FD_CLOEXEC is off, let's turn it on */
3362 my_result
= fcntl( my_fd
, F_SETFD
, 1 );
3364 if ( my_result
== -1 ) {
3365 printf( "fcntl - F_SETFD - failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
3366 goto test_failed_exit
;
3369 /* now check to see if it is set correctly */
3370 my_result
= fcntl( my_fd
, F_GETFD
, 0 );
3371 if ( my_result
== -1 ) {
3372 printf( "fcntl - F_GETFD - failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
3373 goto test_failed_exit
;
3375 if ( my_tmep
== (my_result
& 0x01) ) {
3376 printf( "fcntl - F_SETFD failed to set FD_CLOEXEC correctly!!! \n" );
3377 goto test_failed_exit
;
3381 goto test_passed_exit
;
3389 if ( my_pathp
!= NULL
) {
3391 vm_deallocate(mach_task_self(), (vm_address_t
)my_pathp
, PATH_MAX
);
3396 /* **************************************************************************************************************
3397 * Test getpriority, setpriority system calls.
3398 * **************************************************************************************************************
3400 int getpriority_setpriority_test( void * the_argp
)
3404 int my_new_priority
;
3406 /* getpriority returns scheduling priority so -1 is a valid value */
3408 my_priority
= getpriority( PRIO_PROCESS
, 0 );
3409 if ( my_priority
== -1 && errno
!= 0 ) {
3410 printf( "getpriority - failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
3411 goto test_failed_exit
;
3414 /* change scheduling priority */
3415 my_new_priority
= (my_priority
== PRIO_MIN
) ? (my_priority
+ 10) : (PRIO_MIN
);
3416 my_err
= setpriority( PRIO_PROCESS
, 0, my_new_priority
);
3417 if ( my_err
== -1 ) {
3418 printf( "setpriority - failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
3419 goto test_failed_exit
;
3424 my_priority
= getpriority( PRIO_PROCESS
, 0 );
3425 if ( my_priority
== -1 && errno
!= 0 ) {
3426 printf( "getpriority - failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
3427 goto test_failed_exit
;
3430 if ( my_priority
!= my_new_priority
) {
3431 printf( "setpriority - failed to set correct scheduling priority \n" );
3432 goto test_failed_exit
;
3435 /* reset scheduling priority */
3436 my_err
= setpriority( PRIO_PROCESS
, 0, 0 );
3437 if ( my_err
== -1 ) {
3438 printf( "setpriority - failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
3439 goto test_failed_exit
;
3443 goto test_passed_exit
;
3452 /* **************************************************************************************************************
3453 * Test futimes, gettimeofday, settimeofday, utimes system calls.
3454 * **************************************************************************************************************
3456 int time_tests( void * the_argp
)
3460 char * my_pathp
= NULL
;
3461 struct timeval my_orig_time
;
3462 struct timeval my_temp_time
;
3463 struct timeval my_utimes
[4];
3464 struct timezone my_tz
;
3466 kern_return_t my_kr
;
3468 if ( g_skip_setuid_tests
!= 0 ) {
3469 printf( "\t skipping this test \n" );
3471 goto test_passed_exit
;
3474 my_kr
= vm_allocate((vm_map_t
) mach_task_self(), (vm_address_t
*)&my_pathp
, PATH_MAX
, VM_FLAGS_ANYWHERE
);
3475 if(my_kr
!= KERN_SUCCESS
){
3476 printf( "vm_allocate failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
3477 goto test_failed_exit
;
3481 strcat( my_pathp
, &g_target_path
[0] );
3482 strcat( my_pathp
, "/" );
3484 /* create a test file */
3485 my_err
= create_random_name( my_pathp
, 1 );
3486 if ( my_err
!= 0 ) {
3487 goto test_failed_exit
;
3490 my_err
= gettimeofday( &my_orig_time
, &my_tz
);
3491 if ( my_err
== -1 ) {
3492 printf( "gettimeofday - failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
3493 goto test_failed_exit
;
3495 //printf( "tv_sec %d tv_usec %ld \n", my_orig_time.tv_sec, my_orig_time.tv_usec );
3497 my_temp_time
= my_orig_time
;
3498 my_temp_time
.tv_sec
-= 60;
3499 my_err
= settimeofday( &my_temp_time
, NULL
);
3500 if ( my_err
== -1 ) {
3501 printf( "settimeofday - failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
3502 goto test_failed_exit
;
3505 my_err
= gettimeofday( &my_temp_time
, NULL
);
3506 if ( my_err
== -1 ) {
3507 printf( "gettimeofday - failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
3508 goto test_failed_exit
;
3510 //printf( "tv_sec %d tv_usec %ld \n", my_temp_time.tv_sec, my_temp_time.tv_usec );
3511 if ( my_orig_time
.tv_sec
<= my_temp_time
.tv_sec
) {
3512 printf( "settimeofday did not set correct time \n" );
3513 goto test_failed_exit
;
3516 /* set time back to original value plus 1 second */
3517 my_temp_time
= my_orig_time
;
3518 my_temp_time
.tv_sec
+= 1;
3519 my_err
= settimeofday( &my_temp_time
, NULL
);
3520 if ( my_err
== -1 ) {
3521 printf( "settimeofday - failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
3522 goto test_failed_exit
;
3525 /* test utimes and futimes - get current access and mod times then change them */
3526 my_err
= stat( my_pathp
, &my_sb
);
3527 if ( my_err
!= 0 ) {
3528 printf( "stat - failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
3529 goto test_failed_exit
;
3531 TIMESPEC_TO_TIMEVAL( &my_utimes
[0], &my_sb
.st_atimespec
);
3532 TIMESPEC_TO_TIMEVAL( &my_utimes
[1], &my_sb
.st_mtimespec
);
3533 my_utimes
[0].tv_sec
-= 120; /* make access time 2 minutes older */
3534 my_utimes
[1].tv_sec
-= 120; /* make mod time 2 minutes older */
3536 my_err
= utimes( my_pathp
, &my_utimes
[0] );
3537 if ( my_err
== -1 ) {
3538 printf( "utimes - failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
3539 goto test_failed_exit
;
3542 /* make sure the correct times are set */
3543 my_err
= stat( my_pathp
, &my_sb
);
3544 if ( my_err
!= 0 ) {
3545 printf( "stat - failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
3546 goto test_failed_exit
;
3548 TIMESPEC_TO_TIMEVAL( &my_utimes
[2], &my_sb
.st_atimespec
);
3549 TIMESPEC_TO_TIMEVAL( &my_utimes
[3], &my_sb
.st_mtimespec
);
3550 if ( my_utimes
[0].tv_sec
!= my_utimes
[2].tv_sec
||
3551 my_utimes
[1].tv_sec
!= my_utimes
[3].tv_sec
) {
3552 printf( "utimes failed to set access and mod times \n" );
3553 goto test_failed_exit
;
3556 my_fd
= open( my_pathp
, O_RDWR
, 0 );
3557 if ( my_fd
== -1 ) {
3558 printf( "open call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
3559 goto test_failed_exit
;
3562 my_utimes
[0].tv_sec
-= 120; /* make access time 2 minutes older */
3563 my_utimes
[1].tv_sec
-= 120; /* make mod time 2 minutes older */
3564 my_err
= futimes( my_fd
, &my_utimes
[0] );
3565 if ( my_err
== -1 ) {
3566 printf( "futimes - failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
3567 goto test_failed_exit
;
3570 /* make sure the correct times are set */
3571 my_err
= stat( my_pathp
, &my_sb
);
3572 if ( my_err
!= 0 ) {
3573 printf( "stat - failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
3574 goto test_failed_exit
;
3576 TIMESPEC_TO_TIMEVAL( &my_utimes
[2], &my_sb
.st_atimespec
);
3577 TIMESPEC_TO_TIMEVAL( &my_utimes
[3], &my_sb
.st_mtimespec
);
3578 if ( my_utimes
[0].tv_sec
!= my_utimes
[2].tv_sec
||
3579 my_utimes
[1].tv_sec
!= my_utimes
[3].tv_sec
) {
3580 printf( "futimes failed to set access and mod times \n" );
3581 goto test_failed_exit
;
3585 goto test_passed_exit
;
3593 if ( my_pathp
!= NULL
) {
3595 vm_deallocate(mach_task_self(), (vm_address_t
)my_pathp
, PATH_MAX
);
3600 /* **************************************************************************************************************
3601 * Test rename, stat system calls.
3602 * **************************************************************************************************************
3604 int rename_test( void * the_argp
)
3607 char * my_pathp
= NULL
;
3608 char * my_new_pathp
= NULL
;
3611 kern_return_t my_kr
;
3613 my_kr
= vm_allocate((vm_map_t
) mach_task_self(), (vm_address_t
*)&my_pathp
, PATH_MAX
, VM_FLAGS_ANYWHERE
);
3614 if(my_kr
!= KERN_SUCCESS
){
3615 printf( "vm_allocate failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
3616 goto test_failed_exit
;
3620 strcat( my_pathp
, &g_target_path
[0] );
3621 strcat( my_pathp
, "/" );
3623 /* create a test file */
3624 my_err
= create_random_name( my_pathp
, 1 );
3625 if ( my_err
!= 0 ) {
3626 goto test_failed_exit
;
3629 my_kr
= vm_allocate((vm_map_t
) mach_task_self(), (vm_address_t
*)&my_new_pathp
, PATH_MAX
, VM_FLAGS_ANYWHERE
);
3630 if(my_kr
!= KERN_SUCCESS
){
3631 printf( "vm_allocate failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
3632 goto test_failed_exit
;
3635 *my_new_pathp
= 0x00;
3636 strcat( my_new_pathp
, &g_target_path
[0] );
3637 strcat( my_new_pathp
, "/" );
3639 /* get a unique name for our rename test */
3640 my_err
= create_random_name( my_new_pathp
, 0 );
3641 if ( my_err
!= 0 ) {
3642 goto test_failed_exit
;
3645 /* save file ID for later use */
3646 my_err
= stat( my_pathp
, &my_sb
);
3647 if ( my_err
!= 0 ) {
3648 printf( "stat - failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
3649 goto test_failed_exit
;
3651 my_file_id
= my_sb
.st_ino
;
3654 my_err
= rename( my_pathp
, my_new_pathp
);
3655 if ( my_err
== -1 ) {
3656 printf( "rename - failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
3657 goto test_failed_exit
;
3660 /* make sure old name is no longer there */
3661 my_err
= stat( my_pathp
, &my_sb
);
3662 if ( my_err
== 0 ) {
3663 printf( "rename call failed - found old name \n" );
3664 goto test_failed_exit
;
3667 /* make sure new name is there and is correct file id */
3668 my_err
= stat( my_new_pathp
, &my_sb
);
3669 if ( my_err
!= 0 ) {
3670 printf( "stat - failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
3671 goto test_failed_exit
;
3673 if ( my_file_id
!= my_sb
.st_ino
) {
3674 printf( "rename failed - wrong file id \n" );
3675 goto test_failed_exit
;
3679 goto test_passed_exit
;
3685 if ( my_pathp
!= NULL
) {
3687 vm_deallocate(mach_task_self(), (vm_address_t
)my_pathp
, PATH_MAX
);
3689 if ( my_new_pathp
!= NULL
) {
3690 remove( my_new_pathp
);
3691 vm_deallocate(mach_task_self(), (vm_address_t
)my_new_pathp
, PATH_MAX
);
3696 /* **************************************************************************************************************
3697 * Test locking system calls.
3698 * **************************************************************************************************************
3700 int locking_test( void * the_argp
)
3702 int my_err
, my_status
;
3703 pid_t my_pid
, my_wait_pid
;
3705 char * my_pathp
= NULL
;
3706 kern_return_t my_kr
;
3708 my_kr
= vm_allocate((vm_map_t
) mach_task_self(), (vm_address_t
*)&my_pathp
, PATH_MAX
, VM_FLAGS_ANYWHERE
);
3709 if(my_kr
!= KERN_SUCCESS
){
3710 printf( "vm_allocate failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
3711 goto test_failed_exit
;
3715 strcat( my_pathp
, &g_target_path
[0] );
3716 strcat( my_pathp
, "/" );
3718 /* create a test file */
3719 my_err
= create_random_name( my_pathp
, 1 );
3720 if ( my_err
!= 0 ) {
3721 goto test_failed_exit
;
3725 my_fd
= open( my_pathp
, O_RDWR
, 0 );
3726 if ( my_fd
== -1 ) {
3727 printf( "open call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
3728 goto test_failed_exit
;
3731 my_err
= flock( my_fd
, LOCK_EX
);
3732 if ( my_err
== -1 ) {
3733 printf( "flock - LOCK_EX - failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
3734 goto test_failed_exit
;
3738 * spin off a child process that we will use for testing.
3741 if ( my_pid
== -1 ) {
3742 printf( "fork failed with errno %d - %s \n", errno
, strerror( errno
) );
3743 goto test_failed_exit
;
3745 if ( my_pid
== 0 ) {
3749 int my_child_fd
= -1;
3752 my_child_fd
= open( my_pathp
, O_RDWR
, 0 );
3753 if ( my_child_fd
== -1 ) {
3754 printf( "open call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
3759 my_err
= flock( my_child_fd
, (LOCK_EX
| LOCK_NB
) );
3760 if ( my_err
== -1 ) {
3761 if ( errno
!= EWOULDBLOCK
) {
3762 printf( "flock call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
3768 printf( "flock call should have failed with EWOULDBLOCK err \n" );
3774 if ( my_child_fd
!= -1 )
3775 close( my_child_fd
);
3776 exit( my_child_err
);
3780 * wait for child to exit
3782 my_wait_pid
= wait4( my_pid
, &my_status
, 0, NULL
);
3783 if ( my_wait_pid
== -1 ) {
3784 printf( "wait4 failed with errno %d - %s \n", errno
, strerror( errno
) );
3785 goto test_failed_exit
;
3788 if ( WIFEXITED( my_status
) && WEXITSTATUS( my_status
) != 0 ) {
3789 goto test_failed_exit
;
3792 my_err
= flock( my_fd
, LOCK_UN
);
3793 if ( my_err
== -1 ) {
3794 printf( "flock - LOCK_UN - failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
3795 goto test_failed_exit
;
3799 goto test_passed_exit
;
3807 if ( my_pathp
!= NULL
) {
3809 vm_deallocate(mach_task_self(), (vm_address_t
)my_pathp
, PATH_MAX
);
3814 /* **************************************************************************************************************
3815 * Test mkfifo system calls.
3816 * **************************************************************************************************************
3818 int mkfifo_test( void * the_argp
)
3820 int my_err
, my_status
;
3821 pid_t my_pid
, my_wait_pid
;
3823 char * my_pathp
= NULL
;
3825 off_t my_current_offset
;
3826 kern_return_t my_kr
;
3828 my_kr
= vm_allocate((vm_map_t
) mach_task_self(), (vm_address_t
*)&my_pathp
, PATH_MAX
, VM_FLAGS_ANYWHERE
);
3829 if(my_kr
!= KERN_SUCCESS
){
3830 printf( "vm_allocate failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
3831 goto test_failed_exit
;
3835 strcat( my_pathp
, &g_target_path
[0] );
3836 strcat( my_pathp
, "/" );
3838 /* get unique name for our fifo */
3839 my_err
= create_random_name( my_pathp
, 0 );
3840 if ( my_err
!= 0 ) {
3841 goto test_failed_exit
;
3844 my_err
= mkfifo( my_pathp
, (S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
) );
3845 if ( my_err
!= 0 ) {
3846 printf( "mkfifo failed with errno %d - %s. \n", errno
, strerror( errno
) );
3847 goto test_failed_exit
;
3851 * spin off a child process that we will use for testing.
3854 if ( my_pid
== -1 ) {
3855 printf( "fork failed with errno %d - %s \n", errno
, strerror( errno
) );
3856 goto test_failed_exit
;
3858 if ( my_pid
== 0 ) {
3862 int my_child_fd
= -1;
3866 /* open read end of fifo */
3867 my_child_fd
= open( my_pathp
, O_RDWR
, 0 );
3868 if ( my_child_fd
== -1 ) {
3869 printf( "open call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
3874 /* read message from parent */
3875 bzero( (void *)&my_buffer
[0], sizeof(my_buffer
) );
3876 my_result
= read( my_child_fd
, &my_buffer
[0], sizeof(my_buffer
) );
3877 if ( my_result
== -1 ) {
3878 printf( "read call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
3882 if ( strcmp( "parent to child", &my_buffer
[0] ) != 0 ) {
3883 printf( "read wrong message from parent \n" );
3890 if ( my_child_fd
!= -1 )
3891 close( my_child_fd
);
3892 exit( my_child_err
);
3895 /* parent process - open write end of fifo
3897 my_fd
= open( my_pathp
, O_WRONLY
, 0 );
3898 if ( my_fd
== -1 ) {
3899 printf( "open call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
3900 goto test_failed_exit
;
3903 /* make sure we can't seek on a fifo */
3904 my_current_offset
= lseek( my_fd
, 0, SEEK_CUR
);
3905 if ( my_current_offset
!= -1 ) {
3906 printf( "lseek on fifo should fail but did not \n" );
3907 goto test_failed_exit
;
3910 my_result
= write( my_fd
, "parent to child", 15 );
3911 if ( my_result
== -1 ) {
3912 printf( "write call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
3913 goto test_failed_exit
;
3916 my_wait_pid
= wait4( my_pid
, &my_status
, 0, NULL
);
3917 if ( my_wait_pid
== -1 ) {
3918 printf( "wait4 failed with errno %d - %s \n", errno
, strerror( errno
) );
3919 goto test_failed_exit
;
3922 if ( WIFEXITED( my_status
) && WEXITSTATUS( my_status
) != 0 ) {
3923 goto test_failed_exit
;
3927 goto test_passed_exit
;
3935 if ( my_pathp
!= NULL
) {
3937 vm_deallocate(mach_task_self(), (vm_address_t
)my_pathp
, PATH_MAX
);
3942 /* **************************************************************************************************************
3943 * Test quotactl system calls.
3944 * **************************************************************************************************************
3946 int quotactl_test( void * the_argp
)
3948 #if !TARGET_OS_EMBEDDED
3950 int is_quotas_on
= 0;
3951 struct dqblk my_quota_blk
;
3953 if ( g_skip_setuid_tests
!= 0 ) {
3954 printf( "\t skipping this test \n" );
3956 goto test_passed_exit
;
3959 /* start off by checking the status of quotas on the boot volume */
3960 my_err
= quotactl( "/mach_kernel", QCMD(Q_QUOTASTAT
, USRQUOTA
), 0, (caddr_t
)&is_quotas_on
);
3961 if ( my_err
== -1 ) {
3962 printf( "quotactl - Q_QUOTASTAT - failed with errno %d - %s \n", errno
, strerror( errno
) );
3963 goto test_failed_exit
;
3966 if ( is_quotas_on
== 0 ) {
3967 /* quotas are off */
3969 goto test_passed_exit
;
3972 my_err
= quotactl( "/mach_kernel", QCMD(Q_GETQUOTA
, USRQUOTA
), getuid(), (caddr_t
)&my_quota_blk
);
3973 if ( my_err
== -1 ) {
3974 printf( "quotactl - Q_GETQUOTA - failed with errno %d - %s \n", errno
, strerror( errno
) );
3975 goto test_failed_exit
;
3979 goto test_passed_exit
;
3987 printf( "\t--> Not supported on EMBEDDED TARGET\n" );
3992 /* **************************************************************************************************************
3993 * Test getrlimit, setrlimit system calls.
3994 * **************************************************************************************************************
3996 int limit_tests( void * the_argp
)
3999 struct rlimit my_current_rlimit
;
4000 struct rlimit my_rlimit
;
4002 my_err
= getrlimit( RLIMIT_NOFILE
, &my_current_rlimit
);
4003 if ( my_err
== -1 ) {
4004 printf( "getrlimit - failed with errno %d - %s \n", errno
, strerror( errno
) );
4005 goto test_failed_exit
;
4007 if ( my_current_rlimit
.rlim_cur
!= RLIM_INFINITY
) {
4008 if ( my_current_rlimit
.rlim_cur
!= my_current_rlimit
.rlim_max
)
4009 my_current_rlimit
.rlim_cur
+= 1;
4011 my_current_rlimit
.rlim_cur
-= 1;
4012 my_rlimit
.rlim_cur
= my_current_rlimit
.rlim_cur
;
4013 my_rlimit
.rlim_max
= my_current_rlimit
.rlim_max
;
4014 my_err
= setrlimit( RLIMIT_NOFILE
, &my_rlimit
);
4015 if ( my_err
== -1 ) {
4016 printf( "setrlimit - failed with errno %d - %s \n", errno
, strerror( errno
) );
4017 goto test_failed_exit
;
4020 /* verify that we set a new limit */
4021 bzero( (void *) &my_rlimit
, sizeof( my_rlimit
) );
4022 my_err
= getrlimit( RLIMIT_NOFILE
, &my_rlimit
);
4023 if ( my_err
== -1 ) {
4024 printf( "getrlimit - failed with errno %d - %s \n", errno
, strerror( errno
) );
4025 goto test_failed_exit
;
4027 if ( my_rlimit
.rlim_cur
!= my_current_rlimit
.rlim_cur
) {
4028 printf( "failed to get/set new RLIMIT_NOFILE soft limit \n" );
4029 printf( "soft limits - current %lld should be %lld \n", my_rlimit
.rlim_cur
, my_current_rlimit
.rlim_cur
);
4030 goto test_failed_exit
;
4033 #if CONFORMANCE_CHANGES_IN_XNU // can't do this check until conformance changes get into xnu
4034 printf( "hard limits - current %lld should be %lld \n", my_rlimit
.rlim_max
, my_current_rlimit
.rlim_max
);
4035 if ( my_rlimit
.rlim_max
!= my_current_rlimit
.rlim_max
) {
4036 printf( "failed to get/set new RLIMIT_NOFILE hard limit \n" );
4037 goto test_failed_exit
;
4042 * A test for a limit that won't fit in a signed 32 bits, a la 5414697
4043 * Note: my_rlimit should still have a valid rlim_max.
4045 long long biglim
= 2147483649ll; /* Just over 2^31 */
4046 my_rlimit
.rlim_cur
= biglim
;
4047 my_err
= setrlimit(RLIMIT_CPU
, &my_rlimit
);
4049 printf("failed to set large limit.\n");
4050 goto test_failed_exit
;
4053 bzero(&my_rlimit
, sizeof(struct rlimit
));
4054 my_err
= getrlimit(RLIMIT_CPU
, &my_rlimit
);
4056 printf("after setting large value, failed to getrlimit().\n");
4057 goto test_failed_exit
;
4060 if (my_rlimit
.rlim_cur
!= biglim
) {
4061 printf("didn't retrieve large limit.\n");
4062 goto test_failed_exit
;
4067 goto test_passed_exit
;
4076 /* **************************************************************************************************************
4077 * Test getattrlist, getdirentriesattr, setattrlist system calls.
4078 * **************************************************************************************************************
4080 struct test_attr_buf
{
4082 fsobj_type_t obj_type
;
4084 struct timespec backup_time
;
4087 typedef struct test_attr_buf test_attr_buf
;
4089 int directory_tests( void * the_argp
)
4091 int my_err
, done
, found_it
, i
;
4094 char * my_pathp
= NULL
;
4095 char * my_bufp
= NULL
;
4096 char * my_file_namep
;
4098 unsigned int my_base
;
4099 unsigned int my_count
;
4100 unsigned int my_new_state
;
4102 unsigned long my_base
;
4103 unsigned long my_count
;
4104 unsigned long my_new_state
;
4106 fsobj_id_t my_obj_id
;
4107 struct timespec my_new_backup_time
;
4108 struct attrlist my_attrlist
;
4109 test_attr_buf my_attr_buf
[4];
4110 struct statfs my_statfs_buf
;
4111 kern_return_t my_kr
;
4113 /* need to know type of file system */
4114 my_err
= statfs( &g_target_path
[0], &my_statfs_buf
);
4115 if ( my_err
== -1 ) {
4116 printf( "statfs call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
4117 goto test_failed_exit
;
4119 if ( memcmp( &my_statfs_buf
.f_fstypename
[0], "ufs", 3 ) == 0 ) {
4123 my_kr
= vm_allocate((vm_map_t
) mach_task_self(), (vm_address_t
*)&my_bufp
, (1024 * 5), VM_FLAGS_ANYWHERE
);
4124 if(my_kr
!= KERN_SUCCESS
){
4125 printf( "vm_allocate failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
4126 goto test_failed_exit
;
4129 my_kr
= vm_allocate((vm_map_t
) mach_task_self(), (vm_address_t
*)&my_pathp
, PATH_MAX
, VM_FLAGS_ANYWHERE
);
4130 if(my_kr
!= KERN_SUCCESS
){
4131 printf( "vm_allocate failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
4132 goto test_failed_exit
;
4136 strcat( my_pathp
, &g_target_path
[0] );
4137 strcat( my_pathp
, "/" );
4139 /* create a test file */
4140 my_err
= create_random_name( my_pathp
, 1 );
4141 if ( my_err
!= 0 ) {
4142 goto test_failed_exit
;
4145 /* get pointer to just the file name */
4146 my_file_namep
= strrchr( my_pathp
, '/' );
4149 /* check out the test directory */
4150 my_fd
= open( &g_target_path
[0], (O_RDONLY
), 0 );
4151 if ( my_fd
== -1 ) {
4152 printf( "open failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
4153 goto test_failed_exit
;
4156 /* test get/setattrlist */
4157 memset( &my_attrlist
, 0, sizeof(my_attrlist
) );
4158 my_attrlist
.bitmapcount
= ATTR_BIT_MAP_COUNT
;
4159 my_attrlist
.commonattr
= (ATTR_CMN_OBJTYPE
| ATTR_CMN_OBJID
| ATTR_CMN_BKUPTIME
);
4160 my_err
= getattrlist( my_pathp
, &my_attrlist
, &my_attr_buf
[0], sizeof(my_attr_buf
[0]), 0 );
4162 if ( my_err
!= 0 ) {
4163 if ( errno
== ENOTSUP
&& is_ufs
) {
4164 /* getattr calls not supported on ufs */
4166 goto test_passed_exit
;
4168 printf( "getattrlist call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
4169 goto test_failed_exit
;
4171 /* validate returned data */
4172 if ( my_attr_buf
[0].obj_type
!= VREG
) {
4173 printf( "getattrlist returned incorrect obj_type data. \n" );
4174 goto test_failed_exit
;
4177 /* set new backup time */
4178 my_obj_id
= my_attr_buf
[0].obj_id
;
4179 my_new_backup_time
= my_attr_buf
[0].backup_time
;
4180 my_new_backup_time
.tv_sec
+= 60;
4181 my_attr_buf
[0].backup_time
.tv_sec
= my_new_backup_time
.tv_sec
;
4182 my_attrlist
.commonattr
= (ATTR_CMN_BKUPTIME
);
4183 my_err
= setattrlist( my_pathp
, &my_attrlist
, &my_attr_buf
[0].backup_time
, sizeof(my_attr_buf
[0].backup_time
), 0 );
4184 if ( my_err
!= 0 ) {
4185 printf( "setattrlist call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
4186 goto test_failed_exit
;
4189 /* validate setattrlist using getdirentriesattr */
4191 my_fd
= open( &g_target_path
[0], (O_RDONLY
), 0 );
4192 if ( my_fd
== -1 ) {
4193 printf( "open failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
4194 goto test_failed_exit
;
4196 memset( &my_attrlist
, 0, sizeof(my_attrlist
) );
4197 memset( &my_attr_buf
, 0, sizeof(my_attr_buf
) );
4198 my_attrlist
.bitmapcount
= ATTR_BIT_MAP_COUNT
;
4199 my_attrlist
.commonattr
= (ATTR_CMN_OBJTYPE
| ATTR_CMN_OBJID
| ATTR_CMN_BKUPTIME
);
4202 my_err
= getdirentriesattr( my_fd
, &my_attrlist
, &my_attr_buf
[0], sizeof(my_attr_buf
), &my_count
,
4203 &my_base
, &my_new_state
, 0 );
4205 printf( "getdirentriesattr call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
4206 goto test_failed_exit
;
4210 for ( i
= 0; i
< my_count
; i
++ ) {
4211 if ( my_attr_buf
[i
].obj_id
.fid_objno
== my_obj_id
.fid_objno
&&
4212 my_attr_buf
[i
].obj_id
.fid_generation
== my_obj_id
.fid_generation
) {
4214 if ( my_attr_buf
[i
].backup_time
.tv_sec
!= my_new_backup_time
.tv_sec
) {
4215 printf( "setattrlist failed to set backup time. \n" );
4216 goto test_failed_exit
;
4220 if ( found_it
== 0 ) {
4221 printf( "getdirentriesattr failed to find test file. \n" );
4222 goto test_failed_exit
;
4226 goto test_passed_exit
;
4235 if ( my_pathp
!= NULL
) {
4237 vm_deallocate(mach_task_self(), (vm_address_t
)my_pathp
, PATH_MAX
);
4239 if ( my_bufp
!= NULL
) {
4240 vm_deallocate(mach_task_self(), (vm_address_t
)my_bufp
, (1024 * 5));
4245 /* **************************************************************************************************************
4246 * Test exchangedata system calls.
4247 * **************************************************************************************************************
4249 int exchangedata_test( void * the_argp
)
4254 char * my_file1_pathp
= NULL
;
4255 char * my_file2_pathp
= NULL
;
4258 struct statfs my_statfs_buf
;
4259 kern_return_t my_kr
;
4261 /* need to know type of file system */
4262 my_err
= statfs( &g_target_path
[0], &my_statfs_buf
);
4263 if ( my_err
== -1 ) {
4264 printf( "statfs call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
4265 goto test_failed_exit
;
4267 if ( memcmp( &my_statfs_buf
.f_fstypename
[0], "ufs", 3 ) == 0 ) {
4268 /* ufs does not support exchangedata */
4270 goto test_passed_exit
;
4273 my_kr
= vm_allocate((vm_map_t
) mach_task_self(), (vm_address_t
*)&my_file1_pathp
, PATH_MAX
, VM_FLAGS_ANYWHERE
);
4274 if(my_kr
!= KERN_SUCCESS
){
4275 printf( "vm_allocate failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
4276 goto test_failed_exit
;
4279 *my_file1_pathp
= 0x00;
4280 strcat( my_file1_pathp
, &g_target_path
[0] );
4281 strcat( my_file1_pathp
, "/" );
4283 /* create a test file */
4284 my_err
= create_random_name( my_file1_pathp
, 1 );
4285 if ( my_err
!= 0 ) {
4286 printf( "create_random_name my_err: %d\n", my_err
);
4287 goto test_failed_exit
;
4289 my_fd1
= open( my_file1_pathp
, O_RDWR
, 0 );
4290 if ( my_fd1
== -1 ) {
4291 printf( "open call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
4292 goto test_failed_exit
;
4294 my_result
= write( my_fd1
, "11111111", 8 );
4295 if ( my_result
== -1 ) {
4296 printf( "write call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
4297 goto test_failed_exit
;
4300 my_kr
= vm_allocate((vm_map_t
) mach_task_self(), (vm_address_t
*)&my_file2_pathp
, PATH_MAX
, VM_FLAGS_ANYWHERE
);
4301 if(my_kr
!= KERN_SUCCESS
){
4302 printf( "vm_allocate failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
4303 goto test_failed_exit
;
4306 *my_file2_pathp
= 0x00;
4307 strcat( my_file2_pathp
, &g_target_path
[0] );
4308 strcat( my_file2_pathp
, "/" );
4310 /* create a test file */
4311 my_err
= create_random_name( my_file2_pathp
, 1 );
4312 if ( my_err
!= 0 ) {
4313 printf( "create_random_name my_err: %d\n", my_err
);
4314 goto test_failed_exit
;
4316 my_fd2
= open( my_file2_pathp
, O_RDWR
, 0 );
4317 if ( my_fd2
== -1 ) {
4318 printf( "open call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
4319 goto test_failed_exit
;
4321 my_result
= write( my_fd2
, "22222222", 8 );
4322 if ( my_result
== -1 ) {
4323 printf( "write call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
4324 goto test_failed_exit
;
4331 /* test exchangedata */
4332 my_err
= exchangedata( my_file1_pathp
, my_file2_pathp
, 0 );
4333 if ( my_err
== -1 ) {
4334 printf( "exchangedata failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
4335 goto test_failed_exit
;
4338 /* now validate exchange */
4339 my_fd1
= open( my_file1_pathp
, O_RDONLY
, 0 );
4340 if ( my_fd1
== -1 ) {
4341 printf( "open call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
4342 goto test_failed_exit
;
4344 bzero( (void *)&my_buffer
[0], sizeof(my_buffer
) );
4345 my_result
= read( my_fd1
, &my_buffer
[0], 8 );
4346 if ( my_result
== -1 ) {
4347 printf( "write call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
4348 goto test_failed_exit
;
4351 if ( memcmp( &my_buffer
[0], "22222222", 8 ) != 0 ) {
4352 printf( "exchangedata failed - incorrect data in file \n" );
4353 goto test_failed_exit
;
4357 goto test_passed_exit
;
4365 if ( my_file1_pathp
!= NULL
) {
4366 remove( my_file1_pathp
);
4367 vm_deallocate(mach_task_self(), (vm_address_t
)my_file1_pathp
, PATH_MAX
);
4371 if ( my_file2_pathp
!= NULL
) {
4372 remove( my_file2_pathp
);
4373 vm_deallocate(mach_task_self(), (vm_address_t
)my_file2_pathp
, PATH_MAX
);
4379 /* **************************************************************************************************************
4380 * Test searchfs system calls.
4381 * **************************************************************************************************************
4384 struct packed_name_attr
{
4385 u_int32_t size
; /* Of the remaining fields */
4386 struct attrreference ref
; /* Offset/length of name itself */
4387 char name
[ PATH_MAX
];
4390 struct packed_attr_ref
{
4391 u_int32_t size
; /* Of the remaining fields */
4392 struct attrreference ref
; /* Offset/length of attr itself */
4395 struct packed_result
{
4396 u_int32_t size
; /* Including size field itself */
4397 attrreference_t obj_name
;
4398 struct fsobj_id obj_id
;
4399 struct timespec obj_create_time
;
4400 char room_for_name
[ 64 ];
4402 typedef struct packed_result packed_result
;
4403 typedef struct packed_result
* packed_result_p
;
4405 #define MAX_MATCHES 10
4406 #define MAX_EBUSY_RETRIES 5
4408 int searchfs_test( void * the_argp
)
4410 int my_err
, my_items_found
= 0, my_ebusy_count
;
4411 char * my_pathp
= NULL
;
4412 unsigned long my_matches
;
4413 unsigned long my_search_options
;
4414 struct fssearchblock my_search_blk
;
4415 struct attrlist my_return_list
;
4416 struct searchstate my_search_state
;
4417 struct packed_name_attr my_info1
;
4418 struct packed_attr_ref my_info2
;
4419 packed_result my_result_buffer
[ MAX_MATCHES
];
4420 struct statfs my_statfs_buf
;
4421 kern_return_t my_kr
;
4423 /* need to know type of file system */
4424 my_err
= statfs( &g_target_path
[0], &my_statfs_buf
);
4425 if ( my_err
== -1 ) {
4426 printf( "statfs call failed. got errno %d - %s. \n", errno
, strerror( errno
) );
4427 goto test_failed_exit
;
4429 if ( memcmp( &my_statfs_buf
.f_fstypename
[0], "ufs", 3 ) == 0 ) {
4430 /* ufs does not support exchangedata */
4432 goto test_passed_exit
;
4435 my_kr
= vm_allocate((vm_map_t
) mach_task_self(), (vm_address_t
*)&my_pathp
, PATH_MAX
, VM_FLAGS_ANYWHERE
);
4436 if(my_kr
!= KERN_SUCCESS
){
4437 printf( "vm_allocate failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
4438 goto test_failed_exit
;
4442 strcat( my_pathp
, &g_target_path
[0] );
4443 strcat( my_pathp
, "/" );
4445 /* create test files */
4446 my_err
= create_file_with_name( my_pathp
, "foo", 0 );
4448 printf( "failed to create a test file name in \"%s\" \n", my_pathp
);
4449 goto test_failed_exit
;
4452 my_err
= create_file_with_name( my_pathp
, "foobar", 0 );
4454 printf( "failed to create a test file name in \"%s\" \n", my_pathp
);
4455 goto test_failed_exit
;
4458 my_err
= create_file_with_name( my_pathp
, "foofoo", 0 );
4460 printf( "failed to create a test file name in \"%s\" \n", my_pathp
);
4461 goto test_failed_exit
;
4464 my_err
= create_file_with_name( my_pathp
, "xxxfoo", 0 );
4466 printf( "failed to create a test file name in \"%s\" \n", my_pathp
);
4467 goto test_failed_exit
;
4470 /* EBUSY count updated below the catalogue_changed label */
4474 /* search target volume for all file system objects with "foo" in the name */
4475 /* Set up the attributes we're searching on. */
4476 my_items_found
= 0; /* Set this here in case we're completely restarting */
4477 my_search_blk
.searchattrs
.bitmapcount
= ATTR_BIT_MAP_COUNT
;
4478 my_search_blk
.searchattrs
.reserved
= 0;
4479 my_search_blk
.searchattrs
.commonattr
= ATTR_CMN_NAME
;
4480 my_search_blk
.searchattrs
.volattr
= 0;
4481 my_search_blk
.searchattrs
.dirattr
= 0;
4482 my_search_blk
.searchattrs
.fileattr
= 0;
4483 my_search_blk
.searchattrs
.forkattr
= 0;
4485 /* Set up the attributes we want for all returned matches. */
4486 /* Why is returnattrs a pointer instead of an embedded struct? */
4487 my_search_blk
.returnattrs
= &my_return_list
;
4488 my_return_list
.bitmapcount
= ATTR_BIT_MAP_COUNT
;
4489 my_return_list
.reserved
= 0;
4490 my_return_list
.commonattr
= ATTR_CMN_NAME
| ATTR_CMN_OBJID
| ATTR_CMN_CRTIME
;
4491 my_return_list
.volattr
= 0;
4492 my_return_list
.dirattr
= 0;
4493 my_return_list
.fileattr
= 0;
4494 my_return_list
.forkattr
= 0;
4496 /* Allocate a buffer for returned matches */
4497 my_search_blk
.returnbuffer
= my_result_buffer
;
4498 my_search_blk
.returnbuffersize
= sizeof(my_result_buffer
);
4500 /* Pack the searchparams1 into a buffer */
4501 /* NOTE: A name appears only in searchparams1 */
4502 strcpy( my_info1
.name
, "foo" );
4503 my_info1
.ref
.attr_dataoffset
= sizeof(struct attrreference
);
4504 my_info1
.ref
.attr_length
= strlen(my_info1
.name
) + 1;
4505 my_info1
.size
= sizeof(struct attrreference
) + my_info1
.ref
.attr_length
;
4506 my_search_blk
.searchparams1
= &my_info1
;
4507 my_search_blk
.sizeofsearchparams1
= my_info1
.size
+ sizeof(u_int32_t
);
4509 /* Pack the searchparams2 into a buffer */
4510 my_info2
.size
= sizeof(struct attrreference
);
4511 my_info2
.ref
.attr_dataoffset
= sizeof(struct attrreference
);
4512 my_info2
.ref
.attr_length
= 0;
4513 my_search_blk
.searchparams2
= &my_info2
;
4514 my_search_blk
.sizeofsearchparams2
= sizeof(my_info2
);
4516 /* Maximum number of matches we want */
4517 my_search_blk
.maxmatches
= MAX_MATCHES
;
4519 /* Maximum time to search, per call */
4520 my_search_blk
.timelimit
.tv_sec
= 1;
4521 my_search_blk
.timelimit
.tv_usec
= 0;
4523 my_search_options
= (SRCHFS_START
| SRCHFS_MATCHPARTIALNAMES
|
4524 SRCHFS_MATCHFILES
| SRCHFS_MATCHDIRS
);
4530 my_err
= searchfs( my_pathp
, &my_search_blk
, &my_matches
, 0, my_search_options
, &my_search_state
);
4533 if ( (my_err
== 0 || my_err
== EAGAIN
) && my_matches
> 0 ) {
4534 /* Unpack the results */
4535 // printf("my_matches %d \n", my_matches);
4536 my_ptr
= (char *) &my_result_buffer
[0];
4537 my_end_ptr
= (my_ptr
+ sizeof(my_result_buffer
));
4538 for ( i
= 0; i
< my_matches
; ++i
) {
4539 packed_result_p my_result_p
= (packed_result_p
) my_ptr
;
4542 /* see if we foound all our test files */
4543 my_name_p
= (((char *)(&my_result_p
->obj_name
)) + my_result_p
->obj_name
.attr_dataoffset
);
4544 if ( memcmp( my_name_p
, "foo", 3 ) == 0 ||
4545 memcmp( my_name_p
, "foobar", 6 ) == 0 ||
4546 memcmp( my_name_p
, "foofoo", 6 ) == 0 ||
4547 memcmp( my_name_p
, "xxxfoo", 6 ) == 0 ) {
4551 printf("obj_name \"%.*s\" \n",
4552 (int) my_result_p
->obj_name
.attr_length
,
4553 (((char *)(&my_result_p
->obj_name
)) +
4554 my_result_p
->obj_name
.attr_dataoffset
));
4555 printf("size %d fid_objno %d fid_generation %d tv_sec 0x%02LX \n",
4556 my_result_p
->size
, my_result_p
->obj_id
.fid_objno
,
4557 my_result_p
->obj_id
.fid_generation
,
4558 my_result_p
->obj_create_time
.tv_sec
);
4560 my_ptr
= (my_ptr
+ my_result_p
->size
);
4561 if (my_ptr
> my_end_ptr
)
4566 /* EBUSY indicates catalogue change; retry a few times. */
4567 if ((my_err
== EBUSY
) && (my_ebusy_count
++ < MAX_EBUSY_RETRIES
)) {
4568 goto catalogue_changed
;
4570 if ( !(my_err
== 0 || my_err
== EAGAIN
) ) {
4571 printf( "searchfs failed with error %d - \"%s\" \n", my_err
, strerror( my_err
) );
4573 my_search_options
&= ~SRCHFS_START
;
4574 } while ( my_err
== EAGAIN
);
4576 if ( my_items_found
< 4 ) {
4577 printf( "searchfs failed to find all test files \n" );
4578 goto test_failed_exit
;
4582 goto test_passed_exit
;
4588 if ( my_pathp
!= NULL
) {
4589 char * my_ptr
= (my_pathp
+ strlen( my_pathp
));
4590 strcat( my_pathp
, "foo" );
4593 strcat( my_pathp
, "foobar" );
4596 strcat( my_pathp
, "foofoo" );
4599 strcat( my_pathp
, "xxxfoo" );
4601 vm_deallocate(mach_task_self(), (vm_address_t
)my_pathp
, PATH_MAX
);
4607 #define AIO_TESTS_BUFFER_SIZE (1024 * 4000)
4608 #define AIO_TESTS_OUR_COUNT 5
4609 /* **************************************************************************************************************
4610 * Test aio_error, aio_read, aio_return, aio_suspend, aio_write, fcntl system calls.
4611 * **************************************************************************************************************
4613 int aio_tests( void * the_argp
)
4615 #if !TARGET_OS_EMBEDDED
4618 struct aiocb
* my_aiocbp
;
4620 struct timespec my_timeout
;
4621 int my_fd_list
[ AIO_TESTS_OUR_COUNT
];
4622 char * my_buffers
[ AIO_TESTS_OUR_COUNT
];
4623 struct aiocb
* my_aiocb_list
[ AIO_TESTS_OUR_COUNT
];
4624 struct aiocb my_aiocbs
[ AIO_TESTS_OUR_COUNT
];
4625 char * my_file_paths
[ AIO_TESTS_OUR_COUNT
];
4626 kern_return_t my_kr
;
4628 /* set up to have the ability to fire off up to AIO_TESTS_OUR_COUNT async IOs at once */
4629 memset( &my_fd_list
[0], 0xFF, sizeof( my_fd_list
) );
4630 memset( &my_buffers
[0], 0x00, sizeof( my_buffers
) );
4631 memset( &my_aiocb_list
[0], 0x00, sizeof( my_aiocb_list
) );
4632 memset( &my_file_paths
[0], 0x00, sizeof( my_file_paths
) );
4633 for ( i
= 0; i
< AIO_TESTS_OUR_COUNT
; i
++ ) {
4634 my_kr
= vm_allocate((vm_map_t
) mach_task_self(), (vm_address_t
*)&my_buffers
[ i
], AIO_TESTS_BUFFER_SIZE
, VM_FLAGS_ANYWHERE
);
4635 if(my_kr
!= KERN_SUCCESS
){
4636 printf( "vm_allocate failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
4637 goto test_failed_exit
;
4640 my_kr
= vm_allocate((vm_map_t
) mach_task_self(), (vm_address_t
*)&my_file_paths
[ i
], PATH_MAX
, VM_FLAGS_ANYWHERE
);
4641 if(my_kr
!= KERN_SUCCESS
){
4642 printf( "vm_allocate failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
4643 goto test_failed_exit
;
4646 my_pathp
= my_file_paths
[ i
];
4648 strcat( my_pathp
, &g_target_path
[0] );
4649 strcat( my_pathp
, "/" );
4651 /* create a test file */
4652 my_err
= create_random_name( my_pathp
, 1 );
4653 if ( my_err
!= 0 ) {
4654 goto test_failed_exit
;
4656 my_fd_list
[ i
] = open( my_pathp
, O_RDWR
, 0 );
4657 if ( my_fd_list
[ i
] <= 0 ) {
4658 printf( "open call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
4659 goto test_failed_exit
;
4662 my_aiocbp
= &my_aiocbs
[ i
];
4663 my_aiocb_list
[ i
] = my_aiocbp
;
4664 memset( my_aiocbp
, 0x00, sizeof( *my_aiocbp
) );
4665 my_aiocbp
->aio_fildes
= my_fd_list
[ i
];
4666 my_aiocbp
->aio_buf
= (char *) my_buffers
[ i
];
4667 my_aiocbp
->aio_nbytes
= 1024;
4668 my_aiocbp
->aio_sigevent
.sigev_notify
= SIGEV_NONE
; // no signals at completion;
4669 my_aiocbp
->aio_sigevent
.sigev_signo
= 0;
4672 /* test direct IO (F_NOCACHE) and aio_write */
4673 my_err
= fcntl( my_fd_list
[ 0 ], F_NOCACHE
, 1 );
4674 if ( my_err
!= 0 ) {
4675 printf( "malloc failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
4676 goto test_failed_exit
;
4679 my_aiocbp
= &my_aiocbs
[ 0 ];
4680 my_aiocbp
->aio_fildes
= my_fd_list
[ 0 ];
4681 my_aiocbp
->aio_offset
= 4096;
4682 my_aiocbp
->aio_buf
= my_buffers
[ 0 ];
4683 my_aiocbp
->aio_nbytes
= AIO_TESTS_BUFFER_SIZE
;
4684 my_aiocbp
->aio_reqprio
= 0;
4685 my_aiocbp
->aio_sigevent
.sigev_notify
= 0;
4686 my_aiocbp
->aio_sigevent
.sigev_signo
= 0;
4687 my_aiocbp
->aio_sigevent
.sigev_value
.sival_int
= 0;
4688 my_aiocbp
->aio_sigevent
.sigev_notify_function
= NULL
;
4689 my_aiocbp
->aio_sigevent
.sigev_notify_attributes
= NULL
;
4690 my_aiocbp
->aio_lio_opcode
= 0;
4692 /* write some data */
4693 memset( my_buffers
[ 0 ], 'j', AIO_TESTS_BUFFER_SIZE
);
4694 my_err
= aio_write( my_aiocbp
);
4695 if ( my_err
!= 0 ) {
4696 printf( "aio_write failed with error %d - \"%s\" \n", my_err
, strerror( my_err
) );
4697 goto test_failed_exit
;
4701 my_err
= aio_error( my_aiocbp
);
4702 if ( my_err
== EINPROGRESS
) {
4703 /* wait for IO to complete */
4707 else if ( my_err
== 0 ) {
4709 my_result
= aio_return( my_aiocbp
);
4713 printf( "aio_error failed with error %d - \"%s\" \n", my_err
, strerror( my_err
) );
4714 goto test_failed_exit
;
4718 /* read some data */
4719 memset( my_buffers
[ 0 ], 'x', AIO_TESTS_BUFFER_SIZE
);
4720 my_err
= aio_read( my_aiocbp
);
4723 my_err
= aio_error( my_aiocbp
);
4724 if ( my_err
== EINPROGRESS
) {
4725 /* wait for IO to complete */
4729 else if ( my_err
== 0 ) {
4731 my_result
= aio_return( my_aiocbp
);
4733 if ( *(my_buffers
[ 0 ]) != 'j' || *(my_buffers
[ 0 ] + AIO_TESTS_BUFFER_SIZE
- 1) != 'j' ) {
4734 printf( "aio_read or aio_write failed - wrong data read \n" );
4735 goto test_failed_exit
;
4740 printf( "aio_read failed with error %d - \"%s\" \n", my_err
, strerror( my_err
) );
4741 goto test_failed_exit
;
4745 /* test aio_fsync */
4746 close( my_fd_list
[ 0 ] );
4747 my_fd_list
[ 0 ] = open( my_pathp
, O_RDWR
, 0 );
4748 if ( my_fd_list
[ 0 ] == -1 ) {
4749 printf( "open call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
4750 goto test_failed_exit
;
4753 my_aiocbp
= &my_aiocbs
[ 0 ];
4754 my_aiocbp
->aio_fildes
= my_fd_list
[ 0 ];
4755 my_aiocbp
->aio_offset
= 0;
4756 my_aiocbp
->aio_buf
= my_buffers
[ 0 ];
4757 my_aiocbp
->aio_nbytes
= 1024;
4758 my_aiocbp
->aio_reqprio
= 0;
4759 my_aiocbp
->aio_sigevent
.sigev_notify
= 0;
4760 my_aiocbp
->aio_sigevent
.sigev_signo
= 0;
4761 my_aiocbp
->aio_sigevent
.sigev_value
.sival_int
= 0;
4762 my_aiocbp
->aio_sigevent
.sigev_notify_function
= NULL
;
4763 my_aiocbp
->aio_sigevent
.sigev_notify_attributes
= NULL
;
4764 my_aiocbp
->aio_lio_opcode
= 0;
4766 /* write some data */
4767 memset( my_buffers
[ 0 ], 'e', 1024 );
4768 my_err
= aio_write( my_aiocbp
);
4769 if ( my_err
!= 0 ) {
4770 printf( "aio_write failed with error %d - \"%s\" \n", my_err
, strerror( my_err
) );
4771 goto test_failed_exit
;
4774 my_err
= aio_error( my_aiocbp
);
4775 if ( my_err
== EINPROGRESS
) {
4776 /* wait for IO to complete */
4780 else if ( my_err
== 0 ) {
4782 my_result
= aio_return( my_aiocbp
);
4786 printf( "aio_error failed with error %d - \"%s\" \n", my_err
, strerror( my_err
) );
4787 goto test_failed_exit
;
4791 my_err
= aio_fsync( O_SYNC
, my_aiocbp
);
4792 if ( my_err
!= 0 ) {
4793 printf( "aio_fsync failed with error %d - \"%s\" \n", my_err
, strerror( my_err
) );
4794 goto test_failed_exit
;
4797 my_err
= aio_error( my_aiocbp
);
4798 if ( my_err
== EINPROGRESS
) {
4799 /* wait for IO to complete */
4803 else if ( my_err
== 0 ) {
4804 aio_return( my_aiocbp
);
4808 printf( "aio_error failed with error %d - \"%s\" \n", my_err
, strerror( my_err
) );
4809 goto test_failed_exit
;
4813 /* validate write */
4814 memset( my_buffers
[ 0 ], 0x20, 16 );
4815 lseek( my_fd_list
[ 0 ], 0, SEEK_SET
);
4816 my_result
= read( my_fd_list
[ 0 ], my_buffers
[ 0 ], 16);
4817 if ( my_result
== -1 ) {
4818 printf( "read call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
4819 goto test_failed_exit
;
4821 if ( *(my_buffers
[ 0 ]) != 'e' || *(my_buffers
[ 0 ] + 16 - 1) != 'e' ) {
4822 printf( "aio_fsync or aio_write failed - wrong data read \n" );
4823 goto test_failed_exit
;
4826 /* test aio_suspend and lio_listio */
4827 for ( i
= 0; i
< AIO_TESTS_OUR_COUNT
; i
++ ) {
4828 memset( my_buffers
[ i
], 'a', AIO_TESTS_BUFFER_SIZE
);
4829 my_aiocbp
= &my_aiocbs
[ i
];
4830 my_aiocbp
->aio_nbytes
= AIO_TESTS_BUFFER_SIZE
;
4831 my_aiocbp
->aio_lio_opcode
= LIO_WRITE
;
4833 my_err
= lio_listio( LIO_NOWAIT
, my_aiocb_list
, AIO_TESTS_OUR_COUNT
, NULL
);
4834 if ( my_err
!= 0 ) {
4835 printf( "lio_listio call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
4836 goto test_failed_exit
;
4839 my_timeout
.tv_sec
= 1;
4840 my_timeout
.tv_nsec
= 0;
4841 my_err
= aio_suspend( (const struct aiocb
*const*) my_aiocb_list
, AIO_TESTS_OUR_COUNT
, &my_timeout
);
4842 if ( my_err
!= 0 ) {
4843 printf( "aio_suspend call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
4844 goto test_failed_exit
;
4847 /* test aio_cancel */
4848 for ( i
= 0; i
< AIO_TESTS_OUR_COUNT
; i
++ ) {
4849 my_aiocbp
= &my_aiocbs
[ i
];
4850 my_err
= aio_cancel( my_aiocbp
->aio_fildes
, my_aiocbp
);
4851 if ( my_err
!= AIO_ALLDONE
&& my_err
!= AIO_CANCELED
&& my_err
!= AIO_NOTCANCELED
) {
4852 printf( "aio_cancel failed with error %d - \"%s\" \n", my_err
, strerror( my_err
) );
4853 goto test_failed_exit
;
4858 goto test_passed_exit
;
4864 for ( i
= 0; i
< AIO_TESTS_OUR_COUNT
; i
++ ) {
4865 if ( my_fd_list
[ i
] != -1 ) {
4866 close( my_fd_list
[ i
] );
4867 my_fd_list
[ i
] = -1;
4869 if ( my_file_paths
[ i
] != NULL
) {
4870 remove( my_file_paths
[ i
] );
4871 vm_deallocate(mach_task_self(), (vm_address_t
)my_file_paths
[ i
], PATH_MAX
);
4872 my_file_paths
[ i
] = NULL
;
4874 if ( my_buffers
[ i
] != NULL
) {
4875 vm_deallocate(mach_task_self(), (vm_address_t
)my_buffers
[ i
], AIO_TESTS_BUFFER_SIZE
);
4876 my_buffers
[ i
] = NULL
;
4881 printf( "\t--> Not supported on EMBEDDED TARGET\n" );
4887 /* **************************************************************************************************************
4888 * Test msgctl, msgget, msgrcv, msgsnd system calls.
4889 * **************************************************************************************************************
4891 int message_queue_tests( void * the_argp
)
4893 #if !TARGET_OS_EMBEDDED
4895 int my_msg_queue_id
= -1;
4897 struct msqid_ds my_msq_ds
;
4898 struct testing_msq_message
{
4900 char msq_buffer
[ 32 ];
4903 /* get a message queue established for our use */
4904 my_msg_queue_id
= msgget( IPC_PRIVATE
, (IPC_CREAT
| IPC_EXCL
| IPC_R
| IPC_W
) );
4905 if ( my_msg_queue_id
== -1 ) {
4906 printf( "msgget failed with errno %d - %s \n", errno
, strerror( errno
) );
4907 goto test_failed_exit
;
4910 /* get some stats on our message queue */
4911 my_err
= msgctl( my_msg_queue_id
, IPC_STAT
, &my_msq_ds
);
4912 if ( my_err
== -1 ) {
4913 printf( "msgctl failed with errno %d - %s \n", errno
, strerror( errno
) );
4914 goto test_failed_exit
;
4916 if ( my_msq_ds
.msg_perm
.cuid
!= geteuid( ) ) {
4917 printf( "msgctl IPC_STAT failed to get correct creator uid \n" );
4918 goto test_failed_exit
;
4920 if ( (my_msq_ds
.msg_perm
.mode
& (IPC_R
| IPC_W
)) == 0 ) {
4921 printf( "msgctl IPC_STAT failed to get correct mode \n" );
4922 goto test_failed_exit
;
4925 /* put a message into our queue */
4926 my_msg
.msq_type
= 1;
4927 strcpy( &my_msg
.msq_buffer
[ 0 ], "testing 1, 2, 3" );
4928 my_err
= msgsnd( my_msg_queue_id
, &my_msg
, sizeof( my_msg
.msq_buffer
), 0 );
4929 if ( my_err
== -1 ) {
4930 printf( "msgsnd failed with errno %d - %s \n", errno
, strerror( errno
) );
4931 goto test_failed_exit
;
4934 my_err
= msgctl( my_msg_queue_id
, IPC_STAT
, &my_msq_ds
);
4935 if ( my_err
== -1 ) {
4936 printf( "msgctl failed with errno %d - %s \n", errno
, strerror( errno
) );
4937 goto test_failed_exit
;
4939 if ( my_msq_ds
.msg_qnum
!= 1 ) {
4940 printf( "msgctl IPC_STAT failed to get correct number of messages on the queue \n" );
4941 goto test_failed_exit
;
4944 /* pull message off the queue */
4945 bzero( (void *)&my_msg
, sizeof( my_msg
) );
4946 my_result
= msgrcv( my_msg_queue_id
, &my_msg
, sizeof( my_msg
.msq_buffer
), 0, 0 );
4947 if ( my_result
== -1 ) {
4948 printf( "msgrcv failed with errno %d - %s \n", errno
, strerror( errno
) );
4949 goto test_failed_exit
;
4951 if ( my_result
!= sizeof( my_msg
.msq_buffer
) ) {
4952 printf( "msgrcv failed to return the correct number of bytes in our buffer \n" );
4953 goto test_failed_exit
;
4955 if ( strcmp( &my_msg
.msq_buffer
[ 0 ], "testing 1, 2, 3" ) != 0 ) {
4956 printf( "msgrcv failed to get the correct message \n" );
4957 goto test_failed_exit
;
4960 my_err
= msgctl( my_msg_queue_id
, IPC_STAT
, &my_msq_ds
);
4961 if ( my_err
== -1 ) {
4962 printf( "msgctl failed with errno %d - %s \n", errno
, strerror( errno
) );
4963 goto test_failed_exit
;
4965 if ( my_msq_ds
.msg_qnum
!= 0 ) {
4966 printf( "msgctl IPC_STAT failed to get correct number of messages on the queue \n" );
4967 goto test_failed_exit
;
4970 /* tear down the message queue */
4971 my_err
= msgctl( my_msg_queue_id
, IPC_RMID
, NULL
);
4972 if ( my_err
== -1 ) {
4973 printf( "msgctl IPC_RMID failed with errno %d - %s \n", errno
, strerror( errno
) );
4974 goto test_failed_exit
;
4976 my_msg_queue_id
= -1;
4979 goto test_passed_exit
;
4985 if ( my_msg_queue_id
!= -1 ) {
4986 msgctl( my_msg_queue_id
, IPC_RMID
, NULL
);
4990 printf( "\t--> Not supported on EMBEDDED TARGET \n" );
4996 /* **************************************************************************************************************
4997 * Test execution from data and stack areas.
4998 * **************************************************************************************************************
5000 int data_exec_tests( void * the_argp
)
5005 if ((arch
= get_architecture()) == -1) {
5006 printf("data_exec_test: couldn't determine architecture\n");
5007 goto test_failed_exit
;
5013 * If the machine is 64-bit capable, run both the 32 and 64 bit versions of the test.
5014 * Otherwise, just run the 32-bit version.
5017 if (arch
== INTEL
) {
5019 if (system("arch -arch x86_64 helpers/data_exec") != 0) {
5020 printf("data_exec-x86_64 failed\n");
5021 goto test_failed_exit
;
5025 if (system("arch -arch i386 helpers/data_exec") != 0) {
5026 printf("data_exec-i386 failed\n");
5027 goto test_failed_exit
;
5031 if (arch
== POWERPC
) {
5032 if (system("arch -arch ppc helpers/data_exec") != 0) {
5033 printf("data_exec-ppc failed\n");
5034 goto test_failed_exit
;
5038 /* Add new architectures here similar to the above. */
5040 goto test_passed_exit
;
5050 #if TEST_SYSTEM_CALLS
5052 /* **************************************************************************************************************
5053 * Test xxxxxxxxx system calls.
5054 * **************************************************************************************************************
5056 int sample_test( void * the_argp
)
5060 char * my_pathp
= NULL
;
5061 kern_return_t my_kr
;
5063 my_kr
= vm_allocate((vm_map_t
) mach_task_self(), (vm_address_t
*)&my_pathp
, PATH_MAX
, VM_FLAGS_ANYWHERE
);
5064 if(my_kr
!= KERN_SUCCESS
){
5065 printf( "vm_allocate failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
5066 goto test_failed_exit
;
5070 strcat( my_pathp
, &g_target_path
[0] );
5071 strcat( my_pathp
, "/" );
5073 /* create a test file */
5074 my_err
= create_random_name( my_pathp
, 1 );
5075 if ( my_err
!= 0 ) {
5076 goto test_failed_exit
;
5079 /* add your test code here... */
5083 goto test_passed_exit
;
5091 if ( my_pathp
!= NULL
) {
5093 vm_deallocate(mach_task_self(), (vm_address_t
)my_pathp
, PATH_MAX
);