VirtualBox

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

Last change on this file since 28501 was 27687, checked in by vboxsync, 15 years ago

Additons/common/VBoxGuestLibR3: make saving and restoring video modes work with XFree86 and clean up the code

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