VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxGuestLib/HGCM.cpp@ 719

Last change on this file since 719 was 719, checked in by vboxsync, 18 years ago

Changed the Linux Additions to compile everything from source (no more binaries linked in) and cleaned up those files to compile as plain C.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.2 KB
Line 
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
60int vbglHGCMInit (void)
61{
62 RTSemFastMutexCreate(&g_vbgldata.mutexHGCMHandle);
63
64 return VINF_SUCCESS;
65}
66
67int vbglHGCMTerminate (void)
68{
69 RTSemFastMutexDestroy(g_vbgldata.mutexHGCMHandle);
70
71 return VINF_SUCCESS;
72}
73
74DECLINLINE(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
84DECLINLINE(void) vbglHandleHeapLeave (void)
85{
86 RTSemFastMutexRelease(g_vbgldata.mutexHGCMHandle);
87}
88
89struct VBGLHGCMHANDLEDATA *vbglHGCMHandleAlloc (void)
90{
91 struct VBGLHGCMHANDLEDATA *p;
92 int rc = vbglHandleHeapEnter ();
93
94 if (VBOX_FAILURE (rc))
95 {
96 return NULL;
97 }
98
99 p = NULL;
100
101 /** Simple linear search in array. This will be called not so often, only connect/disconnect.
102 * @todo bitmap for faster search and other obvious optimizations.
103 */
104
105 uint32_t i;
106
107 for (i = 0; i < ELEMENTS(g_vbgldata.aHGCMHandleData); i++)
108 {
109 if (!g_vbgldata.aHGCMHandleData[i].fAllocated)
110 {
111 p = &g_vbgldata.aHGCMHandleData[i];
112
113 p->fAllocated = 1;
114
115 break;
116 }
117 }
118
119 vbglHandleHeapLeave ();
120
121 VBGL_HGCM_ASSERTMsg(p != NULL,
122 ("Not enough HGCM handles.\n"));
123
124 return p;
125}
126
127void vbglHGCMHandleFree (struct VBGLHGCMHANDLEDATA *pHandle)
128{
129 int rc;
130
131 if (!pHandle)
132 {
133 return;
134 }
135
136 rc = vbglHandleHeapEnter ();
137
138 if (VBOX_FAILURE (rc))
139 {
140 return;
141 }
142
143 VBGL_HGCM_ASSERTMsg(pHandle->fAllocated,
144 ("Freeing not allocated handle.\n"));
145
146 memset(pHandle, 0, sizeof (struct VBGLHGCMHANDLEDATA));
147
148 vbglHandleHeapLeave ();
149
150 return;
151}
152
153DECLVBGL(int) VbglHGCMConnect (VBGLHGCMHANDLE *pHandle, VBoxGuestHGCMConnectInfo *pData)
154{
155 int rc;
156 struct VBGLHGCMHANDLEDATA *pHandleData;
157
158 if (!pHandle || !pData)
159 {
160 return VERR_INVALID_PARAMETER;
161 }
162
163 pHandleData = vbglHGCMHandleAlloc ();
164
165 rc = VINF_SUCCESS;
166
167 if (!pHandleData)
168 {
169 rc = VERR_NO_MEMORY;
170 }
171 else
172 {
173 rc = vbglDriverOpen (&pHandleData->driver);
174
175 if (VBOX_SUCCESS(rc))
176 {
177 rc = vbglDriverIOCtl (&pHandleData->driver, IOCTL_VBOXGUEST_HGCM_CONNECT, pData, sizeof (*pData));
178
179 if (VBOX_SUCCESS(rc))
180 {
181 *pHandle = pHandleData;
182 }
183 else
184 {
185 vbglDriverClose (&pHandleData->driver);
186 }
187 }
188
189 if (VBOX_FAILURE(rc))
190 {
191 vbglHGCMHandleFree (pHandleData);
192 }
193 }
194
195 return rc;
196}
197
198DECLVBGL(int) VbglHGCMDisconnect (VBGLHGCMHANDLE handle, VBoxGuestHGCMDisconnectInfo *pData)
199{
200 int rc = VINF_SUCCESS;
201
202 rc = vbglDriverIOCtl (&handle->driver, IOCTL_VBOXGUEST_HGCM_DISCONNECT, pData, sizeof (*pData));
203
204 vbglDriverClose (&handle->driver);
205
206 vbglHGCMHandleFree (handle);
207
208 return rc;
209}
210
211DECLVBGL(int) VbglHGCMCall (VBGLHGCMHANDLE handle, VBoxGuestHGCMCallInfo *pData, uint32_t cbData)
212{
213 int rc = VINF_SUCCESS;
214
215 VBGL_HGCM_ASSERTMsg(cbData >= sizeof (VBoxGuestHGCMCallInfo) + pData->cParms * sizeof (HGCMFunctionParameter),
216 ("cbData = %d, cParms = %d (calculated size %d)\n", cbData, pData->cParms, sizeof (VBoxGuestHGCMCallInfo) + pData->cParms * sizeof (VBoxGuestHGCMCallInfo)));
217
218 rc = vbglDriverIOCtl (&handle->driver, IOCTL_VBOXGUEST_HGCM_CALL, pData, cbData);
219
220 return rc;
221}
222
223#endif /* VBGL_VBOXGUEST */
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