VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR3Lib.cpp@ 10476

Last change on this file since 10476 was 10476, checked in by vboxsync, 17 years ago

Guest Lib R3: IOCTL handling for Windows (not tested yet!) to simplify logging / communication.

  • Property svn:eol-style set to native
  • Property svn:keyword set to Id
  • Property svn:keywords set to Id
File size: 10.4 KB
Line 
1/* $Id: VBoxGuestR3Lib.cpp 10476 2008-07-10 16:09:21Z vboxsync $ */
2/** @file
3 * VBoxGuestR3Lib - Ring-3 Support Library for VirtualBox guest additions, Core.
4 */
5
6/*
7 * Copyright (C) 2007 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22
23/*******************************************************************************
24* Header Files *
25*******************************************************************************/
26#ifdef RT_OS_OS2
27# define INCL_BASE
28# define INCL_ERRORS
29# include <os2.h>
30#elif defined(RT_OS_SOLARIS) || defined(RT_OS_FREEBSD)
31# include <sys/types.h>
32# include <sys/stat.h>
33# include <errno.h>
34# include <unistd.h>
35#endif
36
37#include <iprt/time.h>
38#include <iprt/asm.h>
39#include <iprt/string.h>
40#include <iprt/file.h>
41#include <iprt/assert.h>
42#include <iprt/thread.h>
43#include <VBox/VBoxGuest.h>
44#include <VBox/log.h>
45#include "VBGLR3Internal.h"
46
47#ifdef VBOX_VBGLR3_XFREE86
48/* Rather than try to resolve all the header file conflicts, I will just
49 prototype what we need here. */
50# define XF86_O_RDWR 0x0002
51typedef void *pointer;
52extern "C" int xf86open(const char*, int,...);
53extern "C" int xf86close(int);
54extern "C" int xf86ioctl(int, unsigned long, pointer);
55#endif
56
57/*******************************************************************************
58* Global Variables *
59*******************************************************************************/
60/** The VBoxGuest device handle. */
61#ifdef VBOX_VBGLR3_XFREE86
62static int g_File = -1;
63#elif defined(RT_OS_WINDOWS)
64static HANDLE g_hFile = INVALID_HANDLE_VALUE;
65#else
66static RTFILE g_File = NIL_RTFILE;
67#endif
68/** User counter.
69 * A counter of the number of times the library has been initialised, for use with
70 * X.org drivers, where the library may be shared by multiple independant modules
71 * inside a single process space.
72 */
73static uint32_t volatile g_cInits = 0;
74
75
76VBGLR3DECL(int) VbglR3Init(void)
77{
78 uint32_t cInits = ASMAtomicIncU32(&g_cInits);
79#ifndef VBOX_VBGLR3_XFREE86
80 Assert(cInits > 0);
81#endif
82 if (cInits > 1)
83 {
84 /*
85 * This will fail if two (or more) threads race each other calling VbglR3Init.
86 * However it will work fine for single threaded or otherwise serialized
87 * processed calling us more than once.
88 */
89#ifndef RT_OS_WINDOWS
90 #ifndef VBOX_VBGLR3_XFREE86
91 if (g_File == NIL_RTFILE)
92 #else
93 if (g_File == -1)
94 #endif
95 return VERR_INTERNAL_ERROR;
96#else
97 if (g_hFile == NULL)
98 return VERR_INTERNAL_ERROR;
99#endif
100 return VINF_SUCCESS;
101 }
102
103#ifndef RT_OS_WINDOWS
104 #ifndef VBOX_VBGLR3_XFREE86
105 if (g_File != NIL_RTFILE)
106 #else
107 if (g_File != -1)
108 #endif
109 return VERR_INTERNAL_ERROR;
110#else
111 if (g_hFile != INVALID_HANDLE_VALUE)
112 return VERR_INTERNAL_ERROR;
113#endif
114
115#if defined(RT_OS_OS2)
116 /*
117 * We might wish to compile this with Watcom, so stick to
118 * the OS/2 APIs all the way. And in any case we have to use
119 * DosDevIOCtl for the requests, why not use Dos* for everything.
120 */
121 HFILE hf = NULLHANDLE;
122 ULONG ulAction = 0;
123 APIRET rc = DosOpen((PCSZ)VBOXGUEST_DEVICE_NAME, &hf, &ulAction, 0, FILE_NORMAL,
124 OPEN_ACTION_OPEN_IF_EXISTS,
125 OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NOINHERIT | OPEN_SHARE_DENYNONE | OPEN_ACCESS_READWRITE,
126 NULL);
127 if (rc)
128 return RTErrConvertFromOS2(rc);
129
130 if (hf < 16)
131 {
132 HFILE ahfs[16];
133 unsigned i;
134 for (i = 0; i < RT_ELEMENTS(ahfs); i++)
135 {
136 ahfs[i] = 0xffffffff;
137 rc = DosDupHandle(hf, &ahfs[i]);
138 if (rc)
139 break;
140 }
141
142 if (i-- > 1)
143 {
144 ULONG fulState = 0;
145 rc = DosQueryFHState(ahfs[i], &fulState);
146 if (!rc)
147 {
148 fulState |= OPEN_FLAGS_NOINHERIT;
149 fulState &= OPEN_FLAGS_WRITE_THROUGH | OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NO_CACHE | OPEN_FLAGS_NOINHERIT; /* Turn off non-participating bits. */
150 rc = DosSetFHState(ahfs[i], fulState);
151 }
152 if (!rc)
153 {
154 rc = DosClose(hf);
155 AssertMsg(!rc, ("%ld\n", rc));
156 hf = ahfs[i];
157 }
158 else
159 i++;
160 while (i-- > 0)
161 DosClose(ahfs[i]);
162 }
163 }
164 g_File = hf;
165
166#elif defined(VBOX_VBGLR3_XFREE86) && !defined(RT_OS_FREEBSD)
167 int File = xf86open(VBOXGUEST_DEVICE_NAME, XF86_O_RDWR);
168 if (File == -1)
169 return VERR_OPEN_FAILED;
170 g_File = File;
171
172#elif defined(RT_OS_FREEBSD)
173 /*
174 * Try open the BSD device.
175 */
176# if defined(VBOX_VBGLR3_XFREE86)
177 int File = 0;
178# else
179 RTFILE File = 0;
180# endif
181 int rc;
182 char szDevice[sizeof(VBOXGUEST_DEVICE_NAME) + 16];
183 for (unsigned iUnit = 0; iUnit < 1024; iUnit++)
184 {
185 RTStrPrintf(szDevice, sizeof(szDevice), VBOXGUEST_DEVICE_NAME "%d", iUnit);
186# if defined(VBOX_VBGLR3_XFREE86)
187 File = xf86open(szDevice, XF86_O_RDWR);
188 if (File >= 0)
189 break;
190# else
191 rc = RTFileOpen(&File, szDevice, RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
192 if (RT_SUCCESS(rc))
193 break;
194# endif
195 }
196
197# if defined(VBOX_VBGLR3_XFREE86)
198 if (File == -1)
199 return VERR_OPEN_FAILED;
200# else
201 if (RT_FAILURE(rc))
202 return rc;
203# endif
204
205 g_File = File;
206
207#elif defined(RT_OS_WINDOWS)
208
209 /*
210 * Open a handle to the shadow device.
211 */
212 g_hFile = CreateFile(VBOXGUEST_DEVICE_NAME,
213 GENERIC_READ | GENERIC_WRITE,
214 FILE_SHARE_READ | FILE_SHARE_WRITE,
215 NULL,
216 OPEN_EXISTING,
217 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
218 NULL);
219
220 if (g_hFile == INVALID_HANDLE_VALUE)
221 return VERR_OPEN_FAILED;
222
223#else
224
225 /* The default implemenation. (linux, solaris) */
226 RTFILE File;
227 int rc = RTFileOpen(&File, VBOXGUEST_DEVICE_NAME, RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
228 if (RT_FAILURE(rc))
229 return rc;
230 g_File = File;
231
232#endif
233
234 /*
235 * Create release logger
236 */
237 PRTLOGGER pReleaseLogger;
238 static const char * const s_apszGroups[] = VBOX_LOGGROUP_NAMES;
239 int rc2 = RTLogCreate(&pReleaseLogger, 0, "all", "VBOX_RELEASE_LOG",
240 RT_ELEMENTS(s_apszGroups), &s_apszGroups[0],
241 RTLOGDEST_USER, NULL);
242 /* This may legitimately fail if we are using the mini-runtime. */
243 if (RT_SUCCESS(rc2))
244 RTLogRelSetDefaultInstance(pReleaseLogger);
245
246 return VINF_SUCCESS;
247}
248
249
250VBGLR3DECL(void) VbglR3Term(void)
251{
252 uint32_t cInits = ASMAtomicDecU32(&g_cInits);
253 if (cInits > 0)
254 return;
255#ifndef RT_OS_WINDOWS
256 #ifndef VBOX_VBGLR3_XFREE86
257 AssertReturnVoid(!cInits);
258 RTFILE File = g_File;
259 g_File = NIL_RTFILE;
260 AssertReturnVoid(File != NIL_RTFILE);
261 #else
262 int File = g_File;
263 g_File = -1;
264 if (File == -1)
265 return;
266 #endif
267#else
268 /* Nothing to do here yet. */
269
270#endif
271
272#if defined(RT_OS_OS2)
273 APIRET rc = DosClose(File);
274 AssertMsg(!rc, ("%ld\n", rc));
275
276#elif defined(VBOX_VBGLR3_XFREE86)
277 xf86close(File);
278 File = -1;
279
280#elif defined(RT_OS_WINDOWS)
281 if (INVALID_HANDLE_VALUE != g_hFile)
282 {
283 CloseHandle (g_hFile);
284 g_hFile = INVALID_HANDLE_VALUE;
285 }
286
287#else
288 int rc = RTFileClose(File);
289 AssertRC(rc);
290#endif
291}
292
293
294/**
295 * Internal wrapper around various OS specific ioctl implemenations.
296 *
297 * @returns VBox status code as returned by VBoxGuestCommonIOCtl, or
298 * an failure returned by the OS specific ioctl APIs.
299 *
300 * @param iFunction The requested function.
301 * @param pvData The input and output data buffer.
302 * @param cbData The size of the buffer.
303 *
304 * @remark Exactly how the VBoxGuestCommonIOCtl is ferried back
305 * here is OS specific. On BSD and Darwin we can use errno,
306 * while on OS/2 we use the 2nd buffer of the IOCtl.
307 */
308int vbglR3DoIOCtl(unsigned iFunction, void *pvData, size_t cbData)
309{
310#ifdef RT_OS_OS2
311 ULONG cbOS2Parm = cbData;
312 int32_t vrc = VERR_INTERNAL_ERROR;
313 ULONG cbOS2Data = sizeof(vrc);
314 APIRET rc = DosDevIOCtl(g_File, VBOXGUEST_IOCTL_CATEGORY, iFunction,
315 pvData, cbData, &cbOS2Parm,
316 &vrc, sizeof(vrc), &cbOS2Data);
317 if (RT_LIKELY(!rc))
318 return vrc;
319 return RTErrConvertFromOS2(rc);
320
321#elif defined(RT_OS_SOLARIS) || defined(RT_OS_FREEBSD)
322 VBGLBIGREQ Hdr;
323 Hdr.u32Magic = VBGLBIGREQ_MAGIC;
324 Hdr.cbData = cbData;
325 Hdr.pvDataR3 = pvData;
326
327/** @todo test status code passing! */
328 int rc = ioctl((int)g_File, iFunction, &Hdr);
329 if (rc == -1)
330 {
331 rc = errno;
332 return RTErrConvertFromErrno(rc);
333 }
334 return VINF_SUCCESS;
335
336#elif defined(RT_OS_WINDOWS)
337
338 DWORD cbReturned = 0;
339 if (FALSE == DeviceIoControl(g_hFile, iFunction, pvData, cbData,
340 pvData, cbData, &cbReturned, NULL))
341 return GetLastError(); /* @todo Add better error lookup. */
342
343 return VINF_SUCCESS;
344
345#elif defined(VBOX_VBGLR3_XFREE86)
346 /* PORTME - This is preferred over the RTFileIOCtl variant below, just be careful with the (int). */
347/** @todo test status code passing! */
348 int rc = xf86ioctl(g_File, iFunction, pvData);
349 if (rc == -1)
350 return VERR_FILE_IO_ERROR; /* This is purely legacy stuff, it has to work and no more. */
351 return VINF_SUCCESS;
352
353#else
354 /* Default implementation - PORTME: Do not use this without testings that passing errors works! */
355/** @todo test status code passing! */
356 int rc2 = VERR_INTERNAL_ERROR;
357 int rc = RTFileIoCtl(g_File, (int)iFunction, pvData, cbData, &rc2);
358 if (RT_SUCCESS(rc))
359 rc = rc2;
360 return rc;
361#endif
362}
363
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette