diff --git a/src/misc/syslog.c b/src/misc/syslog.c index 8de34f8..81fb114 100644 --- a/src/misc/syslog.c +++ b/src/misc/syslog.c @@ -11,7 +11,7 @@ #include "libc.h" static int lock[2]; -static const char *log_ident; +static char *log_ident; static int log_opt; static int log_facility = LOG_USER; static int log_mask = 0xff; @@ -43,15 +43,10 @@ void closelog(void) pthread_setcancelstate(cs, 0); } -static void __openlog(const char *ident, int opt, int facility) +static void __openlog() { - log_ident = ident; - log_opt = opt; - log_facility = facility; - - if (!(opt & LOG_NDELAY) || log_fd>=0) return; - log_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0); + connect(log_fd, (void *)&log_addr, sizeof log_addr); } void openlog(const char *ident, int opt, int facility) @@ -59,7 +54,14 @@ void openlog(const char *ident, int opt, int facility) int cs; pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); LOCK(lock); - __openlog(ident, opt, facility); + + free(log_ident); + log_ident = strdup(ident); + log_opt = opt; + log_facility = facility; + + if ((opt & LOG_NDELAY) && log_fd<0) __openlog(); + UNLOCK(lock); pthread_setcancelstate(cs, 0); } @@ -74,11 +76,8 @@ static void _vsyslog(int priority, const char *message, va_list ap) int l, l2; if (log_fd < 0) { - __openlog(log_ident, log_opt | LOG_NDELAY, log_facility); - if (log_fd < 0) { - UNLOCK(lock); - return; - } + __openlog(); + if (log_fd < 0) return; } now = time(NULL); @@ -90,14 +89,14 @@ static void _vsyslog(int priority, const char *message, va_list ap) priority, timebuf, log_ident ? log_ident : "", "["+!pid, pid, "]"+!pid); + if (l >= sizeof buf) return; l2 = vsnprintf(buf+l, sizeof buf - l, message, ap); if (l2 >= 0) { - l += l2; + if (l2 >= sizeof buf - l) l = sizeof buf - 1; + else l += l2; if (buf[l-1] != '\n') buf[l++] = '\n'; - sendto(log_fd, buf, l, 0, (void *)&log_addr, 11); + send(log_fd, buf, l, 0); } - - UNLOCK(lock); } void __vsyslog(int priority, const char *message, va_list ap)