posix_signal.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #include "../../idlib/precompiled.h"
  21. #include "posix_public.h"
  22. #include <string.h>
  23. #include <errno.h>
  24. const int siglist[] = {
  25. SIGHUP,
  26. SIGQUIT,
  27. SIGILL,
  28. SIGTRAP,
  29. SIGIOT,
  30. SIGBUS,
  31. SIGFPE,
  32. SIGSEGV,
  33. SIGPIPE,
  34. SIGABRT,
  35. // SIGTTIN,
  36. // SIGTTOU,
  37. -1
  38. };
  39. const char *signames[] = {
  40. "SIGHUP",
  41. "SIGQUIT",
  42. "SIGILL",
  43. "SIGTRAP",
  44. "SIGIOT",
  45. "SIGBUS",
  46. "SIGFPE",
  47. "SIGSEGV",
  48. "SIGPIPE",
  49. "SIGABRT",
  50. // "SIGTTIN",
  51. // "SIGTTOUT"
  52. };
  53. static char fatalError[ 1024 ];
  54. /*
  55. ================
  56. Posix_ClearSigs
  57. ================
  58. */
  59. void Posix_ClearSigs( ) {
  60. struct sigaction action;
  61. int i;
  62. /* Set up the structure */
  63. action.sa_handler = SIG_DFL;
  64. sigemptyset( &action.sa_mask );
  65. action.sa_flags = 0;
  66. i = 0;
  67. while ( siglist[ i ] != -1 ) {
  68. if ( sigaction( siglist[ i ], &action, NULL ) != 0 ) {
  69. Sys_Printf( "Failed to reset %s handler: %s\n", signames[ i ], strerror( errno ) );
  70. }
  71. i++;
  72. }
  73. }
  74. /*
  75. ================
  76. sig_handler
  77. ================
  78. */
  79. static void sig_handler( int signum, siginfo_t *info, void *context ) {
  80. static bool double_fault = false;
  81. if ( double_fault ) {
  82. Sys_Printf( "double fault %s, bailing out\n", strsignal( signum ) );
  83. Posix_Exit( signum );
  84. }
  85. double_fault = true;
  86. // NOTE: see sigaction man page, could verbose the whole siginfo_t and print human readable si_code
  87. Sys_Printf( "signal caught: %s\nsi_code %d\n", strsignal( signum ), info->si_code );
  88. #ifndef ID_BT_STUB
  89. Sys_Printf( "callstack:\n%s", Sys_GetCallStackCurStr( 30 ) );
  90. #endif
  91. if ( fatalError[ 0 ] ) {
  92. Sys_Printf( "Was in fatal error shutdown: %s\n", fatalError );
  93. }
  94. Sys_Printf( "Trying to exit gracefully..\n" );
  95. Posix_SetExit( signum );
  96. common->Quit();
  97. }
  98. /*
  99. ================
  100. Posix_InitSigs
  101. ================
  102. */
  103. void Posix_InitSigs( ) {
  104. struct sigaction action;
  105. int i;
  106. fatalError[0] = '\0';
  107. /* Set up the structure */
  108. action.sa_sigaction = sig_handler;
  109. sigemptyset( &action.sa_mask );
  110. action.sa_flags = SA_SIGINFO | SA_NODEFER;
  111. i = 0;
  112. while ( siglist[ i ] != -1 ) {
  113. if ( siglist[ i ] == SIGFPE ) {
  114. action.sa_sigaction = Sys_FPE_handler;
  115. if ( sigaction( siglist[ i ], &action, NULL ) != 0 ) {
  116. Sys_Printf( "Failed to set SIGFPE handler: %s\n", strerror( errno ) );
  117. }
  118. action.sa_sigaction = sig_handler;
  119. } else if ( sigaction( siglist[ i ], &action, NULL ) != 0 ) {
  120. Sys_Printf( "Failed to set %s handler: %s\n", signames[ i ], strerror( errno ) );
  121. }
  122. i++;
  123. }
  124. // if the process is backgrounded (running non interactively)
  125. // then SIGTTIN or SIGTOU could be emitted, if not caught, turns into a SIGSTP
  126. signal( SIGTTIN, SIG_IGN );
  127. signal( SIGTTOU, SIG_IGN );
  128. }
  129. /*
  130. ==================
  131. Sys_SetFatalError
  132. ==================
  133. */
  134. void Sys_SetFatalError( const char *error ) {
  135. strncpy( fatalError, error, sizeof( fatalError ) );
  136. }