]> git.saurik.com Git - apple/configd.git/blob - configd.tproj/_notifyviafd.c
configd-84.6.tar.gz
[apple/configd.git] / configd.tproj / _notifyviafd.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 * April 5, 2000 Allan Nathanson <ajn@apple.com>
31 * - initial revision
32 */
33
34 #include <unistd.h>
35 #include <sys/ioctl.h>
36 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <sys/un.h>
39
40 #include "configd.h"
41 #include "session.h"
42
43
44 __private_extern__
45 int
46 __SCDynamicStoreNotifyFileDescriptor(SCDynamicStoreRef store,
47 int32_t identifier,
48 int *fd)
49 {
50 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
51 int sock;
52 CFStringRef sessionKey;
53 CFDictionaryRef info;
54
55 SCLog(_configd_verbose, LOG_DEBUG, CFSTR("__SCDynamicStoreNotifyFileDescriptor:"));
56
57 if (!store || (storePrivate->server == MACH_PORT_NULL)) {
58 return kSCStatusNoStoreSession; /* you must have an open session to play */
59 }
60
61 if (storePrivate->notifyStatus != NotifierNotRegistered) {
62 /* sorry, you can only have one notification registered at once */
63 return kSCStatusNotifierActive;
64 }
65
66 if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
67 SCLog(_configd_verbose, LOG_NOTICE, CFSTR("socket: %s"), strerror(errno));
68 return kSCStatusFailed;
69 }
70
71 *fd = sock;
72
73 /* push out a notification if any changes are pending */
74 sessionKey = CFStringCreateWithFormat(NULL, NULL, CFSTR("%d"), storePrivate->server);
75 info = CFDictionaryGetValue(sessionData, sessionKey);
76 CFRelease(sessionKey);
77 if (info && CFDictionaryContainsKey(info, kSCDChangedKeys)) {
78 CFNumberRef sessionNum;
79
80 if (needsNotification == NULL)
81 needsNotification = CFSetCreateMutable(NULL,
82 0,
83 &kCFTypeSetCallBacks);
84
85 sessionNum = CFNumberCreate(NULL, kCFNumberIntType, &storePrivate->server);
86 CFSetAddValue(needsNotification, sessionNum);
87 CFRelease(sessionNum);
88 }
89
90 return kSCStatusOK;
91 }
92
93
94 __private_extern__
95 kern_return_t
96 _notifyviafd(mach_port_t server,
97 xmlData_t pathRef,
98 mach_msg_type_number_t pathLen,
99 int identifier,
100 int *sc_status
101 )
102 {
103 kern_return_t status;
104 serverSessionRef mySession = getSession(server);
105 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)mySession->store;
106 struct sockaddr_un un;
107 int sock;
108 int bufSiz = sizeof(storePrivate->notifyFileIdentifier);
109 int nbioYes = 1;
110
111 if (_configd_verbose) {
112 SCLog(TRUE, LOG_DEBUG, CFSTR("Send message via UNIX domain socket when a notification key changes."));
113 SCLog(TRUE, LOG_DEBUG, CFSTR(" server = %d"), server);
114 SCLog(TRUE, LOG_DEBUG, CFSTR(" path = %s"), pathRef);
115 }
116
117 /*
118 * if socket currently open, close it!
119 */
120 /* validate the UNIX domain socket path */
121 if (pathLen > (sizeof(un.sun_path) - 1)) {
122 SCLog(_configd_verbose, LOG_NOTICE, CFSTR("domain socket path length too long!"));
123 status = vm_deallocate(mach_task_self(), (vm_address_t)pathRef, pathLen);
124 if (status != KERN_SUCCESS) {
125 SCLog(_configd_verbose, LOG_DEBUG, CFSTR("vm_deallocate(): %s"), mach_error_string(status));
126 /* non-fatal???, proceed */
127 }
128 *sc_status = kSCStatusFailed;
129 return KERN_SUCCESS;
130 }
131
132 /* un-serialize the UNIX domain socket path */
133 un.sun_family = AF_UNIX;
134 bcopy(pathRef, un.sun_path, pathLen);
135 un.sun_path[pathLen] = '\0';
136 status = vm_deallocate(mach_task_self(), (vm_address_t)pathRef, pathLen);
137 if (status != KERN_SUCCESS) {
138 SCLog(_configd_verbose, LOG_DEBUG, CFSTR("vm_deallocate(): %s"), mach_error_string(status));
139 /* non-fatal???, proceed */
140 }
141
142 if (!mySession) {
143 *sc_status = kSCStatusNoStoreSession; /* you must have an open session to play */
144 return KERN_SUCCESS;
145 }
146
147 /* do common sanity checks, get socket */
148 *sc_status = __SCDynamicStoreNotifyFileDescriptor(mySession->store, identifier, &sock);
149
150 /* check status of __SCDynamicStoreNotifyFileDescriptor() */
151 if (*sc_status != kSCStatusOK) {
152 return KERN_SUCCESS;
153 }
154
155 /* establish the connection, get ready for a read() */
156 if (connect(sock, (struct sockaddr *)&un, sizeof(un)) == -1) {
157 SCLog(_configd_verbose, LOG_DEBUG, CFSTR("connect: %s"), strerror(errno));
158 (void) close(sock);
159 storePrivate->notifyStatus = NotifierNotRegistered;
160 storePrivate->notifyFile = -1;
161 *sc_status = kSCStatusFailed;
162 return KERN_SUCCESS;
163 }
164
165 SCLog(_configd_verbose, LOG_NOTICE, CFSTR(" fd = %d"), sock);
166 (void) unlink(un.sun_path);
167
168 if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &bufSiz, sizeof(bufSiz)) < 0) {
169 SCLog(_configd_verbose, LOG_DEBUG, CFSTR("setsockopt: %s"), strerror(errno));
170 (void) close(sock);
171 *sc_status = kSCStatusFailed;
172 return KERN_SUCCESS;
173 }
174
175 if (ioctl(sock, FIONBIO, &nbioYes) == -1) {
176 SCLog(_configd_verbose, LOG_DEBUG, CFSTR("ioctl(,FIONBIO,): %s"), strerror(errno));
177 (void) close(sock);
178 *sc_status = kSCStatusFailed;
179 return KERN_SUCCESS;
180 }
181
182 /* set notifier active */
183 storePrivate->notifyStatus = Using_NotifierInformViaFD;
184 storePrivate->notifyFile = sock;
185 storePrivate->notifyFileIdentifier = identifier;
186
187 return KERN_SUCCESS;
188 }