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>
46 #if TIME_WITH_SYS_TIME
47 # include <sys/time.h>
51 # include <sys/time.h>
69 # define VA_COPY(dst,src) memcpy(&(dst), (src), sizeof(va_list))
75 u_int32_t loglevel
= LLV_BASE
;
78 int print_location
= 0;
80 static struct log
*logp
= NULL
;
81 static pthread_mutex_t logp_mtx
= {0};
82 static char *logfile
= NULL
;
84 static char *plog_common
__P((int, const char *, const char *));
86 static struct plogtags
{
90 { "(not defined)", 0, },
91 { "INFO", LOG_INFO
, },
92 { "NOTIFY", LOG_INFO
, },
93 { "WARNING", LOG_INFO
, },
94 { "ERROR", LOG_INFO
, },
95 { "ERROR", LOG_ERR
, },
96 { "DEBUG", LOG_DEBUG
, },
97 { "DEBUG2", LOG_DEBUG
, },
101 plog_common(pri
, fmt
, func
)
103 const char *fmt
, *func
;
105 static char buf
[800]; /* XXX shoule be allocated every time ? */
110 reslen
= sizeof(buf
);
112 if (logfile
|| f_foreground
) {
118 len
= strftime(p
, reslen
, "%Y-%m-%d %T: ", tm
);
123 if (pri
< ARRAYLEN(ptab
)) {
125 len
= snprintf(p
, reslen
, "[%d] %s: ", getpid(), ptab
[pri
].name
);
127 len
= snprintf(p
, reslen
, "%s: ", ptab
[pri
].name
);
128 if (len
>= 0 && len
< reslen
) {
136 snprintf(p
, reslen
, "%s: %s", func
, fmt
);
138 snprintf(p
, reslen
, "%s", fmt
);
140 while ((p
= strstr(buf
,"%z")) != NULL
)
150 pthread_mutexattr_t attrs
;
151 pthread_mutexattr_init(&attrs
);
152 pthread_mutexattr_settype(&attrs
, PTHREAD_MUTEX_RECURSIVE
);
153 pthread_mutex_init(&logp_mtx
, &attrs
);
154 pthread_mutexattr_destroy(&attrs
);
158 plog_func(int pri
, const char *func
, struct sockaddr
*sa
, const char *fmt
, ...)
163 plogv(pri
, func
, sa
, fmt
, &ap
);
168 plogv(int pri
, const char *func
, struct sockaddr
*sa
,
169 const char *fmt
, va_list *ap
)
177 pthread_mutex_lock(&logp_mtx
);
179 newfmt
= plog_common(pri
, fmt
, func
);
184 vprintf(newfmt
, *ap
);
188 log_vaprint(logp
, newfmt
, ap_bak
);
190 if (pri
< ARRAYLEN(ptab
))
191 vsyslog(ptab
[pri
].priority
, newfmt
, ap_bak
);
193 vsyslog(LOG_ALERT
, newfmt
, ap_bak
);
195 pthread_mutex_unlock(&logp_mtx
);
199 plogdump(pri
, data
, len
)
212 * 2 words a bytes + 1 space 4 bytes + 1 newline 32 bytes
215 buflen
= (len
* 2) + (len
/ 4) + (len
/ 32) + 3;
216 buf
= racoon_malloc(buflen
);
226 snprintf(&buf
[i
], buflen
- i
, "%02x",
227 ((unsigned char *)data
)[j
] & 0xff);
231 if (buflen
- i
>= 2) {
235 plog_func(pri
, LOCATION
, NULL
, "%s", buf
);
243 pthread_mutex_lock(&logp_mtx
);
246 logp
= log_open(250, logfile
);
248 errx(1, "ERROR: failed to open log file %s.", logfile
);
249 pthread_mutex_unlock(&logp_mtx
);
253 openlog(pname
, LOG_NDELAY
, LOG_DAEMON
);
255 pthread_mutex_unlock(&logp_mtx
);
262 pthread_mutex_lock(&logp_mtx
);
264 racoon_free(logfile
);
265 logfile
= racoon_strdup(file
);
266 STRDUP_FATAL(logfile
);
267 pthread_mutex_unlock(&logp_mtx
);
274 pthread_mutex_lock(&logp_mtx
);
276 /* if log paths equal - do nothing */
277 if (logfile
== NULL
&& file
== NULL
) {
278 pthread_mutex_unlock(&logp_mtx
);
281 if (logfile
!= NULL
&& file
!= NULL
)
282 if (!strcmp(logfile
, file
)) {
283 pthread_mutex_unlock(&logp_mtx
);
287 if (logfile
== NULL
) /* no logfile was specified - daemon was used */
288 closelog(); /* close it */
292 racoon_free(logfile
);
300 pthread_mutex_unlock(&logp_mtx
);
304 Returns a printable string from (possibly) binary data ;
305 concatenates all unprintable chars to one space.
306 XXX Maybe the printable chars range is too large...
309 binsanitize(binstr
, n
)
314 for (p
= 0, q
= 0; p
< n
; p
++) {
315 if (isgraph((int)binstr
[p
])) {
316 binstr
[q
++] = binstr
[p
];
318 if (q
&& binstr
[q
- 1] != ' ')