Start 1.33.0 development cycle
[oweals/busybox.git] / libbb / u_signal_names.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Signal name/number conversion routines.
4  *
5  * Copyright 2006 Rob Landley <rob@landley.net>
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8  */
9 //config:config FEATURE_RTMINMAX
10 //config:       bool "Support RTMIN[+n] and RTMAX[-n] signal names"
11 //config:       default y
12 //config:       help
13 //config:       Support RTMIN[+n] and RTMAX[-n] signal names
14 //config:       in kill, killall etc. This costs ~250 bytes.
15 //config:
16 //config:config FEATURE_RTMINMAX_USE_LIBC_DEFINITIONS
17 //config:       bool "Use the definitions of SIGRTMIN/SIGRTMAX provided by libc"
18 //config:       default y
19 //config:       depends on FEATURE_RTMINMAX
20 //config:       help
21 //config:       Some C libraries reserve a few real-time signals for internal
22 //config:       use, and adjust the values of SIGRTMIN/SIGRTMAX seen by
23 //config:       applications accordingly. Saying yes here means that a signal
24 //config:       name RTMIN+n will be interpreted according to the libc definition
25 //config:       of SIGRTMIN, and not the raw definition provided by the kernel.
26 //config:       This behavior matches "kill -l RTMIN+n" from bash.
27
28 #include "libbb.h"
29
30 /* Believe it or not, but some arches have more than 32 SIGs!
31  * HPPA: SIGSTKFLT == 36. */
32
33 static const char signals[][7] ALIGN1 = {
34         // SUSv3 says kill must support these, and specifies the numerical values,
35         // http://www.opengroup.org/onlinepubs/009695399/utilities/kill.html
36         // {0, "EXIT"}, {1, "HUP"}, {2, "INT"}, {3, "QUIT"},
37         // {6, "ABRT"}, {9, "KILL"}, {14, "ALRM"}, {15, "TERM"}
38         // And Posix adds the following:
39         // {SIGILL, "ILL"}, {SIGTRAP, "TRAP"}, {SIGFPE, "FPE"}, {SIGUSR1, "USR1"},
40         // {SIGSEGV, "SEGV"}, {SIGUSR2, "USR2"}, {SIGPIPE, "PIPE"}, {SIGCHLD, "CHLD"},
41         // {SIGCONT, "CONT"}, {SIGSTOP, "STOP"}, {SIGTSTP, "TSTP"}, {SIGTTIN, "TTIN"},
42         // {SIGTTOU, "TTOU"}
43
44         [0] = "EXIT",
45 #ifdef SIGHUP
46         [SIGHUP   ] = "HUP",
47 #endif
48 #ifdef SIGINT
49         [SIGINT   ] = "INT",
50 #endif
51 #ifdef SIGQUIT
52         [SIGQUIT  ] = "QUIT",
53 #endif
54 #ifdef SIGILL
55         [SIGILL   ] = "ILL",
56 #endif
57 #ifdef SIGTRAP
58         [SIGTRAP  ] = "TRAP",
59 #endif
60 #ifdef SIGABRT
61         [SIGABRT  ] = "ABRT",
62 #endif
63 #ifdef SIGBUS
64         [SIGBUS   ] = "BUS",
65 #endif
66 #ifdef SIGFPE
67         [SIGFPE   ] = "FPE",
68 #endif
69 #ifdef SIGKILL
70         [SIGKILL  ] = "KILL",
71 #endif
72 #ifdef SIGUSR1
73         [SIGUSR1  ] = "USR1",
74 #endif
75 #ifdef SIGSEGV
76         [SIGSEGV  ] = "SEGV",
77 #endif
78 #ifdef SIGUSR2
79         [SIGUSR2  ] = "USR2",
80 #endif
81 #ifdef SIGPIPE
82         [SIGPIPE  ] = "PIPE",
83 #endif
84 #ifdef SIGALRM
85         [SIGALRM  ] = "ALRM",
86 #endif
87 #ifdef SIGTERM
88         [SIGTERM  ] = "TERM",
89 #endif
90 #ifdef SIGSTKFLT
91         [SIGSTKFLT] = "STKFLT",
92 #endif
93 #ifdef SIGCHLD
94         [SIGCHLD  ] = "CHLD",
95 #endif
96 #ifdef SIGCONT
97         [SIGCONT  ] = "CONT",
98 #endif
99 #ifdef SIGSTOP
100         [SIGSTOP  ] = "STOP",
101 #endif
102 #ifdef SIGTSTP
103         [SIGTSTP  ] = "TSTP",
104 #endif
105 #ifdef SIGTTIN
106         [SIGTTIN  ] = "TTIN",
107 #endif
108 #ifdef SIGTTOU
109         [SIGTTOU  ] = "TTOU",
110 #endif
111 #ifdef SIGURG
112         [SIGURG   ] = "URG",
113 #endif
114 #ifdef SIGXCPU
115         [SIGXCPU  ] = "XCPU",
116 #endif
117 #ifdef SIGXFSZ
118         [SIGXFSZ  ] = "XFSZ",
119 #endif
120 #ifdef SIGVTALRM
121         [SIGVTALRM] = "VTALRM",
122 #endif
123 #ifdef SIGPROF
124         [SIGPROF  ] = "PROF",
125 #endif
126 #ifdef SIGWINCH
127         [SIGWINCH ] = "WINCH",
128 #endif
129 #ifdef SIGPOLL
130         [SIGPOLL  ] = "POLL",
131 #endif
132 #ifdef SIGPWR
133         [SIGPWR   ] = "PWR",
134 #endif
135 #ifdef SIGSYS
136         [SIGSYS   ] = "SYS",
137 #endif
138 #if ENABLE_FEATURE_RTMINMAX && !ENABLE_FEATURE_RTMINMAX_USE_LIBC_DEFINITIONS
139 # ifdef __SIGRTMIN
140         [__SIGRTMIN] = "RTMIN",
141 # endif
142 // This makes array about x2 bigger.
143 // More compact approach is to special-case SIGRTMAX in print_signames()
144 //# ifdef __SIGRTMAX
145 //      [__SIGRTMAX] = "RTMAX",
146 //# endif
147 #endif
148 };
149
150 // Convert signal name to number.
151
152 int FAST_FUNC get_signum(const char *name)
153 {
154         unsigned i;
155
156         /* bb_strtou returns UINT_MAX on error. NSIG is smaller
157          * than UINT_MAX on any sane Unix. Hence no need
158          * to check errno after bb_strtou().
159          */
160         i = bb_strtou(name, NULL, 10);
161         if (i < NSIG) /* for shells, we allow 0 too */
162                 return i;
163         if (strncasecmp(name, "SIG", 3) == 0)
164                 name += 3;
165         for (i = 0; i < ARRAY_SIZE(signals); i++)
166                 if (strcasecmp(name, signals[i]) == 0)
167                         return i;
168
169 #if ENABLE_DESKTOP
170 # if defined(SIGIOT) || defined(SIGIO)
171         /* SIGIO[T] are aliased to other names,
172          * thus cannot be stored in the signals[] array.
173          * Need special code to recognize them */
174         if ((name[0] | 0x20) == 'i' && (name[1] | 0x20) == 'o') {
175 #  ifdef SIGIO
176                 if (!name[2])
177                         return SIGIO;
178 #  endif
179 #  ifdef SIGIOT
180                 if ((name[2] | 0x20) == 't' && !name[3])
181                         return SIGIOT;
182 #  endif
183         }
184 # endif
185 #endif
186
187 #if ENABLE_FEATURE_RTMINMAX && defined(SIGRTMIN) && defined(SIGRTMAX)
188         {
189 # if ENABLE_FEATURE_RTMINMAX_USE_LIBC_DEFINITIONS
190                 /* Use the libc provided values. */
191                 unsigned sigrtmin = SIGRTMIN;
192                 unsigned sigrtmax = SIGRTMAX;
193 # else
194         /* Use the "raw" SIGRTMIN/MAX. Underscored names, if exist, provide
195          * them. If they don't exist, fall back to non-underscored ones: */
196 #  if !defined(__SIGRTMIN)
197 #   define __SIGRTMIN SIGRTMIN
198 #  endif
199 #  if !defined(__SIGRTMAX)
200 #   define __SIGRTMAX SIGRTMAX
201 #  endif
202
203 #  define sigrtmin __SIGRTMIN
204 #  define sigrtmax __SIGRTMAX
205 # endif
206                 if (strncasecmp(name, "RTMIN", 5) == 0) {
207                         if (!name[5])
208                                 return sigrtmin;
209                         if (name[5] == '+') {
210                                 i = bb_strtou(name + 6, NULL, 10);
211                                 if (i <= sigrtmax - sigrtmin)
212                                         return sigrtmin + i;
213                         }
214                 }
215                 else if (strncasecmp(name, "RTMAX", 5) == 0) {
216                         if (!name[5])
217                                 return sigrtmax;
218                         if (name[5] == '-') {
219                                 i = bb_strtou(name + 6, NULL, 10);
220                                 if (i <= sigrtmax - sigrtmin)
221                                         return sigrtmax - i;
222                         }
223                 }
224 # undef sigrtmin
225 # undef sigrtmax
226         }
227 #endif
228
229         return -1;
230 }
231
232 // Convert signal number to name
233
234 const char* FAST_FUNC get_signame(int number)
235 {
236         if ((unsigned)number < ARRAY_SIZE(signals)) {
237                 if (signals[number][0]) /* if it's not an empty str */
238                         return signals[number];
239         }
240
241         return itoa(number);
242 }
243
244
245 // Print the whole signal list
246
247 void FAST_FUNC print_signames(void)
248 {
249         unsigned signo;
250
251         for (signo = 1; signo < ARRAY_SIZE(signals); signo++) {
252                 const char *name = signals[signo];
253                 if (name[0])
254                         printf("%2u) %s\n", signo, name);
255         }
256 #if ENABLE_FEATURE_RTMINMAX
257 # if ENABLE_FEATURE_RTMINMAX_USE_LIBC_DEFINITIONS
258 #  if defined(SIGRTMIN) && defined(SIGRTMAX)
259         printf("%2u) %s\n", SIGRTMIN, "RTMIN");
260         printf("%2u) %s\n", SIGRTMAX, "RTMAX");
261 #  endif
262 # else
263 // __SIGRTMIN is included in signals[] array.
264 #  ifdef __SIGRTMAX
265         printf("%2u) %s\n", __SIGRTMAX, "RTMAX");
266 #  endif
267 # endif
268 #endif
269 }