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