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