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