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 | * |
Bernhard Reutner-Fischer | 421d9e5 | 2006-04-03 16:39:31 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Bernhard Reutner-Fischer | 421d9e5 | 2006-04-03 16:39:31 +0000 | [diff] [blame] | 10 | #include "libbb.h" |
| 11 | |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 12 | /* Believe it or not, but some arches have more than 32 SIGs! |
| 13 | * HPPA: SIGSTKFLT == 36. */ |
Denis Vlasenko | 5f934b0 | 2007-12-04 10:05:28 +0000 | [diff] [blame] | 14 | |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 15 | static const char signals[][7] = { |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 16 | // SUSv3 says kill must support these, and specifies the numerical values, |
| 17 | // http://www.opengroup.org/onlinepubs/009695399/utilities/kill.html |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 18 | // {0, "EXIT"}, {1, "HUP"}, {2, "INT"}, {3, "QUIT"}, |
| 19 | // {6, "ABRT"}, {9, "KILL"}, {14, "ALRM"}, {15, "TERM"} |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 20 | // And Posix adds the following: |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 21 | // {SIGILL, "ILL"}, {SIGTRAP, "TRAP"}, {SIGFPE, "FPE"}, {SIGUSR1, "USR1"}, |
| 22 | // {SIGSEGV, "SEGV"}, {SIGUSR2, "USR2"}, {SIGPIPE, "PIPE"}, {SIGCHLD, "CHLD"}, |
| 23 | // {SIGCONT, "CONT"}, {SIGSTOP, "STOP"}, {SIGTSTP, "TSTP"}, {SIGTTIN, "TTIN"}, |
| 24 | // {SIGTTOU, "TTOU"} |
Denis Vlasenko | 5f934b0 | 2007-12-04 10:05:28 +0000 | [diff] [blame] | 25 | |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 26 | [0] = "EXIT", |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 27 | #ifdef SIGHUP |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 28 | [SIGHUP ] = "HUP", |
| 29 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 30 | #ifdef SIGINT |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 31 | [SIGINT ] = "INT", |
| 32 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 33 | #ifdef SIGQUIT |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 34 | [SIGQUIT ] = "QUIT", |
| 35 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 36 | #ifdef SIGILL |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 37 | [SIGILL ] = "ILL", |
| 38 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 39 | #ifdef SIGTRAP |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 40 | [SIGTRAP ] = "TRAP", |
| 41 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 42 | #ifdef SIGABRT |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 43 | [SIGABRT ] = "ABRT", |
| 44 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 45 | #ifdef SIGBUS |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 46 | [SIGBUS ] = "BUS", |
| 47 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 48 | #ifdef SIGFPE |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 49 | [SIGFPE ] = "FPE", |
| 50 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 51 | #ifdef SIGKILL |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 52 | [SIGKILL ] = "KILL", |
| 53 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 54 | #ifdef SIGUSR1 |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 55 | [SIGUSR1 ] = "USR1", |
| 56 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 57 | #ifdef SIGSEGV |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 58 | [SIGSEGV ] = "SEGV", |
| 59 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 60 | #ifdef SIGUSR2 |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 61 | [SIGUSR2 ] = "USR2", |
| 62 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 63 | #ifdef SIGPIPE |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 64 | [SIGPIPE ] = "PIPE", |
| 65 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 66 | #ifdef SIGALRM |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 67 | [SIGALRM ] = "ALRM", |
| 68 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 69 | #ifdef SIGTERM |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 70 | [SIGTERM ] = "TERM", |
| 71 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 72 | #ifdef SIGSTKFLT |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 73 | [SIGSTKFLT] = "STKFLT", |
| 74 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 75 | #ifdef SIGCHLD |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 76 | [SIGCHLD ] = "CHLD", |
| 77 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 78 | #ifdef SIGCONT |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 79 | [SIGCONT ] = "CONT", |
| 80 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 81 | #ifdef SIGSTOP |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 82 | [SIGSTOP ] = "STOP", |
| 83 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 84 | #ifdef SIGTSTP |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 85 | [SIGTSTP ] = "TSTP", |
| 86 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 87 | #ifdef SIGTTIN |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 88 | [SIGTTIN ] = "TTIN", |
| 89 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 90 | #ifdef SIGTTOU |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 91 | [SIGTTOU ] = "TTOU", |
| 92 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 93 | #ifdef SIGURG |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 94 | [SIGURG ] = "URG", |
| 95 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 96 | #ifdef SIGXCPU |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 97 | [SIGXCPU ] = "XCPU", |
| 98 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 99 | #ifdef SIGXFSZ |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 100 | [SIGXFSZ ] = "XFSZ", |
| 101 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 102 | #ifdef SIGVTALRM |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 103 | [SIGVTALRM] = "VTALRM", |
| 104 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 105 | #ifdef SIGPROF |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 106 | [SIGPROF ] = "PROF", |
| 107 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 108 | #ifdef SIGWINCH |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 109 | [SIGWINCH ] = "WINCH", |
| 110 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 111 | #ifdef SIGPOLL |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 112 | [SIGPOLL ] = "POLL", |
| 113 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 114 | #ifdef SIGPWR |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 115 | [SIGPWR ] = "PWR", |
| 116 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 117 | #ifdef SIGSYS |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 118 | [SIGSYS ] = "SYS", |
| 119 | #endif |
Eric Andersen | 842757d | 2001-08-02 05:18:55 +0000 | [diff] [blame] | 120 | }; |
| 121 | |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 122 | // Convert signal name to number. |
Eric Andersen | 842757d | 2001-08-02 05:18:55 +0000 | [diff] [blame] | 123 | |
Denis Vlasenko | a77947f | 2006-09-27 14:19:16 +0000 | [diff] [blame] | 124 | int get_signum(const char *name) |
Eric Andersen | 842757d | 2001-08-02 05:18:55 +0000 | [diff] [blame] | 125 | { |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 126 | int i; |
Eric Andersen | 842757d | 2001-08-02 05:18:55 +0000 | [diff] [blame] | 127 | |
Denis Vlasenko | 2a813e2 | 2006-12-23 01:06:21 +0000 | [diff] [blame] | 128 | i = bb_strtou(name, NULL, 10); |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 129 | if (!errno) |
| 130 | return i; |
| 131 | if (strncasecmp(name, "SIG", 3) == 0) |
| 132 | name += 3; |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 133 | for (i = 0; i < ARRAY_SIZE(signals); i++) |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 134 | if (strcasecmp(name, signals[i]) == 0) |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 135 | return i; |
| 136 | |
| 137 | #if ENABLE_DESKTOP && (defined(SIGIOT) || defined(SIGIO)) |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 138 | /* SIGIO[T] are aliased to other names, |
| 139 | * thus cannot be stored in the signals[] array. |
| 140 | * Need special code to recognize them */ |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 141 | if ((name[0] | 0x20) == 'i' && (name[1] | 0x20) == 'o') { |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 142 | #ifdef SIGIO |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 143 | if (!name[2]) |
| 144 | return SIGIO; |
| 145 | #endif |
Denis Vlasenko | b851c42 | 2007-12-04 21:44:52 +0000 | [diff] [blame] | 146 | #ifdef SIGIOT |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 147 | if ((name[2] | 0x20) == 't' && !name[3]) |
| 148 | return SIGIOT; |
| 149 | #endif |
| 150 | } |
| 151 | #endif |
| 152 | |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 153 | return -1; |
| 154 | } |
| 155 | |
| 156 | // Convert signal number to name |
| 157 | |
Denis Vlasenko | a77947f | 2006-09-27 14:19:16 +0000 | [diff] [blame] | 158 | const char *get_signame(int number) |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 159 | { |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 160 | if ((unsigned)number < ARRAY_SIZE(signals)) { |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 161 | if (signals[number][0]) /* if it's not an empty str */ |
| 162 | return signals[number]; |
Eric Andersen | 842757d | 2001-08-02 05:18:55 +0000 | [diff] [blame] | 163 | } |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 164 | |
Denis Vlasenko | 75ab6af | 2007-03-14 21:56:51 +0000 | [diff] [blame] | 165 | return itoa(number); |
Eric Andersen | 842757d | 2001-08-02 05:18:55 +0000 | [diff] [blame] | 166 | } |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 167 | |
| 168 | |
| 169 | // Print the whole signal list |
| 170 | |
Denis Vlasenko | a4f4de9 | 2007-09-30 16:32:01 +0000 | [diff] [blame] | 171 | void print_signames(void) |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 172 | { |
| 173 | int signo; |
| 174 | |
| 175 | for (signo = 1; signo < ARRAY_SIZE(signals); signo++) { |
| 176 | const char *name = signals[signo]; |
| 177 | if (name[0]) |
| 178 | puts(name); |
| 179 | } |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 180 | } |