2 * Copyright(c) 2000-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 * 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"
52 #include <sys/errno.h>
55 const AuthorizationRef kSCPreferencesUseEntitlementAuthorization
= (AuthorizationRef
)CFSTR("UseEntitlement");
58 static __inline__ CFTypeRef
59 isA_SCPreferences(CFTypeRef obj
)
61 return (isA_CFType(obj
, SCPreferencesGetTypeID()));
66 __SCPreferencesCopyDescription(CFTypeRef cf
) {
67 CFAllocatorRef allocator
= CFGetAllocator(cf
);
68 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)cf
;
69 CFMutableStringRef result
;
71 result
= CFStringCreateMutable(allocator
, 0);
72 CFStringAppendFormat(result
, NULL
, CFSTR("<SCPreferences %p [%p]> {"), cf
, allocator
);
73 CFStringAppendFormat(result
, NULL
, CFSTR("name = %@"), prefsPrivate
->name
);
74 CFStringAppendFormat(result
, NULL
, CFSTR(", id = %@"), prefsPrivate
->prefsID
);
75 CFStringAppendFormat(result
, NULL
, CFSTR(", path = %s"),
76 prefsPrivate
->newPath
? prefsPrivate
->newPath
: prefsPrivate
->path
);
77 if (prefsPrivate
->accessed
) {
78 CFStringAppendFormat(result
, NULL
, CFSTR(", accessed"));
80 if (prefsPrivate
->changed
) {
81 CFStringAppendFormat(result
, NULL
, CFSTR(", changed"));
83 if (prefsPrivate
->locked
) {
84 CFStringAppendFormat(result
, NULL
, CFSTR(", locked"));
86 if (prefsPrivate
->helper_port
!= MACH_PORT_NULL
) {
87 CFStringAppendFormat(result
, NULL
, CFSTR(", helper port = %p"), prefsPrivate
->helper_port
);
89 CFStringAppendFormat(result
, NULL
, CFSTR("}"));
96 __SCPreferencesDeallocate(CFTypeRef cf
)
98 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)cf
;
100 /* release resources */
102 pthread_mutex_destroy(&prefsPrivate
->lock
);
104 if (prefsPrivate
->name
) CFRelease(prefsPrivate
->name
);
105 if (prefsPrivate
->prefsID
) CFRelease(prefsPrivate
->prefsID
);
106 if (prefsPrivate
->options
) CFRelease(prefsPrivate
->options
);
107 if (prefsPrivate
->path
) CFAllocatorDeallocate(NULL
, prefsPrivate
->path
);
108 if (prefsPrivate
->newPath
) CFAllocatorDeallocate(NULL
, prefsPrivate
->newPath
);
109 if (prefsPrivate
->lockFD
!= -1) {
110 if (prefsPrivate
->lockPath
!= NULL
) {
111 unlink(prefsPrivate
->lockPath
);
113 close(prefsPrivate
->lockFD
);
115 if (prefsPrivate
->lockPath
) CFAllocatorDeallocate(NULL
, prefsPrivate
->lockPath
);
116 if (prefsPrivate
->signature
) CFRelease(prefsPrivate
->signature
);
117 if (prefsPrivate
->session
) CFRelease(prefsPrivate
->session
);
118 if (prefsPrivate
->sessionKeyLock
) CFRelease(prefsPrivate
->sessionKeyLock
);
119 if (prefsPrivate
->sessionKeyCommit
) CFRelease(prefsPrivate
->sessionKeyCommit
);
120 if (prefsPrivate
->sessionKeyApply
) CFRelease(prefsPrivate
->sessionKeyApply
);
121 if (prefsPrivate
->rlsContext
.release
!= NULL
) {
122 (*prefsPrivate
->rlsContext
.release
)(prefsPrivate
->rlsContext
.info
);
124 if (prefsPrivate
->prefs
) CFRelease(prefsPrivate
->prefs
);
125 if (prefsPrivate
->authorizationData
!= NULL
) CFRelease(prefsPrivate
->authorizationData
);
126 if (prefsPrivate
->helper_port
!= MACH_PORT_NULL
) {
127 (void) _SCHelperExec(prefsPrivate
->helper_port
,
128 SCHELPER_MSG_PREFS_CLOSE
,
132 _SCHelperClose(&prefsPrivate
->helper_port
);
139 static CFTypeID __kSCPreferencesTypeID
= _kCFRuntimeNotATypeID
;
142 static const CFRuntimeClass __SCPreferencesClass
= {
144 "SCPreferences", // className
147 __SCPreferencesDeallocate
, // dealloc
150 NULL
, // copyFormattingDesc
151 __SCPreferencesCopyDescription
// copyDebugDesc
155 static pthread_once_t initialized
= PTHREAD_ONCE_INIT
;
158 __SCPreferencesInitialize(void) {
159 __kSCPreferencesTypeID
= _CFRuntimeRegisterClass(&__SCPreferencesClass
);
164 static SCPreferencesPrivateRef
165 __SCPreferencesCreatePrivate(CFAllocatorRef allocator
)
167 SCPreferencesPrivateRef prefsPrivate
;
170 /* initialize runtime */
171 pthread_once(&initialized
, __SCPreferencesInitialize
);
173 /* allocate prefs session */
174 size
= sizeof(SCPreferencesPrivate
) - sizeof(CFRuntimeBase
);
175 prefsPrivate
= (SCPreferencesPrivateRef
)_CFRuntimeCreateInstance(allocator
,
176 __kSCPreferencesTypeID
,
179 if (prefsPrivate
== NULL
) {
183 pthread_mutex_init(&prefsPrivate
->lock
, NULL
);
185 prefsPrivate
->name
= NULL
;
186 prefsPrivate
->prefsID
= NULL
;
187 prefsPrivate
->options
= NULL
;
188 prefsPrivate
->path
= NULL
;
189 prefsPrivate
->newPath
= NULL
; // new prefs path
190 prefsPrivate
->locked
= FALSE
;
191 prefsPrivate
->lockFD
= -1;
192 prefsPrivate
->lockPath
= NULL
;
193 prefsPrivate
->signature
= NULL
;
194 prefsPrivate
->session
= NULL
;
195 prefsPrivate
->sessionKeyLock
= NULL
;
196 prefsPrivate
->sessionKeyCommit
= NULL
;
197 prefsPrivate
->sessionKeyApply
= NULL
;
198 prefsPrivate
->scheduled
= FALSE
;
199 prefsPrivate
->rls
= NULL
;
200 prefsPrivate
->rlsFunction
= NULL
;
201 prefsPrivate
->rlsContext
.info
= NULL
;
202 prefsPrivate
->rlsContext
.retain
= NULL
;
203 prefsPrivate
->rlsContext
.release
= NULL
;
204 prefsPrivate
->rlsContext
.copyDescription
= NULL
;
205 prefsPrivate
->rlList
= NULL
;
206 prefsPrivate
->dispatchQueue
= NULL
;
207 prefsPrivate
->prefs
= NULL
;
208 prefsPrivate
->accessed
= FALSE
;
209 prefsPrivate
->changed
= FALSE
;
210 prefsPrivate
->isRoot
= (geteuid() == 0);
211 prefsPrivate
->authorizationData
= NULL
;
212 prefsPrivate
->helper_port
= MACH_PORT_NULL
;
218 __private_extern__ Boolean
219 __SCPreferencesCreate_helper(SCPreferencesRef prefs
)
221 CFDataRef data
= NULL
;
222 CFMutableDictionaryRef info
;
225 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
226 uint32_t status
= kSCStatusOK
;
228 uint32_t pid
= getpid();
231 ok
= _SCHelperOpen(prefsPrivate
->authorizationData
,
232 &prefsPrivate
->helper_port
);
237 // create a dictionary of information to pass to the helper
238 info
= CFDictionaryCreateMutable(NULL
,
240 &kCFTypeDictionaryKeyCallBacks
,
241 &kCFTypeDictionaryValueCallBacks
);
244 if (prefsPrivate
->prefsID
!= NULL
) {
245 CFDictionarySetValue(info
, CFSTR("prefsID"), prefsPrivate
->prefsID
);
249 if (prefsPrivate
->options
!= NULL
) {
250 CFDictionarySetValue(info
, CFSTR("options"), prefsPrivate
->options
);
253 // save preferences session "name"
254 CFDictionarySetValue(info
, CFSTR("name"), prefsPrivate
->name
);
257 num
= CFNumberCreate(NULL
, kCFNumberSInt32Type
, &pid
);
258 CFDictionarySetValue(info
, CFSTR("PID"), num
);
262 str
= CFStringCreateWithCString(NULL
, getprogname(), kCFStringEncodingUTF8
);
263 CFDictionarySetValue(info
, CFSTR("PROC_NAME"), str
);
266 // serialize the info
267 ok
= _SCSerialize(info
, &data
, NULL
, NULL
);
269 if (data
== NULL
|| !ok
) {
273 // have the helper "open" the prefs
274 ok
= _SCHelperExec(prefsPrivate
->helper_port
,
275 SCHELPER_MSG_PREFS_OPEN
,
279 if (data
!= NULL
) CFRelease(data
);
284 if (status
!= kSCStatusOK
) {
293 if (prefsPrivate
->helper_port
!= MACH_PORT_NULL
) {
294 _SCHelperClose(&prefsPrivate
->helper_port
);
297 status
= kSCStatusAccessError
;
308 __SCPreferencesAccess_helper(SCPreferencesRef prefs
)
311 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
312 CFDictionaryRef serverDict
= NULL
;
313 CFDictionaryRef serverPrefs
= NULL
;
314 CFDictionaryRef serverSignature
= NULL
;
315 uint32_t status
= kSCStatusOK
;
316 CFDataRef reply
= NULL
;
318 if (prefsPrivate
->helper_port
== MACH_PORT_NULL
) {
319 ok
= __SCPreferencesCreate_helper(prefs
);
325 // have the helper "access" the prefs
326 ok
= _SCHelperExec(prefsPrivate
->helper_port
,
327 SCHELPER_MSG_PREFS_ACCESS
,
335 if (status
!= kSCStatusOK
) {
343 ok
= _SCUnserialize((CFPropertyListRef
*)&serverDict
, reply
, NULL
, 0);
349 if (isA_CFDictionary(serverDict
)) {
350 serverPrefs
= CFDictionaryGetValue(serverDict
, CFSTR("preferences"));
351 serverPrefs
= isA_CFDictionary(serverPrefs
);
353 serverSignature
= CFDictionaryGetValue(serverDict
, CFSTR("signature"));
354 serverSignature
= isA_CFData(serverSignature
);
357 if ((serverPrefs
== NULL
) || (serverSignature
== NULL
)) {
358 CFRelease(serverDict
);
362 prefsPrivate
->prefs
= CFDictionaryCreateMutableCopy(NULL
, 0, serverPrefs
);
363 prefsPrivate
->signature
= CFRetain(serverSignature
);
364 prefsPrivate
->accessed
= TRUE
;
365 CFRelease(serverDict
);
372 if (prefsPrivate
->helper_port
!= MACH_PORT_NULL
) {
373 _SCHelperClose(&prefsPrivate
->helper_port
);
376 status
= kSCStatusAccessError
;
386 static SCPreferencesPrivateRef
387 __SCPreferencesCreate(CFAllocatorRef allocator
,
390 CFDataRef authorizationData
,
391 CFDictionaryRef options
)
394 SCPreferencesPrivateRef prefsPrivate
;
395 int sc_status
= kSCStatusOK
;
398 * allocate and initialize a new prefs session
400 prefsPrivate
= __SCPreferencesCreatePrivate(allocator
);
401 if (prefsPrivate
== NULL
) {
405 prefsPrivate
->name
= CFStringCreateCopy(allocator
, name
);
406 if (prefsID
!= NULL
) {
407 prefsPrivate
->prefsID
= CFStringCreateCopy(allocator
, prefsID
);
409 if (authorizationData
!= NULL
) {
410 prefsPrivate
->authorizationData
= CFRetain(authorizationData
);
412 if (options
!= NULL
) {
413 prefsPrivate
->options
= CFDictionaryCreateCopy(allocator
, options
);
419 * convert prefsID to path
421 prefsPrivate
->path
= __SCPreferencesPath(allocator
,
423 (prefsPrivate
->newPath
== NULL
));
424 if (prefsPrivate
->path
== NULL
) {
425 sc_status
= kSCStatusFailed
;
432 fd
= open(prefsPrivate
->path
, O_RDONLY
, 0644);
439 if ((prefsID
== NULL
) || !CFStringHasPrefix(prefsID
, CFSTR("/"))) {
440 /* if default preference ID or relative path */
441 if (prefsPrivate
->newPath
== NULL
) {
443 * we've looked in the "new" prefs directory
444 * without success. Save the "new" path and
445 * look in the "old" prefs directory.
447 prefsPrivate
->newPath
= prefsPrivate
->path
;
451 * we've looked in both the "new" and "old"
452 * prefs directories without success. Use
455 CFAllocatorDeallocate(NULL
, prefsPrivate
->path
);
456 prefsPrivate
->path
= prefsPrivate
->newPath
;
457 prefsPrivate
->newPath
= NULL
;
461 /* no preference data, start fresh */
462 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
;
484 _SCErrorSet(sc_status
);
489 if (fd
!= -1) (void) close(fd
);
490 CFRelease(prefsPrivate
);
491 _SCErrorSet(sc_status
);
496 __private_extern__
void
497 __SCPreferencesAccess(SCPreferencesRef prefs
)
499 CFAllocatorRef allocator
= CFGetAllocator(prefs
);
501 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
504 if (prefsPrivate
->accessed
) {
505 // if preference data has already been accessed
509 fd
= open(prefsPrivate
->path
, O_RDONLY
, 0644);
512 if (fstat(fd
, &statBuf
) == -1) {
513 SCLog(TRUE
, LOG_ERR
, CFSTR("__SCPreferencesAccess fstat() failed: %s"), strerror(errno
));
514 bzero(&statBuf
, sizeof(statBuf
));
519 /* no preference data, start fresh */
522 if (prefsPrivate
->authorizationData
!= NULL
) {
523 if (__SCPreferencesAccess_helper(prefs
)) {
527 CFSTR("__SCPreferencesAccess_helper() failed: %s"),
528 SCErrorString(SCError()));
534 SCLog(TRUE
, LOG_ERR
, CFSTR("__SCPreferencesAccess open() failed: %s"), strerror(errno
));
537 bzero(&statBuf
, sizeof(statBuf
));
540 if (prefsPrivate
->signature
!= NULL
) CFRelease(prefsPrivate
->signature
);
541 prefsPrivate
->signature
= __SCPSignatureFromStatbuf(&statBuf
);
543 if (statBuf
.st_size
> 0) {
544 CFDictionaryRef dict
;
545 CFErrorRef error
= NULL
;
546 CFMutableDataRef xmlData
;
549 * extract property list
551 xmlData
= CFDataCreateMutable(allocator
, statBuf
.st_size
);
552 CFDataSetLength(xmlData
, statBuf
.st_size
);
553 if (read(fd
, (void *)CFDataGetBytePtr(xmlData
), statBuf
.st_size
) != statBuf
.st_size
) {
554 /* corrupt prefs file, start fresh */
555 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("__SCPreferencesAccess read(): could not load preference data."));
564 dict
= CFPropertyListCreateWithData(allocator
, xmlData
, kCFPropertyListImmutable
, NULL
, &error
);
567 /* corrupt prefs file, start fresh */
570 CFSTR("__SCPreferencesAccess CFPropertyListCreateWithData(): %@"),
578 * make sure that we've got a dictionary
580 if (!isA_CFDictionary(dict
)) {
581 /* corrupt prefs file, start fresh */
582 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("__SCPreferencesAccess CFGetTypeID(): not a dictionary."));
587 prefsPrivate
->prefs
= CFDictionaryCreateMutableCopy(allocator
, 0, dict
);
597 if (prefsPrivate
->prefs
== NULL
) {
599 * new file, create empty preferences
601 // SCLog(_sc_verbose, LOG_DEBUG, CFSTR("__SCPreferencesAccess(): creating new preferences file."));
602 prefsPrivate
->prefs
= CFDictionaryCreateMutable(allocator
,
604 &kCFTypeDictionaryKeyCallBacks
,
605 &kCFTypeDictionaryValueCallBacks
);
606 prefsPrivate
->changed
= TRUE
;
609 prefsPrivate
->accessed
= TRUE
;
615 SCPreferencesCreate(CFAllocatorRef allocator
,
619 SCPreferencesPrivateRef prefsPrivate
;
621 prefsPrivate
= __SCPreferencesCreate(allocator
, name
, prefsID
, NULL
, NULL
);
622 return (SCPreferencesRef
)prefsPrivate
;
627 SCPreferencesCreateWithAuthorization(CFAllocatorRef allocator
,
630 AuthorizationRef authorization
)
632 SCPreferencesRef prefs
;
634 #if !TARGET_OS_IPHONE
635 if (authorization
== NULL
) {
636 authorization
= kSCPreferencesUseEntitlementAuthorization
;
638 #else // !TARGET_OS_IPHONE
639 authorization
= kSCPreferencesUseEntitlementAuthorization
;
640 #endif // !TARGET_OS_IPHONE
642 prefs
= SCPreferencesCreateWithOptions(allocator
, name
, prefsID
, authorization
, NULL
);
648 SCPreferencesCreateWithOptions(CFAllocatorRef allocator
,
651 AuthorizationRef authorization
,
652 CFDictionaryRef options
)
654 CFDataRef authorizationData
= NULL
;
655 SCPreferencesPrivateRef prefsPrivate
;
657 if (options
!= NULL
) {
658 if (!isA_CFDictionary(options
)) {
659 _SCErrorSet(kSCStatusInvalidArgument
);
664 if (authorization
!= NULL
) {
665 CFMutableDictionaryRef authorizationDict
;
667 CFStringRef bundleID
= NULL
;
669 authorizationDict
= CFDictionaryCreateMutable(NULL
,
671 &kCFTypeDictionaryKeyCallBacks
,
672 &kCFTypeDictionaryValueCallBacks
);
673 #if !TARGET_OS_IPHONE
674 if (authorization
!= kSCPreferencesUseEntitlementAuthorization
) {
675 CFDataRef authorizationRefData
;
676 AuthorizationExternalForm extForm
;
679 os_status
= AuthorizationMakeExternalForm(authorization
, &extForm
);
680 if (os_status
!= errAuthorizationSuccess
) {
681 SCLog(TRUE
, LOG_INFO
, CFSTR("_SCHelperOpen AuthorizationMakeExternalForm() failed"));
682 _SCErrorSet(kSCStatusInvalidArgument
);
683 CFRelease(authorizationDict
);
687 authorizationRefData
= CFDataCreate(NULL
, (const UInt8
*)extForm
.bytes
, sizeof(extForm
.bytes
));
688 CFDictionaryAddValue(authorizationDict
,
689 kSCHelperAuthAuthorization
,
690 authorizationRefData
);
691 CFRelease(authorizationRefData
);
695 /* get the application/executable/bundle name */
696 bundle
= CFBundleGetMainBundle();
697 if (bundle
!= NULL
) {
698 bundleID
= CFBundleGetIdentifier(bundle
);
699 if (bundleID
!= NULL
) {
704 url
= CFBundleCopyExecutableURL(bundle
);
706 bundleID
= CFURLCopyPath(url
);
711 if (bundleID
!= NULL
) {
712 if (CFEqual(bundleID
, CFSTR("/"))) {
718 if (bundleID
== NULL
) {
719 bundleID
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("Unknown(%d)"), getpid());
721 CFDictionaryAddValue(authorizationDict
,
722 kSCHelperAuthCallerInfo
,
726 if (authorizationDict
!= NULL
) {
727 _SCSerialize((CFPropertyListRef
)authorizationDict
,
731 CFRelease(authorizationDict
);
735 prefsPrivate
= __SCPreferencesCreate(allocator
, name
, prefsID
, authorizationData
, options
);
736 if (authorizationData
!= NULL
) CFRelease(authorizationData
);
738 return (SCPreferencesRef
)prefsPrivate
;
743 SCPreferencesGetTypeID(void) {
744 pthread_once(&initialized
, __SCPreferencesInitialize
); /* initialize runtime */
745 return __kSCPreferencesTypeID
;
750 prefsNotify(SCDynamicStoreRef store
, CFArrayRef changedKeys
, void *info
)
753 void (*context_release
)(const void *);
756 SCPreferencesNotification notify
= 0;
757 SCPreferencesRef prefs
= (SCPreferencesRef
)info
;
758 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
759 SCPreferencesCallBack rlsFunction
;
761 n
= (changedKeys
!= NULL
) ? CFArrayGetCount(changedKeys
) : 0;
762 for (i
= 0; i
< n
; i
++) {
765 key
= CFArrayGetValueAtIndex(changedKeys
, i
);
766 if (CFEqual(key
, prefsPrivate
->sessionKeyCommit
)) {
767 // if preferences have been saved
768 notify
|= kSCPreferencesNotificationCommit
;
770 if (CFEqual(key
, prefsPrivate
->sessionKeyApply
)) {
771 // if stored preferences should be applied to current configuration
772 notify
|= kSCPreferencesNotificationApply
;
781 pthread_mutex_lock(&prefsPrivate
->lock
);
784 rlsFunction
= prefsPrivate
->rlsFunction
;
785 if (prefsPrivate
->rlsContext
.retain
!= NULL
) {
786 context_info
= (void *)prefsPrivate
->rlsContext
.retain(prefsPrivate
->rlsContext
.info
);
787 context_release
= prefsPrivate
->rlsContext
.release
;
789 context_info
= prefsPrivate
->rlsContext
.info
;
790 context_release
= NULL
;
793 pthread_mutex_unlock(&prefsPrivate
->lock
);
795 if (rlsFunction
!= NULL
) {
796 (*rlsFunction
)(prefs
, notify
, context_info
);
799 if (context_release
!= NULL
) {
800 (*context_release
)(context_info
);
807 __private_extern__ Boolean
808 __SCPreferencesAddSession(SCPreferencesRef prefs
)
810 CFAllocatorRef allocator
= CFGetAllocator(prefs
);
811 SCDynamicStoreContext context
= { 0
817 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
819 /* establish a dynamic store session */
820 prefsPrivate
->session
= SCDynamicStoreCreate(allocator
,
824 if (prefsPrivate
->session
== NULL
) {
825 SCLog(_sc_verbose
, LOG_INFO
, CFSTR("__SCPreferencesAddSession SCDynamicStoreCreate() failed"));
829 /* create the session "commit" key */
830 prefsPrivate
->sessionKeyCommit
= _SCPNotificationKey(NULL
,
831 prefsPrivate
->prefsID
,
832 kSCPreferencesKeyCommit
);
834 /* create the session "apply" key */
835 prefsPrivate
->sessionKeyApply
= _SCPNotificationKey(NULL
,
836 prefsPrivate
->prefsID
,
837 kSCPreferencesKeyApply
);
844 SCPreferencesSetCallback(SCPreferencesRef prefs
,
845 SCPreferencesCallBack callout
,
846 SCPreferencesContext
*context
)
848 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
850 if (!isA_SCPreferences(prefs
)) {
851 /* sorry, you must provide a session */
852 _SCErrorSet(kSCStatusNoPrefsSession
);
856 pthread_mutex_lock(&prefsPrivate
->lock
);
858 if (prefsPrivate
->rlsContext
.release
!= NULL
) {
859 /* let go of the current context */
860 (*prefsPrivate
->rlsContext
.release
)(prefsPrivate
->rlsContext
.info
);
863 prefsPrivate
->rlsFunction
= callout
;
864 prefsPrivate
->rlsContext
.info
= NULL
;
865 prefsPrivate
->rlsContext
.retain
= NULL
;
866 prefsPrivate
->rlsContext
.release
= NULL
;
867 prefsPrivate
->rlsContext
.copyDescription
= NULL
;
868 if (context
!= NULL
) {
869 bcopy(context
, &prefsPrivate
->rlsContext
, sizeof(SCPreferencesContext
));
870 if (context
->retain
!= NULL
) {
871 prefsPrivate
->rlsContext
.info
= (void *)(*context
->retain
)(context
->info
);
875 pthread_mutex_unlock(&prefsPrivate
->lock
);
882 __SCPreferencesScheduleWithRunLoop(SCPreferencesRef prefs
,
883 CFRunLoopRef runLoop
,
884 CFStringRef runLoopMode
,
885 dispatch_queue_t queue
)
888 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
890 pthread_mutex_lock(&prefsPrivate
->lock
);
892 if ((prefsPrivate
->dispatchQueue
!= NULL
) || // if we are already scheduled on a dispatch queue
893 ((queue
!= NULL
) && prefsPrivate
->scheduled
)) { // if we are already scheduled on a CFRunLoop
894 _SCErrorSet(kSCStatusInvalidArgument
);
898 if (!prefsPrivate
->scheduled
) {
899 CFMutableArrayRef keys
;
901 if (prefsPrivate
->session
== NULL
) {
902 ok
= __SCPreferencesAddSession(prefs
);
908 CFRetain(prefs
); // hold a reference to the prefs
910 keys
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
911 CFArrayAppendValue(keys
, prefsPrivate
->sessionKeyCommit
);
912 CFArrayAppendValue(keys
, prefsPrivate
->sessionKeyApply
);
913 (void) SCDynamicStoreSetNotificationKeys(prefsPrivate
->session
, keys
, NULL
);
916 if (runLoop
!= NULL
) {
917 prefsPrivate
->rls
= SCDynamicStoreCreateRunLoopSource(NULL
, prefsPrivate
->session
, 0);
918 prefsPrivate
->rlList
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
921 prefsPrivate
->scheduled
= TRUE
;
925 ok
= SCDynamicStoreSetDispatchQueue(prefsPrivate
->session
, queue
);
927 prefsPrivate
->scheduled
= FALSE
;
928 (void) SCDynamicStoreSetNotificationKeys(prefsPrivate
->session
, NULL
, NULL
);
933 prefsPrivate
->dispatchQueue
= queue
;
934 dispatch_retain(prefsPrivate
->dispatchQueue
);
936 if (!_SC_isScheduled(NULL
, runLoop
, runLoopMode
, prefsPrivate
->rlList
)) {
938 * if we do not already have notifications scheduled with
939 * this runLoop / runLoopMode
941 CFRunLoopAddSource(runLoop
, prefsPrivate
->rls
, runLoopMode
);
944 _SC_schedule(prefs
, runLoop
, runLoopMode
, prefsPrivate
->rlList
);
951 pthread_mutex_unlock(&prefsPrivate
->lock
);
957 __SCPreferencesUnscheduleFromRunLoop(SCPreferencesRef prefs
,
958 CFRunLoopRef runLoop
,
959 CFStringRef runLoopMode
)
961 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
965 pthread_mutex_lock(&prefsPrivate
->lock
);
967 if ((runLoop
!= NULL
) && !prefsPrivate
->scheduled
) { // if we should be scheduled (but are not)
968 _SCErrorSet(kSCStatusInvalidArgument
);
972 if (((runLoop
== NULL
) && (prefsPrivate
->dispatchQueue
== NULL
)) || // if we should be scheduled on a dispatch queue (but are not)
973 ((runLoop
!= NULL
) && (prefsPrivate
->dispatchQueue
!= NULL
))) { // if we should be scheduled on a CFRunLoop (but are scheduled on a dispatch queue)
974 _SCErrorSet(kSCStatusInvalidArgument
);
978 if (runLoop
== NULL
) {
979 SCDynamicStoreSetDispatchQueue(prefsPrivate
->session
, NULL
);
980 dispatch_release(prefsPrivate
->dispatchQueue
);
981 prefsPrivate
->dispatchQueue
= NULL
;
983 if (!_SC_unschedule(prefs
, runLoop
, runLoopMode
, prefsPrivate
->rlList
, FALSE
)) {
984 // if not currently scheduled on this runLoop / runLoopMode
985 _SCErrorSet(kSCStatusInvalidArgument
);
989 n
= CFArrayGetCount(prefsPrivate
->rlList
);
990 if (n
== 0 || !_SC_isScheduled(NULL
, runLoop
, runLoopMode
, prefsPrivate
->rlList
)) {
992 * if we are no longer scheduled to receive notifications for
993 * this runLoop / runLoopMode
995 CFRunLoopRemoveSource(runLoop
, prefsPrivate
->rls
, runLoopMode
);
998 // if *all* notifications have been unscheduled
999 CFRelease(prefsPrivate
->rlList
);
1000 prefsPrivate
->rlList
= NULL
;
1001 CFRunLoopSourceInvalidate(prefsPrivate
->rls
);
1002 CFRelease(prefsPrivate
->rls
);
1003 prefsPrivate
->rls
= NULL
;
1009 CFArrayRef changedKeys
;
1011 // if *all* notifications have been unscheduled
1012 prefsPrivate
->scheduled
= FALSE
;
1014 // no need to track changes
1015 (void) SCDynamicStoreSetNotificationKeys(prefsPrivate
->session
, NULL
, NULL
);
1017 // clear out any pending notifications
1018 changedKeys
= SCDynamicStoreCopyNotifiedKeys(prefsPrivate
->session
);
1019 if (changedKeys
!= NULL
) {
1020 CFRelease(changedKeys
);
1023 // release our reference to the prefs
1031 pthread_mutex_unlock(&prefsPrivate
->lock
);
1037 SCPreferencesScheduleWithRunLoop(SCPreferencesRef prefs
,
1038 CFRunLoopRef runLoop
,
1039 CFStringRef runLoopMode
)
1041 if (!isA_SCPreferences(prefs
) || (runLoop
== NULL
) || (runLoopMode
== NULL
)) {
1042 _SCErrorSet(kSCStatusInvalidArgument
);
1046 return __SCPreferencesScheduleWithRunLoop(prefs
, runLoop
, runLoopMode
, NULL
);
1051 SCPreferencesUnscheduleFromRunLoop(SCPreferencesRef prefs
,
1052 CFRunLoopRef runLoop
,
1053 CFStringRef runLoopMode
)
1055 if (!isA_SCPreferences(prefs
) || (runLoop
== NULL
) || (runLoopMode
== NULL
)) {
1056 _SCErrorSet(kSCStatusInvalidArgument
);
1060 return __SCPreferencesUnscheduleFromRunLoop(prefs
, runLoop
, runLoopMode
);
1065 SCPreferencesSetDispatchQueue(SCPreferencesRef prefs
,
1066 dispatch_queue_t queue
)
1070 if (!isA_SCPreferences(prefs
)) {
1071 /* sorry, you must provide a session */
1072 _SCErrorSet(kSCStatusNoPrefsSession
);
1076 if (queue
!= NULL
) {
1077 ok
= __SCPreferencesScheduleWithRunLoop(prefs
, NULL
, NULL
, queue
);
1079 ok
= __SCPreferencesUnscheduleFromRunLoop(prefs
, NULL
, NULL
);
1087 __SCPreferencesSynchronize_helper(SCPreferencesRef prefs
)
1090 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
1091 uint32_t status
= kSCStatusOK
;
1093 if (prefsPrivate
->helper_port
== MACH_PORT_NULL
) {
1098 // have the helper "synchronize" the prefs
1099 ok
= _SCHelperExec(prefsPrivate
->helper_port
,
1100 SCHELPER_MSG_PREFS_SYNCHRONIZE
,
1106 if (prefsPrivate
->helper_port
!= MACH_PORT_NULL
) {
1107 _SCHelperClose(&prefsPrivate
->helper_port
);
1116 SCPreferencesSynchronize(SCPreferencesRef prefs
)
1118 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
1120 if (!isA_SCPreferences(prefs
)) {
1121 /* sorry, you must provide a session */
1122 _SCErrorSet(kSCStatusNoPrefsSession
);
1126 if (prefsPrivate
->authorizationData
!= NULL
) {
1127 __SCPreferencesSynchronize_helper(prefs
);
1129 if (prefsPrivate
->prefs
!= NULL
) {
1130 CFRelease(prefsPrivate
->prefs
);
1131 prefsPrivate
->prefs
= NULL
;
1133 if (prefsPrivate
->signature
!= NULL
) {
1134 CFRelease(prefsPrivate
->signature
);
1135 prefsPrivate
->signature
= NULL
;
1137 prefsPrivate
->accessed
= FALSE
;
1138 prefsPrivate
->changed
= FALSE
;