]> git.saurik.com Git - apple/configd.git/blob - SystemConfiguration.fproj/SCPUnlock.c
configd-204.tar.gz
[apple/configd.git] / SystemConfiguration.fproj / SCPUnlock.c
1 /*
2 * Copyright (c) 2000, 2001, 2004-2007 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 Boolean
91 SCPreferencesUnlock(SCPreferencesRef prefs)
92 {
93 SCPreferencesPrivateRef prefsPrivate = (SCPreferencesPrivateRef)prefs;
94
95 if (prefs == NULL) {
96 /* sorry, you must provide a session */
97 _SCErrorSet(kSCStatusNoPrefsSession);
98 return FALSE;
99 }
100
101 if (!prefsPrivate->locked) {
102 /* sorry, you don't have the lock */
103 _SCErrorSet(kSCStatusNeedLock);
104 return FALSE;
105 }
106
107 if (prefsPrivate->authorizationData != NULL) {
108 return __SCPreferencesUnlock_helper(prefs);
109 }
110
111 pthread_mutex_lock(&prefsPrivate->lock);
112
113 if (prefsPrivate->lockFD != -1) {
114 if (prefsPrivate->lockPath != NULL) {
115 unlink(prefsPrivate->lockPath);
116 }
117 close(prefsPrivate->lockFD);
118 prefsPrivate->lockFD = -1;
119 }
120
121 prefsPrivate->locked = FALSE;
122
123 pthread_mutex_unlock(&prefsPrivate->lock);
124 return TRUE;
125 }