1 | /* $Id: service.cpp 69496 2017-10-28 14:55:58Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Guest Control Service: Controlling the guest.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011-2017 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 | /**
|
---|
597 | * Set to inidicate that a client call (GUEST_MSG_WAIT) is pending.
|
---|
598 | */
|
---|
599 | int SetPending(const ClientConnection *pConnection)
|
---|
600 | {
|
---|
601 | AssertPtrReturn(pConnection, VERR_INVALID_POINTER);
|
---|
602 |
|
---|
603 | if (mIsPending)
|
---|
604 | {
|
---|
605 | LogFlowFunc(("[Client %RU32] Already is in pending mode\n", mID));
|
---|
606 |
|
---|
607 | /*
|
---|
608 | * Signal that we don't and can't return yet.
|
---|
609 | */
|
---|
610 | return VINF_HGCM_ASYNC_EXECUTE;
|
---|
611 | }
|
---|
612 |
|
---|
613 | if (mHostCmdList.empty())
|
---|
614 | {
|
---|
615 | AssertMsg(mIsPending == false,
|
---|
616 | ("Client ID=%RU32 already is pending but tried to receive a new host command\n", mID));
|
---|
617 |
|
---|
618 | mPendingCon.mHandle = pConnection->mHandle;
|
---|
619 | mPendingCon.mNumParms = pConnection->mNumParms;
|
---|
620 | mPendingCon.mParms = pConnection->mParms;
|
---|
621 |
|
---|
622 | mIsPending = true;
|
---|
623 |
|
---|
624 | LogFlowFunc(("[Client %RU32] Is now in pending mode\n", mID));
|
---|
625 |
|
---|
626 | /*
|
---|
627 | * Signal that we don't and can't return yet.
|
---|
628 | */
|
---|
629 | return VINF_HGCM_ASYNC_EXECUTE;
|
---|
630 | }
|
---|
631 |
|
---|
632 | /*
|
---|
633 | * Signal that there already is a connection pending.
|
---|
634 | * Shouldn't happen in daily usage.
|
---|
635 | */
|
---|
636 | AssertMsgFailed(("Client already has a connection pending\n"));
|
---|
637 | return VERR_SIGNAL_PENDING;
|
---|
638 | }
|
---|
639 |
|
---|
640 | int Run(const ClientConnection *pConnection,
|
---|
641 | HostCommand *pHostCmd)
|
---|
642 | {
|
---|
643 | AssertPtrReturn(pConnection, VERR_INVALID_POINTER);
|
---|
644 | AssertPtrReturn(pHostCmd, VERR_INVALID_POINTER);
|
---|
645 |
|
---|
646 | int rc = VINF_SUCCESS;
|
---|
647 |
|
---|
648 | LogFlowFunc(("[Client %RU32] pConnection=%p, mHostCmdRc=%Rrc, mHostCmdTries=%RU32, mPeekCount=%RU32\n",
|
---|
649 | mID, pConnection, mHostCmdRc, mHostCmdTries, mPeekCount));
|
---|
650 |
|
---|
651 | mHostCmdRc = SendReply(pConnection, pHostCmd);
|
---|
652 | LogFlowFunc(("[Client %RU32] Processing pHostCmd=%p ended with rc=%Rrc\n",
|
---|
653 | mID, pHostCmd, mHostCmdRc));
|
---|
654 |
|
---|
655 | bool fRemove = false;
|
---|
656 | if (RT_FAILURE(mHostCmdRc))
|
---|
657 | {
|
---|
658 | mHostCmdTries++;
|
---|
659 |
|
---|
660 | /*
|
---|
661 | * If the client understood the message but supplied too little buffer space
|
---|
662 | * don't send this message again and drop it after 6 unsuccessful attempts.
|
---|
663 | *
|
---|
664 | * Note: Due to legacy reasons this the retry counter has to be even because on
|
---|
665 | * every peek there will be the actual command retrieval from the client side.
|
---|
666 | * To not get the actual command if the client actually only wants to peek for
|
---|
667 | * the next command, there needs to be two rounds per try, e.g. 3 rounds = 6 tries.
|
---|
668 | *
|
---|
669 | ** @todo Fix the mess stated above. GUEST_MSG_WAIT should be become GUEST_MSG_PEEK, *only*
|
---|
670 | * (and every time) returning the next upcoming host command (if any, blocking). Then
|
---|
671 | * it's up to the client what to do next, either peeking again or getting the actual
|
---|
672 | * host command via an own GUEST_ type message.
|
---|
673 | */
|
---|
674 | if (mHostCmdRc == VERR_TOO_MUCH_DATA)
|
---|
675 | {
|
---|
676 | if (mHostCmdTries == 6)
|
---|
677 | fRemove = true;
|
---|
678 | }
|
---|
679 | /* Client did not understand the message or something else weird happened. Try again one
|
---|
680 | * more time and drop it if it didn't get handled then. */
|
---|
681 | else if (mHostCmdTries > 1)
|
---|
682 | fRemove = true;
|
---|
683 | }
|
---|
684 | else
|
---|
685 | fRemove = true; /* Everything went fine, remove it. */
|
---|
686 |
|
---|
687 | LogFlowFunc(("[Client %RU32] Tried pHostCmd=%p for %RU32 times, (last result=%Rrc, fRemove=%RTbool)\n",
|
---|
688 | mID, pHostCmd, mHostCmdTries, mHostCmdRc, fRemove));
|
---|
689 |
|
---|
690 | if (RT_SUCCESS(rc)) /** @todo r=bird: confusing statement+state, rc hasn't been touched since the top and is always VINF_SUCCESS. */
|
---|
691 | rc = mHostCmdRc;
|
---|
692 |
|
---|
693 | if (fRemove)
|
---|
694 | {
|
---|
695 | /** @todo Fix this (slow) lookup. Too late today. */
|
---|
696 | HostCmdListIter curItem = mHostCmdList.begin();
|
---|
697 | while (curItem != mHostCmdList.end())
|
---|
698 | {
|
---|
699 | if ((*curItem) == pHostCmd)
|
---|
700 | {
|
---|
701 | Dequeue(curItem);
|
---|
702 | break;
|
---|
703 | }
|
---|
704 |
|
---|
705 | ++curItem;
|
---|
706 | }
|
---|
707 | }
|
---|
708 |
|
---|
709 | LogFlowFunc(("[Client %RU32] Returned with rc=%Rrc\n", mID, rc));
|
---|
710 | return rc;
|
---|
711 | }
|
---|
712 |
|
---|
713 | int RunCurrent(const ClientConnection *pConnection)
|
---|
714 | {
|
---|
715 | AssertPtrReturn(pConnection, VERR_INVALID_POINTER);
|
---|
716 |
|
---|
717 | int rc;
|
---|
718 | if (mHostCmdList.empty())
|
---|
719 | {
|
---|
720 | rc = SetPending(pConnection);
|
---|
721 | }
|
---|
722 | else
|
---|
723 | {
|
---|
724 | AssertMsgReturn(!mIsPending,
|
---|
725 | ("Client ID=%RU32 still is in pending mode; can't use another connection\n", mID), VERR_INVALID_PARAMETER);
|
---|
726 |
|
---|
727 | HostCmdListIter curCmd = mHostCmdList.begin();
|
---|
728 | Assert(curCmd != mHostCmdList.end());
|
---|
729 | HostCommand *pHostCmd = (*curCmd);
|
---|
730 | AssertPtrReturn(pHostCmd, VERR_INVALID_POINTER);
|
---|
731 |
|
---|
732 | rc = Run(pConnection, pHostCmd);
|
---|
733 | }
|
---|
734 |
|
---|
735 | return rc;
|
---|
736 | }
|
---|
737 |
|
---|
738 | int Wakeup(void)
|
---|
739 | {
|
---|
740 | int rc = VINF_NO_CHANGE;
|
---|
741 |
|
---|
742 | if (mIsPending)
|
---|
743 | {
|
---|
744 | LogFlowFunc(("[Client %RU32] Waking up ...\n", mID));
|
---|
745 |
|
---|
746 | rc = VINF_SUCCESS;
|
---|
747 |
|
---|
748 | HostCmdListIter curCmd = mHostCmdList.begin();
|
---|
749 | if (curCmd != mHostCmdList.end())
|
---|
750 | {
|
---|
751 | HostCommand *pHostCmd = (*curCmd);
|
---|
752 | AssertPtrReturn(pHostCmd, VERR_INVALID_POINTER);
|
---|
753 |
|
---|
754 | LogFlowFunc(("[Client %RU32] Current host command is pHostCmd=%p, CID=%RU32, cmdType=%RU32, cmdParms=%RU32, refCount=%RU32\n",
|
---|
755 | mID, pHostCmd, pHostCmd->mContextID, pHostCmd->mMsgType, pHostCmd->mParmCount, pHostCmd->mRefCount));
|
---|
756 |
|
---|
757 | rc = Run(&mPendingCon, pHostCmd);
|
---|
758 | }
|
---|
759 | else
|
---|
760 | AssertMsgFailed(("Waking up client ID=%RU32 with no host command in queue is a bad idea\n", mID));
|
---|
761 |
|
---|
762 | return rc;
|
---|
763 | }
|
---|
764 |
|
---|
765 | return VINF_NO_CHANGE;
|
---|
766 | }
|
---|
767 |
|
---|
768 | int CancelWaiting(int rcPending)
|
---|
769 | {
|
---|
770 | LogFlowFunc(("[Client %RU32] Cancelling waiting with %Rrc, isPending=%RTbool, pendingNumParms=%RU32, flags=%x\n",
|
---|
771 | mID, rcPending, mIsPending, mPendingCon.mNumParms, mFlags));
|
---|
772 |
|
---|
773 | int rc;
|
---|
774 | if ( mIsPending
|
---|
775 | && mPendingCon.mNumParms >= 2)
|
---|
776 | {
|
---|
777 | mPendingCon.mParms[0].setUInt32(HOST_CANCEL_PENDING_WAITS); /* Message ID. */
|
---|
778 | mPendingCon.mParms[1].setUInt32(0); /* Required parameters for message. */
|
---|
779 |
|
---|
780 | AssertPtr(mSvcHelpers);
|
---|
781 | mSvcHelpers->pfnCallComplete(mPendingCon.mHandle, rcPending);
|
---|
782 |
|
---|
783 | mIsPending = false;
|
---|
784 |
|
---|
785 | rc = VINF_SUCCESS;
|
---|
786 | }
|
---|
787 | else if (mPendingCon.mNumParms < 2)
|
---|
788 | rc = VERR_BUFFER_OVERFLOW;
|
---|
789 | else /** @todo Enqueue command instead of dropping? */
|
---|
790 | rc = VERR_WRONG_ORDER;
|
---|
791 |
|
---|
792 | return rc;
|
---|
793 | }
|
---|
794 |
|
---|
795 | int SendReply(ClientConnection const *pConnection,
|
---|
796 | HostCommand *pHostCmd)
|
---|
797 | {
|
---|
798 | AssertPtrReturn(pConnection, VERR_INVALID_POINTER);
|
---|
799 | AssertPtrReturn(pHostCmd, VERR_INVALID_POINTER);
|
---|
800 |
|
---|
801 | int rc;
|
---|
802 | /* If the client is in pending mode, always send back
|
---|
803 | * the peek result first. */
|
---|
804 | if (mIsPending)
|
---|
805 | {
|
---|
806 | rc = pHostCmd->Peek(pConnection);
|
---|
807 | mPeekCount++;
|
---|
808 | }
|
---|
809 | else
|
---|
810 | {
|
---|
811 | /* If this is the very first peek, make sure to *always* give back the peeking answer
|
---|
812 | * instead of the actual command, even if this command would fit into the current
|
---|
813 | * connection buffer. */
|
---|
814 | if (!mPeekCount)
|
---|
815 | {
|
---|
816 | rc = pHostCmd->Peek(pConnection);
|
---|
817 | mPeekCount++;
|
---|
818 | }
|
---|
819 | else
|
---|
820 | {
|
---|
821 | /* Try assigning the host command to the client and store the
|
---|
822 | * result code for later use. */
|
---|
823 | rc = pHostCmd->Assign(pConnection);
|
---|
824 | if (RT_FAILURE(rc)) /* If something failed, let the client peek (again). */
|
---|
825 | {
|
---|
826 | rc = pHostCmd->Peek(pConnection);
|
---|
827 | mPeekCount++;
|
---|
828 | }
|
---|
829 | else
|
---|
830 | mPeekCount = 0;
|
---|
831 | }
|
---|
832 | }
|
---|
833 |
|
---|
834 | /* Reset pending status. */
|
---|
835 | mIsPending = false;
|
---|
836 |
|
---|
837 | /* In any case the client did something, so complete
|
---|
838 | * the pending call with the result we just got. */
|
---|
839 | AssertPtr(mSvcHelpers);
|
---|
840 | mSvcHelpers->pfnCallComplete(pConnection->mHandle, rc);
|
---|
841 |
|
---|
842 | LogFlowFunc(("[Client %RU32] mPeekCount=%RU32, pConnection=%p, pHostCmd=%p, replyRc=%Rrc\n",
|
---|
843 | mID, mPeekCount, pConnection, pHostCmd, rc));
|
---|
844 | return rc;
|
---|
845 | }
|
---|
846 |
|
---|
847 | PVBOXHGCMSVCHELPERS mSvcHelpers;
|
---|
848 | /** The client's ID. */
|
---|
849 | uint32_t mID;
|
---|
850 | /** Client flags. @sa CLIENTSTATE_FLAG_ flags. */
|
---|
851 | uint32_t mFlags;
|
---|
852 | /** The context ID filter mask, if any. */
|
---|
853 | uint32_t mFilterMask;
|
---|
854 | /** The context ID filter value, if any. */
|
---|
855 | uint32_t mFilterValue;
|
---|
856 | /** Host command list to process. */
|
---|
857 | HostCmdList mHostCmdList;
|
---|
858 | /** Last (most recent) rc after handling the host command. */
|
---|
859 | int mHostCmdRc;
|
---|
860 | /** How many GUEST_MSG_WAIT calls the client has issued to retrieve one command.
|
---|
861 | *
|
---|
862 | * This is used as a heuristic to remove a message that the client appears not
|
---|
863 | * to be able to successfully retrieve. */
|
---|
864 | uint32_t mHostCmdTries;
|
---|
865 | /** Timestamp (us) of last host command processed. */
|
---|
866 | uint64_t mHostCmdTS;
|
---|
867 | /** Flag indicating whether a client call (GUEST_MSG_WAIT) currently is pending.
|
---|
868 | *
|
---|
869 | * This means the client waits for a new host command to reply and won't return
|
---|
870 | * from the waiting call until a new host command is available.
|
---|
871 | */
|
---|
872 | bool mIsPending;
|
---|
873 | /** Number of times we've peeked at a pending message.
|
---|
874 | *
|
---|
875 | * This is necessary for being compatible with older Guest Additions. In case
|
---|
876 | * there are commands which only have two (2) parameters and therefore would fit
|
---|
877 | * into the GUEST_MSG_WAIT reply immediately, we now can make sure that the
|
---|
878 | * client first gets back the GUEST_MSG_WAIT results first.
|
---|
879 | */
|
---|
880 | uint32_t mPeekCount;
|
---|
881 | /** The client's pending connection. */
|
---|
882 | ClientConnection mPendingCon;
|
---|
883 | } ClientState;
|
---|
884 | typedef std::map< uint32_t, ClientState > ClientStateMap;
|
---|
885 | typedef std::map< uint32_t, ClientState >::iterator ClientStateMapIter;
|
---|
886 | typedef std::map< uint32_t, ClientState >::const_iterator ClientStateMapIterConst;
|
---|
887 |
|
---|
888 | /**
|
---|
889 | * Class containing the shared information service functionality.
|
---|
890 | */
|
---|
891 | class Service : public RTCNonCopyable
|
---|
892 | {
|
---|
893 |
|
---|
894 | private:
|
---|
895 |
|
---|
896 | /** Type definition for use in callback functions. */
|
---|
897 | typedef Service SELF;
|
---|
898 | /** HGCM helper functions. */
|
---|
899 | PVBOXHGCMSVCHELPERS mpHelpers;
|
---|
900 | /**
|
---|
901 | * Callback function supplied by the host for notification of updates
|
---|
902 | * to properties.
|
---|
903 | */
|
---|
904 | PFNHGCMSVCEXT mpfnHostCallback;
|
---|
905 | /** User data pointer to be supplied to the host callback function. */
|
---|
906 | void *mpvHostData;
|
---|
907 | /** List containing all buffered host commands. */
|
---|
908 | RTLISTANCHOR mHostCmdList;
|
---|
909 | /** Map containing all connected clients. The primary key contains
|
---|
910 | * the HGCM client ID to identify the client. */
|
---|
911 | ClientStateMap mClientStateMap;
|
---|
912 | public:
|
---|
913 | explicit Service(PVBOXHGCMSVCHELPERS pHelpers)
|
---|
914 | : mpHelpers(pHelpers)
|
---|
915 | , mpfnHostCallback(NULL)
|
---|
916 | , mpvHostData(NULL)
|
---|
917 | {
|
---|
918 | RTListInit(&mHostCmdList);
|
---|
919 | }
|
---|
920 |
|
---|
921 | /**
|
---|
922 | * @interface_method_impl{VBOXHGCMSVCFNTABLE,pfnUnload}
|
---|
923 | * Simply deletes the service object
|
---|
924 | */
|
---|
925 | static DECLCALLBACK(int) svcUnload(void *pvService)
|
---|
926 | {
|
---|
927 | AssertLogRelReturn(VALID_PTR(pvService), VERR_INVALID_PARAMETER);
|
---|
928 | SELF *pSelf = reinterpret_cast<SELF *>(pvService);
|
---|
929 | int rc = pSelf->uninit();
|
---|
930 | AssertRC(rc);
|
---|
931 | if (RT_SUCCESS(rc))
|
---|
932 | delete pSelf;
|
---|
933 | return rc;
|
---|
934 | }
|
---|
935 |
|
---|
936 | /**
|
---|
937 | * @interface_method_impl{VBOXHGCMSVCFNTABLE,pfnConnect}
|
---|
938 | * Stub implementation of pfnConnect and pfnDisconnect.
|
---|
939 | */
|
---|
940 | static DECLCALLBACK(int) svcConnect(void *pvService,
|
---|
941 | uint32_t u32ClientID,
|
---|
942 | void *pvClient)
|
---|
943 | {
|
---|
944 | AssertLogRelReturn(VALID_PTR(pvService), VERR_INVALID_PARAMETER);
|
---|
945 | SELF *pSelf = reinterpret_cast<SELF *>(pvService);
|
---|
946 | AssertPtrReturn(pSelf, VERR_INVALID_POINTER);
|
---|
947 | return pSelf->clientConnect(u32ClientID, pvClient);
|
---|
948 | }
|
---|
949 |
|
---|
950 | /**
|
---|
951 | * @interface_method_impl{VBOXHGCMSVCFNTABLE,pfnConnect}
|
---|
952 | * Stub implementation of pfnConnect and pfnDisconnect.
|
---|
953 | */
|
---|
954 | static DECLCALLBACK(int) svcDisconnect(void *pvService,
|
---|
955 | uint32_t u32ClientID,
|
---|
956 | void *pvClient)
|
---|
957 | {
|
---|
958 | AssertLogRelReturn(VALID_PTR(pvService), VERR_INVALID_PARAMETER);
|
---|
959 | SELF *pSelf = reinterpret_cast<SELF *>(pvService);
|
---|
960 | AssertPtrReturn(pSelf, VERR_INVALID_POINTER);
|
---|
961 | return pSelf->clientDisconnect(u32ClientID, pvClient);
|
---|
962 | }
|
---|
963 |
|
---|
964 | /**
|
---|
965 | * @interface_method_impl{VBOXHGCMSVCFNTABLE,pfnCall}
|
---|
966 | * Wraps to the call member function
|
---|
967 | */
|
---|
968 | static DECLCALLBACK(void) svcCall(void * pvService,
|
---|
969 | VBOXHGCMCALLHANDLE callHandle,
|
---|
970 | uint32_t u32ClientID,
|
---|
971 | void *pvClient,
|
---|
972 | uint32_t u32Function,
|
---|
973 | uint32_t cParms,
|
---|
974 | VBOXHGCMSVCPARM paParms[])
|
---|
975 | {
|
---|
976 | AssertLogRelReturnVoid(VALID_PTR(pvService));
|
---|
977 | SELF *pSelf = reinterpret_cast<SELF *>(pvService);
|
---|
978 | AssertPtrReturnVoid(pSelf);
|
---|
979 | pSelf->call(callHandle, u32ClientID, pvClient, u32Function, cParms, paParms);
|
---|
980 | }
|
---|
981 |
|
---|
982 | /**
|
---|
983 | * @interface_method_impl{VBOXHGCMSVCFNTABLE,pfnHostCall}
|
---|
984 | * Wraps to the hostCall member function
|
---|
985 | */
|
---|
986 | static DECLCALLBACK(int) svcHostCall(void *pvService,
|
---|
987 | uint32_t u32Function,
|
---|
988 | uint32_t cParms,
|
---|
989 | VBOXHGCMSVCPARM paParms[])
|
---|
990 | {
|
---|
991 | AssertLogRelReturn(VALID_PTR(pvService), VERR_INVALID_PARAMETER);
|
---|
992 | SELF *pSelf = reinterpret_cast<SELF *>(pvService);
|
---|
993 | AssertPtrReturn(pSelf, VERR_INVALID_POINTER);
|
---|
994 | return pSelf->hostCall(u32Function, cParms, paParms);
|
---|
995 | }
|
---|
996 |
|
---|
997 | /**
|
---|
998 | * @interface_method_impl{VBOXHGCMSVCFNTABLE,pfnRegisterExtension}
|
---|
999 | * Installs a host callback for notifications of property changes.
|
---|
1000 | */
|
---|
1001 | static DECLCALLBACK(int) svcRegisterExtension(void *pvService,
|
---|
1002 | PFNHGCMSVCEXT pfnExtension,
|
---|
1003 | void *pvExtension)
|
---|
1004 | {
|
---|
1005 | AssertLogRelReturn(VALID_PTR(pvService), VERR_INVALID_PARAMETER);
|
---|
1006 | SELF *pSelf = reinterpret_cast<SELF *>(pvService);
|
---|
1007 | AssertPtrReturn(pSelf, VERR_INVALID_POINTER);
|
---|
1008 | pSelf->mpfnHostCallback = pfnExtension;
|
---|
1009 | pSelf->mpvHostData = pvExtension;
|
---|
1010 | return VINF_SUCCESS;
|
---|
1011 | }
|
---|
1012 |
|
---|
1013 | private:
|
---|
1014 |
|
---|
1015 | int prepareExecute(uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
|
---|
1016 | int clientConnect(uint32_t u32ClientID, void *pvClient);
|
---|
1017 | int clientDisconnect(uint32_t u32ClientID, void *pvClient);
|
---|
1018 | int clientGetCommand(uint32_t u32ClientID, VBOXHGCMCALLHANDLE callHandle, uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
|
---|
1019 | int clientSetMsgFilterSet(uint32_t u32ClientID, VBOXHGCMCALLHANDLE callHandle, uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
|
---|
1020 | int clientSetMsgFilterUnset(uint32_t u32ClientID, VBOXHGCMCALLHANDLE callHandle, uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
|
---|
1021 | int clientSkipMsg(uint32_t u32ClientID, VBOXHGCMCALLHANDLE callHandle, uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
|
---|
1022 | int cancelHostCmd(uint32_t u32ContextID);
|
---|
1023 | int cancelPendingWaits(uint32_t u32ClientID, int rcPending);
|
---|
1024 | int hostCallback(uint32_t eFunction, uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
|
---|
1025 | int hostProcessCommand(uint32_t eFunction, uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
|
---|
1026 | void call(VBOXHGCMCALLHANDLE callHandle, uint32_t u32ClientID, void *pvClient, uint32_t eFunction, uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
|
---|
1027 | int hostCall(uint32_t eFunction, uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
|
---|
1028 | int sessionClose(uint32_t u32ClientID, VBOXHGCMCALLHANDLE callHandle, uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
|
---|
1029 | int uninit(void);
|
---|
1030 |
|
---|
1031 | DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP(Service);
|
---|
1032 | };
|
---|
1033 |
|
---|
1034 | /**
|
---|
1035 | * Handles a client which just connected.
|
---|
1036 | *
|
---|
1037 | * @return IPRT status code.
|
---|
1038 | * @param u32ClientID
|
---|
1039 | * @param pvClient
|
---|
1040 | */
|
---|
1041 | int Service::clientConnect(uint32_t u32ClientID, void *pvClient)
|
---|
1042 | {
|
---|
1043 | RT_NOREF1(pvClient);
|
---|
1044 | LogFlowFunc(("[Client %RU32] Connected\n", u32ClientID));
|
---|
1045 | #ifdef VBOX_STRICT
|
---|
1046 | ClientStateMapIterConst it = mClientStateMap.find(u32ClientID);
|
---|
1047 | if (it != mClientStateMap.end())
|
---|
1048 | {
|
---|
1049 | AssertMsgFailed(("Client with ID=%RU32 already connected when it should not\n",
|
---|
1050 | u32ClientID));
|
---|
1051 | return VERR_ALREADY_EXISTS;
|
---|
1052 | }
|
---|
1053 | #endif
|
---|
1054 | ClientState clientState(mpHelpers, u32ClientID);
|
---|
1055 | mClientStateMap[u32ClientID] = clientState;
|
---|
1056 | /** @todo Exception handling! */
|
---|
1057 | return VINF_SUCCESS;
|
---|
1058 | }
|
---|
1059 |
|
---|
1060 | /**
|
---|
1061 | * Handles a client which disconnected. This functiond does some
|
---|
1062 | * internal cleanup as well as sends notifications to the host so
|
---|
1063 | * that the host can do the same (if required).
|
---|
1064 | *
|
---|
1065 | * @return IPRT status code.
|
---|
1066 | * @param u32ClientID The client's ID of which disconnected.
|
---|
1067 | * @param pvClient User data, not used at the moment.
|
---|
1068 | */
|
---|
1069 | int Service::clientDisconnect(uint32_t u32ClientID, void *pvClient)
|
---|
1070 | {
|
---|
1071 | RT_NOREF1(pvClient);
|
---|
1072 | LogFlowFunc(("[Client %RU32] Disconnected (%zu clients total)\n",
|
---|
1073 | u32ClientID, mClientStateMap.size()));
|
---|
1074 |
|
---|
1075 | AssertMsg(mClientStateMap.size(),
|
---|
1076 | ("No clients in list anymore when there should (client ID=%RU32)\n", u32ClientID));
|
---|
1077 |
|
---|
1078 | int rc = VINF_SUCCESS;
|
---|
1079 |
|
---|
1080 | ClientStateMapIter itClientState = mClientStateMap.find(u32ClientID);
|
---|
1081 | AssertMsg(itClientState != mClientStateMap.end(),
|
---|
1082 | ("Client ID=%RU32 not found in client list when it should be there\n", u32ClientID));
|
---|
1083 |
|
---|
1084 | if (itClientState != mClientStateMap.end())
|
---|
1085 | {
|
---|
1086 | itClientState->second.DequeueAll();
|
---|
1087 |
|
---|
1088 | mClientStateMap.erase(itClientState);
|
---|
1089 | }
|
---|
1090 |
|
---|
1091 | bool fAllClientsDisconnected = mClientStateMap.empty();
|
---|
1092 | if (fAllClientsDisconnected)
|
---|
1093 | {
|
---|
1094 | LogFlowFunc(("All clients disconnected, cancelling all host commands ...\n"));
|
---|
1095 |
|
---|
1096 | /*
|
---|
1097 | * If all clients disconnected we also need to make sure that all buffered
|
---|
1098 | * host commands need to be notified, because Main is waiting a notification
|
---|
1099 | * via a (multi stage) progress object.
|
---|
1100 | */
|
---|
1101 | HostCommand *pCurCmd = RTListGetFirst(&mHostCmdList, HostCommand, Node);
|
---|
1102 | while (pCurCmd)
|
---|
1103 | {
|
---|
1104 | HostCommand *pNext = RTListNodeGetNext(&pCurCmd->Node, HostCommand, Node);
|
---|
1105 | bool fLast = RTListNodeIsLast(&mHostCmdList, &pCurCmd->Node);
|
---|
1106 |
|
---|
1107 | int rc2 = cancelHostCmd(pCurCmd->mContextID);
|
---|
1108 | if (RT_FAILURE(rc2))
|
---|
1109 | {
|
---|
1110 | LogFlowFunc(("Cancelling host command with CID=%u (refCount=%RU32) failed with rc=%Rrc\n",
|
---|
1111 | pCurCmd->mContextID, pCurCmd->mRefCount, rc2));
|
---|
1112 | /* Keep going. */
|
---|
1113 | }
|
---|
1114 |
|
---|
1115 | while (pCurCmd->Release())
|
---|
1116 | ;
|
---|
1117 | delete pCurCmd;
|
---|
1118 | pCurCmd = NULL;
|
---|
1119 |
|
---|
1120 | if (fLast)
|
---|
1121 | break;
|
---|
1122 |
|
---|
1123 | pCurCmd = pNext;
|
---|
1124 | }
|
---|
1125 |
|
---|
1126 | Assert(RTListIsEmpty(&mHostCmdList));
|
---|
1127 | }
|
---|
1128 |
|
---|
1129 | return rc;
|
---|
1130 | }
|
---|
1131 |
|
---|
1132 | /**
|
---|
1133 | * Either fills in parameters from a pending host command into our guest context or
|
---|
1134 | * defer the guest call until we have something from the host.
|
---|
1135 | *
|
---|
1136 | * @return IPRT status code.
|
---|
1137 | * @param u32ClientID The client's ID.
|
---|
1138 | * @param callHandle The client's call handle.
|
---|
1139 | * @param cParms Number of parameters.
|
---|
1140 | * @param paParms Array of parameters.
|
---|
1141 | */
|
---|
1142 | int Service::clientGetCommand(uint32_t u32ClientID, VBOXHGCMCALLHANDLE callHandle,
|
---|
1143 | uint32_t cParms, VBOXHGCMSVCPARM paParms[])
|
---|
1144 | {
|
---|
1145 | /*
|
---|
1146 | * Lookup client in our list so that we can assign the context ID of
|
---|
1147 | * a command to that client.
|
---|
1148 | */
|
---|
1149 | ClientStateMapIter itClientState = mClientStateMap.find(u32ClientID);
|
---|
1150 | AssertMsg(itClientState != mClientStateMap.end(), ("Client with ID=%RU32 not found when it should be present\n",
|
---|
1151 | u32ClientID));
|
---|
1152 | if (itClientState == mClientStateMap.end())
|
---|
1153 | {
|
---|
1154 | /* Should never happen. Complete the call on the guest side though. */
|
---|
1155 | AssertPtr(mpHelpers);
|
---|
1156 | mpHelpers->pfnCallComplete(callHandle, VERR_NOT_FOUND);
|
---|
1157 |
|
---|
1158 | return VERR_NOT_FOUND;
|
---|
1159 | }
|
---|
1160 |
|
---|
1161 | ClientState &clientState = itClientState->second;
|
---|
1162 |
|
---|
1163 | /* Use the current (inbound) connection. */
|
---|
1164 | ClientConnection thisCon;
|
---|
1165 | thisCon.mHandle = callHandle;
|
---|
1166 | thisCon.mNumParms = cParms;
|
---|
1167 | thisCon.mParms = paParms;
|
---|
1168 |
|
---|
1169 | return clientState.RunCurrent(&thisCon);
|
---|
1170 | }
|
---|
1171 |
|
---|
1172 | int Service::clientSetMsgFilterSet(uint32_t u32ClientID, VBOXHGCMCALLHANDLE callHandle,
|
---|
1173 | uint32_t cParms, VBOXHGCMSVCPARM paParms[])
|
---|
1174 | {
|
---|
1175 | RT_NOREF1(callHandle);
|
---|
1176 |
|
---|
1177 | /*
|
---|
1178 | * Lookup client in our list so that we can assign the context ID of
|
---|
1179 | * a command to that client.
|
---|
1180 | */
|
---|
1181 | ClientStateMapIter itClientState = mClientStateMap.find(u32ClientID);
|
---|
1182 | AssertMsg(itClientState != mClientStateMap.end(), ("Client with ID=%RU32 not found when it should be present\n",
|
---|
1183 | u32ClientID));
|
---|
1184 | if (itClientState == mClientStateMap.end())
|
---|
1185 | return VERR_NOT_FOUND; /* Should never happen. */
|
---|
1186 |
|
---|
1187 | if (cParms != 4)
|
---|
1188 | return VERR_INVALID_PARAMETER;
|
---|
1189 |
|
---|
1190 | uint32_t uValue;
|
---|
1191 | int rc = paParms[0].getUInt32(&uValue);
|
---|
1192 | if (RT_SUCCESS(rc))
|
---|
1193 | {
|
---|
1194 | uint32_t uMaskAdd;
|
---|
1195 | rc = paParms[1].getUInt32(&uMaskAdd);
|
---|
1196 | if (RT_SUCCESS(rc))
|
---|
1197 | {
|
---|
1198 | uint32_t uMaskRemove;
|
---|
1199 | rc = paParms[2].getUInt32(&uMaskRemove);
|
---|
1200 | /** @todo paParm[3] (flags) not used yet. */
|
---|
1201 | if (RT_SUCCESS(rc))
|
---|
1202 | {
|
---|
1203 | ClientState &clientState = itClientState->second;
|
---|
1204 |
|
---|
1205 | clientState.mFlags |= CLIENTSTATE_FLAG_CONTEXTFILTER;
|
---|
1206 | if (uMaskAdd)
|
---|
1207 | clientState.mFilterMask |= uMaskAdd;
|
---|
1208 | if (uMaskRemove)
|
---|
1209 | clientState.mFilterMask &= ~uMaskRemove;
|
---|
1210 |
|
---|
1211 | clientState.mFilterValue = uValue;
|
---|
1212 |
|
---|
1213 | LogFlowFunc(("[Client %RU32] Setting message filterMask=0x%x, filterVal=%RU32 set (flags=0x%x, maskAdd=0x%x, maskRemove=0x%x)\n",
|
---|
1214 | u32ClientID, clientState.mFilterMask, clientState.mFilterValue,
|
---|
1215 | clientState.mFlags, uMaskAdd, uMaskRemove));
|
---|
1216 | }
|
---|
1217 | }
|
---|
1218 | }
|
---|
1219 |
|
---|
1220 | return rc;
|
---|
1221 | }
|
---|
1222 |
|
---|
1223 | int Service::clientSetMsgFilterUnset(uint32_t u32ClientID, VBOXHGCMCALLHANDLE callHandle,
|
---|
1224 | uint32_t cParms, VBOXHGCMSVCPARM paParms[])
|
---|
1225 | {
|
---|
1226 | RT_NOREF2(callHandle, paParms);
|
---|
1227 |
|
---|
1228 | /*
|
---|
1229 | * Lookup client in our list so that we can assign the context ID of
|
---|
1230 | * a command to that client.
|
---|
1231 | */
|
---|
1232 | ClientStateMapIter itClientState = mClientStateMap.find(u32ClientID);
|
---|
1233 | AssertMsg(itClientState != mClientStateMap.end(), ("Client with ID=%RU32 not found when it should be present\n",
|
---|
1234 | u32ClientID));
|
---|
1235 | if (itClientState == mClientStateMap.end())
|
---|
1236 | return VERR_NOT_FOUND; /* Should never happen. */
|
---|
1237 |
|
---|
1238 | if (cParms != 1)
|
---|
1239 | return VERR_INVALID_PARAMETER;
|
---|
1240 |
|
---|
1241 | ClientState &clientState = itClientState->second;
|
---|
1242 |
|
---|
1243 | clientState.mFlags &= ~CLIENTSTATE_FLAG_CONTEXTFILTER;
|
---|
1244 | clientState.mFilterMask = 0;
|
---|
1245 | clientState.mFilterValue = 0;
|
---|
1246 |
|
---|
1247 | LogFlowFunc(("[Client %RU32} Unset message filter\n", u32ClientID));
|
---|
1248 | return VINF_SUCCESS;
|
---|
1249 | }
|
---|
1250 |
|
---|
1251 | int Service::clientSkipMsg(uint32_t u32ClientID, VBOXHGCMCALLHANDLE callHandle,
|
---|
1252 | uint32_t cParms, VBOXHGCMSVCPARM paParms[])
|
---|
1253 | {
|
---|
1254 | RT_NOREF2(callHandle, paParms);
|
---|
1255 |
|
---|
1256 | /*
|
---|
1257 | * Lookup client in our list so that we can assign the context ID of
|
---|
1258 | * a command to that client.
|
---|
1259 | */
|
---|
1260 | ClientStateMapIter itClientState = mClientStateMap.find(u32ClientID);
|
---|
1261 | AssertMsg(itClientState != mClientStateMap.end(), ("Client ID=%RU32 not found when it should be present\n",
|
---|
1262 | u32ClientID));
|
---|
1263 | if (itClientState == mClientStateMap.end())
|
---|
1264 | return VERR_NOT_FOUND; /* Should never happen. */
|
---|
1265 |
|
---|
1266 | if (cParms != 1)
|
---|
1267 | return VERR_INVALID_PARAMETER;
|
---|
1268 |
|
---|
1269 | LogFlowFunc(("[Client %RU32] Skipping current message ...\n", u32ClientID));
|
---|
1270 |
|
---|
1271 | itClientState->second.DequeueCurrent();
|
---|
1272 |
|
---|
1273 | return VINF_SUCCESS;
|
---|
1274 | }
|
---|
1275 |
|
---|
1276 | /**
|
---|
1277 | * Cancels a buffered host command to unblock waiting on Main side
|
---|
1278 | * via callbacks.
|
---|
1279 | *
|
---|
1280 | * @return IPRT status code.
|
---|
1281 | * @param u32ContextID Context ID of host command to cancel.
|
---|
1282 | */
|
---|
1283 | int Service::cancelHostCmd(uint32_t u32ContextID)
|
---|
1284 | {
|
---|
1285 | Assert(mpfnHostCallback);
|
---|
1286 |
|
---|
1287 | LogFlowFunc(("Cancelling CID=%u ...\n", u32ContextID));
|
---|
1288 |
|
---|
1289 | uint32_t cParms = 0;
|
---|
1290 | VBOXHGCMSVCPARM arParms[2];
|
---|
1291 | arParms[cParms++].setUInt32(u32ContextID);
|
---|
1292 |
|
---|
1293 | return hostCallback(GUEST_DISCONNECTED, cParms, arParms);
|
---|
1294 | }
|
---|
1295 |
|
---|
1296 | /**
|
---|
1297 | * Client asks itself (in another thread) to cancel all pending waits which are blocking the client
|
---|
1298 | * from shutting down / doing something else.
|
---|
1299 | *
|
---|
1300 | * @return IPRT status code.
|
---|
1301 | * @param u32ClientID The client's ID.
|
---|
1302 | * @param rcPending Result code for completing pending operation.
|
---|
1303 | */
|
---|
1304 | int Service::cancelPendingWaits(uint32_t u32ClientID, int rcPending)
|
---|
1305 | {
|
---|
1306 | ClientStateMapIter itClientState = mClientStateMap.find(u32ClientID);
|
---|
1307 | if (itClientState != mClientStateMap.end())
|
---|
1308 | return itClientState->second.CancelWaiting(rcPending);
|
---|
1309 |
|
---|
1310 | return VINF_SUCCESS;
|
---|
1311 | }
|
---|
1312 |
|
---|
1313 | /**
|
---|
1314 | * Notifies the host (using low-level HGCM callbacks) about an event
|
---|
1315 | * which was sent from the client.
|
---|
1316 | *
|
---|
1317 | * @return IPRT status code.
|
---|
1318 | * @param eFunction Function (event) that occured.
|
---|
1319 | * @param cParms Number of parameters.
|
---|
1320 | * @param paParms Array of parameters.
|
---|
1321 | */
|
---|
1322 | int Service::hostCallback(uint32_t eFunction, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
|
---|
1323 | {
|
---|
1324 | LogFlowFunc(("eFunction=%ld, cParms=%ld, paParms=%p\n",
|
---|
1325 | eFunction, cParms, paParms));
|
---|
1326 |
|
---|
1327 | int rc;
|
---|
1328 | if (mpfnHostCallback)
|
---|
1329 | {
|
---|
1330 | VBOXGUESTCTRLHOSTCALLBACK data(cParms, paParms);
|
---|
1331 | rc = mpfnHostCallback(mpvHostData, eFunction,
|
---|
1332 | (void *)(&data), sizeof(data));
|
---|
1333 | }
|
---|
1334 | else
|
---|
1335 | rc = VERR_NOT_SUPPORTED;
|
---|
1336 |
|
---|
1337 | LogFlowFunc(("Returning rc=%Rrc\n", rc));
|
---|
1338 | return rc;
|
---|
1339 | }
|
---|
1340 |
|
---|
1341 | /**
|
---|
1342 | * Processes a command received from the host side and re-routes it to
|
---|
1343 | * a connect client on the guest.
|
---|
1344 | *
|
---|
1345 | * @return IPRT status code.
|
---|
1346 | * @param eFunction Function code to process.
|
---|
1347 | * @param cParms Number of parameters.
|
---|
1348 | * @param paParms Array of parameters.
|
---|
1349 | */
|
---|
1350 | int Service::hostProcessCommand(uint32_t eFunction, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
|
---|
1351 | {
|
---|
1352 | /*
|
---|
1353 | * If no client is connected at all we don't buffer any host commands
|
---|
1354 | * and immediately return an error to the host. This avoids the host
|
---|
1355 | * waiting for a response from the guest side in case VBoxService on
|
---|
1356 | * the guest is not running/system is messed up somehow.
|
---|
1357 | */
|
---|
1358 | if (mClientStateMap.empty())
|
---|
1359 | return VERR_NOT_FOUND;
|
---|
1360 |
|
---|
1361 | int rc;
|
---|
1362 |
|
---|
1363 | HostCommand *pHostCmd = NULL;
|
---|
1364 | try
|
---|
1365 | {
|
---|
1366 | pHostCmd = new HostCommand();
|
---|
1367 | rc = pHostCmd->Allocate(eFunction, cParms, paParms);
|
---|
1368 | if (RT_SUCCESS(rc))
|
---|
1369 | /* rc = */ RTListAppend(&mHostCmdList, &pHostCmd->Node);
|
---|
1370 | }
|
---|
1371 | catch (std::bad_alloc)
|
---|
1372 | {
|
---|
1373 | rc = VERR_NO_MEMORY;
|
---|
1374 | }
|
---|
1375 |
|
---|
1376 | if (RT_SUCCESS(rc))
|
---|
1377 | {
|
---|
1378 | LogFlowFunc(("Handling host command CID=%RU32, eFunction=%RU32, cParms=%RU32, paParms=%p, numClients=%zu\n",
|
---|
1379 | pHostCmd->mContextID, eFunction, cParms, paParms, mClientStateMap.size()));
|
---|
1380 |
|
---|
1381 | /*
|
---|
1382 | * Wake up all pending clients which are interested in this
|
---|
1383 | * host command.
|
---|
1384 | */
|
---|
1385 | #ifdef DEBUG
|
---|
1386 | uint32_t uClientsWokenUp = 0;
|
---|
1387 | #endif
|
---|
1388 | ClientStateMapIter itClientState = mClientStateMap.begin();
|
---|
1389 | AssertMsg(itClientState != mClientStateMap.end(), ("Client state map is empty when it should not\n"));
|
---|
1390 | while (itClientState != mClientStateMap.end())
|
---|
1391 | {
|
---|
1392 | ClientState &clientState = itClientState->second;
|
---|
1393 |
|
---|
1394 | /* If a client indicates that it it wants the new host command,
|
---|
1395 | * add a reference to not delete it.*/
|
---|
1396 | if (clientState.WantsHostCommand(pHostCmd))
|
---|
1397 | {
|
---|
1398 | clientState.EnqueueCommand(pHostCmd);
|
---|
1399 |
|
---|
1400 | int rc2 = clientState.Wakeup();
|
---|
1401 | if (RT_FAILURE(rc2))
|
---|
1402 | LogFlowFunc(("Waking up client ID=%RU32 failed with rc=%Rrc\n",
|
---|
1403 | itClientState->first, rc2));
|
---|
1404 | #ifdef DEBUG
|
---|
1405 | uClientsWokenUp++;
|
---|
1406 | #endif
|
---|
1407 | }
|
---|
1408 |
|
---|
1409 | ++itClientState;
|
---|
1410 | }
|
---|
1411 |
|
---|
1412 | #ifdef DEBUG
|
---|
1413 | LogFlowFunc(("%RU32 clients have been woken up\n", uClientsWokenUp));
|
---|
1414 | #endif
|
---|
1415 | }
|
---|
1416 |
|
---|
1417 | return rc;
|
---|
1418 | }
|
---|
1419 |
|
---|
1420 | /**
|
---|
1421 | * @interface_method_impl{VBOXHGCMSVCFNTABLE,pfnCall}
|
---|
1422 | *
|
---|
1423 | * @note All functions which do not involve an unreasonable delay will be
|
---|
1424 | * handled synchronously. If needed, we will add a request handler
|
---|
1425 | * thread in future for those which do.
|
---|
1426 | *
|
---|
1427 | * @thread HGCM
|
---|
1428 | */
|
---|
1429 | void Service::call(VBOXHGCMCALLHANDLE callHandle, uint32_t u32ClientID,
|
---|
1430 | void * /* pvClient */, uint32_t eFunction, uint32_t cParms,
|
---|
1431 | VBOXHGCMSVCPARM paParms[])
|
---|
1432 | {
|
---|
1433 | int rc = VINF_SUCCESS;
|
---|
1434 | LogFlowFunc(("[Client %RU32] eFunction=%RU32, cParms=%RU32, paParms=0x%p\n",
|
---|
1435 | u32ClientID, eFunction, cParms, paParms));
|
---|
1436 | try
|
---|
1437 | {
|
---|
1438 | /*
|
---|
1439 | * The guest asks the host for the next message to process.
|
---|
1440 | */
|
---|
1441 | if (eFunction == GUEST_MSG_WAIT)
|
---|
1442 | {
|
---|
1443 | LogFlowFunc(("[Client %RU32] GUEST_MSG_GET\n", u32ClientID));
|
---|
1444 | rc = clientGetCommand(u32ClientID, callHandle, cParms, paParms);
|
---|
1445 | }
|
---|
1446 | else
|
---|
1447 | {
|
---|
1448 | switch (eFunction)
|
---|
1449 | {
|
---|
1450 | /*
|
---|
1451 | * A client wants to shut down and asks us (this service) to cancel
|
---|
1452 | * all blocking/pending waits (VINF_HGCM_ASYNC_EXECUTE) so that the
|
---|
1453 | * client can gracefully shut down.
|
---|
1454 | */
|
---|
1455 | case GUEST_CANCEL_PENDING_WAITS:
|
---|
1456 | LogFlowFunc(("[Client %RU32] GUEST_CANCEL_PENDING_WAITS\n", u32ClientID));
|
---|
1457 | rc = cancelPendingWaits(u32ClientID, VINF_SUCCESS /* Pending result */);
|
---|
1458 | break;
|
---|
1459 |
|
---|
1460 | /*
|
---|
1461 | * The guest only wants certain messages set by the filter mask(s).
|
---|
1462 | * Since VBox 4.3+.
|
---|
1463 | */
|
---|
1464 | case GUEST_MSG_FILTER_SET:
|
---|
1465 | LogFlowFunc(("[Client %RU32] GUEST_MSG_FILTER_SET\n", u32ClientID));
|
---|
1466 | rc = clientSetMsgFilterSet(u32ClientID, callHandle, cParms, paParms);
|
---|
1467 | break;
|
---|
1468 |
|
---|
1469 | /*
|
---|
1470 | * Unsetting the message filter flag.
|
---|
1471 | */
|
---|
1472 | case GUEST_MSG_FILTER_UNSET:
|
---|
1473 | LogFlowFunc(("[Client %RU32] GUEST_MSG_FILTER_UNSET\n", u32ClientID));
|
---|
1474 | rc = clientSetMsgFilterUnset(u32ClientID, callHandle, cParms, paParms);
|
---|
1475 | break;
|
---|
1476 |
|
---|
1477 | /*
|
---|
1478 | * The guest only wants skip the currently assigned messages. Neded
|
---|
1479 | * for dropping its assigned reference of the current assigned host
|
---|
1480 | * command in queue.
|
---|
1481 | * Since VBox 4.3+.
|
---|
1482 | */
|
---|
1483 | case GUEST_MSG_SKIP:
|
---|
1484 | LogFlowFunc(("[Client %RU32] GUEST_MSG_SKIP\n", u32ClientID));
|
---|
1485 | rc = clientSkipMsg(u32ClientID, callHandle, cParms, paParms);
|
---|
1486 | break;
|
---|
1487 |
|
---|
1488 | /*
|
---|
1489 | * The guest wants to close specific guest session. This is handy for
|
---|
1490 | * shutting down dedicated guest session processes from another process.
|
---|
1491 | */
|
---|
1492 | case GUEST_SESSION_CLOSE:
|
---|
1493 | LogFlowFunc(("[Client %RU32] GUEST_SESSION_CLOSE\n", u32ClientID));
|
---|
1494 | rc = sessionClose(u32ClientID, callHandle, cParms, paParms);
|
---|
1495 | break;
|
---|
1496 |
|
---|
1497 | /*
|
---|
1498 | * For all other regular commands we call our hostCallback
|
---|
1499 | * function. If the current command does not support notifications,
|
---|
1500 | * notifyHost will return VERR_NOT_SUPPORTED.
|
---|
1501 | */
|
---|
1502 | default:
|
---|
1503 | rc = hostCallback(eFunction, cParms, paParms);
|
---|
1504 | break;
|
---|
1505 | }
|
---|
1506 |
|
---|
1507 | if (rc != VINF_HGCM_ASYNC_EXECUTE)
|
---|
1508 | {
|
---|
1509 | /* Tell the client that the call is complete (unblocks waiting). */
|
---|
1510 | AssertPtr(mpHelpers);
|
---|
1511 | mpHelpers->pfnCallComplete(callHandle, rc);
|
---|
1512 | }
|
---|
1513 | }
|
---|
1514 | }
|
---|
1515 | catch (std::bad_alloc)
|
---|
1516 | {
|
---|
1517 | rc = VERR_NO_MEMORY;
|
---|
1518 | }
|
---|
1519 | }
|
---|
1520 |
|
---|
1521 | /**
|
---|
1522 | * Service call handler for the host.
|
---|
1523 | * @interface_method_impl{VBOXHGCMSVCFNTABLE,pfnHostCall}
|
---|
1524 | * @thread hgcm
|
---|
1525 | */
|
---|
1526 | int Service::hostCall(uint32_t eFunction, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
|
---|
1527 | {
|
---|
1528 | int rc = VERR_NOT_SUPPORTED;
|
---|
1529 | LogFlowFunc(("fn=%RU32, cParms=%RU32, paParms=0x%p\n",
|
---|
1530 | eFunction, cParms, paParms));
|
---|
1531 | try
|
---|
1532 | {
|
---|
1533 | switch (eFunction)
|
---|
1534 | {
|
---|
1535 | /**
|
---|
1536 | * Host
|
---|
1537 | */
|
---|
1538 | case HOST_CANCEL_PENDING_WAITS:
|
---|
1539 | {
|
---|
1540 | LogFlowFunc(("HOST_CANCEL_PENDING_WAITS\n"));
|
---|
1541 | ClientStateMapIter itClientState = mClientStateMap.begin();
|
---|
1542 | while (itClientState != mClientStateMap.end())
|
---|
1543 | {
|
---|
1544 | int rc2 = itClientState->second.CancelWaiting(VINF_SUCCESS /* Pending rc. */);
|
---|
1545 | if (RT_FAILURE(rc2))
|
---|
1546 | LogFlowFunc(("Cancelling waiting for client ID=%RU32 failed with rc=%Rrc",
|
---|
1547 | itClientState->first, rc2));
|
---|
1548 | ++itClientState;
|
---|
1549 | }
|
---|
1550 | rc = VINF_SUCCESS;
|
---|
1551 | break;
|
---|
1552 | }
|
---|
1553 |
|
---|
1554 | default:
|
---|
1555 | rc = hostProcessCommand(eFunction, cParms, paParms);
|
---|
1556 | break;
|
---|
1557 | }
|
---|
1558 | }
|
---|
1559 | catch (std::bad_alloc)
|
---|
1560 | {
|
---|
1561 | rc = VERR_NO_MEMORY;
|
---|
1562 | }
|
---|
1563 |
|
---|
1564 | return rc;
|
---|
1565 | }
|
---|
1566 |
|
---|
1567 | /**
|
---|
1568 | * Client asks another client (guest) session to close.
|
---|
1569 | *
|
---|
1570 | * @return IPRT status code.
|
---|
1571 | * @param u32ClientID The client's ID.
|
---|
1572 | * @param callHandle The client's call handle.
|
---|
1573 | * @param cParms Number of parameters.
|
---|
1574 | * @param paParms Array of parameters.
|
---|
1575 | */
|
---|
1576 | int Service::sessionClose(uint32_t u32ClientID, VBOXHGCMCALLHANDLE callHandle, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
|
---|
1577 | {
|
---|
1578 | RT_NOREF2(u32ClientID, callHandle);
|
---|
1579 | if (cParms < 2)
|
---|
1580 | return VERR_INVALID_PARAMETER;
|
---|
1581 |
|
---|
1582 | uint32_t uContextID, uFlags;
|
---|
1583 | int rc = paParms[0].getUInt32(&uContextID);
|
---|
1584 | if (RT_SUCCESS(rc))
|
---|
1585 | rc = paParms[1].getUInt32(&uFlags);
|
---|
1586 |
|
---|
1587 | uint32_t uSessionID = VBOX_GUESTCTRL_CONTEXTID_GET_SESSION(uContextID);
|
---|
1588 |
|
---|
1589 | if (RT_SUCCESS(rc))
|
---|
1590 | rc = hostProcessCommand(HOST_SESSION_CLOSE, cParms, paParms);
|
---|
1591 |
|
---|
1592 | LogFlowFunc(("Closing guest session ID=%RU32 (from client ID=%RU32) returned with rc=%Rrc\n",
|
---|
1593 | uSessionID, u32ClientID, rc)); NOREF(uSessionID);
|
---|
1594 | return rc;
|
---|
1595 | }
|
---|
1596 |
|
---|
1597 | int Service::uninit(void)
|
---|
1598 | {
|
---|
1599 | return VINF_SUCCESS;
|
---|
1600 | }
|
---|
1601 |
|
---|
1602 | } /* namespace guestControl */
|
---|
1603 |
|
---|
1604 | using guestControl::Service;
|
---|
1605 |
|
---|
1606 | /**
|
---|
1607 | * @copydoc VBOXHGCMSVCLOAD
|
---|
1608 | */
|
---|
1609 | extern "C" DECLCALLBACK(DECLEXPORT(int)) VBoxHGCMSvcLoad(VBOXHGCMSVCFNTABLE *pTable)
|
---|
1610 | {
|
---|
1611 | int rc = VINF_SUCCESS;
|
---|
1612 |
|
---|
1613 | LogFlowFunc(("pTable=%p\n", pTable));
|
---|
1614 |
|
---|
1615 | if (!VALID_PTR(pTable))
|
---|
1616 | {
|
---|
1617 | rc = VERR_INVALID_PARAMETER;
|
---|
1618 | }
|
---|
1619 | else
|
---|
1620 | {
|
---|
1621 | LogFlowFunc(("pTable->cbSize=%d, pTable->u32Version=0x%08X\n", pTable->cbSize, pTable->u32Version));
|
---|
1622 |
|
---|
1623 | if ( pTable->cbSize != sizeof (VBOXHGCMSVCFNTABLE)
|
---|
1624 | || pTable->u32Version != VBOX_HGCM_SVC_VERSION)
|
---|
1625 | {
|
---|
1626 | rc = VERR_VERSION_MISMATCH;
|
---|
1627 | }
|
---|
1628 | else
|
---|
1629 | {
|
---|
1630 | Service *pService = NULL;
|
---|
1631 | /* No exceptions may propagate outside. */
|
---|
1632 | try
|
---|
1633 | {
|
---|
1634 | pService = new Service(pTable->pHelpers);
|
---|
1635 | }
|
---|
1636 | catch (int rcThrown)
|
---|
1637 | {
|
---|
1638 | rc = rcThrown;
|
---|
1639 | }
|
---|
1640 | catch(std::bad_alloc &)
|
---|
1641 | {
|
---|
1642 | rc = VERR_NO_MEMORY;
|
---|
1643 | }
|
---|
1644 |
|
---|
1645 | if (RT_SUCCESS(rc))
|
---|
1646 | {
|
---|
1647 | /*
|
---|
1648 | * We don't need an additional client data area on the host,
|
---|
1649 | * because we're a class which can have members for that :-).
|
---|
1650 | */
|
---|
1651 | pTable->cbClient = 0;
|
---|
1652 |
|
---|
1653 | /* Register functions. */
|
---|
1654 | pTable->pfnUnload = Service::svcUnload;
|
---|
1655 | pTable->pfnConnect = Service::svcConnect;
|
---|
1656 | pTable->pfnDisconnect = Service::svcDisconnect;
|
---|
1657 | pTable->pfnCall = Service::svcCall;
|
---|
1658 | pTable->pfnHostCall = Service::svcHostCall;
|
---|
1659 | pTable->pfnSaveState = NULL; /* The service is stateless, so the normal */
|
---|
1660 | pTable->pfnLoadState = NULL; /* construction done before restoring suffices */
|
---|
1661 | pTable->pfnRegisterExtension = Service::svcRegisterExtension;
|
---|
1662 |
|
---|
1663 | /* Service specific initialization. */
|
---|
1664 | pTable->pvService = pService;
|
---|
1665 | }
|
---|
1666 | else
|
---|
1667 | {
|
---|
1668 | if (pService)
|
---|
1669 | {
|
---|
1670 | delete pService;
|
---|
1671 | pService = NULL;
|
---|
1672 | }
|
---|
1673 | }
|
---|
1674 | }
|
---|
1675 | }
|
---|
1676 |
|
---|
1677 | LogFlowFunc(("Returning %Rrc\n", rc));
|
---|
1678 | return rc;
|
---|
1679 | }
|
---|
1680 |
|
---|