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.

340 lines
7.0 KiB

  1. // StartTraceAPI.cpp : Defines the entry point for the DLL application.
  2. //
  3. //***************************************************************************
  4. //
  5. // judyp May 1999
  6. //
  7. //***************************************************************************
  8. #include "stdafx.h"
  9. #pragma warning (disable : 4786)
  10. #pragma warning (disable : 4275)
  11. #include <iostream>
  12. #include <strstream>
  13. #include <fstream>
  14. #include <string>
  15. #include <sstream>
  16. #include <map>
  17. #include <ctime>
  18. #include <list>
  19. using namespace std;
  20. #include <tchar.h>
  21. #include <windows.h>
  22. #ifdef NONNT5
  23. typedef unsigned long ULONG_PTR;
  24. #endif
  25. #include <wmistr.h>
  26. #include <guiddef.h>
  27. #include <initguid.h>
  28. #include <evntrace.h>
  29. #include <WTYPES.H>
  30. #include "t_string.h"
  31. #include "StructureWrappers.h"
  32. #include "StructureWapperHelpers.h"
  33. #include "ConstantMap.h"
  34. #include "TCOData.h"
  35. #include "Persistor.h"
  36. #include "Logger.h"
  37. #include "Validator.h"
  38. #include "Utilities.h"
  39. #include "CollectionControl.h"
  40. int StartTraceAPI
  41. (
  42. IN LPTSTR lptstrAction, // For logging only.
  43. IN LPCTSTR lpctstrDataFile, // For logging only.
  44. IN LPCTSTR lpctstrTCODetailFile, // If valid we will log to it, can be NULL.
  45. IN bool bLogExpected, // If true we log expected vs actual result.
  46. IN OUT TCOData *pstructTCOData, // TCO test data.
  47. OUT int *pAPIReturn // StartTrace API call return
  48. )
  49. {
  50. LPTSTR lpstrReturnedError = NULL;
  51. *pAPIReturn = -1;
  52. CLogger *pDetailLogger = NULL;
  53. int nResult = 0;
  54. // We only log if the test of "interest" is StartTrace.
  55. if (pstructTCOData->m_ulAPITest == TCOData::StartTraceTest)
  56. {
  57. nResult =
  58. OpenLogFiles
  59. (
  60. lpctstrTCODetailFile,
  61. pDetailLogger,
  62. &lpstrReturnedError
  63. );
  64. }
  65. if (FAILED(nResult))
  66. {
  67. delete pDetailLogger;
  68. // Open log files sets error string plpstrReturnedError.
  69. LogSummaryBeforeCall
  70. (
  71. pstructTCOData,
  72. lpctstrDataFile,
  73. lptstrAction,
  74. _T("StartTrace"),
  75. bLogExpected
  76. );
  77. LogSummaryAfterCall
  78. (
  79. pstructTCOData,
  80. lpctstrDataFile,
  81. lptstrAction,
  82. nResult,
  83. lpstrReturnedError,
  84. bLogExpected
  85. );
  86. free(lpstrReturnedError);
  87. lpstrReturnedError = NULL;
  88. return nResult;
  89. }
  90. // This is our log file, not to be confused with the StartTrace
  91. // log file.
  92. if (pDetailLogger)
  93. {
  94. pDetailLogger->LogTCHAR(_T("\n-------------------------------------------------------\n"));
  95. pDetailLogger->LogTCHAR(_T("StartTraceAPI TCO test "));
  96. pDetailLogger->Flush();
  97. }
  98. if (pDetailLogger)
  99. {
  100. pDetailLogger->LogTCHAR(pstructTCOData->m_lptstrShortDesc);
  101. int n = pDetailLogger->LogTCHAR(_T(" started at time "));
  102. time_t Time;
  103. time(&Time);
  104. pDetailLogger->LogTime(Time);
  105. pDetailLogger->LogTCHAR(_T(".\n"));
  106. pDetailLogger->Flush();
  107. }
  108. if (pstructTCOData->m_ulExpectedResult == ERROR_SUCCESS
  109. && pstructTCOData->m_ulAPITest != TCOData::OtherTest)
  110. {
  111. // We will verify that the LogFileName is valid if we expect the
  112. // call to succeed because it will fail if not a valid file.
  113. if (pstructTCOData->m_pProps &&
  114. pstructTCOData->m_pProps->LogFileName != NULL
  115. && _tcslen(pstructTCOData->m_pProps->LogFileName) > 0)
  116. {
  117. // Verify file.
  118. HANDLE fileHandle =
  119. CreateFile
  120. (
  121. pstructTCOData->m_pProps->LogFileName,
  122. GENERIC_READ | GENERIC_WRITE,
  123. FILE_SHARE_READ,
  124. NULL,
  125. OPEN_ALWAYS,
  126. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING,
  127. NULL
  128. );
  129. if (fileHandle == INVALID_HANDLE_VALUE)
  130. {
  131. DWORD dwError = HRESULT_FROM_WIN32(GetLastError());
  132. LPTSTR lptstrError = DecodeStatus(dwError);
  133. t_string tsError;
  134. tsError = _T("StartTraceAPI error on CreateFile for ");
  135. tsError += pstructTCOData->m_pProps->LogFileName;
  136. tsError += _T(": ");
  137. t_string tsError2;
  138. tsError2 = lptstrError;
  139. tsError += tsError2;
  140. free (lptstrError);
  141. lptstrError = NULL;
  142. if (pDetailLogger)
  143. {
  144. pDetailLogger->LogTCHAR(tsError.c_str());
  145. pDetailLogger->Flush();
  146. }
  147. delete pDetailLogger;
  148. LogSummaryBeforeCall
  149. (
  150. pstructTCOData,
  151. lpctstrDataFile,
  152. lptstrAction,
  153. _T("StartTrace"),
  154. bLogExpected
  155. );
  156. LogSummaryAfterCall
  157. (
  158. pstructTCOData,
  159. lpctstrDataFile,
  160. lptstrAction,
  161. *pAPIReturn,
  162. (TCHAR *) tsError.c_str(),
  163. bLogExpected
  164. );
  165. return dwError;
  166. }
  167. CloseHandle(fileHandle);
  168. // Delete the file so that we have a clean slate.
  169. DeleteFile(pstructTCOData->m_pProps->LogFileName);
  170. }
  171. else
  172. {
  173. // File either null of empty.
  174. t_string tsError;
  175. tsError = _T("StartTraceAPI got null or empty LogFileName ");
  176. tsError += _T("from the TCO data file.\n");
  177. if (pDetailLogger)
  178. {
  179. pDetailLogger->LogTCHAR(tsError.c_str());
  180. pDetailLogger->Flush();
  181. }
  182. delete pDetailLogger;
  183. LogSummaryBeforeCall
  184. (
  185. pstructTCOData,
  186. lpctstrDataFile,
  187. lptstrAction,
  188. _T("StartTrace"),
  189. bLogExpected
  190. );
  191. LogSummaryAfterCall
  192. (
  193. pstructTCOData,
  194. lpctstrDataFile,
  195. lptstrAction,
  196. *pAPIReturn,
  197. (TCHAR *) tsError.c_str(),
  198. bLogExpected
  199. );
  200. return -1;
  201. }
  202. }
  203. BOOL bAdmin = IsAdmin();
  204. if (pDetailLogger)
  205. {
  206. // Log argument values before calling StartTrace.
  207. LogDetailBeforeCall
  208. (
  209. pDetailLogger,
  210. pstructTCOData,
  211. bAdmin
  212. );
  213. }
  214. LogSummaryBeforeCall
  215. (
  216. pstructTCOData,
  217. lpctstrDataFile,
  218. lptstrAction,
  219. _T("StartTrace"),
  220. bLogExpected
  221. );
  222. // Finally, make the dang API call!
  223. *pAPIReturn =
  224. StartTrace
  225. (
  226. pstructTCOData->m_pTraceHandle,
  227. pstructTCOData->m_lptstrInstanceName,
  228. pstructTCOData->m_pProps
  229. );
  230. ULONG ulResult = pstructTCOData->m_ulExpectedResult == *pAPIReturn ? ERROR_SUCCESS : -1;
  231. bool bValid = true;
  232. if (ulResult != ERROR_SUCCESS && *pAPIReturn == ERROR_SUCCESS)
  233. {
  234. ulResult = *pAPIReturn;
  235. }
  236. else if (*pAPIReturn != ERROR_SUCCESS)
  237. {
  238. lpstrReturnedError = DecodeStatus(*pAPIReturn);
  239. }
  240. else if (pstructTCOData->m_ulAPITest == TCOData::StartTraceTest &&
  241. pstructTCOData->m_lptstrValidator &&
  242. _tcslen(pstructTCOData->m_lptstrValidator) > 0)
  243. {
  244. CValidator Validator;
  245. bool bValid =
  246. Validator.Validate
  247. (
  248. pstructTCOData->m_pTraceHandle,
  249. pstructTCOData->m_lptstrInstanceName,
  250. pstructTCOData->m_pProps,
  251. pstructTCOData->m_lptstrValidator
  252. );
  253. if (!bValid)
  254. {
  255. ulResult = -1;
  256. lpstrReturnedError = NewTCHAR(_T("Validation routine failed."));
  257. }
  258. }
  259. if (pDetailLogger)
  260. {
  261. LogDetailAfterCall
  262. ( pDetailLogger,
  263. pstructTCOData,
  264. &pstructTCOData->m_pProps,
  265. *pAPIReturn,
  266. lpstrReturnedError,
  267. bValid,
  268. bAdmin
  269. );
  270. }
  271. LogSummaryAfterCall
  272. (
  273. pstructTCOData,
  274. lpctstrDataFile,
  275. lptstrAction,
  276. *pAPIReturn,
  277. lpstrReturnedError,
  278. bLogExpected
  279. );
  280. free(lpstrReturnedError);
  281. lpstrReturnedError = NULL;
  282. delete pDetailLogger;
  283. return ulResult;
  284. }