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