]> git.saurik.com Git - apple/configd.git/blob - configd.tproj/_configlock.c
configd-84.tar.gz
[apple/configd.git] / configd.tproj / _configlock.c
1 /*
2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25
26 /*
27 * Modification History
28 *
29 * June 1, 2001 Allan Nathanson <ajn@apple.com>
30 * - public API conversion
31 *
32 * March 24, 2000 Allan Nathanson <ajn@apple.com>
33 * - initial revision
34 */
35
36 #include "configd.h"
37 #include "configd_server.h"
38 #include "session.h"
39
40
41 __private_extern__
42 int
43 __SCDynamicStoreLock(SCDynamicStoreRef store, Boolean recursive)
44 {
45 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
46 serverSessionRef mySession;
47
48 SCLog(_configd_verbose, LOG_DEBUG, CFSTR("__SCDynamicStoreLock:"));
49
50 if (!store || (storePrivate->server == MACH_PORT_NULL)) {
51 return kSCStatusNoStoreSession; /* you must have an open session to play */
52 }
53
54 if (storeLocked > 0) {
55 if (storePrivate->locked && recursive) {
56 /* if this session holds the lock and this is a recursive (internal) request */
57 storeLocked++;
58 return kSCStatusOK;
59 }
60 return kSCStatusLocked; /* sorry, someone (you) already have the lock */
61 }
62
63 /* check credentials */
64 mySession = getSession(storePrivate->server);
65 if (mySession->callerEUID != 0) {
66 return kSCStatusAccessError;
67 }
68
69 if (!recursive && _configd_trace) {
70 SCTrace(TRUE, _configd_trace, CFSTR("lock : %5d\n"), storePrivate->server);
71 }
72
73 storeLocked = 1; /* global lock flag */
74 storePrivate->locked = TRUE; /* per-session lock flag */
75
76 /*
77 * defer all (actually, most) changes until the call to __SCDynamicStoreUnlock()
78 */
79 if (storeData_s) {
80 CFRelease(storeData_s);
81 CFRelease(patternData_s);
82 CFRelease(changedKeys_s);
83 CFRelease(deferredRemovals_s);
84 CFRelease(removedSessionKeys_s);
85 }
86 storeData_s = CFDictionaryCreateMutableCopy(NULL, 0, storeData);
87 patternData_s = CFDictionaryCreateMutableCopy(NULL, 0, patternData);
88 changedKeys_s = CFSetCreateMutableCopy(NULL, 0, changedKeys);
89 deferredRemovals_s = CFSetCreateMutableCopy(NULL, 0, deferredRemovals);
90 removedSessionKeys_s = CFSetCreateMutableCopy(NULL, 0, removedSessionKeys);
91
92 /* Add a "locked" mode run loop source for this port */
93 CFRunLoopAddSource(CFRunLoopGetCurrent(), mySession->serverRunLoopSource, CFSTR("locked"));
94
95 return kSCStatusOK;
96 }
97
98
99 __private_extern__
100 kern_return_t
101 _configlock(mach_port_t server, int *sc_status)
102 {
103 serverSessionRef mySession = getSession(server);
104
105 if (_configd_verbose) {
106 SCLog(TRUE, LOG_DEBUG, CFSTR("Lock configuration database."));
107 SCLog(TRUE, LOG_DEBUG, CFSTR(" server = %d"), server);
108 }
109
110 if (!mySession) {
111 *sc_status = kSCStatusNoStoreSession; /* you must have an open session to play */
112 return KERN_SUCCESS;
113 }
114
115 *sc_status = __SCDynamicStoreLock(mySession->store, FALSE);
116 if (*sc_status != kSCStatusOK) {
117 SCLog(_configd_verbose, LOG_DEBUG, CFSTR(" SCDynamicStoreLock(): %s"), SCErrorString(*sc_status));
118 return KERN_SUCCESS;
119 }
120
121 return KERN_SUCCESS;
122 }