Initial public busybox upstream commit
[busybox4maemo] / libbb / u_signal_names.c
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 /* Believe it or not, but some arches have more than 32 SIGs!
13  * HPPA: SIGSTKFLT == 36. */
14
15 static const char signals[][7] = {
16         // SUSv3 says kill must support these, and specifies the numerical values,
17         // http://www.opengroup.org/onlinepubs/009695399/utilities/kill.html
18         // {0, "EXIT"}, {1, "HUP"}, {2, "INT"}, {3, "QUIT"},
19         // {6, "ABRT"}, {9, "KILL"}, {14, "ALRM"}, {15, "TERM"}
20         // And Posix adds the following:
21         // {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"}
25
26         [0] = "EXIT",
27 #ifdef SIGHUP
28         [SIGHUP   ] = "HUP",
29 #endif
30 #ifdef SIGINT
31         [SIGINT   ] = "INT",
32 #endif
33 #ifdef SIGQUIT
34         [SIGQUIT  ] = "QUIT",
35 #endif
36 #ifdef SIGILL
37         [SIGILL   ] = "ILL",
38 #endif
39 #ifdef SIGTRAP
40         [SIGTRAP  ] = "TRAP",
41 #endif
42 #ifdef SIGABRT
43         [SIGABRT  ] = "ABRT",
44 #endif
45 #ifdef SIGBUS
46         [SIGBUS   ] = "BUS",
47 #endif
48 #ifdef SIGFPE
49         [SIGFPE   ] = "FPE",
50 #endif
51 #ifdef SIGKILL
52         [SIGKILL  ] = "KILL",
53 #endif
54 #ifdef SIGUSR1
55         [SIGUSR1  ] = "USR1",
56 #endif
57 #ifdef SIGSEGV
58         [SIGSEGV  ] = "SEGV",
59 #endif
60 #ifdef SIGUSR2
61         [SIGUSR2  ] = "USR2",
62 #endif
63 #ifdef SIGPIPE
64         [SIGPIPE  ] = "PIPE",
65 #endif
66 #ifdef SIGALRM
67         [SIGALRM  ] = "ALRM",
68 #endif
69 #ifdef SIGTERM
70         [SIGTERM  ] = "TERM",
71 #endif
72 #ifdef SIGSTKFLT
73         [SIGSTKFLT] = "STKFLT",
74 #endif
75 #ifdef SIGCHLD
76         [SIGCHLD  ] = "CHLD",
77 #endif
78 #ifdef SIGCONT
79         [SIGCONT  ] = "CONT",
80 #endif
81 #ifdef SIGSTOP
82         [SIGSTOP  ] = "STOP",
83 #endif
84 #ifdef SIGTSTP
85         [SIGTSTP  ] = "TSTP",
86 #endif
87 #ifdef SIGTTIN
88         [SIGTTIN  ] = "TTIN",
89 #endif
90 #ifdef SIGTTOU
91         [SIGTTOU  ] = "TTOU",
92 #endif
93 #ifdef SIGURG
94         [SIGURG   ] = "URG",
95 #endif
96 #ifdef SIGXCPU
97         [SIGXCPU  ] = "XCPU",
98 #endif
99 #ifdef SIGXFSZ
100         [SIGXFSZ  ] = "XFSZ",
101 #endif
102 #ifdef SIGVTALRM
103         [SIGVTALRM] = "VTALRM",
104 #endif
105 #ifdef SIGPROF
106         [SIGPROF  ] = "PROF",
107 #endif
108 #ifdef SIGWINCH
109         [SIGWINCH ] = "WINCH",
110 #endif
111 #ifdef SIGPOLL
112         [SIGPOLL  ] = "POLL",
113 #endif
114 #ifdef SIGPWR
115         [SIGPWR   ] = "PWR",
116 #endif
117 #ifdef SIGSYS
118         [SIGSYS   ] = "SYS",
119 #endif
120 };
121
122 // Convert signal name to number.
123
124 int get_signum(const char *name)
125 {
126         int i;
127
128         i = bb_strtou(name, NULL, 10);
129         if (!errno)
130                 return i;
131         if (strncasecmp(name, "SIG", 3) == 0)
132                 name += 3;
133         for (i = 0; i < ARRAY_SIZE(signals); i++)
134                 if (strcasecmp(name, signals[i]) == 0)
135                         return i;
136
137 #if ENABLE_DESKTOP && (defined(SIGIOT) || defined(SIGIO))
138         /* SIGIO[T] are aliased to other names,
139          * thus cannot be stored in the signals[] array.
140          * Need special code to recognize them */
141         if ((name[0] | 0x20) == 'i' && (name[1] | 0x20) == 'o') {
142 #ifdef SIGIO
143                 if (!name[2])
144                         return SIGIO;
145 #endif
146 #ifdef SIGIOT
147                 if ((name[2] | 0x20) == 't' && !name[3])
148                         return SIGIOT;
149 #endif
150         }
151 #endif
152
153         return -1;
154 }
155
156 // Convert signal number to name
157
158 const char *get_signame(int number)
159 {
160         if ((unsigned)number < ARRAY_SIZE(signals)) {
161                 if (signals[number][0]) /* if it's not an empty str */
162                         return signals[number];
163         }
164
165         return itoa(number);
166 }
167
168
169 // Print the whole signal list
170
171 void print_signames(void)
172 {
173         int signo;
174
175         for (signo = 1; signo < ARRAY_SIZE(signals); signo++) {
176                 const char *name = signals[signo];
177                 if (name[0])
178                         puts(name);
179         }
180 }