Leaked source code of windows server 2003
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.

189 lines
4.7 KiB

  1. // Copyright (c) 1996-1999 Microsoft Corporation
  2. //+-------------------------------------------------------------------------
  3. //
  4. // Microsoft Windows
  5. //
  6. // File: svrmain.cxx
  7. //
  8. // Contents: Main startup for Tracking (Server) Service
  9. //
  10. // Classes:
  11. //
  12. // Functions:
  13. //
  14. //
  15. //
  16. // History: 18-Nov-96 BillMo Created.
  17. //
  18. // Notes:
  19. //
  20. // Codework: UnInitialize RPC.
  21. // LnkSvrMoveNotify must not be passed machine id.
  22. // RPC stub routines should not catch exceptions.
  23. //
  24. //--------------------------------------------------------------------------
  25. #include "pch.cxx"
  26. #pragma hdrstop
  27. #define THIS_FILE_NUMBER SVRMAIN_CXX_FILE_NO
  28. #define TRKDATA_ALLOCATE
  29. #include "trksvr.hxx"
  30. #undef TRKDATA_ALLOCATE
  31. //+----------------------------------------------------------------------------
  32. //
  33. //
  34. //+----------------------------------------------------------------------------
  35. HANDLE g_hWait = NULL;
  36. void
  37. ServiceStopCallback( PVOID pContext, BOOLEAN fTimeout )
  38. {
  39. CTrkSvrSvc *ptrksvr = reinterpret_cast<CTrkSvrSvc*>(pContext);
  40. __try
  41. {
  42. UnregisterWait( g_hWait );
  43. g_hWait = NULL;
  44. // Close down the service. This could block while threads are
  45. // completed.
  46. ptrksvr->UnInitialize( S_OK );
  47. TrkLog((TRKDBG_SVR, TEXT("TrkSvr service stopped") ));
  48. CTrkRpcConfig::_fInitialized = FALSE;
  49. delete ptrksvr;
  50. TrkAssert( NULL == g_ptrksvr );
  51. TrkAssert( 0 == g_cThreadPoolRegistrations );
  52. // Uninitialize the DLL since it's never actually unloaded.
  53. CommonDllUnInit( &g_ctrksvr );
  54. }
  55. __except( BreakOnDebuggableException() )
  56. {
  57. TrkLog(( TRKDBG_ERROR, TEXT("Exception during service stop - %08x"), GetExceptionCode() ));
  58. }
  59. #if DBG
  60. TrkDebugDelete( );
  61. #endif
  62. }
  63. //+----------------------------------------------------------------------------
  64. //
  65. // ServiceMain
  66. //
  67. // This function is exported from the dll, and is called when we run
  68. // under svchost.exe.
  69. //
  70. //+----------------------------------------------------------------------------
  71. VOID WINAPI
  72. ServiceMain(DWORD dwArgc, LPTSTR *lptszArgv)
  73. {
  74. SVCS_ENTRY_POINT( dwArgc, lptszArgv, NULL, NULL );
  75. }
  76. //+----------------------------------------------------------------------------
  77. //
  78. // ServiceEntry
  79. //
  80. // This function is also exported from the dll, and is called directly when
  81. // we run under services.exe (the normal case), but is also called by
  82. // ServiceMain when we run under svchost.exe. We distinguish between the
  83. // two by checking pSvcsGlobalData (non-NULL iff running under services.exe).
  84. //
  85. // Since we use the Win32 thread pool, this routine returns after some
  86. // initialization, it isn't held for the lifetime of the service (except
  87. // when run under svchost.exe).
  88. //
  89. //+----------------------------------------------------------------------------
  90. VOID
  91. SVCS_ENTRY_POINT(
  92. DWORD NumArgs,
  93. LPTSTR *ArgsArray,
  94. PSVCHOST_GLOBAL_DATA pSvcsGlobalData,
  95. IN HANDLE SvcRefHandle
  96. )
  97. {
  98. HRESULT hr = S_OK;
  99. BOOL fDllInitialized = FALSE;
  100. CTrkSvrSvc *ptrksvr = NULL;
  101. __try
  102. {
  103. #if DBG
  104. {
  105. CTrkConfiguration cTrkConfiguration;
  106. cTrkConfiguration.Initialize();
  107. TrkDebugCreate( cTrkConfiguration._dwDebugStoreFlags, "TrkSvr" );
  108. cTrkConfiguration.UnInitialize();
  109. }
  110. #endif
  111. // Initialize the DLL itself. This raises if there is already an instance
  112. // of this service running.
  113. CommonDllInit( &g_ctrksvr );
  114. fDllInitialized = TRUE;
  115. TrkLog(( TRKDBG_SVR, TEXT("\n") ));
  116. // Create and initialize the primary service object
  117. ptrksvr = new CTrkSvrSvc;
  118. if( NULL == ptrksvr )
  119. {
  120. TrkLog(( TRKDBG_ERROR, TEXT("Couldn't alloc CTrkSvrSvc") ));
  121. return;
  122. }
  123. ptrksvr->Initialize( pSvcsGlobalData ); // sets g_ptrksvr
  124. }
  125. __except(BreakOnDebuggableException())
  126. {
  127. hr = GetExceptionCode();
  128. TrkLog((TRKDBG_ERROR, TEXT("couldn't initialize, hr=%08X"),hr));
  129. if( NULL != ptrksvr )
  130. {
  131. __try
  132. {
  133. ptrksvr->UnInitialize( hr );
  134. }
  135. __except( EXCEPTION_EXECUTE_HANDLER )
  136. {
  137. TrkAssert( !TEXT("Unexpected exception in trksvr!ServiceEntry") );
  138. }
  139. TrkAssert( NULL == g_ptrksvr );
  140. delete ptrksvr;
  141. ptrksvr = NULL;
  142. }
  143. if( fDllInitialized )
  144. CommonDllUnInit( &g_ctrksvr );
  145. }
  146. }