]> git.saurik.com Git - apple/configd.git/blob - configd.tproj/_notifyviaport.c
configd-84.tar.gz
[apple/configd.git] / configd.tproj / _notifyviaport.c
1 /*
2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25
26 /*
27 * Modification History
28 *
29 * June 1, 2001 Allan Nathanson <ajn@apple.com>
30 * - public API conversion
31 *
32 * March 24, 2000 Allan Nathanson <ajn@apple.com>
33 * - initial revision
34 */
35
36 #include "configd.h"
37 #include "session.h"
38
39 __private_extern__
40 int
41 __SCDynamicStoreNotifyMachPort(SCDynamicStoreRef store,
42 mach_msg_id_t identifier,
43 mach_port_t *port)
44 {
45 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
46 CFStringRef sessionKey;
47 CFDictionaryRef info;
48
49 SCLog(_configd_verbose, LOG_DEBUG, CFSTR("__SCDynamicStoreNotifyMachPort:"));
50
51 if (!store || (storePrivate->server == MACH_PORT_NULL)) {
52 return kSCStatusNoStoreSession; /* you must have an open session to play */
53 }
54
55 if (storePrivate->notifyStatus != NotifierNotRegistered) {
56 /* sorry, you can only have one notification registered at once */
57 return kSCStatusNotifierActive;
58 }
59
60 if (*port == MACH_PORT_NULL) {
61 /* sorry, you must specify a valid mach port */
62 return kSCStatusInvalidArgument;
63 }
64
65 /* push out a notification if any changes are pending */
66 sessionKey = CFStringCreateWithFormat(NULL, NULL, CFSTR("%d"), storePrivate->server);
67 info = CFDictionaryGetValue(sessionData, sessionKey);
68 CFRelease(sessionKey);
69 if (info && CFDictionaryContainsKey(info, kSCDChangedKeys)) {
70 CFNumberRef sessionNum;
71
72 if (needsNotification == NULL)
73 needsNotification = CFSetCreateMutable(NULL,
74 0,
75 &kCFTypeSetCallBacks);
76
77 sessionNum = CFNumberCreate(NULL, kCFNumberIntType, &storePrivate->server);
78 CFSetAddValue(needsNotification, sessionNum);
79 CFRelease(sessionNum);
80 }
81
82 return kSCStatusOK;
83 }
84
85
86 __private_extern__
87 kern_return_t
88 _notifyviaport(mach_port_t server,
89 mach_port_t port,
90 mach_msg_id_t identifier,
91 int *sc_status
92 )
93 {
94 serverSessionRef mySession = getSession(server);
95 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)mySession->store;
96
97 if (_configd_verbose) {
98 SCLog(TRUE, LOG_DEBUG, CFSTR("Send mach message when a notification key changes."));
99 SCLog(TRUE, LOG_DEBUG, CFSTR(" server = %d"), server);
100 SCLog(TRUE, LOG_DEBUG, CFSTR(" port = %d"), port);
101 SCLog(TRUE, LOG_DEBUG, CFSTR(" message id = %d"), identifier);
102 }
103
104 if (!mySession) {
105 *sc_status = kSCStatusNoStoreSession; /* you must have an open session to play */
106 return KERN_SUCCESS;
107 }
108
109 if (storePrivate->notifyPort != MACH_PORT_NULL) {
110 SCLog(_configd_verbose, LOG_DEBUG, CFSTR(" destroying old callback mach port %d"), storePrivate->notifyPort);
111 (void) mach_port_destroy(mach_task_self(), storePrivate->notifyPort);
112 }
113
114 *sc_status = __SCDynamicStoreNotifyMachPort(mySession->store, identifier, &port);
115
116 if (*sc_status == kSCStatusOK) {
117 /* save notification port, requested identifier, and set notifier active */
118 storePrivate->notifyStatus = Using_NotifierInformViaMachPort;
119 storePrivate->notifyPort = port;
120 storePrivate->notifyPortIdentifier = identifier;
121 }
122
123 return KERN_SUCCESS;
124 }