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.
10 //config:config FEATURE_RTMINMAX
11 //config: bool "Support RTMIN[+n] and RTMAX[-n] signal names"
14 //config: Support RTMIN[+n] and RTMAX[-n] signal names
15 //config: in kill, killall etc. This costs ~250 bytes.
19 /* Believe it or not, but some arches have more than 32 SIGs!
20 * HPPA: SIGSTKFLT == 36. */
22 static const char signals
[][7] ALIGN1
= {
23 // SUSv3 says kill must support these, and specifies the numerical values,
24 // http://www.opengroup.org/onlinepubs/009695399/utilities/kill.html
25 // {0, "EXIT"}, {1, "HUP"}, {2, "INT"}, {3, "QUIT"},
26 // {6, "ABRT"}, {9, "KILL"}, {14, "ALRM"}, {15, "TERM"}
27 // And Posix adds the following:
28 // {SIGILL, "ILL"}, {SIGTRAP, "TRAP"}, {SIGFPE, "FPE"}, {SIGUSR1, "USR1"},
29 // {SIGSEGV, "SEGV"}, {SIGUSR2, "USR2"}, {SIGPIPE, "PIPE"}, {SIGCHLD, "CHLD"},
30 // {SIGCONT, "CONT"}, {SIGSTOP, "STOP"}, {SIGTSTP, "TSTP"}, {SIGTTIN, "TTIN"},
80 [SIGSTKFLT
] = "STKFLT",
110 [SIGVTALRM
] = "VTALRM",
116 [SIGWINCH
] = "WINCH",
127 #if ENABLE_FEATURE_RTMINMAX
129 [__SIGRTMIN
] = "RTMIN",
131 // This makes array about x2 bigger.
132 // More compact approach is to special-case SIGRTMAX in print_signames()
134 // [__SIGRTMAX] = "RTMAX",
139 // Convert signal name to number.
141 int FAST_FUNC
get_signum(const char *name
)
145 i
= bb_strtou(name
, NULL
, 10);
148 if (strncasecmp(name
, "SIG", 3) == 0)
150 for (i
= 0; i
< ARRAY_SIZE(signals
); i
++)
151 if (strcasecmp(name
, signals
[i
]) == 0)
155 # if defined(SIGIOT) || defined(SIGIO)
156 /* SIGIO[T] are aliased to other names,
157 * thus cannot be stored in the signals[] array.
158 * Need special code to recognize them */
159 if ((name
[0] | 0x20) == 'i' && (name
[1] | 0x20) == 'o') {
165 if ((name
[2] | 0x20) == 't' && !name
[3])
172 #if ENABLE_FEATURE_RTMINMAX
173 # if defined(SIGRTMIN) && defined(SIGRTMAX)
174 /* libc may use some rt sigs for pthreads and therefore "remap" SIGRTMIN/MAX,
175 * but we want to use "raw" SIGRTMIN/MAX. Underscored names, if exist, provide
176 * them. If they don't exist, fall back to non-underscored ones: */
177 # if !defined(__SIGRTMIN)
178 # define __SIGRTMIN SIGRTMIN
180 # if !defined(__SIGRTMAX)
181 # define __SIGRTMAX SIGRTMAX
183 if (strncasecmp(name
, "RTMIN", 5) == 0) {
186 if (name
[5] == '+') {
187 i
= bb_strtou(name
+ 6, NULL
, 10);
188 if (!errno
&& i
<= __SIGRTMAX
- __SIGRTMIN
)
189 return __SIGRTMIN
+ i
;
192 else if (strncasecmp(name
, "RTMAX", 5) == 0) {
195 if (name
[5] == '-') {
196 i
= bb_strtou(name
+ 6, NULL
, 10);
197 if (!errno
&& i
<= __SIGRTMAX
- __SIGRTMIN
)
198 return __SIGRTMAX
- i
;
207 // Convert signal number to name
209 const char* FAST_FUNC
get_signame(int number
)
211 if ((unsigned)number
< ARRAY_SIZE(signals
)) {
212 if (signals
[number
][0]) /* if it's not an empty str */
213 return signals
[number
];
220 // Print the whole signal list
222 void FAST_FUNC
print_signames(void)
226 for (signo
= 1; signo
< ARRAY_SIZE(signals
); signo
++) {
227 const char *name
= signals
[signo
];
229 printf("%2u) %s\n", signo
, name
);
231 #if ENABLE_FEATURE_RTMINMAX
233 printf("%2u) %s\n", __SIGRTMAX
, "RTMAX");