]> git.saurik.com Git - apple/xnu.git/blob - tests/task_filter_msg.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / tests / task_filter_msg.c
1 #include <stdio.h>
2 #include <mach/mach_vm.h>
3 #include <mach/mach_port.h>
4 #include <mach/mach_error.h>
5 #include <sys/sysctl.h>
6 #include <sys/wait.h>
7 #include <unistd.h>
8
9 #ifdef T_NAMESPACE
10 #undef T_NAMESPACE
11 #endif
12
13 #include <darwintest.h>
14 #include <darwintest_utils.h>
15
16 T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true), T_META_NAMESPACE("xnu.ipc"));
17
18 T_DECL(test_task_filter_msg_flag, "Set the filter msg flag on the task and check if the forked child inherits it",
19 T_META_ASROOT(true), T_META_CHECK_LEAKS(false))
20 {
21 int ret, dev;
22 size_t sysctl_size;
23
24 T_SETUPBEGIN;
25
26 dev = 0;
27 sysctl_size = sizeof(dev);
28 ret = sysctlbyname("kern.development", &dev, &sysctl_size, NULL, 0);
29 T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "sysctl kern.development failed");
30 if (dev == 0) {
31 T_SKIP("Skipping test on release kernel");
32 }
33
34 T_SETUPEND;
35
36 int cur_filter_flag = 0;
37 int new_filter_flag = 1;
38 ret = sysctlbyname("kern.task_set_filter_msg_flag", &cur_filter_flag, &sysctl_size, &new_filter_flag, sizeof(new_filter_flag));
39 T_ASSERT_POSIX_SUCCESS(ret, "sysctlbyname");
40 T_ASSERT_EQ(cur_filter_flag, 0, "Task should not have filtering on");
41
42 cur_filter_flag = 0;
43 ret = sysctlbyname("kern.task_set_filter_msg_flag", &cur_filter_flag, &sysctl_size, NULL, 0);
44 T_ASSERT_POSIX_SUCCESS(ret, "sysctlbyname");
45 T_ASSERT_EQ(cur_filter_flag, 1, "Task should have filtering on");
46
47 pid_t pid = fork();
48 if (pid == 0) {
49 cur_filter_flag = 0;
50 ret = sysctlbyname("kern.task_set_filter_msg_flag", &cur_filter_flag, &sysctl_size, NULL, 0);
51 if (ret == 0) {
52 if (cur_filter_flag == 1) {
53 exit(0);
54 }
55 }
56 exit(1);
57 }
58
59 int status;
60 ret = waitpid(pid, &status, 0);
61 T_ASSERT_POSIX_SUCCESS(ret, "waitpid");
62
63 if (WIFEXITED(status)) {
64 const int exit_code = WEXITSTATUS(status);
65 T_ASSERT_EQ(exit_code, 0, "Child inherited the filter msg flag");
66 }
67
68 /* Turn off task msg filtering */
69 cur_filter_flag = 1;
70 new_filter_flag = 0;
71 ret = sysctlbyname("kern.task_set_filter_msg_flag", &cur_filter_flag, &sysctl_size, &new_filter_flag, sizeof(new_filter_flag));
72 T_ASSERT_POSIX_SUCCESS(ret, "sysctlbyname");
73 T_ASSERT_EQ(cur_filter_flag, 1, "Task should have filtering on");
74
75 T_END;
76 }