blob: 52861d40562e595ae5b450f2be6990dfc8fbd11b [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 Vlasenkoa77947f2006-09-27 14:19:16 +000012static const struct signal_name {
Eric Andersen842757d2001-08-02 05:18:55 +000013 int number;
Bernhard Reutner-Fischerbbda98f2006-12-04 15:58:27 +000014 char name[5];
Rob Landleyc9c1a412006-07-12 19:17:55 +000015} signals[] = {
16 // SUSv3 says kill must support these, and specifies the numerical values,
17 // http://www.opengroup.org/onlinepubs/009695399/utilities/kill.html
Denis Vlasenko2a813e22006-12-23 01:06:21 +000018 // TODO: "[SIG]EXIT" shouldn't work for kill, right?
19 {0, "EXIT"}, {1, "HUP"}, {2, "INT"}, {3, "QUIT"}, {6, "ABRT"}, {9, "KILL"},
Bernhard Reutner-Fischerbbda98f2006-12-04 15:58:27 +000020 {14, "ALRM"}, {15, "TERM"},
Rob Landleyc9c1a412006-07-12 19:17:55 +000021 // And Posix adds the following:
Bernhard Reutner-Fischerbbda98f2006-12-04 15:58:27 +000022 {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 Andersen842757d2001-08-02 05:18:55 +000026};
27
Rob Landleyc9c1a412006-07-12 19:17:55 +000028// Convert signal name to number.
Eric Andersen842757d2001-08-02 05:18:55 +000029
Denis Vlasenkoa77947f2006-09-27 14:19:16 +000030int get_signum(const char *name)
Eric Andersen842757d2001-08-02 05:18:55 +000031{
Rob Landleyc9c1a412006-07-12 19:17:55 +000032 int i;
Eric Andersen842757d2001-08-02 05:18:55 +000033
Denis Vlasenko2a813e22006-12-23 01:06:21 +000034 i = bb_strtou(name, NULL, 10);
35 if (!errno) return i;
Bernhard Reutner-Fischerbbda98f2006-12-04 15:58:27 +000036 for (i = 0; i < sizeof(signals) / sizeof(struct signal_name); i++)
Denis Vlasenko2a813e22006-12-23 01:06:21 +000037 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 Landleyc9c1a412006-07-12 19:17:55 +000041 return -1;
42}
43
44// Convert signal number to name
45
Denis Vlasenkoa77947f2006-09-27 14:19:16 +000046const char *get_signame(int number)
Rob Landleyc9c1a412006-07-12 19:17:55 +000047{
48 int i;
Rob Landleyc9c1a412006-07-12 19:17:55 +000049
Rob Landleyc9c1a412006-07-12 19:17:55 +000050 for (i=0; i < sizeof(signals) / sizeof(struct signal_name); i++) {
51 if (number == signals[i].number) {
Denis Vlasenkoa77947f2006-09-27 14:19:16 +000052 return signals[i].name;
Eric Andersen842757d2001-08-02 05:18:55 +000053 }
54 }
Rob Landleyc9c1a412006-07-12 19:17:55 +000055
Denis Vlasenko75ab6af2007-03-14 21:56:51 +000056 return itoa(number);
Eric Andersen842757d2001-08-02 05:18:55 +000057}