blob: f8eaea76da52311499d1e329e54d60dc6a08e095 [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 Vlasenkodcf4de22007-05-01 20:07:29 +000012static const char signals[32][7] = {
Rob Landleyc9c1a412006-07-12 19:17:55 +000013 // SUSv3 says kill must support these, and specifies the numerical values,
14 // http://www.opengroup.org/onlinepubs/009695399/utilities/kill.html
Denis Vlasenko2a813e22006-12-23 01:06:21 +000015 // TODO: "[SIG]EXIT" shouldn't work for kill, right?
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000016 // {0, "EXIT"}, {1, "HUP"}, {2, "INT"}, {3, "QUIT"},
17 // {6, "ABRT"}, {9, "KILL"}, {14, "ALRM"}, {15, "TERM"}
Rob Landleyc9c1a412006-07-12 19:17:55 +000018 // And Posix adds the following:
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000019 // {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
Eric Andersen842757d2001-08-02 05:18:55 +0000117};
118
Rob Landleyc9c1a412006-07-12 19:17:55 +0000119// Convert signal name to number.
Eric Andersen842757d2001-08-02 05:18:55 +0000120
Denis Vlasenkoa77947f2006-09-27 14:19:16 +0000121int get_signum(const char *name)
Eric Andersen842757d2001-08-02 05:18:55 +0000122{
Rob Landleyc9c1a412006-07-12 19:17:55 +0000123 int i;
Eric Andersen842757d2001-08-02 05:18:55 +0000124
Denis Vlasenko2a813e22006-12-23 01:06:21 +0000125 i = bb_strtou(name, NULL, 10);
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000126 if (!errno)
127 return i;
128 if (strncasecmp(name, "SIG", 3) == 0)
129 name += 3;
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000130 for (i = 0; i < ARRAY_SIZE(signals); i++)
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000131 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
Rob Landleyc9c1a412006-07-12 19:17:55 +0000148 return -1;
149}
150
151// Convert signal number to name
152
Denis Vlasenkoa77947f2006-09-27 14:19:16 +0000153const char *get_signame(int number)
Rob Landleyc9c1a412006-07-12 19:17:55 +0000154{
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000155 if ((unsigned)number < ARRAY_SIZE(signals)) {
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000156 if (signals[number][0]) /* if it's not an empty str */
157 return signals[number];
Eric Andersen842757d2001-08-02 05:18:55 +0000158 }
Rob Landleyc9c1a412006-07-12 19:17:55 +0000159
Denis Vlasenko75ab6af2007-03-14 21:56:51 +0000160 return itoa(number);
Eric Andersen842757d2001-08-02 05:18:55 +0000161}