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.

411 lines
12 KiB

  1. /*++
  2. Copyright (c) 1996-1998 Microsoft Corporation
  3. Module Name:
  4. cldskwmi.c
  5. Abstract:
  6. km wmi tracing code.
  7. Will be shared between our drivers.
  8. Authors:
  9. GorN 10-Aug-1999
  10. Environment:
  11. kernel mode only
  12. Notes:
  13. Revision History:
  14. Comments:
  15. This code is a quick hack to enable WMI tracing in cluster drivers.
  16. It should eventually go away.
  17. WmlTinySystemControl will be replaced with WmilibSystemControl from wmilib.sys .
  18. WmlTrace or equivalent will be added to the kernel in addition to IoWMIWriteEvent(&TraceBuffer);
  19. --*/
  20. #include <ntos.h>
  21. #include <ntrtl.h>
  22. #include <nturtl.h>
  23. #include "stdio.h"
  24. #include <wmistr.h>
  25. #include <evntrace.h>
  26. #include "wmlkm.h"
  27. BOOLEAN
  28. WmlpFindGuid(
  29. IN PWML_CONTROL_GUID_REG GuidList,
  30. IN ULONG GuidCount,
  31. IN LPGUID Guid,
  32. OUT PULONG GuidIndex
  33. )
  34. /*++
  35. Routine Description:
  36. This routine will search the list of guids registered and return
  37. the index for the one that was registered.
  38. Arguments:
  39. GuidList is the list of guids to search
  40. GuidCount is the count of guids in the list
  41. Guid is the guid being searched for
  42. *GuidIndex returns the index to the guid
  43. Return Value:
  44. TRUE if guid is found else FALSE
  45. --*/
  46. {
  47. ULONG i;
  48. for (i = 0; i < GuidCount; i++)
  49. {
  50. if (IsEqualGUID(Guid, &GuidList[i].Guid))
  51. {
  52. *GuidIndex = i;
  53. return(TRUE);
  54. }
  55. }
  56. return(FALSE);
  57. }
  58. NTSTATUS
  59. WmlTinySystemControl(
  60. IN OUT PWML_TINY_INFO WmiLibInfo,
  61. IN PDEVICE_OBJECT DeviceObject,
  62. IN PIRP Irp
  63. )
  64. /*++
  65. Routine Description:
  66. Dispatch routine for IRP_MJ_SYSTEM_CONTROL. This routine will process
  67. all wmi requests received, forwarding them if they are not for this
  68. driver or determining if the guid is valid and if so passing it to
  69. the driver specific function for handing wmi requests.
  70. Arguments:
  71. WmiLibInfo has the WMI information control block
  72. DeviceObject - Supplies a pointer to the device object for this request.
  73. Irp - Supplies the Irp making the request.
  74. Return Value:
  75. status
  76. --*/
  77. {
  78. PIO_STACK_LOCATION irpStack = IoGetCurrentIrpStackLocation(Irp);
  79. ULONG bufferSize;
  80. PUCHAR buffer;
  81. NTSTATUS status;
  82. ULONG retSize;
  83. UCHAR minorFunction;
  84. ULONG guidIndex;
  85. ULONG instanceCount;
  86. ULONG instanceIndex;
  87. //
  88. // If the irp is not a WMI irp or it is not targetted at this device
  89. // or this device has not regstered with WMI then just forward it on.
  90. minorFunction = irpStack->MinorFunction;
  91. if ((minorFunction > IRP_MN_EXECUTE_METHOD) ||
  92. (irpStack->Parameters.WMI.ProviderId != (ULONG_PTR)DeviceObject) ||
  93. ((minorFunction != IRP_MN_REGINFO) &&
  94. (WmiLibInfo->GuidCount == 0) || (WmiLibInfo->ControlGuids == NULL) ))
  95. {
  96. //
  97. // IRP is not for us so forward if there is a lower device object
  98. if (WmiLibInfo->LowerDeviceObject != NULL)
  99. {
  100. IoSkipCurrentIrpStackLocation(Irp);
  101. return(IoCallDriver(WmiLibInfo->LowerDeviceObject, Irp));
  102. } else {
  103. status = STATUS_INVALID_DEVICE_REQUEST;
  104. Irp->IoStatus.Status = status;
  105. IoCompleteRequest(Irp, IO_NO_INCREMENT);
  106. return(status);
  107. }
  108. }
  109. buffer = (PUCHAR)irpStack->Parameters.WMI.Buffer;
  110. bufferSize = irpStack->Parameters.WMI.BufferSize;
  111. if (minorFunction != IRP_MN_REGINFO)
  112. {
  113. //
  114. // For all requests other than query registration info we are passed
  115. // a guid. Determine if the guid is one that is supported by the
  116. // device.
  117. if (WmlpFindGuid(WmiLibInfo->ControlGuids,
  118. WmiLibInfo->GuidCount,
  119. (LPGUID)irpStack->Parameters.WMI.DataPath,
  120. &guidIndex) )
  121. {
  122. status = STATUS_SUCCESS;
  123. } else {
  124. status = STATUS_WMI_GUID_NOT_FOUND;
  125. }
  126. if (!NT_SUCCESS(status))
  127. {
  128. Irp->IoStatus.Status = status;
  129. IoCompleteRequest(Irp, IO_NO_INCREMENT);
  130. return(status);
  131. }
  132. }
  133. switch(minorFunction)
  134. {
  135. case IRP_MN_REGINFO:
  136. {
  137. ULONG guidCount;
  138. PWML_CONTROL_GUID_REG guidList;
  139. PWMIREGINFOW wmiRegInfo;
  140. PWMIREGGUIDW wmiRegGuid;
  141. PDEVICE_OBJECT pdo;
  142. PUNICODE_STRING regPath;
  143. PWCHAR stringPtr;
  144. ULONG registryPathOffset;
  145. ULONG bufferNeeded;
  146. ULONG i;
  147. UNICODE_STRING nullRegistryPath;
  148. regPath = WmiLibInfo->DriverRegPath;
  149. guidList = WmiLibInfo->ControlGuids;
  150. guidCount = WmiLibInfo->GuidCount;
  151. if (regPath == NULL)
  152. {
  153. // No registry path specified. This is a bad thing for
  154. // the device to do, but is not fatal
  155. nullRegistryPath.Buffer = NULL;
  156. nullRegistryPath.Length = 0;
  157. nullRegistryPath.MaximumLength = 0;
  158. regPath = &nullRegistryPath;
  159. }
  160. registryPathOffset = FIELD_OFFSET(WMIREGINFOW, WmiRegGuid) +
  161. guidCount * sizeof(WMIREGGUIDW);
  162. bufferNeeded = registryPathOffset +
  163. regPath->Length + sizeof(USHORT);
  164. if (bufferNeeded <= bufferSize)
  165. {
  166. retSize = bufferNeeded;
  167. RtlZeroMemory(buffer, bufferNeeded);
  168. wmiRegInfo = (PWMIREGINFO)buffer;
  169. wmiRegInfo->BufferSize = bufferNeeded;
  170. // wmiRegInfo->NextWmiRegInfo = 0;
  171. // wmiRegInfo->MofResourceName = 0;
  172. wmiRegInfo->RegistryPath = registryPathOffset;
  173. wmiRegInfo->GuidCount = guidCount;
  174. for (i = 0; i < guidCount; i++)
  175. {
  176. wmiRegGuid = &wmiRegInfo->WmiRegGuid[i];
  177. wmiRegGuid->Guid = guidList[i].Guid;
  178. wmiRegGuid->Flags = WMIREG_FLAG_TRACED_GUID | WMIREG_FLAG_TRACE_CONTROL_GUID;
  179. // wmiRegGuid->InstanceInfo = 0;
  180. // wmiRegGuid->InstanceCount = 0;
  181. }
  182. stringPtr = (PWCHAR)((PUCHAR)buffer + registryPathOffset);
  183. *stringPtr++ = regPath->Length;
  184. RtlCopyMemory(stringPtr,
  185. regPath->Buffer,
  186. regPath->Length);
  187. status = STATUS_SUCCESS;
  188. } else {
  189. status = STATUS_BUFFER_TOO_SMALL;
  190. *((PULONG)buffer) = bufferNeeded;
  191. retSize = sizeof(ULONG);
  192. }
  193. Irp->IoStatus.Status = status;
  194. Irp->IoStatus.Information = retSize;
  195. IoCompleteRequest(Irp, IO_NO_INCREMENT);
  196. return(status);
  197. }
  198. case IRP_MN_ENABLE_EVENTS:
  199. case IRP_MN_DISABLE_EVENTS:
  200. {
  201. PWNODE_HEADER Wnode = irpStack->Parameters.WMI.Buffer;
  202. PWML_CONTROL_GUID_REG Ctx = WmiLibInfo->ControlGuids + guidIndex;
  203. if (irpStack->Parameters.WMI.BufferSize >= sizeof(WNODE_HEADER)) {
  204. status = STATUS_SUCCESS;
  205. if (minorFunction == IRP_MN_DISABLE_EVENTS) {
  206. Ctx->EnableLevel = 0;
  207. Ctx->EnableFlags = 0;
  208. } else {
  209. Ctx->LoggerHandle = (TRACEHANDLE)( Wnode->HistoricalContext );
  210. Ctx->EnableLevel = WmiGetLoggerEnableLevel(Ctx->LoggerHandle);
  211. Ctx->EnableFlags = WmiGetLoggerEnableFlags(Ctx->LoggerHandle);
  212. }
  213. } else {
  214. status = STATUS_INVALID_PARAMETER;
  215. }
  216. break;
  217. }
  218. case IRP_MN_ENABLE_COLLECTION:
  219. case IRP_MN_DISABLE_COLLECTION:
  220. {
  221. status = STATUS_SUCCESS;
  222. break;
  223. }
  224. case IRP_MN_QUERY_ALL_DATA:
  225. case IRP_MN_QUERY_SINGLE_INSTANCE:
  226. case IRP_MN_CHANGE_SINGLE_INSTANCE:
  227. case IRP_MN_CHANGE_SINGLE_ITEM:
  228. case IRP_MN_EXECUTE_METHOD:
  229. {
  230. status = STATUS_INVALID_DEVICE_REQUEST;
  231. break;
  232. }
  233. default:
  234. {
  235. status = STATUS_INVALID_DEVICE_REQUEST;
  236. break;
  237. }
  238. }
  239. Irp->IoStatus.Status = status;
  240. Irp->IoStatus.Information = 0;
  241. IoCompleteRequest(Irp, IO_NO_INCREMENT);
  242. return(status);
  243. }
  244. #define MAX_SCRATCH_LOG 256
  245. typedef struct _TRACE_BUFFER {
  246. union {
  247. EVENT_TRACE_HEADER Trace;
  248. WNODE_HEADER Wnode;
  249. };
  250. union {
  251. MOF_FIELD MofFields[MAX_MOF_FIELDS + 1];
  252. UCHAR ScratchPad[MAX_SCRATCH_LOG];
  253. };
  254. } TRACE_BUFFER, *PTRACE_BUFFER;
  255. //////////////////////////////////////////////////////////////////////
  256. // 0 | Size | ProviderId | 0 |Size.HT.Mk | Typ.Lev.Version|
  257. // 2 | L o g g e r H a n d l e | 2 | T h r e a d I d |
  258. // 4 | T i m e S t a m p | 4 | T i m e S t a m p |
  259. // 6 | G U I D L o w | 6 | GUID Ptr / Guid L o w |
  260. // 8 | G U I D H I g h | 8 | G U I D H i g h |
  261. // 10 | ClientCtx | Flags | 10 |KernelTime | UserTime |
  262. //////////////////////////////////////////////////////////////////////
  263. ULONG
  264. WmlTrace(
  265. IN ULONG Type,
  266. IN LPCGUID TraceGuid,
  267. IN TRACEHANDLE LoggerHandle,
  268. ... // Pairs: Address, Length
  269. )
  270. {
  271. TRACE_BUFFER TraceBuffer;
  272. TraceBuffer.Trace.Version = Type;
  273. TraceBuffer.Wnode.HistoricalContext = LoggerHandle; // [KM]
  274. TraceBuffer.Trace.Guid = *TraceGuid;
  275. TraceBuffer.Wnode.Flags =
  276. WNODE_FLAG_USE_MOF_PTR | // MOF data are dereferenced
  277. WNODE_FLAG_TRACED_GUID; // Trace Event, not a WMI event
  278. {
  279. PMOF_FIELD ptr = TraceBuffer.MofFields;
  280. va_list ap;
  281. va_start(ap, LoggerHandle);
  282. do {
  283. if ( 0 == (ptr->Length = (ULONG)va_arg (ap, size_t)) ) {
  284. break;
  285. }
  286. ptr->DataPtr = (ULONGLONG)va_arg(ap, PVOID);
  287. } while ( ++ptr < &TraceBuffer.MofFields[MAX_MOF_FIELDS] );
  288. va_end(ap);
  289. TraceBuffer.Wnode.BufferSize = (ULONG) ((ULONG_PTR)ptr - (ULONG_PTR)&TraceBuffer);
  290. }
  291. IoWMIWriteEvent(&TraceBuffer); // [KM]
  292. return STATUS_SUCCESS;
  293. }
  294. ULONG
  295. WmlPrintf(
  296. IN ULONG Type,
  297. IN LPCGUID TraceGuid,
  298. IN TRACEHANDLE LoggerHandle,
  299. IN PCHAR FormatString,
  300. ... // printf var args
  301. )
  302. {
  303. TRACE_BUFFER TraceBuffer;
  304. va_list ArgList;
  305. ULONG Length;
  306. TraceBuffer.Trace.Version = Type;
  307. TraceBuffer.Wnode.HistoricalContext = LoggerHandle; // [KM]
  308. TraceBuffer.Trace.Guid = *TraceGuid;
  309. TraceBuffer.Wnode.Flags =
  310. WNODE_FLAG_TRACED_GUID; // Trace Event, not a WMI event
  311. va_start(ArgList, FormatString);
  312. Length = _vsnprintf(TraceBuffer.ScratchPad, MAX_SCRATCH_LOG, FormatString, ArgList);
  313. TraceBuffer.ScratchPad[Length] = 0;
  314. va_end(ArgList);
  315. TraceBuffer.Wnode.BufferSize =
  316. (ULONG) ((ULONG_PTR)(TraceBuffer.ScratchPad + Length) - (ULONG_PTR)&TraceBuffer);
  317. IoWMIWriteEvent(&TraceBuffer); // [KM]
  318. return STATUS_SUCCESS;
  319. }