VirtualBox

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

Last change on this file since 98273 was 98262, checked in by vboxsync, 23 months ago

Main: rc() -> hrc()/vrc(). bugref:10223

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette