VirtualBox

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

Last change on this file since 99416 was 99416, checked in by vboxsync, 13 months ago

Guest Control/Main: Treat reported Guest Additions versions of "0.0.0" (or 0) through VMMDev as Guest Additions not being found or not ready (yet) within GuestSession::i_determineProtocolVersion() (via vmmdevUpdateGuestInfo[2]).

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

© 2023 Oracle
ContactPrivacy policyTerms of Use