blob: 915eea579ac1f4d1e18400bdc64d61b5bdc983bb [file] [log] [blame]
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001/* vi: set sw=4 ts=4: */
2/*
Rob Landleyc9c1a412006-07-12 19:17:55 +00003 * Signal name/number conversion routines.
Eric Andersenbdfd0d72001-10-24 05:00:29 +00004 *
Rob Landleyc9c1a412006-07-12 19:17:55 +00005 * Copyright 2006 Rob Landley <rob@landley.net>
Eric Andersenbdfd0d72001-10-24 05:00:29 +00006 *
Bernhard Reutner-Fischer421d9e52006-04-03 16:39:31 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersenbdfd0d72001-10-24 05:00:29 +00008 */
9
Bernhard Reutner-Fischer421d9e52006-04-03 16:39:31 +000010#include "libbb.h"
11
Denis Vlasenkob851c422007-12-04 21:44:52 +000012/* Believe it or not, but some arches have more than 32 SIGs!
13 * HPPA: SIGSTKFLT == 36. */
Denis Vlasenko5f934b02007-12-04 10:05:28 +000014
Denis Vlasenkob851c422007-12-04 21:44:52 +000015static const char signals[][7] = {
Rob Landleyc9c1a412006-07-12 19:17:55 +000016 // SUSv3 says kill must support these, and specifies the numerical values,
17 // http://www.opengroup.org/onlinepubs/009695399/utilities/kill.html
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000018 // {0, "EXIT"}, {1, "HUP"}, {2, "INT"}, {3, "QUIT"},
19 // {6, "ABRT"}, {9, "KILL"}, {14, "ALRM"}, {15, "TERM"}
Rob Landleyc9c1a412006-07-12 19:17:55 +000020 // And Posix adds the following:
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000021 // {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 Vlasenko5f934b02007-12-04 10:05:28 +000025
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000026 [0] = "EXIT",
Denis Vlasenkob851c422007-12-04 21:44:52 +000027#ifdef SIGHUP
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000028 [SIGHUP ] = "HUP",
29#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000030#ifdef SIGINT
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000031 [SIGINT ] = "INT",
32#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000033#ifdef SIGQUIT
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000034 [SIGQUIT ] = "QUIT",
35#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000036#ifdef SIGILL
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000037 [SIGILL ] = "ILL",
38#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000039#ifdef SIGTRAP
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000040 [SIGTRAP ] = "TRAP",
41#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000042#ifdef SIGABRT
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000043 [SIGABRT ] = "ABRT",
44#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000045#ifdef SIGBUS
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000046 [SIGBUS ] = "BUS",
47#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000048#ifdef SIGFPE
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000049 [SIGFPE ] = "FPE",
50#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000051#ifdef SIGKILL
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000052 [SIGKILL ] = "KILL",
53#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000054#ifdef SIGUSR1
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000055 [SIGUSR1 ] = "USR1",
56#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000057#ifdef SIGSEGV
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000058 [SIGSEGV ] = "SEGV",
59#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000060#ifdef SIGUSR2
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000061 [SIGUSR2 ] = "USR2",
62#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000063#ifdef SIGPIPE
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000064 [SIGPIPE ] = "PIPE",
65#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000066#ifdef SIGALRM
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000067 [SIGALRM ] = "ALRM",
68#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000069#ifdef SIGTERM
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000070 [SIGTERM ] = "TERM",
71#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000072#ifdef SIGSTKFLT
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000073 [SIGSTKFLT] = "STKFLT",
74#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000075#ifdef SIGCHLD
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000076 [SIGCHLD ] = "CHLD",
77#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000078#ifdef SIGCONT
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000079 [SIGCONT ] = "CONT",
80#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000081#ifdef SIGSTOP
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000082 [SIGSTOP ] = "STOP",
83#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000084#ifdef SIGTSTP
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000085 [SIGTSTP ] = "TSTP",
86#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000087#ifdef SIGTTIN
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000088 [SIGTTIN ] = "TTIN",
89#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000090#ifdef SIGTTOU
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000091 [SIGTTOU ] = "TTOU",
92#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000093#ifdef SIGURG
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000094 [SIGURG ] = "URG",
95#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000096#ifdef SIGXCPU
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000097 [SIGXCPU ] = "XCPU",
98#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +000099#ifdef SIGXFSZ
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000100 [SIGXFSZ ] = "XFSZ",
101#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +0000102#ifdef SIGVTALRM
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000103 [SIGVTALRM] = "VTALRM",
104#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +0000105#ifdef SIGPROF
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000106 [SIGPROF ] = "PROF",
107#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +0000108#ifdef SIGWINCH
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000109 [SIGWINCH ] = "WINCH",
110#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +0000111#ifdef SIGPOLL
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000112 [SIGPOLL ] = "POLL",
113#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +0000114#ifdef SIGPWR
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000115 [SIGPWR ] = "PWR",
116#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +0000117#ifdef SIGSYS
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000118 [SIGSYS ] = "SYS",
119#endif
Eric Andersen842757d2001-08-02 05:18:55 +0000120};
121
Rob Landleyc9c1a412006-07-12 19:17:55 +0000122// Convert signal name to number.
Eric Andersen842757d2001-08-02 05:18:55 +0000123
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000124int FAST_FUNC get_signum(const char *name)
Eric Andersen842757d2001-08-02 05:18:55 +0000125{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000126 unsigned i;
Eric Andersen842757d2001-08-02 05:18:55 +0000127
Denis Vlasenko2a813e22006-12-23 01:06:21 +0000128 i = bb_strtou(name, NULL, 10);
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000129 if (!errno)
130 return i;
131 if (strncasecmp(name, "SIG", 3) == 0)
132 name += 3;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000133 for (i = 0; i < ARRAY_SIZE(signals); i++)
Denis Vlasenkob851c422007-12-04 21:44:52 +0000134 if (strcasecmp(name, signals[i]) == 0)
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000135 return i;
136
137#if ENABLE_DESKTOP && (defined(SIGIOT) || defined(SIGIO))
Denis Vlasenkob851c422007-12-04 21:44:52 +0000138 /* SIGIO[T] are aliased to other names,
139 * thus cannot be stored in the signals[] array.
140 * Need special code to recognize them */
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000141 if ((name[0] | 0x20) == 'i' && (name[1] | 0x20) == 'o') {
Denis Vlasenkob851c422007-12-04 21:44:52 +0000142#ifdef SIGIO
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000143 if (!name[2])
144 return SIGIO;
145#endif
Denis Vlasenkob851c422007-12-04 21:44:52 +0000146#ifdef SIGIOT
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000147 if ((name[2] | 0x20) == 't' && !name[3])
148 return SIGIOT;
149#endif
150 }
151#endif
152
Rob Landleyc9c1a412006-07-12 19:17:55 +0000153 return -1;
154}
155
156// Convert signal number to name
157
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000158const char* FAST_FUNC get_signame(int number)
Rob Landleyc9c1a412006-07-12 19:17:55 +0000159{
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000160 if ((unsigned)number < ARRAY_SIZE(signals)) {
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000161 if (signals[number][0]) /* if it's not an empty str */
162 return signals[number];
Eric Andersen842757d2001-08-02 05:18:55 +0000163 }
Rob Landleyc9c1a412006-07-12 19:17:55 +0000164
Denis Vlasenko75ab6af2007-03-14 21:56:51 +0000165 return itoa(number);
Eric Andersen842757d2001-08-02 05:18:55 +0000166}
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000167
168
169// Print the whole signal list
170
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000171void FAST_FUNC print_signames(void)
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000172{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000173 unsigned signo;
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000174
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000175 for (signo = 1; signo < ARRAY_SIZE(signals); signo++) {
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000176 const char *name = signals[signo];
177 if (name[0])
178 puts(name);
179 }
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000180}