strsignal.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /* Copyright (C) 1991, 1994-2002, 2005, 2008-2013 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. # 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. #if defined(_MSC_VER)
  40. # define __snprintf _snprintf
  41. #else
  42. # define __snprintf snprintf
  43. #endif
  44. #endif /* _LIBC */
  45. #ifdef _LIBC
  46. /* Defined in siglist.c. */
  47. extern const char *const _sys_siglist[];
  48. extern const char *const _sys_siglist_internal[] attribute_hidden;
  49. #else /* !_LIBC */
  50. /* NetBSD declares sys_siglist in unistd.h. */
  51. # if HAVE_UNISTD_H
  52. # include <unistd.h>
  53. # endif
  54. # define INTUSE(x) (x)
  55. # if HAVE_DECL_SYS_SIGLIST
  56. # undef _sys_siglist
  57. # define _sys_siglist sys_siglist
  58. # else /* !HAVE_DECL_SYS_SIGLIST */
  59. # ifndef NSIG
  60. # define NSIG 32
  61. # endif /* NSIG */
  62. # if !HAVE_DECL__SYS_SIGLIST
  63. static const char *_sys_siglist[NSIG];
  64. # endif
  65. # endif /* !HAVE_DECL_SYS_SIGLIST */
  66. #endif /* _LIBC */
  67. static __libc_key_t key;
  68. /* If nonzero the key allocation failed and we should better use a
  69. static buffer than fail. */
  70. #define BUFFERSIZ 100
  71. static char local_buf[BUFFERSIZ];
  72. static char *static_buf;
  73. /* Destructor for the thread-specific data. */
  74. static void init (void);
  75. static void free_key_mem (void *mem);
  76. static char *getbuffer (void);
  77. /* Return a string describing the meaning of the signal number SIGNUM. */
  78. char *
  79. strsignal (int signum)
  80. {
  81. const char *desc;
  82. __libc_once_define (static, once);
  83. /* If we have not yet initialized the buffer do it now. */
  84. __libc_once (once, init);
  85. if (
  86. #ifdef SIGRTMIN
  87. (signum >= SIGRTMIN && signum <= SIGRTMAX) ||
  88. #endif
  89. signum < 0 || signum >= NSIG
  90. || (desc = INTUSE(_sys_siglist)[signum]) == NULL)
  91. {
  92. char *buffer = getbuffer ();
  93. int len;
  94. #ifdef SIGRTMIN
  95. if (signum >= SIGRTMIN && signum <= SIGRTMAX)
  96. len = __snprintf (buffer, BUFFERSIZ - 1, _("Real-time signal %d"),
  97. signum - (int) SIGRTMIN);
  98. else
  99. #endif
  100. len = __snprintf (buffer, BUFFERSIZ - 1, _("Unknown signal %d"),
  101. signum);
  102. if (len >= BUFFERSIZ)
  103. buffer = NULL;
  104. else
  105. buffer[len] = '\0';
  106. return buffer;
  107. }
  108. return (char *) _(desc);
  109. }
  110. /* Initialize buffer. */
  111. static void
  112. init (void)
  113. {
  114. #ifdef _LIBC
  115. if (__libc_key_create (&key, free_key_mem))
  116. /* Creating the key failed. This means something really went
  117. wrong. In any case use a static buffer which is better than
  118. nothing. */
  119. static_buf = local_buf;
  120. #else /* !_LIBC */
  121. gl_tls_key_init (key, free_key_mem);
  122. # if !HAVE_DECL_SYS_SIGLIST
  123. memset (_sys_siglist, 0, NSIG * sizeof *_sys_siglist);
  124. /* No need to use a do {} while (0) here since init_sig(...) must expand
  125. to a complete statement. (We cannot use the ISO C99 designated array
  126. initializer syntax since it is not supported by ANSI C compilers and
  127. since some signal numbers might exceed NSIG.) */
  128. # define init_sig(sig, abbrev, desc) \
  129. if (sig >= 0 && sig < NSIG) \
  130. _sys_siglist[sig] = desc;
  131. # include "siglist.h"
  132. # undef init_sig
  133. # endif /* !HAVE_DECL_SYS_SIGLIST */
  134. #endif /* !_LIBC */
  135. }
  136. /* Free the thread specific data, this is done if a thread terminates. */
  137. static void
  138. free_key_mem (void *mem)
  139. {
  140. free (mem);
  141. __libc_setspecific (key, NULL);
  142. }
  143. /* Return the buffer to be used. */
  144. static char *
  145. getbuffer (void)
  146. {
  147. char *result;
  148. if (static_buf != NULL)
  149. result = static_buf;
  150. else
  151. {
  152. /* We don't use the static buffer and so we have a key. Use it
  153. to get the thread-specific buffer. */
  154. result = __libc_getspecific (key);
  155. if (result == NULL)
  156. {
  157. /* No buffer allocated so far. */
  158. result = malloc (BUFFERSIZ);
  159. if (result == NULL)
  160. /* No more memory available. We use the static buffer. */
  161. result = local_buf;
  162. else
  163. __libc_setspecific (key, result);
  164. }
  165. }
  166. return result;
  167. }