1*e4b17023SJohn Marino /* Extended support for using signal values.
2*e4b17023SJohn Marino Written by Fred Fish. fnf@cygnus.com
3*e4b17023SJohn Marino This file is in the public domain. */
4*e4b17023SJohn Marino
5*e4b17023SJohn Marino #include "config.h"
6*e4b17023SJohn Marino #include "ansidecl.h"
7*e4b17023SJohn Marino #include "libiberty.h"
8*e4b17023SJohn Marino
9*e4b17023SJohn Marino /* We need to declare sys_siglist, because even if the system provides
10*e4b17023SJohn Marino it we can't assume that it is declared in <signal.h> (for example,
11*e4b17023SJohn Marino SunOS provides sys_siglist, but it does not declare it in any
12*e4b17023SJohn Marino header file). However, we can't declare sys_siglist portably,
13*e4b17023SJohn Marino because on some systems it is declared with const and on some
14*e4b17023SJohn Marino systems it is declared without const. If we were using autoconf,
15*e4b17023SJohn Marino we could work out the right declaration. Until, then we just
16*e4b17023SJohn Marino ignore any declaration in the system header files, and always
17*e4b17023SJohn Marino declare it ourselves. With luck, this will always work. */
18*e4b17023SJohn Marino #define sys_siglist no_such_symbol
19*e4b17023SJohn Marino #define sys_nsig sys_nsig__no_such_symbol
20*e4b17023SJohn Marino
21*e4b17023SJohn Marino #include <stdio.h>
22*e4b17023SJohn Marino #include <signal.h>
23*e4b17023SJohn Marino
24*e4b17023SJohn Marino /* Routines imported from standard C runtime libraries. */
25*e4b17023SJohn Marino
26*e4b17023SJohn Marino #ifdef HAVE_STDLIB_H
27*e4b17023SJohn Marino #include <stdlib.h>
28*e4b17023SJohn Marino #else
29*e4b17023SJohn Marino extern PTR malloc ();
30*e4b17023SJohn Marino #endif
31*e4b17023SJohn Marino
32*e4b17023SJohn Marino #ifdef HAVE_STRING_H
33*e4b17023SJohn Marino #include <string.h>
34*e4b17023SJohn Marino #else
35*e4b17023SJohn Marino extern PTR memset ();
36*e4b17023SJohn Marino #endif
37*e4b17023SJohn Marino
38*e4b17023SJohn Marino /* Undefine the macro we used to hide the definition of sys_siglist
39*e4b17023SJohn Marino found in the system header files. */
40*e4b17023SJohn Marino #undef sys_siglist
41*e4b17023SJohn Marino #undef sys_nsig
42*e4b17023SJohn Marino
43*e4b17023SJohn Marino #ifndef NULL
44*e4b17023SJohn Marino # define NULL (void *) 0
45*e4b17023SJohn Marino #endif
46*e4b17023SJohn Marino
47*e4b17023SJohn Marino #ifndef MAX
48*e4b17023SJohn Marino # define MAX(a,b) ((a) > (b) ? (a) : (b))
49*e4b17023SJohn Marino #endif
50*e4b17023SJohn Marino
51*e4b17023SJohn Marino static void init_signal_tables (void);
52*e4b17023SJohn Marino
53*e4b17023SJohn Marino /* Translation table for signal values.
54*e4b17023SJohn Marino
55*e4b17023SJohn Marino Note that this table is generally only accessed when it is used at runtime
56*e4b17023SJohn Marino to initialize signal name and message tables that are indexed by signal
57*e4b17023SJohn Marino value.
58*e4b17023SJohn Marino
59*e4b17023SJohn Marino Not all of these signals will exist on all systems. This table is the only
60*e4b17023SJohn Marino thing that should have to be updated as new signal numbers are introduced.
61*e4b17023SJohn Marino It's sort of ugly, but at least its portable. */
62*e4b17023SJohn Marino
63*e4b17023SJohn Marino struct signal_info
64*e4b17023SJohn Marino {
65*e4b17023SJohn Marino const int value; /* The numeric value from <signal.h> */
66*e4b17023SJohn Marino const char *const name; /* The equivalent symbolic value */
67*e4b17023SJohn Marino #ifndef HAVE_SYS_SIGLIST
68*e4b17023SJohn Marino const char *const msg; /* Short message about this value */
69*e4b17023SJohn Marino #endif
70*e4b17023SJohn Marino };
71*e4b17023SJohn Marino
72*e4b17023SJohn Marino #ifndef HAVE_SYS_SIGLIST
73*e4b17023SJohn Marino # define ENTRY(value, name, msg) {value, name, msg}
74*e4b17023SJohn Marino #else
75*e4b17023SJohn Marino # define ENTRY(value, name, msg) {value, name}
76*e4b17023SJohn Marino #endif
77*e4b17023SJohn Marino
78*e4b17023SJohn Marino static const struct signal_info signal_table[] =
79*e4b17023SJohn Marino {
80*e4b17023SJohn Marino #if defined (SIGHUP)
81*e4b17023SJohn Marino ENTRY(SIGHUP, "SIGHUP", "Hangup"),
82*e4b17023SJohn Marino #endif
83*e4b17023SJohn Marino #if defined (SIGINT)
84*e4b17023SJohn Marino ENTRY(SIGINT, "SIGINT", "Interrupt"),
85*e4b17023SJohn Marino #endif
86*e4b17023SJohn Marino #if defined (SIGQUIT)
87*e4b17023SJohn Marino ENTRY(SIGQUIT, "SIGQUIT", "Quit"),
88*e4b17023SJohn Marino #endif
89*e4b17023SJohn Marino #if defined (SIGILL)
90*e4b17023SJohn Marino ENTRY(SIGILL, "SIGILL", "Illegal instruction"),
91*e4b17023SJohn Marino #endif
92*e4b17023SJohn Marino #if defined (SIGTRAP)
93*e4b17023SJohn Marino ENTRY(SIGTRAP, "SIGTRAP", "Trace/breakpoint trap"),
94*e4b17023SJohn Marino #endif
95*e4b17023SJohn Marino /* Put SIGIOT before SIGABRT, so that if SIGIOT==SIGABRT then SIGABRT
96*e4b17023SJohn Marino overrides SIGIOT. SIGABRT is in ANSI and POSIX.1, and SIGIOT isn't. */
97*e4b17023SJohn Marino #if defined (SIGIOT)
98*e4b17023SJohn Marino ENTRY(SIGIOT, "SIGIOT", "IOT trap"),
99*e4b17023SJohn Marino #endif
100*e4b17023SJohn Marino #if defined (SIGABRT)
101*e4b17023SJohn Marino ENTRY(SIGABRT, "SIGABRT", "Aborted"),
102*e4b17023SJohn Marino #endif
103*e4b17023SJohn Marino #if defined (SIGEMT)
104*e4b17023SJohn Marino ENTRY(SIGEMT, "SIGEMT", "Emulation trap"),
105*e4b17023SJohn Marino #endif
106*e4b17023SJohn Marino #if defined (SIGFPE)
107*e4b17023SJohn Marino ENTRY(SIGFPE, "SIGFPE", "Arithmetic exception"),
108*e4b17023SJohn Marino #endif
109*e4b17023SJohn Marino #if defined (SIGKILL)
110*e4b17023SJohn Marino ENTRY(SIGKILL, "SIGKILL", "Killed"),
111*e4b17023SJohn Marino #endif
112*e4b17023SJohn Marino #if defined (SIGBUS)
113*e4b17023SJohn Marino ENTRY(SIGBUS, "SIGBUS", "Bus error"),
114*e4b17023SJohn Marino #endif
115*e4b17023SJohn Marino #if defined (SIGSEGV)
116*e4b17023SJohn Marino ENTRY(SIGSEGV, "SIGSEGV", "Segmentation fault"),
117*e4b17023SJohn Marino #endif
118*e4b17023SJohn Marino #if defined (SIGSYS)
119*e4b17023SJohn Marino ENTRY(SIGSYS, "SIGSYS", "Bad system call"),
120*e4b17023SJohn Marino #endif
121*e4b17023SJohn Marino #if defined (SIGPIPE)
122*e4b17023SJohn Marino ENTRY(SIGPIPE, "SIGPIPE", "Broken pipe"),
123*e4b17023SJohn Marino #endif
124*e4b17023SJohn Marino #if defined (SIGALRM)
125*e4b17023SJohn Marino ENTRY(SIGALRM, "SIGALRM", "Alarm clock"),
126*e4b17023SJohn Marino #endif
127*e4b17023SJohn Marino #if defined (SIGTERM)
128*e4b17023SJohn Marino ENTRY(SIGTERM, "SIGTERM", "Terminated"),
129*e4b17023SJohn Marino #endif
130*e4b17023SJohn Marino #if defined (SIGUSR1)
131*e4b17023SJohn Marino ENTRY(SIGUSR1, "SIGUSR1", "User defined signal 1"),
132*e4b17023SJohn Marino #endif
133*e4b17023SJohn Marino #if defined (SIGUSR2)
134*e4b17023SJohn Marino ENTRY(SIGUSR2, "SIGUSR2", "User defined signal 2"),
135*e4b17023SJohn Marino #endif
136*e4b17023SJohn Marino /* Put SIGCLD before SIGCHLD, so that if SIGCLD==SIGCHLD then SIGCHLD
137*e4b17023SJohn Marino overrides SIGCLD. SIGCHLD is in POXIX.1 */
138*e4b17023SJohn Marino #if defined (SIGCLD)
139*e4b17023SJohn Marino ENTRY(SIGCLD, "SIGCLD", "Child status changed"),
140*e4b17023SJohn Marino #endif
141*e4b17023SJohn Marino #if defined (SIGCHLD)
142*e4b17023SJohn Marino ENTRY(SIGCHLD, "SIGCHLD", "Child status changed"),
143*e4b17023SJohn Marino #endif
144*e4b17023SJohn Marino #if defined (SIGPWR)
145*e4b17023SJohn Marino ENTRY(SIGPWR, "SIGPWR", "Power fail/restart"),
146*e4b17023SJohn Marino #endif
147*e4b17023SJohn Marino #if defined (SIGWINCH)
148*e4b17023SJohn Marino ENTRY(SIGWINCH, "SIGWINCH", "Window size changed"),
149*e4b17023SJohn Marino #endif
150*e4b17023SJohn Marino #if defined (SIGURG)
151*e4b17023SJohn Marino ENTRY(SIGURG, "SIGURG", "Urgent I/O condition"),
152*e4b17023SJohn Marino #endif
153*e4b17023SJohn Marino #if defined (SIGIO)
154*e4b17023SJohn Marino /* "I/O pending" has also been suggested, but is misleading since the
155*e4b17023SJohn Marino signal only happens when the process has asked for it, not everytime
156*e4b17023SJohn Marino I/O is pending. */
157*e4b17023SJohn Marino ENTRY(SIGIO, "SIGIO", "I/O possible"),
158*e4b17023SJohn Marino #endif
159*e4b17023SJohn Marino #if defined (SIGPOLL)
160*e4b17023SJohn Marino ENTRY(SIGPOLL, "SIGPOLL", "Pollable event occurred"),
161*e4b17023SJohn Marino #endif
162*e4b17023SJohn Marino #if defined (SIGSTOP)
163*e4b17023SJohn Marino ENTRY(SIGSTOP, "SIGSTOP", "Stopped (signal)"),
164*e4b17023SJohn Marino #endif
165*e4b17023SJohn Marino #if defined (SIGTSTP)
166*e4b17023SJohn Marino ENTRY(SIGTSTP, "SIGTSTP", "Stopped (user)"),
167*e4b17023SJohn Marino #endif
168*e4b17023SJohn Marino #if defined (SIGCONT)
169*e4b17023SJohn Marino ENTRY(SIGCONT, "SIGCONT", "Continued"),
170*e4b17023SJohn Marino #endif
171*e4b17023SJohn Marino #if defined (SIGTTIN)
172*e4b17023SJohn Marino ENTRY(SIGTTIN, "SIGTTIN", "Stopped (tty input)"),
173*e4b17023SJohn Marino #endif
174*e4b17023SJohn Marino #if defined (SIGTTOU)
175*e4b17023SJohn Marino ENTRY(SIGTTOU, "SIGTTOU", "Stopped (tty output)"),
176*e4b17023SJohn Marino #endif
177*e4b17023SJohn Marino #if defined (SIGVTALRM)
178*e4b17023SJohn Marino ENTRY(SIGVTALRM, "SIGVTALRM", "Virtual timer expired"),
179*e4b17023SJohn Marino #endif
180*e4b17023SJohn Marino #if defined (SIGPROF)
181*e4b17023SJohn Marino ENTRY(SIGPROF, "SIGPROF", "Profiling timer expired"),
182*e4b17023SJohn Marino #endif
183*e4b17023SJohn Marino #if defined (SIGXCPU)
184*e4b17023SJohn Marino ENTRY(SIGXCPU, "SIGXCPU", "CPU time limit exceeded"),
185*e4b17023SJohn Marino #endif
186*e4b17023SJohn Marino #if defined (SIGXFSZ)
187*e4b17023SJohn Marino ENTRY(SIGXFSZ, "SIGXFSZ", "File size limit exceeded"),
188*e4b17023SJohn Marino #endif
189*e4b17023SJohn Marino #if defined (SIGWIND)
190*e4b17023SJohn Marino ENTRY(SIGWIND, "SIGWIND", "SIGWIND"),
191*e4b17023SJohn Marino #endif
192*e4b17023SJohn Marino #if defined (SIGPHONE)
193*e4b17023SJohn Marino ENTRY(SIGPHONE, "SIGPHONE", "SIGPHONE"),
194*e4b17023SJohn Marino #endif
195*e4b17023SJohn Marino #if defined (SIGLOST)
196*e4b17023SJohn Marino ENTRY(SIGLOST, "SIGLOST", "Resource lost"),
197*e4b17023SJohn Marino #endif
198*e4b17023SJohn Marino #if defined (SIGWAITING)
199*e4b17023SJohn Marino ENTRY(SIGWAITING, "SIGWAITING", "Process's LWPs are blocked"),
200*e4b17023SJohn Marino #endif
201*e4b17023SJohn Marino #if defined (SIGLWP)
202*e4b17023SJohn Marino ENTRY(SIGLWP, "SIGLWP", "Signal LWP"),
203*e4b17023SJohn Marino #endif
204*e4b17023SJohn Marino #if defined (SIGDANGER)
205*e4b17023SJohn Marino ENTRY(SIGDANGER, "SIGDANGER", "Swap space dangerously low"),
206*e4b17023SJohn Marino #endif
207*e4b17023SJohn Marino #if defined (SIGGRANT)
208*e4b17023SJohn Marino ENTRY(SIGGRANT, "SIGGRANT", "Monitor mode granted"),
209*e4b17023SJohn Marino #endif
210*e4b17023SJohn Marino #if defined (SIGRETRACT)
211*e4b17023SJohn Marino ENTRY(SIGRETRACT, "SIGRETRACT", "Need to relinguish monitor mode"),
212*e4b17023SJohn Marino #endif
213*e4b17023SJohn Marino #if defined (SIGMSG)
214*e4b17023SJohn Marino ENTRY(SIGMSG, "SIGMSG", "Monitor mode data available"),
215*e4b17023SJohn Marino #endif
216*e4b17023SJohn Marino #if defined (SIGSOUND)
217*e4b17023SJohn Marino ENTRY(SIGSOUND, "SIGSOUND", "Sound completed"),
218*e4b17023SJohn Marino #endif
219*e4b17023SJohn Marino #if defined (SIGSAK)
220*e4b17023SJohn Marino ENTRY(SIGSAK, "SIGSAK", "Secure attention"),
221*e4b17023SJohn Marino #endif
222*e4b17023SJohn Marino ENTRY(0, NULL, NULL)
223*e4b17023SJohn Marino };
224*e4b17023SJohn Marino
225*e4b17023SJohn Marino /* Translation table allocated and initialized at runtime. Indexed by the
226*e4b17023SJohn Marino signal value to find the equivalent symbolic value. */
227*e4b17023SJohn Marino
228*e4b17023SJohn Marino static const char **signal_names;
229*e4b17023SJohn Marino static int num_signal_names = 0;
230*e4b17023SJohn Marino
231*e4b17023SJohn Marino /* Translation table allocated and initialized at runtime, if it does not
232*e4b17023SJohn Marino already exist in the host environment. Indexed by the signal value to find
233*e4b17023SJohn Marino the descriptive string.
234*e4b17023SJohn Marino
235*e4b17023SJohn Marino We don't export it for use in other modules because even though it has the
236*e4b17023SJohn Marino same name, it differs from other implementations in that it is dynamically
237*e4b17023SJohn Marino initialized rather than statically initialized. */
238*e4b17023SJohn Marino
239*e4b17023SJohn Marino #ifndef HAVE_SYS_SIGLIST
240*e4b17023SJohn Marino
241*e4b17023SJohn Marino static int sys_nsig;
242*e4b17023SJohn Marino static const char **sys_siglist;
243*e4b17023SJohn Marino
244*e4b17023SJohn Marino #else
245*e4b17023SJohn Marino
246*e4b17023SJohn Marino #ifdef NSIG
247*e4b17023SJohn Marino static int sys_nsig = NSIG;
248*e4b17023SJohn Marino #else
249*e4b17023SJohn Marino #ifdef _NSIG
250*e4b17023SJohn Marino static int sys_nsig = _NSIG;
251*e4b17023SJohn Marino #endif
252*e4b17023SJohn Marino #endif
253*e4b17023SJohn Marino extern const char * const sys_siglist[];
254*e4b17023SJohn Marino
255*e4b17023SJohn Marino #endif
256*e4b17023SJohn Marino
257*e4b17023SJohn Marino
258*e4b17023SJohn Marino /*
259*e4b17023SJohn Marino
260*e4b17023SJohn Marino NAME
261*e4b17023SJohn Marino
262*e4b17023SJohn Marino init_signal_tables -- initialize the name and message tables
263*e4b17023SJohn Marino
264*e4b17023SJohn Marino SYNOPSIS
265*e4b17023SJohn Marino
266*e4b17023SJohn Marino static void init_signal_tables ();
267*e4b17023SJohn Marino
268*e4b17023SJohn Marino DESCRIPTION
269*e4b17023SJohn Marino
270*e4b17023SJohn Marino Using the signal_table, which is initialized at compile time, generate
271*e4b17023SJohn Marino the signal_names and the sys_siglist (if needed) tables, which are
272*e4b17023SJohn Marino indexed at runtime by a specific signal value.
273*e4b17023SJohn Marino
274*e4b17023SJohn Marino BUGS
275*e4b17023SJohn Marino
276*e4b17023SJohn Marino The initialization of the tables may fail under low memory conditions,
277*e4b17023SJohn Marino in which case we don't do anything particularly useful, but we don't
278*e4b17023SJohn Marino bomb either. Who knows, it might succeed at a later point if we free
279*e4b17023SJohn Marino some memory in the meantime. In any case, the other routines know
280*e4b17023SJohn Marino how to deal with lack of a table after trying to initialize it. This
281*e4b17023SJohn Marino may or may not be considered to be a bug, that we don't specifically
282*e4b17023SJohn Marino warn about this particular failure mode.
283*e4b17023SJohn Marino
284*e4b17023SJohn Marino */
285*e4b17023SJohn Marino
286*e4b17023SJohn Marino static void
init_signal_tables(void)287*e4b17023SJohn Marino init_signal_tables (void)
288*e4b17023SJohn Marino {
289*e4b17023SJohn Marino const struct signal_info *eip;
290*e4b17023SJohn Marino int nbytes;
291*e4b17023SJohn Marino
292*e4b17023SJohn Marino /* If we haven't already scanned the signal_table once to find the maximum
293*e4b17023SJohn Marino signal value, then go find it now. */
294*e4b17023SJohn Marino
295*e4b17023SJohn Marino if (num_signal_names == 0)
296*e4b17023SJohn Marino {
297*e4b17023SJohn Marino for (eip = signal_table; eip -> name != NULL; eip++)
298*e4b17023SJohn Marino {
299*e4b17023SJohn Marino if (eip -> value >= num_signal_names)
300*e4b17023SJohn Marino {
301*e4b17023SJohn Marino num_signal_names = eip -> value + 1;
302*e4b17023SJohn Marino }
303*e4b17023SJohn Marino }
304*e4b17023SJohn Marino }
305*e4b17023SJohn Marino
306*e4b17023SJohn Marino /* Now attempt to allocate the signal_names table, zero it out, and then
307*e4b17023SJohn Marino initialize it from the statically initialized signal_table. */
308*e4b17023SJohn Marino
309*e4b17023SJohn Marino if (signal_names == NULL)
310*e4b17023SJohn Marino {
311*e4b17023SJohn Marino nbytes = num_signal_names * sizeof (char *);
312*e4b17023SJohn Marino if ((signal_names = (const char **) malloc (nbytes)) != NULL)
313*e4b17023SJohn Marino {
314*e4b17023SJohn Marino memset (signal_names, 0, nbytes);
315*e4b17023SJohn Marino for (eip = signal_table; eip -> name != NULL; eip++)
316*e4b17023SJohn Marino {
317*e4b17023SJohn Marino signal_names[eip -> value] = eip -> name;
318*e4b17023SJohn Marino }
319*e4b17023SJohn Marino }
320*e4b17023SJohn Marino }
321*e4b17023SJohn Marino
322*e4b17023SJohn Marino #ifndef HAVE_SYS_SIGLIST
323*e4b17023SJohn Marino
324*e4b17023SJohn Marino /* Now attempt to allocate the sys_siglist table, zero it out, and then
325*e4b17023SJohn Marino initialize it from the statically initialized signal_table. */
326*e4b17023SJohn Marino
327*e4b17023SJohn Marino if (sys_siglist == NULL)
328*e4b17023SJohn Marino {
329*e4b17023SJohn Marino nbytes = num_signal_names * sizeof (char *);
330*e4b17023SJohn Marino if ((sys_siglist = (const char **) malloc (nbytes)) != NULL)
331*e4b17023SJohn Marino {
332*e4b17023SJohn Marino memset (sys_siglist, 0, nbytes);
333*e4b17023SJohn Marino sys_nsig = num_signal_names;
334*e4b17023SJohn Marino for (eip = signal_table; eip -> name != NULL; eip++)
335*e4b17023SJohn Marino {
336*e4b17023SJohn Marino sys_siglist[eip -> value] = eip -> msg;
337*e4b17023SJohn Marino }
338*e4b17023SJohn Marino }
339*e4b17023SJohn Marino }
340*e4b17023SJohn Marino
341*e4b17023SJohn Marino #endif
342*e4b17023SJohn Marino
343*e4b17023SJohn Marino }
344*e4b17023SJohn Marino
345*e4b17023SJohn Marino
346*e4b17023SJohn Marino /*
347*e4b17023SJohn Marino
348*e4b17023SJohn Marino @deftypefn Extension int signo_max (void)
349*e4b17023SJohn Marino
350*e4b17023SJohn Marino Returns the maximum signal value for which a corresponding symbolic
351*e4b17023SJohn Marino name or message is available. Note that in the case where we use the
352*e4b17023SJohn Marino @code{sys_siglist} supplied by the system, it is possible for there to
353*e4b17023SJohn Marino be more symbolic names than messages, or vice versa. In fact, the
354*e4b17023SJohn Marino manual page for @code{psignal(3b)} explicitly warns that one should
355*e4b17023SJohn Marino check the size of the table (@code{NSIG}) before indexing it, since
356*e4b17023SJohn Marino new signal codes may be added to the system before they are added to
357*e4b17023SJohn Marino the table. Thus @code{NSIG} might be smaller than value implied by
358*e4b17023SJohn Marino the largest signo value defined in @code{<signal.h>}.
359*e4b17023SJohn Marino
360*e4b17023SJohn Marino We return the maximum value that can be used to obtain a meaningful
361*e4b17023SJohn Marino symbolic name or message.
362*e4b17023SJohn Marino
363*e4b17023SJohn Marino @end deftypefn
364*e4b17023SJohn Marino
365*e4b17023SJohn Marino */
366*e4b17023SJohn Marino
367*e4b17023SJohn Marino int
signo_max(void)368*e4b17023SJohn Marino signo_max (void)
369*e4b17023SJohn Marino {
370*e4b17023SJohn Marino int maxsize;
371*e4b17023SJohn Marino
372*e4b17023SJohn Marino if (signal_names == NULL)
373*e4b17023SJohn Marino {
374*e4b17023SJohn Marino init_signal_tables ();
375*e4b17023SJohn Marino }
376*e4b17023SJohn Marino maxsize = MAX (sys_nsig, num_signal_names);
377*e4b17023SJohn Marino return (maxsize - 1);
378*e4b17023SJohn Marino }
379*e4b17023SJohn Marino
380*e4b17023SJohn Marino
381*e4b17023SJohn Marino /*
382*e4b17023SJohn Marino
383*e4b17023SJohn Marino @deftypefn Supplemental {const char *} strsignal (int @var{signo})
384*e4b17023SJohn Marino
385*e4b17023SJohn Marino Maps an signal number to an signal message string, the contents of
386*e4b17023SJohn Marino which are implementation defined. On systems which have the external
387*e4b17023SJohn Marino variable @code{sys_siglist}, these strings will be the same as the
388*e4b17023SJohn Marino ones used by @code{psignal()}.
389*e4b17023SJohn Marino
390*e4b17023SJohn Marino If the supplied signal number is within the valid range of indices for
391*e4b17023SJohn Marino the @code{sys_siglist}, but no message is available for the particular
392*e4b17023SJohn Marino signal number, then returns the string @samp{Signal @var{num}}, where
393*e4b17023SJohn Marino @var{num} is the signal number.
394*e4b17023SJohn Marino
395*e4b17023SJohn Marino If the supplied signal number is not a valid index into
396*e4b17023SJohn Marino @code{sys_siglist}, returns @code{NULL}.
397*e4b17023SJohn Marino
398*e4b17023SJohn Marino The returned string is only guaranteed to be valid only until the next
399*e4b17023SJohn Marino call to @code{strsignal}.
400*e4b17023SJohn Marino
401*e4b17023SJohn Marino @end deftypefn
402*e4b17023SJohn Marino
403*e4b17023SJohn Marino */
404*e4b17023SJohn Marino
405*e4b17023SJohn Marino #ifndef HAVE_STRSIGNAL
406*e4b17023SJohn Marino
407*e4b17023SJohn Marino char *
strsignal(int signo)408*e4b17023SJohn Marino strsignal (int signo)
409*e4b17023SJohn Marino {
410*e4b17023SJohn Marino char *msg;
411*e4b17023SJohn Marino static char buf[32];
412*e4b17023SJohn Marino
413*e4b17023SJohn Marino #ifndef HAVE_SYS_SIGLIST
414*e4b17023SJohn Marino
415*e4b17023SJohn Marino if (signal_names == NULL)
416*e4b17023SJohn Marino {
417*e4b17023SJohn Marino init_signal_tables ();
418*e4b17023SJohn Marino }
419*e4b17023SJohn Marino
420*e4b17023SJohn Marino #endif
421*e4b17023SJohn Marino
422*e4b17023SJohn Marino if ((signo < 0) || (signo >= sys_nsig))
423*e4b17023SJohn Marino {
424*e4b17023SJohn Marino /* Out of range, just return NULL */
425*e4b17023SJohn Marino msg = NULL;
426*e4b17023SJohn Marino }
427*e4b17023SJohn Marino else if ((sys_siglist == NULL) || (sys_siglist[signo] == NULL))
428*e4b17023SJohn Marino {
429*e4b17023SJohn Marino /* In range, but no sys_siglist or no entry at this index. */
430*e4b17023SJohn Marino sprintf (buf, "Signal %d", signo);
431*e4b17023SJohn Marino msg = buf;
432*e4b17023SJohn Marino }
433*e4b17023SJohn Marino else
434*e4b17023SJohn Marino {
435*e4b17023SJohn Marino /* In range, and a valid message. Just return the message. We
436*e4b17023SJohn Marino can safely cast away const, since POSIX says the user must
437*e4b17023SJohn Marino not modify the result. */
438*e4b17023SJohn Marino msg = (char *) sys_siglist[signo];
439*e4b17023SJohn Marino }
440*e4b17023SJohn Marino
441*e4b17023SJohn Marino return (msg);
442*e4b17023SJohn Marino }
443*e4b17023SJohn Marino
444*e4b17023SJohn Marino #endif /* ! HAVE_STRSIGNAL */
445*e4b17023SJohn Marino
446*e4b17023SJohn Marino /*
447*e4b17023SJohn Marino
448*e4b17023SJohn Marino @deftypefn Extension {const char*} strsigno (int @var{signo})
449*e4b17023SJohn Marino
450*e4b17023SJohn Marino Given an signal number, returns a pointer to a string containing the
451*e4b17023SJohn Marino symbolic name of that signal number, as found in @code{<signal.h>}.
452*e4b17023SJohn Marino
453*e4b17023SJohn Marino If the supplied signal number is within the valid range of indices for
454*e4b17023SJohn Marino symbolic names, but no name is available for the particular signal
455*e4b17023SJohn Marino number, then returns the string @samp{Signal @var{num}}, where
456*e4b17023SJohn Marino @var{num} is the signal number.
457*e4b17023SJohn Marino
458*e4b17023SJohn Marino If the supplied signal number is not within the range of valid
459*e4b17023SJohn Marino indices, then returns @code{NULL}.
460*e4b17023SJohn Marino
461*e4b17023SJohn Marino The contents of the location pointed to are only guaranteed to be
462*e4b17023SJohn Marino valid until the next call to @code{strsigno}.
463*e4b17023SJohn Marino
464*e4b17023SJohn Marino @end deftypefn
465*e4b17023SJohn Marino
466*e4b17023SJohn Marino */
467*e4b17023SJohn Marino
468*e4b17023SJohn Marino const char *
strsigno(int signo)469*e4b17023SJohn Marino strsigno (int signo)
470*e4b17023SJohn Marino {
471*e4b17023SJohn Marino const char *name;
472*e4b17023SJohn Marino static char buf[32];
473*e4b17023SJohn Marino
474*e4b17023SJohn Marino if (signal_names == NULL)
475*e4b17023SJohn Marino {
476*e4b17023SJohn Marino init_signal_tables ();
477*e4b17023SJohn Marino }
478*e4b17023SJohn Marino
479*e4b17023SJohn Marino if ((signo < 0) || (signo >= num_signal_names))
480*e4b17023SJohn Marino {
481*e4b17023SJohn Marino /* Out of range, just return NULL */
482*e4b17023SJohn Marino name = NULL;
483*e4b17023SJohn Marino }
484*e4b17023SJohn Marino else if ((signal_names == NULL) || (signal_names[signo] == NULL))
485*e4b17023SJohn Marino {
486*e4b17023SJohn Marino /* In range, but no signal_names or no entry at this index. */
487*e4b17023SJohn Marino sprintf (buf, "Signal %d", signo);
488*e4b17023SJohn Marino name = (const char *) buf;
489*e4b17023SJohn Marino }
490*e4b17023SJohn Marino else
491*e4b17023SJohn Marino {
492*e4b17023SJohn Marino /* In range, and a valid name. Just return the name. */
493*e4b17023SJohn Marino name = signal_names[signo];
494*e4b17023SJohn Marino }
495*e4b17023SJohn Marino
496*e4b17023SJohn Marino return (name);
497*e4b17023SJohn Marino }
498*e4b17023SJohn Marino
499*e4b17023SJohn Marino
500*e4b17023SJohn Marino /*
501*e4b17023SJohn Marino
502*e4b17023SJohn Marino @deftypefn Extension int strtosigno (const char *@var{name})
503*e4b17023SJohn Marino
504*e4b17023SJohn Marino Given the symbolic name of a signal, map it to a signal number. If no
505*e4b17023SJohn Marino translation is found, returns 0.
506*e4b17023SJohn Marino
507*e4b17023SJohn Marino @end deftypefn
508*e4b17023SJohn Marino
509*e4b17023SJohn Marino */
510*e4b17023SJohn Marino
511*e4b17023SJohn Marino int
strtosigno(const char * name)512*e4b17023SJohn Marino strtosigno (const char *name)
513*e4b17023SJohn Marino {
514*e4b17023SJohn Marino int signo = 0;
515*e4b17023SJohn Marino
516*e4b17023SJohn Marino if (name != NULL)
517*e4b17023SJohn Marino {
518*e4b17023SJohn Marino if (signal_names == NULL)
519*e4b17023SJohn Marino {
520*e4b17023SJohn Marino init_signal_tables ();
521*e4b17023SJohn Marino }
522*e4b17023SJohn Marino for (signo = 0; signo < num_signal_names; signo++)
523*e4b17023SJohn Marino {
524*e4b17023SJohn Marino if ((signal_names[signo] != NULL) &&
525*e4b17023SJohn Marino (strcmp (name, signal_names[signo]) == 0))
526*e4b17023SJohn Marino {
527*e4b17023SJohn Marino break;
528*e4b17023SJohn Marino }
529*e4b17023SJohn Marino }
530*e4b17023SJohn Marino if (signo == num_signal_names)
531*e4b17023SJohn Marino {
532*e4b17023SJohn Marino signo = 0;
533*e4b17023SJohn Marino }
534*e4b17023SJohn Marino }
535*e4b17023SJohn Marino return (signo);
536*e4b17023SJohn Marino }
537*e4b17023SJohn Marino
538*e4b17023SJohn Marino
539*e4b17023SJohn Marino /*
540*e4b17023SJohn Marino
541*e4b17023SJohn Marino @deftypefn Supplemental void psignal (int @var{signo}, char *@var{message})
542*e4b17023SJohn Marino
543*e4b17023SJohn Marino Print @var{message} to the standard error, followed by a colon,
544*e4b17023SJohn Marino followed by the description of the signal specified by @var{signo},
545*e4b17023SJohn Marino followed by a newline.
546*e4b17023SJohn Marino
547*e4b17023SJohn Marino @end deftypefn
548*e4b17023SJohn Marino
549*e4b17023SJohn Marino */
550*e4b17023SJohn Marino
551*e4b17023SJohn Marino #ifndef HAVE_PSIGNAL
552*e4b17023SJohn Marino
553*e4b17023SJohn Marino void
psignal(int signo,char * message)554*e4b17023SJohn Marino psignal (int signo, char *message)
555*e4b17023SJohn Marino {
556*e4b17023SJohn Marino if (signal_names == NULL)
557*e4b17023SJohn Marino {
558*e4b17023SJohn Marino init_signal_tables ();
559*e4b17023SJohn Marino }
560*e4b17023SJohn Marino if ((signo <= 0) || (signo >= sys_nsig))
561*e4b17023SJohn Marino {
562*e4b17023SJohn Marino fprintf (stderr, "%s: unknown signal\n", message);
563*e4b17023SJohn Marino }
564*e4b17023SJohn Marino else
565*e4b17023SJohn Marino {
566*e4b17023SJohn Marino fprintf (stderr, "%s: %s\n", message, sys_siglist[signo]);
567*e4b17023SJohn Marino }
568*e4b17023SJohn Marino }
569*e4b17023SJohn Marino
570*e4b17023SJohn Marino #endif /* ! HAVE_PSIGNAL */
571*e4b17023SJohn Marino
572*e4b17023SJohn Marino
573*e4b17023SJohn Marino /* A simple little main that does nothing but print all the signal translations
574*e4b17023SJohn Marino if MAIN is defined and this file is compiled and linked. */
575*e4b17023SJohn Marino
576*e4b17023SJohn Marino #ifdef MAIN
577*e4b17023SJohn Marino
578*e4b17023SJohn Marino #include <stdio.h>
579*e4b17023SJohn Marino
580*e4b17023SJohn Marino int
main(void)581*e4b17023SJohn Marino main (void)
582*e4b17023SJohn Marino {
583*e4b17023SJohn Marino int signo;
584*e4b17023SJohn Marino int maxsigno;
585*e4b17023SJohn Marino const char *name;
586*e4b17023SJohn Marino const char *msg;
587*e4b17023SJohn Marino
588*e4b17023SJohn Marino maxsigno = signo_max ();
589*e4b17023SJohn Marino printf ("%d entries in names table.\n", num_signal_names);
590*e4b17023SJohn Marino printf ("%d entries in messages table.\n", sys_nsig);
591*e4b17023SJohn Marino printf ("%d is max useful index.\n", maxsigno);
592*e4b17023SJohn Marino
593*e4b17023SJohn Marino /* Keep printing values until we get to the end of *both* tables, not
594*e4b17023SJohn Marino *either* table. Note that knowing the maximum useful index does *not*
595*e4b17023SJohn Marino relieve us of the responsibility of testing the return pointer for
596*e4b17023SJohn Marino NULL. */
597*e4b17023SJohn Marino
598*e4b17023SJohn Marino for (signo = 0; signo <= maxsigno; signo++)
599*e4b17023SJohn Marino {
600*e4b17023SJohn Marino name = strsigno (signo);
601*e4b17023SJohn Marino name = (name == NULL) ? "<NULL>" : name;
602*e4b17023SJohn Marino msg = strsignal (signo);
603*e4b17023SJohn Marino msg = (msg == NULL) ? "<NULL>" : msg;
604*e4b17023SJohn Marino printf ("%-4d%-18s%s\n", signo, name, msg);
605*e4b17023SJohn Marino }
606*e4b17023SJohn Marino
607*e4b17023SJohn Marino return 0;
608*e4b17023SJohn Marino }
609*e4b17023SJohn Marino
610*e4b17023SJohn Marino #endif
611