2 * Copyright(c) 2000-2013 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 * February 16, 2004 Allan Nathanson <ajn@apple.com>
28 * - add preference notification APIs
30 * June 1, 2001 Allan Nathanson <ajn@apple.com>
31 * - public API conversion
33 * November 9, 2000 Allan Nathanson <ajn@apple.com>
37 #include <Availability.h>
38 #include <TargetConditionals.h>
39 #include <sys/cdefs.h>
40 #include <dispatch/dispatch.h>
41 #include <SystemConfiguration/SystemConfiguration.h>
42 #include <SystemConfiguration/SCValidation.h>
43 #include <SystemConfiguration/SCPrivate.h>
44 #include "SCPreferencesInternal.h"
45 #include "SCHelper_client.h"
47 #include "dy_framework.h"
53 #include <sys/errno.h>
56 const AuthorizationRef kSCPreferencesUseEntitlementAuthorization
= (AuthorizationRef
)CFSTR("UseEntitlement");
59 static __inline__ CFTypeRef
60 isA_SCPreferences(CFTypeRef obj
)
62 return (isA_CFType(obj
, SCPreferencesGetTypeID()));
67 __SCPreferencesCopyDescription(CFTypeRef cf
) {
68 CFAllocatorRef allocator
= CFGetAllocator(cf
);
69 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)cf
;
70 CFMutableStringRef result
;
72 result
= CFStringCreateMutable(allocator
, 0);
73 CFStringAppendFormat(result
, NULL
, CFSTR("<SCPreferences %p [%p]> {"), cf
, allocator
);
74 CFStringAppendFormat(result
, NULL
, CFSTR("name = %@"), prefsPrivate
->name
);
75 CFStringAppendFormat(result
, NULL
, CFSTR(", id = %@"), prefsPrivate
->prefsID
);
76 CFStringAppendFormat(result
, NULL
, CFSTR(", path = %s"),
77 prefsPrivate
->newPath
? prefsPrivate
->newPath
: prefsPrivate
->path
);
78 if (prefsPrivate
->accessed
) {
79 CFStringAppendFormat(result
, NULL
, CFSTR(", accessed"));
81 if (prefsPrivate
->changed
) {
82 CFStringAppendFormat(result
, NULL
, CFSTR(", changed"));
84 if (prefsPrivate
->locked
) {
85 CFStringAppendFormat(result
, NULL
, CFSTR(", locked"));
87 if (prefsPrivate
->helper_port
!= MACH_PORT_NULL
) {
88 CFStringAppendFormat(result
, NULL
, CFSTR(", helper port = 0x%x"), prefsPrivate
->helper_port
);
90 CFStringAppendFormat(result
, NULL
, CFSTR("}"));
97 __SCPreferencesDeallocate(CFTypeRef cf
)
99 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)cf
;
101 /* release resources */
103 pthread_mutex_destroy(&prefsPrivate
->lock
);
105 if (prefsPrivate
->name
) CFRelease(prefsPrivate
->name
);
106 if (prefsPrivate
->prefsID
) CFRelease(prefsPrivate
->prefsID
);
107 if (prefsPrivate
->options
) CFRelease(prefsPrivate
->options
);
108 if (prefsPrivate
->path
) CFAllocatorDeallocate(NULL
, prefsPrivate
->path
);
109 if (prefsPrivate
->newPath
) CFAllocatorDeallocate(NULL
, prefsPrivate
->newPath
);
110 if (prefsPrivate
->lockFD
!= -1) {
111 if (prefsPrivate
->lockPath
!= NULL
) {
112 unlink(prefsPrivate
->lockPath
);
114 close(prefsPrivate
->lockFD
);
116 if (prefsPrivate
->lockPath
) CFAllocatorDeallocate(NULL
, prefsPrivate
->lockPath
);
117 if (prefsPrivate
->signature
) CFRelease(prefsPrivate
->signature
);
118 if (prefsPrivate
->session
) CFRelease(prefsPrivate
->session
);
119 if (prefsPrivate
->sessionKeyLock
) CFRelease(prefsPrivate
->sessionKeyLock
);
120 if (prefsPrivate
->sessionKeyCommit
) CFRelease(prefsPrivate
->sessionKeyCommit
);
121 if (prefsPrivate
->sessionKeyApply
) CFRelease(prefsPrivate
->sessionKeyApply
);
122 if (prefsPrivate
->rlsContext
.release
!= NULL
) {
123 (*prefsPrivate
->rlsContext
.release
)(prefsPrivate
->rlsContext
.info
);
125 if (prefsPrivate
->prefs
) CFRelease(prefsPrivate
->prefs
);
126 if (prefsPrivate
->authorizationData
!= NULL
) CFRelease(prefsPrivate
->authorizationData
);
127 if (prefsPrivate
->helper_port
!= MACH_PORT_NULL
) {
128 (void) _SCHelperExec(prefsPrivate
->helper_port
,
129 SCHELPER_MSG_PREFS_CLOSE
,
133 _SCHelperClose(&prefsPrivate
->helper_port
);
140 static CFTypeID __kSCPreferencesTypeID
= _kCFRuntimeNotATypeID
;
143 static const CFRuntimeClass __SCPreferencesClass
= {
145 "SCPreferences", // className
148 __SCPreferencesDeallocate
, // dealloc
151 NULL
, // copyFormattingDesc
152 __SCPreferencesCopyDescription
// copyDebugDesc
156 static pthread_once_t initialized
= PTHREAD_ONCE_INIT
;
159 __SCPreferencesInitialize(void) {
160 __kSCPreferencesTypeID
= _CFRuntimeRegisterClass(&__SCPreferencesClass
);
165 static SCPreferencesPrivateRef
166 __SCPreferencesCreatePrivate(CFAllocatorRef allocator
)
168 SCPreferencesPrivateRef prefsPrivate
;
171 /* initialize runtime */
172 pthread_once(&initialized
, __SCPreferencesInitialize
);
174 /* allocate prefs session */
175 size
= sizeof(SCPreferencesPrivate
) - sizeof(CFRuntimeBase
);
176 prefsPrivate
= (SCPreferencesPrivateRef
)_CFRuntimeCreateInstance(allocator
,
177 __kSCPreferencesTypeID
,
180 if (prefsPrivate
== NULL
) {
184 pthread_mutex_init(&prefsPrivate
->lock
, NULL
);
186 prefsPrivate
->name
= NULL
;
187 prefsPrivate
->prefsID
= NULL
;
188 prefsPrivate
->options
= NULL
;
189 prefsPrivate
->path
= NULL
;
190 prefsPrivate
->newPath
= NULL
; // new prefs path
191 prefsPrivate
->locked
= FALSE
;
192 prefsPrivate
->lockFD
= -1;
193 prefsPrivate
->lockPath
= NULL
;
194 prefsPrivate
->signature
= NULL
;
195 prefsPrivate
->session
= NULL
;
196 prefsPrivate
->sessionKeyLock
= NULL
;
197 prefsPrivate
->sessionKeyCommit
= NULL
;
198 prefsPrivate
->sessionKeyApply
= NULL
;
199 prefsPrivate
->scheduled
= FALSE
;
200 prefsPrivate
->rls
= NULL
;
201 prefsPrivate
->rlsFunction
= NULL
;
202 prefsPrivate
->rlsContext
.info
= NULL
;
203 prefsPrivate
->rlsContext
.retain
= NULL
;
204 prefsPrivate
->rlsContext
.release
= NULL
;
205 prefsPrivate
->rlsContext
.copyDescription
= NULL
;
206 prefsPrivate
->rlList
= NULL
;
207 prefsPrivate
->dispatchQueue
= NULL
;
208 prefsPrivate
->prefs
= NULL
;
209 prefsPrivate
->accessed
= FALSE
;
210 prefsPrivate
->changed
= FALSE
;
211 prefsPrivate
->isRoot
= (geteuid() == 0);
212 prefsPrivate
->authorizationData
= NULL
;
213 prefsPrivate
->authorizationRequired
= FALSE
;
214 prefsPrivate
->helper_port
= MACH_PORT_NULL
;
220 __private_extern__ Boolean
221 __SCPreferencesCreate_helper(SCPreferencesRef prefs
)
223 CFDataRef data
= NULL
;
224 CFMutableDictionaryRef info
;
227 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
228 uint32_t status
= kSCStatusOK
;
230 uint32_t pid
= getpid();
233 ok
= _SCHelperOpen(prefsPrivate
->authorizationData
,
234 &prefsPrivate
->helper_port
);
239 // create a dictionary of information to pass to the helper
240 info
= CFDictionaryCreateMutable(NULL
,
242 &kCFTypeDictionaryKeyCallBacks
,
243 &kCFTypeDictionaryValueCallBacks
);
246 if (prefsPrivate
->prefsID
!= NULL
) {
247 CFDictionarySetValue(info
, CFSTR("prefsID"), prefsPrivate
->prefsID
);
251 if (prefsPrivate
->options
!= NULL
) {
252 CFDictionarySetValue(info
, CFSTR("options"), prefsPrivate
->options
);
255 // save preferences session "name"
256 CFDictionarySetValue(info
, CFSTR("name"), prefsPrivate
->name
);
259 num
= CFNumberCreate(NULL
, kCFNumberSInt32Type
, &pid
);
260 CFDictionarySetValue(info
, CFSTR("PID"), num
);
264 str
= CFStringCreateWithCString(NULL
, getprogname(), kCFStringEncodingUTF8
);
265 CFDictionarySetValue(info
, CFSTR("PROC_NAME"), str
);
268 // serialize the info
269 ok
= _SCSerialize(info
, &data
, NULL
, NULL
);
271 if (data
== NULL
|| !ok
) {
275 // have the helper "open" the prefs
276 ok
= _SCHelperExec(prefsPrivate
->helper_port
,
277 SCHELPER_MSG_PREFS_OPEN
,
281 if (data
!= NULL
) CFRelease(data
);
286 if (status
!= kSCStatusOK
) {
295 if (prefsPrivate
->helper_port
!= MACH_PORT_NULL
) {
296 _SCHelperClose(&prefsPrivate
->helper_port
);
299 status
= kSCStatusAccessError
;
310 __SCPreferencesAccess_helper(SCPreferencesRef prefs
)
313 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
314 CFDictionaryRef serverDict
= NULL
;
315 CFDictionaryRef serverPrefs
= NULL
;
316 CFDictionaryRef serverSignature
= NULL
;
317 uint32_t status
= kSCStatusOK
;
318 CFDataRef reply
= NULL
;
320 if (prefsPrivate
->helper_port
== MACH_PORT_NULL
) {
321 ok
= __SCPreferencesCreate_helper(prefs
);
327 // have the helper "access" the prefs
328 ok
= _SCHelperExec(prefsPrivate
->helper_port
,
329 SCHELPER_MSG_PREFS_ACCESS
,
337 if (status
!= kSCStatusOK
) {
345 ok
= _SCUnserialize((CFPropertyListRef
*)&serverDict
, reply
, NULL
, 0);
351 if (isA_CFDictionary(serverDict
)) {
352 serverPrefs
= CFDictionaryGetValue(serverDict
, CFSTR("preferences"));
353 serverPrefs
= isA_CFDictionary(serverPrefs
);
355 serverSignature
= CFDictionaryGetValue(serverDict
, CFSTR("signature"));
356 serverSignature
= isA_CFData(serverSignature
);
359 if ((serverPrefs
== NULL
) || (serverSignature
== NULL
)) {
360 if (serverDict
!= NULL
) CFRelease(serverDict
);
364 prefsPrivate
->prefs
= CFDictionaryCreateMutableCopy(NULL
, 0, serverPrefs
);
365 prefsPrivate
->signature
= CFRetain(serverSignature
);
366 prefsPrivate
->accessed
= TRUE
;
367 CFRelease(serverDict
);
374 if (prefsPrivate
->helper_port
!= MACH_PORT_NULL
) {
375 _SCHelperClose(&prefsPrivate
->helper_port
);
378 status
= kSCStatusAccessError
;
388 static SCPreferencesPrivateRef
389 __SCPreferencesCreate(CFAllocatorRef allocator
,
392 CFDataRef authorizationData
,
393 CFDictionaryRef options
)
395 SCPreferencesPrivateRef prefsPrivate
;
396 int sc_status
= kSCStatusOK
;
399 * allocate and initialize a new prefs session
401 prefsPrivate
= __SCPreferencesCreatePrivate(allocator
);
402 if (prefsPrivate
== NULL
) {
406 prefsPrivate
->name
= CFStringCreateCopy(allocator
, name
);
407 if (prefsID
!= NULL
) {
408 prefsPrivate
->prefsID
= CFStringCreateCopy(allocator
, prefsID
);
410 if (authorizationData
!= NULL
) {
411 prefsPrivate
->authorizationData
= CFRetain(authorizationData
);
413 if (options
!= NULL
) {
414 prefsPrivate
->options
= CFDictionaryCreateCopy(allocator
, options
);
420 * convert prefsID to path
422 prefsPrivate
->path
= __SCPreferencesPath(allocator
,
424 (prefsPrivate
->newPath
== NULL
));
425 if (prefsPrivate
->path
== NULL
) {
426 sc_status
= kSCStatusFailed
;
430 if (access(prefsPrivate
->path
, R_OK
) == 0) {
437 if ((prefsID
== NULL
) || !CFStringHasPrefix(prefsID
, CFSTR("/"))) {
438 /* if default preference ID or relative path */
439 if (prefsPrivate
->newPath
== NULL
) {
441 * we've looked in the "new" prefs directory
442 * without success. Save the "new" path and
443 * look in the "old" prefs directory.
445 prefsPrivate
->newPath
= prefsPrivate
->path
;
449 * we've looked in both the "new" and "old"
450 * prefs directories without success. Use
453 CFAllocatorDeallocate(NULL
, prefsPrivate
->path
);
454 prefsPrivate
->path
= prefsPrivate
->newPath
;
455 prefsPrivate
->newPath
= NULL
;
459 /* no preference data, start fresh */
460 sc_status
= kSCStatusNoConfigFile
;
464 if (prefsPrivate
->authorizationData
!= NULL
) {
465 /* no problem, we'll be using the helper */
469 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("__SCPreferencesCreate open() failed: %s"), strerror(errno
));
470 sc_status
= kSCStatusAccessError
;
473 SCLog(TRUE
, LOG_ERR
, CFSTR("__SCPreferencesCreate open() failed: %s"), strerror(errno
));
474 sc_status
= kSCStatusFailed
;
480 CFRelease(prefsPrivate
);
481 _SCErrorSet(sc_status
);
487 _SCErrorSet(sc_status
);
492 __private_extern__
void
493 __SCPreferencesAccess(SCPreferencesRef prefs
)
495 CFAllocatorRef allocator
= CFGetAllocator(prefs
);
497 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
500 if (prefsPrivate
->accessed
) {
501 // if preference data has already been accessed
505 if (!prefsPrivate
->authorizationRequired
) {
506 if (access(prefsPrivate
->path
, R_OK
) == 0) {
507 fd
= open(prefsPrivate
->path
, O_RDONLY
, 0644);
516 if (fstat(fd
, &statBuf
) == -1) {
517 SCLog(TRUE
, LOG_ERR
, CFSTR("__SCPreferencesAccess fstat() failed: %s"), strerror(errno
));
518 bzero(&statBuf
, sizeof(statBuf
));
523 /* no preference data, start fresh */
527 if (prefsPrivate
->authorizationData
!= NULL
) {
528 if (__SCPreferencesAccess_helper(prefs
)) {
532 CFSTR("__SCPreferencesAccess_helper() failed: %s"),
533 SCErrorString(SCError()));
539 SCLog(TRUE
, LOG_ERR
, CFSTR("__SCPreferencesAccess open() failed: %s"), strerror(errno
));
542 bzero(&statBuf
, sizeof(statBuf
));
545 if (prefsPrivate
->signature
!= NULL
) CFRelease(prefsPrivate
->signature
);
546 prefsPrivate
->signature
= __SCPSignatureFromStatbuf(&statBuf
);
548 if (statBuf
.st_size
> 0) {
549 CFDictionaryRef dict
;
550 CFErrorRef error
= NULL
;
551 CFMutableDataRef xmlData
;
554 * extract property list
556 xmlData
= CFDataCreateMutable(allocator
, statBuf
.st_size
);
557 CFDataSetLength(xmlData
, statBuf
.st_size
);
558 if (read(fd
, (void *)CFDataGetBytePtr(xmlData
), statBuf
.st_size
) != statBuf
.st_size
) {
559 /* corrupt prefs file, start fresh */
560 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("__SCPreferencesAccess read(): could not load preference data."));
569 dict
= CFPropertyListCreateWithData(allocator
, xmlData
, kCFPropertyListImmutable
, NULL
, &error
);
572 /* corrupt prefs file, start fresh */
575 CFSTR("__SCPreferencesAccess CFPropertyListCreateWithData(): %@"),
583 * make sure that we've got a dictionary
585 if (!isA_CFDictionary(dict
)) {
586 /* corrupt prefs file, start fresh */
587 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("__SCPreferencesAccess CFGetTypeID(): not a dictionary."));
592 prefsPrivate
->prefs
= CFDictionaryCreateMutableCopy(allocator
, 0, dict
);
602 if (prefsPrivate
->prefs
== NULL
) {
604 * new file, create empty preferences
606 // SCLog(_sc_verbose, LOG_DEBUG, CFSTR("__SCPreferencesAccess(): creating new preferences file."));
607 prefsPrivate
->prefs
= CFDictionaryCreateMutable(allocator
,
609 &kCFTypeDictionaryKeyCallBacks
,
610 &kCFTypeDictionaryValueCallBacks
);
611 prefsPrivate
->changed
= TRUE
;
614 prefsPrivate
->accessed
= TRUE
;
620 SCPreferencesCreate(CFAllocatorRef allocator
,
624 SCPreferencesPrivateRef prefsPrivate
;
626 prefsPrivate
= __SCPreferencesCreate(allocator
, name
, prefsID
, NULL
, NULL
);
627 return (SCPreferencesRef
)prefsPrivate
;
632 SCPreferencesCreateWithAuthorization(CFAllocatorRef allocator
,
635 AuthorizationRef authorization
)
637 SCPreferencesRef prefs
;
639 #if !TARGET_OS_IPHONE
640 if (authorization
== NULL
) {
641 authorization
= kSCPreferencesUseEntitlementAuthorization
;
643 #else // !TARGET_OS_IPHONE
644 authorization
= kSCPreferencesUseEntitlementAuthorization
;
645 #endif // !TARGET_OS_IPHONE
647 prefs
= SCPreferencesCreateWithOptions(allocator
, name
, prefsID
, authorization
, NULL
);
653 SCPreferencesCreateWithOptions(CFAllocatorRef allocator
,
656 AuthorizationRef authorization
,
657 CFDictionaryRef options
)
659 CFDataRef authorizationData
= NULL
;
660 SCPreferencesPrivateRef prefsPrivate
;
662 if (options
!= NULL
) {
663 if (!isA_CFDictionary(options
)) {
664 _SCErrorSet(kSCStatusInvalidArgument
);
669 if (authorization
!= NULL
) {
670 CFMutableDictionaryRef authorizationDict
;
672 CFStringRef bundleID
= NULL
;
674 authorizationDict
= CFDictionaryCreateMutable(NULL
,
676 &kCFTypeDictionaryKeyCallBacks
,
677 &kCFTypeDictionaryValueCallBacks
);
678 #if !TARGET_OS_IPHONE
679 if (authorization
!= kSCPreferencesUseEntitlementAuthorization
) {
681 AuthorizationExternalForm extForm
;
684 os_status
= AuthorizationMakeExternalForm(authorization
, &extForm
);
685 if (os_status
!= errAuthorizationSuccess
) {
686 SCLog(TRUE
, LOG_INFO
, CFSTR("_SCHelperOpen AuthorizationMakeExternalForm() failed"));
687 _SCErrorSet(kSCStatusInvalidArgument
);
688 CFRelease(authorizationDict
);
692 data
= CFDataCreate(NULL
, (const UInt8
*)extForm
.bytes
, sizeof(extForm
.bytes
));
693 CFDictionaryAddValue(authorizationDict
,
694 kSCHelperAuthAuthorization
,
700 /* get the application/executable/bundle name */
701 bundle
= CFBundleGetMainBundle();
702 if (bundle
!= NULL
) {
703 bundleID
= CFBundleGetIdentifier(bundle
);
704 if (bundleID
!= NULL
) {
709 url
= CFBundleCopyExecutableURL(bundle
);
711 bundleID
= CFURLCopyPath(url
);
716 if (bundleID
!= NULL
) {
717 if (CFEqual(bundleID
, CFSTR("/"))) {
723 if (bundleID
== NULL
) {
724 bundleID
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("Unknown(%d)"), getpid());
726 CFDictionaryAddValue(authorizationDict
,
727 kSCHelperAuthCallerInfo
,
731 if (authorizationDict
!= NULL
) {
732 _SCSerialize((CFPropertyListRef
)authorizationDict
,
736 CFRelease(authorizationDict
);
740 prefsPrivate
= __SCPreferencesCreate(allocator
, name
, prefsID
, authorizationData
, options
);
741 if (authorizationData
!= NULL
) CFRelease(authorizationData
);
743 return (SCPreferencesRef
)prefsPrivate
;
748 SCPreferencesGetTypeID(void) {
749 pthread_once(&initialized
, __SCPreferencesInitialize
); /* initialize runtime */
750 return __kSCPreferencesTypeID
;
755 prefsNotify(SCDynamicStoreRef store
, CFArrayRef changedKeys
, void *info
)
758 void (*context_release
)(const void *);
761 SCPreferencesNotification notify
= 0;
762 SCPreferencesRef prefs
= (SCPreferencesRef
)info
;
763 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
764 SCPreferencesCallBack rlsFunction
;
766 n
= (changedKeys
!= NULL
) ? CFArrayGetCount(changedKeys
) : 0;
767 for (i
= 0; i
< n
; i
++) {
770 key
= CFArrayGetValueAtIndex(changedKeys
, i
);
771 if (CFEqual(key
, prefsPrivate
->sessionKeyCommit
)) {
772 // if preferences have been saved
773 notify
|= kSCPreferencesNotificationCommit
;
775 if (CFEqual(key
, prefsPrivate
->sessionKeyApply
)) {
776 // if stored preferences should be applied to current configuration
777 notify
|= kSCPreferencesNotificationApply
;
786 pthread_mutex_lock(&prefsPrivate
->lock
);
789 rlsFunction
= prefsPrivate
->rlsFunction
;
790 if (prefsPrivate
->rlsContext
.retain
!= NULL
) {
791 context_info
= (void *)prefsPrivate
->rlsContext
.retain(prefsPrivate
->rlsContext
.info
);
792 context_release
= prefsPrivate
->rlsContext
.release
;
794 context_info
= prefsPrivate
->rlsContext
.info
;
795 context_release
= NULL
;
798 pthread_mutex_unlock(&prefsPrivate
->lock
);
800 if (rlsFunction
!= NULL
) {
801 (*rlsFunction
)(prefs
, notify
, context_info
);
804 if (context_release
!= NULL
) {
805 (*context_release
)(context_info
);
812 __private_extern__ Boolean
813 __SCPreferencesAddSession(SCPreferencesRef prefs
)
815 CFAllocatorRef allocator
= CFGetAllocator(prefs
);
816 SCDynamicStoreContext context
= { 0
822 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
824 /* establish a dynamic store session */
825 prefsPrivate
->session
= SCDynamicStoreCreate(allocator
,
829 if (prefsPrivate
->session
== NULL
) {
830 SCLog(_sc_verbose
, LOG_INFO
, CFSTR("__SCPreferencesAddSession SCDynamicStoreCreate() failed"));
834 /* create the session "commit" key */
835 prefsPrivate
->sessionKeyCommit
= _SCPNotificationKey(NULL
,
836 prefsPrivate
->prefsID
,
837 kSCPreferencesKeyCommit
);
839 /* create the session "apply" key */
840 prefsPrivate
->sessionKeyApply
= _SCPNotificationKey(NULL
,
841 prefsPrivate
->prefsID
,
842 kSCPreferencesKeyApply
);
849 SCPreferencesSetCallback(SCPreferencesRef prefs
,
850 SCPreferencesCallBack callout
,
851 SCPreferencesContext
*context
)
853 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
855 if (!isA_SCPreferences(prefs
)) {
856 /* sorry, you must provide a session */
857 _SCErrorSet(kSCStatusNoPrefsSession
);
861 pthread_mutex_lock(&prefsPrivate
->lock
);
863 if (prefsPrivate
->rlsContext
.release
!= NULL
) {
864 /* let go of the current context */
865 (*prefsPrivate
->rlsContext
.release
)(prefsPrivate
->rlsContext
.info
);
868 prefsPrivate
->rlsFunction
= callout
;
869 prefsPrivate
->rlsContext
.info
= NULL
;
870 prefsPrivate
->rlsContext
.retain
= NULL
;
871 prefsPrivate
->rlsContext
.release
= NULL
;
872 prefsPrivate
->rlsContext
.copyDescription
= NULL
;
873 if (context
!= NULL
) {
874 bcopy(context
, &prefsPrivate
->rlsContext
, sizeof(SCPreferencesContext
));
875 if (context
->retain
!= NULL
) {
876 prefsPrivate
->rlsContext
.info
= (void *)(*context
->retain
)(context
->info
);
880 pthread_mutex_unlock(&prefsPrivate
->lock
);
887 __SCPreferencesScheduleWithRunLoop(SCPreferencesRef prefs
,
888 CFRunLoopRef runLoop
,
889 CFStringRef runLoopMode
,
890 dispatch_queue_t queue
)
893 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
895 pthread_mutex_lock(&prefsPrivate
->lock
);
897 if ((prefsPrivate
->dispatchQueue
!= NULL
) || // if we are already scheduled on a dispatch queue
898 ((queue
!= NULL
) && prefsPrivate
->scheduled
)) { // if we are already scheduled on a CFRunLoop
899 _SCErrorSet(kSCStatusInvalidArgument
);
903 if (!prefsPrivate
->scheduled
) {
904 CFMutableArrayRef keys
;
906 if (prefsPrivate
->session
== NULL
) {
907 ok
= __SCPreferencesAddSession(prefs
);
913 CFRetain(prefs
); // hold a reference to the prefs
915 keys
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
916 CFArrayAppendValue(keys
, prefsPrivate
->sessionKeyCommit
);
917 CFArrayAppendValue(keys
, prefsPrivate
->sessionKeyApply
);
918 (void) SCDynamicStoreSetNotificationKeys(prefsPrivate
->session
, keys
, NULL
);
921 if (runLoop
!= NULL
) {
922 prefsPrivate
->rls
= SCDynamicStoreCreateRunLoopSource(NULL
, prefsPrivate
->session
, 0);
923 prefsPrivate
->rlList
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
926 prefsPrivate
->scheduled
= TRUE
;
930 ok
= SCDynamicStoreSetDispatchQueue(prefsPrivate
->session
, queue
);
932 prefsPrivate
->scheduled
= FALSE
;
933 (void) SCDynamicStoreSetNotificationKeys(prefsPrivate
->session
, NULL
, NULL
);
938 prefsPrivate
->dispatchQueue
= queue
;
939 dispatch_retain(prefsPrivate
->dispatchQueue
);
941 if (!_SC_isScheduled(NULL
, runLoop
, runLoopMode
, prefsPrivate
->rlList
)) {
943 * if we do not already have notifications scheduled with
944 * this runLoop / runLoopMode
946 CFRunLoopAddSource(runLoop
, prefsPrivate
->rls
, runLoopMode
);
949 _SC_schedule(prefs
, runLoop
, runLoopMode
, prefsPrivate
->rlList
);
956 pthread_mutex_unlock(&prefsPrivate
->lock
);
962 __SCPreferencesUnscheduleFromRunLoop(SCPreferencesRef prefs
,
963 CFRunLoopRef runLoop
,
964 CFStringRef runLoopMode
)
966 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
970 pthread_mutex_lock(&prefsPrivate
->lock
);
972 if ((runLoop
!= NULL
) && !prefsPrivate
->scheduled
) { // if we should be scheduled (but are not)
973 _SCErrorSet(kSCStatusInvalidArgument
);
977 if (((runLoop
== NULL
) && (prefsPrivate
->dispatchQueue
== NULL
)) || // if we should be scheduled on a dispatch queue (but are not)
978 ((runLoop
!= NULL
) && (prefsPrivate
->dispatchQueue
!= NULL
))) { // if we should be scheduled on a CFRunLoop (but are scheduled on a dispatch queue)
979 _SCErrorSet(kSCStatusInvalidArgument
);
983 if (runLoop
== NULL
) {
984 SCDynamicStoreSetDispatchQueue(prefsPrivate
->session
, NULL
);
985 dispatch_release(prefsPrivate
->dispatchQueue
);
986 prefsPrivate
->dispatchQueue
= NULL
;
988 if (!_SC_unschedule(prefs
, runLoop
, runLoopMode
, prefsPrivate
->rlList
, FALSE
)) {
989 // if not currently scheduled on this runLoop / runLoopMode
990 _SCErrorSet(kSCStatusInvalidArgument
);
994 n
= CFArrayGetCount(prefsPrivate
->rlList
);
995 if (n
== 0 || !_SC_isScheduled(NULL
, runLoop
, runLoopMode
, prefsPrivate
->rlList
)) {
997 * if we are no longer scheduled to receive notifications for
998 * this runLoop / runLoopMode
1000 CFRunLoopRemoveSource(runLoop
, prefsPrivate
->rls
, runLoopMode
);
1003 // if *all* notifications have been unscheduled
1004 CFRelease(prefsPrivate
->rlList
);
1005 prefsPrivate
->rlList
= NULL
;
1006 CFRunLoopSourceInvalidate(prefsPrivate
->rls
);
1007 CFRelease(prefsPrivate
->rls
);
1008 prefsPrivate
->rls
= NULL
;
1014 CFArrayRef changedKeys
;
1016 // if *all* notifications have been unscheduled
1017 prefsPrivate
->scheduled
= FALSE
;
1019 // no need to track changes
1020 (void) SCDynamicStoreSetNotificationKeys(prefsPrivate
->session
, NULL
, NULL
);
1022 // clear out any pending notifications
1023 changedKeys
= SCDynamicStoreCopyNotifiedKeys(prefsPrivate
->session
);
1024 if (changedKeys
!= NULL
) {
1025 CFRelease(changedKeys
);
1028 // release our reference to the prefs
1036 pthread_mutex_unlock(&prefsPrivate
->lock
);
1042 SCPreferencesScheduleWithRunLoop(SCPreferencesRef prefs
,
1043 CFRunLoopRef runLoop
,
1044 CFStringRef runLoopMode
)
1046 if (!isA_SCPreferences(prefs
) || (runLoop
== NULL
) || (runLoopMode
== NULL
)) {
1047 _SCErrorSet(kSCStatusInvalidArgument
);
1051 return __SCPreferencesScheduleWithRunLoop(prefs
, runLoop
, runLoopMode
, NULL
);
1056 SCPreferencesUnscheduleFromRunLoop(SCPreferencesRef prefs
,
1057 CFRunLoopRef runLoop
,
1058 CFStringRef runLoopMode
)
1060 if (!isA_SCPreferences(prefs
) || (runLoop
== NULL
) || (runLoopMode
== NULL
)) {
1061 _SCErrorSet(kSCStatusInvalidArgument
);
1065 return __SCPreferencesUnscheduleFromRunLoop(prefs
, runLoop
, runLoopMode
);
1070 SCPreferencesSetDispatchQueue(SCPreferencesRef prefs
,
1071 dispatch_queue_t queue
)
1075 if (!isA_SCPreferences(prefs
)) {
1076 /* sorry, you must provide a session */
1077 _SCErrorSet(kSCStatusNoPrefsSession
);
1081 if (queue
!= NULL
) {
1082 ok
= __SCPreferencesScheduleWithRunLoop(prefs
, NULL
, NULL
, queue
);
1084 ok
= __SCPreferencesUnscheduleFromRunLoop(prefs
, NULL
, NULL
);
1092 __SCPreferencesSynchronize_helper(SCPreferencesRef prefs
)
1095 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
1096 uint32_t status
= kSCStatusOK
;
1098 if (prefsPrivate
->helper_port
== MACH_PORT_NULL
) {
1103 // have the helper "synchronize" the prefs
1104 ok
= _SCHelperExec(prefsPrivate
->helper_port
,
1105 SCHELPER_MSG_PREFS_SYNCHRONIZE
,
1111 if (prefsPrivate
->helper_port
!= MACH_PORT_NULL
) {
1112 _SCHelperClose(&prefsPrivate
->helper_port
);
1121 SCPreferencesSynchronize(SCPreferencesRef prefs
)
1123 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
1125 if (!isA_SCPreferences(prefs
)) {
1126 /* sorry, you must provide a session */
1127 _SCErrorSet(kSCStatusNoPrefsSession
);
1131 if (prefsPrivate
->authorizationData
!= NULL
) {
1132 __SCPreferencesSynchronize_helper(prefs
);
1134 if (prefsPrivate
->prefs
!= NULL
) {
1135 CFRelease(prefsPrivate
->prefs
);
1136 prefsPrivate
->prefs
= NULL
;
1138 if (prefsPrivate
->signature
!= NULL
) {
1139 CFRelease(prefsPrivate
->signature
);
1140 prefsPrivate
->signature
= NULL
;
1142 prefsPrivate
->accessed
= FALSE
;
1143 prefsPrivate
->changed
= FALSE
;