2 * Copyright (c) 2000, 2001, 2004-2010, 2013, 2015, 2016, 2018 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 * November 9, 2000 Allan Nathanson <ajn@apple.com>
34 #include "SCPreferencesInternal.h"
35 #include "SCHelper_client.h"
42 __SCPreferencesUnlock_helper(SCPreferencesRef prefs
)
45 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
46 uint32_t status
= kSCStatusOK
;
48 if (prefsPrivate
->helper_port
== MACH_PORT_NULL
) {
53 // have the helper "unlock" the prefs
54 // status = kSCStatusOK;
55 ok
= _SCHelperExec(prefsPrivate
->helper_port
,
56 SCHELPER_MSG_PREFS_UNLOCK
,
64 if (status
!= kSCStatusOK
) {
68 __SCPreferencesUpdateLockedState(prefs
, FALSE
);
74 if (prefsPrivate
->helper_port
!= MACH_PORT_NULL
) {
75 _SCHelperClose(&prefsPrivate
->helper_port
);
78 status
= kSCStatusAccessError
;
89 reportDelay(SCPreferencesRef prefs
, struct timeval
*delay
)
91 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
94 "SCPreferences(%@:%@) lock held for %d.%3.3d seconds",
96 prefsPrivate
->prefsID
,
98 delay
->tv_usec
/ 1000);
104 SCPreferencesUnlock(SCPreferencesRef prefs
)
106 struct timeval lockElapsed
;
107 struct timeval lockEnd
;
108 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
111 /* sorry, you must provide a session */
112 _SCErrorSet(kSCStatusNoPrefsSession
);
116 if (!prefsPrivate
->locked
) {
117 /* sorry, you don't have the lock */
118 _SCErrorSet(kSCStatusNeedLock
);
122 if (prefsPrivate
->authorizationData
!= NULL
) {
123 return __SCPreferencesUnlock_helper(prefs
);
126 pthread_mutex_lock(&prefsPrivate
->lock
);
128 if (prefsPrivate
->sessionNoO_EXLOCK
!= NULL
) {
129 // Note: closing the session removes the temporary "lock" key
130 CFRelease(prefsPrivate
->sessionNoO_EXLOCK
);
131 prefsPrivate
->sessionNoO_EXLOCK
= NULL
;
134 if (prefsPrivate
->lockFD
!= -1) {
135 if (prefsPrivate
->lockPath
!= NULL
) {
136 unlink(prefsPrivate
->lockPath
);
138 close(prefsPrivate
->lockFD
);
139 prefsPrivate
->lockFD
= -1;
142 (void)gettimeofday(&lockEnd
, NULL
);
143 timersub(&lockEnd
, &prefsPrivate
->lockTime
, &lockElapsed
);
144 if (lockElapsed
.tv_sec
> 0) {
145 // if we held the lock for more than 1 second
146 reportDelay(prefs
, &lockElapsed
);
149 SC_log(LOG_DEBUG
, "SCPreferences() unlock: %s",
150 prefsPrivate
->newPath
? prefsPrivate
->newPath
: prefsPrivate
->path
);
152 __SCPreferencesUpdateLockedState(prefs
, FALSE
);
154 pthread_mutex_unlock(&prefsPrivate
->lock
);