VirtualBox

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

Last change on this file since 94521 was 93166, checked in by vboxsync, 2 years ago

Guest Control/Main: Renamed Guest::i_sessionRemove() -> Guest::i_sessionDestroy() to better match the function's meaning (also to Guest::i_sessionCreate()).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 24.3 KB
Line 
1/* $Id: GuestCtrlImpl.cpp 93166 2022-01-10 16:43:04Z vboxsync $ */
2/** @file
3 * VirtualBox COM class implementation: Guest
4 */
5
6/*
7 * Copyright (C) 2006-2022 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#define LOG_GROUP LOG_GROUP_GUEST_CONTROL
19#include "LoggingNew.h"
20
21#include "GuestImpl.h"
22#ifdef VBOX_WITH_GUEST_CONTROL
23# include "GuestSessionImpl.h"
24# include "GuestSessionImplTasks.h"
25# include "GuestCtrlImplPrivate.h"
26#endif
27
28#include "Global.h"
29#include "ConsoleImpl.h"
30#include "ProgressImpl.h"
31#include "VBoxEvents.h"
32#include "VMMDev.h"
33
34#include "AutoCaller.h"
35
36#include <VBox/VMMDev.h>
37#ifdef VBOX_WITH_GUEST_CONTROL
38# include <VBox/com/array.h>
39# include <VBox/com/ErrorInfo.h>
40#endif
41#include <iprt/cpp/utils.h>
42#include <iprt/file.h>
43#include <iprt/getopt.h>
44#include <iprt/list.h>
45#include <iprt/path.h>
46#include <VBox/vmm/pgm.h>
47#include <VBox/AssertGuest.h>
48
49#include <memory>
50
51
52/*
53 * This #ifdef goes almost to the end of the file where there are a couple of
54 * IGuest method implementations.
55 */
56#ifdef VBOX_WITH_GUEST_CONTROL
57
58
59// public methods only for internal purposes
60/////////////////////////////////////////////////////////////////////////////
61
62/**
63 * Static callback function for receiving updates on guest control messages
64 * from the guest. Acts as a dispatcher for the actual class instance.
65 *
66 * @returns VBox status code.
67 * @param pvExtension Pointer to HGCM service extension.
68 * @param idMessage HGCM message ID the callback was called for.
69 * @param pvData Pointer to user-supplied callback data.
70 * @param cbData Size (in bytes) of user-supplied callback data.
71 */
72/* static */
73DECLCALLBACK(int) Guest::i_notifyCtrlDispatcher(void *pvExtension,
74 uint32_t idMessage,
75 void *pvData,
76 uint32_t cbData)
77{
78 using namespace guestControl;
79
80 /*
81 * No locking, as this is purely a notification which does not make any
82 * changes to the object state.
83 */
84 Log2Func(("pvExtension=%p, idMessage=%RU32, pvParms=%p, cbParms=%RU32\n", pvExtension, idMessage, pvData, cbData));
85
86 ComObjPtr<Guest> pGuest = reinterpret_cast<Guest *>(pvExtension);
87 AssertReturn(pGuest.isNotNull(), VERR_WRONG_ORDER);
88
89 /*
90 * The data packet should ever be a problem, but check to be sure.
91 */
92 AssertMsgReturn(cbData == sizeof(VBOXGUESTCTRLHOSTCALLBACK),
93 ("Guest control host callback data has wrong size (expected %zu, got %zu) - buggy host service!\n",
94 sizeof(VBOXGUESTCTRLHOSTCALLBACK), cbData), VERR_INVALID_PARAMETER);
95 PVBOXGUESTCTRLHOSTCALLBACK pSvcCb = (PVBOXGUESTCTRLHOSTCALLBACK)pvData;
96 AssertPtrReturn(pSvcCb, VERR_INVALID_POINTER);
97
98 /*
99 * Deal with GUEST_MSG_REPORT_FEATURES here as it shouldn't be handed
100 * i_dispatchToSession() and has different parameters.
101 */
102 if (idMessage == GUEST_MSG_REPORT_FEATURES)
103 {
104 Assert(pSvcCb->mParms == 2);
105 Assert(pSvcCb->mpaParms[0].type == VBOX_HGCM_SVC_PARM_64BIT);
106 Assert(pSvcCb->mpaParms[1].type == VBOX_HGCM_SVC_PARM_64BIT);
107 Assert(pSvcCb->mpaParms[1].u.uint64 & VBOX_GUESTCTRL_GF_1_MUST_BE_ONE);
108 pGuest->mData.mfGuestFeatures0 = pSvcCb->mpaParms[0].u.uint64;
109 pGuest->mData.mfGuestFeatures1 = pSvcCb->mpaParms[1].u.uint64;
110 LogRel(("Guest Control: GUEST_MSG_REPORT_FEATURES: %#RX64, %#RX64\n",
111 pGuest->mData.mfGuestFeatures0, pGuest->mData.mfGuestFeatures1));
112 return VINF_SUCCESS;
113 }
114
115 /*
116 * For guest control 2.0 using the legacy messages we need to do the following here:
117 * - Get the callback header to access the context ID
118 * - Get the context ID of the callback
119 * - Extract the session ID out of the context ID
120 * - Dispatch the whole stuff to the appropriate session (if still exists)
121 *
122 * At least context ID parameter must always be present.
123 */
124 ASSERT_GUEST_RETURN(pSvcCb->mParms > 0, VERR_WRONG_PARAMETER_COUNT);
125 ASSERT_GUEST_MSG_RETURN(pSvcCb->mpaParms[0].type == VBOX_HGCM_SVC_PARM_32BIT,
126 ("type=%d\n", pSvcCb->mpaParms[0].type), VERR_WRONG_PARAMETER_TYPE);
127 uint32_t const idContext = pSvcCb->mpaParms[0].u.uint32;
128
129 VBOXGUESTCTRLHOSTCBCTX CtxCb = { idMessage, idContext };
130 int rc = pGuest->i_dispatchToSession(&CtxCb, pSvcCb);
131
132 Log2Func(("CID=%#x, idSession=%RU32, uObject=%RU32, uCount=%RU32, rc=%Rrc\n",
133 idContext, VBOX_GUESTCTRL_CONTEXTID_GET_SESSION(idContext), VBOX_GUESTCTRL_CONTEXTID_GET_OBJECT(idContext),
134 VBOX_GUESTCTRL_CONTEXTID_GET_COUNT(idContext), rc));
135 return rc;
136}
137
138// private methods
139/////////////////////////////////////////////////////////////////////////////
140
141/**
142 * Dispatches a host service callback to the appropriate guest control session object.
143 *
144 * @returns VBox status code.
145 * @param pCtxCb Pointer to host callback context.
146 * @param pSvcCb Pointer to callback parameters.
147 */
148int Guest::i_dispatchToSession(PVBOXGUESTCTRLHOSTCBCTX pCtxCb, PVBOXGUESTCTRLHOSTCALLBACK pSvcCb)
149{
150 LogFlowFunc(("pCtxCb=%p, pSvcCb=%p\n", pCtxCb, pSvcCb));
151
152 AssertPtrReturn(pCtxCb, VERR_INVALID_POINTER);
153 AssertPtrReturn(pSvcCb, VERR_INVALID_POINTER);
154
155 Log2Func(("uMessage=%RU32, uContextID=%RU32, uProtocol=%RU32\n", pCtxCb->uMessage, pCtxCb->uContextID, pCtxCb->uProtocol));
156
157 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
158
159 const uint32_t uSessionID = VBOX_GUESTCTRL_CONTEXTID_GET_SESSION(pCtxCb->uContextID);
160
161 Log2Func(("uSessionID=%RU32 (%zu total)\n", uSessionID, mData.mGuestSessions.size()));
162
163 GuestSessions::const_iterator itSession = mData.mGuestSessions.find(uSessionID);
164
165 int rc;
166 if (itSession != mData.mGuestSessions.end())
167 {
168 ComObjPtr<GuestSession> pSession(itSession->second);
169 Assert(!pSession.isNull());
170
171 alock.release();
172
173#ifdef DEBUG
174 /*
175 * Pre-check: If we got a status message with an error and VERR_TOO_MUCH_DATA
176 * it means that that guest could not handle the entire message
177 * because of its exceeding size. This should not happen on daily
178 * use but testcases might try this. It then makes no sense to dispatch
179 * this further because we don't have a valid context ID.
180 */
181 bool fDispatch = true;
182 rc = VERR_INVALID_FUNCTION;
183 if ( pCtxCb->uMessage == GUEST_MSG_EXEC_STATUS
184 && pSvcCb->mParms >= 5)
185 {
186 CALLBACKDATA_PROC_STATUS dataCb;
187 /* pSvcCb->mpaParms[0] always contains the context ID. */
188 HGCMSvcGetU32(&pSvcCb->mpaParms[1], &dataCb.uPID);
189 HGCMSvcGetU32(&pSvcCb->mpaParms[2], &dataCb.uStatus);
190 HGCMSvcGetU32(&pSvcCb->mpaParms[3], &dataCb.uFlags);
191 HGCMSvcGetPv(&pSvcCb->mpaParms[4], &dataCb.pvData, &dataCb.cbData);
192
193 if ( dataCb.uStatus == PROC_STS_ERROR
194 && (int32_t)dataCb.uFlags == VERR_TOO_MUCH_DATA)
195 {
196 LogFlowFunc(("Requested message with too much data, skipping dispatching ...\n"));
197 Assert(dataCb.uPID == 0);
198 fDispatch = false;
199 }
200 }
201 if (fDispatch)
202#endif
203 {
204 switch (pCtxCb->uMessage)
205 {
206 case GUEST_MSG_DISCONNECTED:
207 rc = pSession->i_dispatchToThis(pCtxCb, pSvcCb);
208 break;
209
210 /* Process stuff. */
211 case GUEST_MSG_EXEC_STATUS:
212 case GUEST_MSG_EXEC_OUTPUT:
213 case GUEST_MSG_EXEC_INPUT_STATUS:
214 case GUEST_MSG_EXEC_IO_NOTIFY:
215 rc = pSession->i_dispatchToObject(pCtxCb, pSvcCb);
216 break;
217
218 /* File stuff. */
219 case GUEST_MSG_FILE_NOTIFY:
220 rc = pSession->i_dispatchToObject(pCtxCb, pSvcCb);
221 break;
222
223 /* Session stuff. */
224 case GUEST_MSG_SESSION_NOTIFY:
225 rc = pSession->i_dispatchToThis(pCtxCb, pSvcCb);
226 break;
227
228 default:
229 rc = pSession->i_dispatchToObject(pCtxCb, pSvcCb);
230 break;
231 }
232 }
233 }
234 else
235 rc = VERR_INVALID_SESSION_ID;
236
237 LogFlowFuncLeaveRC(rc);
238 return rc;
239}
240
241/**
242 * Creates a new guest session.
243 * This will invoke VBoxService running on the guest creating a new (dedicated) guest session
244 * On older Guest Additions this call has no effect on the guest, and only the credentials will be
245 * used for starting/impersonating guest processes.
246 *
247 * @returns VBox status code.
248 * @param ssInfo Guest session startup information.
249 * @param guestCreds Guest OS (user) credentials to use on the guest for creating the session.
250 * The specified user must be able to logon to the guest and able to start new processes.
251 * @param pGuestSession Where to store the created guest session on success.
252 *
253 * @note Takes the write lock.
254 */
255int Guest::i_sessionCreate(const GuestSessionStartupInfo &ssInfo,
256 const GuestCredentials &guestCreds, ComObjPtr<GuestSession> &pGuestSession)
257{
258 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
259
260 int rc = VERR_MAX_PROCS_REACHED;
261 if (mData.mGuestSessions.size() >= VBOX_GUESTCTRL_MAX_SESSIONS)
262 return rc;
263
264 try
265 {
266 /* Create a new session ID and assign it. */
267 uint32_t uNewSessionID = VBOX_GUESTCTRL_SESSION_ID_BASE;
268 uint32_t uTries = 0;
269
270 for (;;)
271 {
272 /* Is the context ID already used? */
273 if (!i_sessionExists(uNewSessionID))
274 {
275 rc = VINF_SUCCESS;
276 break;
277 }
278 uNewSessionID++;
279 if (uNewSessionID >= VBOX_GUESTCTRL_MAX_SESSIONS)
280 uNewSessionID = VBOX_GUESTCTRL_SESSION_ID_BASE;
281
282 if (++uTries == VBOX_GUESTCTRL_MAX_SESSIONS)
283 break; /* Don't try too hard. */
284 }
285 if (RT_FAILURE(rc)) throw rc;
286
287 /* Create the session object. */
288 HRESULT hr = pGuestSession.createObject();
289 if (FAILED(hr)) throw VERR_COM_UNEXPECTED;
290
291 /** @todo Use an overloaded copy operator. Later. */
292 GuestSessionStartupInfo startupInfo;
293 startupInfo.mID = uNewSessionID; /* Assign new session ID. */
294 startupInfo.mName = ssInfo.mName;
295 startupInfo.mOpenFlags = ssInfo.mOpenFlags;
296 startupInfo.mOpenTimeoutMS = ssInfo.mOpenTimeoutMS;
297
298 GuestCredentials guestCredentials;
299 if (!guestCreds.mUser.isEmpty())
300 {
301 /** @todo Use an overloaded copy operator. Later. */
302 guestCredentials.mUser = guestCreds.mUser;
303 guestCredentials.mPassword = guestCreds.mPassword;
304 guestCredentials.mDomain = guestCreds.mDomain;
305 }
306 else
307 {
308 /* Internal (annonymous) session. */
309 startupInfo.mIsInternal = true;
310 }
311
312 rc = pGuestSession->init(this, startupInfo, guestCredentials);
313 if (RT_FAILURE(rc)) throw rc;
314
315 /*
316 * Add session object to our session map. This is necessary
317 * before calling openSession because the guest calls back
318 * with the creation result of this session.
319 */
320 mData.mGuestSessions[uNewSessionID] = pGuestSession;
321
322 alock.release(); /* Release lock before firing off event. */
323
324 ::FireGuestSessionRegisteredEvent(mEventSource, pGuestSession, true /* Registered */);
325 }
326 catch (int rc2)
327 {
328 rc = rc2;
329 }
330
331 LogFlowFuncLeaveRC(rc);
332 return rc;
333}
334
335/**
336 * Destroys a given guest session and removes it from the internal list.
337 *
338 * @returns VBox status code.
339 * @param uSessionID ID of the guest control session to destroy.
340 *
341 * @note Takes the write lock.
342 */
343int Guest::i_sessionDestroy(uint32_t uSessionID)
344{
345 LogFlowThisFuncEnter();
346
347 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
348
349 int rc = VERR_NOT_FOUND;
350
351 LogFlowThisFunc(("Destroying session (ID=%RU32) ...\n", uSessionID));
352
353 GuestSessions::iterator itSessions = mData.mGuestSessions.find(uSessionID);
354 if (itSessions == mData.mGuestSessions.end())
355 return VERR_NOT_FOUND;
356
357 /* Make sure to consume the pointer before the one of the
358 * iterator gets released. */
359 ComObjPtr<GuestSession> pSession = itSessions->second;
360
361 LogFlowThisFunc(("Removing session %RU32 (now total %ld sessions)\n",
362 uSessionID, mData.mGuestSessions.size() ? mData.mGuestSessions.size() - 1 : 0));
363
364 rc = pSession->i_onRemove();
365 mData.mGuestSessions.erase(itSessions);
366
367 alock.release(); /* Release lock before firing off event. */
368
369 ::FireGuestSessionRegisteredEvent(mEventSource, pSession, false /* Unregistered */);
370 pSession.setNull();
371
372 LogFlowFuncLeaveRC(rc);
373 return rc;
374}
375
376/**
377 * Returns whether a guest control session with a specific ID exists or not.
378 *
379 * @returns Returns \c true if the session exists, \c false if not.
380 * @param uSessionID ID to check for.
381 *
382 * @note No locking done, as inline function!
383 */
384inline bool Guest::i_sessionExists(uint32_t uSessionID)
385{
386 GuestSessions::const_iterator itSessions = mData.mGuestSessions.find(uSessionID);
387 return (itSessions == mData.mGuestSessions.end()) ? false : true;
388}
389
390#endif /* VBOX_WITH_GUEST_CONTROL */
391
392
393// implementation of public methods
394/////////////////////////////////////////////////////////////////////////////
395HRESULT Guest::createSession(const com::Utf8Str &aUser, const com::Utf8Str &aPassword, const com::Utf8Str &aDomain,
396 const com::Utf8Str &aSessionName, ComPtr<IGuestSession> &aGuestSession)
397
398{
399#ifndef VBOX_WITH_GUEST_CONTROL
400 ReturnComNotImplemented();
401#else /* VBOX_WITH_GUEST_CONTROL */
402
403 AutoCaller autoCaller(this);
404 if (FAILED(autoCaller.rc())) return autoCaller.rc();
405
406 /* Do not allow anonymous sessions (with system rights) with public API. */
407 if (RT_UNLIKELY(!aUser.length()))
408 return setError(E_INVALIDARG, tr("No user name specified"));
409
410 LogFlowFuncEnter();
411
412 GuestSessionStartupInfo startupInfo;
413 startupInfo.mName = aSessionName;
414
415 GuestCredentials guestCreds;
416 guestCreds.mUser = aUser;
417 guestCreds.mPassword = aPassword;
418 guestCreds.mDomain = aDomain;
419
420 ComObjPtr<GuestSession> pSession;
421 int vrc = i_sessionCreate(startupInfo, guestCreds, pSession);
422 if (RT_SUCCESS(vrc))
423 {
424 /* Return guest session to the caller. */
425 HRESULT hr2 = pSession.queryInterfaceTo(aGuestSession.asOutParam());
426 if (FAILED(hr2))
427 vrc = VERR_COM_OBJECT_NOT_FOUND;
428 }
429
430 if (RT_SUCCESS(vrc))
431 /* Start (fork) the session asynchronously
432 * on the guest. */
433 vrc = pSession->i_startSessionAsync();
434
435 HRESULT hr = S_OK;
436
437 if (RT_FAILURE(vrc))
438 {
439 switch (vrc)
440 {
441 case VERR_MAX_PROCS_REACHED:
442 hr = setErrorBoth(VBOX_E_MAXIMUM_REACHED, vrc, tr("Maximum number of concurrent guest sessions (%d) reached"),
443 VBOX_GUESTCTRL_MAX_SESSIONS);
444 break;
445
446 /** @todo Add more errors here. */
447
448 default:
449 hr = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Could not create guest session: %Rrc"), vrc);
450 break;
451 }
452 }
453
454 LogFlowThisFunc(("Returning rc=%Rhrc\n", hr));
455 return hr;
456#endif /* VBOX_WITH_GUEST_CONTROL */
457}
458
459HRESULT Guest::findSession(const com::Utf8Str &aSessionName, std::vector<ComPtr<IGuestSession> > &aSessions)
460{
461#ifndef VBOX_WITH_GUEST_CONTROL
462 ReturnComNotImplemented();
463#else /* VBOX_WITH_GUEST_CONTROL */
464
465 LogFlowFuncEnter();
466
467 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
468
469 Utf8Str strName(aSessionName);
470 std::list < ComObjPtr<GuestSession> > listSessions;
471
472 GuestSessions::const_iterator itSessions = mData.mGuestSessions.begin();
473 while (itSessions != mData.mGuestSessions.end())
474 {
475 if (strName.contains(itSessions->second->i_getName())) /** @todo Use a (simple) pattern match (IPRT?). */
476 listSessions.push_back(itSessions->second);
477 ++itSessions;
478 }
479
480 LogFlowFunc(("Sessions with \"%s\" = %RU32\n",
481 aSessionName.c_str(), listSessions.size()));
482
483 aSessions.resize(listSessions.size());
484 if (!listSessions.empty())
485 {
486 size_t i = 0;
487 for (std::list < ComObjPtr<GuestSession> >::const_iterator it = listSessions.begin(); it != listSessions.end(); ++it, ++i)
488 (*it).queryInterfaceTo(aSessions[i].asOutParam());
489
490 return S_OK;
491
492 }
493
494 return setErrorNoLog(VBOX_E_OBJECT_NOT_FOUND,
495 tr("Could not find sessions with name '%s'"),
496 aSessionName.c_str());
497#endif /* VBOX_WITH_GUEST_CONTROL */
498}
499
500HRESULT Guest::shutdown(const std::vector<GuestShutdownFlag_T> &aFlags)
501{
502#ifndef VBOX_WITH_GUEST_CONTROL
503 ReturnComNotImplemented();
504#else /* VBOX_WITH_GUEST_CONTROL */
505
506 /* Validate flags. */
507 uint32_t fFlags = GuestShutdownFlag_None;
508 if (aFlags.size())
509 for (size_t i = 0; i < aFlags.size(); ++i)
510 fFlags |= aFlags[i];
511
512 const uint32_t fValidFlags = GuestShutdownFlag_None
513 | GuestShutdownFlag_PowerOff | GuestShutdownFlag_Reboot | GuestShutdownFlag_Force;
514 if (fFlags & ~fValidFlags)
515 return setError(E_INVALIDARG,tr("Unknown flags: flags value %#x, invalid: %#x"), fFlags, fFlags & ~fValidFlags);
516
517 if ( (fFlags & GuestShutdownFlag_PowerOff)
518 && (fFlags & GuestShutdownFlag_Reboot))
519 return setError(E_INVALIDARG, tr("Invalid combination of flags (%#x)"), fFlags);
520
521 Utf8Str strAction = (fFlags & GuestShutdownFlag_Reboot) ? tr("Rebooting") : tr("Shutting down");
522
523 /*
524 * Create an anonymous session. This is required to run shutting down / rebooting
525 * the guest with administrative rights.
526 */
527 GuestSessionStartupInfo startupInfo;
528 startupInfo.mName = (fFlags & GuestShutdownFlag_Reboot) ? tr("Rebooting guest") : tr("Shutting down guest");
529
530 GuestCredentials guestCreds;
531
532 HRESULT hrc = S_OK;
533
534 ComObjPtr<GuestSession> pSession;
535 int vrc = i_sessionCreate(startupInfo, guestCreds, pSession);
536 if (RT_SUCCESS(vrc))
537 {
538 Assert(!pSession.isNull());
539
540 int rcGuest = VERR_GSTCTL_GUEST_ERROR;
541 vrc = pSession->i_startSession(&rcGuest);
542 if (RT_SUCCESS(vrc))
543 {
544 vrc = pSession->i_shutdown(fFlags, &rcGuest);
545 if (RT_FAILURE(vrc))
546 {
547 switch (vrc)
548 {
549 case VERR_NOT_SUPPORTED:
550 hrc = setErrorBoth(VBOX_E_NOT_SUPPORTED, vrc,
551 tr("%s not supported by installed Guest Additions"), strAction.c_str());
552 break;
553
554 default:
555 {
556 if (vrc == VERR_GSTCTL_GUEST_ERROR)
557 vrc = rcGuest;
558 hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Error %s guest: %Rrc"), strAction.c_str(), vrc);
559 break;
560 }
561 }
562 }
563 }
564 else
565 {
566 if (vrc == VERR_GSTCTL_GUEST_ERROR)
567 vrc = rcGuest;
568 hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Could not open guest session: %Rrc"), vrc);
569 }
570 }
571 else
572 {
573 switch (vrc)
574 {
575 case VERR_MAX_PROCS_REACHED:
576 hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Maximum number of concurrent guest sessions (%d) reached"),
577 VBOX_GUESTCTRL_MAX_SESSIONS);
578 break;
579
580 /** @todo Add more errors here. */
581
582 default:
583 hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Could not create guest session: %Rrc"), vrc);
584 break;
585 }
586 }
587
588 LogFlowFunc(("Returning hrc=%Rhrc\n", hrc));
589 return hrc;
590#endif /* VBOX_WITH_GUEST_CONTROL */
591}
592
593HRESULT Guest::updateGuestAdditions(const com::Utf8Str &aSource, const std::vector<com::Utf8Str> &aArguments,
594 const std::vector<AdditionsUpdateFlag_T> &aFlags, ComPtr<IProgress> &aProgress)
595{
596#ifndef VBOX_WITH_GUEST_CONTROL
597 ReturnComNotImplemented();
598#else /* VBOX_WITH_GUEST_CONTROL */
599
600 /* Validate flags. */
601 uint32_t fFlags = AdditionsUpdateFlag_None;
602 if (aFlags.size())
603 for (size_t i = 0; i < aFlags.size(); ++i)
604 fFlags |= aFlags[i];
605
606 if (fFlags && !(fFlags & AdditionsUpdateFlag_WaitForUpdateStartOnly))
607 return setError(E_INVALIDARG, tr("Unknown flags (%#x)"), fFlags);
608
609
610 /* Copy arguments into aArgs: */
611 ProcessArguments aArgs;
612 try
613 {
614 aArgs.resize(0);
615 for (size_t i = 0; i < aArguments.size(); ++i)
616 aArgs.push_back(aArguments[i]);
617 }
618 catch (std::bad_alloc &)
619 {
620 return E_OUTOFMEMORY;
621 }
622
623
624 /*
625 * Create an anonymous session. This is required to run the Guest Additions
626 * update process with administrative rights.
627 */
628 GuestSessionStartupInfo startupInfo;
629 startupInfo.mName = "Updating Guest Additions";
630
631 GuestCredentials guestCreds;
632
633 HRESULT hrc;
634 ComObjPtr<GuestSession> pSession;
635 int vrc = i_sessionCreate(startupInfo, guestCreds, pSession);
636 if (RT_SUCCESS(vrc))
637 {
638 Assert(!pSession.isNull());
639
640 int rcGuest = VERR_GSTCTL_GUEST_ERROR;
641 vrc = pSession->i_startSession(&rcGuest);
642 if (RT_SUCCESS(vrc))
643 {
644 /*
645 * Create the update task.
646 */
647 GuestSessionTaskUpdateAdditions *pTask = NULL;
648 try
649 {
650 pTask = new GuestSessionTaskUpdateAdditions(pSession /* GuestSession */, aSource, aArgs, fFlags);
651 hrc = S_OK;
652 }
653 catch (std::bad_alloc &)
654 {
655 hrc = setError(E_OUTOFMEMORY, tr("Failed to create SessionTaskUpdateAdditions object"));
656 }
657 if (SUCCEEDED(hrc))
658 {
659 try
660 {
661 hrc = pTask->Init(Utf8StrFmt(tr("Updating Guest Additions")));
662 }
663 catch (std::bad_alloc &)
664 {
665 hrc = E_OUTOFMEMORY;
666 }
667 if (SUCCEEDED(hrc))
668 {
669 ComPtr<Progress> ptrProgress = pTask->GetProgressObject();
670
671 /*
672 * Kick off the thread. Note! consumes pTask!
673 */
674 hrc = pTask->createThreadWithType(RTTHREADTYPE_MAIN_HEAVY_WORKER);
675 pTask = NULL;
676 if (SUCCEEDED(hrc))
677 hrc = ptrProgress.queryInterfaceTo(aProgress.asOutParam());
678 else
679 hrc = setError(hrc, tr("Starting thread for updating Guest Additions on the guest failed"));
680 }
681 else
682 {
683 hrc = setError(hrc, tr("Failed to initialize SessionTaskUpdateAdditions object"));
684 delete pTask;
685 }
686 }
687 }
688 else
689 {
690 if (vrc == VERR_GSTCTL_GUEST_ERROR)
691 vrc = rcGuest;
692 hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Could not open guest session: %Rrc"), vrc);
693 }
694 }
695 else
696 {
697 switch (vrc)
698 {
699 case VERR_MAX_PROCS_REACHED:
700 hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Maximum number of concurrent guest sessions (%d) reached"),
701 VBOX_GUESTCTRL_MAX_SESSIONS);
702 break;
703
704 /** @todo Add more errors here. */
705
706 default:
707 hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Could not create guest session: %Rrc"), vrc);
708 break;
709 }
710 }
711
712 LogFlowFunc(("Returning hrc=%Rhrc\n", hrc));
713 return hrc;
714#endif /* VBOX_WITH_GUEST_CONTROL */
715}
716
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use