1/*
2 * Copyright (c) 2000-2004,2011-2012,2014 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25//
26// machrunloopserver - C++ shell for writing Mach 3 servers called by CFRunLoop
27//
28#include "machrunloopserver.h"
29#include <security_utilities/cfutilities.h>
30#include <mach/mach_error.h>
31#include <security_utilities/debugging.h>
32
33
34namespace Security {
35namespace MachPlusPlus {
36
37
38//
39// Generic Mach server
40//
41MachRunLoopServer::MachRunLoopServer(const char *name)
42	: MachServer(name), CFAutoPort(primaryServicePort())
43{
44}
45
46MachRunLoopServer::MachRunLoopServer(const char *name, const Bootstrap &boot)
47	: MachServer(name, boot), CFAutoPort(primaryServicePort())
48{
49}
50
51void MachRunLoopServer::run(mach_msg_size_t bufferSize, mach_msg_options_t options)
52{
53	// allocate reply buffer
54	mReplyMessage.setBuffer(bufferSize);
55
56	// enable reception
57	CFAutoPort::enable();
58
59	// we are it!
60	perThread().server = this;
61}
62
63
64MachRunLoopServer::~MachRunLoopServer()
65{
66    // no longer active on this thread
67    perThread().server = NULL;
68}
69
70
71//
72// Handle dead-port notifications.
73// Since we don't actually run our own runloop here, we can't well use standard
74// notifications to our own server port. So we use a CFMachPort facility instead.
75//
76void MachRunLoopServer::notifyIfDead(Port port, bool doNotify) const
77{
78	if (CFMachPortRef cfPort = CFMachPortCreateWithPort(NULL, port, NULL, NULL, NULL))
79		CFMachPortSetInvalidationCallBack(cfPort, cfInvalidate);
80}
81
82void MachRunLoopServer::cfInvalidate(CFMachPortRef cfPort, void *context)
83{
84	reinterpret_cast<MachRunLoopServer *>(context)->notifyDeadName(CFMachPortGetPort(cfPort));
85	//@@@ should we CFRelease cfPort here?
86}
87
88
89//
90// Reception callback
91//
92void MachRunLoopServer::receive(const Message &request)
93{
94	active().oneRequest(request);
95}
96
97void MachRunLoopServer::oneRequest(const Message &request)
98{
99	if (!handle(request, mReplyMessage)) {	// MIG dispatch failed
100		secdebug("machrls", "MachRunLoopServer dispatch failed");
101	} else {
102		// MIG dispatch handled the call. Send reply back to caller.
103		mReplyMessage.send((MACH_MSGH_BITS_REMOTE(mReplyMessage.bits()) == MACH_MSG_TYPE_MOVE_SEND_ONCE) ?
104			MACH_SEND_MSG :	MACH_SEND_MSG|MACH_SEND_TIMEOUT);
105	}
106	active().releaseDeferredAllocations();
107}
108
109
110} // end namespace MachPlusPlus
111} // end namespace Security
112