2 * Copyright (c) 2000, 2001, 2004-2010, 2013, 2015 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 <SystemConfiguration/SystemConfiguration.h>
35 #include <SystemConfiguration/SCPrivate.h>
36 #include "SCPreferencesInternal.h"
37 #include "SCHelper_client.h"
43 __SCPreferencesUnlock_helper(SCPreferencesRef prefs
)
46 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
47 uint32_t status
= kSCStatusOK
;
49 if (prefsPrivate
->helper_port
== MACH_PORT_NULL
) {
54 // have the helper "unlock" the prefs
55 // status = kSCStatusOK;
56 ok
= _SCHelperExec(prefsPrivate
->helper_port
,
57 SCHELPER_MSG_PREFS_UNLOCK
,
65 if (status
!= kSCStatusOK
) {
69 prefsPrivate
->locked
= FALSE
;
75 if (prefsPrivate
->helper_port
!= MACH_PORT_NULL
) {
76 _SCHelperClose(&prefsPrivate
->helper_port
);
79 status
= kSCStatusAccessError
;
90 reportDelay(SCPreferencesRef prefs
, struct timeval
*delay
)
93 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
96 m
= asl_new(ASL_TYPE_MSG
);
97 asl_set(m
, "com.apple.message.domain", "com.apple.SystemConfiguration.SCPreferencesUnlock");
98 (void) _SC_cfstring_to_cstring(prefsPrivate
->name
, str
, sizeof(str
), kCFStringEncodingUTF8
);
99 asl_set(m
, "com.apple.message.signature", str
);
100 (void) _SC_cfstring_to_cstring(prefsPrivate
->prefsID
, str
, sizeof(str
), kCFStringEncodingUTF8
);
101 asl_set(m
, "com.apple.message.signature2", str
);
102 (void) snprintf(str
, sizeof(str
),
105 delay
->tv_usec
/ 1000);
106 asl_set(m
, "com.apple.message.value", str
);
107 SCLOG(NULL
, m
, ASL_LEVEL_DEBUG
,
108 CFSTR("SCPreferences(%@:%@) lock held for %d.%3.3d seconds"),
110 prefsPrivate
->prefsID
,
112 delay
->tv_usec
/ 1000);
120 SCPreferencesUnlock(SCPreferencesRef prefs
)
122 struct timeval lockElapsed
;
123 struct timeval lockEnd
;
124 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
127 /* sorry, you must provide a session */
128 _SCErrorSet(kSCStatusNoPrefsSession
);
132 if (!prefsPrivate
->locked
) {
133 /* sorry, you don't have the lock */
134 _SCErrorSet(kSCStatusNeedLock
);
138 if (prefsPrivate
->authorizationData
!= NULL
) {
139 return __SCPreferencesUnlock_helper(prefs
);
142 pthread_mutex_lock(&prefsPrivate
->lock
);
144 if (prefsPrivate
->sessionNoO_EXLOCK
!= NULL
) {
145 // Note: closing the session removes the temporary "lock" key
146 CFRelease(prefsPrivate
->sessionNoO_EXLOCK
);
147 prefsPrivate
->sessionNoO_EXLOCK
= NULL
;
150 if (prefsPrivate
->lockFD
!= -1) {
151 if (prefsPrivate
->lockPath
!= NULL
) {
152 unlink(prefsPrivate
->lockPath
);
154 close(prefsPrivate
->lockFD
);
155 prefsPrivate
->lockFD
= -1;
158 (void)gettimeofday(&lockEnd
, NULL
);
159 timersub(&lockEnd
, &prefsPrivate
->lockTime
, &lockElapsed
);
160 if (lockElapsed
.tv_sec
> 0) {
161 // if we held the lock for more than 1 second
162 reportDelay(prefs
, &lockElapsed
);
165 prefsPrivate
->locked
= FALSE
;
167 pthread_mutex_unlock(&prefsPrivate
->lock
);