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.

185 lines
4.6 KiB

  1. /*++
  2. Copyright (C) 1996-2001 Microsoft Corporation
  3. Module Name:
  4. UNLOAD.CPP
  5. Abstract:
  6. Unloading helper.
  7. History:
  8. --*/
  9. #include "precomp.h"
  10. #include <stdio.h>
  11. #include <wbemcomn.h>
  12. #include <unload.h>
  13. #include <arrtempl.h>
  14. CUnloadInstruction::CUnloadInstruction(LPCWSTR wszPath,
  15. IWbemContext* pFirstContext)
  16. : CBasicUnloadInstruction(), m_strPath(SysAllocString(wszPath)),
  17. m_pFirstContext(pFirstContext), m_pNamespace(NULL)
  18. {
  19. m_Interval.SetMilliseconds(0);
  20. if(m_pFirstContext)
  21. m_pFirstContext->AddRef();
  22. }
  23. void CUnloadInstruction::Clear()
  24. {
  25. }
  26. CUnloadInstruction::~CUnloadInstruction()
  27. {
  28. if(m_pNamespace)
  29. m_pNamespace->Release();
  30. SysFreeString(m_strPath);
  31. if(m_pFirstContext)
  32. m_pFirstContext->Release();
  33. }
  34. void CUnloadInstruction::SetToDefault()
  35. {
  36. m_Interval.SetMilliseconds(3600000);
  37. }
  38. void CUnloadInstruction::Reread(IWbemContext* pContext)
  39. {
  40. HRESULT hres;
  41. if(m_pNamespace == NULL)
  42. {
  43. IWbemLocator * pLocator = NULL;
  44. hres = CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL,
  45. CLSCTX_INPROC_SERVER, IID_IWbemLocator, (void**)&pLocator);
  46. if(hres == S_OK)
  47. {
  48. hres = pLocator->ConnectServer(L"ROOT", NULL, NULL, NULL,
  49. 0, NULL, NULL, &m_pNamespace);
  50. pLocator->Release();
  51. }
  52. if(m_pNamespace == NULL)
  53. {
  54. SetToDefault();
  55. return;
  56. }
  57. }
  58. m_Interval = staticRead(m_pNamespace, pContext, m_strPath);
  59. }
  60. CWbemTime CUnloadInstruction::GetFirstFiringTime() const
  61. {
  62. if(m_Interval.IsZero())
  63. {
  64. // This function is const, but Reread is not, so have to cast
  65. // ==========================================================
  66. ((CUnloadInstruction*)this)->Reread(m_pFirstContext);
  67. }
  68. return CBasicUnloadInstruction::GetFirstFiringTime();
  69. }
  70. //*****************************************************************************
  71. //
  72. // BASIC VERSION
  73. //
  74. //*****************************************************************************
  75. CBasicUnloadInstruction::CBasicUnloadInstruction(CWbemInterval Interval)
  76. : m_lRef(0), m_bTerminate(FALSE), m_Interval(Interval)
  77. {
  78. }
  79. void CBasicUnloadInstruction::Terminate()
  80. {
  81. CInCritSec ics(&m_cs);
  82. m_bTerminate = TRUE;
  83. }
  84. // static
  85. CWbemInterval CBasicUnloadInstruction::staticRead(IWbemServices* pRoot,
  86. IWbemContext* pContext, LPCWSTR wszPath)
  87. {
  88. HRESULT hres;
  89. BSTR strPath = SysAllocString(wszPath);
  90. if(strPath == NULL)
  91. return CWbemInterval::GetInfinity();
  92. CSysFreeMe sfm1(strPath);
  93. IWbemClassObject* pObj = NULL;
  94. hres = pRoot->GetObject(strPath, 0, pContext, &pObj, NULL);
  95. if(FAILED(hres))
  96. {
  97. ERRORTRACE((LOG_WBEMCORE, "Unable to read cache configuration object "
  98. "at %S: %X\n", strPath, hres));
  99. return CWbemInterval::GetInfinity();
  100. }
  101. CReleaseMe rm1(pObj);
  102. VARIANT var;
  103. VariantInit(&var);
  104. hres = pObj->Get(L"ClearAfter", 0, &var, NULL, NULL);
  105. if(FAILED(hres))
  106. {
  107. ERRORTRACE((LOG_WBEMCORE, "No ClearCache property in cache "
  108. "configuration object at %S: %X\n", strPath, hres));
  109. return CWbemInterval::GetInfinity();
  110. }
  111. CClearMe cm(&var);
  112. if(var.vt != VT_BSTR)
  113. {
  114. return CWbemInterval::GetInfinity();
  115. }
  116. DWORD dwYears, dwMonths, dwDays, dwHours, dwMinutes, dwSeconds;
  117. if(swscanf(var.bstrVal, L"%4u%2u%2u%2u%2u%2u", &dwYears, &dwMonths,
  118. &dwDays, &dwHours, &dwMinutes, &dwSeconds) != 6)
  119. {
  120. ERRORTRACE((LOG_WBEMCORE, "Unparsable ClearCache property in cache "
  121. "configuration object at %S: %X\n", strPath, hres));
  122. return CWbemInterval::GetInfinity();
  123. }
  124. if(dwYears != 0 || dwMonths != 0)
  125. {
  126. // makes no sense
  127. // ==============
  128. return CWbemInterval::GetInfinity();
  129. }
  130. dwSeconds += dwMinutes * 60 + dwHours * 3600 + dwDays * 3600 * 24;
  131. CWbemInterval Interval;
  132. Interval.SetMilliseconds(1000 * dwSeconds);
  133. return Interval;
  134. }
  135. CWbemTime CBasicUnloadInstruction::GetFirstFiringTime() const
  136. {
  137. return CWbemTime::GetCurrentTime() + m_Interval;
  138. }
  139. CWbemTime CBasicUnloadInstruction::GetNextFiringTime(CWbemTime LastFiringTime,
  140. OUT long* plFiringCount) const
  141. {
  142. if(m_bTerminate)
  143. return CWbemTime::GetInfinity();
  144. *plFiringCount = 1;
  145. return LastFiringTime + m_Interval;
  146. }