]> git.saurik.com Git - apple/configd.git/blob - SystemConfiguration.fproj/SCPUnlock.c
configd-293.4.tar.gz
[apple/configd.git] / SystemConfiguration.fproj / SCPUnlock.c
1 /*
2 * Copyright (c) 2000, 2001, 2004-2008 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 /*
25 * Modification History
26 *
27 * June 1, 2001 Allan Nathanson <ajn@apple.com>
28 * - public API conversion
29 *
30 * November 9, 2000 Allan Nathanson <ajn@apple.com>
31 * - initial revision
32 */
33
34 #include <SystemConfiguration/SystemConfiguration.h>
35 #include <SystemConfiguration/SCPrivate.h>
36 #include "SCPreferencesInternal.h"
37 #include "SCHelper_client.h"
38
39 #include <unistd.h>
40 #include <pthread.h>
41
42 static Boolean
43 __SCPreferencesUnlock_helper(SCPreferencesRef prefs)
44 {
45 Boolean ok;
46 SCPreferencesPrivateRef prefsPrivate = (SCPreferencesPrivateRef)prefs;
47 uint32_t status = kSCStatusOK;
48
49 if (prefsPrivate->helper == -1) {
50 // if no helper
51 goto fail;
52 }
53
54 // have the helper "unlock" the prefs
55 // status = kSCStatusOK;
56 ok = _SCHelperExec(prefsPrivate->helper,
57 SCHELPER_MSG_PREFS_UNLOCK,
58 NULL,
59 &status,
60 NULL);
61 if (!ok) {
62 goto fail;
63 }
64
65 if (status != kSCStatusOK) {
66 goto error;
67 }
68
69 prefsPrivate->locked = FALSE;
70 return TRUE;
71
72 fail :
73
74 // close helper
75 if (prefsPrivate->helper != -1) {
76 _SCHelperClose(prefsPrivate->helper);
77 prefsPrivate->helper = -1;
78 }
79
80 status = kSCStatusAccessError;
81
82 error :
83
84 // return error
85 _SCErrorSet(status);
86 return FALSE;
87 }
88
89
90 static void
91 reportDelay(SCPreferencesRef prefs, struct timeval *delay)
92 {
93 aslmsg m;
94 SCPreferencesPrivateRef prefsPrivate = (SCPreferencesPrivateRef)prefs;
95 char str[256];
96
97 m = asl_new(ASL_TYPE_MSG);
98 asl_set(m, "com.apple.message.domain", "com.apple.SystemConfiguration.SCPreferencesUnlock");
99 (void) _SC_cfstring_to_cstring(prefsPrivate->name, str, sizeof(str), kCFStringEncodingUTF8);
100 asl_set(m, "com.apple.message.signature", str);
101 (void) _SC_cfstring_to_cstring(prefsPrivate->prefsID, str, sizeof(str), kCFStringEncodingUTF8);
102 asl_set(m, "com.apple.message.signature2", str);
103 (void) snprintf(str, sizeof(str),
104 "%d.%3.3d",
105 (int)delay->tv_sec,
106 delay->tv_usec / 1000);
107 asl_set(m, "com.apple.message.value", str);
108 SCLOG(NULL, m, ASL_LEVEL_DEBUG,
109 CFSTR("SCPreferences(%@:%@) lock held for %d.%3.3d seconds"),
110 prefsPrivate->name,
111 prefsPrivate->prefsID,
112 (int)delay->tv_sec,
113 delay->tv_usec / 1000);
114 asl_free(m);
115
116 return;
117 }
118
119
120 Boolean
121 SCPreferencesUnlock(SCPreferencesRef prefs)
122 {
123 struct timeval lockElapsed;
124 struct timeval lockEnd;
125 SCPreferencesPrivateRef prefsPrivate = (SCPreferencesPrivateRef)prefs;
126
127 if (prefs == NULL) {
128 /* sorry, you must provide a session */
129 _SCErrorSet(kSCStatusNoPrefsSession);
130 return FALSE;
131 }
132
133 if (!prefsPrivate->locked) {
134 /* sorry, you don't have the lock */
135 _SCErrorSet(kSCStatusNeedLock);
136 return FALSE;
137 }
138
139 if (prefsPrivate->authorizationData != NULL) {
140 return __SCPreferencesUnlock_helper(prefs);
141 }
142
143 pthread_mutex_lock(&prefsPrivate->lock);
144
145 if (prefsPrivate->lockFD != -1) {
146 if (prefsPrivate->lockPath != NULL) {
147 unlink(prefsPrivate->lockPath);
148 }
149 close(prefsPrivate->lockFD);
150 prefsPrivate->lockFD = -1;
151 }
152
153 (void)gettimeofday(&lockEnd, NULL);
154 timersub(&lockEnd, &prefsPrivate->lockTime, &lockElapsed);
155 if (lockElapsed.tv_sec > 0) {
156 // if we held the lock for more than 1 second
157 reportDelay(prefs, &lockElapsed);
158 }
159
160 prefsPrivate->locked = FALSE;
161
162 pthread_mutex_unlock(&prefsPrivate->lock);
163 return TRUE;
164 }