strsignal.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /* Copyright (C) 1991-2026 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. <https://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 <tls-internal.h>
  20. #include <libc-internal.h>
  21. /* Return a string describing the meaning of the signal number SIGNUM. */
  22. char *
  23. strsignal (int signum)
  24. {
  25. const char *desc = __sigdescr_np (signum);
  26. if (desc != NULL)
  27. return _(desc);
  28. if (__libc_initial)
  29. {
  30. struct tls_internal_t *tls_internal = __glibc_tls_internal ();
  31. free (tls_internal->strsignal_buf);
  32. int r;
  33. #ifdef SIGRTMIN
  34. if (signum >= SIGRTMIN && signum <= SIGRTMAX)
  35. r = __asprintf (&tls_internal->strsignal_buf, _("Real-time signal %d"),
  36. signum - SIGRTMIN);
  37. else
  38. #endif
  39. r = __asprintf (&tls_internal->strsignal_buf, _("Unknown signal %d"),
  40. signum);
  41. if (r >= 0)
  42. return tls_internal->strsignal_buf;
  43. else
  44. tls_internal->strsignal_buf = NULL;
  45. }
  46. /* Fall through on asprintf error, and for !__libc_initial:
  47. secondary namespaces use a different malloc and cannot
  48. participate in the buffer management. */
  49. return _("Unknown signal");
  50. }