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 | |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 12 | static struct signal_name { |
| 13 | char *name; |
Eric Andersen | 842757d | 2001-08-02 05:18:55 +0000 | [diff] [blame] | 14 | int number; |
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 |
| 18 | {"0", 0}, {"HUP", 1}, {"INT", 2}, {"QUIT", 3}, {"ABRT", 6}, {"KILL", 9}, |
| 19 | {"ALRM", 14}, {"TERM", 15}, |
| 20 | // And Posix adds the following: |
| 21 | {"ILL", SIGILL}, {"TRAP", SIGTRAP}, {"FPE", SIGFPE}, {"USR1", SIGUSR1}, |
| 22 | {"SEGV", SIGSEGV}, {"USR2", SIGUSR2}, {"PIPE", SIGPIPE}, {"CHLD", SIGCHLD}, |
| 23 | {"CONT", SIGCONT}, {"STOP", SIGSTOP}, {"TSTP", SIGTSTP}, {"TTIN", SIGTTIN}, |
| 24 | {"TTOU", SIGTTOU} |
Eric Andersen | 842757d | 2001-08-02 05:18:55 +0000 | [diff] [blame] | 25 | }; |
| 26 | |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 27 | // Convert signal name to number. |
Eric Andersen | 842757d | 2001-08-02 05:18:55 +0000 | [diff] [blame] | 28 | |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 29 | int get_signum(char *name) |
Eric Andersen | 842757d | 2001-08-02 05:18:55 +0000 | [diff] [blame] | 30 | { |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 31 | int i; |
Eric Andersen | 842757d | 2001-08-02 05:18:55 +0000 | [diff] [blame] | 32 | |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 33 | i = atoi(name); |
| 34 | if(i) return i; |
| 35 | for(i=0; i < sizeof(signals) / sizeof(struct signal_name); i++) |
| 36 | if (!strcasecmp(signals[i].name, name) || |
| 37 | (!strncasecmp(signals[i].name, "SIG", 3) |
| 38 | && !strcasecmp(signals[i].name+3, signals[i].name))) |
| 39 | return signals[i].number; |
| 40 | return -1; |
| 41 | } |
| 42 | |
| 43 | // Convert signal number to name |
| 44 | |
| 45 | char *get_signame(int number) |
| 46 | { |
| 47 | int i; |
| 48 | static char buf[8]; |
| 49 | |
| 50 | itoa_to_buf(number, buf, 8); |
| 51 | for (i=0; i < sizeof(signals) / sizeof(struct signal_name); i++) { |
| 52 | if (number == signals[i].number) { |
| 53 | sprintf("SIG%s", signals[i].name); |
| 54 | break; |
Eric Andersen | 842757d | 2001-08-02 05:18:55 +0000 | [diff] [blame] | 55 | } |
| 56 | } |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 57 | |
| 58 | return buf; |
Eric Andersen | 842757d | 2001-08-02 05:18:55 +0000 | [diff] [blame] | 59 | } |