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.

559 lines
17 KiB

  1. // Copyright (c) 2000-2001 Microsoft Corporation, All Rights Reserved
  2. // JobObjLimitInfoProv.cpp
  3. //#define _WIN32_WINNT 0x0500
  4. #include "precomp.h"
  5. //#include <windows.h>
  6. #include "cominit.h"
  7. //#include <objbase.h>
  8. //#include <comdef.h>
  9. #include "CUnknown.h"
  10. #include <wbemprov.h>
  11. #include "FRQueryEx.h"
  12. #include "globals.h"
  13. #include "Factory.h"
  14. #include "helpers.h"
  15. #include <map>
  16. #include <vector>
  17. #include "SmartHandle.h"
  18. #include <crtdbg.h>
  19. #include "CVARIANT.h"
  20. #include "CObjProps.h"
  21. #include "CJobObjLimitInfoProps.h"
  22. #include "JobObjLimitInfoProv.h"
  23. /*****************************************************************************/
  24. // QueryInterface override to allow for this component's interface(s)
  25. /*****************************************************************************/
  26. STDMETHODIMP CJobObjLimitInfoProv::QueryInterface(const IID& iid, void** ppv)
  27. {
  28. HRESULT hr = S_OK;
  29. if(iid == IID_IWbemServices)
  30. {
  31. *ppv = static_cast<IWbemServices*>(this);
  32. reinterpret_cast<IUnknown*>(*ppv)->AddRef();
  33. }
  34. else if(iid == IID_IWbemProviderInit)
  35. {
  36. *ppv = static_cast<IWbemProviderInit*>(this);
  37. reinterpret_cast<IUnknown*>(*ppv)->AddRef();
  38. }
  39. else
  40. {
  41. hr = CUnknown::QueryInterface(iid, ppv);
  42. }
  43. return hr;
  44. }
  45. /*****************************************************************************/
  46. // Creation function used by CFactory
  47. /*****************************************************************************/
  48. HRESULT CJobObjLimitInfoProv::CreateInstance(CUnknown** ppNewComponent)
  49. {
  50. HRESULT hr = S_OK;
  51. CUnknown* pUnk = NULL;
  52. pUnk = new CJobObjLimitInfoProv;
  53. if(pUnk != NULL)
  54. {
  55. *ppNewComponent = pUnk;
  56. }
  57. else
  58. {
  59. hr = E_OUTOFMEMORY;
  60. }
  61. return hr ;
  62. }
  63. /*****************************************************************************/
  64. // IWbemProviderInit implementation
  65. /*****************************************************************************/
  66. STDMETHODIMP CJobObjLimitInfoProv::Initialize(
  67. LPWSTR pszUser,
  68. LONG lFlags,
  69. LPWSTR pszNamespace,
  70. LPWSTR pszLocale,
  71. IWbemServices *pNamespace,
  72. IWbemContext *pCtx,
  73. IWbemProviderInitSink *pInitSink)
  74. {
  75. m_pNamespace = pNamespace;
  76. m_chstrNamespace = pszNamespace;
  77. //Let CIMOM know you are initialized
  78. //==================================
  79. return pInitSink->SetStatus(
  80. WBEM_S_INITIALIZED,
  81. 0);
  82. }
  83. /*****************************************************************************/
  84. // IWbemServices implementation
  85. /*****************************************************************************/
  86. STDMETHODIMP CJobObjLimitInfoProv::GetObjectAsync(
  87. const BSTR ObjectPath,
  88. long lFlags,
  89. IWbemContext __RPC_FAR *pCtx,
  90. IWbemObjectSink __RPC_FAR *pResponseHandler)
  91. {
  92. HRESULT hr = WBEM_E_NOT_FOUND;
  93. IWbemClassObjectPtr pStatusObject;
  94. try
  95. {
  96. HRESULT hrImp = CheckImpersonationLevel();
  97. if(SUCCEEDED(hrImp))
  98. {
  99. // We need the name of the instance they requested...
  100. WCHAR wstrObjInstKeyVal[MAX_PATH];
  101. hr = GetObjInstKeyVal(
  102. ObjectPath,
  103. IDS_Win32_NamedJobObjectLimitSetting,
  104. g_rgJobObjLimitInfoPropNames[JOLMTPROP_ID],
  105. wstrObjInstKeyVal,
  106. sizeof(wstrObjInstKeyVal) - sizeof(WCHAR));
  107. if(SUCCEEDED(hr))
  108. {
  109. // wstrObjInstKeyVal now contains the name of the object. See if
  110. // it exists...
  111. CHString chstrUndecoratedJOName;
  112. UndecorateJOName(
  113. wstrObjInstKeyVal,
  114. chstrUndecoratedJOName);
  115. SmartHandle hJob;
  116. hJob = ::OpenJobObjectW(
  117. MAXIMUM_ALLOWED,
  118. FALSE,
  119. chstrUndecoratedJOName);
  120. if(hJob)
  121. {
  122. // We seem to have found one matching the specified name,
  123. // so create a return instance...
  124. IWbemClassObjectPtr pIWCO = NULL;
  125. CJobObjLimitInfoProps cjolip(hJob, m_chstrNamespace);
  126. hr = CreateInst(
  127. m_pNamespace,
  128. &pIWCO,
  129. _bstr_t(IDS_Win32_NamedJobObjectLimitSetting),
  130. pCtx);
  131. if(SUCCEEDED(hr))
  132. {
  133. cjolip.SetReqProps(PROP_ALL_REQUIRED);;
  134. }
  135. if(SUCCEEDED(hr))
  136. {
  137. // set the key properties...
  138. hr = cjolip.SetKeysFromPath(
  139. ObjectPath,
  140. pCtx);
  141. }
  142. if(SUCCEEDED(hr))
  143. {
  144. // set the non-key requested properties...
  145. hr = cjolip.SetNonKeyReqProps();
  146. }
  147. if(SUCCEEDED(hr))
  148. {
  149. // Load requested non-key properties
  150. // to the instance...
  151. hr = cjolip.LoadPropertyValues(
  152. pIWCO);
  153. // Commit the instance...
  154. if(SUCCEEDED(hr))
  155. {
  156. IWbemClassObject *pTmp = (IWbemClassObject*) pIWCO;
  157. hr = pResponseHandler->Indicate(
  158. 1,
  159. &pTmp);
  160. }
  161. }
  162. }
  163. else
  164. {
  165. _ASSERT(0);
  166. hr = WBEM_E_NOT_FOUND;
  167. SetStatusObject(
  168. pCtx,
  169. m_pNamespace,
  170. ::GetLastError(),
  171. NULL,
  172. L"::OpenJobObject",
  173. JOB_OBJECT_NAMESPACE,
  174. &pStatusObject);
  175. }
  176. }
  177. }
  178. else
  179. {
  180. hr = hrImp;
  181. }
  182. }
  183. catch(CVARIANTError& cve)
  184. {
  185. hr = cve.GetWBEMError();
  186. }
  187. catch(...)
  188. {
  189. hr = WBEM_E_PROVIDER_FAILURE;
  190. }
  191. // Set Status
  192. return pResponseHandler->SetStatus(0, hr, NULL, pStatusObject);
  193. }
  194. STDMETHODIMP CJobObjLimitInfoProv::ExecQueryAsync(
  195. const BSTR QueryLanguage,
  196. const BSTR Query,
  197. long lFlags,
  198. IWbemContext __RPC_FAR *pCtx,
  199. IWbemObjectSink __RPC_FAR *pResponseHandler)
  200. {
  201. HRESULT hr = WBEM_S_NO_ERROR;
  202. IWbemClassObjectPtr pStatusObject;
  203. try
  204. {
  205. HRESULT hrImp = CheckImpersonationLevel();
  206. if(SUCCEEDED(hrImp))
  207. {
  208. // We will optimize for those cases in which
  209. // a particular set of named job objects
  210. // (e.g., 1 or more). Enumerate also
  211. // optimizes for the properties that were
  212. // requested.
  213. CFrameworkQuery cfwq;
  214. hr = cfwq.Init(
  215. QueryLanguage,
  216. Query,
  217. lFlags,
  218. m_chstrNamespace);
  219. std::vector<_bstr_t> rgNamedJOs;
  220. if(SUCCEEDED(hr))
  221. {
  222. hr = cfwq.GetValuesForProp(
  223. _bstr_t(g_rgJobObjLimitInfoPropNames[JOLMTPROP_ID]),
  224. rgNamedJOs);
  225. }
  226. // If none were specifically requested, they
  227. // want them all...
  228. if(rgNamedJOs.size() == 0)
  229. {
  230. hr = GetJobObjectList(rgNamedJOs);
  231. }
  232. else
  233. {
  234. // Object paths were specified. Before
  235. // passing them along, we need to un-
  236. // decorate them.
  237. UndecorateNamesInNamedJONameList(rgNamedJOs);
  238. }
  239. // Find out what propeties were requested...
  240. CJobObjLimitInfoProps cjolip(m_chstrNamespace);
  241. cjolip.GetWhichPropsReq(cfwq);
  242. if(SUCCEEDED(hr))
  243. {
  244. hr = Enumerate(
  245. pCtx,
  246. pResponseHandler,
  247. rgNamedJOs,
  248. cjolip,
  249. &pStatusObject);
  250. }
  251. else
  252. {
  253. SetStatusObject(
  254. pCtx,
  255. m_pNamespace,
  256. -1L,
  257. NULL,
  258. L"helpers.cpp::GetJobObjectList",
  259. JOB_OBJECT_NAMESPACE,
  260. &pStatusObject);
  261. }
  262. }
  263. else
  264. {
  265. hr = hrImp;
  266. }
  267. }
  268. catch(CVARIANTError& cve)
  269. {
  270. hr = cve.GetWBEMError();
  271. }
  272. catch(...)
  273. {
  274. hr = WBEM_E_PROVIDER_FAILURE;
  275. }
  276. // Set Status
  277. return pResponseHandler->SetStatus(0, hr, NULL, pStatusObject);
  278. }
  279. STDMETHODIMP CJobObjLimitInfoProv::CreateInstanceEnumAsync(
  280. const BSTR Class,
  281. long lFlags,
  282. IWbemContext __RPC_FAR *pCtx,
  283. IWbemObjectSink __RPC_FAR *pResponseHandler)
  284. {
  285. HRESULT hr = WBEM_S_NO_ERROR;
  286. IWbemClassObjectPtr pStatusObject;
  287. try
  288. {
  289. HRESULT hrImp = CheckImpersonationLevel();
  290. if(SUCCEEDED(hrImp))
  291. {
  292. if(_wcsicmp(
  293. Class,
  294. IDS_Win32_NamedJobObjectLimitSetting) != NULL)
  295. {
  296. hr = WBEM_E_INVALID_CLASS;
  297. }
  298. // For every job object, return all accounting
  299. // info properties...
  300. if(SUCCEEDED(hr))
  301. {
  302. // Get a list of named jobs...
  303. std::vector<_bstr_t> rgNamedJOs;
  304. hr = GetJobObjectList(rgNamedJOs);
  305. if(SUCCEEDED(hr))
  306. {
  307. CJobObjLimitInfoProps cjolip(m_chstrNamespace);
  308. cjolip.SetReqProps(PROP_ALL_REQUIRED);
  309. hr = Enumerate(
  310. pCtx,
  311. pResponseHandler,
  312. rgNamedJOs,
  313. cjolip,
  314. &pStatusObject);
  315. }
  316. else
  317. {
  318. SetStatusObject(
  319. pCtx,
  320. m_pNamespace,
  321. -1L,
  322. NULL,
  323. L"helpers.cpp::GetJobObjectList",
  324. JOB_OBJECT_NAMESPACE,
  325. &pStatusObject);
  326. }
  327. }
  328. }
  329. else
  330. {
  331. hr = hrImp;
  332. }
  333. }
  334. catch(CVARIANTError& cve)
  335. {
  336. hr = cve.GetWBEMError();
  337. }
  338. catch(...)
  339. {
  340. hr = WBEM_E_PROVIDER_FAILURE;
  341. }
  342. // Set Status
  343. return pResponseHandler->SetStatus(0, hr, NULL, pStatusObject);
  344. }
  345. /*****************************************************************************/
  346. // Private member function implementations
  347. /*****************************************************************************/
  348. HRESULT CJobObjLimitInfoProv::Enumerate(
  349. IWbemContext __RPC_FAR *pCtx,
  350. IWbemObjectSink __RPC_FAR *pResponseHandler,
  351. std::vector<_bstr_t>& rgNamedJOs,
  352. CJobObjLimitInfoProps& cjolip,
  353. IWbemClassObject** ppStatusObject)
  354. {
  355. HRESULT hr = S_OK;
  356. long lNumJobs = rgNamedJOs.size();
  357. try // CVARIANT can throw and I want the error...
  358. {
  359. if(lNumJobs > 0)
  360. {
  361. SmartHandle hJob;
  362. for(long m = 0L; m < lNumJobs && SUCCEEDED(hr); m++)
  363. {
  364. cjolip.ClearProps();
  365. // We have the name of a JO; need to open it up
  366. // and get its properties...
  367. hJob = ::OpenJobObjectW(
  368. MAXIMUM_ALLOWED,
  369. FALSE,
  370. rgNamedJOs[m]);
  371. // (NOTE: hJob smarthandle class automatically
  372. // closes its handle on destruction and on
  373. // reassignment.)
  374. if(hJob)
  375. {
  376. // Set the handle...
  377. cjolip.SetHandle(hJob);
  378. // Set the key properties directly...
  379. CHString chstrDecoratedJOName;
  380. DecorateJOName(
  381. rgNamedJOs[m],
  382. chstrDecoratedJOName);
  383. std::vector<CVARIANT> vecvKeys;
  384. CVARIANT vID(chstrDecoratedJOName);
  385. vecvKeys.push_back(vID);
  386. hr = cjolip.SetKeysDirect(vecvKeys);
  387. if(FAILED(hr))
  388. {
  389. SetStatusObject(
  390. pCtx,
  391. m_pNamespace,
  392. ::GetLastError(),
  393. NULL,
  394. L"CJobObjLimitInfoProps::SetKeysDirect",
  395. JOB_OBJECT_NAMESPACE,
  396. ppStatusObject);
  397. }
  398. if(SUCCEEDED(hr))
  399. {
  400. // set the non-key requested
  401. // properties...
  402. hr = cjolip.SetNonKeyReqProps();
  403. if(FAILED(hr))
  404. {
  405. SetStatusObject(
  406. pCtx,
  407. m_pNamespace,
  408. ::GetLastError(),
  409. NULL,
  410. L"CJobObjLimitInfoProps::SetNonKeyReqProps",
  411. JOB_OBJECT_NAMESPACE,
  412. ppStatusObject);
  413. }
  414. }
  415. // Create a new outgoing instance...
  416. IWbemClassObjectPtr pIWCO = NULL;
  417. if(SUCCEEDED(hr))
  418. {
  419. hr = CreateInst(
  420. m_pNamespace,
  421. &pIWCO,
  422. _bstr_t(IDS_Win32_NamedJobObjectLimitSetting),
  423. pCtx);
  424. if(FAILED(hr))
  425. {
  426. SetStatusObject(
  427. pCtx,
  428. m_pNamespace,
  429. ::GetLastError(),
  430. NULL,
  431. L"CJobObjLimitInfoProv::CreateInst",
  432. JOB_OBJECT_NAMESPACE,
  433. ppStatusObject);
  434. }
  435. }
  436. // Load the properties of the
  437. // new outgoing instance...
  438. if(SUCCEEDED(hr))
  439. {
  440. hr = cjolip.LoadPropertyValues(pIWCO);
  441. if(FAILED(hr))
  442. {
  443. SetStatusObject(
  444. pCtx,
  445. m_pNamespace,
  446. ::GetLastError(),
  447. NULL,
  448. L"CJobObjLimitInfoProps::LoadPropertyValues",
  449. JOB_OBJECT_NAMESPACE,
  450. ppStatusObject);
  451. }
  452. }
  453. // And send it out...
  454. if(SUCCEEDED(hr))
  455. {
  456. IWbemClassObject *pTmp = (IWbemClassObject*) pIWCO;
  457. hr = pResponseHandler->Indicate(
  458. 1,
  459. &pTmp);
  460. }
  461. }
  462. else
  463. {
  464. _ASSERT(0);
  465. hr = WBEM_E_NOT_FOUND;
  466. SetStatusObject(
  467. pCtx,
  468. m_pNamespace,
  469. ::GetLastError(),
  470. NULL,
  471. L"::OpenJobObject",
  472. JOB_OBJECT_NAMESPACE,
  473. ppStatusObject);
  474. }
  475. }
  476. }
  477. }
  478. catch(CVARIANTError& cve)
  479. {
  480. hr = cve.GetWBEMError();
  481. }
  482. return hr;
  483. }