ReactOS 0.4.16-dev-974-g5022a45
fltsupport.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS kernel-mode tests
3 * LICENSE: GPLv2+ - See COPYING in the top level directory
4 * PURPOSE: File system mini-filter support routines
5 * PROGRAMMER: Ged Murphy <gedmurphy@reactos.org>
6 */
7
8#include <kmt_test.h>
9
10#define KMT_FLT_USER_MODE
11#include "kmtest.h"
12#include <kmt_public.h>
13
14#include <ndk/setypes.h>
15#include <assert.h>
16#include <debug.h>
17
18/*
19 * We need to call the internal function in the service.c file
20 */
24 IN PCWSTR ServicePath,
25 IN PCWSTR DisplayName OPTIONAL,
26 IN DWORD ServiceType,
27 OUT SC_HANDLE *ServiceHandle);
28
29// move to a shared location
31{
35
37
38extern HANDLE KmtestHandle;
40
41
42
62 _In_z_ PCWSTR DisplayName,
63 _Out_ SC_HANDLE *ServiceHandle)
64{
65 WCHAR ServicePath[MAX_PATH];
66
67 StringCbCopyW(ServicePath, sizeof(ServicePath), ServiceName);
68 StringCbCatW(ServicePath, sizeof(ServicePath), L"_drv.sys");
69
72
74 ServicePath,
75 DisplayName,
77 ServiceHandle);
78}
79
96 _Inout_ SC_HANDLE *ServiceHandle)
97{
98 return KmtDeleteService(ServiceName, ServiceHandle);
99}
100
108DWORD
110 _In_ BOOLEAN EnableDriverLoadPrivilege,
111 _In_ BOOLEAN RestartIfRunning,
112 _In_ BOOLEAN ConnectComms,
113 _Out_ HANDLE *hPort
114)
115{
116 DWORD Error;
117
118 if (EnableDriverLoadPrivilege)
119 {
120 BOOLEAN WasEnabled;
123 TRUE,
124 FALSE, // Enable in current process.
125 &WasEnabled));
126 if (Error)
127 return Error;
128 }
129
131 if ((Error == ERROR_SERVICE_ALREADY_RUNNING) && RestartIfRunning)
132 {
134 if (Error)
135 {
136 // TODO
137 __debugbreak();
138 }
139
141 }
142
143 if (Error)
144 return Error;
145
146 if (ConnectComms)
147 Error = KmtFltConnectComms(hPort);
148
149 return Error;
150}
151
164DWORD
166 _In_ HANDLE *hPort,
167 _In_ BOOLEAN DisonnectComms)
168{
170
171 if (DisonnectComms)
172 {
173 Error = KmtFltDisconnect(hPort);
174
175 if (Error)
176 {
177 return Error;
178 }
179 }
180
182
183 if (Error)
184 {
185 // TODO
186 __debugbreak();
187 }
188
189 return Error;
190}
191
202DWORD
204 _Out_ HANDLE *hPort)
205{
206 return KmtFltConnect(TestServiceName, hPort);
207}
208
219DWORD
221 _In_ HANDLE hPort)
222{
223 return KmtFltDisconnect(hPort);
224}
225
226
239 _Inout_ SC_HANDLE *ServiceHandle)
240{
241 return KmtCloseService(ServiceHandle);
242}
243
256DWORD
258 _In_ HANDLE hPort,
260{
262}
263
276DWORD
278 _In_ HANDLE hPort,
280{
281 assert(hPort);
282 return KmtFltSendBufferToDriver(hPort, Message, NULL, 0, NULL, 0, NULL);
283}
284
300DWORD
302 _In_ HANDLE hPort,
305{
306 assert(hPort);
307 assert(String);
309}
310
325DWORD
327 _In_ HANDLE hPort,
330{
331 return KmtFltSendBufferToDriver(hPort, Message, (PVOID)String, (DWORD)wcslen(String) * sizeof(WCHAR), NULL, 0, NULL);
332}
333
348DWORD
350 _In_ HANDLE hPort,
353{
354 return KmtFltSendBufferToDriver(hPort, Message, &Value, sizeof(Value), NULL, 0, NULL);
355}
356
379DWORD
381 _In_ HANDLE hPort,
388{
393 DWORD Error;
394
395 assert(hPort);
396
397 if (BufferSize)
398 {
399 assert(InBuffer);
400
403 if (!Ptr)
404 {
406 }
408 }
409 else
410 {
412 Ptr = &Header;
413 }
414
415 Ptr->Message = Message;
416 if (BufferSize)
417 {
418 Ptr->Buffer = (Ptr + 1);
419 StringCbCopy(Ptr->Buffer, BufferSize, InBuffer);
420 Ptr->BufferSize = BufferSize;
421 }
422
424
425 if (FreeMemory)
426 {
428 }
429
430 return Error;
431}
432
443DWORD
446{
447 WCHAR DefaultInstance[128];
448 WCHAR KeyPath[256];
449 HKEY hKey = NULL;
450 HKEY hSubKey = NULL;
451 DWORD Zero = 0;
452 LONG Error;
453
454 StringCbCopyW(KeyPath, sizeof(KeyPath), L"SYSTEM\\CurrentControlSet\\Services\\");
455 StringCbCatW(KeyPath, sizeof(KeyPath), TestServiceName);
456 StringCbCatW(KeyPath, sizeof(KeyPath), L"\\Instances\\");
457
459 KeyPath,
460 0,
461 NULL,
464 NULL,
465 &hKey,
466 NULL);
467 if (Error != ERROR_SUCCESS)
468 {
469 return Error;
470 }
471
472 StringCbCopyW(DefaultInstance, sizeof(DefaultInstance), TestServiceName);
473 StringCbCatW(DefaultInstance, sizeof(DefaultInstance), L" Instance");
474
476 L"DefaultInstance",
477 0,
478 REG_SZ,
479 (LPBYTE)DefaultInstance,
480 (lstrlenW(DefaultInstance) + 1) * sizeof(WCHAR));
481 if (Error != ERROR_SUCCESS)
482 {
483 goto Quit;
484 }
485
486 Error = RegCreateKeyW(hKey, DefaultInstance, &hSubKey);
487 if (Error != ERROR_SUCCESS)
488 {
489 goto Quit;
490 }
491
492 Error = RegSetValueExW(hSubKey,
493 L"Altitude",
494 0,
495 REG_SZ,
497 (lstrlenW(Altitude) + 1) * sizeof(WCHAR));
498 if (Error != ERROR_SUCCESS)
499 {
500 goto Quit;
501 }
502
503 Error = RegSetValueExW(hSubKey,
504 L"Flags",
505 0,
506 REG_DWORD,
507 (LPBYTE)&Zero,
508 sizeof(DWORD));
509
510Quit:
511 if (hSubKey)
512 {
513 RegCloseKey(hSubKey);
514 }
515 if (hKey)
516 {
518 }
519
520 return Error;
521
522}
unsigned char BOOLEAN
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
static VOID FreeMemory(PCREATE_DATA Data)
Definition: create.c:134
static WCHAR ServiceName[]
Definition: browser.c:19
BOOL Error
Definition: chkdsk.c:66
#define RegCloseKey(hKey)
Definition: registry.h:49
Definition: Header.h:9
#define ERROR_NOT_ENOUGH_MEMORY
Definition: dderror.h:7
#define BufferSize
Definition: mmc.h:75
#define ERROR_SUCCESS
Definition: deptool.c:10
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
LONG WINAPI RegSetValueExW(_In_ HKEY hKey, _In_ LPCWSTR lpValueName, _In_ DWORD Reserved, _In_ DWORD dwType, _In_ CONST BYTE *lpData, _In_ DWORD cbData)
Definition: reg.c:4882
LONG WINAPI RegCreateKeyW(HKEY hKey, LPCWSTR lpSubKey, PHKEY phkResult)
Definition: reg.c:1201
#define GetProcessHeap()
Definition: compat.h:736
#define HeapAlloc
Definition: compat.h:733
#define MAX_PATH
Definition: compat.h:34
#define HeapFree(x, y, z)
Definition: compat.h:735
#define HEAP_ZERO_MEMORY
Definition: compat.h:134
#define lstrlenW
Definition: compat.h:750
static const WCHAR Message[]
Definition: register.c:74
#define assert(x)
Definition: debug.h:53
IN PVCB IN VBO IN ULONG OUT PBCB OUT PVOID IN BOOLEAN IN BOOLEAN Zero
Definition: fatprocs.h:419
unsigned long DWORD
Definition: ntddk_ex.h:95
_Must_inspect_result_ _Inout_ PFLT_VOLUME _In_ PCUNICODE_STRING Altitude
Definition: fltkernel.h:1173
struct _KMTFLT_MESSAGE_HEADER KMTFLT_MESSAGE_HEADER
static WCHAR TestServiceName[MAX_PATH]
Definition: fltsupport.c:39
DWORD KmtFltCloseService(_Inout_ SC_HANDLE *ServiceHandle)
Definition: fltsupport.c:238
DWORD KmtFltSendToDriver(_In_ HANDLE hPort, _In_ DWORD Message)
Definition: fltsupport.c:277
DWORD KmtFltCreateService(_In_z_ PCWSTR ServiceName, _In_z_ PCWSTR DisplayName, _Out_ SC_HANDLE *ServiceHandle)
Definition: fltsupport.c:60
DWORD KmtFltRunKernelTest(_In_ HANDLE hPort, _In_z_ PCSTR TestName)
Definition: fltsupport.c:257
DWORD KmtFltSendUlongToDriver(_In_ HANDLE hPort, _In_ DWORD Message, _In_ DWORD Value)
Definition: fltsupport.c:349
struct _KMTFLT_MESSAGE_HEADER * PKMTFLT_MESSAGE_HEADER
DWORD KmtFltDisconnectComms(_In_ HANDLE hPort)
Definition: fltsupport.c:220
DWORD KmtFltSendBufferToDriver(_In_ HANDLE hPort, _In_ DWORD Message, _In_reads_bytes_(BufferSize) LPVOID InBuffer, _In_ DWORD BufferSize, _Out_writes_bytes_to_opt_(OutBufferSize, *BytesReturned) LPVOID OutBuffer, _In_ DWORD OutBufferSize, _Out_opt_ LPDWORD BytesReturned)
Definition: fltsupport.c:380
DWORD KmtFltSendStringToDriver(_In_ HANDLE hPort, _In_ DWORD Message, _In_ PCSTR String)
Definition: fltsupport.c:301
DWORD KmtpCreateService(IN PCWSTR ServiceName, IN PCWSTR ServicePath, IN PCWSTR DisplayName OPTIONAL, IN DWORD ServiceType, OUT SC_HANDLE *ServiceHandle)
Definition: service.c:414
DWORD KmtFltLoadDriver(_In_ BOOLEAN EnableDriverLoadPrivilege, _In_ BOOLEAN RestartIfRunning, _In_ BOOLEAN ConnectComms, _Out_ HANDLE *hPort)
Definition: fltsupport.c:109
DWORD KmtFltAddAltitude(_In_z_ LPWSTR Altitude)
Definition: fltsupport.c:444
DWORD KmtFltUnloadDriver(_In_ HANDLE *hPort, _In_ BOOLEAN DisonnectComms)
Definition: fltsupport.c:165
HANDLE KmtestHandle
Definition: kmtest.c:32
DWORD KmtFltSendWStringToDriver(_In_ HANDLE hPort, _In_ DWORD Message, _In_ PCWSTR String)
Definition: fltsupport.c:326
DWORD KmtFltConnectComms(_Out_ HANDLE *hPort)
Definition: fltsupport.c:203
DWORD KmtFltDeleteService(_In_opt_z_ PCWSTR ServiceName, _Inout_ SC_HANDLE *ServiceHandle)
Definition: fltsupport.c:94
_Must_inspect_result_ _In_ PFSRTL_PER_STREAM_CONTEXT Ptr
Definition: fsrtlfuncs.h:898
FxAutoRegKey hKey
_CRTIMP size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
NTSYSAPI ULONG WINAPI RtlNtStatusToDosError(NTSTATUS)
void __cdecl __debugbreak(void)
Definition: intrin_ppc.h:698
#define KMTFLT_RUN_TEST
Definition: kmt_public.h:30
DWORD KmtCloseService(IN OUT SC_HANDLE *ServiceHandle)
Definition: service.c:392
DWORD KmtDeleteService(IN PCWSTR ServiceName OPTIONAL, IN OUT SC_HANDLE *ServiceHandle)
Definition: service.c:356
#define REG_SZ
Definition: layer.c:22
DWORD KmtFltDisconnect(_In_ HANDLE hPort)
Definition: filter.c:151
DWORD KmtFltLoad(_In_z_ PCWSTR ServiceName)
Definition: filter.c:30
DWORD KmtFltSendMessage(_In_ HANDLE hPort, _In_reads_bytes_(dwInBufferSize) LPVOID InBuffer, _In_ DWORD InBufferSize, _Out_writes_bytes_to_opt_(dutBufferSize, *BytesReturned) LPVOID OutBuffer, _In_ DWORD OutBufferSize, _Out_opt_ LPDWORD BytesReturned)
Definition: filter.c:187
DWORD KmtFltUnload(_In_z_ PCWSTR ServiceName)
Definition: filter.c:336
DWORD KmtFltConnect(_In_z_ PCWSTR ServiceName, _Out_ HANDLE *hPort)
Definition: filter.c:119
WCHAR TestName[MAX_PATH]
Definition: main.cpp:13
#define SE_LOAD_DRIVER_PRIVILEGE
Definition: security.c:664
NTSYSAPI NTSTATUS NTAPI RtlAdjustPrivilege(_In_ ULONG Privilege, _In_ BOOLEAN NewValue, _In_ BOOLEAN ForThread, _Out_ PBOOLEAN OldValue)
#define _In_reads_bytes_(s)
Definition: no_sal2.h:170
#define _Out_opt_
Definition: no_sal2.h:214
#define _Inout_
Definition: no_sal2.h:162
#define _In_z_
Definition: no_sal2.h:164
#define _In_opt_z_
Definition: no_sal2.h:218
#define _Out_
Definition: no_sal2.h:160
#define _In_
Definition: no_sal2.h:158
#define _Out_writes_bytes_to_opt_(s, c)
Definition: no_sal2.h:240
#define KEY_CREATE_SUB_KEY
Definition: nt_native.h:1018
#define REG_OPTION_NON_VOLATILE
Definition: nt_native.h:1057
#define KEY_SET_VALUE
Definition: nt_native.h:1017
#define L(x)
Definition: ntvdm.h:50
long LONG
Definition: pedump.c:60
#define REG_DWORD
Definition: sdbapi.c:596
_In_ UCHAR _In_ ULONG _Out_ PUCHAR _Outptr_result_bytebuffer_ OutBufferLength PVOID * OutBuffer
Definition: scsi.h:4071
PULONG MinorVersion OPTIONAL
Definition: CrossNt.h:68
#define StringCbCopy
Definition: strsafe.h:155
STRSAFEAPI StringCbCopyW(STRSAFE_LPWSTR pszDest, size_t cbDest, STRSAFE_LPCWSTR pszSrc)
Definition: strsafe.h:166
STRSAFEAPI StringCbCatW(STRSAFE_LPWSTR pszDest, size_t cbDest, STRSAFE_LPCWSTR pszSrc)
Definition: strsafe.h:342
const uint16_t * PCWSTR
Definition: typedefs.h:57
unsigned char * LPBYTE
Definition: typedefs.h:53
uint32_t * LPDWORD
Definition: typedefs.h:59
const char * PCSTR
Definition: typedefs.h:52
#define IN
Definition: typedefs.h:39
uint32_t ULONG
Definition: typedefs.h:59
#define OUT
Definition: typedefs.h:40
_Must_inspect_result_ _In_ WDFDEVICE _In_ WDFSTRING String
Definition: wdfdevice.h:2433
_Must_inspect_result_ _In_ WDFIOTARGET _In_opt_ WDFREQUEST _In_ ULONG _In_opt_ PWDF_MEMORY_DESCRIPTOR _In_opt_ PWDF_MEMORY_DESCRIPTOR _In_opt_ PWDF_REQUEST_SEND_OPTIONS _Out_opt_ PULONG_PTR BytesReturned
Definition: wdfiotarget.h:1052
_In_ WDFMEMORY _Out_opt_ size_t * BufferSize
Definition: wdfmemory.h:254
_Must_inspect_result_ _In_ WDFKEY _In_ PCUNICODE_STRING _Out_opt_ PUSHORT _Inout_opt_ PUNICODE_STRING Value
Definition: wdfregistry.h:413
_In_ ULONG OutBufferSize
Definition: wdfwmi.h:87
_In_ ULONG InBufferSize
Definition: wdfwmi.h:106
#define ERROR_SERVICE_ALREADY_RUNNING
Definition: winerror.h:607
#define HKEY_LOCAL_MACHINE
Definition: winreg.h:12
#define RegCreateKeyEx
Definition: winreg.h:501
#define SERVICE_FILE_SYSTEM_DRIVER
Definition: cmtypes.h:954
__wchar_t WCHAR
Definition: xmlstorage.h:180
WCHAR * LPWSTR
Definition: xmlstorage.h:184