VirtualBox

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

Last change on this file since 43363 was 43363, checked in by vboxsync, 12 years ago

Haiku Additions.

  • 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 43363 2012-09-20 09:56:07Z 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_HAIKU) \
41 || defined(RT_OS_LINUX) \
42 || defined(RT_OS_SOLARIS)
43# include <sys/types.h>
44# include <sys/stat.h>
45# if defined(RT_OS_LINUX) /** @todo check this on solaris+freebsd as well. */
46# include <sys/ioctl.h>
47# endif
48# include <errno.h>
49# include <unistd.h>
50#endif
51
52#include <iprt/assert.h>
53#include <iprt/asm.h>
54#include <iprt/file.h>
55#include <iprt/time.h>
56#include <iprt/string.h>
57#include <iprt/thread.h>
58#include <VBox/log.h>
59#include "VBGLR3Internal.h"
60
61#ifdef VBOX_VBGLR3_XFREE86
62/* Rather than try to resolve all the header file conflicts, I will just
63 prototype what we need here. */
64# define XF86_O_RDWR 0x0002
65typedef void *pointer;
66extern "C" int xf86open(const char *, int, ...);
67extern "C" int xf86close(int);
68extern "C" int xf86ioctl(int, unsigned long, pointer);
69#endif
70
71
72/*******************************************************************************
73* Global Variables *
74*******************************************************************************/
75/** The VBoxGuest device handle. */
76#ifdef VBOX_VBGLR3_XFREE86
77static int g_File = -1;
78#elif defined(RT_OS_WINDOWS)
79static HANDLE g_hFile = INVALID_HANDLE_VALUE;
80#else
81static RTFILE g_File = NIL_RTFILE;
82#endif
83/** User counter.
84 * A counter of the number of times the library has been initialised, for use with
85 * X.org drivers, where the library may be shared by multiple independent modules
86 * inside a single process space.
87 */
88static uint32_t volatile g_cInits = 0;
89
90
91
92/**
93 * Implementation of VbglR3Init and VbglR3InitUser
94 */
95static int vbglR3Init(const char *pszDeviceName)
96{
97 uint32_t cInits = ASMAtomicIncU32(&g_cInits);
98 Assert(cInits > 0);
99 if (cInits > 1)
100 {
101 /*
102 * This will fail if two (or more) threads race each other calling VbglR3Init.
103 * However it will work fine for single threaded or otherwise serialized
104 * processed calling us more than once.
105 */
106#ifdef RT_OS_WINDOWS
107 if (g_hFile == INVALID_HANDLE_VALUE)
108#elif !defined (VBOX_VBGLR3_XFREE86)
109 if (g_File == NIL_RTFILE)
110#else
111 if (g_File == -1)
112#endif
113 return VERR_INTERNAL_ERROR;
114 return VINF_SUCCESS;
115 }
116#if defined(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
125#if defined(RT_OS_WINDOWS)
126 /*
127 * Have to use CreateFile here as we want to specify FILE_FLAG_OVERLAPPED
128 * and possible some other bits not available thru iprt/file.h.
129 */
130 HANDLE hFile = CreateFile(pszDeviceName,
131 GENERIC_READ | GENERIC_WRITE,
132 FILE_SHARE_READ | FILE_SHARE_WRITE,
133 NULL,
134 OPEN_EXISTING,
135 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
136 NULL);
137
138 if (hFile == INVALID_HANDLE_VALUE)
139 return VERR_OPEN_FAILED;
140 g_hFile = hFile;
141
142#elif defined(RT_OS_OS2)
143 /*
144 * We might wish to compile this with Watcom, so stick to
145 * the OS/2 APIs all the way. And in any case we have to use
146 * DosDevIOCtl for the requests, why not use Dos* for everything.
147 */
148 HFILE hf = NULLHANDLE;
149 ULONG ulAction = 0;
150 APIRET rc = DosOpen((PCSZ)pszDeviceName, &hf, &ulAction, 0, FILE_NORMAL,
151 OPEN_ACTION_OPEN_IF_EXISTS,
152 OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NOINHERIT | OPEN_SHARE_DENYNONE | OPEN_ACCESS_READWRITE,
153 NULL);
154 if (rc)
155 return RTErrConvertFromOS2(rc);
156
157 if (hf < 16)
158 {
159 HFILE ahfs[16];
160 unsigned i;
161 for (i = 0; i < RT_ELEMENTS(ahfs); i++)
162 {
163 ahfs[i] = 0xffffffff;
164 rc = DosDupHandle(hf, &ahfs[i]);
165 if (rc)
166 break;
167 }
168
169 if (i-- > 1)
170 {
171 ULONG fulState = 0;
172 rc = DosQueryFHState(ahfs[i], &fulState);
173 if (!rc)
174 {
175 fulState |= OPEN_FLAGS_NOINHERIT;
176 fulState &= OPEN_FLAGS_WRITE_THROUGH | OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NO_CACHE | OPEN_FLAGS_NOINHERIT; /* Turn off non-participating bits. */
177 rc = DosSetFHState(ahfs[i], fulState);
178 }
179 if (!rc)
180 {
181 rc = DosClose(hf);
182 AssertMsg(!rc, ("%ld\n", rc));
183 hf = ahfs[i];
184 }
185 else
186 i++;
187 while (i-- > 0)
188 DosClose(ahfs[i]);
189 }
190 }
191 g_File = (RTFILE)hf;
192
193#elif defined(VBOX_VBGLR3_XFREE86)
194 int File = xf86open(pszDeviceName, XF86_O_RDWR);
195 if (File == -1)
196 return VERR_OPEN_FAILED;
197 g_File = File;
198
199#else
200
201 /* The default implementation. (linux, solaris, freebsd, haiku) */
202 RTFILE File;
203 int rc = RTFileOpen(&File, pszDeviceName, RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
204 if (RT_FAILURE(rc))
205 return rc;
206 g_File = File;
207
208#endif
209
210#ifndef VBOX_VBGLR3_XFREE86
211 /*
212 * Create release logger
213 */
214 PRTLOGGER pReleaseLogger;
215 static const char * const s_apszGroups[] = VBOX_LOGGROUP_NAMES;
216 int rc2 = RTLogCreate(&pReleaseLogger, 0, "all", "VBOX_RELEASE_LOG",
217 RT_ELEMENTS(s_apszGroups), &s_apszGroups[0], 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((uintptr_t)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 implementations.
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 transferred 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((uintptr_t)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#ifdef VBOX_VBGLR3_XFREE86
348 int rc = xf86ioctl(g_File, iFunction, &Hdr);
349#else
350 if (g_File == NIL_RTFILE)
351 return VERR_INVALID_HANDLE;
352 int rc = ioctl(RTFileToNative(g_File), iFunction, &Hdr);
353#endif
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 if (g_File == NIL_RTFILE)
366 return VERR_INVALID_HANDLE;
367 int rc = ioctl(RTFileToNative(g_File), iFunction, pvData);
368# endif
369 if (RT_LIKELY(rc == 0))
370 return VINF_SUCCESS;
371
372 /* Positive values are negated VBox error status codes. */
373 if (rc > 0)
374 rc = -rc;
375 else
376# ifdef VBOX_VBGLR3_XFREE86
377 rc = VERR_FILE_IO_ERROR;
378# else
379 rc = RTErrConvertFromErrno(errno);
380# endif
381 NOREF(cbData);
382 return rc;
383
384#elif defined(RT_OS_HAIKU)
385 /* The ioctl hook in Haiku does take the len parameter when specified,
386 * so just use it.
387 */
388 int rc = ioctl((int)g_File, iFunction, pvData, cbData);
389 if (RT_LIKELY(rc == 0))
390 return VINF_SUCCESS;
391
392 /* Positive values are negated VBox error status codes. */
393 if (rc > 0)
394 rc = -rc;
395 else
396 rc = RTErrConvertFromErrno(errno);
397 return rc;
398
399#elif defined(VBOX_VBGLR3_XFREE86)
400 /* PORTME - This is preferred over the RTFileIOCtl variant below, just be careful with the (int). */
401/** @todo test status code passing! */
402 int rc = xf86ioctl(g_File, iFunction, pvData);
403 if (rc == -1)
404 return VERR_FILE_IO_ERROR; /* This is purely legacy stuff, it has to work and no more. */
405 return VINF_SUCCESS;
406
407#else
408 /* Default implementation - PORTME: Do not use this without testings that passing errors works! */
409/** @todo test status code passing! */
410 int rc2 = VERR_INTERNAL_ERROR;
411 int rc = RTFileIoCtl(g_File, (int)iFunction, pvData, cbData, &rc2);
412 if (RT_SUCCESS(rc))
413 rc = rc2;
414 return rc;
415#endif
416}
417
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