raise.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* Provide a non-threads replacement for the POSIX raise function.
  2. Copyright (C) 2002-2003, 2005-2006, 2009-2023 Free Software Foundation, Inc.
  3. This file is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as
  5. published by the Free Software Foundation; either version 2.1 of the
  6. License, or (at your option) any later version.
  7. This file 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
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  13. /* written by Jim Meyering and Bruno Haible */
  14. #include <config.h>
  15. /* Specification. */
  16. #include <signal.h>
  17. #if HAVE_RAISE
  18. /* Native Windows platform. */
  19. # include <errno.h>
  20. # if HAVE_MSVC_INVALID_PARAMETER_HANDLER
  21. # include "msvc-inval.h"
  22. # endif
  23. # if HAVE_MSVC_INVALID_PARAMETER_HANDLER
  24. /* Forward declaration. */
  25. static int raise_nothrow (int sig);
  26. # else
  27. # define raise_nothrow raise
  28. # endif
  29. #else
  30. /* An old Unix platform. */
  31. # include <unistd.h>
  32. #endif
  33. int
  34. raise (int sig)
  35. #undef raise
  36. {
  37. #if GNULIB_defined_signal_blocking && GNULIB_defined_SIGPIPE
  38. if (sig == SIGPIPE)
  39. return _gl_raise_SIGPIPE ();
  40. #endif
  41. #if HAVE_RAISE
  42. return raise_nothrow (sig);
  43. #else
  44. return kill (getpid (), sig);
  45. #endif
  46. }
  47. #if HAVE_RAISE && HAVE_MSVC_INVALID_PARAMETER_HANDLER
  48. static int
  49. raise_nothrow (int sig)
  50. {
  51. int result;
  52. TRY_MSVC_INVAL
  53. {
  54. result = raise (sig);
  55. }
  56. CATCH_MSVC_INVAL
  57. {
  58. result = -1;
  59. errno = EINVAL;
  60. }
  61. DONE_MSVC_INVAL;
  62. return result;
  63. }
  64. #endif