2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
27 * Modification History
29 * June 1, 2001 Allan Nathanson <ajn@apple.com>
30 * - public API conversion
32 * April 5, 2000 Allan Nathanson <ajn@apple.com>
37 #include <sys/ioctl.h>
38 #include <sys/types.h>
39 #include <sys/socket.h>
48 __SCDynamicStoreNotifyFileDescriptor(SCDynamicStoreRef store
,
52 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
54 CFStringRef sessionKey
;
57 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("__SCDynamicStoreNotifyFileDescriptor:"));
59 if (!store
|| (storePrivate
->server
== MACH_PORT_NULL
)) {
60 return kSCStatusNoStoreSession
; /* you must have an open session to play */
63 if (storePrivate
->notifyStatus
!= NotifierNotRegistered
) {
64 /* sorry, you can only have one notification registered at once */
65 return kSCStatusNotifierActive
;
68 if ((sock
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
69 SCLog(_configd_verbose
, LOG_NOTICE
, CFSTR("socket: %s"), strerror(errno
));
70 return kSCStatusFailed
;
75 /* push out a notification if any changes are pending */
76 sessionKey
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("%d"), storePrivate
->server
);
77 info
= CFDictionaryGetValue(sessionData
, sessionKey
);
78 CFRelease(sessionKey
);
79 if (info
&& CFDictionaryContainsKey(info
, kSCDChangedKeys
)) {
80 CFNumberRef sessionNum
;
82 if (needsNotification
== NULL
)
83 needsNotification
= CFSetCreateMutable(NULL
,
85 &kCFTypeSetCallBacks
);
87 sessionNum
= CFNumberCreate(NULL
, kCFNumberIntType
, &storePrivate
->server
);
88 CFSetAddValue(needsNotification
, sessionNum
);
89 CFRelease(sessionNum
);
98 _notifyviafd(mach_port_t server
,
100 mach_msg_type_number_t pathLen
,
105 kern_return_t status
;
106 serverSessionRef mySession
= getSession(server
);
107 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)mySession
->store
;
108 struct sockaddr_un un
;
110 int bufSiz
= sizeof(storePrivate
->notifyFileIdentifier
);
113 if (_configd_verbose
) {
114 SCLog(TRUE
, LOG_DEBUG
, CFSTR("Send message via UNIX domain socket when a notification key changes."));
115 SCLog(TRUE
, LOG_DEBUG
, CFSTR(" server = %d"), server
);
116 SCLog(TRUE
, LOG_DEBUG
, CFSTR(" path = %s"), pathRef
);
120 * if socket currently open, close it!
122 /* validate the UNIX domain socket path */
123 if (pathLen
> (sizeof(un
.sun_path
) - 1)) {
124 SCLog(_configd_verbose
, LOG_NOTICE
, CFSTR("domain socket path length too long!"));
125 status
= vm_deallocate(mach_task_self(), (vm_address_t
)pathRef
, pathLen
);
126 if (status
!= KERN_SUCCESS
) {
127 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("vm_deallocate(): %s"), mach_error_string(status
));
128 /* non-fatal???, proceed */
130 *sc_status
= kSCStatusFailed
;
134 /* un-serialize the UNIX domain socket path */
135 un
.sun_family
= AF_UNIX
;
136 bcopy(pathRef
, un
.sun_path
, pathLen
);
137 un
.sun_path
[pathLen
] = '\0';
138 status
= vm_deallocate(mach_task_self(), (vm_address_t
)pathRef
, pathLen
);
139 if (status
!= KERN_SUCCESS
) {
140 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("vm_deallocate(): %s"), mach_error_string(status
));
141 /* non-fatal???, proceed */
145 *sc_status
= kSCStatusNoStoreSession
; /* you must have an open session to play */
149 /* do common sanity checks, get socket */
150 *sc_status
= __SCDynamicStoreNotifyFileDescriptor(mySession
->store
, identifier
, &sock
);
152 /* check status of __SCDynamicStoreNotifyFileDescriptor() */
153 if (*sc_status
!= kSCStatusOK
) {
157 /* establish the connection, get ready for a read() */
158 if (connect(sock
, (struct sockaddr
*)&un
, sizeof(un
)) == -1) {
159 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("connect: %s"), strerror(errno
));
161 storePrivate
->notifyStatus
= NotifierNotRegistered
;
162 storePrivate
->notifyFile
= -1;
163 *sc_status
= kSCStatusFailed
;
167 SCLog(_configd_verbose
, LOG_NOTICE
, CFSTR(" fd = %d"), sock
);
168 (void) unlink(un
.sun_path
);
170 if (setsockopt(sock
, SOL_SOCKET
, SO_SNDBUF
, &bufSiz
, sizeof(bufSiz
)) < 0) {
171 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("setsockopt: %s"), strerror(errno
));
173 *sc_status
= kSCStatusFailed
;
177 if (ioctl(sock
, FIONBIO
, &nbioYes
) == -1) {
178 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("ioctl(,FIONBIO,): %s"), strerror(errno
));
180 *sc_status
= kSCStatusFailed
;
184 /* set notifier active */
185 storePrivate
->notifyStatus
= Using_NotifierInformViaFD
;
186 storePrivate
->notifyFile
= sock
;
187 storePrivate
->notifyFileIdentifier
= identifier
;