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