2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
24 * Modification History
26 * June 1, 2001 Allan Nathanson <ajn@apple.com>
27 * - public API conversion
29 * April 5, 2000 Allan Nathanson <ajn@apple.com>
34 #include <sys/ioctl.h>
35 #include <sys/types.h>
36 #include <sys/socket.h>
44 __SCDynamicStoreNotifyFileDescriptor(SCDynamicStoreRef store
,
48 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
50 CFStringRef sessionKey
;
53 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("__SCDynamicStoreNotifyFileDescriptor:"));
55 if (!store
|| (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(_configd_verbose
, LOG_NOTICE
, CFSTR("socket: %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
);
93 _notifyviafd(mach_port_t server
,
95 mach_msg_type_number_t pathLen
,
100 kern_return_t status
;
101 serverSessionRef mySession
= getSession(server
);
102 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)mySession
->store
;
103 struct sockaddr_un un
;
105 int bufSiz
= sizeof(storePrivate
->notifyFileIdentifier
);
108 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("Send message via UNIX domain socket when a notification key changes."));
109 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR(" server = %d"), server
);
110 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR(" path = %s"), pathRef
);
113 * if socket currently open, close it!
115 /* validate the UNIX domain socket path */
116 if (pathLen
> (sizeof(un
.sun_path
) - 1)) {
117 SCLog(_configd_verbose
, LOG_NOTICE
, CFSTR("domain socket path length too long!"));
118 status
= vm_deallocate(mach_task_self(), (vm_address_t
)pathRef
, pathLen
);
119 if (status
!= KERN_SUCCESS
) {
120 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("vm_deallocate(): %s"), mach_error_string(status
));
121 /* non-fatal???, proceed */
123 *sc_status
= kSCStatusFailed
;
127 /* un-serialize the UNIX domain socket path */
128 un
.sun_family
= AF_UNIX
;
129 bcopy(pathRef
, un
.sun_path
, pathLen
);
130 un
.sun_path
[pathLen
] = '\0';
131 status
= vm_deallocate(mach_task_self(), (vm_address_t
)pathRef
, pathLen
);
132 if (status
!= KERN_SUCCESS
) {
133 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("vm_deallocate(): %s"), mach_error_string(status
));
134 /* non-fatal???, proceed */
137 /* do common sanity checks, get socket */
138 *sc_status
= __SCDynamicStoreNotifyFileDescriptor(mySession
->store
, identifier
, &sock
);
140 /* check status of __SCDynamicStoreNotifyFileDescriptor() */
141 if (*sc_status
!= kSCStatusOK
) {
145 /* establish the connection, get ready for a read() */
146 if (connect(sock
, (struct sockaddr
*)&un
, sizeof(un
)) == -1) {
147 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("connect: %s"), strerror(errno
));
149 storePrivate
->notifyStatus
= NotifierNotRegistered
;
150 storePrivate
->notifyFile
= -1;
151 *sc_status
= kSCStatusFailed
;
155 SCLog(_configd_verbose
, LOG_NOTICE
, CFSTR(" fd = %d"), sock
);
156 (void) unlink(un
.sun_path
);
158 if (setsockopt(sock
, SOL_SOCKET
, SO_SNDBUF
, &bufSiz
, sizeof(bufSiz
)) < 0) {
159 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("setsockopt: %s"), strerror(errno
));
161 *sc_status
= kSCStatusFailed
;
165 if (ioctl(sock
, FIONBIO
, &nbioYes
) == -1) {
166 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("ioctl(,FIONBIO,): %s"), strerror(errno
));
168 *sc_status
= kSCStatusFailed
;
172 /* set notifier active */
173 storePrivate
->notifyStatus
= Using_NotifierInformViaFD
;
174 storePrivate
->notifyFile
= sock
;
175 storePrivate
->notifyFileIdentifier
= identifier
;