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