Make RAW_LOG(FATAL) work while RT.

Generating the backtrace was allocating memory.  Which would then die
again...  So, drop RT permission on the way down.

Change-Id: Iaa14beb029b2d09f208179f81fdb9d62f3209163
Signed-off-by: Austin Schuh <austin.linux@gmail.com>
diff --git a/aos/realtime_test.cc b/aos/realtime_test.cc
index 951fd68..98f53e1 100644
--- a/aos/realtime_test.cc
+++ b/aos/realtime_test.cc
@@ -2,6 +2,7 @@
 
 #include "aos/init.h"
 #include "glog/logging.h"
+#include "glog/raw_logging.h"
 #include "gtest/gtest.h"
 
 DECLARE_bool(die_on_malloc);
@@ -116,6 +117,16 @@
       "SIGSEGV \\(@0x0\\) received by PID.*stack trace:");
 }
 
+// Tests that RAW_LOG(FATAL) explodes properly.
+TEST(RealtimeDeathTest, RawFatal) {
+  EXPECT_DEATH(
+      {
+        ScopedRealtime rt;
+        RAW_LOG(FATAL, "Cute message here\n");
+      },
+      "Cute message here");
+}
+
 }  // namespace testing
 }  // namespace aos
 
diff --git a/third_party/google-glog/src/raw_logging.cc b/third_party/google-glog/src/raw_logging.cc
index 7a7409b..efe6e7d 100644
--- a/third_party/google-glog/src/raw_logging.cc
+++ b/third_party/google-glog/src/raw_logging.cc
@@ -66,6 +66,16 @@
 # define safe_write(fd, s, len)  write(fd, s, len)
 #endif
 
+namespace aos {
+void FatalUnsetRealtimePriority() __attribute__((weak));
+}
+
+static void MaybeUnsetRealtime() {
+  if (&aos::FatalUnsetRealtimePriority != nullptr) {
+    aos::FatalUnsetRealtimePriority();
+  }
+}
+
 _START_GOOGLE_NAMESPACE_
 
 // Data for RawLog__ below. We simply pick up the latest
@@ -152,6 +162,7 @@
   // We write just once to avoid races with other invocations of RawLog__.
   safe_write(STDERR_FILENO, buffer, strlen(buffer));
   if (severity == GLOG_FATAL)  {
+    MaybeUnsetRealtime();
     if (!sync_val_compare_and_swap(&crashed, false, true)) {
       crash_reason.filename = file;
       crash_reason.line_number = line;