1 | /** @file
|
---|
2 | *
|
---|
3 | * VBoxGuestLib - A support library for VirtualBox guest additions:
|
---|
4 | * Host-Guest Communication Manager
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006 InnoTek Systemberatung GmbH
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License as published by the Free Software Foundation,
|
---|
14 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
15 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
16 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | *
|
---|
18 | * If you received this file as part of a commercial VirtualBox
|
---|
19 | * distribution, then only the terms of your commercial VirtualBox
|
---|
20 | * license agreement apply instead of the previous paragraph.
|
---|
21 | */
|
---|
22 |
|
---|
23 | /* These public functions can be only used by other drivers.
|
---|
24 | * They all do an IOCTL to VBoxGuest.
|
---|
25 | */
|
---|
26 |
|
---|
27 | /* Entire file is ifdef'ed with !VBGL_VBOXGUEST */
|
---|
28 | #ifndef VBGL_VBOXGUEST
|
---|
29 |
|
---|
30 | /** @todo r=bird: These two issues with string.h and bool are handled by
|
---|
31 | * iprt/string.h and iprt/types.h respectivly. Please change after the release. */
|
---|
32 | #if defined(__LINUX__) && defined(__KERNEL__)
|
---|
33 | #ifndef bool /* Linux 2.6.19 C++ nightmare */
|
---|
34 | #define bool bool_type
|
---|
35 | #define true true_type
|
---|
36 | #define false false_type
|
---|
37 | #define _Bool int
|
---|
38 | #define bool_HGCM_cpp
|
---|
39 | #endif
|
---|
40 | #include <linux/string.h>
|
---|
41 | #ifdef bool_HGCM_cpp
|
---|
42 | #undef bool
|
---|
43 | #undef true
|
---|
44 | #undef false
|
---|
45 | #undef _Bool
|
---|
46 | #undef bool_HGCM_cpp
|
---|
47 | #endif
|
---|
48 | #else
|
---|
49 | #include <string.h>
|
---|
50 | #endif
|
---|
51 |
|
---|
52 | #include <VBox/VBoxGuestLib.h>
|
---|
53 | #include "VBGLInternal.h"
|
---|
54 |
|
---|
55 | #include <iprt/assert.h>
|
---|
56 | #include <iprt/semaphore.h>
|
---|
57 |
|
---|
58 | #define VBGL_HGCM_ASSERTMsg AssertReleaseMsg
|
---|
59 |
|
---|
60 | int vbglHGCMInit (void)
|
---|
61 | {
|
---|
62 | RTSemFastMutexCreate(&g_vbgldata.mutexHGCMHandle);
|
---|
63 |
|
---|
64 | return VINF_SUCCESS;
|
---|
65 | }
|
---|
66 |
|
---|
67 | int vbglHGCMTerminate (void)
|
---|
68 | {
|
---|
69 | RTSemFastMutexDestroy(g_vbgldata.mutexHGCMHandle);
|
---|
70 |
|
---|
71 | return VINF_SUCCESS;
|
---|
72 | }
|
---|
73 |
|
---|
74 | DECLINLINE(int) vbglHandleHeapEnter (void)
|
---|
75 | {
|
---|
76 | int rc = RTSemFastMutexRequest(g_vbgldata.mutexHGCMHandle);
|
---|
77 |
|
---|
78 | VBGL_HGCM_ASSERTMsg(VBOX_SUCCESS(rc),
|
---|
79 | ("Failed to request handle heap mutex, rc = %Vrc\n", rc));
|
---|
80 |
|
---|
81 | return rc;
|
---|
82 | }
|
---|
83 |
|
---|
84 | DECLINLINE(void) vbglHandleHeapLeave (void)
|
---|
85 | {
|
---|
86 | RTSemFastMutexRelease(g_vbgldata.mutexHGCMHandle);
|
---|
87 | }
|
---|
88 |
|
---|
89 | struct VBGLHGCMHANDLEDATA *vbglHGCMHandleAlloc (void)
|
---|
90 | {
|
---|
91 | struct VBGLHGCMHANDLEDATA *p;
|
---|
92 | int rc = vbglHandleHeapEnter ();
|
---|
93 | uint32_t i;
|
---|
94 |
|
---|
95 | if (VBOX_FAILURE (rc))
|
---|
96 | return NULL;
|
---|
97 |
|
---|
98 | p = NULL;
|
---|
99 |
|
---|
100 | /** Simple linear search in array. This will be called not so often, only connect/disconnect.
|
---|
101 | * @todo bitmap for faster search and other obvious optimizations.
|
---|
102 | */
|
---|
103 |
|
---|
104 | for (i = 0; i < ELEMENTS(g_vbgldata.aHGCMHandleData); i++)
|
---|
105 | {
|
---|
106 | if (!g_vbgldata.aHGCMHandleData[i].fAllocated)
|
---|
107 | {
|
---|
108 | p = &g_vbgldata.aHGCMHandleData[i];
|
---|
109 | p->fAllocated = 1;
|
---|
110 | break;
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | vbglHandleHeapLeave ();
|
---|
115 |
|
---|
116 | VBGL_HGCM_ASSERTMsg(p != NULL,
|
---|
117 | ("Not enough HGCM handles.\n"));
|
---|
118 |
|
---|
119 | return p;
|
---|
120 | }
|
---|
121 |
|
---|
122 | void vbglHGCMHandleFree (struct VBGLHGCMHANDLEDATA *pHandle)
|
---|
123 | {
|
---|
124 | int rc;
|
---|
125 |
|
---|
126 | if (!pHandle)
|
---|
127 | return;
|
---|
128 |
|
---|
129 | rc = vbglHandleHeapEnter ();
|
---|
130 |
|
---|
131 | if (VBOX_FAILURE (rc))
|
---|
132 | return;
|
---|
133 |
|
---|
134 | VBGL_HGCM_ASSERTMsg(pHandle->fAllocated,
|
---|
135 | ("Freeing not allocated handle.\n"));
|
---|
136 |
|
---|
137 | memset(pHandle, 0, sizeof (struct VBGLHGCMHANDLEDATA));
|
---|
138 | vbglHandleHeapLeave ();
|
---|
139 | return;
|
---|
140 | }
|
---|
141 |
|
---|
142 | DECLVBGL(int) VbglHGCMConnect (VBGLHGCMHANDLE *pHandle, VBoxGuestHGCMConnectInfo *pData)
|
---|
143 | {
|
---|
144 | int rc;
|
---|
145 | struct VBGLHGCMHANDLEDATA *pHandleData;
|
---|
146 |
|
---|
147 | if (!pHandle || !pData)
|
---|
148 | return VERR_INVALID_PARAMETER;
|
---|
149 |
|
---|
150 | pHandleData = vbglHGCMHandleAlloc ();
|
---|
151 |
|
---|
152 | rc = VINF_SUCCESS;
|
---|
153 |
|
---|
154 | if (!pHandleData)
|
---|
155 | {
|
---|
156 | rc = VERR_NO_MEMORY;
|
---|
157 | }
|
---|
158 | else
|
---|
159 | {
|
---|
160 | rc = vbglDriverOpen (&pHandleData->driver);
|
---|
161 |
|
---|
162 | if (VBOX_SUCCESS(rc))
|
---|
163 | {
|
---|
164 | rc = vbglDriverIOCtl (&pHandleData->driver, IOCTL_VBOXGUEST_HGCM_CONNECT, pData, sizeof (*pData));
|
---|
165 |
|
---|
166 | if (VBOX_SUCCESS(rc))
|
---|
167 | {
|
---|
168 | *pHandle = pHandleData;
|
---|
169 | }
|
---|
170 | else
|
---|
171 | {
|
---|
172 | vbglDriverClose (&pHandleData->driver);
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | if (VBOX_FAILURE(rc))
|
---|
177 | {
|
---|
178 | vbglHGCMHandleFree (pHandleData);
|
---|
179 | }
|
---|
180 | }
|
---|
181 |
|
---|
182 | return rc;
|
---|
183 | }
|
---|
184 |
|
---|
185 | DECLVBGL(int) VbglHGCMDisconnect (VBGLHGCMHANDLE handle, VBoxGuestHGCMDisconnectInfo *pData)
|
---|
186 | {
|
---|
187 | int rc = VINF_SUCCESS;
|
---|
188 |
|
---|
189 | rc = vbglDriverIOCtl (&handle->driver, IOCTL_VBOXGUEST_HGCM_DISCONNECT, pData, sizeof (*pData));
|
---|
190 |
|
---|
191 | vbglDriverClose (&handle->driver);
|
---|
192 |
|
---|
193 | vbglHGCMHandleFree (handle);
|
---|
194 |
|
---|
195 | return rc;
|
---|
196 | }
|
---|
197 |
|
---|
198 | DECLVBGL(int) VbglHGCMCall (VBGLHGCMHANDLE handle, VBoxGuestHGCMCallInfo *pData, uint32_t cbData)
|
---|
199 | {
|
---|
200 | int rc = VINF_SUCCESS;
|
---|
201 |
|
---|
202 | VBGL_HGCM_ASSERTMsg(cbData >= sizeof (VBoxGuestHGCMCallInfo) + pData->cParms * sizeof (HGCMFunctionParameter),
|
---|
203 | ("cbData = %d, cParms = %d (calculated size %d)\n", cbData, pData->cParms, sizeof (VBoxGuestHGCMCallInfo) + pData->cParms * sizeof (VBoxGuestHGCMCallInfo)));
|
---|
204 |
|
---|
205 | rc = vbglDriverIOCtl (&handle->driver, IOCTL_VBOXGUEST_HGCM_CALL, pData, cbData);
|
---|
206 |
|
---|
207 | return rc;
|
---|
208 | }
|
---|
209 |
|
---|
210 | #endif /* VBGL_VBOXGUEST */
|
---|