#endif
#include <ctype.h>
#include <err.h>
+#include <pthread.h>
+#include <unistd.h>
#include "var.h"
#include "misc.h"
#include "gcmalloc.h"
#ifndef VA_COPY
-# define VA_COPY(dst,src) memcpy(&(dst), &(src), sizeof(va_list))
+# define VA_COPY(dst,src) memcpy(&(dst), (src), sizeof(va_list))
#endif
extern int print_pid;
int print_location = 0;
static struct log *logp = NULL;
+static pthread_mutex_t logp_mtx = {0};
static char *logfile = NULL;
static char *plog_common __P((int, const char *, const char *));
static char *
plog_common(pri, fmt, func)
- int pri;
- const char *fmt, *func;
+int pri;
+const char *fmt, *func;
{
static char buf[800]; /* XXX shoule be allocated every time ? */
char *p;
int reslen, len;
-
+
p = buf;
reslen = sizeof(buf);
-
+
if (logfile || f_foreground) {
time_t t;
struct tm *tm;
-
+
t = time(0);
tm = localtime(&t);
len = strftime(p, reslen, "%Y-%m-%d %T: ", tm);
p += len;
reslen -= len;
}
-
+
if (pri < ARRAYLEN(ptab)) {
if (print_pid)
len = snprintf(p, reslen, "[%d] %s: ", getpid(), ptab[pri].name);
} else
*p = '\0';
}
-
+
if (print_location)
snprintf(p, reslen, "%s: %s", func, fmt);
else
while ((p = strstr(buf,"%z")) != NULL)
p[1] = 'l';
#endif
-
+
return buf;
}
void
-plog(int pri, const char *func, struct sockaddr *sa, const char *fmt, ...)
+plogmtxinit (void)
+{
+ pthread_mutexattr_t attrs;
+ pthread_mutexattr_init(&attrs);
+ pthread_mutexattr_settype(&attrs, PTHREAD_MUTEX_RECURSIVE);
+ pthread_mutex_init(&logp_mtx, &attrs);
+ pthread_mutexattr_destroy(&attrs);
+}
+
+void
+plog_func(int pri, const char *func, struct sockaddr *sa, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
- plogv(pri, func, sa, fmt, ap);
+ plogv(pri, func, sa, fmt, &ap);
va_end(ap);
}
void
plogv(int pri, const char *func, struct sockaddr *sa,
- const char *fmt, va_list ap)
+ const char *fmt, va_list *ap)
{
char *newfmt;
va_list ap_bak;
-
+
if (pri > loglevel)
return;
- newfmt = plog_common(pri, fmt, func);
+ pthread_mutex_lock(&logp_mtx);
+ newfmt = plog_common(pri, fmt, func);
+
VA_COPY(ap_bak, ap);
if (f_foreground)
- vprintf(newfmt, ap);
-
- if (logfile)
+ vprintf(newfmt, *ap);
+
+
+ if (logfile) {
log_vaprint(logp, newfmt, ap_bak);
- else {
+ } else {
if (pri < ARRAYLEN(ptab))
vsyslog(ptab[pri].priority, newfmt, ap_bak);
else
vsyslog(LOG_ALERT, newfmt, ap_bak);
}
+ pthread_mutex_unlock(&logp_mtx);
}
void
buf[i++] = '\n';
buf[i] = '\0';
}
- plog(pri, LOCATION, NULL, "%s", buf);
+ plog_func(pri, LOCATION, NULL, "%s", buf);
racoon_free(buf);
}
void
ploginit()
{
+ pthread_mutex_lock(&logp_mtx);
+
if (logfile) {
logp = log_open(250, logfile);
if (logp == NULL)
errx(1, "ERROR: failed to open log file %s.", logfile);
+ pthread_mutex_unlock(&logp_mtx);
return;
}
openlog(pname, LOG_NDELAY, LOG_DAEMON);
+
+ pthread_mutex_unlock(&logp_mtx);
}
void
plogset(file)
char *file;
{
+ pthread_mutex_lock(&logp_mtx);
if (logfile != NULL)
racoon_free(logfile);
- logfile = strdup(file);
+ logfile = racoon_strdup(file);
+ STRDUP_FATAL(logfile);
+ pthread_mutex_unlock(&logp_mtx);
}
void
plogreset(file)
char *file;
{
-
+ pthread_mutex_lock(&logp_mtx);
+
/* if log paths equal - do nothing */
- if (logfile == NULL && file == NULL)
+ if (logfile == NULL && file == NULL) {
+ pthread_mutex_unlock(&logp_mtx);
return;
+ }
if (logfile != NULL && file != NULL)
- if (!strcmp(logfile, file))
+ if (!strcmp(logfile, file)) {
+ pthread_mutex_unlock(&logp_mtx);
return;
-
+ }
+
if (logfile == NULL) /* no logfile was specified - daemon was used */
closelog(); /* close it */
else {
if (file)
plogset(file);
ploginit();
-}
-
+
+ pthread_mutex_unlock(&logp_mtx);
+}
+
+/*
+ Returns a printable string from (possibly) binary data ;
+ concatenates all unprintable chars to one space.
+ XXX Maybe the printable chars range is too large...
+ */
+char*
+binsanitize(binstr, n)
+ char *binstr;
+ size_t n;
+{
+ int p,q;
+ for (p = 0, q = 0; p < n; p++) {
+ if (isgraph((int)binstr[p])) {
+ binstr[q++] = binstr[p];
+ } else {
+ if (q && binstr[q - 1] != ' ')
+ binstr[q++] = ' ';
+ }
+ }
+ binstr[q++] = '\0';
+ return binstr;
+}
+