Merge pull request #232 from qzmfranklin/bazel
[platform/upstream/glog.git] / src / signalhandler_unittest.cc
index 38a54bc..e85f523 100644 (file)
 
 #include "utilities.h"
 
-#include <pthread.h>
+#if defined(HAVE_PTHREAD)
+# include <pthread.h>
+#endif
 #include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string>
 #include "glog/logging.h"
 
+#ifdef HAVE_LIB_GFLAGS
+#include <gflags/gflags.h>
+using namespace GFLAGS_NAMESPACE;
+#endif
+
 using namespace GOOGLE_NAMESPACE;
 
 void* DieInThread(void*) {
-  fprintf(stderr, "0x%lx is dying\n", pthread_self());
+  // We assume pthread_t is an integral number or a pointer, rather
+  // than a complex struct.  In some environments, pthread_self()
+  // returns an uint64 but in some other environments pthread_self()
+  // returns a pointer.  Hence we use C-style cast here, rather than
+  // reinterpret/static_cast, to support both types of environments.
+  fprintf(stderr, "0x%lx is dying\n", (long)pthread_self());
   // Use volatile to prevent from these to be optimized away.
   volatile int a = 0;
   volatile int b = 1 / a;
+  fprintf(stderr, "We should have died: b=%d\n", b);
+  return NULL;
 }
 
 void WriteToStdout(const char* data, int size) {
-  write(STDOUT_FILENO, data, size);
+  if (write(STDOUT_FILENO, data, size) < 0) {
+    // Ignore errors.
+  }
 }
 
 int main(int argc, char **argv) {
@@ -73,12 +89,20 @@ int main(int argc, char **argv) {
     fprintf(stderr, "looping\n");
     while (true);
   } else if (command == "die_in_thread") {
+#if defined(HAVE_PTHREAD)
     pthread_t thread;
     pthread_create(&thread, NULL, &DieInThread, NULL);
     pthread_join(thread, NULL);
+#else
+    fprintf(stderr, "no pthread\n");
+    return 1;
+#endif
   } else if (command == "dump_to_stdout") {
     InstallFailureWriter(WriteToStdout);
     abort();
+  } else if (command == "installed") {
+    fprintf(stderr, "signal handler installed: %s\n",
+        IsFailureSignalHandlerInstalled() ? "true" : "false");
   } else {
     // Tell the shell script
     puts("OK");