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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
#include "ntstatus.h"
|
|
|
|
#define WIN32_NO_STATUS
|
|
|
|
#include "windef.h"
|
|
|
|
#include "winternl.h"
|
|
|
|
#include "ddk/wdm.h"
|
2016-08-19 08:02:09 +02:00
|
|
|
#include "wine/svcctl.h"
|
2007-05-17 16:51:44 +02:00
|
|
|
#include "wine/debug.h"
|
2018-08-25 05:42:58 +02:00
|
|
|
#include "wine/heap.h"
|
2007-05-17 16:51:44 +02:00
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(winedevice);
|
2018-08-25 05:42:58 +02:00
|
|
|
|
2020-11-03 23:34:37 +01:00
|
|
|
static const WCHAR servicesW[] = L"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\";
|
2007-05-17 16:51:44 +02:00
|
|
|
|
2008-12-16 15:36:59 +01:00
|
|
|
extern NTSTATUS CDECL wine_ntoskrnl_main_loop( HANDLE stop_event );
|
2021-04-02 17:53:35 +02:00
|
|
|
extern void CDECL wine_enumerate_root_devices( const WCHAR *driver_name );
|
2007-05-17 16:51:44 +02:00
|
|
|
|
2020-11-03 23:34:36 +01:00
|
|
|
static WCHAR winedeviceW[] = L"winedevice";
|
2007-05-17 16:51:44 +02:00
|
|
|
static SERVICE_STATUS_HANDLE service_handle;
|
2016-08-19 08:02:09 +02:00
|
|
|
static SC_HANDLE manager_handle;
|
2007-05-17 16:51:44 +02:00
|
|
|
static HANDLE stop_event;
|
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
2021-04-02 17:53:35 +02:00
|
|
|
#define SERVICE_CONTROL_REENUMERATE_ROOT_DEVICES 128
|
|
|
|
|
2018-08-25 05:42:58 +02:00
|
|
|
static DWORD device_handler( DWORD ctrl, const WCHAR *driver_name )
|
2016-08-04 18:59:50 +02:00
|
|
|
{
|
2018-08-25 05:42:58 +02:00
|
|
|
UNICODE_STRING service_name;
|
|
|
|
DWORD result = NO_ERROR;
|
2018-08-25 05:42:57 +02:00
|
|
|
WCHAR *str;
|
|
|
|
|
2019-05-02 11:08:01 +02:00
|
|
|
if (!(str = heap_alloc( sizeof(servicesW) + lstrlenW(driver_name)*sizeof(WCHAR) )))
|
2018-08-25 05:42:57 +02:00
|
|
|
return STATUS_NO_MEMORY;
|
|
|
|
|
2018-08-25 05:42:58 +02:00
|
|
|
lstrcpyW( str, servicesW );
|
|
|
|
lstrcatW( str, driver_name );
|
|
|
|
RtlInitUnicodeString( &service_name, str );
|
2016-09-01 07:39:29 +02:00
|
|
|
|
|
|
|
switch (ctrl)
|
|
|
|
{
|
|
|
|
case SERVICE_CONTROL_START:
|
2018-08-25 05:42:58 +02:00
|
|
|
result = RtlNtStatusToDosError(ZwLoadDriver( &service_name ));
|
2016-09-01 07:39:29 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SERVICE_CONTROL_STOP:
|
2018-08-25 05:42:58 +02:00
|
|
|
result = RtlNtStatusToDosError(ZwUnloadDriver( &service_name ));
|
2016-09-01 07:39:29 +02:00
|
|
|
break;
|
|
|
|
|
2021-04-02 17:53:35 +02:00
|
|
|
case SERVICE_CONTROL_REENUMERATE_ROOT_DEVICES:
|
|
|
|
wine_enumerate_root_devices( driver_name );
|
2021-04-08 06:56:41 +02:00
|
|
|
break;
|
2021-04-02 17:53:35 +02:00
|
|
|
|
2016-09-01 07:39:29 +02:00
|
|
|
default:
|
|
|
|
FIXME( "got driver ctrl %x for %s\n", ctrl, wine_dbgstr_w(driver_name) );
|
|
|
|
break;
|
|
|
|
}
|
2018-08-25 05:42:58 +02:00
|
|
|
|
|
|
|
RtlFreeUnicodeString( &service_name );
|
2016-09-01 07:39:29 +02:00
|
|
|
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 );
|
2018-08-25 05:42:57 +02:00
|
|
|
SetEvent( stop_event );
|
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 )
|
|
|
|
{
|
2021-03-29 16:00:29 +02:00
|
|
|
WCHAR driver_dir[MAX_PATH];
|
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;
|
|
|
|
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
|
|
|
|
2021-03-29 16:00:29 +02:00
|
|
|
GetSystemDirectoryW( driver_dir, MAX_PATH );
|
|
|
|
wcscat( driver_dir, L"\\drivers" );
|
|
|
|
AddDllDirectory( driver_dir );
|
|
|
|
|
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 );
|
|
|
|
CloseHandle( stop_event );
|
2007-05-17 16:51:44 +02:00
|
|
|
}
|
|
|
|
|
2019-10-17 17:05:47 +02:00
|
|
|
int __cdecl wmain( int argc, WCHAR *argv[] )
|
2007-05-17 16:51:44 +02:00
|
|
|
{
|
|
|
|
SERVICE_TABLE_ENTRYW service_table[2];
|
|
|
|
|
2020-11-03 23:34:36 +01:00
|
|
|
service_table[0].lpServiceName = 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;
|
|
|
|
}
|