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