]>
Commit | Line | Data |
---|---|---|
2d21ac55 A |
1 | /* |
2 | * xnu_quick_test - this tool will do a quick test of every (well, to be | |
3 | * honest most) system calls we support in xnu. | |
4 | * | |
5 | * WARNING - this is not meant to be a full regression test of all the | |
6 | * system calls. The intent is to have a quick test of each system call that | |
7 | * can be run very easily and quickly when a new kerenl is built. | |
8 | * | |
9 | * This tool is meant to grow as we find xnu problems that could have be | |
10 | * caught before we submit to a build train. So please add more tests and | |
11 | * make the existing ones better. Some of the original tests are nothing | |
12 | * more than place holders and quite lame. Just keep in mind that the tool | |
13 | * should run as fast as possible. If it gets too slow then most people | |
14 | * will stop running it. | |
15 | * | |
16 | * LP64 testing tip - when adding or modifying tests, keep in mind the | |
17 | * variants in the LP64 world. If xnu gets passed a structure the varies in | |
18 | * size between 32 and 64-bit processes, try to test that a field in the | |
19 | * sructure contains valid data. For example if we know foo structure | |
20 | * looks like: | |
21 | * struct foo { | |
22 | * int an_int; | |
23 | * long a_long; | |
24 | * int another_int; | |
25 | * } | |
26 | * And if we know what another_int should contain then test for the known | |
27 | * value since it's offset will vary depending on whether the calling process | |
28 | * is 32 or 64 bits. | |
29 | * | |
30 | * NOTE - we have several workarounds and test exceptions for some | |
31 | * outstanding bugs in xnu. All the workarounds are marked with "todo" and | |
32 | * some comments noting the radar number of the offending bug. Do a seach | |
33 | * for "todo" in the source files for this project to locate which tests have | |
34 | * known failures. And please tag any new exceptions you find with "todo" | |
35 | * in the comment and the radar number of the bug. | |
36 | */ | |
37 | ||
38 | #include <errno.h> | |
39 | #include <fcntl.h> | |
40 | #include <stdio.h> | |
41 | #include <stdlib.h> | |
42 | #include <string.h> | |
43 | #include <time.h> | |
b0d623f7 | 44 | #include <grp.h> |
2d21ac55 A |
45 | #include <unistd.h> |
46 | #include <sys/mount.h> | |
47 | #include <sys/param.h> | |
48 | #include <sys/select.h> | |
49 | #include <sys/stat.h> | |
50 | #include <sys/syslimits.h> | |
51 | #include <sys/types.h> | |
52 | #include <sys/ucred.h> | |
53 | #include <sys/uio.h> | |
b0d623f7 A |
54 | #include <mach-o/ldsyms.h> |
55 | #include <mach-o/loader.h> | |
56 | #include <mach-o/arch.h> | |
cf7d32b8 A |
57 | #include "tests.h" |
58 | ||
59 | #if !TARGET_OS_EMBEDDED | |
2d21ac55 | 60 | #include <XILog/XILog.h> |
cf7d32b8 | 61 | #endif |
2d21ac55 | 62 | |
2d21ac55 A |
63 | |
64 | ||
65 | /* our table of tests to run */ | |
66 | struct test_entry g_tests[] = | |
67 | { | |
68 | {1, &syscall_test, NULL, "syscall"}, | |
69 | {1, &fork_wait4_exit_test, NULL, "fork, wait4, exit"}, | |
70 | {1, &read_write_test, NULL, "fsync, ftruncate, lseek, pread, pwrite, read, readv, truncate, write, writev"}, | |
71 | {1, &open_close_test, NULL, "close, fpathconf, fstat, open, pathconf"}, | |
72 | {1, &link_stat_unlink_test, NULL, "link, stat, unlink"}, | |
73 | {1, &chdir_fchdir_test, NULL, "chdir, fchdir"}, | |
74 | {1, &access_chmod_fchmod_test, NULL, "access, chmod, fchmod"}, | |
75 | {1, &chown_fchown_lchown_lstat_symlink_test, NULL, "chown, fchown, lchown, lstat, readlink, symlink"}, | |
b0d623f7 A |
76 | {1, &fs_stat_tests, NULL, "fstatfs, getfsstat, statfs, fstatfs64, getfsstat64, statfs64"}, |
77 | #if !TARGET_OS_EMBEDDED | |
78 | {1, &statfs_32bit_inode_tests, NULL, "32-bit inode versions: fstatfs, getfsstat, statfs"}, | |
79 | #endif | |
2d21ac55 | 80 | {1, &getpid_getppid_pipe_test, NULL, "getpid, getppid, pipe"}, |
b0d623f7 | 81 | {1, &uid_tests, NULL, "getauid, gettid, getuid, geteuid, issetugid, setaudit_addr, seteuid, settid, settid_with_pid, setuid"}, |
2d21ac55 A |
82 | {1, &mkdir_rmdir_umask_test, NULL, "mkdir, rmdir, umask"}, |
83 | {1, &mknod_sync_test, NULL, "mknod, sync"}, | |
84 | {1, &socket2_tests, NULL, "fsync, getsockopt, poll, select, setsockopt, socketpair"}, | |
b0d623f7 | 85 | {1, &socket_tests, NULL, "accept, bind, connect, getpeername, getsockname, listen, socket, recvmsg, sendmsg, sendto, sendfile"}, |
2d21ac55 A |
86 | {1, &chflags_fchflags_test, NULL, "chflags, fchflags"}, |
87 | {1, &execve_kill_vfork_test, NULL, "kill, vfork, execve, posix_spawn"}, | |
88 | {1, &groups_test, NULL, "getegid, getgid, getgroups, setegid, setgid, setgroups"}, | |
89 | {1, &dup_test, NULL, "dup, dup2, getdtablesize"}, | |
b0d623f7 | 90 | {1, &getrusage_test, NULL, "getrusage"}, |
2d21ac55 A |
91 | {1, &signals_test, NULL, "getitimer, setitimer, sigaction, sigpending, sigprocmask, sigsuspend, sigwait"}, |
92 | {1, &acct_test, NULL, "acct"}, | |
93 | {1, &ioctl_test, NULL, "ioctl"}, | |
94 | {1, &chroot_test, NULL, "chroot"}, | |
95 | {1, &memory_tests, NULL, "madvise, mincore, minherit, mlock, mlock, mmap, mprotect, msync, munmap"}, | |
96 | {1, &process_group_test, NULL, "getpgrp, getpgid, getsid, setpgid, setpgrp, setsid"}, | |
97 | {1, &fcntl_test, NULL, "fcntl"}, | |
98 | {1, &getlogin_setlogin_test, NULL, "getlogin, setlogin"}, | |
99 | {1, &getpriority_setpriority_test, NULL, "getpriority, setpriority"}, | |
100 | {1, &time_tests, NULL, "futimes, gettimeofday, settimeofday, utimes"}, | |
101 | {1, &rename_test, NULL, "rename, stat"}, | |
102 | {1, &locking_test, NULL, "flock"}, | |
103 | {1, &mkfifo_test, NULL, "mkfifo, read, write"}, | |
104 | {1, "actl_test, NULL, "quotactl"}, | |
105 | {1, &limit_tests, NULL, "getrlimit, setrlimit"}, | |
b0d623f7 A |
106 | {1, &directory_tests, NULL, "getattrlist, getdirentriesattr, setattrlist"}, |
107 | #if !TARGET_OS_EMBEDDED | |
108 | {1, &getdirentries_test, NULL, "getdirentries"}, | |
109 | #endif | |
2d21ac55 A |
110 | {1, &exchangedata_test, NULL, "exchangedata"}, |
111 | {1, &searchfs_test, NULL, "searchfs"}, | |
112 | {1, &sema2_tests, NULL, "sem_close, sem_open, sem_post, sem_trywait, sem_unlink, sem_wait"}, | |
113 | {1, &sema_tests, NULL, "semctl, semget, semop"}, | |
114 | {1, &bsd_shm_tests, NULL, "shm_open, shm_unlink"}, | |
115 | {1, &shm_tests, NULL, "shmat, shmctl, shmdt, shmget"}, | |
116 | {1, &xattr_tests, NULL, "fgetxattr, flistxattr, fremovexattr, fsetxattr, getxattr, listxattr, removexattr, setxattr"}, | |
117 | {1, &aio_tests, NULL, "aio_cancel, aio_error, aio_read, aio_return, aio_suspend, aio_write, fcntl, lio_listio"}, | |
118 | {1, &kqueue_tests, NULL, "kevent, kqueue"}, | |
119 | {1, &message_queue_tests, NULL, "msgctl, msgget, msgrcv, msgsnd"}, | |
b0d623f7 A |
120 | {1, &data_exec_tests, NULL, "data/stack execution"}, |
121 | {1, &machvm_tests, NULL, "Mach VM calls"}, | |
6d2010ae A |
122 | {1, &commpage_data_tests, NULL, "Commpage data"}, |
123 | #if defined(i386) || defined(__x86_64__) | |
124 | {1, &atomic_fifo_queue_test, NULL, "OSAtomicFifoEnqueue, OSAtomicFifoDequeue"}, | |
125 | #endif | |
126 | {1, &sched_tests, NULL, "Scheduler tests"}, | |
2d21ac55 A |
127 | {0, NULL, NULL, "last one"} |
128 | }; | |
129 | ||
130 | static void create_target_directory( const char * the_targetp ); | |
131 | static void list_all_tests( void ); | |
132 | static void mark_tests_to_run( long my_start, long my_end ); | |
133 | static int parse_tests_to_run( int argc, const char * argv[], int * indexp ); | |
134 | static void usage( void ); | |
b0d623f7 A |
135 | static int setgroups_if_single_user(void); |
136 | static const char *current_arch( void ); | |
2d21ac55 A |
137 | |
138 | /* globals */ | |
139 | long g_max_failures = 0; | |
140 | int g_skip_setuid_tests = 0; | |
141 | int g_xilog_active = 0; | |
142 | const char * g_cmd_namep; | |
143 | char g_target_path[ PATH_MAX ]; | |
b0d623f7 | 144 | int g_is_single_user = 0; |
6d2010ae | 145 | int g_testbots_active = 0; |
2d21ac55 A |
146 | |
147 | int main( int argc, const char * argv[] ) | |
148 | { | |
6d2010ae A |
149 | #pragma unused(argc) |
150 | #pragma unused(argv) | |
2d21ac55 A |
151 | int my_tests_count, i; |
152 | int err; | |
153 | int my_failures = 0; | |
154 | int list_the_tests = 0; | |
155 | const char * my_targetp; | |
156 | time_t my_start_time, my_end_time; | |
157 | struct stat my_stat_buf; | |
158 | char my_buffer[64]; | |
159 | /* vars for XILog */ | |
cf7d32b8 | 160 | #if !TARGET_OS_EMBEDDED |
2d21ac55 A |
161 | XILogRef logRef; |
162 | char *logPath = ""; | |
163 | char *config = NULL; | |
164 | int echo = 0; | |
165 | int xml = 0; | |
cf7d32b8 | 166 | #endif |
2d21ac55 A |
167 | sranddev( ); /* set up seed for our random name generator */ |
168 | g_cmd_namep = argv[0]; | |
169 | ||
170 | /* NOTE - code in create_target_directory will append '/' if it is necessary */ | |
171 | my_targetp = getenv("TMPDIR"); | |
172 | if ( my_targetp == NULL ) | |
173 | my_targetp = "/tmp"; | |
174 | ||
175 | /* make sure our executable is owned by root and has set uid bit */ | |
176 | err = stat( g_cmd_namep, &my_stat_buf ); | |
177 | if ( err != 0 ) { | |
178 | err = errno; | |
179 | printf( "stat call on our executable failed - \"%s\" \n", g_cmd_namep ); | |
180 | printf( " failed with error %d - \"%s\" \n", err, strerror( err) ); | |
181 | exit( -1 ); | |
182 | } | |
183 | if ( my_stat_buf.st_uid != 0 || (my_stat_buf.st_mode & S_ISUID) == 0 ) { | |
184 | printf( "executable file - \"%s\" \n", g_cmd_namep ); | |
185 | printf( "does not have correct owner (must be root) or setuid bit is not set \n" ); | |
186 | exit( -1 ); | |
187 | } | |
188 | ||
189 | /* parse input parameters */ | |
190 | for ( i = 1; i < argc; i++ ) { | |
191 | if ( strcmp( argv[i], "-u" ) == 0 ) { | |
192 | usage( ); | |
193 | } | |
194 | if ( strcmp( argv[i], "-t" ) == 0 || | |
195 | strcmp( argv[i], "-target" ) == 0 ) { | |
196 | if ( ++i >= argc ) { | |
197 | printf( "invalid target parameter \n" ); | |
198 | usage( ); | |
199 | } | |
200 | /* verify our target directory exists */ | |
201 | my_targetp = argv[i]; | |
202 | err = stat( my_targetp, &my_stat_buf ); | |
203 | if ( err != 0 || S_ISDIR(my_stat_buf.st_mode) == 0 ) { | |
204 | printf( "invalid target path \n" ); | |
205 | if ( err != 0 ) { | |
206 | printf( "stat call failed with error %d - \"%s\" \n", errno, strerror( errno) ); | |
207 | } | |
208 | usage( ); | |
209 | } | |
210 | continue; | |
211 | } | |
212 | if ( strcmp( argv[i], "-f" ) == 0 || | |
213 | strcmp( argv[i], "-failures" ) == 0 ) { | |
214 | if ( ++i >= argc ) { | |
215 | printf( "invalid failures parameter \n" ); | |
216 | usage( ); | |
217 | } | |
218 | ||
219 | /* get our max number of failures */ | |
220 | g_max_failures = strtol( argv[i], NULL, 10 ); | |
221 | continue; | |
222 | } | |
223 | if ( strcmp( argv[i], "-l" ) == 0 || | |
224 | strcmp( argv[i], "-list" ) == 0 ) { | |
225 | /* list all the tests this tool will do. | |
226 | */ | |
227 | list_the_tests = 1; | |
228 | continue; | |
229 | } | |
230 | if ( strcmp( argv[i], "-r" ) == 0 || | |
231 | strcmp( argv[i], "-run" ) == 0 ) { | |
232 | if ( ++i >= argc ) { | |
233 | printf( "invalid run tests parameter \n" ); | |
234 | usage( ); | |
235 | } | |
236 | ||
237 | /* get which tests to run */ | |
238 | if ( parse_tests_to_run( argc, argv, &i ) != 0 ) { | |
239 | printf( "invalid run tests parameter \n" ); | |
240 | usage( ); | |
241 | } | |
242 | continue; | |
243 | } | |
244 | if ( strcmp( argv[i], "-s" ) == 0 || | |
245 | strcmp( argv[i], "-skip" ) == 0 ) { | |
246 | /* set that want to skip the setuid related tests - this is useful for debgugging since since I can't | |
247 | * get setuid tests to work while in gdb. | |
248 | */ | |
249 | g_skip_setuid_tests = 1; | |
250 | continue; | |
251 | } | |
cf7d32b8 | 252 | #if !TARGET_OS_EMBEDDED |
2d21ac55 A |
253 | if ( strcmp( argv[i], "-x" ) == 0 || |
254 | strcmp( argv[i], "-xilog" ) == 0 ) { | |
255 | g_xilog_active = 1; | |
256 | continue; | |
257 | } | |
cf7d32b8 | 258 | #endif |
2d21ac55 A |
259 | printf( "invalid argument \"%s\" \n", argv[i] ); |
260 | usage( ); | |
261 | } | |
262 | ||
263 | /* done parsing. | |
264 | */ | |
b0d623f7 | 265 | |
6d2010ae A |
266 | /* Check if we are running under testbots */ |
267 | #if RUN_UNDER_TESTBOTS | |
268 | g_testbots_active = 1; | |
269 | #endif | |
270 | /* Code added to run xnu_quick_test under testbots */ | |
271 | if ( g_testbots_active == 1 ) { | |
272 | printf("[TEST] xnu_quick_test \n"); /* Declare the beginning of test suite */ | |
273 | } | |
274 | ||
b0d623f7 A |
275 | /* Populate groups list if we're in single user mode */ |
276 | if (setgroups_if_single_user()) { | |
277 | return 1; | |
278 | } | |
2d21ac55 A |
279 | |
280 | if ( list_the_tests != 0 ) { | |
281 | list_all_tests( ); | |
282 | return 0; | |
283 | } | |
cf7d32b8 | 284 | #if !TARGET_OS_EMBEDDED |
2d21ac55 A |
285 | if (g_xilog_active == 1) { |
286 | logRef = XILogOpenLogExtended( logPath, "xnu_quick_test", "com.apple.coreos", | |
287 | config, xml, echo, NULL, "ResultOwner", | |
288 | "com.apple.coreos", NULL ); | |
289 | if( logRef == NULL ) { | |
290 | fprintf(stderr,"Couldn't create log: %s",logPath); | |
291 | exit(-1); | |
292 | } | |
293 | } | |
cf7d32b8 | 294 | #endif |
2d21ac55 A |
295 | |
296 | /* build a test target directory that we use as our path to create any test | |
297 | * files and directories. | |
298 | */ | |
299 | create_target_directory( my_targetp ); | |
b0d623f7 | 300 | printf( "Will allow %ld failures before testing is aborted \n", g_max_failures ); |
2d21ac55 | 301 | |
2d21ac55 A |
302 | my_start_time = time( NULL ); |
303 | printf( "\nBegin testing - %s \n", ctime_r( &my_start_time, &my_buffer[0] ) ); | |
b0d623f7 A |
304 | printf( "Current architecture is %s\n", current_arch() ); |
305 | ||
6d2010ae A |
306 | /* Code added to run xnu_quick_test under testbots */ |
307 | if ( g_testbots_active == 1 ) { | |
308 | printf("[PASS] xnu_quick_test started\n"); | |
309 | } | |
310 | ||
2d21ac55 A |
311 | /* run each test that is marked to run in our table until we complete all of them or |
312 | * hit the maximum number of failures. | |
313 | */ | |
314 | my_tests_count = (sizeof( g_tests ) / sizeof( g_tests[0] )); | |
315 | for ( i = 0; i < (my_tests_count - 1); i++ ) { | |
316 | int my_err; | |
317 | test_entryp my_testp; | |
318 | ||
319 | my_testp = &g_tests[i]; | |
320 | if ( my_testp->test_run_it == 0 || my_testp->test_routine == NULL ) | |
321 | continue; | |
cf7d32b8 | 322 | #if !TARGET_OS_EMBEDDED |
2d21ac55 A |
323 | if (g_xilog_active == 1) { |
324 | XILogBeginTestCase( logRef, my_testp->test_infop, my_testp->test_infop ); | |
325 | XILogMsg( "test #%d - %s \n", (i + 1), my_testp->test_infop ); | |
326 | } | |
cf7d32b8 | 327 | #endif |
2d21ac55 | 328 | printf( "test #%d - %s \n", (i + 1), my_testp->test_infop ); |
6d2010ae | 329 | fflush(stdout); |
2d21ac55 A |
330 | my_err = my_testp->test_routine( my_testp->test_input ); |
331 | if ( my_err != 0 ) { | |
332 | printf("\t--> FAILED \n"); | |
cf7d32b8 | 333 | #if !TARGET_OS_EMBEDDED |
2d21ac55 A |
334 | if (g_xilog_active == 1) { |
335 | XILogMsg("SysCall %s failed", my_testp->test_infop); | |
336 | XILogErr("Result %d", my_err); | |
337 | } | |
cf7d32b8 | 338 | #endif |
2d21ac55 A |
339 | my_failures++; |
340 | if ( my_failures > g_max_failures ) { | |
cf7d32b8 | 341 | #if !TARGET_OS_EMBEDDED |
6d2010ae A |
342 | if (g_xilog_active == 1) { |
343 | XILogMsg("Reached the maximum number of failures - Aborting xnu_quick_test."); | |
2d21ac55 A |
344 | XILogEndTestCase( logRef, kXILogTestPassOnErrorLevel ); |
345 | } | |
cf7d32b8 | 346 | #endif |
6d2010ae A |
347 | printf( "\n Reached the maximum number of failures - Aborting xnu_quick_test. \n" ); |
348 | /* Code added to run xnu_quick_test under testbots */ | |
349 | if ( g_testbots_active == 1 ) { | |
350 | printf("[FAIL] %s \n", my_testp->test_infop); | |
351 | } | |
2d21ac55 A |
352 | goto exit_this_routine; |
353 | } | |
6d2010ae A |
354 | /* Code added to run xnu_quick_test under testbots */ |
355 | if ( g_testbots_active == 1 ) { | |
356 | printf("[FAIL] %s \n", my_testp->test_infop); | |
357 | } | |
358 | #if !TARGET_OS_EMBEDDED | |
359 | if (g_xilog_active == 1) { | |
360 | XILogEndTestCase( logRef, kXILogTestPassOnErrorLevel ); | |
361 | } | |
362 | #endif | |
363 | continue; | |
2d21ac55 | 364 | } |
cf7d32b8 | 365 | #if !TARGET_OS_EMBEDDED |
2d21ac55 A |
366 | if (g_xilog_active == 1) { |
367 | XILogEndTestCase(logRef, kXILogTestPassOnErrorLevel); | |
368 | } | |
cf7d32b8 | 369 | #endif |
6d2010ae A |
370 | /* Code added to run xnu_quick_test under testbots */ |
371 | if ( g_testbots_active == 1 ) { | |
372 | printf("[PASS] %s \n", my_testp->test_infop); | |
373 | } | |
2d21ac55 A |
374 | } |
375 | ||
376 | exit_this_routine: | |
377 | my_end_time = time( NULL ); | |
378 | printf( "\nEnd testing - %s \n", ctime_r( &my_end_time, &my_buffer[0] ) ); | |
379 | ||
380 | /* clean up our test directory */ | |
381 | rmdir( &g_target_path[0] ); | |
382 | ||
cf7d32b8 | 383 | #if !TARGET_OS_EMBEDDED |
2d21ac55 A |
384 | if (g_xilog_active == 1) { |
385 | XILogCloseLog(logRef); | |
386 | } | |
cf7d32b8 | 387 | #endif |
2d21ac55 A |
388 | |
389 | return 0; | |
390 | } /* main */ | |
391 | ||
392 | ||
393 | /* | |
394 | * parse_tests_to_run - parse the -run argument parameters. the run argument tells us which tests the user | |
395 | * wants to run. we accept ranges (example 1 - 44) and runs of tests (example 2, 33, 34, 100) or a mix of | |
396 | * both (example 1, 44 - 100, 200, 250) | |
397 | */ | |
398 | static int parse_tests_to_run( int argc, const char * argv[], int * indexp ) | |
399 | { | |
400 | int my_tests_count, is_range = 0, i; | |
401 | const char * my_ptr; | |
402 | char * my_temp_ptr; | |
403 | long my_first_test_number, my_last_test_number; | |
404 | char my_buffer[ 128 ]; | |
405 | ||
406 | /* set tests table to not run any tests then go back and set the specific tests the caller asked for */ | |
407 | my_tests_count = (sizeof( g_tests ) / sizeof( g_tests[0] )); | |
408 | for ( i = 0; i < (my_tests_count - 1); i++ ) { | |
409 | g_tests[ i ].test_run_it = 0; | |
410 | } | |
411 | ||
412 | for ( i = *indexp; i < argc; i++ ) { | |
413 | my_ptr = argv[ i ]; | |
414 | if ( strlen( my_ptr ) > 1 && *my_ptr == '-' && isalpha( *(my_ptr + 1) ) ) { | |
415 | /* we have hit a new argument - need to make sure caller uses this argument on the next | |
416 | * pass through its parse loop (which will bump the index value so we want to be one less | |
417 | * than the actual index). | |
418 | */ | |
419 | *indexp = (i - 1); | |
420 | return 0; | |
421 | } | |
422 | ||
423 | if ( strlen( my_ptr ) == 1 && *my_ptr == '-' ) { | |
424 | /* we are dealing with a range of tests, for example: 33 - 44 */ | |
425 | is_range = 1; | |
426 | continue; | |
427 | } | |
428 | ||
429 | if ( strlen( my_ptr ) > (sizeof( my_buffer ) - 1) ) { | |
b0d623f7 | 430 | printf( "-run argument has too many test parameters (max of %lu characters) \n", sizeof( my_buffer ) ); |
2d21ac55 A |
431 | return -1; |
432 | } | |
433 | /* get a local copy of the parameter string to work with - break range into two strings */ | |
434 | strcpy( &my_buffer[0], my_ptr ); | |
435 | ||
436 | my_temp_ptr = strrchr( &my_buffer[0], '-' ); | |
437 | if ( my_temp_ptr != NULL ) { | |
438 | /* we are dealing with a range of tests with no white space, for example: 33-44 or 33- 44 */ | |
439 | my_temp_ptr = strrchr( &my_buffer[0], '-' ); | |
440 | *my_temp_ptr = 0x00; | |
441 | my_first_test_number = strtol( &my_buffer[0], NULL, 10 ); | |
442 | if ( *(my_temp_ptr + 1) == 0x00 ) { | |
443 | /* we hit the case where the range indicator is at the end of one string, for example: 33- */ | |
444 | is_range = 1; | |
445 | continue; | |
446 | } | |
447 | my_last_test_number = strtol( (my_temp_ptr + 1), NULL, 10 ); | |
448 | if ( my_first_test_number < 1 || my_first_test_number > my_last_test_number ) { | |
449 | printf( "-run argument has invalid range parmeters \n" ); | |
450 | return -1; | |
451 | } | |
452 | mark_tests_to_run( my_first_test_number, my_last_test_number ); | |
453 | is_range = 0; | |
454 | continue; | |
455 | } | |
456 | ||
457 | if ( is_range ) { | |
458 | /* should be the second part of the test number range */ | |
459 | my_last_test_number = strtol( &my_buffer[0], NULL, 10 ); | |
460 | if ( my_first_test_number < 1 || my_first_test_number > my_last_test_number ) { | |
461 | printf( "-run argument has invalid range parmeters \n" ); | |
462 | return -1; | |
463 | } | |
464 | ||
465 | mark_tests_to_run( my_first_test_number, my_last_test_number ); | |
466 | is_range = 0; | |
467 | continue; | |
468 | } | |
469 | else { | |
470 | my_first_test_number = strtol( &my_buffer[0], NULL, 10 ); | |
471 | if ( my_first_test_number < 1 ) { | |
472 | printf( "-run argument has invalid test number parameter \n" ); | |
473 | return -1; | |
474 | } | |
475 | mark_tests_to_run( my_first_test_number, my_first_test_number ); | |
476 | continue; | |
477 | } | |
478 | } | |
479 | ||
480 | *indexp = i; | |
481 | return 0; | |
482 | ||
483 | } /* parse_tests_to_run */ | |
484 | ||
485 | ||
486 | static void create_target_directory( const char * the_targetp ) | |
487 | { | |
488 | int err; | |
489 | ||
490 | if ( strlen( the_targetp ) > (sizeof(g_target_path) - 1) ) { | |
491 | printf( "target path too long - \"%s\" \n", the_targetp ); | |
492 | exit( 1 ); | |
493 | } | |
494 | ||
495 | for ( ;; ) { | |
496 | int my_rand; | |
497 | char my_name[64]; | |
498 | ||
499 | my_rand = rand( ); | |
500 | sprintf( &my_name[0], "xnu_quick_test-%d", my_rand ); | |
501 | if ( (strlen( &my_name[0] ) + strlen( the_targetp ) + 2) > PATH_MAX ) { | |
502 | printf( "target path plus our test directory name is too long: \n" ); | |
503 | printf( "target path - \"%s\" \n", the_targetp ); | |
504 | printf( "test directory name - \"%s\" \n", &my_name[0] ); | |
505 | exit( 1 ); | |
506 | } | |
507 | ||
508 | /* append generated directory name onto our path */ | |
509 | g_target_path[0] = 0x00; | |
510 | strcat( &g_target_path[0], the_targetp ); | |
511 | if ( g_target_path[ (strlen(the_targetp) - 1) ] != '/' ) { | |
512 | strcat( &g_target_path[0], "/" ); | |
513 | } | |
514 | strcat( &g_target_path[0], &my_name[0] ); | |
515 | ||
516 | /* try to create the test directory */ | |
517 | err = mkdir( &g_target_path[0], (S_IRWXU | S_IRWXG | S_IROTH) ); | |
518 | if ( err == 0 ) { | |
519 | break; | |
520 | } | |
521 | err = errno; | |
522 | if ( EEXIST != err ) { | |
523 | printf( "test directory creation failed - \"%s\" \n", &g_target_path[0] ); | |
524 | printf( "mkdir call failed with error %d - \"%s\" \n", errno, strerror( err) ); | |
525 | exit( 1 ); | |
526 | } | |
527 | } | |
528 | printf( "created test directory at \"%s\" \n", &g_target_path[0] ); | |
529 | ||
530 | } /* create_target_directory */ | |
531 | ||
532 | ||
2d21ac55 A |
533 | static void mark_tests_to_run( long my_start, long my_end ) |
534 | { | |
535 | int my_tests_count, i; | |
536 | ||
537 | my_tests_count = (sizeof( g_tests ) / sizeof( g_tests[0] )); | |
538 | my_end = (my_end < (my_tests_count - 1)) ? my_end : (my_tests_count - 1); | |
539 | for ( i = (my_start - 1); i < my_end; i++ ) { | |
540 | g_tests[ i ].test_run_it = 1; /* run this test */ | |
541 | } | |
542 | return; | |
543 | } /* mark_tests_to_run */ | |
544 | ||
545 | ||
546 | static void usage( void ) | |
547 | { | |
548 | char * my_ptr; | |
549 | ||
550 | /* skip past full path and just show the tool name */ | |
551 | my_ptr = strrchr( g_cmd_namep, '/' ); | |
552 | if ( my_ptr != NULL ) { | |
553 | my_ptr++; | |
554 | } | |
555 | ||
556 | printf( "\nUSAGE: %s -target TARGET_PATH \n\n", (my_ptr != NULL) ? my_ptr : g_cmd_namep ); | |
557 | printf( "\t -f[ailures] MAX_FAILS_ALLOWED # number of test cases that may fail before we give up. defaults to 0 \n" ); | |
558 | printf( "\t -l[ist] # list all the tests this tool performs \n" ); | |
559 | printf( "\t -r[un] 1, 3, 10 - 19 # run specific tests. enter individual test numbers and/or range of numbers. use -list to list tests. \n" ); | |
560 | printf( "\t -s[kip] # skip setuid tests \n" ); | |
561 | printf( "\t -t[arget] TARGET_PATH # path to directory where tool will create test files. defaults to \"/tmp/\" \n" ); | |
cf7d32b8 | 562 | #if !TARGET_OS_EMBEDDED |
2d21ac55 | 563 | printf( "\t -x[ilog] # use XILog\n"); |
cf7d32b8 | 564 | #endif |
2d21ac55 A |
565 | printf( "\nexamples: \n" ); |
566 | printf( "--- Place all test files and directories at the root of volume \"test_vol\" --- \n" ); | |
567 | printf( "%s -t /Volumes/test_vol/ \n", (my_ptr != NULL) ? my_ptr : g_cmd_namep ); | |
568 | printf( " \n" ); | |
569 | printf( "--- Run the tool for tests 10 thru 15, test 18 and test 20 --- \n" ); | |
570 | printf( "%s -r 10-15, 18, 20 \n", (my_ptr != NULL) ? my_ptr : g_cmd_namep ); | |
571 | printf( " \n" ); | |
572 | exit( 1 ); | |
573 | ||
574 | } /* usage */ | |
575 | ||
b0d623f7 A |
576 | /* This is a private API between Libinfo, Libc, and the DirectoryService daemon. |
577 | * Since we are trying to determine if an external provider will back group | |
578 | * lookups, we can use this, without relying on additional APIs or tools | |
579 | * that might not work yet */ | |
580 | extern int _ds_running(void); | |
581 | ||
582 | #define NUM_GROUPS 6 | |
583 | static int | |
584 | setgroups_if_single_user(void) | |
585 | { | |
586 | int i, retval = -1; | |
587 | struct group *grp; | |
588 | gid_t gids[NUM_GROUPS]; | |
589 | ||
590 | if (!_ds_running()) { | |
591 | printf("In single-user mode.\n"); | |
592 | g_is_single_user = 1; | |
593 | ||
594 | /* We skip 'nobody' and 'anyone' */ | |
595 | getgrent(); | |
596 | getgrent(); | |
597 | for (i = 0; i < NUM_GROUPS; i++) { | |
598 | grp = getgrent(); | |
599 | if (!grp) { | |
600 | break; | |
601 | } | |
602 | ||
603 | gids[i] = grp->gr_gid; | |
604 | } | |
605 | ||
606 | endgrent(); | |
607 | ||
608 | /* Only succeed if we find at least NUM_GROUPS */ | |
609 | if (i == NUM_GROUPS) { | |
610 | retval = setgroups(NUM_GROUPS, gids); | |
611 | if (retval == 0) { | |
612 | getgroups(NUM_GROUPS, gids); | |
613 | printf("After single-user hack, groups are: "); | |
614 | for (i = 0; i < NUM_GROUPS; i++) { | |
615 | printf("%d, ", gids[i]); | |
616 | } | |
617 | putchar('\n'); | |
618 | } else { | |
619 | printf("Setgroups failed.\n"); | |
620 | } | |
621 | } else { | |
622 | printf("Couldn't get sufficient number of groups.\n"); | |
623 | } | |
624 | } else { | |
625 | printf("Not in single user mode.\n"); | |
626 | retval = 0; | |
627 | } | |
628 | ||
629 | ||
630 | return retval; | |
631 | } | |
632 | ||
633 | static const char *current_arch( void ) | |
634 | { | |
635 | cpu_type_t cputype = _mh_execute_header.cputype; | |
636 | cpu_subtype_t cpusubtype = _mh_execute_header.cpusubtype; | |
637 | ||
638 | const NXArchInfo *arch = NXGetArchInfoFromCpuType(cputype, cpusubtype); | |
639 | ||
640 | if (arch) { | |
641 | return arch->name; | |
642 | } else { | |
643 | return "<unknown>"; | |
644 | } | |
645 | } | |
646 | ||
647 | #undef printf /* this makes the "-l" output easier to read */ | |
648 | static void list_all_tests( void ) | |
649 | { | |
650 | int i, my_tests_count; | |
651 | ||
652 | my_tests_count = (sizeof( g_tests ) / sizeof( g_tests[0] )); | |
653 | printf( "\nList of all tests this tool performs... \n" ); | |
654 | ||
655 | for ( i = 0; i < (my_tests_count - 1); i++ ) { | |
656 | printf( " %d \t %s \n", (i + 1), g_tests[ i ].test_infop ); | |
657 | } | |
658 | ||
659 | return; | |
660 | } /* list_all_tests */ |