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