| 1 | /* vi: set sw=4 ts=4: */
|
|---|
| 2 | /*
|
|---|
| 3 | * Signal name/number conversion routines.
|
|---|
| 4 | *
|
|---|
| 5 | * Copyright 2006 Rob Landley <rob@landley.net>
|
|---|
| 6 | *
|
|---|
| 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
|---|
| 8 | */
|
|---|
| 9 |
|
|---|
| 10 | #include "libbb.h"
|
|---|
| 11 |
|
|---|
| 12 | static const char signals[32][7] = {
|
|---|
| 13 | // SUSv3 says kill must support these, and specifies the numerical values,
|
|---|
| 14 | // http://www.opengroup.org/onlinepubs/009695399/utilities/kill.html
|
|---|
| 15 | // TODO: "[SIG]EXIT" shouldn't work for kill, right?
|
|---|
| 16 | // {0, "EXIT"}, {1, "HUP"}, {2, "INT"}, {3, "QUIT"},
|
|---|
| 17 | // {6, "ABRT"}, {9, "KILL"}, {14, "ALRM"}, {15, "TERM"}
|
|---|
| 18 | // And Posix adds the following:
|
|---|
| 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
|
|---|
| 117 | };
|
|---|
| 118 |
|
|---|
| 119 | // Convert signal name to number.
|
|---|
| 120 |
|
|---|
| 121 | int get_signum(const char *name)
|
|---|
| 122 | {
|
|---|
| 123 | int i;
|
|---|
| 124 |
|
|---|
| 125 | i = bb_strtou(name, NULL, 10);
|
|---|
| 126 | if (!errno)
|
|---|
| 127 | return i;
|
|---|
| 128 | if (strncasecmp(name, "SIG", 3) == 0)
|
|---|
| 129 | name += 3;
|
|---|
| 130 | for (i = 0; i < ARRAY_SIZE(signals); i++)
|
|---|
| 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 |
|
|---|
| 148 | return -1;
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | // Convert signal number to name
|
|---|
| 152 |
|
|---|
| 153 | const char *get_signame(int number)
|
|---|
| 154 | {
|
|---|
| 155 | if ((unsigned)number < ARRAY_SIZE(signals)) {
|
|---|
| 156 | if (signals[number][0]) /* if it's not an empty str */
|
|---|
| 157 | return signals[number];
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | return itoa(number);
|
|---|
| 161 | }
|
|---|