2011-01-31 15:26:17 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Hans Leidekker for CodeWeavers
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
|
|
|
|
#include <windows.h>
|
2020-11-25 16:04:15 +01:00
|
|
|
#include <dbt.h>
|
2012-01-23 12:16:36 +01:00
|
|
|
#include "winsvc.h"
|
2011-01-31 15:26:17 +01:00
|
|
|
#include "wine/debug.h"
|
2020-05-05 18:25:38 +02:00
|
|
|
#include "wine/list.h"
|
|
|
|
#include "plugplay.h"
|
2011-01-31 15:26:17 +01:00
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(plugplay);
|
|
|
|
|
2020-10-12 00:02:49 +02:00
|
|
|
static WCHAR plugplayW[] = L"PlugPlay";
|
2011-01-31 15:26:17 +01:00
|
|
|
|
|
|
|
static SERVICE_STATUS_HANDLE service_handle;
|
|
|
|
static HANDLE stop_event;
|
|
|
|
|
2020-05-05 18:25:38 +02:00
|
|
|
void __RPC_FAR * __RPC_USER MIDL_user_allocate( SIZE_T len )
|
|
|
|
{
|
|
|
|
return malloc( len );
|
|
|
|
}
|
|
|
|
|
|
|
|
void __RPC_USER MIDL_user_free( void __RPC_FAR *ptr )
|
|
|
|
{
|
|
|
|
free( ptr );
|
|
|
|
}
|
|
|
|
|
|
|
|
static CRITICAL_SECTION plugplay_cs;
|
|
|
|
static CRITICAL_SECTION_DEBUG plugplay_cs_debug =
|
|
|
|
{
|
|
|
|
0, 0, &plugplay_cs,
|
|
|
|
{ &plugplay_cs_debug.ProcessLocksList, &plugplay_cs_debug.ProcessLocksList },
|
|
|
|
0, 0, { (DWORD_PTR)(__FILE__ ": plugplay_cs") }
|
|
|
|
};
|
|
|
|
static CRITICAL_SECTION plugplay_cs = { &plugplay_cs_debug, -1, 0, 0, 0, 0 };
|
|
|
|
|
|
|
|
static struct list listener_list = LIST_INIT(listener_list);
|
|
|
|
|
|
|
|
struct listener
|
|
|
|
{
|
|
|
|
struct list entry;
|
|
|
|
struct list events;
|
|
|
|
CONDITION_VARIABLE cv;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct event
|
|
|
|
{
|
|
|
|
struct list entry;
|
|
|
|
DWORD code;
|
|
|
|
BYTE *data;
|
|
|
|
unsigned int size;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static void destroy_listener( struct listener *listener )
|
|
|
|
{
|
|
|
|
struct event *event, *next;
|
|
|
|
|
|
|
|
EnterCriticalSection( &plugplay_cs );
|
|
|
|
list_remove( &listener->entry );
|
|
|
|
LeaveCriticalSection( &plugplay_cs );
|
|
|
|
|
|
|
|
LIST_FOR_EACH_ENTRY_SAFE(event, next, &listener->events, struct event, entry)
|
|
|
|
{
|
|
|
|
MIDL_user_free( event->data );
|
|
|
|
list_remove( &event->entry );
|
|
|
|
free( event );
|
|
|
|
}
|
|
|
|
free( listener );
|
|
|
|
}
|
|
|
|
|
|
|
|
void __RPC_USER plugplay_rpc_handle_rundown( plugplay_rpc_handle handle )
|
|
|
|
{
|
|
|
|
destroy_listener( handle );
|
|
|
|
}
|
|
|
|
|
|
|
|
plugplay_rpc_handle __cdecl plugplay_register_listener(void)
|
|
|
|
{
|
|
|
|
struct listener *listener;
|
|
|
|
|
|
|
|
if (!(listener = calloc( 1, sizeof(*listener) )))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
list_init( &listener->events );
|
|
|
|
InitializeConditionVariable( &listener->cv );
|
|
|
|
|
|
|
|
EnterCriticalSection( &plugplay_cs );
|
|
|
|
list_add_tail( &listener_list, &listener->entry );
|
|
|
|
LeaveCriticalSection( &plugplay_cs );
|
|
|
|
|
|
|
|
return listener;
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD __cdecl plugplay_get_event( plugplay_rpc_handle handle, BYTE **data, unsigned int *size )
|
|
|
|
{
|
|
|
|
struct listener *listener = handle;
|
|
|
|
struct event *event;
|
|
|
|
struct list *entry;
|
|
|
|
DWORD ret;
|
|
|
|
|
|
|
|
EnterCriticalSection( &plugplay_cs );
|
|
|
|
|
|
|
|
while (!(entry = list_head( &listener->events )))
|
|
|
|
SleepConditionVariableCS( &listener->cv, &plugplay_cs, INFINITE );
|
|
|
|
|
|
|
|
event = LIST_ENTRY(entry, struct event, entry);
|
|
|
|
list_remove( &event->entry );
|
|
|
|
|
|
|
|
LeaveCriticalSection( &plugplay_cs );
|
|
|
|
|
|
|
|
ret = event->code;
|
|
|
|
*data = event->data;
|
|
|
|
*size = event->size;
|
|
|
|
free( event );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void __cdecl plugplay_unregister_listener( plugplay_rpc_handle handle )
|
|
|
|
{
|
|
|
|
destroy_listener( handle );
|
|
|
|
}
|
|
|
|
|
2020-05-05 18:25:39 +02:00
|
|
|
void __cdecl plugplay_send_event( DWORD code, const BYTE *data, unsigned int size )
|
|
|
|
{
|
|
|
|
struct listener *listener;
|
|
|
|
struct event *event;
|
|
|
|
|
plugplay: Broadcast WM_DEVICECHANGE message asynchronously.
It may otherwise trigger a nasty race condition, where:
1) For explorer.exe to register the CLSID_ShellWindows classes, it
needs RpcSS service to be started,
2) services.exe may start WinePlugPlay service group first, waiting for
its startup to complete,
3) during startup and early device enumeration, hidclass.sys may call
IoSetDeviceInterfaceState, which calls plugplay_send_event [1],
4) plugplay_send_event tries to broadcast a WM_DEVICECHANGE message with
BSF_QUERY, waiting for the individual threads to reply,
5) which times-out because window threads are waiting on explorer.exe
to create its desktop window and reply to the WM_NULL SendMessage.
This happens more likely as there is threads with message queues
being started, each waiting on the desktop window to reply. Usually
explorer.exe is able to reply to some messages with the implicit
message processing while creating its window, but not all of them.
[1] Not completely sure how, it looks like some RPC too, but before
RpcSs is started?
Signed-off-by: Rémi Bernon <rbernon@codeweavers.com>
Signed-off-by: Alexandre Julliard <julliard@winehq.org>
2021-02-15 20:42:00 +01:00
|
|
|
BroadcastSystemMessageW( 0, NULL, WM_DEVICECHANGE, code, (LPARAM)data );
|
|
|
|
BroadcastSystemMessageW( 0, NULL, WM_DEVICECHANGE, DBT_DEVNODES_CHANGED, 0 );
|
2020-05-05 18:25:41 +02:00
|
|
|
|
2020-05-05 18:25:39 +02:00
|
|
|
EnterCriticalSection( &plugplay_cs );
|
|
|
|
|
|
|
|
LIST_FOR_EACH_ENTRY(listener, &listener_list, struct listener, entry)
|
|
|
|
{
|
|
|
|
if (!(event = malloc( sizeof(*event) )))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (!(event->data = malloc( size )))
|
|
|
|
{
|
|
|
|
free( event );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
event->code = code;
|
|
|
|
memcpy( event->data, data, size );
|
|
|
|
event->size = size;
|
|
|
|
list_add_tail( &listener->events, &event->entry );
|
|
|
|
WakeConditionVariable( &listener->cv );
|
|
|
|
}
|
|
|
|
|
|
|
|
LeaveCriticalSection( &plugplay_cs );
|
|
|
|
}
|
|
|
|
|
2011-01-31 15:26:17 +01:00
|
|
|
static DWORD WINAPI service_handler( DWORD ctrl, DWORD event_type, LPVOID event_data, LPVOID context )
|
|
|
|
{
|
|
|
|
SERVICE_STATUS status;
|
|
|
|
|
|
|
|
status.dwServiceType = SERVICE_WIN32;
|
|
|
|
status.dwControlsAccepted = SERVICE_ACCEPT_STOP;
|
|
|
|
status.dwWin32ExitCode = 0;
|
|
|
|
status.dwServiceSpecificExitCode = 0;
|
|
|
|
status.dwCheckPoint = 0;
|
|
|
|
status.dwWaitHint = 0;
|
|
|
|
|
|
|
|
switch(ctrl)
|
|
|
|
{
|
|
|
|
case SERVICE_CONTROL_STOP:
|
|
|
|
case SERVICE_CONTROL_SHUTDOWN:
|
|
|
|
WINE_TRACE( "shutting down\n" );
|
|
|
|
status.dwCurrentState = SERVICE_STOP_PENDING;
|
|
|
|
status.dwControlsAccepted = 0;
|
|
|
|
SetServiceStatus( service_handle, &status );
|
|
|
|
SetEvent( stop_event );
|
|
|
|
return NO_ERROR;
|
|
|
|
default:
|
2022-02-04 09:57:09 +01:00
|
|
|
WINE_FIXME( "got service ctrl %lx\n", ctrl );
|
2011-01-31 15:26:17 +01:00
|
|
|
status.dwCurrentState = SERVICE_RUNNING;
|
|
|
|
SetServiceStatus( service_handle, &status );
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void WINAPI ServiceMain( DWORD argc, LPWSTR *argv )
|
|
|
|
{
|
2020-05-05 18:25:38 +02:00
|
|
|
unsigned char endpoint[] = "\\pipe\\wine_plugplay";
|
2022-01-26 10:50:20 +01:00
|
|
|
unsigned char protseq[] = "ncacn_np";
|
2011-01-31 15:26:17 +01:00
|
|
|
SERVICE_STATUS status;
|
2020-05-05 18:25:38 +02:00
|
|
|
RPC_STATUS err;
|
2011-01-31 15:26:17 +01:00
|
|
|
|
|
|
|
WINE_TRACE( "starting service\n" );
|
|
|
|
|
2020-05-05 18:25:38 +02:00
|
|
|
if ((err = RpcServerUseProtseqEpA( protseq, 0, endpoint, NULL )))
|
|
|
|
{
|
2022-02-04 09:57:09 +01:00
|
|
|
ERR("RpcServerUseProtseqEp() failed, error %lu\n", err);
|
2020-05-05 18:25:38 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ((err = RpcServerRegisterIf( plugplay_v0_0_s_ifspec, NULL, NULL )))
|
|
|
|
{
|
2022-02-04 09:57:09 +01:00
|
|
|
ERR("RpcServerRegisterIf() failed, error %lu\n", err);
|
2020-05-05 18:25:38 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ((err = RpcServerListen( 1, RPC_C_LISTEN_MAX_CALLS_DEFAULT, TRUE )))
|
|
|
|
{
|
2022-02-04 09:57:09 +01:00
|
|
|
ERR("RpcServerListen() failed, error %lu\n", err);
|
2020-05-05 18:25:38 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-01-31 15:26:17 +01:00
|
|
|
stop_event = CreateEventW( NULL, TRUE, FALSE, NULL );
|
|
|
|
|
|
|
|
service_handle = RegisterServiceCtrlHandlerExW( plugplayW, service_handler, NULL );
|
|
|
|
if (!service_handle)
|
|
|
|
return;
|
|
|
|
|
|
|
|
status.dwServiceType = SERVICE_WIN32;
|
|
|
|
status.dwCurrentState = SERVICE_RUNNING;
|
|
|
|
status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
|
|
|
|
status.dwWin32ExitCode = 0;
|
|
|
|
status.dwServiceSpecificExitCode = 0;
|
|
|
|
status.dwCheckPoint = 0;
|
|
|
|
status.dwWaitHint = 10000;
|
|
|
|
SetServiceStatus( service_handle, &status );
|
|
|
|
|
|
|
|
WaitForSingleObject( stop_event, INFINITE );
|
|
|
|
|
2020-05-05 18:25:38 +02:00
|
|
|
RpcMgmtStopServerListening( NULL );
|
|
|
|
RpcServerUnregisterIf( plugplay_v0_0_s_ifspec, NULL, TRUE );
|
|
|
|
RpcMgmtWaitServerListen();
|
|
|
|
|
2011-01-31 15:26:17 +01:00
|
|
|
status.dwCurrentState = SERVICE_STOPPED;
|
|
|
|
status.dwControlsAccepted = 0;
|
|
|
|
SetServiceStatus( service_handle, &status );
|
|
|
|
WINE_TRACE( "service stopped\n" );
|
|
|
|
}
|
|
|
|
|
2019-10-17 17:05:47 +02:00
|
|
|
int __cdecl wmain( int argc, WCHAR *argv[] )
|
2011-01-31 15:26:17 +01:00
|
|
|
{
|
|
|
|
static const SERVICE_TABLE_ENTRYW service_table[] =
|
|
|
|
{
|
|
|
|
{ plugplayW, ServiceMain },
|
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
StartServiceCtrlDispatcherW( service_table );
|
|
|
|
return 0;
|
|
|
|
}
|