2 * Copyright (c) 2000, 2001, 2003-2006, 2008, 2009 Apple 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>
37 #include <sys/types.h>
38 #include <sys/socket.h>
47 __SCDynamicStoreNotifyFileDescriptor(SCDynamicStoreRef store
,
51 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
53 CFStringRef sessionKey
;
56 if ((store
== NULL
) || (storePrivate
->server
== MACH_PORT_NULL
)) {
57 return kSCStatusNoStoreSession
; /* you must have an open session to play */
60 if (storePrivate
->notifyStatus
!= NotifierNotRegistered
) {
61 /* sorry, you can only have one notification registered at once */
62 return kSCStatusNotifierActive
;
65 if ((sock
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
66 SCLog(TRUE
, LOG_NOTICE
, CFSTR("__SCDynamicStoreNotifyFileDescriptor socket() failed: %s"), strerror(errno
));
67 return kSCStatusFailed
;
72 /* push out a notification if any changes are pending */
73 sessionKey
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("%d"), storePrivate
->server
);
74 info
= CFDictionaryGetValue(sessionData
, sessionKey
);
75 CFRelease(sessionKey
);
76 if (info
&& CFDictionaryContainsKey(info
, kSCDChangedKeys
)) {
77 CFNumberRef sessionNum
;
79 if (needsNotification
== NULL
)
80 needsNotification
= CFSetCreateMutable(NULL
,
82 &kCFTypeSetCallBacks
);
84 sessionNum
= CFNumberCreate(NULL
, kCFNumberIntType
, &storePrivate
->server
);
85 CFSetAddValue(needsNotification
, sessionNum
);
86 CFRelease(sessionNum
);
95 _notifyviafd(mach_port_t server
,
97 mach_msg_type_number_t pathLen
,
103 serverSessionRef mySession
= getSession(server
);
106 SCDynamicStorePrivateRef storePrivate
;
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 (void) vm_deallocate(mach_task_self(), (vm_address_t
)pathRef
, pathLen
);
116 *sc_status
= kSCStatusFailed
;
120 /* un-serialize the UNIX domain socket path */
121 un
.sun_family
= AF_UNIX
;
122 bcopy(pathRef
, un
.sun_path
, pathLen
);
123 un
.sun_path
[pathLen
] = '\0';
124 (void) vm_deallocate(mach_task_self(), (vm_address_t
)pathRef
, pathLen
);
126 if (mySession
== NULL
) {
127 *sc_status
= kSCStatusNoStoreSession
; /* you must have an open session to play */
130 storePrivate
= (SCDynamicStorePrivateRef
)mySession
->store
;
132 /* check permissions */
133 if (!hasRootAccess(mySession
)) {
136 bzero(&statbuf
, sizeof(statbuf
));
137 if (stat(un
.sun_path
, &statbuf
) == -1) {
139 SCLog(TRUE
, LOG_DEBUG
, CFSTR("_notifyviafd stat() failed: %s"), strerror(errno
));
142 if (mySession
->callerEUID
!= statbuf
.st_uid
) {
143 *sc_status
= kSCStatusAccessError
;
144 SCLog(TRUE
, LOG_DEBUG
, CFSTR("_notifyviafd permissions error"));
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) {
160 SCLog(TRUE
, LOG_DEBUG
, CFSTR("_notifyviafd connect() failed: %s"), strerror(errno
));
165 bufSiz
= sizeof(storePrivate
->notifyFileIdentifier
);
166 if (setsockopt(sock
, SOL_SOCKET
, SO_SNDBUF
, &bufSiz
, sizeof(bufSiz
)) == -1) {
168 SCLog(TRUE
, LOG_DEBUG
, CFSTR("_notifyviafd setsockopt() failed: %s"), strerror(errno
));
174 if (ioctl(sock
, FIONBIO
, &nbioYes
) == -1) {
176 SCLog(TRUE
, LOG_DEBUG
, CFSTR("_notifyviafd ioctl(,FIONBIO,) failed: %s"), strerror(errno
));
181 /* set notifier active */
182 storePrivate
->notifyStatus
= Using_NotifierInformViaFD
;
183 storePrivate
->notifyFile
= sock
;
184 storePrivate
->notifyFileIdentifier
= identifier
;