1 /* $Id: plog.c,v 1.6.10.1 2005/12/07 10:19:51 vanhu Exp $ */
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/types.h>
35 #include <sys/param.h>
47 #if TIME_WITH_SYS_TIME
48 # include <sys/time.h>
52 # include <sys/time.h>
63 #include <asl_private.h>
70 #include "preferences.h"
73 # define VA_COPY(dst,src) memcpy(&(dst), (src), sizeof(va_list))
76 const char *plog_facility
= "com.apple.racoon";
77 const char *plog_session_id
= "com.apple.racoon.sessionid";
78 const char *plog_session_type
= "com.apple.racoon.sessiontype";
79 const char *plog_session_ver
= "com.apple.racoon.sessionversion";
84 u_int32_t loglevel
= ASL_LEVEL_NOTICE
;
85 //u_int32_t loglevel = ASL_LEVEL_DEBUG;
88 int print_location
= 0;
92 char logFileStr
[MAXPATHLEN
+1];
94 char *gSessType
= NULL
;
95 char *gSessVer
= NULL
;
96 aslclient logRef
= NULL
;
99 plogdump_asl (aslmsg msg
, int pri
, const char *fmt
, ...)
108 level
= ASL_STRING_INFO
;
111 case ASL_LEVEL_NOTICE
:
112 level
= ASL_STRING_NOTICE
;
115 case ASL_LEVEL_WARNING
:
116 level
= ASL_STRING_WARNING
;
120 level
= ASL_STRING_ERR
;
123 case ASL_LEVEL_DEBUG
:
124 level
= ASL_STRING_DEBUG
;
131 asl_set(msg
, ASL_KEY_LEVEL
, level
);
133 buf
= racoon_malloc(buflen
);
137 vsnprintf(buf
, buflen
, fmt
, args
);
138 // asl_set(msg, ASL_KEY_MESSAGE, buf);
145 plogdump_func(int pri
, void *data
, size_t len
, const char *fmt
, ...)
154 * 2 words a bytes + 1 space 4 bytes + 1 newline 32 bytes
157 buflen
= (len
* 2) + (len
/ 4) + (len
/ 32) + 3;
158 buf
= racoon_malloc(buflen
);
168 snprintf(&buf
[i
], buflen
- i
, "%02x",
169 ((unsigned char *)data
)[j
] & 0xff);
173 if (buflen
- i
>= 2) {
180 vsnprintf(fmt_buf
, sizeof(fmt_buf
), fmt
, args
);
183 plog(pri
, "%s %s", fmt_buf
, buf
);
189 clog_func (clog_err_t
*cerr
, clog_err_op_t cerr_op
, int pri
, const char *function
, const char *line
, const char *fmt
, ...)
198 if (!(new = racoon_calloc(1, sizeof(*cerr
)))) {
202 cerr
->clog_err_level
= pri
; /* will be used for filtering */
204 //cerr->clog_err_code;
208 cerr
->description_len
= vasprintf(&cerr
->description
, fmt
, args
);
210 cerr
->function
= function
;
213 // add new to the tail
214 TAILQ_FOREACH(p
, &cerr
->chain_head
, chain
) {
215 if (TAILQ_NEXT(p
, chain
) == NULL
) {
216 TAILQ_NEXT(p
, chain
) = new;
217 new->chain
.tqe_prev
= &TAILQ_NEXT(p
, chain
);
222 if (cerr_op
== CLOG_ERR_DUMP
) {
223 char *prev
= NULL
, *backtrace
= NULL
;
225 TAILQ_FOREACH(p
, &cerr
->chain_head
, chain
) {
226 // collapse list into backtrace
227 if (cerr
->description
) {
231 asprintf(&backtrace
, "%s\n\t\t-> %s", prev
, cerr
->description
);
234 asprintf(&backtrace
, "%s", cerr
->description
);
240 // use plog to dump event.
241 plog(pri
, "%s", backtrace
);
250 syslog(LOG_NOTICE
, "%s: about to add racoon log file: %s\n", __FUNCTION__
, file
? file
:"bad file path");
251 if (logfile
!= NULL
) {
252 racoon_free(logfile
);
253 if (logfile_fd
!= -1) {
254 asl_remove_log_file(logRef
, logfile_fd
);
255 asl_close_auxiliary_file(logfile_fd
);
259 logfile
= racoon_strdup(file
);
260 STRDUP_FATAL(logfile
);
261 if ((logfile_fd
= open(logfile
, O_CREAT
| O_WRONLY
| O_APPEND
| O_NOFOLLOW
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
)) >= 0) {
262 asl_add_log_file(logRef
, logfile_fd
);
264 syslog(LOG_NOTICE
, "%s: failed to add racoon log file: %s. error %d\n", __FUNCTION__
, file
? file
:"bad file path", errno
);
272 /* if log paths equal - do nothing */
273 if (logfile
== NULL
&& file
== NULL
) {
276 if (logfile
!= NULL
&& file
!= NULL
) {
277 if (!strcmp(logfile
, file
)) {
280 if (logfile_fd
!= -1) {
281 asl_remove_log_file(logRef
, logfile_fd
);
288 racoon_free(logfile
);
303 plogsetlevel(int level
)
307 if (level
&& level
>= ASL_LEVEL_EMERG
&& level
<= ASL_LEVEL_DEBUG
) {
310 if (loglevel
>= ASL_LEVEL_INFO
) {
311 mask
= ASL_FILTER_MASK_TUNNEL
;
315 mask
|= ASL_FILTER_MASK_UPTO(loglevel
);
316 syslog(LOG_DEBUG
, "%s: about to set racoon's log level %d, mask %x\n", __FUNCTION__
, level
, mask
);
317 asl_set_filter(NULL
, mask
);
321 plogsetlevelstr(char *levelstr
)
327 if (strncmp(levelstr
, ASL_STRING_EMERG
, sizeof(ASL_STRING_EMERG
) - 1) == 0) {
328 plogsetlevel(ASL_LEVEL_EMERG
);
329 } else if (strncmp(levelstr
, ASL_STRING_ALERT
, sizeof(ASL_STRING_ALERT
) - 1) == 0) {
330 plogsetlevel(ASL_LEVEL_ALERT
);
331 } else if (strncmp(levelstr
, ASL_STRING_CRIT
, sizeof(ASL_STRING_CRIT
) - 1) == 0) {
332 plogsetlevel(ASL_LEVEL_CRIT
);
333 } else if (strncmp(levelstr
, ASL_STRING_ERR
, sizeof(ASL_STRING_ERR
) - 1) == 0) {
334 plogsetlevel(ASL_LEVEL_ERR
);
335 } else if (strncmp(levelstr
, ASL_STRING_WARNING
, sizeof(ASL_STRING_NOTICE
) - 1) == 0) {
336 plogsetlevel(ASL_LEVEL_WARNING
);
337 } else if (strncmp(levelstr
, ASL_STRING_NOTICE
, sizeof(ASL_STRING_NOTICE
) - 1) == 0) {
338 plogsetlevel(ASL_LEVEL_NOTICE
);
339 } else if (strncmp(levelstr
, ASL_STRING_INFO
, sizeof(ASL_STRING_INFO
) - 1) == 0) {
340 plogsetlevel(ASL_LEVEL_INFO
);
341 } else if (strncmp(levelstr
, ASL_STRING_DEBUG
, sizeof(ASL_STRING_DEBUG
) - 1) == 0) {
342 plogsetlevel(ASL_LEVEL_DEBUG
);
347 plogsetlevelquotedstr (char *levelquotedstr
)
351 if (!levelquotedstr
) {
352 plog(ASL_LEVEL_ERR
, "Null log level (quoted string)");
356 len
= strlen(levelquotedstr
);
358 levelquotedstr
[0] != '"' ||
359 levelquotedstr
[len
- 1] != '"') {
360 plog(ASL_LEVEL_ERR
, "Invalid log level (quoted string): %s", levelquotedstr
);
364 levelquotedstr
[len
- 1] = '\0';
365 plogsetlevelstr(&levelquotedstr
[1]);
371 CFPropertyListRef globals
;
372 CFStringRef logFileRef
;
373 CFNumberRef debugLevelRef
;
374 CFStringRef debugLevelStringRef
;
375 char logLevelStr
[16];
380 SCPreferencesSynchronize(gPrefs
);
382 globals
= SCPreferencesGetValue(gPrefs
, CFSTR("Global"));
383 if (!globals
|| (CFGetTypeID(globals
) != CFDictionaryGetTypeID())) {
386 debugLevelRef
= CFDictionaryGetValue(globals
, CFSTR("DebugLevel"));
387 if (debugLevelRef
&& (CFGetTypeID(debugLevelRef
) == CFNumberGetTypeID())) {
388 CFNumberGetValue(debugLevelRef
, kCFNumberSInt32Type
, &level
);
391 debugLevelStringRef
= CFDictionaryGetValue(globals
, CFSTR("DebugLevelString"));
392 if (debugLevelStringRef
&& (CFGetTypeID(debugLevelStringRef
) == CFStringGetTypeID())) {
393 CFStringGetCString(debugLevelStringRef
, logLevelStr
, sizeof(logLevelStr
), kCFStringEncodingMacRoman
);
394 plogsetlevelstr(logLevelStr
);
398 logFileRef
= CFDictionaryGetValue(globals
, CFSTR("DebugLogfile"));
399 if (!logFileRef
|| (CFGetTypeID(logFileRef
) != CFStringGetTypeID())) {
402 CFStringGetCString(logFileRef
, logFileStr
, MAXPATHLEN
, kCFStringEncodingMacRoman
);
403 plogsetfile(logFileStr
);
410 logRef
= NULL
;//asl_open(NULL, plog_facility, 0);
411 plogsetlevel(ASL_LEVEL_NOTICE
);
412 //plogsetlevel(ASL_LEVEL_DEBUG);
417 plogsetsessioninfo (const char *session_id
,
418 const char *session_type
,
419 const char *session_ver
)
427 gSessId
= strdup(session_id
);
435 gSessType
= strdup(session_id
);
443 gSessVer
= strdup(session_ver
);
448 createCStringFromCFString(CFAllocatorRef allocator
, CFStringRef cfstr
)
450 CFIndex cstr_len
= CFStringGetMaximumSizeForEncoding(CFStringGetLength(cfstr
), kCFStringEncodingUTF8
) + 1;
451 char *cstr
= (char *)CFAllocatorAllocate(allocator
, cstr_len
, 0);
452 CFStringGetCString(cfstr
, cstr
, cstr_len
, kCFStringEncodingUTF8
);
457 plogcf(int priority
, CFStringRef fmt
, ...)
464 cfstr
= CFStringCreateWithFormatAndArguments(kCFAllocatorDefault
, NULL
, fmt
, args
);
467 cstr
= createCStringFromCFString(kCFAllocatorDefault
, cfstr
);
468 plog(priority
, "%s", cstr
);
470 CFAllocatorDeallocate(kCFAllocatorDefault
, cstr
);