]>
git.saurik.com Git - apple/xnu.git/blob - tools/tests/xnu_quick_test/misc.c
9545bf1406f11ee0c1d353614e37bc904c6b0f5c
6 * create_random_name - creates a file with a random / unique name in the given directory.
7 * when do_open is true we create a file else we generaate a name that does not exist in the
8 * given directory (we do not create anything when do_open is 0).
9 * WARNING - caller provides enough space in path buffer for longest possible name.
10 * WARNING - assumes caller has appended a trailing '/' on the path passed to us.
11 * RAND_MAX is currently 2147483647 (ten characters plus one for a slash)
13 int create_random_name( char *the_pathp
, int do_open
) {
17 for ( i
= 0; i
< 1; i
++ ) {
23 sprintf( &my_name
[0], "%d", my_rand
);
24 if ( (strlen( &my_name
[0] ) + strlen( the_pathp
) + 2) > PATH_MAX
) {
25 printf( "%s - path to test file greater than PATH_MAX \n", __FUNCTION__
);
29 // append generated file name onto our path
30 myp
= strrchr( the_pathp
, '/' );
32 strcat( the_pathp
, &my_name
[0] );
34 /* create a file with this name */
35 my_fd
= open( the_pathp
, (O_RDWR
| O_CREAT
| O_EXCL
),
36 (S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
) );
38 if ( errno
!= EEXIST
) {
39 printf( "%s - open failed with errno %d - %s \n",
40 __FUNCTION__
, errno
, strerror( errno
) );
43 // name already exists, try another
49 /* make sure the name is unique */
51 my_err
= stat( the_pathp
, &my_sb
);
53 if ( errno
== ENOENT
) {
57 printf( "%s - open failed with errno %d - %s \n",
58 __FUNCTION__
, errno
, strerror( errno
) );
62 /* name already exists, try another */
73 } /* create_random_name */
76 * create_file_with_name - create a file in the given target directory using the given name.
77 * If an existing file or directory is present use the value of remove_existing to determine if the
78 * object is to be deleted.
79 * returns 0 if file could be created, 1 if file exists, 2 if directory exists, else -1
80 * NOTE - will fail if a directory is present with the given name and it is not empty.
82 int create_file_with_name( char *the_target_dirp
, char *the_namep
, int remove_existing
) {
83 int create_test_file
, my_err
, my_result
;
85 char * my_pathp
= NULL
;
90 my_kr
= vm_allocate((vm_map_t
) mach_task_self(), (vm_address_t
*)&my_pathp
, PATH_MAX
, VM_FLAGS_ANYWHERE
);
91 if(my_kr
!= KERN_SUCCESS
){
92 printf( "vm_allocate failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
96 strcpy( my_pathp
, the_target_dirp
);
97 strcat( my_pathp
, the_namep
);
99 /* make sure the name is unique */
101 my_err
= stat( my_pathp
, &my_sb
);
103 create_test_file
= 1;
104 if ( errno
!= ENOENT
) {
109 /* name already exists */
110 if ( S_ISDIR( my_sb
.st_mode
) ) {
111 my_result
= 2; /* tell caller directory exists with target name */
112 if ( remove_existing
) {
113 my_err
= rmdir( my_pathp
);
114 if ( my_err
== -1 ) {
115 printf( "rmdir failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
118 create_test_file
= 1;
122 my_result
= 1; /* tell caller file exists with target name */
123 if ( remove_existing
) {
124 my_err
= unlink( my_pathp
);
125 if ( my_err
== -1 ) {
126 printf( "unlink failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
129 create_test_file
= 1;
134 if ( create_test_file
) {
135 /* create a file with this name */
136 my_fd
= open( my_pathp
, (O_RDWR
| O_CREAT
| O_EXCL
),
137 (S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
) );
139 printf( "open failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
149 if ( my_pathp
!= NULL
) {
150 if ( my_result
== -1 && create_test_file
) {
153 vm_deallocate(mach_task_self(), (vm_address_t
)my_pathp
, PATH_MAX
);
158 } /* create_file_with_name */
164 * This function is needed by both xnu_quick_test proper and the execve() helper
165 * program. It forks a child process and then exec()s an image on that child.
166 * Path, argv, and envp are fed directly to the execve() call.
167 * Parameter killwait decides how long to wait before killing the child.
169 int do_execve_test(char * path
, char * argv
[], void * envp
, int killwait
)
171 int my_err
= 0, my_status
;
172 pid_t my_pid
, my_wait_pid
;
175 printf("do_execve_test(path = %s)\n", path
);
176 printf("CWD= %s\n", getwd(NULL
));
179 /* vfork then execve sleep system command (which we will kill from the parent process) */
182 printf( "vfork failed with errno %d - %s \n", errno
, strerror( errno
) );
183 goto test_failed_exit
;
187 * child process - use execve to start one of the customized helper
188 * binaries, which just sleep for 120 seconds. Let our parent kill us.
191 my_err
= execve(path
, argv
, envp
);
192 if ( my_err
!= 0 ) { /* TODO: execve() on x86_64 inca returns weird error codes, see rdar://4655612 */
193 printf( "execve call failed with return value: %d, errno: %d - \"%s\"; path: %s \n",
194 my_err
, errno
, strerror( errno
), path
);
199 /* should never get here */
200 printf("Execve failed and it was not caught by our test\n");
204 * parent process - let's kill our sleeping child
207 my_err
= kill( my_pid
, SIGKILL
);
208 if ( my_err
== -1 ) {
209 printf( "kill call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
210 goto test_failed_exit
;
213 /* wait for child to exit */
214 my_wait_pid
= wait4( my_pid
, &my_status
, 0, NULL
);
215 if ( my_wait_pid
== -1 ) {
216 printf( "wait4 failed with errno %d - %s \n", errno
, strerror( errno
) );
217 goto test_failed_exit
;
220 /* wait4 should return our child's pid when it exits */
221 if ( my_wait_pid
!= my_pid
) {
222 printf( "wait4 did not return child pid - returned %d should be %d \n", my_wait_pid
, my_pid
);
223 goto test_failed_exit
;
226 if (!(WIFSIGNALED( my_status
))) {
227 printf( "child process was not signaled and should have been\n", my_status
);
228 goto test_failed_exit
;
231 if (WTERMSIG( my_status
) != SIGKILL
) {
232 printf( "wait4 returned wrong signal status - 0x%02X \n", my_status
);
233 goto test_failed_exit
;
237 goto test_passed_exit
;
244 } /* do_execve_test */
247 * Helper function for posix_spawn test
248 * arch: target architecture to spawn for
250 int do_spawn_test(int arch
, int shouldfail
)
252 int my_err
, my_pid
, my_status
;
254 posix_spawnattr_t attr
;
256 char * args
[] = {"helpers/arch", NULL
};
258 my_err
= posix_spawnattr_init(&attr
);
260 printf("posix_spawnattr_init failed\n");
264 /* set spawn to only succeed for arch 'arch' */
265 my_err
= posix_spawnattr_setbinpref_np(&attr
, 1, &arch
, &my_size
);
266 if (my_err
!= 0 || my_size
!= 1) {
267 printf("posix_spawnattr_setbinpref_np failed\n");
271 /* spawn off child process */
272 my_err
= posix_spawn(&my_pid
, args
[0], NULL
, &attr
, args
, NULL
);
275 printf("posix_spawn should have failed on arch %d\n", arch
);
281 * child should exit with return code == arch; note that the
282 * posix_spawn error numers are *returned*, NOT set in errno!!!
285 printf("posix_spawn failed with errno %d - %s\n", my_err
, strerror(my_err
));
289 my_err
= wait4(my_pid
, &my_status
, 0, NULL
);
291 printf("wait4 failed with errno %d - %s\n", errno
, strerror(errno
));
296 if (WEXITSTATUS(my_status
) != (arch
& 0xff)) {
297 printf("child exited with status %d (expected %d)\n",
298 (WEXITSTATUS(my_status
)),
310 * Uses sysctlbyname to determine the cpu type. Currently, XNU classifies G5 as a
311 * 32-bit CPU, so this shouldn't be used to determine whether or not a CPU
314 int get_architecture()
321 errmsg
= "sysctlbyname() failed when getting hw.cputype";
322 if (my_err
= sysctlbyname("hw.cputype", NULL
, &length
, NULL
, 0)) goto finished
; /* get length of data */
323 if (length
!= sizeof(buf
)) goto finished
;
324 if (my_err
= sysctlbyname("hw.cputype", &buf
, &length
, NULL
, 0)) goto finished
; /* copy data */
327 case CPU_TYPE_X86_64
:
336 if (rval
== -1 && errmsg
)
337 printf("%s", errmsg
);
344 * Gets the bit'ed-ness of the current host. Returns either 32 or 64.
345 * This get the hardware capability, but does not tell us whether this
346 * binary is executing in 64 bit or 32 bit mode. Check sizeof long
347 * or pointer to determine that.
354 * On 32-bit systems the sysctls 64bitops and x86_64 don't
355 * even exists, so if we don't find them then we assume
359 /* Check for PPC 64 */
360 if ((my_err
= sysctlbyname("hw.optional.64bitops", NULL
, &len
, NULL
, 0))) goto x86_64check
; /* Request size */
361 if (len
> sizeof(buf
)) goto x86_64check
;
362 if ((my_err
= sysctlbyname("hw.optional.64bitops", &buf
, &len
, NULL
, 0))) goto x86_64check
; /* Copy value out from kernel */
363 if (buf
== 1) rval
= 64;
367 /* Check for x86_64 */
368 if ((my_err
= sysctlbyname("hw.optional.x86_64", NULL
, &len
, NULL
, 0))) goto finished
; /* Request size */
369 if (len
> sizeof(buf
)) goto finished
;
370 if ((my_err
= sysctlbyname("hw.optional.x86_64", &buf
, &len
, NULL
, 0))) goto finished
; /* Copy value out from kernel */
371 if (buf
== 1) rval
= 64;
378 * printf with a date and time stamp so that we can correlate printf's
379 * with the log files of a system in case of test failure.
381 * NB: MY_PRINTF_DATE_FMT chosen to look like syslog to aid "grep".
383 #define MY_PRINTF_DATE_FMT "%b %e %T"
384 #undef printf /* was my_printf */
386 my_printf(const char * __restrict fmt
, ...)
395 /* Get the timestamp for this printf */
397 timeptr
= localtime(&result
);
398 strftime(datebuf
, sizeof(datebuf
), MY_PRINTF_DATE_FMT
, timeptr
);
400 /* do the printf of the requested data to a local buffer */
402 rv
= vasprintf(&bufp
, fmt
, ap
);
406 * if we successfully got a local buffer, then we want to
407 * print a timestamp plus what we would have printed before,
408 * then free the allocated memory.
411 rv
= printf("%s %s", datebuf
, bufp
);