]>
git.saurik.com Git - apple/security.git/blob - Security/libsecurity_utilities/lib/cfmach++.cpp
2 * Copyright (c) 2000-2004,2011-2012,2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
26 // cfmach++ - a marriage of CoreFoundation with Mach/C++
28 #include <security_utilities/cfmach++.h>
32 namespace MachPlusPlus
{
36 // Construct CFAutoPorts
38 CFAutoPort::CFAutoPort()
42 CFAutoPort::CFAutoPort(mach_port_t p
)
43 : Port(p
), mEnabled(false)
48 // On destruction, make sure we're disengaged from the CFRunLoop
50 CFAutoPort::~CFAutoPort()
54 // invalidate everything
57 CFMachPortInvalidate(mPort
);
58 CFRunLoopSourceInvalidate(mSource
);
64 // enable() will lazily allocate needed resources, then click into the runloop
66 void CFAutoPort::enable()
72 // first-time creation of CF resources
73 CFMachPortContext ctx
= { 1, this, NULL
, NULL
, NULL
};
74 CFMachPortRef machPort
= CFMachPortCreateWithPort(NULL
, port(), cfCallback
, &ctx
, NULL
);
77 // using take here because "assignment" causes an extra retain, which will make the
78 // CF objects leak when this data structure goes away.
81 CFRunLoopSourceRef sr
= CFMachPortCreateRunLoopSource(NULL
, mPort
, 10);
84 if (!mPort
|| !mSource
)
85 CFError::throwMe(); // CF won't tell us why...
87 CFRunLoopAddSource(CFRunLoopGetCurrent(), mSource
, kCFRunLoopCommonModes
);
89 secdebug("autoport", "%p enabled", this);
95 // Disable() just removes us from the runloop. All the other resources stay
96 // around, ready to be re-enable()d.
98 void CFAutoPort::disable()
101 CFRunLoopRemoveSource(CFRunLoopGetCurrent(), mSource
, kCFRunLoopCommonModes
);
103 secdebug("autoport", "%p disabled", this);
109 // The CF-sponsored port callback.
110 // We pass this to our receive() virtual and eat all exceptions.
112 static int gNumTimesCalled
= 0;
114 void CFAutoPort::cfCallback(CFMachPortRef cfPort
, void *msg
, CFIndex size
, void *context
)
117 secdebug("adhoc", "Callback was called %d times.", gNumTimesCalled
);
119 #warning Cast to mach_msg_size_t may loose precision
120 Message
message(msg
, (mach_msg_size_t
)size
);
122 reinterpret_cast<CFAutoPort
*>(context
)->receive(message
);
124 secdebug("autoport", "%p receive handler failed with exception", context
);
129 } // end namespace MachPlusPlus
130 } // end namespace Security