blocxx
SignalUtils.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright (C) 2005, Vintela, Inc. All rights reserved.
3 * Copyright (C) 2006, Novell, Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of
14 * Vintela, Inc.,
15 * nor Novell, Inc.,
16 * nor the names of its contributors or employees may be used to
17 * endorse or promote products derived from this software without
18 * specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 *******************************************************************************/
32 
33 
38 #include "blocxx/BLOCXX_config.h"
39 #include "blocxx/SignalUtils.hpp"
40 #include "blocxx/String.hpp"
41 #include <algorithm>
42 
43 #include <signal.h>
44 
45 namespace BLOCXX_NAMESPACE
46 {
47  namespace SignalUtils
48  {
49  namespace // anonymous
50  {
51  struct signalNameMapping
52  {
53  int sig;
54  const char* name;
55  };
56 
57  // Simple comparison functions so that searching (and sorting, if
58  // desired) can be done on a set of signal name mappings.
59  bool operator <(const signalNameMapping& s1, const signalNameMapping& s2)
60  {
61  return s1.sig < s2.sig;
62  }
63  bool operator ==(const signalNameMapping& s1, const signalNameMapping& s2)
64  {
65  return s1.sig == s2.sig;
66  }
67 
68  // An array (not neccessarily sorted) of signal mappings. This is
69  // really ugly, but various platforms duplicate signal numbers with
70  // different constants. Not all platforms duplicate them in the same
71  // way, so a general case statement became difficult to manage.
72  const signalNameMapping supportedSignalMapping[] =
73  {
74  { SIGINT, "SIGINT" },
75  { SIGILL, " SIGILL" },
76  { SIGABRT, "SIGABRT" },
77  { SIGFPE, " SIGFPE" },
78  { SIGSEGV, "SIGSEGV" },
79  { SIGTERM, "SIGTERM" },
80 #ifdef BLOCXX_WIN32
81  { SIGBREAK, "SIGBREAK" }
82 #else
83  { SIGHUP, "SIGHUP" },
84  { SIGQUIT, "SIGQUIT" },
85  { SIGKILL, "SIGKILL" },
86  { SIGPIPE, "SIGPIPE" },
87  { SIGALRM, "SIGALRM" },
88  { SIGUSR1, "SIGUSR1" },
89  { SIGUSR2, "SIGUSR2" },
90  { SIGCHLD, "SIGCHLD" },
91  { SIGCONT, "SIGCONT" },
92  { SIGSTOP, "SIGSTOP" },
93  { SIGTSTP, "SIGTSTP" },
94  { SIGTTIN, "SIGTTIN" },
95  { SIGTTOU, "SIGTTOU" },
96  { SIGBUS, "SIGBUS" },
97 #ifdef SIGPOLL
98  { SIGPOLL, "SIGPOLL" },
99 #endif
100  { SIGPROF, "SIGPROF" },
101  { SIGSYS, "SIGSYS" },
102  { SIGTRAP, "SIGTRAP" },
103  { SIGURG, "SIGURG" },
104  { SIGVTALRM, "SIGVTALRM" },
105  { SIGXCPU, "SIGXCPU" },
106  { SIGXFSZ, "SIGXFSZ" },
107 #ifdef SIGEMT
108  { SIGEMT, "SIGEMT" },
109 #endif
110 #ifdef SIGSTKFLT
111  { SIGSTKFLT, "SIGSTKFLT" },
112 #endif
113 #ifdef SIGPWR
114  { SIGPWR, "SIGPWR" },
115 #endif
116 #ifdef SIGLOST
117  { SIGLOST, "SIGLOST" },
118 #endif
119  { SIGWINCH, "SIGWINCH" },
120 #ifdef SIGINFO
121  { SIGINFO, "SIGINFO" },
122 #endif
123  { SIGIOT, "SIGIOT" },
124  { SIGIO, "SIGIO" },
125 #ifdef SIGCLD
126  { SIGCLD, "SIGCLD" },
127 #endif
128 #ifdef SIGUNUSED
129  { SIGUNUSED, "SIGUNUSED" }
130 #endif
131 #endif // for #ifdef BLOCXX_WIN32....#else...
132  };
133  } // end anonymous namespace
134 
135  const char* signalName(int sig)
136  {
137  signalNameMapping targetSignal = { sig, "" };
138 
139  const signalNameMapping* begin = supportedSignalMapping;
140  const signalNameMapping* end = begin + sizeof(supportedSignalMapping) / sizeof(*supportedSignalMapping);
141 
142  const signalNameMapping* location = std::find(begin, end, targetSignal);
143 
144  if( location != end )
145  {
146  return location->name;
147  }
148  return "UNKNOWN";
149  }
150  } // end namespace SignalUtils
151 } // end namespace BLOCXX_NAMESPACE
152 
153 
154 
155 
156