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.

332 lines
5.6 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. marcw 2-Sep-1999 Moved over from Win9xUpg project.
  11. --*/
  12. #include "pch.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 = INIT_GROWLIST;
  29. extern INT g_RegRefs;
  30. DWORD
  31. pFindKeyReference (
  32. HKEY Key
  33. )
  34. {
  35. INT i;
  36. DWORD Items;
  37. PKEYTRACK KeyTrack;
  38. Items = GlGetSize (&g_KeyTrackList);
  39. for (i = (INT) (Items - 1) ; i >= 0 ; i--) {
  40. KeyTrack = (PKEYTRACK) GlGetItem (&g_KeyTrackList, (DWORD) i);
  41. if (KeyTrack && KeyTrack->Key == Key) {
  42. return (DWORD) i;
  43. }
  44. }
  45. return NO_MATCH;
  46. }
  47. VOID
  48. AddKeyReferenceA (
  49. HKEY Key,
  50. PCSTR SubKey,
  51. PCSTR File,
  52. DWORD Line
  53. )
  54. {
  55. PKEYTRACK KeyTrack;
  56. DWORD Size;
  57. MYASSERT (g_RegRefs > 0);
  58. Size = sizeof (KEYTRACK) + SizeOfStringA (SubKey);
  59. KeyTrack = (PKEYTRACK) MemAlloc (g_hHeap, 0, Size);
  60. KeyTrack->Key = Key;
  61. KeyTrack->File = File;
  62. KeyTrack->Line = Line;
  63. StringCopyA (KeyTrack->SubKey, SubKey);
  64. (VOID)GlAppend (&g_KeyTrackList, (PBYTE) KeyTrack, Size);
  65. MemFree (g_hHeap, 0, KeyTrack);
  66. }
  67. VOID
  68. AddKeyReferenceW (
  69. HKEY Key,
  70. PCWSTR SubKey,
  71. PCSTR File,
  72. DWORD Line
  73. )
  74. {
  75. PCSTR AnsiSubKey;
  76. MYASSERT (g_RegRefs > 0);
  77. AnsiSubKey = ConvertWtoA (SubKey);
  78. AddKeyReferenceA (Key, AnsiSubKey, File, Line);
  79. FreeConvertedStr (AnsiSubKey);
  80. }
  81. BOOL
  82. pDelKeyReference (
  83. HKEY Key
  84. )
  85. {
  86. DWORD Index;
  87. Index = pFindKeyReference (Key);
  88. if (Index != NO_MATCH) {
  89. GlDeleteItem (&g_KeyTrackList, Index);
  90. return TRUE;
  91. }
  92. return FALSE;
  93. }
  94. VOID
  95. DumpOpenKeys (
  96. VOID
  97. )
  98. {
  99. DWORD d;
  100. DWORD Items;
  101. PKEYTRACK KeyTrack;
  102. RegTerminateCache();
  103. Items = GlGetSize (&g_KeyTrackList);
  104. if (Items) {
  105. DEBUGMSG ((DBG_ERROR, "Unclosed reg keys: %u", Items));
  106. }
  107. for (d = 0 ; d < Items ; d++) {
  108. KeyTrack = (PKEYTRACK) GlGetItem (&g_KeyTrackList, d);
  109. DEBUGMSG ((DBG_REGTRACK, "Open Key: %hs (%hs line %u)", KeyTrack->SubKey, KeyTrack->File, KeyTrack->Line));
  110. }
  111. GlFree (&g_KeyTrackList);
  112. }
  113. VOID
  114. RegTrackTerminate (
  115. VOID
  116. )
  117. {
  118. GlFree (&g_KeyTrackList);
  119. }
  120. VOID
  121. OurRegOpenRootKeyA (
  122. HKEY Key,
  123. PCSTR SubKey,
  124. PCSTR File,
  125. DWORD Line
  126. )
  127. {
  128. AddKeyReferenceA (Key, SubKey, File, Line);
  129. }
  130. VOID
  131. OurRegOpenRootKeyW (
  132. HKEY Key,
  133. PCWSTR SubKey,
  134. PCSTR File,
  135. DWORD Line
  136. )
  137. {
  138. AddKeyReferenceW (Key, SubKey, File, Line);
  139. }
  140. LONG
  141. OurRegOpenKeyExA (
  142. HKEY Key,
  143. PCSTR SubKey,
  144. DWORD Unused,
  145. REGSAM SamMask,
  146. PHKEY ResultPtr,
  147. PCSTR File,
  148. DWORD Line
  149. )
  150. {
  151. LONG rc;
  152. rc = RegOpenKeyExA (Key, SubKey, Unused, SamMask, ResultPtr);
  153. if (rc == ERROR_SUCCESS) {
  154. AddKeyReferenceA (*ResultPtr, SubKey, File, Line);
  155. }
  156. return rc;
  157. }
  158. LONG
  159. OurRegOpenKeyExW (
  160. HKEY Key,
  161. PCWSTR SubKey,
  162. DWORD Unused,
  163. REGSAM SamMask,
  164. PHKEY ResultPtr,
  165. PCSTR File,
  166. DWORD Line
  167. )
  168. {
  169. LONG rc;
  170. rc = RegOpenKeyExW (Key, SubKey, Unused, SamMask, ResultPtr);
  171. if (rc == ERROR_SUCCESS) {
  172. AddKeyReferenceW (*ResultPtr, SubKey, File, Line);
  173. }
  174. return rc;
  175. }
  176. LONG
  177. OurCloseRegKey (
  178. HKEY Key,
  179. PCSTR File,
  180. DWORD Line
  181. )
  182. {
  183. LONG rc;
  184. rc = RealCloseRegKey (Key);
  185. if (rc == ERROR_SUCCESS) {
  186. if (!pDelKeyReference (Key)) {
  187. DEBUGMSG ((
  188. DBG_ERROR,
  189. "Reg key handle closed via CloseRegKey, but not opened "
  190. "with a tracked registry API. %s line %u",
  191. File,
  192. Line
  193. ));
  194. }
  195. }
  196. return rc;
  197. }
  198. LONG
  199. OurRegCreateKeyExA (
  200. HKEY Key,
  201. PCSTR SubKey,
  202. DWORD Reserved,
  203. PSTR Class,
  204. DWORD Options,
  205. REGSAM SamMask,
  206. LPSECURITY_ATTRIBUTES SecurityAttribs,
  207. PHKEY ResultPtr,
  208. PDWORD DispositionPtr,
  209. PCSTR File,
  210. DWORD Line
  211. )
  212. {
  213. LONG rc;
  214. rc = RegCreateKeyExA (
  215. Key,
  216. SubKey,
  217. Reserved,
  218. Class,
  219. Options,
  220. SamMask,
  221. SecurityAttribs,
  222. ResultPtr,
  223. DispositionPtr
  224. );
  225. if (rc == ERROR_SUCCESS) {
  226. AddKeyReferenceA (*ResultPtr, SubKey, File, Line);
  227. }
  228. return rc;
  229. }
  230. LONG
  231. OurRegCreateKeyExW (
  232. HKEY Key,
  233. PCWSTR SubKey,
  234. DWORD Reserved,
  235. PWSTR Class,
  236. DWORD Options,
  237. REGSAM SamMask,
  238. LPSECURITY_ATTRIBUTES SecurityAttribs,
  239. PHKEY ResultPtr,
  240. PDWORD DispositionPtr,
  241. PCSTR File,
  242. DWORD Line
  243. )
  244. {
  245. LONG rc;
  246. rc = RegCreateKeyExW (
  247. Key,
  248. SubKey,
  249. Reserved,
  250. Class,
  251. Options,
  252. SamMask,
  253. SecurityAttribs,
  254. ResultPtr,
  255. DispositionPtr
  256. );
  257. if (rc == ERROR_SUCCESS) {
  258. AddKeyReferenceW (*ResultPtr, SubKey, File, Line);
  259. }
  260. return rc;
  261. }
  262. #endif