]> git.saurik.com Git - apple/configd.git/blob - configd.tproj/_configtouch.c
configd-42.tar.gz
[apple/configd.git] / configd.tproj / _configtouch.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22
23 /*
24 * Modification History
25 *
26 * June 1, 2001 Allan Nathanson <ajn@apple.com>
27 * - public API conversion
28 *
29 * June 20, 2000 Allan Nathanson <ajn@apple.com>
30 * - initial revision
31 */
32
33 #include "configd.h"
34 #include "session.h"
35
36 int
37 __SCDynamicStoreTouchValue(SCDynamicStoreRef store, CFStringRef key)
38 {
39 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
40 int sc_status;
41 Boolean newValue = FALSE;
42 CFPropertyListRef value;
43
44 SCLog(_configd_verbose, LOG_DEBUG, CFSTR("__SCDynamicStoreTouchValue:"));
45 SCLog(_configd_verbose, LOG_DEBUG, CFSTR(" key = %@"), key);
46
47 if (!store || (storePrivate->server == MACH_PORT_NULL)) {
48 return kSCStatusNoStoreSession; /* you must have an open session to play */
49 }
50
51 /*
52 * 1. Ensure that we hold the lock.
53 */
54 sc_status = __SCDynamicStoreLock(store, TRUE);
55 if (sc_status != kSCStatusOK) {
56 return sc_status;
57 }
58
59 /*
60 * 2. Grab the current (or establish a new) store entry for this key.
61 */
62 sc_status = __SCDynamicStoreCopyValue(store, key, &value);
63 switch (sc_status) {
64 case kSCStatusNoKey :
65 /* store entry does not exist, create */
66 value = CFDateCreate(NULL, CFAbsoluteTimeGetCurrent());
67 newValue = TRUE;
68 SCLog(_configd_verbose, LOG_DEBUG, CFSTR(" new time stamp = %@"), value);
69 break;
70
71 case kSCStatusOK :
72 /* store entry exists */
73 if (CFGetTypeID(value) == CFDateGetTypeID()) {
74 /* the value is a CFDate, update the time stamp */
75 CFRelease(value);
76 value = CFDateCreate(NULL, CFAbsoluteTimeGetCurrent());
77 newValue = TRUE;
78 SCLog(_configd_verbose, LOG_DEBUG, CFSTR(" new time stamp = %@"), value);
79 } /* else, we'll just save the data (again) to bump the instance */
80 break;
81
82 default :
83 SCLog(_configd_verbose, LOG_DEBUG, CFSTR(" __SCDynamicStoreCopyValue(): %s"), SCErrorString(sc_status));
84 goto done;
85 }
86
87 sc_status = __SCDynamicStoreSetValue(store, key, value);
88
89 if (newValue) {
90 CFRelease(value);
91 }
92
93 done :
94
95 /*
96 * 8. Release our lock.
97 */
98 __SCDynamicStoreUnlock(store, TRUE);
99
100 return kSCStatusOK;
101 }
102
103
104 kern_return_t
105 _configtouch(mach_port_t server,
106 xmlData_t keyRef, /* raw XML bytes */
107 mach_msg_type_number_t keyLen,
108 int *sc_status
109 )
110 {
111 kern_return_t status;
112 serverSessionRef mySession = getSession(server);
113 CFDataRef xmlKey; /* key (XML serialized) */
114 CFStringRef key; /* key (un-serialized) */
115 CFStringRef xmlError;
116
117 SCLog(_configd_verbose, LOG_DEBUG, CFSTR("Touch key in configuration database."));
118 SCLog(_configd_verbose, LOG_DEBUG, CFSTR(" server = %d"), server);
119
120 /* un-serialize the key */
121 xmlKey = CFDataCreate(NULL, keyRef, keyLen);
122 status = vm_deallocate(mach_task_self(), (vm_address_t)keyRef, keyLen);
123 if (status != KERN_SUCCESS) {
124 SCLog(_configd_verbose, LOG_DEBUG, CFSTR("vm_deallocate(): %s"), mach_error_string(status));
125 /* non-fatal???, proceed */
126 }
127 key = CFPropertyListCreateFromXMLData(NULL,
128 xmlKey,
129 kCFPropertyListImmutable,
130 &xmlError);
131 CFRelease(xmlKey);
132 if (!key) {
133 if (xmlError) {
134 SCLog(_configd_verbose, LOG_DEBUG,
135 CFSTR("CFPropertyListCreateFromXMLData() key: %@"),
136 xmlError);
137 CFRelease(xmlError);
138 }
139 *sc_status = kSCStatusFailed;
140 return KERN_SUCCESS;
141 } else if (!isA_CFString(key)) {
142 *sc_status = kSCStatusInvalidArgument;
143 return KERN_SUCCESS;
144 }
145
146 *sc_status = __SCDynamicStoreTouchValue(mySession->store, key);
147 CFRelease(key);
148
149 return KERN_SUCCESS;
150 }