VirtualBox

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

Last change on this file since 26131 was 26054, checked in by vboxsync, 15 years ago

Additions/x11/vboxvideo: use vboxvideo_70 for the XFree86 driver too

  • Property svn:eol-style set to native
  • Property svn:keyword set to Id
  • Property svn:keywords set to Id
File size: 11.7 KB
Line 
1/* $Id: VBoxGuestR3Lib.cpp 26054 2010-01-26 20:46:38Z 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#if defined(RT_OS_WINDOWS)
27# include <Windows.h>
28
29#elif defined(RT_OS_OS2)
30# define INCL_BASE
31# define INCL_ERRORS
32# include <os2.h>
33
34#elif defined(RT_OS_FREEBSD) \
35 || defined(RT_OS_LINUX) \
36 || defined(RT_OS_SOLARIS)
37# include <sys/types.h>
38# include <sys/stat.h>
39# if defined(RT_OS_LINUX) /** @todo check this on solaris+freebsd as well. */
40# include <sys/ioctl.h>
41# endif
42# include <errno.h>
43# include <unistd.h>
44#endif
45
46#include <iprt/assert.h>
47#include <iprt/asm.h>
48#include <iprt/file.h>
49#include <iprt/time.h>
50#include <iprt/string.h>
51#include <iprt/thread.h>
52#include <VBox/log.h>
53#include "VBGLR3Internal.h"
54
55#ifdef VBOX_VBGLR3_XFREE86
56/* Rather than try to resolve all the header file conflicts, I will just
57 prototype what we need here. */
58# define XF86_O_RDWR 0x0002
59typedef void *pointer;
60extern "C" int xf86open(const char *, int, ...);
61extern "C" int xf86close(int);
62extern "C" int xf86ioctl(int, unsigned long, pointer);
63#endif
64
65
66/*******************************************************************************
67* Global Variables *
68*******************************************************************************/
69/** The VBoxGuest device handle. */
70#ifdef VBOX_VBGLR3_XFREE86
71static int g_File = -1;
72#elif defined(RT_OS_WINDOWS)
73static HANDLE g_hFile = INVALID_HANDLE_VALUE;
74#else
75static RTFILE g_File = NIL_RTFILE;
76#endif
77/** User counter.
78 * A counter of the number of times the library has been initialised, for use with
79 * X.org drivers, where the library may be shared by multiple independant modules
80 * inside a single process space.
81 */
82static uint32_t volatile g_cInits = 0;
83
84
85
86/**
87 * Implementation of VbglR3Init and VbglR3InitUser
88 */
89static int vbglR3Init(const char *pszDeviceName)
90{
91 uint32_t cInits = ASMAtomicIncU32(&g_cInits);
92#ifndef VBOX_VBGLR3_XFREE86
93 Assert(cInits > 0);
94#endif
95 if (cInits > 1)
96 {
97 /*
98 * This will fail if two (or more) threads race each other calling VbglR3Init.
99 * However it will work fine for single threaded or otherwise serialized
100 * processed calling us more than once.
101 */
102#ifdef RT_OS_WINDOWS
103 if (g_hFile == INVALID_HANDLE_VALUE)
104#elif !defined (VBOX_VBGLR3_XFREE86)
105 if (g_File == NIL_RTFILE)
106#else
107 if (g_File == -1)
108#endif
109 return VERR_INTERNAL_ERROR;
110 return VINF_SUCCESS;
111 }
112#if defined(RT_OS_WINDOWS)
113 if (g_hFile != INVALID_HANDLE_VALUE)
114#elif !defined(VBOX_VBGLR3_XFREE86)
115 if (g_File != NIL_RTFILE)
116#else
117 if (g_File != -1)
118#endif
119 return VERR_INTERNAL_ERROR;
120
121#if defined(RT_OS_WINDOWS)
122 /*
123 * Have to use CreateFile here as we want to specify FILE_FLAG_OVERLAPPED
124 * and possible some other bits not availble thru iprt/file.h.
125 */
126 HANDLE hFile = CreateFile(pszDeviceName,
127 GENERIC_READ | GENERIC_WRITE,
128 FILE_SHARE_READ | FILE_SHARE_WRITE,
129 NULL,
130 OPEN_EXISTING,
131 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
132 NULL);
133
134 if (hFile == INVALID_HANDLE_VALUE)
135 return VERR_OPEN_FAILED;
136 g_hFile = hFile;
137
138#elif defined(RT_OS_OS2)
139 /*
140 * We might wish to compile this with Watcom, so stick to
141 * the OS/2 APIs all the way. And in any case we have to use
142 * DosDevIOCtl for the requests, why not use Dos* for everything.
143 */
144 HFILE hf = NULLHANDLE;
145 ULONG ulAction = 0;
146 APIRET rc = DosOpen((PCSZ)pszDeviceName, &hf, &ulAction, 0, FILE_NORMAL,
147 OPEN_ACTION_OPEN_IF_EXISTS,
148 OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NOINHERIT | OPEN_SHARE_DENYNONE | OPEN_ACCESS_READWRITE,
149 NULL);
150 if (rc)
151 return RTErrConvertFromOS2(rc);
152
153 if (hf < 16)
154 {
155 HFILE ahfs[16];
156 unsigned i;
157 for (i = 0; i < RT_ELEMENTS(ahfs); i++)
158 {
159 ahfs[i] = 0xffffffff;
160 rc = DosDupHandle(hf, &ahfs[i]);
161 if (rc)
162 break;
163 }
164
165 if (i-- > 1)
166 {
167 ULONG fulState = 0;
168 rc = DosQueryFHState(ahfs[i], &fulState);
169 if (!rc)
170 {
171 fulState |= OPEN_FLAGS_NOINHERIT;
172 fulState &= OPEN_FLAGS_WRITE_THROUGH | OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NO_CACHE | OPEN_FLAGS_NOINHERIT; /* Turn off non-participating bits. */
173 rc = DosSetFHState(ahfs[i], fulState);
174 }
175 if (!rc)
176 {
177 rc = DosClose(hf);
178 AssertMsg(!rc, ("%ld\n", rc));
179 hf = ahfs[i];
180 }
181 else
182 i++;
183 while (i-- > 0)
184 DosClose(ahfs[i]);
185 }
186 }
187 g_File = hf;
188
189#elif defined(VBOX_VBGLR3_XFREE86)
190 int File = xf86open(pszDeviceName, XF86_O_RDWR);
191 if (File == -1)
192 return VERR_OPEN_FAILED;
193 g_File = File;
194
195#else
196
197 /* The default implemenation. (linux, solaris, freebsd) */
198 RTFILE File;
199 int rc = RTFileOpen(&File, pszDeviceName, RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
200 if (RT_FAILURE(rc))
201 return rc;
202 g_File = File;
203
204#endif
205
206#ifndef VBOX_VBGLR3_XFREE86
207 /*
208 * Create release logger
209 */
210 PRTLOGGER pReleaseLogger;
211 static const char * const s_apszGroups[] = VBOX_LOGGROUP_NAMES;
212 int rc2 = RTLogCreate(&pReleaseLogger, 0, "all", "VBOX_RELEASE_LOG",
213 RT_ELEMENTS(s_apszGroups), &s_apszGroups[0],
214 RTLOGDEST_USER, NULL);
215 /* This may legitimately fail if we are using the mini-runtime. */
216 if (RT_SUCCESS(rc2))
217 RTLogRelSetDefaultInstance(pReleaseLogger);
218#endif
219
220 return VINF_SUCCESS;
221}
222
223
224/**
225 * Open the VBox R3 Guest Library. This should be called by system daemons
226 * and processes.
227 */
228VBGLR3DECL(int) VbglR3Init(void)
229{
230 return vbglR3Init(VBOXGUEST_DEVICE_NAME);
231}
232
233
234/**
235 * Open the VBox R3 Guest Library. Equivalent to VbglR3Init, but for user
236 * session processes.
237 */
238VBGLR3DECL(int) VbglR3InitUser(void)
239{
240 return vbglR3Init(VBOXGUEST_USER_DEVICE_NAME);
241}
242
243
244VBGLR3DECL(void) VbglR3Term(void)
245{
246 /*
247 * Decrement the reference count and see if we're the last one out.
248 */
249 uint32_t cInits = ASMAtomicDecU32(&g_cInits);
250 if (cInits > 0)
251 return;
252#if !defined(VBOX_VBGLR3_XFREE86)
253 AssertReturnVoid(!cInits);
254
255# if defined(RT_OS_WINDOWS)
256 HANDLE hFile = g_hFile;
257 g_hFile = INVALID_HANDLE_VALUE;
258 AssertReturnVoid(hFile != INVALID_HANDLE_VALUE);
259 BOOL fRc = CloseHandle(hFile);
260 Assert(fRc); NOREF(fRc);
261
262# elif defined(RT_OS_OS2)
263
264 RTFILE File = g_File;
265 g_File = NIL_RTFILE;
266 AssertReturnVoid(File != NIL_RTFILE);
267 APIRET rc = DosClose(File);
268 AssertMsg(!rc, ("%ld\n", rc));
269
270# else /* The IPRT case. */
271 RTFILE File = g_File;
272 g_File = NIL_RTFILE;
273 AssertReturnVoid(File != NIL_RTFILE);
274 int rc = RTFileClose(File);
275 AssertRC(rc);
276# endif
277
278#else /* VBOX_VBGLR3_XFREE86 */
279 int File = g_File;
280 g_File = -1;
281 if (File == -1)
282 return;
283 xf86close(File);
284#endif /* VBOX_VBGLR3_XFREE86 */
285}
286
287
288/**
289 * Internal wrapper around various OS specific ioctl implemenations.
290 *
291 * @returns VBox status code as returned by VBoxGuestCommonIOCtl, or
292 * an failure returned by the OS specific ioctl APIs.
293 *
294 * @param iFunction The requested function.
295 * @param pvData The input and output data buffer.
296 * @param cbData The size of the buffer.
297 *
298 * @remark Exactly how the VBoxGuestCommonIOCtl is ferried back
299 * here is OS specific. On BSD and Darwin we can use errno,
300 * while on OS/2 we use the 2nd buffer of the IOCtl.
301 */
302int vbglR3DoIOCtl(unsigned iFunction, void *pvData, size_t cbData)
303{
304#if defined(RT_OS_WINDOWS)
305 DWORD cbReturned = 0;
306 if (!DeviceIoControl(g_hFile, iFunction, pvData, (DWORD)cbData, pvData, (DWORD)cbData, &cbReturned, NULL))
307 {
308/** @todo The passing of error codes needs to be tested and fixed (as does *all* the other hosts except for
309 * OS/2). The idea is that the VBox status codes in ring-0 should be transfered without loss down to
310 * ring-3. However, it's not vitally important right now (obviously, since the other guys has been
311 * ignoring it for 1+ years now). On Linux and Solaris the transfer is done, but it is currently not
312 * lossless, so still needs fixing. */
313 DWORD LastErr = GetLastError();
314 return RTErrConvertFromWin32(LastErr);
315 }
316
317 return VINF_SUCCESS;
318
319#elif defined(RT_OS_OS2)
320 ULONG cbOS2Parm = cbData;
321 int32_t vrc = VERR_INTERNAL_ERROR;
322 ULONG cbOS2Data = sizeof(vrc);
323 APIRET rc = DosDevIOCtl(g_File, VBOXGUEST_IOCTL_CATEGORY, iFunction,
324 pvData, cbData, &cbOS2Parm,
325 &vrc, sizeof(vrc), &cbOS2Data);
326 if (RT_LIKELY(!rc))
327 return vrc;
328 return RTErrConvertFromOS2(rc);
329
330#elif defined(RT_OS_SOLARIS) || defined(RT_OS_FREEBSD)
331 VBGLBIGREQ Hdr;
332 Hdr.u32Magic = VBGLBIGREQ_MAGIC;
333 Hdr.cbData = cbData;
334 Hdr.pvDataR3 = pvData;
335# if HC_ARCH_BITS == 32
336 Hdr.u32Padding = 0;
337# endif
338
339/** @todo test status code passing! Check that the kernel doesn't do any
340 * error checks using specific errno values, and just pass an VBox
341 * error instead of an errno.h one. Alternatively, extend/redefine the
342 * header with an error code return field (much better alternative
343 * actually). */
344 int rc = ioctl((int)g_File, iFunction, &Hdr);
345 if (rc == -1)
346 {
347 rc = errno;
348 return RTErrConvertFromErrno(rc);
349 }
350 return VINF_SUCCESS;
351
352#elif defined(RT_OS_LINUX)
353# ifdef VBOX_VBGLR3_XFREE86
354 int rc = xf86ioctl((int)g_File, iFunction, pvData);
355# else
356 int rc = ioctl((int)g_File, iFunction, pvData);
357# endif
358 if (RT_LIKELY(rc == 0))
359 return VINF_SUCCESS;
360
361 /* Positive values are negated VBox error status codes. */
362 if (rc > 0)
363 rc = -rc;
364 else
365# ifdef VBOX_VBGLR3_XFREE86
366 rc = VERR_FILE_IO_ERROR;
367# else
368 rc = RTErrConvertFromErrno(errno);
369# endif
370 NOREF(cbData);
371 return rc;
372
373#elif defined(VBOX_VBGLR3_XFREE86)
374 /* PORTME - This is preferred over the RTFileIOCtl variant below, just be careful with the (int). */
375/** @todo test status code passing! */
376 int rc = xf86ioctl(g_File, iFunction, pvData);
377 if (rc == -1)
378 return VERR_FILE_IO_ERROR; /* This is purely legacy stuff, it has to work and no more. */
379 return VINF_SUCCESS;
380
381#else
382 /* Default implementation - PORTME: Do not use this without testings that passing errors works! */
383/** @todo test status code passing! */
384 int rc2 = VERR_INTERNAL_ERROR;
385 int rc = RTFileIoCtl(g_File, (int)iFunction, pvData, cbData, &rc2);
386 if (RT_SUCCESS(rc))
387 rc = rc2;
388 return rc;
389#endif
390}
391
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