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.

566 lines
17 KiB

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