]> git.saurik.com Git - apple/xnu.git/blob - tests/mktimer_kobject.c
xnu-7195.81.3.tar.gz
[apple/xnu.git] / tests / mktimer_kobject.c
1 #include <stdint.h>
2 #include <stdio.h>
3 #include <unistd.h>
4
5 #include <mach/mach.h>
6 #include <mach/mk_timer.h>
7
8 #include <darwintest.h>
9
10 T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true));
11
12 T_DECL(mktimer_kobject, "mktimer_kobject()", T_META_ALL_VALID_ARCHS(true))
13 {
14 mach_port_t timer_port = MACH_PORT_NULL;
15 mach_port_t notify_port = MACH_PORT_NULL;
16
17 kern_return_t kr = KERN_SUCCESS;
18
19 // timer port
20 // This is a receive right which is also a kobject
21 timer_port = mk_timer_create();
22 T_ASSERT_NE(timer_port, (mach_port_t)MACH_PORT_NULL, "mk_timer_create: %s", mach_error_string(kr));
23
24 mach_port_set_context(mach_task_self(), timer_port, (mach_port_context_t) 0x1);
25 T_ASSERT_EQ(kr, KERN_SUCCESS, "mach_port_set_context(timer_port): %s", mach_error_string(kr));
26
27 // notification port for the mk_timer port to come back on
28 kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &notify_port);
29 T_ASSERT_EQ(kr, KERN_SUCCESS, "mach_port_allocate(notify_port): %s", mach_error_string(kr));
30
31 kr = mach_port_set_context(mach_task_self(), notify_port, (mach_port_context_t) 0x2);
32 T_ASSERT_EQ(kr, KERN_SUCCESS, "mach_port_set_context(notify_port): %s", mach_error_string(kr));
33
34 T_LOG("timer: 0x%x, notify: 0x%x", timer_port, notify_port);
35
36 mach_port_t previous = MACH_PORT_NULL;
37
38 // request a port-destroyed notification on the timer port
39 kr = mach_port_request_notification(mach_task_self(), timer_port, MACH_NOTIFY_PORT_DESTROYED,
40 0, notify_port, MACH_MSG_TYPE_MAKE_SEND_ONCE, &previous);
41 // this should fail!
42 T_ASSERT_NE(kr, KERN_SUCCESS, "notifications should NOT work on mk_timer ports!");
43
44 // destroy the timer port to send the notification
45 mach_port_mod_refs(mach_task_self(), timer_port, MACH_PORT_RIGHT_RECEIVE, -1);
46
47 // destroy the notification port
48 mach_port_mod_refs(mach_task_self(), notify_port, MACH_PORT_RIGHT_RECEIVE, -1);
49
50 T_LOG("done");
51 }