2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
25 * Modification History
27 * June 1, 2001 Allan Nathanson <ajn@apple.com>
28 * - public API conversion
30 * April 5, 2000 Allan Nathanson <ajn@apple.com>
35 #include <sys/ioctl.h>
36 #include <sys/types.h>
37 #include <sys/socket.h>
46 __SCDynamicStoreNotifyFileDescriptor(SCDynamicStoreRef store
,
50 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
52 CFStringRef sessionKey
;
55 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("__SCDynamicStoreNotifyFileDescriptor:"));
57 if (!store
|| (storePrivate
->server
== MACH_PORT_NULL
)) {
58 return kSCStatusNoStoreSession
; /* you must have an open session to play */
61 if (storePrivate
->notifyStatus
!= NotifierNotRegistered
) {
62 /* sorry, you can only have one notification registered at once */
63 return kSCStatusNotifierActive
;
66 if ((sock
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
67 SCLog(_configd_verbose
, LOG_NOTICE
, CFSTR("socket: %s"), strerror(errno
));
68 return kSCStatusFailed
;
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
;
80 if (needsNotification
== NULL
)
81 needsNotification
= CFSetCreateMutable(NULL
,
83 &kCFTypeSetCallBacks
);
85 sessionNum
= CFNumberCreate(NULL
, kCFNumberIntType
, &storePrivate
->server
);
86 CFSetAddValue(needsNotification
, sessionNum
);
87 CFRelease(sessionNum
);
96 _notifyviafd(mach_port_t server
,
98 mach_msg_type_number_t pathLen
,
103 kern_return_t status
;
104 serverSessionRef mySession
= getSession(server
);
105 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)mySession
->store
;
106 struct sockaddr_un un
;
108 int bufSiz
= sizeof(storePrivate
->notifyFileIdentifier
);
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
);
118 * if socket currently open, close it!
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 */
128 *sc_status
= kSCStatusFailed
;
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 */
143 *sc_status
= kSCStatusNoStoreSession
; /* you must have an open session to play */
147 /* do common sanity checks, get socket */
148 *sc_status
= __SCDynamicStoreNotifyFileDescriptor(mySession
->store
, identifier
, &sock
);
150 /* check status of __SCDynamicStoreNotifyFileDescriptor() */
151 if (*sc_status
!= kSCStatusOK
) {
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
));
159 storePrivate
->notifyStatus
= NotifierNotRegistered
;
160 storePrivate
->notifyFile
= -1;
161 *sc_status
= kSCStatusFailed
;
165 SCLog(_configd_verbose
, LOG_NOTICE
, CFSTR(" fd = %d"), sock
);
166 (void) unlink(un
.sun_path
);
168 if (setsockopt(sock
, SOL_SOCKET
, SO_SNDBUF
, &bufSiz
, sizeof(bufSiz
)) < 0) {
169 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("setsockopt: %s"), strerror(errno
));
171 *sc_status
= kSCStatusFailed
;
175 if (ioctl(sock
, FIONBIO
, &nbioYes
) == -1) {
176 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("ioctl(,FIONBIO,): %s"), strerror(errno
));
178 *sc_status
= kSCStatusFailed
;
182 /* set notifier active */
183 storePrivate
->notifyStatus
= Using_NotifierInformViaFD
;
184 storePrivate
->notifyFile
= sock
;
185 storePrivate
->notifyFileIdentifier
= identifier
;