]> git.saurik.com Git - apple/configd.git/blame - configd.tproj/_configlock.c
configd-293.8.tar.gz
[apple/configd.git] / configd.tproj / _configlock.c
CommitLineData
5958d7c0 1/*
a40a14f8 2 * Copyright (c) 2000, 2001, 2003, 2004, 2006, 2009 Apple Inc. All rights reserved.
5958d7c0
A
3 *
4 * @APPLE_LICENSE_HEADER_START@
009ee3c6 5 *
009ee3c6
A
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
5958d7c0
A
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
009ee3c6
A
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 *
5958d7c0
A
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
0fae82ee
A
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
5958d7c0
A
34#include "configd.h"
35#include "configd_server.h"
36#include "session.h"
37
38
009ee3c6 39__private_extern__
0fae82ee
A
40int
41__SCDynamicStoreLock(SCDynamicStoreRef store, Boolean recursive)
5958d7c0 42{
0fae82ee 43 serverSessionRef mySession;
dbf6a266 44 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
5958d7c0 45
edebe297 46 if ((store == NULL) || (storePrivate->server == MACH_PORT_NULL)) {
0fae82ee 47 return kSCStatusNoStoreSession; /* you must have an open session to play */
5958d7c0
A
48 }
49
0fae82ee
A
50 if (storeLocked > 0) {
51 if (storePrivate->locked && recursive) {
52 /* if this session holds the lock and this is a recursive (internal) request */
53 storeLocked++;
54 return kSCStatusOK;
55 }
56 return kSCStatusLocked; /* sorry, someone (you) already have the lock */
5958d7c0
A
57 }
58
59 /* check credentials */
0fae82ee 60 mySession = getSession(storePrivate->server);
a40a14f8 61 if (!hasWriteAccess(mySession)) {
0fae82ee 62 return kSCStatusAccessError;
5958d7c0
A
63 }
64
009ee3c6
A
65 if (!recursive && _configd_trace) {
66 SCTrace(TRUE, _configd_trace, CFSTR("lock : %5d\n"), storePrivate->server);
67 }
68
0fae82ee
A
69 storeLocked = 1; /* global lock flag */
70 storePrivate->locked = TRUE; /* per-session lock flag */
5958d7c0
A
71
72 /*
0fae82ee 73 * defer all (actually, most) changes until the call to __SCDynamicStoreUnlock()
5958d7c0 74 */
0fae82ee
A
75 if (storeData_s) {
76 CFRelease(storeData_s);
009ee3c6 77 CFRelease(patternData_s);
5958d7c0
A
78 CFRelease(changedKeys_s);
79 CFRelease(deferredRemovals_s);
80 CFRelease(removedSessionKeys_s);
81 }
0fae82ee 82 storeData_s = CFDictionaryCreateMutableCopy(NULL, 0, storeData);
009ee3c6 83 patternData_s = CFDictionaryCreateMutableCopy(NULL, 0, patternData);
5958d7c0
A
84 changedKeys_s = CFSetCreateMutableCopy(NULL, 0, changedKeys);
85 deferredRemovals_s = CFSetCreateMutableCopy(NULL, 0, deferredRemovals);
86 removedSessionKeys_s = CFSetCreateMutableCopy(NULL, 0, removedSessionKeys);
87
88 /* Add a "locked" mode run loop source for this port */
89 CFRunLoopAddSource(CFRunLoopGetCurrent(), mySession->serverRunLoopSource, CFSTR("locked"));
90
0fae82ee 91 return kSCStatusOK;
5958d7c0
A
92}
93
94
009ee3c6 95__private_extern__
5958d7c0 96kern_return_t
0fae82ee 97_configlock(mach_port_t server, int *sc_status)
5958d7c0
A
98{
99 serverSessionRef mySession = getSession(server);
100
edebe297 101 if (mySession == NULL) {
009ee3c6
A
102 *sc_status = kSCStatusNoStoreSession; /* you must have an open session to play */
103 return KERN_SUCCESS;
104 }
5958d7c0 105
0fae82ee
A
106 *sc_status = __SCDynamicStoreLock(mySession->store, FALSE);
107 if (*sc_status != kSCStatusOK) {
5958d7c0
A
108 return KERN_SUCCESS;
109 }
110
111 return KERN_SUCCESS;
112}