VirtualBox

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

Last change on this file since 62521 was 62521, checked in by vboxsync, 8 years ago

(C) 2016

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