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.

497 lines
12 KiB

  1. /*++
  2. Copyright (c) 1998-1999 Microsoft Corporation
  3. Module Name:
  4. logdump.c
  5. Abstract:
  6. this file implements functrionality to read and dump the sr logs
  7. Author:
  8. Kanwaljit Marok (kmarok) 01-May-2000
  9. Revision History:
  10. --*/
  11. #include <nt.h>
  12. #include <ntrtl.h>
  13. #include <nturtl.h>
  14. #include <windows.h>
  15. #include <stdio.h>
  16. #include "logfmt.h"
  17. #include "srapi.h"
  18. struct _EVENT_STR_MAP
  19. {
  20. DWORD EventId;
  21. PCHAR pEventStr;
  22. } EventMap[ 13 ] =
  23. {
  24. {SrEventInvalid , "INVALID " },
  25. {SrEventStreamChange, "FILE-MODIFY" },
  26. {SrEventAclChange, "ACL-CHANGE " },
  27. {SrEventAttribChange, "ATTR-CHANGE" },
  28. {SrEventStreamOverwrite,"FILE-MODIFY" },
  29. {SrEventFileDelete, "FILE-DELETE" },
  30. {SrEventFileCreate, "FILE-CREATE" },
  31. {SrEventFileRename, "FILE-RENAME" },
  32. {SrEventDirectoryCreate,"DIR-CREATE " },
  33. {SrEventDirectoryRename,"DIR-RENAME " },
  34. {SrEventDirectoryDelete,"DIR-DELETE " },
  35. {SrEventMountCreate, "MNT-CREATE " },
  36. {SrEventMountDelete, "MNT-DELETE " }
  37. };
  38. BYTE Buffer[4096];
  39. PCHAR
  40. GetEventString(
  41. DWORD EventId
  42. )
  43. {
  44. PCHAR pStr = NULL;
  45. static CHAR EventStringBuffer[8];
  46. for( int i=0; i<sizeof(EventMap)/sizeof(_EVENT_STR_MAP);i++)
  47. {
  48. if ( EventMap[i].EventId == EventId )
  49. {
  50. pStr = EventMap[i].pEventStr;
  51. }
  52. }
  53. if (pStr == NULL)
  54. {
  55. pStr = &EventStringBuffer[0];
  56. wsprintf(pStr, "0x%X", EventId);
  57. }
  58. return pStr;
  59. }
  60. BOOLEAN
  61. ProcessLogEntry(
  62. BOOLEAN bPrintDebug,
  63. LPCSTR pszSerNo,
  64. LPCSTR pszSize,
  65. LPCSTR pszEndSize,
  66. LPCSTR pszSeqNo,
  67. LPCSTR pszFlags,
  68. LPCSTR pszProcess,
  69. LPCSTR pszOperation,
  70. LPCSTR pszAttr,
  71. LPCSTR pszTmpFile,
  72. LPCSTR pszPath1,
  73. LPCSTR pszPath2,
  74. LPCSTR pszAcl,
  75. LPCSTR pszShortName,
  76. LPCSTR pszProcessHandle,
  77. LPCSTR pszThreadHandle)
  78. {
  79. BOOLEAN Status = TRUE;
  80. if( bPrintDebug == FALSE )
  81. {
  82. printf( "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t",
  83. pszSerNo,
  84. pszSize,
  85. pszSeqNo,
  86. pszOperation,
  87. pszAttr,
  88. pszAcl,
  89. pszPath1,
  90. pszShortName);
  91. }
  92. else
  93. {
  94. printf( "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t",
  95. pszSerNo,
  96. pszSize,
  97. pszSeqNo,
  98. pszOperation,
  99. pszAttr,
  100. pszProcess,
  101. pszProcessHandle,
  102. pszThreadHandle,
  103. pszAcl,
  104. pszPath1,
  105. pszShortName);
  106. }
  107. if(pszTmpFile)
  108. {
  109. printf( "%s\t", pszTmpFile);
  110. }
  111. if(pszPath2)
  112. {
  113. printf( "%s\t", pszPath2);
  114. }
  115. printf("\n");
  116. return Status;
  117. }
  118. #define SR_MAX_PATH ((1000) + sizeof (CHAR)) // Name will always be at most 1000 characters plus a NULL.
  119. BOOLEAN
  120. ReadLogData(
  121. BOOLEAN bPrintDebugInfo,
  122. LPTSTR pszFileName
  123. )
  124. {
  125. BOOLEAN Status = FALSE;
  126. BOOLEAN bHaveDebugInfo = FALSE;
  127. HANDLE hFile;
  128. DWORD nRead;
  129. DWORD cbSize = 0, dwEntries = 0, dwEntriesAdded = 0;
  130. DWORD dwSizeLow , dwSizeHigh;
  131. CHAR szSerNo [10];
  132. CHAR szSize [10];
  133. CHAR szEndSize[10];
  134. CHAR szSeqNo [20];
  135. CHAR szOperation[50];
  136. CHAR szAttr[50];
  137. CHAR szFlags[10];
  138. PCHAR szPath1 = NULL;
  139. PCHAR szPath2 = NULL;
  140. CHAR szTmpFile[MAX_PATH];
  141. CHAR szAcl[MAX_PATH];
  142. CHAR szShortName[MAX_PATH];
  143. CHAR szProcess[32];
  144. CHAR szProcessHandle[16];
  145. CHAR szThreadHandle[16];
  146. BYTE LogHeader[2048];
  147. PSR_LOG_HEADER pLogHeader = (PSR_LOG_HEADER)LogHeader;
  148. static INT s_dwEntries = -1;
  149. szPath1 = (PCHAR) LocalAlloc( LMEM_FIXED, SR_MAX_PATH );
  150. if (szPath1 == NULL )
  151. {
  152. fprintf( stderr, "Insufficient memory\n" );
  153. }
  154. szPath2 = (PCHAR) LocalAlloc( LMEM_FIXED, SR_MAX_PATH );
  155. if (szPath2 == NULL )
  156. {
  157. fprintf( stderr, "Insufficient memory\n" );
  158. }
  159. hFile = CreateFile(
  160. pszFileName,
  161. GENERIC_READ,
  162. FILE_SHARE_WRITE,
  163. NULL,
  164. OPEN_EXISTING,
  165. FILE_ATTRIBUTE_NORMAL,
  166. NULL );
  167. if ( hFile != INVALID_HANDLE_VALUE )
  168. {
  169. PBYTE pLoc = NULL;
  170. dwSizeLow = GetFileSize( hFile, &dwSizeHigh );
  171. //
  172. // Read the header size
  173. //
  174. ReadFile(
  175. hFile,
  176. &cbSize,
  177. sizeof(DWORD),
  178. &nRead,
  179. NULL );
  180. SetFilePointer( hFile, - (INT)sizeof(DWORD), NULL, FILE_CURRENT );
  181. // make sure that cbSize does not exceed the size of the
  182. // buffer we have.
  183. if (cbSize > sizeof(LogHeader))
  184. {
  185. fprintf( stderr, "Invalid header or Corrupt log\n" );
  186. CloseHandle(hFile);
  187. goto End;
  188. }
  189. //
  190. // Read the whole header entry
  191. //
  192. ReadFile(
  193. hFile,
  194. pLogHeader,
  195. cbSize,
  196. &nRead,
  197. NULL );
  198. pLoc = (PBYTE)(&pLogHeader->SubRecords);
  199. fprintf( stderr,
  200. "Header Size: %ld, Version: %ld, Tool Version: %ld\n%S\n",
  201. pLogHeader->Header.RecordSize,
  202. pLogHeader->LogVersion,
  203. SR_LOG_VERSION,
  204. (LPWSTR)(pLoc + sizeof(RECORD_HEADER)) );
  205. if( pLogHeader->LogVersion != SR_LOG_VERSION ||
  206. pLogHeader->MagicNum != SR_LOG_MAGIC_NUMBER )
  207. {
  208. fprintf( stderr, "Invalid version or Corrupt log\n" );
  209. CloseHandle(hFile);
  210. goto End;
  211. }
  212. dwSizeLow -= pLogHeader->Header.RecordSize;
  213. SetFilePointer (hFile,
  214. pLogHeader->Header.RecordSize,
  215. NULL,
  216. FILE_BEGIN);
  217. //
  218. // Start reading the log entries
  219. //
  220. while( dwSizeLow )
  221. {
  222. PSR_LOG_ENTRY pLogEntry = (PSR_LOG_ENTRY)Buffer;
  223. ZeroMemory(pLogEntry, sizeof(Buffer));
  224. //
  225. // Read the size of the entry
  226. //
  227. if ( !ReadFile(
  228. hFile,
  229. &pLogEntry->Header.RecordSize,
  230. sizeof(DWORD),
  231. &nRead,
  232. NULL ) )
  233. {
  234. break;
  235. }
  236. cbSize = pLogEntry->Header.RecordSize;
  237. if (cbSize == 0 )
  238. {
  239. //
  240. // Zero size indicates end of the log
  241. //
  242. break;
  243. }
  244. SetFilePointer( hFile, - (INT)sizeof(DWORD), NULL, FILE_CURRENT );
  245. //
  246. // Read the rest of the entry
  247. //
  248. if ( !ReadFile( hFile,
  249. ((PBYTE)pLogEntry),
  250. cbSize,
  251. &nRead,
  252. NULL ) )
  253. {
  254. break;
  255. }
  256. //
  257. // Check the magic number
  258. //
  259. if( pLogEntry->MagicNum != SR_LOG_MAGIC_NUMBER )
  260. {
  261. fprintf(stderr, "Invalid Entry ( Magic num )\n");
  262. break;
  263. }
  264. //
  265. // Read the entries in to the buffer
  266. //
  267. sprintf( szSerNo , "%05d" , dwEntries + 1);
  268. sprintf( szSize , "%04d" , pLogEntry->Header.RecordSize );
  269. sprintf( szOperation, "%s" , GetEventString(
  270. pLogEntry->EntryType ));
  271. sprintf( szFlags , "%08x" , pLogEntry->EntryFlags );
  272. sprintf( szSeqNo , "%010d" , pLogEntry->SequenceNum);
  273. sprintf( szAttr , "%08x" , pLogEntry->Attributes );
  274. sprintf( szProcess , "%12.12s" , pLogEntry->ProcName );
  275. //
  276. // get the first path
  277. //
  278. PBYTE pLoc = (PBYTE)&pLogEntry->SubRecords;
  279. sprintf( szPath1 , "%S" , pLoc + sizeof(RECORD_HEADER) );
  280. if (pLogEntry->EntryFlags & ENTRYFLAGS_TEMPPATH)
  281. {
  282. pLoc += RECORD_SIZE(pLoc);
  283. sprintf( szTmpFile , "%S" , pLoc + sizeof(RECORD_HEADER) );
  284. }
  285. else
  286. {
  287. sprintf( szTmpFile , "" );
  288. }
  289. if (pLogEntry->EntryFlags & ENTRYFLAGS_SECONDPATH)
  290. {
  291. pLoc += RECORD_SIZE(pLoc);
  292. sprintf( szPath2 , "%S" , pLoc + sizeof(RECORD_HEADER) );
  293. }
  294. else
  295. {
  296. sprintf( szPath2 , "" );
  297. }
  298. if (pLogEntry->EntryFlags & ENTRYFLAGS_ACLINFO)
  299. {
  300. ULONG AclInfoSize;
  301. pLoc += RECORD_SIZE(pLoc);
  302. AclInfoSize = RECORD_SIZE(pLoc);
  303. sprintf( szAcl , "ACL(%04d)%" , AclInfoSize );
  304. }
  305. else
  306. {
  307. sprintf( szAcl , "" );
  308. }
  309. if (pLogEntry->EntryFlags & ENTRYFLAGS_DEBUGINFO)
  310. {
  311. bHaveDebugInfo = TRUE;
  312. pLoc += RECORD_SIZE(pLoc);
  313. sprintf( szProcess , "%12.12s",
  314. ((PSR_LOG_DEBUG_INFO)pLoc)->ProcessName );
  315. sprintf( szProcessHandle,"0x%08X",
  316. ((PSR_LOG_DEBUG_INFO)pLoc)->ProcessId );
  317. sprintf( szThreadHandle,"0x%08X",
  318. ((PSR_LOG_DEBUG_INFO)pLoc)->ThreadId );
  319. }
  320. else
  321. {
  322. bHaveDebugInfo = FALSE;
  323. sprintf( szProcess , "" );
  324. sprintf( szThreadHandle , "" );
  325. sprintf( szProcessHandle , "" );
  326. }
  327. if (pLogEntry->EntryFlags & ENTRYFLAGS_SHORTNAME)
  328. {
  329. pLoc += RECORD_SIZE(pLoc);
  330. sprintf( szShortName , "%S" , pLoc + sizeof(RECORD_HEADER) );
  331. }
  332. else
  333. {
  334. sprintf( szShortName , "" );
  335. }
  336. //
  337. // read the trailing record size
  338. //
  339. sprintf( szEndSize , "%04d", GET_END_SIZE(pLogEntry));
  340. ProcessLogEntry(
  341. bPrintDebugInfo && bHaveDebugInfo,
  342. szSerNo,
  343. szSize,
  344. szEndSize,
  345. szSeqNo,
  346. szFlags,
  347. szProcess,
  348. szOperation,
  349. szAttr,
  350. szTmpFile,
  351. szPath1,
  352. szPath2,
  353. szAcl,
  354. szShortName,
  355. szProcessHandle,
  356. szThreadHandle);
  357. dwEntries++;
  358. dwSizeLow -= cbSize;
  359. cbSize = 0;
  360. }
  361. CloseHandle( hFile );
  362. Status = TRUE;
  363. }
  364. else
  365. {
  366. fprintf( stderr, "Error opening LogFile %s\n", pszFileName );
  367. }
  368. End:
  369. fprintf( stderr, "Number of entries read :%d\n", dwEntries );
  370. if (szPath1 != NULL)
  371. LocalFree( szPath1 );
  372. if (szPath2 != NULL)
  373. LocalFree( szPath2 );
  374. return Status;
  375. }
  376. INT
  377. __cdecl
  378. main(
  379. int argc,
  380. char *argv[] )
  381. {
  382. if( argc < 2 || argc > 3 )
  383. {
  384. fprintf( stderr,
  385. "USAGE: %s [-d] <LogFile> \n\t -d : debug info\n",
  386. argv[0] );
  387. }
  388. else
  389. {
  390. int i = 1;
  391. if ( argc == 3 && !strcmp( argv[i], "-d" ) )
  392. {
  393. i++;
  394. ReadLogData(TRUE, argv[i] );
  395. }
  396. else
  397. {
  398. ReadLogData(FALSE, argv[i] );
  399. }
  400. }
  401. return 0;
  402. }