VirtualBox

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

Last change on this file since 49950 was 49349, checked in by vboxsync, 11 years ago

Guest Control:

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