]> git.saurik.com Git - apple/configd.git/blob - configd.tproj/configd_server.c
configd-289.tar.gz
[apple/configd.git] / configd.tproj / configd_server.c
1 /*
2 * Copyright (c) 2000-2009 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 * Modification History
26 *
27 * March 9, 2004 Allan Nathanson <ajn@apple.com>
28 * - add DNS configuration server
29 *
30 * June 1, 2001 Allan Nathanson <ajn@apple.com>
31 * - public API conversion
32 *
33 * March 24, 2000 Allan Nathanson <ajn@apple.com>
34 * - initial revision
35 */
36
37 #include <sysexits.h>
38 #include <unistd.h>
39 #include <sys/types.h>
40 #include <servers/bootstrap.h>
41
42 #include "configd.h"
43 #include "configd_server.h"
44 #include "notify_server.h"
45 #include "session.h"
46
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 *);
50
51 #include "shared_dns_info_types.h"
52 #include "dnsinfo_server.h"
53
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 *);
57
58 /* configd server port (for new session requests) */
59 static CFMachPortRef configd_port = NULL;
60
61 __private_extern__
62 boolean_t
63 config_demux(mach_msg_header_t *request, mach_msg_header_t *reply)
64 {
65 Boolean processed = FALSE;
66
67 /*
68 * (attempt to) process SCDynamicStore requests.
69 */
70 processed = config_server(request, reply);
71 if (processed) {
72 return TRUE;
73 }
74
75 /*
76 * (attempt to) process DNS configuration requests.
77 */
78 processed = shared_dns_info_server(request, reply);
79 if (processed) {
80 return TRUE;
81 }
82
83 /*
84 * (attempt to) process (NO MORE SENDERS) notification messages.
85 */
86 processed = notify_server(request, reply);
87 if (processed) {
88 return TRUE;
89 }
90
91 /*
92 * unknown message ID, log and return an error.
93 */
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;
102
103 return FALSE;
104 }
105
106
107 #define MACH_MSG_BUFFER_SIZE 128
108
109
110 __private_extern__
111 void
112 configdCallback(CFMachPortRef port, void *msg, CFIndex size, void *info)
113 {
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;
119 int options;
120
121 if (bufSize == 0) {
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;
126 }
127
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);
133 }
134 }
135
136 if (bufSize > sizeof(bufReply_q)) {
137 bufReply = CFAllocatorAllocate(NULL, _config_subsystem.maxsize, 0);
138 }
139
140 /* we have a request message */
141 (void) config_demux(&bufRequest->Head, &bufReply->Head);
142
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)) {
148 /*
149 * destroy the request - but not the reply port
150 */
151 bufRequest->Head.msgh_remote_port = MACH_PORT_NULL;
152 mach_msg_destroy(&bufRequest->Head);
153 }
154 }
155
156 if (bufReply->Head.msgh_remote_port != MACH_PORT_NULL) {
157 /*
158 * send reply.
159 *
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.
167 */
168
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;
172 }
173 mr = mach_msg(&bufReply->Head, /* msg */
174 options, /* option */
175 bufReply->Head.msgh_size, /* send_size */
176 0, /* rcv_size */
177 MACH_PORT_NULL, /* rcv_name */
178 MACH_MSG_TIMEOUT_NONE, /* timeout */
179 MACH_PORT_NULL); /* notify */
180
181 /* Has a message error occurred? */
182 switch (mr) {
183 case MACH_SEND_INVALID_DEST:
184 case MACH_SEND_TIMED_OUT:
185 break;
186 default :
187 /* Includes success case. */
188 goto done;
189 }
190 }
191
192 if (bufReply->Head.msgh_bits & MACH_MSGH_BITS_COMPLEX) {
193 mach_msg_destroy(&bufReply->Head);
194 }
195
196 done :
197
198 if (bufReply != (mig_reply_error_t *)bufReply_q)
199 CFAllocatorDeallocate(NULL, bufReply);
200 return;
201 }
202
203
204 static CFStringRef
205 serverMPCopyDescription(const void *info)
206 {
207 return CFStringCreateWithFormat(NULL, NULL, CFSTR("<main DynamicStore MP>"));
208 }
209
210
211 __private_extern__
212 void
213 server_init()
214 {
215 serverSessionRef mySession;
216 CFRunLoopSourceRef rls;
217 char *service_name;
218 mach_port_t service_port = MACH_PORT_NULL;
219 kern_return_t status;
220
221 service_name = getenv("SCD_SERVER");
222 if (!service_name) {
223 service_name = SCD_SERVER;
224 }
225
226 /* Check "configd" server status */
227 status = bootstrap_check_in(bootstrap_port, service_name, &service_port);
228 switch (status) {
229 case BOOTSTRAP_SUCCESS :
230 /* if we are being [re-]started by launchd */
231 break;
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);
240 default :
241 SCLog(TRUE, LOG_ERR,
242 CFSTR("server_init bootstrap_check_in() failed: %s"),
243 bootstrap_strerror(status));
244 exit (EX_UNAVAILABLE);
245 }
246
247 /* Create the primary / new connection port and backing session */
248 mySession = addSession(service_port, serverMPCopyDescription);
249 configd_port = mySession->serverPort;
250
251 /*
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
256 * session.
257 */
258 rls = CFMachPortCreateRunLoopSource(NULL, configd_port, 0);
259 CFRunLoopAddSource(CFRunLoopGetCurrent(), rls, kCFRunLoopDefaultMode);
260 CFRunLoopAddSource(CFRunLoopGetCurrent(), rls, CFSTR("locked"));
261 CFRelease(rls);
262
263 return;
264 }
265
266
267 __private_extern__
268 int
269 server_shutdown()
270 {
271 if (configd_port != NULL) {
272 mach_port_t service_port;
273
274 service_port = CFMachPortGetPort(configd_port);
275 if (service_port != MACH_PORT_NULL) {
276 (void) mach_port_mod_refs(mach_task_self(),
277 service_port,
278 MACH_PORT_RIGHT_RECEIVE,
279 -1);
280 }
281
282 CFMachPortInvalidate(configd_port);
283 CFRelease(configd_port);
284 configd_port = NULL;
285 }
286
287 return EX_OK;
288 }
289
290
291 __private_extern__
292 void
293 server_loop()
294 {
295 CFStringRef rlMode;
296
297 while (TRUE) {
298 /*
299 * process one run loop event
300 */
301 rlMode = (storeLocked > 0) ? CFSTR("locked") : kCFRunLoopDefaultMode;
302 CFRunLoopRunInMode(rlMode, 1.0e10, TRUE);
303
304 /*
305 * check for, and if necessary, push out change notifications
306 * to other processes.
307 */
308 pushNotifications(_configd_trace);
309 }
310 }