#include "tests.h"
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/syscall.h>
#include <sys/sysctl.h>
#include <sys/kas_info.h>
#include <AvailabilityMacros.h>
#include <libkern/OSByteOrder.h>
#include <mach/mach.h>
extern char g_target_path[ PATH_MAX ];
extern int g_skip_setuid_tests;
extern int g_is_single_user;
void print_acct_debug_strings( char * my_ac_comm );
#if TEST_SYSTEM_CALLS
"reboot",
"revoke",
"sbrk",
"sstk",
"mount",
"unmount",
"undelete",
"watchevent",
"waitevent",
"modwatch",
"fsctl",
"initgroups",
"semsys",
"semconfig",
"msgsys",
"shmsys",
"load_shared_file",
"reset_shared_file",
"new_system_shared_regions",
"shared_region_map_file_np",
"shared_region_make_private_np",
"__pthread_kill",
"pthread_sigmask",
"__disable_threadsignal",
"__pthread_markcancel",
"__pthread_canceled",
"__semwait_signal",
"audit",
"auditon",
"getaudit",
"setaudit",
"getaudit_addr",
"setaudit_addr",
"auditctl",
#endif
int syscall_test( void * the_argp )
{
int my_err;
int my_fd = -1;
char * my_pathp;
kern_return_t my_kr;
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
*my_pathp = 0x00;
strcpy( my_pathp, &g_target_path[0] );
strcat( my_pathp, "/" );
my_err = create_random_name( my_pathp, 1 );
if ( my_err != 0 ) {
goto test_failed_exit;
}
my_fd = syscall( SYS_open, my_pathp, (O_RDWR | O_EXCL), 0 );
if ( my_fd == -1 ) {
printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
printf( "\t file we attempted to open -> \"%s\" \n", my_pathp );
goto test_failed_exit;
}
my_err = 0;
goto test_passed_exit;
test_failed_exit:
my_err = -1;
test_passed_exit:
if ( my_fd != -1 )
close( my_fd );
if ( my_pathp != NULL ) {
remove( my_pathp );
vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
}
return( my_err );
}
int fork_wait4_exit_test( void * the_argp )
{
int my_err, my_status;
pid_t my_pid, my_wait_pid;
struct rusage my_usage;
my_pid = fork( );
if ( my_pid == -1 ) {
printf( "fork failed with errno %d - %s \n", errno, strerror( errno ) );
return( -1 );
}
else if ( my_pid == 0 ) {
struct stat my_sb;
my_err = stat( &g_target_path[0], &my_sb );
if ( my_err != 0 ) {
printf( "stat call failed with error %d - \"%s\" \n", errno, strerror( errno) );
printf( "\t path we stated \"%s\" \n", &g_target_path[0] );
exit( -1 );
}
exit( 44 );
}
my_wait_pid = wait4( my_pid, &my_status, 0, &my_usage );
if ( my_wait_pid == -1 ) {
printf( "wait4 failed with errno %d - %s \n", errno, strerror( errno ) );
return( -1 );
}
if ( my_wait_pid != my_pid ) {
printf( "wait4 did not return child pid - returned %d should be %d \n", my_wait_pid, my_pid );
return( -1 );
}
if ( my_usage.ru_utime.tv_sec > 1 || my_usage.ru_stime.tv_sec > 1 ||
my_usage.ru_majflt > 1000 || my_usage.ru_msgsnd > 100 ) {
printf( "wait4 returned an odd looking rusage structure \n" );
return( -1 );
}
if ( WIFEXITED( my_status ) && WEXITSTATUS( my_status ) == 44 ) {
}
else {
printf( "wait4 returned wrong exit status - 0x%02X \n", my_status );
return( -1 );
}
return( 0 );
}
int read_write_test( void * the_argp )
{
int my_fd = -1;
int my_err;
char * my_pathp = NULL;
char * my_bufp = NULL;
ssize_t my_result;
off_t my_current_offset;
struct iovec my_iovs[2];
struct stat my_sb;
kern_return_t my_kr;
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_bufp, MY_BUFFER_SIZE, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
*my_pathp = 0x00;
strcat( my_pathp, &g_target_path[0] );
strcat( my_pathp, "/" );
my_err = create_random_name( my_pathp, 1 );
if ( my_err != 0 ) {
goto test_failed_exit;
}
my_fd = open( my_pathp, O_RDONLY, 0 );
if ( my_fd == -1 ) {
printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
printf( "\t file we attempted to open -> \"%s\" \n", my_pathp );
goto test_failed_exit;
}
my_result = read( my_fd, my_bufp, 10);
if ( my_result == -1 ) {
printf( "read call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
if ( my_result != 0 ) {
if ( sizeof( ssize_t ) > sizeof( int ) ) {
printf( "read call failed - should have read 0 bytes on empty file - read %ld \n", (long int) my_result );
}
else {
printf( "read call failed - should have read 0 bytes on empty file - read %d \n", (int) my_result );
}
goto test_failed_exit;
}
my_result = write( my_fd, my_bufp, 10 );
my_err = errno;
if ( my_result != -1 ) {
if ( sizeof( ssize_t ) > sizeof( int ) ) {
printf( "write should have failed for read only fd - %ld \n", (long int) my_result );
}
else {
printf( "write should have failed for read only fd - %d \n", (int) my_result );
}
goto test_failed_exit;
}
if ( my_err != EBADF ) {
printf( "write call failed with error %d - \"%s\" \n", errno, strerror( errno) );
printf( "should have failed with EBADF error %d \n", EBADF );
goto test_failed_exit;
}
close( my_fd );
my_fd = open( my_pathp, O_RDWR, 0 );
if ( my_fd == -1 ) {
printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
printf( "\t file we attempted to open -> \"%s\" \n", my_pathp );
goto test_failed_exit;
}
memset( my_bufp, 'j', MY_BUFFER_SIZE );
my_result = write( my_fd, my_bufp, MY_BUFFER_SIZE );
if ( my_result == -1 ) {
printf( "write call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
if ( my_result != MY_BUFFER_SIZE ) {
printf( "write failed to write out all the data \n" );
goto test_failed_exit;
}
my_err = fsync( my_fd );
if ( my_err == -1 ) {
printf( "fsync failed with errno %d - %s \n", errno, strerror( errno ) );
goto test_failed_exit;
}
lseek( my_fd, 0, SEEK_SET );
bzero( (void *)my_bufp, MY_BUFFER_SIZE );
my_iovs[0].iov_base = my_bufp;
my_iovs[0].iov_len = 16;
my_iovs[1].iov_base = (my_bufp + MY_BUFFER_SIZE - 16) ;
my_iovs[1].iov_len = 16;
my_result = readv( my_fd, &my_iovs[0], 2 );
if ( my_result == -1 ) {
printf( "readv call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
if ( my_result != 32 ) {
printf( "readv failed to get all the data - asked for %d got back %d\n", MY_BUFFER_SIZE, (int) my_result );
goto test_failed_exit;
}
if ( *my_bufp != 'j' || *(my_bufp + (MY_BUFFER_SIZE - 1)) != 'j' ) {
printf( "readv failed to get correct data \n" );
goto test_failed_exit;
}
my_err = ftruncate( my_fd, 0 );
if ( my_err == -1 ) {
printf( "ftruncate call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_err = fstat( my_fd, &my_sb );
if ( my_err == -1 ) {
printf( "fstat call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
if ( my_sb.st_size != 0 ) {
printf( "ftruncate call failed - file size is wrong \n" );
goto test_failed_exit;
}
lseek( my_fd, 0, SEEK_SET );
memset( my_bufp, 'z', MY_BUFFER_SIZE );
my_iovs[0].iov_base = my_bufp;
my_iovs[0].iov_len = 8;
my_iovs[1].iov_base = (my_bufp + MY_BUFFER_SIZE - 8) ;
my_iovs[1].iov_len = 8;
my_result = writev( my_fd, &my_iovs[0], 2 );
if ( my_result == -1 ) {
printf( "writev call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
if ( my_result != 16 ) {
printf( "writev failed to get all the data - asked for %d got back %d\n", MY_BUFFER_SIZE, (int) my_result );
goto test_failed_exit;
}
lseek( my_fd, 0, SEEK_SET );
bzero( (void *)my_bufp, MY_BUFFER_SIZE );
my_iovs[0].iov_base = my_bufp;
my_iovs[0].iov_len = 8;
my_iovs[1].iov_base = (my_bufp + MY_BUFFER_SIZE - 8) ;
my_iovs[1].iov_len = 8;
my_result = readv( my_fd, &my_iovs[0], 2 );
if ( my_result == -1 ) {
printf( "readv call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
if ( my_result != 16 ) {
printf( "readv failed to get all the data - asked for %d got back %d\n", MY_BUFFER_SIZE, (int) my_result );
goto test_failed_exit;
}
if ( *my_bufp != 'z' || *(my_bufp + (MY_BUFFER_SIZE - 1)) != 'z' ) {
printf( "readv failed to get correct data \n" );
goto test_failed_exit;
}
my_current_offset = lseek( my_fd, 0, SEEK_CUR );
if ( my_current_offset == -1 ) {
printf( "lseek call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_result = pwrite( my_fd, "jer", 3, my_current_offset );
if ( my_result == -1 ) {
printf( "pwrite call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
if ( my_result != 3 ) {
printf( "pwrite failed to write all the data \n" );
goto test_failed_exit;
}
if ( my_current_offset != lseek( my_fd, 0, SEEK_CUR ) ) {
printf( "pwrite advanced file positiion \n" );
goto test_failed_exit;
}
bzero( (void *)my_bufp, MY_BUFFER_SIZE );
my_result = pread( my_fd, my_bufp, 3, my_current_offset );
if ( my_result == -1 ) {
printf( "pread call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
if ( my_result != 3 ) {
printf( "pread failed to write all the data \n" );
goto test_failed_exit;
}
if ( my_current_offset != lseek( my_fd, 0, SEEK_CUR ) ) {
printf( "pread advanced file positiion \n" );
goto test_failed_exit;
}
if ( strcmp( my_bufp, "jer" ) != 0 ) {
printf( "pread or pwrite failed to read / write correct data \n" );
goto test_failed_exit;
}
my_err = truncate( my_pathp, 0 );
if ( my_err == -1 ) {
printf( "truncate call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_err = stat( my_pathp, &my_sb );
if ( my_err == -1 ) {
printf( "stat call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
if ( my_sb.st_size != 0 ) {
printf( "truncate call failed - file size is wrong \n" );
goto test_failed_exit;
}
my_err = 0;
goto test_passed_exit;
test_failed_exit:
my_err = -1;
test_passed_exit:
if ( my_fd != -1 )
close( my_fd );
if ( my_pathp != NULL ) {
remove( my_pathp );
vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
}
if ( my_bufp != NULL )
vm_deallocate(mach_task_self(), (vm_address_t)my_bufp, MY_BUFFER_SIZE);
return( my_err );
}
int open_close_test( void * the_argp )
{
int my_err;
int my_fd = -1;
char * my_pathp = NULL;
ssize_t my_result;
long my_pconf_result;
struct stat my_sb;
char my_buffer[32];
kern_return_t my_kr;
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
*my_pathp = 0x00;
strcat( my_pathp, &g_target_path[0] );
strcat( my_pathp, "/" );
my_err = create_random_name( my_pathp, 1 );
if ( my_err != 0 ) {
goto test_failed_exit;
}
my_fd = open( my_pathp, O_WRONLY, 0 );
if ( my_fd == -1 ) {
printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
printf( "\t file we attempted to open -> \"%s\" \n", my_pathp );
goto test_failed_exit;
}
my_pconf_result = pathconf( my_pathp, _PC_PATH_MAX );
if ( my_pconf_result == -1 ) {
printf( "pathconf - _PC_PATH_MAX - failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
if ( my_pconf_result < PATH_MAX ) {
printf( "pathconf - _PC_PATH_MAX - looks like wrong results \n" );
goto test_failed_exit;
}
my_pconf_result = fpathconf( my_fd, _PC_NAME_MAX );
if ( my_pconf_result == -1 ) {
printf( "fpathconf - _PC_PATH_MAX - failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
if ( my_pconf_result < 6 ) {
printf( "fpathconf - _PC_NAME_MAX - looks like wrong results \n" );
goto test_failed_exit;
}
my_result = write( my_fd, "kat", 3 );
my_err = errno;
if ( my_result != 3 ) {
if ( sizeof( ssize_t ) > sizeof( int ) ) {
printf( "write failed. should have written 3 bytes actually wrote - %ld \n", (long int) my_result );
}
else {
printf( "write failed. should have written 3 bytes actually wrote - %d \n", (int) my_result );
}
goto test_failed_exit;
}
my_result = read( my_fd, &my_buffer[0], sizeof(my_buffer) );
my_err = errno;
if ( my_result != -1 ) {
printf( "read call should have failed with errno 9 (EBADF) \n" );
goto test_failed_exit;
}
else if ( my_err != EBADF ) {
printf( "read call should have failed with errno 9 (EBADF). actually failed with %d - \"%s\" \n", my_err, strerror( my_err) );
goto test_failed_exit;
}
close( my_fd );
my_fd = open( my_pathp, (O_RDWR | O_TRUNC | O_APPEND), 0 );
if ( my_fd == -1 ) {
printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
printf( "\t file we attempted to open -> \"%s\" \n", my_pathp );
goto test_failed_exit;
}
my_result = read( my_fd, &my_buffer[0], sizeof(my_buffer) );
if ( my_result == -1 ) {
printf( "read call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
if ( my_result != 0 ) {
printf( "read failed - should have read 0 bytes. \n" );
goto test_failed_exit;
}
my_result = write( my_fd, "kat", 3 );
my_err = errno;
if ( my_result != 3 ) {
if ( sizeof( ssize_t ) > sizeof( int ) ) {
printf( "write failed. should have written 3 bytes actually wrote - %ld \n", (long int) my_result );
}
else {
printf( "write failed. should have written 3 bytes actually wrote - %d \n", (int) my_result );
}
goto test_failed_exit;
}
lseek( my_fd, 0, SEEK_SET );
my_result = write( my_fd, "zzz", 3 );
my_err = errno;
if ( my_result != 3 ) {
if ( sizeof( ssize_t ) > sizeof( int ) ) {
printf( "write failed. should have written 3 bytes actually wrote - %ld \n", (long int) my_result );
}
else {
printf( "write failed. should have written 3 bytes actually wrote - %d \n", (int) my_result );
}
goto test_failed_exit;
}
bzero( (void *)&my_buffer[0], sizeof(my_buffer) );
lseek( my_fd, 0, SEEK_SET );
my_result = read( my_fd, &my_buffer[0], sizeof(my_buffer) );
if ( my_result == -1 ) {
printf( "read call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
if ( my_buffer[0] != 'k' || my_buffer[5] != 'z' ) {
printf( "read failed to get correct data \n" );
goto test_failed_exit;
}
my_err = fstat( my_fd, &my_sb );
if ( my_err == -1 ) {
printf( "fstat call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
if ( my_sb.st_size != 6 ) {
printf( "fstat call failed - st_size is wrong \n" );
goto test_failed_exit;
}
if ( !S_ISREG( my_sb.st_mode ) ) {
printf( "fstat call failed - st_mode does not indicate regular file \n" );
goto test_failed_exit;
}
my_err = 0;
goto test_passed_exit;
test_failed_exit:
my_err = -1;
test_passed_exit:
if ( my_fd != -1 )
close( my_fd );
if ( my_pathp != NULL ) {
remove( my_pathp );
vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
}
return( my_err );
}
int link_stat_unlink_test( void * the_argp )
{
int my_err;
int my_fd = -1;
char * my_pathp = NULL;
char * my_path2p = NULL;
nlink_t my_link_count;
ssize_t my_result;
struct stat my_sb;
kern_return_t my_kr;
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_path2p, PATH_MAX, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
*my_pathp = 0x00;
*my_path2p = 0x00;
strcat( my_pathp, &g_target_path[0] );
strcat( my_pathp, "/" );
my_err = create_random_name( my_pathp, 1 );
if ( my_err != 0 ) {
goto test_failed_exit;
}
strcat( my_path2p, my_pathp );
strcat( my_path2p, "link" );
my_err = stat( my_pathp, &my_sb );
if ( my_err != 0 ) {
printf( "stat call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
my_link_count = my_sb.st_nlink;
if ( my_sb.st_size != 0 ) {
printf( "stat structure looks bogus for test file \"%s\" \n", my_pathp );
printf( "st_size is not 0 \n" );
goto test_failed_exit;
}
my_fd = open( my_pathp, O_RDWR, 0 );
if ( my_fd == -1 ) {
printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
printf( "\t file we attempted to open -> \"%s\" \n", my_pathp );
goto test_failed_exit;
}
my_result = write( my_fd, "kat", 3 );
my_err = errno;
if ( my_result != 3 ) {
if ( sizeof( ssize_t ) > sizeof( int ) ) {
printf( "write failed. should have written 3 bytes actually wrote - %ld \n", (long int) my_result );
}
else {
printf( "write failed. should have written 3 bytes actually wrote - %d \n", (int) my_result );
}
goto test_failed_exit;
}
close( my_fd );
my_fd = -1;
my_err = link( my_pathp, my_path2p );
if ( my_err != 0 ) {
printf( "link call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
my_err = stat( my_pathp, &my_sb );
if ( my_err != 0 ) {
printf( "stat call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
if ( (my_link_count + 1) != my_sb.st_nlink ) {
printf( "stat structure looks bogus for test file \"%s\" \n", my_pathp );
printf( "incorrect st_nlink \n" );
goto test_failed_exit;
}
if ( my_sb.st_size != 3 ) {
printf( "stat structure looks bogus for test file \"%s\" \n", my_pathp );
printf( "st_size is not 3 \n" );
goto test_failed_exit;
}
my_err = unlink( my_path2p );
if ( my_err != 0 ) {
printf( "unlink call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
my_err = stat( my_pathp, &my_sb );
if ( my_err != 0 ) {
printf( "stat call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
if ( my_link_count != my_sb.st_nlink ) {
printf( "stat structure looks bogus for test file \"%s\" \n", my_pathp );
printf( "incorrect st_nlink \n" );
goto test_failed_exit;
}
my_err = 0;
goto test_passed_exit;
test_failed_exit:
my_err = -1;
test_passed_exit:
if ( my_fd != -1 )
close( my_fd );
if ( my_pathp != NULL ) {
remove( my_pathp );
vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
}
if ( my_path2p != NULL ) {
remove( my_path2p );
vm_deallocate(mach_task_self(), (vm_address_t)my_path2p, PATH_MAX);
}
return( my_err );
}
int chdir_fchdir_test( void * the_argp )
{
int my_err;
int my_fd = -1;
char * my_pathp = NULL;
char * my_file_namep;
struct stat my_sb;
struct stat my_sb2;
kern_return_t my_kr;
char *cwd = getwd(NULL);
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
*my_pathp = 0x00;
strcat( my_pathp, &g_target_path[0] );
strcat( my_pathp, "/" );
my_err = create_random_name( my_pathp, 1 );
if ( my_err != 0 ) {
goto test_failed_exit;
}
my_err = stat( my_pathp, &my_sb );
if ( my_err != 0 ) {
printf( "stat call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
my_err = chdir( &g_target_path[0] );
if ( my_err != 0 ) {
printf( "chdir call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
my_file_namep = strrchr( my_pathp, '/' );
my_file_namep++;
my_err = stat( my_file_namep, &my_sb2 );
if ( my_err != 0 ) {
printf( "stat call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
if ( my_sb.st_ino != my_sb2.st_ino || my_sb.st_size != my_sb2.st_size ||
my_sb.st_mtimespec.tv_sec != my_sb2.st_mtimespec.tv_sec ||
my_sb.st_mtimespec.tv_nsec != my_sb2.st_mtimespec.tv_nsec ) {
printf( "chdir call appears to have failed. stat buffer contents do not match! \n" );
goto test_failed_exit;
}
my_err = chdir( "/" );
if ( my_err != 0 ) {
printf( "chdir call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
my_err = stat( my_file_namep, &my_sb2 );
if ( my_err == 0 ) {
printf( "chdir to root volume has failed \n" );
goto test_failed_exit;
}
my_fd = open( &g_target_path[0], O_RDONLY, 0 );
if ( my_fd == -1 ) {
printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
printf( "\t we attempted to open -> \"%s\" \n", &g_target_path[0] );
goto test_failed_exit;
}
my_err = fchdir( my_fd );
if ( my_err == -1 ) {
printf( "fchdir call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
my_err = stat( my_file_namep, &my_sb2 );
if ( my_err != 0 ) {
printf( "stat call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
if ( my_sb.st_ino != my_sb2.st_ino || my_sb.st_size != my_sb2.st_size ||
my_sb.st_mtimespec.tv_sec != my_sb2.st_mtimespec.tv_sec ||
my_sb.st_mtimespec.tv_nsec != my_sb2.st_mtimespec.tv_nsec ) {
printf( "chdir call appears to have failed. stat buffer contents do not match! \n" );
goto test_failed_exit;
}
my_err = 0;
goto test_passed_exit;
test_failed_exit:
my_err = -1;
test_passed_exit:
if ( my_fd != -1 )
close( my_fd );
if ( my_pathp != NULL ) {
remove( my_pathp );
vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
}
if ( chdir(cwd) != 0)
my_err = -1;
return( my_err );
}
int access_chmod_fchmod_test( void * the_argp )
{
int error_occurred;
int my_err;
int my_fd = -1;
char * my_pathp = NULL;
uid_t euid,ruid;
struct stat my_sb;
FILE * file_handle;
kern_return_t my_kr;
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
*my_pathp = 0x00;
strcat( my_pathp, &g_target_path[0] );
strcat( my_pathp, "/" );
my_err = create_random_name( my_pathp, 1 );
if ( my_err != 0 ) {
goto test_failed_exit;
}
my_err = chmod( my_pathp, S_IRWXU );
if ( my_err == -1 ) {
printf( "chmod call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
my_err = chmod( my_pathp, (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP) );
if ( my_err == -1 ) {
printf( "chmod call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
my_err = access( my_pathp, (X_OK) );
if ( my_err == 0 ) {
printf( "access call should have failed, but did not. \n" );
goto test_failed_exit;
}
else if ( my_err == -1 ) {
int tmp = 0;
tmp = getuid( );
my_err = errno;
if ( ( tmp == 0 && my_err != EPERM) || (tmp != 0 && my_err != EACCES) ) {
printf( "access failed with errno %d - %s. \n", my_err, strerror( my_err ) );
goto test_failed_exit;
}
}
my_err = stat( my_pathp, &my_sb );
if ( my_err != 0 ) {
printf( "stat call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
if ( (my_sb.st_mode & (S_IRWXO | S_IXGRP)) != 0 ||
(my_sb.st_mode & (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP)) == 0 ) {
printf( "chmod call appears to have failed. stat shows incorrect values in st_mode! \n" );
goto test_failed_exit;
}
file_handle = fopen(FILE_NOTME, "w");
fclose(file_handle);
euid = geteuid();
ruid = getuid();
setreuid(ruid, ruid);
file_handle = fopen(FILE_ME, "w");
fclose(file_handle);
error_occurred = 0;
my_err = unlink(FILE_NOTME);
if (my_err < 0) {
my_err = errno;
}
if (my_err == 0) {
printf("Unresolved: First attempt deleted '" FILE_NOTME "'! \n");
error_occurred = 1;
} else {
printf("Status: First attempt to delete '" FILE_NOTME "' failed with error %d - %s.\n", my_err, strerror( my_err ));
access(FILE_ME, _DELETE_OK);
my_err = unlink(FILE_NOTME);
if (my_err < 0) {
my_err = errno;
}
if (my_err == 0) {
printf("Failed: Second attempt deleted '" FILE_NOTME "'!\n");
error_occurred = 1;
} else if (my_err == 13) {
printf("Passed: Second attempt to delete '" FILE_NOTME "' failed with error %d - %s.\n", my_err, strerror( my_err ));
} else {
printf("Failed: Second attempt to delete '" FILE_NOTME "' failed with error %d - %s.\n", my_err, strerror( my_err ));
error_occurred = 1;
}
}
setreuid(ruid, euid);
if(error_occurred == 1) {
goto test_failed_exit;
}
my_fd = open( my_pathp, O_RDONLY, 0 );
if ( my_fd == -1 ) {
printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
printf( "\t we attempted to open -> \"%s\" \n", &g_target_path[0] );
goto test_failed_exit;
}
my_err = fchmod( my_fd, S_IRWXU );
if ( my_err == -1 ) {
printf( "fchmod call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
my_err = stat( my_pathp, &my_sb );
if ( my_err != 0 ) {
printf( "stat call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
if ( (my_sb.st_mode & (S_IRWXG | S_IRWXO)) != 0 ||
(my_sb.st_mode & (S_IRWXU)) == 0 ) {
printf( "fchmod call appears to have failed. stat shows incorrect values in st_mode! \n" );
goto test_failed_exit;
}
my_err = 0;
goto test_passed_exit;
test_failed_exit:
my_err = -1;
test_passed_exit:
if ( my_fd != -1 )
close( my_fd );
if ( my_pathp != NULL ) {
remove( my_pathp );
vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
}
return( my_err );
}
static bool _prime_groups(void)
{
gid_t my_exp_groups[] = { getegid(), 20, 61, 12 };
int my_err;
my_err = setgroups( ( sizeof(my_exp_groups) / sizeof(*my_exp_groups) ), &my_exp_groups[0] );
if ( my_err == -1 ) {
printf( "initial setgroups call failed. got errno %d - %s. \n", errno, strerror( errno ) );
return false;
}
return true;
}
int chown_fchown_lchown_lstat_symlink_test( void * the_argp )
{
int my_err, my_group_count, i;
int my_fd = -1;
char * my_pathp = NULL;
char * my_link_pathp = NULL;
uid_t my_orig_uid;
gid_t my_orig_gid, my_new_gid1 = 0, my_new_gid2 = 0;
ssize_t my_result;
struct stat my_sb;
gid_t my_groups[ NGROUPS_MAX ];
char my_buffer[ 64 ];
kern_return_t my_kr;
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
*my_pathp = 0x00;
strcat( my_pathp, &g_target_path[0] );
strcat( my_pathp, "/" );
my_err = create_random_name( my_pathp, 1 );
if ( my_err != 0 ) {
goto test_failed_exit;
}
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_link_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
*my_link_pathp = 0x00;
strcat( my_link_pathp, &g_target_path[0] );
strcat( my_link_pathp, "/" );
my_err = create_random_name( my_link_pathp, 0 );
if ( my_err != 0 ) {
goto test_failed_exit;
}
if ( !_prime_groups() ) {
goto test_failed_exit;
}
my_group_count = getgroups( NGROUPS_MAX, &my_groups[0] );
if ( my_group_count == -1 || my_group_count < 1 ) {
printf( "getgroups call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
my_err = stat( my_pathp, &my_sb );
if ( my_err != 0 ) {
printf( "stat call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
my_orig_gid = my_sb.st_gid;
my_orig_uid = my_sb.st_uid;
for ( i = 0; i < my_group_count; i++ ) {
if ( my_orig_gid != my_groups[ i ] ) {
if ( my_new_gid1 == 0 ) {
my_new_gid1 = my_groups[ i ];
}
else if( my_new_gid1 != my_groups[ i ] ) {
my_new_gid2 = my_groups[ i ];
break;
}
}
}
if ( i >= my_group_count ) {
printf( "not enough groups to choose from. st_gid is the same as current groups! \n" );
goto test_failed_exit;
}
my_err = chown( my_pathp, my_orig_uid, my_new_gid1 );
if ( my_err != 0 ) {
printf( "chown call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
my_err = stat( my_pathp, &my_sb );
if ( my_err != 0 ) {
printf( "stat call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
if ( my_sb.st_gid == my_orig_gid ) {
printf( "chown call failed. st_gid is not correct! \n" );
goto test_failed_exit;
}
my_fd = open( my_pathp, O_RDWR, 0 );
if ( my_fd == -1 ) {
printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
printf( "\t we attempted to open -> \"%s\" \n", &g_target_path[0] );
goto test_failed_exit;
}
my_err = fchown( my_fd, my_orig_uid, my_new_gid2 );
if ( my_err != 0 ) {
printf( "fchown call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
my_err = stat( my_pathp, &my_sb );
if ( my_err != 0 ) {
printf( "stat call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
if ( my_sb.st_gid == my_new_gid1 ) {
printf( "fchown call failed. st_gid is not correct! \n" );
goto test_failed_exit;
}
my_err = symlink( my_pathp, my_link_pathp );
if ( my_err != 0 ) {
printf( "symlink call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
my_err = lstat( my_link_pathp, &my_sb );
if ( my_err != 0 ) {
printf( "lstat call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
my_orig_gid = my_sb.st_gid;
my_orig_uid = my_sb.st_uid;
my_err = lchown( my_link_pathp, my_orig_uid, my_new_gid1 );
if ( my_err != 0 ) {
printf( "lchown call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
my_err = lstat( my_link_pathp, &my_sb );
if ( my_err != 0 ) {
printf( "lstat call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
if ( my_sb.st_gid == my_new_gid2 ) {
printf( "lchown call failed. st_gid is not correct! \n" );
goto test_failed_exit;
}
my_result = readlink( my_link_pathp, &my_buffer[0], sizeof(my_buffer) );
if ( my_result == -1 ) {
printf( "readlink call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
if ( my_result < 1 ) {
printf( "readlink failed to read any data. \n" );
goto test_failed_exit;
}
my_err = 0;
goto test_passed_exit;
test_failed_exit:
my_err = -1;
test_passed_exit:
if ( my_fd != -1 )
close( my_fd );
if ( my_pathp != NULL ) {
remove( my_pathp );
vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
}
if ( my_link_pathp != NULL ) {
unlink( my_link_pathp );
vm_deallocate(mach_task_self(), (vm_address_t)my_link_pathp, PATH_MAX);
}
return( my_err );
}
#pragma pack(4)
struct vol_attr_buf {
u_int32_t length;
off_t volume_size;
u_int32_t io_blksize;
};
#pragma pack()
typedef struct vol_attr_buf vol_attr_buf;
#define STATFS_TEST_PATH "/tmp"
int fs_stat_tests( void * the_argp )
{
int my_err, my_count, i;
int my_buffer_size, my_buffer64_size;
int my_fd = -1;
int is_ufs = 0;
long my_io_size;
fsid_t my_fsid;
struct attrlist my_attrlist;
vol_attr_buf my_attr_buf;
void * my_bufferp = NULL;
struct statfs * my_statfsp;
kern_return_t my_kr;
void * my_buffer64p = NULL;
struct statfs64 * my_statfs64p;
my_buffer64_size = (sizeof(struct statfs64) * 10);
my_kr = vm_allocate((vm_map_t) mach_task_self(),(vm_address_t*) &my_buffer64p, my_buffer64_size, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_buffer_size = (sizeof(struct statfs) * 10);
my_kr = vm_allocate((vm_map_t) mach_task_self(),(vm_address_t*) &my_bufferp, my_buffer_size, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_statfsp = (struct statfs *) my_bufferp;
my_err = statfs( STATFS_TEST_PATH, my_statfsp );
if ( my_err == -1 ) {
printf( "statfs call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
if ( memcmp( &my_statfsp->f_fstypename[0], "ufs", 3 ) == 0 ) {
is_ufs = 1;
}
my_count = getfsstat( (struct statfs *)my_bufferp, my_buffer_size, MNT_NOWAIT );
if ( my_count == -1 ) {
printf( "getfsstat call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
my_statfsp = (struct statfs *) my_bufferp;
for ( i = 0; i < my_count; i++, my_statfsp++ ) {
if ( memcmp( &my_statfsp->f_fstypename[0], "hfs", 3 ) == 0 ||
memcmp( &my_statfsp->f_fstypename[0], "ufs", 3 ) == 0 ||
memcmp( &my_statfsp->f_fstypename[0], "devfs", 5 ) == 0 ||
memcmp( &my_statfsp->f_fstypename[0], "volfs", 5 ) == 0 ) {
break;
}
}
if ( i >= my_count ) {
printf( "getfsstat call failed. could not find valid f_fstypename! \n" );
goto test_failed_exit;
}
my_statfs64p = (struct statfs64 *) my_buffer64p;
my_err = statfs64( STATFS_TEST_PATH, my_statfs64p );
if ( my_err == -1 ) {
printf( "statfs64 call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
if ( my_statfs64p->f_fsid.val[0] != my_statfsp->f_fsid.val[0] ||
my_statfs64p->f_fsid.val[1] != my_statfsp->f_fsid.val[1] ) {
printf( "statfs64 call failed. wrong f_fsid! \n" );
goto test_failed_exit;
}
my_count = getfsstat64( (struct statfs64 *)my_buffer64p, my_buffer64_size, MNT_NOWAIT );
if ( my_count == -1 ) {
printf( "getfsstat64 call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
my_statfs64p = (struct statfs64 *) my_buffer64p;
for ( i = 0; i < my_count; i++, my_statfs64p++ ) {
if ( memcmp( &my_statfs64p->f_fstypename[0], "hfs", 3 ) == 0 ||
memcmp( &my_statfs64p->f_fstypename[0], "ufs", 3 ) == 0 ||
memcmp( &my_statfs64p->f_fstypename[0], "devfs", 5 ) == 0 ||
memcmp( &my_statfs64p->f_fstypename[0], "volfs", 5 ) == 0 ) {
break;
}
}
if ( i >= my_count ) {
printf( "getfsstat64 call failed. could not find valid f_fstypename! \n" );
goto test_failed_exit;
}
if ( is_ufs == 0 ) {
memset( &my_attrlist, 0, sizeof(my_attrlist) );
my_attrlist.bitmapcount = ATTR_BIT_MAP_COUNT;
my_attrlist.volattr = (ATTR_VOL_SIZE | ATTR_VOL_IOBLOCKSIZE);
my_err = getattrlist( "/", &my_attrlist, &my_attr_buf, sizeof(my_attr_buf), 0 );
if ( my_err != 0 ) {
printf( "getattrlist call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
}
my_fd = open( STATFS_TEST_PATH, O_RDONLY, 0 );
if ( my_fd == -1 ) {
printf( "open call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
my_statfs64p = (struct statfs64 *) my_buffer64p;
my_err = fstatfs64( my_fd, my_statfs64p );
if ( my_err == -1 ) {
printf( "fstatfs64 call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
if ( !(memcmp( &my_statfs64p->f_fstypename[0], "hfs", 3 ) == 0 ||
memcmp( &my_statfs64p->f_fstypename[0], "ufs", 3 ) == 0) ) {
printf( "fstatfs64 call failed. could not find valid f_fstypename! \n" );
goto test_failed_exit;
}
my_statfsp = (struct statfs *) my_bufferp;
my_err = fstatfs( my_fd, my_statfsp );
if ( my_err == -1 ) {
printf( "fstatfs call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
if ( !(memcmp( &my_statfsp->f_fstypename[0], "hfs", 3 ) == 0 ||
memcmp( &my_statfsp->f_fstypename[0], "ufs", 3 ) == 0) ) {
printf( "fstatfs call failed. could not find valid f_fstypename! \n" );
goto test_failed_exit;
}
my_io_size = my_statfsp->f_iosize;
my_fsid = my_statfsp->f_fsid;
if ( is_ufs == 0 && my_statfsp->f_iosize != my_attr_buf.io_blksize ) {
printf( "fstatfs and getattrlist results do not match for volume block size \n" );
goto test_failed_exit;
}
my_err = statfs( STATFS_TEST_PATH , my_statfsp );
if ( my_err == -1 ) {
printf( "statfs call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
if ( my_io_size != my_statfsp->f_iosize || my_fsid.val[0] != my_statfsp->f_fsid.val[0] ||
my_fsid.val[1] != my_statfsp->f_fsid.val[1] ) {
printf( "statfs call failed. wrong f_iosize or f_fsid! \n" );
goto test_failed_exit;
}
if ( is_ufs == 0 && my_statfsp->f_iosize != my_attr_buf.io_blksize ) {
printf( "statfs and getattrlist results do not match for volume block size \n" );
goto test_failed_exit;
}
my_err = 0;
goto test_passed_exit;
test_failed_exit:
my_err = -1;
test_passed_exit:
if ( my_fd != -1 )
close( my_fd );
if ( my_bufferp != NULL ) {
vm_deallocate(mach_task_self(), (vm_address_t)my_bufferp, my_buffer_size);
}
if ( my_buffer64p != NULL ) {
vm_deallocate(mach_task_self(), (vm_address_t)my_buffer64p, my_buffer64_size);
}
return( my_err );
}
int getpid_getppid_pipe_test( void * the_argp )
{
int my_err, my_status;
pid_t my_pid, my_wait_pid;
ssize_t my_count;
int my_fildes[2] = {-1, -1};
off_t my_current_offset;
char my_pid_string[64];
my_err = pipe( &my_fildes[0] );
if ( my_err != 0 ) {
printf( "pipe call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
my_current_offset = lseek( my_fildes[0], 0, SEEK_CUR );
if ( my_current_offset != -1 ) {
printf( "lseek on pipe should fail but did not \n" );
goto test_failed_exit;
}
my_pid = fork( );
if ( my_pid == -1 ) {
printf( "fork failed with errno %d - %s \n", errno, strerror( errno ) );
goto test_failed_exit;
}
else if ( my_pid == 0 ) {
unsigned long my_ppid;
char my_buffer[64];
close( my_fildes[1] );
my_fildes[1] = -1;
my_count = read( my_fildes[0], &my_buffer[0], sizeof(my_buffer) );
if ( my_count == -1 ) {
printf( "read from pipe failed. got errno %d - %s. \n", errno, strerror( errno ) );
exit(-1);
}
my_ppid = strtoul( &my_buffer[0], NULL, 10 );
if ( my_ppid == 0 ) {
printf( "strtoul failed. got errno %d - %s. \n", errno, strerror( errno ) );
exit(-1);
}
if ( getppid( ) != my_ppid ) {
printf( "getppid failed. pid we got from parent does not match getppid result. \n" );
exit(-1);
}
exit(0);
}
close( my_fildes[0] );
my_fildes[0] = -1;
sprintf( &my_pid_string[0], "%d\n", getpid( ) );
my_count = write( my_fildes[1], &my_pid_string[0], sizeof(my_pid_string) );
if ( my_count == -1 ) {
printf( "write to pipe failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
my_wait_pid = wait4( my_pid, &my_status, 0, NULL );
if ( my_wait_pid == -1 ) {
printf( "wait4 failed with errno %d - %s \n", errno, strerror( errno ) );
goto test_failed_exit;
}
if ( my_wait_pid != my_pid ) {
printf( "wait4 did not return child pid - returned %d should be %d \n", my_wait_pid, my_pid );
goto test_failed_exit;
}
if ( WIFEXITED( my_status ) && WEXITSTATUS( my_status ) != 0 ) {
printf( "wait4 returned wrong exit status - 0x%02X \n", my_status );
goto test_failed_exit;
}
my_err = 0;
goto test_passed_exit;
test_failed_exit:
my_err = -1;
test_passed_exit:
if ( my_fildes[0] != -1 )
close( my_fildes[0] );
if ( my_fildes[1] != -1 )
close( my_fildes[1] );
return( my_err );
}
int uid_tests( void * the_argp )
{
int my_err, my_status;
pid_t my_pid, my_wait_pid;
if ( g_skip_setuid_tests != 0 ) {
printf("\t skipping this test \n");
my_err = 0;
goto test_passed_exit;
}
if (!g_is_single_user) {
my_err = issetugid( );
if ( getuid( ) == 0 ) {
if ( my_err == 1 ) {
printf( "issetugid should return false \n" );
goto test_failed_exit;
}
}
else {
if ( my_err == 0 ) {
printf( "issetugid should return true \n" );
goto test_failed_exit;
}
}
}
my_pid = fork( );
if ( my_pid == -1 ) {
printf( "fork failed with errno %d - %s \n", errno, strerror( errno ) );
goto test_failed_exit;
}
else if ( my_pid == 0 ) {
uid_t my_ruid, my_euid;
uid_t my_uid, my_temp_uid;
gid_t my_gid, my_temp_gid;
auditinfo_addr_t my_aia;
my_ruid = getuid( );
my_euid = geteuid( );
if ( my_ruid == my_euid ) {
exit( 0 );
}
my_uid = getuid( );
my_gid = getgid( );
my_err = syscall( SYS_settid, 4444, 5555 );
if (my_err != 0) {
printf( "settid call failed with error %d - \"%s\" \n", errno, strerror( errno) );
exit( -1 );
}
my_err = syscall( SYS_gettid, &my_temp_uid, &my_temp_gid );
if (my_err != 0) {
printf( "gettid call failed with error %d - \"%s\" \n", errno, strerror( errno) );
exit( -1 );
}
if (my_temp_uid != 4444) {
printf("get / settid test failed - wrong uid was set - %d \n", my_temp_uid);
exit( -1 );
}
if (my_temp_gid != 5555) {
printf("get / settid test failed - wrong gid was set - %d \n", my_temp_gid);
exit( -1 );
}
my_err = syscall( SYS_settid, KAUTH_UID_NONE, KAUTH_GID_NONE );
if (my_err != 0) {
printf( "settid revert - failed with error %d - \"%s\" \n", errno, strerror( errno) );
exit( -1 );
}
my_temp_uid = getuid( );
if (my_temp_uid == 4444) {
printf("test failed - wrong uid was set - %d \n", my_temp_uid);
exit( -1 );
}
my_temp_gid = getgid( );
if (my_temp_gid == 5555) {
printf("test failed - wrong gid was set - %d \n", my_temp_gid);
exit( -1 );
}
my_err = syscall( SYS_settid_with_pid, getppid( ), 1 );
if (my_err != 0) {
printf( "settid_with_pid assume - failed with error %d - \"%s\" \n", errno, strerror( errno) );
exit( -1 );
}
my_err = syscall( SYS_settid_with_pid, 0, 0 );
if (my_err != 0) {
printf( "settid_with_pid resume - failed with error %d - \"%s\" \n", errno, strerror( errno) );
exit( -1 );
}
bzero( &my_aia, sizeof(my_aia) );
my_aia.ai_auid = 442344;
my_aia.ai_asid = AU_ASSIGN_ASID;
my_aia.ai_termid.at_type = AU_IPv4;
my_err = setaudit_addr( &my_aia, sizeof(my_aia) );
if (my_err != 0) {
printf( "setaudit_addr - failed with error %d - \"%s\" \n", errno, strerror( errno) );
exit( -1 );
}
my_aia.ai_auid = 0;
my_err = getaudit_addr( &my_aia, sizeof(my_aia) );
if (my_err != 0) {
printf( "getaudit_addr - failed with error %d - \"%s\" \n", errno, strerror( errno) );
exit( -1 );
}
if (my_aia.ai_auid != 442344) {
printf("test failed - wrong audit ID was set - %d \n", my_aia.ai_auid);
exit( -1 );
}
my_err = setuid( my_euid );
if ( my_err == -1 ) {
printf( "setuid call failed with error %d - \"%s\" \n", errno, strerror( errno) );
exit( -1 );
}
if ( getuid( ) != my_euid ) {
printf( "setuid call failed to set the real uid \n" );
exit( -1 );
}
my_err = seteuid( my_euid );
if ( my_err == -1 ) {
printf( "seteuid call failed with error %d - \"%s\" \n", errno, strerror( errno) );
exit( -1 );
}
if ( geteuid( ) != my_euid ) {
printf( "seteuid call failed to set the original euid \n" );
exit( -1 );
}
my_err = setuid( my_ruid );
if ( my_err == -1 ) {
printf( "setuid call failed with error %d - \"%s\" \n", errno, strerror( errno) );
exit( -1 );
}
if ( getuid( ) != my_ruid ) {
printf( "setuid call failed to set the real uid \n" );
exit( -1 );
}
exit(0);
}
my_wait_pid = wait4( my_pid, &my_status, 0, NULL );
if ( my_wait_pid == -1 ) {
printf( "wait4 failed with errno %d - %s \n", errno, strerror( errno ) );
goto test_failed_exit;
}
if ( my_wait_pid != my_pid ) {
printf( "wait4 did not return child pid - returned %d should be %d \n", my_wait_pid, my_pid );
goto test_failed_exit;
}
if ( WIFEXITED( my_status ) && WEXITSTATUS( my_status ) != 0 ) {
printf( "wait4 returned wrong exit status - 0x%02X \n", my_status );
goto test_failed_exit;
}
my_err = 0;
goto test_passed_exit;
test_failed_exit:
my_err = -1;
test_passed_exit:
return( my_err );
}
int mknod_sync_test( void * the_argp )
{
int my_err;
char * my_pathp = NULL;
kern_return_t my_kr;
if ( g_skip_setuid_tests != 0 ) {
printf("\t skipping this test \n");
my_err = 0;
goto test_passed_exit;
}
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
*my_pathp = 0x00;
strcat( my_pathp, "/dev/" );
my_err = create_random_name( my_pathp, 0 );
if ( my_err != 0 ) {
goto test_failed_exit;
}
my_err = mknod( my_pathp, (S_IFCHR | S_IRWXU), 0 );
if ( my_err == -1 ) {
printf( "mknod failed with errno %d - %s \n", errno, strerror( errno ) );
printf( "path \"%s\" \n", my_pathp );
goto test_failed_exit;
}
sync( );
my_err = 0;
goto test_passed_exit;
test_failed_exit:
my_err = -1;
test_passed_exit:
if ( my_pathp != NULL ) {
remove( my_pathp );
vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
}
return( my_err );
}
int chflags_fchflags_test( void * the_argp )
{
int my_err;
int my_fd = -1;
u_int my_flags;
char * my_pathp = NULL;
struct stat my_sb;
kern_return_t my_kr;
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
*my_pathp = 0x00;
strcat( my_pathp, &g_target_path[0] );
strcat( my_pathp, "/" );
my_err = create_random_name( my_pathp, 1 );
if ( my_err != 0 ) {
goto test_failed_exit;
}
my_err = stat( my_pathp, &my_sb );
if ( my_err != 0 ) {
printf( "stat call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_flags = (my_sb.st_flags | UF_IMMUTABLE);
my_err = chflags( my_pathp, my_flags );
if ( my_err != 0 ) {
printf( "chflags call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_fd = open( my_pathp, O_RDWR, 0 );
if ( my_fd == -1 && errno != EPERM ) {
printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
printf( "open failed with wrong error - should be EPERM \n" );
goto test_failed_exit;
}
my_fd = open( my_pathp, O_RDONLY, 0 );
if ( my_fd == -1 ) {
printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_err = stat( my_pathp, &my_sb );
if ( my_err != 0 ) {
printf( "stat call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_flags = (my_sb.st_flags & ~UF_IMMUTABLE);
my_err = fchflags( my_fd, my_flags );
if ( my_err != 0 ) {
printf( "chflags call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
close( my_fd );
my_fd = -1;
my_fd = open( my_pathp, O_RDWR, 0 );
if ( my_fd == -1 ) {
printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_err = 0;
goto test_passed_exit;
test_failed_exit:
my_err = -1;
test_passed_exit:
if ( my_fd != -1 )
close( my_fd );
if ( my_pathp != NULL ) {
remove( my_pathp );
vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
}
return( my_err );
}
int execve_kill_vfork_test( void * the_argp )
{
int my_err, my_status;
pid_t my_pid, my_wait_pid;
char * errmsg = NULL;
char * argvs[2] = {"", NULL};
int bits = get_bits();
if (bits != 32 && bits != 64) {
printf("Determination of processor bit-ness failed, get_bits() returned %d.\n", get_bits());
return(-1);
}
if (get_architecture() == -1) {
errmsg = "get_architecture() could not determine the CPU architecture.\n";
goto test_failed_exit;
}
if (get_architecture() == INTEL) {
struct stat sb;
if (bits == 64 && sizeof(long) == 8) {
errmsg = "execve failed: from x86_64 forking and exec()ing 64-bit x86_64 process w/ 4G pagezero.\n";
argvs[0] = "sleep-x86_64-4G";
if (do_execve_test("helpers/sleep-x86_64-4G", argvs, NULL, 1)) goto test_failed_exit;
errmsg = "execve failed: from x86_64 forking and exec()ing 64-bit x86_64 process w/ 4K Pagezero.\n";
argvs[0] = "sleep-x86_64-4K";
if (do_execve_test("helpers/sleep-x86_64-4K", argvs, NULL, 1)) goto test_failed_exit;
errmsg = "execve failed: from x64_64 forking and exec()ing 32-bit i386 process.\n";
argvs[0] = "sleep-i386";
if (do_execve_test("helpers/sleep-i386", argvs, NULL, 1)) goto test_failed_exit;
errmsg = "execve failed to exec the helper process.\n";
argvs[0] = "launch-i386";
if (do_execve_test("helpers/launch-i386", argvs, NULL, 1) != 0) goto test_failed_exit;
errmsg = NULL;
if (do_spawn_test(CPU_TYPE_I386, 0))
goto test_failed_exit;
if (do_spawn_test(CPU_TYPE_X86_64, 0))
goto test_failed_exit;
}
else if (bits == 64 && sizeof(long) == 4) {
errmsg = "execve failed: from i386 forking and exec()ing i386 process.\n";
argvs[0] = "sleep-i386";
if (do_execve_test("helpers/sleep-i386", argvs, NULL, 0)) goto test_failed_exit;
errmsg = "execve failed: from i386 forking and exec()ing x86_64 process w/ 4G pagezero.\n";
argvs[0] = "sleep-x86_64-4G";
if (do_execve_test("helpers/sleep-x86_64-4G", argvs, NULL, 0)) goto test_failed_exit;
errmsg = "execve failed: from i386 forking and exec()ing x86_64 process w/ 4K pagezero.\n";
argvs[0] = "sleep-x86_64-4K";
if (do_execve_test("helpers/sleep-x86_64-4K", argvs, NULL, 0)) goto test_failed_exit;
errmsg = "execve failed to exec the helper process.\n";
argvs[0] = "launch-x86_64";
if (do_execve_test("helpers/launch-x86_64", argvs, NULL, 1) != 0) goto test_failed_exit;
errmsg = NULL;
if (do_spawn_test(CPU_TYPE_I386, 0))
goto test_failed_exit;
if (do_spawn_test(CPU_TYPE_X86_64, 0))
goto test_failed_exit;
}
else if (bits == 32) {
errmsg = "execve failed: from i386 forking and exec()ing 32-bit i386 process.\n";
argvs[0] = "sleep-i386";
if (do_execve_test("helpers/sleep-i386", argvs, NULL, 1)) goto test_failed_exit;
errmsg = NULL;
if (do_spawn_test(CPU_TYPE_X86_64, 1))
goto test_failed_exit;
if (do_spawn_test(CPU_TYPE_I386, 0))
goto test_failed_exit;
}
} else if(get_architecture() == ARM) {
#ifdef CPU_TYPE_ARM64
if (bits == 64) {
errmsg = "execve failed: from arm64 forking and exec()ing 64-bit arm process.\n";
argvs[0] = "sleep-arm";
if (do_execve_test("helpers/sleep-arm64", argvs, NULL, 1))
goto test_failed_exit;
errmsg = NULL;
if (do_spawn_test(CPU_TYPE_ARM64, 0))
goto test_failed_exit;
}
#endif
errmsg = "execve failed: from arm forking and exec()ing 32-bit arm process.\n";
argvs[0] = "sleep-arm";
if (do_execve_test("helpers/sleep-arm", argvs, NULL, 1))
goto test_failed_exit;
errmsg = NULL;
if (do_spawn_test(CPU_TYPE_ARM, 0))
goto test_failed_exit;
}
else {
printf("get_architecture() returned unknown architecture");
return(-1);
}
return 0;
test_failed_exit:
if (errmsg)
printf("%s", errmsg);
return -1;
}
int groups_test( void * the_argp )
{
int my_err, i;
int my_group_count, my_orig_group_count;
gid_t my_real_gid;
gid_t my_effective_gid;
gid_t my_removed_gid;
gid_t my_new_gid;
gid_t my_groups[ NGROUPS_MAX ];
if ( g_skip_setuid_tests != 0 ) {
printf("\t skipping this test \n");
my_err = 0;
goto test_passed_exit;
}
my_real_gid = getgid( );
my_effective_gid = getegid( );
if ( !_prime_groups() ) {
goto test_failed_exit;
}
my_orig_group_count = getgroups( NGROUPS_MAX, &my_groups[0] );
if ( my_orig_group_count == -1 || my_orig_group_count < 1 ) {
printf( "getgroups call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
for ( i = 0; i < my_orig_group_count; i++ ) {
if ( my_groups[i] == my_real_gid )
break;
}
if ( i >= my_orig_group_count ) {
printf( "getgid or getgroups call failed. could not find real gid in list of groups. \n" );
goto test_failed_exit;
}
for ( i = 0; i < my_orig_group_count; i++ ) {
if ( my_groups[i] == my_effective_gid )
break;
}
if ( i >= my_orig_group_count ) {
printf( "getegid or getgroups call failed. could not find effective gid in list of groups. \n" );
goto test_failed_exit;
}
my_removed_gid = my_groups[ (my_orig_group_count - 1) ];
my_err = setgroups( (my_orig_group_count - 1), &my_groups[0] );
if ( my_err == -1 ) {
printf( "setgroups call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
my_group_count = getgroups( NGROUPS_MAX, &my_groups[0] );
if ( my_group_count == -1 || my_group_count < 1 ) {
printf( "getgroups call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
if ( my_orig_group_count <= my_group_count ) {
printf( "setgroups call failed. current group count is too high. \n" );
goto test_failed_exit;
}
my_groups[ (my_orig_group_count - 1) ] = my_removed_gid;
my_err = setgroups( my_orig_group_count, &my_groups[0] );
if ( my_err == -1 ) {
printf( "setgroups call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
my_new_gid = -1;
for ( i = 0; i < my_orig_group_count; i++ ) {
if ( my_groups[i] == my_effective_gid || my_groups[i] == my_real_gid )
continue;
my_new_gid = my_groups[i];
}
if ( my_new_gid == -1 ) {
printf( "could not find a gid to switch to. \n" );
goto test_failed_exit;
}
my_err = setegid( my_new_gid );
if ( my_err == -1 ) {
printf( "setegid call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
if ( getegid( ) != my_new_gid ) {
printf( "setegid failed to change the effective gid. \n" );
goto test_failed_exit;
}
my_err = setegid( my_effective_gid );
if ( my_err == -1 ) {
printf( "setegid call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
my_err = setgid( my_new_gid );
if ( my_err == -1 ) {
printf( "setgid call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
if ( getgid( ) != my_new_gid ) {
printf( "setgid failed to change the real gid. \n" );
goto test_failed_exit;
}
my_err = setgid( my_real_gid );
if ( my_err == -1 ) {
printf( "setegid call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
my_err = 0;
goto test_passed_exit;
test_failed_exit:
my_err = -1;
test_passed_exit:
return( my_err );
}
int dup_test( void * the_argp )
{
int my_err;
int my_fd = -1;
int my_newfd = -1;
int my_table_size, my_loop_counter = 0;
char * my_pathp = NULL;
ssize_t my_count;
char my_buffer[64];
kern_return_t my_kr;
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
*my_pathp = 0x00;
strcat( my_pathp, &g_target_path[0] );
strcat( my_pathp, "/" );
my_err = create_random_name( my_pathp, 1 );
if ( my_err != 0 ) {
goto test_failed_exit;
}
my_table_size = getdtablesize( );
if ( my_table_size < 20 ) {
printf( "getdtablesize should return at least 20, returned %d \n", my_table_size );
goto test_failed_exit;
}
my_fd = open( my_pathp, O_RDWR, 0 );
if ( my_fd == -1 ) {
printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_newfd = dup( my_fd );
if ( my_newfd == -1 ) {
printf( "dup call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
redo:
my_err = ftruncate( my_fd, 0 );
if ( my_err == -1 ) {
printf( "ftruncate call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
lseek( my_fd, 0, SEEK_SET );
my_count = write( my_fd, "aa", 2 );
if ( my_count == -1 ) {
printf( "write call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
my_count = write( my_newfd, "xx", 2 );
if ( my_count == -1 ) {
printf( "write call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
lseek( my_fd, 0, SEEK_SET );
my_count = read( my_fd, &my_buffer[0], sizeof(my_buffer) );
if ( my_count == -1 ) {
printf( "read call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
if ( my_buffer[0] != 'a' || my_buffer[1] != 'a' || my_buffer[2] != 'x' || my_buffer[3] != 'x' ) {
printf( "wrong data in test file. \n" );
goto test_failed_exit;
}
bzero( &my_buffer[0], sizeof(my_buffer) );
lseek( my_newfd, 0, SEEK_SET );
my_count = read( my_newfd, &my_buffer[0], sizeof(my_buffer) );
if ( my_count == -1 ) {
printf( "read call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
if ( my_buffer[0] != 'a' || my_buffer[1] != 'a' || my_buffer[2] != 'x' || my_buffer[3] != 'x' ) {
printf( "wrong data in test file. \n" );
goto test_failed_exit;
}
if ( my_loop_counter < 1 ) {
my_loop_counter++;
close( my_newfd );
my_err = dup2( my_fd, my_newfd );
if ( my_err == -1 ) {
printf( "dup2 call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
goto redo;
}
my_err = 0;
goto test_passed_exit;
test_failed_exit:
my_err = -1;
test_passed_exit:
if ( my_fd != -1 )
close( my_fd );
if ( my_newfd != -1 )
close( my_newfd );
if ( my_pathp != NULL ) {
remove( my_pathp );
vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
}
return( my_err );
}
int getrusage_test( void * the_argp )
{
int my_err;
struct rusage my_rusage;
my_err = getrusage( RUSAGE_SELF, &my_rusage );
if ( my_err == -1 ) {
printf( "getrusage failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
if ( my_rusage.ru_msgrcv > 1000 || my_rusage.ru_msgrcv < 0 ) {
printf( "getrusage seems to report wrong data - ru_msgrcv looks odd. \n" );
goto test_failed_exit;
}
if ( my_rusage.ru_nsignals > 1000 || my_rusage.ru_nsignals < 0 ) {
printf( "getrusage seems to report wrong data - ru_nsignals looks odd. \n" );
goto test_failed_exit;
}
my_err = 0;
goto test_passed_exit;
test_failed_exit:
my_err = -1;
test_passed_exit:
return( my_err );
}
int alarm_global = 0;
void test_alarm_handler( int the_arg );
void test_alarm_handler( int the_arg )
{
alarm_global = 4;
if ( the_arg == 0 ) {
}
return;
}
void test_signal_handler( int the_arg );
void test_signal_handler( int the_arg )
{
if ( the_arg == 0 ) {
}
return;
}
int signals_test( void * the_argp )
{
int my_err, my_status;
int my_fd = -1;
char * my_pathp = NULL;
pid_t my_pid, my_wait_pid;
kern_return_t my_kr;
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
*my_pathp = 0x00;
strcat( my_pathp, &g_target_path[0] );
strcat( my_pathp, "/" );
my_err = create_random_name( my_pathp, 1 );
if ( my_err != 0 ) {
goto test_failed_exit;
}
my_pid = fork( );
if ( my_pid == -1 ) {
printf( "fork failed with errno %d - %s \n", errno, strerror( errno ) );
goto test_failed_exit;
}
if ( my_pid == 0 ) {
int my_signal;
sigset_t my_sigset;
struct sigaction my_sigaction;
#ifdef MAC_OS_X_VERSION_10_5
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
struct __darwin_sigaltstack my_sigaltstack;
#endif
#else
struct sigaltstack my_sigaltstack;
#endif
struct itimerval my_timer;
my_err = sigaltstack( NULL, &my_sigaltstack );
if ( my_err == -1 ) {
printf( "sigaction failed with errno %d - %s \n", errno, strerror( errno ) );
exit( -1 );
}
if ( (my_sigaltstack.ss_flags & SS_DISABLE) == 0 ) {
printf( "sigaction must have failed - SS_DISABLE is cleared \n" );
exit( -1 );
}
my_sigaction.sa_handler = test_signal_handler;
my_sigaction.sa_flags = SA_RESTART;
my_sigaction.sa_mask = 0;
my_err = sigaction( SIGUSR1, &my_sigaction, NULL );
if ( my_err == -1 ) {
printf( "sigaction failed with errno %d - %s \n", errno, strerror( errno ) );
exit( -1 );
}
sigemptyset( &my_sigset );
my_err = sigsuspend( &my_sigset );
if ( my_err == -1 ) {
if ( errno != EINTR ) {
printf( "sigsuspend should have returned with errno EINTR \n" );
exit( -1 );
}
}
sigemptyset( &my_sigset );
sigaddset( &my_sigset, SIGUSR1 );
if ( sigismember( &my_sigset, SIGUSR1 ) == 0 ) {
printf( "sigaddset call failed to add SIGUSR1 to signal set \n" );
exit( -1 );
}
my_err = sigprocmask( SIG_BLOCK, &my_sigset, NULL );
if ( my_err == -1 ) {
printf( "sigprocmask failed with errno %d - %s \n", errno, strerror( errno ) );
exit( -1 );
}
sigemptyset( &my_sigset );
my_err = sigprocmask( 0, NULL, &my_sigset );
if ( my_err == -1 ) {
printf( "sigprocmask failed with errno %d - %s \n", errno, strerror( errno ) );
exit( -1 );
}
if ( sigismember( &my_sigset, SIGUSR1 ) == 0 ) {
printf( "sigaddset call failed to add SIGUSR1 to signal set \n" );
exit( -1 );
}
sigemptyset( &my_sigset );
sigaddset( &my_sigset, SIGUSR1 );
my_err = sigwait( &my_sigset, &my_signal );
if ( my_err == -1 ) {
printf( "sigwait failed with errno %d - %s \n", errno, strerror( errno ) );
exit( -1 );
}
if ( my_signal != SIGUSR1 ) {
printf( "sigwait failed to catch a pending SIGUSR1 signal. \n" );
exit( -1 );
}
sigfillset( &my_sigset );
sigdelset( &my_sigset, SIGUSR1 );
my_err = sigprocmask( SIG_UNBLOCK, &my_sigset, NULL );
if ( my_err == -1 ) {
printf( "sigprocmask failed with errno %d - %s \n", errno, strerror( errno ) );
exit( -1 );
}
if ( sigismember( &my_sigset, SIGUSR1 ) != 0 ) {
printf( "sigprocmask call failed to unblock SIGUSR1 \n" );
exit( -1 );
}
timerclear( &my_timer.it_interval );
timerclear( &my_timer.it_value );
my_err = setitimer( ITIMER_VIRTUAL, &my_timer, NULL );
if ( my_err == -1 ) {
printf( "setitimer - ITIMER_VIRTUAL - failed with errno %d - %s \n", errno, strerror( errno ) );
exit( -1 );
}
my_err = setitimer( ITIMER_PROF, &my_timer, NULL );
if ( my_err == -1 ) {
printf( "setitimer - ITIMER_PROF - failed with errno %d - %s \n", errno, strerror( errno ) );
exit( -1 );
}
alarm_global = 0;
my_sigaction.sa_handler = test_alarm_handler;
my_sigaction.sa_flags = SA_RESTART;
my_sigaction.sa_mask = 0;
my_err = sigaction( SIGALRM, &my_sigaction, NULL );
if ( my_err == -1 ) {
printf( "sigaction - SIGALRM - failed with errno %d - %s \n", errno, strerror( errno ) );
exit( -1 );
}
my_timer.it_value.tv_usec = (1000000 / 2);
my_err = setitimer( ITIMER_REAL, &my_timer, NULL );
if ( my_err == -1 ) {
printf( "setitimer - ITIMER_REAL - failed with errno %d - %s \n", errno, strerror( errno ) );
exit( -1 );
}
sigfillset( &my_sigset );
sigdelset( &my_sigset, SIGALRM );
my_err = sigsuspend( &my_sigset );
if ( my_err == -1 ) {
if ( errno != EINTR ) {
printf( "sigsuspend should have returned with errno EINTR \n" );
exit( -1 );
}
}
if ( alarm_global != 4 ) {
printf( "setitimer test failed - did not catch SIGALRM \n" );
exit( -1 );
}
my_timer.it_value.tv_sec = 44;
my_timer.it_value.tv_usec = 44;
my_err = getitimer( ITIMER_REAL, &my_timer );
if ( my_err == -1 ) {
printf( "getitimer - ITIMER_REAL - failed with errno %d - %s \n", errno, strerror( errno ) );
exit( -1 );
}
if ( timerisset( &my_timer.it_value ) || timerisset( &my_timer.it_interval ) ) {
printf( "ITIMER_REAL is set, but should not be \n" );
exit( -1 );
}
exit(0);
}
sleep( 1 );
my_err = kill( my_pid, SIGUSR1 );
if ( my_err == -1 ) {
printf( "kill call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
sleep( 1 );
my_err = kill( my_pid, SIGUSR1 );
if ( my_err == -1 ) {
printf( "kill call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_wait_pid = wait4( my_pid, &my_status, 0, NULL );
if ( my_wait_pid == -1 ) {
printf( "wait4 failed with errno %d - %s \n", errno, strerror( errno ) );
goto test_failed_exit;
}
if ( WIFSIGNALED( my_status ) || ( WIFEXITED( my_status ) && WEXITSTATUS( my_status ) != 0 ) ) {
goto test_failed_exit;
}
my_err = 0;
goto test_passed_exit;
test_failed_exit:
my_err = -1;
test_passed_exit:
if ( my_fd != -1 )
close( my_fd );
if ( my_pathp != NULL ) {
remove( my_pathp );
vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
}
return( my_err );
}
int getlogin_setlogin_test( void * the_argp )
{
int my_err, my_status;
pid_t my_pid, my_wait_pid;
kern_return_t my_kr;
if ( g_skip_setuid_tests != 0 ) {
printf("\t skipping this test \n");
my_err = 0;
goto test_passed_exit;
}
my_pid = fork( );
if ( my_pid == -1 ) {
printf( "fork failed with errno %d - %s \n", errno, strerror( errno ) );
goto test_failed_exit;
}
if ( my_pid == 0 ) {
char * my_namep = NULL;
int my_len;
char * my_new_namep = NULL;
my_namep = getlogin( );
if ( my_namep == NULL ) {
printf( "getlogin returned NULL name pointer \n" );
my_err = -1;
goto exit_child;
}
my_len = strlen( my_namep ) + 4;
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_new_namep, my_len, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
my_err = -1;
goto exit_child;
}
bzero( (void *)my_new_namep, my_len );
strcat( my_new_namep, my_namep );
strcat( my_new_namep, "2" );
my_err = setlogin( my_new_namep );
if ( my_err == -1 ) {
printf( "When setting new login name, setlogin failed with error %d - \"%s\" \n", errno, strerror( errno) );
my_err = -1;
goto exit_child;
}
my_namep = getlogin( );
if ( my_namep == NULL ) {
printf( "getlogin returned NULL name pointer \n" );
my_err = -1;
goto exit_child;
}
if ( memcmp( my_namep, my_new_namep, strlen( my_new_namep ) ) != 0 ) {
printf( "setlogin failed to set the new name \n" );
my_err = -1;
goto exit_child;
}
my_len = strlen ( my_namep );
my_namep[ my_len - 1 ] = '\0';
my_err = setlogin( my_namep );
if ( my_err == -1 ) {
printf( "When resetting login name, setlogin failed with error %d - \"%s\" \n", errno, strerror( errno) );
my_err = -1;
goto exit_child;
}
my_err = 0;
exit_child:
if ( my_new_namep != NULL ) {
vm_deallocate(mach_task_self(), (vm_address_t)my_new_namep, my_len);
}
exit( my_err );
}
my_wait_pid = wait4( my_pid, &my_status, 0, NULL );
if ( my_wait_pid == -1 ) {
printf( "wait4 failed with errno %d - %s \n", errno, strerror( errno ) );
goto test_failed_exit;
}
if ( WIFEXITED( my_status ) && WEXITSTATUS( my_status ) != 0 ) {
goto test_failed_exit;
}
my_err = 0;
goto test_passed_exit;
test_failed_exit:
my_err = -1;
test_passed_exit:
return( my_err );
}
int acct_test( void * the_argp )
{
int my_err, my_status;
int my_fd = -1;
char * my_pathp = NULL;
struct acct * my_acctp;
pid_t my_pid, my_wait_pid;
ssize_t my_count;
char my_buffer[ (sizeof(struct acct) + 32) ];
kern_return_t my_kr;
int acct_record_found;
char * test_bin_name = NULL;
if ( g_skip_setuid_tests != 0 ) {
printf("\t skipping this test \n");
my_err = 0;
goto test_passed_exit;
}
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
*my_pathp = 0x00;
strcat( my_pathp, &g_target_path[0] );
strcat( my_pathp, "/" );
my_err = create_random_name( my_pathp, 1 );
if ( my_err != 0 ) {
goto test_failed_exit;
}
my_err = acct( my_pathp );
if ( my_err == -1 ) {
printf( "acct failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_pid = fork( );
if ( my_pid == -1 ) {
printf( "fork failed with errno %d - %s \n", errno, strerror( errno ) );
goto test_failed_exit;
}
if ( my_pid == 0 ) {
char *argv[2];
argv[0] = "/usr/bin/true";
argv[1] = 0;
my_err = execv( argv[0], argv);
exit( 0 );
}
my_wait_pid = wait4( my_pid, &my_status, 0, NULL );
if ( my_wait_pid == -1 ) {
printf( "wait4 failed with errno %d - %s \n", errno, strerror( errno ) );
goto test_failed_exit;
}
if ( WIFEXITED( my_status ) && WEXITSTATUS( my_status ) != 0 ) {
printf("unexpected child exit status for accounting test load: %d\n", WEXITSTATUS( my_status));
goto test_failed_exit;
}
my_err = acct( NULL );
if ( my_err == -1 ) {
printf( "acct failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_fd = open( my_pathp, O_RDONLY, 0 );
if ( my_fd == -1 ) {
printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
lseek( my_fd, 0, SEEK_SET );
bzero( (void *)&my_buffer[0], sizeof(my_buffer) );
acct_record_found = 0;
test_bin_name = "true";
while(1) {
my_count = read( my_fd, &my_buffer[0], sizeof(struct acct) );
if ( my_count == -1 ) {
printf( "read call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
if ( my_count < sizeof(struct acct)) {
printf("Reached end of accounting records with last read count: %d\n", my_count);
break;
}
my_acctp = (struct acct *) &my_buffer[0];
if ( (getuid() == my_acctp->ac_uid) && (getgid() == my_acctp->ac_gid) &&
(!strncmp(my_acctp->ac_comm, test_bin_name, strlen(test_bin_name))) ) {
acct_record_found = 1;
break;
}
}
if (acct_record_found) {
my_err = 0;
goto test_passed_exit;
} else {
printf( "------------------------\n" );
printf( "Expected Accounting Record for child process %s not found\n", test_bin_name );
printf( "Expected uid: %lu Expected gid: %lu\n" , (unsigned long) getuid(), (unsigned long) getgid() );
printf( "Account file path: %s\n", my_pathp );
goto test_failed_exit;
}
test_failed_exit:
my_err = -1;
test_passed_exit:
if ( my_fd != -1 )
close( my_fd );
if ( my_pathp != NULL ) {
remove( my_pathp );
vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
}
return( my_err );
}
void print_acct_debug_strings( char * my_ac_comm )
{
char my_cmd_str[11];
char my_hex_str[128];
int i;
my_hex_str[0] = '\0';
for(i = 0; i < 10; i++)
{
sprintf( my_hex_str, "%s \'0x%x\' ", my_hex_str, my_ac_comm[i]);
}
memccpy(my_cmd_str, my_ac_comm, '\0', 10);
my_cmd_str[10] = '\0';
printf( "my_acctp->ac_comm = \"%s\" (should begin with: \"tr\")\n", my_cmd_str);
printf( "my_acctp->ac_comm = \"%s\"\n", my_hex_str);
printf( "------------------------\n" );
}
int ioctl_test( void * the_argp )
{
int my_err, my_result;
int my_fd = -1;
struct statfs * my_infop;
char * my_ptr;
int my_blksize;
long long my_block_count;
char my_name[ 128 ];
my_result = getmntinfo( &my_infop, MNT_NOWAIT );
if ( my_result < 1 ) {
printf( "getmntinfo failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
strcpy( &my_name[0], &my_infop->f_mntfromname[0] );
if ( (my_ptr = strrchr( &my_name[0], '/' )) != 0 ) {
if ( my_ptr[1] != 'r' ) {
my_ptr[ strlen( my_ptr ) ] = 0x00;
memmove( &my_ptr[2], &my_ptr[1], (strlen( &my_ptr[1] ) + 1) );
my_ptr[1] = 'r';
}
}
my_fd = open(&my_name[0], O_RDONLY );
if ( my_fd == -1 ) {
printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_err = ioctl( my_fd, DKIOCGETBLOCKCOUNT, &my_block_count );
if ( my_err == -1 ) {
printf( "ioctl DKIOCGETBLOCKCOUNT failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_err = ioctl( my_fd, DKIOCGETBLOCKSIZE, &my_blksize );
if ( my_err == -1 ) {
printf( "ioctl DKIOCGETBLOCKSIZE failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
if ( my_blksize < 0 || my_blksize > (1024 * 1000) ) {
printf( "ioctl appears to have returned incorrect block size data \n" );
goto test_failed_exit;
}
my_err = 0;
goto test_passed_exit;
test_failed_exit:
my_err = -1;
test_passed_exit:
if ( my_fd != -1 )
close( my_fd );
return( my_err );
}
int mkdir_rmdir_umask_test( void * the_argp )
{
int my_err;
int my_fd = -1;
int did_umask = 0;
char * my_pathp = NULL;
mode_t my_orig_mask;
struct stat my_sb;
kern_return_t my_kr;
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
*my_pathp = 0x00;
strcat( my_pathp, &g_target_path[0] );
strcat( my_pathp, "/" );
my_err = create_random_name( my_pathp, 0 );
if ( my_err != 0 ) {
printf( "create_random_name failed with error %d\n", my_err );
goto test_failed_exit;
}
my_orig_mask = umask( (S_IXUSR | S_IWGRP | S_IXGRP | S_IWOTH | S_IXOTH) );
did_umask = 1;
my_err = mkdir( my_pathp, (S_IRWXU | S_IRWXG | S_IRWXO) );
if ( my_err == -1 ) {
printf( "mkdir failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_err = stat( my_pathp, &my_sb );
if ( my_err != 0 ) {
printf( "stat call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
if ( (my_sb.st_mode & (S_IXUSR | S_IWGRP | S_IXGRP | S_IWOTH | S_IXOTH)) != 0 ) {
printf( "umask did not limit modes as it should have \n" );
goto test_failed_exit;
}
my_err = rmdir( my_pathp );
if ( my_err == -1 ) {
printf( "rmdir failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_err = 0;
goto test_passed_exit;
test_failed_exit:
my_err = -1;
test_passed_exit:
if ( my_fd != -1 )
close( my_fd );
if ( my_pathp != NULL ) {
rmdir( my_pathp );
vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
}
if ( did_umask != 0 ) {
umask( my_orig_mask );
}
return( my_err );
}
int chroot_test( void * the_argp )
{
int my_err, my_status;
pid_t my_pid, my_wait_pid;
char * my_pathp = NULL;
kern_return_t my_kr;
if ( g_skip_setuid_tests != 0 ) {
printf("\t skipping this test \n");
my_err = 0;
goto test_passed_exit;
}
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
*my_pathp = 0x00;
strcat( my_pathp, &g_target_path[0] );
strcat( my_pathp, "/" );
my_err = create_random_name( my_pathp, 0 );
if ( my_err != 0 ) {
goto test_failed_exit;
}
my_err = mkdir( my_pathp, (S_IRWXU | S_IRWXG | S_IRWXO) );
if ( my_err == -1 ) {
printf( "mkdir failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_pid = fork( );
if ( my_pid == -1 ) {
printf( "fork failed with errno %d - %s \n", errno, strerror( errno ) );
goto test_failed_exit;
}
if ( my_pid == 0 ) {
struct stat my_sb;
my_err = chroot( my_pathp );
if ( my_err != 0 ) {
printf( "chroot failed with error %d - \"%s\" \n", errno, strerror( errno) );
exit( -1 );
}
my_err = stat( "/", &my_sb );
if ( my_err != 0 ) {
printf( "stat call failed with error %d - \"%s\" \n", errno, strerror( errno) );
exit( -1 );
}
if ( my_sb.st_nlink > 2 ) {
printf( "root dir should be emnpty! \n" );
exit( -1 );
}
exit( 0 );
}
my_wait_pid = wait4( my_pid, &my_status, 0, NULL );
if ( my_wait_pid == -1 ) {
printf( "wait4 failed with errno %d - %s \n", errno, strerror( errno ) );
goto test_failed_exit;
}
if ( WIFEXITED( my_status ) && WEXITSTATUS( my_status ) != 0 ) {
printf( "bad exit status\n" );
goto test_failed_exit;
}
my_err = 0;
goto test_passed_exit;
test_failed_exit:
my_err = -1;
test_passed_exit:
if ( my_pathp != NULL ) {
my_err = rmdir( my_pathp );
if ( my_err != 0 ) {
printf( "rmdir failed with error %d - \"%s\" path %p\n", errno, strerror( errno), my_pathp );
}
vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
}
return( my_err );
}
int process_group_test( void * the_argp )
{
int my_err = 0, i = 0;
pid_t my_session_id, my_pid, my_process_group;
my_session_id = getsid( 0 );
if ( my_session_id == -1 ) {
printf( "getsid call failed with error %d - \"%s\" \n",
errno, strerror( errno ) );
goto test_failed_exit;
}
my_pid = getpid( );
my_process_group = getpgrp( );
if ( my_process_group != getpgid( 0 ) ) {
printf( "getpgrp and getpgid did not return the same process group ID \n" );
printf( "getpgid: %d, my_process_group: %d\n", getpgid( 0 ), my_process_group );
goto test_failed_exit;
}
if ( my_pid == my_process_group ) {
my_err = setsid( );
if ( my_err == 0 || errno != EPERM ) {
printf( "setsid call should have failed with EPERM\n" );
goto test_failed_exit;
}
} else {
my_err = setsid( );
if ( my_err == -1 ) {
printf( "setsid call failed with error %d - \"%s\" \n",
errno, strerror( errno ) );
goto test_failed_exit;
}
if ( my_process_group == getpgid( 0 ) ) {
printf( "process group was not reset \n" );
goto test_failed_exit;
}
}
for ( i = 10000; i < 1000000; i++ ) {
my_process_group = getpgid( i );
if ( my_process_group == -1 ) {
break;
}
}
my_err = setpgid( 0, my_process_group );
if ( my_err != -1 ) {
printf( "setpgid should have failed, but did not \n" );
goto test_failed_exit;
}
my_err = 0;
goto test_passed_exit;
test_failed_exit:
my_err = -1;
test_passed_exit:
return( my_err );
}
int fcntl_test( void * the_argp )
{
int my_err, my_result, my_tmep;
int my_fd = -1;
int my_newfd = -1;
char * my_pathp = NULL;
kern_return_t my_kr;
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
*my_pathp = 0x00;
strcat( my_pathp, &g_target_path[0] );
strcat( my_pathp, "/" );
my_err = create_random_name( my_pathp, 1 );
if ( my_err != 0 ) {
goto test_failed_exit;
}
my_fd = open( my_pathp, O_RDONLY, 0 );
if ( my_fd == -1 ) {
printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_result = fcntl( my_fd, F_GETFD, 0 );
if ( my_result == -1 ) {
printf( "fcntl - F_GETFD - failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_tmep = (my_result & FD_CLOEXEC);
if ( my_tmep ) {
my_result = fcntl( my_fd, F_SETFD, 0 );
}
else {
my_result = fcntl( my_fd, F_SETFD, 1 );
}
if ( my_result == -1 ) {
printf( "fcntl - F_SETFD - failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_result = fcntl( my_fd, F_GETFD, 0 );
if ( my_result == -1 ) {
printf( "fcntl - F_GETFD - failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
if ( my_tmep == (my_result & 0x01) ) {
printf( "fcntl - F_SETFD failed to set FD_CLOEXEC correctly!!! \n" );
goto test_failed_exit;
}
my_result = fcntl( my_fd, F_DUPFD_CLOEXEC, 0);
if ( my_result == -1 ) {
printf( "fcntl - F_DUPFD_CLOEXEC - failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_newfd = my_result;
my_result = fcntl( my_newfd, F_GETFD, 0);
if ( my_result == -1 ) {
printf( "fcntl - F_GETFD - failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
if ( (my_result & FD_CLOEXEC) == 0 ) {
printf( "fcntl - F_DUPFD_CLOEXEC failed to set FD_CLOEXEC!!! \n" );
goto test_failed_exit;
}
close( my_newfd );
my_newfd = -1;
{
char devfdpath[PATH_MAX];
(void) snprintf( devfdpath, sizeof (devfdpath),
"/dev/fd/%u", my_fd );
my_result = open( devfdpath, O_RDONLY | O_CLOEXEC );
}
if ( my_result == -1 ) {
printf( "open call failed on /dev/fd/%u with error %d - \"%s\" \n", my_fd, errno, strerror( errno) );
goto test_failed_exit;
}
my_newfd = my_result;
my_result = fcntl( my_newfd, F_GETFD, 0);
if ( my_result == -1 ) {
printf( "fcntl - F_GETFD - failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
if ( (my_result & FD_CLOEXEC) == 0 ) {
printf( "fcntl - O_CLOEXEC open of /dev/fd/%u failed to set FD_CLOEXEC!!! \n", my_fd );
goto test_failed_exit;
}
close ( my_newfd );
my_newfd = -1;
my_err = 0;
goto test_passed_exit;
test_failed_exit:
my_err = -1;
test_passed_exit:
if ( my_newfd != -1)
close ( my_newfd );
if ( my_fd != -1 )
close( my_fd );
if ( my_pathp != NULL ) {
remove( my_pathp );
vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
}
return( my_err );
}
int getpriority_setpriority_test( void * the_argp )
{
int my_err;
int my_priority;
int my_new_priority;
errno = 0;
my_priority = getpriority( PRIO_PROCESS, 0 );
if ( my_priority == -1 && errno != 0 ) {
printf( "getpriority - failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_new_priority = (my_priority == PRIO_MIN) ? (my_priority + 10) : (PRIO_MIN);
my_err = setpriority( PRIO_PROCESS, 0, my_new_priority );
if ( my_err == -1 ) {
printf( "setpriority - failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
errno = 0;
my_priority = getpriority( PRIO_PROCESS, 0 );
if ( my_priority == -1 && errno != 0 ) {
printf( "getpriority - failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
if ( my_priority != my_new_priority ) {
printf( "setpriority - failed to set correct scheduling priority \n" );
goto test_failed_exit;
}
my_err = setpriority( PRIO_PROCESS, 0, 0 );
if ( my_err == -1 ) {
printf( "setpriority - failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_err = 0;
goto test_passed_exit;
test_failed_exit:
my_err = -1;
test_passed_exit:
return( my_err );
}
int time_tests( void * the_argp )
{
int my_err;
int my_fd = -1;
char * my_pathp = NULL;
struct timeval my_orig_time;
struct timeval my_temp_time;
struct timeval my_utimes[4];
struct timezone my_tz;
struct stat my_sb;
kern_return_t my_kr;
if ( g_skip_setuid_tests != 0 ) {
printf( "\t skipping this test \n" );
my_err = 0;
goto test_passed_exit;
}
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
*my_pathp = 0x00;
strcat( my_pathp, &g_target_path[0] );
strcat( my_pathp, "/" );
my_err = create_random_name( my_pathp, 1 );
if ( my_err != 0 ) {
goto test_failed_exit;
}
my_err = gettimeofday( &my_orig_time, &my_tz );
if ( my_err == -1 ) {
printf( "gettimeofday - failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_temp_time = my_orig_time;
my_temp_time.tv_sec -= 60;
my_err = settimeofday( &my_temp_time, NULL );
if ( my_err == -1 ) {
printf( "settimeofday - failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_err = gettimeofday( &my_temp_time, NULL );
if ( my_err == -1 ) {
printf( "gettimeofday - failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
if ( my_orig_time.tv_sec <= my_temp_time.tv_sec ) {
printf( "settimeofday did not set correct time \n" );
goto test_failed_exit;
}
my_temp_time = my_orig_time;
my_temp_time.tv_sec += 1;
my_err = settimeofday( &my_temp_time, NULL );
if ( my_err == -1 ) {
printf( "settimeofday - failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_err = stat( my_pathp, &my_sb );
if ( my_err != 0 ) {
printf( "stat - failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
TIMESPEC_TO_TIMEVAL( &my_utimes[0], &my_sb.st_atimespec );
TIMESPEC_TO_TIMEVAL( &my_utimes[1], &my_sb.st_mtimespec );
my_utimes[0].tv_sec -= 120;
my_utimes[1].tv_sec -= 120;
my_err = utimes( my_pathp, &my_utimes[0] );
if ( my_err == -1 ) {
printf( "utimes - failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_err = stat( my_pathp, &my_sb );
if ( my_err != 0 ) {
printf( "stat - failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
TIMESPEC_TO_TIMEVAL( &my_utimes[2], &my_sb.st_atimespec );
TIMESPEC_TO_TIMEVAL( &my_utimes[3], &my_sb.st_mtimespec );
if ( my_utimes[0].tv_sec != my_utimes[2].tv_sec ||
my_utimes[1].tv_sec != my_utimes[3].tv_sec ) {
printf( "utimes failed to set access and mod times \n" );
goto test_failed_exit;
}
my_fd = open( my_pathp, O_RDWR, 0 );
if ( my_fd == -1 ) {
printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_utimes[0].tv_sec -= 120;
my_utimes[1].tv_sec -= 120;
my_err = futimes( my_fd, &my_utimes[0] );
if ( my_err == -1 ) {
printf( "futimes - failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_err = stat( my_pathp, &my_sb );
if ( my_err != 0 ) {
printf( "stat - failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
TIMESPEC_TO_TIMEVAL( &my_utimes[2], &my_sb.st_atimespec );
TIMESPEC_TO_TIMEVAL( &my_utimes[3], &my_sb.st_mtimespec );
if ( my_utimes[0].tv_sec != my_utimes[2].tv_sec ||
my_utimes[1].tv_sec != my_utimes[3].tv_sec ) {
printf( "futimes failed to set access and mod times \n" );
goto test_failed_exit;
}
my_err = 0;
goto test_passed_exit;
test_failed_exit:
my_err = -1;
test_passed_exit:
if ( my_fd != -1 )
close( my_fd );
if ( my_pathp != NULL ) {
remove( my_pathp );
vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
}
return( my_err );
}
int rename_test( void * the_argp )
{
int my_err;
char * my_pathp = NULL;
char * my_new_pathp = NULL;
ino_t my_file_id;
struct stat my_sb;
kern_return_t my_kr;
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
*my_pathp = 0x00;
strcat( my_pathp, &g_target_path[0] );
strcat( my_pathp, "/" );
my_err = create_random_name( my_pathp, 1 );
if ( my_err != 0 ) {
goto test_failed_exit;
}
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_new_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
*my_new_pathp = 0x00;
strcat( my_new_pathp, &g_target_path[0] );
strcat( my_new_pathp, "/" );
my_err = create_random_name( my_new_pathp, 0 );
if ( my_err != 0 ) {
goto test_failed_exit;
}
my_err = stat( my_pathp, &my_sb );
if ( my_err != 0 ) {
printf( "stat - failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_file_id = my_sb.st_ino;
my_err = rename( my_pathp, my_new_pathp );
if ( my_err == -1 ) {
printf( "rename - failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_err = stat( my_pathp, &my_sb );
if ( my_err == 0 ) {
printf( "rename call failed - found old name \n" );
goto test_failed_exit;
}
my_err = stat( my_new_pathp, &my_sb );
if ( my_err != 0 ) {
printf( "stat - failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
if ( my_file_id != my_sb.st_ino ) {
printf( "rename failed - wrong file id \n" );
goto test_failed_exit;
}
my_err = 0;
goto test_passed_exit;
test_failed_exit:
my_err = -1;
test_passed_exit:
if ( my_pathp != NULL ) {
remove( my_pathp );
vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
}
if ( my_new_pathp != NULL ) {
remove( my_new_pathp );
vm_deallocate(mach_task_self(), (vm_address_t)my_new_pathp, PATH_MAX);
}
return( my_err );
}
int locking_test( void * the_argp )
{
int my_err, my_status;
pid_t my_pid, my_wait_pid;
int my_fd = -1;
char * my_pathp = NULL;
kern_return_t my_kr;
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
*my_pathp = 0x00;
strcat( my_pathp, &g_target_path[0] );
strcat( my_pathp, "/" );
my_err = create_random_name( my_pathp, 1 );
if ( my_err != 0 ) {
goto test_failed_exit;
}
my_fd = open( my_pathp, O_RDWR, 0 );
if ( my_fd == -1 ) {
printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_err = flock( my_fd, LOCK_EX );
if ( my_err == -1 ) {
printf( "flock - LOCK_EX - failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_pid = fork( );
if ( my_pid == -1 ) {
printf( "fork failed with errno %d - %s \n", errno, strerror( errno ) );
goto test_failed_exit;
}
if ( my_pid == 0 ) {
int my_child_fd = -1;
int my_child_err;
my_child_fd = open( my_pathp, O_RDWR, 0 );
if ( my_child_fd == -1 ) {
printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
my_child_err = -1;
goto child_exit;
}
my_err = flock( my_child_fd, (LOCK_EX | LOCK_NB) );
if ( my_err == -1 ) {
if ( errno != EWOULDBLOCK ) {
printf( "flock call failed with error %d - \"%s\" \n", errno, strerror( errno) );
my_child_err = -1;
goto child_exit;
}
}
else {
printf( "flock call should have failed with EWOULDBLOCK err \n" );
my_child_err = -1;
goto child_exit;
}
my_child_err = 0;
child_exit:
if ( my_child_fd != -1 )
close( my_child_fd );
exit( my_child_err );
}
my_wait_pid = wait4( my_pid, &my_status, 0, NULL );
if ( my_wait_pid == -1 ) {
printf( "wait4 failed with errno %d - %s \n", errno, strerror( errno ) );
goto test_failed_exit;
}
if ( WIFEXITED( my_status ) && WEXITSTATUS( my_status ) != 0 ) {
goto test_failed_exit;
}
my_err = flock( my_fd, LOCK_UN );
if ( my_err == -1 ) {
printf( "flock - LOCK_UN - failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_err = 0;
goto test_passed_exit;
test_failed_exit:
my_err = -1;
test_passed_exit:
if ( my_fd != -1 )
close( my_fd );
if ( my_pathp != NULL ) {
remove( my_pathp );
vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
}
return( my_err );
}
int mkfifo_test( void * the_argp )
{
int my_err, my_status;
pid_t my_pid, my_wait_pid;
int my_fd = -1;
char * my_pathp = NULL;
ssize_t my_result;
off_t my_current_offset;
kern_return_t my_kr;
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
*my_pathp = 0x00;
strcat( my_pathp, &g_target_path[0] );
strcat( my_pathp, "/" );
my_err = create_random_name( my_pathp, 0 );
if ( my_err != 0 ) {
goto test_failed_exit;
}
my_err = mkfifo( my_pathp, (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) );
if ( my_err != 0 ) {
printf( "mkfifo failed with errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
my_pid = fork( );
if ( my_pid == -1 ) {
printf( "fork failed with errno %d - %s \n", errno, strerror( errno ) );
goto test_failed_exit;
}
if ( my_pid == 0 ) {
int my_child_fd = -1;
int my_child_err;
char my_buffer[64];
my_child_fd = open( my_pathp, O_RDWR, 0 );
if ( my_child_fd == -1 ) {
printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
my_child_err = -1;
goto child_exit;
}
bzero( (void *)&my_buffer[0], sizeof(my_buffer) );
my_result = read( my_child_fd, &my_buffer[0], sizeof(my_buffer) );
if ( my_result == -1 ) {
printf( "read call failed with error %d - \"%s\" \n", errno, strerror( errno) );
my_child_err = -1;
goto child_exit;
}
if ( strcmp( "parent to child", &my_buffer[0] ) != 0 ) {
printf( "read wrong message from parent \n" );
my_child_err = -1;
goto child_exit;
}
my_child_err = 0;
child_exit:
if ( my_child_fd != -1 )
close( my_child_fd );
exit( my_child_err );
}
my_fd = open( my_pathp, O_WRONLY, 0 );
if ( my_fd == -1 ) {
printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_current_offset = lseek( my_fd, 0, SEEK_CUR );
if ( my_current_offset != -1 ) {
printf( "lseek on fifo should fail but did not \n" );
goto test_failed_exit;
}
my_result = write( my_fd, "parent to child", 15 );
if ( my_result == -1 ) {
printf( "write call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_wait_pid = wait4( my_pid, &my_status, 0, NULL );
if ( my_wait_pid == -1 ) {
printf( "wait4 failed with errno %d - %s \n", errno, strerror( errno ) );
goto test_failed_exit;
}
if ( WIFEXITED( my_status ) && WEXITSTATUS( my_status ) != 0 ) {
goto test_failed_exit;
}
my_err = 0;
goto test_passed_exit;
test_failed_exit:
my_err = -1;
test_passed_exit:
if ( my_fd != -1 )
close( my_fd );
if ( my_pathp != NULL ) {
remove( my_pathp );
vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
}
return( my_err );
}
int quotactl_test( void * the_argp )
{
int my_err;
int is_quotas_on = 0;
struct dqblk my_quota_blk;
if ( g_skip_setuid_tests != 0 ) {
printf( "\t skipping this test \n" );
my_err = 0;
goto test_passed_exit;
}
my_err = quotactl( "/System/Library/Kernels/kernel", QCMD(Q_QUOTASTAT, USRQUOTA), 0, (caddr_t)&is_quotas_on );
if ( my_err == -1 ) {
printf( "quotactl - Q_QUOTASTAT - failed with errno %d - %s \n", errno, strerror( errno ) );
goto test_failed_exit;
}
if ( is_quotas_on == 0 ) {
my_err = 0;
goto test_passed_exit;
}
my_err = quotactl( "/System/Library/Kernels/kernel", QCMD(Q_GETQUOTA, USRQUOTA), getuid(), (caddr_t)&my_quota_blk );
if ( my_err == -1 ) {
printf( "quotactl - Q_GETQUOTA - failed with errno %d - %s \n", errno, strerror( errno ) );
goto test_failed_exit;
}
my_err = 0;
goto test_passed_exit;
test_failed_exit:
my_err = -1;
test_passed_exit:
return( my_err );
}
int limit_tests( void * the_argp )
{
int my_err;
struct rlimit my_current_rlimit;
struct rlimit my_rlimit;
my_err = getrlimit( RLIMIT_NOFILE, &my_current_rlimit );
if ( my_err == -1 ) {
printf( "getrlimit - failed with errno %d - %s \n", errno, strerror( errno ) );
goto test_failed_exit;
}
if ( my_current_rlimit.rlim_cur != RLIM_INFINITY ) {
if ( my_current_rlimit.rlim_cur != my_current_rlimit.rlim_max )
my_current_rlimit.rlim_cur += 1;
else
my_current_rlimit.rlim_cur -= 1;
my_rlimit.rlim_cur = my_current_rlimit.rlim_cur;
my_rlimit.rlim_max = my_current_rlimit.rlim_max;
my_err = setrlimit( RLIMIT_NOFILE, &my_rlimit );
if ( my_err == -1 ) {
printf( "setrlimit - failed with errno %d - %s \n", errno, strerror( errno ) );
goto test_failed_exit;
}
bzero( (void *) &my_rlimit, sizeof( my_rlimit ) );
my_err = getrlimit( RLIMIT_NOFILE, &my_rlimit );
if ( my_err == -1 ) {
printf( "getrlimit - failed with errno %d - %s \n", errno, strerror( errno ) );
goto test_failed_exit;
}
if ( my_rlimit.rlim_cur != my_current_rlimit.rlim_cur ) {
printf( "failed to get/set new RLIMIT_NOFILE soft limit \n" );
printf( "soft limits - current %lld should be %lld \n", my_rlimit.rlim_cur, my_current_rlimit.rlim_cur );
goto test_failed_exit;
}
#if CONFORMANCE_CHANGES_IN_XNU // can't do this check until conformance changes get into xnu
printf( "hard limits - current %lld should be %lld \n", my_rlimit.rlim_max, my_current_rlimit.rlim_max );
if ( my_rlimit.rlim_max != my_current_rlimit.rlim_max ) {
printf( "failed to get/set new RLIMIT_NOFILE hard limit \n" );
goto test_failed_exit;
}
#endif
long long biglim = 2147483649ll;
my_rlimit.rlim_cur = biglim;
my_err = setrlimit(RLIMIT_CPU, &my_rlimit);
if (my_err == -1) {
printf("failed to set large limit.\n");
goto test_failed_exit;
}
bzero(&my_rlimit, sizeof(struct rlimit));
my_err = getrlimit(RLIMIT_CPU, &my_rlimit);
if (my_err == -1) {
printf("after setting large value, failed to getrlimit().\n");
goto test_failed_exit;
}
if (my_rlimit.rlim_cur != biglim) {
printf("didn't retrieve large limit.\n");
goto test_failed_exit;
}
}
my_err = 0;
goto test_passed_exit;
test_failed_exit:
my_err = -1;
test_passed_exit:
return( my_err );
}
struct test_attr_buf {
uint32_t length;
fsobj_type_t obj_type;
fsobj_id_t obj_id;
struct timespec backup_time;
};
typedef struct test_attr_buf test_attr_buf;
int directory_tests( void * the_argp )
{
int my_err, done, found_it, i;
int my_fd = -1;
int is_ufs = 0;
char * my_pathp = NULL;
char * my_bufp = NULL;
char * my_file_namep;
#ifdef __LP64__
unsigned int my_base;
unsigned int my_count;
unsigned int my_new_state;
#else
unsigned long my_base;
unsigned long my_count;
unsigned long my_new_state;
#endif
fsobj_id_t my_obj_id;
struct timespec my_new_backup_time;
struct attrlist my_attrlist;
test_attr_buf my_attr_buf[4];
struct statfs my_statfs_buf;
kern_return_t my_kr;
my_err = statfs( &g_target_path[0], &my_statfs_buf );
if ( my_err == -1 ) {
printf( "statfs call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
if ( memcmp( &my_statfs_buf.f_fstypename[0], "ufs", 3 ) == 0 ) {
is_ufs = 1;
}
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_bufp, (1024 * 5), VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
*my_pathp = 0x00;
strcat( my_pathp, &g_target_path[0] );
strcat( my_pathp, "/" );
my_err = create_random_name( my_pathp, 1 );
if ( my_err != 0 ) {
goto test_failed_exit;
}
my_file_namep = strrchr( my_pathp, '/' );
my_file_namep++;
my_fd = open( &g_target_path[0], (O_RDONLY), 0 );
if ( my_fd == -1 ) {
printf( "open failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
memset( &my_attrlist, 0, sizeof(my_attrlist) );
my_attrlist.bitmapcount = ATTR_BIT_MAP_COUNT;
my_attrlist.commonattr = (ATTR_CMN_OBJTYPE | ATTR_CMN_OBJID | ATTR_CMN_BKUPTIME);
my_err = getattrlist( my_pathp, &my_attrlist, &my_attr_buf[0], sizeof(my_attr_buf[0]), 0 );
if ( my_err != 0 ) {
if ( errno == ENOTSUP && is_ufs ) {
my_err = 0;
goto test_passed_exit;
}
printf( "getattrlist call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
if ( my_attr_buf[0].obj_type != VREG ) {
printf( "getattrlist returned incorrect obj_type data. \n" );
goto test_failed_exit;
}
my_obj_id = my_attr_buf[0].obj_id;
my_new_backup_time = my_attr_buf[0].backup_time;
my_new_backup_time.tv_sec += 60;
my_attr_buf[0].backup_time.tv_sec = my_new_backup_time.tv_sec;
my_attrlist.commonattr = (ATTR_CMN_BKUPTIME);
my_err = setattrlist( my_pathp, &my_attrlist, &my_attr_buf[0].backup_time, sizeof(my_attr_buf[0].backup_time), 0 );
if ( my_err != 0 ) {
printf( "setattrlist call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
close( my_fd );
my_fd = open( &g_target_path[0], (O_RDONLY), 0 );
if ( my_fd == -1 ) {
printf( "open failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
memset( &my_attrlist, 0, sizeof(my_attrlist) );
memset( &my_attr_buf, 0, sizeof(my_attr_buf) );
my_attrlist.bitmapcount = ATTR_BIT_MAP_COUNT;
my_attrlist.commonattr = (ATTR_CMN_OBJTYPE | ATTR_CMN_OBJID | ATTR_CMN_BKUPTIME);
my_count = 4;
my_base = 0;
my_err = getdirentriesattr( my_fd, &my_attrlist, &my_attr_buf[0], sizeof(my_attr_buf), &my_count,
&my_base, &my_new_state, 0 );
if ( my_err < 0 ) {
printf( "getdirentriesattr call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
found_it = 0;
for ( i = 0; i < my_count; i++ ) {
if ( my_attr_buf[i].obj_id.fid_objno == my_obj_id.fid_objno &&
my_attr_buf[i].obj_id.fid_generation == my_obj_id.fid_generation ) {
found_it = 1;
if ( my_attr_buf[i].backup_time.tv_sec != my_new_backup_time.tv_sec ) {
printf( "setattrlist failed to set backup time. \n" );
goto test_failed_exit;
}
}
}
if ( found_it == 0 ) {
printf( "getdirentriesattr failed to find test file. \n" );
goto test_failed_exit;
}
my_err = 0;
goto test_passed_exit;
test_failed_exit:
if(my_err != 0)
my_err = -1;
test_passed_exit:
if ( my_fd != -1 )
close( my_fd );
if ( my_pathp != NULL ) {
remove( my_pathp );
vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
}
if ( my_bufp != NULL ) {
vm_deallocate(mach_task_self(), (vm_address_t)my_bufp, (1024 * 5));
}
return( my_err );
}
int exchangedata_test( void * the_argp )
{
int my_err;
int my_fd1 = -1;
int my_fd2 = -1;
char * my_file1_pathp = NULL;
char * my_file2_pathp = NULL;
ssize_t my_result;
char my_buffer[16];
struct statfs my_statfs_buf;
kern_return_t my_kr;
my_err = statfs( &g_target_path[0], &my_statfs_buf );
if ( my_err == -1 ) {
printf( "statfs call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
if ( memcmp( &my_statfs_buf.f_fstypename[0], "ufs", 3 ) == 0 ) {
my_err = 0;
goto test_passed_exit;
}
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_file1_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
*my_file1_pathp = 0x00;
strcat( my_file1_pathp, &g_target_path[0] );
strcat( my_file1_pathp, "/" );
my_err = create_random_name( my_file1_pathp, 1 );
if ( my_err != 0 ) {
printf( "create_random_name my_err: %d\n", my_err );
goto test_failed_exit;
}
my_fd1 = open( my_file1_pathp, O_RDWR, 0 );
if ( my_fd1 == -1 ) {
printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_result = write( my_fd1, "11111111", 8 );
if ( my_result == -1 ) {
printf( "write call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_file2_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
*my_file2_pathp = 0x00;
strcat( my_file2_pathp, &g_target_path[0] );
strcat( my_file2_pathp, "/" );
my_err = create_random_name( my_file2_pathp, 1 );
if ( my_err != 0 ) {
printf( "create_random_name my_err: %d\n", my_err );
goto test_failed_exit;
}
my_fd2 = open( my_file2_pathp, O_RDWR, 0 );
if ( my_fd2 == -1 ) {
printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_result = write( my_fd2, "22222222", 8 );
if ( my_result == -1 ) {
printf( "write call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
close(my_fd1);
my_fd1 = -1;
close(my_fd2);
my_fd2 = -1;
my_err = exchangedata( my_file1_pathp, my_file2_pathp, 0 );
if ( my_err == -1 ) {
printf( "exchangedata failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_fd1 = open( my_file1_pathp, O_RDONLY, 0 );
if ( my_fd1 == -1 ) {
printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
bzero( (void *)&my_buffer[0], sizeof(my_buffer) );
my_result = read( my_fd1, &my_buffer[0], 8 );
if ( my_result == -1 ) {
printf( "write call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
if ( memcmp( &my_buffer[0], "22222222", 8 ) != 0 ) {
printf( "exchangedata failed - incorrect data in file \n" );
goto test_failed_exit;
}
my_err = 0;
goto test_passed_exit;
test_failed_exit:
my_err = -1;
test_passed_exit:
if ( my_fd1 != -1 )
close( my_fd1 );
if ( my_file1_pathp != NULL ) {
remove( my_file1_pathp );
vm_deallocate(mach_task_self(), (vm_address_t)my_file1_pathp, PATH_MAX);
}
if ( my_fd2 != -1 )
close( my_fd2 );
if ( my_file2_pathp != NULL ) {
remove( my_file2_pathp );
vm_deallocate(mach_task_self(), (vm_address_t)my_file2_pathp, PATH_MAX);
}
return( my_err );
}
struct packed_name_attr {
u_int32_t size;
struct attrreference ref;
char name[ PATH_MAX ];
};
struct packed_attr_ref {
u_int32_t size;
struct attrreference ref;
};
struct packed_result {
u_int32_t size;
attrreference_t obj_name;
struct fsobj_id obj_id;
struct timespec obj_create_time;
char room_for_name[ 64 ];
};
typedef struct packed_result packed_result;
typedef struct packed_result * packed_result_p;
#define MAX_MATCHES 10
#define MAX_EBUSY_RETRIES 20
int searchfs_test( void * the_argp )
{
int my_err, my_items_found = 0, my_ebusy_count;
char * my_pathp = NULL;
unsigned long my_matches;
unsigned long my_search_options;
struct fssearchblock my_search_blk;
struct attrlist my_return_list;
struct searchstate my_search_state;
struct packed_name_attr my_info1;
struct packed_attr_ref my_info2;
packed_result my_result_buffer[ MAX_MATCHES ];
struct statfs my_statfs_buf;
kern_return_t my_kr;
my_err = statfs( &g_target_path[0], &my_statfs_buf );
if ( my_err == -1 ) {
printf( "statfs call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
if ( memcmp( &my_statfs_buf.f_fstypename[0], "ufs", 3 ) == 0 ) {
my_err = 0;
goto test_passed_exit;
}
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
*my_pathp = 0x00;
strcat( my_pathp, &g_target_path[0] );
strcat( my_pathp, "/" );
my_err = create_file_with_name( my_pathp, "foo", 0 );
if ( my_err < 0 ) {
printf( "failed to create a test file name in \"%s\" \n", my_pathp );
goto test_failed_exit;
}
my_err = create_file_with_name( my_pathp, "foobar", 0 );
if ( my_err < 0 ) {
printf( "failed to create a test file name in \"%s\" \n", my_pathp );
goto test_failed_exit;
}
my_err = create_file_with_name( my_pathp, "foofoo", 0 );
if ( my_err < 0 ) {
printf( "failed to create a test file name in \"%s\" \n", my_pathp );
goto test_failed_exit;
}
my_err = create_file_with_name( my_pathp, "xxxfoo", 0 );
if ( my_err < 0 ) {
printf( "failed to create a test file name in \"%s\" \n", my_pathp );
goto test_failed_exit;
}
my_ebusy_count = 0;
catalogue_changed:
my_items_found = 0;
my_search_blk.searchattrs.bitmapcount = ATTR_BIT_MAP_COUNT;
my_search_blk.searchattrs.reserved = 0;
my_search_blk.searchattrs.commonattr = ATTR_CMN_NAME;
my_search_blk.searchattrs.volattr = 0;
my_search_blk.searchattrs.dirattr = 0;
my_search_blk.searchattrs.fileattr = 0;
my_search_blk.searchattrs.forkattr = 0;
my_search_blk.returnattrs = &my_return_list;
my_return_list.bitmapcount = ATTR_BIT_MAP_COUNT;
my_return_list.reserved = 0;
my_return_list.commonattr = ATTR_CMN_NAME | ATTR_CMN_OBJID | ATTR_CMN_CRTIME;
my_return_list.volattr = 0;
my_return_list.dirattr = 0;
my_return_list.fileattr = 0;
my_return_list.forkattr = 0;
my_search_blk.returnbuffer = my_result_buffer;
my_search_blk.returnbuffersize = sizeof(my_result_buffer);
strcpy( my_info1.name, "foo" );
my_info1.ref.attr_dataoffset = sizeof(struct attrreference);
my_info1.ref.attr_length = strlen(my_info1.name) + 1;
my_info1.size = sizeof(struct attrreference) + my_info1.ref.attr_length;
my_search_blk.searchparams1 = &my_info1;
my_search_blk.sizeofsearchparams1 = my_info1.size + sizeof(u_int32_t);
my_info2.size = sizeof(struct attrreference);
my_info2.ref.attr_dataoffset = sizeof(struct attrreference);
my_info2.ref.attr_length = 0;
my_search_blk.searchparams2 = &my_info2;
my_search_blk.sizeofsearchparams2 = sizeof(my_info2);
my_search_blk.maxmatches = MAX_MATCHES;
my_search_blk.timelimit.tv_sec = 1;
my_search_blk.timelimit.tv_usec = 0;
my_search_options = (SRCHFS_START | SRCHFS_MATCHPARTIALNAMES |
SRCHFS_MATCHFILES | SRCHFS_MATCHDIRS);
do {
char * my_end_ptr;
char * my_ptr;
int i;
my_err = searchfs( my_pathp, &my_search_blk, &my_matches, 0, my_search_options, &my_search_state );
if ( my_err == -1 )
my_err = errno;
if ( (my_err == 0 || my_err == EAGAIN) && my_matches > 0 ) {
my_ptr = (char *) &my_result_buffer[0];
my_end_ptr = (my_ptr + sizeof(my_result_buffer));
for ( i = 0; i < my_matches; ++i ) {
packed_result_p my_result_p = (packed_result_p) my_ptr;
char * my_name_p;
my_name_p = (((char *)(&my_result_p->obj_name)) + my_result_p->obj_name.attr_dataoffset);
if ( memcmp( my_name_p, "foo", 3 ) == 0 ||
memcmp( my_name_p, "foobar", 6 ) == 0 ||
memcmp( my_name_p, "foofoo", 6 ) == 0 ||
memcmp( my_name_p, "xxxfoo", 6 ) == 0 ) {
my_items_found++;
}
#if DEBUG
printf("obj_name \"%.*s\" \n",
(int) my_result_p->obj_name.attr_length,
(((char *)(&my_result_p->obj_name)) +
my_result_p->obj_name.attr_dataoffset));
printf("size %d fid_objno %d fid_generation %d tv_sec 0x%02LX \n",
my_result_p->size, my_result_p->obj_id.fid_objno,
my_result_p->obj_id.fid_generation,
my_result_p->obj_create_time.tv_sec);
#endif
my_ptr = (my_ptr + my_result_p->size);
if (my_ptr > my_end_ptr)
break;
}
}
if ((my_err == EBUSY) && (my_ebusy_count++ < MAX_EBUSY_RETRIES)) {
goto catalogue_changed;
}
if ( !(my_err == 0 || my_err == EAGAIN) ) {
printf( "searchfs failed with error %d - \"%s\" \n", my_err, strerror( my_err) );
}
my_search_options &= ~SRCHFS_START;
} while ( my_err == EAGAIN );
if ( my_items_found < 4 ) {
printf( "searchfs failed to find all test files \n" );
goto test_failed_exit;
}
my_err = 0;
goto test_passed_exit;
test_failed_exit:
my_err = -1;
test_passed_exit:
if ( my_pathp != NULL ) {
char * my_ptr = (my_pathp + strlen( my_pathp ));
strcat( my_pathp, "foo" );
remove( my_pathp );
*my_ptr = 0x00;
strcat( my_pathp, "foobar" );
remove( my_pathp );
*my_ptr = 0x00;
strcat( my_pathp, "foofoo" );
remove( my_pathp );
*my_ptr = 0x00;
strcat( my_pathp, "xxxfoo" );
remove( my_pathp );
vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
}
return( my_err );
}
#define AIO_TESTS_BUFFER_SIZE (1024 * 4000)
#define AIO_TESTS_OUR_COUNT 5
int aio_tests( void * the_argp )
{
int my_err, i;
char * my_pathp;
struct aiocb * my_aiocbp;
ssize_t my_result;
struct timespec my_timeout;
int my_fd_list[ AIO_TESTS_OUR_COUNT ];
char * my_buffers[ AIO_TESTS_OUR_COUNT ];
struct aiocb * my_aiocb_list[ AIO_TESTS_OUR_COUNT ];
struct aiocb my_aiocbs[ AIO_TESTS_OUR_COUNT ];
char * my_file_paths[ AIO_TESTS_OUR_COUNT ];
kern_return_t my_kr;
memset( &my_fd_list[0], 0xFF, sizeof( my_fd_list ) );
memset( &my_buffers[0], 0x00, sizeof( my_buffers ) );
memset( &my_aiocb_list[0], 0x00, sizeof( my_aiocb_list ) );
memset( &my_file_paths[0], 0x00, sizeof( my_file_paths ) );
for ( i = 0; i < AIO_TESTS_OUR_COUNT; i++ ) {
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_buffers[ i ], AIO_TESTS_BUFFER_SIZE, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_file_paths[ i ], PATH_MAX, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_pathp = my_file_paths[ i ];
*my_pathp = 0x00;
strcat( my_pathp, &g_target_path[0] );
strcat( my_pathp, "/" );
my_err = create_random_name( my_pathp, 1 );
if ( my_err != 0 ) {
goto test_failed_exit;
}
my_fd_list[ i ] = open( my_pathp, O_RDWR, 0 );
if ( my_fd_list[ i ] <= 0 ) {
printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_aiocbp = &my_aiocbs[ i ];
my_aiocb_list[ i ] = my_aiocbp;
memset( my_aiocbp, 0x00, sizeof( *my_aiocbp ) );
my_aiocbp->aio_fildes = my_fd_list[ i ];
my_aiocbp->aio_buf = (char *) my_buffers[ i ];
my_aiocbp->aio_nbytes = 1024;
my_aiocbp->aio_sigevent.sigev_notify = SIGEV_NONE; my_aiocbp->aio_sigevent.sigev_signo = 0;
}
my_err = fcntl( my_fd_list[ 0 ], F_NOCACHE, 1 );
if ( my_err != 0 ) {
printf( "malloc failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_aiocbp = &my_aiocbs[ 0 ];
my_aiocbp->aio_fildes = my_fd_list[ 0 ];
my_aiocbp->aio_offset = 4096;
my_aiocbp->aio_buf = my_buffers[ 0 ];
my_aiocbp->aio_nbytes = AIO_TESTS_BUFFER_SIZE;
my_aiocbp->aio_reqprio = 0;
my_aiocbp->aio_sigevent.sigev_notify = 0;
my_aiocbp->aio_sigevent.sigev_signo = 0;
my_aiocbp->aio_sigevent.sigev_value.sival_int = 0;
my_aiocbp->aio_sigevent.sigev_notify_function = NULL;
my_aiocbp->aio_sigevent.sigev_notify_attributes = NULL;
my_aiocbp->aio_lio_opcode = 0;
memset( my_buffers[ 0 ], 'j', AIO_TESTS_BUFFER_SIZE );
my_err = aio_write( my_aiocbp );
if ( my_err != 0 ) {
printf( "aio_write failed with error %d - \"%s\" \n", my_err, strerror( my_err) );
goto test_failed_exit;
}
while ( 1 ) {
my_err = aio_error( my_aiocbp );
if ( my_err == EINPROGRESS ) {
sleep( 1 );
continue;
}
else if ( my_err == 0 ) {
ssize_t my_result;
my_result = aio_return( my_aiocbp );
break;
}
else {
printf( "aio_error failed with error %d - \"%s\" \n", my_err, strerror( my_err ) );
goto test_failed_exit;
}
}
memset( my_buffers[ 0 ], 'x', AIO_TESTS_BUFFER_SIZE );
my_err = aio_read( my_aiocbp );
while ( 1 ) {
my_err = aio_error( my_aiocbp );
if ( my_err == EINPROGRESS ) {
sleep( 1 );
continue;
}
else if ( my_err == 0 ) {
ssize_t my_result;
my_result = aio_return( my_aiocbp );
if ( *(my_buffers[ 0 ]) != 'j' || *(my_buffers[ 0 ] + AIO_TESTS_BUFFER_SIZE - 1) != 'j' ) {
printf( "aio_read or aio_write failed - wrong data read \n" );
goto test_failed_exit;
}
break;
}
else {
printf( "aio_read failed with error %d - \"%s\" \n", my_err, strerror( my_err ) );
goto test_failed_exit;
}
}
close( my_fd_list[ 0 ] );
my_fd_list[ 0 ] = open( my_pathp, O_RDWR, 0 );
if ( my_fd_list[ 0 ] == -1 ) {
printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_aiocbp = &my_aiocbs[ 0 ];
my_aiocbp->aio_fildes = my_fd_list[ 0 ];
my_aiocbp->aio_offset = 0;
my_aiocbp->aio_buf = my_buffers[ 0 ];
my_aiocbp->aio_nbytes = 1024;
my_aiocbp->aio_reqprio = 0;
my_aiocbp->aio_sigevent.sigev_notify = 0;
my_aiocbp->aio_sigevent.sigev_signo = 0;
my_aiocbp->aio_sigevent.sigev_value.sival_int = 0;
my_aiocbp->aio_sigevent.sigev_notify_function = NULL;
my_aiocbp->aio_sigevent.sigev_notify_attributes = NULL;
my_aiocbp->aio_lio_opcode = 0;
memset( my_buffers[ 0 ], 'e', 1024 );
my_err = aio_write( my_aiocbp );
if ( my_err != 0 ) {
printf( "aio_write failed with error %d - \"%s\" \n", my_err, strerror( my_err) );
goto test_failed_exit;
}
while ( 1 ) {
my_err = aio_error( my_aiocbp );
if ( my_err == EINPROGRESS ) {
sleep( 1 );
continue;
}
else if ( my_err == 0 ) {
ssize_t my_result;
my_result = aio_return( my_aiocbp );
break;
}
else {
printf( "aio_error failed with error %d - \"%s\" \n", my_err, strerror( my_err ) );
goto test_failed_exit;
}
}
my_err = aio_fsync( O_SYNC, my_aiocbp );
if ( my_err != 0 ) {
printf( "aio_fsync failed with error %d - \"%s\" \n", my_err, strerror( my_err) );
goto test_failed_exit;
}
while ( 1 ) {
my_err = aio_error( my_aiocbp );
if ( my_err == EINPROGRESS ) {
sleep( 1 );
continue;
}
else if ( my_err == 0 ) {
aio_return( my_aiocbp );
break;
}
else {
printf( "aio_error failed with error %d - \"%s\" \n", my_err, strerror( my_err ) );
goto test_failed_exit;
}
}
memset( my_buffers[ 0 ], 0x20, 16 );
lseek( my_fd_list[ 0 ], 0, SEEK_SET );
my_result = read( my_fd_list[ 0 ], my_buffers[ 0 ], 16);
if ( my_result == -1 ) {
printf( "read call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
if ( *(my_buffers[ 0 ]) != 'e' || *(my_buffers[ 0 ] + 16 - 1) != 'e' ) {
printf( "aio_fsync or aio_write failed - wrong data read \n" );
goto test_failed_exit;
}
for ( i = 0; i < AIO_TESTS_OUR_COUNT; i++ ) {
memset( my_buffers[ i ], 'a', AIO_TESTS_BUFFER_SIZE );
my_aiocbp = &my_aiocbs[ i ];
my_aiocbp->aio_nbytes = AIO_TESTS_BUFFER_SIZE;
my_aiocbp->aio_lio_opcode = LIO_WRITE;
}
my_err = lio_listio( LIO_NOWAIT, my_aiocb_list, AIO_TESTS_OUR_COUNT, NULL );
if ( my_err != 0 ) {
printf( "lio_listio call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_timeout.tv_sec = 1;
my_timeout.tv_nsec = 0;
my_err = aio_suspend( (const struct aiocb *const*) my_aiocb_list, AIO_TESTS_OUR_COUNT, &my_timeout );
if ( my_err != 0 ) {
printf( "aio_suspend call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
for ( i = 0; i < AIO_TESTS_OUR_COUNT; i++ ) {
my_aiocbp = &my_aiocbs[ i ];
my_err = aio_cancel( my_aiocbp->aio_fildes, my_aiocbp );
if ( my_err != AIO_ALLDONE && my_err != AIO_CANCELED && my_err != AIO_NOTCANCELED ) {
printf( "aio_cancel failed with error %d - \"%s\" \n", my_err, strerror( my_err) );
goto test_failed_exit;
}
}
my_err = 0;
goto test_passed_exit;
test_failed_exit:
my_err = -1;
test_passed_exit:
for ( i = 0; i < AIO_TESTS_OUR_COUNT; i++ ) {
if ( my_fd_list[ i ] != -1 ) {
close( my_fd_list[ i ] );
my_fd_list[ i ] = -1;
}
if ( my_file_paths[ i ] != NULL ) {
remove( my_file_paths[ i ] );
vm_deallocate(mach_task_self(), (vm_address_t)my_file_paths[ i ], PATH_MAX);
my_file_paths[ i ] = NULL;
}
if ( my_buffers[ i ] != NULL ) {
vm_deallocate(mach_task_self(), (vm_address_t)my_buffers[ i ], AIO_TESTS_BUFFER_SIZE);
my_buffers[ i ] = NULL;
}
}
return( my_err );
}
int message_queue_tests( void * the_argp )
{
int my_err;
int my_msg_queue_id = -1;
ssize_t my_result;
struct msqid_ds my_msq_ds;
struct testing_msq_message {
long msq_type;
char msq_buffer[ 32 ];
} my_msg;
my_msg_queue_id = msgget( IPC_PRIVATE, (IPC_CREAT | IPC_EXCL | IPC_R | IPC_W) );
if ( my_msg_queue_id == -1 ) {
printf( "msgget failed with errno %d - %s \n", errno, strerror( errno ) );
goto test_failed_exit;
}
my_err = msgctl( my_msg_queue_id, IPC_STAT, &my_msq_ds );
if ( my_err == -1 ) {
printf( "msgctl failed with errno %d - %s \n", errno, strerror( errno ) );
goto test_failed_exit;
}
if ( my_msq_ds.msg_perm.cuid != geteuid( ) ) {
printf( "msgctl IPC_STAT failed to get correct creator uid \n" );
goto test_failed_exit;
}
if ( (my_msq_ds.msg_perm.mode & (IPC_R | IPC_W)) == 0 ) {
printf( "msgctl IPC_STAT failed to get correct mode \n" );
goto test_failed_exit;
}
my_msg.msq_type = 1;
strcpy( &my_msg.msq_buffer[ 0 ], "testing 1, 2, 3" );
my_err = msgsnd( my_msg_queue_id, &my_msg, sizeof( my_msg.msq_buffer ), 0 );
if ( my_err == -1 ) {
printf( "msgsnd failed with errno %d - %s \n", errno, strerror( errno ) );
goto test_failed_exit;
}
my_err = msgctl( my_msg_queue_id, IPC_STAT, &my_msq_ds );
if ( my_err == -1 ) {
printf( "msgctl failed with errno %d - %s \n", errno, strerror( errno ) );
goto test_failed_exit;
}
if ( my_msq_ds.msg_qnum != 1 ) {
printf( "msgctl IPC_STAT failed to get correct number of messages on the queue \n" );
goto test_failed_exit;
}
bzero( (void *)&my_msg, sizeof( my_msg ) );
my_result = msgrcv( my_msg_queue_id, &my_msg, sizeof( my_msg.msq_buffer ), 0, 0 );
if ( my_result == -1 ) {
printf( "msgrcv failed with errno %d - %s \n", errno, strerror( errno ) );
goto test_failed_exit;
}
if ( my_result != sizeof( my_msg.msq_buffer ) ) {
printf( "msgrcv failed to return the correct number of bytes in our buffer \n" );
goto test_failed_exit;
}
if ( strcmp( &my_msg.msq_buffer[ 0 ], "testing 1, 2, 3" ) != 0 ) {
printf( "msgrcv failed to get the correct message \n" );
goto test_failed_exit;
}
my_err = msgctl( my_msg_queue_id, IPC_STAT, &my_msq_ds );
if ( my_err == -1 ) {
printf( "msgctl failed with errno %d - %s \n", errno, strerror( errno ) );
goto test_failed_exit;
}
if ( my_msq_ds.msg_qnum != 0 ) {
printf( "msgctl IPC_STAT failed to get correct number of messages on the queue \n" );
goto test_failed_exit;
}
my_err = msgctl( my_msg_queue_id, IPC_RMID, NULL );
if ( my_err == -1 ) {
printf( "msgctl IPC_RMID failed with errno %d - %s \n", errno, strerror( errno ) );
goto test_failed_exit;
}
my_msg_queue_id = -1;
my_err = 0;
goto test_passed_exit;
test_failed_exit:
my_err = -1;
test_passed_exit:
if ( my_msg_queue_id != -1 ) {
msgctl( my_msg_queue_id, IPC_RMID, NULL );
}
return( my_err );
}
int data_exec_tests( void * the_argp )
{
int my_err = 0;
int arch, bits;
posix_spawnattr_t attrp;
char *argv[] = { "helpers/data_exec32nonxspawn", NULL };
int my_pid, my_status, ret;
if ((arch = get_architecture()) == -1) {
printf("data_exec_test: couldn't determine architecture\n");
goto test_failed_exit;
}
bits = get_bits();
if (arch == INTEL) {
if (bits == 64) {
if (system("arch -arch x86_64 helpers/data_exec") != 0) {
printf("data_exec-x86_64 failed\n");
goto test_failed_exit;
}
}
if (system("arch -arch i386 helpers/data_exec") != 0) {
printf("data_exec-i386 failed\n");
goto test_failed_exit;
}
posix_spawnattr_init(&attrp);
posix_spawnattr_setflags(&attrp, _POSIX_SPAWN_ALLOW_DATA_EXEC );
ret = posix_spawn(&my_pid, "helpers/data_exec32nonxspawn", NULL, &attrp, argv, NULL);
if (ret) {
printf("data_exec-i386 failed in posix_spawn %s\n", strerror(errno));
goto test_failed_exit;
}
ret = wait4(my_pid, &my_status, 0, NULL);
if (ret == -1) {
printf("data_exec-i386 wait4 failed with errno %d - %s\n", errno, strerror(errno));
goto test_failed_exit;
}
if (WEXITSTATUS(my_status) != 0) {
printf("data_exec-i386 _POSIX_SPAWN_ALLOW_DATA_EXEC failed\n");
goto test_failed_exit;
}
}
goto test_passed_exit;
test_failed_exit:
my_err = -1;
test_passed_exit:
return my_err;
}
int kaslr_test( void * the_argp )
{
int result = 0;
uint64_t slide = 0;
size_t size;
int slide_enabled;
size = sizeof(slide_enabled);
result = sysctlbyname("kern.slide", &slide_enabled, &size, NULL, 0);
if (result != 0) {
printf("sysctlbyname(\"kern.slide\") failed with errno %d\n", errno);
goto test_failed_exit;
}
size = sizeof(slide);
result = kas_info(KAS_INFO_KERNEL_TEXT_SLIDE_SELECTOR, &slide, &size);
if (result == 0) {
if (slide_enabled && (slide == 0)) {
printf("kas_info(KAS_INFO_KERNEL_TEXT_SLIDE_SELECTOR, &slide, &size) reported slide of 0x%016llx\n", slide);
goto test_failed_exit;
}
if (size != sizeof(slide)) {
printf("kas_info(KAS_INFO_KERNEL_TEXT_SLIDE_SELECTOR, &slide, &size) reported size of %lu\n", size);
goto test_failed_exit;
}
} else {
if (errno == ENOTSUP) {
return 0;
} else {
printf("kas_info(KAS_INFO_KERNEL_TEXT_SLIDE_SELECTOR, &slide, &size) returned unexpected errno (errno %d)\n", errno);
goto test_failed_exit;
}
}
size = sizeof(slide);
result = kas_info(KAS_INFO_KERNEL_TEXT_SLIDE_SELECTOR, NULL , &size);
if ((result == 0) || (errno != EFAULT)) {
printf("kas_info(KAS_INFO_KERNEL_TEXT_SLIDE_SELECTOR, NULL, &size) returned unexpected success or errno (result %d errno %d)\n", result, errno);
goto test_failed_exit;
}
size = sizeof(slide) + 1;
result = kas_info(KAS_INFO_KERNEL_TEXT_SLIDE_SELECTOR, NULL, &size);
if ((result == 0) || (errno != EINVAL)) {
printf("kas_info(KAS_INFO_KERNEL_TEXT_SLIDE_SELECTOR, NULL, &size+1) returned unexpected success or errno (result %d errno %d)\n", result, errno);
goto test_failed_exit;
}
result = kas_info(KAS_INFO_KERNEL_TEXT_SLIDE_SELECTOR, NULL , NULL );
if ((result == 0) || (errno != EFAULT)) {
printf("kas_info(KAS_INFO_KERNEL_TEXT_SLIDE_SELECTOR, NULL, NULL) returned unexpected success or errno (result %d errno %d)\n", result, errno);
goto test_failed_exit;
}
size = sizeof(slide);
result = kas_info(KAS_INFO_MAX_SELECTOR , &slide, &size);
if ((result == 0) || (errno != EINVAL)) {
printf("kas_info(KAS_INFO_MAX_SELECTOR, &slide, &size) returned unexpected success or errno (result %d errno %d)\n", result, errno);
goto test_failed_exit;
}
return 0;
test_failed_exit:
return -1;
}
typedef struct attrs {
uint32_t attrs_length;
attribute_set_t attrs_returned;
uint32_t attr_error;
attrreference_t attr_name;
fsobj_type_t attr_obj_type;
union {
struct {
uint32_t entry_count;
} directory;
struct {
off_t size;
} file;
} attr_obj;
} attrs_t;
int getattrlistbulk_test( void * the_argp )
{
int error;
struct attrlist attr_list;
attrs_t *attrsptr;
char *entry_start;
int retcount = 0, totalcount = 0;
int index;
char *nameptr;
int attr_buf_size;
char *attr_buf;
int dirfd = -1;
char* target = "/System/Library/CoreServices";
memset(&attr_list, 0, sizeof(attr_list));
attr_list.bitmapcount = ATTR_BIT_MAP_COUNT;
attr_list.commonattr = ATTR_CMN_RETURNED_ATTRS |
ATTR_CMN_NAME |
ATTR_CMN_OBJTYPE |
ATTR_CMN_ERROR |
ATTR_FILE_TOTALSIZE|
ATTR_DIR_ENTRYCOUNT;
error = 0;
attr_buf_size = 10 * (sizeof(attrs_t) + FILENAME_MAX );
if (vm_allocate((vm_map_t) mach_task_self(),
(vm_address_t*)&attr_buf,
attr_buf_size, VM_FLAGS_ANYWHERE) != KERN_SUCCESS) {
printf( "vm_allocate failed with error %d - \"%s\" \n",
errno, strerror( errno) );
attr_buf = NULL;
error = -1;
goto last_exit;
}
dirfd = openat (AT_FDCWD, target, O_RDONLY, 0);
if (dirfd == -1) {
printf("openat \"%s\" failed with error %d - \"%s\" \n",
target, errno, strerror( errno));
error = -1;
goto last_exit;
}
do {
retcount = getattrlistbulk(dirfd,
&attr_list, &attr_buf[0],
attr_buf_size, FSOPT_PACK_INVAL_ATTRS);
if (retcount == -1) {
printf("getattrlistbulk on %s returned %d items\n",
target, totalcount);
printf("getattrlistbulk failed with error %d - \"%s\" \n",
errno, strerror( errno));
error = -1;
break;
} else if (retcount == 0) {
printf("getattrlistbulk succeded: found %d entries in %s\n", totalcount, target);
error = 0;
break;
} else {
totalcount += retcount;
entry_start = &attr_buf[0];
for (index = 0; index < retcount; index++) {
attrsptr = (attrs_t *)entry_start;
entry_start += attrsptr->attrs_length;
if ((attrsptr->attrs_returned.commonattr & ATTR_CMN_ERROR) &&
attrsptr->attr_error) {
nameptr = (char*)(&(attrsptr->attr_name)) + attrsptr->attr_name.attr_dataoffset;
printf("getattrlistbulk item \"%s\" ATTR_CMN_ERROR %d \"%s\"\n",
nameptr, attrsptr->attr_error,
strerror(attrsptr->attr_error));
}
}
}
} while (1);
last_exit:
if (dirfd != -1) {
(void)close(dirfd);
}
if (attr_buf != NULL) {
vm_deallocate(
mach_task_self(), (vm_address_t)attr_buf, attr_buf_size);
}
return error;
}
#define INVALID_FD -173
static int create_random_name_at(int the_dirfd, char *the_dirpathp,
char *the_namep, size_t the_namep_len,
char *the_pathp, size_t the_pathp_len,
int do_create );
int create_random_name_at(int the_dirfd, char *the_dirpathp,
char *the_namep, size_t the_namep_len,
char *the_pathp, size_t the_pathp_len,
int do_create )
{
int i, my_err;
int my_fd = -1;
for ( i = 0; i < 1; i++ ) {
int my_rand;
char *myp;
char my_name[32];
my_rand = rand( );
sprintf( &my_name[0], "%d", my_rand );
if ( (strlen( &my_name[0] ) + strlen( the_dirpathp ) + 2) > PATH_MAX ) {
printf( "%s - path to test file greater than PATH_MAX \n", __FUNCTION__ );
return( -1 );
}
myp = the_namep;
*(myp) = (char)0x00;
strlcat(the_namep, &my_name[0], the_namep_len);
if (the_pathp && the_dirpathp) {
*the_pathp = (char)0x00;
strlcat(the_pathp, the_dirpathp, the_pathp_len);
strlcat(the_pathp, "/", the_pathp_len);
strlcat(the_pathp, the_namep, the_pathp_len);
}
if (do_create) {
my_fd = openat( the_dirfd, the_namep, (O_RDWR | O_CREAT | O_EXCL),
(S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) );
if ( my_fd == -1 ) {
if ( errno != EEXIST ) {
printf( "%s - open failed with errno %d - %s \n",
__FUNCTION__, errno, strerror( errno ) );
return( -1 );
}
i--;
continue;
}
}
else {
struct stat my_sb;
my_err = fstatat( the_dirfd, the_namep, &my_sb, 0 );
if ( my_err != 0 ) {
if ( errno == ENOENT ) {
break;
}
else {
printf( "%s - open failed with errno %d - %s \n",
__FUNCTION__, errno, strerror( errno ) );
return( -1 );
}
}
i--;
continue;
}
}
if ( my_fd != -1 )
close( my_fd );
return( 0 );
}
int openat_close_test( void * the_argp )
{
int my_err;
int my_dirfd = -1;
int my_fd = -1;
int error_fd = -1;
char * my_dirpathp = NULL;
char * my_namep = NULL;
char * my_pathp = NULL;
ssize_t my_result;
long my_pconf_result;
struct stat my_sb;
char my_buffer[32];
kern_return_t my_kr;
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_dirpathp, PATH_MAX, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
*my_dirpathp = 0x00;
strlcat( my_dirpathp, &g_target_path[0], PATH_MAX );
my_dirfd = openat(AT_FDCWD, my_dirpathp, O_RDONLY, 0 );
if ( my_dirfd == -1 ) {
printf( "openat call failed with error %d - \"%s\" \n", errno, strerror( errno) );
printf( "\t Directory we attempted to open -> \"%s\" \n", my_dirpathp );
goto test_failed_exit;
}
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_namep, NAME_MAX, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
*my_namep = 0x00;
if (my_pathp) {
*my_pathp = 0x00;
}
if (*my_dirpathp == '/') {
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
}
if (*my_dirpathp == '/') {
my_dirfd = openat( INVALID_FD, my_dirpathp, O_RDONLY, 0 );
if ( my_dirfd == -1 ) {
printf( "openat call failed with error %d - \"%s\" \n", errno, strerror( errno) );
printf( "\t Directory we attempted to open -> \"%s\" \n", my_dirpathp );
printf( "\t Was Absolute pathname, invalid fd, %d, provided as input \n", INVALID_FD);
goto test_failed_exit;
}
close(my_dirfd);
}
my_dirfd = openat( AT_FDCWD, my_dirpathp, O_RDONLY, 0 );
if ( my_dirfd == -1 ) {
printf( "openat call failed with error %d - \"%s\" \n", errno, strerror( errno) );
printf( "\t Directory we attempted to open -> \"%s\" \n", my_dirpathp );
goto test_failed_exit;
}
my_err = create_random_name_at( my_dirfd, my_dirpathp, my_namep, NAME_MAX, my_pathp, PATH_MAX, 1 );
if ( my_err != 0 ) {
goto test_failed_exit;
}
if (my_pathp) {
my_fd = openat( INVALID_FD, my_namep, O_WRONLY, 0 );
if ( my_fd != -1 ) {
printf( "openat call relative to invalid dir fd worked\n");
printf( "\t file we attempted to open -> \"%s\" relative to fd -173\n", my_pathp );
goto test_failed_exit;
}
}
my_fd = openat( my_dirfd, my_namep, O_WRONLY, 0 );
if ( my_fd == -1 ) {
printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
printf( "\t file we attempted to open -> \"%s\" \n", my_pathp );
goto test_failed_exit;
}
if ((error_fd = openat(my_fd, my_namep, O_WRONLY, 0)) != -1) {
printf( "openat call succeded with fd being a non-directory fd\n");
printf( "\t file we attempted to open (reltive to itself)-> \"%s\" \n", my_pathp );
close(error_fd);
goto test_failed_exit;
} else if (errno != ENOTDIR) {
printf( "openat call should have failed with errno 20 (ENOTDIR). actually failed with %d - \"%s\" \n", my_err, strerror( my_err) );
}
my_pconf_result = fpathconf( my_fd, _PC_NAME_MAX );
if ( my_pconf_result == -1 ) {
printf( "fpathconf - _PC_PATH_MAX - failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
if ( my_pconf_result < 6 ) {
printf( "fpathconf - _PC_NAME_MAX - looks like wrong results \n" );
goto test_failed_exit;
}
my_result = write( my_fd, "kat", 3 );
my_err = errno;
if ( my_result != 3 ) {
if ( sizeof( ssize_t ) > sizeof( int ) ) {
printf( "write failed. should have written 3 bytes actually wrote - %ld \n", (long int) my_result );
}
else {
printf( "write failed. should have written 3 bytes actually wrote - %d \n", (int) my_result );
}
goto test_failed_exit;
}
my_result = read( my_fd, &my_buffer[0], sizeof(my_buffer) );
my_err = errno;
if ( my_result != -1 ) {
printf( "read call should have failed with errno 9 (EBADF) \n" );
goto test_failed_exit;
}
else if ( my_err != EBADF ) {
printf( "read call should have failed with errno 9 (EBADF). actually failed with %d - \"%s\" \n", my_err, strerror( my_err) );
goto test_failed_exit;
}
close( my_fd );
my_fd = openat( my_dirfd, my_namep, (O_RDWR | O_TRUNC | O_APPEND), 0 );
if ( my_fd == -1 ) {
printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
printf( "\t file we attempted to open -> \"%s\" \n", my_pathp );
goto test_failed_exit;
}
my_result = read( my_fd, &my_buffer[0], sizeof(my_buffer) );
if ( my_result == -1 ) {
printf( "read call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
if ( my_result != 0 ) {
printf( "read failed - should have read 0 bytes. \n" );
goto test_failed_exit;
}
my_result = write( my_fd, "kat", 3 );
my_err = errno;
if ( my_result != 3 ) {
if ( sizeof( ssize_t ) > sizeof( int ) ) {
printf( "write failed. should have written 3 bytes actually wrote - %ld \n", (long int) my_result );
}
else {
printf( "write failed. should have written 3 bytes actually wrote - %d \n", (int) my_result );
}
goto test_failed_exit;
}
lseek( my_fd, 0, SEEK_SET );
my_result = write( my_fd, "zzz", 3 );
my_err = errno;
if ( my_result != 3 ) {
if ( sizeof( ssize_t ) > sizeof( int ) ) {
printf( "write failed. should have written 3 bytes actually wrote - %ld \n", (long int) my_result );
}
else {
printf( "write failed. should have written 3 bytes actually wrote - %d \n", (int) my_result );
}
goto test_failed_exit;
}
bzero( (void *)&my_buffer[0], sizeof(my_buffer) );
lseek( my_fd, 0, SEEK_SET );
my_result = read( my_fd, &my_buffer[0], sizeof(my_buffer) );
if ( my_result == -1 ) {
printf( "read call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
if ( my_buffer[0] != 'k' || my_buffer[5] != 'z' ) {
printf( "read failed to get correct data \n" );
goto test_failed_exit;
}
if ((fstatat( my_fd, my_namep, &my_sb, 0 )) != -1) {
printf( "fstatat call succeded with fd being a non-directory fd\n");
printf( "\t file we attempted to stat (relative to itself)-> \"%s\" \n", my_pathp );
goto test_failed_exit;
} else if (errno != ENOTDIR) {
printf( "fstatat call should have failed with errno 20 (ENOTDIR). actually failed with %d - \"%s\" \n", my_err, strerror( my_err) );
}
my_err = fstatat( my_dirfd, my_namep, &my_sb, 0 );
if ( my_err == -1 ) {
printf( "fstatat call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
if ( my_sb.st_size != 6 ) {
printf( "fstatat call failed - st_size is wrong \n" );
goto test_failed_exit;
}
if ( !S_ISREG( my_sb.st_mode ) ) {
printf( "fstatat call failed - st_mode does not indicate regular file \n" );
goto test_failed_exit;
}
my_err = 0;
goto test_passed_exit;
test_failed_exit:
my_err = -1;
test_passed_exit:
if ( my_fd != -1 )
close( my_fd );
if ( my_pathp != NULL ) {
remove(my_pathp);
vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
}
if ( my_namep ) {
unlinkat( my_dirfd, my_pathp, 0 );
vm_deallocate(mach_task_self(), (vm_address_t)my_namep, NAME_MAX);
}
if ( my_dirfd != -1)
close(my_dirfd);
if ( my_dirpathp != NULL ) {
vm_deallocate(mach_task_self(), (vm_address_t)my_dirpathp, PATH_MAX);
}
return( my_err );
}
int linkat_fstatat_unlinkat_test( void * the_argp )
{
int my_err;
int my_dirfd = -1;
int my_fd = -1;
char * my_dirpathp = NULL;
char * my_namep = NULL;
char * my_pathp = NULL;
char * my_name2p = NULL;
nlink_t my_link_count;
ssize_t my_result;
struct stat my_sb;
kern_return_t my_kr;
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_dirpathp, PATH_MAX, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
*my_dirpathp = 0x00;
strlcat( my_dirpathp, &g_target_path[0], PATH_MAX );
my_dirfd = openat(AT_FDCWD, my_dirpathp, O_RDONLY, 0 );
if ( my_dirfd == -1 ) {
printf( "openat call failed with error %d - \"%s\" \n", errno, strerror( errno) );
printf( "\t Directory we attempted to open -> \"%s\" \n", my_dirpathp );
goto test_failed_exit;
}
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_namep, NAME_MAX, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
*my_namep = 0x00;
if (my_pathp) {
*my_pathp = 0x00;
}
if (*my_dirpathp == '/') {
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
}
my_err = create_random_name_at( my_dirfd, my_dirpathp, my_namep, NAME_MAX, my_pathp, PATH_MAX, 1 );
if ( my_err != 0 ) {
goto test_failed_exit;
}
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_name2p, NAME_MAX, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
*my_name2p = 0x00;
strlcat( my_name2p, my_namep, NAME_MAX );
strlcat( my_name2p, "link", NAME_MAX );
my_err = fstatat( my_dirfd, my_namep, &my_sb, 0 );
if ( my_err != 0 ) {
printf( "stat call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
my_link_count = my_sb.st_nlink;
if (my_pathp) {
my_err = fstatat(INVALID_FD, my_pathp, &my_sb, 0 );
if ( my_err != 0 ) {
printf( "fstatat with INVALID_FD and absolute pathname failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
if (my_link_count != my_sb.st_nlink) {
printf( "fstatat call did not return correct number of links" );
goto test_failed_exit;
}
}
if ( my_sb.st_size != 0 ) {
printf( "stat structure looks bogus for test file \"%s\" \n", my_pathp );
printf( "st_size is not 0 \n" );
goto test_failed_exit;
}
my_fd = openat(my_dirfd, my_namep, O_RDWR, 0 );
if ( my_fd == -1 ) {
printf( "openat call failed with error %d - \"%s\" \n", errno, strerror( errno) );
printf( "\t file we attempted to open -> \"%s\" \n", my_pathp );
goto test_failed_exit;
}
my_result = write( my_fd, "kat", 3 );
my_err = errno;
if ( my_result != 3 ) {
if ( sizeof( ssize_t ) > sizeof( int ) ) {
printf( "write failed. should have written 3 bytes actually wrote - %ld \n", (long int) my_result );
}
else {
printf( "write failed. should have written 3 bytes actually wrote - %d \n", (int) my_result );
}
goto test_failed_exit;
}
close( my_fd );
my_fd = -1;
my_err = linkat( my_dirfd, my_namep, my_dirfd, my_name2p, AT_SYMLINK_FOLLOW );
if ( my_err != 0 ) {
printf( "linkat call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
my_err = fstatat( my_dirfd, my_pathp, &my_sb, 0 );
if ( my_err != 0 ) {
printf( "fstatat call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
if ( (my_link_count + 1) != my_sb.st_nlink ) {
printf( "stat structure looks bogus for test file \"%s\" \n", my_pathp );
printf( "incorrect st_nlink \n" );
goto test_failed_exit;
}
if ( my_sb.st_size != 3 ) {
printf( "stat structure looks bogus for test file \"%s\" \n", my_pathp );
printf( "st_size is not 3 \n" );
goto test_failed_exit;
}
my_err = unlinkat( my_dirfd, my_name2p, 0 );
if ( my_err != 0 ) {
printf( "unlinkat call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
my_err = fstatat( my_dirfd, my_namep, &my_sb, 0 );
if ( my_err != 0 ) {
printf( "stat call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
if ( my_link_count != my_sb.st_nlink ) {
printf( "stat structure looks bogus for test file \"%s\" \n", my_pathp );
printf( "incorrect st_nlink \n" );
goto test_failed_exit;
}
my_err = 0;
goto test_passed_exit;
test_failed_exit:
my_err = -1;
test_passed_exit:
if ( my_fd != -1 )
close( my_fd );
if ( my_name2p != NULL ) {
(void)unlinkat( my_dirfd, my_name2p, 0 );
vm_deallocate(mach_task_self(), (vm_address_t)my_name2p, NAME_MAX);
}
if ( my_namep != NULL ) {
(void)unlinkat( my_dirfd, my_name2p, 0 );
vm_deallocate(mach_task_self(), (vm_address_t)my_name2p, NAME_MAX);
}
if ( my_pathp != NULL ) {
remove( my_pathp );
vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
}
if ( my_dirpathp != NULL ) {
vm_deallocate(mach_task_self(), (vm_address_t)my_dirpathp, PATH_MAX);
}
if ( my_dirfd != -1 )
close( my_dirfd );
return( my_err );
}
int faccessat_fchmodat_fchmod_test( void * the_argp )
{
int error_occurred;
int is_absolute_path = 0;
int my_err;
int my_dirfd = -1;
int my_fd = -1;
char * my_dirpathp = NULL;
char * my_namep = NULL;
char * my_pathp = NULL;
uid_t euid,ruid;
struct stat my_sb;
FILE * file_handle;
kern_return_t my_kr;
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_dirpathp, PATH_MAX, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
*my_dirpathp = 0x00;
strlcat( my_dirpathp, &g_target_path[0], PATH_MAX );
if (*my_dirpathp == '/') {
is_absolute_path = 1;
my_dirfd = openat(INVALID_FD, my_dirpathp, O_RDONLY, 0 );
if ( my_dirfd == -1 ) {
printf( "openat call failed with error %d - \"%s\" \n", errno, strerror( errno) );
printf( "\t Directory we attempted to open -> \"%s\" \n", my_dirpathp );
printf( "\t Was Absolute pathname, invalid fd, %d, provided as input \n", INVALID_FD);
goto test_failed_exit;
}
close( my_dirfd );
}
my_dirfd = openat(AT_FDCWD, my_dirpathp, O_RDONLY, 0 );
if ( my_dirfd == -1 ) {
printf( "openat call failed with error %d - \"%s\" \n", errno, strerror( errno) );
printf( "\t Directory we attempted to open -> \"%s\" \n", my_dirpathp );
goto test_failed_exit;
}
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_namep, NAME_MAX, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
*my_namep = 0x00;
if (is_absolute_path) {
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
*my_pathp = 0x00;
}
my_err = create_random_name_at(my_dirfd, my_dirpathp, my_namep, NAME_MAX, my_pathp, PATH_MAX, 1);
if ( my_err != 0 ) {
goto test_failed_exit;
}
my_err = fchmodat(my_dirfd, my_namep, S_IRWXU, 0);
if ( my_err == -1 ) {
printf( "chmod call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
my_err = fchmodat( my_dirfd, my_namep, (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP), 0 );
if ( my_err == -1 ) {
printf( "chmod call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
my_err = faccessat( my_dirfd, my_namep, (X_OK), 0 );
if ( my_err == 0 ) {
printf( "access call should have failed, but did not. \n" );
goto test_failed_exit;
}
else if ( my_err == -1 ) {
int tmp = 0;
tmp = getuid( );
my_err = errno;
if ( ( tmp == 0 && my_err != EPERM) || (tmp != 0 && my_err != EACCES) ) {
printf( "access failed with errno %d - %s. \n", my_err, strerror( my_err ) );
goto test_failed_exit;
}
}
if (is_absolute_path) {
my_err = fstatat( INVALID_FD, my_pathp, &my_sb, 0 );
if ( my_err != 0 ) {
printf( "fstatat call failed with an absolute pathname. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
}
my_err = fstatat( my_dirfd, my_namep, &my_sb, 0 );
if ( my_err != 0 ) {
printf( "stat call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
if ( (my_sb.st_mode & (S_IRWXO | S_IXGRP)) != 0 ||
(my_sb.st_mode & (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP)) == 0 ) {
printf( "chmod call appears to have failed. stat shows incorrect values in st_mode! \n" );
goto test_failed_exit;
}
file_handle = fopen(FILE_NOTME, "w");
fclose(file_handle);
euid = geteuid();
ruid = getuid();
setreuid(ruid, ruid);
file_handle = fopen(FILE_ME, "w");
fclose(file_handle);
error_occurred = 0;
my_err = unlinkat( AT_FDCWD, FILE_NOTME, 0 );
if (my_err < 0) {
my_err = errno;
}
if (my_err == 0) {
printf("Unresolved: First attempt deleted '" FILE_NOTME "'! \n");
error_occurred = 1;
} else {
printf("Passed: First attempt to delete '" FILE_NOTME "' failed with error %d - %s.\n", my_err, strerror( my_err ));
faccessat(AT_FDCWD, FILE_ME, _DELETE_OK, 0 );
my_err = unlinkat(AT_FDCWD, FILE_NOTME, 0);
if (my_err < 0) {
my_err = errno;
}
if (my_err == 0) {
printf("Failed: Second attempt deleted '" FILE_NOTME "'!\n");
error_occurred = 1;
} else if (my_err == 13) {
printf("Passed: Second attempt to delete '" FILE_NOTME "' failed with error %d - %s.\n", my_err, strerror( my_err ));
} else {
printf("Failed: Second attempt to delete '" FILE_NOTME "' failed with error %d - %s.\n", my_err, strerror( my_err ));
error_occurred = 1;
}
}
setreuid(ruid, euid);
if(error_occurred == 1) {
goto test_failed_exit;
}
my_fd = openat( my_dirfd, my_namep, O_RDONLY, 0);
if ( my_fd == -1 ) {
printf( "openat call failed with error %d - \"%s\" \n", errno, strerror( errno) );
printf( "\t we attempted to open -> \"%s\" \n", &g_target_path[0] );
goto test_failed_exit;
}
my_err = fchmod( my_fd, S_IRWXU );
if ( my_err == -1 ) {
printf( "fchmod call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
my_err = fstatat( INVALID_FD, my_pathp, &my_sb, 0 );
if ( my_err != 0 ) {
printf( "stat call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
if ( (my_sb.st_mode & (S_IRWXG | S_IRWXO)) != 0 ||
(my_sb.st_mode & (S_IRWXU)) == 0 ) {
printf( "fchmod call appears to have failed. stat shows incorrect values in st_mode! \n" );
goto test_failed_exit;
}
my_err = 0;
goto test_passed_exit;
test_failed_exit:
my_err = -1;
test_passed_exit:
if ( my_fd != -1 )
close( my_fd );
if ( my_pathp != NULL ) {
remove( my_pathp );
vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
}
if ( my_namep != NULL ) {
unlinkat(my_dirfd, my_namep, 0);
vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, NAME_MAX);
}
if ( my_dirfd != -1)
close( my_dirfd);
if ( my_dirpathp != NULL ) {
vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
}
return( my_err );
}
int fchownat_fchown_symlinkat_test( void * the_argp )
{
int my_err, my_group_count, i;
int my_fd = -1;
int my_dirfd = -1;
char * my_dirpathp = NULL;
char * my_namep = NULL;
char * my_link_namep = NULL;
char * my_pathp = NULL;
char * my_link_pathp = NULL;
int is_absolute_path = 0;
uid_t my_orig_uid;
gid_t my_orig_gid, my_new_gid1 = 0, my_new_gid2 = 0;
ssize_t my_result;
struct stat my_sb;
gid_t my_groups[ NGROUPS_MAX ];
char my_buffer[ 64 ];
kern_return_t my_kr;
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_dirpathp, PATH_MAX, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
*my_dirpathp = 0x00;
strlcat( my_dirpathp, &g_target_path[0], PATH_MAX );
if (*my_dirpathp == '/') {
is_absolute_path = 1;
my_dirfd = openat(INVALID_FD, my_dirpathp, O_RDONLY, 0 );
if ( my_dirfd == -1 ) {
printf( "openat call failed with error %d - \"%s\" \n", errno, strerror( errno) );
printf( "\t Directory we attempted to open -> \"%s\" \n", my_dirpathp );
printf( "\t Was Absolute pathname, invalid fd, %d, provided as input \n", INVALID_FD);
goto test_failed_exit;
}
close( my_dirfd );
}
my_dirfd = openat(AT_FDCWD, my_dirpathp, O_RDONLY, 0 );
if ( my_dirfd == -1 ) {
printf( "openat call failed with error %d - \"%s\" \n", errno, strerror( errno) );
printf( "\t Directory we attempted to open -> \"%s\" \n", my_dirpathp );
goto test_failed_exit;
}
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_namep, NAME_MAX, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
*my_namep = 0x00;
if (is_absolute_path) {
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
*my_pathp = 0x00;
}
my_err = create_random_name_at(my_dirfd, my_dirpathp, my_namep, NAME_MAX, my_pathp, PATH_MAX, 1);
if ( my_err != 0 ) {
goto test_failed_exit;
}
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_link_namep, NAME_MAX, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
*my_link_namep = 0x00;
if (is_absolute_path) {
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_link_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
*my_link_pathp = 0x00;
}
my_err = create_random_name_at(my_dirfd, my_dirpathp, my_link_namep, NAME_MAX, my_link_pathp, PATH_MAX, 0 );
if ( my_err != 0 ) {
goto test_failed_exit;
}
if ( !_prime_groups() ) {
goto test_failed_exit;
}
my_group_count = getgroups( NGROUPS_MAX, &my_groups[0] );
if ( my_group_count == -1 || my_group_count < 1 ) {
printf( "getgroups call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
my_err = fstatat( my_dirfd, my_namep, &my_sb, 0 );
if ( my_err != 0 ) {
printf( "stat call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
my_orig_gid = my_sb.st_gid;
my_orig_uid = my_sb.st_uid;
for ( i = 0; i < my_group_count; i++ ) {
if ( my_orig_gid != my_groups[ i ] ) {
if ( my_new_gid1 == 0 ) {
my_new_gid1 = my_groups[ i ];
}
else if( my_new_gid1 != my_groups[ i ] ) {
my_new_gid2 = my_groups[ i ];
break;
}
}
}
if ( i >= my_group_count ) {
printf( "not enough groups to choose from. st_gid is the same as current groups! \n" );
goto test_failed_exit;
}
my_err = fchownat( my_dirfd, my_namep, my_orig_uid, my_new_gid1, 0 );
if ( my_err != 0 ) {
printf( "chown call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
my_err = fstatat( my_dirfd, my_namep, &my_sb, 0 );
if ( my_err != 0 ) {
printf( "stat call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
if ( my_sb.st_gid == my_orig_gid ) {
printf( "chown call failed. st_gid is not correct! \n" );
goto test_failed_exit;
}
if (is_absolute_path) {
my_fd = openat( INVALID_FD, my_pathp, O_RDWR, 0 );
} else {
my_fd = openat( my_dirfd, my_namep, O_RDWR, 0 );
}
if ( my_fd == -1 ) {
printf( "openat call failed with error %d - \"%s\" \n", errno, strerror( errno) );
printf( "\t we attempted to open -> \"%s\" \n", &g_target_path[0] );
goto test_failed_exit;
}
my_err = fchown( my_fd, my_orig_uid, my_new_gid2 );
if ( my_err != 0 ) {
printf( "fchown call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
my_err = fstatat( my_dirfd, my_namep, &my_sb, 0 );
if ( my_err != 0 ) {
printf( "fstatat call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
if ( my_sb.st_gid == my_new_gid1 ) {
printf( "fchown call failed. st_gid is not correct! \n" );
goto test_failed_exit;
}
my_err = symlinkat( my_namep, my_dirfd, my_link_namep );
if ( my_err != 0 ) {
printf( "symlinkat call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
my_err = fstatat( my_dirfd, my_link_namep, &my_sb, AT_SYMLINK_NOFOLLOW );
if ( my_err != 0 ) {
printf( "fstatat call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
my_orig_gid = my_sb.st_gid;
my_orig_uid = my_sb.st_uid;
my_err = fchownat( my_dirfd, my_link_namep, my_orig_uid, my_new_gid1, AT_SYMLINK_NOFOLLOW );
if ( my_err != 0 ) {
printf( "fchownat call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
my_err = fstatat( my_dirfd, my_link_namep, &my_sb, AT_SYMLINK_NOFOLLOW );
if ( my_err != 0 ) {
printf( "fstatat call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
if ( my_sb.st_gid == my_new_gid2 ) {
printf( "fchownat call failed. st_gid is not correct! \n" );
goto test_failed_exit;
}
my_result = readlinkat( my_dirfd, my_link_namep, &my_buffer[0], sizeof(my_buffer) );
if ( my_result == -1 ) {
printf( "readlinkat call failed. got errno %d - %s. \n", errno, strerror( errno ) );
goto test_failed_exit;
}
if ( my_result < 1 ) {
printf( "readlinkat failed to read any data. \n" );
goto test_failed_exit;
}
my_err = 0;
goto test_passed_exit;
test_failed_exit:
my_err = -1;
test_passed_exit:
if ( my_fd != -1 )
close( my_fd );
if ( my_namep ) {
unlinkat( my_dirfd, my_namep, 0);
vm_deallocate(mach_task_self(), (vm_address_t)my_namep, NAME_MAX);
}
if ( my_pathp != NULL ) {
remove( my_pathp );
vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
}
if ( my_link_namep ) {
unlinkat( my_dirfd, my_link_namep, 0);
vm_deallocate(mach_task_self(), (vm_address_t)my_link_namep, NAME_MAX);
}
if ( my_link_pathp != NULL ) {
unlink( my_link_pathp );
vm_deallocate(mach_task_self(), (vm_address_t)my_link_pathp, PATH_MAX);
}
if ( my_dirfd != -1 )
close(my_dirfd);
if ( my_dirpathp != NULL ) {
vm_deallocate(mach_task_self(), (vm_address_t)my_dirpathp, PATH_MAX);
}
return( my_err );
}
int mkdirat_unlinkat_umask_test( void * the_argp )
{
int my_err;
int my_dirfd = -1;
int my_fd = -1;
int did_umask = 0;
char * my_dirpathp = NULL;
char * my_namep = NULL;
char * my_pathp = NULL;
mode_t my_orig_mask;
struct stat my_sb;
kern_return_t my_kr;
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_dirpathp, PATH_MAX, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
*my_dirpathp = 0x00;
strlcat( my_dirpathp, &g_target_path[0], PATH_MAX );
my_dirfd = openat(AT_FDCWD, my_dirpathp, O_RDONLY, 0 );
if ( my_dirfd == -1 ) {
printf( "openat call failed with error %d - \"%s\" \n", errno, strerror( errno) );
printf( "\t Directory we attempted to open -> \"%s\" \n", my_dirpathp );
goto test_failed_exit;
}
if (*my_dirpathp == '/') {
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
}
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_namep, NAME_MAX, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
*my_namep = 0x00;
if (my_pathp) {
*my_pathp = 0x00;
}
my_err = create_random_name_at( my_dirfd, my_dirpathp, my_namep, NAME_MAX, my_pathp, PATH_MAX, 0 );
if ( my_err != 0 ) {
goto test_failed_exit;
}
my_orig_mask = umask( (S_IXUSR | S_IWGRP | S_IXGRP | S_IWOTH | S_IXOTH) );
did_umask = 1;
my_err = mkdirat( my_dirfd, my_namep, (S_IRWXU | S_IRWXG | S_IRWXO) );
if ( my_err == -1 ) {
printf( "mkdirat failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_err = fstatat( my_dirfd, my_pathp, &my_sb, 0 );
if ( my_err != 0 ) {
printf( "fstat call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
if ( (my_sb.st_mode & (S_IXUSR | S_IWGRP | S_IXGRP | S_IWOTH | S_IXOTH)) != 0 ) {
printf( "umask did not limit modes as it should have \n" );
goto test_failed_exit;
}
my_err = unlinkat( my_dirfd, my_namep, AT_REMOVEDIR );
if ( my_err == -1 ) {
printf( "unlinkat(..., AT_REMOVEDIR) failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_err = 0;
goto test_passed_exit;
test_failed_exit:
my_err = -1;
test_passed_exit:
if ( my_fd != -1 )
close( my_fd );
if ( my_namep ) {
unlinkat( my_dirfd, my_namep, AT_REMOVEDIR );
vm_deallocate(mach_task_self(), (vm_address_t)my_namep, NAME_MAX);
}
if ( my_pathp != NULL ) {
rmdir( my_pathp );
vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
}
if ( my_dirfd != -1 )
close(my_dirfd);
if ( my_dirpathp != NULL ) {
vm_deallocate(mach_task_self(), (vm_address_t)my_dirpathp, PATH_MAX);
}
if ( did_umask != 0 ) {
umask( my_orig_mask );
}
return( my_err );
}
int renameat_test( void * the_argp )
{
int my_err;
int my_dirfd = -1;
char * my_dirpathp = NULL;
char * my_namep = NULL;
char * my_pathp = NULL;
char * my_new_namep = NULL;
char * my_new_pathp = NULL;
ino_t my_file_id;
struct stat my_sb;
kern_return_t my_kr;
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_dirpathp, PATH_MAX, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
*my_dirpathp = 0x00;
strlcat( my_dirpathp, &g_target_path[0], PATH_MAX );
my_dirfd = openat(AT_FDCWD, my_dirpathp, O_RDONLY, 0 );
if ( my_dirfd == -1 ) {
printf( "openat call failed with error %d - \"%s\" \n", errno, strerror( errno) );
printf( "\t Directory we attempted to open -> \"%s\" \n", my_dirpathp );
goto test_failed_exit;
}
if (*my_dirpathp == '/') {
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
}
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_namep, NAME_MAX, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
*my_namep = 0x00;
if (my_pathp) {
*my_pathp = 0x00;
}
my_err = create_random_name_at( my_dirfd, my_dirpathp, my_namep, NAME_MAX, my_pathp, PATH_MAX, 1 );
if ( my_err != 0 ) {
goto test_failed_exit;
}
if (*my_dirpathp == '/') {
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_new_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
}
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_new_namep, NAME_MAX, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
*my_new_namep = 0x00;
if (my_new_pathp) {
*my_new_pathp = 0x00;
}
my_err = create_random_name_at( my_dirfd, my_dirpathp, my_new_namep, NAME_MAX, my_new_pathp, PATH_MAX, 0 );
if ( my_err != 0 ) {
goto test_failed_exit;
}
my_err = fstatat( my_dirfd, my_namep, &my_sb, 0 );
if ( my_err != 0 ) {
printf( "fstatat - failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_file_id = my_sb.st_ino;
my_err = renameat( my_dirfd, my_namep, my_dirfd, my_new_namep );
if ( my_err == -1 ) {
printf( "rename - failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
my_err = fstatat( my_dirfd, my_namep, &my_sb, 0 );
if ( my_err == 0 ) {
printf( "renameat call failed - found old name \n" );
goto test_failed_exit;
}
my_err = fstatat( my_dirfd, my_new_namep, &my_sb, 0 );
if ( my_err != 0 ) {
printf( "stat - failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
if ( my_file_id != my_sb.st_ino ) {
printf( "rename failed - wrong file id \n" );
goto test_failed_exit;
}
if (my_new_pathp) {
my_err = fstatat( INVALID_FD, my_new_pathp, &my_sb, 0 );
if ( my_err != 0 ) {
printf( "stat - failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
if ( my_file_id != my_sb.st_ino ) {
printf( "rename failed - wrong file id \n" );
goto test_failed_exit;
}
}
my_err = 0;
goto test_passed_exit;
test_failed_exit:
my_err = -1;
test_passed_exit:
if ( my_pathp != NULL ) {
remove( my_pathp );
vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
}
if ( my_new_pathp != NULL ) {
remove( my_new_pathp );
vm_deallocate(mach_task_self(), (vm_address_t)my_new_pathp, PATH_MAX);
}
return( my_err );
}
static int __get_except_port(int which, mach_port_t *portp,
exception_behavior_t *behaviorp,
thread_state_flavor_t *flavorp)
{
exception_mask_t masks[EXC_TYPES_COUNT];
mach_msg_type_number_t nmasks = 0;
exception_port_t ports[EXC_TYPES_COUNT];
exception_behavior_t behaviors[EXC_TYPES_COUNT];
thread_state_flavor_t flavors[EXC_TYPES_COUNT];
*portp = MACH_PORT_NULL;
*behaviorp = 0;
*flavorp = 0;
kern_return_t kr = KERN_FAILURE;
if (which == 0) {
kr = host_get_exception_ports(mach_host_self(), EXC_MASK_BAD_ACCESS,
masks, &nmasks, ports, behaviors, flavors);
} else if (which == 1) {
kr = task_get_exception_ports(mach_task_self(), EXC_MASK_BAD_ACCESS,
masks, &nmasks, ports, behaviors, flavors);
} else if (which == 2) {
kr = thread_get_exception_ports(mach_thread_self(), EXC_MASK_BAD_ACCESS,
masks, &nmasks, ports, behaviors, flavors);
} else {
printf("ERROR: invalid 'which' in %s\n", __func__);
return -1;
}
if (kr != KERN_SUCCESS) {
printf("ERROR getting %s exception port!\n", which == 0 ? "task" : "host");
return -1;
}
*portp = ports[0];
*behaviorp = behaviors[0];
*flavorp = flavors[0];
return 0;
}
int set_exception_ports_test( void * the_argp )
{
int testFlavor = -900000;
kern_return_t ret;
mach_port_t exception_port;
mach_port_t old_except_port;
exception_behavior_t old_behavior;
thread_state_flavor_t old_flavor;
ret = mach_port_allocate( mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &exception_port );
if (ret != KERN_SUCCESS) {
printf("ERROR allocating new exception port?!\n");
return -1;
}
ret = mach_port_insert_right( mach_task_self(), exception_port, exception_port, MACH_MSG_TYPE_MAKE_SEND );
if (ret != KERN_SUCCESS) {
printf("ERROR inserting send right into new exception port?!\n");
goto test_failed_exit;
}
if (__get_except_port(2, &old_except_port, &old_behavior, &old_flavor) < 0)
goto test_failed_exit;
ret = thread_set_exception_ports( mach_thread_self(),
EXC_MASK_BAD_ACCESS,
exception_port,
EXCEPTION_STATE_IDENTITY,
testFlavor );
if (ret == KERN_SUCCESS) {
thread_set_exception_ports( mach_thread_self(),
EXC_MASK_BAD_ACCESS,
old_except_port, old_behavior, old_flavor );
printf("thread_set_exception_ports failed: expected !KERN_SUCCESS for flavor %d\n", testFlavor);
goto test_failed_exit;
}
mach_port_deallocate( mach_task_self(), old_except_port );
if (__get_except_port(1, &old_except_port, &old_behavior, &old_flavor) < 0)
goto test_failed_exit;
ret = task_set_exception_ports( mach_task_self(),
EXC_MASK_BAD_ACCESS,
exception_port,
EXCEPTION_STATE_IDENTITY,
testFlavor );
if (ret == KERN_SUCCESS) {
task_set_exception_ports( mach_task_self(),
EXC_MASK_BAD_ACCESS,
old_except_port, old_behavior, old_flavor );
printf("task_set_exception_ports failed: expected !KERN_SUCCESS for flavor %d\n", testFlavor);
goto test_failed_exit;
}
mach_port_deallocate( mach_task_self(), old_except_port );
if (__get_except_port(0, &old_except_port, &old_behavior, &old_flavor) < 0)
goto test_failed_exit;
ret = host_set_exception_ports( mach_host_self(),
EXC_MASK_BAD_ACCESS,
exception_port,
EXCEPTION_STATE_IDENTITY,
testFlavor );
if (ret == KERN_SUCCESS) {
host_set_exception_ports( mach_host_self(),
EXC_MASK_BAD_ACCESS,
old_except_port, old_behavior, old_flavor );
printf("host_set_exception_ports failed: expected !KERN_SUCCESS for flavor %d\n", testFlavor);
goto test_failed_exit;
}
mach_port_deallocate( mach_task_self(), exception_port );
mach_port_deallocate( mach_task_self(), old_except_port );
return 0;
test_failed_exit:
mach_port_deallocate( mach_task_self(), exception_port );
if (old_except_port != MACH_PORT_NULL)
mach_port_deallocate( mach_task_self(), old_except_port );
return -1;
}
#if TEST_SYSTEM_CALLS
int sample_test( void * the_argp )
{
int my_err;
int my_fd = -1;
char * my_pathp = NULL;
kern_return_t my_kr;
my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
if(my_kr != KERN_SUCCESS){
printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto test_failed_exit;
}
*my_pathp = 0x00;
strcat( my_pathp, &g_target_path[0] );
strcat( my_pathp, "/" );
my_err = create_random_name( my_pathp, 1 );
if ( my_err != 0 ) {
goto test_failed_exit;
}
my_err = 0;
goto test_passed_exit;
test_failed_exit:
my_err = -1;
test_passed_exit:
if ( my_fd != -1 )
close( my_fd );
if ( my_pathp != NULL ) {
remove( my_pathp );
vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
}
return( my_err );
}
#endif