1 /* strsignal.c -- implement strsignal() for architectures without it
2 Written by Fred Fish. fnf@cygnus.com
3 This file is in the public domain.
4 */
5
6 #include "hstdinc.h"
7
8 #include "hercules.h"
9
10 #if !defined(HAVE_STRSIGNAL)
11
12 /* We need to declare sys_siglist, because even if the system provides
13 it we can't assume that it is declared in <signal.h> (for example,
14 SunOS provides sys_siglist, but it does not declare it in any
15 header file). fHowever, we can't declare sys_siglist portably,
16 because on some systems it is declared with const and on some
17 systems it is declared without const. If we were using autoconf,
18 we could work out the right declaration. Until, then we just
19 ignore any declaration in the system header files, and always
20 declare it ourselves. With luck, this will always work. */
21 #define sys_siglist no_such_symbol
22
23 /* Routines imported from standard C runtime libraries. */
24
25 #ifdef __STDC__
26 #include <stddef.h>
27 extern void *malloc (size_t size); /* 4.10.3.3 */
28 extern void *memset (void *s, int c, size_t n); /* 4.11.6.1 */
29 #else /* !__STDC__ */
30 extern char *malloc (); /* Standard memory allocater */
31 extern char *memset ();
32 #endif /* __STDC__ */
33
34 /* Undefine the macro we used to hide the definition of sys_siglist
35 found in the system header files. */
36 #undef sys_siglist
37
38 #ifndef NULL
39 # ifdef __STDC__
40 # define NULL (void *) 0
41 # else
42 # define NULL 0
43 # endif
44 #endif
45
46 #ifndef MAX
47 # define MAX(a,b) ((a) > (b) ? (a) : (b))
48 #endif
49
50 /* Translation table for signal values.
51
52 Note that this table is generally only accessed when it is used at runtime
53 to initialize signal name and message tables that are indexed by signal
54 value.
55
56 Not all of these signals will exist on all systems. This table is the only
57 thing that should have to be updated as new signal numbers are introduced.
58 It's sort of ugly, but at least its portable. */
59
60 struct signal_info
61 {
62 int value; /* The numeric value from <signal.h> */
63 const char *name; /* The equivalent symbolic value */
64 #ifndef HAVE_SYS_SIGLIST
65 const char *msg; /* Short message about this value */
66 #endif
67 };
68
69 #ifndef HAVE_SYS_SIGLIST
70 # define ENTRY(value, name, msg) {value, name, msg}
71 #else
72 # define ENTRY(value, name, msg) {value, name}
73 #endif
74
75 static const struct signal_info signal_table[] =
76 {
77 #if defined (SIGHUP)
78 ENTRY(SIGHUP, "SIGHUP", "Hangup"),
79 #endif
80 #if defined (SIGINT)
81 ENTRY(SIGINT, "SIGINT", "Interrupt"),
82 #endif
83 #if defined (SIGQUIT)
84 ENTRY(SIGQUIT, "SIGQUIT", "Quit"),
85 #endif
86 #if defined (SIGILL)
87 ENTRY(SIGILL, "SIGILL", "Illegal instruction"),
88 #endif
89 #if defined (SIGTRAP)
90 ENTRY(SIGTRAP, "SIGTRAP", "Trace/breakpoint trap"),
91 #endif
92 /* Put SIGIOT before SIGABRT, so that if SIGIOT==SIGABRT then SIGABRT
93 overrides SIGIOT. SIGABRT is in ANSI and POSIX.1, and SIGIOT isn't. */
94 #if defined (SIGIOT)
95 ENTRY(SIGIOT, "SIGIOT", "IOT trap"),
96 #endif
97 #if defined (SIGABRT)
98 ENTRY(SIGABRT, "SIGABRT", "Aborted"),
99 #endif
100 #if defined (SIGEMT)
101 ENTRY(SIGEMT, "SIGEMT", "Emulation trap"),
102 #endif
103 #if defined (SIGFPE)
104 ENTRY(SIGFPE, "SIGFPE", "Arithmetic exception"),
105 #endif
106 #if defined (SIGKILL)
107 ENTRY(SIGKILL, "SIGKILL", "Killed"),
108 #endif
109 #if defined (SIGBUS)
110 ENTRY(SIGBUS, "SIGBUS", "Bus error"),
111 #endif
112 #if defined (SIGSEGV)
113 ENTRY(SIGSEGV, "SIGSEGV", "Segmentation fault"),
114 #endif
115 #if defined (SIGSYS)
116 ENTRY(SIGSYS, "SIGSYS", "Bad system call"),
117 #endif
118 #if defined (SIGPIPE)
119 ENTRY(SIGPIPE, "SIGPIPE", "Broken pipe"),
120 #endif
121 #if defined (SIGALRM)
122 ENTRY(SIGALRM, "SIGALRM", "Alarm clock"),
123 #endif
124 #if defined (SIGTERM)
125 ENTRY(SIGTERM, "SIGTERM", "Terminated"),
126 #endif
127 #if defined (SIGUSR1)
128 ENTRY(SIGUSR1, "SIGUSR1", "User defined signal 1"),
129 #endif
130 #if defined (SIGUSR2)
131 ENTRY(SIGUSR2, "SIGUSR2", "User defined signal 2"),
132 #endif
133 /* Put SIGCLD before SIGCHLD, so that if SIGCLD==SIGCHLD then SIGCHLD
134 overrides SIGCLD. SIGCHLD is in POXIX.1 */
135 #if defined (SIGCLD)
136 ENTRY(SIGCLD, "SIGCLD", "Child status changed"),
137 #endif
138 #if defined (SIGCHLD)
139 ENTRY(SIGCHLD, "SIGCHLD", "Child status changed"),
140 #endif
141 #if defined (SIGPWR)
142 ENTRY(SIGPWR, "SIGPWR", "Power fail/restart"),
143 #endif
144 #if defined (SIGWINCH)
145 ENTRY(SIGWINCH, "SIGWINCH", "Window size changed"),
146 #endif
147 #if defined (SIGURG)
148 ENTRY(SIGURG, "SIGURG", "Urgent I/O condition"),
149 #endif
150 #if defined (SIGIO)
151 /* "I/O pending" has also been suggested, but is misleading since the
152 signal only happens when the process has asked for it, not everytime
153 I/O is pending. */
154 ENTRY(SIGIO, "SIGIO", "I/O possible"),
155 #endif
156 #if defined (SIGPOLL)
157 ENTRY(SIGPOLL, "SIGPOLL", "Pollable event occurred"),
158 #endif
159 #if defined (SIGSTOP)
160 ENTRY(SIGSTOP, "SIGSTOP", "Stopped (signal)"),
161 #endif
162 #if defined (SIGTSTP)
163 ENTRY(SIGTSTP, "SIGTSTP", "Stopped (user)"),
164 #endif
165 #if defined (SIGCONT)
166 ENTRY(SIGCONT, "SIGCONT", "Continued"),
167 #endif
168 #if defined (SIGTTIN)
169 ENTRY(SIGTTIN, "SIGTTIN", "Stopped (tty input)"),
170 #endif
171 #if defined (SIGTTOU)
172 ENTRY(SIGTTOU, "SIGTTOU", "Stopped (tty output)"),
173 #endif
174 #if defined (SIGVTALRM)
175 ENTRY(SIGVTALRM, "SIGVTALRM", "Virtual timer expired"),
176 #endif
177 #if defined (SIGPROF)
178 ENTRY(SIGPROF, "SIGPROF", "Profiling timer expired"),
179 #endif
180 #if defined (SIGXCPU)
181 ENTRY(SIGXCPU, "SIGXCPU", "CPU time limit exceeded"),
182 #endif
183 #if defined (SIGXFSZ)
184 ENTRY(SIGXFSZ, "SIGXFSZ", "File size limit exceeded"),
185 #endif
186 #if defined (SIGWIND)
187 ENTRY(SIGWIND, "SIGWIND", "SIGWIND"),
188 #endif
189 #if defined (SIGPHONE)
190 ENTRY(SIGPHONE, "SIGPHONE", "SIGPHONE"),
191 #endif
192 #if defined (SIGLOST)
193 ENTRY(SIGLOST, "SIGLOST", "Resource lost"),
194 #endif
195 #if defined (SIGWAITING)
196 ENTRY(SIGWAITING, "SIGWAITING", "Process's LWPs are blocked"),
197 #endif
198 #if defined (SIGLWP)
199 ENTRY(SIGLWP, "SIGLWP", "Signal LWP"),
200 #endif
201 #if defined (SIGDANGER)
202 ENTRY(SIGDANGER, "SIGDANGER", "Swap space dangerously low"),
203 #endif
204 #if defined (SIGGRANT)
205 ENTRY(SIGGRANT, "SIGGRANT", "Monitor mode granted"),
206 #endif
207 #if defined (SIGRETRACT)
208 ENTRY(SIGRETRACT, "SIGRETRACT", "Need to relinguish monitor mode"),
209 #endif
210 #if defined (SIGMSG)
211 ENTRY(SIGMSG, "SIGMSG", "Monitor mode data available"),
212 #endif
213 #if defined (SIGSOUND)
214 ENTRY(SIGSOUND, "SIGSOUND", "Sound completed"),
215 #endif
216 #if defined (SIGSAK)
217 ENTRY(SIGSAK, "SIGSAK", "Secure attention"),
218 #endif
219 ENTRY(0, NULL, NULL)
220 };
221
222 /* Translation table allocated and initialized at runtime. Indexed by the
223 signal value to find the equivalent symbolic value. */
224
225 static const char **signal_names;
226 static int num_signal_names = 0;
227
228 /* Translation table allocated and initialized at runtime, if it does not
229 already exist in the host environment. Indexed by the signal value to find
230 the descriptive string.
231
232 We don't export it for use in other modules because even though it has the
233 same name, it differs from other implementations in that it is dynamically
234 initialized rather than statically initialized. */
235
236 #ifndef HAVE_SYS_SIGLIST
237
238 static int sys_nsig;
239 static const char **sys_siglist;
240
241 #else
242
243 #ifdef NSIG
244 static int sys_nsig = NSIG;
245 #else
246 #ifdef _NSIG
247 static int sys_nsig = _NSIG;
248 #endif
249 #endif
250 extern const char * const sys_siglist[];
251
252 #endif
253
254
255 #ifndef HAVE_SYS_SIGLIST
256 /*
257
258 NAME
259
260 init_signal_tables -- initialize the name and message tables
261
262 SYNOPSIS
263
264 static void init_signal_tables ();
265
266 DESCRIPTION
267
268 Using the signal_table, which is initialized at compile time, generate
269 the signal_names and the sys_siglist (if needed) tables, which are
270 indexed at runtime by a specific signal value.
271
272 BUGS
273
274 The initialization of the tables may fail under low memory conditions,
275 in which case we don't do anything particularly useful, but we don't
276 bomb either. Who knows, it might succeed at a later point if we free
277 some memory in the meantime. In any case, the other routines know
278 how to deal with lack of a table after trying to initialize it. This
279 may or may not be considered to be a bug, that we don't specifically
280 warn about this particular failure mode.
281
282 */
283
284 static void
init_signal_tables()285 init_signal_tables ()
286 {
287 const struct signal_info *eip;
288 int nbytes;
289
290 /* If we haven't already scanned the signal_table once to find the maximum
291 signal value, then go find it now. */
292
293 if (num_signal_names == 0)
294 {
295 for (eip = signal_table; eip -> name != NULL; eip++)
296 {
297 if (eip -> value >= num_signal_names)
298 {
299 num_signal_names = eip -> value + 1;
300 }
301 }
302 }
303
304 /* Now attempt to allocate the sys_siglist table, zero it out, and then
305 initialize it from the statically initialized signal_table. */
306
307 if (sys_siglist == NULL)
308 {
309 nbytes = num_signal_names * sizeof (char *);
310 if ((sys_siglist = (const char **) malloc (nbytes)) != NULL)
311 {
312 memset (sys_siglist, 0, nbytes);
313 sys_nsig = num_signal_names;
314 for (eip = signal_table; eip -> name != NULL; eip++)
315 {
316 sys_siglist[eip -> value] = eip -> msg;
317 }
318 }
319 }
320 }
321 #endif
322
323
324 /*
325
326 NAME
327
328 strsignal -- map a signal number to a signal message string
329
330 SYNOPSIS
331
332 const char *strsignal (int signo)
333
334 DESCRIPTION
335
336 Maps an signal number to an signal message string, the contents of
337 which are implementation defined. On systems which have the external
338 variable sys_siglist, these strings will be the same as the ones used
339 by psignal().
340
341 If the supplied signal number is within the valid range of indices
342 for the sys_siglist, but no message is available for the particular
343 signal number, then returns the string "Signal NUM", where NUM is the
344 signal number.
345
346 If the supplied signal number is not a valid index into sys_siglist,
347 returns NULL.
348
349 The returned string is only guaranteed to be valid only until the
350 next call to strsignal.
351
352 */
353
354 const char *
strsignal(signo)355 strsignal (signo)
356 int signo;
357 {
358 const char *msg;
359 static char buf[32];
360
361 #ifndef HAVE_SYS_SIGLIST
362
363 if (signal_names == NULL)
364 {
365 init_signal_tables ();
366 }
367
368 #endif
369
370 if ((signo < 0) || (signo >= sys_nsig))
371 {
372 /* Out of range, just return NULL */
373 msg = NULL;
374 }
375 else if ((sys_siglist == NULL) || (sys_siglist[signo] == NULL))
376 {
377 /* In range, but no sys_siglist or no entry at this index. */
378 sprintf (buf, "Signal %d", signo);
379 msg = (const char *) buf;
380 }
381 else
382 {
383 /* In range, and a valid message. Just return the message. */
384 msg = (const char *) sys_siglist[signo];
385 }
386
387 return (msg);
388 }
389
390 #endif // !defined(HAVE_STRSIGNAL)
391