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