Bash-4.3 distribution sources and documentation
[platform/upstream/bash.git] / support / signames.c
1 /* signames.c -- Create an array of signal names. */
2
3 /* Copyright (C) 2006 Free Software Foundation, Inc.
4
5    This file is part of GNU Bash, the Bourne Again SHell.
6
7    Bash is free software: you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation, either version 3 of the License, or
10    (at your option) any later version.
11
12    Bash is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with Bash.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <config.h>
22
23 #include <stdio.h>
24
25 #include <sys/types.h>
26 #include <signal.h>
27
28 #if defined (HAVE_STDLIB_H)
29 #  include <stdlib.h>
30 #else
31 #  include "ansi_stdlib.h"
32 #endif /* HAVE_STDLIB_H */
33
34 #if !defined (NSIG)
35 #  define NSIG 64
36 #endif
37
38 /*
39  * Special traps:
40  *      EXIT == 0
41  *      DEBUG == NSIG
42  *      ERR == NSIG+1
43  *      RETURN == NSIG+2
44  */
45 #define LASTSIG NSIG+2
46
47 char *signal_names[2 * (LASTSIG)];
48
49 #define signal_names_size (sizeof(signal_names)/sizeof(signal_names[0]))
50
51 /* AIX 4.3 defines SIGRTMIN and SIGRTMAX as 888 and 999 respectively.
52    I don't want to allocate so much unused space for the intervening signal
53    numbers, so we just punt if SIGRTMAX is past the bounds of the
54    signal_names array (handled in configure). */
55 #if defined (SIGRTMAX) && defined (UNUSABLE_RT_SIGNALS)
56 #  undef SIGRTMAX
57 #  undef SIGRTMIN
58 #endif
59
60 #if defined (SIGRTMAX) || defined (SIGRTMIN)
61 #  define RTLEN 14
62 #  define RTLIM 256
63 #endif
64
65 #if defined (BUILDTOOL)
66 extern char *progname;
67 #endif
68
69 void
70 initialize_signames ()
71 {
72   register int i;
73 #if defined (SIGRTMAX) || defined (SIGRTMIN)
74   int rtmin, rtmax, rtcnt;
75 #endif
76
77   for (i = 1; i < signal_names_size; i++)
78     signal_names[i] = (char *)NULL;
79
80   /* `signal' 0 is what we do on exit. */
81   signal_names[0] = "EXIT";
82
83   /* Place signal names which can be aliases for more common signal
84      names first.  This allows (for example) SIGABRT to overwrite SIGLOST. */
85
86   /* POSIX 1003.1b-1993 real time signals, but take care of incomplete
87      implementations. Acoording to the standard, both, SIGRTMIN and
88      SIGRTMAX must be defined, SIGRTMIN must be strictly less than
89      SIGRTMAX, and the difference must be at least 7, that is, there
90      must be at least eight distinct real time signals. */
91
92   /* The generated signal names are SIGRTMIN, SIGRTMIN+1, ...,
93      SIGRTMIN+x, SIGRTMAX-x, ..., SIGRTMAX-1, SIGRTMAX. If the number
94      of RT signals is odd, there is an extra SIGRTMIN+(x+1).
95      These names are the ones used by ksh and /usr/xpg4/bin/sh on SunOS5. */
96
97 #if defined (SIGRTMIN)
98   rtmin = SIGRTMIN;
99   signal_names[rtmin] = "SIGRTMIN";
100 #endif
101
102 #if defined (SIGRTMAX)
103   rtmax = SIGRTMAX;
104   signal_names[rtmax] = "SIGRTMAX";
105 #endif
106
107 #if defined (SIGRTMAX) && defined (SIGRTMIN)
108   if (rtmax > rtmin)
109     {
110       rtcnt = (rtmax - rtmin - 1) / 2;
111       /* croak if there are too many RT signals */
112       if (rtcnt >= RTLIM/2)
113         {
114           rtcnt = RTLIM/2-1;
115 #ifdef BUILDTOOL
116           fprintf(stderr, "%s: error: more than %d real time signals, fix `%s'\n",
117                   progname, RTLIM, progname);
118 #endif
119         }
120
121       for (i = 1; i <= rtcnt; i++)
122         {
123           signal_names[rtmin+i] = (char *)malloc(RTLEN);
124           if (signal_names[rtmin+i])
125             sprintf (signal_names[rtmin+i], "SIGRTMIN+%d", i);
126           signal_names[rtmax-i] = (char *)malloc(RTLEN);
127           if (signal_names[rtmax-i])
128             sprintf (signal_names[rtmax-i], "SIGRTMAX-%d", i);
129         }
130
131       if (rtcnt < RTLIM/2-1 && rtcnt != (rtmax-rtmin)/2)
132         {
133           /* Need an extra RTMIN signal */
134           signal_names[rtmin+rtcnt+1] = (char *)malloc(RTLEN);
135           if (signal_names[rtmin+rtcnt+1])
136             sprintf (signal_names[rtmin+rtcnt+1], "SIGRTMIN+%d", rtcnt+1);
137         }
138     }
139 #endif /* SIGRTMIN && SIGRTMAX */
140
141 #if defined (SIGLOST)   /* resource lost (eg, record-lock lost) */
142   signal_names[SIGLOST] = "SIGLOST";
143 #endif
144
145 /* AIX */
146 #if defined (SIGMSG)    /* HFT input data pending */
147   signal_names[SIGMSG] = "SIGMSG";
148 #endif
149
150 #if defined (SIGDANGER) /* system crash imminent */
151   signal_names[SIGDANGER] = "SIGDANGER";
152 #endif
153
154 #if defined (SIGMIGRATE) /* migrate process to another CPU */
155   signal_names[SIGMIGRATE] = "SIGMIGRATE";
156 #endif
157
158 #if defined (SIGPRE)    /* programming error */
159   signal_names[SIGPRE] = "SIGPRE";
160 #endif
161
162 #if defined (SIGVIRT)   /* AIX virtual time alarm */
163   signal_names[SIGVIRT] = "SIGVIRT";
164 #endif
165
166 #if defined (SIGALRM1)  /* m:n condition variables */
167   signal_names[SIGALRM1] = "SIGALRM1";
168 #endif
169
170 #if defined (SIGWAITING)        /* m:n scheduling */
171   signal_names[SIGWAITING] = "SIGWAITING";
172 #endif
173
174 #if defined (SIGGRANT)  /* HFT monitor mode granted */
175   signal_names[SIGGRANT] = "SIGGRANT";
176 #endif
177
178 #if defined (SIGKAP)    /* keep alive poll from native keyboard */
179   signal_names[SIGKAP] = "SIGKAP";
180 #endif
181
182 #if defined (SIGRETRACT) /* HFT monitor mode retracted */
183   signal_names[SIGRETRACT] = "SIGRETRACT";
184 #endif
185
186 #if defined (SIGSOUND)  /* HFT sound sequence has completed */
187   signal_names[SIGSOUND] = "SIGSOUND";
188 #endif
189
190 #if defined (SIGSAK)    /* Secure Attention Key */
191   signal_names[SIGSAK] = "SIGSAK";
192 #endif
193
194 /* SunOS5 */
195 #if defined (SIGLWP)    /* Solaris: special signal used by thread library */
196   signal_names[SIGLWP] = "SIGLWP";
197 #endif
198
199 #if defined (SIGFREEZE) /* Solaris: special signal used by CPR */
200   signal_names[SIGFREEZE] = "SIGFREEZE";
201 #endif
202
203 #if defined (SIGTHAW)   /* Solaris: special signal used by CPR */
204   signal_names[SIGTHAW] = "SIGTHAW";
205 #endif
206
207 #if defined (SIGCANCEL) /* Solaris: thread cancellation signal used by libthread */
208   signal_names[SIGCANCEL] = "SIGCANCEL";
209 #endif
210
211 #if defined (SIGXRES)   /* Solaris: resource control exceeded */
212   signal_names[SIGXRES] = "SIGXRES";
213 #endif
214
215 #if defined (SIGJVM1)   /* Solaris: Java Virtual Machine 1 */
216   signal_names[SIGJVM1] = "SIGJVM1";
217 #endif
218
219 #if defined (SIGJVM2)   /* Solaris: Java Virtual Machine 2 */
220   signal_names[SIGJVM2] = "SIGJVM2";
221 #endif
222
223 /* HP-UX */
224 #if defined (SIGDIL)    /* DIL signal (?) */
225   signal_names[SIGDIL] = "SIGDIL";
226 #endif
227
228 /* System V */
229 #if defined (SIGCLD)    /* Like SIGCHLD.  */
230   signal_names[SIGCLD] = "SIGCLD";
231 #endif
232
233 #if defined (SIGPWR)    /* power state indication */
234   signal_names[SIGPWR] = "SIGPWR";
235 #endif
236
237 #if defined (SIGPOLL)   /* Pollable event (for streams)  */
238   signal_names[SIGPOLL] = "SIGPOLL";
239 #endif
240
241 /* Unknown */
242 #if defined (SIGWINDOW)
243   signal_names[SIGWINDOW] = "SIGWINDOW";
244 #endif
245
246 /* Linux */
247 #if defined (SIGSTKFLT)
248   signal_names[SIGSTKFLT] = "SIGSTKFLT";
249 #endif
250
251 /* FreeBSD */
252 #if defined (SIGTHR)    /* thread interrupt */
253   signal_names[SIGTHR] = "SIGTHR";
254 #endif
255
256 /* Common */
257 #if defined (SIGHUP)    /* hangup */
258   signal_names[SIGHUP] = "SIGHUP";
259 #endif
260
261 #if defined (SIGINT)    /* interrupt */
262   signal_names[SIGINT] = "SIGINT";
263 #endif
264
265 #if defined (SIGQUIT)   /* quit */
266   signal_names[SIGQUIT] = "SIGQUIT";
267 #endif
268
269 #if defined (SIGILL)    /* illegal instruction (not reset when caught) */
270   signal_names[SIGILL] = "SIGILL";
271 #endif
272
273 #if defined (SIGTRAP)   /* trace trap (not reset when caught) */
274   signal_names[SIGTRAP] = "SIGTRAP";
275 #endif
276
277 #if defined (SIGIOT)    /* IOT instruction */
278   signal_names[SIGIOT] = "SIGIOT";
279 #endif
280
281 #if defined (SIGABRT)   /* Cause current process to dump core. */
282   signal_names[SIGABRT] = "SIGABRT";
283 #endif
284
285 #if defined (SIGEMT)    /* EMT instruction */
286   signal_names[SIGEMT] = "SIGEMT";
287 #endif
288
289 #if defined (SIGFPE)    /* floating point exception */
290   signal_names[SIGFPE] = "SIGFPE";
291 #endif
292
293 #if defined (SIGKILL)   /* kill (cannot be caught or ignored) */
294   signal_names[SIGKILL] = "SIGKILL";
295 #endif
296
297 #if defined (SIGBUS)    /* bus error */
298   signal_names[SIGBUS] = "SIGBUS";
299 #endif
300
301 #if defined (SIGSEGV)   /* segmentation violation */
302   signal_names[SIGSEGV] = "SIGSEGV";
303 #endif
304
305 #if defined (SIGSYS)    /* bad argument to system call */
306   signal_names[SIGSYS] = "SIGSYS";
307 #endif
308
309 #if defined (SIGPIPE)   /* write on a pipe with no one to read it */
310   signal_names[SIGPIPE] = "SIGPIPE";
311 #endif
312
313 #if defined (SIGALRM)   /* alarm clock */
314   signal_names[SIGALRM] = "SIGALRM";
315 #endif
316
317 #if defined (SIGTERM)   /* software termination signal from kill */
318   signal_names[SIGTERM] = "SIGTERM";
319 #endif
320
321 #if defined (SIGURG)    /* urgent condition on IO channel */
322   signal_names[SIGURG] = "SIGURG";
323 #endif
324
325 #if defined (SIGSTOP)   /* sendable stop signal not from tty */
326   signal_names[SIGSTOP] = "SIGSTOP";
327 #endif
328
329 #if defined (SIGTSTP)   /* stop signal from tty */
330   signal_names[SIGTSTP] = "SIGTSTP";
331 #endif
332
333 #if defined (SIGCONT)   /* continue a stopped process */
334   signal_names[SIGCONT] = "SIGCONT";
335 #endif
336
337 #if defined (SIGCHLD)   /* to parent on child stop or exit */
338   signal_names[SIGCHLD] = "SIGCHLD";
339 #endif
340
341 #if defined (SIGTTIN)   /* to readers pgrp upon background tty read */
342   signal_names[SIGTTIN] = "SIGTTIN";
343 #endif
344
345 #if defined (SIGTTOU)   /* like TTIN for output if (tp->t_local&LTOSTOP) */
346   signal_names[SIGTTOU] = "SIGTTOU";
347 #endif
348
349 #if defined (SIGIO)     /* input/output possible signal */
350   signal_names[SIGIO] = "SIGIO";
351 #endif
352
353 #if defined (SIGXCPU)   /* exceeded CPU time limit */
354   signal_names[SIGXCPU] = "SIGXCPU";
355 #endif
356
357 #if defined (SIGXFSZ)   /* exceeded file size limit */
358   signal_names[SIGXFSZ] = "SIGXFSZ";
359 #endif
360
361 #if defined (SIGVTALRM) /* virtual time alarm */
362   signal_names[SIGVTALRM] = "SIGVTALRM";
363 #endif
364
365 #if defined (SIGPROF)   /* profiling time alarm */
366   signal_names[SIGPROF] = "SIGPROF";
367 #endif
368
369 #if defined (SIGWINCH)  /* window changed */
370   signal_names[SIGWINCH] = "SIGWINCH";
371 #endif
372
373 /* 4.4 BSD */
374 #if defined (SIGINFO) && !defined (_SEQUENT_)   /* information request */
375   signal_names[SIGINFO] = "SIGINFO";
376 #endif
377
378 #if defined (SIGUSR1)   /* user defined signal 1 */
379   signal_names[SIGUSR1] = "SIGUSR1";
380 #endif
381
382 #if defined (SIGUSR2)   /* user defined signal 2 */
383   signal_names[SIGUSR2] = "SIGUSR2";
384 #endif
385
386 #if defined (SIGKILLTHR)        /* BeOS: Kill Thread */
387   signal_names[SIGKILLTHR] = "SIGKILLTHR";
388 #endif
389
390   for (i = 0; i < NSIG; i++)
391     if (signal_names[i] == (char *)NULL)
392       {
393         signal_names[i] = (char *)malloc (18);
394         if (signal_names[i])
395           sprintf (signal_names[i], "SIGJUNK(%d)", i);
396       }
397
398   signal_names[NSIG] = "DEBUG";
399   signal_names[NSIG+1] = "ERR";
400   signal_names[NSIG+2] = "RETURN";
401 }