VirtualBox

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

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

alternative license for VBoxGuestLib is CDDL

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