x86UNIXProcessControl.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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 "platform/platformInput.h"
  26. #include "console/console.h"
  27. #include <stdlib.h>
  28. #include <unistd.h>
  29. #include <signal.h>
  30. #ifndef TORQUE_DEDICATED
  31. #include <SDL/SDL.h>
  32. #endif
  33. //-----------------------------------------------------------------------------
  34. // This is a mainly a debugging function for intercepting a nonzero exit code
  35. // and generating a core dump for a stack trace.
  36. static void CheckExitCode(S64 exitCode)
  37. {
  38. if (exitCode != 0)
  39. {
  40. Con::errorf(ConsoleLogEntry::General,
  41. "Nonzero exit code: %d, triggering SIGSEGV for core dump",
  42. exitCode);
  43. kill(getpid(), SIGSEGV);
  44. }
  45. }
  46. //-----------------------------------------------------------------------------
  47. static void SignalHandler(int sigtype)
  48. {
  49. if (sigtype == SIGSEGV || sigtype == SIGTRAP)
  50. {
  51. signal(SIGSEGV, SIG_DFL);
  52. signal(SIGTRAP, SIG_DFL);
  53. // restore the signal handling to default so that we don't get into
  54. // a crash loop with ImmediateShutdown
  55. ImmediateShutdown(-sigtype, sigtype);
  56. }
  57. else
  58. {
  59. signal(sigtype, SIG_DFL);
  60. dPrintf("Unknown signal caught by SignalHandler: %d\n", sigtype);
  61. // exit to be safe
  62. ImmediateShutdown(1);
  63. }
  64. }
  65. //-----------------------------------------------------------------------------
  66. void Cleanup(bool minimal)
  67. {
  68. if (!minimal)
  69. {
  70. Input::destroy();
  71. }
  72. StdConsole::destroy();
  73. #ifndef TORQUE_DEDICATED
  74. SDL_Quit();
  75. #endif
  76. }
  77. //-----------------------------------------------------------------------------
  78. void ImmediateShutdown(S32 exitCode, S32 signalNum)
  79. {
  80. bool segfault = signalNum > 0;
  81. Cleanup(segfault);
  82. if (!segfault)
  83. {
  84. dPrintf("Exiting\n");
  85. // exit (doesn't call destructors)
  86. _exit(exitCode);
  87. }
  88. else
  89. {
  90. // there is a problem in kernel 2.4.17 which causes a hang when a segfault
  91. // occurs. also subsequent runs of "ps" will hang and the machine has to be
  92. // hard reset to clear up the problem
  93. // JMQ: this bug appears to be fixed in 2.4.18
  94. //#define KERNEL_2_4_WORKAROUND
  95. #ifdef KERNEL_2_4_WORKAROUND
  96. dPrintf("Segmentation Fault (Exiting without core dump due to #define KERNEL_2_4_WORKAROUND)\n");
  97. dFflushStdout();
  98. _exit(exitCode);
  99. #else
  100. // kill with signal
  101. kill(getpid(), signalNum);
  102. #endif
  103. }
  104. }
  105. //-----------------------------------------------------------------------------
  106. void ProcessControlInit()
  107. {
  108. // JMQ: ignore IO signals background read/write terminal (so that we don't
  109. // get suspended in daemon mode)
  110. signal(SIGTTIN, SIG_IGN);
  111. signal(SIGTTOU, SIG_IGN);
  112. // we're not interested in the exit status of child processes, so this
  113. // prevents zombies from accumulating.
  114. #if defined(__FreeBSD__)
  115. signal(SIGCHLD, SIG_IGN);
  116. #else
  117. signal(SIGCLD, SIG_IGN);
  118. #endif
  119. // install signal handler for SIGSEGV, so that we can attempt
  120. // clean shutdown
  121. signal(SIGSEGV, &SignalHandler);
  122. signal(SIGTRAP, &SignalHandler);
  123. }
  124. //-----------------------------------------------------------------------------
  125. void Platform::postQuitMessage(const S32 in_quitVal)
  126. {
  127. // if we have a window send a quit event, otherwise just force shutdown
  128. #if 0
  129. if (x86UNIXState->windowCreated())
  130. {
  131. CheckExitCode(in_quitVal);
  132. SendQuitEvent();
  133. }
  134. else
  135. #endif
  136. {
  137. forceShutdown(in_quitVal);
  138. }
  139. }
  140. //-----------------------------------------------------------------------------
  141. void Platform::debugBreak()
  142. {
  143. // in windows, "Calling DebugBreak causes the program to display
  144. // a dialog box as if it had crashed." So we segfault.
  145. Con::errorf(ConsoleLogEntry::General,
  146. "Platform::debugBreak: triggering SIGSEGV for core dump");
  147. //kill(getpid(), SIGSEGV);
  148. kill(getpid(), SIGTRAP);
  149. }
  150. //-----------------------------------------------------------------------------
  151. void Platform::forceShutdown(S32 returnValue)
  152. {
  153. #if 0
  154. // if a dedicated server is running, turn it off
  155. if (x86UNIXState->isDedicated() && Game->isRunning())
  156. Game->setRunning(false);
  157. else
  158. #endif
  159. ImmediateShutdown(returnValue);
  160. }
  161. //-----------------------------------------------------------------------------
  162. void Platform::outputDebugString(const char *string, ...)
  163. {
  164. char buffer[2048];
  165. va_list args;
  166. va_start( args, string );
  167. dVsprintf( buffer, sizeof(buffer), string, args );
  168. va_end( args );
  169. U32 length = dStrlen(buffer);
  170. if( length == (sizeof(buffer) - 1 ) )
  171. length--;
  172. buffer[length++] = '\n';
  173. buffer[length] = '\0';
  174. fwrite(buffer, sizeof(char), length, stderr);
  175. }
  176. //-----------------------------------------------------------------------------
  177. // testing function
  178. ConsoleFunction(debug_debugbreak, void, 1, 1, "debug_debugbreak()")
  179. {
  180. Platform::debugBreak();
  181. }
  182. //-----------------------------------------------------------------------------
  183. void Platform::restartInstance()
  184. {
  185. /*
  186. if (Game->isRunning() )
  187. {
  188. //Con::errorf( "Error restarting Instance. Game is Still running!");
  189. return;
  190. }
  191. char cmd[2048];
  192. sprintf(cmd, "%s &", x86UNIXState->getExePathName());
  193. system(cmd);
  194. */
  195. exit(0);
  196. }