Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

231 lines
5.5 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. //-----------------------------------------------------------------------------
  9. // This is just a little redirection tool so I can get all the dlls in bin
  10. //-----------------------------------------------------------------------------
  11. #ifdef _WIN32
  12. #include <windows.h>
  13. #include <stdio.h>
  14. #include <assert.h>
  15. #include <direct.h>
  16. #elif POSIX
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <dlfcn.h>
  20. #include <string.h>
  21. #include <limits.h>
  22. #include <errno.h>
  23. #include <unistd.h>
  24. #define MAX_PATH PATH_MAX
  25. #endif
  26. #include "basetypes.h"
  27. #ifdef _WIN32
  28. typedef int (*DedicatedMain_t)( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  29. LPSTR lpCmdLine, int nCmdShow );
  30. #elif POSIX
  31. typedef int (*DedicatedMain_t)( int argc, char *argv[] );
  32. #endif
  33. //-----------------------------------------------------------------------------
  34. // Purpose: Return the directory where this .exe is running from
  35. // Output : char
  36. //-----------------------------------------------------------------------------
  37. static char *GetBaseDir( const char *pszBuffer )
  38. {
  39. static char basedir[ MAX_PATH ];
  40. char szBuffer[ MAX_PATH ];
  41. size_t j;
  42. char *pBuffer = NULL;
  43. strcpy( szBuffer, pszBuffer );
  44. pBuffer = strrchr( szBuffer,'\\' );
  45. if ( pBuffer )
  46. {
  47. *(pBuffer+1) = '\0';
  48. }
  49. strcpy( basedir, szBuffer );
  50. j = strlen( basedir );
  51. if (j > 0)
  52. {
  53. if ( ( basedir[ j-1 ] == '\\' ) ||
  54. ( basedir[ j-1 ] == '/' ) )
  55. {
  56. basedir[ j-1 ] = 0;
  57. }
  58. }
  59. return basedir;
  60. }
  61. #ifdef _WIN32
  62. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
  63. {
  64. // Must add 'bin' to the path....
  65. char* pPath = getenv("PATH");
  66. // Use the .EXE name to determine the root directory
  67. char moduleName[ MAX_PATH ];
  68. char szBuffer[ 4096 ];
  69. if ( !GetModuleFileName( hInstance, moduleName, MAX_PATH ) )
  70. {
  71. MessageBox( 0, "Failed calling GetModuleFileName", "Launcher Error", MB_OK );
  72. return 0;
  73. }
  74. // Get the root directory the .exe is in
  75. char* pRootDir = GetBaseDir( moduleName );
  76. #ifdef _DEBUG
  77. int len =
  78. #endif
  79. _snprintf( szBuffer, sizeof( szBuffer ) - 1, "PATH=%s\\bin\\;%s", pRootDir, pPath );
  80. szBuffer[ ARRAYSIZE(szBuffer) - 1 ] = 0;
  81. assert( len < 4096 );
  82. _putenv( szBuffer );
  83. HINSTANCE launcher = LoadLibrary("bin\\dedicated.dll"); // STEAM OK ... filesystem not mounted yet
  84. if (!launcher)
  85. {
  86. char *pszError;
  87. FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&pszError, 0, NULL);
  88. char szBuf[1024];
  89. _snprintf(szBuf, sizeof( szBuf ) - 1, "Failed to load the launcher DLL:\n\n%s", pszError);
  90. szBuf[ ARRAYSIZE(szBuf) - 1 ] = 0;
  91. MessageBox( 0, szBuf, "Launcher Error", MB_OK );
  92. LocalFree(pszError);
  93. return 0;
  94. }
  95. DedicatedMain_t main = (DedicatedMain_t)GetProcAddress( launcher, "DedicatedMain" );
  96. return main( hInstance, hPrevInstance, lpCmdLine, nCmdShow );
  97. }
  98. #elif POSIX
  99. #if defined( LINUX )
  100. #include <fcntl.h>
  101. static bool IsDebuggerPresent( int time )
  102. {
  103. // Need to get around the __wrap_open() stuff. Just find the open symbol
  104. // directly and use it...
  105. typedef int (open_func_t)( const char *pathname, int flags, mode_t mode );
  106. open_func_t *open_func = (open_func_t *)dlsym( RTLD_NEXT, "open" );
  107. if ( open_func )
  108. {
  109. for ( int i = 0; i < time; i++ )
  110. {
  111. int tracerpid = -1;
  112. int fd = (*open_func)( "/proc/self/status", O_RDONLY, S_IRUSR );
  113. if (fd >= 0)
  114. {
  115. char buf[ 4096 ];
  116. static const char tracerpid_str[] = "TracerPid:";
  117. const int len = read( fd, buf, sizeof(buf) - 1 );
  118. if ( len > 0 )
  119. {
  120. buf[ len ] = 0;
  121. const char *str = strstr( buf, tracerpid_str );
  122. tracerpid = str ? atoi( str + sizeof( tracerpid_str ) ) : -1;
  123. }
  124. close( fd );
  125. }
  126. if ( tracerpid > 0 )
  127. return true;
  128. sleep( 1 );
  129. }
  130. }
  131. return false;
  132. }
  133. static void WaitForDebuggerConnect( int argc, char *argv[], int time )
  134. {
  135. for ( int i = 1; i < argc; i++ )
  136. {
  137. if ( strstr( argv[i], "-wait_for_debugger" ) )
  138. {
  139. printf( "\nArg -wait_for_debugger found.\nWaiting %dsec for debugger...\n", time );
  140. printf( " pid = %d\n", getpid() );
  141. if ( IsDebuggerPresent( time ) )
  142. printf("Debugger connected...\n\n");
  143. break;
  144. }
  145. }
  146. }
  147. #else
  148. static void WaitForDebuggerConnect( int argc, char *argv[], int time )
  149. {
  150. }
  151. #endif // !LINUX
  152. int main( int argc, char *argv[] )
  153. {
  154. // Must add 'bin' to the path....
  155. char* pPath = getenv("LD_LIBRARY_PATH");
  156. char szBuffer[4096];
  157. char cwd[ MAX_PATH ];
  158. if ( !getcwd( cwd, sizeof(cwd)) )
  159. {
  160. printf( "getcwd failed (%s)", strerror(errno));
  161. }
  162. snprintf( szBuffer, sizeof( szBuffer ) - 1, "LD_LIBRARY_PATH=%s/bin:%s", cwd, pPath );
  163. int ret = putenv( szBuffer );
  164. if ( ret )
  165. {
  166. printf( "%s\n", strerror(errno) );
  167. }
  168. void *tier0 = dlopen( "libtier0" DLL_EXT_STRING, RTLD_NOW );
  169. void *vstdlib = dlopen( "libvstdlib" DLL_EXT_STRING, RTLD_NOW );
  170. const char *pBinaryName = "dedicated" DLL_EXT_STRING;
  171. void *dedicated = dlopen( pBinaryName, RTLD_NOW );
  172. if ( !dedicated )
  173. {
  174. printf( "Failed to open %s (%s)\n", pBinaryName, dlerror());
  175. return -1;
  176. }
  177. DedicatedMain_t dedicated_main = (DedicatedMain_t)dlsym( dedicated, "DedicatedMain" );
  178. if ( !dedicated_main )
  179. {
  180. printf( "Failed to find dedicated server entry point (%s)\n", dlerror() );
  181. return -1;
  182. }
  183. WaitForDebuggerConnect( argc, argv, 30 );
  184. ret = dedicated_main( argc,argv );
  185. dlclose( dedicated );
  186. dlclose( vstdlib );
  187. dlclose( tier0 );
  188. }
  189. #endif