x86UNIXProcessControl.cpp 6.9 KB

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