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 | a77947f | 2006-09-27 14:19:16 +0000 | [diff] [blame] | 12 | static const struct signal_name { |
Eric Andersen | 842757d | 2001-08-02 05:18:55 +0000 | [diff] [blame] | 13 | int number; |
Bernhard Reutner-Fischer | bbda98f | 2006-12-04 15:58:27 +0000 | [diff] [blame] | 14 | char name[5]; |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 15 | } signals[] = { |
| 16 | // SUSv3 says kill must support these, and specifies the numerical values, |
| 17 | // http://www.opengroup.org/onlinepubs/009695399/utilities/kill.html |
Denis Vlasenko | 2a813e2 | 2006-12-23 01:06:21 +0000 | [diff] [blame] | 18 | // TODO: "[SIG]EXIT" shouldn't work for kill, right? |
| 19 | {0, "EXIT"}, {1, "HUP"}, {2, "INT"}, {3, "QUIT"}, {6, "ABRT"}, {9, "KILL"}, |
Bernhard Reutner-Fischer | bbda98f | 2006-12-04 15:58:27 +0000 | [diff] [blame] | 20 | {14, "ALRM"}, {15, "TERM"}, |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 21 | // And Posix adds the following: |
Bernhard Reutner-Fischer | bbda98f | 2006-12-04 15:58:27 +0000 | [diff] [blame] | 22 | {SIGILL, "ILL"}, {SIGTRAP, "TRAP"}, {SIGFPE, "FPE"}, {SIGUSR1, "USR1"}, |
| 23 | {SIGSEGV, "SEGV"}, {SIGUSR2, "USR2"}, {SIGPIPE, "PIPE"}, {SIGCHLD, "CHLD"}, |
| 24 | {SIGCONT, "CONT"}, {SIGSTOP, "STOP"}, {SIGTSTP, "TSTP"}, {SIGTTIN, "TTIN"}, |
| 25 | {SIGTTOU, "TTOU"} |
Eric Andersen | 842757d | 2001-08-02 05:18:55 +0000 | [diff] [blame] | 26 | }; |
| 27 | |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 28 | // Convert signal name to number. |
Eric Andersen | 842757d | 2001-08-02 05:18:55 +0000 | [diff] [blame] | 29 | |
Denis Vlasenko | a77947f | 2006-09-27 14:19:16 +0000 | [diff] [blame] | 30 | int get_signum(const char *name) |
Eric Andersen | 842757d | 2001-08-02 05:18:55 +0000 | [diff] [blame] | 31 | { |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 32 | int i; |
Eric Andersen | 842757d | 2001-08-02 05:18:55 +0000 | [diff] [blame] | 33 | |
Denis Vlasenko | 2a813e2 | 2006-12-23 01:06:21 +0000 | [diff] [blame] | 34 | i = bb_strtou(name, NULL, 10); |
| 35 | if (!errno) return i; |
Bernhard Reutner-Fischer | bbda98f | 2006-12-04 15:58:27 +0000 | [diff] [blame] | 36 | for (i = 0; i < sizeof(signals) / sizeof(struct signal_name); i++) |
Denis Vlasenko | 2a813e2 | 2006-12-23 01:06:21 +0000 | [diff] [blame] | 37 | if (strcasecmp(name, signals[i].name) == 0 |
| 38 | || (strncasecmp(name, "SIG", 3) == 0 |
| 39 | && strcasecmp(&name[3], signals[i].name) == 0)) |
| 40 | return signals[i].number; |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 41 | return -1; |
| 42 | } |
| 43 | |
| 44 | // Convert signal number to name |
| 45 | |
Denis Vlasenko | a77947f | 2006-09-27 14:19:16 +0000 | [diff] [blame] | 46 | const char *get_signame(int number) |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 47 | { |
| 48 | int i; |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 49 | |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 50 | for (i=0; i < sizeof(signals) / sizeof(struct signal_name); i++) { |
| 51 | if (number == signals[i].number) { |
Denis Vlasenko | a77947f | 2006-09-27 14:19:16 +0000 | [diff] [blame] | 52 | return signals[i].name; |
Eric Andersen | 842757d | 2001-08-02 05:18:55 +0000 | [diff] [blame] | 53 | } |
| 54 | } |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 55 | |
Denis Vlasenko | 75ab6af | 2007-03-14 21:56:51 +0000 | [diff] [blame^] | 56 | return itoa(number); |
Eric Andersen | 842757d | 2001-08-02 05:18:55 +0000 | [diff] [blame] | 57 | } |