strsignal.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* Copyright (C) 1991-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <http://www.gnu.org/licenses/>. */
  14. #include <signal.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <libintl.h>
  19. #include <libc-lock.h>
  20. static __libc_key_t key;
  21. /* If nonzero the key allocation failed and we should better use a
  22. static buffer than fail. */
  23. #define BUFFERSIZ 100
  24. static char local_buf[BUFFERSIZ];
  25. static char *static_buf;
  26. /* Destructor for the thread-specific data. */
  27. static void init (void);
  28. static void free_key_mem (void *mem);
  29. static char *getbuffer (void);
  30. /* Return a string describing the meaning of the signal number SIGNUM. */
  31. char *
  32. strsignal (int signum)
  33. {
  34. __libc_once_define (static, once);
  35. const char *desc;
  36. /* If we have not yet initialized the buffer do it now. */
  37. __libc_once (once, init);
  38. if (
  39. #ifdef SIGRTMIN
  40. (signum >= SIGRTMIN && signum <= SIGRTMAX) ||
  41. #endif
  42. signum < 0 || signum >= NSIG
  43. || (desc = _sys_siglist[signum]) == NULL)
  44. {
  45. char *buffer = getbuffer ();
  46. int len;
  47. #ifdef SIGRTMIN
  48. if (signum >= SIGRTMIN && signum <= SIGRTMAX)
  49. len = __snprintf (buffer, BUFFERSIZ - 1, _("Real-time signal %d"),
  50. signum - SIGRTMIN);
  51. else
  52. #endif
  53. len = __snprintf (buffer, BUFFERSIZ - 1, _("Unknown signal %d"),
  54. signum);
  55. if (len >= BUFFERSIZ)
  56. buffer = NULL;
  57. else
  58. buffer[len] = '\0';
  59. return buffer;
  60. }
  61. return (char *) _(desc);
  62. }
  63. /* Initialize buffer. */
  64. static void
  65. init (void)
  66. {
  67. if (__libc_key_create (&key, free_key_mem))
  68. /* Creating the key failed. This means something really went
  69. wrong. In any case use a static buffer which is better than
  70. nothing. */
  71. static_buf = local_buf;
  72. }
  73. /* Free the thread specific data, this is done if a thread terminates. */
  74. static void
  75. free_key_mem (void *mem)
  76. {
  77. free (mem);
  78. __libc_setspecific (key, NULL);
  79. }
  80. /* Return the buffer to be used. */
  81. static char *
  82. getbuffer (void)
  83. {
  84. char *result;
  85. if (static_buf != NULL)
  86. result = static_buf;
  87. else
  88. {
  89. /* We don't use the static buffer and so we have a key. Use it
  90. to get the thread-specific buffer. */
  91. result = __libc_getspecific (key);
  92. if (result == NULL)
  93. {
  94. /* No buffer allocated so far. */
  95. result = malloc (BUFFERSIZ);
  96. if (result == NULL)
  97. /* No more memory available. We use the static buffer. */
  98. result = local_buf;
  99. else
  100. __libc_setspecific (key, result);
  101. }
  102. }
  103. return result;
  104. }