x86UNIXProcessControl.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platformX86UNIX/platformX86UNIX.h"
  23. #include "platformX86UNIX/x86UNIXState.h"
  24. #include "platformX86UNIX/x86UNIXStdConsole.h"
  25. #include "platformX86UNIX/x86UNIXMutex.h"
  26. #include "game/gameInterface.h"
  27. #include "platform/platformAudio.h"
  28. #include "platform/platformVideo.h"
  29. #include "platform/platformInput.h"
  30. #include "console/console.h"
  31. #include <stdlib.h>
  32. #include <unistd.h>
  33. #include <signal.h>
  34. #ifndef DEDICATED
  35. #include <SDL/SDL.h>
  36. #endif
  37. extern void SendQuitEvent();
  38. ProcessMutex pMutex;
  39. //-----------------------------------------------------------------------------
  40. // This is a mainly a debugging function for intercepting a nonzero exit code
  41. // and generating a core dump for a stack trace.
  42. // Need an S64 here because postQuitMessage uses a U32, and
  43. // forceshutdown uses an S32. So S64 is needed to
  44. // accomodate them both
  45. static void CheckExitCode(S64 exitCode)
  46. {
  47. if (exitCode != 0)
  48. {
  49. Con::errorf(ConsoleLogEntry::General,
  50. "Nonzero exit code: %d, triggering SIGSEGV for core dump",
  51. exitCode);
  52. kill(getpid(), SIGSEGV);
  53. }
  54. }
  55. //-----------------------------------------------------------------------------
  56. static void SignalHandler(int sigtype)
  57. {
  58. if (sigtype == SIGSEGV || sigtype == SIGTRAP)
  59. {
  60. signal(SIGSEGV, SIG_DFL);
  61. signal(SIGTRAP, SIG_DFL);
  62. // restore the signal handling to default so that we don't get into
  63. // a crash loop with ImmediateShutdown
  64. ImmediateShutdown(-sigtype, sigtype);
  65. }
  66. else
  67. {
  68. signal(sigtype, SIG_DFL);
  69. dPrintf("Unknown signal caught by SignalHandler: %d\n", sigtype);
  70. dFflushStdout();
  71. // exit to be safe
  72. ImmediateShutdown(1);
  73. }
  74. }
  75. //-----------------------------------------------------------------------------
  76. void Cleanup(bool minimal)
  77. {
  78. if (!minimal)
  79. {
  80. Audio::OpenALShutdown();
  81. Video::destroy();
  82. Input::destroy();
  83. }
  84. StdConsole::destroy();
  85. #ifndef DEDICATED
  86. GLLoader::OpenGLShutdown();
  87. SDL_Quit();
  88. #endif
  89. // Display* display = x86UNIXState->GetDisplayPointer();
  90. // if (display != NULL)
  91. // XCloseDisplay(display);
  92. pMutex.release();
  93. }
  94. //-----------------------------------------------------------------------------
  95. void ImmediateShutdown(S32 exitCode, S32 signalNum)
  96. {
  97. bool segfault = signalNum > 0;
  98. Cleanup(segfault);
  99. if (!segfault)
  100. {
  101. dPrintf("Exiting\n");
  102. // exit (doesn't call destructors)
  103. _exit(exitCode);
  104. }
  105. else
  106. {
  107. // there is a problem in kernel 2.4.17 which causes a hang when a segfault
  108. // occurs. also subsequent runs of "ps" will hang and the machine has to be
  109. // hard reset to clear up the problem
  110. // JMQ: this bug appears to be fixed in 2.4.18
  111. //#define KERNEL_2_4_WORKAROUND
  112. #ifdef KERNEL_2_4_WORKAROUND
  113. dPrintf("Segmentation Fault (Exiting without core dump due to #define KERNEL_2_4_WORKAROUND)\n");
  114. dFflushStdout();
  115. _exit(exitCode);
  116. #else
  117. // kill with signal
  118. kill(getpid(), signalNum);
  119. #endif
  120. }
  121. }
  122. //-----------------------------------------------------------------------------
  123. void ProcessControlInit()
  124. {
  125. // JMQ: ignore IO signals background read/write terminal (so that we don't
  126. // get suspended in daemon mode)
  127. signal(SIGTTIN, SIG_IGN);
  128. signal(SIGTTOU, SIG_IGN);
  129. // we're not interested in the exit status of child processes, so this
  130. // prevents zombies from accumulating.
  131. #if defined(__FreeBSD__) || defined(__OpenBSD__)
  132. signal(SIGCHLD, SIG_IGN);
  133. #else
  134. signal(SIGCLD, SIG_IGN);
  135. #endif
  136. // install signal handler for SIGSEGV, so that we can attempt
  137. // clean shutdown
  138. signal(SIGSEGV, &SignalHandler);
  139. signal(SIGTRAP, &SignalHandler);
  140. }
  141. //-----------------------------------------------------------------------------
  142. bool AcquireProcessMutex(const char *mutexName)
  143. {
  144. return pMutex.acquire(mutexName);
  145. }
  146. //-----------------------------------------------------------------------------
  147. void Platform::postQuitMessage(const U32 in_quitVal)
  148. {
  149. // if we have a window send a quit event, otherwise just force shutdown
  150. #ifndef DEDICATED
  151. if (x86UNIXState->windowCreated())
  152. {
  153. CheckExitCode(in_quitVal);
  154. SendQuitEvent();
  155. }
  156. else
  157. #endif
  158. {
  159. forceShutdown(in_quitVal);
  160. }
  161. }
  162. //-----------------------------------------------------------------------------
  163. void Platform::debugBreak()
  164. {
  165. // in windows, "Calling DebugBreak causes the program to display
  166. // a dialog box as if it had crashed." So we segfault.
  167. Con::errorf(ConsoleLogEntry::General,
  168. "Platform::debugBreak: triggering SIGSEGV for core dump");
  169. //kill(getpid(), SIGSEGV);
  170. kill(getpid(), SIGTRAP);
  171. }
  172. //-----------------------------------------------------------------------------
  173. void Platform::forceShutdown(S32 returnValue)
  174. {
  175. CheckExitCode(returnValue);
  176. // if a dedicated server is running, turn it off
  177. if (x86UNIXState->isDedicated() && Game->isRunning())
  178. Game->setRunning(false);
  179. else
  180. ImmediateShutdown(returnValue);
  181. }
  182. //-----------------------------------------------------------------------------
  183. // testing function
  184. ConsoleFunction(debug_debugbreak, void, 1, 1, "debug_debugbreak()")
  185. {
  186. Platform::debugBreak();
  187. }