Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 3 | * Signal name/number conversion routines. |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 4 | * |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 5 | * Copyright 2006 Rob Landley <rob@landley.net> |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Denys Vlasenko | 2753aae | 2010-12-30 01:17:03 +0100 | [diff] [blame] | 10 | //config:config FEATURE_RTMINMAX |
| 11 | //config: bool "Support RTMIN[+n] and RTMAX[-n] signal names" |
| 12 | //config: default y |
| 13 | //config: help |
| 14 | //config: Support RTMIN[+n] and RTMAX[-n] signal names |
| 15 | //config: in kill, killall etc. This costs ~250 bytes. |
| 16 | |
Bernhard Reutner-Fischer | 421d9e5 | 2006-04-03 16:39:31 +0000 | [diff] [blame] | 17 | #include "libbb.h" |
| 18 | |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 19 | /* Believe it or not, but some arches have more than 32 SIGs! |
| 20 | * HPPA: SIGSTKFLT == 36. */ |
Denis Vlasenko | 5f934b0 | 2007-12-04 10:05:28 +0000 | [diff] [blame] | 21 | |
Denys Vlasenko | 3e134eb | 2016-04-22 18:09:21 +0200 | [diff] [blame] | 22 | static const char signals[][7] ALIGN1 = { |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 23 | // SUSv3 says kill must support these, and specifies the numerical values, |
| 24 | // http://www.opengroup.org/onlinepubs/009695399/utilities/kill.html |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 25 | // {0, "EXIT"}, {1, "HUP"}, {2, "INT"}, {3, "QUIT"}, |
| 26 | // {6, "ABRT"}, {9, "KILL"}, {14, "ALRM"}, {15, "TERM"} |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 27 | // And Posix adds the following: |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 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"}, |
| 31 | // {SIGTTOU, "TTOU"} |
Denis Vlasenko | 5f934b0 | 2007-12-04 10:05:28 +0000 | [diff] [blame] | 32 | |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 33 | [0] = "EXIT", |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 34 | #ifdef SIGHUP |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 35 | [SIGHUP ] = "HUP", |
| 36 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 37 | #ifdef SIGINT |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 38 | [SIGINT ] = "INT", |
| 39 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 40 | #ifdef SIGQUIT |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 41 | [SIGQUIT ] = "QUIT", |
| 42 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 43 | #ifdef SIGILL |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 44 | [SIGILL ] = "ILL", |
| 45 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 46 | #ifdef SIGTRAP |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 47 | [SIGTRAP ] = "TRAP", |
| 48 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 49 | #ifdef SIGABRT |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 50 | [SIGABRT ] = "ABRT", |
| 51 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 52 | #ifdef SIGBUS |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 53 | [SIGBUS ] = "BUS", |
| 54 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 55 | #ifdef SIGFPE |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 56 | [SIGFPE ] = "FPE", |
| 57 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 58 | #ifdef SIGKILL |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 59 | [SIGKILL ] = "KILL", |
| 60 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 61 | #ifdef SIGUSR1 |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 62 | [SIGUSR1 ] = "USR1", |
| 63 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 64 | #ifdef SIGSEGV |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 65 | [SIGSEGV ] = "SEGV", |
| 66 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 67 | #ifdef SIGUSR2 |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 68 | [SIGUSR2 ] = "USR2", |
| 69 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 70 | #ifdef SIGPIPE |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 71 | [SIGPIPE ] = "PIPE", |
| 72 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 73 | #ifdef SIGALRM |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 74 | [SIGALRM ] = "ALRM", |
| 75 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 76 | #ifdef SIGTERM |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 77 | [SIGTERM ] = "TERM", |
| 78 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 79 | #ifdef SIGSTKFLT |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 80 | [SIGSTKFLT] = "STKFLT", |
| 81 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 82 | #ifdef SIGCHLD |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 83 | [SIGCHLD ] = "CHLD", |
| 84 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 85 | #ifdef SIGCONT |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 86 | [SIGCONT ] = "CONT", |
| 87 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 88 | #ifdef SIGSTOP |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 89 | [SIGSTOP ] = "STOP", |
| 90 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 91 | #ifdef SIGTSTP |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 92 | [SIGTSTP ] = "TSTP", |
| 93 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 94 | #ifdef SIGTTIN |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 95 | [SIGTTIN ] = "TTIN", |
| 96 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 97 | #ifdef SIGTTOU |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 98 | [SIGTTOU ] = "TTOU", |
| 99 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 100 | #ifdef SIGURG |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 101 | [SIGURG ] = "URG", |
| 102 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 103 | #ifdef SIGXCPU |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 104 | [SIGXCPU ] = "XCPU", |
| 105 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 106 | #ifdef SIGXFSZ |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 107 | [SIGXFSZ ] = "XFSZ", |
| 108 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 109 | #ifdef SIGVTALRM |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 110 | [SIGVTALRM] = "VTALRM", |
| 111 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 112 | #ifdef SIGPROF |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 113 | [SIGPROF ] = "PROF", |
| 114 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 115 | #ifdef SIGWINCH |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 116 | [SIGWINCH ] = "WINCH", |
| 117 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 118 | #ifdef SIGPOLL |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 119 | [SIGPOLL ] = "POLL", |
| 120 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 121 | #ifdef SIGPWR |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 122 | [SIGPWR ] = "PWR", |
| 123 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 124 | #ifdef SIGSYS |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 125 | [SIGSYS ] = "SYS", |
| 126 | #endif |
Denys Vlasenko | 327d288 | 2011-01-03 13:08:58 +0100 | [diff] [blame] | 127 | #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 Andersen | 842757d | 2001-08-02 05:18:55 +0000 | [diff] [blame] | 137 | }; |
| 138 | |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 139 | // Convert signal name to number. |
Eric Andersen | 842757d | 2001-08-02 05:18:55 +0000 | [diff] [blame] | 140 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 141 | int FAST_FUNC get_signum(const char *name) |
Eric Andersen | 842757d | 2001-08-02 05:18:55 +0000 | [diff] [blame] | 142 | { |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 143 | unsigned i; |
Eric Andersen | 842757d | 2001-08-02 05:18:55 +0000 | [diff] [blame] | 144 | |
Denis Vlasenko | 2a813e2 | 2006-12-23 01:06:21 +0000 | [diff] [blame] | 145 | i = bb_strtou(name, NULL, 10); |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 146 | if (!errno) |
| 147 | return i; |
| 148 | if (strncasecmp(name, "SIG", 3) == 0) |
| 149 | name += 3; |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 150 | for (i = 0; i < ARRAY_SIZE(signals); i++) |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 151 | if (strcasecmp(name, signals[i]) == 0) |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 152 | return i; |
| 153 | |
Denys Vlasenko | 2753aae | 2010-12-30 01:17:03 +0100 | [diff] [blame] | 154 | #if ENABLE_DESKTOP |
| 155 | # if defined(SIGIOT) || defined(SIGIO) |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 156 | /* SIGIO[T] are aliased to other names, |
| 157 | * thus cannot be stored in the signals[] array. |
| 158 | * Need special code to recognize them */ |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 159 | if ((name[0] | 0x20) == 'i' && (name[1] | 0x20) == 'o') { |
Denys Vlasenko | 2753aae | 2010-12-30 01:17:03 +0100 | [diff] [blame] | 160 | # ifdef SIGIO |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 161 | if (!name[2]) |
| 162 | return SIGIO; |
Denys Vlasenko | 2753aae | 2010-12-30 01:17:03 +0100 | [diff] [blame] | 163 | # endif |
| 164 | # ifdef SIGIOT |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 165 | if ((name[2] | 0x20) == 't' && !name[3]) |
| 166 | return SIGIOT; |
Denys Vlasenko | 2753aae | 2010-12-30 01:17:03 +0100 | [diff] [blame] | 167 | # endif |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 168 | } |
Denys Vlasenko | 2753aae | 2010-12-30 01:17:03 +0100 | [diff] [blame] | 169 | # endif |
| 170 | #endif |
| 171 | |
| 172 | #if ENABLE_FEATURE_RTMINMAX |
| 173 | # if defined(SIGRTMIN) && defined(SIGRTMAX) |
Denys Vlasenko | 7b276fc | 2011-01-03 12:51:13 +0100 | [diff] [blame] | 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 |
| 179 | # endif |
| 180 | # if !defined(__SIGRTMAX) |
| 181 | # define __SIGRTMAX SIGRTMAX |
| 182 | # endif |
Denys Vlasenko | 2753aae | 2010-12-30 01:17:03 +0100 | [diff] [blame] | 183 | if (strncasecmp(name, "RTMIN", 5) == 0) { |
| 184 | if (!name[5]) |
Denys Vlasenko | 7b276fc | 2011-01-03 12:51:13 +0100 | [diff] [blame] | 185 | return __SIGRTMIN; |
Denys Vlasenko | 2753aae | 2010-12-30 01:17:03 +0100 | [diff] [blame] | 186 | if (name[5] == '+') { |
| 187 | i = bb_strtou(name + 6, NULL, 10); |
Denys Vlasenko | 7b276fc | 2011-01-03 12:51:13 +0100 | [diff] [blame] | 188 | 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 Vlasenko | 2753aae | 2010-12-30 01:17:03 +0100 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | # endif |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 202 | #endif |
| 203 | |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 204 | return -1; |
| 205 | } |
| 206 | |
| 207 | // Convert signal number to name |
| 208 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 209 | const char* FAST_FUNC get_signame(int number) |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 210 | { |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 211 | if ((unsigned)number < ARRAY_SIZE(signals)) { |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 212 | if (signals[number][0]) /* if it's not an empty str */ |
| 213 | return signals[number]; |
Eric Andersen | 842757d | 2001-08-02 05:18:55 +0000 | [diff] [blame] | 214 | } |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 215 | |
Denis Vlasenko | 75ab6af | 2007-03-14 21:56:51 +0000 | [diff] [blame] | 216 | return itoa(number); |
Eric Andersen | 842757d | 2001-08-02 05:18:55 +0000 | [diff] [blame] | 217 | } |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 218 | |
| 219 | |
| 220 | // Print the whole signal list |
| 221 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 222 | void FAST_FUNC print_signames(void) |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 223 | { |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 224 | unsigned signo; |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 225 | |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 226 | for (signo = 1; signo < ARRAY_SIZE(signals); signo++) { |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 227 | const char *name = signals[signo]; |
| 228 | if (name[0]) |
Denys Vlasenko | 327d288 | 2011-01-03 13:08:58 +0100 | [diff] [blame] | 229 | printf("%2u) %s\n", signo, name); |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 230 | } |
Denys Vlasenko | 327d288 | 2011-01-03 13:08:58 +0100 | [diff] [blame] | 231 | #if ENABLE_FEATURE_RTMINMAX |
| 232 | # ifdef __SIGRTMAX |
| 233 | printf("%2u) %s\n", __SIGRTMAX, "RTMAX"); |
| 234 | # endif |
| 235 | #endif |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 236 | } |