server: Make timeout status for async I/O specifiable. Fix mailslots timeout handling.
[wine] / dlls / kernel32 / 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 "winerror.h"
40 #include "winternl.h"
41 #include "winioctl.h"
42 #include "kernel_private.h"
43 #include "wine/library.h"
44 #include "wine/unicode.h"
45 #include "wine/server.h"
46 #include "wine/debug.h"
47
48 WINE_DEFAULT_DEBUG_CHANNEL(vxd);
49
50 typedef BOOL (WINAPI *DeviceIoProc)(DWORD, LPVOID, DWORD, LPVOID, DWORD, LPDWORD, LPOVERLAPPED);
51 typedef DWORD (WINAPI *VxDCallProc)(DWORD, CONTEXT86 *);
52
53 struct vxd_module
54 {
55     LARGE_INTEGER index;
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, { (DWORD_PTR)(__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     DeviceIoProc ret = NULL;
137     int status, i;
138     IO_STATUS_BLOCK io;
139     FILE_INTERNAL_INFORMATION info;
140
141     status = NtQueryInformationFile( handle, &io, &info, sizeof(info), FileInternalInformation );
142     if (status)
143     {
144         SetLastError( RtlNtStatusToDosError(status) );
145         return NULL;
146     }
147
148     RtlEnterCriticalSection( &vxd_section );
149
150     for (i = 0; i < MAX_VXD_MODULES; i++)
151     {
152         if (!vxd_modules[i].module) break;
153         if (vxd_modules[i].index.QuadPart == info.IndexNumber.QuadPart)
154         {
155             if (!(ret = vxd_modules[i].proc)) SetLastError( ERROR_INVALID_FUNCTION );
156             goto done;
157         }
158     }
159     /* FIXME: Here we could go through the directory to find the VxD name and load it. */
160     /* Let's wait to find out if there are actually apps out there that try to share   */
161     /* VxD handles between processes, before we go to the trouble of implementing it.  */
162     ERR( "handle %p not found in module list, inherited from another process?\n", handle );
163
164 done:
165     RtlLeaveCriticalSection( &vxd_section );
166     return ret;
167 }
168
169
170 /* load a VxD and return a file handle to it */
171 HANDLE VXD_Open( LPCWSTR filenameW, DWORD access, SECURITY_ATTRIBUTES *sa )
172 {
173     static const WCHAR dotVxDW[] = {'.','v','x','d',0};
174     int i;
175     HANDLE handle;
176     HMODULE module;
177     WCHAR *p, name[16];
178
179     if (!(GetVersion() & 0x80000000))  /* there are no VxDs on NT */
180     {
181         SetLastError( ERROR_FILE_NOT_FOUND );
182         return 0;
183     }
184
185     /* normalize the filename */
186
187     if (strlenW( filenameW ) >= sizeof(name)/sizeof(WCHAR) - 4 ||
188         strchrW( filenameW, '/' ) || strchrW( filenameW, '\\' ))
189     {
190         SetLastError( ERROR_FILE_NOT_FOUND );
191         return 0;
192     }
193     strcpyW( name, filenameW );
194     strlwrW( name );
195     p = strchrW( name, '.' );
196     if (!p) strcatW( name, dotVxDW );
197     else if (strcmpiW( p, dotVxDW ))  /* existing extension has to be .vxd */
198     {
199         SetLastError( ERROR_FILE_NOT_FOUND );
200         return 0;
201     }
202
203     /* try to load the module first */
204
205     if (!(module = LoadLibraryW( name )))
206     {
207         FIXME( "Unknown/unsupported VxD %s. Try setting Windows version to 'nt40' or 'win31'.\n",
208                debugstr_w(name) );
209         SetLastError( ERROR_FILE_NOT_FOUND );
210         return 0;
211     }
212
213     /* register the module in the global list if necessary */
214
215     RtlEnterCriticalSection( &vxd_section );
216
217     for (i = 0; i < MAX_VXD_MODULES; i++)
218     {
219         if (vxd_modules[i].module == module)
220         {
221             handle = vxd_modules[i].handle;
222             goto done;  /* already registered */
223         }
224         if (!vxd_modules[i].module)  /* new one, register it */
225         {
226             IO_STATUS_BLOCK io;
227             FILE_INTERNAL_INFORMATION info;
228
229             /* get a file handle to the dummy file */
230             if (!(handle = open_vxd_handle( name )))
231             {
232                 FreeLibrary( module );
233                 goto done;
234             }
235             if (!NtQueryInformationFile( handle, &io, &info, sizeof(info), FileInternalInformation ))
236                 vxd_modules[i].index = info.IndexNumber;
237
238             vxd_modules[i].module = module;
239             vxd_modules[i].handle = handle;
240             vxd_modules[i].proc = (DeviceIoProc)GetProcAddress( module, "DeviceIoControl" );
241             goto done;
242         }
243     }
244
245     ERR("too many open VxD modules, please report\n" );
246     FreeLibrary( module );
247     handle = 0;
248
249 done:
250     RtlLeaveCriticalSection( &vxd_section );
251     if (!DuplicateHandle( GetCurrentProcess(), handle, GetCurrentProcess(), &handle, 0,
252                           (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle),
253                           DUP_HANDLE_SAME_ACCESS ))
254         handle = 0;
255     return handle;
256 }
257
258
259 /***********************************************************************
260  *              VxDCall0 (KERNEL32.1)
261  *              VxDCall1 (KERNEL32.2)
262  *              VxDCall2 (KERNEL32.3)
263  *              VxDCall3 (KERNEL32.4)
264  *              VxDCall4 (KERNEL32.5)
265  *              VxDCall5 (KERNEL32.6)
266  *              VxDCall6 (KERNEL32.7)
267  *              VxDCall7 (KERNEL32.8)
268  *              VxDCall8 (KERNEL32.9)
269  */
270 void WINAPI __regs_VxDCall( DWORD service, CONTEXT86 *context )
271 {
272     int i;
273     VxDCallProc proc = NULL;
274
275     RtlEnterCriticalSection( &vxd_section );
276     for (i = 0; i < NB_VXD_SERVICES; i++)
277     {
278         if (HIWORD(service) != vxd_services[i].service) continue;
279         if (!vxd_services[i].module)  /* need to load it */
280         {
281             if ((vxd_services[i].module = LoadLibraryW( vxd_services[i].name )))
282                 vxd_services[i].proc = (VxDCallProc)GetProcAddress( vxd_services[i].module, "VxDCall" );
283         }
284         proc = vxd_services[i].proc;
285         break;
286     }
287     RtlLeaveCriticalSection( &vxd_section );
288
289     if (proc) context->Eax = proc( service, context );
290     else
291     {
292         FIXME( "Unknown/unimplemented VxD (%08x)\n", service);
293         context->Eax = 0xffffffff; /* FIXME */
294     }
295 }
296 #ifdef DEFINE_REGS_ENTRYPOINT
297 DEFINE_REGS_ENTRYPOINT( VxDCall, 4, 4 );
298 #endif
299
300
301 /***********************************************************************
302  *              OpenVxDHandle (KERNEL32.@)
303  *
304  *      This function is supposed to return the corresponding Ring 0
305  *      ("kernel") handle for a Ring 3 handle in Win9x.
306  *      Evidently, Wine will have problems with this. But we try anyway,
307  *      maybe it helps...
308  */
309 HANDLE WINAPI OpenVxDHandle(HANDLE hHandleRing3)
310 {
311     FIXME( "(%p), stub! (returning Ring 3 handle instead of Ring 0)\n", hHandleRing3);
312     return hHandleRing3;
313 }
314
315
316 /****************************************************************************
317  *              DeviceIoControl (KERNEL32.@)
318  * This is one of those big ugly nasty procedure which can do
319  * a million and one things when it comes to devices. It can also be
320  * used for VxD communication.
321  *
322  * A return value of FALSE indicates that something has gone wrong which
323  * GetLastError can decipher.
324  */
325 BOOL WINAPI DeviceIoControl(HANDLE hDevice, DWORD dwIoControlCode,
326                             LPVOID lpvInBuffer, DWORD cbInBuffer,
327                             LPVOID lpvOutBuffer, DWORD cbOutBuffer,
328                             LPDWORD lpcbBytesReturned,
329                             LPOVERLAPPED lpOverlapped)
330 {
331     NTSTATUS status;
332
333     TRACE( "(%p,%x,%p,%d,%p,%d,%p,%p)\n",
334            hDevice,dwIoControlCode,lpvInBuffer,cbInBuffer,
335            lpvOutBuffer,cbOutBuffer,lpcbBytesReturned,lpOverlapped );
336
337     /* Check if this is a user defined control code for a VxD */
338
339     if( HIWORD( dwIoControlCode ) == 0 )
340     {
341         DeviceIoProc proc = get_vxd_proc( hDevice );
342         if (proc) return proc( dwIoControlCode, lpvInBuffer, cbInBuffer,
343                                lpvOutBuffer, cbOutBuffer, lpcbBytesReturned, lpOverlapped );
344         return FALSE;
345     }
346
347     /* Not a VxD, let ntdll handle it */
348
349     if (lpOverlapped)
350     {
351         if (HIWORD(dwIoControlCode) == FILE_DEVICE_FILE_SYSTEM)
352             status = NtFsControlFile(hDevice, lpOverlapped->hEvent,
353                                      NULL, NULL, (PIO_STATUS_BLOCK)lpOverlapped,
354                                      dwIoControlCode, lpvInBuffer, cbInBuffer,
355                                      lpvOutBuffer, cbOutBuffer);
356         else
357             status = NtDeviceIoControlFile(hDevice, lpOverlapped->hEvent,
358                                            NULL, NULL, (PIO_STATUS_BLOCK)lpOverlapped,
359                                            dwIoControlCode, lpvInBuffer, cbInBuffer,
360                                            lpvOutBuffer, cbOutBuffer);
361         if (lpcbBytesReturned) *lpcbBytesReturned = lpOverlapped->InternalHigh;
362     }
363     else
364     {
365         IO_STATUS_BLOCK iosb;
366
367         if (HIWORD(dwIoControlCode) == FILE_DEVICE_FILE_SYSTEM)
368             status = NtFsControlFile(hDevice, NULL, NULL, NULL, &iosb,
369                                      dwIoControlCode, lpvInBuffer, cbInBuffer,
370                                      lpvOutBuffer, cbOutBuffer);
371         else
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 }