VirtualBox

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

Last change on this file since 30037 was 30013, checked in by vboxsync, 14 years ago

scm cleanup.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 30.5 KB
Line 
1/* $Id: service.cpp 30013 2010-06-03 14:40:59Z vboxsync $ */
2/** @file
3 * Guest Control Service: Controlling the guest.
4 */
5
6/*
7 * Copyright (C) 2010 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 a service. A client is represented by its HGCM client ID.
30 * - Context ID: A (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 happend, 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
54/*******************************************************************************
55* Header Files *
56*******************************************************************************/
57#define LOG_GROUP LOG_GROUP_HGCM
58#include <VBox/HostServices/GuestControlSvc.h>
59
60#include <VBox/log.h>
61#include <iprt/asm.h> /* For ASMBreakpoint(). */
62#include <iprt/assert.h>
63#include <iprt/cpp/autores.h>
64#include <iprt/cpp/utils.h>
65#include <iprt/err.h>
66#include <iprt/mem.h>
67#include <iprt/req.h>
68#include <iprt/string.h>
69#include <iprt/thread.h>
70#include <iprt/time.h>
71
72#include <memory> /* for auto_ptr */
73#include <string>
74#include <list>
75
76#include "gctrl.h"
77
78namespace guestControl {
79
80/**
81 * Structure for holding all clients with their
82 * generated host contexts. This is necessary for
83 * mainting the relationship between a client and its context IDs.
84 */
85struct ClientContexts
86{
87 /** This client ID. */
88 uint32_t mClientID;
89 /** The list of contexts a client is assigned to. */
90 std::list< uint32_t > mContextList;
91
92 /** The normal contructor. */
93 ClientContexts(uint32_t aClientID)
94 : mClientID(aClientID) {}
95};
96/** The client list + iterator type */
97typedef std::list< ClientContexts > ClientContextsList;
98typedef std::list< ClientContexts >::iterator ClientContextsListIter;
99typedef std::list< ClientContexts >::const_iterator ClientContextsListIterConst;
100
101/**
102 * Structure for holding an uncompleted guest call.
103 */
104struct ClientWaiter
105{
106 /** Client ID; a client can have multiple handles! */
107 uint32_t mClientID;
108 /** The call handle */
109 VBOXHGCMCALLHANDLE mHandle;
110 /** The call parameters */
111 VBOXHGCMSVCPARM *mParms;
112 /** Number of parameters */
113 uint32_t mNumParms;
114
115 /** The standard contructor. */
116 ClientWaiter() : mClientID(0), mHandle(0), mParms(NULL), mNumParms(0) {}
117 /** The normal contructor. */
118 ClientWaiter(uint32_t aClientID, VBOXHGCMCALLHANDLE aHandle,
119 VBOXHGCMSVCPARM aParms[], uint32_t cParms)
120 : mClientID(aClientID), mHandle(aHandle), mParms(aParms), mNumParms(cParms) {}
121};
122/** The guest call list type */
123typedef std::list< ClientWaiter > ClientWaiterList;
124typedef std::list< ClientWaiter >::iterator CallListIter;
125typedef std::list< ClientWaiter >::const_iterator CallListIterConst;
126
127/**
128 * Structure for holding a buffered host command.
129 */
130struct HostCmd
131{
132 /** The context ID this command belongs to. Will be extracted
133 * from the HGCM parameters. */
134 uint32_t mContextID;
135 /** Dynamic structure for holding the HGCM parms */
136 VBOXGUESTCTRPARAMBUFFER mParmBuf;
137
138 /** The standard contructor. */
139 HostCmd() : mContextID(0) {}
140};
141/** The host cmd list + iterator type */
142typedef std::list< HostCmd > HostCmdList;
143typedef std::list< HostCmd >::iterator HostCmdListIter;
144typedef std::list< HostCmd >::const_iterator HostCmdListIterConst;
145
146/**
147 * Class containing the shared information service functionality.
148 */
149class Service : public stdx::non_copyable
150{
151private:
152 /** Type definition for use in callback functions. */
153 typedef Service SELF;
154 /** HGCM helper functions. */
155 PVBOXHGCMSVCHELPERS mpHelpers;
156 /*
157 * Callback function supplied by the host for notification of updates
158 * to properties.
159 */
160 PFNHGCMSVCEXT mpfnHostCallback;
161 /** User data pointer to be supplied to the host callback function. */
162 void *mpvHostData;
163 /** The deferred calls list. */
164 ClientWaiterList mClientWaiterList;
165 /** The host command list. */
166 HostCmdList mHostCmds;
167 /** Client contexts list. */
168 ClientContextsList mClientContextsList;
169
170public:
171 explicit Service(PVBOXHGCMSVCHELPERS pHelpers)
172 : mpHelpers(pHelpers)
173 , mpfnHostCallback(NULL)
174 , mpvHostData(NULL)
175 {
176 }
177
178 /**
179 * @copydoc VBOXHGCMSVCHELPERS::pfnUnload
180 * Simply deletes the service object
181 */
182 static DECLCALLBACK(int) svcUnload (void *pvService)
183 {
184 AssertLogRelReturn(VALID_PTR(pvService), VERR_INVALID_PARAMETER);
185 SELF *pSelf = reinterpret_cast<SELF *>(pvService);
186 int rc = pSelf->uninit();
187 AssertRC(rc);
188 if (RT_SUCCESS(rc))
189 delete pSelf;
190 return rc;
191 }
192
193 /**
194 * @copydoc VBOXHGCMSVCHELPERS::pfnConnect
195 * Stub implementation of pfnConnect and pfnDisconnect.
196 */
197 static DECLCALLBACK(int) svcConnect (void *pvService,
198 uint32_t u32ClientID,
199 void *pvClient)
200 {
201 AssertLogRelReturn(VALID_PTR(pvService), VERR_INVALID_PARAMETER);
202 LogFlowFunc (("pvService=%p, u32ClientID=%u, pvClient=%p\n", pvService, u32ClientID, pvClient));
203 SELF *pSelf = reinterpret_cast<SELF *>(pvService);
204 int rc = pSelf->clientConnect(u32ClientID, pvClient);
205 LogFlowFunc (("rc=%Rrc\n", rc));
206 return rc;
207 }
208
209 /**
210 * @copydoc VBOXHGCMSVCHELPERS::pfnConnect
211 * Stub implementation of pfnConnect and pfnDisconnect.
212 */
213 static DECLCALLBACK(int) svcDisconnect (void *pvService,
214 uint32_t u32ClientID,
215 void *pvClient)
216 {
217 AssertLogRelReturn(VALID_PTR(pvService), VERR_INVALID_PARAMETER);
218 LogFlowFunc (("pvService=%p, u32ClientID=%u, pvClient=%p\n", pvService, u32ClientID, pvClient));
219 SELF *pSelf = reinterpret_cast<SELF *>(pvService);
220 int rc = pSelf->clientDisconnect(u32ClientID, pvClient);
221 LogFlowFunc (("rc=%Rrc\n", rc));
222 return rc;
223 }
224
225 /**
226 * @copydoc VBOXHGCMSVCHELPERS::pfnCall
227 * Wraps to the call member function
228 */
229 static DECLCALLBACK(void) svcCall (void * pvService,
230 VBOXHGCMCALLHANDLE callHandle,
231 uint32_t u32ClientID,
232 void *pvClient,
233 uint32_t u32Function,
234 uint32_t cParms,
235 VBOXHGCMSVCPARM paParms[])
236 {
237 AssertLogRelReturnVoid(VALID_PTR(pvService));
238 LogFlowFunc (("pvService=%p, callHandle=%p, u32ClientID=%u, pvClient=%p, u32Function=%u, cParms=%u, paParms=%p\n", pvService, callHandle, u32ClientID, pvClient, u32Function, cParms, paParms));
239 SELF *pSelf = reinterpret_cast<SELF *>(pvService);
240 pSelf->call(callHandle, u32ClientID, pvClient, u32Function, cParms, paParms);
241 LogFlowFunc (("returning\n"));
242 }
243
244 /**
245 * @copydoc VBOXHGCMSVCHELPERS::pfnHostCall
246 * Wraps to the hostCall member function
247 */
248 static DECLCALLBACK(int) svcHostCall (void *pvService,
249 uint32_t u32Function,
250 uint32_t cParms,
251 VBOXHGCMSVCPARM paParms[])
252 {
253 AssertLogRelReturn(VALID_PTR(pvService), VERR_INVALID_PARAMETER);
254 LogFlowFunc (("pvService=%p, u32Function=%u, cParms=%u, paParms=%p\n", pvService, u32Function, cParms, paParms));
255 SELF *pSelf = reinterpret_cast<SELF *>(pvService);
256 int rc = pSelf->hostCall(u32Function, cParms, paParms);
257 LogFlowFunc (("rc=%Rrc\n", rc));
258 return rc;
259 }
260
261 /**
262 * @copydoc VBOXHGCMSVCHELPERS::pfnRegisterExtension
263 * Installs a host callback for notifications of property changes.
264 */
265 static DECLCALLBACK(int) svcRegisterExtension (void *pvService,
266 PFNHGCMSVCEXT pfnExtension,
267 void *pvExtension)
268 {
269 AssertLogRelReturn(VALID_PTR(pvService), VERR_INVALID_PARAMETER);
270 SELF *pSelf = reinterpret_cast<SELF *>(pvService);
271 pSelf->mpfnHostCallback = pfnExtension;
272 pSelf->mpvHostData = pvExtension;
273 return VINF_SUCCESS;
274 }
275private:
276 int paramBufferAllocate(PVBOXGUESTCTRPARAMBUFFER pBuf, uint32_t uMsg, uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
277 void paramBufferFree(PVBOXGUESTCTRPARAMBUFFER pBuf);
278 int paramBufferAssign(PVBOXGUESTCTRPARAMBUFFER pBuf, uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
279 int prepareExecute(uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
280 int clientConnect(uint32_t u32ClientID, void *pvClient);
281 int clientDisconnect(uint32_t u32ClientID, void *pvClient);
282 int sendHostCmdToGuest(HostCmd *pCmd, VBOXHGCMCALLHANDLE callHandle, uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
283 int retrieveNextHostCmd(uint32_t u32ClientID, VBOXHGCMCALLHANDLE callHandle, uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
284 int cancelPendingWaits(uint32_t u32ClientID);
285 int notifyHost(uint32_t eFunction, uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
286 int processHostCmd(uint32_t eFunction, uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
287 void call(VBOXHGCMCALLHANDLE callHandle, uint32_t u32ClientID,
288 void *pvClient, uint32_t eFunction, uint32_t cParms,
289 VBOXHGCMSVCPARM paParms[]);
290 int hostCall(uint32_t eFunction, uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
291 int uninit();
292};
293
294
295/** @todo Write some nice doc headers! */
296/* Stores a HGCM request in an internal buffer (pEx). Needs to be freed later using execBufferFree(). */
297int Service::paramBufferAllocate(PVBOXGUESTCTRPARAMBUFFER pBuf, uint32_t uMsg, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
298{
299 AssertPtr(pBuf);
300 int rc = VINF_SUCCESS;
301
302 /* Paranoia. */
303 if (cParms > 256)
304 cParms = 256;
305
306 /*
307 * Don't verify anything here (yet), because this function only buffers
308 * the HGCM data into an internal structure and reaches it back to the guest (client)
309 * in an unmodified state.
310 */
311 if (RT_SUCCESS(rc))
312 {
313 pBuf->uMsg = uMsg;
314 pBuf->uParmCount = cParms;
315 pBuf->pParms = (VBOXHGCMSVCPARM*)RTMemAlloc(sizeof(VBOXHGCMSVCPARM) * pBuf->uParmCount);
316 if (NULL == pBuf->pParms)
317 {
318 rc = VERR_NO_MEMORY;
319 }
320 else
321 {
322 for (uint32_t i = 0; i < pBuf->uParmCount; i++)
323 {
324 pBuf->pParms[i].type = paParms[i].type;
325 switch (paParms[i].type)
326 {
327 case VBOX_HGCM_SVC_PARM_32BIT:
328 pBuf->pParms[i].u.uint32 = paParms[i].u.uint32;
329 break;
330
331 case VBOX_HGCM_SVC_PARM_64BIT:
332 /* Not supported yet. */
333 break;
334
335 case VBOX_HGCM_SVC_PARM_PTR:
336 pBuf->pParms[i].u.pointer.size = paParms[i].u.pointer.size;
337 if (pBuf->pParms[i].u.pointer.size > 0)
338 {
339 pBuf->pParms[i].u.pointer.addr = RTMemAlloc(pBuf->pParms[i].u.pointer.size);
340 if (NULL == pBuf->pParms[i].u.pointer.addr)
341 {
342 rc = VERR_NO_MEMORY;
343 break;
344 }
345 else
346 memcpy(pBuf->pParms[i].u.pointer.addr,
347 paParms[i].u.pointer.addr,
348 pBuf->pParms[i].u.pointer.size);
349 }
350 break;
351
352 default:
353 break;
354 }
355 if (RT_FAILURE(rc))
356 break;
357 }
358 }
359 }
360 return rc;
361}
362
363/* Frees a buffered HGCM request. */
364void Service::paramBufferFree(PVBOXGUESTCTRPARAMBUFFER pBuf)
365{
366 AssertPtr(pBuf);
367 for (uint32_t i = 0; i < pBuf->uParmCount; i++)
368 {
369 switch (pBuf->pParms[i].type)
370 {
371 case VBOX_HGCM_SVC_PARM_PTR:
372 if (pBuf->pParms[i].u.pointer.size > 0)
373 RTMemFree(pBuf->pParms[i].u.pointer.addr);
374 break;
375 }
376 }
377 if (pBuf->uParmCount)
378 {
379 RTMemFree(pBuf->pParms);
380 pBuf->uParmCount = 0;
381 }
382}
383
384/* Assigns data from a buffered HGCM request to the current HGCM request. */
385int Service::paramBufferAssign(PVBOXGUESTCTRPARAMBUFFER pBuf, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
386{
387 AssertPtr(pBuf);
388 int rc = VINF_SUCCESS;
389 if (cParms != pBuf->uParmCount)
390 {
391 rc = VERR_INVALID_PARAMETER;
392 }
393 else
394 {
395 /** @todo Add check to verify if the HGCM request is the same *type* as the buffered one! */
396 for (uint32_t i = 0; i < pBuf->uParmCount; i++)
397 {
398 paParms[i].type = pBuf->pParms[i].type;
399 switch (paParms[i].type)
400 {
401 case VBOX_HGCM_SVC_PARM_32BIT:
402 paParms[i].u.uint32 = pBuf->pParms[i].u.uint32;
403 break;
404
405 case VBOX_HGCM_SVC_PARM_64BIT:
406 /* Not supported yet. */
407 break;
408
409 case VBOX_HGCM_SVC_PARM_PTR:
410 memcpy(paParms[i].u.pointer.addr,
411 pBuf->pParms[i].u.pointer.addr,
412 pBuf->pParms[i].u.pointer.size);
413 break;
414
415 default:
416 break;
417 }
418 }
419 }
420 return rc;
421}
422
423int Service::clientConnect(uint32_t u32ClientID, void *pvClient)
424{
425 LogFlowFunc(("New client (%ld) connected\n", u32ClientID));
426 return VINF_SUCCESS;
427}
428
429int Service::clientDisconnect(uint32_t u32ClientID, void *pvClient)
430{
431 LogFlowFunc(("Client (%ld) disconnected\n", u32ClientID));
432
433 /*
434 * Throw out all stale clients.
435 */
436 int rc = VINF_SUCCESS;
437
438 CallListIter itCall = mClientWaiterList.begin();
439 while (itCall != mClientWaiterList.end())
440 {
441 if (itCall->mClientID == u32ClientID)
442 {
443 itCall = mClientWaiterList.erase(itCall);
444 }
445 else
446 itCall++;
447 }
448
449 ClientContextsListIter it = mClientContextsList.begin();
450 while ( it != mClientContextsList.end()
451 && RT_SUCCESS(rc))
452 {
453 if (it->mClientID == u32ClientID)
454 {
455 std::list< uint32_t >::iterator itContext = it->mContextList.begin();
456 while ( itContext != it->mContextList.end()
457 && RT_SUCCESS(rc))
458 {
459 LogFlowFunc(("Notifying host context %u of disconnect ...\n", (*itContext)));
460
461 /*
462 * Notify the host that clients with u32ClientID are no longer
463 * around and need to be cleaned up (canceling waits etc).
464 */
465 if (mpfnHostCallback)
466 {
467 CALLBACKDATACLIENTDISCONNECTED data;
468 data.hdr.u32Magic = CALLBACKDATAMAGICCLIENTDISCONNECTED;
469 data.hdr.u32ContextID = (*itContext);
470 rc = mpfnHostCallback(mpvHostData, GUEST_DISCONNECTED, (void *)(&data), sizeof(data));
471 if (RT_FAILURE(rc))
472 LogFlowFunc(("Notification of host context %u failed with %Rrc\n", rc));
473 }
474 itContext++;
475 }
476 it = mClientContextsList.erase(it);
477 }
478 else
479 it++;
480 }
481 return rc;
482}
483
484int Service::sendHostCmdToGuest(HostCmd *pCmd, VBOXHGCMCALLHANDLE callHandle, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
485{
486 AssertPtr(pCmd);
487 int rc;
488
489 /* Sufficient parameter space? */
490 if (pCmd->mParmBuf.uParmCount > cParms)
491 {
492 paParms[0].setUInt32(pCmd->mParmBuf.uMsg); /* Message ID */
493 paParms[1].setUInt32(pCmd->mParmBuf.uParmCount); /* Required parameters for message */
494
495 /*
496 * So this call apparently failed because the guest wanted to peek
497 * how much parameters it has to supply in order to successfully retrieve
498 * this command. Let's tell him so!
499 */
500 rc = VERR_TOO_MUCH_DATA;
501 }
502 else
503 {
504 rc = paramBufferAssign(&pCmd->mParmBuf, cParms, paParms);
505 }
506 return rc;
507}
508
509/*
510 * Either fills in parameters from a pending host command into our guest context or
511 * defer the guest call until we have something from the host.
512 */
513int Service::retrieveNextHostCmd(uint32_t u32ClientID, VBOXHGCMCALLHANDLE callHandle,
514 uint32_t cParms, VBOXHGCMSVCPARM paParms[])
515{
516 int rc = VINF_SUCCESS;
517
518 /*
519 * Lookup client in our list so that we can assign the context ID of
520 * a command to that client.
521 */
522 std::list< ClientContexts >::reverse_iterator it = mClientContextsList.rbegin();
523 while (it != mClientContextsList.rend())
524 {
525 if (it->mClientID == u32ClientID)
526 break;
527 it++;
528 }
529
530 /* Not found? Add client to list. */
531 if (it == mClientContextsList.rend())
532 {
533 mClientContextsList.push_back(ClientContexts(u32ClientID));
534 it = mClientContextsList.rbegin();
535 }
536 Assert(it != mClientContextsList.rend());
537
538 /*
539 * If host command list is empty (nothing to do right now) just
540 * defer the call until we got something to do (makes the client
541 * wait, depending on the flags set).
542 */
543 if (mHostCmds.empty()) /* If command list is empty, defer ... */
544 {
545 mClientWaiterList.push_back(ClientWaiter(u32ClientID, callHandle, paParms, cParms));
546 rc = VINF_HGCM_ASYNC_EXECUTE;
547 }
548 else
549 {
550 /*
551 * Get the next unassigned host command in the list.
552 */
553 HostCmd curCmd = mHostCmds.front();
554 rc = sendHostCmdToGuest(&curCmd, callHandle, cParms, paParms);
555 if (RT_SUCCESS(rc))
556 {
557 /* Remember which client processes which context (for
558 * later reference & cleanup). */
559 Assert(curCmd.mContextID > 0);
560 /// @todo r=bird: check if already in the list.
561 it->mContextList.push_back(curCmd.mContextID);
562
563 /* Only if the guest really got and understood the message remove it from the list. */
564 paramBufferFree(&curCmd.mParmBuf);
565 mHostCmds.pop_front();
566 }
567 }
568 return rc;
569}
570
571/*
572 * Client asks itself (in another thread) to cancel all pending waits which are blocking the client
573 * from shutting down / doing something else.
574 */
575int Service::cancelPendingWaits(uint32_t u32ClientID)
576{
577 int rc = VINF_SUCCESS;
578 CallListIter it = mClientWaiterList.begin();
579 while (it != mClientWaiterList.end())
580 {
581 if (it->mClientID == u32ClientID)
582 {
583 if (it->mNumParms >= 2)
584 {
585 it->mParms[0].setUInt32(GETHOSTMSG_EXEC_HOST_CANCEL_WAIT); /* Message ID. */
586 it->mParms[1].setUInt32(0); /* Required parameters for message. */
587 }
588 if (mpHelpers)
589 mpHelpers->pfnCallComplete(it->mHandle, rc);
590 it = mClientWaiterList.erase(it);
591 }
592 else
593 it++;
594 }
595 return rc;
596}
597
598int Service::notifyHost(uint32_t eFunction, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
599{
600 LogFlowFunc(("eFunction=%ld, cParms=%ld, paParms=%p\n",
601 eFunction, cParms, paParms));
602 int rc = VINF_SUCCESS;
603 if ( eFunction == GUEST_EXEC_SEND_STATUS
604 && cParms == 5)
605 {
606 CALLBACKDATAEXECSTATUS data;
607 data.hdr.u32Magic = CALLBACKDATAMAGICEXECSTATUS;
608 paParms[0].getUInt32(&data.hdr.u32ContextID);
609
610 paParms[1].getUInt32(&data.u32PID);
611 paParms[2].getUInt32(&data.u32Status);
612 paParms[3].getUInt32(&data.u32Flags);
613 paParms[4].getPointer(&data.pvData, &data.cbData);
614
615 if (mpfnHostCallback)
616 rc = mpfnHostCallback(mpvHostData, eFunction,
617 (void *)(&data), sizeof(data));
618 }
619 else if ( eFunction == GUEST_EXEC_SEND_OUTPUT
620 && cParms == 5)
621 {
622 CALLBACKDATAEXECOUT data;
623 data.hdr.u32Magic = CALLBACKDATAMAGICEXECOUT;
624 paParms[0].getUInt32(&data.hdr.u32ContextID);
625
626 paParms[1].getUInt32(&data.u32PID);
627 paParms[2].getUInt32(&data.u32HandleId);
628 paParms[3].getUInt32(&data.u32Flags);
629 paParms[4].getPointer(&data.pvData, &data.cbData);
630
631 if (mpfnHostCallback)
632 rc = mpfnHostCallback(mpvHostData, eFunction,
633 (void *)(&data), sizeof(data));
634 }
635 else
636 rc = VERR_NOT_SUPPORTED;
637 LogFlowFunc(("returning %Rrc\n", rc));
638 return rc;
639}
640
641int Service::processHostCmd(uint32_t eFunction, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
642{
643 int rc = VINF_SUCCESS;
644
645 HostCmd newCmd;
646 rc = paramBufferAllocate(&newCmd.mParmBuf, eFunction, cParms, paParms);
647 if ( RT_SUCCESS(rc)
648 && newCmd.mParmBuf.uParmCount > 0)
649 {
650 /*
651 * Assume that the context ID *always* is the first parameter,
652 * assign the context ID to the command.
653 */
654 newCmd.mParmBuf.pParms[0].getUInt32(&newCmd.mContextID);
655 Assert(newCmd.mContextID > 0);
656 }
657
658 bool fProcessed = false;
659 if (RT_SUCCESS(rc))
660 {
661 /* Can we wake up a waiting client on guest? */
662 if (!mClientWaiterList.empty())
663 {
664 ClientWaiter guest = mClientWaiterList.front();
665 rc = sendHostCmdToGuest(&newCmd,
666 guest.mHandle, guest.mNumParms, guest.mParms);
667
668 /* In any case the client did something, so wake up and remove from list. */
669 AssertPtr(mpHelpers);
670 mpHelpers->pfnCallComplete(guest.mHandle, rc);
671 mClientWaiterList.pop_front();
672
673 /* If we got VERR_TOO_MUCH_DATA we buffer the host command in the next block
674 * and return success to the host. */
675 if (rc == VERR_TOO_MUCH_DATA)
676 {
677 rc = VINF_SUCCESS;
678 }
679 else /* If command was understood by the client, free and remove from host commands list. */
680 {
681 paramBufferFree(&newCmd.mParmBuf);
682 fProcessed = true;
683 }
684 }
685
686 /* If not processed, buffer it ... */
687 if (!fProcessed)
688 {
689 mHostCmds.push_back(newCmd);
690#if 0
691 /* Limit list size by deleting oldest element. */
692 if (mHostCmds.size() > 256) /** @todo Use a define! */
693 mHostCmds.pop_front();
694#endif
695 }
696 }
697 return rc;
698}
699
700/**
701 * Handle an HGCM service call.
702 * @copydoc VBOXHGCMSVCFNTABLE::pfnCall
703 * @note All functions which do not involve an unreasonable delay will be
704 * handled synchronously. If needed, we will add a request handler
705 * thread in future for those which do.
706 *
707 * @thread HGCM
708 */
709void Service::call(VBOXHGCMCALLHANDLE callHandle, uint32_t u32ClientID,
710 void * /* pvClient */, uint32_t eFunction, uint32_t cParms,
711 VBOXHGCMSVCPARM paParms[])
712{
713 int rc = VINF_SUCCESS;
714 LogFlowFunc(("u32ClientID = %d, fn = %d, cParms = %d, pparms = %d\n",
715 u32ClientID, eFunction, cParms, paParms));
716 try
717 {
718 switch (eFunction)
719 {
720 /* The guest asks the host for the next messsage to process. */
721 case GUEST_GET_HOST_MSG:
722 LogFlowFunc(("GUEST_GET_HOST_MSG\n"));
723 rc = retrieveNextHostCmd(u32ClientID, callHandle, cParms, paParms);
724 break;
725
726 case GUEST_CANCEL_PENDING_WAITS:
727 LogFlowFunc(("GUEST_CANCEL_PENDING_WAITS\n"));
728 rc = cancelPendingWaits(u32ClientID);
729 break;
730
731 /* The guest notifies the host that some output at stdout/stderr is available. */
732 case GUEST_EXEC_SEND_OUTPUT:
733 LogFlowFunc(("GUEST_EXEC_SEND_OUTPUT\n"));
734 rc = notifyHost(eFunction, cParms, paParms);
735 break;
736
737 /* The guest notifies the host of the current client status. */
738 case GUEST_EXEC_SEND_STATUS:
739 LogFlowFunc(("SEND_STATUS\n"));
740 rc = notifyHost(eFunction, cParms, paParms);
741 break;
742
743 default:
744 rc = VERR_NOT_SUPPORTED;
745 break;
746 }
747 if (rc != VINF_HGCM_ASYNC_EXECUTE)
748 {
749 /* Tell the client that the call is complete (unblocks waiting). */
750 AssertPtr(mpHelpers);
751 mpHelpers->pfnCallComplete(callHandle, rc);
752 }
753 }
754 catch (std::bad_alloc)
755 {
756 rc = VERR_NO_MEMORY;
757 }
758 LogFlowFunc(("rc = %Rrc\n", rc));
759}
760
761/**
762 * Service call handler for the host.
763 * @copydoc VBOXHGCMSVCFNTABLE::pfnHostCall
764 * @thread hgcm
765 */
766int Service::hostCall(uint32_t eFunction, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
767{
768 int rc = VINF_SUCCESS;
769 LogFlowFunc(("fn = %d, cParms = %d, pparms = %d\n",
770 eFunction, cParms, paParms));
771 try
772 {
773 switch (eFunction)
774 {
775 /* The host wants to execute something. */
776 case HOST_EXEC_CMD:
777 LogFlowFunc(("HOST_EXEC_CMD\n"));
778 rc = processHostCmd(eFunction, cParms, paParms);
779 break;
780
781 /* The host wants to send something to the guest's stdin pipe. */
782 case HOST_EXEC_SET_INPUT:
783 LogFlowFunc(("HOST_EXEC_SET_INPUT\n"));
784 break;
785
786 case HOST_EXEC_GET_OUTPUT:
787 LogFlowFunc(("HOST_EXEC_GET_OUTPUT\n"));
788 rc = processHostCmd(eFunction, cParms, paParms);
789 break;
790
791 default:
792 rc = VERR_NOT_SUPPORTED;
793 break;
794 }
795 }
796 catch (std::bad_alloc)
797 {
798 rc = VERR_NO_MEMORY;
799 }
800
801 LogFlowFunc(("rc = %Rrc\n", rc));
802 return rc;
803}
804
805int Service::uninit()
806{
807 /* Free allocated buffered host commands. */
808 HostCmdListIter it;
809 for (it = mHostCmds.begin(); it != mHostCmds.end(); it++)
810 paramBufferFree(&it->mParmBuf);
811 mHostCmds.clear();
812
813 return VINF_SUCCESS;
814}
815
816} /* namespace guestControl */
817
818using guestControl::Service;
819
820/**
821 * @copydoc VBOXHGCMSVCLOAD
822 */
823extern "C" DECLCALLBACK(DECLEXPORT(int)) VBoxHGCMSvcLoad(VBOXHGCMSVCFNTABLE *ptable)
824{
825 int rc = VINF_SUCCESS;
826
827 LogFlowFunc(("ptable = %p\n", ptable));
828
829 if (!VALID_PTR(ptable))
830 {
831 rc = VERR_INVALID_PARAMETER;
832 }
833 else
834 {
835 LogFlowFunc(("ptable->cbSize = %d, ptable->u32Version = 0x%08X\n", ptable->cbSize, ptable->u32Version));
836
837 if ( ptable->cbSize != sizeof (VBOXHGCMSVCFNTABLE)
838 || ptable->u32Version != VBOX_HGCM_SVC_VERSION)
839 {
840 rc = VERR_VERSION_MISMATCH;
841 }
842 else
843 {
844 std::auto_ptr<Service> apService;
845 /* No exceptions may propogate outside. */
846 try {
847 apService = std::auto_ptr<Service>(new Service(ptable->pHelpers));
848 } catch (int rcThrown) {
849 rc = rcThrown;
850 } catch (...) {
851 rc = VERR_UNRESOLVED_ERROR;
852 }
853
854 if (RT_SUCCESS(rc))
855 {
856 /*
857 * We don't need an additional client data area on the host,
858 * because we're a class which can have members for that :-).
859 */
860 ptable->cbClient = 0;
861
862 /* Register functions. */
863 ptable->pfnUnload = Service::svcUnload;
864 ptable->pfnConnect = Service::svcConnect;
865 ptable->pfnDisconnect = Service::svcDisconnect;
866 ptable->pfnCall = Service::svcCall;
867 ptable->pfnHostCall = Service::svcHostCall;
868 ptable->pfnSaveState = NULL; /* The service is stateless, so the normal */
869 ptable->pfnLoadState = NULL; /* construction done before restoring suffices */
870 ptable->pfnRegisterExtension = Service::svcRegisterExtension;
871
872 /* Service specific initialization. */
873 ptable->pvService = apService.release();
874 }
875 }
876 }
877
878 LogFlowFunc(("returning %Rrc\n", rc));
879 return rc;
880}
881
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use