1 /* vi: set sw=4 ts=4: */
3 * Signal name/number conversion routines.
5 * Copyright 2006 Rob Landley <rob@landley.net>
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
9 //config:config FEATURE_RTMINMAX
10 //config: bool "Support RTMIN[+n] and RTMAX[-n] signal names"
13 //config: Support RTMIN[+n] and RTMAX[-n] signal names
14 //config: in kill, killall etc. This costs ~250 bytes.
16 //config:config FEATURE_RTMINMAX_USE_LIBC_DEFINITIONS
17 //config: bool "Use the definitions of SIGRTMIN/SIGRTMAX provided by libc"
19 //config: depends on FEATURE_RTMINMAX
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.
30 /* Believe it or not, but some arches have more than 32 SIGs!
31 * HPPA: SIGSTKFLT == 36. */
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"},
91 [SIGSTKFLT] = "STKFLT",
121 [SIGVTALRM] = "VTALRM",
127 [SIGWINCH ] = "WINCH",
138 #if ENABLE_FEATURE_RTMINMAX && !ENABLE_FEATURE_RTMINMAX_USE_LIBC_DEFINITIONS
140 [__SIGRTMIN] = "RTMIN",
142 // This makes array about x2 bigger.
143 // More compact approach is to special-case SIGRTMAX in print_signames()
145 // [__SIGRTMAX] = "RTMAX",
150 // Convert signal name to number.
152 int FAST_FUNC get_signum(const char *name)
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().
160 i = bb_strtou(name, NULL, 10);
161 if (i < NSIG) /* for shells, we allow 0 too */
163 if (strncasecmp(name, "SIG", 3) == 0)
165 for (i = 0; i < ARRAY_SIZE(signals); i++)
166 if (strcasecmp(name, signals[i]) == 0)
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') {
180 if ((name[2] | 0x20) == 't' && !name[3])
187 #if ENABLE_FEATURE_RTMINMAX && defined(SIGRTMIN) && defined(SIGRTMAX)
189 # if ENABLE_FEATURE_RTMINMAX_USE_LIBC_DEFINITIONS
190 /* Use the libc provided values. */
191 unsigned sigrtmin = SIGRTMIN;
192 unsigned sigrtmax = SIGRTMAX;
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
199 # if !defined(__SIGRTMAX)
200 # define __SIGRTMAX SIGRTMAX
203 # define sigrtmin __SIGRTMIN
204 # define sigrtmax __SIGRTMAX
206 if (strncasecmp(name, "RTMIN", 5) == 0) {
209 if (name[5] == '+') {
210 i = bb_strtou(name + 6, NULL, 10);
211 if (i <= sigrtmax - sigrtmin)
215 else if (strncasecmp(name, "RTMAX", 5) == 0) {
218 if (name[5] == '-') {
219 i = bb_strtou(name + 6, NULL, 10);
220 if (i <= sigrtmax - sigrtmin)
232 // Convert signal number to name
234 const char* FAST_FUNC get_signame(int number)
236 if ((unsigned)number < ARRAY_SIZE(signals)) {
237 if (signals[number][0]) /* if it's not an empty str */
238 return signals[number];
245 // Print the whole signal list
247 void FAST_FUNC print_signames(void)
251 for (signo = 1; signo < ARRAY_SIZE(signals); signo++) {
252 const char *name = signals[signo];
254 printf("%2u) %s\n", signo, name);
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");
263 // __SIGRTMIN is included in signals[] array.
265 printf("%2u) %s\n", __SIGRTMAX, "RTMAX");