]>
git.saurik.com Git - apple/xnu.git/blob - tools/tests/xnu_quick_test/misc.c
b274ddfd124779d19aef0f8d0c1de0275824ad7b
5 * create_random_name - creates a file with a random / unique name in the given directory.
6 * when do_open is true we create a file else we generaate a name that does not exist in the
7 * given directory (we do not create anything when do_open is 0).
8 * WARNING - caller provides enough space in path buffer for longest possible name.
9 * WARNING - assumes caller has appended a trailing '/' on the path passed to us.
10 * RAND_MAX is currently 2147483647 (ten characters plus one for a slash)
12 int create_random_name( char *the_pathp
, int do_open
) {
16 for ( i
= 0; i
< 1; i
++ ) {
22 sprintf( &my_name
[0], "%d", my_rand
);
23 if ( (strlen( &my_name
[0] ) + strlen( the_pathp
) + 2) > PATH_MAX
) {
24 printf( "%s - path to test file greater than PATH_MAX \n", __FUNCTION__
);
28 // append generated file name onto our path
29 myp
= strrchr( the_pathp
, '/' );
31 strcat( the_pathp
, &my_name
[0] );
33 /* create a file with this name */
34 my_fd
= open( the_pathp
, (O_RDWR
| O_CREAT
| O_EXCL
),
35 (S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
) );
37 if ( errno
!= EEXIST
) {
38 printf( "%s - open failed with errno %d - %s \n",
39 __FUNCTION__
, errno
, strerror( errno
) );
42 // name already exists, try another
48 /* make sure the name is unique */
50 my_err
= stat( the_pathp
, &my_sb
);
52 if ( errno
== ENOENT
) {
56 printf( "%s - open failed with errno %d - %s \n",
57 __FUNCTION__
, errno
, strerror( errno
) );
61 /* name already exists, try another */
72 } /* create_random_name */
75 * create_file_with_name - create a file in the given target directory using the given name.
76 * If an existing file or directory is present use the value of remove_existing to determine if the
77 * object is to be deleted.
78 * returns 0 if file could be created, 1 if file exists, 2 if directory exists, else -1
79 * NOTE - will fail if a directory is present with the given name and it is not empty.
81 int create_file_with_name( char *the_target_dirp
, char *the_namep
, int remove_existing
) {
82 int create_test_file
, my_err
, my_result
;
84 char * my_pathp
= NULL
;
88 my_pathp
= (char *) malloc( PATH_MAX
);
89 if ( my_pathp
== NULL
) {
90 printf( "malloc failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
93 strcpy( my_pathp
, the_target_dirp
);
94 strcat( my_pathp
, the_namep
);
96 /* make sure the name is unique */
98 my_err
= stat( my_pathp
, &my_sb
);
100 create_test_file
= 1;
101 if ( errno
!= ENOENT
) {
106 /* name already exists */
107 if ( S_ISDIR( my_sb
.st_mode
) ) {
108 my_result
= 2; /* tell caller directory exists with target name */
109 if ( remove_existing
) {
110 my_err
= rmdir( my_pathp
);
111 if ( my_err
== -1 ) {
112 printf( "rmdir failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
115 create_test_file
= 1;
119 my_result
= 1; /* tell caller file exists with target name */
120 if ( remove_existing
) {
121 my_err
= unlink( my_pathp
);
122 if ( my_err
== -1 ) {
123 printf( "unlink failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
126 create_test_file
= 1;
131 if ( create_test_file
) {
132 /* create a file with this name */
133 my_fd
= open( my_pathp
, (O_RDWR
| O_CREAT
| O_EXCL
),
134 (S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
) );
136 printf( "open failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
146 if ( my_pathp
!= NULL
) {
147 if ( my_result
== -1 && create_test_file
) {
155 } /* create_file_with_name */
161 * This function is needed by both xnu_quick_test proper and the execve() helper
162 * program. It forks a child process and then exec()s an image on that child.
163 * Path, argv, and envp are fed directly to the execve() call.
164 * Parameter killwait decides how long to wait before killing the child.
166 int do_execve_test(char * path
, char * argv
[], void * envp
, int killwait
)
168 int my_err
= 0, my_status
;
169 pid_t my_pid
, my_wait_pid
;
172 printf("do_execve_test(path = %s)\n", path
);
173 printf("CWD= %s\n", getwd(NULL
));
176 /* vfork then execve sleep system command (which we will kill from the parent process) */
179 printf( "vfork failed with errno %d - %s \n", errno
, strerror( errno
) );
180 goto test_failed_exit
;
184 * child process - use execve to start one of the customized helper
185 * binaries, which just sleep for 120 seconds. Let our parent kill us.
188 my_err
= execve(path
, argv
, envp
);
189 if ( my_err
!= 0 ) { /* TODO: execve() on x86_64 inca returns weird error codes, see rdar://4655612 */
190 printf( "execve call failed with return value: %d, errno: %d - \"%s\"; path: %s \n",
191 my_err
, errno
, strerror( errno
), path
);
196 /* should never get here */
197 printf("Execve failed and it was not caught by our test\n");
201 * parent process - let's kill our sleeping child
204 my_err
= kill( my_pid
, SIGKILL
);
205 if ( my_err
== -1 ) {
206 printf( "kill call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
207 goto test_failed_exit
;
210 /* wait for child to exit */
211 my_wait_pid
= wait4( my_pid
, &my_status
, 0, NULL
);
212 if ( my_wait_pid
== -1 ) {
213 printf( "wait4 failed with errno %d - %s \n", errno
, strerror( errno
) );
214 goto test_failed_exit
;
217 /* wait4 should return our child's pid when it exits */
218 if ( my_wait_pid
!= my_pid
) {
219 printf( "wait4 did not return child pid - returned %d should be %d \n", my_wait_pid
, my_pid
);
220 goto test_failed_exit
;
223 if ( WIFSIGNALED( my_status
) && WTERMSIG( my_status
) != SIGKILL
) {
224 printf( "wait4 returned wrong signal status - 0x%02X \n", my_status
);
225 goto test_failed_exit
;
229 goto test_passed_exit
;
236 } /* do_execve_test */
239 * Helper function for posix_spawn test
240 * arch: target architecture to spawn for
242 int do_spawn_test(int arch
, int shouldfail
)
244 int my_err
, my_pid
, my_status
;
246 posix_spawnattr_t attr
;
248 char * args
[] = {"helpers/arch", NULL
};
250 my_err
= posix_spawnattr_init(&attr
);
252 printf("posix_spawnattr_init failed\n");
256 /* set spawn to only succeed for arch 'arch' */
257 my_err
= posix_spawnattr_setbinpref_np(&attr
, 1, &arch
, &my_size
);
258 if (my_err
!= 0 || my_size
!= 1) {
259 printf("posix_spawnattr_setbinpref_np failed\n");
263 /* spawn off child process */
264 my_err
= posix_spawn(&my_pid
, args
[0], NULL
, &attr
, args
, NULL
);
267 printf("posix_spawn should have failed on arch %d\n", arch
);
272 /* child should exit with return code == arch */
274 printf("posix_spawn failed with errno %d - %s\n", errno
, strerror(errno
));
278 my_err
= wait4(my_pid
, &my_status
, 0, NULL
);
280 printf("wait4 failed with errno %d - %s\n", errno
, strerror(errno
));
285 if (WEXITSTATUS(my_status
) != (arch
& 0xff)) {
286 printf("child exited with status %d (expected %d)\n",
287 (WEXITSTATUS(my_status
)),
299 * Uses sysctlbyname to determine the cpu type. Currently, XNU classifies G5 as a
300 * 32-bit CPU, so this shouldn't be used to determine whether or not a CPU
303 int get_architecture()
310 errmsg
= "sysctlbyname() failed when getting hw.cputype";
311 if (my_err
= sysctlbyname("hw.cputype", NULL
, &length
, NULL
, 0)) goto finished
; /* get length of data */
312 if (length
!= sizeof(buf
)) goto finished
;
313 if (my_err
= sysctlbyname("hw.cputype", &buf
, &length
, NULL
, 0)) goto finished
; /* copy data */
316 case CPU_TYPE_X86_64
:
319 case CPU_TYPE_POWERPC
:
320 case CPU_TYPE_POWERPC64
:
326 if (rval
== -1 && errmsg
)
327 printf("%s", errmsg
);
334 * Gets the bit'ed-ness of the current host. Returns either 32 or 64.
335 * This get the hardware capability, but does not tell us whether this
336 * binary is executing in 64 bit or 32 bit mode. Check sizeof long
337 * or pointer to determine that.
344 * On 32-bit systems the sysctls 64bitops and x86_64 don't
345 * even exists, so if we don't find them then we assume
349 /* Check for PPC 64 */
350 if ((my_err
= sysctlbyname("hw.optional.64bitops", NULL
, &len
, NULL
, 0))) goto x86_64check
; /* Request size */
351 if (len
> sizeof(buf
)) goto x86_64check
;
352 if ((my_err
= sysctlbyname("hw.optional.64bitops", &buf
, &len
, NULL
, 0))) goto x86_64check
; /* Copy value out from kernel */
353 if (buf
== 1) rval
= 64;
357 /* Check for x86_64 */
358 if ((my_err
= sysctlbyname("hw.optional.x86_64", NULL
, &len
, NULL
, 0))) goto finished
; /* Request size */
359 if (len
> sizeof(buf
)) goto finished
;
360 if ((my_err
= sysctlbyname("hw.optional.x86_64", &buf
, &len
, NULL
, 0))) goto finished
; /* Copy value out from kernel */
361 if (buf
== 1) rval
= 64;