]> git.saurik.com Git - apple/configd.git/blob - SystemConfiguration.fproj/SCDNotifierInformViaCallback.c
f5cac0806bf071542a354f2f7a134cdae3dfd6a2
[apple/configd.git] / SystemConfiguration.fproj / SCDNotifierInformViaCallback.c
1 /*
2 * Copyright (c) 2000-2003 Apple Computer, 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 * June 1, 2001 Allan Nathanson <ajn@apple.com>
28 * - public API conversion
29 *
30 * March 31, 2000 Allan Nathanson <ajn@apple.com>
31 * - initial revision
32 */
33
34 #include <mach/mach.h>
35 #include <mach/mach_error.h>
36
37 #include <SystemConfiguration/SystemConfiguration.h>
38 #include <SystemConfiguration/SCPrivate.h>
39 #include "SCDynamicStoreInternal.h"
40 #include "config.h" /* MiG generated file */
41
42 static void
43 informCallback(CFMachPortRef port, void *msg, CFIndex size, void *info)
44 {
45 SCDynamicStoreRef store = (SCDynamicStoreRef)info;
46 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
47 mach_msg_empty_rcv_t *buf = msg;
48 mach_msg_id_t msgid = buf->header.msgh_id;
49 SCDynamicStoreCallBack_v1 cbFunc = storePrivate->callbackFunction;
50 void *cbArg = storePrivate->callbackArgument;
51
52 if (msgid == MACH_NOTIFY_NO_SENDERS) {
53 /* the server died, disable additional callbacks */
54 SCLog(_sc_verbose, LOG_DEBUG, CFSTR(" notifier port closed, disabling notifier"));
55 } else if (cbFunc == NULL) {
56 /* there is no (longer) a callback function, disable additional callbacks */
57 SCLog(_sc_verbose, LOG_DEBUG, CFSTR(" no callback function, disabling notifier"));
58 } else {
59 SCLog(_sc_verbose, LOG_DEBUG, CFSTR(" executing notifiction function"));
60 if ((*cbFunc)(store, cbArg)) {
61 /*
62 * callback function returned success.
63 */
64 return;
65 } else {
66 SCLog(_sc_verbose, LOG_DEBUG, CFSTR(" callback returned error, disabling notifier"));
67 }
68 }
69
70 #ifdef DEBUG
71 if (port != storePrivate->callbackPort) {
72 SCLog(_sc_verbose, LOG_DEBUG, CFSTR("informCallback, why is port != callbackPort?"));
73 }
74 #endif /* DEBUG */
75
76 /* remove the run loop source */
77 CFRunLoopRemoveSource(storePrivate->callbackRunLoop,
78 storePrivate->callbackRunLoopSource,
79 kCFRunLoopDefaultMode);
80 CFRelease(storePrivate->callbackRunLoopSource);
81 storePrivate->callbackRunLoop = NULL;
82 storePrivate->callbackRunLoopSource = NULL;
83
84 /* invalidate port */
85 CFMachPortInvalidate(storePrivate->callbackPort);
86 CFRelease(storePrivate->callbackPort);
87 storePrivate->callbackPort = NULL;
88
89 /* disable notifier */
90 storePrivate->notifyStatus = NotifierNotRegistered;
91 storePrivate->callbackArgument = NULL;
92 storePrivate->callbackFunction = NULL;
93
94 return;
95 }
96
97
98 Boolean
99 SCDynamicStoreNotifyCallback(SCDynamicStoreRef store,
100 CFRunLoopRef runLoop,
101 SCDynamicStoreCallBack_v1 func,
102 void *arg)
103 {
104 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
105 kern_return_t status;
106 mach_port_t port;
107 mach_port_t oldNotify;
108 int sc_status;
109 CFMachPortContext context = { 0
110 , (void *)store
111 , CFRetain
112 , CFRelease
113 , NULL
114 };
115
116 SCLog(_sc_verbose, LOG_DEBUG, CFSTR("SCDynamicStoreNotifyCallback:"));
117
118 if (!store) {
119 /* sorry, you must provide a session */
120 _SCErrorSet(kSCStatusNoStoreSession);
121 return FALSE;
122 }
123
124 if (storePrivate->server == MACH_PORT_NULL) {
125 /* sorry, you must have an open session to play */
126 _SCErrorSet(kSCStatusNoStoreServer);
127 return FALSE;
128 }
129
130 if (storePrivate->notifyStatus != NotifierNotRegistered) {
131 /* sorry, you can only have one notification registered at once */
132 _SCErrorSet(kSCStatusNotifierActive);
133 return FALSE;
134 }
135
136 /* Allocating port (for server response) */
137 storePrivate->callbackPort = CFMachPortCreate(NULL,
138 informCallback,
139 &context,
140 NULL);
141
142 /* Request a notification when/if the server dies */
143 port = CFMachPortGetPort(storePrivate->callbackPort);
144 status = mach_port_request_notification(mach_task_self(),
145 port,
146 MACH_NOTIFY_NO_SENDERS,
147 1,
148 port,
149 MACH_MSG_TYPE_MAKE_SEND_ONCE,
150 &oldNotify);
151 if (status != KERN_SUCCESS) {
152 SCLog(_sc_verbose, LOG_DEBUG, CFSTR("mach_port_request_notification(): %s"), mach_error_string(status));
153 CFMachPortInvalidate(storePrivate->callbackPort);
154 CFRelease(storePrivate->callbackPort);
155 _SCErrorSet(status);
156 return FALSE;
157 }
158
159 if (oldNotify != MACH_PORT_NULL) {
160 SCLog(_sc_verbose, LOG_ERR, CFSTR("SCDynamicStoreNotifyCallback(): why is oldNotify != MACH_PORT_NULL?"));
161 }
162
163 /* Requesting notification via mach port */
164 status = notifyviaport(storePrivate->server,
165 port,
166 0,
167 (int *)&sc_status);
168
169 if (status != KERN_SUCCESS) {
170 if (status != MACH_SEND_INVALID_DEST)
171 SCLog(_sc_verbose, LOG_DEBUG, CFSTR("notifyviaport(): %s"), mach_error_string(status));
172 CFMachPortInvalidate(storePrivate->callbackPort);
173 CFRelease(storePrivate->callbackPort);
174 (void) mach_port_destroy(mach_task_self(), storePrivate->server);
175 storePrivate->server = MACH_PORT_NULL;
176 _SCErrorSet(status);
177 return FALSE;
178 }
179
180 if (sc_status != kSCStatusOK) {
181 _SCErrorSet(sc_status);
182 return FALSE;
183 }
184
185 /* set notifier active */
186 storePrivate->notifyStatus = Using_NotifierInformViaCallback;
187
188 /* Creating/adding a run loop source for the port */
189 storePrivate->callbackArgument = arg;
190 storePrivate->callbackFunction = func;
191 storePrivate->callbackRunLoop = runLoop;
192 storePrivate->callbackRunLoopSource =
193 CFMachPortCreateRunLoopSource(NULL, storePrivate->callbackPort, 0);
194
195 CFRunLoopAddSource(storePrivate->callbackRunLoop,
196 storePrivate->callbackRunLoopSource,
197 kCFRunLoopDefaultMode);
198
199 return TRUE;
200 }
201
202
203 static void
204 rlsCallback(CFMachPortRef port, void *msg, CFIndex size, void *info)
205 {
206 mach_msg_empty_rcv_t *buf = msg;
207 mach_msg_id_t msgid = buf->header.msgh_id;
208 SCDynamicStoreRef store = (SCDynamicStoreRef)info;
209 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
210
211 if (msgid == MACH_NOTIFY_NO_SENDERS) {
212 /* the server died, disable additional callbacks */
213 SCLog(_sc_verbose, LOG_INFO, CFSTR(" rlsCallback(), notifier port closed"));
214
215 #ifdef DEBUG
216 if (port != storePrivate->callbackPort) {
217 SCLog(_sc_verbose, LOG_DEBUG, CFSTR("rlsCallback(), why is port != callbackPort?"));
218 }
219 #endif /* DEBUG */
220
221 /* remove the run loop source(s) */
222 CFRunLoopSourceInvalidate(storePrivate->callbackRunLoopSource);
223 CFRelease(storePrivate->callbackRunLoopSource);
224 storePrivate->callbackRunLoopSource = NULL;
225
226 /* invalidate port */
227 CFMachPortInvalidate(storePrivate->callbackPort);
228 CFRelease(storePrivate->callbackPort);
229 storePrivate->callbackPort = NULL;
230
231 return;
232 }
233
234 /* signal the real runloop source */
235 CFRunLoopSourceSignal(storePrivate->rls);
236 return;
237 }
238
239
240 static void
241 rlsPortInvalidate(CFMachPortRef mp, void *info) {
242 mach_port_t port = CFMachPortGetPort(mp);
243
244 // A simple deallocate won't get rid of all the references we've accumulated
245 SCLog(_sc_verbose, LOG_DEBUG, CFSTR(" invalidate = %d"), port);
246 (void)mach_port_mod_refs(mach_task_self(), port, MACH_PORT_RIGHT_RECEIVE, -1);
247 }
248
249
250 static void
251 rlsSchedule(void *info, CFRunLoopRef rl, CFStringRef mode)
252 {
253 SCDynamicStoreRef store = (SCDynamicStoreRef)info;
254 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
255
256 SCLog(_sc_verbose, LOG_DEBUG, CFSTR("schedule notifications for mode %@"), mode);
257
258 if (storePrivate->rlsRefs++ == 0) {
259 CFMachPortContext context = { 0
260 , (void *)store
261 , CFRetain
262 , CFRelease
263 , NULL
264 };
265 mach_port_t oldNotify;
266 mach_port_t port;
267 int sc_status;
268 kern_return_t status;
269
270 SCLog(_sc_verbose, LOG_DEBUG, CFSTR(" activate callback runloop source"));
271
272 /* Allocating port (for server response) */
273 status = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &port);
274 if (status != KERN_SUCCESS) {
275 SCLog(_sc_verbose, LOG_DEBUG, CFSTR("mach_port_allocate(): %s"), mach_error_string(status));
276 return;
277 }
278 SCLog(_sc_verbose, LOG_DEBUG, CFSTR(" port = %d"), port);
279
280 status = mach_port_insert_right(mach_task_self(),
281 port,
282 port,
283 MACH_MSG_TYPE_MAKE_SEND);
284 if (status != KERN_SUCCESS) {
285 SCLog(_sc_verbose, LOG_DEBUG, CFSTR("mach_port_insert_right(): %s"), mach_error_string(status));
286 (void) mach_port_destroy(mach_task_self(), port);
287 return;
288 }
289
290 /* Request a notification when/if the server dies */
291 status = mach_port_request_notification(mach_task_self(),
292 port,
293 MACH_NOTIFY_NO_SENDERS,
294 1,
295 port,
296 MACH_MSG_TYPE_MAKE_SEND_ONCE,
297 &oldNotify);
298 if (status != KERN_SUCCESS) {
299 SCLog(_sc_verbose, LOG_DEBUG, CFSTR("mach_port_request_notification(): %s"), mach_error_string(status));
300 (void) mach_port_destroy(mach_task_self(), port);
301 return;
302 }
303
304 if (oldNotify != MACH_PORT_NULL) {
305 SCLog(_sc_verbose, LOG_ERR, CFSTR("rlsSchedule(): why is oldNotify != MACH_PORT_NULL?"));
306 }
307
308 status = notifyviaport(storePrivate->server, port, 0, (int *)&sc_status);
309 if (status != KERN_SUCCESS) {
310 if (status != MACH_SEND_INVALID_DEST)
311 SCLog(_sc_verbose, LOG_DEBUG, CFSTR("notifyviaport(): %s"), mach_error_string(status));
312 (void) mach_port_destroy(mach_task_self(), port);
313 port = MACH_PORT_NULL;
314 (void) mach_port_destroy(mach_task_self(), storePrivate->server);
315 storePrivate->server = MACH_PORT_NULL;
316 return;
317 }
318
319 storePrivate->callbackPort = CFMachPortCreateWithPort(NULL, port, rlsCallback, &context, NULL);
320 CFMachPortSetInvalidationCallBack(storePrivate->callbackPort, rlsPortInvalidate);
321 storePrivate->callbackRunLoopSource = CFMachPortCreateRunLoopSource(NULL, storePrivate->callbackPort, 0);
322 }
323
324 if (storePrivate->callbackRunLoopSource) {
325 CFRunLoopAddSource(rl, storePrivate->callbackRunLoopSource, mode);
326 }
327
328 return;
329 }
330
331
332 static void
333 rlsCancel(void *info, CFRunLoopRef rl, CFStringRef mode)
334 {
335 SCDynamicStoreRef store = (SCDynamicStoreRef)info;
336 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
337
338 SCLog(_sc_verbose, LOG_DEBUG, CFSTR("cancel notifications for mode %@"), mode);
339
340 if (storePrivate->callbackRunLoopSource) {
341 CFRunLoopRemoveSource(rl, storePrivate->callbackRunLoopSource, mode);
342 }
343
344 if (--storePrivate->rlsRefs == 0) {
345 int sc_status;
346 kern_return_t status;
347
348 SCLog(_sc_verbose, LOG_DEBUG, CFSTR(" cancel callback runloop source"));
349
350 if (storePrivate->callbackRunLoopSource) {
351 /* remove the run loop source */
352 CFRelease(storePrivate->callbackRunLoopSource);
353 storePrivate->callbackRunLoopSource = NULL;
354 }
355
356 if (storePrivate->callbackPort) {
357 /* invalidate port */
358 CFMachPortInvalidate(storePrivate->callbackPort);
359 CFRelease(storePrivate->callbackPort);
360 storePrivate->callbackPort = NULL;
361 }
362
363 if (storePrivate->server) {
364 status = notifycancel(storePrivate->server, (int *)&sc_status);
365 if (status != KERN_SUCCESS) {
366 if (status != MACH_SEND_INVALID_DEST)
367 SCLog(_sc_verbose, LOG_INFO, CFSTR("notifycancel(): %s"), mach_error_string(status));
368 (void) mach_port_destroy(mach_task_self(), storePrivate->server);
369 storePrivate->server = MACH_PORT_NULL;
370 return;
371 }
372 }
373 }
374 return;
375 }
376
377
378 static void
379 rlsPerform(void *info)
380 {
381 CFArrayRef changedKeys;
382 void *context_info;
383 void (*context_release)(const void *);
384 SCDynamicStoreCallBack rlsFunction;
385 SCDynamicStoreRef store = (SCDynamicStoreRef)info;
386 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
387
388 SCLog(_sc_verbose, LOG_DEBUG, CFSTR(" executing notifiction function"));
389
390 changedKeys = SCDynamicStoreCopyNotifiedKeys(store);
391 if (!changedKeys) {
392 /* something happened to the server */
393 return;
394 }
395
396 rlsFunction = storePrivate->rlsFunction;
397
398 if (NULL != storePrivate->rlsContext.retain) {
399 context_info = (void *)storePrivate->rlsContext.retain(storePrivate->rlsContext.info);
400 context_release = storePrivate->rlsContext.release;
401 } else {
402 context_info = storePrivate->rlsContext.info;
403 context_release = NULL;
404 }
405 (*rlsFunction)(store, changedKeys, context_info);
406 if (context_release) {
407 context_release(context_info);
408 }
409
410 CFRelease(changedKeys);
411 return;
412 }
413
414
415 static CFTypeRef
416 rlsRetain(CFTypeRef cf)
417 {
418 SCDynamicStoreRef store = (SCDynamicStoreRef)cf;
419 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
420
421 if (storePrivate->notifyStatus != Using_NotifierInformViaRunLoop) {
422 /* mark RLS active */
423 storePrivate->notifyStatus = Using_NotifierInformViaRunLoop;
424 /* keep a reference to the store */
425 CFRetain(store);
426 }
427
428 return cf;
429 }
430
431
432 static void
433 rlsRelease(CFTypeRef cf)
434 {
435 SCDynamicStoreRef store = (SCDynamicStoreRef)cf;
436 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
437
438 /* mark RLS inactive */
439 storePrivate->notifyStatus = NotifierNotRegistered;
440 storePrivate->rls = NULL;
441
442 /* release our reference to the store */
443 CFRelease(store);
444
445 return;
446 }
447
448
449 CFRunLoopSourceRef
450 SCDynamicStoreCreateRunLoopSource(CFAllocatorRef allocator,
451 SCDynamicStoreRef store,
452 CFIndex order)
453 {
454 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
455
456 SCLog(_sc_verbose, LOG_DEBUG, CFSTR("SCDynamicStoreCreateRunLoopSource:"));
457
458 if (!store) {
459 /* sorry, you must provide a session */
460 _SCErrorSet(kSCStatusNoStoreSession);
461 return NULL;
462 }
463
464 if (storePrivate->server == MACH_PORT_NULL) {
465 /* sorry, you must have an open session to play */
466 _SCErrorSet(kSCStatusNoStoreServer);
467 return NULL;
468 }
469
470 switch (storePrivate->notifyStatus) {
471 case NotifierNotRegistered :
472 case Using_NotifierInformViaRunLoop :
473 /* OK to enable runloop notification */
474 break;
475 default :
476 /* sorry, you can only have one notification registered at once */
477 _SCErrorSet(kSCStatusNotifierActive);
478 return NULL;
479 }
480
481 if (storePrivate->rls) {
482 CFRetain(storePrivate->rls);
483 } else {
484 CFRunLoopSourceContext context = { 0 // version
485 , (void *)store // info
486 , rlsRetain // retain
487 , rlsRelease // release
488 , CFCopyDescription // copyDescription
489 , CFEqual // equal
490 , CFHash // hash
491 , rlsSchedule // schedule
492 , rlsCancel // cancel
493 , rlsPerform // perform
494 };
495
496 storePrivate->rls = CFRunLoopSourceCreate(allocator, order, &context);
497 }
498
499 if (!storePrivate->rls) {
500 _SCErrorSet(kSCStatusFailed);
501 return NULL;
502 }
503
504 return storePrivate->rls;
505 }