1 #include "eventlistener.h"
5 namespace SecurityServer
{
7 EventListener::EventListener (CssmAllocator
&standard
, CssmAllocator
&returning
) : mClientSession (standard
, returning
),
9 mRunLoopSourceRef (NULL
)
17 EventListener::~EventListener ()
19 if (mMachPortRef
!= NULL
)
21 mach_port_t mp
= CFMachPortGetPort (mMachPortRef
);
22 mClientSession
.stopNotification (mp
);
23 CFRelease (mMachPortRef
);
26 if (mRunLoopSourceRef
!= NULL
)
28 CFRelease (mRunLoopSourceRef
);
34 void EventListener::Callback (CFMachPortRef port
, void *msg
, CFIndex size
, void *info
)
36 reinterpret_cast<EventListener
*>(info
)->HandleCallback (port
, msg
, size
);
41 void EventListener::Initialize ()
43 // create a callback information structure
44 CFMachPortContext context
= {1, this, NULL
, NULL
, NULL
};
46 // create the CFMachPort
47 mMachPortRef
= CFMachPortCreate (NULL
, Callback
, &context
, NULL
);
48 if (mMachPortRef
== NULL
)
53 // set the buffer limit for the port
54 mach_port_t mp
= CFMachPortGetPort (mMachPortRef
);
56 mach_port_limits_t limits
;
57 limits
.mpl_qlimit
= MACH_PORT_QLIMIT_MAX
;
58 kern_return_t result
=
59 mach_port_set_attributes (mach_task_self (), mp
, MACH_PORT_LIMITS_INFO
,
60 mach_port_info_t (&limits
), MACH_PORT_LIMITS_INFO_COUNT
);
62 if (result
!= KERN_SUCCESS
)
64 secdebug ("notify", "Got error %d when trying to maximize queue size", result
);
67 // make a run loop source for this ref
68 mRunLoopSourceRef
= CFMachPortCreateRunLoopSource (NULL
, mMachPortRef
, NULL
);
69 if (mRunLoopSourceRef
== NULL
)
71 CFRelease (mMachPortRef
);
75 // attach this run loop source to the main run loop
76 CFRunLoopAddSource (CFRunLoopGetCurrent (), mRunLoopSourceRef
, kCFRunLoopDefaultMode
);
78 // extract the actual port from the run loop, and request callbacks on that port
79 mClientSession
.requestNotification (mp
, Listener::databaseNotifications
,
85 void EventListener::HandleCallback (CFMachPortRef port
, void *msg
, CFIndex size
)
87 // we need to parse the message and see what happened
88 mClientSession
.dispatchNotification (reinterpret_cast<mach_msg_header_t
*>(msg
), ProcessMessage
, this);
93 OSStatus
EventListener::ProcessMessage (Listener::Domain domain
, Listener::Event event
, const void *data
, size_t dataLength
, void *context
)
95 reinterpret_cast<EventListener
*>(context
)->EventReceived (domain
, event
, data
, dataLength
);
101 void EventListener::RequestEvents (Listener::Domain whichDomain
, Listener::EventMask whichEvents
)
103 // stop the old event request and change to the new one
104 mach_port_t mp
= CFMachPortGetPort (mMachPortRef
);
105 mClientSession
.stopNotification (mp
);
106 mClientSession
.requestNotification (mp
, whichDomain
, whichEvents
);
111 void EventListener::EventReceived (Listener::Domain domain
, Listener::Event event
, const void* data
, size_t dataLength
)