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.

326 lines
5.5 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. regtrack.c
  5. Abstract:
  6. Routines to track calls to registry APIs. Used for debugging only.
  7. Author:
  8. Jim Schmidt (jimschm) 02-Sept-1997
  9. Revisions:
  10. --*/
  11. #include "pch.h"
  12. #include "migutilp.h"
  13. #include "regp.h"
  14. #ifdef DEBUG
  15. #undef RegOpenKeyExA
  16. #undef RegCreateKeyExA
  17. #undef RegOpenKeyExW
  18. #undef RegCreateKeyExW
  19. #define DBG_REGTRACK "RegTrack"
  20. #define NO_MATCH 0xffffffff
  21. DWORD g_DontCare;
  22. typedef struct {
  23. PCSTR File;
  24. DWORD Line;
  25. HKEY Key;
  26. CHAR SubKey[];
  27. } KEYTRACK, *PKEYTRACK;
  28. GROWLIST g_KeyTrackList = GROWLIST_INIT;
  29. DWORD
  30. pFindKeyReference (
  31. HKEY Key
  32. )
  33. {
  34. INT i;
  35. DWORD Items;
  36. PKEYTRACK KeyTrack;
  37. Items = GrowListGetSize (&g_KeyTrackList);
  38. for (i = (INT) (Items - 1) ; i >= 0 ; i--) {
  39. KeyTrack = (PKEYTRACK) GrowListGetItem (&g_KeyTrackList, (DWORD) i);
  40. if (KeyTrack && KeyTrack->Key == Key) {
  41. return (DWORD) i;
  42. }
  43. }
  44. return NO_MATCH;
  45. }
  46. VOID
  47. AddKeyReferenceA (
  48. HKEY Key,
  49. PCSTR SubKey,
  50. PCSTR File,
  51. DWORD Line
  52. )
  53. {
  54. PKEYTRACK KeyTrack;
  55. DWORD Size;
  56. Size = sizeof (KEYTRACK) + SizeOfString (SubKey);
  57. KeyTrack = (PKEYTRACK) MemAlloc (g_hHeap, 0, Size);
  58. KeyTrack->Key = Key;
  59. KeyTrack->File = File;
  60. KeyTrack->Line = Line;
  61. //Note that we are copying to memory immediately after the KEYTRACK structure
  62. StringCopy (KeyTrack->SubKey, SubKey);
  63. GrowListAppend (&g_KeyTrackList, (PBYTE) KeyTrack, Size);
  64. MemFree (g_hHeap, 0, KeyTrack);
  65. }
  66. VOID
  67. AddKeyReferenceW (
  68. HKEY Key,
  69. PCWSTR SubKey,
  70. PCSTR File,
  71. DWORD Line
  72. )
  73. {
  74. PCSTR AnsiSubKey;
  75. AnsiSubKey = ConvertWtoA (SubKey);
  76. AddKeyReferenceA (Key, AnsiSubKey, File, Line);
  77. FreeConvertedStr (AnsiSubKey);
  78. }
  79. BOOL
  80. pDelKeyReference (
  81. HKEY Key
  82. )
  83. {
  84. DWORD Index;
  85. Index = pFindKeyReference (Key);
  86. if (Index != NO_MATCH) {
  87. GrowListDeleteItem (&g_KeyTrackList, Index);
  88. return TRUE;
  89. }
  90. return FALSE;
  91. }
  92. VOID
  93. DumpOpenKeys (
  94. VOID
  95. )
  96. {
  97. DWORD d;
  98. DWORD Items;
  99. PKEYTRACK KeyTrack;
  100. Items = GrowListGetSize (&g_KeyTrackList);
  101. if (Items) {
  102. DEBUGMSG ((DBG_ERROR, "Unclosed reg keys: %u", Items));
  103. }
  104. for (d = 0 ; d < Items ; d++) {
  105. KeyTrack = (PKEYTRACK) GrowListGetItem (&g_KeyTrackList, d);
  106. DEBUGMSG ((DBG_REGTRACK, "Open Key: %hs (%hs line %u)", KeyTrack->SubKey, KeyTrack->File, KeyTrack->Line));
  107. }
  108. }
  109. VOID
  110. RegTrackTerminate (
  111. VOID
  112. )
  113. {
  114. FreeGrowList (&g_KeyTrackList);
  115. }
  116. VOID
  117. OurRegOpenRootKeyA (
  118. HKEY Key,
  119. PCSTR SubKey,
  120. PCSTR File,
  121. DWORD Line
  122. )
  123. {
  124. AddKeyReferenceA (Key, SubKey, File, Line);
  125. }
  126. VOID
  127. OurRegOpenRootKeyW (
  128. HKEY Key,
  129. PCWSTR SubKey,
  130. PCSTR File,
  131. DWORD Line
  132. )
  133. {
  134. AddKeyReferenceW (Key, SubKey, File, Line);
  135. }
  136. LONG
  137. OurRegOpenKeyExA (
  138. HKEY Key,
  139. PCSTR SubKey,
  140. DWORD Unused,
  141. REGSAM SamMask,
  142. PHKEY ResultPtr,
  143. PCSTR File,
  144. DWORD Line
  145. )
  146. {
  147. LONG rc;
  148. rc = RegOpenKeyExA (Key, SubKey, Unused, SamMask, ResultPtr);
  149. if (rc == ERROR_SUCCESS) {
  150. AddKeyReferenceA (*ResultPtr, SubKey, File, Line);
  151. }
  152. return rc;
  153. }
  154. LONG
  155. OurRegOpenKeyExW (
  156. HKEY Key,
  157. PCWSTR SubKey,
  158. DWORD Unused,
  159. REGSAM SamMask,
  160. PHKEY ResultPtr,
  161. PCSTR File,
  162. DWORD Line
  163. )
  164. {
  165. LONG rc;
  166. rc = RegOpenKeyExW (Key, SubKey, Unused, SamMask, ResultPtr);
  167. if (rc == ERROR_SUCCESS) {
  168. AddKeyReferenceW (*ResultPtr, SubKey, File, Line);
  169. }
  170. return rc;
  171. }
  172. LONG
  173. OurCloseRegKey (
  174. HKEY Key,
  175. PCSTR File,
  176. DWORD Line
  177. )
  178. {
  179. LONG rc;
  180. rc = RealCloseRegKey (Key);
  181. if (rc == ERROR_SUCCESS) {
  182. if (!pDelKeyReference (Key)) {
  183. DEBUGMSG ((
  184. DBG_ERROR,
  185. "Reg key handle closed via CloseRegKey, but not opened "
  186. "with a tracked registry API. %s line %u",
  187. File,
  188. Line
  189. ));
  190. }
  191. }
  192. return rc;
  193. }
  194. LONG
  195. OurRegCreateKeyExA (
  196. HKEY Key,
  197. PCSTR SubKey,
  198. DWORD Reserved,
  199. PSTR Class,
  200. DWORD Options,
  201. REGSAM SamMask,
  202. LPSECURITY_ATTRIBUTES SecurityAttribs,
  203. PHKEY ResultPtr,
  204. PDWORD DispositionPtr,
  205. PCSTR File,
  206. DWORD Line
  207. )
  208. {
  209. LONG rc;
  210. rc = RegCreateKeyExA (
  211. Key,
  212. SubKey,
  213. Reserved,
  214. Class,
  215. Options,
  216. SamMask,
  217. SecurityAttribs,
  218. ResultPtr,
  219. DispositionPtr
  220. );
  221. if (rc == ERROR_SUCCESS) {
  222. AddKeyReferenceA (*ResultPtr, SubKey, File, Line);
  223. }
  224. return rc;
  225. }
  226. LONG
  227. OurRegCreateKeyExW (
  228. HKEY Key,
  229. PCWSTR SubKey,
  230. DWORD Reserved,
  231. PWSTR Class,
  232. DWORD Options,
  233. REGSAM SamMask,
  234. LPSECURITY_ATTRIBUTES SecurityAttribs,
  235. PHKEY ResultPtr,
  236. PDWORD DispositionPtr,
  237. PCSTR File,
  238. DWORD Line
  239. )
  240. {
  241. LONG rc;
  242. rc = RegCreateKeyExW (
  243. Key,
  244. SubKey,
  245. Reserved,
  246. Class,
  247. Options,
  248. SamMask,
  249. SecurityAttribs,
  250. ResultPtr,
  251. DispositionPtr
  252. );
  253. if (rc == ERROR_SUCCESS) {
  254. AddKeyReferenceW (*ResultPtr, SubKey, File, Line);
  255. }
  256. return rc;
  257. }
  258. #endif