2001-10-24 10:30:29 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
2006-07-13 00:47:55 +05:30
|
|
|
* Signal name/number conversion routines.
|
2001-10-24 10:30:29 +05:30
|
|
|
*
|
2006-07-13 00:47:55 +05:30
|
|
|
* Copyright 2006 Rob Landley <rob@landley.net>
|
2001-10-24 10:30:29 +05:30
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2001-10-24 10:30:29 +05:30
|
|
|
*/
|
2010-12-30 05:47:03 +05:30
|
|
|
//config:config FEATURE_RTMINMAX
|
|
|
|
//config: bool "Support RTMIN[+n] and RTMAX[-n] signal names"
|
|
|
|
//config: default y
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: Support RTMIN[+n] and RTMAX[-n] signal names
|
|
|
|
//config: in kill, killall etc. This costs ~250 bytes.
|
2018-09-12 19:36:36 +05:30
|
|
|
//config:
|
|
|
|
//config:config FEATURE_RTMINMAX_USE_LIBC_DEFINITIONS
|
|
|
|
//config: bool "Use the definitions of SIGRTMIN/SIGRTMAX provided by libc"
|
|
|
|
//config: default y
|
|
|
|
//config: depends on FEATURE_RTMINMAX
|
|
|
|
//config: help
|
|
|
|
//config: Some C libraries reserve a few real-time signals for internal
|
|
|
|
//config: use, and adjust the values of SIGRTMIN/SIGRTMAX seen by
|
|
|
|
//config: applications accordingly. Saying yes here means that a signal
|
|
|
|
//config: name RTMIN+n will be interpreted according to the libc definition
|
|
|
|
//config: of SIGRTMIN, and not the raw definition provided by the kernel.
|
|
|
|
//config: This behavior matches "kill -l RTMIN+n" from bash.
|
2010-12-30 05:47:03 +05:30
|
|
|
|
2006-04-03 22:09:31 +05:30
|
|
|
#include "libbb.h"
|
|
|
|
|
2007-12-05 03:14:52 +05:30
|
|
|
/* Believe it or not, but some arches have more than 32 SIGs!
|
|
|
|
* HPPA: SIGSTKFLT == 36. */
|
2007-12-04 15:35:28 +05:30
|
|
|
|
2016-04-22 21:39:21 +05:30
|
|
|
static const char signals[][7] ALIGN1 = {
|
2006-07-13 00:47:55 +05:30
|
|
|
// SUSv3 says kill must support these, and specifies the numerical values,
|
|
|
|
// http://www.opengroup.org/onlinepubs/009695399/utilities/kill.html
|
2007-05-02 01:37:29 +05:30
|
|
|
// {0, "EXIT"}, {1, "HUP"}, {2, "INT"}, {3, "QUIT"},
|
|
|
|
// {6, "ABRT"}, {9, "KILL"}, {14, "ALRM"}, {15, "TERM"}
|
2006-07-13 00:47:55 +05:30
|
|
|
// And Posix adds the following:
|
2007-05-02 01:37:29 +05:30
|
|
|
// {SIGILL, "ILL"}, {SIGTRAP, "TRAP"}, {SIGFPE, "FPE"}, {SIGUSR1, "USR1"},
|
|
|
|
// {SIGSEGV, "SEGV"}, {SIGUSR2, "USR2"}, {SIGPIPE, "PIPE"}, {SIGCHLD, "CHLD"},
|
|
|
|
// {SIGCONT, "CONT"}, {SIGSTOP, "STOP"}, {SIGTSTP, "TSTP"}, {SIGTTIN, "TTIN"},
|
|
|
|
// {SIGTTOU, "TTOU"}
|
2007-12-04 15:35:28 +05:30
|
|
|
|
2007-05-02 01:37:29 +05:30
|
|
|
[0] = "EXIT",
|
2007-12-05 03:14:52 +05:30
|
|
|
#ifdef SIGHUP
|
2007-05-02 01:37:29 +05:30
|
|
|
[SIGHUP ] = "HUP",
|
|
|
|
#endif
|
2007-12-05 03:14:52 +05:30
|
|
|
#ifdef SIGINT
|
2007-05-02 01:37:29 +05:30
|
|
|
[SIGINT ] = "INT",
|
|
|
|
#endif
|
2007-12-05 03:14:52 +05:30
|
|
|
#ifdef SIGQUIT
|
2007-05-02 01:37:29 +05:30
|
|
|
[SIGQUIT ] = "QUIT",
|
|
|
|
#endif
|
2007-12-05 03:14:52 +05:30
|
|
|
#ifdef SIGILL
|
2007-05-02 01:37:29 +05:30
|
|
|
[SIGILL ] = "ILL",
|
|
|
|
#endif
|
2007-12-05 03:14:52 +05:30
|
|
|
#ifdef SIGTRAP
|
2007-05-02 01:37:29 +05:30
|
|
|
[SIGTRAP ] = "TRAP",
|
|
|
|
#endif
|
2007-12-05 03:14:52 +05:30
|
|
|
#ifdef SIGABRT
|
2007-05-02 01:37:29 +05:30
|
|
|
[SIGABRT ] = "ABRT",
|
|
|
|
#endif
|
2007-12-05 03:14:52 +05:30
|
|
|
#ifdef SIGBUS
|
2007-05-02 01:37:29 +05:30
|
|
|
[SIGBUS ] = "BUS",
|
|
|
|
#endif
|
2007-12-05 03:14:52 +05:30
|
|
|
#ifdef SIGFPE
|
2007-05-02 01:37:29 +05:30
|
|
|
[SIGFPE ] = "FPE",
|
|
|
|
#endif
|
2007-12-05 03:14:52 +05:30
|
|
|
#ifdef SIGKILL
|
2007-05-02 01:37:29 +05:30
|
|
|
[SIGKILL ] = "KILL",
|
|
|
|
#endif
|
2007-12-05 03:14:52 +05:30
|
|
|
#ifdef SIGUSR1
|
2007-05-02 01:37:29 +05:30
|
|
|
[SIGUSR1 ] = "USR1",
|
|
|
|
#endif
|
2007-12-05 03:14:52 +05:30
|
|
|
#ifdef SIGSEGV
|
2007-05-02 01:37:29 +05:30
|
|
|
[SIGSEGV ] = "SEGV",
|
|
|
|
#endif
|
2007-12-05 03:14:52 +05:30
|
|
|
#ifdef SIGUSR2
|
2007-05-02 01:37:29 +05:30
|
|
|
[SIGUSR2 ] = "USR2",
|
|
|
|
#endif
|
2007-12-05 03:14:52 +05:30
|
|
|
#ifdef SIGPIPE
|
2007-05-02 01:37:29 +05:30
|
|
|
[SIGPIPE ] = "PIPE",
|
|
|
|
#endif
|
2007-12-05 03:14:52 +05:30
|
|
|
#ifdef SIGALRM
|
2007-05-02 01:37:29 +05:30
|
|
|
[SIGALRM ] = "ALRM",
|
|
|
|
#endif
|
2007-12-05 03:14:52 +05:30
|
|
|
#ifdef SIGTERM
|
2007-05-02 01:37:29 +05:30
|
|
|
[SIGTERM ] = "TERM",
|
|
|
|
#endif
|
2007-12-05 03:14:52 +05:30
|
|
|
#ifdef SIGSTKFLT
|
2007-05-02 01:37:29 +05:30
|
|
|
[SIGSTKFLT] = "STKFLT",
|
|
|
|
#endif
|
2007-12-05 03:14:52 +05:30
|
|
|
#ifdef SIGCHLD
|
2007-05-02 01:37:29 +05:30
|
|
|
[SIGCHLD ] = "CHLD",
|
|
|
|
#endif
|
2007-12-05 03:14:52 +05:30
|
|
|
#ifdef SIGCONT
|
2007-05-02 01:37:29 +05:30
|
|
|
[SIGCONT ] = "CONT",
|
|
|
|
#endif
|
2007-12-05 03:14:52 +05:30
|
|
|
#ifdef SIGSTOP
|
2007-05-02 01:37:29 +05:30
|
|
|
[SIGSTOP ] = "STOP",
|
|
|
|
#endif
|
2007-12-05 03:14:52 +05:30
|
|
|
#ifdef SIGTSTP
|
2007-05-02 01:37:29 +05:30
|
|
|
[SIGTSTP ] = "TSTP",
|
|
|
|
#endif
|
2007-12-05 03:14:52 +05:30
|
|
|
#ifdef SIGTTIN
|
2007-05-02 01:37:29 +05:30
|
|
|
[SIGTTIN ] = "TTIN",
|
|
|
|
#endif
|
2007-12-05 03:14:52 +05:30
|
|
|
#ifdef SIGTTOU
|
2007-05-02 01:37:29 +05:30
|
|
|
[SIGTTOU ] = "TTOU",
|
|
|
|
#endif
|
2007-12-05 03:14:52 +05:30
|
|
|
#ifdef SIGURG
|
2007-05-02 01:37:29 +05:30
|
|
|
[SIGURG ] = "URG",
|
|
|
|
#endif
|
2007-12-05 03:14:52 +05:30
|
|
|
#ifdef SIGXCPU
|
2007-05-02 01:37:29 +05:30
|
|
|
[SIGXCPU ] = "XCPU",
|
|
|
|
#endif
|
2007-12-05 03:14:52 +05:30
|
|
|
#ifdef SIGXFSZ
|
2007-05-02 01:37:29 +05:30
|
|
|
[SIGXFSZ ] = "XFSZ",
|
|
|
|
#endif
|
2007-12-05 03:14:52 +05:30
|
|
|
#ifdef SIGVTALRM
|
2007-05-02 01:37:29 +05:30
|
|
|
[SIGVTALRM] = "VTALRM",
|
|
|
|
#endif
|
2007-12-05 03:14:52 +05:30
|
|
|
#ifdef SIGPROF
|
2007-05-02 01:37:29 +05:30
|
|
|
[SIGPROF ] = "PROF",
|
|
|
|
#endif
|
2007-12-05 03:14:52 +05:30
|
|
|
#ifdef SIGWINCH
|
2007-05-02 01:37:29 +05:30
|
|
|
[SIGWINCH ] = "WINCH",
|
|
|
|
#endif
|
2007-12-05 03:14:52 +05:30
|
|
|
#ifdef SIGPOLL
|
2007-05-02 01:37:29 +05:30
|
|
|
[SIGPOLL ] = "POLL",
|
|
|
|
#endif
|
2007-12-05 03:14:52 +05:30
|
|
|
#ifdef SIGPWR
|
2007-05-02 01:37:29 +05:30
|
|
|
[SIGPWR ] = "PWR",
|
|
|
|
#endif
|
2007-12-05 03:14:52 +05:30
|
|
|
#ifdef SIGSYS
|
2007-05-02 01:37:29 +05:30
|
|
|
[SIGSYS ] = "SYS",
|
|
|
|
#endif
|
2018-09-12 19:36:36 +05:30
|
|
|
#if ENABLE_FEATURE_RTMINMAX && !ENABLE_FEATURE_RTMINMAX_USE_LIBC_DEFINITIONS
|
2011-01-03 17:38:58 +05:30
|
|
|
# ifdef __SIGRTMIN
|
|
|
|
[__SIGRTMIN] = "RTMIN",
|
|
|
|
# endif
|
|
|
|
// This makes array about x2 bigger.
|
|
|
|
// More compact approach is to special-case SIGRTMAX in print_signames()
|
|
|
|
//# ifdef __SIGRTMAX
|
|
|
|
// [__SIGRTMAX] = "RTMAX",
|
|
|
|
//# endif
|
|
|
|
#endif
|
2001-08-02 10:48:55 +05:30
|
|
|
};
|
|
|
|
|
2006-07-13 00:47:55 +05:30
|
|
|
// Convert signal name to number.
|
2001-08-02 10:48:55 +05:30
|
|
|
|
2008-06-27 08:22:20 +05:30
|
|
|
int FAST_FUNC get_signum(const char *name)
|
2006-07-13 00:47:55 +05:30
|
|
|
{
|
2008-05-16 03:00:45 +05:30
|
|
|
unsigned i;
|
2006-07-13 00:47:55 +05:30
|
|
|
|
2018-09-12 19:36:37 +05:30
|
|
|
/* bb_strtou returns UINT_MAX on error. NSIG is smaller
|
|
|
|
* than UINT_MAX on any sane Unix. Hence no need
|
|
|
|
* to check errno after bb_strtou().
|
|
|
|
*/
|
2006-12-23 06:36:21 +05:30
|
|
|
i = bb_strtou(name, NULL, 10);
|
2018-09-12 19:36:37 +05:30
|
|
|
if (i < NSIG) /* for shells, we allow 0 too */
|
2007-05-02 01:37:29 +05:30
|
|
|
return i;
|
|
|
|
if (strncasecmp(name, "SIG", 3) == 0)
|
|
|
|
name += 3;
|
2008-05-16 03:00:45 +05:30
|
|
|
for (i = 0; i < ARRAY_SIZE(signals); i++)
|
2007-12-05 03:14:52 +05:30
|
|
|
if (strcasecmp(name, signals[i]) == 0)
|
2007-05-02 01:37:29 +05:30
|
|
|
return i;
|
|
|
|
|
2010-12-30 05:47:03 +05:30
|
|
|
#if ENABLE_DESKTOP
|
|
|
|
# if defined(SIGIOT) || defined(SIGIO)
|
2007-12-05 03:14:52 +05:30
|
|
|
/* SIGIO[T] are aliased to other names,
|
|
|
|
* thus cannot be stored in the signals[] array.
|
|
|
|
* Need special code to recognize them */
|
2007-05-02 01:37:29 +05:30
|
|
|
if ((name[0] | 0x20) == 'i' && (name[1] | 0x20) == 'o') {
|
2010-12-30 05:47:03 +05:30
|
|
|
# ifdef SIGIO
|
2007-05-02 01:37:29 +05:30
|
|
|
if (!name[2])
|
|
|
|
return SIGIO;
|
2010-12-30 05:47:03 +05:30
|
|
|
# endif
|
|
|
|
# ifdef SIGIOT
|
2007-05-02 01:37:29 +05:30
|
|
|
if ((name[2] | 0x20) == 't' && !name[3])
|
|
|
|
return SIGIOT;
|
2010-12-30 05:47:03 +05:30
|
|
|
# endif
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2018-09-12 19:36:36 +05:30
|
|
|
#if ENABLE_FEATURE_RTMINMAX && defined(SIGRTMIN) && defined(SIGRTMAX)
|
|
|
|
{
|
|
|
|
# if ENABLE_FEATURE_RTMINMAX_USE_LIBC_DEFINITIONS
|
|
|
|
/* Use the libc provided values. */
|
|
|
|
unsigned sigrtmin = SIGRTMIN;
|
|
|
|
unsigned sigrtmax = SIGRTMAX;
|
|
|
|
# else
|
|
|
|
/* Use the "raw" SIGRTMIN/MAX. Underscored names, if exist, provide
|
|
|
|
* them. If they don't exist, fall back to non-underscored ones: */
|
2011-01-03 17:21:13 +05:30
|
|
|
# if !defined(__SIGRTMIN)
|
|
|
|
# define __SIGRTMIN SIGRTMIN
|
|
|
|
# endif
|
|
|
|
# if !defined(__SIGRTMAX)
|
|
|
|
# define __SIGRTMAX SIGRTMAX
|
|
|
|
# endif
|
2018-09-12 19:36:36 +05:30
|
|
|
|
|
|
|
# define sigrtmin __SIGRTMIN
|
|
|
|
# define sigrtmax __SIGRTMAX
|
|
|
|
# endif
|
|
|
|
if (strncasecmp(name, "RTMIN", 5) == 0) {
|
|
|
|
if (!name[5])
|
|
|
|
return sigrtmin;
|
|
|
|
if (name[5] == '+') {
|
|
|
|
i = bb_strtou(name + 6, NULL, 10);
|
2018-09-12 19:36:37 +05:30
|
|
|
if (i <= sigrtmax - sigrtmin)
|
2018-09-12 19:36:36 +05:30
|
|
|
return sigrtmin + i;
|
|
|
|
}
|
2010-12-30 05:47:03 +05:30
|
|
|
}
|
2018-09-12 19:36:36 +05:30
|
|
|
else if (strncasecmp(name, "RTMAX", 5) == 0) {
|
|
|
|
if (!name[5])
|
|
|
|
return sigrtmax;
|
|
|
|
if (name[5] == '-') {
|
|
|
|
i = bb_strtou(name + 6, NULL, 10);
|
2018-09-12 19:36:37 +05:30
|
|
|
if (i <= sigrtmax - sigrtmin)
|
2018-09-12 19:36:36 +05:30
|
|
|
return sigrtmax - i;
|
|
|
|
}
|
2010-12-30 05:47:03 +05:30
|
|
|
}
|
2018-09-12 19:36:36 +05:30
|
|
|
# undef sigrtmin
|
|
|
|
# undef sigrtmax
|
2007-05-02 01:37:29 +05:30
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-07-13 00:47:55 +05:30
|
|
|
return -1;
|
|
|
|
}
|
2001-08-02 10:48:55 +05:30
|
|
|
|
2006-07-13 00:47:55 +05:30
|
|
|
// Convert signal number to name
|
2001-08-02 10:48:55 +05:30
|
|
|
|
2008-06-27 08:22:20 +05:30
|
|
|
const char* FAST_FUNC get_signame(int number)
|
2001-08-02 10:48:55 +05:30
|
|
|
{
|
2007-06-25 16:25:35 +05:30
|
|
|
if ((unsigned)number < ARRAY_SIZE(signals)) {
|
2007-05-02 01:37:29 +05:30
|
|
|
if (signals[number][0]) /* if it's not an empty str */
|
|
|
|
return signals[number];
|
2001-08-02 10:48:55 +05:30
|
|
|
}
|
2006-07-13 00:47:55 +05:30
|
|
|
|
2007-03-15 03:26:51 +05:30
|
|
|
return itoa(number);
|
2001-08-02 10:48:55 +05:30
|
|
|
}
|
2007-09-30 03:56:01 +05:30
|
|
|
|
|
|
|
|
|
|
|
// Print the whole signal list
|
|
|
|
|
2008-06-27 08:22:20 +05:30
|
|
|
void FAST_FUNC print_signames(void)
|
2007-09-30 03:56:01 +05:30
|
|
|
{
|
2008-05-16 03:00:45 +05:30
|
|
|
unsigned signo;
|
2007-09-30 03:56:01 +05:30
|
|
|
|
2008-05-16 03:00:45 +05:30
|
|
|
for (signo = 1; signo < ARRAY_SIZE(signals); signo++) {
|
2007-09-30 03:56:01 +05:30
|
|
|
const char *name = signals[signo];
|
|
|
|
if (name[0])
|
2011-01-03 17:38:58 +05:30
|
|
|
printf("%2u) %s\n", signo, name);
|
2007-09-30 03:56:01 +05:30
|
|
|
}
|
2011-01-03 17:38:58 +05:30
|
|
|
#if ENABLE_FEATURE_RTMINMAX
|
2018-09-12 19:36:36 +05:30
|
|
|
# if ENABLE_FEATURE_RTMINMAX_USE_LIBC_DEFINITIONS
|
|
|
|
# if defined(SIGRTMIN) && defined(SIGRTMAX)
|
|
|
|
printf("%2u) %s\n", SIGRTMIN, "RTMIN");
|
|
|
|
printf("%2u) %s\n", SIGRTMAX, "RTMAX");
|
|
|
|
# endif
|
|
|
|
# else
|
|
|
|
// __SIGRTMIN is included in signals[] array.
|
|
|
|
# ifdef __SIGRTMAX
|
2011-01-03 17:38:58 +05:30
|
|
|
printf("%2u) %s\n", __SIGRTMAX, "RTMAX");
|
2018-09-12 19:36:36 +05:30
|
|
|
# endif
|
2011-01-03 17:38:58 +05:30
|
|
|
# endif
|
|
|
|
#endif
|
2007-09-30 03:56:01 +05:30
|
|
|
}
|