usterr: make error reporting functions signal safe
authorPierre-Marc Fournier <pierre-marc.fournier@polymtl.ca>
Mon, 28 Sep 2009 23:49:53 +0000 (19:49 -0400)
committerPierre-Marc Fournier <pierre-marc.fournier@polymtl.ca>
Mon, 28 Sep 2009 23:57:51 +0000 (19:57 -0400)
Previously, they could cause corruption / lockings

share/usterr.h

index a1a9dac2714fc97d0c75418bfa3035cb1ba339df..1819f973069b99f670fd5411f6d7809c35daffee 100644 (file)
@@ -5,6 +5,9 @@
 #include <sys/types.h>
 #include <sys/syscall.h>
 #include <errno.h>
+#include <stdarg.h>
+
+#include "share.h"
 
 #ifndef UST_COMPONENT
 //#error UST_COMPONENT is undefined
 #define XSTR(d) STR(d)
 #define STR(s) #s
 
+/* We sometimes print in the tracing path, and tracing can occur in
+ * signal handlers, so we must use a print method which is signal safe.
+ */
+
+#define sigsafe_print_err(fmt, args...) \
+{ \
+       /* Can't use dynamic allocation. Limit ourselves to 250 chars. */ \
+       char ____buf[250]; \
+       int ____saved_errno; \
+\
+       /* Save the errno. */ \
+       ____saved_errno = errno; \
+\
+       snprintf(____buf, sizeof(____buf), fmt, ## args); \
+\
+       /* Add end of string in case of buffer overflow. */ \
+       ____buf[sizeof(____buf)-1] = 0; \
+\
+       patient_write(STDERR_FILENO, ____buf, strlen(____buf)); \
+       /* Can't print errors because we are in the error printing code path. */ \
+\
+       /* Restore errno, in order to be async-signal safe. */ \
+       errno = ____saved_errno; \
+}
+
 #define UST_STR_COMPONENT XSTR(UST_COMPONENT)
 
-#define ERRMSG(fmt, args...) do { fprintf(stderr, UST_STR_COMPONENT "[%ld/%ld]: " fmt " (" __FILE__ ":" XSTR(__LINE__) ")\n", (long) getpid(), (long) syscall(SYS_gettid), ## args); fflush(stderr); } while(0)
+#define ERRMSG(fmt, args...) do { sigsafe_print_err(UST_STR_COMPONENT "[%ld/%ld]: " fmt " (" __FILE__ ":" XSTR(__LINE__) ")\n", (long) getpid(), (long) syscall(SYS_gettid), ## args); fflush(stderr); } while(0)
 
 #define DEBUG
 #ifdef DEBUG
This page took 0.024529 seconds and 4 git commands to generate.