2 * Copyright (c) 2000, 2001, 2003-2006, 2008-2011 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 (storePrivate
->notifyStatus
!= NotifierNotRegistered
) {
57 /* sorry, you can only have one notification registered at once */
58 return kSCStatusNotifierActive
;
61 if ((sock
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
62 SCLog(TRUE
, LOG_NOTICE
, CFSTR("__SCDynamicStoreNotifyFileDescriptor socket() failed: %s"), strerror(errno
));
63 return kSCStatusFailed
;
68 /* push out a notification if any changes are pending */
69 sessionKey
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("%d"), storePrivate
->server
);
70 info
= CFDictionaryGetValue(sessionData
, sessionKey
);
71 CFRelease(sessionKey
);
72 if (info
&& CFDictionaryContainsKey(info
, kSCDChangedKeys
)) {
73 CFNumberRef sessionNum
;
75 if (needsNotification
== NULL
)
76 needsNotification
= CFSetCreateMutable(NULL
,
78 &kCFTypeSetCallBacks
);
80 sessionNum
= CFNumberCreate(NULL
, kCFNumberIntType
, &storePrivate
->server
);
81 CFSetAddValue(needsNotification
, sessionNum
);
82 CFRelease(sessionNum
);
91 _notifyviafd(mach_port_t server
,
93 mach_msg_type_number_t pathLen
,
99 serverSessionRef mySession
= getSession(server
);
102 SCDynamicStorePrivateRef storePrivate
;
103 struct sockaddr_un un
;
106 * if socket currently open, close it!
108 /* validate the UNIX domain socket path */
109 if (pathLen
> (sizeof(un
.sun_path
) - 1)) {
110 SCLog(TRUE
, LOG_NOTICE
, CFSTR("_notifyviafd(): domain socket path length too long!"));
111 (void) vm_deallocate(mach_task_self(), (vm_address_t
)pathRef
, pathLen
);
112 *sc_status
= kSCStatusFailed
;
116 /* un-serialize the UNIX domain socket path */
117 un
.sun_family
= AF_UNIX
;
118 bcopy(pathRef
, un
.sun_path
, pathLen
);
119 un
.sun_path
[pathLen
] = '\0';
120 (void) vm_deallocate(mach_task_self(), (vm_address_t
)pathRef
, pathLen
);
122 if (mySession
== NULL
) {
123 *sc_status
= kSCStatusNoStoreSession
; /* you must have an open session to play */
126 storePrivate
= (SCDynamicStorePrivateRef
)mySession
->store
;
128 /* check permissions */
129 if (!hasRootAccess(mySession
)) {
132 bzero(&statbuf
, sizeof(statbuf
));
133 if (stat(un
.sun_path
, &statbuf
) == -1) {
135 SCLog(TRUE
, LOG_DEBUG
, CFSTR("_notifyviafd stat() failed: %s"), strerror(errno
));
138 if (mySession
->callerEUID
!= statbuf
.st_uid
) {
139 *sc_status
= kSCStatusAccessError
;
140 SCLog(TRUE
, LOG_DEBUG
, CFSTR("_notifyviafd permissions error"));
145 if (!hasPathAccess(mySession
, un
.sun_path
)) {
146 *sc_status
= kSCStatusAccessError
;
147 SCLog(TRUE
, LOG_DEBUG
, CFSTR("_notifyviafd permissions error"));
151 /* do common sanity checks, get socket */
152 *sc_status
= __SCDynamicStoreNotifyFileDescriptor(mySession
->store
, identifier
, &sock
);
154 /* check status of __SCDynamicStoreNotifyFileDescriptor() */
155 if (*sc_status
!= kSCStatusOK
) {
159 /* establish the connection, get ready for a read() */
160 if (connect(sock
, (struct sockaddr
*)&un
, sizeof(un
)) == -1) {
162 SCLog(TRUE
, LOG_DEBUG
, CFSTR("_notifyviafd connect() failed: %s"), strerror(errno
));
167 bufSiz
= sizeof(storePrivate
->notifyFileIdentifier
);
168 if (setsockopt(sock
, SOL_SOCKET
, SO_SNDBUF
, &bufSiz
, sizeof(bufSiz
)) == -1) {
170 SCLog(TRUE
, LOG_DEBUG
, CFSTR("_notifyviafd setsockopt() failed: %s"), strerror(errno
));
176 if (ioctl(sock
, FIONBIO
, &nbioYes
) == -1) {
178 SCLog(TRUE
, LOG_DEBUG
, CFSTR("_notifyviafd ioctl(,FIONBIO,) failed: %s"), strerror(errno
));
183 /* set notifier active */
184 storePrivate
->notifyStatus
= Using_NotifierInformViaFD
;
185 storePrivate
->notifyFile
= sock
;
186 storePrivate
->notifyFileIdentifier
= identifier
;