blob: bf984a44e2b2bff0e74b9d70712822d3daeb81ca [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 */
9
Denys Vlasenko2753aae2010-12-30 01:17:03 +010010//config:config FEATURE_RTMINMAX
11//config: bool "Support RTMIN[+n] and RTMAX[-n] signal names"
12//config: default y
13//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020014//config: Support RTMIN[+n] and RTMAX[-n] signal names
15//config: in kill, killall etc. This costs ~250 bytes.
Denys Vlasenko2753aae2010-12-30 01:17:03 +010016
Bernhard Reutner-Fischer421d9e52006-04-03 16:39:31 +000017#include "libbb.h"
18
Denis Vlasenkob851c422007-12-04 21:44:52 +000019/* Believe it or not, but some arches have more than 32 SIGs!
20 * HPPA: SIGSTKFLT == 36. */
Denis Vlasenko5f934b02007-12-04 10:05:28 +000021
Denys Vlasenko3e134eb2016-04-22 18:09:21 +020022static const char signals[][7] ALIGN1 = {
Rob Landleyc9c1a412006-07-12 19:17:55 +000023 // SUSv3 says kill must support these, and specifies the numerical values,
24 // http://www.opengroup.org/onlinepubs/009695399/utilities/kill.html
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000025 // {0, "EXIT"}, {1, "HUP"}, {2, "INT"}, {3, "QUIT"},
26 // {6, "ABRT"}, {9, "KILL"}, {14, "ALRM"}, {15, "TERM"}
Rob Landleyc9c1a412006-07-12 19:17:55 +000027 // And Posix adds the following:
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000028 // {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"},
31 // {SIGTTOU, "TTOU"}
Denis Vlasenko5f934b02007-12-04 10:05:28 +000032
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000033 [0] = "EXIT",
Denis Vlasenkob851c422007-12-04 21:44:52 +000034#ifdef SIGHUP
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000035 [SIGHUP ] = "HUP",
36#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000037#ifdef SIGINT
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000038 [SIGINT ] = "INT",
39#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000040#ifdef SIGQUIT
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000041 [SIGQUIT ] = "QUIT",
42#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000043#ifdef SIGILL
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000044 [SIGILL ] = "ILL",
45#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000046#ifdef SIGTRAP
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000047 [SIGTRAP ] = "TRAP",
48#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000049#ifdef SIGABRT
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000050 [SIGABRT ] = "ABRT",
51#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000052#ifdef SIGBUS
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000053 [SIGBUS ] = "BUS",
54#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000055#ifdef SIGFPE
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000056 [SIGFPE ] = "FPE",
57#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000058#ifdef SIGKILL
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000059 [SIGKILL ] = "KILL",
60#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000061#ifdef SIGUSR1
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000062 [SIGUSR1 ] = "USR1",
63#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000064#ifdef SIGSEGV
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000065 [SIGSEGV ] = "SEGV",
66#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000067#ifdef SIGUSR2
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000068 [SIGUSR2 ] = "USR2",
69#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000070#ifdef SIGPIPE
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000071 [SIGPIPE ] = "PIPE",
72#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000073#ifdef SIGALRM
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000074 [SIGALRM ] = "ALRM",
75#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000076#ifdef SIGTERM
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000077 [SIGTERM ] = "TERM",
78#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000079#ifdef SIGSTKFLT
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000080 [SIGSTKFLT] = "STKFLT",
81#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000082#ifdef SIGCHLD
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000083 [SIGCHLD ] = "CHLD",
84#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000085#ifdef SIGCONT
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000086 [SIGCONT ] = "CONT",
87#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000088#ifdef SIGSTOP
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000089 [SIGSTOP ] = "STOP",
90#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000091#ifdef SIGTSTP
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000092 [SIGTSTP ] = "TSTP",
93#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000094#ifdef SIGTTIN
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000095 [SIGTTIN ] = "TTIN",
96#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000097#ifdef SIGTTOU
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000098 [SIGTTOU ] = "TTOU",
99#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +0000100#ifdef SIGURG
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000101 [SIGURG ] = "URG",
102#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +0000103#ifdef SIGXCPU
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000104 [SIGXCPU ] = "XCPU",
105#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +0000106#ifdef SIGXFSZ
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000107 [SIGXFSZ ] = "XFSZ",
108#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +0000109#ifdef SIGVTALRM
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000110 [SIGVTALRM] = "VTALRM",
111#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +0000112#ifdef SIGPROF
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000113 [SIGPROF ] = "PROF",
114#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +0000115#ifdef SIGWINCH
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000116 [SIGWINCH ] = "WINCH",
117#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +0000118#ifdef SIGPOLL
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000119 [SIGPOLL ] = "POLL",
120#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +0000121#ifdef SIGPWR
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000122 [SIGPWR ] = "PWR",
123#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +0000124#ifdef SIGSYS
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000125 [SIGSYS ] = "SYS",
126#endif
Denys Vlasenko327d2882011-01-03 13:08:58 +0100127#if ENABLE_FEATURE_RTMINMAX
128# ifdef __SIGRTMIN
129 [__SIGRTMIN] = "RTMIN",
130# endif
131// This makes array about x2 bigger.
132// More compact approach is to special-case SIGRTMAX in print_signames()
133//# ifdef __SIGRTMAX
134// [__SIGRTMAX] = "RTMAX",
135//# endif
136#endif
Eric Andersen842757d2001-08-02 05:18:55 +0000137};
138
Rob Landleyc9c1a412006-07-12 19:17:55 +0000139// Convert signal name to number.
Eric Andersen842757d2001-08-02 05:18:55 +0000140
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000141int FAST_FUNC get_signum(const char *name)
Eric Andersen842757d2001-08-02 05:18:55 +0000142{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000143 unsigned i;
Eric Andersen842757d2001-08-02 05:18:55 +0000144
Denis Vlasenko2a813e22006-12-23 01:06:21 +0000145 i = bb_strtou(name, NULL, 10);
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000146 if (!errno)
147 return i;
148 if (strncasecmp(name, "SIG", 3) == 0)
149 name += 3;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000150 for (i = 0; i < ARRAY_SIZE(signals); i++)
Denis Vlasenkob851c422007-12-04 21:44:52 +0000151 if (strcasecmp(name, signals[i]) == 0)
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000152 return i;
153
Denys Vlasenko2753aae2010-12-30 01:17:03 +0100154#if ENABLE_DESKTOP
155# if defined(SIGIOT) || defined(SIGIO)
Denis Vlasenkob851c422007-12-04 21:44:52 +0000156 /* SIGIO[T] are aliased to other names,
157 * thus cannot be stored in the signals[] array.
158 * Need special code to recognize them */
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000159 if ((name[0] | 0x20) == 'i' && (name[1] | 0x20) == 'o') {
Denys Vlasenko2753aae2010-12-30 01:17:03 +0100160# ifdef SIGIO
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000161 if (!name[2])
162 return SIGIO;
Denys Vlasenko2753aae2010-12-30 01:17:03 +0100163# endif
164# ifdef SIGIOT
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000165 if ((name[2] | 0x20) == 't' && !name[3])
166 return SIGIOT;
Denys Vlasenko2753aae2010-12-30 01:17:03 +0100167# endif
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000168 }
Denys Vlasenko2753aae2010-12-30 01:17:03 +0100169# endif
170#endif
171
172#if ENABLE_FEATURE_RTMINMAX
173# if defined(SIGRTMIN) && defined(SIGRTMAX)
Denys Vlasenko7b276fc2011-01-03 12:51:13 +0100174/* 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
179# endif
180# if !defined(__SIGRTMAX)
181# define __SIGRTMAX SIGRTMAX
182# endif
Denys Vlasenko2753aae2010-12-30 01:17:03 +0100183 if (strncasecmp(name, "RTMIN", 5) == 0) {
184 if (!name[5])
Denys Vlasenko7b276fc2011-01-03 12:51:13 +0100185 return __SIGRTMIN;
Denys Vlasenko2753aae2010-12-30 01:17:03 +0100186 if (name[5] == '+') {
187 i = bb_strtou(name + 6, NULL, 10);
Denys Vlasenko7b276fc2011-01-03 12:51:13 +0100188 if (!errno && i <= __SIGRTMAX - __SIGRTMIN)
189 return __SIGRTMIN + i;
190 }
191 }
192 else if (strncasecmp(name, "RTMAX", 5) == 0) {
193 if (!name[5])
194 return __SIGRTMAX;
195 if (name[5] == '-') {
196 i = bb_strtou(name + 6, NULL, 10);
197 if (!errno && i <= __SIGRTMAX - __SIGRTMIN)
198 return __SIGRTMAX - i;
Denys Vlasenko2753aae2010-12-30 01:17:03 +0100199 }
200 }
201# endif
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000202#endif
203
Rob Landleyc9c1a412006-07-12 19:17:55 +0000204 return -1;
205}
206
207// Convert signal number to name
208
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000209const char* FAST_FUNC get_signame(int number)
Rob Landleyc9c1a412006-07-12 19:17:55 +0000210{
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000211 if ((unsigned)number < ARRAY_SIZE(signals)) {
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000212 if (signals[number][0]) /* if it's not an empty str */
213 return signals[number];
Eric Andersen842757d2001-08-02 05:18:55 +0000214 }
Rob Landleyc9c1a412006-07-12 19:17:55 +0000215
Denis Vlasenko75ab6af2007-03-14 21:56:51 +0000216 return itoa(number);
Eric Andersen842757d2001-08-02 05:18:55 +0000217}
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000218
219
220// Print the whole signal list
221
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000222void FAST_FUNC print_signames(void)
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000223{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000224 unsigned signo;
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000225
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000226 for (signo = 1; signo < ARRAY_SIZE(signals); signo++) {
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000227 const char *name = signals[signo];
228 if (name[0])
Denys Vlasenko327d2882011-01-03 13:08:58 +0100229 printf("%2u) %s\n", signo, name);
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000230 }
Denys Vlasenko327d2882011-01-03 13:08:58 +0100231#if ENABLE_FEATURE_RTMINMAX
232# ifdef __SIGRTMAX
233 printf("%2u) %s\n", __SIGRTMAX, "RTMAX");
234# endif
235#endif
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000236}