]> git.saurik.com Git - apple/configd.git/blob - SystemConfiguration.fproj/SCD.c
configd-24.1.tar.gz
[apple/configd.git] / SystemConfiguration.fproj / SCD.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 <SystemConfiguration/SCD.h>
24 #include "config.h" /* MiG generated file */
25 #include "SCDPrivate.h"
26
27
28 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
29
30 static _SCDFlags globalFlags = {
31 FALSE /* debug */,
32 FALSE /* verbose */,
33 FALSE /* useSyslog */,
34 FALSE /* isLocked */,
35 TRUE /* useCFRunLoop */,
36 };
37
38 static boolean_t isSCDServer = FALSE;
39
40 static const struct scd_errmsg {
41 SCDStatus status;
42 char *message;
43 } scd_errmsgs[] = {
44 { SCD_OK, "Success!" },
45 { SCD_NOSESSION, "Configuration daemon session not active" },
46 { SCD_NOSERVER, "Configuration daemon not (no longer) available" },
47 { SCD_LOCKED, "Lock already held" },
48 { SCD_NEEDLOCK, "Lock required for this operation" },
49 { SCD_EACCESS, "Permission denied (must be root to obtain lock)" },
50 { SCD_NOKEY, "No such key" },
51 { SCD_EXISTS, "Data associated with key already defined" },
52 { SCD_STALE, "Write attempted on stale version of object" },
53 { SCD_INVALIDARGUMENT, "Invalid argument" },
54 { SCD_NOTIFIERACTIVE, "Notifier is currently active" },
55 { SCD_FAILED, "Failed!" }
56 };
57 #define nSCD_ERRMSGS (sizeof(scd_errmsgs)/sizeof(struct scd_errmsg))
58
59
60 int
61 SCDOptionGet(SCDSessionRef session, const int option)
62 {
63 _SCDFlags *theFlags = &globalFlags;
64 int value = 0;
65
66 if (session != NULL) {
67 theFlags = &((SCDSessionPrivateRef)session)->flags;
68 }
69
70 switch (option) {
71
72 /* session dependent flags */
73
74 case kSCDOptionDebug :
75 value = theFlags->debug;
76 break;
77
78 case kSCDOptionVerbose :
79 value = theFlags->verbose;
80 break;
81
82 case kSCDOptionIsLocked :
83 value = theFlags->isLocked;
84 break;
85
86 case kSCDOptionUseSyslog :
87 value = theFlags->useSyslog;
88 break;
89
90 case kSCDOptionUseCFRunLoop :
91 value = theFlags->useCFRunLoop;
92 break;
93
94 /* session independent flags */
95
96 case kSCDOptionIsServer :
97 value = isSCDServer;
98 break;
99 }
100
101 return value;
102 }
103
104
105 void
106 SCDOptionSet(SCDSessionRef session, int option, const int value)
107 {
108 _SCDFlags *theFlags = &globalFlags;
109
110 if (session != NULL) {
111 theFlags = &((SCDSessionPrivateRef)session)->flags;
112 }
113
114 switch (option) {
115
116 /* session independent flags */
117
118 case kSCDOptionDebug :
119 theFlags->debug = value;
120 break;
121
122 case kSCDOptionVerbose :
123 theFlags->verbose = value;
124 break;
125
126 case kSCDOptionIsLocked :
127 theFlags->isLocked = value;
128 break;
129
130 case kSCDOptionUseSyslog :
131 theFlags->useSyslog = value;
132 break;
133
134 case kSCDOptionUseCFRunLoop :
135 theFlags->useCFRunLoop = value;
136 break;
137
138 /* session independent flags */
139
140 case kSCDOptionIsServer :
141 isSCDServer = value;
142 break;
143 }
144
145 return;
146 }
147
148
149 static void
150 _SCDLog(SCDSessionRef session, int level, CFArrayRef lines)
151 {
152 FILE *f = (LOG_PRI(level) > LOG_NOTICE) ? stderr : stdout;
153 CFDataRef line;
154 int i;
155
156 if ((LOG_PRI(level) == LOG_DEBUG) && !SCDOptionGet(session, kSCDOptionVerbose)) {
157 /* it's a debug message and we haven't requested verbose logging */
158 return;
159 }
160
161 pthread_mutex_lock(&lock);
162
163 for (i=0; i<CFArrayGetCount(lines); i++) {
164 line = CFStringCreateExternalRepresentation(NULL,
165 CFArrayGetValueAtIndex(lines, i),
166 kCFStringEncodingMacRoman,
167 '?');
168 if (line != NULL) {
169 if (SCDOptionGet(session, kSCDOptionUseSyslog)) {
170 syslog (level, "%.*s", (int)CFDataGetLength(line), CFDataGetBytePtr(line));
171 } else {
172 fprintf(f, "%.*s\n", (int)CFDataGetLength(line), CFDataGetBytePtr(line));
173 fflush (f);
174 }
175 CFRelease(line);
176 }
177 }
178
179 pthread_mutex_unlock(&lock);
180 }
181
182
183 void
184 SCDSessionLog(SCDSessionRef session, int level, CFStringRef formatString, ...)
185 {
186 va_list argList;
187 CFStringRef resultString;
188 CFArrayRef lines;
189
190 va_start(argList, formatString);
191 resultString = CFStringCreateWithFormatAndArguments(NULL, NULL, formatString, argList);
192 va_end(argList);
193
194 lines = CFStringCreateArrayBySeparatingStrings(NULL, resultString, CFSTR("\n"));
195 _SCDLog(session, level, lines);
196 CFRelease(lines);
197 CFRelease(resultString);
198 }
199
200
201 void
202 SCDLog(int level, CFStringRef formatString, ...)
203 {
204 va_list argList;
205 CFStringRef resultString;
206 CFArrayRef lines;
207
208 va_start(argList, formatString);
209 resultString = CFStringCreateWithFormatAndArguments(NULL, NULL, formatString, argList);
210 va_end(argList);
211
212 lines = CFStringCreateArrayBySeparatingStrings(NULL, resultString, CFSTR("\n"));
213 _SCDLog(NULL, level, lines);
214 CFRelease(lines);
215 CFRelease(resultString);
216 }
217
218
219 const char *
220 SCDError(SCDStatus status)
221 {
222 int i;
223
224 for (i = 0; i < nSCD_ERRMSGS; i++) {
225 if (scd_errmsgs[i].status == status) {
226 return scd_errmsgs[i].message;
227 }
228 }
229 return "(unknown error)";
230 }