VirtualBox

source: vbox/trunk/src/VBox/Main/src-client/GuestCtrlImpl.cpp@ 46526

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

build fix

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.3 KB
Line 
1/* $Id: GuestCtrlImpl.cpp 46526 2013-06-13 12:24:29Z vboxsync $ */
2/** @file
3 * VirtualBox COM class implementation: Guest
4 */
5
6/*
7 * Copyright (C) 2006-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#include "GuestImpl.h"
19#include "GuestSessionImpl.h"
20#include "GuestCtrlImplPrivate.h"
21
22#include "Global.h"
23#include "ConsoleImpl.h"
24#include "ProgressImpl.h"
25#include "VBoxEvents.h"
26#include "VMMDev.h"
27
28#include "AutoCaller.h"
29
30#include <VBox/VMMDev.h>
31#ifdef VBOX_WITH_GUEST_CONTROL
32# include <VBox/com/array.h>
33# include <VBox/com/ErrorInfo.h>
34#endif
35#include <iprt/cpp/utils.h>
36#include <iprt/file.h>
37#include <iprt/getopt.h>
38#include <iprt/isofs.h>
39#include <iprt/list.h>
40#include <iprt/path.h>
41#include <VBox/vmm/pgm.h>
42
43#include <memory>
44
45#ifdef LOG_GROUP
46 #undef LOG_GROUP
47#endif
48#define LOG_GROUP LOG_GROUP_GUEST_CONTROL
49#include <VBox/log.h>
50
51
52// public methods only for internal purposes
53/////////////////////////////////////////////////////////////////////////////
54
55#ifdef VBOX_WITH_GUEST_CONTROL
56/**
57 * Static callback function for receiving updates on guest control commands
58 * from the guest. Acts as a dispatcher for the actual class instance.
59 *
60 * @returns VBox status code.
61 *
62 * @todo
63 *
64 */
65/* static */
66DECLCALLBACK(int) Guest::notifyCtrlDispatcher(void *pvExtension,
67 uint32_t u32Function,
68 void *pvData,
69 uint32_t cbData)
70{
71 using namespace guestControl;
72
73 /*
74 * No locking, as this is purely a notification which does not make any
75 * changes to the object state.
76 */
77 LogFlowFunc(("pvExtension=%p, u32Function=%RU32, pvParms=%p, cbParms=%RU32\n",
78 pvExtension, u32Function, pvData, cbData));
79 ComObjPtr<Guest> pGuest = reinterpret_cast<Guest *>(pvExtension);
80 Assert(!pGuest.isNull());
81
82 /*
83 * For guest control 2.0 using the legacy commands we need to do the following here:
84 * - Get the callback header to access the context ID
85 * - Get the context ID of the callback
86 * - Extract the session ID out of the context ID
87 * - Dispatch the whole stuff to the appropriate session (if still exists)
88 */
89 if (cbData != sizeof(VBOXGUESTCTRLHOSTCALLBACK))
90 return VERR_NOT_SUPPORTED;
91 PVBOXGUESTCTRLHOSTCALLBACK pSvcCb = (PVBOXGUESTCTRLHOSTCALLBACK)pvData;
92 AssertPtr(pSvcCb);
93
94 if (!pSvcCb->mParms) /* At least context ID must be present. */
95 return VERR_INVALID_PARAMETER;
96
97 uint32_t uContextID;
98 int rc = pSvcCb->mpaParms[0].getUInt32(&uContextID);
99 AssertMsgRC(rc, ("Unable to extract callback context ID, pvData=%p\n", pSvcCb));
100 if (RT_FAILURE(rc))
101 return rc;
102#ifdef DEBUG
103 LogFlowFunc(("CID=%RU32, uSession=%RU32, uObject=%RU32, uCount=%RU32\n",
104 uContextID,
105 VBOX_GUESTCTRL_CONTEXTID_GET_SESSION(uContextID),
106 VBOX_GUESTCTRL_CONTEXTID_GET_OBJECT(uContextID),
107 VBOX_GUESTCTRL_CONTEXTID_GET_COUNT(uContextID)));
108#endif
109
110 VBOXGUESTCTRLHOSTCBCTX ctxCb = { u32Function, uContextID };
111 rc = pGuest->dispatchToSession(&ctxCb, pSvcCb);
112 LogFlowFuncLeaveRC(rc);
113 return rc;
114}
115#endif /* VBOX_WITH_GUEST_CONTROL */
116
117STDMETHODIMP Guest::UpdateGuestAdditions(IN_BSTR aSource, ComSafeArrayIn(IN_BSTR, aArguments),
118 ComSafeArrayIn(AdditionsUpdateFlag_T, aFlags), IProgress **aProgress)
119{
120#ifndef VBOX_WITH_GUEST_CONTROL
121 ReturnComNotImplemented();
122#else /* VBOX_WITH_GUEST_CONTROL */
123 CheckComArgStrNotEmptyOrNull(aSource);
124 CheckComArgOutPointerValid(aProgress);
125
126 AutoCaller autoCaller(this);
127 if (FAILED(autoCaller.rc())) return autoCaller.rc();
128
129 /* Validate flags. */
130 uint32_t fFlags = AdditionsUpdateFlag_None;
131 if (aFlags)
132 {
133 com::SafeArray<CopyFileFlag_T> flags(ComSafeArrayInArg(aFlags));
134 for (size_t i = 0; i < flags.size(); i++)
135 fFlags |= flags[i];
136 }
137
138 if (fFlags)
139 {
140 if (!(fFlags & AdditionsUpdateFlag_WaitForUpdateStartOnly))
141 return setError(E_INVALIDARG, tr("Unknown flags (%#x)"), aFlags);
142 }
143
144 int rc = VINF_SUCCESS;
145
146 ProcessArguments aArgs;
147 if (aArguments)
148 {
149 try
150 {
151 com::SafeArray<IN_BSTR> arguments(ComSafeArrayInArg(aArguments));
152 for (size_t i = 0; i < arguments.size(); i++)
153 aArgs.push_back(Utf8Str(arguments[i]));
154 }
155 catch(std::bad_alloc &)
156 {
157 rc = VERR_NO_MEMORY;
158 }
159 }
160
161 HRESULT hr = S_OK;
162
163 /*
164 * Create an anonymous session. This is required to run the Guest Additions
165 * update process with administrative rights.
166 */
167 GuestSessionStartupInfo startupInfo;
168 startupInfo.mName = "Updating Guest Additions";
169
170 GuestCredentials guestCreds;
171 RT_ZERO(guestCreds);
172
173 ComObjPtr<GuestSession> pSession;
174 if (RT_SUCCESS(rc))
175 rc = sessionCreate(startupInfo, guestCreds, pSession);
176 if (RT_FAILURE(rc))
177 {
178 switch (rc)
179 {
180 case VERR_MAX_PROCS_REACHED:
181 hr = setError(VBOX_E_IPRT_ERROR, tr("Maximum number of concurrent guest sessions (%ld) reached"),
182 VBOX_GUESTCTRL_MAX_SESSIONS);
183 break;
184
185 /** @todo Add more errors here. */
186
187 default:
188 hr = setError(VBOX_E_IPRT_ERROR, tr("Could not create guest session: %Rrc"), rc);
189 break;
190 }
191 }
192 else
193 {
194 Assert(!pSession.isNull());
195 int guestRc;
196 rc = pSession->startSessionIntenal(&guestRc);
197 if (RT_FAILURE(rc))
198 {
199 /** @todo Handle guestRc! */
200
201 hr = setError(VBOX_E_IPRT_ERROR, tr("Could not open guest session: %Rrc"), rc);
202 }
203 else
204 {
205 try
206 {
207 ComObjPtr<Progress> pProgress;
208 SessionTaskUpdateAdditions *pTask = new SessionTaskUpdateAdditions(pSession /* GuestSession */,
209 Utf8Str(aSource), aArgs, fFlags);
210 rc = pSession->startTaskAsync(tr("Updating Guest Additions"), pTask, pProgress);
211 if (RT_SUCCESS(rc))
212 {
213 /* Return progress to the caller. */
214 hr = pProgress.queryInterfaceTo(aProgress);
215 }
216 else
217 hr = setError(VBOX_E_IPRT_ERROR,
218 tr("Starting task for updating Guest Additions on the guest failed: %Rrc"), rc);
219 }
220 catch(std::bad_alloc &)
221 {
222 hr = E_OUTOFMEMORY;
223 }
224 }
225 }
226 return hr;
227#endif /* VBOX_WITH_GUEST_CONTROL */
228}
229
230// private methods
231/////////////////////////////////////////////////////////////////////////////
232
233int Guest::dispatchToSession(PVBOXGUESTCTRLHOSTCBCTX pCtxCb, PVBOXGUESTCTRLHOSTCALLBACK pSvcCb)
234{
235 LogFlowFunc(("pCtxCb=%p, pSvcCb=%p\n", pCtxCb, pSvcCb));
236
237 AssertPtrReturn(pCtxCb, VERR_INVALID_POINTER);
238 AssertPtrReturn(pSvcCb, VERR_INVALID_POINTER);
239
240 LogFlowFunc(("uFunction=%RU32, uContextID=%RU32, uProtocol=%RU32\n",
241 pCtxCb->uFunction, pCtxCb->uContextID, pCtxCb->uProtocol));
242
243 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
244
245 uint32_t uSessionID = VBOX_GUESTCTRL_CONTEXTID_GET_SESSION(pCtxCb->uContextID);
246#ifdef DEBUG
247 LogFlowFunc(("uSessionID=%RU32 (%zu total)\n",
248 uSessionID, mData.mGuestSessions.size()));
249#endif
250 GuestSessions::const_iterator itSession
251 = mData.mGuestSessions.find(uSessionID);
252
253 int rc;
254 if (itSession != mData.mGuestSessions.end())
255 {
256 ComObjPtr<GuestSession> pSession(itSession->second);
257 Assert(!pSession.isNull());
258
259 alock.release();
260
261 bool fDispatch = true;
262#ifdef DEBUG
263 /*
264 * Pre-check: If we got a status message with an error and VERR_TOO_MUCH_DATA
265 * it means that that guest could not handle the entire message
266 * because of its exceeding size. This should not happen on daily
267 * use but testcases might try this. It then makes no sense to dispatch
268 * this further because we don't have a valid context ID.
269 */
270 if ( pCtxCb->uFunction == GUEST_EXEC_STATUS
271 && pSvcCb->mParms >= 5)
272 {
273 CALLBACKDATA_PROC_STATUS dataCb;
274 /* pSvcCb->mpaParms[0] always contains the context ID. */
275 pSvcCb->mpaParms[1].getUInt32(&dataCb.uPID);
276 pSvcCb->mpaParms[2].getUInt32(&dataCb.uStatus);
277 pSvcCb->mpaParms[3].getUInt32(&dataCb.uFlags);
278 pSvcCb->mpaParms[4].getPointer(&dataCb.pvData, &dataCb.cbData);
279
280 if ( (dataCb.uStatus == PROC_STS_ERROR)
281 && (dataCb.uFlags == VERR_TOO_MUCH_DATA))
282 {
283 LogFlowFunc(("Requested command with too much data, skipping dispatching ...\n"));
284
285 Assert(dataCb.uPID == 0);
286 fDispatch = false;
287 }
288 }
289#endif
290 if (fDispatch)
291 {
292 switch (pCtxCb->uFunction)
293 {
294 case GUEST_DISCONNECTED:
295 rc = pSession->dispatchToThis(pCtxCb, pSvcCb);
296 break;
297
298 case GUEST_SESSION_NOTIFY:
299 rc = pSession->dispatchToThis(pCtxCb, pSvcCb);
300 break;
301
302 case GUEST_EXEC_STATUS:
303 case GUEST_EXEC_OUTPUT:
304 case GUEST_EXEC_INPUT_STATUS:
305 case GUEST_EXEC_IO_NOTIFY:
306 rc = pSession->dispatchToProcess(pCtxCb, pSvcCb);
307 break;
308
309 case GUEST_FILE_NOTIFY:
310 rc = pSession->dispatchToFile(pCtxCb, pSvcCb);
311 break;
312
313 default:
314 rc = VERR_NOT_SUPPORTED;
315 break;
316 }
317 }
318 else
319 rc = VERR_NOT_FOUND;
320 }
321 else
322 rc = VERR_NOT_FOUND;
323
324 LogFlowFuncLeaveRC(rc);
325 return rc;
326}
327
328int Guest::sessionRemove(GuestSession *pSession)
329{
330 LogFlowThisFuncEnter();
331
332 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
333
334 int rc = VERR_NOT_FOUND;
335
336 LogFlowFunc(("Closing session (ID=%RU32) ...\n", pSession->getId()));
337
338 GuestSessions::iterator itSessions = mData.mGuestSessions.begin();
339 while (itSessions != mData.mGuestSessions.end())
340 {
341 if (pSession == itSessions->second)
342 {
343 LogFlowFunc(("Removing session (pSession=%p, ID=%RU32) (now total %ld sessions)\n",
344 (GuestSession *)itSessions->second, itSessions->second->getId(), mData.mGuestSessions.size() - 1));
345
346 mData.mGuestSessions.erase(itSessions);
347
348 fireGuestSessionRegisteredEvent(mEventSource, pSession,
349 false /* Unregistered */);
350 rc = VINF_SUCCESS;
351 break;
352 }
353
354 itSessions++;
355 }
356
357 LogFlowFuncLeaveRC(rc);
358 return rc;
359}
360
361int Guest::sessionCreate(const GuestSessionStartupInfo &ssInfo,
362 const GuestCredentials &guestCreds, ComObjPtr<GuestSession> &pGuestSession)
363{
364 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
365
366 int rc = VERR_MAX_PROCS_REACHED;
367 if (mData.mGuestSessions.size() >= VBOX_GUESTCTRL_MAX_SESSIONS)
368 return rc;
369
370 try
371 {
372 /* Create a new session ID and assign it. */
373 uint32_t uNewSessionID = 0;
374 uint32_t uTries = 0;
375
376 for (;;)
377 {
378 /* Is the context ID already used? */
379 if (!sessionExists(uNewSessionID))
380 {
381 rc = VINF_SUCCESS;
382 break;
383 }
384 uNewSessionID++;
385 if (uNewSessionID >= VBOX_GUESTCTRL_MAX_SESSIONS)
386 uNewSessionID = 0;
387
388 if (++uTries == VBOX_GUESTCTRL_MAX_SESSIONS)
389 break; /* Don't try too hard. */
390 }
391 if (RT_FAILURE(rc)) throw rc;
392
393 /* Create the session object. */
394 HRESULT hr = pGuestSession.createObject();
395 if (FAILED(hr)) throw VERR_COM_UNEXPECTED;
396
397 /** @todo Use an overloaded copy operator. Later. */
398 GuestSessionStartupInfo startupInfo;
399 startupInfo.mID = uNewSessionID; /* Assign new session ID. */
400 startupInfo.mName = ssInfo.mName;
401 startupInfo.mOpenFlags = ssInfo.mOpenFlags;
402 startupInfo.mOpenTimeoutMS = ssInfo.mOpenTimeoutMS;
403
404 GuestCredentials guestCredentials;
405 if (!guestCreds.mUser.isEmpty())
406 {
407 /** @todo Use an overloaded copy operator. Later. */
408 guestCredentials.mUser = guestCreds.mUser;
409 guestCredentials.mPassword = guestCreds.mPassword;
410 guestCredentials.mDomain = guestCreds.mDomain;
411 }
412 else
413 {
414 /* Internal (annonymous) session. */
415 startupInfo.mIsInternal = true;
416 }
417
418 rc = pGuestSession->init(this, startupInfo, guestCredentials);
419 if (RT_FAILURE(rc)) throw rc;
420
421 /*
422 * Add session object to our session map. This is necessary
423 * before calling openSession because the guest calls back
424 * with the creation result of this session.
425 */
426 mData.mGuestSessions[uNewSessionID] = pGuestSession;
427
428 fireGuestSessionRegisteredEvent(mEventSource, pGuestSession,
429 true /* Registered */);
430 }
431 catch (int rc2)
432 {
433 rc = rc2;
434 }
435
436 LogFlowFuncLeaveRC(rc);
437 return rc;
438}
439
440inline bool Guest::sessionExists(uint32_t uSessionID)
441{
442 GuestSessions::const_iterator itSessions = mData.mGuestSessions.find(uSessionID);
443 return (itSessions == mData.mGuestSessions.end()) ? false : true;
444}
445
446// implementation of public methods
447/////////////////////////////////////////////////////////////////////////////
448
449STDMETHODIMP Guest::CreateSession(IN_BSTR aUser, IN_BSTR aPassword, IN_BSTR aDomain, IN_BSTR aSessionName, IGuestSession **aGuestSession)
450{
451#ifndef VBOX_WITH_GUEST_CONTROL
452 ReturnComNotImplemented();
453#else /* VBOX_WITH_GUEST_CONTROL */
454
455 LogFlowFuncEnter();
456
457 /* Do not allow anonymous sessions (with system rights) with public API. */
458 if (RT_UNLIKELY((aUser) == NULL || *(aUser) == '\0'))
459 return setError(E_INVALIDARG, tr("No user name specified"));
460 CheckComArgOutPointerValid(aGuestSession);
461 /* Rest is optional. */
462
463 AutoCaller autoCaller(this);
464 if (FAILED(autoCaller.rc())) return autoCaller.rc();
465
466 GuestSessionStartupInfo startupInfo;
467 startupInfo.mName = aSessionName;
468
469 GuestCredentials guestCreds;
470 guestCreds.mUser = aUser;
471 guestCreds.mPassword = aPassword;
472 guestCreds.mDomain = aDomain;
473
474 ComObjPtr<GuestSession> pSession;
475 int rc = sessionCreate(startupInfo, guestCreds, pSession);
476 if (RT_SUCCESS(rc))
477 {
478 /* Return guest session to the caller. */
479 HRESULT hr2 = pSession.queryInterfaceTo(aGuestSession);
480 if (FAILED(hr2))
481 rc = VERR_COM_OBJECT_NOT_FOUND;
482 }
483
484 if (RT_SUCCESS(rc))
485 {
486 /* Start (fork) the session asynchronously
487 * on the guest. */
488 rc = pSession->startSessionAsync();
489 }
490
491 HRESULT hr = S_OK;
492
493 if (RT_FAILURE(rc))
494 {
495 switch (rc)
496 {
497 case VERR_MAX_PROCS_REACHED:
498 hr = setError(VBOX_E_IPRT_ERROR, tr("Maximum number of concurrent guest sessions (%ld) reached"),
499 VBOX_GUESTCTRL_MAX_SESSIONS);
500 break;
501
502 /** @todo Add more errors here. */
503
504 default:
505 hr = setError(VBOX_E_IPRT_ERROR, tr("Could not create guest session: %Rrc"), rc);
506 break;
507 }
508 }
509
510 LogFlowFuncLeaveRC(rc);
511 return hr;
512#endif /* VBOX_WITH_GUEST_CONTROL */
513}
514
515STDMETHODIMP Guest::FindSession(IN_BSTR aSessionName, ComSafeArrayOut(IGuestSession *, aSessions))
516{
517#ifndef VBOX_WITH_GUEST_CONTROL
518 ReturnComNotImplemented();
519#else /* VBOX_WITH_GUEST_CONTROL */
520
521 CheckComArgOutSafeArrayPointerValid(aSessions);
522
523 LogFlowFuncEnter();
524
525 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
526
527 Utf8Str strName(aSessionName);
528 std::list < ComObjPtr<GuestSession> > listSessions;
529
530 GuestSessions::const_iterator itSessions = mData.mGuestSessions.begin();
531 while (itSessions != mData.mGuestSessions.end())
532 {
533 if (strName.contains(itSessions->second->getName())) /** @todo Use a (simple) pattern match (IPRT?). */
534 listSessions.push_back(itSessions->second);
535 itSessions++;
536 }
537
538 LogFlowFunc(("Sessions with \"%ls\" = %RU32\n",
539 aSessionName, listSessions.size()));
540
541 if (listSessions.size())
542 {
543 SafeIfaceArray<IGuestSession> sessionIfacs(listSessions);
544 sessionIfacs.detachTo(ComSafeArrayOutArg(aSessions));
545
546 return S_OK;
547 }
548
549 return setErrorNoLog(VBOX_E_OBJECT_NOT_FOUND,
550 tr("Could not find sessions with name '%ls'"),
551 aSessionName);
552#endif /* VBOX_WITH_GUEST_CONTROL */
553}
554
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use