]> mj.ucw.cz Git - libucw.git/commitdiff
Quick fix of possible daedlock when logging from Shepherd's singal handlers.
authorPavel Charvat <pavel.charvat@netcentrum.cz>
Mon, 28 Apr 2008 12:37:59 +0000 (14:37 +0200)
committerPavel Charvat <pavel.charvat@netcentrum.cz>
Mon, 28 Apr 2008 12:37:59 +0000 (14:37 +0200)
lib/lib.h
lib/log.c

index f8659ed192a3b276fd604b1ba884d367d11476b7..6fdefd642adc5cc0f0a4229df30f39761953ca82 100644 (file)
--- 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);
index 0063f3ea7eed13d2239f28f85cda35d8a475d927..4d91b315bec14b43f40ae8d903bb3aecbc96efcc 100644 (file)
--- 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, ...)
 {