xref: /xnu-8792.81.2/tests/task_info_28439149.c (revision 19c3b8c28c31cb8130e034cfb5df6bf9ba342d90)
1*19c3b8c2SApple OSS Distributions #include <darwintest.h>
2*19c3b8c2SApple OSS Distributions #include <mach/host_priv.h>
3*19c3b8c2SApple OSS Distributions #include <mach/mach.h>
4*19c3b8c2SApple OSS Distributions #include <mach/mach_types.h>
5*19c3b8c2SApple OSS Distributions #include <mach/processor_set.h>
6*19c3b8c2SApple OSS Distributions #include <mach/task.h>
7*19c3b8c2SApple OSS Distributions #include <sys/sysctl.h>
8*19c3b8c2SApple OSS Distributions #include <unistd.h>
9*19c3b8c2SApple OSS Distributions #include <mach-o/dyld.h>
10*19c3b8c2SApple OSS Distributions #include <mach-o/dyld_images.h>
11*19c3b8c2SApple OSS Distributions #include <sys/types.h>
12*19c3b8c2SApple OSS Distributions #include <sys/wait.h>
13*19c3b8c2SApple OSS Distributions #include <stdlib.h>
14*19c3b8c2SApple OSS Distributions 
15*19c3b8c2SApple OSS Distributions T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true));
16*19c3b8c2SApple OSS Distributions 
17*19c3b8c2SApple OSS Distributions static void
do_child(int * pipefd)18*19c3b8c2SApple OSS Distributions do_child(int *pipefd)
19*19c3b8c2SApple OSS Distributions {
20*19c3b8c2SApple OSS Distributions 	int exit = 0;
21*19c3b8c2SApple OSS Distributions 
22*19c3b8c2SApple OSS Distributions 	close(pipefd[1]);
23*19c3b8c2SApple OSS Distributions 	read(pipefd[0], &exit, sizeof(int));
24*19c3b8c2SApple OSS Distributions 	T_QUIET; T_EXPECT_EQ_INT(exit, 1, "exit");
25*19c3b8c2SApple OSS Distributions 	close(pipefd[0]);
26*19c3b8c2SApple OSS Distributions }
27*19c3b8c2SApple OSS Distributions 
28*19c3b8c2SApple OSS Distributions T_DECL(task_info_28439149, "ensure that task_info has the correct permission",
29*19c3b8c2SApple OSS Distributions     T_META_CHECK_LEAKS(false), T_META_ASROOT(true))
30*19c3b8c2SApple OSS Distributions {
31*19c3b8c2SApple OSS Distributions 	int pipefd[2];
32*19c3b8c2SApple OSS Distributions 
33*19c3b8c2SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(pipe(pipefd), "pipe");
34*19c3b8c2SApple OSS Distributions 
35*19c3b8c2SApple OSS Distributions 	int pid = fork();
36*19c3b8c2SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(pid, "fork");
37*19c3b8c2SApple OSS Distributions 
38*19c3b8c2SApple OSS Distributions 	if (pid == 0) {
39*19c3b8c2SApple OSS Distributions 		do_child(pipefd);
40*19c3b8c2SApple OSS Distributions 		return;
41*19c3b8c2SApple OSS Distributions 	}
42*19c3b8c2SApple OSS Distributions 
43*19c3b8c2SApple OSS Distributions 	close(pipefd[0]);
44*19c3b8c2SApple OSS Distributions 
45*19c3b8c2SApple OSS Distributions 	int exit;
46*19c3b8c2SApple OSS Distributions 	mach_msg_type_number_t count;
47*19c3b8c2SApple OSS Distributions 	struct task_basic_info_64 ti;
48*19c3b8c2SApple OSS Distributions 	task_dyld_info_data_t di;
49*19c3b8c2SApple OSS Distributions 
50*19c3b8c2SApple OSS Distributions 	task_t self = mach_task_self();
51*19c3b8c2SApple OSS Distributions 	task_t other_name;
52*19c3b8c2SApple OSS Distributions 	task_t other;
53*19c3b8c2SApple OSS Distributions 	int ret;
54*19c3b8c2SApple OSS Distributions 
55*19c3b8c2SApple OSS Distributions 	T_EXPECT_MACH_SUCCESS(task_for_pid(self, pid, &other), NULL);
56*19c3b8c2SApple OSS Distributions 	T_EXPECT_MACH_SUCCESS(task_name_for_pid(self, pid, &other_name), NULL);
57*19c3b8c2SApple OSS Distributions 
58*19c3b8c2SApple OSS Distributions 	count = TASK_BASIC_INFO_64_COUNT;
59*19c3b8c2SApple OSS Distributions 	T_EXPECT_MACH_SUCCESS(task_info(self, TASK_BASIC_INFO_64, (task_info_t)&ti,
60*19c3b8c2SApple OSS Distributions 	    &count), "task_info(self, TASK_BASIC_INFO_64 ...)");
61*19c3b8c2SApple OSS Distributions 	count = TASK_BASIC_INFO_64_COUNT;
62*19c3b8c2SApple OSS Distributions 	T_EXPECT_MACH_SUCCESS(task_info(other, TASK_BASIC_INFO_64, (task_info_t)&ti,
63*19c3b8c2SApple OSS Distributions 	    &count), "task_info(other_name, TASK_BASIC_INFO_64 ...)");
64*19c3b8c2SApple OSS Distributions 	count = TASK_BASIC_INFO_64_COUNT;
65*19c3b8c2SApple OSS Distributions 	T_EXPECT_MACH_SUCCESS(task_info(other_name, TASK_BASIC_INFO_64, (task_info_t)&ti,
66*19c3b8c2SApple OSS Distributions 	    &count), "task_info(other_name, TASK_BASIC_INFO_64 ...)");
67*19c3b8c2SApple OSS Distributions 
68*19c3b8c2SApple OSS Distributions 
69*19c3b8c2SApple OSS Distributions 	count = TASK_DYLD_INFO_COUNT;
70*19c3b8c2SApple OSS Distributions 	T_EXPECT_MACH_SUCCESS(task_info(self, TASK_DYLD_INFO, (task_info_t)&di,
71*19c3b8c2SApple OSS Distributions 	    &count), "task_info(self, TASK_DYLD_INFO ...)");
72*19c3b8c2SApple OSS Distributions 	count = TASK_DYLD_INFO_COUNT;
73*19c3b8c2SApple OSS Distributions 	T_EXPECT_MACH_SUCCESS(task_info(other, TASK_DYLD_INFO, (task_info_t)&di,
74*19c3b8c2SApple OSS Distributions 	    &count), "task_info(other_name, TASK_DYLD_INFO ...)");
75*19c3b8c2SApple OSS Distributions 	count = TASK_DYLD_INFO_COUNT;
76*19c3b8c2SApple OSS Distributions 	ret = task_info(other_name, TASK_DYLD_INFO, (task_info_t)&di, &count);
77*19c3b8c2SApple OSS Distributions 	T_EXPECT_EQ_INT(ret, KERN_INVALID_ARGUMENT, "task info TASK_DYLD_INFO should fail with mach_port_name");
78*19c3b8c2SApple OSS Distributions 
79*19c3b8c2SApple OSS Distributions 	exit = 1;
80*19c3b8c2SApple OSS Distributions 	write(pipefd[1], &exit, sizeof(int));
81*19c3b8c2SApple OSS Distributions 	close(pipefd[1]);
82*19c3b8c2SApple OSS Distributions 
83*19c3b8c2SApple OSS Distributions 	wait(NULL);
84*19c3b8c2SApple OSS Distributions }
85