SignalHandler.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //
  2. // SignalHandler.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/SignalHandler.cpp#2 $
  5. //
  6. // Library: Foundation
  7. // Package: Threading
  8. // Module: SignalHandler
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/SignalHandler.h"
  16. #if defined(POCO_OS_FAMILY_UNIX) && !defined(POCO_VXWORKS)
  17. #include "Poco/Thread.h"
  18. #include "Poco/NumberFormatter.h"
  19. #include "Poco/Exception.h"
  20. #include <cstdlib>
  21. #include <signal.h>
  22. namespace Poco {
  23. SignalHandler::JumpBufferVec SignalHandler::_jumpBufferVec;
  24. SignalHandler::SignalHandler()
  25. {
  26. JumpBufferVec& jbv = jumpBufferVec();
  27. JumpBuffer buf;
  28. jbv.push_back(buf);
  29. }
  30. SignalHandler::~SignalHandler()
  31. {
  32. jumpBufferVec().pop_back();
  33. }
  34. sigjmp_buf& SignalHandler::jumpBuffer()
  35. {
  36. return jumpBufferVec().back().buf;
  37. }
  38. void SignalHandler::throwSignalException(int sig)
  39. {
  40. switch (sig)
  41. {
  42. case SIGILL:
  43. throw SignalException("Illegal instruction");
  44. case SIGBUS:
  45. throw SignalException("Bus error");
  46. case SIGSEGV:
  47. throw SignalException("Segmentation violation");
  48. case SIGSYS:
  49. throw SignalException("Invalid system call");
  50. default:
  51. throw SignalException(NumberFormatter::formatHex(sig));
  52. }
  53. }
  54. void SignalHandler::install()
  55. {
  56. #ifndef POCO_NO_SIGNAL_HANDLER
  57. struct sigaction sa;
  58. sa.sa_handler = handleSignal;
  59. sa.sa_flags = 0;
  60. sigemptyset(&sa.sa_mask);
  61. sigaction(SIGILL, &sa, 0);
  62. sigaction(SIGBUS, &sa, 0);
  63. sigaction(SIGSEGV, &sa, 0);
  64. sigaction(SIGSYS, &sa, 0);
  65. #endif
  66. }
  67. void SignalHandler::handleSignal(int sig)
  68. {
  69. JumpBufferVec& jb = jumpBufferVec();
  70. if (!jb.empty())
  71. siglongjmp(jb.back().buf, sig);
  72. // Abort if no jump buffer registered
  73. std::abort();
  74. }
  75. SignalHandler::JumpBufferVec& SignalHandler::jumpBufferVec()
  76. {
  77. ThreadImpl* pThread = ThreadImpl::currentImpl();
  78. if (pThread)
  79. return pThread->_jumpBufferVec;
  80. else
  81. return _jumpBufferVec;
  82. }
  83. } // namespace Poco
  84. #endif // POCO_OS_FAMILY_UNIX