]> git.saurik.com Git - apple/configd.git/blob - configd.tproj/_configlock.c
02ecc715d21e2005ed50db31b8382be7432b68d5
[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 * 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 * March 24, 2000 Allan Nathanson <ajn@apple.com>
31 * - initial revision
32 */
33
34 #include "configd.h"
35 #include "configd_server.h"
36 #include "session.h"
37
38
39 __private_extern__
40 int
41 __SCDynamicStoreLock(SCDynamicStoreRef store, Boolean recursive)
42 {
43 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
44 serverSessionRef mySession;
45
46 SCLog(_configd_verbose, LOG_DEBUG, CFSTR("__SCDynamicStoreLock:"));
47
48 if (!store || (storePrivate->server == MACH_PORT_NULL)) {
49 return kSCStatusNoStoreSession; /* you must have an open session to play */
50 }
51
52 if (storeLocked > 0) {
53 if (storePrivate->locked && recursive) {
54 /* if this session holds the lock and this is a recursive (internal) request */
55 storeLocked++;
56 return kSCStatusOK;
57 }
58 return kSCStatusLocked; /* sorry, someone (you) already have the lock */
59 }
60
61 /* check credentials */
62 mySession = getSession(storePrivate->server);
63 if (mySession->callerEUID != 0) {
64 return kSCStatusAccessError;
65 }
66
67 if (!recursive && _configd_trace) {
68 SCTrace(TRUE, _configd_trace, CFSTR("lock : %5d\n"), storePrivate->server);
69 }
70
71 storeLocked = 1; /* global lock flag */
72 storePrivate->locked = TRUE; /* per-session lock flag */
73
74 /*
75 * defer all (actually, most) changes until the call to __SCDynamicStoreUnlock()
76 */
77 if (storeData_s) {
78 CFRelease(storeData_s);
79 CFRelease(patternData_s);
80 CFRelease(changedKeys_s);
81 CFRelease(deferredRemovals_s);
82 CFRelease(removedSessionKeys_s);
83 }
84 storeData_s = CFDictionaryCreateMutableCopy(NULL, 0, storeData);
85 patternData_s = CFDictionaryCreateMutableCopy(NULL, 0, patternData);
86 changedKeys_s = CFSetCreateMutableCopy(NULL, 0, changedKeys);
87 deferredRemovals_s = CFSetCreateMutableCopy(NULL, 0, deferredRemovals);
88 removedSessionKeys_s = CFSetCreateMutableCopy(NULL, 0, removedSessionKeys);
89
90 /* Add a "locked" mode run loop source for this port */
91 CFRunLoopAddSource(CFRunLoopGetCurrent(), mySession->serverRunLoopSource, CFSTR("locked"));
92
93 return kSCStatusOK;
94 }
95
96
97 __private_extern__
98 kern_return_t
99 _configlock(mach_port_t server, int *sc_status)
100 {
101 serverSessionRef mySession = getSession(server);
102
103 if (_configd_verbose) {
104 SCLog(TRUE, LOG_DEBUG, CFSTR("Lock configuration database."));
105 SCLog(TRUE, LOG_DEBUG, CFSTR(" server = %d"), server);
106 }
107
108 if (!mySession) {
109 *sc_status = kSCStatusNoStoreSession; /* you must have an open session to play */
110 return KERN_SUCCESS;
111 }
112
113 *sc_status = __SCDynamicStoreLock(mySession->store, FALSE);
114 if (*sc_status != kSCStatusOK) {
115 SCLog(_configd_verbose, LOG_DEBUG, CFSTR(" SCDynamicStoreLock(): %s"), SCErrorString(*sc_status));
116 return KERN_SUCCESS;
117 }
118
119 return KERN_SUCCESS;
120 }