]> git.saurik.com Git - apple/configd.git/blob - SystemConfiguration.fproj/SCDList.c
configd-24.1.tar.gz
[apple/configd.git] / SystemConfiguration.fproj / SCDList.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 #include <sys/types.h>
24 #include <regex.h>
25
26 #include <mach/mach.h>
27 #include <mach/mach_error.h>
28
29 #include <SystemConfiguration/SCD.h>
30 #include "config.h" /* MiG generated file */
31 #include "SCDPrivate.h"
32
33
34 static CFComparisonResult
35 sort_keys(const void *p1, const void *p2, void *context) {
36 CFStringRef key1 = (CFStringRef)p1;
37 CFStringRef key2 = (CFStringRef)p2;
38 return CFStringCompare(key1, key2, 0);
39 }
40
41
42 SCDStatus
43 SCDList(SCDSessionRef session, CFStringRef key, int regexOptions, CFArrayRef *subKeys)
44 {
45 SCDSessionPrivateRef sessionPrivate = (SCDSessionPrivateRef)session;
46 kern_return_t status;
47 CFDataRef xmlKey; /* serialized key */
48 xmlData_t myKeyRef;
49 CFIndex myKeyLen;
50 CFDataRef xmlData; /* data (XML serialized) */
51 xmlDataOut_t xmlDataRef; /* serialized data */
52 int xmlDataLen;
53 SCDStatus scd_status;
54 CFArrayRef allKeys;
55 CFMutableArrayRef sortedKeys;
56 CFStringRef xmlError;
57
58 SCDLog(LOG_DEBUG, CFSTR("SCDList:"));
59 SCDLog(LOG_DEBUG, CFSTR(" key = %@"), key);
60 SCDLog(LOG_DEBUG, CFSTR(" regexOptions = %0o"), regexOptions);
61
62 if (key == NULL) {
63 return SCD_INVALIDARGUMENT; /* no key specified */
64 }
65
66 if ((session == NULL) || (sessionPrivate->server == MACH_PORT_NULL)) {
67 return SCD_NOSESSION;
68 }
69
70 /* serialize the key */
71 xmlKey = CFPropertyListCreateXMLData(NULL, key);
72 myKeyRef = (xmlData_t)CFDataGetBytePtr(xmlKey);
73 myKeyLen = CFDataGetLength(xmlKey);
74
75 /* send the key & fetch the associated data from the server */
76 status = configlist(sessionPrivate->server,
77 myKeyRef,
78 myKeyLen,
79 regexOptions,
80 &xmlDataRef,
81 &xmlDataLen,
82 (int *)&scd_status);
83
84 /* clean up */
85 CFRelease(xmlKey);
86
87 if (status != KERN_SUCCESS) {
88 if (status != MACH_SEND_INVALID_DEST)
89 SCDLog(LOG_DEBUG, CFSTR("configlist(): %s"), mach_error_string(status));
90 (void) mach_port_destroy(mach_task_self(), sessionPrivate->server);
91 sessionPrivate->server = MACH_PORT_NULL;
92 return SCD_NOSERVER;
93 }
94
95 if (scd_status != SCD_OK) {
96 status = vm_deallocate(mach_task_self(), (vm_address_t)xmlDataRef, xmlDataLen);
97 if (status != KERN_SUCCESS) {
98 SCDLog(LOG_DEBUG, CFSTR("vm_deallocate(): %s"), mach_error_string(status));
99 /* non-fatal???, proceed */
100 }
101 *subKeys = NULL;
102 return scd_status;
103 }
104
105 /* un-serialize the list of keys */
106 xmlData = CFDataCreate(NULL, xmlDataRef, xmlDataLen);
107 status = vm_deallocate(mach_task_self(), (vm_address_t)xmlDataRef, xmlDataLen);
108 if (status != KERN_SUCCESS) {
109 SCDLog(LOG_DEBUG, CFSTR("vm_deallocate(): %s"), mach_error_string(status));
110 /* non-fatal???, proceed */
111 }
112 allKeys = CFPropertyListCreateFromXMLData(NULL,
113 xmlData,
114 kCFPropertyListImmutable,
115 &xmlError);
116 CFRelease(xmlData);
117 if (xmlError) {
118 SCDLog(LOG_DEBUG, CFSTR("CFPropertyListCreateFromXMLData() list: %s"), xmlError);
119 return SCD_FAILED;
120 }
121
122 myKeyLen = CFArrayGetCount(allKeys);
123 sortedKeys = CFArrayCreateMutableCopy(NULL, myKeyLen, allKeys);
124 CFRelease(allKeys);
125 CFArraySortValues(sortedKeys,
126 CFRangeMake(0, myKeyLen),
127 sort_keys,
128 NULL);
129
130 *subKeys = sortedKeys;
131 return scd_status;
132 }