1 | /* $Id: GuestCtrlImpl.cpp 83323 2020-03-19 09:40:22Z 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 */
|
---|
73 | DECLCALLBACK(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 | */
|
---|
148 | int 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 | */
|
---|
247 | int 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 | */
|
---|
292 | int 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,
|
---|
362 | true /* Registered */);
|
---|
363 | }
|
---|
364 | catch (int rc2)
|
---|
365 | {
|
---|
366 | rc = rc2;
|
---|
367 | }
|
---|
368 |
|
---|
369 | LogFlowFuncLeaveRC(rc);
|
---|
370 | return rc;
|
---|
371 | }
|
---|
372 |
|
---|
373 | /**
|
---|
374 | * Returns whether a guest control session with a specific ID exists or not.
|
---|
375 | *
|
---|
376 | * @returns Returns \c true if the session exists, \c false if not.
|
---|
377 | * @param uSessionID ID to check for.
|
---|
378 | */
|
---|
379 | inline bool Guest::i_sessionExists(uint32_t uSessionID)
|
---|
380 | {
|
---|
381 | GuestSessions::const_iterator itSessions = mData.mGuestSessions.find(uSessionID);
|
---|
382 | return (itSessions == mData.mGuestSessions.end()) ? false : true;
|
---|
383 | }
|
---|
384 |
|
---|
385 | #endif /* VBOX_WITH_GUEST_CONTROL */
|
---|
386 |
|
---|
387 |
|
---|
388 | // implementation of public methods
|
---|
389 | /////////////////////////////////////////////////////////////////////////////
|
---|
390 | HRESULT Guest::createSession(const com::Utf8Str &aUser, const com::Utf8Str &aPassword, const com::Utf8Str &aDomain,
|
---|
391 | const com::Utf8Str &aSessionName, ComPtr<IGuestSession> &aGuestSession)
|
---|
392 |
|
---|
393 | {
|
---|
394 | #ifndef VBOX_WITH_GUEST_CONTROL
|
---|
395 | ReturnComNotImplemented();
|
---|
396 | #else /* VBOX_WITH_GUEST_CONTROL */
|
---|
397 |
|
---|
398 | AutoCaller autoCaller(this);
|
---|
399 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
400 |
|
---|
401 | /* Do not allow anonymous sessions (with system rights) with public API. */
|
---|
402 | if (RT_UNLIKELY(!aUser.length()))
|
---|
403 | return setError(E_INVALIDARG, tr("No user name specified"));
|
---|
404 |
|
---|
405 | LogFlowFuncEnter();
|
---|
406 |
|
---|
407 | GuestSessionStartupInfo startupInfo;
|
---|
408 | startupInfo.mName = aSessionName;
|
---|
409 |
|
---|
410 | GuestCredentials guestCreds;
|
---|
411 | guestCreds.mUser = aUser;
|
---|
412 | guestCreds.mPassword = aPassword;
|
---|
413 | guestCreds.mDomain = aDomain;
|
---|
414 |
|
---|
415 | ComObjPtr<GuestSession> pSession;
|
---|
416 | int vrc = i_sessionCreate(startupInfo, guestCreds, pSession);
|
---|
417 | if (RT_SUCCESS(vrc))
|
---|
418 | {
|
---|
419 | /* Return guest session to the caller. */
|
---|
420 | HRESULT hr2 = pSession.queryInterfaceTo(aGuestSession.asOutParam());
|
---|
421 | if (FAILED(hr2))
|
---|
422 | vrc = VERR_COM_OBJECT_NOT_FOUND;
|
---|
423 | }
|
---|
424 |
|
---|
425 | if (RT_SUCCESS(vrc))
|
---|
426 | /* Start (fork) the session asynchronously
|
---|
427 | * on the guest. */
|
---|
428 | vrc = pSession->i_startSessionAsync();
|
---|
429 |
|
---|
430 | HRESULT hr = S_OK;
|
---|
431 |
|
---|
432 | if (RT_FAILURE(vrc))
|
---|
433 | {
|
---|
434 | switch (vrc)
|
---|
435 | {
|
---|
436 | case VERR_MAX_PROCS_REACHED:
|
---|
437 | hr = setErrorBoth(VBOX_E_MAXIMUM_REACHED, vrc, tr("Maximum number of concurrent guest sessions (%d) reached"),
|
---|
438 | VBOX_GUESTCTRL_MAX_SESSIONS);
|
---|
439 | break;
|
---|
440 |
|
---|
441 | /** @todo Add more errors here. */
|
---|
442 |
|
---|
443 | default:
|
---|
444 | hr = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Could not create guest session: %Rrc"), vrc);
|
---|
445 | break;
|
---|
446 | }
|
---|
447 | }
|
---|
448 |
|
---|
449 | LogFlowThisFunc(("Returning rc=%Rhrc\n", hr));
|
---|
450 | return hr;
|
---|
451 | #endif /* VBOX_WITH_GUEST_CONTROL */
|
---|
452 | }
|
---|
453 |
|
---|
454 | HRESULT Guest::findSession(const com::Utf8Str &aSessionName, std::vector<ComPtr<IGuestSession> > &aSessions)
|
---|
455 | {
|
---|
456 | #ifndef VBOX_WITH_GUEST_CONTROL
|
---|
457 | ReturnComNotImplemented();
|
---|
458 | #else /* VBOX_WITH_GUEST_CONTROL */
|
---|
459 |
|
---|
460 | LogFlowFuncEnter();
|
---|
461 |
|
---|
462 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
463 |
|
---|
464 | Utf8Str strName(aSessionName);
|
---|
465 | std::list < ComObjPtr<GuestSession> > listSessions;
|
---|
466 |
|
---|
467 | GuestSessions::const_iterator itSessions = mData.mGuestSessions.begin();
|
---|
468 | while (itSessions != mData.mGuestSessions.end())
|
---|
469 | {
|
---|
470 | if (strName.contains(itSessions->second->i_getName())) /** @todo Use a (simple) pattern match (IPRT?). */
|
---|
471 | listSessions.push_back(itSessions->second);
|
---|
472 | ++itSessions;
|
---|
473 | }
|
---|
474 |
|
---|
475 | LogFlowFunc(("Sessions with \"%s\" = %RU32\n",
|
---|
476 | aSessionName.c_str(), listSessions.size()));
|
---|
477 |
|
---|
478 | aSessions.resize(listSessions.size());
|
---|
479 | if (!listSessions.empty())
|
---|
480 | {
|
---|
481 | size_t i = 0;
|
---|
482 | for (std::list < ComObjPtr<GuestSession> >::const_iterator it = listSessions.begin(); it != listSessions.end(); ++it, ++i)
|
---|
483 | (*it).queryInterfaceTo(aSessions[i].asOutParam());
|
---|
484 |
|
---|
485 | return S_OK;
|
---|
486 |
|
---|
487 | }
|
---|
488 |
|
---|
489 | return setErrorNoLog(VBOX_E_OBJECT_NOT_FOUND,
|
---|
490 | tr("Could not find sessions with name '%s'"),
|
---|
491 | aSessionName.c_str());
|
---|
492 | #endif /* VBOX_WITH_GUEST_CONTROL */
|
---|
493 | }
|
---|
494 |
|
---|
495 | HRESULT Guest::updateGuestAdditions(const com::Utf8Str &aSource, const std::vector<com::Utf8Str> &aArguments,
|
---|
496 | const std::vector<AdditionsUpdateFlag_T> &aFlags, ComPtr<IProgress> &aProgress)
|
---|
497 | {
|
---|
498 | #ifndef VBOX_WITH_GUEST_CONTROL
|
---|
499 | ReturnComNotImplemented();
|
---|
500 | #else /* VBOX_WITH_GUEST_CONTROL */
|
---|
501 |
|
---|
502 | /* Validate flags. */
|
---|
503 | uint32_t fFlags = AdditionsUpdateFlag_None;
|
---|
504 | if (aFlags.size())
|
---|
505 | for (size_t i = 0; i < aFlags.size(); ++i)
|
---|
506 | fFlags |= aFlags[i];
|
---|
507 |
|
---|
508 | if (fFlags && !(fFlags & AdditionsUpdateFlag_WaitForUpdateStartOnly))
|
---|
509 | return setError(E_INVALIDARG, tr("Unknown flags (%#x)"), fFlags);
|
---|
510 |
|
---|
511 |
|
---|
512 | /* Copy arguments into aArgs: */
|
---|
513 | ProcessArguments aArgs;
|
---|
514 | try
|
---|
515 | {
|
---|
516 | aArgs.resize(0);
|
---|
517 | for (size_t i = 0; i < aArguments.size(); ++i)
|
---|
518 | aArgs.push_back(aArguments[i]);
|
---|
519 | }
|
---|
520 | catch (std::bad_alloc &)
|
---|
521 | {
|
---|
522 | return E_OUTOFMEMORY;
|
---|
523 | }
|
---|
524 |
|
---|
525 |
|
---|
526 | /*
|
---|
527 | * Create an anonymous session. This is required to run the Guest Additions
|
---|
528 | * update process with administrative rights.
|
---|
529 | */
|
---|
530 | GuestSessionStartupInfo startupInfo;
|
---|
531 | startupInfo.mName = "Updating Guest Additions";
|
---|
532 |
|
---|
533 | GuestCredentials guestCreds;
|
---|
534 |
|
---|
535 | HRESULT hrc;
|
---|
536 | ComObjPtr<GuestSession> pSession;
|
---|
537 | int vrc = i_sessionCreate(startupInfo, guestCreds, pSession);
|
---|
538 | if (RT_SUCCESS(vrc))
|
---|
539 | {
|
---|
540 | Assert(!pSession.isNull());
|
---|
541 |
|
---|
542 | int rcGuest = VERR_GSTCTL_GUEST_ERROR;
|
---|
543 | vrc = pSession->i_startSession(&rcGuest);
|
---|
544 | if (RT_SUCCESS(vrc))
|
---|
545 | {
|
---|
546 | /*
|
---|
547 | * Create the update task.
|
---|
548 | */
|
---|
549 | GuestSessionTaskUpdateAdditions *pTask = NULL;
|
---|
550 | try
|
---|
551 | {
|
---|
552 | pTask = new GuestSessionTaskUpdateAdditions(pSession /* GuestSession */, aSource, aArgs, fFlags);
|
---|
553 | hrc = S_OK;
|
---|
554 | }
|
---|
555 | catch (std::bad_alloc &)
|
---|
556 | {
|
---|
557 | hrc = setError(E_OUTOFMEMORY, tr("Failed to create SessionTaskUpdateAdditions object"));
|
---|
558 | }
|
---|
559 | if (SUCCEEDED(hrc))
|
---|
560 | {
|
---|
561 | try
|
---|
562 | {
|
---|
563 | hrc = pTask->Init(Utf8StrFmt(tr("Updating Guest Additions")));
|
---|
564 | }
|
---|
565 | catch (std::bad_alloc &)
|
---|
566 | {
|
---|
567 | hrc = E_OUTOFMEMORY;
|
---|
568 | }
|
---|
569 | if (SUCCEEDED(hrc))
|
---|
570 | {
|
---|
571 | ComPtr<Progress> ptrProgress = pTask->GetProgressObject();
|
---|
572 |
|
---|
573 | /*
|
---|
574 | * Kick off the thread. Note! consumes pTask!
|
---|
575 | */
|
---|
576 | hrc = pTask->createThreadWithType(RTTHREADTYPE_MAIN_HEAVY_WORKER);
|
---|
577 | pTask = NULL;
|
---|
578 | if (SUCCEEDED(hrc))
|
---|
579 | hrc = ptrProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
580 | else
|
---|
581 | hrc = setError(hrc, tr("Starting thread for updating Guest Additions on the guest failed"));
|
---|
582 | }
|
---|
583 | else
|
---|
584 | {
|
---|
585 | hrc = setError(hrc, tr("Failed to initialize SessionTaskUpdateAdditions object"));
|
---|
586 | delete pTask;
|
---|
587 | }
|
---|
588 | }
|
---|
589 | }
|
---|
590 | else
|
---|
591 | {
|
---|
592 | if (vrc == VERR_GSTCTL_GUEST_ERROR)
|
---|
593 | vrc = rcGuest;
|
---|
594 | hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Could not open guest session: %Rrc"), vrc);
|
---|
595 | }
|
---|
596 | }
|
---|
597 | else
|
---|
598 | {
|
---|
599 | switch (vrc)
|
---|
600 | {
|
---|
601 | case VERR_MAX_PROCS_REACHED:
|
---|
602 | hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Maximum number of concurrent guest sessions (%d) reached"),
|
---|
603 | VBOX_GUESTCTRL_MAX_SESSIONS);
|
---|
604 | break;
|
---|
605 |
|
---|
606 | /** @todo Add more errors here. */
|
---|
607 |
|
---|
608 | default:
|
---|
609 | hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Could not create guest session: %Rrc"), vrc);
|
---|
610 | break;
|
---|
611 | }
|
---|
612 | }
|
---|
613 |
|
---|
614 | LogFlowFunc(("Returning hrc=%Rhrc\n", hrc));
|
---|
615 | return hrc;
|
---|
616 | #endif /* VBOX_WITH_GUEST_CONTROL */
|
---|
617 | }
|
---|
618 |
|
---|