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