VirtualBox

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

Last change on this file since 67637 was 63495, checked in by vboxsync, 9 years ago

warnings (clang)

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