2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
23 #include <SystemConfiguration/SCD.h>
24 #include "config.h" /* MiG generated file */
25 #include "SCDPrivate.h"
28 static pthread_mutex_t lock
= PTHREAD_MUTEX_INITIALIZER
;
30 static _SCDFlags globalFlags
= {
33 FALSE
/* useSyslog */,
35 TRUE
/* useCFRunLoop */,
38 static boolean_t isSCDServer
= FALSE
;
40 static const struct scd_errmsg
{
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!" }
57 #define nSCD_ERRMSGS (sizeof(scd_errmsgs)/sizeof(struct scd_errmsg))
61 SCDOptionGet(SCDSessionRef session
, const int option
)
63 _SCDFlags
*theFlags
= &globalFlags
;
66 if (session
!= NULL
) {
67 theFlags
= &((SCDSessionPrivateRef
)session
)->flags
;
72 /* session dependent flags */
74 case kSCDOptionDebug
:
75 value
= theFlags
->debug
;
78 case kSCDOptionVerbose
:
79 value
= theFlags
->verbose
;
82 case kSCDOptionIsLocked
:
83 value
= theFlags
->isLocked
;
86 case kSCDOptionUseSyslog
:
87 value
= theFlags
->useSyslog
;
90 case kSCDOptionUseCFRunLoop
:
91 value
= theFlags
->useCFRunLoop
;
94 /* session independent flags */
96 case kSCDOptionIsServer
:
106 SCDOptionSet(SCDSessionRef session
, int option
, const int value
)
108 _SCDFlags
*theFlags
= &globalFlags
;
110 if (session
!= NULL
) {
111 theFlags
= &((SCDSessionPrivateRef
)session
)->flags
;
116 /* session independent flags */
118 case kSCDOptionDebug
:
119 theFlags
->debug
= value
;
122 case kSCDOptionVerbose
:
123 theFlags
->verbose
= value
;
126 case kSCDOptionIsLocked
:
127 theFlags
->isLocked
= value
;
130 case kSCDOptionUseSyslog
:
131 theFlags
->useSyslog
= value
;
134 case kSCDOptionUseCFRunLoop
:
135 theFlags
->useCFRunLoop
= value
;
138 /* session independent flags */
140 case kSCDOptionIsServer
:
150 _SCDLog(SCDSessionRef session
, int level
, CFArrayRef lines
)
152 FILE *f
= (LOG_PRI(level
) > LOG_NOTICE
) ? stderr
: stdout
;
156 if ((LOG_PRI(level
) == LOG_DEBUG
) && !SCDOptionGet(session
, kSCDOptionVerbose
)) {
157 /* it's a debug message and we haven't requested verbose logging */
161 pthread_mutex_lock(&lock
);
163 for (i
=0; i
<CFArrayGetCount(lines
); i
++) {
164 line
= CFStringCreateExternalRepresentation(NULL
,
165 CFArrayGetValueAtIndex(lines
, i
),
166 kCFStringEncodingMacRoman
,
169 if (SCDOptionGet(session
, kSCDOptionUseSyslog
)) {
170 syslog (level
, "%.*s", (int)CFDataGetLength(line
), CFDataGetBytePtr(line
));
172 fprintf(f
, "%.*s\n", (int)CFDataGetLength(line
), CFDataGetBytePtr(line
));
179 pthread_mutex_unlock(&lock
);
184 SCDSessionLog(SCDSessionRef session
, int level
, CFStringRef formatString
, ...)
187 CFStringRef resultString
;
190 va_start(argList
, formatString
);
191 resultString
= CFStringCreateWithFormatAndArguments(NULL
, NULL
, formatString
, argList
);
194 lines
= CFStringCreateArrayBySeparatingStrings(NULL
, resultString
, CFSTR("\n"));
195 _SCDLog(session
, level
, lines
);
197 CFRelease(resultString
);
202 SCDLog(int level
, CFStringRef formatString
, ...)
205 CFStringRef resultString
;
208 va_start(argList
, formatString
);
209 resultString
= CFStringCreateWithFormatAndArguments(NULL
, NULL
, formatString
, argList
);
212 lines
= CFStringCreateArrayBySeparatingStrings(NULL
, resultString
, CFSTR("\n"));
213 _SCDLog(NULL
, level
, lines
);
215 CFRelease(resultString
);
220 SCDError(SCDStatus status
)
224 for (i
= 0; i
< nSCD_ERRMSGS
; i
++) {
225 if (scd_errmsgs
[i
].status
== status
) {
226 return scd_errmsgs
[i
].message
;
229 return "(unknown error)";