]> git.saurik.com Git - apple/xnu.git/blob - tools/tests/darwintests/xnu_quick_test.c
5e2e66701e77c33d1eda8295e70733d95a689c9c
[apple/xnu.git] / tools / tests / darwintests / xnu_quick_test.c
1 #define T_NAMESPACE xnu.quicktest
2
3 #include <darwintest.h>
4
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include <sys/stat.h>
8 #include <sys/wait.h>
9
10 /* **************************************************************************************************************
11 * Test fork wait4, and exit system calls.
12 * **************************************************************************************************************
13 */
14 T_DECL(fork_wait4_exit_test,
15 "Tests forking off a process and waiting for the child to exit", T_META_CHECK_LEAKS(false))
16 {
17 int my_err, my_status;
18 pid_t my_pid, my_wait_pid;
19 struct rusage my_usage;
20 char * g_target_path="/";
21
22 /* spin off another process */
23 T_ASSERT_NE(my_pid = fork(), -1, "Fork off a process");
24
25 if ( my_pid == 0 ) {
26 struct stat my_sb;
27
28 /* child process does very little then exits */
29 my_err = stat( &g_target_path[0], &my_sb );
30 T_WITH_ERRNO;
31 T_ASSERT_TRUE(my_err == 0, "stat call with path: \"%s\" returned \"%d\"", &g_target_path[0], errno);
32 exit( 44 );
33 }
34
35 /* parent process waits for child to exit */
36 T_ASSERT_NE(my_wait_pid = wait4( my_pid, &my_status, 0, &my_usage ), -1,
37 "Wait for child to exit\n");
38
39 /* wait4 should return our child's pid when it exits */
40 T_ASSERT_EQ(my_wait_pid, my_pid,
41 "wait4 should return our child's pid when it exits");
42
43 /* kind of just guessing on these values so if this fails we should take a closer
44 * look at the returned rusage structure.
45 */
46 T_ASSERT_FALSE(( my_usage.ru_utime.tv_sec > 1 ||
47 my_usage.ru_stime.tv_sec > 1 || my_usage.ru_majflt > 1000 ||
48 my_usage.ru_msgsnd > 100 ), "wait4 returned rusage structure");
49
50 T_ASSERT_TRUE(( WIFEXITED( my_status ) && WEXITSTATUS( my_status ) == 44 ),
51 "check if wait4 returns right exit status");
52 }