strsignal.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. /* Extended support for using signal values.
  2. Written by Fred Fish. fnf@cygnus.com
  3. This file is in the public domain. */
  4. #include "config.h"
  5. #include "ansidecl.h"
  6. #include "libiberty.h"
  7. /* We need to declare sys_siglist, because even if the system provides
  8. it we can't assume that it is declared in <signal.h> (for example,
  9. SunOS provides sys_siglist, but it does not declare it in any
  10. header file). However, we can't declare sys_siglist portably,
  11. because on some systems it is declared with const and on some
  12. systems it is declared without const. If we were using autoconf,
  13. we could work out the right declaration. Until, then we just
  14. ignore any declaration in the system header files, and always
  15. declare it ourselves. With luck, this will always work. */
  16. #define sys_siglist no_such_symbol
  17. #define sys_nsig sys_nsig__no_such_symbol
  18. #include <stdio.h>
  19. #include <signal.h>
  20. /* Routines imported from standard C runtime libraries. */
  21. #ifdef HAVE_STDLIB_H
  22. #include <stdlib.h>
  23. #else
  24. extern PTR malloc ();
  25. #endif
  26. #ifdef HAVE_STRING_H
  27. #include <string.h>
  28. #else
  29. extern PTR memset ();
  30. #endif
  31. /* Undefine the macro we used to hide the definition of sys_siglist
  32. found in the system header files. */
  33. #undef sys_siglist
  34. #undef sys_nsig
  35. #ifndef NULL
  36. # define NULL (void *) 0
  37. #endif
  38. #ifndef MAX
  39. # define MAX(a,b) ((a) > (b) ? (a) : (b))
  40. #endif
  41. static void init_signal_tables (void);
  42. /* Translation table for signal values.
  43. Note that this table is generally only accessed when it is used at runtime
  44. to initialize signal name and message tables that are indexed by signal
  45. value.
  46. Not all of these signals will exist on all systems. This table is the only
  47. thing that should have to be updated as new signal numbers are introduced.
  48. It's sort of ugly, but at least its portable. */
  49. struct signal_info
  50. {
  51. const int value; /* The numeric value from <signal.h> */
  52. const char *const name; /* The equivalent symbolic value */
  53. #ifndef HAVE_SYS_SIGLIST
  54. const char *const msg; /* Short message about this value */
  55. #endif
  56. };
  57. #ifndef HAVE_SYS_SIGLIST
  58. # define ENTRY(value, name, msg) {value, name, msg}
  59. #else
  60. # define ENTRY(value, name, msg) {value, name}
  61. #endif
  62. static const struct signal_info signal_table[] =
  63. {
  64. #if defined (SIGHUP)
  65. ENTRY(SIGHUP, "SIGHUP", "Hangup"),
  66. #endif
  67. #if defined (SIGINT)
  68. ENTRY(SIGINT, "SIGINT", "Interrupt"),
  69. #endif
  70. #if defined (SIGQUIT)
  71. ENTRY(SIGQUIT, "SIGQUIT", "Quit"),
  72. #endif
  73. #if defined (SIGILL)
  74. ENTRY(SIGILL, "SIGILL", "Illegal instruction"),
  75. #endif
  76. #if defined (SIGTRAP)
  77. ENTRY(SIGTRAP, "SIGTRAP", "Trace/breakpoint trap"),
  78. #endif
  79. /* Put SIGIOT before SIGABRT, so that if SIGIOT==SIGABRT then SIGABRT
  80. overrides SIGIOT. SIGABRT is in ANSI and POSIX.1, and SIGIOT isn't. */
  81. #if defined (SIGIOT)
  82. ENTRY(SIGIOT, "SIGIOT", "IOT trap"),
  83. #endif
  84. #if defined (SIGABRT)
  85. ENTRY(SIGABRT, "SIGABRT", "Aborted"),
  86. #endif
  87. #if defined (SIGEMT)
  88. ENTRY(SIGEMT, "SIGEMT", "Emulation trap"),
  89. #endif
  90. #if defined (SIGFPE)
  91. ENTRY(SIGFPE, "SIGFPE", "Arithmetic exception"),
  92. #endif
  93. #if defined (SIGKILL)
  94. ENTRY(SIGKILL, "SIGKILL", "Killed"),
  95. #endif
  96. #if defined (SIGBUS)
  97. ENTRY(SIGBUS, "SIGBUS", "Bus error"),
  98. #endif
  99. #if defined (SIGSEGV)
  100. ENTRY(SIGSEGV, "SIGSEGV", "Segmentation fault"),
  101. #endif
  102. #if defined (SIGSYS)
  103. ENTRY(SIGSYS, "SIGSYS", "Bad system call"),
  104. #endif
  105. #if defined (SIGPIPE)
  106. ENTRY(SIGPIPE, "SIGPIPE", "Broken pipe"),
  107. #endif
  108. #if defined (SIGALRM)
  109. ENTRY(SIGALRM, "SIGALRM", "Alarm clock"),
  110. #endif
  111. #if defined (SIGTERM)
  112. ENTRY(SIGTERM, "SIGTERM", "Terminated"),
  113. #endif
  114. #if defined (SIGUSR1)
  115. ENTRY(SIGUSR1, "SIGUSR1", "User defined signal 1"),
  116. #endif
  117. #if defined (SIGUSR2)
  118. ENTRY(SIGUSR2, "SIGUSR2", "User defined signal 2"),
  119. #endif
  120. /* Put SIGCLD before SIGCHLD, so that if SIGCLD==SIGCHLD then SIGCHLD
  121. overrides SIGCLD. SIGCHLD is in POXIX.1 */
  122. #if defined (SIGCLD)
  123. ENTRY(SIGCLD, "SIGCLD", "Child status changed"),
  124. #endif
  125. #if defined (SIGCHLD)
  126. ENTRY(SIGCHLD, "SIGCHLD", "Child status changed"),
  127. #endif
  128. #if defined (SIGPWR)
  129. ENTRY(SIGPWR, "SIGPWR", "Power fail/restart"),
  130. #endif
  131. #if defined (SIGWINCH)
  132. ENTRY(SIGWINCH, "SIGWINCH", "Window size changed"),
  133. #endif
  134. #if defined (SIGURG)
  135. ENTRY(SIGURG, "SIGURG", "Urgent I/O condition"),
  136. #endif
  137. #if defined (SIGIO)
  138. /* "I/O pending" has also been suggested, but is misleading since the
  139. signal only happens when the process has asked for it, not everytime
  140. I/O is pending. */
  141. ENTRY(SIGIO, "SIGIO", "I/O possible"),
  142. #endif
  143. #if defined (SIGPOLL)
  144. ENTRY(SIGPOLL, "SIGPOLL", "Pollable event occurred"),
  145. #endif
  146. #if defined (SIGSTOP)
  147. ENTRY(SIGSTOP, "SIGSTOP", "Stopped (signal)"),
  148. #endif
  149. #if defined (SIGTSTP)
  150. ENTRY(SIGTSTP, "SIGTSTP", "Stopped (user)"),
  151. #endif
  152. #if defined (SIGCONT)
  153. ENTRY(SIGCONT, "SIGCONT", "Continued"),
  154. #endif
  155. #if defined (SIGTTIN)
  156. ENTRY(SIGTTIN, "SIGTTIN", "Stopped (tty input)"),
  157. #endif
  158. #if defined (SIGTTOU)
  159. ENTRY(SIGTTOU, "SIGTTOU", "Stopped (tty output)"),
  160. #endif
  161. #if defined (SIGVTALRM)
  162. ENTRY(SIGVTALRM, "SIGVTALRM", "Virtual timer expired"),
  163. #endif
  164. #if defined (SIGPROF)
  165. ENTRY(SIGPROF, "SIGPROF", "Profiling timer expired"),
  166. #endif
  167. #if defined (SIGXCPU)
  168. ENTRY(SIGXCPU, "SIGXCPU", "CPU time limit exceeded"),
  169. #endif
  170. #if defined (SIGXFSZ)
  171. ENTRY(SIGXFSZ, "SIGXFSZ", "File size limit exceeded"),
  172. #endif
  173. #if defined (SIGWIND)
  174. ENTRY(SIGWIND, "SIGWIND", "SIGWIND"),
  175. #endif
  176. #if defined (SIGPHONE)
  177. ENTRY(SIGPHONE, "SIGPHONE", "SIGPHONE"),
  178. #endif
  179. #if defined (SIGLOST)
  180. ENTRY(SIGLOST, "SIGLOST", "Resource lost"),
  181. #endif
  182. #if defined (SIGWAITING)
  183. ENTRY(SIGWAITING, "SIGWAITING", "Process's LWPs are blocked"),
  184. #endif
  185. #if defined (SIGLWP)
  186. ENTRY(SIGLWP, "SIGLWP", "Signal LWP"),
  187. #endif
  188. #if defined (SIGDANGER)
  189. ENTRY(SIGDANGER, "SIGDANGER", "Swap space dangerously low"),
  190. #endif
  191. #if defined (SIGGRANT)
  192. ENTRY(SIGGRANT, "SIGGRANT", "Monitor mode granted"),
  193. #endif
  194. #if defined (SIGRETRACT)
  195. ENTRY(SIGRETRACT, "SIGRETRACT", "Need to relinguish monitor mode"),
  196. #endif
  197. #if defined (SIGMSG)
  198. ENTRY(SIGMSG, "SIGMSG", "Monitor mode data available"),
  199. #endif
  200. #if defined (SIGSOUND)
  201. ENTRY(SIGSOUND, "SIGSOUND", "Sound completed"),
  202. #endif
  203. #if defined (SIGSAK)
  204. ENTRY(SIGSAK, "SIGSAK", "Secure attention"),
  205. #endif
  206. ENTRY(0, NULL, NULL)
  207. };
  208. /* Translation table allocated and initialized at runtime. Indexed by the
  209. signal value to find the equivalent symbolic value. */
  210. static const char **signal_names;
  211. static int num_signal_names = 0;
  212. /* Translation table allocated and initialized at runtime, if it does not
  213. already exist in the host environment. Indexed by the signal value to find
  214. the descriptive string.
  215. We don't export it for use in other modules because even though it has the
  216. same name, it differs from other implementations in that it is dynamically
  217. initialized rather than statically initialized. */
  218. #ifndef HAVE_SYS_SIGLIST
  219. static int sys_nsig;
  220. static const char **sys_siglist;
  221. #else
  222. #ifdef NSIG
  223. static int sys_nsig = NSIG;
  224. #else
  225. #ifdef _NSIG
  226. static int sys_nsig = _NSIG;
  227. #endif
  228. #endif
  229. extern const char * const sys_siglist[];
  230. #endif
  231. /*
  232. NAME
  233. init_signal_tables -- initialize the name and message tables
  234. SYNOPSIS
  235. static void init_signal_tables ();
  236. DESCRIPTION
  237. Using the signal_table, which is initialized at compile time, generate
  238. the signal_names and the sys_siglist (if needed) tables, which are
  239. indexed at runtime by a specific signal value.
  240. BUGS
  241. The initialization of the tables may fail under low memory conditions,
  242. in which case we don't do anything particularly useful, but we don't
  243. bomb either. Who knows, it might succeed at a later point if we free
  244. some memory in the meantime. In any case, the other routines know
  245. how to deal with lack of a table after trying to initialize it. This
  246. may or may not be considered to be a bug, that we don't specifically
  247. warn about this particular failure mode.
  248. */
  249. static void
  250. init_signal_tables (void)
  251. {
  252. const struct signal_info *eip;
  253. int nbytes;
  254. /* If we haven't already scanned the signal_table once to find the maximum
  255. signal value, then go find it now. */
  256. if (num_signal_names == 0)
  257. {
  258. for (eip = signal_table; eip -> name != NULL; eip++)
  259. {
  260. if (eip -> value >= num_signal_names)
  261. {
  262. num_signal_names = eip -> value + 1;
  263. }
  264. }
  265. }
  266. /* Now attempt to allocate the signal_names table, zero it out, and then
  267. initialize it from the statically initialized signal_table. */
  268. if (signal_names == NULL)
  269. {
  270. nbytes = num_signal_names * sizeof (char *);
  271. if ((signal_names = (const char **) malloc (nbytes)) != NULL)
  272. {
  273. memset (signal_names, 0, nbytes);
  274. for (eip = signal_table; eip -> name != NULL; eip++)
  275. {
  276. signal_names[eip -> value] = eip -> name;
  277. }
  278. }
  279. }
  280. #ifndef HAVE_SYS_SIGLIST
  281. /* Now attempt to allocate the sys_siglist table, zero it out, and then
  282. initialize it from the statically initialized signal_table. */
  283. if (sys_siglist == NULL)
  284. {
  285. nbytes = num_signal_names * sizeof (char *);
  286. if ((sys_siglist = (const char **) malloc (nbytes)) != NULL)
  287. {
  288. memset (sys_siglist, 0, nbytes);
  289. sys_nsig = num_signal_names;
  290. for (eip = signal_table; eip -> name != NULL; eip++)
  291. {
  292. sys_siglist[eip -> value] = eip -> msg;
  293. }
  294. }
  295. }
  296. #endif
  297. }
  298. /*
  299. @deftypefn Extension int signo_max (void)
  300. Returns the maximum signal value for which a corresponding symbolic
  301. name or message is available. Note that in the case where we use the
  302. @code{sys_siglist} supplied by the system, it is possible for there to
  303. be more symbolic names than messages, or vice versa. In fact, the
  304. manual page for @code{psignal(3b)} explicitly warns that one should
  305. check the size of the table (@code{NSIG}) before indexing it, since
  306. new signal codes may be added to the system before they are added to
  307. the table. Thus @code{NSIG} might be smaller than value implied by
  308. the largest signo value defined in @code{<signal.h>}.
  309. We return the maximum value that can be used to obtain a meaningful
  310. symbolic name or message.
  311. @end deftypefn
  312. */
  313. int
  314. signo_max (void)
  315. {
  316. int maxsize;
  317. if (signal_names == NULL)
  318. {
  319. init_signal_tables ();
  320. }
  321. maxsize = MAX (sys_nsig, num_signal_names);
  322. return (maxsize - 1);
  323. }
  324. /*
  325. @deftypefn Supplemental {const char *} strsignal (int @var{signo})
  326. Maps an signal number to an signal message string, the contents of
  327. which are implementation defined. On systems which have the external
  328. variable @code{sys_siglist}, these strings will be the same as the
  329. ones used by @code{psignal()}.
  330. If the supplied signal number is within the valid range of indices for
  331. the @code{sys_siglist}, but no message is available for the particular
  332. signal number, then returns the string @samp{Signal @var{num}}, where
  333. @var{num} is the signal number.
  334. If the supplied signal number is not a valid index into
  335. @code{sys_siglist}, returns @code{NULL}.
  336. The returned string is only guaranteed to be valid only until the next
  337. call to @code{strsignal}.
  338. @end deftypefn
  339. */
  340. #ifndef HAVE_STRSIGNAL
  341. char *
  342. strsignal (int signo)
  343. {
  344. char *msg;
  345. static char buf[32];
  346. #ifndef HAVE_SYS_SIGLIST
  347. if (signal_names == NULL)
  348. {
  349. init_signal_tables ();
  350. }
  351. #endif
  352. if ((signo < 0) || (signo >= sys_nsig))
  353. {
  354. /* Out of range, just return NULL */
  355. msg = NULL;
  356. }
  357. else if ((sys_siglist == NULL) || (sys_siglist[signo] == NULL))
  358. {
  359. /* In range, but no sys_siglist or no entry at this index. */
  360. sprintf (buf, "Signal %d", signo);
  361. msg = buf;
  362. }
  363. else
  364. {
  365. /* In range, and a valid message. Just return the message. We
  366. can safely cast away const, since POSIX says the user must
  367. not modify the result. */
  368. msg = (char *) sys_siglist[signo];
  369. }
  370. return (msg);
  371. }
  372. #endif /* ! HAVE_STRSIGNAL */
  373. /*
  374. @deftypefn Extension {const char*} strsigno (int @var{signo})
  375. Given an signal number, returns a pointer to a string containing the
  376. symbolic name of that signal number, as found in @code{<signal.h>}.
  377. If the supplied signal number is within the valid range of indices for
  378. symbolic names, but no name is available for the particular signal
  379. number, then returns the string @samp{Signal @var{num}}, where
  380. @var{num} is the signal number.
  381. If the supplied signal number is not within the range of valid
  382. indices, then returns @code{NULL}.
  383. The contents of the location pointed to are only guaranteed to be
  384. valid until the next call to @code{strsigno}.
  385. @end deftypefn
  386. */
  387. const char *
  388. strsigno (int signo)
  389. {
  390. const char *name;
  391. static char buf[32];
  392. if (signal_names == NULL)
  393. {
  394. init_signal_tables ();
  395. }
  396. if ((signo < 0) || (signo >= num_signal_names))
  397. {
  398. /* Out of range, just return NULL */
  399. name = NULL;
  400. }
  401. else if ((signal_names == NULL) || (signal_names[signo] == NULL))
  402. {
  403. /* In range, but no signal_names or no entry at this index. */
  404. sprintf (buf, "Signal %d", signo);
  405. name = (const char *) buf;
  406. }
  407. else
  408. {
  409. /* In range, and a valid name. Just return the name. */
  410. name = signal_names[signo];
  411. }
  412. return (name);
  413. }
  414. /*
  415. @deftypefn Extension int strtosigno (const char *@var{name})
  416. Given the symbolic name of a signal, map it to a signal number. If no
  417. translation is found, returns 0.
  418. @end deftypefn
  419. */
  420. int
  421. strtosigno (const char *name)
  422. {
  423. int signo = 0;
  424. if (name != NULL)
  425. {
  426. if (signal_names == NULL)
  427. {
  428. init_signal_tables ();
  429. }
  430. for (signo = 0; signo < num_signal_names; signo++)
  431. {
  432. if ((signal_names[signo] != NULL) &&
  433. (strcmp (name, signal_names[signo]) == 0))
  434. {
  435. break;
  436. }
  437. }
  438. if (signo == num_signal_names)
  439. {
  440. signo = 0;
  441. }
  442. }
  443. return (signo);
  444. }
  445. /*
  446. @deftypefn Supplemental void psignal (int @var{signo}, char *@var{message})
  447. Print @var{message} to the standard error, followed by a colon,
  448. followed by the description of the signal specified by @var{signo},
  449. followed by a newline.
  450. @end deftypefn
  451. */
  452. #ifndef HAVE_PSIGNAL
  453. void
  454. psignal (int signo, char *message)
  455. {
  456. if (signal_names == NULL)
  457. {
  458. init_signal_tables ();
  459. }
  460. if ((signo <= 0) || (signo >= sys_nsig))
  461. {
  462. fprintf (stderr, "%s: unknown signal\n", message);
  463. }
  464. else
  465. {
  466. fprintf (stderr, "%s: %s\n", message, sys_siglist[signo]);
  467. }
  468. }
  469. #endif /* ! HAVE_PSIGNAL */
  470. /* A simple little main that does nothing but print all the signal translations
  471. if MAIN is defined and this file is compiled and linked. */
  472. #ifdef MAIN
  473. #include <stdio.h>
  474. int
  475. main (void)
  476. {
  477. int signo;
  478. int maxsigno;
  479. const char *name;
  480. const char *msg;
  481. maxsigno = signo_max ();
  482. printf ("%d entries in names table.\n", num_signal_names);
  483. printf ("%d entries in messages table.\n", sys_nsig);
  484. printf ("%d is max useful index.\n", maxsigno);
  485. /* Keep printing values until we get to the end of *both* tables, not
  486. *either* table. Note that knowing the maximum useful index does *not*
  487. relieve us of the responsibility of testing the return pointer for
  488. NULL. */
  489. for (signo = 0; signo <= maxsigno; signo++)
  490. {
  491. name = strsigno (signo);
  492. name = (name == NULL) ? "<NULL>" : name;
  493. msg = strsignal (signo);
  494. msg = (msg == NULL) ? "<NULL>" : msg;
  495. printf ("%-4d%-18s%s\n", signo, name, msg);
  496. }
  497. return 0;
  498. }
  499. #endif