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