blob: b3038e32ddd0c8219c42c2edf2a899e6c9faf6f5 [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.
Denys Vlasenko2753aae2010-12-30 01:17:03 +010015
Bernhard Reutner-Fischer421d9e52006-04-03 16:39:31 +000016#include "libbb.h"
17
Denis Vlasenkob851c422007-12-04 21:44:52 +000018/* Believe it or not, but some arches have more than 32 SIGs!
19 * HPPA: SIGSTKFLT == 36. */
Denis Vlasenko5f934b02007-12-04 10:05:28 +000020
Denys Vlasenko3e134eb2016-04-22 18:09:21 +020021static const char signals[][7] ALIGN1 = {
Rob Landleyc9c1a412006-07-12 19:17:55 +000022 // SUSv3 says kill must support these, and specifies the numerical values,
23 // http://www.opengroup.org/onlinepubs/009695399/utilities/kill.html
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000024 // {0, "EXIT"}, {1, "HUP"}, {2, "INT"}, {3, "QUIT"},
25 // {6, "ABRT"}, {9, "KILL"}, {14, "ALRM"}, {15, "TERM"}
Rob Landleyc9c1a412006-07-12 19:17:55 +000026 // And Posix adds the following:
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000027 // {SIGILL, "ILL"}, {SIGTRAP, "TRAP"}, {SIGFPE, "FPE"}, {SIGUSR1, "USR1"},
28 // {SIGSEGV, "SEGV"}, {SIGUSR2, "USR2"}, {SIGPIPE, "PIPE"}, {SIGCHLD, "CHLD"},
29 // {SIGCONT, "CONT"}, {SIGSTOP, "STOP"}, {SIGTSTP, "TSTP"}, {SIGTTIN, "TTIN"},
30 // {SIGTTOU, "TTOU"}
Denis Vlasenko5f934b02007-12-04 10:05:28 +000031
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000032 [0] = "EXIT",
Denis Vlasenkob851c422007-12-04 21:44:52 +000033#ifdef SIGHUP
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000034 [SIGHUP ] = "HUP",
35#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000036#ifdef SIGINT
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000037 [SIGINT ] = "INT",
38#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000039#ifdef SIGQUIT
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000040 [SIGQUIT ] = "QUIT",
41#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000042#ifdef SIGILL
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000043 [SIGILL ] = "ILL",
44#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000045#ifdef SIGTRAP
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000046 [SIGTRAP ] = "TRAP",
47#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000048#ifdef SIGABRT
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000049 [SIGABRT ] = "ABRT",
50#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000051#ifdef SIGBUS
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000052 [SIGBUS ] = "BUS",
53#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000054#ifdef SIGFPE
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000055 [SIGFPE ] = "FPE",
56#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000057#ifdef SIGKILL
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000058 [SIGKILL ] = "KILL",
59#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000060#ifdef SIGUSR1
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000061 [SIGUSR1 ] = "USR1",
62#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000063#ifdef SIGSEGV
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000064 [SIGSEGV ] = "SEGV",
65#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000066#ifdef SIGUSR2
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000067 [SIGUSR2 ] = "USR2",
68#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000069#ifdef SIGPIPE
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000070 [SIGPIPE ] = "PIPE",
71#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000072#ifdef SIGALRM
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000073 [SIGALRM ] = "ALRM",
74#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000075#ifdef SIGTERM
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000076 [SIGTERM ] = "TERM",
77#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000078#ifdef SIGSTKFLT
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000079 [SIGSTKFLT] = "STKFLT",
80#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000081#ifdef SIGCHLD
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000082 [SIGCHLD ] = "CHLD",
83#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000084#ifdef SIGCONT
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000085 [SIGCONT ] = "CONT",
86#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000087#ifdef SIGSTOP
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000088 [SIGSTOP ] = "STOP",
89#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000090#ifdef SIGTSTP
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000091 [SIGTSTP ] = "TSTP",
92#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000093#ifdef SIGTTIN
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000094 [SIGTTIN ] = "TTIN",
95#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000096#ifdef SIGTTOU
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000097 [SIGTTOU ] = "TTOU",
98#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000099#ifdef SIGURG
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000100 [SIGURG ] = "URG",
101#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +0000102#ifdef SIGXCPU
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000103 [SIGXCPU ] = "XCPU",
104#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +0000105#ifdef SIGXFSZ
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000106 [SIGXFSZ ] = "XFSZ",
107#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +0000108#ifdef SIGVTALRM
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000109 [SIGVTALRM] = "VTALRM",
110#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +0000111#ifdef SIGPROF
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000112 [SIGPROF ] = "PROF",
113#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +0000114#ifdef SIGWINCH
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000115 [SIGWINCH ] = "WINCH",
116#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +0000117#ifdef SIGPOLL
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000118 [SIGPOLL ] = "POLL",
119#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +0000120#ifdef SIGPWR
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000121 [SIGPWR ] = "PWR",
122#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +0000123#ifdef SIGSYS
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000124 [SIGSYS ] = "SYS",
125#endif
Denys Vlasenko327d2882011-01-03 13:08:58 +0100126#if ENABLE_FEATURE_RTMINMAX
127# ifdef __SIGRTMIN
128 [__SIGRTMIN] = "RTMIN",
129# endif
130// This makes array about x2 bigger.
131// More compact approach is to special-case SIGRTMAX in print_signames()
132//# ifdef __SIGRTMAX
133// [__SIGRTMAX] = "RTMAX",
134//# endif
135#endif
Eric Andersen842757d2001-08-02 05:18:55 +0000136};
137
Rob Landleyc9c1a412006-07-12 19:17:55 +0000138// Convert signal name to number.
Eric Andersen842757d2001-08-02 05:18:55 +0000139
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000140int FAST_FUNC get_signum(const char *name)
Eric Andersen842757d2001-08-02 05:18:55 +0000141{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000142 unsigned i;
Eric Andersen842757d2001-08-02 05:18:55 +0000143
Denis Vlasenko2a813e22006-12-23 01:06:21 +0000144 i = bb_strtou(name, NULL, 10);
Denys Vlasenko86981e32017-07-25 20:06:17 +0200145 if (!errno && i < NSIG) /* for shells, we allow 0 too */
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000146 return i;
147 if (strncasecmp(name, "SIG", 3) == 0)
148 name += 3;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000149 for (i = 0; i < ARRAY_SIZE(signals); i++)
Denis Vlasenkob851c422007-12-04 21:44:52 +0000150 if (strcasecmp(name, signals[i]) == 0)
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000151 return i;
152
Denys Vlasenko2753aae2010-12-30 01:17:03 +0100153#if ENABLE_DESKTOP
154# if defined(SIGIOT) || defined(SIGIO)
Denis Vlasenkob851c422007-12-04 21:44:52 +0000155 /* SIGIO[T] are aliased to other names,
156 * thus cannot be stored in the signals[] array.
157 * Need special code to recognize them */
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000158 if ((name[0] | 0x20) == 'i' && (name[1] | 0x20) == 'o') {
Denys Vlasenko2753aae2010-12-30 01:17:03 +0100159# ifdef SIGIO
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000160 if (!name[2])
161 return SIGIO;
Denys Vlasenko2753aae2010-12-30 01:17:03 +0100162# endif
163# ifdef SIGIOT
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000164 if ((name[2] | 0x20) == 't' && !name[3])
165 return SIGIOT;
Denys Vlasenko2753aae2010-12-30 01:17:03 +0100166# endif
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000167 }
Denys Vlasenko2753aae2010-12-30 01:17:03 +0100168# endif
169#endif
170
171#if ENABLE_FEATURE_RTMINMAX
172# if defined(SIGRTMIN) && defined(SIGRTMAX)
Denys Vlasenko7b276fc2011-01-03 12:51:13 +0100173/* libc may use some rt sigs for pthreads and therefore "remap" SIGRTMIN/MAX,
174 * but we want to use "raw" SIGRTMIN/MAX. Underscored names, if exist, provide
175 * them. If they don't exist, fall back to non-underscored ones: */
176# if !defined(__SIGRTMIN)
177# define __SIGRTMIN SIGRTMIN
178# endif
179# if !defined(__SIGRTMAX)
180# define __SIGRTMAX SIGRTMAX
181# endif
Denys Vlasenko2753aae2010-12-30 01:17:03 +0100182 if (strncasecmp(name, "RTMIN", 5) == 0) {
183 if (!name[5])
Denys Vlasenko7b276fc2011-01-03 12:51:13 +0100184 return __SIGRTMIN;
Denys Vlasenko2753aae2010-12-30 01:17:03 +0100185 if (name[5] == '+') {
186 i = bb_strtou(name + 6, NULL, 10);
Denys Vlasenko7b276fc2011-01-03 12:51:13 +0100187 if (!errno && i <= __SIGRTMAX - __SIGRTMIN)
188 return __SIGRTMIN + i;
189 }
190 }
191 else if (strncasecmp(name, "RTMAX", 5) == 0) {
192 if (!name[5])
193 return __SIGRTMAX;
194 if (name[5] == '-') {
195 i = bb_strtou(name + 6, NULL, 10);
196 if (!errno && i <= __SIGRTMAX - __SIGRTMIN)
197 return __SIGRTMAX - i;
Denys Vlasenko2753aae2010-12-30 01:17:03 +0100198 }
199 }
200# endif
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000201#endif
202
Rob Landleyc9c1a412006-07-12 19:17:55 +0000203 return -1;
204}
205
206// Convert signal number to name
207
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000208const char* FAST_FUNC get_signame(int number)
Rob Landleyc9c1a412006-07-12 19:17:55 +0000209{
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000210 if ((unsigned)number < ARRAY_SIZE(signals)) {
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000211 if (signals[number][0]) /* if it's not an empty str */
212 return signals[number];
Eric Andersen842757d2001-08-02 05:18:55 +0000213 }
Rob Landleyc9c1a412006-07-12 19:17:55 +0000214
Denis Vlasenko75ab6af2007-03-14 21:56:51 +0000215 return itoa(number);
Eric Andersen842757d2001-08-02 05:18:55 +0000216}
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000217
218
219// Print the whole signal list
220
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000221void FAST_FUNC print_signames(void)
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000222{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000223 unsigned signo;
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000224
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000225 for (signo = 1; signo < ARRAY_SIZE(signals); signo++) {
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000226 const char *name = signals[signo];
227 if (name[0])
Denys Vlasenko327d2882011-01-03 13:08:58 +0100228 printf("%2u) %s\n", signo, name);
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000229 }
Denys Vlasenko327d2882011-01-03 13:08:58 +0100230#if ENABLE_FEATURE_RTMINMAX
231# ifdef __SIGRTMAX
232 printf("%2u) %s\n", __SIGRTMAX, "RTMAX");
233# endif
234#endif
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000235}