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