2 * Copyright (c) 2000, 2001, 2003-2006 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 if ((store
== NULL
) || (storePrivate
->server
== MACH_PORT_NULL
)) {
56 return kSCStatusNoStoreSession
; /* you must have an open session to play */
59 if (storePrivate
->notifyStatus
!= NotifierNotRegistered
) {
60 /* sorry, you can only have one notification registered at once */
61 return kSCStatusNotifierActive
;
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
;
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
;
78 if (needsNotification
== NULL
)
79 needsNotification
= CFSetCreateMutable(NULL
,
81 &kCFTypeSetCallBacks
);
83 sessionNum
= CFNumberCreate(NULL
, kCFNumberIntType
, &storePrivate
->server
);
84 CFSetAddValue(needsNotification
, sessionNum
);
85 CFRelease(sessionNum
);
94 _notifyviafd(mach_port_t server
,
96 mach_msg_type_number_t pathLen
,
102 serverSessionRef mySession
= getSession(server
);
105 kern_return_t status
;
106 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)mySession
->store
;
107 struct sockaddr_un un
;
110 * if socket currently open, close it!
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
);
117 if (status
!= KERN_SUCCESS
) {
118 SCLog(TRUE
, LOG_DEBUG
, CFSTR("_notifyviafd vm_deallocate() failed: %s"), mach_error_string(status
));
119 /* non-fatal???, proceed */
122 *sc_status
= kSCStatusFailed
;
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
);
132 if (status
!= KERN_SUCCESS
) {
133 SCLog(TRUE
, LOG_DEBUG
, CFSTR("_notifyviafd vm_deallocate() failed: %s"), mach_error_string(status
));
134 /* non-fatal???, proceed */
138 if (mySession
== NULL
) {
139 *sc_status
= kSCStatusNoStoreSession
; /* you must have an open session to play */
143 /* do common sanity checks, get socket */
144 *sc_status
= __SCDynamicStoreNotifyFileDescriptor(mySession
->store
, identifier
, &sock
);
146 /* check status of __SCDynamicStoreNotifyFileDescriptor() */
147 if (*sc_status
!= kSCStatusOK
) {
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
));
155 storePrivate
->notifyStatus
= NotifierNotRegistered
;
156 storePrivate
->notifyFile
= -1;
157 *sc_status
= kSCStatusFailed
;
161 (void) unlink(un
.sun_path
);
163 bufSiz
= sizeof(storePrivate
->notifyFileIdentifier
);
164 if (setsockopt(sock
, SOL_SOCKET
, SO_SNDBUF
, &bufSiz
, sizeof(bufSiz
)) == -1) {
165 SCLog(TRUE
, LOG_DEBUG
, CFSTR("_notifyviafd setsockopt() failed: %s"), strerror(errno
));
167 *sc_status
= kSCStatusFailed
;
172 if (ioctl(sock
, FIONBIO
, &nbioYes
) == -1) {
173 SCLog(TRUE
, LOG_DEBUG
, CFSTR("_notifyviafd ioctl(,FIONBIO,) failed: %s"), strerror(errno
));
175 *sc_status
= kSCStatusFailed
;
179 /* set notifier active */
180 storePrivate
->notifyStatus
= Using_NotifierInformViaFD
;
181 storePrivate
->notifyFile
= sock
;
182 storePrivate
->notifyFileIdentifier
= identifier
;