2 * Copyright (c) 2000-2009 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@
25 * Modification History
27 * March 9, 2004 Allan Nathanson <ajn@apple.com>
28 * - add DNS configuration server
30 * June 1, 2001 Allan Nathanson <ajn@apple.com>
31 * - public API conversion
33 * March 24, 2000 Allan Nathanson <ajn@apple.com>
39 #include <sys/types.h>
40 #include <servers/bootstrap.h>
43 #include "configd_server.h"
44 #include "notify_server.h"
47 /* MiG generated externals and functions */
48 extern struct mig_subsystem _config_subsystem
;
49 extern boolean_t
config_server(mach_msg_header_t
*, mach_msg_header_t
*);
51 #include "shared_dns_info_types.h"
52 #include "dnsinfo_server.h"
54 /* MiG generated externals and functions */
55 extern struct mig_subsystem _shared_dns_info_subsystem
;
56 extern boolean_t
shared_dns_info_server(mach_msg_header_t
*, mach_msg_header_t
*);
58 /* configd server port (for new session requests) */
59 static CFMachPortRef configd_port
= NULL
;
63 config_demux(mach_msg_header_t
*request
, mach_msg_header_t
*reply
)
65 Boolean processed
= FALSE
;
68 * (attempt to) process SCDynamicStore requests.
70 processed
= config_server(request
, reply
);
76 * (attempt to) process DNS configuration requests.
78 processed
= shared_dns_info_server(request
, reply
);
84 * (attempt to) process (NO MORE SENDERS) notification messages.
86 processed
= notify_server(request
, reply
);
92 * unknown message ID, log and return an error.
94 SCLog(TRUE
, LOG_ERR
, CFSTR("config_demux(): unknown message ID (%d) received"), request
->msgh_id
);
95 reply
->msgh_bits
= MACH_MSGH_BITS(MACH_MSGH_BITS_REMOTE(request
->msgh_bits
), 0);
96 reply
->msgh_remote_port
= request
->msgh_remote_port
;
97 reply
->msgh_size
= sizeof(mig_reply_error_t
); /* Minimal size */
98 reply
->msgh_local_port
= MACH_PORT_NULL
;
99 reply
->msgh_id
= request
->msgh_id
+ 100;
100 ((mig_reply_error_t
*)reply
)->NDR
= NDR_record
;
101 ((mig_reply_error_t
*)reply
)->RetCode
= MIG_BAD_ID
;
107 #define MACH_MSG_BUFFER_SIZE 128
112 configdCallback(CFMachPortRef port
, void *msg
, CFIndex size
, void *info
)
114 mig_reply_error_t
* bufRequest
= msg
;
115 uint32_t bufReply_q
[MACH_MSG_BUFFER_SIZE
/sizeof(uint32_t)];
116 mig_reply_error_t
* bufReply
= (mig_reply_error_t
*)bufReply_q
;
117 static CFIndex bufSize
= 0;
118 mach_msg_return_t mr
;
122 // get max size for MiG reply buffers
123 bufSize
= _config_subsystem
.maxsize
;
124 if (_shared_dns_info_subsystem
.maxsize
> bufSize
) {
125 bufSize
= _shared_dns_info_subsystem
.maxsize
;
128 // check if our on-the-stack reply buffer will be big enough
129 if (bufSize
> sizeof(bufReply_q
)) {
130 SCLog(TRUE
, LOG_NOTICE
,
131 CFSTR("configdCallback(): buffer size should be increased > %d"),
132 _config_subsystem
.maxsize
);
136 if (bufSize
> sizeof(bufReply_q
)) {
137 bufReply
= CFAllocatorAllocate(NULL
, _config_subsystem
.maxsize
, 0);
140 /* we have a request message */
141 (void) config_demux(&bufRequest
->Head
, &bufReply
->Head
);
143 if (!(bufReply
->Head
.msgh_bits
& MACH_MSGH_BITS_COMPLEX
)) {
144 if (bufReply
->RetCode
== MIG_NO_REPLY
) {
145 bufReply
->Head
.msgh_remote_port
= MACH_PORT_NULL
;
146 } else if ((bufReply
->RetCode
!= KERN_SUCCESS
) &&
147 (bufRequest
->Head
.msgh_bits
& MACH_MSGH_BITS_COMPLEX
)) {
149 * destroy the request - but not the reply port
151 bufRequest
->Head
.msgh_remote_port
= MACH_PORT_NULL
;
152 mach_msg_destroy(&bufRequest
->Head
);
156 if (bufReply
->Head
.msgh_remote_port
!= MACH_PORT_NULL
) {
160 * We don't want to block indefinitely because the client
161 * isn't receiving messages from the reply port.
162 * If we have a send-once right for the reply port, then
163 * this isn't a concern because the send won't block.
164 * If we have a send right, we need to use MACH_SEND_TIMEOUT.
165 * To avoid falling off the kernel's fast RPC path unnecessarily,
166 * we only supply MACH_SEND_TIMEOUT when absolutely necessary.
169 options
= MACH_SEND_MSG
;
170 if (MACH_MSGH_BITS_REMOTE(bufReply
->Head
.msgh_bits
) != MACH_MSG_TYPE_MOVE_SEND_ONCE
) {
171 options
|= MACH_SEND_TIMEOUT
;
173 mr
= mach_msg(&bufReply
->Head
, /* msg */
174 options
, /* option */
175 bufReply
->Head
.msgh_size
, /* send_size */
177 MACH_PORT_NULL
, /* rcv_name */
178 MACH_MSG_TIMEOUT_NONE
, /* timeout */
179 MACH_PORT_NULL
); /* notify */
181 /* Has a message error occurred? */
183 case MACH_SEND_INVALID_DEST
:
184 case MACH_SEND_TIMED_OUT
:
187 /* Includes success case. */
192 if (bufReply
->Head
.msgh_bits
& MACH_MSGH_BITS_COMPLEX
) {
193 mach_msg_destroy(&bufReply
->Head
);
198 if (bufReply
!= (mig_reply_error_t
*)bufReply_q
)
199 CFAllocatorDeallocate(NULL
, bufReply
);
205 serverMPCopyDescription(const void *info
)
207 return CFStringCreateWithFormat(NULL
, NULL
, CFSTR("<main DynamicStore MP>"));
215 serverSessionRef mySession
;
216 CFRunLoopSourceRef rls
;
218 mach_port_t service_port
= MACH_PORT_NULL
;
219 kern_return_t status
;
221 service_name
= getenv("SCD_SERVER");
223 service_name
= SCD_SERVER
;
226 /* Check "configd" server status */
227 status
= bootstrap_check_in(bootstrap_port
, service_name
, &service_port
);
229 case BOOTSTRAP_SUCCESS
:
230 /* if we are being [re-]started by launchd */
232 case BOOTSTRAP_NOT_PRIVILEGED
:
233 /* if another instance of the server is starting */
234 SCLog(TRUE
, LOG_ERR
, CFSTR("'%s' server already starting"), service_name
);
235 exit (EX_UNAVAILABLE
);
236 case BOOTSTRAP_SERVICE_ACTIVE
:
237 /* if another instance of the server is active */
238 SCLog(TRUE
, LOG_ERR
, CFSTR("'%s' server already active"), service_name
);
239 exit (EX_UNAVAILABLE
);
242 CFSTR("server_init bootstrap_check_in() failed: %s"),
243 bootstrap_strerror(status
));
244 exit (EX_UNAVAILABLE
);
247 /* Create the primary / new connection port and backing session */
248 mySession
= addSession(service_port
, serverMPCopyDescription
);
249 configd_port
= mySession
->serverPort
;
252 * Create and add a run loop source for the port and add this source
253 * to both the default run loop mode and the "locked" mode. These two
254 * modes will be used for normal (unlocked) communication with the
255 * server and when multiple (locked) updates are requested by a single
258 rls
= CFMachPortCreateRunLoopSource(NULL
, configd_port
, 0);
259 CFRunLoopAddSource(CFRunLoopGetCurrent(), rls
, kCFRunLoopDefaultMode
);
260 CFRunLoopAddSource(CFRunLoopGetCurrent(), rls
, CFSTR("locked"));
271 if (configd_port
!= NULL
) {
272 mach_port_t service_port
;
274 service_port
= CFMachPortGetPort(configd_port
);
275 if (service_port
!= MACH_PORT_NULL
) {
276 (void) mach_port_mod_refs(mach_task_self(),
278 MACH_PORT_RIGHT_RECEIVE
,
282 CFMachPortInvalidate(configd_port
);
283 CFRelease(configd_port
);
299 * process one run loop event
301 rlMode
= (storeLocked
> 0) ? CFSTR("locked") : kCFRunLoopDefaultMode
;
302 CFRunLoopRunInMode(rlMode
, 1.0e10
, TRUE
);
305 * check for, and if necessary, push out change notifications
306 * to other processes.
308 pushNotifications(_configd_trace
);