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
17 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1993.
  5. //
  6. // File: scmrot.hxx
  7. //
  8. // Contents: Classes used in implementing the ROT in the SCM
  9. //
  10. // Functions:
  11. //
  12. // History: 20-Jan-95 Ricksa Created
  13. //
  14. //--------------------------------------------------------------------------
  15. #ifndef __SCMROT_HXX__
  16. #define __SCMROT_HXX__
  17. InterfaceData *AllocateAndCopy(InterfaceData *pifdIn);
  18. #define SCM_HASH_SIZE 251
  19. #define SCMROT_SIG 0x746f7263
  20. //+-------------------------------------------------------------------------
  21. //
  22. // Class: CScmRotEntry (sre)
  23. //
  24. // Purpose: Entry in the SCM's ROT
  25. //
  26. // Interface: IsEqual - tells whether entry is equal to input key
  27. // GetPRocessID - accessor to process ID for entry
  28. // SetTime - set the time for the entry
  29. // GetTime - get the time for the entry
  30. // GetObject - get the marshaled object for the entry
  31. // GetMoniker - get marshaled moniker for the entry
  32. // IsValid - determines if signitures for entry are valid
  33. // Key - pointer to key for the entry
  34. // cKey - get count of bytes in the key
  35. //
  36. // History: 20-Jan-95 Ricksa Created
  37. //
  38. // Notes:
  39. //
  40. //--------------------------------------------------------------------------
  41. class CScmRotEntry : public CScmHashEntry
  42. {
  43. public:
  44. CScmRotEntry(
  45. DWORD dwScmRotId,
  46. DWORD dwHash,
  47. MNKEQBUF *pmnkeqbuf,
  48. FILETIME *pfiletime,
  49. DWORD _dwProcessID,
  50. CToken *pToken,
  51. WCHAR *pwszWinstaDesktop,
  52. InterfaceData *pifdObject,
  53. InterfaceData *pifdObjectName);
  54. void AddRef();
  55. void Release();
  56. BOOL IsEqual(LPVOID pKey, UINT cbKey);
  57. DWORD GetProcessID(void);
  58. void SetTime(FILETIME *pfiletime);
  59. void GetTime(FILETIME *pfiletime);
  60. InterfaceData * GetObject(void);
  61. InterfaceData * GetMoniker(void);
  62. CToken * Token();
  63. WCHAR * WinstaDesktop();
  64. BOOL IsValid(DWORD dwScmId);
  65. BYTE * Key(void);
  66. DWORD cKey(void);
  67. void SetScmRegKey(SCMREGKEY *psrkOut);
  68. // Real new for this object.
  69. void *operator new(
  70. size_t size,
  71. size_t sizeExtra);
  72. void operator delete(void *pvMem);
  73. void SetProcessNext(CScmRotEntry *pNext);
  74. CScmRotEntry *GetProcessNext();
  75. private:
  76. ~CScmRotEntry(void);
  77. #if DBG == 1
  78. // For debug builds we override this operator to
  79. // prevent its use by causing a run time error.
  80. //
  81. // We also make it private, to hopefully cause a
  82. // compile-time error.
  83. void *operator new(size_t size);
  84. #endif // DBG
  85. // Signiture for object in memory
  86. DWORD _dwSig;
  87. // SCM id for uniqueness
  88. DWORD _dwScmRotId;
  89. // Reference count
  90. LONG _cRefs;
  91. // Hash in table
  92. DWORD _dwHash;
  93. // Process ID for registered object. Used for
  94. // internal support of finding objects that
  95. // a process has registered.
  96. DWORD _dwProcessID;
  97. // Time of last change
  98. FILETIME _filetimeLastChange;
  99. // Object identifier used by client to get
  100. // actual object from the object server
  101. InterfaceData * _pifdObject;
  102. // Comparison buffer.
  103. MNKEQBUF * _pmkeqbufKey;
  104. // Object name (really only used for enumeration).
  105. InterfaceData * _pifdObjectName;
  106. // Token object of the registering server.
  107. CToken * _pToken;
  108. // Window station - desktop of server.
  109. WCHAR * _pwszWinstaDesktop;
  110. // Next ROT entry for the process.
  111. CScmRotEntry * _pProcessNext;
  112. // Where the data for _pifdObject, _pmkeqbufKey,
  113. // _pifdObjectName, and _pwszWinstaDesktop are stored.
  114. BYTE _ab[1];
  115. };
  116. //+-------------------------------------------------------------------------
  117. //
  118. // Member: CScmRotEntry::~CScmRotEntry
  119. //
  120. // Synopsis: Clean up object and invalidate signitures
  121. //
  122. // History: 20-Jan-95 Ricksa Created
  123. //
  124. //--------------------------------------------------------------------------
  125. inline CScmRotEntry::~CScmRotEntry(void)
  126. {
  127. // Invalidate signiture identifiers.
  128. _dwSig = 0;
  129. _dwScmRotId = 0;
  130. if ( _pToken )
  131. _pToken->Release();
  132. // Remember that all the data allocated in pointers was allocated
  133. // when this object was created so the freeing of the object's memory
  134. // will free all the memory for this object.
  135. }
  136. //+-------------------------------------------------------------------------
  137. //
  138. // Member: CScmRotEntry::AddRef
  139. //
  140. // Synopsis: Increment the reference count on the registration.
  141. //
  142. // History: 07-Mar-02 JohnDoty Created
  143. //
  144. //--------------------------------------------------------------------------
  145. inline void CScmRotEntry::AddRef(void)
  146. {
  147. InterlockedIncrement(&_cRefs);
  148. }
  149. //+-------------------------------------------------------------------------
  150. //
  151. // Member: CScmRotEntry::Release
  152. //
  153. // Synopsis: Decrement the reference count on the registration, delete
  154. // if necessary.
  155. //
  156. // History: 07-Mar-02 JohnDoty Created
  157. //
  158. //--------------------------------------------------------------------------
  159. inline void CScmRotEntry::Release(void)
  160. {
  161. LONG cRefs = InterlockedDecrement(&_cRefs);
  162. if (cRefs == 0)
  163. delete this;
  164. }
  165. //+-------------------------------------------------------------------------
  166. //
  167. // Member: CScmRotEntry::GetProcessID
  168. //
  169. // Synopsis: Get the process ID for the registration
  170. //
  171. // History: 20-Jan-95 Ricksa Created
  172. //
  173. //--------------------------------------------------------------------------
  174. inline DWORD CScmRotEntry::GetProcessID(void)
  175. {
  176. return _dwProcessID;
  177. }
  178. //+-------------------------------------------------------------------------
  179. //
  180. // Member: CScmRotEntry::SetTime
  181. //
  182. // Synopsis: Set the time for the entry
  183. //
  184. // History: 20-Jan-95 Ricksa Created
  185. //
  186. //--------------------------------------------------------------------------
  187. inline void CScmRotEntry::SetTime(FILETIME *pfiletime)
  188. {
  189. _filetimeLastChange = *pfiletime;
  190. }
  191. //+-------------------------------------------------------------------------
  192. //
  193. // Member: CScmRotEntry::GetTime
  194. //
  195. // Synopsis: Get the time for an entry
  196. //
  197. // History: 20-Jan-95 Ricksa Created
  198. //
  199. //--------------------------------------------------------------------------
  200. inline void CScmRotEntry::GetTime(FILETIME *pfiletime)
  201. {
  202. *pfiletime = _filetimeLastChange;
  203. }
  204. //+-------------------------------------------------------------------------
  205. //
  206. // Member: CScmRotEntry::GetObject
  207. //
  208. // Synopsis: Return the marshaled interface for the object
  209. //
  210. // History: 20-Jan-95 Ricksa Created
  211. //
  212. //--------------------------------------------------------------------------
  213. inline InterfaceData *CScmRotEntry::GetObject(void)
  214. {
  215. return _pifdObject;
  216. }
  217. //+-------------------------------------------------------------------------
  218. //
  219. // Member: CScmRotEntry::GetMoniker
  220. //
  221. // Synopsis: Return the marshaled moniker
  222. //
  223. // History: 20-Jan-95 Ricksa Created
  224. //
  225. //--------------------------------------------------------------------------
  226. inline InterfaceData *CScmRotEntry::GetMoniker(void)
  227. {
  228. return _pifdObjectName;
  229. }
  230. //+-------------------------------------------------------------------------
  231. //
  232. // Member: CScmRotEntry::Token
  233. //
  234. // Synopsis: Return the CToken.
  235. //
  236. //--------------------------------------------------------------------------
  237. inline CToken * CScmRotEntry::Token()
  238. {
  239. return _pToken;
  240. }
  241. //+-------------------------------------------------------------------------
  242. //
  243. // Member: CScmRotEntry::WinstaDesktop
  244. //
  245. // Synopsis: Return the winsta\desktop string.
  246. //
  247. //--------------------------------------------------------------------------
  248. inline WCHAR * CScmRotEntry::WinstaDesktop()
  249. {
  250. return _pwszWinstaDesktop;
  251. }
  252. //+-------------------------------------------------------------------------
  253. //
  254. // Member: CScmRotEntry::IsValid
  255. //
  256. // Synopsis: Validate signitures for the object
  257. //
  258. // History: 20-Jan-95 Ricksa Created
  259. //
  260. //--------------------------------------------------------------------------
  261. inline BOOL CScmRotEntry::IsValid(DWORD dwScmId)
  262. {
  263. return (SCMROT_SIG == _dwSig) && (_dwScmRotId == dwScmId);
  264. }
  265. //+-------------------------------------------------------------------------
  266. //
  267. // Member: CScmRotEntry::Key
  268. //
  269. // Synopsis: Get the buffer for the marshaled data
  270. //
  271. // History: 20-Jan-95 Ricksa Created
  272. //
  273. //--------------------------------------------------------------------------
  274. inline BYTE *CScmRotEntry::Key(void)
  275. {
  276. return &_pmkeqbufKey->abEqData[0];
  277. }
  278. //+-------------------------------------------------------------------------
  279. //
  280. // Member: CScmRotEntry::cKey
  281. //
  282. // Synopsis: Get count of bytes in the marshaled buffer
  283. //
  284. // History: 20-Jan-95 Ricksa Created
  285. //
  286. //--------------------------------------------------------------------------
  287. inline DWORD CScmRotEntry::cKey(void)
  288. {
  289. return _pmkeqbufKey->cdwSize;
  290. }
  291. //+-------------------------------------------------------------------------
  292. //
  293. // Member: CScmRotEntry::SetScmRegKey
  294. //
  295. // Synopsis: Copy out the registration key
  296. //
  297. // History: 23-Feb-95 Ricksa Created
  298. //
  299. //--------------------------------------------------------------------------
  300. inline void CScmRotEntry::SetScmRegKey(SCMREGKEY *psrkOut)
  301. {
  302. psrkOut->dwEntryLoc = (ULONG64) this;
  303. psrkOut->dwHash = _dwHash;
  304. psrkOut->dwScmId = _dwScmRotId;
  305. }
  306. //+-------------------------------------------------------------------------
  307. //
  308. // Member: CScmRotEntry::SetProcessNext
  309. //
  310. // Synopsis: Link to the next ScmRotEntry for a process.
  311. //
  312. // History: 07-Mar-02 JohnDoty Created
  313. //
  314. //--------------------------------------------------------------------------
  315. inline void CScmRotEntry::SetProcessNext(CScmRotEntry *pNext)
  316. {
  317. _pProcessNext = pNext;
  318. }
  319. //+-------------------------------------------------------------------------
  320. //
  321. // Member: CScmRotEntry::GetProcessNext
  322. //
  323. // Synopsis: Get the link to the next ScmRotEntry for a process.
  324. //
  325. // History: 07-Mar-02 JohnDoty Created
  326. //
  327. //--------------------------------------------------------------------------
  328. inline CScmRotEntry *CScmRotEntry::GetProcessNext()
  329. {
  330. return _pProcessNext;
  331. }
  332. //+-------------------------------------------------------------------------
  333. //
  334. // Member: CScmRotEntry::operator new
  335. //
  336. // Synopsis: Stop this class being allocated by wrong new
  337. //
  338. // History: 20-Jan-95 Ricksa Created
  339. //
  340. //--------------------------------------------------------------------------
  341. #if DBG == 1
  342. inline void *CScmRotEntry::operator new(size_t size)
  343. {
  344. ASSERT(0 && "Called regular new on CScmRotEntry");
  345. return NULL;
  346. }
  347. #endif // DBG
  348. //+-------------------------------------------------------------------------
  349. //
  350. // Member: CScmRotEntry::operator new
  351. //
  352. // Synopsis: Force contiguous allocation of data in entry
  353. //
  354. // History: 20-Jan-95 Ricksa Created
  355. //
  356. //--------------------------------------------------------------------------
  357. inline void *CScmRotEntry::operator new(
  358. size_t size,
  359. size_t cbExtra)
  360. {
  361. return PrivMemAlloc((ULONG)(size + cbExtra));
  362. }
  363. //+-------------------------------------------------------------------------
  364. //
  365. // Member: CScmRotEntry::operator delete
  366. //
  367. // Synopsis: Make sure the proper allocator is always used for this
  368. // class.
  369. //
  370. // History: 04-Oct-2000 JohnDoty Created
  371. //
  372. //--------------------------------------------------------------------------
  373. inline void CScmRotEntry::operator delete(void *pvMem)
  374. {
  375. PrivMemFree(pvMem);
  376. }
  377. //+-------------------------------------------------------------------------
  378. //
  379. // Class: CScmRot (sr)
  380. //
  381. // Purpose: Provide implementation of the ROT in the SCM
  382. //
  383. // Interface: Register - register a new entry in the ROT.
  384. // Revoke - remove an entry from the ROT
  385. // IsRunning - determine if an entry is in the ROT
  386. // NoteChangeTime - set time last changed in the ROT
  387. // GetTimeOfLastChange - get time of change in the ROT
  388. // EnumRunning - get all running monikers
  389. //
  390. // History: 20-Jan-95 Ricksa Created
  391. //
  392. // Notes:
  393. //
  394. //--------------------------------------------------------------------------
  395. class CScmRot : public CPrivAlloc
  396. {
  397. public:
  398. CScmRot(
  399. HRESULT& hr,
  400. WCHAR *pwszNameHintTable);
  401. HRESULT Register(
  402. CProcess *pProcess,
  403. WCHAR *pwszWinstaDesktop,
  404. MNKEQBUF *pmnkeqbuf,
  405. InterfaceData *pifdObject,
  406. InterfaceData *pifdObjectName,
  407. FILETIME *pfiletime,
  408. DWORD dwProcessID,
  409. WCHAR *pwszServerExe,
  410. SCMREGKEY *pdwRegister);
  411. HRESULT Revoke(
  412. CProcess *pProcess,
  413. SCMREGKEY *psrkRegister,
  414. InterfaceData **pifdObject,
  415. InterfaceData **pifdName);
  416. HRESULT IsRunning(
  417. CToken *pToken,
  418. WCHAR *pwszWinstaDesktop,
  419. MNKEQBUF *pmnkeqbuf);
  420. HRESULT GetObject(
  421. CToken *pToken,
  422. WCHAR *pwszWinstaDesktop,
  423. DWORD dwProcessID,
  424. MNKEQBUF *pmnkeqbuf,
  425. SCMREGKEY *psrkRegister,
  426. InterfaceData **ppifdObject);
  427. HRESULT NoteChangeTime(
  428. SCMREGKEY *psrkRegister,
  429. FILETIME *pfiletime);
  430. HRESULT GetTimeOfLastChange(
  431. CToken *pToken,
  432. WCHAR *pwszWinstaDesktop,
  433. MNKEQBUF *pmnkeqbuf,
  434. FILETIME *pfiletime);
  435. HRESULT EnumRunning(
  436. CToken *pToken,
  437. WCHAR *pwszWinstaDesktop,
  438. MkInterfaceList **ppMkIFList);
  439. private:
  440. // This is a friend for Chicago initialization purposes.
  441. friend HRESULT StartSCM(VOID);
  442. CScmRotEntry * GetRotEntry(
  443. CToken *pToken,
  444. WCHAR *pwszWinstaDesktop,
  445. MNKEQBUF *pmnkeqbuf);
  446. CScmRotEntry * GetEntryFromScmReg(
  447. SCMREGKEY *psrk);
  448. // Mutex to protect from multithreaded update
  449. // This mutex must be static so we can initialize
  450. // it per process in Chicago.
  451. CDynamicPortableMutex _mxs;
  452. // ID Counter to make entries unique
  453. DWORD _dwIdCntr;
  454. // Table that tells clients whether they need to
  455. // contact the SCM for accurate information.
  456. CScmRotHintTable _rht;
  457. // Scm Hash Table
  458. CScmHashTable _sht;
  459. };
  460. //+-------------------------------------------------------------------------
  461. //
  462. // Member: CScmRot::CScmRot
  463. //
  464. // Synopsis: Initialize the SCM ROT
  465. //
  466. // History: 20-Jan-95 Ricksa Created
  467. //
  468. //--------------------------------------------------------------------------
  469. inline CScmRot::CScmRot(HRESULT& hr, WCHAR *pwszNameHintTable)
  470. : _dwIdCntr(0),
  471. _rht(pwszNameHintTable),
  472. _sht(SCM_HASH_SIZE)
  473. {
  474. hr = (_sht.InitOK() && _rht.InitOK()) ? S_OK : E_OUTOFMEMORY;
  475. if (hr == S_OK)
  476. {
  477. if (_mxs.FInit() == FALSE)
  478. {
  479. ASSERT(FALSE);
  480. hr = E_OUTOFMEMORY;
  481. }
  482. }
  483. }
  484. #endif __SCMROT_HXX__