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