blocxx

SignalUtils.cpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2005, Vintela, Inc. All rights reserved.
00003 * Copyright (C) 2006, Novell, Inc. All rights reserved.
00004 * 
00005 * Redistribution and use in source and binary forms, with or without
00006 * modification, are permitted provided that the following conditions are met:
00007 * 
00008 *     * Redistributions of source code must retain the above copyright notice,
00009 *       this list of conditions and the following disclaimer.
00010 *     * Redistributions in binary form must reproduce the above copyright
00011 *       notice, this list of conditions and the following disclaimer in the
00012 *       documentation and/or other materials provided with the distribution.
00013 *     * Neither the name of 
00014 *       Vintela, Inc., 
00015 *       nor Novell, Inc., 
00016 *       nor the names of its contributors or employees may be used to 
00017 *       endorse or promote products derived from this software without 
00018 *       specific prior written permission.
00019 * 
00020 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00021 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00022 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00023 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00024 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00025 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00026 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00027 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00028 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00029 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00030 * POSSIBILITY OF SUCH DAMAGE.
00031 *******************************************************************************/
00032 
00033 
00038 #include "blocxx/BLOCXX_config.h"
00039 #include "blocxx/SignalUtils.hpp"
00040 #include "blocxx/String.hpp"
00041 #include <algorithm>
00042 
00043 #include <signal.h>
00044 
00045 namespace BLOCXX_NAMESPACE
00046 {
00047    namespace SignalUtils
00048    {
00049       namespace // anonymous
00050       {
00051          struct signalNameMapping
00052          {
00053             int sig;
00054             const char* name;
00055          };
00056 
00057          // Simple comparison functions so that searching (and sorting, if
00058          // desired) can be done on a set of signal name mappings.
00059          bool operator <(const signalNameMapping& s1, const signalNameMapping& s2)
00060          {
00061             return s1.sig < s2.sig;
00062          }
00063          bool operator ==(const signalNameMapping& s1, const signalNameMapping& s2)
00064          {
00065             return s1.sig == s2.sig;
00066          }
00067 
00068          // An array (not neccessarily sorted) of signal mappings.  This is
00069          // really ugly, but various platforms duplicate signal numbers with
00070          // different constants.  Not all platforms duplicate them in the same
00071          // way, so a general case statement became difficult to manage.
00072          const signalNameMapping supportedSignalMapping[] =
00073             {
00074                { SIGINT, "SIGINT" },
00075                { SIGILL, " SIGILL" },
00076                { SIGABRT, "SIGABRT" },
00077                { SIGFPE, " SIGFPE" },
00078                { SIGSEGV, "SIGSEGV" },
00079                { SIGTERM, "SIGTERM" },
00080 #ifdef BLOCXX_WIN32
00081                { SIGBREAK, "SIGBREAK" }
00082 #else
00083                { SIGHUP, "SIGHUP" },
00084                { SIGQUIT, "SIGQUIT" },
00085                { SIGKILL, "SIGKILL" },
00086                { SIGPIPE, "SIGPIPE" },
00087                { SIGALRM, "SIGALRM" },
00088                { SIGUSR1, "SIGUSR1" },
00089                { SIGUSR2, "SIGUSR2" },
00090                { SIGCHLD, "SIGCHLD" },
00091                { SIGCONT, "SIGCONT" },
00092                { SIGSTOP, "SIGSTOP" },
00093                { SIGTSTP, "SIGTSTP" },
00094                { SIGTTIN, "SIGTTIN" },
00095                { SIGTTOU, "SIGTTOU" },
00096                { SIGBUS, "SIGBUS" },
00097 #ifdef SIGPOLL
00098                { SIGPOLL, "SIGPOLL" },
00099 #endif
00100                { SIGPROF, "SIGPROF" },
00101                { SIGSYS, "SIGSYS" },
00102                { SIGTRAP, "SIGTRAP" },
00103                { SIGURG, "SIGURG" },
00104                { SIGVTALRM, "SIGVTALRM" },
00105                { SIGXCPU, "SIGXCPU" },
00106                { SIGXFSZ, "SIGXFSZ" },
00107 #ifdef SIGEMT
00108                { SIGEMT, "SIGEMT" },
00109 #endif
00110 #ifdef SIGSTKFLT
00111                { SIGSTKFLT, "SIGSTKFLT" },
00112 #endif
00113 #ifdef SIGPWR
00114                { SIGPWR, "SIGPWR" },
00115 #endif
00116 #ifdef SIGLOST
00117                { SIGLOST, "SIGLOST" },
00118 #endif
00119                { SIGWINCH, "SIGWINCH" },
00120 #ifdef SIGINFO
00121                { SIGINFO, "SIGINFO" },
00122 #endif
00123                { SIGIOT, "SIGIOT" },
00124                { SIGIO, "SIGIO" },
00125 #ifdef SIGCLD
00126                { SIGCLD, "SIGCLD" },
00127 #endif
00128 #ifdef SIGUNUSED
00129                { SIGUNUSED, "SIGUNUSED" }
00130 #endif
00131 #endif // for  #ifdef BLOCXX_WIN32....#else...
00132             };
00133       } // end anonymous namespace
00134 
00135       const char* signalName(int sig)
00136       {
00137          signalNameMapping targetSignal = { sig, "" };
00138 
00139          const signalNameMapping* begin = supportedSignalMapping;
00140          const signalNameMapping* end = begin + sizeof(supportedSignalMapping) / sizeof(*supportedSignalMapping);
00141 
00142          const signalNameMapping* location = std::find(begin, end, targetSignal);
00143 
00144          if( location != end )
00145          {
00146             return location->name;
00147          }
00148          return "UNKNOWN";
00149       }
00150    } // end namespace SignalUtils
00151 } // end namespace BLOCXX_NAMESPACE
00152 
00153 
00154 
00155 
00156