xref: /xnu-8020.121.3/tests/task_fatal_port.c (revision fdd8201d7b966f0c3ea610489d29bd841d358941)
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 
22 
23 T_DECL(task_fatal_port, "Create a child and kill it using the task fatal port")
24 {
25 	mach_port_name_t old_port_name = 0, new_port_name = 1;
26 	int port_name_size, status, ret;
27 	pid_t child, pid;
28 
29 #if defined(__arm__) || TARGET_OS_BRIDGE
30 	T_SKIP("Skip this test on 32 bit kernels and bridgeos");
31 #endif
32 
33 	child = fork();
34 	if (child == 0) {
35 		port_name_size = sizeof(old_port_name);
36 		ret = sysctlbyname("machdep.task_get_fatal_port", &old_port_name, &port_name_size, &new_port_name, sizeof(new_port_name));
37 		if (ret < 0) {
38 			printf("sysctlbyname failed");
39 			exit(2);
40 		}
41 		printf("old_port_name = %d \n", old_port_name);
42 		mach_port_deallocate(mach_task_self(), old_port_name);
43 
44 		while (1) {
45 			sleep(1);
46 		}
47 	}
48 
49 	pid = waitpid(child, &status, 0);
50 	T_ASSERT_EQ(pid, child, "waitpid returns correct pid");
51 	T_EXPECT_EQ(WIFSIGNALED(status), true, "child was signaled");
52 	T_EXPECT_EQ(WTERMSIG(status), SIGKILL, "child was sent SIGKILL");
53 }
54