strsignal.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /* Copyright (C) 1991, 1994-2002, 2005, 2008-2016 Free Software Foundation,
  2. Inc.
  3. This file is part of the GNU C Library.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #ifndef _LIBC
  15. # include <config.h>
  16. #endif
  17. /* Specification. */
  18. #include <string.h>
  19. #include <signal.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #ifdef _LIBC
  23. # include <libintl.h>
  24. #else /* !_LIBC */
  25. # include "gettext.h"
  26. # define _(msgid) gettext (msgid)
  27. # define N_(msgid) gettext_noop (msgid)
  28. #endif /* _LIBC */
  29. #ifdef _LIBC
  30. # error #include <bits/libc-lock.h>
  31. #else /* !_LIBC */
  32. # include "glthread/lock.h"
  33. # include "glthread/tls.h"
  34. # define __libc_once_define(CLASS, NAME) gl_once_define (CLASS, NAME)
  35. # define __libc_once(NAME, INIT) gl_once ((NAME), (INIT))
  36. # define __libc_key_t gl_tls_key_t
  37. # define __libc_getspecific(NAME) gl_tls_get ((NAME))
  38. # define __libc_setspecific(NAME, POINTER) gl_tls_set ((NAME), (POINTER))
  39. # define __snprintf snprintf
  40. #endif /* _LIBC */
  41. #ifdef _LIBC
  42. /* Defined in siglist.c. */
  43. extern const char *const _sys_siglist[];
  44. extern const char *const _sys_siglist_internal[] attribute_hidden;
  45. #else /* !_LIBC */
  46. /* NetBSD declares sys_siglist in unistd.h. */
  47. # if HAVE_UNISTD_H
  48. # include <unistd.h>
  49. # endif
  50. # define INTUSE(x) (x)
  51. # if HAVE_DECL_SYS_SIGLIST
  52. # undef _sys_siglist
  53. # define _sys_siglist sys_siglist
  54. # else /* !HAVE_DECL_SYS_SIGLIST */
  55. # ifndef NSIG
  56. # define NSIG 32
  57. # endif /* NSIG */
  58. # if !HAVE_DECL__SYS_SIGLIST
  59. static const char *_sys_siglist[NSIG];
  60. # endif
  61. # endif /* !HAVE_DECL_SYS_SIGLIST */
  62. #endif /* _LIBC */
  63. static __libc_key_t key;
  64. /* If nonzero the key allocation failed and we should better use a
  65. static buffer than fail. */
  66. #define BUFFERSIZ 100
  67. static char local_buf[BUFFERSIZ];
  68. static char *static_buf;
  69. /* Destructor for the thread-specific data. */
  70. static void init (void);
  71. static void free_key_mem (void *mem);
  72. static char *getbuffer (void);
  73. /* Return a string describing the meaning of the signal number SIGNUM. */
  74. char *
  75. strsignal (int signum)
  76. {
  77. const char *desc;
  78. __libc_once_define (static, once);
  79. /* If we have not yet initialized the buffer do it now. */
  80. __libc_once (once, init);
  81. if (
  82. #ifdef SIGRTMIN
  83. (signum >= SIGRTMIN && signum <= SIGRTMAX) ||
  84. #endif
  85. signum < 0 || signum >= NSIG
  86. || (desc = INTUSE(_sys_siglist)[signum]) == NULL)
  87. {
  88. char *buffer = getbuffer ();
  89. int len;
  90. #ifdef SIGRTMIN
  91. if (signum >= SIGRTMIN && signum <= SIGRTMAX)
  92. len = __snprintf (buffer, BUFFERSIZ - 1, _("Real-time signal %d"),
  93. signum - (int) SIGRTMIN);
  94. else
  95. #endif
  96. len = __snprintf (buffer, BUFFERSIZ - 1, _("Unknown signal %d"),
  97. signum);
  98. if (len >= BUFFERSIZ)
  99. buffer = NULL;
  100. else
  101. buffer[len] = '\0';
  102. return buffer;
  103. }
  104. return (char *) _(desc);
  105. }
  106. /* Initialize buffer. */
  107. static void
  108. init (void)
  109. {
  110. #ifdef _LIBC
  111. if (__libc_key_create (&key, free_key_mem))
  112. /* Creating the key failed. This means something really went
  113. wrong. In any case use a static buffer which is better than
  114. nothing. */
  115. static_buf = local_buf;
  116. #else /* !_LIBC */
  117. gl_tls_key_init (key, free_key_mem);
  118. # if !HAVE_DECL_SYS_SIGLIST
  119. memset (_sys_siglist, 0, NSIG * sizeof *_sys_siglist);
  120. /* No need to use a do {} while (0) here since init_sig(...) must expand
  121. to a complete statement. (We cannot use the ISO C99 designated array
  122. initializer syntax since it is not supported by ANSI C compilers and
  123. since some signal numbers might exceed NSIG.) */
  124. # define init_sig(sig, abbrev, desc) \
  125. if (sig >= 0 && sig < NSIG) \
  126. _sys_siglist[sig] = desc;
  127. # include "siglist.h"
  128. # undef init_sig
  129. # endif /* !HAVE_DECL_SYS_SIGLIST */
  130. #endif /* !_LIBC */
  131. }
  132. /* Free the thread specific data, this is done if a thread terminates. */
  133. static void
  134. free_key_mem (void *mem)
  135. {
  136. free (mem);
  137. __libc_setspecific (key, NULL);
  138. }
  139. /* Return the buffer to be used. */
  140. static char *
  141. getbuffer (void)
  142. {
  143. char *result;
  144. if (static_buf != NULL)
  145. result = static_buf;
  146. else
  147. {
  148. /* We don't use the static buffer and so we have a key. Use it
  149. to get the thread-specific buffer. */
  150. result = __libc_getspecific (key);
  151. if (result == NULL)
  152. {
  153. /* No buffer allocated so far. */
  154. result = malloc (BUFFERSIZ);
  155. if (result == NULL)
  156. /* No more memory available. We use the static buffer. */
  157. result = local_buf;
  158. else
  159. __libc_setspecific (key, result);
  160. }
  161. }
  162. return result;
  163. }