POSIXProcessControl.cpp 6.9 KB

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