From e3ade0c93d1a233a0826096691db1bc7253c577c Mon Sep 17 00:00:00 2001 From: Pavel Charvat Date: Mon, 28 Apr 2008 14:37:59 +0200 Subject: [PATCH] Quick fix of possible daedlock when logging from Shepherd's singal handlers. --- lib/lib.h | 2 ++ lib/log.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/lib/lib.h b/lib/lib.h index f8659ed1..6fdefd64 100644 --- a/lib/lib.h +++ b/lib/lib.h @@ -101,6 +101,8 @@ extern void (*log_switch_hook)(struct tm *tm); void msg(uns cat, const char *fmt, ...) FORMAT_CHECK(printf,2,3); void vmsg(uns cat, const char *fmt, va_list args); +void safe_msg(uns cat, const char *fmt, ...) FORMAT_CHECK(printf,2,3); +void safe_vmsg(uns cat, const char *fmt, va_list args); void die(const char *, ...) NONRET FORMAT_CHECK(printf,1,2); void log_init(const char *argv0); void log_file(const char *name); diff --git a/lib/log.c b/lib/log.c index 0063f3ea..4d91b315 100644 --- a/lib/log.c +++ b/lib/log.c @@ -96,6 +96,67 @@ msg(unsigned int cat, const char *fmt, ...) va_end(args); } +void +safe_vmsg(unsigned int cat, const char *fmt, va_list args) +{ + byte *buf, *p; + int buflen = 256; + int l, l0, r; + va_list args2; + + while (1) + { + p = buf = alloca(buflen); + *p++ = cat; + p += sprintf(p, " \?\?\?\?-\?\?-\?\? \?\?:\?\?:\?\?"); + if (log_precise_timings) + p += sprintf(p, ".\?\?\?\?\?\?"); + *p++ = ' '; + if (log_title) + { + if (log_pid) + p += sprintf(p, "[%s (%d)] ", log_title, log_pid); + else + p += sprintf(p, "[%s] ", log_title); + } + else + { + if (log_pid) + p += sprintf(p, "[%d] ", log_pid); + } + l0 = p - buf + 1; + r = buflen - l0; + va_copy(args2, args); + l = vsnprintf(p, r, fmt, args2); + va_end(args2); + if (l < 0) + l = r; + else if (l < r) + { + while (*p) + { + if (*p < 0x20 && *p != '\t') + *p = 0x7f; + p++; + } + *p = '\n'; + write(2, buf, l + l0); + return; + } + buflen = l + l0 + 1; + } +} + +void +safe_msg(unsigned int cat, const char *fmt, ...) +{ + va_list args; + + va_start(args, fmt); + safe_vmsg(cat, fmt, args); + va_end(args); +} + void die(const char *fmt, ...) { -- 2.39.2