POSIXProcessControl.cpp 6.8 KB

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