2007-05-17 16:51:44 +02:00
|
|
|
/*
|
|
|
|
* Service process to load a kernel driver
|
|
|
|
*
|
|
|
|
* Copyright 2007 Alexandre Julliard
|
2016-08-23 10:13:42 +02:00
|
|
|
* Copyright 2016 Sebastian Lackner
|
2007-05-17 16:51:44 +02:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2008-04-02 20:40:27 +02:00
|
|
|
#include "config.h"
|
|
|
|
#include "wine/port.h"
|
|
|
|
|
2007-05-17 16:51:44 +02:00
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
#include "ntstatus.h"
|
|
|
|
#define WIN32_NO_STATUS
|
|
|
|
#include "windef.h"
|
|
|
|
#include "winbase.h"
|
|
|
|
#include "winternl.h"
|
|
|
|
#include "winreg.h"
|
|
|
|
#include "winnls.h"
|
|
|
|
#include "winsvc.h"
|
|
|
|
#include "ddk/wdm.h"
|
2016-08-19 08:02:09 +02:00
|
|
|
#include "wine/rbtree.h"
|
|
|
|
#include "wine/svcctl.h"
|
2007-05-17 16:51:44 +02:00
|
|
|
#include "wine/unicode.h"
|
|
|
|
#include "wine/debug.h"
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(winedevice);
|
|
|
|
WINE_DECLARE_DEBUG_CHANNEL(relay);
|
|
|
|
|
2008-12-16 15:36:59 +01:00
|
|
|
extern NTSTATUS CDECL wine_ntoskrnl_main_loop( HANDLE stop_event );
|
2007-05-17 16:51:44 +02:00
|
|
|
|
2016-09-01 07:39:29 +02:00
|
|
|
static const WCHAR winedeviceW[] = {'w','i','n','e','d','e','v','i','c','e',0};
|
2007-05-17 16:51:44 +02:00
|
|
|
static SERVICE_STATUS_HANDLE service_handle;
|
2016-08-23 10:13:42 +02:00
|
|
|
static PTP_CLEANUP_GROUP cleanup_group;
|
2016-08-19 08:02:09 +02:00
|
|
|
static SC_HANDLE manager_handle;
|
2016-08-23 10:13:42 +02:00
|
|
|
static BOOL shutdown_in_progress;
|
2007-05-17 16:51:44 +02:00
|
|
|
static HANDLE stop_event;
|
|
|
|
|
2016-08-19 08:02:09 +02:00
|
|
|
struct wine_driver
|
|
|
|
{
|
|
|
|
struct wine_rb_entry entry;
|
|
|
|
|
|
|
|
SERVICE_STATUS_HANDLE handle;
|
|
|
|
DRIVER_OBJECT *driver_obj;
|
|
|
|
WCHAR name[1];
|
|
|
|
};
|
|
|
|
|
2016-08-30 21:30:48 +02:00
|
|
|
static int wine_drivers_rb_compare( const void *key, const struct wine_rb_entry *entry )
|
|
|
|
{
|
|
|
|
const struct wine_driver *driver = WINE_RB_ENTRY_VALUE( entry, const struct wine_driver, entry );
|
|
|
|
return strcmpW( (const WCHAR *)key, driver->name );
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct wine_rb_tree wine_drivers = { wine_drivers_rb_compare };
|
2016-08-19 08:02:09 +02:00
|
|
|
|
2016-08-23 10:13:42 +02:00
|
|
|
static CRITICAL_SECTION drivers_cs;
|
|
|
|
static CRITICAL_SECTION_DEBUG critsect_debug =
|
|
|
|
{
|
|
|
|
0, 0, &drivers_cs,
|
|
|
|
{ &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
|
|
|
|
0, 0, { (DWORD_PTR)(__FILE__ ": drivers_cs") }
|
|
|
|
};
|
|
|
|
static CRITICAL_SECTION drivers_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
|
|
|
|
|
2007-05-17 16:51:44 +02:00
|
|
|
/* find the LDR_MODULE corresponding to the driver module */
|
|
|
|
static LDR_MODULE *find_ldr_module( HMODULE module )
|
|
|
|
{
|
2016-02-26 06:11:26 +01:00
|
|
|
LDR_MODULE *ldr;
|
|
|
|
ULONG_PTR magic;
|
2007-05-17 16:51:44 +02:00
|
|
|
|
2016-02-26 06:11:26 +01:00
|
|
|
LdrLockLoaderLock( 0, NULL, &magic );
|
|
|
|
if (LdrFindEntryForAddress( module, &ldr ))
|
2007-05-17 16:51:44 +02:00
|
|
|
{
|
2016-02-26 06:11:26 +01:00
|
|
|
WARN( "module not found for %p\n", module );
|
|
|
|
ldr = NULL;
|
2007-05-17 16:51:44 +02:00
|
|
|
}
|
2016-02-26 06:11:26 +01:00
|
|
|
LdrUnlockLoaderLock( 0, magic );
|
|
|
|
|
|
|
|
return ldr;
|
2007-05-17 16:51:44 +02:00
|
|
|
}
|
|
|
|
|
2008-04-02 20:40:27 +02:00
|
|
|
/* load the driver module file */
|
|
|
|
static HMODULE load_driver_module( const WCHAR *name )
|
|
|
|
{
|
2008-12-22 15:51:25 +01:00
|
|
|
IMAGE_NT_HEADERS *nt;
|
|
|
|
const IMAGE_IMPORT_DESCRIPTOR *imports;
|
2013-01-08 22:01:48 +01:00
|
|
|
SYSTEM_BASIC_INFORMATION info;
|
2010-06-23 13:51:46 +02:00
|
|
|
int i;
|
|
|
|
INT_PTR delta;
|
2008-12-22 15:51:25 +01:00
|
|
|
ULONG size;
|
2008-04-02 20:40:27 +02:00
|
|
|
HMODULE module = LoadLibraryW( name );
|
|
|
|
|
|
|
|
if (!module) return NULL;
|
|
|
|
nt = RtlImageNtHeader( module );
|
|
|
|
|
|
|
|
if (!(delta = (char *)module - (char *)nt->OptionalHeader.ImageBase)) return module;
|
|
|
|
|
|
|
|
/* the loader does not apply relocations to non page-aligned binaries or executables,
|
|
|
|
* we have to do it ourselves */
|
|
|
|
|
2013-01-08 22:01:48 +01:00
|
|
|
NtQuerySystemInformation( SystemBasicInformation, &info, sizeof(info), NULL );
|
|
|
|
if (nt->OptionalHeader.SectionAlignment < info.PageSize ||
|
2008-04-02 20:40:27 +02:00
|
|
|
!(nt->FileHeader.Characteristics & IMAGE_FILE_DLL))
|
|
|
|
{
|
|
|
|
DWORD old;
|
|
|
|
IMAGE_BASE_RELOCATION *rel, *end;
|
|
|
|
|
|
|
|
if ((rel = RtlImageDirectoryEntryToData( module, TRUE, IMAGE_DIRECTORY_ENTRY_BASERELOC, &size )))
|
|
|
|
{
|
|
|
|
WINE_TRACE( "%s: relocating from %p to %p\n",
|
|
|
|
wine_dbgstr_w(name), (char *)module - delta, module );
|
|
|
|
end = (IMAGE_BASE_RELOCATION *)((char *)rel + size);
|
|
|
|
while (rel < end && rel->SizeOfBlock)
|
|
|
|
{
|
|
|
|
void *page = (char *)module + rel->VirtualAddress;
|
2013-01-08 22:01:48 +01:00
|
|
|
VirtualProtect( page, info.PageSize, PAGE_EXECUTE_READWRITE, &old );
|
2008-04-02 20:40:27 +02:00
|
|
|
rel = LdrProcessRelocationBlock( page, (rel->SizeOfBlock - sizeof(*rel)) / sizeof(USHORT),
|
|
|
|
(USHORT *)(rel + 1), delta );
|
2015-05-02 18:43:34 +02:00
|
|
|
if (old != PAGE_EXECUTE_READWRITE) VirtualProtect( page, info.PageSize, old, &old );
|
2008-04-02 20:40:27 +02:00
|
|
|
if (!rel) goto error;
|
|
|
|
}
|
2008-12-22 15:51:25 +01:00
|
|
|
/* make sure we don't try again */
|
2010-06-15 14:29:25 +02:00
|
|
|
size = FIELD_OFFSET( IMAGE_NT_HEADERS, OptionalHeader ) + nt->FileHeader.SizeOfOptionalHeader;
|
|
|
|
VirtualProtect( nt, size, PAGE_READWRITE, &old );
|
2008-12-22 15:51:25 +01:00
|
|
|
nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress = 0;
|
2015-05-02 18:43:34 +02:00
|
|
|
VirtualProtect( nt, size, old, &old );
|
2008-04-02 20:40:27 +02:00
|
|
|
}
|
|
|
|
}
|
2008-12-22 15:51:25 +01:00
|
|
|
|
|
|
|
/* make sure imports are relocated too */
|
|
|
|
|
|
|
|
if ((imports = RtlImageDirectoryEntryToData( module, TRUE, IMAGE_DIRECTORY_ENTRY_IMPORT, &size )))
|
|
|
|
{
|
|
|
|
for (i = 0; imports[i].Name && imports[i].FirstThunk; i++)
|
|
|
|
{
|
|
|
|
char *name = (char *)module + imports[i].Name;
|
|
|
|
WCHAR buffer[32], *p = buffer;
|
|
|
|
|
|
|
|
while (p < buffer + 32) if (!(*p++ = *name++)) break;
|
|
|
|
if (p <= buffer + 32) FreeLibrary( load_driver_module( buffer ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-02 20:40:27 +02:00
|
|
|
return module;
|
|
|
|
|
|
|
|
error:
|
|
|
|
FreeLibrary( module );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-05-17 16:51:44 +02:00
|
|
|
/* load the .sys module for a device driver */
|
2016-08-04 18:59:50 +02:00
|
|
|
static HMODULE load_driver( const WCHAR *driver_name, const UNICODE_STRING *keyname )
|
2007-05-17 16:51:44 +02:00
|
|
|
{
|
2008-09-17 00:33:26 +02:00
|
|
|
static const WCHAR driversW[] = {'\\','d','r','i','v','e','r','s','\\',0};
|
2011-11-23 13:12:31 +01:00
|
|
|
static const WCHAR systemrootW[] = {'\\','S','y','s','t','e','m','R','o','o','t','\\',0};
|
2008-09-17 00:33:26 +02:00
|
|
|
static const WCHAR postfixW[] = {'.','s','y','s',0};
|
2007-05-17 16:51:44 +02:00
|
|
|
static const WCHAR ntprefixW[] = {'\\','?','?','\\',0};
|
|
|
|
static const WCHAR ImagePathW[] = {'I','m','a','g','e','P','a','t','h',0};
|
2016-02-26 06:11:36 +01:00
|
|
|
HKEY driver_hkey;
|
2007-05-17 16:51:44 +02:00
|
|
|
HMODULE module;
|
|
|
|
LPWSTR path = NULL, str;
|
|
|
|
DWORD type, size;
|
|
|
|
|
2016-08-04 18:59:50 +02:00
|
|
|
if (RegOpenKeyW( HKEY_LOCAL_MACHINE, keyname->Buffer + 18 /* skip \registry\machine */, &driver_hkey ))
|
2007-05-17 16:51:44 +02:00
|
|
|
{
|
2016-08-04 18:59:50 +02:00
|
|
|
WINE_ERR( "cannot open key %s, err=%u\n", wine_dbgstr_w(keyname->Buffer), GetLastError() );
|
2016-02-26 06:11:36 +01:00
|
|
|
return NULL;
|
2007-05-17 16:51:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* read the executable path from memory */
|
|
|
|
size = 0;
|
2008-09-17 00:33:26 +02:00
|
|
|
if (!RegQueryValueExW( driver_hkey, ImagePathW, NULL, &type, NULL, &size ))
|
|
|
|
{
|
|
|
|
str = HeapAlloc( GetProcessHeap(), 0, size );
|
|
|
|
if (!RegQueryValueExW( driver_hkey, ImagePathW, NULL, &type, (LPBYTE)str, &size ))
|
|
|
|
{
|
|
|
|
size = ExpandEnvironmentStringsW(str,NULL,0);
|
|
|
|
path = HeapAlloc(GetProcessHeap(),0,size*sizeof(WCHAR));
|
|
|
|
ExpandEnvironmentStringsW(str,path,size);
|
|
|
|
}
|
|
|
|
HeapFree( GetProcessHeap(), 0, str );
|
2016-02-26 06:11:36 +01:00
|
|
|
if (!path)
|
|
|
|
{
|
|
|
|
RegCloseKey( driver_hkey );
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-11-23 13:12:31 +01:00
|
|
|
|
|
|
|
if (!strncmpiW( path, systemrootW, 12 ))
|
|
|
|
{
|
|
|
|
WCHAR buffer[MAX_PATH];
|
|
|
|
|
|
|
|
GetWindowsDirectoryW(buffer, MAX_PATH);
|
|
|
|
|
|
|
|
str = HeapAlloc(GetProcessHeap(), 0, (size -11 + strlenW(buffer))
|
|
|
|
* sizeof(WCHAR));
|
|
|
|
lstrcpyW(str, buffer);
|
|
|
|
lstrcatW(str, path + 11);
|
|
|
|
HeapFree( GetProcessHeap(), 0, path );
|
|
|
|
path = str;
|
|
|
|
}
|
|
|
|
else if (!strncmpW( path, ntprefixW, 4 ))
|
|
|
|
str = path + 4;
|
|
|
|
else
|
|
|
|
str = path;
|
2008-09-17 00:33:26 +02:00
|
|
|
}
|
|
|
|
else
|
2007-05-17 16:51:44 +02:00
|
|
|
{
|
2008-09-17 00:33:26 +02:00
|
|
|
/* default is to use the driver name + ".sys" */
|
|
|
|
WCHAR buffer[MAX_PATH];
|
|
|
|
GetSystemDirectoryW(buffer, MAX_PATH);
|
|
|
|
path = HeapAlloc(GetProcessHeap(),0,
|
|
|
|
(strlenW(buffer) + strlenW(driversW) + strlenW(driver_name) + strlenW(postfixW) + 1)
|
|
|
|
*sizeof(WCHAR));
|
|
|
|
lstrcpyW(path, buffer);
|
|
|
|
lstrcatW(path, driversW);
|
|
|
|
lstrcatW(path, driver_name);
|
|
|
|
lstrcatW(path, postfixW);
|
2011-11-23 13:12:31 +01:00
|
|
|
str = path;
|
2007-05-17 16:51:44 +02:00
|
|
|
}
|
2016-02-26 06:11:36 +01:00
|
|
|
RegCloseKey( driver_hkey );
|
2007-05-17 16:51:44 +02:00
|
|
|
|
|
|
|
WINE_TRACE( "loading driver %s\n", wine_dbgstr_w(str) );
|
|
|
|
|
2008-04-02 20:40:27 +02:00
|
|
|
module = load_driver_module( str );
|
2007-05-17 16:51:44 +02:00
|
|
|
HeapFree( GetProcessHeap(), 0, path );
|
2016-08-04 18:59:50 +02:00
|
|
|
return module;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* call the driver init entry point */
|
|
|
|
static NTSTATUS WINAPI init_driver( DRIVER_OBJECT *driver_object, UNICODE_STRING *keyname )
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
NTSTATUS status;
|
|
|
|
const IMAGE_NT_HEADERS *nt;
|
|
|
|
const WCHAR *driver_name;
|
2016-08-04 19:00:00 +02:00
|
|
|
HMODULE module;
|
2016-08-04 18:59:50 +02:00
|
|
|
|
|
|
|
/* Retrieve driver name from the keyname */
|
|
|
|
driver_name = strrchrW( keyname->Buffer, '\\' );
|
|
|
|
driver_name++;
|
|
|
|
|
2016-08-04 19:00:00 +02:00
|
|
|
module = load_driver( driver_name, keyname );
|
|
|
|
if (!module)
|
2016-08-04 18:59:50 +02:00
|
|
|
return STATUS_DLL_INIT_FAILED;
|
|
|
|
|
2016-08-04 19:00:00 +02:00
|
|
|
driver_object->DriverSection = find_ldr_module( module );
|
2016-08-04 18:59:50 +02:00
|
|
|
|
2016-08-04 19:00:00 +02:00
|
|
|
nt = RtlImageNtHeader( module );
|
2016-08-04 18:59:50 +02:00
|
|
|
if (!nt->OptionalHeader.AddressOfEntryPoint) return STATUS_SUCCESS;
|
2016-08-04 19:00:00 +02:00
|
|
|
driver_object->DriverInit = (PDRIVER_INITIALIZE)((char *)module + nt->OptionalHeader.AddressOfEntryPoint);
|
2016-08-04 18:59:50 +02:00
|
|
|
|
2017-11-25 11:48:19 +01:00
|
|
|
TRACE_(relay)( "\1Call driver init %p (obj=%p,str=%s)\n",
|
|
|
|
driver_object->DriverInit, driver_object, wine_dbgstr_w(keyname->Buffer) );
|
2016-08-04 18:59:50 +02:00
|
|
|
|
|
|
|
status = driver_object->DriverInit( driver_object, keyname );
|
|
|
|
|
2017-11-25 11:48:19 +01:00
|
|
|
TRACE_(relay)( "\1Ret driver init %p (obj=%p,str=%s) retval=%08x\n",
|
|
|
|
driver_object->DriverInit, driver_object, wine_dbgstr_w(keyname->Buffer), status );
|
2016-08-04 18:59:50 +02:00
|
|
|
|
|
|
|
WINE_TRACE( "init done for %s obj %p\n", wine_dbgstr_w(driver_name), driver_object );
|
|
|
|
WINE_TRACE( "- DriverInit = %p\n", driver_object->DriverInit );
|
|
|
|
WINE_TRACE( "- DriverStartIo = %p\n", driver_object->DriverStartIo );
|
|
|
|
WINE_TRACE( "- DriverUnload = %p\n", driver_object->DriverUnload );
|
|
|
|
for (i = 0; i <= IRP_MJ_MAXIMUM_FUNCTION; i++)
|
|
|
|
WINE_TRACE( "- MajorFunction[%d] = %p\n", i, driver_object->MajorFunction[i] );
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2016-08-19 08:02:09 +02:00
|
|
|
/* helper function to update service status */
|
|
|
|
static void set_service_status( SERVICE_STATUS_HANDLE handle, DWORD state, DWORD accepted )
|
|
|
|
{
|
|
|
|
SERVICE_STATUS status;
|
|
|
|
status.dwServiceType = SERVICE_WIN32;
|
|
|
|
status.dwCurrentState = state;
|
|
|
|
status.dwControlsAccepted = accepted;
|
|
|
|
status.dwWin32ExitCode = 0;
|
|
|
|
status.dwServiceSpecificExitCode = 0;
|
|
|
|
status.dwCheckPoint = 0;
|
|
|
|
status.dwWaitHint = (state == SERVICE_START_PENDING) ? 10000 : 0;
|
|
|
|
SetServiceStatus( handle, &status );
|
|
|
|
}
|
|
|
|
|
2016-08-23 10:13:42 +02:00
|
|
|
static void WINAPI async_unload_driver( PTP_CALLBACK_INSTANCE instance, void *context )
|
|
|
|
{
|
|
|
|
struct wine_driver *driver = context;
|
|
|
|
DRIVER_OBJECT *driver_obj = driver->driver_obj;
|
|
|
|
LDR_MODULE *ldr;
|
|
|
|
|
2017-11-25 11:48:19 +01:00
|
|
|
TRACE_(relay)( "\1Call driver unload %p (obj=%p)\n", driver_obj->DriverUnload, driver_obj );
|
2016-08-23 10:13:42 +02:00
|
|
|
|
|
|
|
driver_obj->DriverUnload( driver_obj );
|
|
|
|
|
2017-11-25 11:48:19 +01:00
|
|
|
TRACE_(relay)( "\1Ret driver unload %p (obj=%p)\n", driver_obj->DriverUnload, driver_obj );
|
2016-08-23 10:13:42 +02:00
|
|
|
|
|
|
|
ldr = driver_obj->DriverSection;
|
|
|
|
FreeLibrary( ldr->BaseAddress );
|
|
|
|
IoDeleteDriver( driver_obj );
|
|
|
|
ObDereferenceObject( driver_obj );
|
|
|
|
|
|
|
|
set_service_status( driver->handle, SERVICE_STOPPED, 0 );
|
|
|
|
CloseServiceHandle( (void *)driver->handle );
|
|
|
|
HeapFree( GetProcessHeap(), 0, driver );
|
|
|
|
}
|
|
|
|
|
2016-08-04 18:59:50 +02:00
|
|
|
/* call the driver unload function */
|
2016-08-23 10:13:42 +02:00
|
|
|
static NTSTATUS unload_driver( struct wine_rb_entry *entry, BOOL destroy )
|
2016-08-04 18:59:50 +02:00
|
|
|
{
|
2016-08-23 10:13:42 +02:00
|
|
|
TP_CALLBACK_ENVIRON environment;
|
2016-08-19 08:02:09 +02:00
|
|
|
struct wine_driver *driver = WINE_RB_ENTRY_VALUE( entry, struct wine_driver, entry );
|
|
|
|
DRIVER_OBJECT *driver_obj = driver->driver_obj;
|
2016-08-23 10:13:42 +02:00
|
|
|
|
|
|
|
if (!driver_obj)
|
|
|
|
{
|
|
|
|
TRACE( "driver %s has not finished loading yet\n", wine_dbgstr_w(driver->name) );
|
|
|
|
return STATUS_UNSUCCESSFUL;
|
|
|
|
}
|
|
|
|
if (!driver_obj->DriverUnload)
|
|
|
|
{
|
|
|
|
TRACE( "driver %s does not support unloading\n", wine_dbgstr_w(driver->name) );
|
|
|
|
return STATUS_UNSUCCESSFUL;
|
|
|
|
}
|
2016-08-04 19:00:00 +02:00
|
|
|
|
2016-08-19 08:02:09 +02:00
|
|
|
TRACE( "stopping driver %s\n", wine_dbgstr_w(driver->name) );
|
|
|
|
set_service_status( driver->handle, SERVICE_STOP_PENDING, 0 );
|
|
|
|
|
2016-08-23 10:13:42 +02:00
|
|
|
if (destroy)
|
2016-02-26 06:11:36 +01:00
|
|
|
{
|
2016-08-23 10:13:42 +02:00
|
|
|
async_unload_driver( NULL, driver );
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
2016-08-04 18:59:50 +02:00
|
|
|
|
2016-09-12 13:44:09 +02:00
|
|
|
wine_rb_remove( &wine_drivers, &driver->entry );
|
2016-08-04 18:59:50 +02:00
|
|
|
|
2016-08-23 10:13:42 +02:00
|
|
|
memset( &environment, 0, sizeof(environment) );
|
|
|
|
environment.Version = 1;
|
|
|
|
environment.CleanupGroup = cleanup_group;
|
|
|
|
|
|
|
|
/* don't block the service control handler */
|
|
|
|
if (!TrySubmitThreadpoolCallback( async_unload_driver, driver, &environment ))
|
|
|
|
async_unload_driver( NULL, driver );
|
|
|
|
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void WINAPI async_create_driver( PTP_CALLBACK_INSTANCE instance, void *context )
|
|
|
|
{
|
|
|
|
static const WCHAR driverW[] = {'\\','D','r','i','v','e','r','\\',0};
|
|
|
|
struct wine_driver *driver = context;
|
|
|
|
DRIVER_OBJECT *driver_obj;
|
|
|
|
UNICODE_STRING drv_name;
|
|
|
|
NTSTATUS status;
|
|
|
|
WCHAR *str;
|
|
|
|
|
|
|
|
if (!(str = HeapAlloc( GetProcessHeap(), 0, sizeof(driverW) + strlenW(driver->name)*sizeof(WCHAR) )))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
lstrcpyW( str, driverW);
|
|
|
|
lstrcatW( str, driver->name );
|
|
|
|
RtlInitUnicodeString( &drv_name, str );
|
|
|
|
|
|
|
|
status = IoCreateDriver( &drv_name, init_driver );
|
|
|
|
if (status != STATUS_SUCCESS)
|
|
|
|
{
|
|
|
|
ERR( "failed to create driver %s: %08x\n", debugstr_w(driver->name), status );
|
|
|
|
RtlFreeUnicodeString( &drv_name );
|
|
|
|
goto error;
|
2016-02-26 06:11:36 +01:00
|
|
|
}
|
2016-08-04 19:00:00 +02:00
|
|
|
|
2016-08-23 10:13:42 +02:00
|
|
|
status = ObReferenceObjectByName( &drv_name, OBJ_CASE_INSENSITIVE, NULL,
|
|
|
|
0, NULL, KernelMode, NULL, (void **)&driver_obj );
|
|
|
|
RtlFreeUnicodeString( &drv_name );
|
|
|
|
if (status != STATUS_SUCCESS)
|
|
|
|
{
|
|
|
|
ERR( "failed to locate driver %s: %08x\n", debugstr_w(driver->name), status );
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
EnterCriticalSection( &drivers_cs );
|
|
|
|
driver->driver_obj = driver_obj;
|
|
|
|
set_service_status( driver->handle, SERVICE_RUNNING,
|
|
|
|
SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN );
|
|
|
|
LeaveCriticalSection( &drivers_cs );
|
|
|
|
return;
|
|
|
|
|
|
|
|
error:
|
|
|
|
EnterCriticalSection( &drivers_cs );
|
2016-09-12 13:44:09 +02:00
|
|
|
wine_rb_remove( &wine_drivers, &driver->entry );
|
2016-08-23 10:13:42 +02:00
|
|
|
LeaveCriticalSection( &drivers_cs );
|
2016-08-19 08:02:09 +02:00
|
|
|
|
|
|
|
set_service_status( driver->handle, SERVICE_STOPPED, 0 );
|
|
|
|
CloseServiceHandle( (void *)driver->handle );
|
|
|
|
HeapFree( GetProcessHeap(), 0, driver );
|
2016-08-04 18:59:50 +02:00
|
|
|
}
|
2007-05-17 16:51:44 +02:00
|
|
|
|
2016-08-19 08:02:09 +02:00
|
|
|
/* load a driver and notify services.exe about the status change */
|
|
|
|
static NTSTATUS create_driver( const WCHAR *driver_name )
|
2016-08-04 18:59:50 +02:00
|
|
|
{
|
2016-08-23 10:13:42 +02:00
|
|
|
TP_CALLBACK_ENVIRON environment;
|
2016-08-19 08:02:09 +02:00
|
|
|
struct wine_driver *driver;
|
|
|
|
DWORD length;
|
2016-08-04 18:59:50 +02:00
|
|
|
|
2016-08-19 08:02:09 +02:00
|
|
|
length = FIELD_OFFSET( struct wine_driver, name[strlenW(driver_name) + 1] );
|
|
|
|
if (!(driver = HeapAlloc( GetProcessHeap(), 0, length )))
|
|
|
|
return STATUS_NO_MEMORY;
|
|
|
|
|
|
|
|
strcpyW( driver->name, driver_name );
|
|
|
|
driver->driver_obj = NULL;
|
|
|
|
|
|
|
|
if (!(driver->handle = (void *)OpenServiceW( manager_handle, driver_name, SERVICE_SET_STATUS )))
|
|
|
|
{
|
|
|
|
HeapFree( GetProcessHeap(), 0, driver );
|
|
|
|
return STATUS_UNSUCCESSFUL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wine_rb_put( &wine_drivers, driver_name, &driver->entry ))
|
|
|
|
{
|
|
|
|
CloseServiceHandle( (void *)driver->handle );
|
|
|
|
HeapFree( GetProcessHeap(), 0, driver );
|
|
|
|
return STATUS_UNSUCCESSFUL;
|
|
|
|
}
|
|
|
|
|
|
|
|
TRACE( "starting driver %s\n", wine_dbgstr_w(driver_name) );
|
|
|
|
set_service_status( driver->handle, SERVICE_START_PENDING, 0 );
|
|
|
|
|
2016-08-23 10:13:42 +02:00
|
|
|
memset( &environment, 0, sizeof(environment) );
|
|
|
|
environment.Version = 1;
|
|
|
|
environment.CleanupGroup = cleanup_group;
|
2016-08-04 18:59:50 +02:00
|
|
|
|
2016-08-23 10:13:42 +02:00
|
|
|
/* don't block the service control handler */
|
|
|
|
if (!TrySubmitThreadpoolCallback( async_create_driver, driver, &environment ))
|
|
|
|
async_create_driver( NULL, driver );
|
2016-08-08 15:35:49 +02:00
|
|
|
|
2016-08-23 10:13:42 +02:00
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
2016-08-19 08:02:09 +02:00
|
|
|
|
2016-08-23 10:13:42 +02:00
|
|
|
static void wine_drivers_rb_destroy( struct wine_rb_entry *entry, void *context )
|
|
|
|
{
|
2016-08-29 13:50:34 +02:00
|
|
|
if (unload_driver( entry, TRUE ) != STATUS_SUCCESS)
|
|
|
|
{
|
|
|
|
struct wine_driver *driver = WINE_RB_ENTRY_VALUE( entry, struct wine_driver, entry );
|
|
|
|
ObDereferenceObject( driver->driver_obj );
|
|
|
|
CloseServiceHandle( (void *)driver->handle );
|
|
|
|
HeapFree( GetProcessHeap(), 0, driver );
|
|
|
|
}
|
2016-08-23 10:13:42 +02:00
|
|
|
}
|
2016-08-19 08:02:09 +02:00
|
|
|
|
2016-08-23 10:13:42 +02:00
|
|
|
static void WINAPI async_shutdown_drivers( PTP_CALLBACK_INSTANCE instance, void *context )
|
|
|
|
{
|
|
|
|
CloseThreadpoolCleanupGroupMembers( cleanup_group, FALSE, NULL );
|
|
|
|
|
|
|
|
EnterCriticalSection( &drivers_cs );
|
|
|
|
wine_rb_destroy( &wine_drivers, wine_drivers_rb_destroy, NULL );
|
|
|
|
LeaveCriticalSection( &drivers_cs );
|
|
|
|
|
|
|
|
SetEvent( stop_event );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void shutdown_drivers( void )
|
|
|
|
{
|
|
|
|
if (shutdown_in_progress) return;
|
|
|
|
|
|
|
|
/* don't block the service control handler */
|
|
|
|
if (!TrySubmitThreadpoolCallback( async_shutdown_drivers, NULL, NULL ))
|
|
|
|
async_shutdown_drivers( NULL, NULL );
|
|
|
|
|
|
|
|
shutdown_in_progress = TRUE;
|
2007-05-17 16:51:44 +02:00
|
|
|
}
|
|
|
|
|
2016-09-01 07:39:29 +02:00
|
|
|
static DWORD device_handler( DWORD ctrl, const WCHAR *driver_name )
|
|
|
|
{
|
|
|
|
struct wine_rb_entry *entry;
|
|
|
|
DWORD result = NO_ERROR;
|
|
|
|
|
|
|
|
if (shutdown_in_progress)
|
|
|
|
return ERROR_SERVICE_CANNOT_ACCEPT_CTRL;
|
|
|
|
|
|
|
|
EnterCriticalSection( &drivers_cs );
|
|
|
|
entry = wine_rb_get( &wine_drivers, driver_name );
|
|
|
|
|
|
|
|
switch (ctrl)
|
|
|
|
{
|
|
|
|
case SERVICE_CONTROL_START:
|
|
|
|
if (entry) break;
|
|
|
|
result = RtlNtStatusToDosError(create_driver( driver_name ));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SERVICE_CONTROL_STOP:
|
|
|
|
if (!entry) break;
|
|
|
|
result = RtlNtStatusToDosError(unload_driver( entry, FALSE ));
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
FIXME( "got driver ctrl %x for %s\n", ctrl, wine_dbgstr_w(driver_name) );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
LeaveCriticalSection( &drivers_cs );
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2007-05-17 16:51:44 +02:00
|
|
|
static DWORD WINAPI service_handler( DWORD ctrl, DWORD event_type, LPVOID event_data, LPVOID context )
|
|
|
|
{
|
2016-09-01 07:39:29 +02:00
|
|
|
const WCHAR *service_group = context;
|
|
|
|
|
|
|
|
if (ctrl & SERVICE_CONTROL_FORWARD_FLAG)
|
|
|
|
{
|
|
|
|
if (!event_data) return ERROR_INVALID_PARAMETER;
|
|
|
|
return device_handler( ctrl & ~SERVICE_CONTROL_FORWARD_FLAG, (const WCHAR *)event_data );
|
|
|
|
}
|
2007-05-17 16:51:44 +02:00
|
|
|
|
2016-08-19 08:02:09 +02:00
|
|
|
switch (ctrl)
|
2007-05-17 16:51:44 +02:00
|
|
|
{
|
|
|
|
case SERVICE_CONTROL_STOP:
|
|
|
|
case SERVICE_CONTROL_SHUTDOWN:
|
2016-09-01 07:39:29 +02:00
|
|
|
TRACE( "shutting down %s\n", wine_dbgstr_w(service_group) );
|
2016-08-19 08:02:09 +02:00
|
|
|
set_service_status( service_handle, SERVICE_STOP_PENDING, 0 );
|
2016-08-23 10:13:42 +02:00
|
|
|
shutdown_drivers();
|
2007-05-17 16:51:44 +02:00
|
|
|
return NO_ERROR;
|
|
|
|
default:
|
2016-09-01 07:39:29 +02:00
|
|
|
FIXME( "got service ctrl %x for %s\n", ctrl, wine_dbgstr_w(service_group) );
|
2016-08-19 08:02:09 +02:00
|
|
|
set_service_status( service_handle, SERVICE_RUNNING,
|
|
|
|
SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN );
|
2007-05-17 16:51:44 +02:00
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void WINAPI ServiceMain( DWORD argc, LPWSTR *argv )
|
|
|
|
{
|
2016-09-01 07:39:29 +02:00
|
|
|
const WCHAR *service_group = (argc >= 2) ? argv[1] : argv[0];
|
2007-05-17 16:51:44 +02:00
|
|
|
|
2016-08-19 08:02:09 +02:00
|
|
|
if (!(stop_event = CreateEventW( NULL, TRUE, FALSE, NULL )))
|
|
|
|
return;
|
2016-08-23 10:13:42 +02:00
|
|
|
if (!(cleanup_group = CreateThreadpoolCleanupGroup()))
|
|
|
|
return;
|
2016-08-19 08:02:09 +02:00
|
|
|
if (!(manager_handle = OpenSCManagerW( NULL, NULL, SC_MANAGER_CONNECT )))
|
|
|
|
return;
|
2016-09-01 07:39:29 +02:00
|
|
|
if (!(service_handle = RegisterServiceCtrlHandlerExW( winedeviceW, service_handler, (void *)service_group )))
|
2008-03-28 18:52:24 +01:00
|
|
|
return;
|
2007-05-17 16:51:44 +02:00
|
|
|
|
2016-09-01 07:39:29 +02:00
|
|
|
TRACE( "starting service group %s\n", wine_dbgstr_w(service_group) );
|
|
|
|
set_service_status( service_handle, SERVICE_RUNNING,
|
|
|
|
SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN );
|
2016-08-23 10:13:42 +02:00
|
|
|
|
2016-09-01 07:39:29 +02:00
|
|
|
wine_ntoskrnl_main_loop( stop_event );
|
2007-05-17 16:51:44 +02:00
|
|
|
|
2016-09-01 07:39:29 +02:00
|
|
|
TRACE( "service group %s stopped\n", wine_dbgstr_w(service_group) );
|
2016-08-19 08:02:09 +02:00
|
|
|
set_service_status( service_handle, SERVICE_STOPPED, 0 );
|
|
|
|
CloseServiceHandle( manager_handle );
|
2016-08-23 10:13:42 +02:00
|
|
|
CloseThreadpoolCleanupGroup( cleanup_group );
|
2016-08-19 08:02:09 +02:00
|
|
|
CloseHandle( stop_event );
|
2007-05-17 16:51:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int wmain( int argc, WCHAR *argv[] )
|
|
|
|
{
|
|
|
|
SERVICE_TABLE_ENTRYW service_table[2];
|
|
|
|
|
2016-09-01 07:39:29 +02:00
|
|
|
service_table[0].lpServiceName = (void *)winedeviceW;
|
2007-05-17 16:51:44 +02:00
|
|
|
service_table[0].lpServiceProc = ServiceMain;
|
|
|
|
service_table[1].lpServiceName = NULL;
|
|
|
|
service_table[1].lpServiceProc = NULL;
|
|
|
|
|
|
|
|
StartServiceCtrlDispatcherW( service_table );
|
|
|
|
return 0;
|
|
|
|
}
|