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 // mach++ - C++ bindings for useful Mach primitives
31 #include <security_utilities/utilities.h>
32 #include <security_utilities/errors.h>
33 #include <security_utilities/threading.h>
34 #include <security_utilities/globalizer.h>
35 #include <security_utilities/debugging_internal.h>
36 #include <mach/mach.h>
37 #include <servers/bootstrap.h>
40 // yes, we use some UNIX (non-mach) headers...
41 #include <sys/types.h>
45 namespace MachPlusPlus
{
49 // Exceptions thrown by the mach++ interface.
51 class Error
: public CommonError
{
53 // actually, kern_return_t can be just about any subsystem type return code
54 Error(kern_return_t err
);
56 virtual ~Error() _NOEXCEPT
;
58 virtual OSStatus
osStatus() const;
59 virtual int unixError() const;
61 const kern_return_t error
;
63 static void check(kern_return_t err
);
64 static void throwMe(kern_return_t err
) __attribute__((noreturn
));
67 // generic return code checker
68 inline void check(kern_return_t status
)
69 { Error::check(status
); }
73 // Simple vm_allocate/deallocate glue
75 void *allocate(size_t size
);
76 void deallocate(vm_address_t addr
, size_t size
);
78 inline void deallocate(const void *addr
, size_t size
)
79 { deallocate(reinterpret_cast<vm_address_t
>(addr
), size
); }
83 // An encapsulation of a Mach 3 port
87 static mach_port_t
self() { return mach_task_self(); }
90 Port() { mPort
= MACH_PORT_NULL
; }
91 Port(mach_port_t port
) { mPort
= port
; }
93 // devolve to Mach primitive type
94 operator mach_port_t () const { return mPort
; }
96 // access reference (for primitives storing into &mach_port_t)
97 mach_port_t
&port () { return mPort
; }
98 const mach_port_t
&port () const { return mPort
; }
101 mach_port_type_t
type() const
102 { mach_port_type_t typ
; check(mach_port_type(self(), mPort
, &typ
)); return typ
; }
104 bool isType(mach_port_type_t typ
) const { return type() & typ
; }
105 bool isDead() const { return isType(MACH_PORT_TYPE_DEAD_NAME
); }
107 // port allocation and management
108 void allocate(mach_port_right_t right
= MACH_PORT_RIGHT_RECEIVE
)
109 { check(mach_port_allocate(self(), right
, &mPort
)); }
112 * mach_port_deallocate() only deallocates send, send-once, dead-name, or port-set.
113 * Since allocate() defaults to receive, allocate() and deallocate() do not actually
114 * balance each other; deallocate() will fail with an invalid-right error.
116 void deallocate() { check(mach_port_deallocate(self(), mPort
)); mPort
= MACH_PORT_NULL
;}
118 void insertRight(mach_msg_type_name_t type
)
119 { check(mach_port_insert_right(self(), mPort
, mPort
, type
)); }
121 void modRefs(mach_port_right_t right
, mach_port_delta_t delta
= 1)
122 { check(mach_port_mod_refs(self(), mPort
, right
, delta
)); }
124 mach_port_urefs_t
getRefs(mach_port_right_t right
);
126 // port notification interface
127 mach_port_t
requestNotify(mach_port_t notify
,
128 mach_msg_id_t type
= MACH_NOTIFY_DEAD_NAME
, mach_port_mscount_t sync
= 1);
129 mach_port_t
cancelNotify(mach_msg_id_t type
= MACH_NOTIFY_DEAD_NAME
);
131 // queue state management
132 mach_port_msgcount_t
qlimit() const;
133 void qlimit(mach_port_msgcount_t limit
);
135 IFDUMP(void dump(const char *name
= NULL
));
143 // Ports representing PortSets
145 class PortSet
: public Port
{
147 PortSet() { allocate(MACH_PORT_RIGHT_PORT_SET
); }
148 ~PortSet() { deallocate(); }
150 void operator += (const Port
&port
)
151 { check(mach_port_move_member(self(), port
, mPort
)); }
153 void operator -= (const Port
&port
)
154 { check(mach_port_move_member(self(), port
, MACH_PORT_NULL
)); }
156 set
<Port
> members() const;
157 bool contains(Port member
) const; // relatively slow
162 // Ports that are bootstrap ports
164 class Bootstrap
: public Port
{
166 Bootstrap() { check(task_get_bootstrap_port(mach_task_self(), &mPort
)); }
167 Bootstrap(mach_port_t bootp
) : Port(bootp
) { }
169 mach_port_t
checkIn(const char *name
) const;
170 mach_port_t
checkInOptional(const char *name
) const;
172 void registerAs(mach_port_t port
, const char *name
) const;
174 mach_port_t
lookup(const char *name
) const;
175 mach_port_t
lookup2(const char *name
) const;
176 mach_port_t
lookupOptional(const char *name
) const;
178 Bootstrap
subset(Port requestor
);
183 // officially, the register/lookup IPCs take an array of 128 characters (not a zero-end string)
184 mutable char nameBuffer
[BOOTSTRAP_MAX_NAME_LEN
];
187 char *makeName(const char *s
) const
188 { return strncpy(nameBuffer
, s
, BOOTSTRAP_MAX_NAME_LEN
); }
193 // Ports that are Task Ports
195 class TaskPort
: public Port
{
197 TaskPort() { mPort
= self(); }
198 TaskPort(mach_port_t p
) : Port(p
) { }
199 TaskPort(const Port
&p
) : Port(p
) { }
202 Bootstrap
bootstrap() const
203 { mach_port_t boot
; check(task_get_bootstrap_port(mPort
, &boot
)); return boot
; }
204 void bootstrap(Bootstrap boot
)
205 { check(task_set_bootstrap_port(mPort
, boot
)); }
212 // Ports that are are self-allocated and have receive rights
214 class ReceivePort
: public Port
{
216 ReceivePort() { allocate(); }
217 ReceivePort(const char *name
, const Bootstrap
&bootstrap
, bool tryCheckin
= true);
218 ~ReceivePort() { modRefs(MACH_PORT_RIGHT_RECEIVE
, -1); }
223 // A little stack utility for temporarily switching your bootstrap around.
224 // Essentially, it restores your bootstrap port when it dies. Since the
225 // "current bootstrap port" is a process-global item, this uses a global
226 // zone of exclusion (aka critical region). There's no protection against
227 // someone else calling the underlying system service, of course.
231 StBootstrap(const Bootstrap
&boot
, const TaskPort
&task
= TaskPort());
237 StLock
<Mutex
> locker
;
238 static ModuleNexus
<Mutex
> critical
; // critical region guard (of a sort)
243 // A Mach-level memory guard.
244 // This will vm_deallocate its argument when it gets destroyed.
248 VMGuard(void *addr
, size_t length
) : mAddr(addr
), mLength(length
) { }
249 ~VMGuard() { deallocate(mAddr
, mLength
); }
258 // Message buffers for Mach messages.
259 // The logic here is somewhat inverted from the usual: send/receive
260 // are methods on the buffers (rather than buffers being arguments to send/receive).
261 // It's rather handy once you get used to that view.
265 Message(void *buffer
, mach_msg_size_t size
); // use buffer with size
266 Message(mach_msg_size_t size
); // allocate buffer with size
267 Message(); // set buffer later
270 void setBuffer(void *buffer
, mach_msg_size_t size
); // use buffer with size
271 void setBuffer(mach_msg_size_t size
); // allocate buffer with size
272 void clearBuffer(void);
273 void release(); // discard buffer (if any)
275 operator mig_reply_error_t
& () const { return *mBuffer
; }
276 operator mach_msg_header_t
& () const { return mBuffer
->Head
; }
277 operator mig_reply_error_t
* () const { return mBuffer
; }
278 operator mach_msg_header_t
* () const { return &mBuffer
->Head
; }
279 operator NDR_record_t
& () const { return mBuffer
->NDR
; }
281 void *data() const { return mBuffer
; }
282 mach_msg_size_t
length() const { return mBuffer
->Head
.msgh_size
; }
283 Port
localPort() const { return mBuffer
->Head
.msgh_local_port
; }
284 Port
remotePort() const { return mBuffer
->Head
.msgh_remote_port
; }
285 mach_msg_id_t
msgId() const { return mBuffer
->Head
.msgh_id
; }
286 mach_msg_bits_t
bits() const { return mBuffer
->Head
.msgh_bits
; }
287 kern_return_t
returnCode() const { return mBuffer
->RetCode
; }
288 mach_msg_audit_trailer_t
*auditTrailer();
290 void localPort(mach_port_t p
) { mBuffer
->Head
.msgh_local_port
= p
; }
291 void remotePort(mach_port_t p
) { mBuffer
->Head
.msgh_remote_port
= p
; }
294 bool send(mach_msg_option_t options
= 0,
295 mach_msg_timeout_t timeout
= MACH_MSG_TIMEOUT_NONE
,
296 mach_port_name_t notify
= MACH_PORT_NULL
);
297 bool receive(mach_port_t receivePort
,
298 mach_msg_option_t options
= 0,
299 mach_msg_timeout_t timeout
= MACH_MSG_TIMEOUT_NONE
,
300 mach_port_name_t notify
= MACH_PORT_NULL
);
301 bool sendReceive(mach_port_t receivePort
,
302 mach_msg_option_t options
= 0,
303 mach_msg_timeout_t timeout
= MACH_MSG_TIMEOUT_NONE
,
304 mach_port_name_t notify
= MACH_PORT_NULL
);
306 void destroy() { mach_msg_destroy(*this); }
309 bool check(kern_return_t status
);
312 mig_reply_error_t
*mBuffer
;
313 mach_msg_size_t mSize
;
318 } // end namespace MachPlusPlus
319 } // end namespace Security