Converted all the kernel32 register functions to the
[wine] / dlls / kernel / vxd.c
1 /*
2  * Win32 VxD functions
3  *
4  * Copyright 1998 Marcus Meissner
5  * Copyright 1998 Ulrich Weigand
6  * Copyright 1998 Patrik Stridvall
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include "config.h"
24 #include "wine/port.h"
25
26 #include <stdlib.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #include <sys/types.h>
31 #ifdef HAVE_SYS_STAT_H
32 # include <sys/stat.h>
33 #endif
34 #include <string.h>
35 #include <stdarg.h>
36
37 #include "windef.h"
38 #include "winbase.h"
39 #include "winreg.h"
40 #include "winerror.h"
41 #include "kernel_private.h"
42 #include "wine/library.h"
43 #include "wine/unicode.h"
44 #include "wine/server.h"
45 #include "wine/debug.h"
46
47 WINE_DEFAULT_DEBUG_CHANNEL(vxd);
48
49 typedef BOOL (WINAPI *DeviceIoProc)(DWORD, LPVOID, DWORD, LPVOID, DWORD, LPDWORD, LPOVERLAPPED);
50 typedef DWORD (WINAPI *VxDCallProc)(DWORD, CONTEXT86 *);
51
52 struct vxd_module
53 {
54     dev_t        dev;
55     ino_t        ino;
56     HANDLE       handle;
57     HMODULE      module;
58     DeviceIoProc proc;
59 };
60
61 struct vxdcall_service
62 {
63     WCHAR       name[12];
64     DWORD       service;
65     HMODULE     module;
66     VxDCallProc proc;
67 };
68
69 #define MAX_VXD_MODULES 32
70
71 static struct vxd_module vxd_modules[MAX_VXD_MODULES];
72
73 static struct vxdcall_service vxd_services[] =
74 {
75     { {'v','m','m','.','v','x','d',0},             0x0001, NULL, NULL },
76     { {'v','w','i','n','3','2','.','v','x','d',0}, 0x002a, NULL, NULL }
77 };
78
79 #define NB_VXD_SERVICES  (sizeof(vxd_services)/sizeof(vxd_services[0]))
80
81 static CRITICAL_SECTION vxd_section;
82 static CRITICAL_SECTION_DEBUG critsect_debug =
83 {
84     0, 0, &vxd_section,
85     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
86       0, 0, { 0, (DWORD)(__FILE__ ": vxd_section") }
87 };
88 static CRITICAL_SECTION vxd_section = { &critsect_debug, -1, 0, 0, 0, 0 };
89
90
91 /* create a file handle to represent a VxD, by opening a dummy file in the wineserver directory */
92 static HANDLE open_vxd_handle( LPCWSTR name )
93 {
94     const char *dir = wine_get_server_dir();
95     int len;
96     HANDLE ret;
97     NTSTATUS status;
98     OBJECT_ATTRIBUTES attr;
99     UNICODE_STRING nameW;
100     IO_STATUS_BLOCK io;
101
102     len = MultiByteToWideChar( CP_UNIXCP, 0, dir, -1, NULL, 0 );
103     nameW.Length = (len + 1 + strlenW( name )) * sizeof(WCHAR);
104     nameW.MaximumLength = nameW.Length + sizeof(WCHAR);
105     if (!(nameW.Buffer = HeapAlloc( GetProcessHeap(), 0, nameW.Length )))
106     {
107         SetLastError( ERROR_NOT_ENOUGH_MEMORY );
108         return 0;
109     }
110     MultiByteToWideChar( CP_UNIXCP, 0, dir, -1, nameW.Buffer, len );
111     nameW.Buffer[len-1] = '/';
112     strcpyW( nameW.Buffer + len, name );
113
114     attr.Length = sizeof(attr);
115     attr.RootDirectory = 0;
116     attr.Attributes = 0;
117     attr.ObjectName = &nameW;
118     attr.SecurityDescriptor = NULL;
119     attr.SecurityQualityOfService = NULL;
120
121     status = NtCreateFile( &ret, 0, &attr, &io, NULL, 0,
122                            FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN_IF,
123                            FILE_SYNCHRONOUS_IO_ALERT, NULL, 0 );
124     if (status)
125     {
126         ret = 0;
127         SetLastError( RtlNtStatusToDosError(status) );
128     }
129     RtlFreeUnicodeString( &nameW );
130     return ret;
131 }
132
133 /* retrieve the DeviceIoControl function for a Vxd given a file handle */
134 static DeviceIoProc get_vxd_proc( HANDLE handle )
135 {
136     struct stat st;
137     DeviceIoProc ret = NULL;
138     int status, i, fd;
139
140     status = wine_server_handle_to_fd( handle, 0, &fd, NULL );
141     if (status)
142     {
143         SetLastError( RtlNtStatusToDosError(status) );
144         return NULL;
145     }
146     if (fstat( fd, &st ) == -1)
147     {
148         wine_server_release_fd( handle, fd );
149         SetLastError( ERROR_INVALID_HANDLE );
150         return NULL;
151     }
152     wine_server_release_fd( handle, fd );
153
154     RtlEnterCriticalSection( &vxd_section );
155
156     for (i = 0; i < MAX_VXD_MODULES; i++)
157     {
158         if (!vxd_modules[i].module) break;
159         if (vxd_modules[i].dev == st.st_dev && vxd_modules[i].ino == st.st_ino)
160         {
161             if (!(ret = vxd_modules[i].proc)) SetLastError( ERROR_INVALID_FUNCTION );
162             goto done;
163         }
164     }
165     /* FIXME: Here we could go through the directory to find the VxD name and load it. */
166     /* Let's wait to find out if there are actually apps out there that try to share   */
167     /* VxD handles between processes, before we go to the trouble of implementing it.  */
168     ERR( "handle %p not found in module list, inherited from another process?\n", handle );
169
170 done:
171     RtlLeaveCriticalSection( &vxd_section );
172     return ret;
173 }
174
175
176 /* load a VxD and return a file handle to it */
177 HANDLE VXD_Open( LPCWSTR filenameW, DWORD access, SECURITY_ATTRIBUTES *sa )
178 {
179     static const WCHAR dotVxDW[] = {'.','v','x','d',0};
180     int i;
181     HANDLE handle;
182     HMODULE module;
183     WCHAR *p, name[16];
184
185     if (!(GetVersion() & 0x80000000))  /* there are no VxDs on NT */
186     {
187         SetLastError( ERROR_FILE_NOT_FOUND );
188         return 0;
189     }
190
191     /* normalize the filename */
192
193     if (strlenW( filenameW ) >= sizeof(name)/sizeof(WCHAR) - 4 ||
194         strchrW( filenameW, '/' ) || strchrW( filenameW, '\\' ))
195     {
196         SetLastError( ERROR_FILE_NOT_FOUND );
197         return 0;
198     }
199     strcpyW( name, filenameW );
200     strlwrW( name );
201     p = strchrW( name, '.' );
202     if (!p) strcatW( name, dotVxDW );
203     else if (strcmpW( p, dotVxDW ))  /* existing extension has to be .vxd */
204     {
205         SetLastError( ERROR_FILE_NOT_FOUND );
206         return 0;
207     }
208
209     /* try to load the module first */
210
211     if (!(module = LoadLibraryW( name )))
212     {
213         FIXME( "Unknown/unsupported VxD %s. Try setting Windows version to 'nt40' or 'win31'.\n",
214                debugstr_w(name) );
215         SetLastError( ERROR_FILE_NOT_FOUND );
216         return 0;
217     }
218
219     /* register the module in the global list if necessary */
220
221     RtlEnterCriticalSection( &vxd_section );
222
223     for (i = 0; i < MAX_VXD_MODULES; i++)
224     {
225         if (vxd_modules[i].module == module)
226         {
227             handle = vxd_modules[i].handle;
228             goto done;  /* already registered */
229         }
230         if (!vxd_modules[i].module)  /* new one, register it */
231         {
232             struct stat st;
233             int fd;
234
235             /* get a file handle to the dummy file */
236             if (!(handle = open_vxd_handle( name )))
237             {
238                 FreeLibrary( module );
239                 goto done;
240             }
241             wine_server_handle_to_fd( handle, 0, &fd, NULL );
242             if (fstat( fd, &st ) != -1)
243             {
244                 vxd_modules[i].dev = st.st_dev;
245                 vxd_modules[i].ino = st.st_ino;
246             }
247             vxd_modules[i].module = module;
248             vxd_modules[i].handle = handle;
249             vxd_modules[i].proc = (DeviceIoProc)GetProcAddress( module, "DeviceIoControl" );
250             wine_server_release_fd( handle, fd );
251             goto done;
252         }
253     }
254
255     ERR("too many open VxD modules, please report\n" );
256     CloseHandle( handle );
257     FreeLibrary( module );
258     handle = 0;
259
260 done:
261     RtlLeaveCriticalSection( &vxd_section );
262     if (!DuplicateHandle( GetCurrentProcess(), handle, GetCurrentProcess(), &handle, 0,
263                           (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle),
264                           DUP_HANDLE_SAME_ACCESS ))
265         handle = 0;
266     return handle;
267 }
268
269
270 /***********************************************************************
271  *              VxDCall0 (KERNEL32.1)
272  *              VxDCall1 (KERNEL32.2)
273  *              VxDCall2 (KERNEL32.3)
274  *              VxDCall3 (KERNEL32.4)
275  *              VxDCall4 (KERNEL32.5)
276  *              VxDCall5 (KERNEL32.6)
277  *              VxDCall6 (KERNEL32.7)
278  *              VxDCall7 (KERNEL32.8)
279  *              VxDCall8 (KERNEL32.9)
280  */
281 void WINAPI __regs_VxDCall( DWORD service, CONTEXT86 *context )
282 {
283     int i;
284     VxDCallProc proc = NULL;
285
286     RtlEnterCriticalSection( &vxd_section );
287     for (i = 0; i < NB_VXD_SERVICES; i++)
288     {
289         if (HIWORD(service) != vxd_services[i].service) continue;
290         if (!vxd_services[i].module)  /* need to load it */
291         {
292             if ((vxd_services[i].module = LoadLibraryW( vxd_services[i].name )))
293                 vxd_services[i].proc = (VxDCallProc)GetProcAddress( vxd_services[i].module, "VxDCall" );
294         }
295         proc = vxd_services[i].proc;
296         break;
297     }
298     RtlLeaveCriticalSection( &vxd_section );
299
300     if (proc) context->Eax = proc( service, context );
301     else
302     {
303         FIXME( "Unknown/unimplemented VxD (%08lx)\n", service);
304         context->Eax = 0xffffffff; /* FIXME */
305     }
306 }
307 #ifdef DEFINE_REGS_ENTRYPOINT
308 DEFINE_REGS_ENTRYPOINT( VxDCall, 4, 4 );
309 #endif
310
311
312 /***********************************************************************
313  *              OpenVxDHandle (KERNEL32.@)
314  *
315  *      This function is supposed to return the corresponding Ring 0
316  *      ("kernel") handle for a Ring 3 handle in Win9x.
317  *      Evidently, Wine will have problems with this. But we try anyway,
318  *      maybe it helps...
319  */
320 HANDLE WINAPI OpenVxDHandle(HANDLE hHandleRing3)
321 {
322     FIXME( "(%p), stub! (returning Ring 3 handle instead of Ring 0)\n", hHandleRing3);
323     return hHandleRing3;
324 }
325
326
327 /****************************************************************************
328  *              DeviceIoControl (KERNEL32.@)
329  * This is one of those big ugly nasty procedure which can do
330  * a million and one things when it comes to devices. It can also be
331  * used for VxD communication.
332  *
333  * A return value of FALSE indicates that something has gone wrong which
334  * GetLastError can decipher.
335  */
336 BOOL WINAPI DeviceIoControl(HANDLE hDevice, DWORD dwIoControlCode,
337                             LPVOID lpvInBuffer, DWORD cbInBuffer,
338                             LPVOID lpvOutBuffer, DWORD cbOutBuffer,
339                             LPDWORD lpcbBytesReturned,
340                             LPOVERLAPPED lpOverlapped)
341 {
342     NTSTATUS status;
343
344     TRACE( "(%p,%lx,%p,%ld,%p,%ld,%p,%p)\n",
345            hDevice,dwIoControlCode,lpvInBuffer,cbInBuffer,
346            lpvOutBuffer,cbOutBuffer,lpcbBytesReturned,lpOverlapped );
347
348     /* Check if this is a user defined control code for a VxD */
349
350     if( HIWORD( dwIoControlCode ) == 0 )
351     {
352         DeviceIoProc proc = get_vxd_proc( hDevice );
353         if (proc) return proc( dwIoControlCode, lpvInBuffer, cbInBuffer,
354                                lpvOutBuffer, cbOutBuffer, lpcbBytesReturned, lpOverlapped );
355         return FALSE;
356     }
357
358     /* Not a VxD, let ntdll handle it */
359
360     if (lpOverlapped)
361     {
362         status = NtDeviceIoControlFile(hDevice, lpOverlapped->hEvent,
363                                        NULL, NULL, (PIO_STATUS_BLOCK)lpOverlapped,
364                                        dwIoControlCode, lpvInBuffer, cbInBuffer,
365                                        lpvOutBuffer, cbOutBuffer);
366         if (lpcbBytesReturned) *lpcbBytesReturned = lpOverlapped->InternalHigh;
367     }
368     else
369     {
370         IO_STATUS_BLOCK iosb;
371
372         status = NtDeviceIoControlFile(hDevice, NULL, NULL, NULL, &iosb,
373                                        dwIoControlCode, lpvInBuffer, cbInBuffer,
374                                        lpvOutBuffer, cbOutBuffer);
375         if (lpcbBytesReturned) *lpcbBytesReturned = iosb.Information;
376     }
377     if (status) SetLastError( RtlNtStatusToDosError(status) );
378     return !status;
379 }