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