| 1 |
/* |
| 2 |
* signals.c - signal name, and number, conversions |
| 3 |
* Copyright 1998-2003 by Albert Cahalan |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2.1 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Lesser General Public |
| 16 |
* License along with this library; if not, write to the Free Software |
| 17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <ctype.h> |
| 21 |
#include <signal.h> |
| 22 |
#include <string.h> |
| 23 |
#include <stdio.h> |
| 24 |
#include <stdlib.h> |
| 25 |
#include "signals.h" |
| 26 |
#include "c.h" |
| 27 |
|
| 28 |
/* Linux signals: |
| 29 |
* |
| 30 |
* SIGSYS is required by Unix98. |
| 31 |
* SIGEMT is part of SysV, BSD, and ancient UNIX tradition. |
| 32 |
* |
| 33 |
* They are provided by these Linux ports: alpha, mips, sparc, and sparc64. |
| 34 |
* You get SIGSTKFLT and SIGUNUSED instead on i386, m68k, ppc, and arm. |
| 35 |
* (this is a Linux & libc bug -- both must be fixed) |
| 36 |
* |
| 37 |
* Total garbage: SIGIO SIGINFO SIGIOT SIGCLD |
| 38 |
* (popular ones are handled as aliases) |
| 39 |
* SIGLOST |
| 40 |
* (except on the Hurd; reused to mean a server died) |
| 41 |
* Nearly garbage: SIGSTKFLT SIGUNUSED (nothing else to fill slots) |
| 42 |
*/ |
| 43 |
|
| 44 |
/* Linux 2.3.29 replaces SIGUNUSED with the standard SIGSYS signal */ |
| 45 |
#ifndef SIGSYS |
| 46 |
# warning Standards require that <signal.h> define SIGSYS |
| 47 |
# define SIGSYS SIGUNUSED |
| 48 |
#endif |
| 49 |
|
| 50 |
/* If we see both, it is likely SIGSTKFLT (junk) was replaced. */ |
| 51 |
#ifdef SIGEMT |
| 52 |
# undef SIGSTKFLT |
| 53 |
#endif |
| 54 |
|
| 55 |
#ifndef SIGRTMIN |
| 56 |
# warning Standards require that <signal.h> define SIGRTMIN; assuming 32 |
| 57 |
# define SIGRTMIN 32 |
| 58 |
#endif |
| 59 |
|
| 60 |
/* It seems the SPARC libc does not know the kernel supports SIGPWR. */ |
| 61 |
#if defined(__linux__) && !defined(SIGPWR) |
| 62 |
# warning Your header files lack SIGPWR. (assuming it is number 29) |
| 63 |
# define SIGPWR 29 |
| 64 |
#endif |
| 65 |
|
| 66 |
typedef struct mapstruct { |
| 67 |
const char *name; |
| 68 |
int num; |
| 69 |
} mapstruct; |
| 70 |
|
| 71 |
|
| 72 |
static const mapstruct sigtable[] = { |
| 73 |
{"ABRT", SIGABRT}, /* IOT */ |
| 74 |
{"ALRM", SIGALRM}, |
| 75 |
{"BUS", SIGBUS}, |
| 76 |
{"CHLD", SIGCHLD}, /* CLD */ |
| 77 |
{"CONT", SIGCONT}, |
| 78 |
#ifdef SIGEMT |
| 79 |
{"EMT", SIGEMT}, |
| 80 |
#endif |
| 81 |
{"FPE", SIGFPE}, |
| 82 |
{"HUP", SIGHUP}, |
| 83 |
{"ILL", SIGILL}, |
| 84 |
{"INT", SIGINT}, |
| 85 |
{"KILL", SIGKILL}, |
| 86 |
#if defined(__GNU__) |
| 87 |
{"LOST", SIGLOST}, /* Hurd-specific */ |
| 88 |
#endif |
| 89 |
{"PIPE", SIGPIPE}, |
| 90 |
{"POLL", SIGPOLL}, /* IO */ |
| 91 |
{"PROF", SIGPROF}, |
| 92 |
#ifdef SIGPWR |
| 93 |
{"PWR", SIGPWR}, |
| 94 |
#endif |
| 95 |
{"QUIT", SIGQUIT}, |
| 96 |
{"SEGV", SIGSEGV}, |
| 97 |
#ifdef SIGSTKFLT |
| 98 |
{"STKFLT", SIGSTKFLT}, |
| 99 |
#endif |
| 100 |
{"STOP", SIGSTOP}, |
| 101 |
{"SYS", SIGSYS}, /* UNUSED */ |
| 102 |
{"TERM", SIGTERM}, |
| 103 |
{"TRAP", SIGTRAP}, |
| 104 |
{"TSTP", SIGTSTP}, |
| 105 |
{"TTIN", SIGTTIN}, |
| 106 |
{"TTOU", SIGTTOU}, |
| 107 |
{"URG", SIGURG}, |
| 108 |
{"USR1", SIGUSR1}, |
| 109 |
{"USR2", SIGUSR2}, |
| 110 |
{"VTALRM", SIGVTALRM}, |
| 111 |
{"WINCH", SIGWINCH}, |
| 112 |
{"XCPU", SIGXCPU}, |
| 113 |
{"XFSZ", SIGXFSZ} |
| 114 |
}; |
| 115 |
|
| 116 |
|
| 117 |
#define number_of_signals (sizeof(sigtable)/sizeof(mapstruct)) |
| 118 |
|
| 119 |
#define XJOIN(a, b) JOIN(a, b) |
| 120 |
#define JOIN(a, b) a##b |
| 121 |
#define STATIC_ASSERT(x) typedef int XJOIN(static_assert_on_line_,__LINE__)[(x) ? 1 : -1] |
| 122 |
|
| 123 |
/* sanity check */ |
| 124 |
#if defined(__linux__) |
| 125 |
STATIC_ASSERT(number_of_signals == 31); |
| 126 |
#elif defined(__FreeBSD_kernel__) || defined(__FreeBSD__) |
| 127 |
STATIC_ASSERT(number_of_signals == 30); |
| 128 |
#elif defined(__GNU__) |
| 129 |
STATIC_ASSERT(number_of_signals == 31); |
| 130 |
#elif defined(__CYGWIN__) |
| 131 |
STATIC_ASSERT(number_of_signals == 31); |
| 132 |
#else |
| 133 |
# warning Unknown operating system; assuming number_of_signals is correct |
| 134 |
#endif |
| 135 |
|
| 136 |
static int compare_signal_names(const void *a, const void *b){ |
| 137 |
return strcasecmp( ((const mapstruct*)a)->name, ((const mapstruct*)b)->name ); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
const char *get_sigtable_name(int row) |
| 142 |
{ |
| 143 |
if (row < 0 || row >= number_of_signals) |
| 144 |
return NULL; |
| 145 |
return sigtable[row].name; |
| 146 |
} |
| 147 |
|
| 148 |
const int get_sigtable_num(int row) |
| 149 |
{ |
| 150 |
if (row < 0 || row >= number_of_signals) |
| 151 |
return -1; |
| 152 |
return sigtable[row].num; |
| 153 |
} |
| 154 |
|
| 155 |
/* return -1 on failure */ |
| 156 |
int signal_name_to_number(const char *restrict name){ |
| 157 |
long val; |
| 158 |
int offset; |
| 159 |
|
| 160 |
/* clean up name */ |
| 161 |
if(!strncasecmp(name,"SIG",3)) |
| 162 |
name += 3; |
| 163 |
|
| 164 |
if(!strcasecmp(name,"CLD")) |
| 165 |
return SIGCHLD; |
| 166 |
if(!strcasecmp(name,"IO")) |
| 167 |
return SIGPOLL; |
| 168 |
if(!strcasecmp(name,"IOT")) |
| 169 |
return SIGABRT; |
| 170 |
/* search the table */ |
| 171 |
{ |
| 172 |
const mapstruct ms = {name,0}; |
| 173 |
const mapstruct *restrict const ptr = bsearch( |
| 174 |
&ms, |
| 175 |
sigtable, |
| 176 |
number_of_signals, |
| 177 |
sizeof(mapstruct), |
| 178 |
compare_signal_names); |
| 179 |
if(ptr) |
| 180 |
return ptr->num; |
| 181 |
} |
| 182 |
|
| 183 |
if(!strcasecmp(name,"RTMIN")) |
| 184 |
return SIGRTMIN; |
| 185 |
if(!strcasecmp(name,"EXIT")) |
| 186 |
return 0; |
| 187 |
if(!strcasecmp(name,"NULL")) |
| 188 |
return 0; |
| 189 |
|
| 190 |
offset = 0; |
| 191 |
if(!strncasecmp(name,"RTMIN+",6)) { |
| 192 |
name += 6; |
| 193 |
offset = SIGRTMIN; |
| 194 |
} |
| 195 |
|
| 196 |
/* not found, so try as a number */ |
| 197 |
{ |
| 198 |
char *endp; |
| 199 |
val = strtol(name,&endp,10); |
| 200 |
if(*endp || endp==name) |
| 201 |
return -1; /* not valid */ |
| 202 |
} |
| 203 |
if(val<0 || val+SIGRTMIN>127) |
| 204 |
return -1; /* not valid */ |
| 205 |
return val+offset; |
| 206 |
} |
| 207 |
|
| 208 |
const char *signal_number_to_name(int signo) |
| 209 |
{ |
| 210 |
static char buf[32]; |
| 211 |
int n = number_of_signals; |
| 212 |
signo &= 0x7f; /* need to process exit values too */ |
| 213 |
while (n--) { |
| 214 |
if(sigtable[n].num==signo) |
| 215 |
return sigtable[n].name; |
| 216 |
} |
| 217 |
if (signo == SIGRTMIN) |
| 218 |
return "RTMIN"; |
| 219 |
if (signo) |
| 220 |
sprintf(buf, "RTMIN+%d", signo-SIGRTMIN); |
| 221 |
else |
| 222 |
strcpy(buf,"0"); /* AIX has NULL; Solaris has EXIT */ |
| 223 |
return buf; |
| 224 |
} |
| 225 |
|
| 226 |
int skill_sig_option(int *argc, char **argv) |
| 227 |
{ |
| 228 |
int i; |
| 229 |
int signo = -1; |
| 230 |
for (i = 1; i < *argc; i++) { |
| 231 |
if (argv[i][0] == '-') { |
| 232 |
signo = signal_name_to_number(argv[i] + 1); |
| 233 |
if (-1 < signo) { |
| 234 |
memmove(argv + i, argv + i + 1, |
| 235 |
sizeof(char *) * (*argc - i)); |
| 236 |
(*argc)--; |
| 237 |
return signo; |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
return signo; |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
/* strtosig is similar to print_given_signals() with exception, that |
| 246 |
* this function takes a string, and converts it to a signal name or |
| 247 |
* a number string depending on which way a round conversion is |
| 248 |
* queried. Non-existing signals return NULL. Notice that the |
| 249 |
* returned string should be freed after use. |
| 250 |
*/ |
| 251 |
char *strtosig(const char *restrict s) |
| 252 |
{ |
| 253 |
char *converted = NULL, *copy, *p, *endp; |
| 254 |
int i, numsignal = 0; |
| 255 |
|
| 256 |
copy = strdup(s); |
| 257 |
if (!copy) |
| 258 |
xerrx(EXIT_FAILURE, "cannot duplicate string"); |
| 259 |
for (p = copy; *p != '\0'; p++) |
| 260 |
*p = toupper(*p); |
| 261 |
p = copy; |
| 262 |
if (p[0] == 'S' && p[1] == 'I' && p[2] == 'G') |
| 263 |
p += 3; |
| 264 |
if (isdigit(*p)){ |
| 265 |
numsignal = strtol(s,&endp,10); |
| 266 |
if(*endp || endp==s){ |
| 267 |
free(p); |
| 268 |
return NULL; /* not valid */ |
| 269 |
} |
| 270 |
} |
| 271 |
if (numsignal){ |
| 272 |
for (i = 0; i < number_of_signals; i++){ |
| 273 |
if (numsignal == get_sigtable_num(i)){ |
| 274 |
converted = strdup(get_sigtable_name(i)); |
| 275 |
break; |
| 276 |
} |
| 277 |
} |
| 278 |
} else { |
| 279 |
for (i = 0; i < number_of_signals; i++){ |
| 280 |
if (strcmp(p, get_sigtable_name(i)) == 0){ |
| 281 |
converted = malloc(12); |
| 282 |
if (converted) |
| 283 |
snprintf(converted, 12, "%d", sigtable[i].num); |
| 284 |
break; |
| 285 |
} |
| 286 |
} |
| 287 |
} |
| 288 |
free(copy); |
| 289 |
return converted; |
| 290 |
} |
| 291 |
|
| 292 |
void unix_print_signals(void) |
| 293 |
{ |
| 294 |
int pos = 0; |
| 295 |
int i = 0; |
| 296 |
while(++i <= number_of_signals){ |
| 297 |
if(i-1) printf("%c", (pos>73)?(pos=0,'\n'):(pos++,' ') ); |
| 298 |
pos += printf("%s", signal_number_to_name(i)); |
| 299 |
} |
| 300 |
printf("\n"); |
| 301 |
} |
| 302 |
|
| 303 |
void pretty_print_signals(void) |
| 304 |
{ |
| 305 |
int i = 0; |
| 306 |
while(++i <= number_of_signals){ |
| 307 |
int n; |
| 308 |
n = printf("%2d %s", i, signal_number_to_name(i)); |
| 309 |
if(n>0 && i%7) |
| 310 |
printf("%s", " \0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" + n); |
| 311 |
else |
| 312 |
printf("\n"); |
| 313 |
} |
| 314 |
if((i-1)%7) printf("\n"); |
| 315 |
} |
| 316 |
|