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