ReactOS 0.4.15-dev-8390-g075894b
NtApphelpCacheControl.c File Reference
#include "precomp.h"
#include <winsvc.h>
#include <versionhelpers.h>
Include dependency graph for NtApphelpCacheControl.c:

Go to the source code of this file.

Enumerations

enum  ServiceCommands { RegisterShimCacheWithHandle = 128 , RegisterShimCacheWithoutHandle = 129 }
 

Functions

static NTSTATUS (NTAPI *pNtApphelpCacheControl)(APPHELPCACHESERVICECLASS
 
NTSTATUS CallCacheControl (UNICODE_STRING *PathName, BOOLEAN WithMapping, APPHELPCACHESERVICECLASS Service)
 
int InitEnv (UNICODE_STRING *PathName)
 
void CheckValidation (UNICODE_STRING *PathName)
 
static BOOLEAN RequestAddition (SC_HANDLE service_handle, BOOLEAN WithMapping)
 
static void RunApphelpCacheControlTests (SC_HANDLE service_handle)
 
static SERVICE_STATUS_HANDLE (WINAPI *pRegisterServiceCtrlHandlerExA)(LPCSTR
 
static BOOLEAN RegisterInShimCache (BOOLEAN WithMapping)
 
static DWORD WINAPI service_handler (DWORD ctrl, DWORD event_type, void *event_data, void *context)
 
static void WINAPI service_main (DWORD argc, char **argv)
 
static SC_HANDLE InstallService (SC_HANDLE scm_handle)
 
static void WaitService (SC_HANDLE service_handle, DWORD Status, SERVICE_STATUS_PROCESS *ssp)
 
static void RunTest ()
 
 START_TEST (NtApphelpCacheControl)
 

Variables

static PAPPHELP_CACHE_SERVICE_LOOKUP
 
static LPHANDLER_FUNCTION_EX
 
static LPVOID
 
static char service_name [100] = "apphelp_test_service"
 
static HANDLE service_stop_event
 
static SERVICE_STATUS_HANDLE service_status
 

Enumeration Type Documentation

◆ ServiceCommands

Enumerator
RegisterShimCacheWithHandle 
RegisterShimCacheWithoutHandle 

Definition at line 13 of file NtApphelpCacheControl.c.

14{
17};
@ RegisterShimCacheWithHandle
@ RegisterShimCacheWithoutHandle

Function Documentation

◆ CallCacheControl()

NTSTATUS CallCacheControl ( UNICODE_STRING PathName,
BOOLEAN  WithMapping,
APPHELPCACHESERVICECLASS  Service 
)

Definition at line 21 of file NtApphelpCacheControl.c.

22{
23 APPHELP_CACHE_SERVICE_LOOKUP CacheEntry = { {0} };
25 CacheEntry.ImageName = *PathName;
26 if (WithMapping)
27 {
28 OBJECT_ATTRIBUTES LocalObjectAttributes;
30 InitializeObjectAttributes(&LocalObjectAttributes, PathName,
32 Status = NtOpenFile(&CacheEntry.ImageHandle,
34 &LocalObjectAttributes, &IoStatusBlock,
38 }
39 else
40 {
42 }
43 Status = pNtApphelpCacheControl(Service, &CacheEntry);
44 if (CacheEntry.ImageHandle != INVALID_HANDLE_VALUE)
45 NtClose(CacheEntry.ImageHandle);
46 return Status;
47}
#define ok_ntstatus(status, expected)
Definition: atltest.h:135
LONG NTSTATUS
Definition: precomp.h:26
#define FILE_NON_DIRECTORY_FILE
Definition: constants.h:492
#define NULL
Definition: types.h:112
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
#define FILE_SHARE_READ
Definition: compat.h:136
#define FILE_SYNCHRONOUS_IO_NONALERT
Definition: from_kernel.h:31
Status
Definition: gdiplustypes.h:25
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
static OUT PIO_STATUS_BLOCK IoStatusBlock
Definition: pipe.c:75
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
NTSYSAPI NTSTATUS NTAPI NtOpenFile(OUT PHANDLE phFile, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes, OUT PIO_STATUS_BLOCK pIoStatusBlock, IN ULONG ShareMode, IN ULONG OpenMode)
Definition: file.c:3952
#define SYNCHRONIZE
Definition: nt_native.h:61
#define FILE_READ_DATA
Definition: nt_native.h:628
#define FILE_READ_ATTRIBUTES
Definition: nt_native.h:647
#define FILE_SHARE_DELETE
Definition: nt_native.h:682
#define FILE_EXECUTE
Definition: nt_native.h:642
NTSTATUS NTAPI NtClose(IN HANDLE Handle)
Definition: obhandle.c:3402
@ Service
Definition: ntsecapi.h:292
#define STATUS_SUCCESS
Definition: shellext.h:65
UNICODE_STRING ImageName
Definition: pstypes.h:986

Referenced by InitEnv(), RegisterInShimCache(), and RunApphelpCacheControlTests().

◆ CheckValidation()

void CheckValidation ( UNICODE_STRING PathName)

Definition at line 63 of file NtApphelpCacheControl.c.

64{
65 APPHELP_CACHE_SERVICE_LOOKUP CacheEntry = { {0} };
67
68 /* Validate the handling of a NULL pointer */
69 Status = pNtApphelpCacheControl(ApphelpCacheServiceRemove, NULL);
71 Status = pNtApphelpCacheControl(ApphelpCacheServiceLookup, NULL);
73
74 /* Validate the handling of a NULL pointer inside the struct */
75 Status = pNtApphelpCacheControl(ApphelpCacheServiceRemove, &CacheEntry);
77 Status = pNtApphelpCacheControl(ApphelpCacheServiceLookup, &CacheEntry);
79
80 /* Just call the dump function */
81 Status = pNtApphelpCacheControl(ApphelpCacheServiceDump, NULL);
83
84 /* Validate the handling of an invalid handle inside the struct */
85 CacheEntry.ImageName = *PathName;
86 CacheEntry.ImageHandle = (HANDLE)2;
87 Status = pNtApphelpCacheControl(ApphelpCacheServiceLookup, &CacheEntry);
89
90 /* Validate the handling of an invalid service number */
91 Status = pNtApphelpCacheControl(999, NULL);
93 Status = pNtApphelpCacheControl(999, &CacheEntry);
95}
@ ApphelpCacheServiceLookup
Definition: pstypes.h:973
@ ApphelpCacheServiceRemove
Definition: pstypes.h:974
@ ApphelpCacheServiceDump
Definition: pstypes.h:977
#define STATUS_NOT_FOUND
Definition: shellext.h:72
PVOID HANDLE
Definition: typedefs.h:73
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
VERSIONHELPERAPI IsWindows7OrGreater()

Referenced by RunApphelpCacheControlTests().

◆ InitEnv()

int InitEnv ( UNICODE_STRING PathName)

Definition at line 49 of file NtApphelpCacheControl.c.

50{
53 {
54 /* Windows Vista+ has a different layout for APPHELP_CACHE_SERVICE_LOOKUP */
55 return 0;
56 }
58 "Wrong value for Status, expected: SUCCESS or NOT_FOUND, got: 0x%lx\n",
59 Status);
60 return 1;
61}
NTSTATUS CallCacheControl(UNICODE_STRING *PathName, BOOLEAN WithMapping, APPHELPCACHESERVICECLASS Service)
#define ok(value,...)
Definition: atltest.h:57
#define FALSE
Definition: types.h:117

Referenced by RunApphelpCacheControlTests().

◆ InstallService()

static SC_HANDLE InstallService ( SC_HANDLE  scm_handle)
static

Definition at line 275 of file NtApphelpCacheControl.c.

276{
277 char service_cmd[MAX_PATH+150], *ptr;
278 SC_HANDLE service;
279
280 ptr = service_cmd + GetModuleFileNameA(NULL, service_cmd, MAX_PATH);
281 strcpy(ptr, " NtApphelpCacheControl service");
282 ptr += strlen(ptr);
283
286 service_cmd, NULL, NULL, NULL, NULL, NULL);
287 if (!service)
288 {
289 skip("Could not create helper service\n");
290 return NULL;
291 }
292 return service;
293}
static char service_name[100]
static SC_HANDLE scm_handle
Definition: ServiceArgs.c:20
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
char * strcpy(char *DstString, const char *SrcString)
Definition: utclib.c:388
#define skip(...)
Definition: atltest.h:64
#define MAX_PATH
Definition: compat.h:34
DWORD WINAPI GetModuleFileNameA(HINSTANCE hModule, LPSTR lpFilename, DWORD nSize)
Definition: loader.c:539
static PVOID ptr
Definition: dispmode.c:27
#define GENERIC_ALL
Definition: nt_native.h:92
SC_HANDLE WINAPI CreateServiceA(SC_HANDLE hSCManager, LPCSTR lpServiceName, LPCSTR lpDisplayName, DWORD dwDesiredAccess, DWORD dwServiceType, DWORD dwStartType, DWORD dwErrorControl, LPCSTR lpBinaryPathName, LPCSTR lpLoadOrderGroup, LPDWORD lpdwTagId, LPCSTR lpDependencies, LPCSTR lpServiceStartName, LPCSTR lpPassword)
Definition: scm.c:680
#define SERVICE_DEMAND_START
Definition: cmtypes.h:978
#define SERVICE_WIN32_OWN_PROCESS
Definition: cmtypes.h:962
#define SERVICE_ERROR_IGNORE
Definition: cmtypes.h:981

Referenced by RunTest().

◆ NTSTATUS()

static NTSTATUS ( NTAPI pNtApphelpCacheControl)
static

◆ RegisterInShimCache()

static BOOLEAN RegisterInShimCache ( BOOLEAN  WithMapping)
static

Definition at line 194 of file NtApphelpCacheControl.c.

195{
197 UNICODE_STRING ntPath;
200 GetModuleFileNameW(NULL, szPath, sizeof(szPath) / sizeof(szPath[0]));
202 if (!Result)
203 {
204 DbgPrint("RegisterInShimCache: RtlDosPathNameToNtPathName_U failed\n");
205 return FALSE;
206 }
207
208 Status = CallCacheControl(&ntPath, WithMapping, ApphelpCacheServiceUpdate);
209 if (!NT_SUCCESS(Status))
210 {
211 DbgPrint("RegisterInShimCache: CallCacheControl failed\n");
212 RtlFreeHeap(RtlGetProcessHeap(), 0, ntPath.Buffer);
213 return FALSE;
214 }
215 RtlFreeHeap(RtlGetProcessHeap(), 0, ntPath.Buffer);
216 return TRUE;
217}
unsigned char BOOLEAN
BOOLEAN NTAPI RtlFreeHeap(IN PVOID HeapHandle, IN ULONG Flags, IN PVOID HeapBase)
Definition: heap.c:608
#define TRUE
Definition: types.h:120
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:33
DWORD WINAPI GetModuleFileNameW(HINSTANCE hModule, LPWSTR lpFilename, DWORD nSize)
Definition: loader.c:600
#define DbgPrint
Definition: hal.h:12
@ ApphelpCacheServiceUpdate
Definition: pstypes.h:975
LPCWSTR szPath
Definition: env.c:37
NTSYSAPI BOOLEAN NTAPI RtlDosPathNameToNtPathName_U(_In_opt_z_ PCWSTR DosPathName, _Out_ PUNICODE_STRING NtPathName, _Out_opt_ PCWSTR *NtFileNamePart, _Out_opt_ PRTL_RELATIVE_NAME_U DirectoryInfo)
_At_(*)(_In_ PWSK_CLIENT Client, _In_opt_ PUNICODE_STRING NodeName, _In_opt_ PUNICODE_STRING ServiceName, _In_opt_ ULONG NameSpace, _In_opt_ GUID *Provider, _In_opt_ PADDRINFOEXW Hints, _Outptr_ PADDRINFOEXW *Result, _In_opt_ PEPROCESS OwningProcess, _In_opt_ PETHREAD OwningThread, _Inout_ PIRP Irp Result)(Mem)) NTSTATUS(WSKAPI *PFN_WSK_GET_ADDRESS_INFO
Definition: wsk.h:409
__wchar_t WCHAR
Definition: xmlstorage.h:180

Referenced by service_handler().

◆ RequestAddition()

static BOOLEAN RequestAddition ( SC_HANDLE  service_handle,
BOOLEAN  WithMapping 
)
static

Definition at line 97 of file NtApphelpCacheControl.c.

98{
102 /* TODO: how to get a return code from the service? */
103 return TRUE;
104}
static SERVICE_STATUS_HANDLE service_handle
Definition: rpcss_main.c:38
BOOL WINAPI ControlService(SC_HANDLE hService, DWORD dwControl, LPSERVICE_STATUS lpServiceStatus)
Definition: scm.c:622

Referenced by RunApphelpCacheControlTests().

◆ RunApphelpCacheControlTests()

static void RunApphelpCacheControlTests ( SC_HANDLE  service_handle)
static

Definition at line 106 of file NtApphelpCacheControl.c.

107{
109 UNICODE_STRING ntPath;
113
114 GetModuleFileNameW(NULL, szPath, sizeof(szPath) / sizeof(szPath[0]));
116 ok(Result == TRUE, "RtlDosPathNameToNtPathName_U\n");
117 if (!InitEnv(&ntPath))
118 {
119 skip("NtApphelpCacheControl expects a different structure layout\n");
120 return;
121 }
122 /* At this point we have made sure that our binary is not present in the cache,
123 and that the NtApphelpCacheControl function expects the struct layout we use. */
124 CheckValidation(&ntPath);
125
126 /* We expect not to find it */
131
132 /* First we add our process without a file handle (so it will be registered without file info) */
134
135 /* now we try to find it without validating file info */
138 /* when validating file info the cache notices the file is wrong, so it is dropped from the cache */
141 /* making the second check without info also fail. */
144
145
146 /* Now we add the file with file info */
148
149 /* so both checks should succeed */
154
155 /* We know the file is in the cache now (assuming previous tests succeeded,
156 let's test invalid handle behavior */
157 CacheEntry.ImageName = ntPath;
158 CacheEntry.ImageHandle = 0;
159 Status = pNtApphelpCacheControl(ApphelpCacheServiceLookup, &CacheEntry);
161
162 /* re-add it for the next test */
166 CacheEntry.ImageHandle = (HANDLE)1;
167 Status = pNtApphelpCacheControl(ApphelpCacheServiceLookup, &CacheEntry);
169
170 /* and again */
174#ifdef _WIN64
175 CacheEntry.ImageHandle = (HANDLE)0x8000000000000000ULL;
176#else
177 CacheEntry.ImageHandle = (HANDLE)0x80000000;
178#endif
179 Status = pNtApphelpCacheControl(ApphelpCacheServiceLookup, &CacheEntry);
181
182 RtlFreeHeap(RtlGetProcessHeap(), 0, ntPath.Buffer);
183}
static BOOLEAN RequestAddition(SC_HANDLE service_handle, BOOLEAN WithMapping)
int InitEnv(UNICODE_STRING *PathName)
void CheckValidation(UNICODE_STRING *PathName)

Referenced by RunTest().

◆ RunTest()

static void RunTest ( )
static

Definition at line 317 of file NtApphelpCacheControl.c.

318{
321 if (service_handle)
322 {
323 SERVICE_STATUS_PROCESS ssp = {0};
325 if (res)
326 {
331 }
332 else
333 {
334 skip("Could not start helper service\n");
335 }
337 }
339}
static void WaitService(SC_HANDLE service_handle, DWORD Status, SERVICE_STATUS_PROCESS *ssp)
static void RunApphelpCacheControlTests(SC_HANDLE service_handle)
static SC_HANDLE InstallService(SC_HANDLE scm_handle)
unsigned int BOOL
Definition: ntddk_ex.h:94
GLuint res
Definition: glext.h:9613
BOOL WINAPI DeleteService(SC_HANDLE hService)
Definition: scm.c:921
BOOL WINAPI StartServiceA(SC_HANDLE hService, DWORD dwNumServiceArgs, LPCSTR *lpServiceArgVectors)
Definition: scm.c:2942
SC_HANDLE WINAPI OpenSCManagerA(LPCSTR lpMachineName, LPCSTR lpDatabaseName, DWORD dwDesiredAccess)
Definition: scm.c:2024
BOOL WINAPI CloseServiceHandle(SC_HANDLE hSCObject)
Definition: scm.c:580
#define SERVICE_STOPPED
Definition: winsvc.h:21
#define SERVICE_RUNNING
Definition: winsvc.h:24
#define SERVICE_CONTROL_STOP
Definition: winsvc.h:36
#define SC_MANAGER_ALL_ACCESS
Definition: winsvc.h:13

Referenced by START_TEST().

◆ service_handler()

static DWORD WINAPI service_handler ( DWORD  ctrl,
DWORD  event_type,
void event_data,
void context 
)
static

Definition at line 220 of file NtApphelpCacheControl.c.

221{
223 status.dwServiceType = SERVICE_WIN32;
224 status.dwControlsAccepted = SERVICE_ACCEPT_STOP;
225
226 switch(ctrl)
227 {
230 status.dwCurrentState = SERVICE_STOP_PENDING;
231 status.dwControlsAccepted = 0;
234 return NO_ERROR;
237 {
238 /* TODO: how should we communicate a failure? */
239 }
240 break;
243 {
244 /* TODO: how should we communicate a failure? */
245 }
246 break;
247 default:
248 DbgPrint("Unhandled: %d\n", ctrl);
249 break;
250 }
251 status.dwCurrentState = SERVICE_RUNNING;
253 return NO_ERROR;
254}
static HANDLE service_stop_event
static SERVICE_STATUS_HANDLE service_status
static BOOLEAN RegisterInShimCache(BOOLEAN WithMapping)
#define NO_ERROR
Definition: dderror.h:5
#define ctrl
Definition: input.c:1756
BOOL WINAPI SetServiceStatus(SERVICE_STATUS_HANDLE hServiceStatus, LPSERVICE_STATUS lpServiceStatus)
Definition: sctrl.c:997
Definition: ps.c:97
BOOL WINAPI DECLSPEC_HOTPATCH SetEvent(IN HANDLE hEvent)
Definition: synch.c:733
#define SERVICE_ACCEPT_STOP
Definition: winsvc.h:28
#define SERVICE_STOP_PENDING
Definition: winsvc.h:23
#define SERVICE_CONTROL_SHUTDOWN
Definition: winsvc.h:40
#define SERVICE_WIN32
Definition: cmtypes.h:964

Referenced by service_main().

◆ service_main()

static void WINAPI service_main ( DWORD  argc,
char **  argv 
)
static

Definition at line 256 of file NtApphelpCacheControl.c.

257{
259 service_status = pRegisterServiceCtrlHandlerExA(service_name, service_handler, NULL);
260 if(!service_status)
261 return;
262
263 status.dwServiceType = SERVICE_WIN32;
264 status.dwCurrentState = SERVICE_RUNNING;
267
269
270 status.dwCurrentState = SERVICE_STOPPED;
271 status.dwControlsAccepted = 0;
273}
static DWORD WINAPI service_handler(DWORD ctrl, DWORD event_type, void *event_data, void *context)
#define INFINITE
Definition: serial.h:102
DWORD WINAPI WaitForSingleObject(IN HANDLE hHandle, IN DWORD dwMilliseconds)
Definition: synch.c:82
#define SERVICE_ACCEPT_SHUTDOWN
Definition: winsvc.h:30

Referenced by START_TEST().

◆ SERVICE_STATUS_HANDLE()

◆ START_TEST()

START_TEST ( NtApphelpCacheControl  )

Definition at line 341 of file NtApphelpCacheControl.c.

342{
343 char **argv;
344 int argc;
345
346 pRegisterServiceCtrlHandlerExA = (void*)GetProcAddress(GetModuleHandleA("advapi32.dll"), "RegisterServiceCtrlHandlerExA");
347 if (!pRegisterServiceCtrlHandlerExA)
348 {
349 win_skip("RegisterServiceCtrlHandlerExA not available, skipping tests\n");
350 return;
351 }
352
353 pNtApphelpCacheControl = (void*)GetProcAddress(GetModuleHandleA("ntdll.dll"), "NtApphelpCacheControl");
354 if (!pNtApphelpCacheControl)
355 {
356 win_skip("NtApphelpCacheControl not available, skipping tests\n");
357 return;
358 }
359
361 if(argc < 3)
362 {
363 RunTest();
364 }
365 else
366 {
367 SERVICE_TABLE_ENTRYA servtbl[] = {
369 {NULL, NULL}
370 };
373 Sleep(50);
375 }
376}
static void WINAPI service_main(DWORD argc, char **argv)
static void RunTest()
static int argc
Definition: ServiceArgs.c:12
#define CloseHandle
Definition: compat.h:739
#define GetProcAddress(x, y)
Definition: compat.h:753
HMODULE WINAPI DECLSPEC_HOTPATCH GetModuleHandleA(LPCSTR lpModuleName)
Definition: loader.c:812
#define argv
Definition: mplay32.c:18
BOOL WINAPI StartServiceCtrlDispatcherA(const SERVICE_TABLE_ENTRYA *lpServiceStartTable)
Definition: sctrl.c:1035
#define win_skip
Definition: test.h:163
int winetest_get_mainargs(char ***pargv)
VOID WINAPI DECLSPEC_HOTPATCH Sleep(IN DWORD dwMilliseconds)
Definition: synch.c:790
HANDLE WINAPI DECLSPEC_HOTPATCH CreateEventA(IN LPSECURITY_ATTRIBUTES lpEventAttributes OPTIONAL, IN BOOL bManualReset, IN BOOL bInitialState, IN LPCSTR lpName OPTIONAL)
Definition: synch.c:637

◆ WaitService()

static void WaitService ( SC_HANDLE  service_handle,
DWORD  Status,
SERVICE_STATUS_PROCESS ssp 
)
static

Definition at line 295 of file NtApphelpCacheControl.c.

296{
297 DWORD dwBytesNeeded;
298 DWORD dwStartTime = GetTickCount();
299 while (ssp->dwCurrentState != Status)
300 {
301 Sleep(40);
303 (LPBYTE)ssp, sizeof(SERVICE_STATUS_PROCESS), &dwBytesNeeded ))
304 {
305 ok(0, "QueryServiceStatusEx failed waiting for %lu\n", Status);
306 break;
307 }
308 if ((GetTickCount() - dwStartTime) > 1000)
309 {
310 ok(0, "Timeout waiting for (%lu) from service, is: %lu.\n",
311 Status, ssp->dwCurrentState);
312 break;
313 }
314 }
315}
DWORD WINAPI GetTickCount(VOID)
Definition: time.c:455
unsigned long DWORD
Definition: ntddk_ex.h:95
BOOL WINAPI QueryServiceStatusEx(SC_HANDLE hService, SC_STATUS_TYPE InfoLevel, LPBYTE lpBuffer, DWORD cbBufSize, LPDWORD pcbBytesNeeded)
Definition: scm.c:2887
unsigned char * LPBYTE
Definition: typedefs.h:53
@ SC_STATUS_PROCESS_INFO
Definition: winsvc.h:119

Referenced by RunTest().

Variable Documentation

◆ LPHANDLER_FUNCTION_EX

LPHANDLER_FUNCTION_EX

Definition at line 189 of file NtApphelpCacheControl.c.

◆ LPVOID

Definition at line 189 of file NtApphelpCacheControl.c.

◆ PAPPHELP_CACHE_SERVICE_LOOKUP

◆ service_name

◆ service_status

SERVICE_STATUS_HANDLE service_status
static

Definition at line 192 of file NtApphelpCacheControl.c.

Referenced by service_handler(), and service_main().

◆ service_stop_event

HANDLE service_stop_event
static

Definition at line 191 of file NtApphelpCacheControl.c.

Referenced by service_handler(), service_main(), and START_TEST().