blob: f7d598c7ad1482986d7e898f1e238e5c4d54fa99 [file] [log] [blame]
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001/* vi: set sw=4 ts=4: */
2/*
Rob Landleyc9c1a412006-07-12 19:17:55 +00003 * Signal name/number conversion routines.
Eric Andersenbdfd0d72001-10-24 05:00:29 +00004 *
Rob Landleyc9c1a412006-07-12 19:17:55 +00005 * Copyright 2006 Rob Landley <rob@landley.net>
Eric Andersenbdfd0d72001-10-24 05:00:29 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersenbdfd0d72001-10-24 05:00:29 +00008 */
Denys Vlasenko2753aae2010-12-30 01:17:03 +01009//config:config FEATURE_RTMINMAX
10//config: bool "Support RTMIN[+n] and RTMAX[-n] signal names"
11//config: default y
12//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020013//config: Support RTMIN[+n] and RTMAX[-n] signal names
14//config: in kill, killall etc. This costs ~250 bytes.
Rasmus Villemoes571e5252018-09-12 16:06:36 +020015//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.
Denys Vlasenko2753aae2010-12-30 01:17:03 +010027
Bernhard Reutner-Fischer421d9e52006-04-03 16:39:31 +000028#include "libbb.h"
29
Denis Vlasenkob851c422007-12-04 21:44:52 +000030/* Believe it or not, but some arches have more than 32 SIGs!
31 * HPPA: SIGSTKFLT == 36. */
Denis Vlasenko5f934b02007-12-04 10:05:28 +000032
Denys Vlasenko3e134eb2016-04-22 18:09:21 +020033static const char signals[][7] ALIGN1 = {
Rob Landleyc9c1a412006-07-12 19:17:55 +000034 // SUSv3 says kill must support these, and specifies the numerical values,
35 // http://www.opengroup.org/onlinepubs/009695399/utilities/kill.html
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000036 // {0, "EXIT"}, {1, "HUP"}, {2, "INT"}, {3, "QUIT"},
37 // {6, "ABRT"}, {9, "KILL"}, {14, "ALRM"}, {15, "TERM"}
Rob Landleyc9c1a412006-07-12 19:17:55 +000038 // And Posix adds the following:
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000039 // {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"}
Denis Vlasenko5f934b02007-12-04 10:05:28 +000043
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000044 [0] = "EXIT",
Denis Vlasenkob851c422007-12-04 21:44:52 +000045#ifdef SIGHUP
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000046 [SIGHUP ] = "HUP",
47#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000048#ifdef SIGINT
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000049 [SIGINT ] = "INT",
50#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000051#ifdef SIGQUIT
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000052 [SIGQUIT ] = "QUIT",
53#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000054#ifdef SIGILL
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000055 [SIGILL ] = "ILL",
56#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000057#ifdef SIGTRAP
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000058 [SIGTRAP ] = "TRAP",
59#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000060#ifdef SIGABRT
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000061 [SIGABRT ] = "ABRT",
62#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000063#ifdef SIGBUS
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000064 [SIGBUS ] = "BUS",
65#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000066#ifdef SIGFPE
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000067 [SIGFPE ] = "FPE",
68#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000069#ifdef SIGKILL
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000070 [SIGKILL ] = "KILL",
71#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000072#ifdef SIGUSR1
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000073 [SIGUSR1 ] = "USR1",
74#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000075#ifdef SIGSEGV
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000076 [SIGSEGV ] = "SEGV",
77#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000078#ifdef SIGUSR2
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000079 [SIGUSR2 ] = "USR2",
80#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000081#ifdef SIGPIPE
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000082 [SIGPIPE ] = "PIPE",
83#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000084#ifdef SIGALRM
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000085 [SIGALRM ] = "ALRM",
86#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000087#ifdef SIGTERM
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000088 [SIGTERM ] = "TERM",
89#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000090#ifdef SIGSTKFLT
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000091 [SIGSTKFLT] = "STKFLT",
92#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000093#ifdef SIGCHLD
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000094 [SIGCHLD ] = "CHLD",
95#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000096#ifdef SIGCONT
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000097 [SIGCONT ] = "CONT",
98#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000099#ifdef SIGSTOP
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000100 [SIGSTOP ] = "STOP",
101#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +0000102#ifdef SIGTSTP
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000103 [SIGTSTP ] = "TSTP",
104#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +0000105#ifdef SIGTTIN
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000106 [SIGTTIN ] = "TTIN",
107#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +0000108#ifdef SIGTTOU
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000109 [SIGTTOU ] = "TTOU",
110#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +0000111#ifdef SIGURG
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000112 [SIGURG ] = "URG",
113#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +0000114#ifdef SIGXCPU
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000115 [SIGXCPU ] = "XCPU",
116#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +0000117#ifdef SIGXFSZ
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000118 [SIGXFSZ ] = "XFSZ",
119#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +0000120#ifdef SIGVTALRM
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000121 [SIGVTALRM] = "VTALRM",
122#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +0000123#ifdef SIGPROF
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000124 [SIGPROF ] = "PROF",
125#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +0000126#ifdef SIGWINCH
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000127 [SIGWINCH ] = "WINCH",
128#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +0000129#ifdef SIGPOLL
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000130 [SIGPOLL ] = "POLL",
131#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +0000132#ifdef SIGPWR
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000133 [SIGPWR ] = "PWR",
134#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +0000135#ifdef SIGSYS
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000136 [SIGSYS ] = "SYS",
137#endif
Rasmus Villemoes571e5252018-09-12 16:06:36 +0200138#if ENABLE_FEATURE_RTMINMAX && !ENABLE_FEATURE_RTMINMAX_USE_LIBC_DEFINITIONS
Denys Vlasenko327d2882011-01-03 13:08:58 +0100139# 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
Eric Andersen842757d2001-08-02 05:18:55 +0000148};
149
Rob Landleyc9c1a412006-07-12 19:17:55 +0000150// Convert signal name to number.
Eric Andersen842757d2001-08-02 05:18:55 +0000151
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000152int FAST_FUNC get_signum(const char *name)
Eric Andersen842757d2001-08-02 05:18:55 +0000153{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000154 unsigned i;
Eric Andersen842757d2001-08-02 05:18:55 +0000155
Rasmus Villemoes656ca7b2018-09-12 16:06:37 +0200156 /* 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 */
Denis Vlasenko2a813e22006-12-23 01:06:21 +0000160 i = bb_strtou(name, NULL, 10);
Rasmus Villemoes656ca7b2018-09-12 16:06:37 +0200161 if (i < NSIG) /* for shells, we allow 0 too */
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000162 return i;
163 if (strncasecmp(name, "SIG", 3) == 0)
164 name += 3;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000165 for (i = 0; i < ARRAY_SIZE(signals); i++)
Denis Vlasenkob851c422007-12-04 21:44:52 +0000166 if (strcasecmp(name, signals[i]) == 0)
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000167 return i;
168
Denys Vlasenko2753aae2010-12-30 01:17:03 +0100169#if ENABLE_DESKTOP
170# if defined(SIGIOT) || defined(SIGIO)
Denis Vlasenkob851c422007-12-04 21:44:52 +0000171 /* SIGIO[T] are aliased to other names,
172 * thus cannot be stored in the signals[] array.
173 * Need special code to recognize them */
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000174 if ((name[0] | 0x20) == 'i' && (name[1] | 0x20) == 'o') {
Denys Vlasenko2753aae2010-12-30 01:17:03 +0100175# ifdef SIGIO
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000176 if (!name[2])
177 return SIGIO;
Denys Vlasenko2753aae2010-12-30 01:17:03 +0100178# endif
179# ifdef SIGIOT
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000180 if ((name[2] | 0x20) == 't' && !name[3])
181 return SIGIOT;
Denys Vlasenko2753aae2010-12-30 01:17:03 +0100182# endif
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000183 }
Denys Vlasenko2753aae2010-12-30 01:17:03 +0100184# endif
185#endif
186
Rasmus Villemoes571e5252018-09-12 16:06:36 +0200187#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: */
Denys Vlasenko7b276fc2011-01-03 12:51:13 +0100196# if !defined(__SIGRTMIN)
197# define __SIGRTMIN SIGRTMIN
198# endif
199# if !defined(__SIGRTMAX)
200# define __SIGRTMAX SIGRTMAX
201# endif
Rasmus Villemoes571e5252018-09-12 16:06:36 +0200202
203# define sigrtmin __SIGRTMIN
204# define sigrtmax __SIGRTMAX
Denys Vlasenko2753aae2010-12-30 01:17:03 +0100205# endif
Rasmus Villemoes571e5252018-09-12 16:06:36 +0200206 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);
Rasmus Villemoes656ca7b2018-09-12 16:06:37 +0200211 if (i <= sigrtmax - sigrtmin)
Rasmus Villemoes571e5252018-09-12 16:06:36 +0200212 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);
Rasmus Villemoes656ca7b2018-09-12 16:06:37 +0200220 if (i <= sigrtmax - sigrtmin)
Rasmus Villemoes571e5252018-09-12 16:06:36 +0200221 return sigrtmax - i;
222 }
223 }
224# undef sigrtmin
225# undef sigrtmax
226 }
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000227#endif
228
Rob Landleyc9c1a412006-07-12 19:17:55 +0000229 return -1;
230}
231
232// Convert signal number to name
233
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000234const char* FAST_FUNC get_signame(int number)
Rob Landleyc9c1a412006-07-12 19:17:55 +0000235{
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000236 if ((unsigned)number < ARRAY_SIZE(signals)) {
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000237 if (signals[number][0]) /* if it's not an empty str */
238 return signals[number];
Eric Andersen842757d2001-08-02 05:18:55 +0000239 }
Rob Landleyc9c1a412006-07-12 19:17:55 +0000240
Denis Vlasenko75ab6af2007-03-14 21:56:51 +0000241 return itoa(number);
Eric Andersen842757d2001-08-02 05:18:55 +0000242}
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000243
244
245// Print the whole signal list
246
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000247void FAST_FUNC print_signames(void)
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000248{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000249 unsigned signo;
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000250
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000251 for (signo = 1; signo < ARRAY_SIZE(signals); signo++) {
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000252 const char *name = signals[signo];
253 if (name[0])
Denys Vlasenko327d2882011-01-03 13:08:58 +0100254 printf("%2u) %s\n", signo, name);
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000255 }
Denys Vlasenko327d2882011-01-03 13:08:58 +0100256#if ENABLE_FEATURE_RTMINMAX
Rasmus Villemoes571e5252018-09-12 16:06:36 +0200257# 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
Denys Vlasenko327d2882011-01-03 13:08:58 +0100265 printf("%2u) %s\n", __SIGRTMAX, "RTMAX");
Rasmus Villemoes571e5252018-09-12 16:06:36 +0200266# endif
Denys Vlasenko327d2882011-01-03 13:08:58 +0100267# endif
268#endif
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000269}