VirtualBox

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

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

fixed includes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.6 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#include <VBox/VBoxGuestLib.h>
31#include "VBGLInternal.h"
32
33#include <iprt/assert.h>
34#include <iprt/semaphore.h>
35#include <iprt/string.h>
36
37#define VBGL_HGCM_ASSERTMsg AssertReleaseMsg
38
39int vbglHGCMInit (void)
40{
41 RTSemFastMutexCreate(&g_vbgldata.mutexHGCMHandle);
42
43 return VINF_SUCCESS;
44}
45
46int vbglHGCMTerminate (void)
47{
48 RTSemFastMutexDestroy(g_vbgldata.mutexHGCMHandle);
49
50 return VINF_SUCCESS;
51}
52
53DECLINLINE(int) vbglHandleHeapEnter (void)
54{
55 int rc = RTSemFastMutexRequest(g_vbgldata.mutexHGCMHandle);
56
57 VBGL_HGCM_ASSERTMsg(VBOX_SUCCESS(rc),
58 ("Failed to request handle heap mutex, rc = %Vrc\n", rc));
59
60 return rc;
61}
62
63DECLINLINE(void) vbglHandleHeapLeave (void)
64{
65 RTSemFastMutexRelease(g_vbgldata.mutexHGCMHandle);
66}
67
68struct VBGLHGCMHANDLEDATA *vbglHGCMHandleAlloc (void)
69{
70 struct VBGLHGCMHANDLEDATA *p;
71 int rc = vbglHandleHeapEnter ();
72 uint32_t i;
73
74 if (VBOX_FAILURE (rc))
75 return NULL;
76
77 p = NULL;
78
79 /** Simple linear search in array. This will be called not so often, only connect/disconnect.
80 * @todo bitmap for faster search and other obvious optimizations.
81 */
82
83 for (i = 0; i < ELEMENTS(g_vbgldata.aHGCMHandleData); i++)
84 {
85 if (!g_vbgldata.aHGCMHandleData[i].fAllocated)
86 {
87 p = &g_vbgldata.aHGCMHandleData[i];
88 p->fAllocated = 1;
89 break;
90 }
91 }
92
93 vbglHandleHeapLeave ();
94
95 VBGL_HGCM_ASSERTMsg(p != NULL,
96 ("Not enough HGCM handles.\n"));
97
98 return p;
99}
100
101void vbglHGCMHandleFree (struct VBGLHGCMHANDLEDATA *pHandle)
102{
103 int rc;
104
105 if (!pHandle)
106 return;
107
108 rc = vbglHandleHeapEnter ();
109
110 if (VBOX_FAILURE (rc))
111 return;
112
113 VBGL_HGCM_ASSERTMsg(pHandle->fAllocated,
114 ("Freeing not allocated handle.\n"));
115
116 memset(pHandle, 0, sizeof (struct VBGLHGCMHANDLEDATA));
117 vbglHandleHeapLeave ();
118 return;
119}
120
121DECLVBGL(int) VbglHGCMConnect (VBGLHGCMHANDLE *pHandle, VBoxGuestHGCMConnectInfo *pData)
122{
123 int rc;
124 struct VBGLHGCMHANDLEDATA *pHandleData;
125
126 if (!pHandle || !pData)
127 return VERR_INVALID_PARAMETER;
128
129 pHandleData = vbglHGCMHandleAlloc ();
130
131 rc = VINF_SUCCESS;
132
133 if (!pHandleData)
134 {
135 rc = VERR_NO_MEMORY;
136 }
137 else
138 {
139 rc = vbglDriverOpen (&pHandleData->driver);
140
141 if (VBOX_SUCCESS(rc))
142 {
143 rc = vbglDriverIOCtl (&pHandleData->driver, IOCTL_VBOXGUEST_HGCM_CONNECT, pData, sizeof (*pData));
144
145 if (VBOX_SUCCESS(rc))
146 {
147 *pHandle = pHandleData;
148 }
149 else
150 {
151 vbglDriverClose (&pHandleData->driver);
152 }
153 }
154
155 if (VBOX_FAILURE(rc))
156 {
157 vbglHGCMHandleFree (pHandleData);
158 }
159 }
160
161 return rc;
162}
163
164DECLVBGL(int) VbglHGCMDisconnect (VBGLHGCMHANDLE handle, VBoxGuestHGCMDisconnectInfo *pData)
165{
166 int rc = VINF_SUCCESS;
167
168 rc = vbglDriverIOCtl (&handle->driver, IOCTL_VBOXGUEST_HGCM_DISCONNECT, pData, sizeof (*pData));
169
170 vbglDriverClose (&handle->driver);
171
172 vbglHGCMHandleFree (handle);
173
174 return rc;
175}
176
177DECLVBGL(int) VbglHGCMCall (VBGLHGCMHANDLE handle, VBoxGuestHGCMCallInfo *pData, uint32_t cbData)
178{
179 int rc = VINF_SUCCESS;
180
181 VBGL_HGCM_ASSERTMsg(cbData >= sizeof (VBoxGuestHGCMCallInfo) + pData->cParms * sizeof (HGCMFunctionParameter),
182 ("cbData = %d, cParms = %d (calculated size %d)\n", cbData, pData->cParms, sizeof (VBoxGuestHGCMCallInfo) + pData->cParms * sizeof (VBoxGuestHGCMCallInfo)));
183
184 rc = vbglDriverIOCtl (&handle->driver, IOCTL_VBOXGUEST_HGCM_CALL, pData, cbData);
185
186 return rc;
187}
188
189#endif /* VBGL_VBOXGUEST */
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