[PATCH 3/6] Add LogMessageVerbSigSafe() for logging messages while in signal context

Chase Douglas chase.douglas at canonical.com
Fri Apr 6 11:25:56 PDT 2012


Signed-off-by: Chase Douglas <chase.douglas at canonical.com>
---
 include/os.h |    2 ++
 os/log.c     |   26 ++++++++++++++++++++++++++
 2 files changed, 28 insertions(+), 0 deletions(-)

diff --git a/include/os.h b/include/os.h
index 166c60c..21e9312 100644
--- a/include/os.h
+++ b/include/os.h
@@ -603,6 +603,8 @@ _X_ATTRIBUTE_PRINTF(3, 4);
 extern _X_EXPORT void
 LogMessage(MessageType type, const char *format, ...)
 _X_ATTRIBUTE_PRINTF(2, 3);
+extern _X_EXPORT void
+LogMessageVerbSigSafe(MessageType type, int verb, const char *message);
 
 extern _X_EXPORT void
 LogVHdrMessageVerb(MessageType type, int verb,
diff --git a/os/log.c b/os/log.c
index cd924a3..840439a 100644
--- a/os/log.c
+++ b/os/log.c
@@ -172,6 +172,14 @@ asm(".desc ___crashreporter_info__, 0x10");
 #define X_NONE_STRING                   ""
 #endif
 
+static size_t
+strlen_sigsafe(const char *s)
+{
+    size_t len;
+    for (len = 0; s[len]; len++);
+    return len;
+}
+
 /*
  * LogInit is called to start logging to a file.  It is also called (with
  * NULL arguments) when logging to a file is not wanted.  It must always be
@@ -420,6 +428,24 @@ LogMessage(MessageType type, const char *format, ...)
     va_end(ap);
 }
 
+/* Log a message using only signal safe functions. */
+void
+LogMessageVerbSigSafe(MessageType type, int verb, const char *message)
+{
+    const char *type_str;
+
+    type_str = LogMessageTypeVerbString(type, verb);
+    if (!type_str)
+        return;
+
+    if (type_str[0] != '\0') {
+        write(logFileFd, type_str, strlen_sigsafe(type_str));
+        write(logFileFd, " ", 1);
+    }
+
+    write(logFileFd, message, strlen_sigsafe(message));
+}
+
 void
 LogVHdrMessageVerb(MessageType type, int verb, const char *msg_format,
                    va_list msg_args, const char *hdr_format, va_list hdr_args)
-- 
1.7.9.1



More information about the xorg-devel mailing list