VirtualBox

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

Last change on this file since 86506 was 85300, checked in by vboxsync, 4 years ago

Main/comimpl.xsl,++: fireXxxxEvent -> ::FireXxxxEvent. bugref:9790

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 24.1 KB
Line 
1/* $Id: GuestCtrlImpl.cpp 85300 2020-07-13 10:04:45Z vboxsync $ */
2/** @file
3 * VirtualBox COM class implementation: Guest
4 */
5
6/*
7 * Copyright (C) 2006-2020 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 * Removes a guest control session from the internal list and destroys the session.
243 *
244 * @returns VBox status code.
245 * @param uSessionID ID of the guest control session to remove.
246 */
247int Guest::i_sessionRemove(uint32_t uSessionID)
248{
249 LogFlowThisFuncEnter();
250
251 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
252
253 int rc = VERR_NOT_FOUND;
254
255 LogFlowThisFunc(("Removing session (ID=%RU32) ...\n", uSessionID));
256
257 GuestSessions::iterator itSessions = mData.mGuestSessions.find(uSessionID);
258 if (itSessions == mData.mGuestSessions.end())
259 return VERR_NOT_FOUND;
260
261 /* Make sure to consume the pointer before the one of the
262 * iterator gets released. */
263 ComObjPtr<GuestSession> pSession = itSessions->second;
264
265 LogFlowThisFunc(("Removing session %RU32 (now total %ld sessions)\n",
266 uSessionID, mData.mGuestSessions.size() ? mData.mGuestSessions.size() - 1 : 0));
267
268 rc = pSession->i_onRemove();
269 mData.mGuestSessions.erase(itSessions);
270
271 alock.release(); /* Release lock before firing off event. */
272
273 ::FireGuestSessionRegisteredEvent(mEventSource, pSession, false /* Unregistered */);
274 pSession.setNull();
275
276 LogFlowFuncLeaveRC(rc);
277 return rc;
278}
279
280/**
281 * Creates a new guest session.
282 * This will invoke VBoxService running on the guest creating a new (dedicated) guest session
283 * On older Guest Additions this call has no effect on the guest, and only the credentials will be
284 * used for starting/impersonating guest processes.
285 *
286 * @returns VBox status code.
287 * @param ssInfo Guest session startup information.
288 * @param guestCreds Guest OS (user) credentials to use on the guest for creating the session.
289 * The specified user must be able to logon to the guest and able to start new processes.
290 * @param pGuestSession Where to store the created guest session on success.
291 */
292int Guest::i_sessionCreate(const GuestSessionStartupInfo &ssInfo,
293 const GuestCredentials &guestCreds, ComObjPtr<GuestSession> &pGuestSession)
294{
295 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
296
297 int rc = VERR_MAX_PROCS_REACHED;
298 if (mData.mGuestSessions.size() >= VBOX_GUESTCTRL_MAX_SESSIONS)
299 return rc;
300
301 try
302 {
303 /* Create a new session ID and assign it. */
304 uint32_t uNewSessionID = VBOX_GUESTCTRL_SESSION_ID_BASE;
305 uint32_t uTries = 0;
306
307 for (;;)
308 {
309 /* Is the context ID already used? */
310 if (!i_sessionExists(uNewSessionID))
311 {
312 rc = VINF_SUCCESS;
313 break;
314 }
315 uNewSessionID++;
316 if (uNewSessionID >= VBOX_GUESTCTRL_MAX_SESSIONS)
317 uNewSessionID = VBOX_GUESTCTRL_SESSION_ID_BASE;
318
319 if (++uTries == VBOX_GUESTCTRL_MAX_SESSIONS)
320 break; /* Don't try too hard. */
321 }
322 if (RT_FAILURE(rc)) throw rc;
323
324 /* Create the session object. */
325 HRESULT hr = pGuestSession.createObject();
326 if (FAILED(hr)) throw VERR_COM_UNEXPECTED;
327
328 /** @todo Use an overloaded copy operator. Later. */
329 GuestSessionStartupInfo startupInfo;
330 startupInfo.mID = uNewSessionID; /* Assign new session ID. */
331 startupInfo.mName = ssInfo.mName;
332 startupInfo.mOpenFlags = ssInfo.mOpenFlags;
333 startupInfo.mOpenTimeoutMS = ssInfo.mOpenTimeoutMS;
334
335 GuestCredentials guestCredentials;
336 if (!guestCreds.mUser.isEmpty())
337 {
338 /** @todo Use an overloaded copy operator. Later. */
339 guestCredentials.mUser = guestCreds.mUser;
340 guestCredentials.mPassword = guestCreds.mPassword;
341 guestCredentials.mDomain = guestCreds.mDomain;
342 }
343 else
344 {
345 /* Internal (annonymous) session. */
346 startupInfo.mIsInternal = true;
347 }
348
349 rc = pGuestSession->init(this, startupInfo, guestCredentials);
350 if (RT_FAILURE(rc)) throw rc;
351
352 /*
353 * Add session object to our session map. This is necessary
354 * before calling openSession because the guest calls back
355 * with the creation result of this session.
356 */
357 mData.mGuestSessions[uNewSessionID] = pGuestSession;
358
359 alock.release(); /* Release lock before firing off event. */
360
361 ::FireGuestSessionRegisteredEvent(mEventSource, pGuestSession, true /* Registered */);
362 }
363 catch (int rc2)
364 {
365 rc = rc2;
366 }
367
368 LogFlowFuncLeaveRC(rc);
369 return rc;
370}
371
372/**
373 * Returns whether a guest control session with a specific ID exists or not.
374 *
375 * @returns Returns \c true if the session exists, \c false if not.
376 * @param uSessionID ID to check for.
377 */
378inline bool Guest::i_sessionExists(uint32_t uSessionID)
379{
380 GuestSessions::const_iterator itSessions = mData.mGuestSessions.find(uSessionID);
381 return (itSessions == mData.mGuestSessions.end()) ? false : true;
382}
383
384#endif /* VBOX_WITH_GUEST_CONTROL */
385
386
387// implementation of public methods
388/////////////////////////////////////////////////////////////////////////////
389HRESULT Guest::createSession(const com::Utf8Str &aUser, const com::Utf8Str &aPassword, const com::Utf8Str &aDomain,
390 const com::Utf8Str &aSessionName, ComPtr<IGuestSession> &aGuestSession)
391
392{
393#ifndef VBOX_WITH_GUEST_CONTROL
394 ReturnComNotImplemented();
395#else /* VBOX_WITH_GUEST_CONTROL */
396
397 AutoCaller autoCaller(this);
398 if (FAILED(autoCaller.rc())) return autoCaller.rc();
399
400 /* Do not allow anonymous sessions (with system rights) with public API. */
401 if (RT_UNLIKELY(!aUser.length()))
402 return setError(E_INVALIDARG, tr("No user name specified"));
403
404 LogFlowFuncEnter();
405
406 GuestSessionStartupInfo startupInfo;
407 startupInfo.mName = aSessionName;
408
409 GuestCredentials guestCreds;
410 guestCreds.mUser = aUser;
411 guestCreds.mPassword = aPassword;
412 guestCreds.mDomain = aDomain;
413
414 ComObjPtr<GuestSession> pSession;
415 int vrc = i_sessionCreate(startupInfo, guestCreds, pSession);
416 if (RT_SUCCESS(vrc))
417 {
418 /* Return guest session to the caller. */
419 HRESULT hr2 = pSession.queryInterfaceTo(aGuestSession.asOutParam());
420 if (FAILED(hr2))
421 vrc = VERR_COM_OBJECT_NOT_FOUND;
422 }
423
424 if (RT_SUCCESS(vrc))
425 /* Start (fork) the session asynchronously
426 * on the guest. */
427 vrc = pSession->i_startSessionAsync();
428
429 HRESULT hr = S_OK;
430
431 if (RT_FAILURE(vrc))
432 {
433 switch (vrc)
434 {
435 case VERR_MAX_PROCS_REACHED:
436 hr = setErrorBoth(VBOX_E_MAXIMUM_REACHED, vrc, tr("Maximum number of concurrent guest sessions (%d) reached"),
437 VBOX_GUESTCTRL_MAX_SESSIONS);
438 break;
439
440 /** @todo Add more errors here. */
441
442 default:
443 hr = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Could not create guest session: %Rrc"), vrc);
444 break;
445 }
446 }
447
448 LogFlowThisFunc(("Returning rc=%Rhrc\n", hr));
449 return hr;
450#endif /* VBOX_WITH_GUEST_CONTROL */
451}
452
453HRESULT Guest::findSession(const com::Utf8Str &aSessionName, std::vector<ComPtr<IGuestSession> > &aSessions)
454{
455#ifndef VBOX_WITH_GUEST_CONTROL
456 ReturnComNotImplemented();
457#else /* VBOX_WITH_GUEST_CONTROL */
458
459 LogFlowFuncEnter();
460
461 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
462
463 Utf8Str strName(aSessionName);
464 std::list < ComObjPtr<GuestSession> > listSessions;
465
466 GuestSessions::const_iterator itSessions = mData.mGuestSessions.begin();
467 while (itSessions != mData.mGuestSessions.end())
468 {
469 if (strName.contains(itSessions->second->i_getName())) /** @todo Use a (simple) pattern match (IPRT?). */
470 listSessions.push_back(itSessions->second);
471 ++itSessions;
472 }
473
474 LogFlowFunc(("Sessions with \"%s\" = %RU32\n",
475 aSessionName.c_str(), listSessions.size()));
476
477 aSessions.resize(listSessions.size());
478 if (!listSessions.empty())
479 {
480 size_t i = 0;
481 for (std::list < ComObjPtr<GuestSession> >::const_iterator it = listSessions.begin(); it != listSessions.end(); ++it, ++i)
482 (*it).queryInterfaceTo(aSessions[i].asOutParam());
483
484 return S_OK;
485
486 }
487
488 return setErrorNoLog(VBOX_E_OBJECT_NOT_FOUND,
489 tr("Could not find sessions with name '%s'"),
490 aSessionName.c_str());
491#endif /* VBOX_WITH_GUEST_CONTROL */
492}
493
494HRESULT Guest::shutdown(const std::vector<GuestShutdownFlag_T> &aFlags)
495{
496#ifndef VBOX_WITH_GUEST_CONTROL
497 ReturnComNotImplemented();
498#else /* VBOX_WITH_GUEST_CONTROL */
499
500 /* Validate flags. */
501 uint32_t fFlags = GuestShutdownFlag_None;
502 if (aFlags.size())
503 for (size_t i = 0; i < aFlags.size(); ++i)
504 fFlags |= aFlags[i];
505
506 const uint32_t fValidFlags = GuestShutdownFlag_None
507 | GuestShutdownFlag_PowerOff | GuestShutdownFlag_Reboot | GuestShutdownFlag_Force;
508 if (fFlags & ~fValidFlags)
509 return setError(E_INVALIDARG,tr("Unknown flags: flags value %#x, invalid: %#x"), fFlags, fFlags & ~fValidFlags);
510
511 if ( (fFlags & GuestShutdownFlag_PowerOff)
512 && (fFlags & GuestShutdownFlag_Reboot))
513 return setError(E_INVALIDARG, tr("Invalid combination of flags (%#x)"), fFlags);
514
515 Utf8Str strAction = (fFlags & GuestShutdownFlag_Reboot) ? tr("Rebooting") : tr("Shutting down");
516
517 /*
518 * Create an anonymous session. This is required to run shutting down / rebooting
519 * the guest with administrative rights.
520 */
521 GuestSessionStartupInfo startupInfo;
522 startupInfo.mName = strAction + " guest";
523
524 GuestCredentials guestCreds;
525
526 HRESULT hrc = S_OK;
527
528 ComObjPtr<GuestSession> pSession;
529 int vrc = i_sessionCreate(startupInfo, guestCreds, pSession);
530 if (RT_SUCCESS(vrc))
531 {
532 Assert(!pSession.isNull());
533
534 int rcGuest = VERR_GSTCTL_GUEST_ERROR;
535 vrc = pSession->i_startSession(&rcGuest);
536 if (RT_SUCCESS(vrc))
537 {
538 vrc = pSession->i_shutdown(fFlags, &rcGuest);
539 if (RT_FAILURE(vrc))
540 {
541 switch (vrc)
542 {
543 case VERR_NOT_SUPPORTED:
544 hrc = setErrorBoth(VBOX_E_NOT_SUPPORTED, vrc,
545 tr("%s not supported by installed Guest Additions"), strAction.c_str());
546 break;
547
548 default:
549 {
550 if (vrc == VERR_GSTCTL_GUEST_ERROR)
551 vrc = rcGuest;
552 hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Error %s guest: %Rrc"), strAction.c_str(), vrc);
553 break;
554 }
555 }
556 }
557 }
558 else
559 {
560 if (vrc == VERR_GSTCTL_GUEST_ERROR)
561 vrc = rcGuest;
562 hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Could not open guest session: %Rrc"), vrc);
563 }
564 }
565 else
566 {
567 switch (vrc)
568 {
569 case VERR_MAX_PROCS_REACHED:
570 hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Maximum number of concurrent guest sessions (%d) reached"),
571 VBOX_GUESTCTRL_MAX_SESSIONS);
572 break;
573
574 /** @todo Add more errors here. */
575
576 default:
577 hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Could not create guest session: %Rrc"), vrc);
578 break;
579 }
580 }
581
582 LogFlowFunc(("Returning hrc=%Rhrc\n", hrc));
583 return hrc;
584#endif /* VBOX_WITH_GUEST_CONTROL */
585}
586
587HRESULT Guest::updateGuestAdditions(const com::Utf8Str &aSource, const std::vector<com::Utf8Str> &aArguments,
588 const std::vector<AdditionsUpdateFlag_T> &aFlags, ComPtr<IProgress> &aProgress)
589{
590#ifndef VBOX_WITH_GUEST_CONTROL
591 ReturnComNotImplemented();
592#else /* VBOX_WITH_GUEST_CONTROL */
593
594 /* Validate flags. */
595 uint32_t fFlags = AdditionsUpdateFlag_None;
596 if (aFlags.size())
597 for (size_t i = 0; i < aFlags.size(); ++i)
598 fFlags |= aFlags[i];
599
600 if (fFlags && !(fFlags & AdditionsUpdateFlag_WaitForUpdateStartOnly))
601 return setError(E_INVALIDARG, tr("Unknown flags (%#x)"), fFlags);
602
603
604 /* Copy arguments into aArgs: */
605 ProcessArguments aArgs;
606 try
607 {
608 aArgs.resize(0);
609 for (size_t i = 0; i < aArguments.size(); ++i)
610 aArgs.push_back(aArguments[i]);
611 }
612 catch (std::bad_alloc &)
613 {
614 return E_OUTOFMEMORY;
615 }
616
617
618 /*
619 * Create an anonymous session. This is required to run the Guest Additions
620 * update process with administrative rights.
621 */
622 GuestSessionStartupInfo startupInfo;
623 startupInfo.mName = "Updating Guest Additions";
624
625 GuestCredentials guestCreds;
626
627 HRESULT hrc;
628 ComObjPtr<GuestSession> pSession;
629 int vrc = i_sessionCreate(startupInfo, guestCreds, pSession);
630 if (RT_SUCCESS(vrc))
631 {
632 Assert(!pSession.isNull());
633
634 int rcGuest = VERR_GSTCTL_GUEST_ERROR;
635 vrc = pSession->i_startSession(&rcGuest);
636 if (RT_SUCCESS(vrc))
637 {
638 /*
639 * Create the update task.
640 */
641 GuestSessionTaskUpdateAdditions *pTask = NULL;
642 try
643 {
644 pTask = new GuestSessionTaskUpdateAdditions(pSession /* GuestSession */, aSource, aArgs, fFlags);
645 hrc = S_OK;
646 }
647 catch (std::bad_alloc &)
648 {
649 hrc = setError(E_OUTOFMEMORY, tr("Failed to create SessionTaskUpdateAdditions object"));
650 }
651 if (SUCCEEDED(hrc))
652 {
653 try
654 {
655 hrc = pTask->Init(Utf8StrFmt(tr("Updating Guest Additions")));
656 }
657 catch (std::bad_alloc &)
658 {
659 hrc = E_OUTOFMEMORY;
660 }
661 if (SUCCEEDED(hrc))
662 {
663 ComPtr<Progress> ptrProgress = pTask->GetProgressObject();
664
665 /*
666 * Kick off the thread. Note! consumes pTask!
667 */
668 hrc = pTask->createThreadWithType(RTTHREADTYPE_MAIN_HEAVY_WORKER);
669 pTask = NULL;
670 if (SUCCEEDED(hrc))
671 hrc = ptrProgress.queryInterfaceTo(aProgress.asOutParam());
672 else
673 hrc = setError(hrc, tr("Starting thread for updating Guest Additions on the guest failed"));
674 }
675 else
676 {
677 hrc = setError(hrc, tr("Failed to initialize SessionTaskUpdateAdditions object"));
678 delete pTask;
679 }
680 }
681 }
682 else
683 {
684 if (vrc == VERR_GSTCTL_GUEST_ERROR)
685 vrc = rcGuest;
686 hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Could not open guest session: %Rrc"), vrc);
687 }
688 }
689 else
690 {
691 switch (vrc)
692 {
693 case VERR_MAX_PROCS_REACHED:
694 hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Maximum number of concurrent guest sessions (%d) reached"),
695 VBOX_GUESTCTRL_MAX_SESSIONS);
696 break;
697
698 /** @todo Add more errors here. */
699
700 default:
701 hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Could not create guest session: %Rrc"), vrc);
702 break;
703 }
704 }
705
706 LogFlowFunc(("Returning hrc=%Rhrc\n", hrc));
707 return hrc;
708#endif /* VBOX_WITH_GUEST_CONTROL */
709}
710
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use