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.

573 lines
18 KiB

  1. // Copyright (c) 2000-2001 Microsoft Corporation, All Rights Reserved
  2. // CJobObjLimitInfoProps.cpp
  3. //#define _WIN32_WINNT 0x0500
  4. #include "precomp.h"
  5. #include <wbemprov.h>
  6. #include "FRQueryEx.h"
  7. #include <vector>
  8. #include "helpers.h"
  9. #include "CVARIANT.h"
  10. #include "CObjProps.h"
  11. #include "CJobObjLimitInfoProps.h"
  12. #include <crtdbg.h>
  13. //*****************************************************************************
  14. // BEGIN: Declaration of Win32_JobObjectLimitInfo class properties.
  15. //*****************************************************************************
  16. // WARNING!! MUST KEEP MEMBERS OF THE FOLLOWING ARRAY
  17. // IN SYNCH WITH THE JOB_OBJ_PROPS ENUMERATION DECLARED
  18. // IN CJobObjProps.h !!!
  19. LPCWSTR g_rgJobObjLimitInfoPropNames[] =
  20. {
  21. { L"SettingID" },
  22. { L"PerProcessUserTimeLimit" },
  23. { L"PerJobUserTimeLimit" },
  24. { L"LimitFlags" },
  25. { L"MinimumWorkingSetSize" },
  26. { L"MaximumWorkingSetSize" },
  27. { L"ActiveProcessLimit" },
  28. { L"Affinity" },
  29. { L"PriorityClass" },
  30. { L"SchedulingClass" },
  31. { L"ProcessMemoryLimit" },
  32. { L"JobMemoryLimit" }
  33. };
  34. //*****************************************************************************
  35. // END: Declaration of Win32_JobObjectLimitInfo class properties.
  36. //*****************************************************************************
  37. CJobObjLimitInfoProps::CJobObjLimitInfoProps(CHString& chstrNamespace)
  38. : CObjProps(chstrNamespace)
  39. {
  40. }
  41. CJobObjLimitInfoProps::CJobObjLimitInfoProps(
  42. HANDLE hJob,
  43. CHString& chstrNamespace)
  44. : CObjProps(chstrNamespace),
  45. m_hJob(hJob)
  46. {
  47. }
  48. CJobObjLimitInfoProps::~CJobObjLimitInfoProps()
  49. {
  50. }
  51. // Clients call this to establish which properties
  52. // were requested. This function calls a base class
  53. // helper, which calls our CheckProps function.
  54. // The base class helper finally stores the result
  55. // in the base class member m_dwReqProps.
  56. HRESULT CJobObjLimitInfoProps::GetWhichPropsReq(
  57. CFrameworkQuery& cfwq)
  58. {
  59. HRESULT hr = S_OK;
  60. // Call base class version for help.
  61. // Base class version will call our
  62. // CheckProps function.
  63. hr = CObjProps::GetWhichPropsReq(
  64. cfwq,
  65. CheckProps);
  66. return hr;
  67. }
  68. DWORD CJobObjLimitInfoProps::CheckProps(
  69. CFrameworkQuery& Query)
  70. {
  71. DWORD dwReqProps = PROP_NONE_REQUIRED;
  72. // Get the requested properties for this
  73. // specific object...
  74. if (Query.IsPropertyRequired(g_rgJobObjLimitInfoPropNames[JOLMTPROP_ID]))
  75. dwReqProps |= PROP_JOLimitInfoID;
  76. if (Query.IsPropertyRequired(g_rgJobObjLimitInfoPropNames[JOLMTPROP_PerProcessUserTimeLimit]))
  77. dwReqProps |= PROP_PerProcessUserTimeLimit;
  78. if (Query.IsPropertyRequired(g_rgJobObjLimitInfoPropNames[JOLMTPROP_PerJobUserTimeLimit]))
  79. dwReqProps |= PROP_PerJobUserTimeLimit;
  80. if (Query.IsPropertyRequired(g_rgJobObjLimitInfoPropNames[JOLMTPROP_LimitFlags]))
  81. dwReqProps |= PROP_LimitFlags;
  82. if (Query.IsPropertyRequired(g_rgJobObjLimitInfoPropNames[JOLMTPROP_MinimumWorkingSetSize]))
  83. dwReqProps |= PROP_MinimumWorkingSetSize;
  84. if (Query.IsPropertyRequired(g_rgJobObjLimitInfoPropNames[JOLMTPROP_MaximumWorkingSetSize]))
  85. dwReqProps |= PROP_MaximumWorkingSetSize;
  86. if (Query.IsPropertyRequired(g_rgJobObjLimitInfoPropNames[JOLMTPROP_ActiveProcessLimit]))
  87. dwReqProps |= PROP_ActiveProcessLimit;
  88. if (Query.IsPropertyRequired(g_rgJobObjLimitInfoPropNames[JOLMTPROP_Affinity]))
  89. dwReqProps |= PROP_Affinity;
  90. if (Query.IsPropertyRequired(g_rgJobObjLimitInfoPropNames[JOLMTPROP_PriorityClass]))
  91. dwReqProps |= PROP_PriorityClass;
  92. if (Query.IsPropertyRequired(g_rgJobObjLimitInfoPropNames[JOLMTPROP_SchedulingClass]))
  93. dwReqProps |= PROP_SchedulingClass;
  94. if (Query.IsPropertyRequired(g_rgJobObjLimitInfoPropNames[JOLMTPROP_ProcessMemoryLimit]))
  95. dwReqProps |= PROP_ProcessMemoryLimit;
  96. if (Query.IsPropertyRequired(g_rgJobObjLimitInfoPropNames[JOLMTPROP_JobMemoryLimit]))
  97. dwReqProps |= PROP_JobMemoryLimit;
  98. return dwReqProps;
  99. }
  100. void CJobObjLimitInfoProps::SetHandle(
  101. const HANDLE hJob)
  102. {
  103. m_hJob = hJob;
  104. }
  105. HANDLE& CJobObjLimitInfoProps::GetHandle()
  106. {
  107. _ASSERT(m_hJob);
  108. return m_hJob;
  109. }
  110. // Sets the key properties from the ObjectPath.
  111. HRESULT CJobObjLimitInfoProps::SetKeysFromPath(
  112. const BSTR ObjectPath,
  113. IWbemContext __RPC_FAR *pCtx)
  114. {
  115. HRESULT hr = WBEM_S_NO_ERROR;
  116. // This array contains the key field names
  117. CHStringArray rgchstrKeys;
  118. rgchstrKeys.Add(g_rgJobObjLimitInfoPropNames[JOLMTPROP_ID]);
  119. // This array contains the index numbers
  120. // in m_PropMap corresponding to the keys.
  121. short sKeyNum[1];
  122. sKeyNum[0] = JOLMTPROP_ID;
  123. hr = CObjProps::SetKeysFromPath(
  124. ObjectPath,
  125. pCtx,
  126. IDS_Win32_NamedJobObjectLimitSetting,
  127. rgchstrKeys,
  128. sKeyNum);
  129. return hr;
  130. }
  131. // Sets the key property from in supplied
  132. // parameter.
  133. HRESULT CJobObjLimitInfoProps::SetKeysDirect(
  134. std::vector<CVARIANT>& vecvKeys)
  135. {
  136. HRESULT hr = WBEM_S_NO_ERROR;
  137. if(vecvKeys.size() == 1)
  138. {
  139. short sKeyNum[1];
  140. sKeyNum[0] = JOLMTPROP_ID;
  141. hr = CObjProps::SetKeysDirect(
  142. vecvKeys,
  143. sKeyNum);
  144. }
  145. else
  146. {
  147. hr = WBEM_E_INVALID_PARAMETER;
  148. }
  149. return hr;
  150. }
  151. // Sets the non-key properties. Only those
  152. // properties requested are set (as determined
  153. // by base class member m_dwReqProps).
  154. HRESULT CJobObjLimitInfoProps::SetNonKeyReqProps()
  155. {
  156. HRESULT hr = WBEM_S_NO_ERROR;
  157. DWORD dwReqProps = GetReqProps();
  158. _ASSERT(m_hJob);
  159. if(!m_hJob) return WBEM_E_INVALID_PARAMETER;
  160. // Because all the properties of this class
  161. // come from the same underlying win32 job
  162. // object structure, we only need to get that
  163. // structure one time. We only need to get
  164. // it at all if at least one non-key property
  165. // was requested.
  166. if(dwReqProps != PROP_NONE_REQUIRED)
  167. {
  168. // Get the value from the underlying JO:
  169. JOBOBJECT_EXTENDED_LIMIT_INFORMATION joeli;
  170. BOOL fQIJO = ::QueryInformationJobObject(
  171. m_hJob,
  172. JobObjectExtendedLimitInformation,
  173. &joeli,
  174. sizeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION),
  175. NULL);
  176. if(!fQIJO)
  177. {
  178. _ASSERT(0);
  179. hr = WBEM_E_FAILED;
  180. }
  181. else
  182. {
  183. try // CVARIANT can throw
  184. {
  185. // Get all the reequested values...
  186. if(dwReqProps & PROP_PerProcessUserTimeLimit)
  187. {
  188. ULONGLONG llPerProcessUserTimeLimit = (ULONGLONG)joeli.BasicLimitInformation.PerProcessUserTimeLimit.QuadPart;
  189. m_PropMap.insert(SHORT2PVARIANT::value_type(
  190. JOLMTPROP_PerProcessUserTimeLimit,
  191. new CVARIANT(llPerProcessUserTimeLimit)));
  192. }
  193. if(dwReqProps & PROP_PerJobUserTimeLimit)
  194. {
  195. ULONGLONG llPerJobUserTimeLimit = (ULONGLONG)joeli.BasicLimitInformation.PerJobUserTimeLimit.QuadPart;
  196. m_PropMap.insert(SHORT2PVARIANT::value_type(
  197. JOLMTPROP_PerJobUserTimeLimit,
  198. new CVARIANT(llPerJobUserTimeLimit)));
  199. }
  200. if(dwReqProps & PROP_LimitFlags)
  201. {
  202. DWORD dwLimitFlags = joeli.BasicLimitInformation.LimitFlags;
  203. m_PropMap.insert(SHORT2PVARIANT::value_type(
  204. JOLMTPROP_LimitFlags,
  205. new CVARIANT(dwLimitFlags)));
  206. }
  207. if(dwReqProps & PROP_MinimumWorkingSetSize)
  208. {
  209. DWORD dwMinimumWorkingSetSize = joeli.BasicLimitInformation.MinimumWorkingSetSize;
  210. m_PropMap.insert(SHORT2PVARIANT::value_type(
  211. JOLMTPROP_MinimumWorkingSetSize,
  212. new CVARIANT(dwMinimumWorkingSetSize)));
  213. }
  214. if(dwReqProps & PROP_MaximumWorkingSetSize)
  215. {
  216. DWORD dwMaximumWorkingSetSize = joeli.BasicLimitInformation.MaximumWorkingSetSize;
  217. m_PropMap.insert(SHORT2PVARIANT::value_type(
  218. JOLMTPROP_MaximumWorkingSetSize,
  219. new CVARIANT(dwMaximumWorkingSetSize)));
  220. }
  221. if(dwReqProps & PROP_ActiveProcessLimit)
  222. {
  223. DWORD dwActiveProcessLimit = joeli.BasicLimitInformation.ActiveProcessLimit;
  224. m_PropMap.insert(SHORT2PVARIANT::value_type(
  225. JOLMTPROP_ActiveProcessLimit,
  226. new CVARIANT(dwActiveProcessLimit)));
  227. }
  228. if(dwReqProps & PROP_Affinity)
  229. {
  230. DWORD dwAffinity = joeli.BasicLimitInformation.Affinity;
  231. m_PropMap.insert(SHORT2PVARIANT::value_type(
  232. JOLMTPROP_Affinity,
  233. new CVARIANT(dwAffinity)));
  234. }
  235. if(dwReqProps & PROP_PriorityClass)
  236. {
  237. DWORD dwPriorityClass = joeli.BasicLimitInformation.PriorityClass;
  238. m_PropMap.insert(SHORT2PVARIANT::value_type(
  239. JOLMTPROP_PriorityClass,
  240. new CVARIANT(dwPriorityClass)));
  241. }
  242. if(dwReqProps & PROP_SchedulingClass)
  243. {
  244. DWORD dwSchedulingClass = joeli.BasicLimitInformation.SchedulingClass;
  245. m_PropMap.insert(SHORT2PVARIANT::value_type(
  246. JOLMTPROP_SchedulingClass,
  247. new CVARIANT(dwSchedulingClass)));
  248. }
  249. if(dwReqProps & PROP_ProcessMemoryLimit)
  250. {
  251. DWORD dwProcessMemoryLimit = joeli.ProcessMemoryLimit ;
  252. m_PropMap.insert(SHORT2PVARIANT::value_type(
  253. JOLMTPROP_ProcessMemoryLimit,
  254. new CVARIANT(dwProcessMemoryLimit)));
  255. }
  256. if(dwReqProps & PROP_JobMemoryLimit)
  257. {
  258. DWORD dwJobMemoryLimit = joeli.JobMemoryLimit ;
  259. m_PropMap.insert(SHORT2PVARIANT::value_type(
  260. JOLMTPROP_JobMemoryLimit,
  261. new CVARIANT(dwJobMemoryLimit)));
  262. }
  263. }
  264. catch(CVARIANTError& cve)
  265. {
  266. hr = cve.GetWBEMError();
  267. }
  268. }
  269. }
  270. return hr;
  271. }
  272. // Used by PutInstance to write out properties.
  273. HRESULT CJobObjLimitInfoProps::SetWin32JOLimitInfoProps(
  274. IWbemClassObject __RPC_FAR *pInst)
  275. {
  276. HRESULT hr = WBEM_S_NO_ERROR;
  277. _ASSERT(pInst);
  278. if(!pInst) return WBEM_E_INVALID_PARAMETER;
  279. // Go through the instance and extract all
  280. // specified values into the win32 structure.
  281. // If a value was not specified, set it to zero.
  282. CVARIANT v;
  283. JOBOBJECT_EXTENDED_LIMIT_INFORMATION joeli;
  284. ::ZeroMemory(&joeli, sizeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION));
  285. hr = pInst->Get(
  286. g_rgJobObjLimitInfoPropNames[JOLMTPROP_PerProcessUserTimeLimit],
  287. 0,
  288. &v,
  289. NULL,
  290. NULL);
  291. if(SUCCEEDED(hr))
  292. {
  293. (V_VT(&v) == VT_BSTR) ?
  294. joeli.BasicLimitInformation.PerProcessUserTimeLimit.QuadPart = _wtoi64(V_BSTR(&v)) :
  295. joeli.BasicLimitInformation.PerProcessUserTimeLimit.QuadPart = 0;
  296. }
  297. v.Clear();
  298. if(SUCCEEDED(hr))
  299. {
  300. hr = pInst->Get(
  301. g_rgJobObjLimitInfoPropNames[JOLMTPROP_PerJobUserTimeLimit],
  302. 0,
  303. &v,
  304. NULL,
  305. NULL);
  306. if(SUCCEEDED(hr))
  307. {
  308. (V_VT(&v) == VT_BSTR) ?
  309. joeli.BasicLimitInformation.PerJobUserTimeLimit.QuadPart = _wtoi64(V_BSTR(&v)) :
  310. joeli.BasicLimitInformation.PerJobUserTimeLimit.QuadPart = 0;
  311. }
  312. }
  313. v.Clear();
  314. if(SUCCEEDED(hr))
  315. {
  316. hr = pInst->Get(
  317. g_rgJobObjLimitInfoPropNames[JOLMTPROP_LimitFlags],
  318. 0,
  319. &v,
  320. NULL,
  321. NULL);
  322. if(SUCCEEDED(hr))
  323. {
  324. (V_VT(&v) == VT_I4) ?
  325. joeli.BasicLimitInformation.LimitFlags = V_I4(&v) :
  326. joeli.BasicLimitInformation.LimitFlags = 0;
  327. }
  328. }
  329. v.Clear();
  330. if(SUCCEEDED(hr))
  331. {
  332. hr = pInst->Get(
  333. g_rgJobObjLimitInfoPropNames[JOLMTPROP_MinimumWorkingSetSize],
  334. 0,
  335. &v,
  336. NULL,
  337. NULL);
  338. if(SUCCEEDED(hr))
  339. {
  340. (V_VT(&v) == VT_I4) ?
  341. joeli.BasicLimitInformation.MinimumWorkingSetSize = V_I4(&v) :
  342. joeli.BasicLimitInformation.MinimumWorkingSetSize = 0;
  343. }
  344. }
  345. v.Clear();
  346. if(SUCCEEDED(hr))
  347. {
  348. hr = pInst->Get(
  349. g_rgJobObjLimitInfoPropNames[JOLMTPROP_MaximumWorkingSetSize],
  350. 0,
  351. &v,
  352. NULL,
  353. NULL);
  354. if(SUCCEEDED(hr))
  355. {
  356. (V_VT(&v) == VT_I4) ?
  357. joeli.BasicLimitInformation.MaximumWorkingSetSize = V_I4(&v) :
  358. joeli.BasicLimitInformation.MaximumWorkingSetSize = 0;
  359. }
  360. }
  361. v.Clear();
  362. if(SUCCEEDED(hr))
  363. {
  364. hr = pInst->Get(
  365. g_rgJobObjLimitInfoPropNames[JOLMTPROP_ActiveProcessLimit],
  366. 0,
  367. &v,
  368. NULL,
  369. NULL);
  370. if(SUCCEEDED(hr))
  371. {
  372. (V_VT(&v) == VT_I4) ?
  373. joeli.BasicLimitInformation.ActiveProcessLimit = V_I4(&v) :
  374. joeli.BasicLimitInformation.ActiveProcessLimit = 0;
  375. }
  376. }
  377. v.Clear();
  378. if(SUCCEEDED(hr))
  379. {
  380. hr = pInst->Get(
  381. g_rgJobObjLimitInfoPropNames[JOLMTPROP_Affinity],
  382. 0,
  383. &v,
  384. NULL,
  385. NULL);
  386. if(SUCCEEDED(hr))
  387. {
  388. (V_VT(&v) == VT_I4) ?
  389. joeli.BasicLimitInformation.Affinity = V_I4(&v) :
  390. joeli.BasicLimitInformation.Affinity = 0;
  391. }
  392. }
  393. v.Clear();
  394. if(SUCCEEDED(hr))
  395. {
  396. hr = pInst->Get(
  397. g_rgJobObjLimitInfoPropNames[JOLMTPROP_PriorityClass],
  398. 0,
  399. &v,
  400. NULL,
  401. NULL);
  402. if(SUCCEEDED(hr))
  403. {
  404. (V_VT(&v) == VT_I4) ?
  405. joeli.BasicLimitInformation.PriorityClass = V_I4(&v) :
  406. joeli.BasicLimitInformation.PriorityClass = 0;
  407. }
  408. }
  409. v.Clear();
  410. if(SUCCEEDED(hr))
  411. {
  412. hr = pInst->Get(
  413. g_rgJobObjLimitInfoPropNames[JOLMTPROP_SchedulingClass],
  414. 0,
  415. &v,
  416. NULL,
  417. NULL);
  418. if(SUCCEEDED(hr))
  419. {
  420. (V_VT(&v) == VT_I4) ?
  421. joeli.BasicLimitInformation.SchedulingClass = V_I4(&v) :
  422. joeli.BasicLimitInformation.SchedulingClass = 0;
  423. }
  424. }
  425. v.Clear();
  426. if(SUCCEEDED(hr))
  427. {
  428. hr = pInst->Get(
  429. g_rgJobObjLimitInfoPropNames[JOLMTPROP_ProcessMemoryLimit],
  430. 0,
  431. &v,
  432. NULL,
  433. NULL);
  434. if(SUCCEEDED(hr))
  435. {
  436. (V_VT(&v) == VT_I4) ?
  437. joeli.ProcessMemoryLimit = V_I4(&v) :
  438. joeli.ProcessMemoryLimit = 0;
  439. }
  440. }
  441. v.Clear();
  442. if(SUCCEEDED(hr))
  443. {
  444. hr = pInst->Get(
  445. g_rgJobObjLimitInfoPropNames[JOLMTPROP_JobMemoryLimit],
  446. 0,
  447. &v,
  448. NULL,
  449. NULL);
  450. if(SUCCEEDED(hr))
  451. {
  452. (V_VT(&v) == VT_I4) ?
  453. joeli.JobMemoryLimit = V_I4(&v) :
  454. joeli.JobMemoryLimit = 0;
  455. }
  456. }
  457. // Now write the info out...
  458. if(SUCCEEDED(hr))
  459. {
  460. if(!::SetInformationJobObject(
  461. m_hJob,
  462. JobObjectExtendedLimitInformation,
  463. &joeli,
  464. sizeof(joeli)))
  465. {
  466. hr = WinErrorToWBEMhResult(::GetLastError());
  467. }
  468. }
  469. return hr;
  470. }
  471. HRESULT CJobObjLimitInfoProps::LoadPropertyValues(
  472. IWbemClassObject* pIWCO)
  473. {
  474. HRESULT hr = WBEM_S_NO_ERROR;
  475. if(!pIWCO) return E_POINTER;
  476. hr = CObjProps::LoadPropertyValues(
  477. g_rgJobObjLimitInfoPropNames,
  478. pIWCO);
  479. return hr;
  480. }