VirtualBox

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

Last change on this file since 21201 was 21201, checked in by vboxsync, 16 years ago

VBoxGuestR3Lib.cpp: Solaris+freebsd todo.

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