Source code of Windows XP (NT5)
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.

135 lines
3.8 KiB

  1. // wuauserv.cpp
  2. #include <windows.h>
  3. #include <ausvc.h>
  4. #include <wusafefn.h>
  5. typedef HRESULT (WINAPI *AUSERVICEMAIN)(DWORD dwNumServicesArg,
  6. LPWSTR *lpServiceArgVectors,
  7. AUSERVICEHANDLER pfnServiceHandler,
  8. BOOL fJustSelfUpdated);
  9. AUSERVICEMAIN g_pfnServiceMain = NULL;
  10. AUSERVICEHANDLER g_pfnServiceHandler = NULL;
  11. AUGETENGSTATUS g_pfnGetEngineStatusInfo = NULL;
  12. AUREGSERVICEVER g_pfnRegisterServiceVersion = NULL;
  13. CRITICAL_SECTION csWuaueng;
  14. DWORD WINAPI ServiceHandler(DWORD fdwControl, DWORD dwEventType, LPVOID pEventData, LPVOID lpContext)
  15. {
  16. DWORD dwRet = NOERROR;
  17. EnterCriticalSection(&csWuaueng);
  18. if ( NULL != g_pfnServiceHandler )
  19. {
  20. dwRet = g_pfnServiceHandler(fdwControl, dwEventType, pEventData, lpContext);
  21. }
  22. else
  23. {
  24. dwRet = ERROR_CALL_NOT_IMPLEMENTED;
  25. }
  26. LeaveCriticalSection(&csWuaueng);
  27. return dwRet;
  28. }
  29. void WINAPI ServiceMain(DWORD dwNumServicesArg, LPWSTR *lpServiceArgVectors)
  30. {
  31. HRESULT hr = S_OK;
  32. HMODULE hModule = NULL;
  33. AUENGINEINFO_VER_1 engineInfo;
  34. DWORD dwEngineVersion = 0;
  35. BOOL fCompatibleEngineVersion = FALSE;
  36. if ( !SafeInitializeCriticalSection(&csWuaueng) )
  37. {
  38. return;
  39. }
  40. do
  41. {
  42. EnterCriticalSection(&csWuaueng);
  43. fCompatibleEngineVersion = FALSE;
  44. g_pfnServiceHandler = NULL;
  45. g_pfnGetEngineStatusInfo = NULL;
  46. g_pfnRegisterServiceVersion = NULL;
  47. // check if we need to release wuaueng.dll
  48. if ( (S_FALSE == hr) && !FreeLibrary(hModule) )
  49. {
  50. hr = E_FAIL;
  51. hModule = NULL;
  52. g_pfnServiceMain = NULL;
  53. g_pfnServiceHandler = NULL;
  54. }
  55. else
  56. {
  57. // if we can't load wuaueng.dll, we fail to start
  58. if ( (NULL == (hModule = LoadLibraryFromSystemDir(TEXT("wuaueng.dll")))) ||
  59. (NULL == (g_pfnServiceMain = (AUSERVICEMAIN)::GetProcAddress(hModule, "ServiceMain"))) ||
  60. (NULL == (g_pfnServiceHandler = (AUSERVICEHANDLER)::GetProcAddress(hModule, "ServiceHandler"))) )
  61. {
  62. hr = E_FAIL;
  63. g_pfnServiceMain = NULL;
  64. g_pfnServiceHandler = NULL;
  65. }
  66. else //wuaueng.dll successfully loaded, check to see if the engine supports following entry points
  67. {
  68. if ( (NULL != (g_pfnRegisterServiceVersion = (AUREGSERVICEVER)::GetProcAddress(hModule, "RegisterServiceVersion"))) &&
  69. (NULL != (g_pfnGetEngineStatusInfo = (AUGETENGSTATUS)::GetProcAddress(hModule, "GetEngineStatusInfo"))) )
  70. {
  71. fCompatibleEngineVersion = TRUE;
  72. }
  73. }
  74. }
  75. LeaveCriticalSection(&csWuaueng);
  76. if ( SUCCEEDED(hr) )
  77. {
  78. if (fCompatibleEngineVersion)
  79. {
  80. // Register service version with engine and check if engine supports the service version
  81. fCompatibleEngineVersion = g_pfnRegisterServiceVersion(AUSRV_VERSION, &dwEngineVersion);
  82. }
  83. hr = g_pfnServiceMain(dwNumServicesArg, lpServiceArgVectors, ServiceHandler,
  84. (S_FALSE == hr) ? TRUE: FALSE /* we just reloaded wuaueng.dll */);
  85. if(fCompatibleEngineVersion)
  86. {
  87. //The engine service main has exited, set the service status to SERVICE_STOP_PENDING
  88. fCompatibleEngineVersion = g_pfnGetEngineStatusInfo((void*)&engineInfo);
  89. }
  90. }
  91. } while ( S_FALSE == hr );
  92. EnterCriticalSection(&csWuaueng);
  93. if ( NULL != hModule )
  94. {
  95. FreeLibrary(hModule);
  96. }
  97. g_pfnServiceHandler = NULL;
  98. g_pfnGetEngineStatusInfo = NULL;
  99. g_pfnRegisterServiceVersion = NULL;
  100. LeaveCriticalSection(&csWuaueng);
  101. DeleteCriticalSection(&csWuaueng);
  102. if(fCompatibleEngineVersion)
  103. {
  104. //stop the service
  105. engineInfo.serviceStatus.dwCurrentState = SERVICE_STOPPED;
  106. SetServiceStatus(engineInfo.hServiceStatus, &engineInfo.serviceStatus);
  107. }
  108. return;
  109. }