2 * Copyright(c) 2000-2014 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
->limit_SCNetworkConfiguration
= FALSE
;
213 prefsPrivate
->authorizationData
= NULL
;
214 prefsPrivate
->authorizationRequired
= FALSE
;
215 prefsPrivate
->helper_port
= MACH_PORT_NULL
;
221 __private_extern__ Boolean
222 __SCPreferencesCreate_helper(SCPreferencesRef prefs
)
224 CFDataRef data
= NULL
;
225 CFMutableDictionaryRef info
;
228 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
229 uint32_t status
= kSCStatusOK
;
231 uint32_t pid
= getpid();
234 ok
= _SCHelperOpen(prefsPrivate
->authorizationData
,
235 &prefsPrivate
->helper_port
);
240 // create a dictionary of information to pass to the helper
241 info
= CFDictionaryCreateMutable(NULL
,
243 &kCFTypeDictionaryKeyCallBacks
,
244 &kCFTypeDictionaryValueCallBacks
);
247 if (prefsPrivate
->prefsID
!= NULL
) {
248 CFDictionarySetValue(info
, CFSTR("prefsID"), prefsPrivate
->prefsID
);
252 if (prefsPrivate
->options
!= NULL
) {
253 CFDictionarySetValue(info
, CFSTR("options"), prefsPrivate
->options
);
256 // save preferences session "name"
257 CFDictionarySetValue(info
, CFSTR("name"), prefsPrivate
->name
);
260 num
= CFNumberCreate(NULL
, kCFNumberSInt32Type
, &pid
);
261 CFDictionarySetValue(info
, CFSTR("PID"), num
);
265 str
= CFStringCreateWithCString(NULL
, getprogname(), kCFStringEncodingUTF8
);
266 CFDictionarySetValue(info
, CFSTR("PROC_NAME"), str
);
269 // serialize the info
270 ok
= _SCSerialize(info
, &data
, NULL
, NULL
);
272 if (data
== NULL
|| !ok
) {
276 // have the helper "open" the prefs
277 ok
= _SCHelperExec(prefsPrivate
->helper_port
,
278 SCHELPER_MSG_PREFS_OPEN
,
282 if (data
!= NULL
) CFRelease(data
);
287 if (status
!= kSCStatusOK
) {
296 if (prefsPrivate
->helper_port
!= MACH_PORT_NULL
) {
297 _SCHelperClose(&prefsPrivate
->helper_port
);
300 status
= kSCStatusAccessError
;
311 __SCPreferencesAccess_helper(SCPreferencesRef prefs
)
314 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
315 CFDictionaryRef serverDict
= NULL
;
316 CFDictionaryRef serverPrefs
= NULL
;
317 CFDictionaryRef serverSignature
= NULL
;
318 uint32_t status
= kSCStatusOK
;
319 CFDataRef reply
= NULL
;
321 if (prefsPrivate
->helper_port
== MACH_PORT_NULL
) {
322 ok
= __SCPreferencesCreate_helper(prefs
);
328 // have the helper "access" the prefs
329 ok
= _SCHelperExec(prefsPrivate
->helper_port
,
330 SCHELPER_MSG_PREFS_ACCESS
,
338 if (status
!= kSCStatusOK
) {
346 ok
= _SCUnserialize((CFPropertyListRef
*)&serverDict
, reply
, NULL
, 0);
352 if (isA_CFDictionary(serverDict
)) {
353 serverPrefs
= CFDictionaryGetValue(serverDict
, CFSTR("preferences"));
354 serverPrefs
= isA_CFDictionary(serverPrefs
);
356 serverSignature
= CFDictionaryGetValue(serverDict
, CFSTR("signature"));
357 serverSignature
= isA_CFData(serverSignature
);
360 if ((serverPrefs
== NULL
) || (serverSignature
== NULL
)) {
361 if (serverDict
!= NULL
) CFRelease(serverDict
);
365 prefsPrivate
->prefs
= CFDictionaryCreateMutableCopy(NULL
, 0, serverPrefs
);
366 prefsPrivate
->signature
= CFRetain(serverSignature
);
367 prefsPrivate
->accessed
= TRUE
;
368 CFRelease(serverDict
);
375 if (prefsPrivate
->helper_port
!= MACH_PORT_NULL
) {
376 _SCHelperClose(&prefsPrivate
->helper_port
);
379 status
= kSCStatusAccessError
;
389 static SCPreferencesPrivateRef
390 __SCPreferencesCreate(CFAllocatorRef allocator
,
393 CFDataRef authorizationData
,
394 CFDictionaryRef options
)
396 SCPreferencesPrivateRef prefsPrivate
;
397 int sc_status
= kSCStatusOK
;
400 * allocate and initialize a new prefs session
402 prefsPrivate
= __SCPreferencesCreatePrivate(allocator
);
403 if (prefsPrivate
== NULL
) {
407 prefsPrivate
->name
= CFStringCreateCopy(allocator
, name
);
408 if (prefsID
!= NULL
) {
409 prefsPrivate
->prefsID
= CFStringCreateCopy(allocator
, prefsID
);
411 if (authorizationData
!= NULL
) {
412 prefsPrivate
->authorizationData
= CFRetain(authorizationData
);
414 if (options
!= NULL
) {
415 prefsPrivate
->options
= CFDictionaryCreateCopy(allocator
, options
);
421 * convert prefsID to path
423 prefsPrivate
->path
= __SCPreferencesPath(allocator
,
425 (prefsPrivate
->newPath
== NULL
));
426 if (prefsPrivate
->path
== NULL
) {
427 sc_status
= kSCStatusFailed
;
431 if (access(prefsPrivate
->path
, R_OK
) == 0) {
438 if ((prefsID
== NULL
) || !CFStringHasPrefix(prefsID
, CFSTR("/"))) {
439 /* if default preference ID or relative path */
440 if (prefsPrivate
->newPath
== NULL
) {
442 * we've looked in the "new" prefs directory
443 * without success. Save the "new" path and
444 * look in the "old" prefs directory.
446 prefsPrivate
->newPath
= prefsPrivate
->path
;
450 * we've looked in both the "new" and "old"
451 * prefs directories without success. Use
454 CFAllocatorDeallocate(NULL
, prefsPrivate
->path
);
455 prefsPrivate
->path
= prefsPrivate
->newPath
;
456 prefsPrivate
->newPath
= NULL
;
460 /* no preference data, start fresh */
461 sc_status
= kSCStatusNoConfigFile
;
465 if (prefsPrivate
->authorizationData
!= NULL
) {
466 /* no problem, we'll be using the helper */
470 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("__SCPreferencesCreate open() failed: %s"), strerror(errno
));
471 sc_status
= kSCStatusAccessError
;
474 SCLog(TRUE
, LOG_ERR
, CFSTR("__SCPreferencesCreate open() failed: %s"), strerror(errno
));
475 sc_status
= kSCStatusFailed
;
481 CFRelease(prefsPrivate
);
482 _SCErrorSet(sc_status
);
488 _SCErrorSet(sc_status
);
493 __private_extern__
void
494 __SCPreferencesAccess(SCPreferencesRef prefs
)
496 CFAllocatorRef allocator
= CFGetAllocator(prefs
);
498 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
501 if (prefsPrivate
->accessed
) {
502 // if preference data has already been accessed
506 if (!prefsPrivate
->authorizationRequired
) {
507 if (access(prefsPrivate
->path
, R_OK
) == 0) {
508 fd
= open(prefsPrivate
->path
, O_RDONLY
, 0644);
517 if (fstat(fd
, &statBuf
) == -1) {
518 SCLog(TRUE
, LOG_ERR
, CFSTR("__SCPreferencesAccess fstat() failed: %s"), strerror(errno
));
519 bzero(&statBuf
, sizeof(statBuf
));
524 /* no preference data, start fresh */
528 if (prefsPrivate
->authorizationData
!= NULL
) {
529 if (__SCPreferencesAccess_helper(prefs
)) {
533 CFSTR("__SCPreferencesAccess_helper() failed: %s"),
534 SCErrorString(SCError()));
540 SCLog(TRUE
, LOG_ERR
, CFSTR("__SCPreferencesAccess open() failed: %s"), strerror(errno
));
543 bzero(&statBuf
, sizeof(statBuf
));
546 if (prefsPrivate
->signature
!= NULL
) CFRelease(prefsPrivate
->signature
);
547 prefsPrivate
->signature
= __SCPSignatureFromStatbuf(&statBuf
);
549 if (statBuf
.st_size
> 0) {
550 CFDictionaryRef dict
;
551 CFErrorRef error
= NULL
;
552 CFMutableDataRef xmlData
;
555 * extract property list
557 xmlData
= CFDataCreateMutable(allocator
, (CFIndex
)statBuf
.st_size
);
558 CFDataSetLength(xmlData
, (CFIndex
)statBuf
.st_size
);
559 if (read(fd
, (void *)CFDataGetBytePtr(xmlData
), (CFIndex
)statBuf
.st_size
) != (CFIndex
)statBuf
.st_size
) {
560 /* corrupt prefs file, start fresh */
561 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("__SCPreferencesAccess read(): could not load preference data."));
570 dict
= CFPropertyListCreateWithData(allocator
, xmlData
, kCFPropertyListImmutable
, NULL
, &error
);
573 /* corrupt prefs file, start fresh */
576 CFSTR("__SCPreferencesAccess CFPropertyListCreateWithData(): %@"),
584 * make sure that we've got a dictionary
586 if (!isA_CFDictionary(dict
)) {
587 /* corrupt prefs file, start fresh */
588 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("__SCPreferencesAccess CFGetTypeID(): not a dictionary."));
593 prefsPrivate
->prefs
= CFDictionaryCreateMutableCopy(allocator
, 0, dict
);
603 if (prefsPrivate
->prefs
== NULL
) {
605 * new file, create empty preferences
607 // SCLog(_sc_verbose, LOG_DEBUG, CFSTR("__SCPreferencesAccess(): creating new preferences file."));
608 prefsPrivate
->prefs
= CFDictionaryCreateMutable(allocator
,
610 &kCFTypeDictionaryKeyCallBacks
,
611 &kCFTypeDictionaryValueCallBacks
);
612 prefsPrivate
->changed
= TRUE
;
615 prefsPrivate
->accessed
= TRUE
;
621 SCPreferencesCreate(CFAllocatorRef allocator
,
625 SCPreferencesPrivateRef prefsPrivate
;
627 prefsPrivate
= __SCPreferencesCreate(allocator
, name
, prefsID
, NULL
, NULL
);
628 return (SCPreferencesRef
)prefsPrivate
;
633 SCPreferencesCreateWithAuthorization(CFAllocatorRef allocator
,
636 AuthorizationRef authorization
)
638 SCPreferencesRef prefs
;
640 #if !TARGET_OS_IPHONE
641 if (authorization
== NULL
) {
642 authorization
= kSCPreferencesUseEntitlementAuthorization
;
644 #else // !TARGET_OS_IPHONE
645 authorization
= kSCPreferencesUseEntitlementAuthorization
;
646 #endif // !TARGET_OS_IPHONE
648 prefs
= SCPreferencesCreateWithOptions(allocator
, name
, prefsID
, authorization
, NULL
);
654 SCPreferencesCreateWithOptions(CFAllocatorRef allocator
,
657 AuthorizationRef authorization
,
658 CFDictionaryRef options
)
660 CFDataRef authorizationData
= NULL
;
661 SCPreferencesPrivateRef prefsPrivate
;
663 if (options
!= NULL
) {
664 if (!isA_CFDictionary(options
)) {
665 _SCErrorSet(kSCStatusInvalidArgument
);
670 if (authorization
!= NULL
) {
671 CFMutableDictionaryRef authorizationDict
;
673 CFStringRef bundleID
= NULL
;
675 authorizationDict
= CFDictionaryCreateMutable(NULL
,
677 &kCFTypeDictionaryKeyCallBacks
,
678 &kCFTypeDictionaryValueCallBacks
);
679 #if !TARGET_OS_IPHONE
680 if (authorization
!= kSCPreferencesUseEntitlementAuthorization
) {
682 AuthorizationExternalForm extForm
;
685 os_status
= AuthorizationMakeExternalForm(authorization
, &extForm
);
686 if (os_status
!= errAuthorizationSuccess
) {
687 SCLog(TRUE
, LOG_INFO
, CFSTR("_SCHelperOpen AuthorizationMakeExternalForm() failed"));
688 _SCErrorSet(kSCStatusInvalidArgument
);
689 CFRelease(authorizationDict
);
693 data
= CFDataCreate(NULL
, (const UInt8
*)extForm
.bytes
, sizeof(extForm
.bytes
));
694 CFDictionaryAddValue(authorizationDict
,
695 kSCHelperAuthAuthorization
,
701 /* get the application/executable/bundle name */
702 bundle
= CFBundleGetMainBundle();
703 if (bundle
!= NULL
) {
704 bundleID
= CFBundleGetIdentifier(bundle
);
705 if (bundleID
!= NULL
) {
710 url
= CFBundleCopyExecutableURL(bundle
);
712 bundleID
= CFURLCopyPath(url
);
717 if (bundleID
!= NULL
) {
718 if (CFEqual(bundleID
, CFSTR("/"))) {
724 if (bundleID
== NULL
) {
725 bundleID
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("Unknown(%d)"), getpid());
727 CFDictionaryAddValue(authorizationDict
,
728 kSCHelperAuthCallerInfo
,
732 if (authorizationDict
!= NULL
) {
733 _SCSerialize((CFPropertyListRef
)authorizationDict
,
737 CFRelease(authorizationDict
);
741 prefsPrivate
= __SCPreferencesCreate(allocator
, name
, prefsID
, authorizationData
, options
);
742 if (authorizationData
!= NULL
) CFRelease(authorizationData
);
744 return (SCPreferencesRef
)prefsPrivate
;
749 SCPreferencesGetTypeID(void) {
750 pthread_once(&initialized
, __SCPreferencesInitialize
); /* initialize runtime */
751 return __kSCPreferencesTypeID
;
756 prefsNotify(SCDynamicStoreRef store
, CFArrayRef changedKeys
, void *info
)
759 void (*context_release
)(const void *);
762 SCPreferencesNotification notify
= 0;
763 SCPreferencesRef prefs
= (SCPreferencesRef
)info
;
764 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
765 SCPreferencesCallBack rlsFunction
;
767 n
= (changedKeys
!= NULL
) ? CFArrayGetCount(changedKeys
) : 0;
768 for (i
= 0; i
< n
; i
++) {
771 key
= CFArrayGetValueAtIndex(changedKeys
, i
);
772 if (CFEqual(key
, prefsPrivate
->sessionKeyCommit
)) {
773 // if preferences have been saved
774 notify
|= kSCPreferencesNotificationCommit
;
776 if (CFEqual(key
, prefsPrivate
->sessionKeyApply
)) {
777 // if stored preferences should be applied to current configuration
778 notify
|= kSCPreferencesNotificationApply
;
787 pthread_mutex_lock(&prefsPrivate
->lock
);
790 rlsFunction
= prefsPrivate
->rlsFunction
;
791 if (prefsPrivate
->rlsContext
.retain
!= NULL
) {
792 context_info
= (void *)prefsPrivate
->rlsContext
.retain(prefsPrivate
->rlsContext
.info
);
793 context_release
= prefsPrivate
->rlsContext
.release
;
795 context_info
= prefsPrivate
->rlsContext
.info
;
796 context_release
= NULL
;
799 pthread_mutex_unlock(&prefsPrivate
->lock
);
801 if (rlsFunction
!= NULL
) {
802 (*rlsFunction
)(prefs
, notify
, context_info
);
805 if (context_release
!= NULL
) {
806 (*context_release
)(context_info
);
813 __private_extern__ Boolean
814 __SCPreferencesAddSession(SCPreferencesRef prefs
)
816 CFAllocatorRef allocator
= CFGetAllocator(prefs
);
817 SCDynamicStoreContext context
= { 0
823 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
825 /* establish a dynamic store session */
826 prefsPrivate
->session
= SCDynamicStoreCreate(allocator
,
830 if (prefsPrivate
->session
== NULL
) {
831 SCLog(_sc_verbose
, LOG_INFO
, CFSTR("__SCPreferencesAddSession SCDynamicStoreCreate() failed"));
835 /* create the session "commit" key */
836 prefsPrivate
->sessionKeyCommit
= _SCPNotificationKey(NULL
,
837 prefsPrivate
->prefsID
,
838 kSCPreferencesKeyCommit
);
840 /* create the session "apply" key */
841 prefsPrivate
->sessionKeyApply
= _SCPNotificationKey(NULL
,
842 prefsPrivate
->prefsID
,
843 kSCPreferencesKeyApply
);
850 SCPreferencesSetCallback(SCPreferencesRef prefs
,
851 SCPreferencesCallBack callout
,
852 SCPreferencesContext
*context
)
854 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
856 if (!isA_SCPreferences(prefs
)) {
857 /* sorry, you must provide a session */
858 _SCErrorSet(kSCStatusNoPrefsSession
);
862 pthread_mutex_lock(&prefsPrivate
->lock
);
864 if (prefsPrivate
->rlsContext
.release
!= NULL
) {
865 /* let go of the current context */
866 (*prefsPrivate
->rlsContext
.release
)(prefsPrivate
->rlsContext
.info
);
869 prefsPrivate
->rlsFunction
= callout
;
870 prefsPrivate
->rlsContext
.info
= NULL
;
871 prefsPrivate
->rlsContext
.retain
= NULL
;
872 prefsPrivate
->rlsContext
.release
= NULL
;
873 prefsPrivate
->rlsContext
.copyDescription
= NULL
;
874 if (context
!= NULL
) {
875 bcopy(context
, &prefsPrivate
->rlsContext
, sizeof(SCPreferencesContext
));
876 if (context
->retain
!= NULL
) {
877 prefsPrivate
->rlsContext
.info
= (void *)(*context
->retain
)(context
->info
);
881 pthread_mutex_unlock(&prefsPrivate
->lock
);
888 __SCPreferencesScheduleWithRunLoop(SCPreferencesRef prefs
,
889 CFRunLoopRef runLoop
,
890 CFStringRef runLoopMode
,
891 dispatch_queue_t queue
)
894 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
896 pthread_mutex_lock(&prefsPrivate
->lock
);
898 if ((prefsPrivate
->dispatchQueue
!= NULL
) || // if we are already scheduled on a dispatch queue
899 ((queue
!= NULL
) && prefsPrivate
->scheduled
)) { // if we are already scheduled on a CFRunLoop
900 _SCErrorSet(kSCStatusInvalidArgument
);
904 if (!prefsPrivate
->scheduled
) {
905 CFMutableArrayRef keys
;
907 if (prefsPrivate
->session
== NULL
) {
908 ok
= __SCPreferencesAddSession(prefs
);
914 CFRetain(prefs
); // hold a reference to the prefs
916 keys
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
917 CFArrayAppendValue(keys
, prefsPrivate
->sessionKeyCommit
);
918 CFArrayAppendValue(keys
, prefsPrivate
->sessionKeyApply
);
919 (void) SCDynamicStoreSetNotificationKeys(prefsPrivate
->session
, keys
, NULL
);
922 if (runLoop
!= NULL
) {
923 prefsPrivate
->rls
= SCDynamicStoreCreateRunLoopSource(NULL
, prefsPrivate
->session
, 0);
924 prefsPrivate
->rlList
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
927 prefsPrivate
->scheduled
= TRUE
;
931 ok
= SCDynamicStoreSetDispatchQueue(prefsPrivate
->session
, queue
);
933 prefsPrivate
->scheduled
= FALSE
;
934 (void) SCDynamicStoreSetNotificationKeys(prefsPrivate
->session
, NULL
, NULL
);
939 prefsPrivate
->dispatchQueue
= queue
;
940 dispatch_retain(prefsPrivate
->dispatchQueue
);
942 if (!_SC_isScheduled(NULL
, runLoop
, runLoopMode
, prefsPrivate
->rlList
)) {
944 * if we do not already have notifications scheduled with
945 * this runLoop / runLoopMode
947 CFRunLoopAddSource(runLoop
, prefsPrivate
->rls
, runLoopMode
);
950 _SC_schedule(prefs
, runLoop
, runLoopMode
, prefsPrivate
->rlList
);
957 pthread_mutex_unlock(&prefsPrivate
->lock
);
963 __SCPreferencesUnscheduleFromRunLoop(SCPreferencesRef prefs
,
964 CFRunLoopRef runLoop
,
965 CFStringRef runLoopMode
)
967 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
971 pthread_mutex_lock(&prefsPrivate
->lock
);
973 if ((runLoop
!= NULL
) && !prefsPrivate
->scheduled
) { // if we should be scheduled (but are not)
974 _SCErrorSet(kSCStatusInvalidArgument
);
978 if (((runLoop
== NULL
) && (prefsPrivate
->dispatchQueue
== NULL
)) || // if we should be scheduled on a dispatch queue (but are not)
979 ((runLoop
!= NULL
) && (prefsPrivate
->dispatchQueue
!= NULL
))) { // if we should be scheduled on a CFRunLoop (but are scheduled on a dispatch queue)
980 _SCErrorSet(kSCStatusInvalidArgument
);
984 if (runLoop
== NULL
) {
985 SCDynamicStoreSetDispatchQueue(prefsPrivate
->session
, NULL
);
986 dispatch_release(prefsPrivate
->dispatchQueue
);
987 prefsPrivate
->dispatchQueue
= NULL
;
989 if (!_SC_unschedule(prefs
, runLoop
, runLoopMode
, prefsPrivate
->rlList
, FALSE
)) {
990 // if not currently scheduled on this runLoop / runLoopMode
991 _SCErrorSet(kSCStatusInvalidArgument
);
995 n
= CFArrayGetCount(prefsPrivate
->rlList
);
996 if (n
== 0 || !_SC_isScheduled(NULL
, runLoop
, runLoopMode
, prefsPrivate
->rlList
)) {
998 * if we are no longer scheduled to receive notifications for
999 * this runLoop / runLoopMode
1001 CFRunLoopRemoveSource(runLoop
, prefsPrivate
->rls
, runLoopMode
);
1004 // if *all* notifications have been unscheduled
1005 CFRelease(prefsPrivate
->rlList
);
1006 prefsPrivate
->rlList
= NULL
;
1007 CFRunLoopSourceInvalidate(prefsPrivate
->rls
);
1008 CFRelease(prefsPrivate
->rls
);
1009 prefsPrivate
->rls
= NULL
;
1015 CFArrayRef changedKeys
;
1017 // if *all* notifications have been unscheduled
1018 prefsPrivate
->scheduled
= FALSE
;
1020 // no need to track changes
1021 (void) SCDynamicStoreSetNotificationKeys(prefsPrivate
->session
, NULL
, NULL
);
1023 // clear out any pending notifications
1024 changedKeys
= SCDynamicStoreCopyNotifiedKeys(prefsPrivate
->session
);
1025 if (changedKeys
!= NULL
) {
1026 CFRelease(changedKeys
);
1029 // release our reference to the prefs
1037 pthread_mutex_unlock(&prefsPrivate
->lock
);
1043 SCPreferencesScheduleWithRunLoop(SCPreferencesRef prefs
,
1044 CFRunLoopRef runLoop
,
1045 CFStringRef runLoopMode
)
1047 if (!isA_SCPreferences(prefs
) || (runLoop
== NULL
) || (runLoopMode
== NULL
)) {
1048 _SCErrorSet(kSCStatusInvalidArgument
);
1052 return __SCPreferencesScheduleWithRunLoop(prefs
, runLoop
, runLoopMode
, NULL
);
1057 SCPreferencesUnscheduleFromRunLoop(SCPreferencesRef prefs
,
1058 CFRunLoopRef runLoop
,
1059 CFStringRef runLoopMode
)
1061 if (!isA_SCPreferences(prefs
) || (runLoop
== NULL
) || (runLoopMode
== NULL
)) {
1062 _SCErrorSet(kSCStatusInvalidArgument
);
1066 return __SCPreferencesUnscheduleFromRunLoop(prefs
, runLoop
, runLoopMode
);
1071 SCPreferencesSetDispatchQueue(SCPreferencesRef prefs
,
1072 dispatch_queue_t queue
)
1076 if (!isA_SCPreferences(prefs
)) {
1077 /* sorry, you must provide a session */
1078 _SCErrorSet(kSCStatusNoPrefsSession
);
1082 if (queue
!= NULL
) {
1083 ok
= __SCPreferencesScheduleWithRunLoop(prefs
, NULL
, NULL
, queue
);
1085 ok
= __SCPreferencesUnscheduleFromRunLoop(prefs
, NULL
, NULL
);
1093 __SCPreferencesSynchronize_helper(SCPreferencesRef prefs
)
1096 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
1097 uint32_t status
= kSCStatusOK
;
1099 if (prefsPrivate
->helper_port
== MACH_PORT_NULL
) {
1104 // have the helper "synchronize" the prefs
1105 ok
= _SCHelperExec(prefsPrivate
->helper_port
,
1106 SCHELPER_MSG_PREFS_SYNCHRONIZE
,
1112 if (prefsPrivate
->helper_port
!= MACH_PORT_NULL
) {
1113 _SCHelperClose(&prefsPrivate
->helper_port
);
1122 SCPreferencesSynchronize(SCPreferencesRef prefs
)
1124 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
1126 if (!isA_SCPreferences(prefs
)) {
1127 /* sorry, you must provide a session */
1128 _SCErrorSet(kSCStatusNoPrefsSession
);
1132 if (prefsPrivate
->authorizationData
!= NULL
) {
1133 __SCPreferencesSynchronize_helper(prefs
);
1135 if (prefsPrivate
->prefs
!= NULL
) {
1136 CFRelease(prefsPrivate
->prefs
);
1137 prefsPrivate
->prefs
= NULL
;
1139 if (prefsPrivate
->signature
!= NULL
) {
1140 CFRelease(prefsPrivate
->signature
);
1141 prefsPrivate
->signature
= NULL
;
1143 prefsPrivate
->accessed
= FALSE
;
1144 prefsPrivate
->changed
= FALSE
;