]> git.saurik.com Git - apple/configd.git/blobdiff - configd.tproj/_configget.c
configd-293.8.tar.gz
[apple/configd.git] / configd.tproj / _configget.c
index c51edd33d1b40d545477b97df72c36ef9bbe09d5..13bc53f6402389d9bdbc2b92a1fae6c4240a64b8 100644 (file)
@@ -1,22 +1,23 @@
 /*
- * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2000-2004, 2006, 2008 Apple Inc. All rights reserved.
  *
  * @APPLE_LICENSE_HEADER_START@
- *
- * The contents of this file constitute Original Code as defined in and
- * are subject to the Apple Public Source License Version 1.1 (the
- * "License").  You may not use this file except in compliance with the
- * License.  Please obtain a copy of the License at
- * http://www.apple.com/publicsource and read it before using this file.
- *
- * This Original Code and all software distributed under the License are
- * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
+ * 
+ * This file contains Original Code and/or Modifications of Original Code
+ * as defined in and that are subject to the Apple Public Source License
+ * Version 2.0 (the 'License'). You may not use this file except in
+ * compliance with the License. Please obtain a copy of the License at
+ * http://www.opensource.apple.com/apsl/ and read it before using this
+ * file.
+ * 
+ * The Original Code and all software distributed under the License are
+ * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
- * License for the specific language governing rights and limitations
- * under the License.
- *
+ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
+ * Please see the License for the specific language governing rights and
+ * limitations under the License.
+ * 
  * @APPLE_LICENSE_HEADER_END@
  */
 
 #include "configd.h"
 #include "session.h"
 
+__private_extern__
 int
-__SCDynamicStoreCopyValue(SCDynamicStoreRef store, CFStringRef key, CFPropertyListRef *value)
+__SCDynamicStoreCopyValue(SCDynamicStoreRef store, CFStringRef key, CFDataRef *value, Boolean internal)
 {
        SCDynamicStorePrivateRef        storePrivate = (SCDynamicStorePrivateRef)store;
        CFDictionaryRef                 dict;
 
-       SCLog(_configd_verbose, LOG_DEBUG, CFSTR("__SCDynamicStoreCopyValue:"));
-       SCLog(_configd_verbose, LOG_DEBUG, CFSTR("  key      = %@"), key);
-
-       if (!store || (storePrivate->server == MACH_PORT_NULL)) {
+       if ((store == NULL) || (storePrivate->server == MACH_PORT_NULL)) {
                return kSCStatusNoStoreSession; /* you must have an open session to play */
        }
 
+       if (_configd_trace) {
+               SCTrace(TRUE, _configd_trace,
+                       CFSTR("%s : %5d : %@\n"),
+                       internal ? "*copy  " : "copy   ",
+                       storePrivate->server,
+                       key);
+       }
+
        dict = CFDictionaryGetValue(storeData, key);
        if ((dict == NULL) || (CFDictionaryContainsKey(dict, kSCDData) == FALSE)) {
                /* key doesn't exist (or data never defined) */
@@ -55,11 +62,10 @@ __SCDynamicStoreCopyValue(SCDynamicStoreRef store, CFStringRef key, CFPropertyLi
        /* Return the data associated with the key */
        *value = CFRetain(CFDictionaryGetValue(dict, kSCDData));
 
-       SCLog(_configd_verbose, LOG_DEBUG, CFSTR("  value    = %@"), *value);
-
        return kSCStatusOK;
 }
 
+__private_extern__
 kern_return_t
 _configget(mach_port_t                 server,
           xmlData_t                    keyRef,         /* raw XML bytes */
@@ -70,41 +76,42 @@ _configget(mach_port_t                      server,
           int                          *sc_status
 )
 {
-       CFStringRef             key;            /* key  (un-serialized) */
-       serverSessionRef        mySession = getSession(server);
+       CFStringRef             key             = NULL;         /* key  (un-serialized) */
+       serverSessionRef        mySession;
        Boolean                 ok;
-       CFPropertyListRef       value;
-
-       SCLog(_configd_verbose, LOG_DEBUG, CFSTR("Get key from configuration database."));
-       SCLog(_configd_verbose, LOG_DEBUG, CFSTR("  server = %d"), server);
+       CFDataRef               value;
 
        *dataRef = NULL;
        *dataLen = 0;
 
        /* un-serialize the key */
-       if (!_SCUnserialize((CFPropertyListRef *)&key, (void *)keyRef, keyLen)) {
+       if (!_SCUnserializeString(&key, NULL, (void *)keyRef, keyLen)) {
                *sc_status = kSCStatusFailed;
-               return KERN_SUCCESS;
+               goto done;
        }
 
        if (!isA_CFString(key)) {
-               CFRelease(key);
                *sc_status = kSCStatusInvalidArgument;
-               return KERN_SUCCESS;
+               goto done;
+       }
+
+       mySession = getSession(server);
+       if (mySession == NULL) {
+               *sc_status = kSCStatusNoStoreSession;   /* you must have an open session to play */
+               goto done;
        }
 
-       *sc_status = __SCDynamicStoreCopyValue(mySession->store, key, &value);
-       CFRelease(key);
+       *sc_status = __SCDynamicStoreCopyValue(mySession->store, key, &value, FALSE);
        if (*sc_status != kSCStatusOK) {
-               return KERN_SUCCESS;
+               goto done;
        }
 
        /* serialize the data */
-       ok = _SCSerialize(value, NULL, (void **)dataRef, (CFIndex *)dataLen);
+       ok = _SCSerializeData(value, (void **)dataRef, (CFIndex *)dataLen);
        CFRelease(value);
        if (!ok) {
                *sc_status = kSCStatusFailed;
-               return KERN_SUCCESS;
+               goto done;
        }
 
        /*
@@ -112,6 +119,9 @@ _configget(mach_port_t                      server,
         */
        *newInstance = 1;
 
+    done :
+
+       if (key != NULL)        CFRelease(key);
        return KERN_SUCCESS;
 }
 
@@ -126,16 +136,16 @@ typedef struct {
 static void
 addSpecificKey(const void *value, void *context)
 {
+       CFDataRef               data;
        CFStringRef             key             = (CFStringRef)value;
        addSpecificRef          myContextRef    = (addSpecificRef)context;
        int                     sc_status;
-       CFPropertyListRef       data;
 
        if (!isA_CFString(key)) {
                return;
        }
 
-       sc_status = __SCDynamicStoreCopyValue(myContextRef->store, key, &data);
+       sc_status = __SCDynamicStoreCopyValue(myContextRef->store, key, &data, TRUE);
        if (sc_status == kSCStatusOK) {
                CFDictionaryAddValue(myContextRef->dict, key, data);
                CFRelease(data);
@@ -168,6 +178,52 @@ addSpecificPattern(const void *value, void *context)
        return;
 }
 
+__private_extern__
+int
+__SCDynamicStoreCopyMultiple(SCDynamicStoreRef store, CFArrayRef keys, CFArrayRef patterns, CFDictionaryRef *values)
+{
+       SCDynamicStorePrivateRef        storePrivate = (SCDynamicStorePrivateRef)store;
+       addSpecific                     myContext;
+
+       if ((store == NULL) || (storePrivate->server == MACH_PORT_NULL)) {
+               return kSCStatusNoStoreSession; /* you must have an open session to play */
+       }
+
+       if (_configd_trace) {
+               SCTrace(TRUE, _configd_trace,
+                       CFSTR("copy m  : %5d : %d keys, %d patterns\n"),
+                       storePrivate->server,
+                       keys     ? CFArrayGetCount(keys)     : 0,
+                       patterns ? CFArrayGetCount(patterns) : 0);
+       }
+
+       myContext.store = store;
+       myContext.dict  = CFDictionaryCreateMutable(NULL,
+                                                   0,
+                                                   &kCFTypeDictionaryKeyCallBacks,
+                                                   &kCFTypeDictionaryValueCallBacks);
+
+       if (keys) {
+               CFArrayApplyFunction(keys,
+                                    CFRangeMake(0, CFArrayGetCount(keys)),
+                                    addSpecificKey,
+                                    &myContext);
+       }
+
+       if (patterns) {
+               CFArrayApplyFunction(patterns,
+                                    CFRangeMake(0, CFArrayGetCount(patterns)),
+                                    addSpecificPattern,
+                                    &myContext);
+       }
+
+       /* Return the keys/values associated with the key */
+       *values = myContext.dict;
+
+       return kSCStatusOK;
+}
+
+__private_extern__
 kern_return_t
 _configget_m(mach_port_t               server,
             xmlData_t                  keysRef,
@@ -178,76 +234,64 @@ _configget_m(mach_port_t          server,
             mach_msg_type_number_t     *dataLen,
             int                        *sc_status)
 {
+       CFDictionaryRef         dict            = NULL; /* keys/values (un-serialized) */
        CFArrayRef              keys            = NULL; /* keys (un-serialized) */
-       addSpecific             myContext;
-       serverSessionRef        mySession       = getSession(server);
+       serverSessionRef        mySession;
        Boolean                 ok;
        CFArrayRef              patterns        = NULL; /* patterns (un-serialized) */
 
-       SCLog(_configd_verbose, LOG_DEBUG, CFSTR("Get key from configuration database."));
-       SCLog(_configd_verbose, LOG_DEBUG, CFSTR("  server = %d"), server);
-
        *dataRef = NULL;
        *dataLen = 0;
+
        *sc_status = kSCStatusOK;
 
        if (keysRef && (keysLen > 0)) {
                /* un-serialize the keys */
-               if (!_SCUnserialize((CFPropertyListRef *)&keys, (void *)keysRef, keysLen)) {
+               if (!_SCUnserialize((CFPropertyListRef *)&keys, NULL, (void *)keysRef, keysLen)) {
                        *sc_status = kSCStatusFailed;
                }
-
-               if (!isA_CFArray(keys)) {
-                       *sc_status = kSCStatusInvalidArgument;
-               }
        }
 
        if (patternsRef && (patternsLen > 0)) {
                /* un-serialize the patterns */
-               if (!_SCUnserialize((CFPropertyListRef *)&patterns, (void *)patternsRef, patternsLen)) {
+               if (!_SCUnserialize((CFPropertyListRef *)&patterns, NULL, (void *)patternsRef, patternsLen)) {
                        *sc_status = kSCStatusFailed;
                }
-
-               if (!isA_CFArray(patterns)) {
-                       *sc_status = kSCStatusInvalidArgument;
-               }
        }
 
        if (*sc_status != kSCStatusOK) {
-               if (keys)       CFRelease(keys);
-               if (patterns)   CFRelease(patterns);
-               return KERN_SUCCESS;
+               goto done;
        }
 
-       myContext.store = mySession->store;
-       myContext.dict  = CFDictionaryCreateMutable(NULL,
-                                                   0,
-                                                   &kCFTypeDictionaryKeyCallBacks,
-                                                   &kCFTypeDictionaryValueCallBacks);
+       if ((keys != NULL) && !isA_CFArray(keys)) {
+               *sc_status = kSCStatusInvalidArgument;
+               goto done;
+       }
 
-       if (keys) {
-               CFArrayApplyFunction(keys,
-                                    CFRangeMake(0, CFArrayGetCount(keys)),
-                                    addSpecificKey,
-                                    &myContext);
-               CFRelease(keys);
+       if ((patterns != NULL) && !isA_CFArray(patterns)) {
+               *sc_status = kSCStatusInvalidArgument;
+               goto done;
        }
 
-       if (patterns) {
-               CFArrayApplyFunction(patterns,
-                                    CFRangeMake(0, CFArrayGetCount(patterns)),
-                                    addSpecificPattern,
-                                    &myContext);
-               CFRelease(patterns);
+       mySession = getSession(server);
+       if (mySession == NULL) {
+               *sc_status = kSCStatusNoStoreSession;   /* you must have an open session to play */
+               goto done;
        }
 
+       /* fetch the requested information */
+       *sc_status = __SCDynamicStoreCopyMultiple(mySession->store, keys, patterns, &dict);
+
        /* serialize the dictionary of matching keys/patterns */
-       ok = _SCSerialize(myContext.dict, NULL, (void **)dataRef, (CFIndex *)dataLen);
-       CFRelease(myContext.dict);
+       ok = _SCSerialize(dict, NULL, (void **)dataRef, (CFIndex *)dataLen);
+       CFRelease(dict);
        if (!ok) {
                *sc_status = kSCStatusFailed;
-               return KERN_SUCCESS;
        }
 
+    done :
+
+       if (keys)       CFRelease(keys);
+       if (patterns)   CFRelease(patterns);
        return KERN_SUCCESS;
 }