X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=src%2Fsignalhandler.cc;h=a7aef8b99d294b234c4f5ec74cc054edb28f6124;hb=8e98eb2a5a68fe9d5b710e7ef176126cd8e395ef;hp=58f197e9f99f4daba83b18ee97c0c7da830f7d52;hpb=9de1077eeddbb722714315ebca819ea4288ebb3e;p=platform%2Fupstream%2Fglog.git diff --git a/src/signalhandler.cc b/src/signalhandler.cc index 58f197e..a7aef8b 100644 --- a/src/signalhandler.cc +++ b/src/signalhandler.cc @@ -1,4 +1,32 @@ -// Copyright 2008 Google Inc. All Rights Reserved. +// Copyright (c) 2008, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// // Author: Satoru Takabayashi // // Implementation of InstallFailureSignalHandler(). @@ -13,10 +41,16 @@ #ifdef HAVE_UCONTEXT_H # include #endif +#ifdef HAVE_SYS_UCONTEXT_H +# include +#endif #include _START_GOOGLE_NAMESPACE_ +// TOOD(hamaji): Use signal instead of sigaction? +#ifdef HAVE_SIGACTION + namespace { // We'll install the failure signal handler for these signals. We could @@ -38,7 +72,7 @@ const struct { // Returns the program counter from signal context, NULL if unknown. void* GetPC(void* ucontext_in_void) { -#if defined(HAVE_UCONTEXT_H) && defined(PC_FROM_UCONTEXT) +#if (defined(HAVE_UCONTEXT_H) || defined(HAVE_SYS_UCONTEXT_H)) && defined(PC_FROM_UCONTEXT) if (ucontext_in_void != NULL) { ucontext_t *context = reinterpret_cast(ucontext_in_void); return (void*)context->PC_FROM_UCONTEXT; @@ -111,7 +145,9 @@ class MinimalFormatter { // Writes the given data with the size to the standard error. void WriteToStderr(const char* data, int size) { - write(STDERR_FILENO, data, size); + if (write(STDERR_FILENO, data, size) < 0) { + // Ignore errors. + } } // The writer function can be changed by InstallFailureWriter(). @@ -136,7 +172,7 @@ void DumpTimeInfo() { void DumpSignalInfo(int signal_number, siginfo_t *siginfo) { // Get the signal name. const char* signal_name = NULL; - for (int i = 0; i < ARRAYSIZE(kFailureSignals); ++i) { + for (size_t i = 0; i < ARRAYSIZE(kFailureSignals); ++i) { if (signal_number == kFailureSignals[i].number) { signal_name = kFailureSignals[i].name; } @@ -204,7 +240,8 @@ void DumpStackFrameInfo(const char* prefix, void* pc) { // Invoke the default signal handler. void InvokeDefaultSignalHandler(int signal_number) { - struct sigaction sig_action = {}; // Zero-clear. + struct sigaction sig_action; + memset(&sig_action, 0, sizeof(sig_action)); sigemptyset(&sig_action.sa_mask); sig_action.sa_handler = SIG_DFL; sigaction(signal_number, &sig_action, NULL); @@ -296,20 +333,43 @@ void FailureSignalHandler(int signal_number, } // namespace +#endif // HAVE_SIGACTION + +namespace glog_internal_namespace_ { + +bool IsFailureSignalHandlerInstalled() { +#ifdef HAVE_SIGACTION + struct sigaction sig_action; + memset(&sig_action, 0, sizeof(sig_action)); + sigemptyset(&sig_action.sa_mask); + sigaction(SIGABRT, NULL, &sig_action); + if (sig_action.sa_sigaction == &FailureSignalHandler) + return true; +#endif // HAVE_SIGACTION + return false; +} + +} // namespace glog_internal_namespace_ + void InstallFailureSignalHandler() { +#ifdef HAVE_SIGACTION // Build the sigaction struct. - struct sigaction sig_action = {}; // Zero-clear. + struct sigaction sig_action; + memset(&sig_action, 0, sizeof(sig_action)); sigemptyset(&sig_action.sa_mask); sig_action.sa_flags |= SA_SIGINFO; sig_action.sa_sigaction = &FailureSignalHandler; - for (int i = 0; i < ARRAYSIZE(kFailureSignals); ++i) { + for (size_t i = 0; i < ARRAYSIZE(kFailureSignals); ++i) { CHECK_ERR(sigaction(kFailureSignals[i].number, &sig_action, NULL)); } +#endif // HAVE_SIGACTION } void InstallFailureWriter(void (*writer)(const char* data, int size)) { +#ifdef HAVE_SIGACTION g_failure_writer = writer; +#endif // HAVE_SIGACTION } _END_GOOGLE_NAMESPACE_