xref: /xnu-11215/tests/task_fatal_port.c (revision 8d741a5d)
1 #include <servers/bootstrap.h>
2 #include <mach/mach.h>
3 #include <mach/message.h>
4 #include <stdlib.h>
5 #include <sys/sysctl.h>
6 #include <unistd.h>
7 #include <pthread.h>
8 
9 #ifdef T_NAMESPACE
10 #undef T_NAMESPACE
11 #endif
12 
13 #include <darwintest.h>
14 #include <darwintest_utils.h>
15 #include <darwintest_multiprocess.h>
16 
17 T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true),
18     T_META_NAMESPACE("xnu.ipc"),
19     T_META_RADAR_COMPONENT_NAME("xnu"),
20     T_META_RADAR_COMPONENT_VERSION("IPC"),
21     T_META_TAG_VM_PREFERRED);
22 
23 
24 T_DECL(task_fatal_port, "Create a child and kill it using the task fatal port")
25 {
26 	mach_port_name_t old_port_name = 0, new_port_name = 1;
27 	int port_name_size, status, ret;
28 	pid_t child, pid;
29 
30 #if TARGET_OS_BRIDGE
31 	T_SKIP("Skip this test on 32 bit kernels and bridgeos");
32 #endif
33 
34 	child = fork();
35 	if (child == 0) {
36 		port_name_size = sizeof(old_port_name);
37 		ret = sysctlbyname("machdep.task_get_fatal_port", &old_port_name, &port_name_size, &new_port_name, sizeof(new_port_name));
38 		if (ret < 0) {
39 			printf("sysctlbyname failed");
40 			exit(2);
41 		}
42 		printf("old_port_name = %d \n", old_port_name);
43 		mach_port_deallocate(mach_task_self(), old_port_name);
44 
45 		while (1) {
46 			sleep(1);
47 		}
48 	}
49 
50 	pid = waitpid(child, &status, 0);
51 	T_ASSERT_EQ(pid, child, "waitpid returns correct pid");
52 	T_EXPECT_EQ(WIFSIGNALED(status), true, "child was signaled");
53 	T_EXPECT_EQ(WTERMSIG(status), SIGKILL, "child was sent SIGKILL");
54 }
55