configure: Make the libfontconfig soname check depend on the header check.
[wine/multimedia.git] / programs / winedevice / device.c
blob9af85f4e1fd2c50438daa60f7dfbf22ba81dd8a6
1 /*
2 * Service process to load a kernel driver
4 * Copyright 2007 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdarg.h>
23 #include "ntstatus.h"
24 #define WIN32_NO_STATUS
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winternl.h"
28 #include "winreg.h"
29 #include "winnls.h"
30 #include "winsvc.h"
31 #include "ddk/wdm.h"
32 #include "wine/unicode.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(winedevice);
36 WINE_DECLARE_DEBUG_CHANNEL(relay);
38 extern NTSTATUS wine_ntoskrnl_main_loop( HANDLE stop_event );
40 static WCHAR *driver_name;
41 static SERVICE_STATUS_HANDLE service_handle;
42 static HKEY driver_hkey;
43 static HANDLE stop_event;
44 static DRIVER_OBJECT driver_obj;
45 static DRIVER_EXTENSION driver_extension;
47 /* find the LDR_MODULE corresponding to the driver module */
48 static LDR_MODULE *find_ldr_module( HMODULE module )
50 LIST_ENTRY *entry, *list = &NtCurrentTeb()->Peb->LdrData->InMemoryOrderModuleList;
52 for (entry = list->Flink; entry != list; entry = entry->Flink)
54 LDR_MODULE *ldr = CONTAINING_RECORD(entry, LDR_MODULE, InMemoryOrderModuleList);
55 if (ldr->BaseAddress == module) return ldr;
56 if (ldr->BaseAddress > (void *)module) break;
58 return NULL;
61 /* call the driver init entry point */
62 static NTSTATUS init_driver( HMODULE module, UNICODE_STRING *keyname )
64 unsigned int i;
65 NTSTATUS status;
66 const IMAGE_NT_HEADERS *nt = RtlImageNtHeader( module );
68 if (!nt->OptionalHeader.AddressOfEntryPoint) return STATUS_SUCCESS;
70 driver_obj.Size = sizeof(driver_obj);
71 driver_obj.DriverSection = find_ldr_module( module );
72 driver_obj.DriverInit = (PDRIVER_INITIALIZE)((char *)module + nt->OptionalHeader.AddressOfEntryPoint);
73 driver_obj.DriverExtension = &driver_extension;
75 driver_extension.DriverObject = &driver_obj;
76 driver_extension.ServiceKeyName = *keyname;
78 if (WINE_TRACE_ON(relay))
79 WINE_DPRINTF( "%04x:Call driver init %p (obj=%p,str=%s)\n", GetCurrentThreadId(),
80 driver_obj.DriverInit, &driver_obj, wine_dbgstr_w(keyname->Buffer) );
82 status = driver_obj.DriverInit( &driver_obj, keyname );
84 if (WINE_TRACE_ON(relay))
85 WINE_DPRINTF( "%04x:Ret driver init %p (obj=%p,str=%s) retval=%08x\n", GetCurrentThreadId(),
86 driver_obj.DriverInit, &driver_obj, wine_dbgstr_w(keyname->Buffer), status );
88 WINE_TRACE( "init done for %s obj %p\n", wine_dbgstr_w(driver_name), &driver_obj );
89 WINE_TRACE( "- DriverInit = %p\n", driver_obj.DriverInit );
90 WINE_TRACE( "- DriverStartIo = %p\n", driver_obj.DriverStartIo );
91 WINE_TRACE( "- DriverUnload = %p\n", driver_obj.DriverUnload );
92 for (i = 0; i <= IRP_MJ_MAXIMUM_FUNCTION; i++)
93 WINE_TRACE( "- MajorFunction[%d] = %p\n", i, driver_obj.MajorFunction[i] );
95 return status;
98 /* load the .sys module for a device driver */
99 static BOOL load_driver(void)
101 static const WCHAR ntprefixW[] = {'\\','?','?','\\',0};
102 static const WCHAR ImagePathW[] = {'I','m','a','g','e','P','a','t','h',0};
103 static const WCHAR servicesW[] = {'\\','R','e','g','i','s','t','r','y',
104 '\\','M','a','c','h','i','n','e',
105 '\\','S','y','s','t','e','m',
106 '\\','C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t',
107 '\\','S','e','r','v','i','c','e','s','\\',0};
109 UNICODE_STRING keypath;
110 HMODULE module;
111 LPWSTR path = NULL, str;
112 DWORD type, size;
114 str = HeapAlloc( GetProcessHeap(), 0, sizeof(servicesW) + strlenW(driver_name)*sizeof(WCHAR) );
115 lstrcpyW( str, servicesW );
116 lstrcatW( str, driver_name );
118 if (RegOpenKeyW( HKEY_LOCAL_MACHINE, str + 18 /* skip \registry\machine */, &driver_hkey ))
120 WINE_ERR( "cannot open key %s, err=%u\n", wine_dbgstr_w(str), GetLastError() );
121 return FALSE;
123 RtlInitUnicodeString( &keypath, str );
125 /* read the executable path from memory */
126 size = 0;
127 if (RegQueryValueExW( driver_hkey, ImagePathW, NULL, &type, NULL, &size )) return FALSE;
129 str = HeapAlloc( GetProcessHeap(), 0, size );
130 if (!RegQueryValueExW( driver_hkey, ImagePathW, NULL, &type, (LPBYTE)str, &size ))
132 size = ExpandEnvironmentStringsW(str,NULL,0);
133 path = HeapAlloc(GetProcessHeap(),0,size*sizeof(WCHAR));
134 ExpandEnvironmentStringsW(str,path,size);
136 HeapFree( GetProcessHeap(), 0, str );
137 if (!path) return FALSE;
139 /* make sure msvcrt is loaded to resolve the ntoskrnl.exe forwards */
140 LoadLibraryA( "msvcrt.dll" );
142 /* GameGuard uses an NT-style path name */
143 str = path;
144 if (!strncmpW( path, ntprefixW, 4 )) str += 4;
146 WINE_TRACE( "loading driver %s\n", wine_dbgstr_w(str) );
148 module = LoadLibraryW( str );
149 HeapFree( GetProcessHeap(), 0, path );
150 if (!module) return FALSE;
152 init_driver( module, &keypath );
153 return TRUE;
156 static DWORD WINAPI service_handler( DWORD ctrl, DWORD event_type, LPVOID event_data, LPVOID context )
158 SERVICE_STATUS status;
160 status.dwServiceType = SERVICE_WIN32;
161 status.dwControlsAccepted = SERVICE_ACCEPT_STOP;
162 status.dwWin32ExitCode = 0;
163 status.dwServiceSpecificExitCode = 0;
164 status.dwCheckPoint = 0;
165 status.dwWaitHint = 0;
167 switch(ctrl)
169 case SERVICE_CONTROL_STOP:
170 case SERVICE_CONTROL_SHUTDOWN:
171 WINE_TRACE( "shutting down %s\n", wine_dbgstr_w(driver_name) );
172 status.dwCurrentState = SERVICE_STOP_PENDING;
173 status.dwControlsAccepted = 0;
174 SetServiceStatus( service_handle, &status );
175 SetEvent( stop_event );
176 return NO_ERROR;
177 default:
178 WINE_FIXME( "got service ctrl %x for %s\n", ctrl, wine_dbgstr_w(driver_name) );
179 status.dwCurrentState = SERVICE_RUNNING;
180 SetServiceStatus( service_handle, &status );
181 return NO_ERROR;
185 static void WINAPI ServiceMain( DWORD argc, LPWSTR *argv )
187 SERVICE_STATUS status;
189 WINE_TRACE( "starting service %s\n", wine_dbgstr_w(driver_name) );
191 stop_event = CreateEventW( NULL, TRUE, FALSE, NULL );
193 service_handle = RegisterServiceCtrlHandlerExW( driver_name, service_handler, NULL );
195 status.dwServiceType = SERVICE_WIN32;
196 status.dwCurrentState = SERVICE_START_PENDING;
197 status.dwControlsAccepted = 0;
198 status.dwWin32ExitCode = 0;
199 status.dwServiceSpecificExitCode = 0;
200 status.dwCheckPoint = 0;
201 status.dwWaitHint = 10000;
202 SetServiceStatus( service_handle, &status );
204 if (load_driver())
206 status.dwCurrentState = SERVICE_RUNNING;
207 status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
208 SetServiceStatus( service_handle, &status );
210 wine_ntoskrnl_main_loop( stop_event );
212 else WINE_ERR( "driver %s failed to load\n", wine_dbgstr_w(driver_name) );
214 status.dwCurrentState = SERVICE_STOPPED;
215 status.dwControlsAccepted = 0;
216 SetServiceStatus( service_handle, &status );
217 WINE_TRACE( "service %s stopped\n", wine_dbgstr_w(driver_name) );
220 int wmain( int argc, WCHAR *argv[] )
222 SERVICE_TABLE_ENTRYW service_table[2];
224 if (!(driver_name = argv[1]))
226 WINE_ERR( "missing device name, winedevice isn't supposed to be run manually\n" );
227 return 1;
230 service_table[0].lpServiceName = argv[1];
231 service_table[0].lpServiceProc = ServiceMain;
232 service_table[1].lpServiceName = NULL;
233 service_table[1].lpServiceProc = NULL;
235 StartServiceCtrlDispatcherW( service_table );
236 return 0;