blob: c741f81d718f12c397f64f35c1e2977c9863b0a9 [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 Vlasenko5f934b02007-12-04 10:05:28 +000012#define KILL_MAX_SIG 32
13
Denis Vlasenkoc689fcf2007-12-04 10:20:48 +000014static const char signals[KILL_MAX_SIG][6] = {
Rob Landleyc9c1a412006-07-12 19:17:55 +000015 // SUSv3 says kill must support these, and specifies the numerical values,
16 // http://www.opengroup.org/onlinepubs/009695399/utilities/kill.html
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000017 // {0, "EXIT"}, {1, "HUP"}, {2, "INT"}, {3, "QUIT"},
18 // {6, "ABRT"}, {9, "KILL"}, {14, "ALRM"}, {15, "TERM"}
Rob Landleyc9c1a412006-07-12 19:17:55 +000019 // And Posix adds the following:
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000020 // {SIGILL, "ILL"}, {SIGTRAP, "TRAP"}, {SIGFPE, "FPE"}, {SIGUSR1, "USR1"},
21 // {SIGSEGV, "SEGV"}, {SIGUSR2, "USR2"}, {SIGPIPE, "PIPE"}, {SIGCHLD, "CHLD"},
22 // {SIGCONT, "CONT"}, {SIGSTOP, "STOP"}, {SIGTSTP, "TSTP"}, {SIGTTIN, "TTIN"},
23 // {SIGTTOU, "TTOU"}
Denis Vlasenko5f934b02007-12-04 10:05:28 +000024
25/* Believe it or not, but some arches have more than 32 SIGs!
26 * HPPA: SIGSTKFLT == 36. We don't include those. */
Denis Vlasenkoc689fcf2007-12-04 10:20:48 +000027
28/* NB: longest (6-char) names are NOT nul-terminated */
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000029 [0] = "EXIT",
Denis Vlasenko5f934b02007-12-04 10:05:28 +000030#if defined SIGHUP && SIGHUP < KILL_MAX_SIG
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000031 [SIGHUP ] = "HUP",
32#endif
Denis Vlasenko5f934b02007-12-04 10:05:28 +000033#if defined SIGINT && SIGINT < KILL_MAX_SIG
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000034 [SIGINT ] = "INT",
35#endif
Denis Vlasenko5f934b02007-12-04 10:05:28 +000036#if defined SIGQUIT && SIGQUIT < KILL_MAX_SIG
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000037 [SIGQUIT ] = "QUIT",
38#endif
Denis Vlasenko5f934b02007-12-04 10:05:28 +000039#if defined SIGILL && SIGILL < KILL_MAX_SIG
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000040 [SIGILL ] = "ILL",
41#endif
Denis Vlasenko5f934b02007-12-04 10:05:28 +000042#if defined SIGTRAP && SIGTRAP < KILL_MAX_SIG
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000043 [SIGTRAP ] = "TRAP",
44#endif
Denis Vlasenko5f934b02007-12-04 10:05:28 +000045#if defined SIGABRT && SIGABRT < KILL_MAX_SIG
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000046 [SIGABRT ] = "ABRT",
47#endif
Denis Vlasenko5f934b02007-12-04 10:05:28 +000048#if defined SIGBUS && SIGBUS < KILL_MAX_SIG
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000049 [SIGBUS ] = "BUS",
50#endif
Denis Vlasenko5f934b02007-12-04 10:05:28 +000051#if defined SIGFPE && SIGFPE < KILL_MAX_SIG
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000052 [SIGFPE ] = "FPE",
53#endif
Denis Vlasenko5f934b02007-12-04 10:05:28 +000054#if defined SIGKILL && SIGKILL < KILL_MAX_SIG
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000055 [SIGKILL ] = "KILL",
56#endif
Denis Vlasenko5f934b02007-12-04 10:05:28 +000057#if defined SIGUSR1 && SIGUSR1 < KILL_MAX_SIG
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000058 [SIGUSR1 ] = "USR1",
59#endif
Denis Vlasenko5f934b02007-12-04 10:05:28 +000060#if defined SIGSEGV && SIGSEGV < KILL_MAX_SIG
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000061 [SIGSEGV ] = "SEGV",
62#endif
Denis Vlasenko5f934b02007-12-04 10:05:28 +000063#if defined SIGUSR2 && SIGUSR2 < KILL_MAX_SIG
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000064 [SIGUSR2 ] = "USR2",
65#endif
Denis Vlasenko5f934b02007-12-04 10:05:28 +000066#if defined SIGPIPE && SIGPIPE < KILL_MAX_SIG
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000067 [SIGPIPE ] = "PIPE",
68#endif
Denis Vlasenko5f934b02007-12-04 10:05:28 +000069#if defined SIGALRM && SIGALRM < KILL_MAX_SIG
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000070 [SIGALRM ] = "ALRM",
71#endif
Denis Vlasenko5f934b02007-12-04 10:05:28 +000072#if defined SIGTERM && SIGTERM < KILL_MAX_SIG
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000073 [SIGTERM ] = "TERM",
74#endif
Denis Vlasenko5f934b02007-12-04 10:05:28 +000075#if defined SIGSTKFLT && SIGSTKFLT < KILL_MAX_SIG
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000076 [SIGSTKFLT] = "STKFLT",
77#endif
Denis Vlasenko5f934b02007-12-04 10:05:28 +000078#if defined SIGCHLD && SIGCHLD < KILL_MAX_SIG
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000079 [SIGCHLD ] = "CHLD",
80#endif
Denis Vlasenko5f934b02007-12-04 10:05:28 +000081#if defined SIGCONT && SIGCONT < KILL_MAX_SIG
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000082 [SIGCONT ] = "CONT",
83#endif
Denis Vlasenko5f934b02007-12-04 10:05:28 +000084#if defined SIGSTOP && SIGSTOP < KILL_MAX_SIG
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000085 [SIGSTOP ] = "STOP",
86#endif
Denis Vlasenko5f934b02007-12-04 10:05:28 +000087#if defined SIGTSTP && SIGTSTP < KILL_MAX_SIG
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000088 [SIGTSTP ] = "TSTP",
89#endif
Denis Vlasenko5f934b02007-12-04 10:05:28 +000090#if defined SIGTTIN && SIGTTIN < KILL_MAX_SIG
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000091 [SIGTTIN ] = "TTIN",
92#endif
Denis Vlasenko5f934b02007-12-04 10:05:28 +000093#if defined SIGTTOU && SIGTTOU < KILL_MAX_SIG
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000094 [SIGTTOU ] = "TTOU",
95#endif
Denis Vlasenko5f934b02007-12-04 10:05:28 +000096#if defined SIGURG && SIGURG < KILL_MAX_SIG
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000097 [SIGURG ] = "URG",
98#endif
Denis Vlasenko5f934b02007-12-04 10:05:28 +000099#if defined SIGXCPU && SIGXCPU < KILL_MAX_SIG
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000100 [SIGXCPU ] = "XCPU",
101#endif
Denis Vlasenko5f934b02007-12-04 10:05:28 +0000102#if defined SIGXFSZ && SIGXFSZ < KILL_MAX_SIG
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000103 [SIGXFSZ ] = "XFSZ",
104#endif
Denis Vlasenko5f934b02007-12-04 10:05:28 +0000105#if defined SIGVTALRM && SIGVTALRM < KILL_MAX_SIG
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000106 [SIGVTALRM] = "VTALRM",
107#endif
Denis Vlasenko5f934b02007-12-04 10:05:28 +0000108#if defined SIGPROF && SIGPROF < KILL_MAX_SIG
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000109 [SIGPROF ] = "PROF",
110#endif
Denis Vlasenko5f934b02007-12-04 10:05:28 +0000111#if defined SIGWINCH && SIGWINCH < KILL_MAX_SIG
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000112 [SIGWINCH ] = "WINCH",
113#endif
Denis Vlasenko5f934b02007-12-04 10:05:28 +0000114#if defined SIGPOLL && SIGPOLL < KILL_MAX_SIG
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000115 [SIGPOLL ] = "POLL",
116#endif
Denis Vlasenko5f934b02007-12-04 10:05:28 +0000117#if defined SIGPWR && SIGPWR < KILL_MAX_SIG
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000118 [SIGPWR ] = "PWR",
119#endif
Denis Vlasenko5f934b02007-12-04 10:05:28 +0000120#if defined SIGSYS && SIGSYS < KILL_MAX_SIG
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000121 [SIGSYS ] = "SYS",
122#endif
Eric Andersen842757d2001-08-02 05:18:55 +0000123};
124
Rob Landleyc9c1a412006-07-12 19:17:55 +0000125// Convert signal name to number.
Eric Andersen842757d2001-08-02 05:18:55 +0000126
Denis Vlasenkoa77947f2006-09-27 14:19:16 +0000127int get_signum(const char *name)
Eric Andersen842757d2001-08-02 05:18:55 +0000128{
Rob Landleyc9c1a412006-07-12 19:17:55 +0000129 int i;
Eric Andersen842757d2001-08-02 05:18:55 +0000130
Denis Vlasenko2a813e22006-12-23 01:06:21 +0000131 i = bb_strtou(name, NULL, 10);
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000132 if (!errno)
133 return i;
134 if (strncasecmp(name, "SIG", 3) == 0)
135 name += 3;
Denis Vlasenkoc689fcf2007-12-04 10:20:48 +0000136 if (strlen(name) > 6)
137 return -1;
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000138 for (i = 0; i < ARRAY_SIZE(signals); i++)
Denis Vlasenkoc689fcf2007-12-04 10:20:48 +0000139 if (strncasecmp(name, signals[i], 6) == 0)
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000140 return i;
141
142#if ENABLE_DESKTOP && (defined(SIGIOT) || defined(SIGIO))
143 /* These are aliased to other names */
144 if ((name[0] | 0x20) == 'i' && (name[1] | 0x20) == 'o') {
Denis Vlasenko5f934b02007-12-04 10:05:28 +0000145#if defined SIGIO
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000146 if (!name[2])
147 return SIGIO;
148#endif
Denis Vlasenko5f934b02007-12-04 10:05:28 +0000149#if defined SIGIOT
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000150 if ((name[2] | 0x20) == 't' && !name[3])
151 return SIGIOT;
152#endif
153 }
154#endif
155
Rob Landleyc9c1a412006-07-12 19:17:55 +0000156 return -1;
157}
158
159// Convert signal number to name
160
Denis Vlasenkoa77947f2006-09-27 14:19:16 +0000161const char *get_signame(int number)
Rob Landleyc9c1a412006-07-12 19:17:55 +0000162{
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000163 if ((unsigned)number < ARRAY_SIZE(signals)) {
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000164 if (signals[number][0]) /* if it's not an empty str */
165 return signals[number];
Eric Andersen842757d2001-08-02 05:18:55 +0000166 }
Rob Landleyc9c1a412006-07-12 19:17:55 +0000167
Denis Vlasenko75ab6af2007-03-14 21:56:51 +0000168 return itoa(number);
Eric Andersen842757d2001-08-02 05:18:55 +0000169}
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000170
171
172// Print the whole signal list
173
Denis Vlasenkoa4f4de92007-09-30 16:32:01 +0000174void print_signames(void)
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000175{
176 int signo;
177
178 for (signo = 1; signo < ARRAY_SIZE(signals); signo++) {
179 const char *name = signals[signo];
180 if (name[0])
181 puts(name);
182 }
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000183}