VirtualBox

source: vbox/trunk/src/VBox/HostServices/GuestControl/service.cpp@ 72075

Last change on this file since 72075 was 71431, checked in by vboxsync, 7 years ago

Guest Control/service: Partly reverted r121279 (also let the host know that the current message has been skipped.), not needed.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette