VirtualBox

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

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

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keyword set to Id
  • Property svn:keywords set to Id
File size: 11.9 KB
Line 
1/* $Id: VBoxGuestR3Lib.cpp 28800 2010-04-27 08:22:32Z 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 independant 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 availble 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 = 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 implemenation. (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],
217 RTLOGDEST_USER, NULL);
218 /* This may legitimately fail if we are using the mini-runtime. */
219 if (RT_SUCCESS(rc2))
220 RTLogRelSetDefaultInstance(pReleaseLogger);
221#endif
222
223 return VINF_SUCCESS;
224}
225
226
227/**
228 * Open the VBox R3 Guest Library. This should be called by system daemons
229 * and processes.
230 */
231VBGLR3DECL(int) VbglR3Init(void)
232{
233 return vbglR3Init(VBOXGUEST_DEVICE_NAME);
234}
235
236
237/**
238 * Open the VBox R3 Guest Library. Equivalent to VbglR3Init, but for user
239 * session processes.
240 */
241VBGLR3DECL(int) VbglR3InitUser(void)
242{
243 return vbglR3Init(VBOXGUEST_USER_DEVICE_NAME);
244}
245
246
247VBGLR3DECL(void) VbglR3Term(void)
248{
249 /*
250 * Decrement the reference count and see if we're the last one out.
251 */
252 uint32_t cInits = ASMAtomicDecU32(&g_cInits);
253 if (cInits > 0)
254 return;
255#if !defined(VBOX_VBGLR3_XFREE86)
256 AssertReturnVoid(!cInits);
257
258# if defined(RT_OS_WINDOWS)
259 HANDLE hFile = g_hFile;
260 g_hFile = INVALID_HANDLE_VALUE;
261 AssertReturnVoid(hFile != INVALID_HANDLE_VALUE);
262 BOOL fRc = CloseHandle(hFile);
263 Assert(fRc); NOREF(fRc);
264
265# elif defined(RT_OS_OS2)
266
267 RTFILE File = g_File;
268 g_File = NIL_RTFILE;
269 AssertReturnVoid(File != NIL_RTFILE);
270 APIRET rc = DosClose(File);
271 AssertMsg(!rc, ("%ld\n", rc));
272
273# else /* The IPRT case. */
274 RTFILE File = g_File;
275 g_File = NIL_RTFILE;
276 AssertReturnVoid(File != NIL_RTFILE);
277 int rc = RTFileClose(File);
278 AssertRC(rc);
279# endif
280
281#else /* VBOX_VBGLR3_XFREE86 */
282 int File = g_File;
283 g_File = -1;
284 if (File == -1)
285 return;
286 xf86close(File);
287#endif /* VBOX_VBGLR3_XFREE86 */
288}
289
290
291/**
292 * Internal wrapper around various OS specific ioctl implemenations.
293 *
294 * @returns VBox status code as returned by VBoxGuestCommonIOCtl, or
295 * an failure returned by the OS specific ioctl APIs.
296 *
297 * @param iFunction The requested function.
298 * @param pvData The input and output data buffer.
299 * @param cbData The size of the buffer.
300 *
301 * @remark Exactly how the VBoxGuestCommonIOCtl is ferried back
302 * here is OS specific. On BSD and Darwin we can use errno,
303 * while on OS/2 we use the 2nd buffer of the IOCtl.
304 */
305int vbglR3DoIOCtl(unsigned iFunction, void *pvData, size_t cbData)
306{
307#if defined(RT_OS_WINDOWS)
308 DWORD cbReturned = 0;
309 if (!DeviceIoControl(g_hFile, iFunction, pvData, (DWORD)cbData, pvData, (DWORD)cbData, &cbReturned, NULL))
310 {
311/** @todo The passing of error codes needs to be tested and fixed (as does *all* the other hosts except for
312 * OS/2). The idea is that the VBox status codes in ring-0 should be transfered without loss down to
313 * ring-3. However, it's not vitally important right now (obviously, since the other guys has been
314 * ignoring it for 1+ years now). On Linux and Solaris the transfer is done, but it is currently not
315 * lossless, so still needs fixing. */
316 DWORD LastErr = GetLastError();
317 return RTErrConvertFromWin32(LastErr);
318 }
319
320 return VINF_SUCCESS;
321
322#elif defined(RT_OS_OS2)
323 ULONG cbOS2Parm = cbData;
324 int32_t vrc = VERR_INTERNAL_ERROR;
325 ULONG cbOS2Data = sizeof(vrc);
326 APIRET rc = DosDevIOCtl(g_File, VBOXGUEST_IOCTL_CATEGORY, iFunction,
327 pvData, cbData, &cbOS2Parm,
328 &vrc, sizeof(vrc), &cbOS2Data);
329 if (RT_LIKELY(!rc))
330 return vrc;
331 return RTErrConvertFromOS2(rc);
332
333#elif defined(RT_OS_SOLARIS) || defined(RT_OS_FREEBSD)
334 VBGLBIGREQ Hdr;
335 Hdr.u32Magic = VBGLBIGREQ_MAGIC;
336 Hdr.cbData = cbData;
337 Hdr.pvDataR3 = pvData;
338# if HC_ARCH_BITS == 32
339 Hdr.u32Padding = 0;
340# endif
341
342/** @todo test status code passing! Check that the kernel doesn't do any
343 * error checks using specific errno values, and just pass an VBox
344 * error instead of an errno.h one. Alternatively, extend/redefine the
345 * header with an error code return field (much better alternative
346 * actually). */
347 int rc = ioctl((int)g_File, iFunction, &Hdr);
348 if (rc == -1)
349 {
350 rc = errno;
351 return RTErrConvertFromErrno(rc);
352 }
353 return VINF_SUCCESS;
354
355#elif defined(RT_OS_LINUX)
356# ifdef VBOX_VBGLR3_XFREE86
357 int rc = xf86ioctl((int)g_File, iFunction, pvData);
358# else
359 int rc = ioctl((int)g_File, iFunction, pvData);
360# endif
361 if (RT_LIKELY(rc == 0))
362 return VINF_SUCCESS;
363
364 /* Positive values are negated VBox error status codes. */
365 if (rc > 0)
366 rc = -rc;
367 else
368# ifdef VBOX_VBGLR3_XFREE86
369 rc = VERR_FILE_IO_ERROR;
370# else
371 rc = RTErrConvertFromErrno(errno);
372# endif
373 NOREF(cbData);
374 return rc;
375
376#elif defined(VBOX_VBGLR3_XFREE86)
377 /* PORTME - This is preferred over the RTFileIOCtl variant below, just be careful with the (int). */
378/** @todo test status code passing! */
379 int rc = xf86ioctl(g_File, iFunction, pvData);
380 if (rc == -1)
381 return VERR_FILE_IO_ERROR; /* This is purely legacy stuff, it has to work and no more. */
382 return VINF_SUCCESS;
383
384#else
385 /* Default implementation - PORTME: Do not use this without testings that passing errors works! */
386/** @todo test status code passing! */
387 int rc2 = VERR_INTERNAL_ERROR;
388 int rc = RTFileIoCtl(g_File, (int)iFunction, pvData, cbData, &rc2);
389 if (RT_SUCCESS(rc))
390 rc = rc2;
391 return rc;
392#endif
393}
394
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