1 | /* $Id: VBoxGuestR3Lib.cpp 6538 2008-01-28 19:50:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxGuestR3Lib - Ring-3 Support Library for VirtualBox guest additions, Core.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2007 innotek GmbH
|
---|
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 |
|
---|
18 |
|
---|
19 | /*******************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *******************************************************************************/
|
---|
22 | #ifdef RT_OS_OS2
|
---|
23 | # define INCL_BASE
|
---|
24 | # define INCL_ERRORS
|
---|
25 | # include <os2.h>
|
---|
26 | #elif defined(RT_OS_SOLARIS)
|
---|
27 | # include <sys/types.h>
|
---|
28 | # include <sys/stat.h>
|
---|
29 | # include <errno.h>
|
---|
30 | # include <unistd.h>
|
---|
31 | #endif
|
---|
32 |
|
---|
33 | #include <iprt/time.h>
|
---|
34 | #include <iprt/asm.h>
|
---|
35 | #include <iprt/string.h>
|
---|
36 | #ifdef VBOX_VBGLR3_XFREE86
|
---|
37 | /* Definitions for X server library functions such as xf86open and mappings to C functions. */
|
---|
38 | /* Make the headers C++-compatible */
|
---|
39 | # define class xf86_vbox_class
|
---|
40 | # define bool xf86_vbox_bool
|
---|
41 | # define private xf86_vbox_private
|
---|
42 | # define new xf86_vbox_new
|
---|
43 | extern "C"
|
---|
44 | {
|
---|
45 | # include "xf86.h"
|
---|
46 | # include "xf86_OSproc.h"
|
---|
47 | # include "xf86Resources.h"
|
---|
48 | # include "xf86_ansic.h"
|
---|
49 | }
|
---|
50 | # undef class
|
---|
51 | # undef bool
|
---|
52 | # undef private
|
---|
53 | # undef new
|
---|
54 | #else
|
---|
55 | # include <iprt/file.h>
|
---|
56 | # include <iprt/assert.h>
|
---|
57 | # include <iprt/thread.h>
|
---|
58 | #endif
|
---|
59 | #include <VBox/VBoxGuest.h>
|
---|
60 | #include "VBGLR3Internal.h"
|
---|
61 |
|
---|
62 |
|
---|
63 | /*******************************************************************************
|
---|
64 | * Global Variables *
|
---|
65 | *******************************************************************************/
|
---|
66 | /** The VBoxGuest device handle. */
|
---|
67 | #ifdef VBOX_VBGLR3_XFREE86
|
---|
68 | static int g_File = -1;
|
---|
69 | #else
|
---|
70 | static RTFILE g_File = NIL_RTFILE;
|
---|
71 | #endif
|
---|
72 | /**
|
---|
73 | * A counter of the number of times the library has been initialised, for use with
|
---|
74 | * X.org drivers, where the library may be shared by multiple independant modules
|
---|
75 | * inside a single process space.
|
---|
76 | */
|
---|
77 | static uint32_t g_cInits = 0;
|
---|
78 |
|
---|
79 | VBGLR3DECL(int) VbglR3Init(void)
|
---|
80 | {
|
---|
81 | uint32_t cInits = ASMAtomicIncU32(&g_cInits);
|
---|
82 | #ifdef VBOX_VBGLR3_XFREE86
|
---|
83 | if (1 != cInits)
|
---|
84 | return VINF_SUCCESS;
|
---|
85 | if (-1 != g_File)
|
---|
86 | return VERR_INTERNAL_ERROR;
|
---|
87 | #else
|
---|
88 | Assert(cInits > 0);
|
---|
89 | if (1 > cInits)
|
---|
90 | {
|
---|
91 | /* This will not work when the library is shared inside a multi-threaded
|
---|
92 | process. Hopefully no-one will try that, as we can't use the threads
|
---|
93 | APIs here. */
|
---|
94 | if (NIL_RTFILE == g_File)
|
---|
95 | return VERR_INTERNAL_ERROR;
|
---|
96 | return VINF_SUCCESS;
|
---|
97 | }
|
---|
98 | if (NIL_RTFILE != g_File)
|
---|
99 | return VERR_INTERNAL_ERROR;
|
---|
100 | #endif
|
---|
101 |
|
---|
102 | #if defined(RT_OS_OS2)
|
---|
103 | /*
|
---|
104 | * We might wish to compile this with Watcom, so stick to
|
---|
105 | * the OS/2 APIs all the way. And in any case we have to use
|
---|
106 | * DosDevIOCtl for the requests, why not use Dos* for everything.
|
---|
107 | */
|
---|
108 | HFILE hf = NULLHANDLE;
|
---|
109 | ULONG ulAction = 0;
|
---|
110 | APIRET rc = DosOpen((PCSZ)VBOXGUEST_DEVICE_NAME, &hf, &ulAction, 0, FILE_NORMAL,
|
---|
111 | OPEN_ACTION_OPEN_IF_EXISTS,
|
---|
112 | OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NOINHERIT | OPEN_SHARE_DENYNONE | OPEN_ACCESS_READWRITE,
|
---|
113 | NULL);
|
---|
114 | if (rc)
|
---|
115 | return RTErrConvertFromOS2(rc);
|
---|
116 |
|
---|
117 | if (hf < 16)
|
---|
118 | {
|
---|
119 | HFILE ahfs[16];
|
---|
120 | unsigned i;
|
---|
121 | for (i = 0; i < RT_ELEMENTS(ahfs); i++)
|
---|
122 | {
|
---|
123 | ahfs[i] = 0xffffffff;
|
---|
124 | rc = DosDupHandle(hf, &ahfs[i]);
|
---|
125 | if (rc)
|
---|
126 | break;
|
---|
127 | }
|
---|
128 |
|
---|
129 | if (i-- > 1)
|
---|
130 | {
|
---|
131 | ULONG fulState = 0;
|
---|
132 | rc = DosQueryFHState(ahfs[i], &fulState);
|
---|
133 | if (!rc)
|
---|
134 | {
|
---|
135 | fulState |= OPEN_FLAGS_NOINHERIT;
|
---|
136 | fulState &= OPEN_FLAGS_WRITE_THROUGH | OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NO_CACHE | OPEN_FLAGS_NOINHERIT; /* Turn off non-participating bits. */
|
---|
137 | rc = DosSetFHState(ahfs[i], fulState);
|
---|
138 | }
|
---|
139 | if (!rc)
|
---|
140 | {
|
---|
141 | rc = DosClose(hf);
|
---|
142 | AssertMsg(!rc, ("%ld\n", rc));
|
---|
143 | hf = ahfs[i];
|
---|
144 | }
|
---|
145 | else
|
---|
146 | i++;
|
---|
147 | while (i-- > 0)
|
---|
148 | DosClose(ahfs[i]);
|
---|
149 | }
|
---|
150 | }
|
---|
151 | g_File = hf;
|
---|
152 |
|
---|
153 | #elif defined(VBOX_VBGLR3_XFREE86)
|
---|
154 | int File = open(VBOXGUEST_DEVICE_NAME, O_RDWR);
|
---|
155 | if (-1 == File)
|
---|
156 | {
|
---|
157 | return VERR_UNRESOLVED_ERROR;
|
---|
158 | }
|
---|
159 | g_File = File;
|
---|
160 |
|
---|
161 | #else
|
---|
162 | /* the default implemenation. (linux, solaris) */
|
---|
163 | RTFILE File;
|
---|
164 | int rc = RTFileOpen(&File, VBOXGUEST_DEVICE_NAME, RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
|
---|
165 | if (RT_FAILURE(rc))
|
---|
166 | return rc;
|
---|
167 | g_File = File;
|
---|
168 |
|
---|
169 | #endif
|
---|
170 |
|
---|
171 | return VINF_SUCCESS;
|
---|
172 | }
|
---|
173 |
|
---|
174 |
|
---|
175 | VBGLR3DECL(void) VbglR3Term(void)
|
---|
176 | {
|
---|
177 | uint32_t cInits = ASMAtomicDecU32(&g_cInits);
|
---|
178 | if (cInits > 0)
|
---|
179 | return;
|
---|
180 | #ifndef VBOX_VBGLR3_XFREE86
|
---|
181 | AssertReturnVoid(0 == cInits);
|
---|
182 | RTFILE File = g_File;
|
---|
183 | g_File = NIL_RTFILE;
|
---|
184 | AssertReturnVoid(NIL_RTFILE != File);
|
---|
185 | #else
|
---|
186 | int File = g_File;
|
---|
187 | g_File = -1;
|
---|
188 | if (-1 == File)
|
---|
189 | return;
|
---|
190 | #endif
|
---|
191 | #if defined(RT_OS_OS2)
|
---|
192 | APIRET rc = DosClose(File);
|
---|
193 | AssertMsg(!rc, ("%ld\n", rc));
|
---|
194 | #elif defined(VBOX_VBGLR3_XFREE86)
|
---|
195 | /* if (-1 == close(File))
|
---|
196 | {
|
---|
197 | int iErr = errno;
|
---|
198 | AssertRC(RTErrConvertFromErrno(iErr));
|
---|
199 | } */
|
---|
200 | close(File); /* iprt is not available here. */
|
---|
201 | File = -1;
|
---|
202 | #else
|
---|
203 | int rc = RTFileClose(File);
|
---|
204 | AssertRC(rc);
|
---|
205 | #endif
|
---|
206 | }
|
---|
207 |
|
---|
208 |
|
---|
209 | /**
|
---|
210 | * Internal wrapper around various OS specific ioctl implemenations.
|
---|
211 | *
|
---|
212 | * @returns VBox status code as returned by VBoxGuestCommonIOCtl, or
|
---|
213 | * an failure returned by the OS specific ioctl APIs.
|
---|
214 | *
|
---|
215 | * @param iFunction The requested function.
|
---|
216 | * @param pvData The input and output data buffer.
|
---|
217 | * @param cbData The size of the buffer.
|
---|
218 | *
|
---|
219 | * @remark Exactly how the VBoxGuestCommonIOCtl is ferried back
|
---|
220 | * here is OS specific. On BSD and Darwin we can use errno,
|
---|
221 | * while on OS/2 we use the 2nd buffer of the IOCtl.
|
---|
222 | */
|
---|
223 | int vbglR3DoIOCtl(unsigned iFunction, void *pvData, size_t cbData)
|
---|
224 | {
|
---|
225 | #ifdef RT_OS_OS2
|
---|
226 | ULONG cbOS2Parm = cbData;
|
---|
227 | int32_t vrc = VERR_INTERNAL_ERROR;
|
---|
228 | ULONG cbOS2Data = sizeof(vrc);
|
---|
229 | APIRET rc = DosDevIOCtl(g_File, VBOXGUEST_IOCTL_CATEGORY, iFunction,
|
---|
230 | pvData, cbData, &cbOS2Parm,
|
---|
231 | &vrc, sizeof(vrc), &cbOS2Data);
|
---|
232 | if (RT_LIKELY(!rc))
|
---|
233 | return vrc;
|
---|
234 | return RTErrConvertFromOS2(rc);
|
---|
235 |
|
---|
236 | #elif defined(RT_OS_SOLARIS)
|
---|
237 | VBGLBIGREQ Hdr;
|
---|
238 | Hdr.u32Magic = VBGLBIGREQ_MAGIC;
|
---|
239 | Hdr.cbData = cbData;
|
---|
240 | Hdr.pvDataR3 = pvData;
|
---|
241 |
|
---|
242 | int rc = ioctl((int)g_File, iFunction, &Hdr);
|
---|
243 | if (rc == -1)
|
---|
244 | {
|
---|
245 | rc = errno;
|
---|
246 | return RTErrConvertFromErrno(rc);
|
---|
247 | }
|
---|
248 | return VINF_SUCCESS;
|
---|
249 |
|
---|
250 | #else
|
---|
251 | /* Default implementation - PORTME: Do not use this without testings that error passing works! */
|
---|
252 | # ifdef VBOX_VBGLR3_XFREE86
|
---|
253 | int rc = ioctl(g_File, (int) iFunction, pvData);
|
---|
254 | if (rc == -1)
|
---|
255 | {
|
---|
256 | rc = errno;
|
---|
257 | return RTErrConvertFromErrno(rc);
|
---|
258 | }
|
---|
259 | return VINF_SUCCESS;
|
---|
260 | # else
|
---|
261 | int rc2 = VERR_INTERNAL_ERROR;
|
---|
262 | int rc = RTFileIoCtl(g_File, (int)iFunction, pvData, cbData, &rc2);
|
---|
263 | if (RT_SUCCESS(rc))
|
---|
264 | rc = rc2;
|
---|
265 | return rc;
|
---|
266 | # endif
|
---|
267 | #endif
|
---|
268 | }
|
---|
269 |
|
---|