]> git.saurik.com Git - apple/xnu.git/blame - tools/tests/xnu_quick_test/tests.c
xnu-1456.1.26.tar.gz
[apple/xnu.git] / tools / tests / xnu_quick_test / tests.c
CommitLineData
2d21ac55
A
1/*
2 * tests.c
3 * xnu_quick_test
4 *
5 * Created by Jerry Cottingham on 3/25/05.
b0d623f7 6 * Copyright 2008 Apple Inc. All rights reserved.
2d21ac55
A
7 *
8 */
9
10#include "tests.h"
2d21ac55
A
11#include <sys/ipc.h> /* for message queue tests */
12#include <sys/msg.h> /* for message queue tests */
13#include <sys/syscall.h> /* for get / settid */
14#include <sys/sysctl.h> /* for determining hw */
15#include <AvailabilityMacros.h> /* for determination of Mac OS X version (tiger, leopard, etc.) */
16#include <libkern/OSByteOrder.h> /* for OSSwap32() */
b0d623f7 17#include <mach/mach.h>
cf7d32b8 18
2d21ac55
A
19extern char g_target_path[ PATH_MAX ];
20extern int g_skip_setuid_tests;
21extern int g_is_under_rosetta;
b0d623f7
A
22extern int g_is_single_user;
23
24
25void print_acct_debug_strings( char * my_ac_comm );
2d21ac55 26
2d21ac55
A
27
28#if TEST_SYSTEM_CALLS /* system calls to do */
29 "reboot", /* 55 = reboot */
30 "revoke", /* 56 = revoke */
31 "sbrk", /* 69 = sbrk */
32 "sstk", /* 70 = sstk */
33 "mount", /* 167 = mount */
34 "unmount", /* 159 = unmount */
35 "undelete", /* 205 = undelete */
36 "watchevent", /* 231 = watchevent */
37 "waitevent", /* 232 = waitevent */
38 "modwatch", /* 233 = modwatch */
39 "fsctl", /* 242 = fsctl */
40 "initgroups", /* 243 = initgroups */
41 "semsys", /* 251 = semsys */
42 "semconfig", /* 257 = semconfig */
43 "msgsys", /* 252 = msgsys */
44 "shmsys", /* 253 = shmsys */
45 "load_shared_file", /* 296 = load_shared_file */
46 "reset_shared_file", /* 297 = reset_shared_file */
47 "new_system_shared_regions", /* 298 = new_system_shared_regions */
48 "shared_region_map_file_np", /* 299 = shared_region_map_file_np */
49 "shared_region_make_private_np", /* 300 = shared_region_make_private_np */
50 "__pthread_kill", /* 328 = __pthread_kill */
51 "pthread_sigmask", /* 329 = pthread_sigmask */
52 "__disable_threadsignal", /* 331 = __disable_threadsignal */
53 "__pthread_markcancel", /* 332 = __pthread_markcancel */
54 "__pthread_canceled", /* 333 = __pthread_canceled */
55 "__semwait_signal", /* 334 = __semwait_signal */
56 "audit", /* 350 = audit */
57 "auditon", /* 351 = auditon */
58 "getaudit", /* 355 = getaudit */
59 "setaudit", /* 356 = setaudit */
60 "getaudit_addr", /* 357 = getaudit_addr */
61 "setaudit_addr", /* 358 = setaudit_addr */
62 "auditctl", /* 359 = auditctl */
63#endif
64
65/* **************************************************************************************************************
66 * Test the syscall system call.
67 * **************************************************************************************************************
68 */
69int syscall_test( void * the_argp )
70{
71 int my_err;
72 int my_fd = -1;
b0d623f7
A
73 char * my_pathp;
74 kern_return_t my_kr;
2d21ac55 75
b0d623f7
A
76 my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
77 if(my_kr != KERN_SUCCESS){
78 printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
79 goto test_failed_exit;
80 }
2d21ac55
A
81
82 *my_pathp = 0x00;
83 strcpy( my_pathp, &g_target_path[0] );
84 strcat( my_pathp, "/" );
85
86 /* create a test file */
87 my_err = create_random_name( my_pathp, 1 );
88 if ( my_err != 0 ) {
89 goto test_failed_exit;
90 }
91
92 /* use an indirect system call to open our test file.
93 * I picked open since it uses a path pointer which grows to 64 bits in an LP64 environment.
94 */
95 my_fd = syscall( SYS_open, my_pathp, (O_RDWR | O_EXCL), 0 );
96 if ( my_fd == -1 ) {
97 printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
98 printf( "\t file we attempted to open -> \"%s\" \n", my_pathp );
99 goto test_failed_exit;
100 }
101
102 my_err = 0;
103 goto test_passed_exit;
104
105test_failed_exit:
106 my_err = -1;
107
108test_passed_exit:
109 if ( my_fd != -1 )
110 close( my_fd );
111 if ( my_pathp != NULL ) {
112 remove( my_pathp );
b0d623f7 113 vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
2d21ac55
A
114 }
115 return( my_err );
116}
117
118/* **************************************************************************************************************
119 * Test fork wait4, and exit system calls.
120 * **************************************************************************************************************
121 */
122int fork_wait4_exit_test( void * the_argp )
123{
124 int my_err, my_status;
125 pid_t my_pid, my_wait_pid;
126 struct rusage my_usage;
127
128 /* spin off another process */
129 my_pid = fork( );
130 if ( my_pid == -1 ) {
131 printf( "fork failed with errno %d - %s \n", errno, strerror( errno ) );
132 return( -1 );
133 }
134 else if ( my_pid == 0 ) {
135 struct stat my_sb;
136
137 /* child process does very little then exits */
138 my_err = stat( &g_target_path[0], &my_sb );
139 if ( my_err != 0 ) {
140 printf( "stat call failed with error %d - \"%s\" \n", errno, strerror( errno) );
141 printf( "\t path we stated \"%s\" \n", &g_target_path[0] );
142 exit( -1 );
143 }
144 exit( 44 );
145 }
146
147 /* parent process waits for child to exit */
148 my_wait_pid = wait4( my_pid, &my_status, 0, &my_usage );
149 if ( my_wait_pid == -1 ) {
150 printf( "wait4 failed with errno %d - %s \n", errno, strerror( errno ) );
151 return( -1 );
152 }
153
154 /* wait4 should return our child's pid when it exits */
155 if ( my_wait_pid != my_pid ) {
156 printf( "wait4 did not return child pid - returned %d should be %d \n", my_wait_pid, my_pid );
157 return( -1 );
158 }
159
160 /* kind of just guessing on these values so if this fails we should take a closer
161 * look at the returned rusage structure.
162 */
163 if ( my_usage.ru_utime.tv_sec > 1 || my_usage.ru_stime.tv_sec > 1 ||
164 my_usage.ru_majflt > 1000 || my_usage.ru_msgsnd > 100 ) {
165 printf( "wait4 returned an odd looking rusage structure \n" );
166 return( -1 );
167 }
168
169 if ( WIFEXITED( my_status ) && WEXITSTATUS( my_status ) == 44 ) {
170 }
171 else {
172 printf( "wait4 returned wrong exit status - 0x%02X \n", my_status );
173 return( -1 );
174 }
175
176 return( 0 );
177}
178
179/* **************************************************************************************************************
180 * Test fsync, ftruncate, lseek, pread, pwrite, read, readv, truncate, write, writev system calls.
181 * **************************************************************************************************************
182 */
183int read_write_test( void * the_argp )
184{
b0d623f7
A
185 int my_fd = -1;
186 int my_err;
2d21ac55
A
187 char * my_pathp = NULL;
188 char * my_bufp = NULL;
189 ssize_t my_result;
190 off_t my_current_offset;
b0d623f7 191 struct iovec my_iovs[2];
2d21ac55 192 struct stat my_sb;
b0d623f7 193 kern_return_t my_kr;
2d21ac55 194
b0d623f7
A
195 my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
196 if(my_kr != KERN_SUCCESS){
197 printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
198 goto test_failed_exit;
199 }
2d21ac55 200
b0d623f7
A
201 my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_bufp, MY_BUFFER_SIZE, VM_FLAGS_ANYWHERE);
202 if(my_kr != KERN_SUCCESS){
203 printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
204 goto test_failed_exit;
205 }
2d21ac55
A
206
207 *my_pathp = 0x00;
208 strcat( my_pathp, &g_target_path[0] );
209 strcat( my_pathp, "/" );
210
211 /* create a test file */
212 my_err = create_random_name( my_pathp, 1 );
213 if ( my_err != 0 ) {
214 goto test_failed_exit;
215 }
216
217 my_fd = open( my_pathp, O_RDONLY, 0 );
218 if ( my_fd == -1 ) {
219 printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
220 printf( "\t file we attempted to open -> \"%s\" \n", my_pathp );
221 goto test_failed_exit;
222 }
223
224 /* should get EOF since the file is empty at this point */
225 my_result = read( my_fd, my_bufp, 10);
226 if ( my_result == -1 ) {
227 printf( "read call failed with error %d - \"%s\" \n", errno, strerror( errno) );
228 goto test_failed_exit;
229 }
230 if ( my_result != 0 ) {
231 if ( sizeof( ssize_t ) > sizeof( int ) ) {
b0d623f7 232 printf( "read call failed - should have read 0 bytes on empty file - read %ld \n", (long int) my_result );
2d21ac55
A
233 }
234 else {
b0d623f7 235 printf( "read call failed - should have read 0 bytes on empty file - read %d \n", (int) my_result );
2d21ac55
A
236 }
237 goto test_failed_exit;
238 }
239
240 /* this write should fail since we opened for read only */
241 my_result = write( my_fd, my_bufp, 10 );
242 my_err = errno;
243 if ( my_result != -1 ) {
244 if ( sizeof( ssize_t ) > sizeof( int ) ) {
b0d623f7 245 printf( "write should have failed for read only fd - %ld \n", (long int) my_result );
2d21ac55
A
246 }
247 else {
b0d623f7 248 printf( "write should have failed for read only fd - %d \n", (int) my_result );
2d21ac55
A
249 }
250 goto test_failed_exit;
251 }
252 if ( my_err != EBADF ) {
253 printf( "write call failed with error %d - \"%s\" \n", errno, strerror( errno) );
254 printf( "should have failed with EBADF error %d \n", EBADF );
255 goto test_failed_exit;
256 }
257
258 /* now really write some data */
259 close( my_fd );
260 my_fd = open( my_pathp, O_RDWR, 0 );
261 if ( my_fd == -1 ) {
262 printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
263 printf( "\t file we attempted to open -> \"%s\" \n", my_pathp );
264 goto test_failed_exit;
265 }
266
267 memset( my_bufp, 'j', MY_BUFFER_SIZE );
268 my_result = write( my_fd, my_bufp, MY_BUFFER_SIZE );
269 if ( my_result == -1 ) {
270 printf( "write call failed with error %d - \"%s\" \n", errno, strerror( errno) );
271 goto test_failed_exit;
272 }
273 if ( my_result != MY_BUFFER_SIZE ) {
274 printf( "write failed to write out all the data \n" );
275 goto test_failed_exit;
276 }
277
278 /* push data to disk */
279 my_err = fsync( my_fd );
280 if ( my_err == -1 ) {
281 printf( "fsync failed with errno %d - %s \n", errno, strerror( errno ) );
282 goto test_failed_exit;
283 }
284
285 /* now verify the write worked OK using readv */
286 lseek( my_fd, 0, SEEK_SET );
287 bzero( (void *)my_bufp, MY_BUFFER_SIZE );
288 my_iovs[0].iov_base = my_bufp;
289 my_iovs[0].iov_len = 16;
290 my_iovs[1].iov_base = (my_bufp + MY_BUFFER_SIZE - 16) ;
291 my_iovs[1].iov_len = 16;
292
293 my_result = readv( my_fd, &my_iovs[0], 2 );
294 if ( my_result == -1 ) {
295 printf( "readv call failed with error %d - \"%s\" \n", errno, strerror( errno) );
296 goto test_failed_exit;
297 }
298 if ( my_result != 32 ) {
b0d623f7 299 printf( "readv failed to get all the data - asked for %d got back %d\n", MY_BUFFER_SIZE, (int) my_result );
2d21ac55
A
300 goto test_failed_exit;
301 }
302 if ( *my_bufp != 'j' || *(my_bufp + (MY_BUFFER_SIZE - 1)) != 'j' ) {
303 printf( "readv failed to get correct data \n" );
304 goto test_failed_exit;
305 }
306
307 /* test ftruncate */
308 my_err = ftruncate( my_fd, 0 );
309 if ( my_err == -1 ) {
310 printf( "ftruncate call failed with error %d - \"%s\" \n", errno, strerror( errno) );
311 goto test_failed_exit;
312 }
313
314 my_err = fstat( my_fd, &my_sb );
315 if ( my_err == -1 ) {
316 printf( "fstat call failed with error %d - \"%s\" \n", errno, strerror( errno) );
317 goto test_failed_exit;
318 }
319 if ( my_sb.st_size != 0 ) {
320 printf( "ftruncate call failed - file size is wrong \n" );
321 goto test_failed_exit;
322 }
323
324 /* test writev */
325 lseek( my_fd, 0, SEEK_SET );
326 memset( my_bufp, 'z', MY_BUFFER_SIZE );
327 my_iovs[0].iov_base = my_bufp;
328 my_iovs[0].iov_len = 8;
329 my_iovs[1].iov_base = (my_bufp + MY_BUFFER_SIZE - 8) ;
330 my_iovs[1].iov_len = 8;
331 my_result = writev( my_fd, &my_iovs[0], 2 );
332 if ( my_result == -1 ) {
333 printf( "writev call failed with error %d - \"%s\" \n", errno, strerror( errno) );
334 goto test_failed_exit;
335 }
336 if ( my_result != 16 ) {
b0d623f7 337 printf( "writev failed to get all the data - asked for %d got back %d\n", MY_BUFFER_SIZE, (int) my_result );
2d21ac55
A
338 goto test_failed_exit;
339 }
340
341 /* now verify the writev worked OK */
342 lseek( my_fd, 0, SEEK_SET );
343 bzero( (void *)my_bufp, MY_BUFFER_SIZE );
344 my_iovs[0].iov_base = my_bufp;
345 my_iovs[0].iov_len = 8;
346 my_iovs[1].iov_base = (my_bufp + MY_BUFFER_SIZE - 8) ;
347 my_iovs[1].iov_len = 8;
348
349 my_result = readv( my_fd, &my_iovs[0], 2 );
350 if ( my_result == -1 ) {
351 printf( "readv call failed with error %d - \"%s\" \n", errno, strerror( errno) );
352 goto test_failed_exit;
353 }
354 if ( my_result != 16 ) {
b0d623f7 355 printf( "readv failed to get all the data - asked for %d got back %d\n", MY_BUFFER_SIZE, (int) my_result );
2d21ac55
A
356 goto test_failed_exit;
357 }
358 if ( *my_bufp != 'z' || *(my_bufp + (MY_BUFFER_SIZE - 1)) != 'z' ) {
359 printf( "readv failed to get correct data \n" );
360 goto test_failed_exit;
361 }
362
363 /* test pread and pwrite */
364 my_current_offset = lseek( my_fd, 0, SEEK_CUR );
365 if ( my_current_offset == -1 ) {
366 printf( "lseek call failed with error %d - \"%s\" \n", errno, strerror( errno) );
367 goto test_failed_exit;
368 }
369
370 my_result = pwrite( my_fd, "jer", 3, my_current_offset );
371 if ( my_result == -1 ) {
372 printf( "pwrite call failed with error %d - \"%s\" \n", errno, strerror( errno) );
373 goto test_failed_exit;
374 }
375 if ( my_result != 3 ) {
376 printf( "pwrite failed to write all the data \n" );
377 goto test_failed_exit;
378 }
379
380 /* make sure file position did not advance */
381 if ( my_current_offset != lseek( my_fd, 0, SEEK_CUR ) ) {
382 printf( "pwrite advanced file positiion \n" );
383 goto test_failed_exit;
384 }
385
386 bzero( (void *)my_bufp, MY_BUFFER_SIZE );
387 my_result = pread( my_fd, my_bufp, 3, my_current_offset );
388 if ( my_result == -1 ) {
389 printf( "pread call failed with error %d - \"%s\" \n", errno, strerror( errno) );
390 goto test_failed_exit;
391 }
392 if ( my_result != 3 ) {
393 printf( "pread failed to write all the data \n" );
394 goto test_failed_exit;
395 }
396
397 /* make sure file position did not advance */
398 if ( my_current_offset != lseek( my_fd, 0, SEEK_CUR ) ) {
399 printf( "pread advanced file positiion \n" );
400 goto test_failed_exit;
401 }
402
403 /* make sure pread and pwrite transferred correct data */
404 if ( strcmp( my_bufp, "jer" ) != 0 ) {
405 printf( "pread or pwrite failed to read / write correct data \n" );
406 goto test_failed_exit;
407 }
408
409 /* test truncate */
410 my_err = truncate( my_pathp, 0 );
411 if ( my_err == -1 ) {
412 printf( "truncate call failed with error %d - \"%s\" \n", errno, strerror( errno) );
413 goto test_failed_exit;
414 }
415
416 my_err = stat( my_pathp, &my_sb );
417 if ( my_err == -1 ) {
418 printf( "stat call failed with error %d - \"%s\" \n", errno, strerror( errno) );
419 goto test_failed_exit;
420 }
421 if ( my_sb.st_size != 0 ) {
422 printf( "truncate call failed - file size is wrong \n" );
423 goto test_failed_exit;
424 }
425
426 my_err = 0;
427 goto test_passed_exit;
428
429test_failed_exit:
430 my_err = -1;
431
432test_passed_exit:
433 if ( my_fd != -1 )
434 close( my_fd );
435 if ( my_pathp != NULL ) {
b0d623f7
A
436 remove( my_pathp );
437 vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
2d21ac55
A
438 }
439 if ( my_bufp != NULL )
b0d623f7 440 vm_deallocate(mach_task_self(), (vm_address_t)my_bufp, MY_BUFFER_SIZE);
2d21ac55
A
441 return( my_err );
442}
443
444/* **************************************************************************************************************
445 * Test close, fpathconf, fstat, open, pathconf system calls.
446 * **************************************************************************************************************
447 */
448int open_close_test( void * the_argp )
449{
b0d623f7
A
450 int my_err;
451 int my_fd = -1;
2d21ac55
A
452 char * my_pathp = NULL;
453 ssize_t my_result;
454 long my_pconf_result;
455 struct stat my_sb;
456 char my_buffer[32];
b0d623f7 457 kern_return_t my_kr;
2d21ac55 458
b0d623f7
A
459 my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
460 if(my_kr != KERN_SUCCESS){
461 printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
462 goto test_failed_exit;
463 }
2d21ac55
A
464
465 *my_pathp = 0x00;
466 strcat( my_pathp, &g_target_path[0] );
467 strcat( my_pathp, "/" );
468
469 /* create a test file */
470 my_err = create_random_name( my_pathp, 1 );
471 if ( my_err != 0 ) {
472 goto test_failed_exit;
473 }
474
475 /* test O_WRONLY case */
476 my_fd = open( my_pathp, O_WRONLY, 0 );
477 if ( my_fd == -1 ) {
478 printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
479 printf( "\t file we attempted to open -> \"%s\" \n", my_pathp );
480 goto test_failed_exit;
481 }
482
483 /* test pathconf and fpathconf */
484 my_pconf_result = pathconf( my_pathp, _PC_PATH_MAX );
485 if ( my_pconf_result == -1 ) {
486 printf( "pathconf - _PC_PATH_MAX - failed with error %d - \"%s\" \n", errno, strerror( errno) );
487 goto test_failed_exit;
488 }
489// printf( "_PC_PATH_MAX %ld \n", my_pconf_result );
490 /* results look OK? */
491 if ( my_pconf_result < PATH_MAX ) {
b0d623f7 492 printf( "pathconf - _PC_PATH_MAX - looks like wrong results \n" );
2d21ac55
A
493 goto test_failed_exit;
494 }
495
496 my_pconf_result = fpathconf( my_fd, _PC_NAME_MAX );
497 if ( my_pconf_result == -1 ) {
498 printf( "fpathconf - _PC_PATH_MAX - failed with error %d - \"%s\" \n", errno, strerror( errno) );
499 goto test_failed_exit;
500 }
501// printf( "_PC_NAME_MAX %ld \n", my_pconf_result );
502 /* results look OK? */
503 if ( my_pconf_result < 6 ) {
b0d623f7 504 printf( "fpathconf - _PC_NAME_MAX - looks like wrong results \n" );
2d21ac55
A
505 goto test_failed_exit;
506 }
507
508 /* write some data then try to read it */
509 my_result = write( my_fd, "kat", 3 );
510 my_err = errno;
511 if ( my_result != 3 ) {
512 if ( sizeof( ssize_t ) > sizeof( int ) ) {
b0d623f7 513 printf( "write failed. should have written 3 bytes actually wrote - %ld \n", (long int) my_result );
2d21ac55
A
514 }
515 else {
b0d623f7 516 printf( "write failed. should have written 3 bytes actually wrote - %d \n", (int) my_result );
2d21ac55
A
517 }
518 goto test_failed_exit;
519 }
520
521 /* Try to read - this should fail since we opened file with O_WRONLY */
522 my_result = read( my_fd, &my_buffer[0], sizeof(my_buffer) );
523 my_err = errno;
524 if ( my_result != -1 ) {
525 printf( "read call should have failed with errno 9 (EBADF) \n" );
526 goto test_failed_exit;
527 }
528 else if ( my_err != EBADF ) {
529 printf( "read call should have failed with errno 9 (EBADF). actually failed with %d - \"%s\" \n", my_err, strerror( my_err) );
530 goto test_failed_exit;
531 }
532
533 close( my_fd );
534
535 /* test O_TRUNC and O_APPEND case */
536 my_fd = open( my_pathp, (O_RDWR | O_TRUNC | O_APPEND), 0 );
537 if ( my_fd == -1 ) {
538 printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
539 printf( "\t file we attempted to open -> \"%s\" \n", my_pathp );
540 goto test_failed_exit;
541 }
542
543 my_result = read( my_fd, &my_buffer[0], sizeof(my_buffer) );
544 if ( my_result == -1 ) {
545 printf( "read call failed with error %d - \"%s\" \n", errno, strerror( errno) );
546 goto test_failed_exit;
547 }
548 if ( my_result != 0 ) {
549 printf( "read failed - should have read 0 bytes. \n" );
550 goto test_failed_exit;
551 }
552
553 my_result = write( my_fd, "kat", 3 );
554 my_err = errno;
555 if ( my_result != 3 ) {
556 if ( sizeof( ssize_t ) > sizeof( int ) ) {
b0d623f7 557 printf( "write failed. should have written 3 bytes actually wrote - %ld \n", (long int) my_result );
2d21ac55
A
558 }
559 else {
b0d623f7 560 printf( "write failed. should have written 3 bytes actually wrote - %d \n", (int) my_result );
2d21ac55
A
561 }
562 goto test_failed_exit;
563 }
564
565 /* add some more data to the test file - this should be appended */
566 lseek( my_fd, 0, SEEK_SET );
567 my_result = write( my_fd, "zzz", 3 );
568 my_err = errno;
569 if ( my_result != 3 ) {
570 if ( sizeof( ssize_t ) > sizeof( int ) ) {
b0d623f7 571 printf( "write failed. should have written 3 bytes actually wrote - %ld \n", (long int) my_result );
2d21ac55
A
572 }
573 else {
b0d623f7 574 printf( "write failed. should have written 3 bytes actually wrote - %d \n", (int) my_result );
2d21ac55
A
575 }
576 goto test_failed_exit;
577 }
578
579 /* now verify the writes */
580 bzero( (void *)&my_buffer[0], sizeof(my_buffer) );
581 lseek( my_fd, 0, SEEK_SET );
582 my_result = read( my_fd, &my_buffer[0], sizeof(my_buffer) );
583 if ( my_result == -1 ) {
584 printf( "read call failed with error %d - \"%s\" \n", errno, strerror( errno) );
585 goto test_failed_exit;
586 }
587 if ( my_buffer[0] != 'k' || my_buffer[5] != 'z' ) {
588 printf( "read failed to get correct data \n" );
589 goto test_failed_exit;
590 }
591
592 /* test fstat */
593 my_err = fstat( my_fd, &my_sb );
594 if ( my_err == -1 ) {
595 printf( "fstat call failed with error %d - \"%s\" \n", errno, strerror( errno) );
596 goto test_failed_exit;
597 }
598 if ( my_sb.st_size != 6 ) {
599 printf( "fstat call failed - st_size is wrong \n" );
600 goto test_failed_exit;
601 }
602 if ( !S_ISREG( my_sb.st_mode ) ) {
603 printf( "fstat call failed - st_mode does not indicate regular file \n" );
604 goto test_failed_exit;
605 }
606
607 my_err = 0;
608 goto test_passed_exit;
609
610test_failed_exit:
611 my_err = -1;
612
613test_passed_exit:
614 if ( my_fd != -1 )
615 close( my_fd );
616 if ( my_pathp != NULL ) {
b0d623f7
A
617 remove( my_pathp );
618 vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
2d21ac55
A
619 }
620 return( my_err );
621}
622
623/* **************************************************************************************************************
624 * Test link, stat and unlink system calls.
625 * **************************************************************************************************************
626 */
627int link_stat_unlink_test( void * the_argp )
628{
b0d623f7
A
629 int my_err;
630 int my_fd = -1;
2d21ac55
A
631 char * my_pathp = NULL;
632 char * my_path2p = NULL;
633 nlink_t my_link_count;
634 ssize_t my_result;
635 struct stat my_sb;
b0d623f7 636 kern_return_t my_kr;
2d21ac55 637
b0d623f7
A
638 my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
639 if(my_kr != KERN_SUCCESS){
640 printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
641 goto test_failed_exit;
642 }
643
644 my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_path2p, PATH_MAX, VM_FLAGS_ANYWHERE);
645 if(my_kr != KERN_SUCCESS){
646 printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
647 goto test_failed_exit;
648 }
2d21ac55
A
649
650 *my_pathp = 0x00;
651 *my_path2p = 0x00;
652 strcat( my_pathp, &g_target_path[0] );
653 strcat( my_pathp, "/" );
654
655 /* create a test file */
656 my_err = create_random_name( my_pathp, 1 );
657 if ( my_err != 0 ) {
658 goto test_failed_exit;
659 }
660
661 /* now create a name for the link file */
662 strcat( my_path2p, my_pathp );
663 strcat( my_path2p, "link" );
664
665 /* get the current link count */
666 my_err = stat( my_pathp, &my_sb );
667 if ( my_err != 0 ) {
668 printf( "stat call failed. got errno %d - %s. \n", errno, strerror( errno ) );
669 goto test_failed_exit;
670 }
671 my_link_count = my_sb.st_nlink;
672
673 /* check file size (should be 0) */
674 if ( my_sb.st_size != 0 ) {
675 printf( "stat structure looks bogus for test file \"%s\" \n", my_pathp );
676 printf( "st_size is not 0 \n" );
677 goto test_failed_exit;
678 }
679
680 /* change file size */
681 my_fd = open( my_pathp, O_RDWR, 0 );
682 if ( my_fd == -1 ) {
683 printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
684 printf( "\t file we attempted to open -> \"%s\" \n", my_pathp );
685 goto test_failed_exit;
686 }
687 my_result = write( my_fd, "kat", 3 );
688 my_err = errno;
689 if ( my_result != 3 ) {
690 if ( sizeof( ssize_t ) > sizeof( int ) ) {
b0d623f7 691 printf( "write failed. should have written 3 bytes actually wrote - %ld \n", (long int) my_result );
2d21ac55
A
692 }
693 else {
b0d623f7 694 printf( "write failed. should have written 3 bytes actually wrote - %d \n", (int) my_result );
2d21ac55
A
695 }
696 goto test_failed_exit;
697 }
698 close( my_fd );
699 my_fd = -1;
700
701 /* now link another file to our test file and recheck link count */
702 my_err = link( my_pathp, my_path2p );
703 if ( my_err != 0 ) {
704 printf( "link call failed. got errno %d - %s. \n", errno, strerror( errno ) );
705 goto test_failed_exit;
706 }
707 my_err = stat( my_pathp, &my_sb );
708 if ( my_err != 0 ) {
709 printf( "stat call failed. got errno %d - %s. \n", errno, strerror( errno ) );
710 goto test_failed_exit;
711 }
712 if ( (my_link_count + 1) != my_sb.st_nlink ) {
713 printf( "stat structure looks bogus for test file \"%s\" \n", my_pathp );
714 printf( "incorrect st_nlink \n" );
715 goto test_failed_exit;
716 }
717
718 /* check file size (should be 3) */
719 if ( my_sb.st_size != 3 ) {
720 printf( "stat structure looks bogus for test file \"%s\" \n", my_pathp );
721 printf( "st_size is not 3 \n" );
722 goto test_failed_exit;
723 }
724
725 /* now make sure unlink works OK */
726 my_err = unlink( my_path2p );
727 if ( my_err != 0 ) {
728 printf( "unlink call failed. got errno %d - %s. \n", errno, strerror( errno ) );
729 goto test_failed_exit;
730 }
731 my_err = stat( my_pathp, &my_sb );
732 if ( my_err != 0 ) {
733 printf( "stat call failed. got errno %d - %s. \n", errno, strerror( errno ) );
734 goto test_failed_exit;
735 }
736 if ( my_link_count != my_sb.st_nlink ) {
737 printf( "stat structure looks bogus for test file \"%s\" \n", my_pathp );
738 printf( "incorrect st_nlink \n" );
739 goto test_failed_exit;
740 }
741
742 my_err = 0;
743 goto test_passed_exit;
744
745test_failed_exit:
746 my_err = -1;
747
748test_passed_exit:
749 if ( my_fd != -1 )
750 close( my_fd );
751 if ( my_pathp != NULL ) {
b0d623f7
A
752 remove( my_pathp );
753 vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
2d21ac55
A
754 }
755 if ( my_path2p != NULL ) {
756 remove( my_path2p );
b0d623f7 757 vm_deallocate(mach_task_self(), (vm_address_t)my_path2p, PATH_MAX);
2d21ac55
A
758 }
759 return( my_err );
760}
761
762/* **************************************************************************************************************
763 * Test chdir and fchdir system calls.
764 * **************************************************************************************************************
765 */
766int chdir_fchdir_test( void * the_argp )
767{
b0d623f7
A
768 int my_err;
769 int my_fd = -1;
2d21ac55
A
770 char * my_pathp = NULL;
771 char * my_file_namep;
772 struct stat my_sb;
773 struct stat my_sb2;
b0d623f7 774 kern_return_t my_kr;
2d21ac55
A
775
776 char *cwd = getwd(NULL); /* Save current working directory so we can restore later */
777
b0d623f7
A
778 my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
779 if(my_kr != KERN_SUCCESS){
780 printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
781 goto test_failed_exit;
782 }
783
2d21ac55
A
784 *my_pathp = 0x00;
785 strcat( my_pathp, &g_target_path[0] );
786 strcat( my_pathp, "/" );
787
788 /* create a test file */
789 my_err = create_random_name( my_pathp, 1 );
790 if ( my_err != 0 ) {
791 goto test_failed_exit;
792 }
793
794 /* test by doing a stat on the test file using a full path and a partial path.
795 * get full path first.
796 */
797 my_err = stat( my_pathp, &my_sb );
798 if ( my_err != 0 ) {
799 printf( "stat call failed. got errno %d - %s. \n", errno, strerror( errno ) );
800 goto test_failed_exit;
801 }
802
803 /* now do the chdir to our test directory and then do the stat relative to that location */
804 my_err = chdir( &g_target_path[0] );
805 if ( my_err != 0 ) {
806 printf( "chdir call failed. got errno %d - %s. \n", errno, strerror( errno ) );
807 goto test_failed_exit;
808 }
809
810 my_file_namep = strrchr( my_pathp, '/' );
811 my_file_namep++;
812 my_err = stat( my_file_namep, &my_sb2 );
813 if ( my_err != 0 ) {
814 printf( "stat call failed. got errno %d - %s. \n", errno, strerror( errno ) );
815 goto test_failed_exit;
816 }
817
818 /* both stat buffers should contain the same data since they should be referencing the same
819 * file.
820 */
821 if ( my_sb.st_ino != my_sb2.st_ino || my_sb.st_size != my_sb2.st_size ||
822 my_sb.st_mtimespec.tv_sec != my_sb2.st_mtimespec.tv_sec ||
823 my_sb.st_mtimespec.tv_nsec != my_sb2.st_mtimespec.tv_nsec ) {
824 printf( "chdir call appears to have failed. stat buffer contents do not match! \n" );
825 goto test_failed_exit;
826 }
827
828 /* now change our current directory to "/" and use fchdir to get back to our test directory */
829 my_err = chdir( "/" );
830 if ( my_err != 0 ) {
831 printf( "chdir call failed. got errno %d - %s. \n", errno, strerror( errno ) );
832 goto test_failed_exit;
833 }
834
835 /* we should not find our test file at the root of the volume */
836 my_err = stat( my_file_namep, &my_sb2 );
837 if ( my_err == 0 ) {
838 printf( "chdir to root volume has failed \n" );
839 goto test_failed_exit;
840 }
841
842 /* get a file descriptor to the test directory for use with fchdir */
843 my_fd = open( &g_target_path[0], O_RDONLY, 0 );
844 if ( my_fd == -1 ) {
845 printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
846 printf( "\t we attempted to open -> \"%s\" \n", &g_target_path[0] );
847 goto test_failed_exit;
848 }
849
850 my_err = fchdir( my_fd );
851 if ( my_err == -1 ) {
852 printf( "fchdir call failed. got errno %d - %s. \n", errno, strerror( errno ) );
853 goto test_failed_exit;
854 }
855
856 my_err = stat( my_file_namep, &my_sb2 );
857 if ( my_err != 0 ) {
858 printf( "stat call failed. got errno %d - %s. \n", errno, strerror( errno ) );
859 goto test_failed_exit;
860 }
861
862 /* both stat buffers should contain the same data since they should be referencing the same
863 * file.
864 */
865 if ( my_sb.st_ino != my_sb2.st_ino || my_sb.st_size != my_sb2.st_size ||
866 my_sb.st_mtimespec.tv_sec != my_sb2.st_mtimespec.tv_sec ||
867 my_sb.st_mtimespec.tv_nsec != my_sb2.st_mtimespec.tv_nsec ) {
868 printf( "chdir call appears to have failed. stat buffer contents do not match! \n" );
869 goto test_failed_exit;
870 }
871
872 my_err = 0;
873 goto test_passed_exit;
874
875test_failed_exit:
876 my_err = -1;
877
878test_passed_exit:
879 if ( my_fd != -1 )
880 close( my_fd );
881 if ( my_pathp != NULL ) {
b0d623f7
A
882 remove( my_pathp );
883 vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
2d21ac55
A
884 }
885 if ( chdir(cwd) != 0) /* Changes back to original directory, don't screw up the env. */
886 my_err = -1;
887 return( my_err );
888}
889
890/* **************************************************************************************************************
891 * Test access, chmod and fchmod system calls.
892 * **************************************************************************************************************
893 */
894int access_chmod_fchmod_test( void * the_argp )
895{
b0d623f7
A
896 int my_err;
897 int my_fd = -1;
898 char * my_pathp = NULL;
2d21ac55 899 struct stat my_sb;
b0d623f7 900 kern_return_t my_kr;
2d21ac55 901
b0d623f7
A
902 my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
903 if(my_kr != KERN_SUCCESS){
904 printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
905 goto test_failed_exit;
906 }
2d21ac55
A
907
908 *my_pathp = 0x00;
909 strcat( my_pathp, &g_target_path[0] );
910 strcat( my_pathp, "/" );
911
912 /* create a test file */
913 my_err = create_random_name( my_pathp, 1 );
914 if ( my_err != 0 ) {
915 goto test_failed_exit;
916 }
917
918 /* test chmod */
919 my_err = chmod( my_pathp, S_IRWXU );
920 if ( my_err == -1 ) {
921 printf( "chmod call failed. got errno %d - %s. \n", errno, strerror( errno ) );
922 goto test_failed_exit;
923 }
924
925 my_err = chmod( my_pathp, (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP) );
926 if ( my_err == -1 ) {
927 printf( "chmod call failed. got errno %d - %s. \n", errno, strerror( errno ) );
928 goto test_failed_exit;
929 }
930
931 /* test access - this should fail */
932 my_err = access( my_pathp, (X_OK) );
933 if ( my_err == 0 ) {
934 printf( "access call should have failed, but did not. \n" );
935 goto test_failed_exit;
936 }
937 else if ( my_err == -1 ) {
cf7d32b8
A
938 int tmp = 0;
939 tmp = getuid( );
940
2d21ac55
A
941 /* special case when running as root - we get back EPERM when running as root */
942 my_err = errno;
cf7d32b8
A
943#if !TARGET_OS_EMBEDDED
944 if ( ( tmp == 0 && my_err != EPERM) || (tmp != 0 && my_err != EACCES) ) {
945 printf( "access failed with errno %d - %s. \n", my_err, strerror( my_err ) );
946 goto test_failed_exit;
947 }
948#else
949 if ( ( tmp == 0 && my_err != EACCES) || (tmp != 0 && my_err != EACCES) ) {
2d21ac55
A
950 printf( "access failed with errno %d - %s. \n", my_err, strerror( my_err ) );
951 goto test_failed_exit;
952 }
cf7d32b8 953#endif
2d21ac55
A
954 }
955
956 /* verify correct modes are set */
957 my_err = stat( my_pathp, &my_sb );
958 if ( my_err != 0 ) {
959 printf( "stat call failed. got errno %d - %s. \n", errno, strerror( errno ) );
960 goto test_failed_exit;
961 }
962
963 if ( (my_sb.st_mode & (S_IRWXO | S_IXGRP)) != 0 ||
964 (my_sb.st_mode & (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP)) == 0 ) {
965 printf( "chmod call appears to have failed. stat shows incorrect values in st_mode! \n" );
966 goto test_failed_exit;
967 }
968
969 /* test fchmod */
970 my_fd = open( my_pathp, O_RDONLY, 0 );
971 if ( my_fd == -1 ) {
972 printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
973 printf( "\t we attempted to open -> \"%s\" \n", &g_target_path[0] );
974 goto test_failed_exit;
975 }
976
977 my_err = fchmod( my_fd, S_IRWXU );
978 if ( my_err == -1 ) {
979 printf( "fchmod call failed. got errno %d - %s. \n", errno, strerror( errno ) );
980 goto test_failed_exit;
981 }
982
983 my_err = stat( my_pathp, &my_sb );
984 if ( my_err != 0 ) {
985 printf( "stat call failed. got errno %d - %s. \n", errno, strerror( errno ) );
986 goto test_failed_exit;
987 }
988
989 /* verify correct modes are set */
990 if ( (my_sb.st_mode & (S_IRWXG | S_IRWXO)) != 0 ||
991 (my_sb.st_mode & (S_IRWXU)) == 0 ) {
992 printf( "fchmod call appears to have failed. stat shows incorrect values in st_mode! \n" );
993 goto test_failed_exit;
994 }
995
996 my_err = 0;
997 goto test_passed_exit;
998
999test_failed_exit:
1000 my_err = -1;
1001
1002test_passed_exit:
1003 if ( my_fd != -1 )
1004 close( my_fd );
1005 if ( my_pathp != NULL ) {
1006 remove( my_pathp );
b0d623f7 1007 vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
2d21ac55
A
1008 }
1009 return( my_err );
1010}
1011
1012/* **************************************************************************************************************
1013 * Test chown, fchown, lchown, lstat, readlink, symlink system calls.
1014 * **************************************************************************************************************
1015 */
1016int chown_fchown_lchown_lstat_symlink_test( void * the_argp )
1017{
cf7d32b8 1018#if !TARGET_OS_EMBEDDED
b0d623f7
A
1019 int my_err, my_group_count, i;
1020 int my_fd = -1;
2d21ac55
A
1021 char * my_pathp = NULL;
1022 char * my_link_pathp = NULL;
1023 uid_t my_orig_uid;
1024 gid_t my_orig_gid, my_new_gid1 = 0, my_new_gid2 = 0;
1025 ssize_t my_result;
1026 struct stat my_sb;
1027 gid_t my_groups[ NGROUPS_MAX ];
1028 char my_buffer[ 64 ];
b0d623f7 1029 kern_return_t my_kr;
2d21ac55 1030
b0d623f7
A
1031 my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
1032 if(my_kr != KERN_SUCCESS){
1033 printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
1034 goto test_failed_exit;
1035 }
2d21ac55
A
1036
1037 *my_pathp = 0x00;
1038 strcat( my_pathp, &g_target_path[0] );
1039 strcat( my_pathp, "/" );
1040
1041 /* create a test file */
1042 my_err = create_random_name( my_pathp, 1 );
1043 if ( my_err != 0 ) {
1044 goto test_failed_exit;
1045 }
1046
b0d623f7
A
1047 my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_link_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
1048 if(my_kr != KERN_SUCCESS){
1049 printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
1050 goto test_failed_exit;
1051 }
2d21ac55
A
1052
1053 *my_link_pathp = 0x00;
1054 strcat( my_link_pathp, &g_target_path[0] );
1055 strcat( my_link_pathp, "/" );
1056
1057 /* get a test file name for the link */
1058 my_err = create_random_name( my_link_pathp, 0 );
1059 if ( my_err != 0 ) {
1060 goto test_failed_exit;
1061 }
1062
1063 /* set up by getting a list of groups */
1064 my_group_count = getgroups( NGROUPS_MAX, &my_groups[0] );
cf7d32b8 1065
2d21ac55
A
1066 if ( my_group_count == -1 || my_group_count < 1 ) {
1067 printf( "getgroups call failed. got errno %d - %s. \n", errno, strerror( errno ) );
1068 goto test_failed_exit;
1069 }
1070
1071 my_err = stat( my_pathp, &my_sb );
1072 if ( my_err != 0 ) {
1073 printf( "stat call failed. got errno %d - %s. \n", errno, strerror( errno ) );
1074 goto test_failed_exit;
1075 }
1076
1077 /* now change group owner to something other than current value */
1078 my_orig_gid = my_sb.st_gid;
1079 my_orig_uid = my_sb.st_uid;
cf7d32b8 1080
2d21ac55
A
1081 for ( i = 0; i < my_group_count; i++ ) {
1082 if ( my_orig_gid != my_groups[ i ] ) {
1083 if ( my_new_gid1 == 0 ) {
1084 my_new_gid1 = my_groups[ i ];
1085 }
1086 else {
1087 my_new_gid2 = my_groups[ i ];
1088 break;
1089 }
1090 }
1091 }
1092 if ( i >= my_group_count ) {
1093 printf( "not enough groups to choose from. st_gid is the same as current groups! \n" );
1094 goto test_failed_exit;
1095 }
1096
1097 my_err = chown( my_pathp, my_orig_uid, my_new_gid1 );
1098 if ( my_err != 0 ) {
1099 printf( "chown call failed. got errno %d - %s. \n", errno, strerror( errno ) );
1100 goto test_failed_exit;
1101 }
1102
1103 /* make sure the group owner was changed */
1104 my_err = stat( my_pathp, &my_sb );
1105 if ( my_err != 0 ) {
1106 printf( "stat call failed. got errno %d - %s. \n", errno, strerror( errno ) );
1107 goto test_failed_exit;
1108 }
1109 if ( my_sb.st_gid == my_orig_gid ) {
1110 printf( "chown call failed. st_gid is not correct! \n" );
1111 goto test_failed_exit;
1112 }
1113
1114 /* change group owner back using fchown */
1115 my_fd = open( my_pathp, O_RDWR, 0 );
1116 if ( my_fd == -1 ) {
1117 printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
1118 printf( "\t we attempted to open -> \"%s\" \n", &g_target_path[0] );
1119 goto test_failed_exit;
1120 }
1121
1122 my_err = fchown( my_fd, my_orig_uid, my_new_gid2 );
1123 if ( my_err != 0 ) {
1124 printf( "fchown call failed. got errno %d - %s. \n", errno, strerror( errno ) );
1125 goto test_failed_exit;
1126 }
1127
1128 /* make sure the group owner was changed back to the original value */
1129 my_err = stat( my_pathp, &my_sb );
1130 if ( my_err != 0 ) {
1131 printf( "stat call failed. got errno %d - %s. \n", errno, strerror( errno ) );
1132 goto test_failed_exit;
1133 }
1134 if ( my_sb.st_gid == my_new_gid1 ) {
1135 printf( "fchown call failed. st_gid is not correct! \n" );
1136 goto test_failed_exit;
1137 }
1138
1139 /* create a link file and test lchown */
1140 my_err = symlink( my_pathp, my_link_pathp );
1141 if ( my_err != 0 ) {
1142 printf( "symlink call failed. got errno %d - %s. \n", errno, strerror( errno ) );
1143 goto test_failed_exit;
1144 }
1145
1146 my_err = lstat( my_link_pathp, &my_sb );
1147 if ( my_err != 0 ) {
1148 printf( "lstat call failed. got errno %d - %s. \n", errno, strerror( errno ) );
1149 goto test_failed_exit;
1150 }
1151
1152 /* now change group owner to something other than current value */
1153 my_orig_gid = my_sb.st_gid;
1154 my_orig_uid = my_sb.st_uid;
1155 my_err = lchown( my_link_pathp, my_orig_uid, my_new_gid1 );
1156 if ( my_err != 0 ) {
1157 printf( "lchown call failed. got errno %d - %s. \n", errno, strerror( errno ) );
1158 goto test_failed_exit;
1159 }
1160
1161 /* make sure the group owner was changed to new value */
1162 my_err = lstat( my_link_pathp, &my_sb );
1163 if ( my_err != 0 ) {
1164 printf( "lstat call failed. got errno %d - %s. \n", errno, strerror( errno ) );
1165 goto test_failed_exit;
1166 }
1167 if ( my_sb.st_gid == my_new_gid2 ) {
1168 printf( "lchown call failed. st_gid is not correct! \n" );
1169 goto test_failed_exit;
1170 }
1171
1172 /* make sure we can read the symlink file */
1173 my_result = readlink( my_link_pathp, &my_buffer[0], sizeof(my_buffer) );
1174 if ( my_result == -1 ) {
1175 printf( "readlink call failed. got errno %d - %s. \n", errno, strerror( errno ) );
1176 goto test_failed_exit;
1177 }
1178 /* make sure we read some data */
1179 if ( my_result < 1 ) {
1180 printf( "readlink failed to read any data. \n" );
1181 goto test_failed_exit;
1182 }
1183
1184 my_err = 0;
1185 goto test_passed_exit;
1186
1187test_failed_exit:
1188 my_err = -1;
1189
1190test_passed_exit:
1191 if ( my_fd != -1 )
1192 close( my_fd );
1193 if ( my_pathp != NULL ) {
b0d623f7
A
1194 remove( my_pathp );
1195 vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
2d21ac55
A
1196 }
1197 if ( my_link_pathp != NULL ) {
1198 unlink( my_link_pathp );
b0d623f7 1199 vm_deallocate(mach_task_self(), (vm_address_t)my_link_pathp, PATH_MAX);
2d21ac55
A
1200 }
1201 return( my_err );
cf7d32b8
A
1202#else
1203 printf( "\t--> Test not designed for EMBEDDED TARGET\n" );
1204 return 0;
1205#endif
2d21ac55
A
1206}
1207
1208/* **************************************************************************************************************
1209 * Test fstatfs, getattrlist, getfsstat, statfs, getfsstat64, statfs64, fstatfs64 system calls.
1210 * **************************************************************************************************************
1211 */
1212
1213#pragma pack(4)
1214struct vol_attr_buf {
1215 u_int32_t length;
1216 off_t volume_size;
1217 u_int32_t io_blksize;
1218};
1219#pragma pack()
1220typedef struct vol_attr_buf vol_attr_buf;
1221
1222int fs_stat_tests( void * the_argp )
1223{
b0d623f7
A
1224 int my_err, my_count, i;
1225 int my_buffer_size, my_buffer64_size;
1226 int my_fd = -1;
1227 int is_ufs = 0;
1228 long my_io_size;
1229 fsid_t my_fsid;
1230 struct attrlist my_attrlist;
1231 vol_attr_buf my_attr_buf;
2d21ac55 1232 void * my_bufferp = NULL;
2d21ac55 1233 struct statfs * my_statfsp;
b0d623f7
A
1234 kern_return_t my_kr;
1235
1236#if !TARGET_OS_EMBEDDED
1237 void * my_buffer64p = NULL;
2d21ac55 1238 struct statfs64 * my_statfs64p;
2d21ac55 1239
2d21ac55 1240 my_buffer64_size = (sizeof(struct statfs64) * 10);
2d21ac55 1241
b0d623f7
A
1242 my_kr = vm_allocate((vm_map_t) mach_task_self(),(vm_address_t*) &my_buffer64p, my_buffer64_size, VM_FLAGS_ANYWHERE);
1243 if(my_kr != KERN_SUCCESS){
1244 printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
1245 goto test_failed_exit;
2d21ac55
A
1246 }
1247
b0d623f7
A
1248#endif
1249 my_buffer_size = (sizeof(struct statfs) * 10);
1250
1251 my_kr = vm_allocate((vm_map_t) mach_task_self(),(vm_address_t*) &my_bufferp, my_buffer_size, VM_FLAGS_ANYWHERE);
1252 if(my_kr != KERN_SUCCESS){
1253 printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
1254 goto test_failed_exit;
1255 }
1256
2d21ac55
A
1257 my_statfsp = (struct statfs *) my_bufferp;
1258 my_err = statfs( "/", my_statfsp );
1259 if ( my_err == -1 ) {
1260 printf( "statfs call failed. got errno %d - %s. \n", errno, strerror( errno ) );
1261 goto test_failed_exit;
1262 }
1263 if ( memcmp( &my_statfsp->f_fstypename[0], "ufs", 3 ) == 0 ) {
1264 is_ufs = 1;
1265 }
1266
1267 my_count = getfsstat( (struct statfs *)my_bufferp, my_buffer_size, MNT_NOWAIT );
1268 if ( my_count == -1 ) {
1269 printf( "getfsstat call failed. got errno %d - %s. \n", errno, strerror( errno ) );
1270 goto test_failed_exit;
1271 }
1272
1273 /* validate results */
1274 my_statfsp = (struct statfs *) my_bufferp;
1275 for ( i = 0; i < my_count; i++, my_statfsp++ ) {
1276 if ( memcmp( &my_statfsp->f_fstypename[0], "hfs", 3 ) == 0 ||
1277 memcmp( &my_statfsp->f_fstypename[0], "ufs", 3 ) == 0 ||
1278 memcmp( &my_statfsp->f_fstypename[0], "devfs", 5 ) == 0 ||
1279 memcmp( &my_statfsp->f_fstypename[0], "volfs", 5 ) == 0 ) {
1280 /* found a valid entry */
1281 break;
1282 }
1283 }
1284 if ( i >= my_count ) {
1285 printf( "getfsstat call failed. could not find valid f_fstypename! \n" );
1286 goto test_failed_exit;
1287 }
1288
b0d623f7 1289#if !TARGET_OS_EMBEDDED
2d21ac55
A
1290 /* now try statfs64 */
1291 my_statfs64p = (struct statfs64 *) my_buffer64p;
1292 my_err = statfs64( "/", my_statfs64p );
1293 if ( my_err == -1 ) {
1294 printf( "statfs64 call failed. got errno %d - %s. \n", errno, strerror( errno ) );
1295 goto test_failed_exit;
1296 }
1297 if ( my_statfs64p->f_fsid.val[0] != my_statfsp->f_fsid.val[0] ||
1298 my_statfs64p->f_fsid.val[1] != my_statfsp->f_fsid.val[1] ) {
1299 printf( "statfs64 call failed. wrong f_fsid! \n" );
1300 goto test_failed_exit;
1301 }
1302
1303 my_count = getfsstat64( (struct statfs64 *)my_buffer64p, my_buffer64_size, MNT_NOWAIT );
1304 if ( my_count == -1 ) {
1305 printf( "getfsstat64 call failed. got errno %d - %s. \n", errno, strerror( errno ) );
1306 goto test_failed_exit;
1307 }
1308
1309 /* validate results */
1310 my_statfs64p = (struct statfs64 *) my_buffer64p;
1311 for ( i = 0; i < my_count; i++, my_statfs64p++ ) {
1312 if ( memcmp( &my_statfs64p->f_fstypename[0], "hfs", 3 ) == 0 ||
1313 memcmp( &my_statfs64p->f_fstypename[0], "ufs", 3 ) == 0 ||
1314 memcmp( &my_statfs64p->f_fstypename[0], "devfs", 5 ) == 0 ||
1315 memcmp( &my_statfs64p->f_fstypename[0], "volfs", 5 ) == 0 ) {
1316 /* found a valid entry */
1317 break;
1318 }
1319 }
1320 if ( i >= my_count ) {
1321 printf( "getfsstat64 call failed. could not find valid f_fstypename! \n" );
1322 goto test_failed_exit;
1323 }
b0d623f7 1324#endif
2d21ac55
A
1325
1326 /* set up to validate results via multiple sources. we use getattrlist to get volume
1327 * related attributes to verify against results from fstatfs and statfs - but only if
1328 * we are not targeting ufs volume since it doesn't support getattr calls
1329 */
1330 if ( is_ufs == 0 ) {
1331 memset( &my_attrlist, 0, sizeof(my_attrlist) );
1332 my_attrlist.bitmapcount = ATTR_BIT_MAP_COUNT;
1333 my_attrlist.volattr = (ATTR_VOL_SIZE | ATTR_VOL_IOBLOCKSIZE);
1334 my_err = getattrlist( "/", &my_attrlist, &my_attr_buf, sizeof(my_attr_buf), 0 );
1335 if ( my_err != 0 ) {
1336 printf( "getattrlist call failed. got errno %d - %s. \n", errno, strerror( errno ) );
1337 goto test_failed_exit;
1338 }
1339 }
1340
1341 /* open kernel to use as test file for fstatfs */
1342 my_fd = open( "/mach_kernel", O_RDONLY, 0 );
1343 if ( my_fd == -1 ) {
1344 printf( "open call failed. got errno %d - %s. \n", errno, strerror( errno ) );
1345 goto test_failed_exit;
1346 }
1347
b0d623f7 1348#if !TARGET_OS_EMBEDDED
2d21ac55
A
1349 /* testing fstatfs64 */
1350 my_statfs64p = (struct statfs64 *) my_buffer64p;
1351 my_err = fstatfs64( my_fd, my_statfs64p );
1352 if ( my_err == -1 ) {
1353 printf( "fstatfs64 call failed. got errno %d - %s. \n", errno, strerror( errno ) );
1354 goto test_failed_exit;
1355 }
1356
1357 /* validate results - assumes we only boot from hfs or ufs */
1358 if ( !(memcmp( &my_statfs64p->f_fstypename[0], "hfs", 3 ) == 0 ||
1359 memcmp( &my_statfs64p->f_fstypename[0], "ufs", 3 ) == 0) ) {
1360 printf( "fstatfs64 call failed. could not find valid f_fstypename! \n" );
1361 goto test_failed_exit;
1362 }
b0d623f7 1363#endif
2d21ac55
A
1364
1365 /* testing fstatfs */
1366 my_statfsp = (struct statfs *) my_bufferp;
1367 my_err = fstatfs( my_fd, my_statfsp );
1368 if ( my_err == -1 ) {
1369 printf( "fstatfs call failed. got errno %d - %s. \n", errno, strerror( errno ) );
1370 goto test_failed_exit;
1371 }
1372
b0d623f7 1373 /* validate results */
2d21ac55
A
1374 if ( !(memcmp( &my_statfsp->f_fstypename[0], "hfs", 3 ) == 0 ||
1375 memcmp( &my_statfsp->f_fstypename[0], "ufs", 3 ) == 0) ) {
1376 printf( "fstatfs call failed. could not find valid f_fstypename! \n" );
1377 goto test_failed_exit;
1378 }
1379 my_io_size = my_statfsp->f_iosize;
1380 my_fsid = my_statfsp->f_fsid;
1381 if ( is_ufs == 0 && my_statfsp->f_iosize != my_attr_buf.io_blksize ) {
1382 printf( "fstatfs and getattrlist results do not match for volume block size \n" );
1383 goto test_failed_exit;
1384 }
1385
1386 /* try again with statfs */
1387 my_err = statfs( "/mach_kernel", my_statfsp );
1388 if ( my_err == -1 ) {
1389 printf( "statfs call failed. got errno %d - %s. \n", errno, strerror( errno ) );
1390 goto test_failed_exit;
1391 }
1392
b0d623f7 1393 /* validate results */
2d21ac55
A
1394 if ( my_io_size != my_statfsp->f_iosize || my_fsid.val[0] != my_statfsp->f_fsid.val[0] ||
1395 my_fsid.val[1] != my_statfsp->f_fsid.val[1] ) {
1396 printf( "statfs call failed. wrong f_iosize or f_fsid! \n" );
1397 goto test_failed_exit;
1398 }
1399 if ( is_ufs == 0 && my_statfsp->f_iosize != my_attr_buf.io_blksize ) {
1400 printf( "statfs and getattrlist results do not match for volume block size \n" );
1401 goto test_failed_exit;
1402 }
cf7d32b8 1403
2d21ac55
A
1404 my_err = 0;
1405 goto test_passed_exit;
1406
1407test_failed_exit:
1408 my_err = -1;
1409
1410test_passed_exit:
1411 if ( my_fd != -1 )
1412 close( my_fd );
1413 if ( my_bufferp != NULL ) {
b0d623f7 1414 vm_deallocate(mach_task_self(), (vm_address_t)my_bufferp, my_buffer_size);
2d21ac55 1415 }
b0d623f7 1416#if !TARGET_OS_EMBEDDED
2d21ac55 1417 if ( my_buffer64p != NULL ) {
b0d623f7 1418 vm_deallocate(mach_task_self(), (vm_address_t)my_buffer64p, my_buffer64_size);
2d21ac55 1419 }
b0d623f7 1420#endif
cf7d32b8 1421
2d21ac55
A
1422 return( my_err );
1423}
1424
1425/* **************************************************************************************************************
1426 * Test getpid, getppid, and pipe system calls.
1427 * **************************************************************************************************************
1428 */
1429int getpid_getppid_pipe_test( void * the_argp )
1430{
1431 int my_err, my_status;
1432 pid_t my_pid, my_wait_pid;
1433 ssize_t my_count;
1434 int my_fildes[2] = {-1, -1};
1435 off_t my_current_offset;
1436 char my_pid_string[64];
1437
1438 my_err = pipe( &my_fildes[0] );
1439 if ( my_err != 0 ) {
1440 printf( "pipe call failed. got errno %d - %s. \n", errno, strerror( errno ) );
1441 goto test_failed_exit;
1442 }
1443
1444 /* make sure we can't seek on a pipe */
1445 my_current_offset = lseek( my_fildes[0], 0, SEEK_CUR );
1446 if ( my_current_offset != -1 ) {
1447 printf( "lseek on pipe should fail but did not \n" );
1448 goto test_failed_exit;
1449 }
1450
1451 /* fork here and use pipe to communicate */
1452 my_pid = fork( );
1453 if ( my_pid == -1 ) {
1454 printf( "fork failed with errno %d - %s \n", errno, strerror( errno ) );
1455 goto test_failed_exit;
1456 }
1457 else if ( my_pid == 0 ) {
1458 /* child process */
1459 unsigned long my_ppid;
1460 char my_buffer[64];
1461
1462 close( my_fildes[1] ); /* close write end of pipe */
1463 my_fildes[1] = -1;
1464
1465 /* get the parent's pid using getppid and from the parent (using getpid in porent) */
1466 my_count = read( my_fildes[0], &my_buffer[0], sizeof(my_buffer) );
1467 if ( my_count == -1 ) {
1468 printf( "read from pipe failed. got errno %d - %s. \n", errno, strerror( errno ) );
1469 exit(-1);
1470 }
1471
1472 /* parent wrote (to our pipe) its pid as character string */
1473 my_ppid = strtoul( &my_buffer[0], NULL, 10 );
1474 if ( my_ppid == 0 ) {
1475 printf( "strtoul failed. got errno %d - %s. \n", errno, strerror( errno ) );
1476 exit(-1);
1477 }
1478
1479 if ( getppid( ) != my_ppid ) {
1480 printf( "getppid failed. pid we got from parent does not match getppid result. \n" );
1481 exit(-1);
1482 }
1483 exit(0);
1484 }
1485
1486 /* parent process - get our pid using getpid and send it to child for verification */
1487 close( my_fildes[0] ); /* close read end of pipe */
1488 my_fildes[0] = -1;
1489
1490 sprintf( &my_pid_string[0], "%d\n", getpid( ) );
1491
1492 my_count = write( my_fildes[1], &my_pid_string[0], sizeof(my_pid_string) );
1493 if ( my_count == -1 ) {
1494 printf( "write to pipe failed. got errno %d - %s. \n", errno, strerror( errno ) );
1495 goto test_failed_exit;
1496 }
1497
1498 /* wait for child to exit */
1499 my_wait_pid = wait4( my_pid, &my_status, 0, NULL );
1500 if ( my_wait_pid == -1 ) {
1501 printf( "wait4 failed with errno %d - %s \n", errno, strerror( errno ) );
1502 goto test_failed_exit;
1503 }
1504
1505 /* wait4 should return our child's pid when it exits */
1506 if ( my_wait_pid != my_pid ) {
1507 printf( "wait4 did not return child pid - returned %d should be %d \n", my_wait_pid, my_pid );
1508 goto test_failed_exit;
1509 }
1510
1511 if ( WIFEXITED( my_status ) && WEXITSTATUS( my_status ) != 0 ) {
1512 printf( "wait4 returned wrong exit status - 0x%02X \n", my_status );
1513 goto test_failed_exit;
1514 }
1515
1516 my_err = 0;
1517 goto test_passed_exit;
1518
1519test_failed_exit:
1520 my_err = -1;
1521
1522test_passed_exit:
1523 if ( my_fildes[0] != -1 )
1524 close( my_fildes[0] );
1525 if ( my_fildes[1] != -1 )
1526 close( my_fildes[1] );
1527 return( my_err );
1528}
1529
1530
1531/* **************************************************************************************************************
b0d623f7 1532 * Test getauid, gettid, getuid, geteuid, issetugid, setaudit_addr, seteuid, settid, settid_with_pid, setuid system calls.
2d21ac55
A
1533 * **************************************************************************************************************
1534 */
1535int uid_tests( void * the_argp )
1536{
1537 int my_err, my_status;
1538 pid_t my_pid, my_wait_pid;
1539
1540 if ( g_skip_setuid_tests != 0 ) {
1541 printf("\t skipping this test \n");
1542 my_err = 0;
1543 goto test_passed_exit;
1544 }
1545
b0d623f7
A
1546 /* test issetugid - should return 1 when not root and 0 when root
1547 * Figuring out setugid will not work in single-user mode; skip
1548 * this test in that case.
1549 */
1550 if (!g_is_single_user) {
1551 my_err = issetugid( );
1552 if ( getuid( ) == 0 ) {
1553 if ( my_err == 1 ) {
1554 printf( "issetugid should return false \n" );
1555 goto test_failed_exit;
1556 }
2d21ac55 1557 }
b0d623f7
A
1558 else {
1559 if ( my_err == 0 ) {
1560 printf( "issetugid should return true \n" );
1561 goto test_failed_exit;
1562 }
2d21ac55
A
1563 }
1564 }
1565
1566 /*
1567 * fork here and do the setuid work in the child
1568 */
1569 my_pid = fork( );
1570 if ( my_pid == -1 ) {
1571 printf( "fork failed with errno %d - %s \n", errno, strerror( errno ) );
1572 goto test_failed_exit;
1573 }
1574 else if ( my_pid == 0 ) {
1575 /*
1576 * child process
1577 */
b0d623f7
A
1578 uid_t my_ruid, my_euid;
1579 uid_t my_uid, my_temp_uid;
1580 gid_t my_gid, my_temp_gid;
1581 auditinfo_addr_t my_aia;
2d21ac55
A
1582
1583 my_ruid = getuid( );
1584 my_euid = geteuid( );
1585 if ( my_ruid == my_euid ) {
1586 exit( 0 );
1587 }
1588
b0d623f7 1589 /* Test getauid, gettid, setaudit_addr, settid, settid_with_pid */
2d21ac55
A
1590 /* get our current uid and gid for comparison later */
1591 my_uid = getuid( );
1592 my_gid = getgid( );
1593
1594 my_err = syscall( SYS_settid, 4444, 5555 );
1595 //my_err = settid( 4444, 5555 );
1596 if (my_err != 0) {
1597 printf( "settid call failed with error %d - \"%s\" \n", errno, strerror( errno) );
1598 exit( -1 );
1599 }
1600
1601 my_err = syscall( SYS_gettid, &my_temp_uid, &my_temp_gid );
1602 //my_err = gettid( &my_temp_uid, &my_temp_gid );
1603 if (my_err != 0) {
1604 printf( "gettid call failed with error %d - \"%s\" \n", errno, strerror( errno) );
1605 exit( -1 );
1606 }
1607 if (my_temp_uid != 4444) {
1608 printf("get / settid test failed - wrong uid was set - %d \n", my_temp_uid);
1609 exit( -1 );
1610 }
1611 if (my_temp_gid != 5555) {
1612 printf("get / settid test failed - wrong gid was set - %d \n", my_temp_gid);
1613 exit( -1 );
1614 }
1615
1616 /* resume original identity */
1617 my_err = syscall( SYS_settid, KAUTH_UID_NONE, KAUTH_GID_NONE );
1618 //my_err = settid( KAUTH_UID_NONE, KAUTH_GID_NONE );
1619 if (my_err != 0) {
1620 printf( "settid revert - failed with error %d - \"%s\" \n", errno, strerror( errno) );
1621 exit( -1 );
1622 }
1623
1624 /* values should be returned to original settings */
1625 my_temp_uid = getuid( );
1626 if (my_temp_uid == 4444) {
1627 printf("test failed - wrong uid was set - %d \n", my_temp_uid);
1628 exit( -1 );
1629 }
1630 my_temp_gid = getgid( );
1631 if (my_temp_gid == 5555) {
1632 printf("test failed - wrong gid was set - %d \n", my_temp_gid);
1633 exit( -1 );
1634 }
1635
1636 /*
1637 * Assume the identity of our parent.
1638 */
1639 my_err = syscall( SYS_settid_with_pid, getppid( ), 1 );
1640 //my_err = settid_with_pid, my_target_pid, 1 );
1641 if (my_err != 0) {
1642 printf( "settid_with_pid assume - failed with error %d - \"%s\" \n", errno, strerror( errno) );
1643 exit( -1 );
1644 }
1645
1646 /*
1647 * Resume our identity.
1648 */
1649 my_err = syscall( SYS_settid_with_pid, 0, 0 );
1650 //my_err = settid_with_pid( my_target_pid, 0 );
1651 if (my_err != 0) {
1652 printf( "settid_with_pid resume - failed with error %d - \"%s\" \n", errno, strerror( errno) );
1653 exit( -1 );
1654 }
1655
1656 /*
b0d623f7 1657 * test to make sure setaudit_addr doesn't cause audit info to get lost from
2d21ac55
A
1658 * the credential.
1659 */
b0d623f7
A
1660 bzero( &my_aia, sizeof(my_aia) );
1661 my_aia.ai_auid = 442344;
1662 my_aia.ai_asid = AU_ASSIGN_ASID;
1663 my_aia.ai_termid.at_type = AU_IPv4;
1664 my_err = setaudit_addr( &my_aia, sizeof(my_aia) );
2d21ac55 1665 if (my_err != 0) {
b0d623f7 1666 printf( "setaudit_addr - failed with error %d - \"%s\" \n", errno, strerror( errno) );
2d21ac55
A
1667 exit( -1 );
1668 }
1669
b0d623f7
A
1670 my_aia.ai_auid = 0;
1671 my_err = getaudit_addr( &my_aia, sizeof(my_aia) );
2d21ac55 1672 if (my_err != 0) {
b0d623f7 1673 printf( "getaudit_addr - failed with error %d - \"%s\" \n", errno, strerror( errno) );
2d21ac55
A
1674 exit( -1 );
1675 }
b0d623f7 1676 //printf("new audit ID is %d \n", my_aia.ai_auid);
2d21ac55 1677
b0d623f7
A
1678 if (my_aia.ai_auid != 442344) {
1679 printf("test failed - wrong audit ID was set - %d \n", my_aia.ai_auid);
2d21ac55
A
1680 exit( -1 );
1681 }
1682
2d21ac55
A
1683 /* change real uid and effective uid to current euid */
1684 my_err = setuid( my_euid );
1685 if ( my_err == -1 ) {
1686 printf( "setuid call failed with error %d - \"%s\" \n", errno, strerror( errno) );
1687 exit( -1 );
1688 }
1689 if ( getuid( ) != my_euid ) {
1690 printf( "setuid call failed to set the real uid \n" );
1691 exit( -1 );
1692 }
1693
1694 /* change effective uid to current euid - really a NOP */
1695 my_err = seteuid( my_euid );
1696 if ( my_err == -1 ) {
1697 printf( "seteuid call failed with error %d - \"%s\" \n", errno, strerror( errno) );
1698 exit( -1 );
1699 }
1700 if ( geteuid( ) != my_euid ) {
1701 printf( "seteuid call failed to set the original euid \n" );
1702 exit( -1 );
1703 }
1704
1705 /* change real uid and effective uid to original real uid */
1706 my_err = setuid( my_ruid );
1707 if ( my_err == -1 ) {
1708 printf( "setuid call failed with error %d - \"%s\" \n", errno, strerror( errno) );
1709 exit( -1 );
1710 }
1711 if ( getuid( ) != my_ruid ) {
1712 printf( "setuid call failed to set the real uid \n" );
1713 exit( -1 );
1714 }
1715
1716 exit(0);
1717 }
1718
1719 /*
1720 * parent process -
1721 * wait for child to exit
1722 */
1723 my_wait_pid = wait4( my_pid, &my_status, 0, NULL );
1724 if ( my_wait_pid == -1 ) {
1725 printf( "wait4 failed with errno %d - %s \n", errno, strerror( errno ) );
1726 goto test_failed_exit;
1727 }
1728
1729 /* wait4 should return our child's pid when it exits */
1730 if ( my_wait_pid != my_pid ) {
1731 printf( "wait4 did not return child pid - returned %d should be %d \n", my_wait_pid, my_pid );
1732 goto test_failed_exit;
1733 }
1734
1735 if ( WIFEXITED( my_status ) && WEXITSTATUS( my_status ) != 0 ) {
1736 printf( "wait4 returned wrong exit status - 0x%02X \n", my_status );
1737 goto test_failed_exit;
1738 }
1739
1740 my_err = 0;
1741 goto test_passed_exit;
1742
1743test_failed_exit:
1744 my_err = -1;
1745
1746test_passed_exit:
1747 return( my_err );
1748}
1749
1750/* **************************************************************************************************************
1751 * Test mknod, sync system calls.
1752 * **************************************************************************************************************
1753 */
1754int mknod_sync_test( void * the_argp )
1755{
1756 int my_err;
b0d623f7
A
1757 char * my_pathp = NULL;
1758 kern_return_t my_kr;
2d21ac55
A
1759
1760 if ( g_skip_setuid_tests != 0 ) {
1761 printf("\t skipping this test \n");
1762 my_err = 0;
1763 goto test_passed_exit;
1764 }
1765
b0d623f7
A
1766 my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
1767 if(my_kr != KERN_SUCCESS){
1768 printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
1769 goto test_failed_exit;
1770 }
2d21ac55
A
1771
1772 *my_pathp = 0x00;
1773 strcat( my_pathp, "/dev/" );
1774
1775 /* get a unique name for our test file */
1776 my_err = create_random_name( my_pathp, 0 );
1777 if ( my_err != 0 ) {
1778 goto test_failed_exit;
1779 }
1780
1781 my_err = mknod( my_pathp, (S_IFCHR | S_IRWXU), 0 );
1782 if ( my_err == -1 ) {
1783 printf( "mknod failed with errno %d - %s \n", errno, strerror( errno ) );
1784 printf( "path \"%s\" \n", my_pathp );
1785 goto test_failed_exit;
1786 }
1787
1788 /* not really sure what to do with sync call test */
1789 sync( );
1790 my_err = 0;
1791 goto test_passed_exit;
1792
1793test_failed_exit:
1794 my_err = -1;
1795
1796test_passed_exit:
1797 if ( my_pathp != NULL ) {
b0d623f7
A
1798 remove( my_pathp );
1799 vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
2d21ac55
A
1800 }
1801 return( my_err );
1802}
1803
1804/* **************************************************************************************************************
1805 * Test chflags, fchflags system calls.
1806 * **************************************************************************************************************
1807 */
1808int chflags_fchflags_test( void * the_argp )
1809{
1810 int my_err;
1811 int my_fd = -1;
1812 u_int my_flags;
1813 char * my_pathp = NULL;
1814 struct stat my_sb;
b0d623f7 1815 kern_return_t my_kr;
2d21ac55 1816
b0d623f7
A
1817 my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
1818 if(my_kr != KERN_SUCCESS){
1819 printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
1820 goto test_failed_exit;
1821 }
2d21ac55
A
1822
1823 *my_pathp = 0x00;
1824 strcat( my_pathp, &g_target_path[0] );
1825 strcat( my_pathp, "/" );
1826
1827 /* create a test file */
1828 my_err = create_random_name( my_pathp, 1 );
1829 if ( my_err != 0 ) {
1830 goto test_failed_exit;
1831 }
1832
1833 /* make test file unchangable */
1834 my_err = stat( my_pathp, &my_sb );
1835 if ( my_err != 0 ) {
1836 printf( "stat call failed with error %d - \"%s\" \n", errno, strerror( errno) );
1837 goto test_failed_exit;
1838 }
1839
1840 my_flags = (my_sb.st_flags | UF_IMMUTABLE);
1841 my_err = chflags( my_pathp, my_flags );
1842 if ( my_err != 0 ) {
1843 printf( "chflags call failed with error %d - \"%s\" \n", errno, strerror( errno) );
1844 goto test_failed_exit;
1845 }
1846
1847 /* should fail with EPERM since we cannot change the file now */
1848 my_fd = open( my_pathp, O_RDWR, 0 );
1849 if ( my_fd == -1 && errno != EPERM ) {
1850 printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
1851 printf( "open failed with wrong error - should be EPERM \n" );
1852 goto test_failed_exit;
1853 }
1854
1855 /* this open should work OK */
1856 my_fd = open( my_pathp, O_RDONLY, 0 );
1857 if ( my_fd == -1 ) {
1858 printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
1859 goto test_failed_exit;
1860 }
1861
1862 my_err = stat( my_pathp, &my_sb );
1863 if ( my_err != 0 ) {
1864 printf( "stat call failed with error %d - \"%s\" \n", errno, strerror( errno) );
1865 goto test_failed_exit;
1866 }
1867
1868 my_flags = (my_sb.st_flags & ~UF_IMMUTABLE);
1869 my_err = fchflags( my_fd, my_flags );
1870 if ( my_err != 0 ) {
1871 printf( "chflags call failed with error %d - \"%s\" \n", errno, strerror( errno) );
1872 goto test_failed_exit;
1873 }
1874
1875 close( my_fd );
1876 my_fd = -1;
1877
1878 /* should now work */
1879 my_fd = open( my_pathp, O_RDWR, 0 );
1880 if ( my_fd == -1 ) {
1881 printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
1882 goto test_failed_exit;
1883 }
1884
1885 my_err = 0;
1886 goto test_passed_exit;
1887
1888test_failed_exit:
1889 my_err = -1;
1890
1891test_passed_exit:
1892 if ( my_fd != -1 )
1893 close( my_fd );
1894 if ( my_pathp != NULL ) {
b0d623f7
A
1895 remove( my_pathp );
1896 vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
2d21ac55
A
1897 }
1898 return( my_err );
1899}
1900
1901
1902/* **************************************************************************************************************
1903 * Test kill, vfork, execve system calls.
1904 * **************************************************************************************************************
1905 */
1906/* There are many new exec() situations to test now that 64-bit is in. These extra tests are in response to
1907 * rdar://4606399 and rdar://4607285. It should cover every permutation of the following variables.
1908 *
1909 * - Current Process "Bitness": 64 or 32
1910 * - exec()'ed process "bitness": 64 or 32
1911 * (if 64 bit, size of page zero:) (4GB or 4KB)
1912 * - Parent Process "Bitness": 64 or 32
1913
1914 * Test to make sure certain inheritance properties of fork()'ed children
1915 * are correctly set.
1916 * 1. 64 bit process forking() 64-bit child, child execing() 64-bit file (4GB pagezero)
1917 * 2. 64 bit process forking() 64-bit child, child execing() 64-bit file (4KB pagezero)
1918 * 3. 64 bit process forking() 64-bit child, child execing() 32-bit file
1919 * 4. 32 bit process forking() 32-bit child, child execing() 32-bit file
1920 * 5. 32 bit process forking() 32-bit child, child execing() 64 bit file (4GB pagezero)
1921 * 6. 32 bit process forking() 32-bit child, child execing() 64 bit file (4KB pagezero)
1922 *
1923 */
1924
1925
1926int execve_kill_vfork_test( void * the_argp )
1927{
1928 int my_err, my_status;
1929 pid_t my_pid, my_wait_pid;
1930 char * errmsg = NULL;
1931 char * argvs[2] = {"", NULL};
1932 int bits = get_bits(); /* Gets actual processor bit-ness. */
cf7d32b8 1933
2d21ac55
A
1934 if (bits != 32 && bits != 64) {
1935 printf("Determination of processor bit-ness failed, get_bits() returned %d.\n", get_bits());
1936 return(-1);
1937 }
1938
1939 if (get_architecture() == -1) {
1940 errmsg = "get_architecture() could not determine the CPU architecture.\n";
1941 goto test_failed_exit;
1942 }
cf7d32b8 1943
2d21ac55 1944 if (get_architecture() == INTEL) {
b0d623f7
A
1945 int ppc_fail_flag = 0;
1946 struct stat sb;
1947
1948 if (stat("/usr/libexec/oah/translate", &sb))
1949 ppc_fail_flag = 1;
1950
1951 if (bits == 64 && sizeof(long) == 8) {
2d21ac55
A
1952 /*
1953 * Running on x86_64 hardware and running in 64-bit mode.
1954 * Check cases 1, 2, 3 and fork a child to check 4, 5, 6.
1955 */
1956 errmsg = "execve failed: from x86_64 forking and exec()ing 64-bit x86_64 process w/ 4G pagezero.\n";
1957 argvs[0] = "sleep-x86_64-4G";
1958 if (do_execve_test("helpers/sleep-x86_64-4G", argvs, NULL, 1)) goto test_failed_exit;
1959
1960 errmsg = "execve failed: from x86_64 forking and exec()ing 64-bit x86_64 process w/ 4K Pagezero.\n";
1961 argvs[0] = "sleep-x86_64-4K";
1962 if (do_execve_test("helpers/sleep-x86_64-4K", argvs, NULL, 1)) goto test_failed_exit;
1963
1964 errmsg = "execve failed: from x64_64 forking and exec()ing 32-bit i386 process.\n";
1965 argvs[0] = "sleep-i386";
1966 if (do_execve_test("helpers/sleep-i386", argvs, NULL, 1)) goto test_failed_exit;
1967
1968 /* Fork off a helper process and load a 32-bit program in it to test 32->64 bit exec(). */
1969 errmsg = "execve failed to exec the helper process.\n";
1970 argvs[0] = "launch-i386";
1971 if (do_execve_test("helpers/launch-i386", argvs, NULL, 1) != 0) goto test_failed_exit;
1972
b0d623f7 1973 /* Test posix_spawn for i386, x86_64, and ppc (should succeed) */
2d21ac55 1974 errmsg = NULL;
2d21ac55
A
1975 if (do_spawn_test(CPU_TYPE_I386, 0))
1976 goto test_failed_exit;
1977 if (do_spawn_test(CPU_TYPE_X86_64, 0))
1978 goto test_failed_exit;
b0d623f7
A
1979 /*
1980 * Note: rosetta is no go in single-user mode
1981 */
1982 if (!g_is_single_user) {
1983 if (do_spawn_test(CPU_TYPE_POWERPC, ppc_fail_flag))
1984 goto test_failed_exit;
1985 }
2d21ac55
A
1986 }
1987 else if (bits == 64 && sizeof(long) == 4) {
1988 /*
1989 * Running on x86_64 hardware, but actually running in 32-bit mode.
1990 * Check cases 4, 5, 6 and fork a child to check 1, 2, 3.
1991 */
1992 errmsg = "execve failed: from i386 forking and exec()ing i386 process.\n";
1993 argvs[0] = "sleep-i386";
1994 if (do_execve_test("helpers/sleep-i386", argvs, NULL, 0)) goto test_failed_exit;
1995
1996 errmsg = "execve failed: from i386 forking and exec()ing x86_64 process w/ 4G pagezero.\n";
1997 argvs[0] = "sleep-x86_64-4G";
1998 if (do_execve_test("helpers/sleep-x86_64-4G", argvs, NULL, 0)) goto test_failed_exit;
1999
2000 errmsg = "execve failed: from i386 forking and exec()ing x86_64 process w/ 4K pagezero.\n";
2001 argvs[0] = "sleep-x86_64-4K";
2002 if (do_execve_test("helpers/sleep-x86_64-4K", argvs, NULL, 0)) goto test_failed_exit;
2003
2004 /* Fork off a helper process and load a 64-bit program in it to test 64->32 bit exec(). */
2005 errmsg = "execve failed to exec the helper process.\n";
2006 argvs[0] = "launch-x86_64";
2007 if (do_execve_test("helpers/launch-x86_64", argvs, NULL, 1) != 0) goto test_failed_exit;
2008
b0d623f7 2009 /* Test posix_spawn for i386, x86_64, and ppc (should succeed) */
2d21ac55 2010 errmsg = NULL;
2d21ac55
A
2011 if (do_spawn_test(CPU_TYPE_I386, 0))
2012 goto test_failed_exit;
2013 if (do_spawn_test(CPU_TYPE_X86_64, 0))
2014 goto test_failed_exit;
b0d623f7
A
2015 /*
2016 * Note: rosetta is no go in single-user mode
2017 */
2018 if (!g_is_single_user) {
2019 if (do_spawn_test(CPU_TYPE_POWERPC, ppc_fail_flag))
2020 goto test_failed_exit;
2021 }
2d21ac55
A
2022 }
2023 else if (bits == 32) {
2024 /* Running on i386 hardware. Check cases 4. */
2025 errmsg = "execve failed: from i386 forking and exec()ing 32-bit i386 process.\n";
2026 argvs[0] = "sleep-i386";
2027 if (do_execve_test("helpers/sleep-i386", argvs, NULL, 1)) goto test_failed_exit;
2028
2029 /* Test posix_spawn for x86_64 (should fail), i386, and ppc (should succeed) */
2030 errmsg = NULL;
2031 if (do_spawn_test(CPU_TYPE_X86_64, 1))
2032 goto test_failed_exit;
2033 if (do_spawn_test(CPU_TYPE_I386, 0))
2034 goto test_failed_exit;
b0d623f7
A
2035 /*
2036 * Note: rosetta is no go in single-user mode
2037 */
2038 if (!g_is_single_user) {
2039 if (do_spawn_test(CPU_TYPE_POWERPC, ppc_fail_flag))
2040 goto test_failed_exit;
2041 }
2d21ac55
A
2042 }
2043 }
2044 else if (get_architecture() == POWERPC) {
2045 if (bits == 64 && sizeof(long) == 8) {
2046 /*
2047 * Running on PPC64 hardware and running in 64-bit mode.
b0d623f7 2048 * No longer supported on SnowLeopard.
2d21ac55 2049 */
b0d623f7
A
2050 errmsg = "runnning ppc64 on snowleopard";
2051 goto test_failed_exit;
2d21ac55
A
2052 }
2053 else if (bits == 64 && sizeof(long) == 4) {
2054 /*
b0d623f7 2055 * Running as PPC on PPC64 hardware or under Rosetta on x86_64 hardware.
2d21ac55
A
2056 * Check cases 4, 5, 6 and fork a child to check 1, 2, 3.
2057 */
2058 errmsg = "execve failed: from ppc forking and exec()ing ppc process.\n";
2059 argvs[0] = "sleep-ppc32";
2060 if (do_execve_test("helpers/sleep-ppc32", argvs, NULL, 0)) goto test_failed_exit;
2061
b0d623f7 2062 /* Test posix_spawn for i386 and ppc */
2d21ac55 2063 errmsg = NULL;
b0d623f7 2064 if (do_spawn_test(CPU_TYPE_I386, (g_is_under_rosetta ? 0 : 1)))
2d21ac55
A
2065 goto test_failed_exit;
2066 if (do_spawn_test(CPU_TYPE_POWERPC, 0))
2067 goto test_failed_exit;
2068 }
2069 else if (bits == 32) {
2070 /* Running on ppc hardware. Check cases 4. */
2071 errmsg = "execve failed: from ppc forking and exec()ing 32 bit ppc process.\n";
2072 argvs[0] = "sleep-ppc32";
2073 if (do_execve_test("helpers/sleep-ppc32", argvs, NULL, 1)) goto test_failed_exit;
2074 /* Test posix_spawn for i386 (should fail) and ppc (should succeed) */
2075 errmsg = NULL;
2076 /* when under Rosetta, this process is CPU_TYPE_POWERPC, but the system should be able to run CPU_TYPE_I386 binaries */
2077 if (do_spawn_test(CPU_TYPE_I386, (g_is_under_rosetta ? 0 : 1)))
2078 goto test_failed_exit;
2079 if (do_spawn_test(CPU_TYPE_POWERPC, 0))
2080 goto test_failed_exit;
2081 }
2082 }
cf7d32b8
A
2083 else if(get_architecture() == ARM) {
2084 if (bits == 32) {
2085
2086 /* Running on arm hardware. Check cases 2. */
2087 errmsg = "execve failed: from arm forking and exec()ing 32-bit arm process.\n";
2088 argvs[0] = "sleep-arm";
2089 if (do_execve_test("helpers/sleep-arm", argvs, NULL, 1))
2090 goto test_failed_exit;
2091
2092 /* Test posix_spawn for arm (should succeed) */
2093 errmsg = NULL;
2094 if (do_spawn_test(CPU_TYPE_ARM, 0))
2095 goto test_failed_exit;
2096 }
2097 }
2d21ac55
A
2098 else {
2099 /* Just in case someone decides we need more architectures in the future */
2100 printf("get_architecture() returned unknown architecture");
2101 return(-1);
2102 }
2103
2104 return 0;
2105
2106test_failed_exit:
2107 if (errmsg)
2108 printf("%s", errmsg);
2109 return -1;
2110}
2111
2112
2113/* **************************************************************************************************************
2114 * Test getegid, getgid, getgroups, setegid, setgid, setgroups system calls.
2115 * **************************************************************************************************************
2116 */
2117int groups_test( void * the_argp )
2118{
cf7d32b8 2119#if !TARGET_OS_EMBEDDED
2d21ac55
A
2120 int my_err, i;
2121 int my_group_count, my_orig_group_count;
2122 gid_t my_real_gid;
2123 gid_t my_effective_gid;
2124 gid_t my_removed_gid;
2125 gid_t my_new_gid;
2126 gid_t my_groups[ NGROUPS_MAX ];
2127
2128 if ( g_skip_setuid_tests != 0 ) {
2129 printf("\t skipping this test \n");
2130 my_err = 0;
2131 goto test_passed_exit;
2132 }
2133
2134 my_real_gid = getgid( );
2135 my_effective_gid = getegid( );
2136
2137 /* start by getting list of groups the current user belongs to */
2138 my_orig_group_count = getgroups( NGROUPS_MAX, &my_groups[0] );
cf7d32b8 2139
2d21ac55
A
2140 if ( my_orig_group_count == -1 || my_orig_group_count < 1 ) {
2141 printf( "getgroups call failed. got errno %d - %s. \n", errno, strerror( errno ) );
2142 goto test_failed_exit;
2143 }
2144
2145 /* make sure real and effective gids are correct */
2146 for ( i = 0; i < my_orig_group_count; i++ ) {
2147 if ( my_groups[i] == my_real_gid )
2148 break;
2149 }
2150 if ( i >= my_orig_group_count ) {
2151 printf( "getgid or getgroups call failed. could not find real gid in list of groups. \n" );
2152 goto test_failed_exit;
2153 }
2154 for ( i = 0; i < my_orig_group_count; i++ ) {
2155 if ( my_groups[i] == my_effective_gid )
2156 break;
2157 }
2158 if ( i >= my_orig_group_count ) {
2159 printf( "getegid or getgroups call failed. could not find effective gid in list of groups. \n" );
2160 goto test_failed_exit;
2161 }
2162
2163 /* remove the last group */
2164 my_removed_gid = my_groups[ (my_orig_group_count - 1) ];
2165 my_err = setgroups( (my_orig_group_count - 1), &my_groups[0] );
2166 if ( my_err == -1 ) {
2167 printf( "setgroups call failed. got errno %d - %s. \n", errno, strerror( errno ) );
2168 goto test_failed_exit;
2169 }
2170
2171 my_group_count = getgroups( NGROUPS_MAX, &my_groups[0] );
cf7d32b8 2172
2d21ac55
A
2173 if ( my_group_count == -1 || my_group_count < 1 ) {
2174 printf( "getgroups call failed. got errno %d - %s. \n", errno, strerror( errno ) );
2175 goto test_failed_exit;
2176 }
2177
2178 /* make sure setgroups dropped one */
2179 if ( my_orig_group_count <= my_group_count ) {
2180 printf( "setgroups call failed. current group count is too high. \n" );
2181 goto test_failed_exit;
2182 }
2183
2184 /* now put removed gid back */
2185 my_groups[ (my_orig_group_count - 1) ] = my_removed_gid;
2186 my_err = setgroups( my_orig_group_count, &my_groups[0] );
2187 if ( my_err == -1 ) {
2188 printf( "setgroups call failed. got errno %d - %s. \n", errno, strerror( errno ) );
2189 goto test_failed_exit;
2190 }
2191
2192 /* find a group to change real and effective gid to then do it */
2193 my_new_gid = -1;
2194 for ( i = 0; i < my_orig_group_count; i++ ) {
2195 if ( my_groups[i] == my_effective_gid || my_groups[i] == my_real_gid )
2196 continue;
2197 my_new_gid = my_groups[i];
2198 }
2199
2200 if ( my_new_gid == -1 ) {
2201 printf( "could not find a gid to switch to. \n" );
2202 goto test_failed_exit;
2203 }
2204
2205 /* test setegid */
2206 my_err = setegid( my_new_gid );
2207 if ( my_err == -1 ) {
2208 printf( "setegid call failed. got errno %d - %s. \n", errno, strerror( errno ) );
2209 goto test_failed_exit;
2210 }
2211 /* verify it changed */
2212 if ( getegid( ) != my_new_gid ) {
2213 printf( "setegid failed to change the effective gid. \n" );
2214 goto test_failed_exit;
2215 }
2216 /* change it back to original value */
2217 my_err = setegid( my_effective_gid );
2218 if ( my_err == -1 ) {
2219 printf( "setegid call failed. got errno %d - %s. \n", errno, strerror( errno ) );
2220 goto test_failed_exit;
2221 }
2222
2223 /* test setgid */
2224 my_err = setgid( my_new_gid );
2225 if ( my_err == -1 ) {
2226 printf( "setgid call failed. got errno %d - %s. \n", errno, strerror( errno ) );
2227 goto test_failed_exit;
2228 }
2229 /* verify it changed */
2230 if ( getgid( ) != my_new_gid ) {
2231 printf( "setgid failed to change the real gid. \n" );
2232 goto test_failed_exit;
2233 }
2234 /* change it back to original value */
2235 my_err = setgid( my_real_gid );
2236 if ( my_err == -1 ) {
2237 printf( "setegid call failed. got errno %d - %s. \n", errno, strerror( errno ) );
2238 goto test_failed_exit;
2239 }
2240
2241 my_err = 0;
2242 goto test_passed_exit;
2243
2244test_failed_exit:
2245 my_err = -1;
2246
2247test_passed_exit:
2248 return( my_err );
cf7d32b8
A
2249#else
2250 printf( "\t--> Test not designed for EMBEDDED TARGET\n" );
2251 return 0;
2252#endif
2d21ac55
A
2253}
2254
2255
2256/* **************************************************************************************************************
2257 * Test dup, dup2, getdtablesize system calls.
2258 * **************************************************************************************************************
2259 */
2260int dup_test( void * the_argp )
2261{
2262 int my_err;
2263 int my_fd = -1;
2264 int my_newfd = -1;
2265 int my_table_size, my_loop_counter = 0;
2266 char * my_pathp = NULL;
2267 ssize_t my_count;
2268 char my_buffer[64];
b0d623f7 2269 kern_return_t my_kr;
2d21ac55 2270
b0d623f7
A
2271 my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
2272 if(my_kr != KERN_SUCCESS){
2273 printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
2274 goto test_failed_exit;
2275 }
2d21ac55
A
2276
2277 *my_pathp = 0x00;
2278 strcat( my_pathp, &g_target_path[0] );
2279 strcat( my_pathp, "/" );
2280
2281 /* create a test file */
2282 my_err = create_random_name( my_pathp, 1 );
2283 if ( my_err != 0 ) {
2284 goto test_failed_exit;
2285 }
2286
2287 /* test dup, dup2, getdtablesize */
2288 my_table_size = getdtablesize( );
2289 if ( my_table_size < 20 ) {
2290 printf( "getdtablesize should return at least 20, returned %d \n", my_table_size );
2291 goto test_failed_exit;
2292 }
2293
2294 my_fd = open( my_pathp, O_RDWR, 0 );
2295 if ( my_fd == -1 ) {
2296 printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
2297 goto test_failed_exit;
2298 }
2299
2300 my_newfd = dup( my_fd );
2301 if ( my_newfd == -1 ) {
2302 printf( "dup call failed with error %d - \"%s\" \n", errno, strerror( errno) );
2303 goto test_failed_exit;
2304 }
2305
2306redo:
2307 /* now write somne data to the orginal and new fd */
2308 /* make sure test file is empty */
2309 my_err = ftruncate( my_fd, 0 );
2310 if ( my_err == -1 ) {
2311 printf( "ftruncate call failed with error %d - \"%s\" \n", errno, strerror( errno) );
2312 goto test_failed_exit;
2313 }
2314
2315 lseek( my_fd, 0, SEEK_SET );
2316 my_count = write( my_fd, "aa", 2 );
2317 if ( my_count == -1 ) {
2318 printf( "write call failed. got errno %d - %s. \n", errno, strerror( errno ) );
2319 goto test_failed_exit;
2320 }
2321
2322 my_count = write( my_newfd, "xx", 2 );
2323 if ( my_count == -1 ) {
2324 printf( "write call failed. got errno %d - %s. \n", errno, strerror( errno ) );
2325 goto test_failed_exit;
2326 }
2327
2328 /* now read it back and make sure data is correct */
2329 lseek( my_fd, 0, SEEK_SET );
2330 my_count = read( my_fd, &my_buffer[0], sizeof(my_buffer) );
2331 if ( my_count == -1 ) {
2332 printf( "read call failed with error %d - \"%s\" \n", errno, strerror( errno) );
2333 goto test_failed_exit;
2334 }
2335 if ( my_buffer[0] != 'a' || my_buffer[1] != 'a' || my_buffer[2] != 'x' || my_buffer[3] != 'x' ) {
2336 printf( "wrong data in test file. \n" );
2337 goto test_failed_exit;
2338 }
2339
2340 bzero( &my_buffer[0], sizeof(my_buffer) );
2341 lseek( my_newfd, 0, SEEK_SET );
2342 my_count = read( my_newfd, &my_buffer[0], sizeof(my_buffer) );
2343 if ( my_count == -1 ) {
2344 printf( "read call failed with error %d - \"%s\" \n", errno, strerror( errno) );
2345 goto test_failed_exit;
2346 }
2347 if ( my_buffer[0] != 'a' || my_buffer[1] != 'a' || my_buffer[2] != 'x' || my_buffer[3] != 'x' ) {
2348 printf( "wrong data in test file. \n" );
2349 goto test_failed_exit;
2350 }
2351
2352 /* we do the above tests twice - once for dup and once for dup2 */
2353 if ( my_loop_counter < 1 ) {
2354 my_loop_counter++;
2355 close( my_newfd );
2356
2357 my_err = dup2( my_fd, my_newfd );
2358 if ( my_err == -1 ) {
2359 printf( "dup2 call failed with error %d - \"%s\" \n", errno, strerror( errno) );
2360 goto test_failed_exit;
2361 }
2362
2363 goto redo;
2364 }
2365
2366 my_err = 0;
2367 goto test_passed_exit;
2368
2369test_failed_exit:
2370 my_err = -1;
2371
2372test_passed_exit:
2373 if ( my_fd != -1 )
2374 close( my_fd );
2375 if ( my_newfd != -1 )
2376 close( my_newfd );
2377 if ( my_pathp != NULL ) {
b0d623f7
A
2378 remove( my_pathp );
2379 vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
2d21ac55
A
2380 }
2381 return( my_err );
2382}
2383
2384
2385/* **************************************************************************************************************
b0d623f7 2386 * Test getrusage system call.
2d21ac55
A
2387 * **************************************************************************************************************
2388 */
b0d623f7 2389int getrusage_test( void * the_argp )
2d21ac55
A
2390{
2391 int my_err;
2d21ac55
A
2392 struct rusage my_rusage;
2393
2d21ac55
A
2394 my_err = getrusage( RUSAGE_SELF, &my_rusage );
2395 if ( my_err == -1 ) {
2396 printf( "getrusage failed with error %d - \"%s\" \n", errno, strerror( errno) );
2397 goto test_failed_exit;
2398 }
2399
2400 /* do a sanity check on the getrusage results */
2401 if ( my_rusage.ru_msgrcv > 1000 || my_rusage.ru_msgrcv < 0 ) {
2402 printf( "getrusage seems to report wrong data - ru_msgrcv looks odd. \n" );
2403 goto test_failed_exit;
2404 }
2405 if ( my_rusage.ru_nsignals > 1000 || my_rusage.ru_nsignals < 0 ) {
2406 printf( "getrusage seems to report wrong data - ru_nsignals looks odd. \n" );
2407 goto test_failed_exit;
2408 }
2409
2d21ac55
A
2410 my_err = 0;
2411 goto test_passed_exit;
2412
2413test_failed_exit:
2414 my_err = -1;
2415
2416test_passed_exit:
2d21ac55
A
2417 return( my_err );
2418}
2419
2420/* **************************************************************************************************************
2421 * Test getitimer, setitimer, sigaction, sigpending, sigprocmask, sigsuspend, sigwait system calls.
2422 * **************************************************************************************************************
2423 */
2424
2425int alarm_global = 0;
2426void test_alarm_handler( int the_arg );
2427void test_alarm_handler( int the_arg )
2428{
2429 alarm_global = 4;
2430 //printf( "test_alarm_handler - got here \n" );
2431 if ( the_arg == 0 ) {
2432 }
2433 return;
2434}
2435
2436void test_signal_handler( int the_arg );
2437void test_signal_handler( int the_arg )
2438{
2439 //printf( "test_signal_handler - got here \n" );
2440 if ( the_arg == 0 ) {
2441 }
2442 return;
2443}
2444
2445int signals_test( void * the_argp )
2446{
2447 int my_err, my_status;
2448 int my_fd = -1;
2449 char * my_pathp = NULL;
2450 pid_t my_pid, my_wait_pid;
b0d623f7 2451 kern_return_t my_kr;
2d21ac55 2452
b0d623f7
A
2453 my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
2454 if(my_kr != KERN_SUCCESS){
2455 printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
2456 goto test_failed_exit;
2457 }
2d21ac55
A
2458
2459 *my_pathp = 0x00;
2460 strcat( my_pathp, &g_target_path[0] );
2461 strcat( my_pathp, "/" );
2462
2463 /* create a test file */
2464 my_err = create_random_name( my_pathp, 1 );
2465 if ( my_err != 0 ) {
2466 goto test_failed_exit;
2467 }
2468
2469 /*
2470 * spin off a child process that we will use for signal related testing.
2471 */
2472 my_pid = fork( );
2473 if ( my_pid == -1 ) {
2474 printf( "fork failed with errno %d - %s \n", errno, strerror( errno ) );
2475 goto test_failed_exit;
2476 }
2477 if ( my_pid == 0 ) {
2478 /*
2479 * child process - test signal related system calls.
2480 */
2481 //int my_counter;
2482 int my_signal;
2483 sigset_t my_sigset;
2484 struct sigaction my_sigaction;
2485#ifdef MAC_OS_X_VERSION_10_5
2486#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
2487 /* If this is Leopard. To allow compiling for Inca x86_64 this definition cannot
2488 * be included. But it is needed to compile on Leopard.
2489 */
2490 struct __darwin_sigaltstack my_sigaltstack;
2491#endif
2492#else
2493 struct sigaltstack my_sigaltstack;
2494#endif
2495 struct itimerval my_timer;
2496
2497
2498 /* test getting the current signal stack context */
2499 my_err = sigaltstack( NULL, &my_sigaltstack );
2500 if ( my_err == -1 ) {
2501 printf( "sigaction failed with errno %d - %s \n", errno, strerror( errno ) );
2502 exit( -1 );
2503 }
2504 if ( (my_sigaltstack.ss_flags & SS_DISABLE) == 0 ) {
2505 printf( "sigaction must have failed - SS_DISABLE is cleared \n" );
2506 exit( -1 );
2507 }
2508
2509 /* set up to catch SIGUSR1 */
2510 my_sigaction.sa_handler = test_signal_handler;
2511 my_sigaction.sa_flags = SA_RESTART;
2512 my_sigaction.sa_mask = 0;
2513
2514 my_err = sigaction( SIGUSR1, &my_sigaction, NULL );
2515 if ( my_err == -1 ) {
2516 printf( "sigaction failed with errno %d - %s \n", errno, strerror( errno ) );
2517 exit( -1 );
2518 }
2519
2520 /* now suspend until signal SIGUSR1 is sent */
2521 sigemptyset( &my_sigset );
2522 my_err = sigsuspend( &my_sigset );
2523 if ( my_err == -1 ) {
2524 if ( errno != EINTR ) {
2525 printf( "sigsuspend should have returned with errno EINTR \n" );
2526 exit( -1 );
2527 }
2528 }
2529
2530 /* block SIGUSR1 */
2531 sigemptyset( &my_sigset );
2532 sigaddset( &my_sigset, SIGUSR1 );
2533 if ( sigismember( &my_sigset, SIGUSR1 ) == 0 ) {
2534 printf( "sigaddset call failed to add SIGUSR1 to signal set \n" );
2535 exit( -1 );
2536 }
2537 my_err = sigprocmask( SIG_BLOCK, &my_sigset, NULL );
2538 if ( my_err == -1 ) {
2539 printf( "sigprocmask failed with errno %d - %s \n", errno, strerror( errno ) );
2540 exit( -1 );
2541 }
2542
2543 /* make sure we are blocking SIGUSR1 */
2544 sigemptyset( &my_sigset );
2545 my_err = sigprocmask( 0, NULL, &my_sigset );
2546 if ( my_err == -1 ) {
2547 printf( "sigprocmask failed with errno %d - %s \n", errno, strerror( errno ) );
2548 exit( -1 );
2549 }
2550 if ( sigismember( &my_sigset, SIGUSR1 ) == 0 ) {
2551 printf( "sigaddset call failed to add SIGUSR1 to signal set \n" );
2552 exit( -1 );
2553 }
2554
2555 /* our parent will send a 2nd SIGUSR1 signal which we should now see getting
2556 * blocked.
2557 */
2558 sigemptyset( &my_sigset );
2559 sigaddset( &my_sigset, SIGUSR1 );
2560 my_err = sigwait( &my_sigset, &my_signal );
2561 if ( my_err == -1 ) {
2562 printf( "sigwait failed with errno %d - %s \n", errno, strerror( errno ) );
2563 exit( -1 );
2564 }
2565 //printf( "%s - %d - signal 0x%02X %d \n", __FUNCTION__, __LINE__, my_signal, my_signal );
2566 if ( my_signal != SIGUSR1 ) {
2567 printf( "sigwait failed to catch a pending SIGUSR1 signal. \n" );
2568 exit( -1 );
2569 }
2570
2571 /* now unblock SIGUSR1 */
2572 sigfillset( &my_sigset );
2573 sigdelset( &my_sigset, SIGUSR1 );
2574 my_err = sigprocmask( SIG_UNBLOCK, &my_sigset, NULL );
2575 if ( my_err == -1 ) {
2576 printf( "sigprocmask failed with errno %d - %s \n", errno, strerror( errno ) );
2577 exit( -1 );
2578 }
2579 if ( sigismember( &my_sigset, SIGUSR1 ) != 0 ) {
2580 printf( "sigprocmask call failed to unblock SIGUSR1 \n" );
2581 exit( -1 );
2582 }
2583
2584 /* test get / setitimer */
2585 timerclear( &my_timer.it_interval );
2586 timerclear( &my_timer.it_value );
2587 my_err = setitimer( ITIMER_VIRTUAL, &my_timer, NULL );
2588 if ( my_err == -1 ) {
2589 printf( "setitimer - ITIMER_VIRTUAL - failed with errno %d - %s \n", errno, strerror( errno ) );
2590 exit( -1 );
2591 }
2592 my_err = setitimer( ITIMER_PROF, &my_timer, NULL );
2593 if ( my_err == -1 ) {
2594 printf( "setitimer - ITIMER_PROF - failed with errno %d - %s \n", errno, strerror( errno ) );
2595 exit( -1 );
2596 }
2597
2598 /* set up to catch SIGALRM */
2599 alarm_global = 0;
2600 my_sigaction.sa_handler = test_alarm_handler;
2601 my_sigaction.sa_flags = SA_RESTART;
2602 my_sigaction.sa_mask = 0;
2603
2604 my_err = sigaction( SIGALRM, &my_sigaction, NULL );
2605 if ( my_err == -1 ) {
2606 printf( "sigaction - SIGALRM - failed with errno %d - %s \n", errno, strerror( errno ) );
2607 exit( -1 );
2608 }
2609
2610 /* set timer for half a second */
2611 my_timer.it_value.tv_usec = (1000000 / 2);
2612 my_err = setitimer( ITIMER_REAL, &my_timer, NULL );
2613 if ( my_err == -1 ) {
2614 printf( "setitimer - ITIMER_REAL - failed with errno %d - %s \n", errno, strerror( errno ) );
2615 exit( -1 );
2616 }
2617
2618 /* now suspend until signal SIGALRM is sent */
2619 sigfillset( &my_sigset );
2620 sigdelset( &my_sigset, SIGALRM );
2621 my_err = sigsuspend( &my_sigset );
2622 if ( my_err == -1 ) {
2623 if ( errno != EINTR ) {
2624 printf( "sigsuspend should have returned with errno EINTR \n" );
2625 exit( -1 );
2626 }
2627 }
2628 if ( alarm_global != 4 ) {
2629 printf( "setitimer test failed - did not catch SIGALRM \n" );
2630 exit( -1 );
2631 }
2632
2633 /* make sure ITIMER_REAL is now clear */
2634 my_timer.it_value.tv_sec = 44;
2635 my_timer.it_value.tv_usec = 44;
2636 my_err = getitimer( ITIMER_REAL, &my_timer );
2637 if ( my_err == -1 ) {
2638 printf( "getitimer - ITIMER_REAL - failed with errno %d - %s \n", errno, strerror( errno ) );
2639 exit( -1 );
2640 }
2641 if ( timerisset( &my_timer.it_value ) || timerisset( &my_timer.it_interval ) ) {
2642 printf( "ITIMER_REAL is set, but should not be \n" );
2643 exit( -1 );
2644 }
2645
2646 exit(0);
2647 }
2648
2649 /*
2650 * parent process - let child set up to suspend then signal it with SIGUSR1
2651 */
2652 sleep( 1 );
2653 my_err = kill( my_pid, SIGUSR1 );
2654 if ( my_err == -1 ) {
2655 printf( "kill call failed with error %d - \"%s\" \n", errno, strerror( errno) );
2656 goto test_failed_exit;
2657 }
2658
2659 /* send 2nd signal to suspended child - which should be blocking SIGUSR1 signals */
2660 sleep( 1 );
2661 my_err = kill( my_pid, SIGUSR1 );
2662 if ( my_err == -1 ) {
2663 printf( "kill call failed with error %d - \"%s\" \n", errno, strerror( errno) );
2664 goto test_failed_exit;
2665 }
2666
2667 /* wait for child to exit */
2668 my_wait_pid = wait4( my_pid, &my_status, 0, NULL );
2669 if ( my_wait_pid == -1 ) {
2670 printf( "wait4 failed with errno %d - %s \n", errno, strerror( errno ) );
2671 goto test_failed_exit;
2672 }
2673
2674 if ( WIFEXITED( my_status ) && WEXITSTATUS( my_status ) != 0 ) {
2675 goto test_failed_exit;
2676 }
2677
2678 my_err = 0;
2679 goto test_passed_exit;
2680
2681test_failed_exit:
2682 my_err = -1;
2683
2684test_passed_exit:
2685 if ( my_fd != -1 )
2686 close( my_fd );
2687 if ( my_pathp != NULL ) {
b0d623f7
A
2688 remove( my_pathp );
2689 vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
2d21ac55
A
2690 }
2691 return( my_err );
2692}
2693
2694/* **************************************************************************************************************
2695 * Test getlogin, setlogin system calls.
2696 * **************************************************************************************************************
2697 */
2698int getlogin_setlogin_test( void * the_argp )
2699{
2700 int my_err, my_status;
2701 pid_t my_pid, my_wait_pid;
b0d623f7 2702 kern_return_t my_kr;
2d21ac55
A
2703
2704 if ( g_skip_setuid_tests != 0 ) {
2705 printf("\t skipping this test \n");
2706 my_err = 0;
2707 goto test_passed_exit;
2708 }
2709
2710 /*
2711 * spin off a child process that we will use for testing.
2712 */
2713 my_pid = fork( );
2714 if ( my_pid == -1 ) {
2715 printf( "fork failed with errno %d - %s \n", errno, strerror( errno ) );
2716 goto test_failed_exit;
2717 }
2718 if ( my_pid == 0 ) {
2719 /*
2720 * child process - do getlogin and setlogin testing.
2721 */
b0d623f7
A
2722 char * my_namep = NULL;
2723 int my_len;
2d21ac55 2724 char * my_new_namep = NULL;
b0d623f7 2725
2d21ac55
A
2726 my_namep = getlogin( );
2727 if ( my_namep == NULL ) {
2728 printf( "getlogin returned NULL name pointer \n" );
2729 my_err = -1;
2730 goto exit_child;
2731 }
b0d623f7 2732
2d21ac55
A
2733 my_len = strlen( my_namep ) + 4;
2734
b0d623f7
A
2735 my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_new_namep, my_len, VM_FLAGS_ANYWHERE);
2736 if(my_kr != KERN_SUCCESS){
2737 printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
2738 my_err = -1;
2d21ac55 2739 goto exit_child;
b0d623f7
A
2740 }
2741
2d21ac55 2742 bzero( (void *)my_new_namep, my_len );
b0d623f7 2743
2d21ac55
A
2744 strcat( my_new_namep, my_namep );
2745 strcat( my_new_namep, "2" );
2746
b0d623f7 2747
2d21ac55
A
2748 /* set new name */
2749 my_err = setlogin( my_new_namep );
2750 if ( my_err == -1 ) {
b0d623f7 2751 printf( "When setting new login name, setlogin failed with error %d - \"%s\" \n", errno, strerror( errno) );
2d21ac55
A
2752 my_err = -1;
2753 goto exit_child;
2754 }
2755
2756 /* make sure we set new name */
2757 my_namep = getlogin( );
2758 if ( my_namep == NULL ) {
2759 printf( "getlogin returned NULL name pointer \n" );
2760 my_err = -1;
2761 goto exit_child;
2762 }
b0d623f7 2763
2d21ac55
A
2764 if ( memcmp( my_namep, my_new_namep, strlen( my_new_namep ) ) != 0 ) {
2765 printf( "setlogin failed to set the new name \n" );
2766 my_err = -1;
2767 goto exit_child;
2768 }
b0d623f7
A
2769
2770 /* reset to original name */
2771 my_len = strlen ( my_namep );
2772 my_namep[ my_len - 1 ] = '\0';
2773
2774 my_err = setlogin( my_namep );
2775 if ( my_err == -1 ) {
2776 printf( "When resetting login name, setlogin failed with error %d - \"%s\" \n", errno, strerror( errno) );
2777 my_err = -1;
2778 goto exit_child;
2779 }
2780
2d21ac55
A
2781
2782 my_err = 0;
2783exit_child:
2784 if ( my_new_namep != NULL ) {
b0d623f7 2785 vm_deallocate(mach_task_self(), (vm_address_t)my_new_namep, my_len);
2d21ac55
A
2786 }
2787 exit( my_err );
2788 }
2789
2790 /* parent process -
2791 * wait for child to exit
2792 */
2793 my_wait_pid = wait4( my_pid, &my_status, 0, NULL );
2794 if ( my_wait_pid == -1 ) {
2795 printf( "wait4 failed with errno %d - %s \n", errno, strerror( errno ) );
2796 goto test_failed_exit;
2797 }
2798
2799 if ( WIFEXITED( my_status ) && WEXITSTATUS( my_status ) != 0 ) {
2800 goto test_failed_exit;
2801 }
2802 my_err = 0;
2803 goto test_passed_exit;
2804
2805test_failed_exit:
2806 my_err = -1;
2807
2808test_passed_exit:
2809 return( my_err );
2810}
2811
2812/* **************************************************************************************************************
2813 * Test acct system call.
2814 * **************************************************************************************************************
2815 */
2816int acct_test( void * the_argp )
2817{
b0d623f7
A
2818 int my_err, my_status;
2819 int my_fd = -1;
2820 char * my_pathp = NULL;
2d21ac55 2821 struct acct * my_acctp;
b0d623f7
A
2822 pid_t my_pid, my_wait_pid;
2823 ssize_t my_count;
2824 char my_buffer[ (sizeof(struct acct) + 32) ];
2825 kern_return_t my_kr;
2d21ac55
A
2826
2827 if ( g_skip_setuid_tests != 0 ) {
2828 printf("\t skipping this test \n");
2829 my_err = 0;
2830 goto test_passed_exit;
2831 }
2832
b0d623f7
A
2833 my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
2834 if(my_kr != KERN_SUCCESS){
2835 printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
2836 goto test_failed_exit;
2837 }
2838
2d21ac55
A
2839 *my_pathp = 0x00;
2840 strcat( my_pathp, &g_target_path[0] );
2841 strcat( my_pathp, "/" );
2842
2843 /* create a test file */
2844 my_err = create_random_name( my_pathp, 1 );
2845 if ( my_err != 0 ) {
2846 goto test_failed_exit;
2847 }
2848
2849 /* enable process accounting */
2850 my_err = acct( my_pathp );
2851 if ( my_err == -1 ) {
2852 printf( "acct failed with error %d - \"%s\" \n", errno, strerror( errno) );
2853 goto test_failed_exit;
2854 }
2855
2856 /*
2857 * spin off a child process that we will use for testing.
2858 */
2859 my_pid = fork( );
2860 if ( my_pid == -1 ) {
2861 printf( "fork failed with errno %d - %s \n", errno, strerror( errno ) );
2862 goto test_failed_exit;
2863 }
2864 if ( my_pid == 0 ) {
b0d623f7
A
2865 char *argv[2]; /* supply valid argv array to execv() */
2866 argv[0] = "/usr/bin/true";
2867 argv[1] = 0;
2868
2d21ac55
A
2869 /*
2870 * child process - do a little work then exit.
2871 */
b0d623f7 2872 my_err = execv( argv[0], argv);
2d21ac55
A
2873 exit( 0 );
2874 }
2875
2876 /* parent process -
2877 * wait for child to exit
2878 */
2879 my_wait_pid = wait4( my_pid, &my_status, 0, NULL );
2880 if ( my_wait_pid == -1 ) {
2881 printf( "wait4 failed with errno %d - %s \n", errno, strerror( errno ) );
2882 goto test_failed_exit;
2883 }
2884
2885 if ( WIFEXITED( my_status ) && WEXITSTATUS( my_status ) != 0 ) {
b0d623f7 2886 printf("unexpected child exit status for accounting test load: %d\n", WEXITSTATUS( my_status));
2d21ac55
A
2887 goto test_failed_exit;
2888 }
2889
b0d623f7 2890 /* disable process accounting */
2d21ac55
A
2891 my_err = acct( NULL );
2892 if ( my_err == -1 ) {
2893 printf( "acct failed with error %d - \"%s\" \n", errno, strerror( errno) );
2894 goto test_failed_exit;
2895 }
2896
2897 /* now verify that there is accounting info in the log file */
2898 my_fd = open( my_pathp, O_RDONLY, 0 );
2899 if ( my_fd == -1 ) {
2900 printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
2901 goto test_failed_exit;
2902 }
2903
2904 lseek( my_fd, 0, SEEK_SET );
2905 bzero( (void *)&my_buffer[0], sizeof(my_buffer) );
2906 my_count = read( my_fd, &my_buffer[0], sizeof(struct acct) );
2907 if ( my_count == -1 ) {
2908 printf( "read call failed with error %d - \"%s\" \n", errno, strerror( errno) );
2909 goto test_failed_exit;
2910 }
2911
2912 my_acctp = (struct acct *) &my_buffer[0];
b0d623f7 2913
2d21ac55
A
2914 /* first letters in ac_comm should match the name of the executable */
2915 if ( getuid( ) != my_acctp->ac_uid || getgid( ) != my_acctp->ac_gid ||
2916 my_acctp->ac_comm[0] != 't' || my_acctp->ac_comm[1] != 'r' ) {
2917 if (g_is_under_rosetta) {
2918 // on x86 systems, data written by kernel to accounting info file is little endian;
2919 // but Rosetta processes expects it to be big endian; so swap the uid for our test
2920 if ( getuid( ) != OSSwapInt32(my_acctp->ac_uid) ||
2921 getgid( ) != OSSwapInt32(my_acctp->ac_gid) ||
2922 my_acctp->ac_comm[0] != 't' ||
2923 my_acctp->ac_comm[1] != 'r' ) {
b0d623f7
A
2924 printf( "accounting data does not look correct under Rosetta:\n" );
2925 printf( "------------------------\n" );
2926 printf( "my_acctp->ac_uid = %lu (should be: %lu)\n",
2927 (unsigned long) OSSwapInt32( my_acctp->ac_uid ), (unsigned long) getuid() );
2928 printf( "my_acctp->ac_gid = %lu (should be: %lu)\n",
2929 (unsigned long) OSSwapInt32( my_acctp->ac_gid ), (unsigned long) getgid() );
2930
2931 print_acct_debug_strings(my_acctp->ac_comm);
2d21ac55
A
2932 }
2933 else {
2934 // is cool under Rosetta
2935 my_err = 0;
2936 goto test_passed_exit;
2937 }
2938 }
2939 else {
b0d623f7
A
2940 printf( "accounting data does not look correct:\n" );
2941 printf( "------------------------\n" );
2942 printf( "my_acctp->ac_uid = %lu (should be: %lu)\n", (unsigned long) my_acctp->ac_uid, (unsigned long) getuid() );
2943 printf( "my_acctp->ac_gid = %lu (should be: %lu)\n", (unsigned long) my_acctp->ac_gid, (unsigned long) getgid() );
2944
2945 print_acct_debug_strings(my_acctp->ac_comm);
2d21ac55 2946 }
b0d623f7 2947
2d21ac55
A
2948 goto test_failed_exit;
2949 }
2950 my_err = 0;
2951 goto test_passed_exit;
2952
2953test_failed_exit:
2954 my_err = -1;
2955
2956test_passed_exit:
2957 if ( my_fd != -1 )
2958 close( my_fd );
2959 if ( my_pathp != NULL ) {
b0d623f7
A
2960 remove( my_pathp );
2961 vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
2d21ac55
A
2962 }
2963 return( my_err );
2964}
2965
b0d623f7
A
2966void print_acct_debug_strings( char * my_ac_comm )
2967{
2968 char my_cmd_str[11]; /* sizeof(acct_cmd) + 1 for '\0' if acct_cmd is bogus */
2969 char my_hex_str[128];
2970 int i;
2971
2972 my_hex_str[0] = '\0';
2973 for(i = 0; i < 10; i++)
2974 {
2975 sprintf( my_hex_str, "%s \'0x%x\' ", my_hex_str, my_ac_comm[i]);
2976 }
2977
2978 memccpy(my_cmd_str, my_ac_comm, '\0', 10);
2979 my_cmd_str[10] = '\0'; /* In case ac_comm was bogus */
2980
2981
2982 printf( "my_acctp->ac_comm = \"%s\" (should begin with: \"tr\")\n", my_cmd_str);
2983 printf( "my_acctp->ac_comm = \"%s\"\n", my_hex_str);
2984 printf( "------------------------\n" );
2985}
2986
2d21ac55
A
2987
2988/* **************************************************************************************************************
2989 * Test ioctl system calls.
2990 * **************************************************************************************************************
2991 */
2992int ioctl_test( void * the_argp )
2993{
2994 int my_err, my_result;
2995 int my_fd = -1;
2996 struct statfs * my_infop;
2997 char * my_ptr;
2998 int my_blksize;
2999 long long my_block_count;
3000 char my_name[ 128 ];
3001
3002 my_result = getmntinfo( &my_infop, MNT_NOWAIT );
3003 if ( my_result < 1 ) {
3004 printf( "getmntinfo failed with error %d - \"%s\" \n", errno, strerror( errno) );
3005 goto test_failed_exit;
3006 }
3007
3008 /* make this a raw device */
3009 strcpy( &my_name[0], &my_infop->f_mntfromname[0] );
3010 if ( (my_ptr = strrchr( &my_name[0], '/' )) != 0 ) {
3011 if ( my_ptr[1] != 'r' ) {
3012 my_ptr[ strlen( my_ptr ) ] = 0x00;
3013 memmove( &my_ptr[2], &my_ptr[1], (strlen( &my_ptr[1] ) + 1) );
3014 my_ptr[1] = 'r';
3015 }
3016 }
3017
3018 my_fd = open(&my_name[0], O_RDONLY );
3019 if ( my_fd == -1 ) {
3020 printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
3021 goto test_failed_exit;
3022 }
3023
3024 /* obtain the size of the media (in blocks) */
3025 my_err = ioctl( my_fd, DKIOCGETBLOCKCOUNT, &my_block_count );
3026 if ( my_err == -1 ) {
3027 printf( "ioctl DKIOCGETBLOCKCOUNT failed with error %d - \"%s\" \n", errno, strerror( errno) );
3028 goto test_failed_exit;
3029 }
3030
3031 /* obtain the block size of the media */
3032 my_err = ioctl( my_fd, DKIOCGETBLOCKSIZE, &my_blksize );
3033 if ( my_err == -1 ) {
3034 printf( "ioctl DKIOCGETBLOCKSIZE failed with error %d - \"%s\" \n", errno, strerror( errno) );
3035 goto test_failed_exit;
3036 }
3037 //printf( "my_block_count %qd my_blksize %d \n", my_block_count, my_blksize );
3038
3039 /* make sure the returned data looks somewhat valid */
3040 if ( my_blksize < 0 || my_blksize > (1024 * 1000) ) {
3041 printf( "ioctl appears to have returned incorrect block size data \n" );
3042 goto test_failed_exit;
3043 }
3044
3045 my_err = 0;
3046 goto test_passed_exit;
3047
3048test_failed_exit:
3049 my_err = -1;
3050
3051test_passed_exit:
3052 if ( my_fd != -1 )
3053 close( my_fd );
3054 return( my_err );
3055}
3056
3057/* **************************************************************************************************************
3058 * Test mkdir, rmdir, umask system calls.
3059 * **************************************************************************************************************
3060 */
3061int mkdir_rmdir_umask_test( void * the_argp )
3062{
3063 int my_err;
3064 int my_fd = -1;
3065 int did_umask = 0;
3066 char * my_pathp = NULL;
3067 mode_t my_orig_mask;
3068 struct stat my_sb;
b0d623f7
A
3069 kern_return_t my_kr;
3070
3071 my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
3072 if(my_kr != KERN_SUCCESS){
3073 printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
3074 goto test_failed_exit;
3075 }
2d21ac55 3076
2d21ac55
A
3077 *my_pathp = 0x00;
3078 strcat( my_pathp, &g_target_path[0] );
3079 strcat( my_pathp, "/" );
3080
3081 /* get a unique name to use with mkdir */
3082 my_err = create_random_name( my_pathp, 0 );
3083 if ( my_err != 0 ) {
3084 printf( "create_random_name failed with error %d\n", my_err );
3085 goto test_failed_exit;
3086 }
3087
3088 /* set umask to clear WX for other and group and clear X for user */
3089 my_orig_mask = umask( (S_IXUSR | S_IWGRP | S_IXGRP | S_IWOTH | S_IXOTH) );
3090 did_umask = 1;
3091
3092 /* create a directory with RWX for user, group, other (which should be limited by umask) */
3093 my_err = mkdir( my_pathp, (S_IRWXU | S_IRWXG | S_IRWXO) );
3094 if ( my_err == -1 ) {
3095 printf( "mkdir failed with error %d - \"%s\" \n", errno, strerror( errno) );
3096 goto test_failed_exit;
3097 }
3098
3099 /* verify results - (S_IXUSR | S_IWGRP | S_IXGRP | S_IWOTH | S_IXOTH) should be clear*/
3100 my_err = stat( my_pathp, &my_sb );
3101 if ( my_err != 0 ) {
3102 printf( "stat call failed with error %d - \"%s\" \n", errno, strerror( errno) );
3103 goto test_failed_exit;
3104 }
3105 if ( (my_sb.st_mode & (S_IXUSR | S_IWGRP | S_IXGRP | S_IWOTH | S_IXOTH)) != 0 ) {
3106 printf( "umask did not limit modes as it should have \n" );
3107 goto test_failed_exit;
3108 }
3109
3110 /* get rid of our test directory */
3111 my_err = rmdir( my_pathp );
3112 if ( my_err == -1 ) {
3113 printf( "rmdir failed with error %d - \"%s\" \n", errno, strerror( errno) );
3114 goto test_failed_exit;
3115 }
3116 my_err = 0;
3117 goto test_passed_exit;
3118
3119test_failed_exit:
3120 my_err = -1;
3121
3122test_passed_exit:
3123 if ( my_fd != -1 )
3124 close( my_fd );
3125 if ( my_pathp != NULL ) {
b0d623f7
A
3126 rmdir( my_pathp );
3127 vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
2d21ac55
A
3128 }
3129 if ( did_umask != 0 ) {
3130 umask( my_orig_mask );
3131 }
3132
3133 return( my_err );
3134}
3135
3136/* **************************************************************************************************************
3137 * Test chroot system call.
3138 * **************************************************************************************************************
3139 */
3140int chroot_test( void * the_argp )
3141{
3142 int my_err, my_status;
3143 pid_t my_pid, my_wait_pid;
3144 char * my_pathp = NULL;
b0d623f7 3145 kern_return_t my_kr;
2d21ac55
A
3146
3147 if ( g_skip_setuid_tests != 0 ) {
3148 printf("\t skipping this test \n");
3149 my_err = 0;
3150 goto test_passed_exit;
3151 }
3152
b0d623f7
A
3153 my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
3154 if(my_kr != KERN_SUCCESS){
3155 printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
3156 goto test_failed_exit;
3157 }
3158
2d21ac55
A
3159 *my_pathp = 0x00;
3160 strcat( my_pathp, &g_target_path[0] );
3161 strcat( my_pathp, "/" );
3162
3163 /* get a unique name for our test directory */
3164 my_err = create_random_name( my_pathp, 0 );
3165 if ( my_err != 0 ) {
3166 goto test_failed_exit;
3167 }
3168
3169 /* create a test directory */
3170 my_err = mkdir( my_pathp, (S_IRWXU | S_IRWXG | S_IRWXO) );
3171 if ( my_err == -1 ) {
3172 printf( "mkdir failed with error %d - \"%s\" \n", errno, strerror( errno) );
3173 goto test_failed_exit;
3174 }
3175
3176 /*
3177 * spin off a child process that we will use for testing.
3178 */
3179 my_pid = fork( );
3180 if ( my_pid == -1 ) {
3181 printf( "fork failed with errno %d - %s \n", errno, strerror( errno ) );
3182 goto test_failed_exit;
3183 }
3184 if ( my_pid == 0 ) {
3185 /*
3186 * child process - do getlogin and setlogin testing.
3187 */
3188 struct stat my_sb;
3189
3190 /* change our root to our new test directory */
3191 my_err = chroot( my_pathp );
3192 if ( my_err != 0 ) {
3193 printf( "chroot failed with error %d - \"%s\" \n", errno, strerror( errno) );
3194 exit( -1 );
3195 }
3196
3197 /* verify root directory is now an empty directory */
3198 my_err = stat( "/", &my_sb );
3199 if ( my_err != 0 ) {
3200 printf( "stat call failed with error %d - \"%s\" \n", errno, strerror( errno) );
3201 exit( -1 );
3202 }
3203 if ( my_sb.st_nlink > 2 ) {
3204 printf( "root dir should be emnpty! \n" );
3205 exit( -1 );
3206 }
3207 exit( 0 );
3208 }
3209
3210 /* parent process -
3211 * wait for child to exit
3212 */
3213 my_wait_pid = wait4( my_pid, &my_status, 0, NULL );
3214 if ( my_wait_pid == -1 ) {
3215 printf( "wait4 failed with errno %d - %s \n", errno, strerror( errno ) );
3216 goto test_failed_exit;
3217 }
3218
3219 if ( WIFEXITED( my_status ) && WEXITSTATUS( my_status ) != 0 ) {
3220 printf( "bad exit status\n" );
3221 goto test_failed_exit;
3222 }
3223
3224 my_err = 0;
3225 goto test_passed_exit;
3226
3227test_failed_exit:
3228 my_err = -1;
3229
3230test_passed_exit:
3231 if ( my_pathp != NULL ) {
3232 my_err = rmdir( my_pathp );
3233 if ( my_err != 0 ) {
3234 printf( "rmdir failed with error %d - \"%s\" path %p\n", errno, strerror( errno), my_pathp );
3235 }
b0d623f7 3236 vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
2d21ac55
A
3237 }
3238 return( my_err );
3239}
3240
3241/* **************************************************************************************************************
3242 * Test getpgrp, getpgid, getsid, setpgid, setpgrp, setsid system calls.
3243 * **************************************************************************************************************
3244 */
3245int process_group_test( void * the_argp )
3246{
3247 int my_err = 0, i = 0;
3248 pid_t my_session_id, my_pid, my_process_group;
3249
3250 /* get current session ID, pgid, and pid */
3251 my_session_id = getsid( 0 );
3252 if ( my_session_id == -1 ) {
3253 printf( "getsid call failed with error %d - \"%s\" \n",
3254 errno, strerror( errno ) );
3255 goto test_failed_exit;
3256 }
3257
3258 my_pid = getpid( );
3259 my_process_group = getpgrp( );
3260
2d21ac55
A
3261 /* test getpgrp and getpgid - they should return the same results when 0 is passed to getpgid */
3262 if ( my_process_group != getpgid( 0 ) ) {
3263 printf( "getpgrp and getpgid did not return the same process group ID \n" );
cf7d32b8 3264 printf( "getpgid: %d, my_process_group: %d\n", getpgid( 0 ), my_process_group );
2d21ac55
A
3265 goto test_failed_exit;
3266 }
3267
3268 if ( my_pid == my_process_group ) {
3269 /* we are process group leader */
3270 my_err = setsid( );
3271 if ( my_err == 0 || errno != EPERM ) {
3272 printf( "setsid call should have failed with EPERM\n" );
3273 goto test_failed_exit;
3274 }
3275 } else {
3276 /* we are not process group leader: try creating new session */
3277 my_err = setsid( );
3278 if ( my_err == -1 ) {
3279 printf( "setsid call failed with error %d - \"%s\" \n",
3280 errno, strerror( errno ) );
3281 goto test_failed_exit;
3282 }
3283
3284 if ( my_process_group == getpgid( 0 ) ) {
3285 printf( "process group was not reset \n" );
3286 goto test_failed_exit;
3287 }
3288 }
3289
3290 /* find an unused process group ID */
3291 for ( i = 10000; i < 1000000; i++ ) {
3292 my_process_group = getpgid( i );
3293 if ( my_process_group == -1 ) {
3294 break;
3295 }
3296 }
3297
3298 /* this should fail */
3299 my_err = setpgid( 0, my_process_group );
3300 if ( my_err != -1 ) {
3301 printf( "setpgid should have failed, but did not \n" );
3302 goto test_failed_exit;
3303 }
3304
3305 my_err = 0;
3306 goto test_passed_exit;
3307
3308test_failed_exit:
3309 my_err = -1;
3310
3311test_passed_exit:
3312 return( my_err );
3313}
3314
3315/* **************************************************************************************************************
3316 * Test fcntl system calls.
3317 * **************************************************************************************************************
3318 */
3319int fcntl_test( void * the_argp )
3320{
3321 int my_err, my_result, my_tmep;
3322 int my_fd = -1;
3323 char * my_pathp = NULL;
b0d623f7
A
3324 kern_return_t my_kr;
3325
3326 my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
3327 if(my_kr != KERN_SUCCESS){
3328 printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
3329 goto test_failed_exit;
3330 }
2d21ac55 3331
2d21ac55
A
3332 *my_pathp = 0x00;
3333 strcat( my_pathp, &g_target_path[0] );
3334 strcat( my_pathp, "/" );
3335
3336 /* create a test file */
3337 my_err = create_random_name( my_pathp, 1 );
3338 if ( my_err != 0 ) {
3339 goto test_failed_exit;
3340 }
3341
3342 /* open our test file and use fcntl to get / set file descriptor flags */
3343 my_fd = open( my_pathp, O_RDONLY, 0 );
3344 if ( my_fd == -1 ) {
3345 printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
3346 goto test_failed_exit;
3347 }
3348
3349 my_result = fcntl( my_fd, F_GETFD, 0 );
3350 if ( my_result == -1 ) {
3351 printf( "fcntl - F_GETFD - failed with error %d - \"%s\" \n", errno, strerror( errno) );
3352 goto test_failed_exit;
3353 }
3354
3355 my_tmep = (my_result & FD_CLOEXEC);
3356 if ( my_tmep ) {
3357 /* FD_CLOEXEC is on, let's turn it off */
3358 my_result = fcntl( my_fd, F_SETFD, 0 );
3359 }
3360 else {
3361 /* FD_CLOEXEC is off, let's turn it on */
3362 my_result = fcntl( my_fd, F_SETFD, 1 );
3363 }
3364 if ( my_result == -1 ) {
3365 printf( "fcntl - F_SETFD - failed with error %d - \"%s\" \n", errno, strerror( errno) );
3366 goto test_failed_exit;
3367 }
3368
3369 /* now check to see if it is set correctly */
3370 my_result = fcntl( my_fd, F_GETFD, 0 );
3371 if ( my_result == -1 ) {
3372 printf( "fcntl - F_GETFD - failed with error %d - \"%s\" \n", errno, strerror( errno) );
3373 goto test_failed_exit;
3374 }
3375 if ( my_tmep == (my_result & 0x01) ) {
3376 printf( "fcntl - F_SETFD failed to set FD_CLOEXEC correctly!!! \n" );
3377 goto test_failed_exit;
3378 }
3379
3380 my_err = 0;
3381 goto test_passed_exit;
3382
3383test_failed_exit:
3384 my_err = -1;
3385
3386test_passed_exit:
3387 if ( my_fd != -1 )
3388 close( my_fd );
3389 if ( my_pathp != NULL ) {
b0d623f7
A
3390 remove( my_pathp );
3391 vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
2d21ac55
A
3392 }
3393 return( my_err );
3394}
3395
3396/* **************************************************************************************************************
3397 * Test getpriority, setpriority system calls.
3398 * **************************************************************************************************************
3399 */
3400int getpriority_setpriority_test( void * the_argp )
3401{
3402 int my_err;
3403 int my_priority;
3404 int my_new_priority;
3405
3406 /* getpriority returns scheduling priority so -1 is a valid value */
3407 errno = 0;
3408 my_priority = getpriority( PRIO_PROCESS, 0 );
3409 if ( my_priority == -1 && errno != 0 ) {
3410 printf( "getpriority - failed with error %d - \"%s\" \n", errno, strerror( errno) );
3411 goto test_failed_exit;
3412 }
3413
3414 /* change scheduling priority */
3415 my_new_priority = (my_priority == PRIO_MIN) ? (my_priority + 10) : (PRIO_MIN);
3416 my_err = setpriority( PRIO_PROCESS, 0, my_new_priority );
3417 if ( my_err == -1 ) {
3418 printf( "setpriority - failed with error %d - \"%s\" \n", errno, strerror( errno) );
3419 goto test_failed_exit;
3420 }
3421
3422 /* verify change */
3423 errno = 0;
3424 my_priority = getpriority( PRIO_PROCESS, 0 );
3425 if ( my_priority == -1 && errno != 0 ) {
3426 printf( "getpriority - failed with error %d - \"%s\" \n", errno, strerror( errno) );
3427 goto test_failed_exit;
3428 }
3429
3430 if ( my_priority != my_new_priority ) {
3431 printf( "setpriority - failed to set correct scheduling priority \n" );
3432 goto test_failed_exit;
3433 }
3434
3435 /* reset scheduling priority */
3436 my_err = setpriority( PRIO_PROCESS, 0, 0 );
3437 if ( my_err == -1 ) {
3438 printf( "setpriority - failed with error %d - \"%s\" \n", errno, strerror( errno) );
3439 goto test_failed_exit;
3440 }
3441
3442 my_err = 0;
3443 goto test_passed_exit;
3444
3445test_failed_exit:
3446 my_err = -1;
3447
3448test_passed_exit:
3449 return( my_err );
3450}
3451
3452/* **************************************************************************************************************
3453 * Test futimes, gettimeofday, settimeofday, utimes system calls.
3454 * **************************************************************************************************************
3455 */
3456int time_tests( void * the_argp )
3457{
3458 int my_err;
3459 int my_fd = -1;
3460 char * my_pathp = NULL;
3461 struct timeval my_orig_time;
3462 struct timeval my_temp_time;
3463 struct timeval my_utimes[4];
3464 struct timezone my_tz;
3465 struct stat my_sb;
b0d623f7 3466 kern_return_t my_kr;
2d21ac55
A
3467
3468 if ( g_skip_setuid_tests != 0 ) {
3469 printf( "\t skipping this test \n" );
3470 my_err = 0;
3471 goto test_passed_exit;
3472 }
3473
b0d623f7
A
3474 my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
3475 if(my_kr != KERN_SUCCESS){
3476 printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
3477 goto test_failed_exit;
3478 }
3479
2d21ac55
A
3480 *my_pathp = 0x00;
3481 strcat( my_pathp, &g_target_path[0] );
3482 strcat( my_pathp, "/" );
3483
3484 /* create a test file */
3485 my_err = create_random_name( my_pathp, 1 );
3486 if ( my_err != 0 ) {
3487 goto test_failed_exit;
3488 }
3489
3490 my_err = gettimeofday( &my_orig_time, &my_tz );
3491 if ( my_err == -1 ) {
3492 printf( "gettimeofday - failed with error %d - \"%s\" \n", errno, strerror( errno) );
3493 goto test_failed_exit;
3494 }
3495 //printf( "tv_sec %d tv_usec %ld \n", my_orig_time.tv_sec, my_orig_time.tv_usec );
3496
3497 my_temp_time = my_orig_time;
3498 my_temp_time.tv_sec -= 60;
3499 my_err = settimeofday( &my_temp_time, NULL );
3500 if ( my_err == -1 ) {
3501 printf( "settimeofday - failed with error %d - \"%s\" \n", errno, strerror( errno) );
3502 goto test_failed_exit;
3503 }
3504
3505 my_err = gettimeofday( &my_temp_time, NULL );
3506 if ( my_err == -1 ) {
3507 printf( "gettimeofday - failed with error %d - \"%s\" \n", errno, strerror( errno) );
3508 goto test_failed_exit;
3509 }
3510 //printf( "tv_sec %d tv_usec %ld \n", my_temp_time.tv_sec, my_temp_time.tv_usec );
3511 if ( my_orig_time.tv_sec <= my_temp_time.tv_sec ) {
3512 printf( "settimeofday did not set correct time \n" );
3513 goto test_failed_exit;
3514 }
3515
3516 /* set time back to original value plus 1 second */
3517 my_temp_time = my_orig_time;
3518 my_temp_time.tv_sec += 1;
3519 my_err = settimeofday( &my_temp_time, NULL );
3520 if ( my_err == -1 ) {
3521 printf( "settimeofday - failed with error %d - \"%s\" \n", errno, strerror( errno) );
3522 goto test_failed_exit;
3523 }
3524
3525 /* test utimes and futimes - get current access and mod times then change them */
3526 my_err = stat( my_pathp, &my_sb );
3527 if ( my_err != 0 ) {
3528 printf( "stat - failed with error %d - \"%s\" \n", errno, strerror( errno) );
3529 goto test_failed_exit;
3530 }
3531 TIMESPEC_TO_TIMEVAL( &my_utimes[0], &my_sb.st_atimespec );
3532 TIMESPEC_TO_TIMEVAL( &my_utimes[1], &my_sb.st_mtimespec );
3533 my_utimes[0].tv_sec -= 120; /* make access time 2 minutes older */
3534 my_utimes[1].tv_sec -= 120; /* make mod time 2 minutes older */
3535
3536 my_err = utimes( my_pathp, &my_utimes[0] );
3537 if ( my_err == -1 ) {
3538 printf( "utimes - failed with error %d - \"%s\" \n", errno, strerror( errno) );
3539 goto test_failed_exit;
3540 }
3541
3542 /* make sure the correct times are set */
3543 my_err = stat( my_pathp, &my_sb );
3544 if ( my_err != 0 ) {
3545 printf( "stat - failed with error %d - \"%s\" \n", errno, strerror( errno) );
3546 goto test_failed_exit;
3547 }
3548 TIMESPEC_TO_TIMEVAL( &my_utimes[2], &my_sb.st_atimespec );
3549 TIMESPEC_TO_TIMEVAL( &my_utimes[3], &my_sb.st_mtimespec );
3550 if ( my_utimes[0].tv_sec != my_utimes[2].tv_sec ||
3551 my_utimes[1].tv_sec != my_utimes[3].tv_sec ) {
3552 printf( "utimes failed to set access and mod times \n" );
3553 goto test_failed_exit;
3554 }
3555
3556 my_fd = open( my_pathp, O_RDWR, 0 );
3557 if ( my_fd == -1 ) {
3558 printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
3559 goto test_failed_exit;
3560 }
3561
3562 my_utimes[0].tv_sec -= 120; /* make access time 2 minutes older */
3563 my_utimes[1].tv_sec -= 120; /* make mod time 2 minutes older */
3564 my_err = futimes( my_fd, &my_utimes[0] );
3565 if ( my_err == -1 ) {
3566 printf( "futimes - failed with error %d - \"%s\" \n", errno, strerror( errno) );
3567 goto test_failed_exit;
3568 }
3569
3570 /* make sure the correct times are set */
3571 my_err = stat( my_pathp, &my_sb );
3572 if ( my_err != 0 ) {
3573 printf( "stat - failed with error %d - \"%s\" \n", errno, strerror( errno) );
3574 goto test_failed_exit;
3575 }
3576 TIMESPEC_TO_TIMEVAL( &my_utimes[2], &my_sb.st_atimespec );
3577 TIMESPEC_TO_TIMEVAL( &my_utimes[3], &my_sb.st_mtimespec );
3578 if ( my_utimes[0].tv_sec != my_utimes[2].tv_sec ||
3579 my_utimes[1].tv_sec != my_utimes[3].tv_sec ) {
3580 printf( "futimes failed to set access and mod times \n" );
3581 goto test_failed_exit;
3582 }
3583
3584 my_err = 0;
3585 goto test_passed_exit;
3586
3587test_failed_exit:
3588 my_err = -1;
3589
3590test_passed_exit:
3591 if ( my_fd != -1 )
3592 close( my_fd );
3593 if ( my_pathp != NULL ) {
b0d623f7
A
3594 remove( my_pathp );
3595 vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
2d21ac55
A
3596 }
3597 return( my_err );
3598}
3599
3600/* **************************************************************************************************************
3601 * Test rename, stat system calls.
3602 * **************************************************************************************************************
3603 */
3604int rename_test( void * the_argp )
3605{
3606 int my_err;
3607 char * my_pathp = NULL;
3608 char * my_new_pathp = NULL;
3609 ino_t my_file_id;
3610 struct stat my_sb;
b0d623f7
A
3611 kern_return_t my_kr;
3612
3613 my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
3614 if(my_kr != KERN_SUCCESS){
3615 printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
3616 goto test_failed_exit;
3617 }
2d21ac55 3618
2d21ac55
A
3619 *my_pathp = 0x00;
3620 strcat( my_pathp, &g_target_path[0] );
3621 strcat( my_pathp, "/" );
3622
3623 /* create a test file */
3624 my_err = create_random_name( my_pathp, 1 );
3625 if ( my_err != 0 ) {
3626 goto test_failed_exit;
3627 }
3628
b0d623f7
A
3629 my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_new_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
3630 if(my_kr != KERN_SUCCESS){
3631 printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
3632 goto test_failed_exit;
3633 }
3634
2d21ac55
A
3635 *my_new_pathp = 0x00;
3636 strcat( my_new_pathp, &g_target_path[0] );
3637 strcat( my_new_pathp, "/" );
3638
3639 /* get a unique name for our rename test */
3640 my_err = create_random_name( my_new_pathp, 0 );
3641 if ( my_err != 0 ) {
3642 goto test_failed_exit;
3643 }
3644
3645 /* save file ID for later use */
3646 my_err = stat( my_pathp, &my_sb );
3647 if ( my_err != 0 ) {
3648 printf( "stat - failed with error %d - \"%s\" \n", errno, strerror( errno) );
3649 goto test_failed_exit;
3650 }
3651 my_file_id = my_sb.st_ino;
3652
3653 /* test rename */
3654 my_err = rename( my_pathp, my_new_pathp );
3655 if ( my_err == -1 ) {
3656 printf( "rename - failed with error %d - \"%s\" \n", errno, strerror( errno) );
3657 goto test_failed_exit;
3658 }
3659
3660 /* make sure old name is no longer there */
3661 my_err = stat( my_pathp, &my_sb );
3662 if ( my_err == 0 ) {
3663 printf( "rename call failed - found old name \n" );
3664 goto test_failed_exit;
3665 }
3666
3667 /* make sure new name is there and is correct file id */
3668 my_err = stat( my_new_pathp, &my_sb );
3669 if ( my_err != 0 ) {
3670 printf( "stat - failed with error %d - \"%s\" \n", errno, strerror( errno) );
3671 goto test_failed_exit;
3672 }
3673 if ( my_file_id != my_sb.st_ino ) {
3674 printf( "rename failed - wrong file id \n" );
3675 goto test_failed_exit;
3676 }
3677
3678 my_err = 0;
3679 goto test_passed_exit;
3680
3681test_failed_exit:
3682 my_err = -1;
3683
3684test_passed_exit:
3685 if ( my_pathp != NULL ) {
b0d623f7
A
3686 remove( my_pathp );
3687 vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
2d21ac55
A
3688 }
3689 if ( my_new_pathp != NULL ) {
b0d623f7
A
3690 remove( my_new_pathp );
3691 vm_deallocate(mach_task_self(), (vm_address_t)my_new_pathp, PATH_MAX);
2d21ac55
A
3692 }
3693 return( my_err );
3694}
3695
3696/* **************************************************************************************************************
3697 * Test locking system calls.
3698 * **************************************************************************************************************
3699 */
3700int locking_test( void * the_argp )
3701{
3702 int my_err, my_status;
3703 pid_t my_pid, my_wait_pid;
3704 int my_fd = -1;
3705 char * my_pathp = NULL;
b0d623f7
A
3706 kern_return_t my_kr;
3707
3708 my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
3709 if(my_kr != KERN_SUCCESS){
3710 printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
3711 goto test_failed_exit;
3712 }
2d21ac55 3713
2d21ac55
A
3714 *my_pathp = 0x00;
3715 strcat( my_pathp, &g_target_path[0] );
3716 strcat( my_pathp, "/" );
3717
3718 /* create a test file */
3719 my_err = create_random_name( my_pathp, 1 );
3720 if ( my_err != 0 ) {
3721 goto test_failed_exit;
3722 }
3723
3724 /* test flock */
3725 my_fd = open( my_pathp, O_RDWR, 0 );
3726 if ( my_fd == -1 ) {
3727 printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
3728 goto test_failed_exit;
3729 }
3730
3731 my_err = flock( my_fd, LOCK_EX );
3732 if ( my_err == -1 ) {
3733 printf( "flock - LOCK_EX - failed with error %d - \"%s\" \n", errno, strerror( errno) );
3734 goto test_failed_exit;
3735 }
3736
3737 /*
3738 * spin off a child process that we will use for testing.
3739 */
3740 my_pid = fork( );
3741 if ( my_pid == -1 ) {
3742 printf( "fork failed with errno %d - %s \n", errno, strerror( errno ) );
3743 goto test_failed_exit;
3744 }
3745 if ( my_pid == 0 ) {
3746 /*
3747 * child process.
3748 */
3749 int my_child_fd = -1;
3750 int my_child_err;
3751
3752 my_child_fd = open( my_pathp, O_RDWR, 0 );
3753 if ( my_child_fd == -1 ) {
3754 printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
3755 my_child_err = -1;
3756 goto child_exit;
3757 }
3758
3759 my_err = flock( my_child_fd, (LOCK_EX | LOCK_NB) );
3760 if ( my_err == -1 ) {
3761 if ( errno != EWOULDBLOCK ) {
3762 printf( "flock call failed with error %d - \"%s\" \n", errno, strerror( errno) );
3763 my_child_err = -1;
3764 goto child_exit;
3765 }
3766 }
3767 else {
3768 printf( "flock call should have failed with EWOULDBLOCK err \n" );
3769 my_child_err = -1;
3770 goto child_exit;
3771 }
3772 my_child_err = 0;
3773child_exit:
3774 if ( my_child_fd != -1 )
3775 close( my_child_fd );
3776 exit( my_child_err );
3777 }
3778
3779 /* parent process -
3780 * wait for child to exit
3781 */
3782 my_wait_pid = wait4( my_pid, &my_status, 0, NULL );
3783 if ( my_wait_pid == -1 ) {
3784 printf( "wait4 failed with errno %d - %s \n", errno, strerror( errno ) );
3785 goto test_failed_exit;
3786 }
3787
3788 if ( WIFEXITED( my_status ) && WEXITSTATUS( my_status ) != 0 ) {
3789 goto test_failed_exit;
3790 }
3791
3792 my_err = flock( my_fd, LOCK_UN );
3793 if ( my_err == -1 ) {
3794 printf( "flock - LOCK_UN - failed with error %d - \"%s\" \n", errno, strerror( errno) );
3795 goto test_failed_exit;
3796 }
3797
3798 my_err = 0;
3799 goto test_passed_exit;
3800
3801test_failed_exit:
3802 my_err = -1;
3803
3804test_passed_exit:
3805 if ( my_fd != -1 )
3806 close( my_fd );
3807 if ( my_pathp != NULL ) {
b0d623f7
A
3808 remove( my_pathp );
3809 vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
2d21ac55
A
3810 }
3811 return( my_err );
3812}
3813
3814/* **************************************************************************************************************
3815 * Test mkfifo system calls.
3816 * **************************************************************************************************************
3817 */
3818int mkfifo_test( void * the_argp )
3819{
3820 int my_err, my_status;
3821 pid_t my_pid, my_wait_pid;
3822 int my_fd = -1;
3823 char * my_pathp = NULL;
3824 ssize_t my_result;
3825 off_t my_current_offset;
b0d623f7
A
3826 kern_return_t my_kr;
3827
3828 my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
3829 if(my_kr != KERN_SUCCESS){
3830 printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
3831 goto test_failed_exit;
3832 }
2d21ac55 3833
2d21ac55
A
3834 *my_pathp = 0x00;
3835 strcat( my_pathp, &g_target_path[0] );
3836 strcat( my_pathp, "/" );
3837
3838 /* get unique name for our fifo */
3839 my_err = create_random_name( my_pathp, 0 );
3840 if ( my_err != 0 ) {
3841 goto test_failed_exit;
3842 }
3843
3844 my_err = mkfifo( my_pathp, (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) );
3845 if ( my_err != 0 ) {
3846 printf( "mkfifo failed with errno %d - %s. \n", errno, strerror( errno ) );
3847 goto test_failed_exit;
3848 }
3849
3850 /*
3851 * spin off a child process that we will use for testing.
3852 */
3853 my_pid = fork( );
3854 if ( my_pid == -1 ) {
3855 printf( "fork failed with errno %d - %s \n", errno, strerror( errno ) );
3856 goto test_failed_exit;
3857 }
3858 if ( my_pid == 0 ) {
3859 /*
3860 * child process.
3861 */
3862 int my_child_fd = -1;
3863 int my_child_err;
3864 char my_buffer[64];
3865
3866 /* open read end of fifo */
3867 my_child_fd = open( my_pathp, O_RDWR, 0 );
3868 if ( my_child_fd == -1 ) {
3869 printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
3870 my_child_err = -1;
3871 goto child_exit;
3872 }
3873
3874 /* read message from parent */
3875 bzero( (void *)&my_buffer[0], sizeof(my_buffer) );
3876 my_result = read( my_child_fd, &my_buffer[0], sizeof(my_buffer) );
3877 if ( my_result == -1 ) {
3878 printf( "read call failed with error %d - \"%s\" \n", errno, strerror( errno) );
3879 my_child_err = -1;
3880 goto child_exit;
3881 }
3882 if ( strcmp( "parent to child", &my_buffer[0] ) != 0 ) {
3883 printf( "read wrong message from parent \n" );
3884 my_child_err = -1;
3885 goto child_exit;
3886 }
3887
3888 my_child_err = 0;
3889child_exit:
3890 if ( my_child_fd != -1 )
3891 close( my_child_fd );
3892 exit( my_child_err );
3893 }
3894
3895 /* parent process - open write end of fifo
3896 */
3897 my_fd = open( my_pathp, O_WRONLY, 0 );
3898 if ( my_fd == -1 ) {
3899 printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
3900 goto test_failed_exit;
3901 }
3902
3903 /* make sure we can't seek on a fifo */
3904 my_current_offset = lseek( my_fd, 0, SEEK_CUR );
3905 if ( my_current_offset != -1 ) {
3906 printf( "lseek on fifo should fail but did not \n" );
3907 goto test_failed_exit;
3908 }
3909
3910 my_result = write( my_fd, "parent to child", 15 );
3911 if ( my_result == -1 ) {
3912 printf( "write call failed with error %d - \"%s\" \n", errno, strerror( errno) );
3913 goto test_failed_exit;
3914 }
3915
3916 my_wait_pid = wait4( my_pid, &my_status, 0, NULL );
3917 if ( my_wait_pid == -1 ) {
3918 printf( "wait4 failed with errno %d - %s \n", errno, strerror( errno ) );
3919 goto test_failed_exit;
3920 }
3921
3922 if ( WIFEXITED( my_status ) && WEXITSTATUS( my_status ) != 0 ) {
3923 goto test_failed_exit;
3924 }
3925
3926 my_err = 0;
3927 goto test_passed_exit;
3928
3929test_failed_exit:
3930 my_err = -1;
3931
3932test_passed_exit:
3933 if ( my_fd != -1 )
3934 close( my_fd );
3935 if ( my_pathp != NULL ) {
b0d623f7
A
3936 remove( my_pathp );
3937 vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
2d21ac55
A
3938 }
3939 return( my_err );
3940}
3941
3942/* **************************************************************************************************************
3943 * Test quotactl system calls.
3944 * **************************************************************************************************************
3945 */
3946int quotactl_test( void * the_argp )
3947{
cf7d32b8 3948#if !TARGET_OS_EMBEDDED
2d21ac55
A
3949 int my_err;
3950 int is_quotas_on = 0;
3951 struct dqblk my_quota_blk;
3952
3953 if ( g_skip_setuid_tests != 0 ) {
3954 printf( "\t skipping this test \n" );
3955 my_err = 0;
3956 goto test_passed_exit;
3957 }
3958
3959 /* start off by checking the status of quotas on the boot volume */
3960 my_err = quotactl( "/mach_kernel", QCMD(Q_QUOTASTAT, USRQUOTA), 0, (caddr_t)&is_quotas_on );
3961 if ( my_err == -1 ) {
3962 printf( "quotactl - Q_QUOTASTAT - failed with errno %d - %s \n", errno, strerror( errno ) );
3963 goto test_failed_exit;
3964 }
3965
3966 if ( is_quotas_on == 0 ) {
3967 /* quotas are off */
3968 my_err = 0;
3969 goto test_passed_exit;
3970 }
3971
3972 my_err = quotactl( "/mach_kernel", QCMD(Q_GETQUOTA, USRQUOTA), getuid(), (caddr_t)&my_quota_blk );
3973 if ( my_err == -1 ) {
3974 printf( "quotactl - Q_GETQUOTA - failed with errno %d - %s \n", errno, strerror( errno ) );
3975 goto test_failed_exit;
3976 }
3977
3978 my_err = 0;
3979 goto test_passed_exit;
3980
3981test_failed_exit:
3982 my_err = -1;
3983
3984test_passed_exit:
3985 return( my_err );
cf7d32b8
A
3986#else
3987 printf( "\t--> Not supported on EMBEDDED TARGET\n" );
3988 return 0;
3989#endif
2d21ac55
A
3990}
3991
3992/* **************************************************************************************************************
3993 * Test getrlimit, setrlimit system calls.
3994 * **************************************************************************************************************
3995 */
3996int limit_tests( void * the_argp )
3997{
3998 int my_err;
3999 struct rlimit my_current_rlimit;
4000 struct rlimit my_rlimit;
4001
4002 my_err = getrlimit( RLIMIT_NOFILE, &my_current_rlimit );
4003 if ( my_err == -1 ) {
4004 printf( "getrlimit - failed with errno %d - %s \n", errno, strerror( errno ) );
4005 goto test_failed_exit;
4006 }
4007 if ( my_current_rlimit.rlim_cur != RLIM_INFINITY ) {
4008 if ( my_current_rlimit.rlim_cur != my_current_rlimit.rlim_max )
4009 my_current_rlimit.rlim_cur += 1;
4010 else
4011 my_current_rlimit.rlim_cur -= 1;
4012 my_rlimit.rlim_cur = my_current_rlimit.rlim_cur;
4013 my_rlimit.rlim_max = my_current_rlimit.rlim_max;
4014 my_err = setrlimit( RLIMIT_NOFILE, &my_rlimit );
4015 if ( my_err == -1 ) {
4016 printf( "setrlimit - failed with errno %d - %s \n", errno, strerror( errno ) );
4017 goto test_failed_exit;
4018 }
4019
4020 /* verify that we set a new limit */
4021 bzero( (void *) &my_rlimit, sizeof( my_rlimit ) );
4022 my_err = getrlimit( RLIMIT_NOFILE, &my_rlimit );
4023 if ( my_err == -1 ) {
4024 printf( "getrlimit - failed with errno %d - %s \n", errno, strerror( errno ) );
4025 goto test_failed_exit;
4026 }
4027 if ( my_rlimit.rlim_cur != my_current_rlimit.rlim_cur ) {
4028 printf( "failed to get/set new RLIMIT_NOFILE soft limit \n" );
4029 printf( "soft limits - current %lld should be %lld \n", my_rlimit.rlim_cur, my_current_rlimit.rlim_cur );
4030 goto test_failed_exit;
4031 }
b0d623f7 4032
2d21ac55
A
4033#if CONFORMANCE_CHANGES_IN_XNU // can't do this check until conformance changes get into xnu
4034 printf( "hard limits - current %lld should be %lld \n", my_rlimit.rlim_max, my_current_rlimit.rlim_max );
4035 if ( my_rlimit.rlim_max != my_current_rlimit.rlim_max ) {
4036 printf( "failed to get/set new RLIMIT_NOFILE hard limit \n" );
4037 goto test_failed_exit;
4038 }
4039#endif
b0d623f7
A
4040
4041 /*
4042 * A test for a limit that won't fit in a signed 32 bits, a la 5414697
4043 * Note: my_rlimit should still have a valid rlim_max.
4044 */
4045 long long biglim = 2147483649ll; /* Just over 2^31 */
4046 my_rlimit.rlim_cur = biglim;
4047 my_err = setrlimit(RLIMIT_CPU, &my_rlimit);
4048 if (my_err == -1) {
4049 printf("failed to set large limit.\n");
4050 goto test_failed_exit;
4051 }
4052
4053 bzero(&my_rlimit, sizeof(struct rlimit));
4054 my_err = getrlimit(RLIMIT_CPU, &my_rlimit);
4055 if (my_err == -1) {
4056 printf("after setting large value, failed to getrlimit().\n");
4057 goto test_failed_exit;
4058 }
4059
4060 if (my_rlimit.rlim_cur != biglim) {
4061 printf("didn't retrieve large limit.\n");
4062 goto test_failed_exit;
4063 }
2d21ac55
A
4064 }
4065
4066 my_err = 0;
4067 goto test_passed_exit;
4068
4069test_failed_exit:
4070 my_err = -1;
4071
4072test_passed_exit:
4073 return( my_err );
4074}
4075
4076/* **************************************************************************************************************
b0d623f7 4077 * Test getattrlist, getdirentriesattr, setattrlist system calls.
2d21ac55
A
4078 * **************************************************************************************************************
4079 */
4080struct test_attr_buf {
4081 uint32_t length;
4082 fsobj_type_t obj_type;
4083 fsobj_id_t obj_id;
4084 struct timespec backup_time;
4085};
4086
4087typedef struct test_attr_buf test_attr_buf;
4088
4089int directory_tests( void * the_argp )
4090{
4091 int my_err, done, found_it, i;
4092 int my_fd = -1;
4093 int is_ufs = 0;
4094 char * my_pathp = NULL;
4095 char * my_bufp = NULL;
4096 char * my_file_namep;
4097#ifdef __LP64__
4098 unsigned int my_base;
4099 unsigned int my_count;
4100 unsigned int my_new_state;
4101#else
4102 unsigned long my_base;
4103 unsigned long my_count;
4104 unsigned long my_new_state;
4105#endif
4106 fsobj_id_t my_obj_id;
4107 struct timespec my_new_backup_time;
4108 struct attrlist my_attrlist;
4109 test_attr_buf my_attr_buf[4];
4110 struct statfs my_statfs_buf;
b0d623f7 4111 kern_return_t my_kr;
2d21ac55
A
4112
4113 /* need to know type of file system */
4114 my_err = statfs( &g_target_path[0], &my_statfs_buf );
4115 if ( my_err == -1 ) {
4116 printf( "statfs call failed. got errno %d - %s. \n", errno, strerror( errno ) );
4117 goto test_failed_exit;
4118 }
4119 if ( memcmp( &my_statfs_buf.f_fstypename[0], "ufs", 3 ) == 0 ) {
4120 is_ufs = 1;
4121 }
4122
b0d623f7
A
4123 my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_bufp, (1024 * 5), VM_FLAGS_ANYWHERE);
4124 if(my_kr != KERN_SUCCESS){
4125 printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
4126 goto test_failed_exit;
4127 }
4128
4129 my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
4130 if(my_kr != KERN_SUCCESS){
4131 printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
4132 goto test_failed_exit;
4133 }
4134
2d21ac55
A
4135 *my_pathp = 0x00;
4136 strcat( my_pathp, &g_target_path[0] );
4137 strcat( my_pathp, "/" );
4138
4139 /* create a test file */
4140 my_err = create_random_name( my_pathp, 1 );
4141 if ( my_err != 0 ) {
4142 goto test_failed_exit;
4143 }
4144
4145 /* get pointer to just the file name */
4146 my_file_namep = strrchr( my_pathp, '/' );
4147 my_file_namep++;
4148
4149 /* check out the test directory */
4150 my_fd = open( &g_target_path[0], (O_RDONLY), 0 );
4151 if ( my_fd == -1 ) {
4152 printf( "open failed with error %d - \"%s\" \n", errno, strerror( errno) );
4153 goto test_failed_exit;
4154 }
2d21ac55
A
4155
4156 /* test get/setattrlist */
4157 memset( &my_attrlist, 0, sizeof(my_attrlist) );
4158 my_attrlist.bitmapcount = ATTR_BIT_MAP_COUNT;
4159 my_attrlist.commonattr = (ATTR_CMN_OBJTYPE | ATTR_CMN_OBJID | ATTR_CMN_BKUPTIME);
4160 my_err = getattrlist( my_pathp, &my_attrlist, &my_attr_buf[0], sizeof(my_attr_buf[0]), 0 );
cf7d32b8 4161
2d21ac55
A
4162 if ( my_err != 0 ) {
4163 if ( errno == ENOTSUP && is_ufs ) {
4164 /* getattr calls not supported on ufs */
4165 my_err = 0;
4166 goto test_passed_exit;
4167 }
4168 printf( "getattrlist call failed. got errno %d - %s. \n", errno, strerror( errno ) );
4169 goto test_failed_exit;
4170 }
4171 /* validate returned data */
4172 if ( my_attr_buf[0].obj_type != VREG ) {
4173 printf( "getattrlist returned incorrect obj_type data. \n" );
4174 goto test_failed_exit;
4175 }
4176
4177 /* set new backup time */
4178 my_obj_id = my_attr_buf[0].obj_id;
4179 my_new_backup_time = my_attr_buf[0].backup_time;
4180 my_new_backup_time.tv_sec += 60;
4181 my_attr_buf[0].backup_time.tv_sec = my_new_backup_time.tv_sec;
4182 my_attrlist.commonattr = (ATTR_CMN_BKUPTIME);
4183 my_err = setattrlist( my_pathp, &my_attrlist, &my_attr_buf[0].backup_time, sizeof(my_attr_buf[0].backup_time), 0 );
4184 if ( my_err != 0 ) {
4185 printf( "setattrlist call failed. got errno %d - %s. \n", errno, strerror( errno ) );
4186 goto test_failed_exit;
4187 }
4188
4189 /* validate setattrlist using getdirentriesattr */
4190 close( my_fd );
4191 my_fd = open( &g_target_path[0], (O_RDONLY), 0 );
4192 if ( my_fd == -1 ) {
4193 printf( "open failed with error %d - \"%s\" \n", errno, strerror( errno) );
4194 goto test_failed_exit;
4195 }
4196 memset( &my_attrlist, 0, sizeof(my_attrlist) );
4197 memset( &my_attr_buf, 0, sizeof(my_attr_buf) );
4198 my_attrlist.bitmapcount = ATTR_BIT_MAP_COUNT;
4199 my_attrlist.commonattr = (ATTR_CMN_OBJTYPE | ATTR_CMN_OBJID | ATTR_CMN_BKUPTIME);
4200 my_count = 4;
4201 my_base = 0;
4202 my_err = getdirentriesattr( my_fd, &my_attrlist, &my_attr_buf[0], sizeof(my_attr_buf), &my_count,
4203 &my_base, &my_new_state, 0 );
4204 if ( my_err < 0 ) {
4205 printf( "getdirentriesattr call failed. got errno %d - %s. \n", errno, strerror( errno ) );
4206 goto test_failed_exit;
4207 }
4208
4209 found_it = 0;
4210 for ( i = 0; i < my_count; i++ ) {
4211 if ( my_attr_buf[i].obj_id.fid_objno == my_obj_id.fid_objno &&
4212 my_attr_buf[i].obj_id.fid_generation == my_obj_id.fid_generation ) {
4213 found_it = 1;
4214 if ( my_attr_buf[i].backup_time.tv_sec != my_new_backup_time.tv_sec ) {
4215 printf( "setattrlist failed to set backup time. \n" );
4216 goto test_failed_exit;
4217 }
4218 }
4219 }
4220 if ( found_it == 0 ) {
4221 printf( "getdirentriesattr failed to find test file. \n" );
4222 goto test_failed_exit;
4223 }
4224
4225 my_err = 0;
4226 goto test_passed_exit;
4227
4228test_failed_exit:
cf7d32b8
A
4229 if(my_err != 0)
4230 my_err = -1;
2d21ac55
A
4231
4232test_passed_exit:
4233 if ( my_fd != -1 )
4234 close( my_fd );
4235 if ( my_pathp != NULL ) {
b0d623f7
A
4236 remove( my_pathp );
4237 vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
2d21ac55
A
4238 }
4239 if ( my_bufp != NULL ) {
b0d623f7 4240 vm_deallocate(mach_task_self(), (vm_address_t)my_bufp, (1024 * 5));
2d21ac55
A
4241 }
4242 return( my_err );
4243}
4244
4245/* **************************************************************************************************************
4246 * Test exchangedata system calls.
4247 * **************************************************************************************************************
4248 */
4249int exchangedata_test( void * the_argp )
4250{
4251 int my_err;
4252 int my_fd1 = -1;
4253 int my_fd2 = -1;
4254 char * my_file1_pathp = NULL;
4255 char * my_file2_pathp = NULL;
4256 ssize_t my_result;
4257 char my_buffer[16];
4258 struct statfs my_statfs_buf;
b0d623f7 4259 kern_return_t my_kr;
2d21ac55
A
4260
4261 /* need to know type of file system */
4262 my_err = statfs( &g_target_path[0], &my_statfs_buf );
4263 if ( my_err == -1 ) {
4264 printf( "statfs call failed. got errno %d - %s. \n", errno, strerror( errno ) );
4265 goto test_failed_exit;
4266 }
4267 if ( memcmp( &my_statfs_buf.f_fstypename[0], "ufs", 3 ) == 0 ) {
4268 /* ufs does not support exchangedata */
4269 my_err = 0;
4270 goto test_passed_exit;
4271 }
4272
b0d623f7
A
4273 my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_file1_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
4274 if(my_kr != KERN_SUCCESS){
4275 printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
4276 goto test_failed_exit;
4277 }
4278
2d21ac55
A
4279 *my_file1_pathp = 0x00;
4280 strcat( my_file1_pathp, &g_target_path[0] );
4281 strcat( my_file1_pathp, "/" );
4282
4283 /* create a test file */
4284 my_err = create_random_name( my_file1_pathp, 1 );
4285 if ( my_err != 0 ) {
cf7d32b8 4286 printf( "create_random_name my_err: %d\n", my_err );
2d21ac55
A
4287 goto test_failed_exit;
4288 }
4289 my_fd1 = open( my_file1_pathp, O_RDWR, 0 );
4290 if ( my_fd1 == -1 ) {
4291 printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
4292 goto test_failed_exit;
4293 }
4294 my_result = write( my_fd1, "11111111", 8 );
4295 if ( my_result == -1 ) {
4296 printf( "write call failed with error %d - \"%s\" \n", errno, strerror( errno) );
4297 goto test_failed_exit;
4298 }
4299
b0d623f7
A
4300 my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_file2_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
4301 if(my_kr != KERN_SUCCESS){
4302 printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
4303 goto test_failed_exit;
4304 }
4305
2d21ac55
A
4306 *my_file2_pathp = 0x00;
4307 strcat( my_file2_pathp, &g_target_path[0] );
4308 strcat( my_file2_pathp, "/" );
4309
4310 /* create a test file */
4311 my_err = create_random_name( my_file2_pathp, 1 );
4312 if ( my_err != 0 ) {
cf7d32b8 4313 printf( "create_random_name my_err: %d\n", my_err );
2d21ac55
A
4314 goto test_failed_exit;
4315 }
4316 my_fd2 = open( my_file2_pathp, O_RDWR, 0 );
4317 if ( my_fd2 == -1 ) {
4318 printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
4319 goto test_failed_exit;
4320 }
4321 my_result = write( my_fd2, "22222222", 8 );
4322 if ( my_result == -1 ) {
4323 printf( "write call failed with error %d - \"%s\" \n", errno, strerror( errno) );
4324 goto test_failed_exit;
4325 }
4326 close(my_fd1);
4327 my_fd1 = -1;
4328 close(my_fd2);
4329 my_fd2 = -1;
4330
4331 /* test exchangedata */
4332 my_err = exchangedata( my_file1_pathp, my_file2_pathp, 0 );
4333 if ( my_err == -1 ) {
4334 printf( "exchangedata failed with error %d - \"%s\" \n", errno, strerror( errno) );
4335 goto test_failed_exit;
4336 }
4337
4338 /* now validate exchange */
4339 my_fd1 = open( my_file1_pathp, O_RDONLY, 0 );
4340 if ( my_fd1 == -1 ) {
4341 printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
4342 goto test_failed_exit;
4343 }
4344 bzero( (void *)&my_buffer[0], sizeof(my_buffer) );
4345 my_result = read( my_fd1, &my_buffer[0], 8 );
4346 if ( my_result == -1 ) {
4347 printf( "write call failed with error %d - \"%s\" \n", errno, strerror( errno) );
4348 goto test_failed_exit;
4349 }
4350
4351 if ( memcmp( &my_buffer[0], "22222222", 8 ) != 0 ) {
4352 printf( "exchangedata failed - incorrect data in file \n" );
4353 goto test_failed_exit;
4354 }
4355
4356 my_err = 0;
4357 goto test_passed_exit;
4358
4359test_failed_exit:
4360 my_err = -1;
4361
4362test_passed_exit:
4363 if ( my_fd1 != -1 )
4364 close( my_fd1 );
4365 if ( my_file1_pathp != NULL ) {
b0d623f7
A
4366 remove( my_file1_pathp );
4367 vm_deallocate(mach_task_self(), (vm_address_t)my_file1_pathp, PATH_MAX);
2d21ac55
A
4368 }
4369 if ( my_fd2 != -1 )
4370 close( my_fd2 );
4371 if ( my_file2_pathp != NULL ) {
b0d623f7
A
4372 remove( my_file2_pathp );
4373 vm_deallocate(mach_task_self(), (vm_address_t)my_file2_pathp, PATH_MAX);
2d21ac55
A
4374 }
4375 return( my_err );
4376}
4377
4378
4379/* **************************************************************************************************************
4380 * Test searchfs system calls.
4381 * **************************************************************************************************************
4382 */
4383
4384struct packed_name_attr {
4385 u_int32_t size; /* Of the remaining fields */
4386 struct attrreference ref; /* Offset/length of name itself */
4387 char name[ PATH_MAX ];
4388};
4389
4390struct packed_attr_ref {
4391 u_int32_t size; /* Of the remaining fields */
4392 struct attrreference ref; /* Offset/length of attr itself */
4393};
4394
4395struct packed_result {
4396 u_int32_t size; /* Including size field itself */
4397 attrreference_t obj_name;
4398 struct fsobj_id obj_id;
4399 struct timespec obj_create_time;
4400 char room_for_name[ 64 ];
4401};
4402typedef struct packed_result packed_result;
4403typedef struct packed_result * packed_result_p;
4404
4405#define MAX_MATCHES 10
b0d623f7 4406#define MAX_EBUSY_RETRIES 5
2d21ac55
A
4407
4408int searchfs_test( void * the_argp )
4409{
b0d623f7 4410 int my_err, my_items_found = 0, my_ebusy_count;
2d21ac55
A
4411 char * my_pathp = NULL;
4412 unsigned long my_matches;
4413 unsigned long my_search_options;
4414 struct fssearchblock my_search_blk;
4415 struct attrlist my_return_list;
4416 struct searchstate my_search_state;
4417 struct packed_name_attr my_info1;
4418 struct packed_attr_ref my_info2;
4419 packed_result my_result_buffer[ MAX_MATCHES ];
4420 struct statfs my_statfs_buf;
b0d623f7 4421 kern_return_t my_kr;
2d21ac55
A
4422
4423 /* need to know type of file system */
4424 my_err = statfs( &g_target_path[0], &my_statfs_buf );
4425 if ( my_err == -1 ) {
4426 printf( "statfs call failed. got errno %d - %s. \n", errno, strerror( errno ) );
4427 goto test_failed_exit;
4428 }
4429 if ( memcmp( &my_statfs_buf.f_fstypename[0], "ufs", 3 ) == 0 ) {
4430 /* ufs does not support exchangedata */
4431 my_err = 0;
4432 goto test_passed_exit;
4433 }
4434
b0d623f7
A
4435 my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
4436 if(my_kr != KERN_SUCCESS){
4437 printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
4438 goto test_failed_exit;
4439 }
4440
2d21ac55
A
4441 *my_pathp = 0x00;
4442 strcat( my_pathp, &g_target_path[0] );
4443 strcat( my_pathp, "/" );
4444
4445 /* create test files */
4446 my_err = create_file_with_name( my_pathp, "foo", 0 );
4447 if ( my_err < 0 ) {
4448 printf( "failed to create a test file name in \"%s\" \n", my_pathp );
4449 goto test_failed_exit;
4450 }
4451
4452 my_err = create_file_with_name( my_pathp, "foobar", 0 );
4453 if ( my_err < 0 ) {
4454 printf( "failed to create a test file name in \"%s\" \n", my_pathp );
4455 goto test_failed_exit;
4456 }
4457
4458 my_err = create_file_with_name( my_pathp, "foofoo", 0 );
4459 if ( my_err < 0 ) {
4460 printf( "failed to create a test file name in \"%s\" \n", my_pathp );
4461 goto test_failed_exit;
4462 }
4463
4464 my_err = create_file_with_name( my_pathp, "xxxfoo", 0 );
4465 if ( my_err < 0 ) {
4466 printf( "failed to create a test file name in \"%s\" \n", my_pathp );
4467 goto test_failed_exit;
4468 }
b0d623f7
A
4469
4470 /* EBUSY count updated below the catalogue_changed label */
4471 my_ebusy_count = 0;
4472
4473catalogue_changed:
2d21ac55
A
4474 /* search target volume for all file system objects with "foo" in the name */
4475 /* Set up the attributes we're searching on. */
b0d623f7 4476 my_items_found = 0; /* Set this here in case we're completely restarting */
2d21ac55
A
4477 my_search_blk.searchattrs.bitmapcount = ATTR_BIT_MAP_COUNT;
4478 my_search_blk.searchattrs.reserved = 0;
4479 my_search_blk.searchattrs.commonattr = ATTR_CMN_NAME;
4480 my_search_blk.searchattrs.volattr = 0;
4481 my_search_blk.searchattrs.dirattr = 0;
4482 my_search_blk.searchattrs.fileattr = 0;
4483 my_search_blk.searchattrs.forkattr = 0;
4484
4485 /* Set up the attributes we want for all returned matches. */
4486 /* Why is returnattrs a pointer instead of an embedded struct? */
4487 my_search_blk.returnattrs = &my_return_list;
4488 my_return_list.bitmapcount = ATTR_BIT_MAP_COUNT;
4489 my_return_list.reserved = 0;
4490 my_return_list.commonattr = ATTR_CMN_NAME | ATTR_CMN_OBJID | ATTR_CMN_CRTIME;
4491 my_return_list.volattr = 0;
4492 my_return_list.dirattr = 0;
4493 my_return_list.fileattr = 0;
4494 my_return_list.forkattr = 0;
4495
4496 /* Allocate a buffer for returned matches */
4497 my_search_blk.returnbuffer = my_result_buffer;
4498 my_search_blk.returnbuffersize = sizeof(my_result_buffer);
4499
4500 /* Pack the searchparams1 into a buffer */
4501 /* NOTE: A name appears only in searchparams1 */
4502 strcpy( my_info1.name, "foo" );
4503 my_info1.ref.attr_dataoffset = sizeof(struct attrreference);
4504 my_info1.ref.attr_length = strlen(my_info1.name) + 1;
4505 my_info1.size = sizeof(struct attrreference) + my_info1.ref.attr_length;
4506 my_search_blk.searchparams1 = &my_info1;
4507 my_search_blk.sizeofsearchparams1 = my_info1.size + sizeof(u_int32_t);
4508
4509 /* Pack the searchparams2 into a buffer */
4510 my_info2.size = sizeof(struct attrreference);
4511 my_info2.ref.attr_dataoffset = sizeof(struct attrreference);
4512 my_info2.ref.attr_length = 0;
4513 my_search_blk.searchparams2 = &my_info2;
4514 my_search_blk.sizeofsearchparams2 = sizeof(my_info2);
4515
4516 /* Maximum number of matches we want */
4517 my_search_blk.maxmatches = MAX_MATCHES;
4518
4519 /* Maximum time to search, per call */
4520 my_search_blk.timelimit.tv_sec = 1;
4521 my_search_blk.timelimit.tv_usec = 0;
4522
4523 my_search_options = (SRCHFS_START | SRCHFS_MATCHPARTIALNAMES |
4524 SRCHFS_MATCHFILES | SRCHFS_MATCHDIRS);
4525 do {
4526 char * my_end_ptr;
4527 char * my_ptr;
4528 int i;
4529
4530 my_err = searchfs( my_pathp, &my_search_blk, &my_matches, 0, my_search_options, &my_search_state );
4531 if ( my_err == -1 )
4532 my_err = errno;
4533 if ( (my_err == 0 || my_err == EAGAIN) && my_matches > 0 ) {
4534 /* Unpack the results */
4535 // printf("my_matches %d \n", my_matches);
4536 my_ptr = (char *) &my_result_buffer[0];
4537 my_end_ptr = (my_ptr + sizeof(my_result_buffer));
4538 for ( i = 0; i < my_matches; ++i ) {
4539 packed_result_p my_result_p = (packed_result_p) my_ptr;
4540 char * my_name_p;
4541
4542 /* see if we foound all our test files */
4543 my_name_p = (((char *)(&my_result_p->obj_name)) + my_result_p->obj_name.attr_dataoffset);
4544 if ( memcmp( my_name_p, "foo", 3 ) == 0 ||
4545 memcmp( my_name_p, "foobar", 6 ) == 0 ||
4546 memcmp( my_name_p, "foofoo", 6 ) == 0 ||
4547 memcmp( my_name_p, "xxxfoo", 6 ) == 0 ) {
4548 my_items_found++;
4549 }
4550#if DEBUG
4551 printf("obj_name \"%.*s\" \n",
4552 (int) my_result_p->obj_name.attr_length,
4553 (((char *)(&my_result_p->obj_name)) +
4554 my_result_p->obj_name.attr_dataoffset));
4555 printf("size %d fid_objno %d fid_generation %d tv_sec 0x%02LX \n",
4556 my_result_p->size, my_result_p->obj_id.fid_objno,
4557 my_result_p->obj_id.fid_generation,
4558 my_result_p->obj_create_time.tv_sec);
4559#endif
4560 my_ptr = (my_ptr + my_result_p->size);
4561 if (my_ptr > my_end_ptr)
4562 break;
4563 }
4564 }
b0d623f7
A
4565
4566 /* EBUSY indicates catalogue change; retry a few times. */
4567 if ((my_err == EBUSY) && (my_ebusy_count++ < MAX_EBUSY_RETRIES)) {
4568 goto catalogue_changed;
4569 }
2d21ac55
A
4570 if ( !(my_err == 0 || my_err == EAGAIN) ) {
4571 printf( "searchfs failed with error %d - \"%s\" \n", my_err, strerror( my_err) );
4572 }
4573 my_search_options &= ~SRCHFS_START;
4574 } while ( my_err == EAGAIN );
4575
4576 if ( my_items_found < 4 ) {
4577 printf( "searchfs failed to find all test files \n" );
4578 goto test_failed_exit;
4579 }
4580
4581 my_err = 0;
4582 goto test_passed_exit;
4583
4584test_failed_exit:
4585 my_err = -1;
4586
4587test_passed_exit:
4588 if ( my_pathp != NULL ) {
4589 char * my_ptr = (my_pathp + strlen( my_pathp ));
4590 strcat( my_pathp, "foo" );
4591 remove( my_pathp );
4592 *my_ptr = 0x00;
4593 strcat( my_pathp, "foobar" );
4594 remove( my_pathp );
4595 *my_ptr = 0x00;
4596 strcat( my_pathp, "foofoo" );
4597 remove( my_pathp );
4598 *my_ptr = 0x00;
4599 strcat( my_pathp, "xxxfoo" );
b0d623f7
A
4600 remove( my_pathp );
4601 vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
2d21ac55
A
4602 }
4603 return( my_err );
4604}
4605
4606
4607#define AIO_TESTS_BUFFER_SIZE (1024 * 4000)
4608#define AIO_TESTS_OUR_COUNT 5
4609/* **************************************************************************************************************
4610 * Test aio_error, aio_read, aio_return, aio_suspend, aio_write, fcntl system calls.
4611 * **************************************************************************************************************
4612 */
4613int aio_tests( void * the_argp )
4614{
cf7d32b8 4615#if !TARGET_OS_EMBEDDED
2d21ac55
A
4616 int my_err, i;
4617 char * my_pathp;
4618 struct aiocb * my_aiocbp;
4619 ssize_t my_result;
4620 struct timespec my_timeout;
4621 int my_fd_list[ AIO_TESTS_OUR_COUNT ];
4622 char * my_buffers[ AIO_TESTS_OUR_COUNT ];
4623 struct aiocb * my_aiocb_list[ AIO_TESTS_OUR_COUNT ];
4624 struct aiocb my_aiocbs[ AIO_TESTS_OUR_COUNT ];
4625 char * my_file_paths[ AIO_TESTS_OUR_COUNT ];
b0d623f7 4626 kern_return_t my_kr;
2d21ac55
A
4627
4628 /* set up to have the ability to fire off up to AIO_TESTS_OUR_COUNT async IOs at once */
4629 memset( &my_fd_list[0], 0xFF, sizeof( my_fd_list ) );
4630 memset( &my_buffers[0], 0x00, sizeof( my_buffers ) );
4631 memset( &my_aiocb_list[0], 0x00, sizeof( my_aiocb_list ) );
4632 memset( &my_file_paths[0], 0x00, sizeof( my_file_paths ) );
4633 for ( i = 0; i < AIO_TESTS_OUR_COUNT; i++ ) {
b0d623f7
A
4634 my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_buffers[ i ], AIO_TESTS_BUFFER_SIZE, VM_FLAGS_ANYWHERE);
4635 if(my_kr != KERN_SUCCESS){
4636 printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
4637 goto test_failed_exit;
4638 }
4639
4640 my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_file_paths[ i ], PATH_MAX, VM_FLAGS_ANYWHERE);
4641 if(my_kr != KERN_SUCCESS){
4642 printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
4643 goto test_failed_exit;
4644 }
2d21ac55 4645
2d21ac55
A
4646 my_pathp = my_file_paths[ i ];
4647 *my_pathp = 0x00;
4648 strcat( my_pathp, &g_target_path[0] );
4649 strcat( my_pathp, "/" );
4650
4651 /* create a test file */
4652 my_err = create_random_name( my_pathp, 1 );
4653 if ( my_err != 0 ) {
4654 goto test_failed_exit;
4655 }
4656 my_fd_list[ i ] = open( my_pathp, O_RDWR, 0 );
4657 if ( my_fd_list[ i ] <= 0 ) {
4658 printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
4659 goto test_failed_exit;
4660 }
4661
4662 my_aiocbp = &my_aiocbs[ i ];
4663 my_aiocb_list[ i ] = my_aiocbp;
4664 memset( my_aiocbp, 0x00, sizeof( *my_aiocbp ) );
4665 my_aiocbp->aio_fildes = my_fd_list[ i ];
4666 my_aiocbp->aio_buf = (char *) my_buffers[ i ];
4667 my_aiocbp->aio_nbytes = 1024;
4668 my_aiocbp->aio_sigevent.sigev_notify = SIGEV_NONE; // no signals at completion;
4669 my_aiocbp->aio_sigevent.sigev_signo = 0;
4670 }
4671
4672 /* test direct IO (F_NOCACHE) and aio_write */
4673 my_err = fcntl( my_fd_list[ 0 ], F_NOCACHE, 1 );
4674 if ( my_err != 0 ) {
4675 printf( "malloc failed with error %d - \"%s\" \n", errno, strerror( errno) );
4676 goto test_failed_exit;
4677 }
4678
4679 my_aiocbp = &my_aiocbs[ 0 ];
4680 my_aiocbp->aio_fildes = my_fd_list[ 0 ];
4a3eedf9 4681 my_aiocbp->aio_offset = 4096;
2d21ac55
A
4682 my_aiocbp->aio_buf = my_buffers[ 0 ];
4683 my_aiocbp->aio_nbytes = AIO_TESTS_BUFFER_SIZE;
4684 my_aiocbp->aio_reqprio = 0;
4685 my_aiocbp->aio_sigevent.sigev_notify = 0;
4686 my_aiocbp->aio_sigevent.sigev_signo = 0;
4687 my_aiocbp->aio_sigevent.sigev_value.sival_int = 0;
4688 my_aiocbp->aio_sigevent.sigev_notify_function = NULL;
4689 my_aiocbp->aio_sigevent.sigev_notify_attributes = NULL;
4690 my_aiocbp->aio_lio_opcode = 0;
4691
4692 /* write some data */
4693 memset( my_buffers[ 0 ], 'j', AIO_TESTS_BUFFER_SIZE );
4694 my_err = aio_write( my_aiocbp );
4695 if ( my_err != 0 ) {
4696 printf( "aio_write failed with error %d - \"%s\" \n", my_err, strerror( my_err) );
4697 goto test_failed_exit;
4698 }
4699
4700 while ( 1 ) {
4701 my_err = aio_error( my_aiocbp );
4702 if ( my_err == EINPROGRESS ) {
4703 /* wait for IO to complete */
4704 sleep( 1 );
4705 continue;
4706 }
4707 else if ( my_err == 0 ) {
4708 ssize_t my_result;
4709 my_result = aio_return( my_aiocbp );
4710 break;
4711 }
4712 else {
4713 printf( "aio_error failed with error %d - \"%s\" \n", my_err, strerror( my_err ) );
4714 goto test_failed_exit;
4715 }
4716 } /* while loop */
4717
4718 /* read some data */
4719 memset( my_buffers[ 0 ], 'x', AIO_TESTS_BUFFER_SIZE );
4720 my_err = aio_read( my_aiocbp );
4721
4722 while ( 1 ) {
4723 my_err = aio_error( my_aiocbp );
4724 if ( my_err == EINPROGRESS ) {
4725 /* wait for IO to complete */
4726 sleep( 1 );
4727 continue;
4728 }
4729 else if ( my_err == 0 ) {
4730 ssize_t my_result;
4731 my_result = aio_return( my_aiocbp );
4732
4733 if ( *(my_buffers[ 0 ]) != 'j' || *(my_buffers[ 0 ] + AIO_TESTS_BUFFER_SIZE - 1) != 'j' ) {
4734 printf( "aio_read or aio_write failed - wrong data read \n" );
4735 goto test_failed_exit;
4736 }
4737 break;
4738 }
4739 else {
4740 printf( "aio_read failed with error %d - \"%s\" \n", my_err, strerror( my_err ) );
4741 goto test_failed_exit;
4742 }
4743 } /* while loop */
4744
4745 /* test aio_fsync */
4746 close( my_fd_list[ 0 ] );
4747 my_fd_list[ 0 ] = open( my_pathp, O_RDWR, 0 );
4748 if ( my_fd_list[ 0 ] == -1 ) {
4749 printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) );
4750 goto test_failed_exit;
4751 }
4752
4753 my_aiocbp = &my_aiocbs[ 0 ];
4754 my_aiocbp->aio_fildes = my_fd_list[ 0 ];
4755 my_aiocbp->aio_offset = 0;
4756 my_aiocbp->aio_buf = my_buffers[ 0 ];
4757 my_aiocbp->aio_nbytes = 1024;
4758 my_aiocbp->aio_reqprio = 0;
4759 my_aiocbp->aio_sigevent.sigev_notify = 0;
4760 my_aiocbp->aio_sigevent.sigev_signo = 0;
4761 my_aiocbp->aio_sigevent.sigev_value.sival_int = 0;
4762 my_aiocbp->aio_sigevent.sigev_notify_function = NULL;
4763 my_aiocbp->aio_sigevent.sigev_notify_attributes = NULL;
4764 my_aiocbp->aio_lio_opcode = 0;
4765
4766 /* write some data */
4767 memset( my_buffers[ 0 ], 'e', 1024 );
4768 my_err = aio_write( my_aiocbp );
4769 if ( my_err != 0 ) {
4770 printf( "aio_write failed with error %d - \"%s\" \n", my_err, strerror( my_err) );
4771 goto test_failed_exit;
4772 }
4773 while ( 1 ) {
4774 my_err = aio_error( my_aiocbp );
4775 if ( my_err == EINPROGRESS ) {
4776 /* wait for IO to complete */
4777 sleep( 1 );
4778 continue;
4779 }
4780 else if ( my_err == 0 ) {
4781 ssize_t my_result;
4782 my_result = aio_return( my_aiocbp );
4783 break;
4784 }
4785 else {
4786 printf( "aio_error failed with error %d - \"%s\" \n", my_err, strerror( my_err ) );
4787 goto test_failed_exit;
4788 }
4789 } /* while loop */
4790
4791 my_err = aio_fsync( O_SYNC, my_aiocbp );
4792 if ( my_err != 0 ) {
4793 printf( "aio_fsync failed with error %d - \"%s\" \n", my_err, strerror( my_err) );
4794 goto test_failed_exit;
4795 }
4796 while ( 1 ) {
4797 my_err = aio_error( my_aiocbp );
4798 if ( my_err == EINPROGRESS ) {
4799 /* wait for IO to complete */
4800 sleep( 1 );
4801 continue;
4802 }
4803 else if ( my_err == 0 ) {
4804 aio_return( my_aiocbp );
4805 break;
4806 }
4807 else {
4808 printf( "aio_error failed with error %d - \"%s\" \n", my_err, strerror( my_err ) );
4809 goto test_failed_exit;
4810 }
4811 } /* while loop */
4812
4813 /* validate write */
4814 memset( my_buffers[ 0 ], 0x20, 16 );
4815 lseek( my_fd_list[ 0 ], 0, SEEK_SET );
4816 my_result = read( my_fd_list[ 0 ], my_buffers[ 0 ], 16);
4817 if ( my_result == -1 ) {
4818 printf( "read call failed with error %d - \"%s\" \n", errno, strerror( errno) );
4819 goto test_failed_exit;
4820 }
4821 if ( *(my_buffers[ 0 ]) != 'e' || *(my_buffers[ 0 ] + 16 - 1) != 'e' ) {
4822 printf( "aio_fsync or aio_write failed - wrong data read \n" );
4823 goto test_failed_exit;
4824 }
4825
4826 /* test aio_suspend and lio_listio */
4827 for ( i = 0; i < AIO_TESTS_OUR_COUNT; i++ ) {
4828 memset( my_buffers[ i ], 'a', AIO_TESTS_BUFFER_SIZE );
4829 my_aiocbp = &my_aiocbs[ i ];
4830 my_aiocbp->aio_nbytes = AIO_TESTS_BUFFER_SIZE;
4831 my_aiocbp->aio_lio_opcode = LIO_WRITE;
4832 }
4833 my_err = lio_listio( LIO_NOWAIT, my_aiocb_list, AIO_TESTS_OUR_COUNT, NULL );
4834 if ( my_err != 0 ) {
4835 printf( "lio_listio call failed with error %d - \"%s\" \n", errno, strerror( errno) );
4836 goto test_failed_exit;
4837 }
4838
4839 my_timeout.tv_sec = 1;
4840 my_timeout.tv_nsec = 0;
4841 my_err = aio_suspend( (const struct aiocb *const*) my_aiocb_list, AIO_TESTS_OUR_COUNT, &my_timeout );
4842 if ( my_err != 0 ) {
4843 printf( "aio_suspend call failed with error %d - \"%s\" \n", errno, strerror( errno) );
4844 goto test_failed_exit;
4845 }
4846
4847 /* test aio_cancel */
4848 for ( i = 0; i < AIO_TESTS_OUR_COUNT; i++ ) {
4849 my_aiocbp = &my_aiocbs[ i ];
4850 my_err = aio_cancel( my_aiocbp->aio_fildes, my_aiocbp );
4851 if ( my_err != AIO_ALLDONE && my_err != AIO_CANCELED && my_err != AIO_NOTCANCELED ) {
4852 printf( "aio_cancel failed with error %d - \"%s\" \n", my_err, strerror( my_err) );
4853 goto test_failed_exit;
4854 }
4855 }
4856
4857 my_err = 0;
4858 goto test_passed_exit;
4859
4860test_failed_exit:
4861 my_err = -1;
4862
4863test_passed_exit:
4864 for ( i = 0; i < AIO_TESTS_OUR_COUNT; i++ ) {
4865 if ( my_fd_list[ i ] != -1 ) {
4866 close( my_fd_list[ i ] );
4867 my_fd_list[ i ] = -1;
4868 }
4869 if ( my_file_paths[ i ] != NULL ) {
b0d623f7
A
4870 remove( my_file_paths[ i ] );
4871 vm_deallocate(mach_task_self(), (vm_address_t)my_file_paths[ i ], PATH_MAX);
2d21ac55
A
4872 my_file_paths[ i ] = NULL;
4873 }
4874 if ( my_buffers[ i ] != NULL ) {
b0d623f7 4875 vm_deallocate(mach_task_self(), (vm_address_t)my_buffers[ i ], AIO_TESTS_BUFFER_SIZE);
2d21ac55
A
4876 my_buffers[ i ] = NULL;
4877 }
4878 }
4879 return( my_err );
cf7d32b8
A
4880#else
4881 printf( "\t--> Not supported on EMBEDDED TARGET\n" );
4882 return 0;
4883#endif
2d21ac55
A
4884}
4885
4886
2d21ac55
A
4887/* **************************************************************************************************************
4888 * Test msgctl, msgget, msgrcv, msgsnd system calls.
4889 * **************************************************************************************************************
4890 */
4891int message_queue_tests( void * the_argp )
4892{
cf7d32b8 4893#if !TARGET_OS_EMBEDDED
2d21ac55
A
4894 int my_err;
4895 int my_msg_queue_id = -1;
4896 ssize_t my_result;
4897 struct msqid_ds my_msq_ds;
4898 struct testing_msq_message {
4899 long msq_type;
4900 char msq_buffer[ 32 ];
4901 } my_msg;
4902
4903 /* get a message queue established for our use */
4904 my_msg_queue_id = msgget( IPC_PRIVATE, (IPC_CREAT | IPC_EXCL | IPC_R | IPC_W) );
4905 if ( my_msg_queue_id == -1 ) {
4906 printf( "msgget failed with errno %d - %s \n", errno, strerror( errno ) );
4907 goto test_failed_exit;
4908 }
4909
4910 /* get some stats on our message queue */
4911 my_err = msgctl( my_msg_queue_id, IPC_STAT, &my_msq_ds );
4912 if ( my_err == -1 ) {
4913 printf( "msgctl failed with errno %d - %s \n", errno, strerror( errno ) );
4914 goto test_failed_exit;
4915 }
4916 if ( my_msq_ds.msg_perm.cuid != geteuid( ) ) {
4917 printf( "msgctl IPC_STAT failed to get correct creator uid \n" );
4918 goto test_failed_exit;
4919 }
4920 if ( (my_msq_ds.msg_perm.mode & (IPC_R | IPC_W)) == 0 ) {
4921 printf( "msgctl IPC_STAT failed to get correct mode \n" );
4922 goto test_failed_exit;
4923 }
4924
4925 /* put a message into our queue */
4926 my_msg.msq_type = 1;
4927 strcpy( &my_msg.msq_buffer[ 0 ], "testing 1, 2, 3" );
4928 my_err = msgsnd( my_msg_queue_id, &my_msg, sizeof( my_msg.msq_buffer ), 0 );
4929 if ( my_err == -1 ) {
4930 printf( "msgsnd failed with errno %d - %s \n", errno, strerror( errno ) );
4931 goto test_failed_exit;
4932 }
4933
4934 my_err = msgctl( my_msg_queue_id, IPC_STAT, &my_msq_ds );
4935 if ( my_err == -1 ) {
4936 printf( "msgctl failed with errno %d - %s \n", errno, strerror( errno ) );
4937 goto test_failed_exit;
4938 }
4939 if ( my_msq_ds.msg_qnum != 1 ) {
4940 printf( "msgctl IPC_STAT failed to get correct number of messages on the queue \n" );
4941 goto test_failed_exit;
4942 }
4943
4944 /* pull message off the queue */
4945 bzero( (void *)&my_msg, sizeof( my_msg ) );
4946 my_result = msgrcv( my_msg_queue_id, &my_msg, sizeof( my_msg.msq_buffer ), 0, 0 );
4947 if ( my_result == -1 ) {
4948 printf( "msgrcv failed with errno %d - %s \n", errno, strerror( errno ) );
4949 goto test_failed_exit;
4950 }
4951 if ( my_result != sizeof( my_msg.msq_buffer ) ) {
4952 printf( "msgrcv failed to return the correct number of bytes in our buffer \n" );
4953 goto test_failed_exit;
4954 }
4955 if ( strcmp( &my_msg.msq_buffer[ 0 ], "testing 1, 2, 3" ) != 0 ) {
4956 printf( "msgrcv failed to get the correct message \n" );
4957 goto test_failed_exit;
4958 }
4959
4960 my_err = msgctl( my_msg_queue_id, IPC_STAT, &my_msq_ds );
4961 if ( my_err == -1 ) {
4962 printf( "msgctl failed with errno %d - %s \n", errno, strerror( errno ) );
4963 goto test_failed_exit;
4964 }
4965 if ( my_msq_ds.msg_qnum != 0 ) {
4966 printf( "msgctl IPC_STAT failed to get correct number of messages on the queue \n" );
4967 goto test_failed_exit;
4968 }
4969
4970 /* tear down the message queue */
4971 my_err = msgctl( my_msg_queue_id, IPC_RMID, NULL );
4972 if ( my_err == -1 ) {
4973 printf( "msgctl IPC_RMID failed with errno %d - %s \n", errno, strerror( errno ) );
4974 goto test_failed_exit;
4975 }
4976 my_msg_queue_id = -1;
4977
4978 my_err = 0;
4979 goto test_passed_exit;
4980
4981test_failed_exit:
4982 my_err = -1;
4983
4984test_passed_exit:
4985 if ( my_msg_queue_id != -1 ) {
4986 msgctl( my_msg_queue_id, IPC_RMID, NULL );
4987 }
4988 return( my_err );
cf7d32b8
A
4989#else
4990 printf( "\t--> Not supported on EMBEDDED TARGET \n" );
4991 return 0;
4992#endif
2d21ac55
A
4993}
4994
4995
b0d623f7
A
4996/* **************************************************************************************************************
4997 * Test execution from data and stack areas.
4998 * **************************************************************************************************************
4999 */
5000int data_exec_tests( void * the_argp )
5001{
5002 int my_err = 0;
5003 int arch, bits;
5004
5005 if ((arch = get_architecture()) == -1) {
5006 printf("data_exec_test: couldn't determine architecture\n");
5007 goto test_failed_exit;
5008 }
5009
5010 bits = get_bits();
5011
5012 /*
5013 * If the machine is 64-bit capable, run both the 32 and 64 bit versions of the test.
5014 * Otherwise, just run the 32-bit version.
5015 */
5016
5017 if (arch == INTEL) {
5018 if (bits == 64) {
5019 if (system("arch -arch x86_64 helpers/data_exec") != 0) {
5020 printf("data_exec-x86_64 failed\n");
5021 goto test_failed_exit;
5022 }
5023 }
5024
5025 if (system("arch -arch i386 helpers/data_exec") != 0) {
5026 printf("data_exec-i386 failed\n");
5027 goto test_failed_exit;
5028 }
5029 }
5030
5031 if (arch == POWERPC) {
5032 if (system("arch -arch ppc helpers/data_exec") != 0) {
5033 printf("data_exec-ppc failed\n");
5034 goto test_failed_exit;
5035 }
5036 }
5037
5038 /* Add new architectures here similar to the above. */
5039
5040 goto test_passed_exit;
5041
5042test_failed_exit:
5043 my_err = -1;
5044
5045test_passed_exit:
5046 return my_err;
5047}
5048
5049
2d21ac55
A
5050#if TEST_SYSTEM_CALLS
5051
5052/* **************************************************************************************************************
5053 * Test xxxxxxxxx system calls.
5054 * **************************************************************************************************************
5055 */
5056int sample_test( void * the_argp )
5057{
5058 int my_err;
5059 int my_fd = -1;
5060 char * my_pathp = NULL;
b0d623f7
A
5061 kern_return_t my_kr;
5062
5063 my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
5064 if(my_kr != KERN_SUCCESS){
5065 printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
5066 goto test_failed_exit;
5067 }
2d21ac55 5068
2d21ac55
A
5069 *my_pathp = 0x00;
5070 strcat( my_pathp, &g_target_path[0] );
5071 strcat( my_pathp, "/" );
5072
5073 /* create a test file */
5074 my_err = create_random_name( my_pathp, 1 );
5075 if ( my_err != 0 ) {
5076 goto test_failed_exit;
5077 }
5078
5079 /* add your test code here... */
5080
5081
5082 my_err = 0;
5083 goto test_passed_exit;
5084
5085test_failed_exit:
5086 my_err = -1;
5087
5088test_passed_exit:
5089 if ( my_fd != -1 )
5090 close( my_fd );
5091 if ( my_pathp != NULL ) {
b0d623f7
A
5092 remove( my_pathp );
5093 vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
2d21ac55
A
5094 }
5095 return( my_err );
5096}
5097
5098#endif