VirtualBox

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

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

Additions/common/VBoxGuestLib: add support for the new user guest device to the R3 guest library

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