1 | /* $Id: service.cpp 65102 2017-01-04 12:08:05Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Guest Control Service: Controlling the guest.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011-2016 Oracle Corporation
|
---|
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 | /** @page pg_svc_guest_control Guest Control HGCM Service
|
---|
19 | *
|
---|
20 | * This service acts as a proxy for handling and buffering host command requests
|
---|
21 | * and clients on the guest. It tries to be as transparent as possible to let
|
---|
22 | * the guest (client) and host side do their protocol handling as desired.
|
---|
23 | *
|
---|
24 | * The following terms are used:
|
---|
25 | * - Host: A host process (e.g. VBoxManage or another tool utilizing the Main API)
|
---|
26 | * which wants to control something on the guest.
|
---|
27 | * - Client: A client (e.g. VBoxService) running inside the guest OS waiting for
|
---|
28 | * new host commands to perform. There can be multiple clients connected
|
---|
29 | * to this service. A client is represented by its unique HGCM client ID.
|
---|
30 | * - Context ID: An (almost) unique ID automatically generated on the host (Main API)
|
---|
31 | * to not only distinguish clients but individual requests. Because
|
---|
32 | * the host does not know anything about connected clients it needs
|
---|
33 | * an indicator which it can refer to later. This context ID gets
|
---|
34 | * internally bound by the service to a client which actually processes
|
---|
35 | * the command in order to have a relationship between client<->context ID(s).
|
---|
36 | *
|
---|
37 | * The host can trigger commands which get buffered by the service (with full HGCM
|
---|
38 | * parameter info). As soon as a client connects (or is ready to do some new work)
|
---|
39 | * it gets a buffered host command to process it. This command then will be immediately
|
---|
40 | * removed from the command list. If there are ready clients but no new commands to be
|
---|
41 | * processed, these clients will be set into a deferred state (that is being blocked
|
---|
42 | * to return until a new command is available).
|
---|
43 | *
|
---|
44 | * If a client needs to inform the host that something happened, it can send a
|
---|
45 | * message to a low level HGCM callback registered in Main. This callback contains
|
---|
46 | * the actual data as well as the context ID to let the host do the next necessary
|
---|
47 | * steps for this context. This context ID makes it possible to wait for an event
|
---|
48 | * inside the host's Main API function (like starting a process on the guest and
|
---|
49 | * wait for getting its PID returned by the client) as well as cancelling blocking
|
---|
50 | * host calls in order the client terminated/crashed (HGCM detects disconnected
|
---|
51 | * clients and reports it to this service's callback).
|
---|
52 | *
|
---|
53 | * Starting at VBox 4.2 the context ID itself consists of a session ID, an object
|
---|
54 | * ID (for example a process or file ID) and a count. This is necessary to not break
|
---|
55 | * compatibility between older hosts and to manage guest session on the host.
|
---|
56 | */
|
---|
57 |
|
---|
58 |
|
---|
59 | /*********************************************************************************************************************************
|
---|
60 | * Header Files *
|
---|
61 | *********************************************************************************************************************************/
|
---|
62 | #ifdef LOG_GROUP
|
---|
63 | #undef LOG_GROUP
|
---|
64 | #endif
|
---|
65 | #define LOG_GROUP LOG_GROUP_GUEST_CONTROL
|
---|
66 | #include <VBox/HostServices/GuestControlSvc.h>
|
---|
67 |
|
---|
68 | #include <VBox/log.h>
|
---|
69 | #include <iprt/assert.h>
|
---|
70 | #include <iprt/cpp/autores.h>
|
---|
71 | #include <iprt/cpp/utils.h>
|
---|
72 | #include <iprt/err.h>
|
---|
73 | #include <iprt/mem.h>
|
---|
74 | #include <iprt/list.h>
|
---|
75 | #include <iprt/req.h>
|
---|
76 | #include <iprt/string.h>
|
---|
77 | #include <iprt/thread.h>
|
---|
78 | #include <iprt/time.h>
|
---|
79 |
|
---|
80 | #include <map>
|
---|
81 | #include <memory> /* for auto_ptr */
|
---|
82 | #include <string>
|
---|
83 | #include <list>
|
---|
84 |
|
---|
85 |
|
---|
86 | namespace guestControl {
|
---|
87 |
|
---|
88 | /** Flag for indicating that the client only is interested in
|
---|
89 | * messages of specific context IDs. */
|
---|
90 | #define CLIENTSTATE_FLAG_CONTEXTFILTER RT_BIT(0)
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Structure for maintaining a pending (that is, a deferred and not yet completed)
|
---|
94 | * client command.
|
---|
95 | */
|
---|
96 | typedef struct ClientConnection
|
---|
97 | {
|
---|
98 | /** The call handle */
|
---|
99 | VBOXHGCMCALLHANDLE mHandle;
|
---|
100 | /** Number of parameters */
|
---|
101 | uint32_t mNumParms;
|
---|
102 | /** The call parameters */
|
---|
103 | VBOXHGCMSVCPARM *mParms;
|
---|
104 | /** The standard constructor. */
|
---|
105 | ClientConnection(void)
|
---|
106 | : mHandle(0), mNumParms(0), mParms(NULL) {}
|
---|
107 | } ClientConnection;
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Structure for holding a buffered host command which has
|
---|
111 | * not been processed yet.
|
---|
112 | */
|
---|
113 | typedef struct HostCommand
|
---|
114 | {
|
---|
115 | RTLISTNODE Node;
|
---|
116 |
|
---|
117 | uint32_t AddRef(void)
|
---|
118 | {
|
---|
119 | #ifdef DEBUG_andy
|
---|
120 | LogFlowFunc(("Adding reference pHostCmd=%p, CID=%RU32, new refCount=%RU32\n",
|
---|
121 | this, mContextID, mRefCount + 1));
|
---|
122 | #endif
|
---|
123 | return ++mRefCount;
|
---|
124 | }
|
---|
125 |
|
---|
126 | uint32_t Release(void)
|
---|
127 | {
|
---|
128 | #ifdef DEBUG_andy
|
---|
129 | LogFlowFunc(("Releasing reference pHostCmd=%p, CID=%RU32, new refCount=%RU32\n",
|
---|
130 | this, mContextID, mRefCount - 1));
|
---|
131 | #endif
|
---|
132 | /* Release reference for current command. */
|
---|
133 | Assert(mRefCount);
|
---|
134 | if (--mRefCount == 0)
|
---|
135 | Free();
|
---|
136 |
|
---|
137 | return mRefCount;
|
---|
138 | }
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * Allocates the command with an HGCM request. Needs to be free'd using Free().
|
---|
142 | *
|
---|
143 | * @return IPRT status code.
|
---|
144 | * @param uMsg Message type.
|
---|
145 | * @param cParms Number of parameters of HGCM request.
|
---|
146 | * @param paParms Array of parameters of HGCM request.
|
---|
147 | */
|
---|
148 | int Allocate(uint32_t uMsg, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
|
---|
149 | {
|
---|
150 | LogFlowFunc(("Allocating pHostCmd=%p, uMsg=%RU32, cParms=%RU32, paParms=%p\n",
|
---|
151 | this, uMsg, cParms, paParms));
|
---|
152 |
|
---|
153 | if (!cParms) /* At least one parameter (context ID) must be present. */
|
---|
154 | return VERR_INVALID_PARAMETER;
|
---|
155 |
|
---|
156 | AssertPtrReturn(paParms, VERR_INVALID_POINTER);
|
---|
157 |
|
---|
158 | /* Paranoia. */
|
---|
159 | if (cParms > 256)
|
---|
160 | cParms = 256;
|
---|
161 |
|
---|
162 | int rc = VINF_SUCCESS;
|
---|
163 |
|
---|
164 | /*
|
---|
165 | * Don't verify anything here (yet), because this function only buffers
|
---|
166 | * the HGCM data into an internal structure and reaches it back to the guest (client)
|
---|
167 | * in an unmodified state.
|
---|
168 | */
|
---|
169 | mMsgType = uMsg;
|
---|
170 | mParmCount = cParms;
|
---|
171 | if (mParmCount)
|
---|
172 | {
|
---|
173 | mpParms = (VBOXHGCMSVCPARM*)RTMemAllocZ(sizeof(VBOXHGCMSVCPARM) * mParmCount);
|
---|
174 | if (NULL == mpParms)
|
---|
175 | rc = VERR_NO_MEMORY;
|
---|
176 | }
|
---|
177 |
|
---|
178 | if (RT_SUCCESS(rc))
|
---|
179 | {
|
---|
180 | for (uint32_t i = 0; i < mParmCount; i++)
|
---|
181 | {
|
---|
182 | mpParms[i].type = paParms[i].type;
|
---|
183 | switch (paParms[i].type)
|
---|
184 | {
|
---|
185 | case VBOX_HGCM_SVC_PARM_32BIT:
|
---|
186 | mpParms[i].u.uint32 = paParms[i].u.uint32;
|
---|
187 | break;
|
---|
188 |
|
---|
189 | case VBOX_HGCM_SVC_PARM_64BIT:
|
---|
190 | mpParms[i].u.uint64 = paParms[i].u.uint64;
|
---|
191 | break;
|
---|
192 |
|
---|
193 | case VBOX_HGCM_SVC_PARM_PTR:
|
---|
194 | mpParms[i].u.pointer.size = paParms[i].u.pointer.size;
|
---|
195 | if (mpParms[i].u.pointer.size > 0)
|
---|
196 | {
|
---|
197 | mpParms[i].u.pointer.addr = RTMemAlloc(mpParms[i].u.pointer.size);
|
---|
198 | if (NULL == mpParms[i].u.pointer.addr)
|
---|
199 | {
|
---|
200 | rc = VERR_NO_MEMORY;
|
---|
201 | break;
|
---|
202 | }
|
---|
203 | else
|
---|
204 | memcpy(mpParms[i].u.pointer.addr,
|
---|
205 | paParms[i].u.pointer.addr,
|
---|
206 | mpParms[i].u.pointer.size);
|
---|
207 | }
|
---|
208 | else
|
---|
209 | {
|
---|
210 | /* Size is 0 -- make sure we don't have any pointer. */
|
---|
211 | mpParms[i].u.pointer.addr = NULL;
|
---|
212 | }
|
---|
213 | break;
|
---|
214 |
|
---|
215 | default:
|
---|
216 | break;
|
---|
217 | }
|
---|
218 | if (RT_FAILURE(rc))
|
---|
219 | break;
|
---|
220 | }
|
---|
221 | }
|
---|
222 |
|
---|
223 | if (RT_SUCCESS(rc))
|
---|
224 | {
|
---|
225 | /*
|
---|
226 | * Assume that the context ID *always* is the first parameter,
|
---|
227 | * assign the context ID to the command.
|
---|
228 | */
|
---|
229 | rc = mpParms[0].getUInt32(&mContextID);
|
---|
230 |
|
---|
231 | /* Set timestamp so that clients can distinguish between already
|
---|
232 | * processed commands and new ones. */
|
---|
233 | mTimestamp = RTTimeNanoTS();
|
---|
234 | }
|
---|
235 |
|
---|
236 | LogFlowFunc(("Returned with rc=%Rrc\n", rc));
|
---|
237 | return rc;
|
---|
238 | }
|
---|
239 |
|
---|
240 | /**
|
---|
241 | * Frees the buffered HGCM request.
|
---|
242 | *
|
---|
243 | * @return IPRT status code.
|
---|
244 | */
|
---|
245 | void Free(void)
|
---|
246 | {
|
---|
247 | AssertMsg(mRefCount == 0, ("pHostCmd=%p, CID=%RU32 still being used by a client (%RU32 refs), cannot free yet\n",
|
---|
248 | this, mContextID, mRefCount));
|
---|
249 |
|
---|
250 | LogFlowFunc(("Freeing host command pHostCmd=%p, CID=%RU32, mMsgType=%RU32, mParmCount=%RU32, mpParms=%p\n",
|
---|
251 | this, mContextID, mMsgType, mParmCount, mpParms));
|
---|
252 |
|
---|
253 | for (uint32_t i = 0; i < mParmCount; i++)
|
---|
254 | {
|
---|
255 | switch (mpParms[i].type)
|
---|
256 | {
|
---|
257 | case VBOX_HGCM_SVC_PARM_PTR:
|
---|
258 | if (mpParms[i].u.pointer.size > 0)
|
---|
259 | RTMemFree(mpParms[i].u.pointer.addr);
|
---|
260 | break;
|
---|
261 |
|
---|
262 | default:
|
---|
263 | break;
|
---|
264 | }
|
---|
265 | }
|
---|
266 |
|
---|
267 | if (mpParms)
|
---|
268 | {
|
---|
269 | RTMemFree(mpParms);
|
---|
270 | mpParms = NULL;
|
---|
271 | }
|
---|
272 |
|
---|
273 | mParmCount = 0;
|
---|
274 |
|
---|
275 | /* Removes the command from its list */
|
---|
276 | RTListNodeRemove(&Node);
|
---|
277 | }
|
---|
278 |
|
---|
279 | /**
|
---|
280 | * Copies data from the buffered HGCM request to the current HGCM request.
|
---|
281 | *
|
---|
282 | * @return IPRT status code.
|
---|
283 | * @param paDstParms Array of parameters of HGCM request to fill the data into.
|
---|
284 | * @param cDstParms Number of parameters the HGCM request can handle.
|
---|
285 | */
|
---|
286 | int CopyTo(VBOXHGCMSVCPARM paDstParms[], uint32_t cDstParms) const
|
---|
287 | {
|
---|
288 | LogFlowFunc(("pHostCmd=%p, mMsgType=%RU32, mParmCount=%RU32, mContextID=%RU32 (Session %RU32)\n",
|
---|
289 | this, mMsgType, mParmCount, mContextID, VBOX_GUESTCTRL_CONTEXTID_GET_SESSION(mContextID)));
|
---|
290 |
|
---|
291 | int rc = VINF_SUCCESS;
|
---|
292 | if (cDstParms != mParmCount)
|
---|
293 | {
|
---|
294 | LogFlowFunc(("Parameter count does not match (got %RU32, expected %RU32)\n",
|
---|
295 | cDstParms, mParmCount));
|
---|
296 | rc = VERR_INVALID_PARAMETER;
|
---|
297 | }
|
---|
298 |
|
---|
299 | if (RT_SUCCESS(rc))
|
---|
300 | {
|
---|
301 | for (uint32_t i = 0; i < mParmCount; i++)
|
---|
302 | {
|
---|
303 | if (paDstParms[i].type != mpParms[i].type)
|
---|
304 | {
|
---|
305 | LogFlowFunc(("Parameter %RU32 type mismatch (got %RU32, expected %RU32)\n",
|
---|
306 | i, paDstParms[i].type, mpParms[i].type));
|
---|
307 | rc = VERR_INVALID_PARAMETER;
|
---|
308 | }
|
---|
309 | else
|
---|
310 | {
|
---|
311 | switch (mpParms[i].type)
|
---|
312 | {
|
---|
313 | case VBOX_HGCM_SVC_PARM_32BIT:
|
---|
314 | #ifdef DEBUG_andy
|
---|
315 | LogFlowFunc(("\tmpParms[%RU32] = %RU32 (uint32_t)\n",
|
---|
316 | i, mpParms[i].u.uint32));
|
---|
317 | #endif
|
---|
318 | paDstParms[i].u.uint32 = mpParms[i].u.uint32;
|
---|
319 | break;
|
---|
320 |
|
---|
321 | case VBOX_HGCM_SVC_PARM_64BIT:
|
---|
322 | #ifdef DEBUG_andy
|
---|
323 | LogFlowFunc(("\tmpParms[%RU32] = %RU64 (uint64_t)\n",
|
---|
324 | i, mpParms[i].u.uint64));
|
---|
325 | #endif
|
---|
326 | paDstParms[i].u.uint64 = mpParms[i].u.uint64;
|
---|
327 | break;
|
---|
328 |
|
---|
329 | case VBOX_HGCM_SVC_PARM_PTR:
|
---|
330 | {
|
---|
331 | #ifdef DEBUG_andy
|
---|
332 | LogFlowFunc(("\tmpParms[%RU32] = %p (ptr), size = %RU32\n",
|
---|
333 | i, mpParms[i].u.pointer.addr, mpParms[i].u.pointer.size));
|
---|
334 | #endif
|
---|
335 | if (!mpParms[i].u.pointer.size)
|
---|
336 | continue; /* Only copy buffer if there actually is something to copy. */
|
---|
337 |
|
---|
338 | if (!paDstParms[i].u.pointer.addr)
|
---|
339 | rc = VERR_INVALID_PARAMETER;
|
---|
340 |
|
---|
341 | if ( RT_SUCCESS(rc)
|
---|
342 | && paDstParms[i].u.pointer.size < mpParms[i].u.pointer.size)
|
---|
343 | rc = VERR_BUFFER_OVERFLOW;
|
---|
344 |
|
---|
345 | if (RT_SUCCESS(rc))
|
---|
346 | {
|
---|
347 | memcpy(paDstParms[i].u.pointer.addr,
|
---|
348 | mpParms[i].u.pointer.addr,
|
---|
349 | mpParms[i].u.pointer.size);
|
---|
350 | }
|
---|
351 |
|
---|
352 | break;
|
---|
353 | }
|
---|
354 |
|
---|
355 | default:
|
---|
356 | LogFlowFunc(("Parameter %RU32 of type %RU32 is not supported yet\n",
|
---|
357 | i, mpParms[i].type));
|
---|
358 | rc = VERR_NOT_SUPPORTED;
|
---|
359 | break;
|
---|
360 | }
|
---|
361 | }
|
---|
362 |
|
---|
363 | if (RT_FAILURE(rc))
|
---|
364 | {
|
---|
365 | LogFlowFunc(("Parameter %RU32 invalid (%Rrc), refusing\n",
|
---|
366 | i, rc));
|
---|
367 | break;
|
---|
368 | }
|
---|
369 | }
|
---|
370 | }
|
---|
371 |
|
---|
372 | LogFlowFunc(("Returned with rc=%Rrc\n", rc));
|
---|
373 | return rc;
|
---|
374 | }
|
---|
375 |
|
---|
376 | int Assign(const ClientConnection *pConnection)
|
---|
377 | {
|
---|
378 | AssertPtrReturn(pConnection, VERR_INVALID_POINTER);
|
---|
379 |
|
---|
380 | int rc;
|
---|
381 |
|
---|
382 | LogFlowFunc(("pHostCmd=%p, mMsgType=%RU32, mParmCount=%RU32, mpParms=%p\n",
|
---|
383 | this, mMsgType, mParmCount, mpParms));
|
---|
384 |
|
---|
385 | /* Does the current host command need more parameter space which
|
---|
386 | * the client does not provide yet? */
|
---|
387 | if (mParmCount > pConnection->mNumParms)
|
---|
388 | {
|
---|
389 | LogFlowFunc(("pHostCmd=%p requires %RU32 parms, only got %RU32 from client\n",
|
---|
390 | this, mParmCount, pConnection->mNumParms));
|
---|
391 |
|
---|
392 | /*
|
---|
393 | * So this call apparently failed because the guest wanted to peek
|
---|
394 | * how much parameters it has to supply in order to successfully retrieve
|
---|
395 | * this command. Let's tell him so!
|
---|
396 | */
|
---|
397 | rc = VERR_TOO_MUCH_DATA;
|
---|
398 | }
|
---|
399 | else
|
---|
400 | {
|
---|
401 | rc = CopyTo(pConnection->mParms, pConnection->mNumParms);
|
---|
402 |
|
---|
403 | /*
|
---|
404 | * Has there been enough parameter space but the wrong parameter types
|
---|
405 | * were submitted -- maybe the client was just asking for the next upcoming
|
---|
406 | * host message?
|
---|
407 | *
|
---|
408 | * Note: To keep this compatible to older clients we return VERR_TOO_MUCH_DATA
|
---|
409 | * in every case.
|
---|
410 | */
|
---|
411 | if (RT_FAILURE(rc))
|
---|
412 | rc = VERR_TOO_MUCH_DATA;
|
---|
413 | }
|
---|
414 |
|
---|
415 | return rc;
|
---|
416 | }
|
---|
417 |
|
---|
418 | int Peek(const ClientConnection *pConnection)
|
---|
419 | {
|
---|
420 | AssertPtrReturn(pConnection, VERR_INVALID_POINTER);
|
---|
421 |
|
---|
422 | LogFlowFunc(("pHostCmd=%p, mMsgType=%RU32, mParmCount=%RU32, mpParms=%p\n",
|
---|
423 | this, mMsgType, mParmCount, mpParms));
|
---|
424 |
|
---|
425 | if (pConnection->mNumParms >= 2)
|
---|
426 | {
|
---|
427 | pConnection->mParms[0].setUInt32(mMsgType); /* Message ID */
|
---|
428 | pConnection->mParms[1].setUInt32(mParmCount); /* Required parameters for message */
|
---|
429 | }
|
---|
430 | else
|
---|
431 | LogFlowFunc(("Warning: Client has not (yet) submitted enough parameters (%RU32, must be at least 2) to at least peak for the next message\n",
|
---|
432 | pConnection->mNumParms));
|
---|
433 |
|
---|
434 | /*
|
---|
435 | * Always return VERR_TOO_MUCH_DATA data here to
|
---|
436 | * keep it compatible with older clients and to
|
---|
437 | * have correct accounting (mHostRc + mHostCmdTries).
|
---|
438 | */
|
---|
439 | return VERR_TOO_MUCH_DATA;
|
---|
440 | }
|
---|
441 |
|
---|
442 | /** Reference count for keeping track how many connected
|
---|
443 | * clients still need to process this command until it can
|
---|
444 | * be removed. */
|
---|
445 | uint32_t mRefCount;
|
---|
446 | /** The context ID this command belongs to. Will be extracted
|
---|
447 | * *always* from HGCM parameter [0]. */
|
---|
448 | uint32_t mContextID;
|
---|
449 | /** Dynamic structure for holding the HGCM parms */
|
---|
450 | uint32_t mMsgType;
|
---|
451 | /** Number of HGCM parameters. */
|
---|
452 | uint32_t mParmCount;
|
---|
453 | /** Array of HGCM parameters. */
|
---|
454 | PVBOXHGCMSVCPARM mpParms;
|
---|
455 | /** Incoming timestamp (us). */
|
---|
456 | uint64_t mTimestamp;
|
---|
457 | } HostCommand;
|
---|
458 | typedef std::list< HostCommand *> HostCmdList;
|
---|
459 | typedef std::list< HostCommand *>::iterator HostCmdListIter;
|
---|
460 | typedef std::list< HostCommand *>::const_iterator HostCmdListIterConst;
|
---|
461 |
|
---|
462 | /**
|
---|
463 | * Per-client structure used for book keeping/state tracking a
|
---|
464 | * certain host command.
|
---|
465 | */
|
---|
466 | typedef struct ClientContext
|
---|
467 | {
|
---|
468 | /* Pointer to list node of this command. */
|
---|
469 | HostCommand *mpHostCmd;
|
---|
470 | /** The standard constructor. */
|
---|
471 | ClientContext(void) : mpHostCmd(NULL) {}
|
---|
472 | /** Internal constrcutor. */
|
---|
473 | ClientContext(HostCommand *pHostCmd) : mpHostCmd(pHostCmd) {}
|
---|
474 | } ClientContext;
|
---|
475 | typedef std::map< uint32_t, ClientContext > ClientContextMap;
|
---|
476 | typedef std::map< uint32_t, ClientContext >::iterator ClientContextMapIter;
|
---|
477 | typedef std::map< uint32_t, ClientContext >::const_iterator ClientContextMapIterConst;
|
---|
478 |
|
---|
479 | /**
|
---|
480 | * Structure for holding a connected guest client
|
---|
481 | * state.
|
---|
482 | */
|
---|
483 | typedef struct ClientState
|
---|
484 | {
|
---|
485 | ClientState(void)
|
---|
486 | : mSvcHelpers(NULL),
|
---|
487 | mID(0),
|
---|
488 | mFlags(0),
|
---|
489 | mFilterMask(0), mFilterValue(0),
|
---|
490 | mHostCmdRc(VINF_SUCCESS), mHostCmdTries(0),
|
---|
491 | mHostCmdTS(0),
|
---|
492 | mIsPending(false),
|
---|
493 | mPeekCount(0) { }
|
---|
494 |
|
---|
495 | ClientState(PVBOXHGCMSVCHELPERS pSvcHelpers, uint32_t uClientID)
|
---|
496 | : mSvcHelpers(pSvcHelpers),
|
---|
497 | mID(uClientID),
|
---|
498 | mFlags(0),
|
---|
499 | mFilterMask(0), mFilterValue(0),
|
---|
500 | mHostCmdRc(VINF_SUCCESS), mHostCmdTries(0),
|
---|
501 | mHostCmdTS(0),
|
---|
502 | mIsPending(false),
|
---|
503 | mPeekCount(0){ }
|
---|
504 |
|
---|
505 | void DequeueAll(void)
|
---|
506 | {
|
---|
507 | HostCmdListIter curItem = mHostCmdList.begin();
|
---|
508 | while (curItem != mHostCmdList.end())
|
---|
509 | curItem = Dequeue(curItem);
|
---|
510 | }
|
---|
511 |
|
---|
512 | void DequeueCurrent(void)
|
---|
513 | {
|
---|
514 | HostCmdListIter curCmd = mHostCmdList.begin();
|
---|
515 | if (curCmd != mHostCmdList.end())
|
---|
516 | Dequeue(curCmd);
|
---|
517 | }
|
---|
518 |
|
---|
519 | HostCmdListIter Dequeue(HostCmdListIter &curItem)
|
---|
520 | {
|
---|
521 | HostCommand *pHostCmd = (*curItem);
|
---|
522 | AssertPtr(pHostCmd);
|
---|
523 |
|
---|
524 | if (pHostCmd->Release() == 0)
|
---|
525 | {
|
---|
526 | LogFlowFunc(("[Client %RU32] Destroying pHostCmd=%p\n",
|
---|
527 | mID, (*curItem)));
|
---|
528 |
|
---|
529 | delete pHostCmd;
|
---|
530 | pHostCmd = NULL;
|
---|
531 | }
|
---|
532 |
|
---|
533 | HostCmdListIter nextItem = mHostCmdList.erase(curItem);
|
---|
534 |
|
---|
535 | /* Reset everything else. */
|
---|
536 | mHostCmdRc = VINF_SUCCESS;
|
---|
537 | mHostCmdTries = 0;
|
---|
538 | mPeekCount = 0;
|
---|
539 |
|
---|
540 | return nextItem;
|
---|
541 | }
|
---|
542 |
|
---|
543 | int EnqueueCommand(HostCommand *pHostCmd)
|
---|
544 | {
|
---|
545 | AssertPtrReturn(pHostCmd, VERR_INVALID_POINTER);
|
---|
546 |
|
---|
547 | int rc = VINF_SUCCESS;
|
---|
548 |
|
---|
549 | try
|
---|
550 | {
|
---|
551 | mHostCmdList.push_back(pHostCmd);
|
---|
552 | pHostCmd->AddRef();
|
---|
553 | }
|
---|
554 | catch (std::bad_alloc)
|
---|
555 | {
|
---|
556 | rc = VERR_NO_MEMORY;
|
---|
557 | }
|
---|
558 |
|
---|
559 | return rc;
|
---|
560 | }
|
---|
561 |
|
---|
562 | bool WantsHostCommand(const HostCommand *pHostCmd) const
|
---|
563 | {
|
---|
564 | AssertPtrReturn(pHostCmd, false);
|
---|
565 |
|
---|
566 | #ifdef DEBUG_andy
|
---|
567 | LogFlowFunc(("mHostCmdTS=%RU64, pHostCmdTS=%RU64\n",
|
---|
568 | mHostCmdTS, pHostCmd->mTimestamp));
|
---|
569 | #endif
|
---|
570 |
|
---|
571 | /* Only process newer commands. */
|
---|
572 | if (pHostCmd->mTimestamp <= mHostCmdTS)
|
---|
573 | return false;
|
---|
574 |
|
---|
575 | /*
|
---|
576 | * If a sesseion filter is set, only obey those commands we're interested in
|
---|
577 | * by applying our context ID filter mask and compare the result with the
|
---|
578 | * original context ID.
|
---|
579 | */
|
---|
580 | bool fWant;
|
---|
581 | if (mFlags & CLIENTSTATE_FLAG_CONTEXTFILTER)
|
---|
582 | {
|
---|
583 | fWant = (pHostCmd->mContextID & mFilterMask) == mFilterValue;
|
---|
584 | }
|
---|
585 | else /* Client is interested in all commands. */
|
---|
586 | fWant = true;
|
---|
587 |
|
---|
588 | LogFlowFunc(("[Client %RU32] mFlags=0x%x, mContextID=%RU32 (session %RU32), mFilterMask=0x%x, mFilterValue=%RU32, fWant=%RTbool\n",
|
---|
589 | mID, mFlags, pHostCmd->mContextID,
|
---|
590 | VBOX_GUESTCTRL_CONTEXTID_GET_SESSION(pHostCmd->mContextID),
|
---|
591 | mFilterMask, mFilterValue, fWant));
|
---|
592 |
|
---|
593 | return fWant;
|
---|
594 | }
|
---|
595 |
|
---|
596 | int SetPending(const ClientConnection *pConnection)
|
---|
597 | {
|
---|
598 | AssertPtrReturn(pConnection, VERR_INVALID_POINTER);
|
---|
599 |
|
---|
600 | if (mIsPending)
|
---|
601 | {
|
---|
602 | LogFlowFunc(("[Client %RU32] Already is in pending mode\n", mID));
|
---|
603 |
|
---|
604 | /*
|
---|
605 | * Signal that we don't and can't return yet.
|
---|
606 | */
|
---|
607 | return VINF_HGCM_ASYNC_EXECUTE;
|
---|
608 | }
|
---|
609 |
|
---|
610 | if (mHostCmdList.empty())
|
---|
611 | {
|
---|
612 | AssertMsg(mIsPending == false,
|
---|
613 | ("Client ID=%RU32 already is pending but tried to receive a new host command\n", mID));
|
---|
614 |
|
---|
615 | mPendingCon.mHandle = pConnection->mHandle;
|
---|
616 | mPendingCon.mNumParms = pConnection->mNumParms;
|
---|
617 | mPendingCon.mParms = pConnection->mParms;
|
---|
618 |
|
---|
619 | mIsPending = true;
|
---|
620 |
|
---|
621 | LogFlowFunc(("[Client %RU32] Is now in pending mode\n", mID));
|
---|
622 |
|
---|
623 | /*
|
---|
624 | * Signal that we don't and can't return yet.
|
---|
625 | */
|
---|
626 | return VINF_HGCM_ASYNC_EXECUTE;
|
---|
627 | }
|
---|
628 |
|
---|
629 | /*
|
---|
630 | * Signal that there already is a connection pending.
|
---|
631 | * Shouldn't happen in daily usage.
|
---|
632 | */
|
---|
633 | AssertMsgFailed(("Client already has a connection pending\n"));
|
---|
634 | return VERR_SIGNAL_PENDING;
|
---|
635 | }
|
---|
636 |
|
---|
637 | int Run(const ClientConnection *pConnection,
|
---|
638 | HostCommand *pHostCmd)
|
---|
639 | {
|
---|
640 | AssertPtrReturn(pConnection, VERR_INVALID_POINTER);
|
---|
641 | AssertPtrReturn(pHostCmd, VERR_INVALID_POINTER);
|
---|
642 |
|
---|
643 | int rc = VINF_SUCCESS;
|
---|
644 |
|
---|
645 | LogFlowFunc(("[Client %RU32] pConnection=%p, mHostCmdRc=%Rrc, mHostCmdTries=%RU32, mPeekCount=%RU32\n",
|
---|
646 | mID, pConnection, mHostCmdRc, mHostCmdTries, mPeekCount));
|
---|
647 |
|
---|
648 | mHostCmdRc = SendReply(pConnection, pHostCmd);
|
---|
649 | LogFlowFunc(("[Client %RU32] Processing pHostCmd=%p ended with rc=%Rrc\n",
|
---|
650 | mID, pHostCmd, mHostCmdRc));
|
---|
651 |
|
---|
652 | bool fRemove = false;
|
---|
653 | if (RT_FAILURE(mHostCmdRc))
|
---|
654 | {
|
---|
655 | mHostCmdTries++;
|
---|
656 |
|
---|
657 | /*
|
---|
658 | * If the client understood the message but supplied too little buffer space
|
---|
659 | * don't send this message again and drop it after 6 unsuccessful attempts.
|
---|
660 | *
|
---|
661 | * Note: Due to legacy reasons this the retry counter has to be even because on
|
---|
662 | * every peek there will be the actual command retrieval from the client side.
|
---|
663 | * To not get the actual command if the client actually only wants to peek for
|
---|
664 | * the next command, there needs to be two rounds per try, e.g. 3 rounds = 6 tries.
|
---|
665 | *
|
---|
666 | ** @todo Fix the mess stated above. GUEST_MSG_WAIT should be become GUEST_MSG_PEEK, *only*
|
---|
667 | * (and every time) returning the next upcoming host command (if any, blocking). Then
|
---|
668 | * it's up to the client what to do next, either peeking again or getting the actual
|
---|
669 | * host command via an own GUEST_ type message.
|
---|
670 | */
|
---|
671 | if (mHostCmdRc == VERR_TOO_MUCH_DATA)
|
---|
672 | {
|
---|
673 | if (mHostCmdTries == 6)
|
---|
674 | fRemove = true;
|
---|
675 | }
|
---|
676 | /* Client did not understand the message or something else weird happened. Try again one
|
---|
677 | * more time and drop it if it didn't get handled then. */
|
---|
678 | else if (mHostCmdTries > 1)
|
---|
679 | fRemove = true;
|
---|
680 | }
|
---|
681 | else
|
---|
682 | fRemove = true; /* Everything went fine, remove it. */
|
---|
683 |
|
---|
684 | LogFlowFunc(("[Client %RU32] Tried pHostCmd=%p for %RU32 times, (last result=%Rrc, fRemove=%RTbool)\n",
|
---|
685 | mID, pHostCmd, mHostCmdTries, mHostCmdRc, fRemove));
|
---|
686 |
|
---|
687 | if (RT_SUCCESS(rc))
|
---|
688 | rc = mHostCmdRc;
|
---|
689 |
|
---|
690 | if (fRemove)
|
---|
691 | {
|
---|
692 | /** @todo Fix this (slow) lookup. Too late today. */
|
---|
693 | HostCmdListIter curItem = mHostCmdList.begin();
|
---|
694 | while (curItem != mHostCmdList.end())
|
---|
695 | {
|
---|
696 | if ((*curItem) == pHostCmd)
|
---|
697 | {
|
---|
698 | Dequeue(curItem);
|
---|
699 | break;
|
---|
700 | }
|
---|
701 |
|
---|
702 | ++curItem;
|
---|
703 | }
|
---|
704 | }
|
---|
705 |
|
---|
706 | LogFlowFunc(("[Client %RU32] Returned with rc=%Rrc\n", mID, rc));
|
---|
707 | return rc;
|
---|
708 | }
|
---|
709 |
|
---|
710 | int RunCurrent(const ClientConnection *pConnection)
|
---|
711 | {
|
---|
712 | AssertPtrReturn(pConnection, VERR_INVALID_POINTER);
|
---|
713 |
|
---|
714 | int rc;
|
---|
715 | if (mHostCmdList.empty())
|
---|
716 | {
|
---|
717 | rc = SetPending(pConnection);
|
---|
718 | }
|
---|
719 | else
|
---|
720 | {
|
---|
721 | AssertMsgReturn(!mIsPending,
|
---|
722 | ("Client ID=%RU32 still is in pending mode; can't use another connection\n", mID), VERR_INVALID_PARAMETER);
|
---|
723 |
|
---|
724 | HostCmdListIter curCmd = mHostCmdList.begin();
|
---|
725 | Assert(curCmd != mHostCmdList.end());
|
---|
726 | HostCommand *pHostCmd = (*curCmd);
|
---|
727 | AssertPtrReturn(pHostCmd, VERR_INVALID_POINTER);
|
---|
728 |
|
---|
729 | rc = Run(pConnection, pHostCmd);
|
---|
730 | }
|
---|
731 |
|
---|
732 | return rc;
|
---|
733 | }
|
---|
734 |
|
---|
735 | int Wakeup(void)
|
---|
736 | {
|
---|
737 | int rc = VINF_NO_CHANGE;
|
---|
738 |
|
---|
739 | if (mIsPending)
|
---|
740 | {
|
---|
741 | LogFlowFunc(("[Client %RU32] Waking up ...\n", mID));
|
---|
742 |
|
---|
743 | rc = VINF_SUCCESS;
|
---|
744 |
|
---|
745 | HostCmdListIter curCmd = mHostCmdList.begin();
|
---|
746 | if (curCmd != mHostCmdList.end())
|
---|
747 | {
|
---|
748 | HostCommand *pHostCmd = (*curCmd);
|
---|
749 | AssertPtrReturn(pHostCmd, VERR_INVALID_POINTER);
|
---|
750 |
|
---|
751 | LogFlowFunc(("[Client %RU32] Current host command is pHostCmd=%p, CID=%RU32, cmdType=%RU32, cmdParms=%RU32, refCount=%RU32\n",
|
---|
752 | mID, pHostCmd, pHostCmd->mContextID, pHostCmd->mMsgType, pHostCmd->mParmCount, pHostCmd->mRefCount));
|
---|
753 |
|
---|
754 | rc = Run(&mPendingCon, pHostCmd);
|
---|
755 | }
|
---|
756 | else
|
---|
757 | AssertMsgFailed(("Waking up client ID=%RU32 with no host command in queue is a bad idea\n", mID));
|
---|
758 |
|
---|
759 | return rc;
|
---|
760 | }
|
---|
761 |
|
---|
762 | return VINF_NO_CHANGE;
|
---|
763 | }
|
---|
764 |
|
---|
765 | int CancelWaiting(int rcPending)
|
---|
766 | {
|
---|
767 | LogFlowFunc(("[Client %RU32] Cancelling waiting with %Rrc, isPending=%RTbool, pendingNumParms=%RU32, flags=%x\n",
|
---|
768 | mID, rcPending, mIsPending, mPendingCon.mNumParms, mFlags));
|
---|
769 |
|
---|
770 | int rc;
|
---|
771 | if ( mIsPending
|
---|
772 | && mPendingCon.mNumParms >= 2)
|
---|
773 | {
|
---|
774 | mPendingCon.mParms[0].setUInt32(HOST_CANCEL_PENDING_WAITS); /* Message ID. */
|
---|
775 | mPendingCon.mParms[1].setUInt32(0); /* Required parameters for message. */
|
---|
776 |
|
---|
777 | AssertPtr(mSvcHelpers);
|
---|
778 | mSvcHelpers->pfnCallComplete(mPendingCon.mHandle, rcPending);
|
---|
779 |
|
---|
780 | mIsPending = false;
|
---|
781 |
|
---|
782 | rc = VINF_SUCCESS;
|
---|
783 | }
|
---|
784 | else if (mPendingCon.mNumParms < 2)
|
---|
785 | rc = VERR_BUFFER_OVERFLOW;
|
---|
786 | else /** @todo Enqueue command instead of dropping? */
|
---|
787 | rc = VERR_WRONG_ORDER;
|
---|
788 |
|
---|
789 | return rc;
|
---|
790 | }
|
---|
791 |
|
---|
792 | int SendReply(ClientConnection const *pConnection,
|
---|
793 | HostCommand *pHostCmd)
|
---|
794 | {
|
---|
795 | AssertPtrReturn(pConnection, VERR_INVALID_POINTER);
|
---|
796 | AssertPtrReturn(pHostCmd, VERR_INVALID_POINTER);
|
---|
797 |
|
---|
798 | int rc;
|
---|
799 | /* If the client is in pending mode, always send back
|
---|
800 | * the peek result first. */
|
---|
801 | if (mIsPending)
|
---|
802 | {
|
---|
803 | rc = pHostCmd->Peek(pConnection);
|
---|
804 | mPeekCount++;
|
---|
805 | }
|
---|
806 | else
|
---|
807 | {
|
---|
808 | /* If this is the very first peek, make sure to *always* give back the peeking answer
|
---|
809 | * instead of the actual command, even if this command would fit into the current
|
---|
810 | * connection buffer. */
|
---|
811 | if (!mPeekCount)
|
---|
812 | {
|
---|
813 | rc = pHostCmd->Peek(pConnection);
|
---|
814 | mPeekCount++;
|
---|
815 | }
|
---|
816 | else
|
---|
817 | {
|
---|
818 | /* Try assigning the host command to the client and store the
|
---|
819 | * result code for later use. */
|
---|
820 | rc = pHostCmd->Assign(pConnection);
|
---|
821 | if (RT_FAILURE(rc)) /* If something failed, let the client peek (again). */
|
---|
822 | {
|
---|
823 | rc = pHostCmd->Peek(pConnection);
|
---|
824 | mPeekCount++;
|
---|
825 | }
|
---|
826 | else
|
---|
827 | mPeekCount = 0;
|
---|
828 | }
|
---|
829 | }
|
---|
830 |
|
---|
831 | /* Reset pending status. */
|
---|
832 | mIsPending = false;
|
---|
833 |
|
---|
834 | /* In any case the client did something, so complete
|
---|
835 | * the pending call with the result we just got. */
|
---|
836 | AssertPtr(mSvcHelpers);
|
---|
837 | mSvcHelpers->pfnCallComplete(pConnection->mHandle, rc);
|
---|
838 |
|
---|
839 | LogFlowFunc(("[Client %RU32] mPeekCount=%RU32, pConnection=%p, pHostCmd=%p, replyRc=%Rrc\n",
|
---|
840 | mID, mPeekCount, pConnection, pHostCmd, rc));
|
---|
841 | return rc;
|
---|
842 | }
|
---|
843 |
|
---|
844 | PVBOXHGCMSVCHELPERS mSvcHelpers;
|
---|
845 | /** The client's ID. */
|
---|
846 | uint32_t mID;
|
---|
847 | /** Client flags. @sa CLIENTSTATE_FLAG_ flags. */
|
---|
848 | uint32_t mFlags;
|
---|
849 | /** The context ID filter mask, if any. */
|
---|
850 | uint32_t mFilterMask;
|
---|
851 | /** The context ID filter value, if any. */
|
---|
852 | uint32_t mFilterValue;
|
---|
853 | /** Host command list to process. */
|
---|
854 | HostCmdList mHostCmdList;
|
---|
855 | /** Last (most recent) rc after handling the
|
---|
856 | * host command. */
|
---|
857 | int mHostCmdRc;
|
---|
858 | /** How many times the host service has tried to deliver this
|
---|
859 | * command to the according client. */
|
---|
860 | uint32_t mHostCmdTries;
|
---|
861 | /** Timestamp (us) of last host command processed. */
|
---|
862 | uint64_t mHostCmdTS;
|
---|
863 | /**
|
---|
864 | * Flag indicating whether the client currently is pending.
|
---|
865 | * This means the client waits for a new host command to reply
|
---|
866 | * and won't return from the waiting call until a new host
|
---|
867 | * command is available.
|
---|
868 | */
|
---|
869 | bool mIsPending;
|
---|
870 | /**
|
---|
871 | * This is necessary for being compatible with older
|
---|
872 | * Guest Additions. In case there are commands which only
|
---|
873 | * have two (2) parameters and therefore would fit into the
|
---|
874 | * GUEST_MSG_WAIT reply immediately, we now can make sure
|
---|
875 | * that the client first gets back the GUEST_MSG_WAIT results
|
---|
876 | * first.
|
---|
877 | */
|
---|
878 | uint32_t mPeekCount;
|
---|
879 | /** The client's pending connection. */
|
---|
880 | ClientConnection mPendingCon;
|
---|
881 | } ClientState;
|
---|
882 | typedef std::map< uint32_t, ClientState > ClientStateMap;
|
---|
883 | typedef std::map< uint32_t, ClientState >::iterator ClientStateMapIter;
|
---|
884 | typedef std::map< uint32_t, ClientState >::const_iterator ClientStateMapIterConst;
|
---|
885 |
|
---|
886 | /**
|
---|
887 | * Class containing the shared information service functionality.
|
---|
888 | */
|
---|
889 | class Service : public RTCNonCopyable
|
---|
890 | {
|
---|
891 |
|
---|
892 | private:
|
---|
893 |
|
---|
894 | /** Type definition for use in callback functions. */
|
---|
895 | typedef Service SELF;
|
---|
896 | /** HGCM helper functions. */
|
---|
897 | PVBOXHGCMSVCHELPERS mpHelpers;
|
---|
898 | /**
|
---|
899 | * Callback function supplied by the host for notification of updates
|
---|
900 | * to properties.
|
---|
901 | */
|
---|
902 | PFNHGCMSVCEXT mpfnHostCallback;
|
---|
903 | /** User data pointer to be supplied to the host callback function. */
|
---|
904 | void *mpvHostData;
|
---|
905 | /** List containing all buffered host commands. */
|
---|
906 | RTLISTANCHOR mHostCmdList;
|
---|
907 | /** Map containing all connected clients. The primary key contains
|
---|
908 | * the HGCM client ID to identify the client. */
|
---|
909 | ClientStateMap mClientStateMap;
|
---|
910 | public:
|
---|
911 | explicit Service(PVBOXHGCMSVCHELPERS pHelpers)
|
---|
912 | : mpHelpers(pHelpers)
|
---|
913 | , mpfnHostCallback(NULL)
|
---|
914 | , mpvHostData(NULL)
|
---|
915 | {
|
---|
916 | RTListInit(&mHostCmdList);
|
---|
917 | }
|
---|
918 |
|
---|
919 | /**
|
---|
920 | * @interface_method_impl{VBOXHGCMSVCFNTABLE,pfnUnload}
|
---|
921 | * Simply deletes the service object
|
---|
922 | */
|
---|
923 | static DECLCALLBACK(int) svcUnload(void *pvService)
|
---|
924 | {
|
---|
925 | AssertLogRelReturn(VALID_PTR(pvService), VERR_INVALID_PARAMETER);
|
---|
926 | SELF *pSelf = reinterpret_cast<SELF *>(pvService);
|
---|
927 | int rc = pSelf->uninit();
|
---|
928 | AssertRC(rc);
|
---|
929 | if (RT_SUCCESS(rc))
|
---|
930 | delete pSelf;
|
---|
931 | return rc;
|
---|
932 | }
|
---|
933 |
|
---|
934 | /**
|
---|
935 | * @interface_method_impl{VBOXHGCMSVCFNTABLE,pfnConnect}
|
---|
936 | * Stub implementation of pfnConnect and pfnDisconnect.
|
---|
937 | */
|
---|
938 | static DECLCALLBACK(int) svcConnect(void *pvService,
|
---|
939 | uint32_t u32ClientID,
|
---|
940 | void *pvClient)
|
---|
941 | {
|
---|
942 | AssertLogRelReturn(VALID_PTR(pvService), VERR_INVALID_PARAMETER);
|
---|
943 | SELF *pSelf = reinterpret_cast<SELF *>(pvService);
|
---|
944 | AssertPtrReturn(pSelf, VERR_INVALID_POINTER);
|
---|
945 | return pSelf->clientConnect(u32ClientID, pvClient);
|
---|
946 | }
|
---|
947 |
|
---|
948 | /**
|
---|
949 | * @interface_method_impl{VBOXHGCMSVCFNTABLE,pfnConnect}
|
---|
950 | * Stub implementation of pfnConnect and pfnDisconnect.
|
---|
951 | */
|
---|
952 | static DECLCALLBACK(int) svcDisconnect(void *pvService,
|
---|
953 | uint32_t u32ClientID,
|
---|
954 | void *pvClient)
|
---|
955 | {
|
---|
956 | AssertLogRelReturn(VALID_PTR(pvService), VERR_INVALID_PARAMETER);
|
---|
957 | SELF *pSelf = reinterpret_cast<SELF *>(pvService);
|
---|
958 | AssertPtrReturn(pSelf, VERR_INVALID_POINTER);
|
---|
959 | return pSelf->clientDisconnect(u32ClientID, pvClient);
|
---|
960 | }
|
---|
961 |
|
---|
962 | /**
|
---|
963 | * @interface_method_impl{VBOXHGCMSVCFNTABLE,pfnCall}
|
---|
964 | * Wraps to the call member function
|
---|
965 | */
|
---|
966 | static DECLCALLBACK(void) svcCall(void * pvService,
|
---|
967 | VBOXHGCMCALLHANDLE callHandle,
|
---|
968 | uint32_t u32ClientID,
|
---|
969 | void *pvClient,
|
---|
970 | uint32_t u32Function,
|
---|
971 | uint32_t cParms,
|
---|
972 | VBOXHGCMSVCPARM paParms[])
|
---|
973 | {
|
---|
974 | AssertLogRelReturnVoid(VALID_PTR(pvService));
|
---|
975 | SELF *pSelf = reinterpret_cast<SELF *>(pvService);
|
---|
976 | AssertPtrReturnVoid(pSelf);
|
---|
977 | pSelf->call(callHandle, u32ClientID, pvClient, u32Function, cParms, paParms);
|
---|
978 | }
|
---|
979 |
|
---|
980 | /**
|
---|
981 | * @interface_method_impl{VBOXHGCMSVCFNTABLE,pfnHostCall}
|
---|
982 | * Wraps to the hostCall member function
|
---|
983 | */
|
---|
984 | static DECLCALLBACK(int) svcHostCall(void *pvService,
|
---|
985 | uint32_t u32Function,
|
---|
986 | uint32_t cParms,
|
---|
987 | VBOXHGCMSVCPARM paParms[])
|
---|
988 | {
|
---|
989 | AssertLogRelReturn(VALID_PTR(pvService), VERR_INVALID_PARAMETER);
|
---|
990 | SELF *pSelf = reinterpret_cast<SELF *>(pvService);
|
---|
991 | AssertPtrReturn(pSelf, VERR_INVALID_POINTER);
|
---|
992 | return pSelf->hostCall(u32Function, cParms, paParms);
|
---|
993 | }
|
---|
994 |
|
---|
995 | /**
|
---|
996 | * @interface_method_impl{VBOXHGCMSVCFNTABLE,pfnRegisterExtension}
|
---|
997 | * Installs a host callback for notifications of property changes.
|
---|
998 | */
|
---|
999 | static DECLCALLBACK(int) svcRegisterExtension(void *pvService,
|
---|
1000 | PFNHGCMSVCEXT pfnExtension,
|
---|
1001 | void *pvExtension)
|
---|
1002 | {
|
---|
1003 | AssertLogRelReturn(VALID_PTR(pvService), VERR_INVALID_PARAMETER);
|
---|
1004 | SELF *pSelf = reinterpret_cast<SELF *>(pvService);
|
---|
1005 | AssertPtrReturn(pSelf, VERR_INVALID_POINTER);
|
---|
1006 | pSelf->mpfnHostCallback = pfnExtension;
|
---|
1007 | pSelf->mpvHostData = pvExtension;
|
---|
1008 | return VINF_SUCCESS;
|
---|
1009 | }
|
---|
1010 |
|
---|
1011 | private:
|
---|
1012 |
|
---|
1013 | int prepareExecute(uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
|
---|
1014 | int clientConnect(uint32_t u32ClientID, void *pvClient);
|
---|
1015 | int clientDisconnect(uint32_t u32ClientID, void *pvClient);
|
---|
1016 | int clientGetCommand(uint32_t u32ClientID, VBOXHGCMCALLHANDLE callHandle, uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
|
---|
1017 | int clientSetMsgFilterSet(uint32_t u32ClientID, VBOXHGCMCALLHANDLE callHandle, uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
|
---|
1018 | int clientSetMsgFilterUnset(uint32_t u32ClientID, VBOXHGCMCALLHANDLE callHandle, uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
|
---|
1019 | int clientSkipMsg(uint32_t u32ClientID, VBOXHGCMCALLHANDLE callHandle, uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
|
---|
1020 | int cancelHostCmd(uint32_t u32ContextID);
|
---|
1021 | int cancelPendingWaits(uint32_t u32ClientID, int rcPending);
|
---|
1022 | int hostCallback(uint32_t eFunction, uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
|
---|
1023 | int hostProcessCommand(uint32_t eFunction, uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
|
---|
1024 | void call(VBOXHGCMCALLHANDLE callHandle, uint32_t u32ClientID, void *pvClient, uint32_t eFunction, uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
|
---|
1025 | int hostCall(uint32_t eFunction, uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
|
---|
1026 | int sessionClose(uint32_t u32ClientID, VBOXHGCMCALLHANDLE callHandle, uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
|
---|
1027 | int uninit(void);
|
---|
1028 |
|
---|
1029 | DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP(Service);
|
---|
1030 | };
|
---|
1031 |
|
---|
1032 | /**
|
---|
1033 | * Handles a client which just connected.
|
---|
1034 | *
|
---|
1035 | * @return IPRT status code.
|
---|
1036 | * @param u32ClientID
|
---|
1037 | * @param pvClient
|
---|
1038 | */
|
---|
1039 | int Service::clientConnect(uint32_t u32ClientID, void *pvClient)
|
---|
1040 | {
|
---|
1041 | RT_NOREF1(pvClient);
|
---|
1042 | LogFlowFunc(("[Client %RU32] Connected\n", u32ClientID));
|
---|
1043 | #ifdef VBOX_STRICT
|
---|
1044 | ClientStateMapIterConst it = mClientStateMap.find(u32ClientID);
|
---|
1045 | if (it != mClientStateMap.end())
|
---|
1046 | {
|
---|
1047 | AssertMsgFailed(("Client with ID=%RU32 already connected when it should not\n",
|
---|
1048 | u32ClientID));
|
---|
1049 | return VERR_ALREADY_EXISTS;
|
---|
1050 | }
|
---|
1051 | #endif
|
---|
1052 | ClientState clientState(mpHelpers, u32ClientID);
|
---|
1053 | mClientStateMap[u32ClientID] = clientState;
|
---|
1054 | /** @todo Exception handling! */
|
---|
1055 | return VINF_SUCCESS;
|
---|
1056 | }
|
---|
1057 |
|
---|
1058 | /**
|
---|
1059 | * Handles a client which disconnected. This functiond does some
|
---|
1060 | * internal cleanup as well as sends notifications to the host so
|
---|
1061 | * that the host can do the same (if required).
|
---|
1062 | *
|
---|
1063 | * @return IPRT status code.
|
---|
1064 | * @param u32ClientID The client's ID of which disconnected.
|
---|
1065 | * @param pvClient User data, not used at the moment.
|
---|
1066 | */
|
---|
1067 | int Service::clientDisconnect(uint32_t u32ClientID, void *pvClient)
|
---|
1068 | {
|
---|
1069 | RT_NOREF1(pvClient);
|
---|
1070 | LogFlowFunc(("[Client %RU32] Disconnected (%zu clients total)\n",
|
---|
1071 | u32ClientID, mClientStateMap.size()));
|
---|
1072 |
|
---|
1073 | AssertMsg(mClientStateMap.size(),
|
---|
1074 | ("No clients in list anymore when there should (client ID=%RU32)\n", u32ClientID));
|
---|
1075 |
|
---|
1076 | int rc = VINF_SUCCESS;
|
---|
1077 |
|
---|
1078 | ClientStateMapIter itClientState = mClientStateMap.find(u32ClientID);
|
---|
1079 | AssertMsg(itClientState != mClientStateMap.end(),
|
---|
1080 | ("Client ID=%RU32 not found in client list when it should be there\n", u32ClientID));
|
---|
1081 |
|
---|
1082 | if (itClientState != mClientStateMap.end())
|
---|
1083 | {
|
---|
1084 | itClientState->second.DequeueAll();
|
---|
1085 |
|
---|
1086 | mClientStateMap.erase(itClientState);
|
---|
1087 | }
|
---|
1088 |
|
---|
1089 | bool fAllClientsDisconnected = mClientStateMap.empty();
|
---|
1090 | if (fAllClientsDisconnected)
|
---|
1091 | {
|
---|
1092 | LogFlowFunc(("All clients disconnected, cancelling all host commands ...\n"));
|
---|
1093 |
|
---|
1094 | /*
|
---|
1095 | * If all clients disconnected we also need to make sure that all buffered
|
---|
1096 | * host commands need to be notified, because Main is waiting a notification
|
---|
1097 | * via a (multi stage) progress object.
|
---|
1098 | */
|
---|
1099 | HostCommand *pCurCmd = RTListGetFirst(&mHostCmdList, HostCommand, Node);
|
---|
1100 | while (pCurCmd)
|
---|
1101 | {
|
---|
1102 | HostCommand *pNext = RTListNodeGetNext(&pCurCmd->Node, HostCommand, Node);
|
---|
1103 | bool fLast = RTListNodeIsLast(&mHostCmdList, &pCurCmd->Node);
|
---|
1104 |
|
---|
1105 | int rc2 = cancelHostCmd(pCurCmd->mContextID);
|
---|
1106 | if (RT_FAILURE(rc2))
|
---|
1107 | {
|
---|
1108 | LogFlowFunc(("Cancelling host command with CID=%u (refCount=%RU32) failed with rc=%Rrc\n",
|
---|
1109 | pCurCmd->mContextID, pCurCmd->mRefCount, rc2));
|
---|
1110 | /* Keep going. */
|
---|
1111 | }
|
---|
1112 |
|
---|
1113 | while (pCurCmd->Release())
|
---|
1114 | ;
|
---|
1115 | delete pCurCmd;
|
---|
1116 | pCurCmd = NULL;
|
---|
1117 |
|
---|
1118 | if (fLast)
|
---|
1119 | break;
|
---|
1120 |
|
---|
1121 | pCurCmd = pNext;
|
---|
1122 | }
|
---|
1123 |
|
---|
1124 | Assert(RTListIsEmpty(&mHostCmdList));
|
---|
1125 | }
|
---|
1126 |
|
---|
1127 | return rc;
|
---|
1128 | }
|
---|
1129 |
|
---|
1130 | /**
|
---|
1131 | * Either fills in parameters from a pending host command into our guest context or
|
---|
1132 | * defer the guest call until we have something from the host.
|
---|
1133 | *
|
---|
1134 | * @return IPRT status code.
|
---|
1135 | * @param u32ClientID The client's ID.
|
---|
1136 | * @param callHandle The client's call handle.
|
---|
1137 | * @param cParms Number of parameters.
|
---|
1138 | * @param paParms Array of parameters.
|
---|
1139 | */
|
---|
1140 | int Service::clientGetCommand(uint32_t u32ClientID, VBOXHGCMCALLHANDLE callHandle,
|
---|
1141 | uint32_t cParms, VBOXHGCMSVCPARM paParms[])
|
---|
1142 | {
|
---|
1143 | /*
|
---|
1144 | * Lookup client in our list so that we can assign the context ID of
|
---|
1145 | * a command to that client.
|
---|
1146 | */
|
---|
1147 | ClientStateMapIter itClientState = mClientStateMap.find(u32ClientID);
|
---|
1148 | AssertMsg(itClientState != mClientStateMap.end(), ("Client with ID=%RU32 not found when it should be present\n",
|
---|
1149 | u32ClientID));
|
---|
1150 | if (itClientState == mClientStateMap.end())
|
---|
1151 | {
|
---|
1152 | /* Should never happen. Complete the call on the guest side though. */
|
---|
1153 | AssertPtr(mpHelpers);
|
---|
1154 | mpHelpers->pfnCallComplete(callHandle, VERR_NOT_FOUND);
|
---|
1155 |
|
---|
1156 | return VERR_NOT_FOUND;
|
---|
1157 | }
|
---|
1158 |
|
---|
1159 | ClientState &clientState = itClientState->second;
|
---|
1160 |
|
---|
1161 | /* Use the current (inbound) connection. */
|
---|
1162 | ClientConnection thisCon;
|
---|
1163 | thisCon.mHandle = callHandle;
|
---|
1164 | thisCon.mNumParms = cParms;
|
---|
1165 | thisCon.mParms = paParms;
|
---|
1166 |
|
---|
1167 | return clientState.RunCurrent(&thisCon);
|
---|
1168 | }
|
---|
1169 |
|
---|
1170 | int Service::clientSetMsgFilterSet(uint32_t u32ClientID, VBOXHGCMCALLHANDLE callHandle,
|
---|
1171 | uint32_t cParms, VBOXHGCMSVCPARM paParms[])
|
---|
1172 | {
|
---|
1173 | RT_NOREF1(callHandle);
|
---|
1174 |
|
---|
1175 | /*
|
---|
1176 | * Lookup client in our list so that we can assign the context ID of
|
---|
1177 | * a command to that client.
|
---|
1178 | */
|
---|
1179 | ClientStateMapIter itClientState = mClientStateMap.find(u32ClientID);
|
---|
1180 | AssertMsg(itClientState != mClientStateMap.end(), ("Client with ID=%RU32 not found when it should be present\n",
|
---|
1181 | u32ClientID));
|
---|
1182 | if (itClientState == mClientStateMap.end())
|
---|
1183 | return VERR_NOT_FOUND; /* Should never happen. */
|
---|
1184 |
|
---|
1185 | if (cParms != 4)
|
---|
1186 | return VERR_INVALID_PARAMETER;
|
---|
1187 |
|
---|
1188 | uint32_t uValue;
|
---|
1189 | int rc = paParms[0].getUInt32(&uValue);
|
---|
1190 | if (RT_SUCCESS(rc))
|
---|
1191 | {
|
---|
1192 | uint32_t uMaskAdd;
|
---|
1193 | rc = paParms[1].getUInt32(&uMaskAdd);
|
---|
1194 | if (RT_SUCCESS(rc))
|
---|
1195 | {
|
---|
1196 | uint32_t uMaskRemove;
|
---|
1197 | rc = paParms[2].getUInt32(&uMaskRemove);
|
---|
1198 | /** @todo paParm[3] (flags) not used yet. */
|
---|
1199 | if (RT_SUCCESS(rc))
|
---|
1200 | {
|
---|
1201 | ClientState &clientState = itClientState->second;
|
---|
1202 |
|
---|
1203 | clientState.mFlags |= CLIENTSTATE_FLAG_CONTEXTFILTER;
|
---|
1204 | if (uMaskAdd)
|
---|
1205 | clientState.mFilterMask |= uMaskAdd;
|
---|
1206 | if (uMaskRemove)
|
---|
1207 | clientState.mFilterMask &= ~uMaskRemove;
|
---|
1208 |
|
---|
1209 | clientState.mFilterValue = uValue;
|
---|
1210 |
|
---|
1211 | LogFlowFunc(("[Client %RU32] Setting message filterMask=0x%x, filterVal=%RU32 set (flags=0x%x, maskAdd=0x%x, maskRemove=0x%x)\n",
|
---|
1212 | u32ClientID, clientState.mFilterMask, clientState.mFilterValue,
|
---|
1213 | clientState.mFlags, uMaskAdd, uMaskRemove));
|
---|
1214 | }
|
---|
1215 | }
|
---|
1216 | }
|
---|
1217 |
|
---|
1218 | return rc;
|
---|
1219 | }
|
---|
1220 |
|
---|
1221 | int Service::clientSetMsgFilterUnset(uint32_t u32ClientID, VBOXHGCMCALLHANDLE callHandle,
|
---|
1222 | uint32_t cParms, VBOXHGCMSVCPARM paParms[])
|
---|
1223 | {
|
---|
1224 | RT_NOREF2(callHandle, paParms);
|
---|
1225 |
|
---|
1226 | /*
|
---|
1227 | * Lookup client in our list so that we can assign the context ID of
|
---|
1228 | * a command to that client.
|
---|
1229 | */
|
---|
1230 | ClientStateMapIter itClientState = mClientStateMap.find(u32ClientID);
|
---|
1231 | AssertMsg(itClientState != mClientStateMap.end(), ("Client with ID=%RU32 not found when it should be present\n",
|
---|
1232 | u32ClientID));
|
---|
1233 | if (itClientState == mClientStateMap.end())
|
---|
1234 | return VERR_NOT_FOUND; /* Should never happen. */
|
---|
1235 |
|
---|
1236 | if (cParms != 1)
|
---|
1237 | return VERR_INVALID_PARAMETER;
|
---|
1238 |
|
---|
1239 | ClientState &clientState = itClientState->second;
|
---|
1240 |
|
---|
1241 | clientState.mFlags &= ~CLIENTSTATE_FLAG_CONTEXTFILTER;
|
---|
1242 | clientState.mFilterMask = 0;
|
---|
1243 | clientState.mFilterValue = 0;
|
---|
1244 |
|
---|
1245 | LogFlowFunc(("[Client %RU32} Unset message filter\n", u32ClientID));
|
---|
1246 | return VINF_SUCCESS;
|
---|
1247 | }
|
---|
1248 |
|
---|
1249 | int Service::clientSkipMsg(uint32_t u32ClientID, VBOXHGCMCALLHANDLE callHandle,
|
---|
1250 | uint32_t cParms, VBOXHGCMSVCPARM paParms[])
|
---|
1251 | {
|
---|
1252 | RT_NOREF2(callHandle, paParms);
|
---|
1253 |
|
---|
1254 | /*
|
---|
1255 | * Lookup client in our list so that we can assign the context ID of
|
---|
1256 | * a command to that client.
|
---|
1257 | */
|
---|
1258 | ClientStateMapIter itClientState = mClientStateMap.find(u32ClientID);
|
---|
1259 | AssertMsg(itClientState != mClientStateMap.end(), ("Client ID=%RU32 not found when it should be present\n",
|
---|
1260 | u32ClientID));
|
---|
1261 | if (itClientState == mClientStateMap.end())
|
---|
1262 | return VERR_NOT_FOUND; /* Should never happen. */
|
---|
1263 |
|
---|
1264 | if (cParms != 1)
|
---|
1265 | return VERR_INVALID_PARAMETER;
|
---|
1266 |
|
---|
1267 | LogFlowFunc(("[Client %RU32] Skipping current message ...\n", u32ClientID));
|
---|
1268 |
|
---|
1269 | itClientState->second.DequeueCurrent();
|
---|
1270 |
|
---|
1271 | return VINF_SUCCESS;
|
---|
1272 | }
|
---|
1273 |
|
---|
1274 | /**
|
---|
1275 | * Cancels a buffered host command to unblock waiting on Main side
|
---|
1276 | * via callbacks.
|
---|
1277 | *
|
---|
1278 | * @return IPRT status code.
|
---|
1279 | * @param u32ContextID Context ID of host command to cancel.
|
---|
1280 | */
|
---|
1281 | int Service::cancelHostCmd(uint32_t u32ContextID)
|
---|
1282 | {
|
---|
1283 | Assert(mpfnHostCallback);
|
---|
1284 |
|
---|
1285 | LogFlowFunc(("Cancelling CID=%u ...\n", u32ContextID));
|
---|
1286 |
|
---|
1287 | uint32_t cParms = 0;
|
---|
1288 | VBOXHGCMSVCPARM arParms[2];
|
---|
1289 | arParms[cParms++].setUInt32(u32ContextID);
|
---|
1290 |
|
---|
1291 | return hostCallback(GUEST_DISCONNECTED, cParms, arParms);
|
---|
1292 | }
|
---|
1293 |
|
---|
1294 | /**
|
---|
1295 | * Client asks itself (in another thread) to cancel all pending waits which are blocking the client
|
---|
1296 | * from shutting down / doing something else.
|
---|
1297 | *
|
---|
1298 | * @return IPRT status code.
|
---|
1299 | * @param u32ClientID The client's ID.
|
---|
1300 | * @param rcPending Result code for completing pending operation.
|
---|
1301 | */
|
---|
1302 | int Service::cancelPendingWaits(uint32_t u32ClientID, int rcPending)
|
---|
1303 | {
|
---|
1304 | ClientStateMapIter itClientState = mClientStateMap.find(u32ClientID);
|
---|
1305 | if (itClientState != mClientStateMap.end())
|
---|
1306 | return itClientState->second.CancelWaiting(rcPending);
|
---|
1307 |
|
---|
1308 | return VINF_SUCCESS;
|
---|
1309 | }
|
---|
1310 |
|
---|
1311 | /**
|
---|
1312 | * Notifies the host (using low-level HGCM callbacks) about an event
|
---|
1313 | * which was sent from the client.
|
---|
1314 | *
|
---|
1315 | * @return IPRT status code.
|
---|
1316 | * @param eFunction Function (event) that occured.
|
---|
1317 | * @param cParms Number of parameters.
|
---|
1318 | * @param paParms Array of parameters.
|
---|
1319 | */
|
---|
1320 | int Service::hostCallback(uint32_t eFunction, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
|
---|
1321 | {
|
---|
1322 | LogFlowFunc(("eFunction=%ld, cParms=%ld, paParms=%p\n",
|
---|
1323 | eFunction, cParms, paParms));
|
---|
1324 |
|
---|
1325 | int rc;
|
---|
1326 | if (mpfnHostCallback)
|
---|
1327 | {
|
---|
1328 | VBOXGUESTCTRLHOSTCALLBACK data(cParms, paParms);
|
---|
1329 | rc = mpfnHostCallback(mpvHostData, eFunction,
|
---|
1330 | (void *)(&data), sizeof(data));
|
---|
1331 | }
|
---|
1332 | else
|
---|
1333 | rc = VERR_NOT_SUPPORTED;
|
---|
1334 |
|
---|
1335 | LogFlowFunc(("Returning rc=%Rrc\n", rc));
|
---|
1336 | return rc;
|
---|
1337 | }
|
---|
1338 |
|
---|
1339 | /**
|
---|
1340 | * Processes a command received from the host side and re-routes it to
|
---|
1341 | * a connect client on the guest.
|
---|
1342 | *
|
---|
1343 | * @return IPRT status code.
|
---|
1344 | * @param eFunction Function code to process.
|
---|
1345 | * @param cParms Number of parameters.
|
---|
1346 | * @param paParms Array of parameters.
|
---|
1347 | */
|
---|
1348 | int Service::hostProcessCommand(uint32_t eFunction, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
|
---|
1349 | {
|
---|
1350 | /*
|
---|
1351 | * If no client is connected at all we don't buffer any host commands
|
---|
1352 | * and immediately return an error to the host. This avoids the host
|
---|
1353 | * waiting for a response from the guest side in case VBoxService on
|
---|
1354 | * the guest is not running/system is messed up somehow.
|
---|
1355 | */
|
---|
1356 | if (mClientStateMap.empty())
|
---|
1357 | return VERR_NOT_FOUND;
|
---|
1358 |
|
---|
1359 | int rc;
|
---|
1360 |
|
---|
1361 | HostCommand *pHostCmd = NULL;
|
---|
1362 | try
|
---|
1363 | {
|
---|
1364 | pHostCmd = new HostCommand();
|
---|
1365 | rc = pHostCmd->Allocate(eFunction, cParms, paParms);
|
---|
1366 | if (RT_SUCCESS(rc))
|
---|
1367 | /* rc = */ RTListAppend(&mHostCmdList, &pHostCmd->Node);
|
---|
1368 | }
|
---|
1369 | catch (std::bad_alloc)
|
---|
1370 | {
|
---|
1371 | rc = VERR_NO_MEMORY;
|
---|
1372 | }
|
---|
1373 |
|
---|
1374 | if (RT_SUCCESS(rc))
|
---|
1375 | {
|
---|
1376 | LogFlowFunc(("Handling host command CID=%RU32, eFunction=%RU32, cParms=%RU32, paParms=%p, numClients=%zu\n",
|
---|
1377 | pHostCmd->mContextID, eFunction, cParms, paParms, mClientStateMap.size()));
|
---|
1378 |
|
---|
1379 | /*
|
---|
1380 | * Wake up all pending clients which are interested in this
|
---|
1381 | * host command.
|
---|
1382 | */
|
---|
1383 | #ifdef DEBUG
|
---|
1384 | uint32_t uClientsWokenUp = 0;
|
---|
1385 | #endif
|
---|
1386 | ClientStateMapIter itClientState = mClientStateMap.begin();
|
---|
1387 | AssertMsg(itClientState != mClientStateMap.end(), ("Client state map is empty when it should not\n"));
|
---|
1388 | while (itClientState != mClientStateMap.end())
|
---|
1389 | {
|
---|
1390 | ClientState &clientState = itClientState->second;
|
---|
1391 |
|
---|
1392 | /* If a client indicates that it it wants the new host command,
|
---|
1393 | * add a reference to not delete it.*/
|
---|
1394 | if (clientState.WantsHostCommand(pHostCmd))
|
---|
1395 | {
|
---|
1396 | clientState.EnqueueCommand(pHostCmd);
|
---|
1397 |
|
---|
1398 | int rc2 = clientState.Wakeup();
|
---|
1399 | if (RT_FAILURE(rc2))
|
---|
1400 | LogFlowFunc(("Waking up client ID=%RU32 failed with rc=%Rrc\n",
|
---|
1401 | itClientState->first, rc2));
|
---|
1402 | #ifdef DEBUG
|
---|
1403 | uClientsWokenUp++;
|
---|
1404 | #endif
|
---|
1405 | }
|
---|
1406 |
|
---|
1407 | ++itClientState;
|
---|
1408 | }
|
---|
1409 |
|
---|
1410 | #ifdef DEBUG
|
---|
1411 | LogFlowFunc(("%RU32 clients have been woken up\n", uClientsWokenUp));
|
---|
1412 | #endif
|
---|
1413 | }
|
---|
1414 |
|
---|
1415 | return rc;
|
---|
1416 | }
|
---|
1417 |
|
---|
1418 | /**
|
---|
1419 | * @interface_method_impl{VBOXHGCMSVCFNTABLE,pfnCall}
|
---|
1420 | *
|
---|
1421 | * @note All functions which do not involve an unreasonable delay will be
|
---|
1422 | * handled synchronously. If needed, we will add a request handler
|
---|
1423 | * thread in future for those which do.
|
---|
1424 | *
|
---|
1425 | * @thread HGCM
|
---|
1426 | */
|
---|
1427 | void Service::call(VBOXHGCMCALLHANDLE callHandle, uint32_t u32ClientID,
|
---|
1428 | void * /* pvClient */, uint32_t eFunction, uint32_t cParms,
|
---|
1429 | VBOXHGCMSVCPARM paParms[])
|
---|
1430 | {
|
---|
1431 | int rc = VINF_SUCCESS;
|
---|
1432 | LogFlowFunc(("[Client %RU32] eFunction=%RU32, cParms=%RU32, paParms=0x%p\n",
|
---|
1433 | u32ClientID, eFunction, cParms, paParms));
|
---|
1434 | try
|
---|
1435 | {
|
---|
1436 | /*
|
---|
1437 | * The guest asks the host for the next message to process.
|
---|
1438 | */
|
---|
1439 | if (eFunction == GUEST_MSG_WAIT)
|
---|
1440 | {
|
---|
1441 | LogFlowFunc(("[Client %RU32] GUEST_MSG_GET\n", u32ClientID));
|
---|
1442 | rc = clientGetCommand(u32ClientID, callHandle, cParms, paParms);
|
---|
1443 | }
|
---|
1444 | else
|
---|
1445 | {
|
---|
1446 | switch (eFunction)
|
---|
1447 | {
|
---|
1448 | /*
|
---|
1449 | * A client wants to shut down and asks us (this service) to cancel
|
---|
1450 | * all blocking/pending waits (VINF_HGCM_ASYNC_EXECUTE) so that the
|
---|
1451 | * client can gracefully shut down.
|
---|
1452 | */
|
---|
1453 | case GUEST_CANCEL_PENDING_WAITS:
|
---|
1454 | LogFlowFunc(("[Client %RU32] GUEST_CANCEL_PENDING_WAITS\n", u32ClientID));
|
---|
1455 | rc = cancelPendingWaits(u32ClientID, VINF_SUCCESS /* Pending result */);
|
---|
1456 | break;
|
---|
1457 |
|
---|
1458 | /*
|
---|
1459 | * The guest only wants certain messages set by the filter mask(s).
|
---|
1460 | * Since VBox 4.3+.
|
---|
1461 | */
|
---|
1462 | case GUEST_MSG_FILTER_SET:
|
---|
1463 | LogFlowFunc(("[Client %RU32] GUEST_MSG_FILTER_SET\n", u32ClientID));
|
---|
1464 | rc = clientSetMsgFilterSet(u32ClientID, callHandle, cParms, paParms);
|
---|
1465 | break;
|
---|
1466 |
|
---|
1467 | /*
|
---|
1468 | * Unsetting the message filter flag.
|
---|
1469 | */
|
---|
1470 | case GUEST_MSG_FILTER_UNSET:
|
---|
1471 | LogFlowFunc(("[Client %RU32] GUEST_MSG_FILTER_UNSET\n", u32ClientID));
|
---|
1472 | rc = clientSetMsgFilterUnset(u32ClientID, callHandle, cParms, paParms);
|
---|
1473 | break;
|
---|
1474 |
|
---|
1475 | /*
|
---|
1476 | * The guest only wants skip the currently assigned messages. Neded
|
---|
1477 | * for dropping its assigned reference of the current assigned host
|
---|
1478 | * command in queue.
|
---|
1479 | * Since VBox 4.3+.
|
---|
1480 | */
|
---|
1481 | case GUEST_MSG_SKIP:
|
---|
1482 | LogFlowFunc(("[Client %RU32] GUEST_MSG_SKIP\n", u32ClientID));
|
---|
1483 | rc = clientSkipMsg(u32ClientID, callHandle, cParms, paParms);
|
---|
1484 | break;
|
---|
1485 |
|
---|
1486 | /*
|
---|
1487 | * The guest wants to close specific guest session. This is handy for
|
---|
1488 | * shutting down dedicated guest session processes from another process.
|
---|
1489 | */
|
---|
1490 | case GUEST_SESSION_CLOSE:
|
---|
1491 | LogFlowFunc(("[Client %RU32] GUEST_SESSION_CLOSE\n", u32ClientID));
|
---|
1492 | rc = sessionClose(u32ClientID, callHandle, cParms, paParms);
|
---|
1493 | break;
|
---|
1494 |
|
---|
1495 | /*
|
---|
1496 | * For all other regular commands we call our hostCallback
|
---|
1497 | * function. If the current command does not support notifications,
|
---|
1498 | * notifyHost will return VERR_NOT_SUPPORTED.
|
---|
1499 | */
|
---|
1500 | default:
|
---|
1501 | rc = hostCallback(eFunction, cParms, paParms);
|
---|
1502 | break;
|
---|
1503 | }
|
---|
1504 |
|
---|
1505 | if (rc != VINF_HGCM_ASYNC_EXECUTE)
|
---|
1506 | {
|
---|
1507 | /* Tell the client that the call is complete (unblocks waiting). */
|
---|
1508 | AssertPtr(mpHelpers);
|
---|
1509 | mpHelpers->pfnCallComplete(callHandle, rc);
|
---|
1510 | }
|
---|
1511 | }
|
---|
1512 | }
|
---|
1513 | catch (std::bad_alloc)
|
---|
1514 | {
|
---|
1515 | rc = VERR_NO_MEMORY;
|
---|
1516 | }
|
---|
1517 | }
|
---|
1518 |
|
---|
1519 | /**
|
---|
1520 | * Service call handler for the host.
|
---|
1521 | * @interface_method_impl{VBOXHGCMSVCFNTABLE,pfnHostCall}
|
---|
1522 | * @thread hgcm
|
---|
1523 | */
|
---|
1524 | int Service::hostCall(uint32_t eFunction, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
|
---|
1525 | {
|
---|
1526 | int rc = VERR_NOT_SUPPORTED;
|
---|
1527 | LogFlowFunc(("fn=%RU32, cParms=%RU32, paParms=0x%p\n",
|
---|
1528 | eFunction, cParms, paParms));
|
---|
1529 | try
|
---|
1530 | {
|
---|
1531 | switch (eFunction)
|
---|
1532 | {
|
---|
1533 | /**
|
---|
1534 | * Host
|
---|
1535 | */
|
---|
1536 | case HOST_CANCEL_PENDING_WAITS:
|
---|
1537 | {
|
---|
1538 | LogFlowFunc(("HOST_CANCEL_PENDING_WAITS\n"));
|
---|
1539 | ClientStateMapIter itClientState = mClientStateMap.begin();
|
---|
1540 | while (itClientState != mClientStateMap.end())
|
---|
1541 | {
|
---|
1542 | int rc2 = itClientState->second.CancelWaiting(VINF_SUCCESS /* Pending rc. */);
|
---|
1543 | if (RT_FAILURE(rc2))
|
---|
1544 | LogFlowFunc(("Cancelling waiting for client ID=%RU32 failed with rc=%Rrc",
|
---|
1545 | itClientState->first, rc2));
|
---|
1546 | ++itClientState;
|
---|
1547 | }
|
---|
1548 | rc = VINF_SUCCESS;
|
---|
1549 | break;
|
---|
1550 | }
|
---|
1551 |
|
---|
1552 | default:
|
---|
1553 | rc = hostProcessCommand(eFunction, cParms, paParms);
|
---|
1554 | break;
|
---|
1555 | }
|
---|
1556 | }
|
---|
1557 | catch (std::bad_alloc)
|
---|
1558 | {
|
---|
1559 | rc = VERR_NO_MEMORY;
|
---|
1560 | }
|
---|
1561 |
|
---|
1562 | return rc;
|
---|
1563 | }
|
---|
1564 |
|
---|
1565 | /**
|
---|
1566 | * Client asks another client (guest) session to close.
|
---|
1567 | *
|
---|
1568 | * @return IPRT status code.
|
---|
1569 | * @param u32ClientID The client's ID.
|
---|
1570 | * @param callHandle The client's call handle.
|
---|
1571 | * @param cParms Number of parameters.
|
---|
1572 | * @param paParms Array of parameters.
|
---|
1573 | */
|
---|
1574 | int Service::sessionClose(uint32_t u32ClientID, VBOXHGCMCALLHANDLE callHandle, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
|
---|
1575 | {
|
---|
1576 | RT_NOREF2(u32ClientID, callHandle);
|
---|
1577 | if (cParms < 2)
|
---|
1578 | return VERR_INVALID_PARAMETER;
|
---|
1579 |
|
---|
1580 | uint32_t uContextID, uFlags;
|
---|
1581 | int rc = paParms[0].getUInt32(&uContextID);
|
---|
1582 | if (RT_SUCCESS(rc))
|
---|
1583 | rc = paParms[1].getUInt32(&uFlags);
|
---|
1584 |
|
---|
1585 | uint32_t uSessionID = VBOX_GUESTCTRL_CONTEXTID_GET_SESSION(uContextID);
|
---|
1586 |
|
---|
1587 | if (RT_SUCCESS(rc))
|
---|
1588 | rc = hostProcessCommand(HOST_SESSION_CLOSE, cParms, paParms);
|
---|
1589 |
|
---|
1590 | LogFlowFunc(("Closing guest session ID=%RU32 (from client ID=%RU32) returned with rc=%Rrc\n",
|
---|
1591 | uSessionID, u32ClientID, rc)); NOREF(uSessionID);
|
---|
1592 | return rc;
|
---|
1593 | }
|
---|
1594 |
|
---|
1595 | int Service::uninit(void)
|
---|
1596 | {
|
---|
1597 | return VINF_SUCCESS;
|
---|
1598 | }
|
---|
1599 |
|
---|
1600 | } /* namespace guestControl */
|
---|
1601 |
|
---|
1602 | using guestControl::Service;
|
---|
1603 |
|
---|
1604 | /**
|
---|
1605 | * @copydoc VBOXHGCMSVCLOAD
|
---|
1606 | */
|
---|
1607 | extern "C" DECLCALLBACK(DECLEXPORT(int)) VBoxHGCMSvcLoad(VBOXHGCMSVCFNTABLE *pTable)
|
---|
1608 | {
|
---|
1609 | int rc = VINF_SUCCESS;
|
---|
1610 |
|
---|
1611 | LogFlowFunc(("pTable=%p\n", pTable));
|
---|
1612 |
|
---|
1613 | if (!VALID_PTR(pTable))
|
---|
1614 | {
|
---|
1615 | rc = VERR_INVALID_PARAMETER;
|
---|
1616 | }
|
---|
1617 | else
|
---|
1618 | {
|
---|
1619 | LogFlowFunc(("pTable->cbSize=%d, pTable->u32Version=0x%08X\n", pTable->cbSize, pTable->u32Version));
|
---|
1620 |
|
---|
1621 | if ( pTable->cbSize != sizeof (VBOXHGCMSVCFNTABLE)
|
---|
1622 | || pTable->u32Version != VBOX_HGCM_SVC_VERSION)
|
---|
1623 | {
|
---|
1624 | rc = VERR_VERSION_MISMATCH;
|
---|
1625 | }
|
---|
1626 | else
|
---|
1627 | {
|
---|
1628 | Service *pService = NULL;
|
---|
1629 | /* No exceptions may propagate outside. */
|
---|
1630 | try
|
---|
1631 | {
|
---|
1632 | pService = new Service(pTable->pHelpers);
|
---|
1633 | }
|
---|
1634 | catch (int rcThrown)
|
---|
1635 | {
|
---|
1636 | rc = rcThrown;
|
---|
1637 | }
|
---|
1638 | catch(std::bad_alloc &)
|
---|
1639 | {
|
---|
1640 | rc = VERR_NO_MEMORY;
|
---|
1641 | }
|
---|
1642 |
|
---|
1643 | if (RT_SUCCESS(rc))
|
---|
1644 | {
|
---|
1645 | /*
|
---|
1646 | * We don't need an additional client data area on the host,
|
---|
1647 | * because we're a class which can have members for that :-).
|
---|
1648 | */
|
---|
1649 | pTable->cbClient = 0;
|
---|
1650 |
|
---|
1651 | /* Register functions. */
|
---|
1652 | pTable->pfnUnload = Service::svcUnload;
|
---|
1653 | pTable->pfnConnect = Service::svcConnect;
|
---|
1654 | pTable->pfnDisconnect = Service::svcDisconnect;
|
---|
1655 | pTable->pfnCall = Service::svcCall;
|
---|
1656 | pTable->pfnHostCall = Service::svcHostCall;
|
---|
1657 | pTable->pfnSaveState = NULL; /* The service is stateless, so the normal */
|
---|
1658 | pTable->pfnLoadState = NULL; /* construction done before restoring suffices */
|
---|
1659 | pTable->pfnRegisterExtension = Service::svcRegisterExtension;
|
---|
1660 |
|
---|
1661 | /* Service specific initialization. */
|
---|
1662 | pTable->pvService = pService;
|
---|
1663 | }
|
---|
1664 | else
|
---|
1665 | {
|
---|
1666 | if (pService)
|
---|
1667 | {
|
---|
1668 | delete pService;
|
---|
1669 | pService = NULL;
|
---|
1670 | }
|
---|
1671 | }
|
---|
1672 | }
|
---|
1673 | }
|
---|
1674 |
|
---|
1675 | LogFlowFunc(("Returning %Rrc\n", rc));
|
---|
1676 | return rc;
|
---|
1677 | }
|
---|
1678 |
|
---|