VirtualBox

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

Last change on this file since 38114 was 37596, checked in by vboxsync, 13 years ago

*: RTFILE becomes a pointer, RTFileOpen++ expands it's flags paramter from uint32_t to uint64_t.

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

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