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.

239 lines
7.1 KiB

  1. /*---------------------------------------------------------------------------
  2. File: Dispatcher.cpp
  3. Comments: COM server implementation for dispatcher, generated by ATL.
  4. (c) Copyright 1999, Mission Critical Software, Inc., All Rights Reserved
  5. Proprietary and confidential to Mission Critical Software, Inc.
  6. REVISION LOG ENTRY
  7. Revision By: Christy Boles
  8. Revised on 02/18/99 11:34:16
  9. ---------------------------------------------------------------------------
  10. */// Dispatcher.cpp : Implementation of WinMain
  11. // Note: Proxy/Stub Information
  12. // To build a separate proxy/stub DLL,
  13. // run nmake -f Dispatcherps.mk in the project directory.
  14. #include "stdafx.h"
  15. #include "resource.h"
  16. #include <initguid.h>
  17. //#include "..\Common\Include\McsDispatcher.h"
  18. #include "Dispatch.h"
  19. //#include "McsDispatcher_i.c"
  20. #include "Dispatch_i.c"
  21. #include "DDisp.h"
  22. #include "DInst.h"
  23. #include <MigrationMutex.h>
  24. #include "sdhelper.h"
  25. const DWORD dwTimeOut = 5000; // time for EXE to be idle before shutting down
  26. const DWORD dwPause = 1000; // time to wait for threads to finish up
  27. // Passed to CreateThread to monitor the shutdown event
  28. static DWORD WINAPI MonitorProc(void* pv)
  29. {
  30. CExeModule* p = (CExeModule*)pv;
  31. p->MonitorShutdown();
  32. return 0;
  33. }
  34. LONG CExeModule::Unlock()
  35. {
  36. LONG l = CComModule::Unlock();
  37. if (l == 0)
  38. {
  39. bActivity = true;
  40. SetEvent(hEventShutdown); // tell monitor that we transitioned to zero
  41. }
  42. return l;
  43. }
  44. //Monitors the shutdown event
  45. void CExeModule::MonitorShutdown()
  46. {
  47. while (1)
  48. {
  49. WaitForSingleObject(hEventShutdown, INFINITE);
  50. DWORD dwWait=0;
  51. do
  52. {
  53. bActivity = false;
  54. dwWait = WaitForSingleObject(hEventShutdown, dwTimeOut);
  55. } while (dwWait == WAIT_OBJECT_0);
  56. // timed out
  57. if (!bActivity && m_nLockCnt == 0) // if no activity let's really bail
  58. {
  59. #if _WIN32_WINNT >= 0x0400 & defined(_ATL_FREE_THREADED)
  60. CoSuspendClassObjects();
  61. if (!bActivity && m_nLockCnt == 0)
  62. #endif
  63. break;
  64. }
  65. }
  66. CloseHandle(hEventShutdown);
  67. PostThreadMessage(dwThreadID, WM_QUIT, 0, 0);
  68. }
  69. bool CExeModule::StartMonitor()
  70. {
  71. bool bCreated = false;
  72. hEventShutdown = CreateEvent(NULL, false, false, NULL);
  73. if (hEventShutdown == NULL)
  74. return false;
  75. DWORD dwThreadID;
  76. HANDLE h = CreateThread(NULL, 0, MonitorProc, this, 0, &dwThreadID);
  77. if (h != NULL)
  78. bCreated = true;
  79. CloseHandle(h);
  80. return bCreated;
  81. }
  82. CExeModule _Module;
  83. BEGIN_OBJECT_MAP(ObjectMap)
  84. OBJECT_ENTRY(CLSID_DCTDispatcher, CDCTDispatcher)
  85. OBJECT_ENTRY(CLSID_DCTInstaller, CDCTInstaller)
  86. END_OBJECT_MAP()
  87. LPCTSTR FindOneOf(LPCTSTR p1, LPCTSTR p2)
  88. {
  89. while (p1 != NULL && *p1 != NULL)
  90. {
  91. LPCTSTR p = p2;
  92. while (p != NULL && *p != NULL)
  93. {
  94. if (*p1 == *p)
  95. return CharNext(p1);
  96. p = CharNext(p);
  97. }
  98. p1 = CharNext(p1);
  99. }
  100. return NULL;
  101. }
  102. /////////////////////////////////////////////////////////////////////////////
  103. //
  104. extern "C" int WINAPI _tWinMain(HINSTANCE hInstance,
  105. HINSTANCE /*hPrevInstance*/, LPTSTR lpCmdLine, int /*nShowCmd*/)
  106. {
  107. ATLTRACE(_T("{McsDispatcher.exe}_tWinMain(hInstance=0x%08lX,...)\n"), hInstance);
  108. // obtain dispatcher mutex
  109. // the migration driver uses this mutex to determine
  110. // if the dispatcher process is currently running
  111. CMigrationMutex mutex(DISPATCHER_MUTEX, true);
  112. // set debug flags to check memory allocations and leaks
  113. _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF|_CRTDBG_LEAK_CHECK_DF);
  114. lpCmdLine = GetCommandLine(); //this line necessary for _ATL_MIN_CRT
  115. #if defined(_ATL_FREE_THREADED)
  116. HRESULT hRes = CoInitializeEx(NULL, COINIT_MULTITHREADED);
  117. #else
  118. HRESULT hRes = CoInitialize(NULL);
  119. #endif
  120. BOOL bSecurityInitialized = FALSE;
  121. _ASSERTE(SUCCEEDED(hRes));
  122. if ( SUCCEEDED(hRes) )
  123. {
  124. TSD* adminsAndSystemSD = BuildAdminsAndSystemSDForCOM();
  125. if (adminsAndSystemSD)
  126. {
  127. hRes = CoInitializeSecurity(
  128. const_cast<SECURITY_DESCRIPTOR*>(adminsAndSystemSD->GetSD()), //Points to security descriptor
  129. -1, //Count of entries in asAuthSvc
  130. NULL, //Array of names to register
  131. NULL, //Reserved for future use
  132. RPC_C_AUTHN_LEVEL_PKT_PRIVACY, //The default authentication
  133. //level for proxies
  134. RPC_C_IMP_LEVEL_IMPERSONATE, //The default impersonation
  135. //level for proxies
  136. NULL, //Reserved; must be set to NULL
  137. EOAC_NONE, //Additional client or
  138. //server-side capabilities
  139. NULL //Reserved for future use
  140. );
  141. if (SUCCEEDED(hRes))
  142. bSecurityInitialized = TRUE;
  143. if (adminsAndSystemSD)
  144. delete adminsAndSystemSD;
  145. }
  146. }
  147. else
  148. {
  149. // if CoInitialize fails, returns an error
  150. return 1;
  151. }
  152. // if CoInitializeSecurity fails, returns an error
  153. if (!bSecurityInitialized)
  154. {
  155. CoUninitialize();
  156. return 1;
  157. }
  158. _Module.Init(ObjectMap, hInstance, &LIBID_MCSDISPATCHERLib);
  159. _Module.dwThreadID = GetCurrentThreadId();
  160. TCHAR szTokens[] = _T("-/");
  161. int nRet = 0;
  162. BOOL bRun = TRUE;
  163. LPCTSTR lpszToken = FindOneOf(lpCmdLine, szTokens);
  164. while (lpszToken != NULL)
  165. {
  166. if (lstrcmpi(lpszToken, _T("UnregServer"))==0)
  167. {
  168. _Module.UpdateRegistryFromResource(IDR_Dispatcher, FALSE);
  169. nRet = _Module.UnregisterServer(TRUE);
  170. bRun = FALSE;
  171. break;
  172. }
  173. if (lstrcmpi(lpszToken, _T("RegServer"))==0)
  174. {
  175. _Module.UpdateRegistryFromResource(IDR_Dispatcher, TRUE);
  176. nRet = _Module.RegisterServer(TRUE);
  177. bRun = FALSE;
  178. break;
  179. }
  180. lpszToken = FindOneOf(lpszToken, szTokens);
  181. }
  182. if (bRun)
  183. {
  184. _Module.StartMonitor();
  185. #if defined(_ATL_FREE_THREADED)
  186. hRes = _Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER,
  187. REGCLS_MULTIPLEUSE | REGCLS_SUSPENDED);
  188. _ASSERTE(SUCCEEDED(hRes));
  189. hRes = CoResumeClassObjects();
  190. #else
  191. hRes = _Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER,
  192. REGCLS_MULTIPLEUSE);
  193. #endif
  194. _ASSERTE(SUCCEEDED(hRes));
  195. MSG msg;
  196. while (GetMessage(&msg, 0, 0, 0))
  197. DispatchMessage(&msg);
  198. _Module.RevokeClassObjects();
  199. Sleep(dwPause); //wait for any threads to finish
  200. }
  201. ATLTRACE(_T("{McsDispatcher.exe}_tWinMain() : hInstance=0x%08lX\n"), hInstance);
  202. _Module.Term();
  203. CoUninitialize();
  204. return nRet;
  205. }