1 | /* $Id: GuestCtrlImpl.cpp 98278 2023-01-24 11:55:00Z 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 */
|
---|
83 | DECLCALLBACK(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 | */
|
---|
158 | int 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 | */
|
---|
265 | int 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 | */
|
---|
353 | int 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 | */
|
---|
394 | inline 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 | /////////////////////////////////////////////////////////////////////////////
|
---|
405 | HRESULT 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 hrc = S_OK;
|
---|
446 | if (RT_FAILURE(vrc))
|
---|
447 | {
|
---|
448 | switch (vrc)
|
---|
449 | {
|
---|
450 | case VERR_MAX_PROCS_REACHED:
|
---|
451 | hrc = setErrorBoth(VBOX_E_MAXIMUM_REACHED, vrc, tr("Maximum number of concurrent guest sessions (%d) reached"),
|
---|
452 | VBOX_GUESTCTRL_MAX_SESSIONS);
|
---|
453 | break;
|
---|
454 |
|
---|
455 | /** @todo Add more errors here. */
|
---|
456 |
|
---|
457 | default:
|
---|
458 | hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Could not create guest session: %Rrc"), vrc);
|
---|
459 | break;
|
---|
460 | }
|
---|
461 | }
|
---|
462 |
|
---|
463 | LogFlowThisFunc(("Returning hrc=%Rhrc\n", hrc));
|
---|
464 | return hrc;
|
---|
465 | #endif /* VBOX_WITH_GUEST_CONTROL */
|
---|
466 | }
|
---|
467 |
|
---|
468 | HRESULT Guest::findSession(const com::Utf8Str &aSessionName, std::vector<ComPtr<IGuestSession> > &aSessions)
|
---|
469 | {
|
---|
470 | #ifndef VBOX_WITH_GUEST_CONTROL
|
---|
471 | ReturnComNotImplemented();
|
---|
472 | #else /* VBOX_WITH_GUEST_CONTROL */
|
---|
473 |
|
---|
474 | LogFlowFuncEnter();
|
---|
475 |
|
---|
476 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
477 |
|
---|
478 | Utf8Str strName(aSessionName);
|
---|
479 | std::list < ComObjPtr<GuestSession> > listSessions;
|
---|
480 |
|
---|
481 | GuestSessions::const_iterator itSessions = mData.mGuestSessions.begin();
|
---|
482 | while (itSessions != mData.mGuestSessions.end())
|
---|
483 | {
|
---|
484 | if (strName.contains(itSessions->second->i_getName())) /** @todo Use a (simple) pattern match (IPRT?). */
|
---|
485 | listSessions.push_back(itSessions->second);
|
---|
486 | ++itSessions;
|
---|
487 | }
|
---|
488 |
|
---|
489 | LogFlowFunc(("Sessions with \"%s\" = %RU32\n",
|
---|
490 | aSessionName.c_str(), listSessions.size()));
|
---|
491 |
|
---|
492 | aSessions.resize(listSessions.size());
|
---|
493 | if (!listSessions.empty())
|
---|
494 | {
|
---|
495 | size_t i = 0;
|
---|
496 | for (std::list < ComObjPtr<GuestSession> >::const_iterator it = listSessions.begin(); it != listSessions.end(); ++it, ++i)
|
---|
497 | (*it).queryInterfaceTo(aSessions[i].asOutParam());
|
---|
498 |
|
---|
499 | return S_OK;
|
---|
500 |
|
---|
501 | }
|
---|
502 |
|
---|
503 | return setErrorNoLog(VBOX_E_OBJECT_NOT_FOUND,
|
---|
504 | tr("Could not find sessions with name '%s'"),
|
---|
505 | aSessionName.c_str());
|
---|
506 | #endif /* VBOX_WITH_GUEST_CONTROL */
|
---|
507 | }
|
---|
508 |
|
---|
509 | HRESULT Guest::shutdown(const std::vector<GuestShutdownFlag_T> &aFlags)
|
---|
510 | {
|
---|
511 | #ifndef VBOX_WITH_GUEST_CONTROL
|
---|
512 | ReturnComNotImplemented();
|
---|
513 | #else /* VBOX_WITH_GUEST_CONTROL */
|
---|
514 |
|
---|
515 | /* Validate flags. */
|
---|
516 | uint32_t fFlags = GuestShutdownFlag_None;
|
---|
517 | if (aFlags.size())
|
---|
518 | for (size_t i = 0; i < aFlags.size(); ++i)
|
---|
519 | fFlags |= aFlags[i];
|
---|
520 |
|
---|
521 | const uint32_t fValidFlags = GuestShutdownFlag_None
|
---|
522 | | GuestShutdownFlag_PowerOff | GuestShutdownFlag_Reboot | GuestShutdownFlag_Force;
|
---|
523 | if (fFlags & ~fValidFlags)
|
---|
524 | return setError(E_INVALIDARG,tr("Unknown flags: flags value %#x, invalid: %#x"), fFlags, fFlags & ~fValidFlags);
|
---|
525 |
|
---|
526 | if ( (fFlags & GuestShutdownFlag_PowerOff)
|
---|
527 | && (fFlags & GuestShutdownFlag_Reboot))
|
---|
528 | return setError(E_INVALIDARG, tr("Invalid combination of flags (%#x)"), fFlags);
|
---|
529 |
|
---|
530 | Utf8Str strAction = (fFlags & GuestShutdownFlag_Reboot) ? tr("Rebooting") : tr("Shutting down");
|
---|
531 |
|
---|
532 | /*
|
---|
533 | * Create an anonymous session. This is required to run shutting down / rebooting
|
---|
534 | * the guest with administrative rights.
|
---|
535 | */
|
---|
536 | GuestSessionStartupInfo startupInfo;
|
---|
537 | startupInfo.mName = (fFlags & GuestShutdownFlag_Reboot) ? tr("Rebooting guest") : tr("Shutting down guest");
|
---|
538 |
|
---|
539 | GuestCredentials guestCreds;
|
---|
540 |
|
---|
541 | HRESULT hrc = S_OK;
|
---|
542 |
|
---|
543 | ComObjPtr<GuestSession> pSession;
|
---|
544 | int vrc = i_sessionCreate(startupInfo, guestCreds, pSession);
|
---|
545 | if (RT_SUCCESS(vrc))
|
---|
546 | {
|
---|
547 | Assert(!pSession.isNull());
|
---|
548 |
|
---|
549 | int vrcGuest = VERR_GSTCTL_GUEST_ERROR;
|
---|
550 | vrc = pSession->i_startSession(&vrcGuest);
|
---|
551 | if (RT_SUCCESS(vrc))
|
---|
552 | {
|
---|
553 | vrc = pSession->i_shutdown(fFlags, &vrcGuest);
|
---|
554 | if (RT_FAILURE(vrc))
|
---|
555 | {
|
---|
556 | switch (vrc)
|
---|
557 | {
|
---|
558 | case VERR_NOT_SUPPORTED:
|
---|
559 | hrc = setErrorBoth(VBOX_E_NOT_SUPPORTED, vrc,
|
---|
560 | tr("%s not supported by installed Guest Additions"), strAction.c_str());
|
---|
561 | break;
|
---|
562 |
|
---|
563 | default:
|
---|
564 | {
|
---|
565 | if (vrc == VERR_GSTCTL_GUEST_ERROR)
|
---|
566 | vrc = vrcGuest;
|
---|
567 | hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Error %s guest: %Rrc"), strAction.c_str(), vrc);
|
---|
568 | break;
|
---|
569 | }
|
---|
570 | }
|
---|
571 | }
|
---|
572 | }
|
---|
573 | else
|
---|
574 | {
|
---|
575 | if (vrc == VERR_GSTCTL_GUEST_ERROR)
|
---|
576 | vrc = vrcGuest;
|
---|
577 | hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Could not open guest session: %Rrc"), vrc);
|
---|
578 | }
|
---|
579 | }
|
---|
580 | else
|
---|
581 | {
|
---|
582 | switch (vrc)
|
---|
583 | {
|
---|
584 | case VERR_MAX_PROCS_REACHED:
|
---|
585 | hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Maximum number of concurrent guest sessions (%d) reached"),
|
---|
586 | VBOX_GUESTCTRL_MAX_SESSIONS);
|
---|
587 | break;
|
---|
588 |
|
---|
589 | /** @todo Add more errors here. */
|
---|
590 |
|
---|
591 | default:
|
---|
592 | hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Could not create guest session: %Rrc"), vrc);
|
---|
593 | break;
|
---|
594 | }
|
---|
595 | }
|
---|
596 |
|
---|
597 | LogFlowFunc(("Returning hrc=%Rhrc\n", hrc));
|
---|
598 | return hrc;
|
---|
599 | #endif /* VBOX_WITH_GUEST_CONTROL */
|
---|
600 | }
|
---|
601 |
|
---|
602 | HRESULT Guest::updateGuestAdditions(const com::Utf8Str &aSource, const std::vector<com::Utf8Str> &aArguments,
|
---|
603 | const std::vector<AdditionsUpdateFlag_T> &aFlags, ComPtr<IProgress> &aProgress)
|
---|
604 | {
|
---|
605 | #ifndef VBOX_WITH_GUEST_CONTROL
|
---|
606 | ReturnComNotImplemented();
|
---|
607 | #else /* VBOX_WITH_GUEST_CONTROL */
|
---|
608 |
|
---|
609 | /* Validate flags. */
|
---|
610 | uint32_t fFlags = AdditionsUpdateFlag_None;
|
---|
611 | if (aFlags.size())
|
---|
612 | for (size_t i = 0; i < aFlags.size(); ++i)
|
---|
613 | fFlags |= aFlags[i];
|
---|
614 |
|
---|
615 | if (fFlags && !(fFlags & AdditionsUpdateFlag_WaitForUpdateStartOnly))
|
---|
616 | return setError(E_INVALIDARG, tr("Unknown flags (%#x)"), fFlags);
|
---|
617 |
|
---|
618 |
|
---|
619 | /* Copy arguments into aArgs: */
|
---|
620 | ProcessArguments aArgs;
|
---|
621 | try
|
---|
622 | {
|
---|
623 | aArgs.resize(0);
|
---|
624 | for (size_t i = 0; i < aArguments.size(); ++i)
|
---|
625 | aArgs.push_back(aArguments[i]);
|
---|
626 | }
|
---|
627 | catch (std::bad_alloc &)
|
---|
628 | {
|
---|
629 | return E_OUTOFMEMORY;
|
---|
630 | }
|
---|
631 |
|
---|
632 |
|
---|
633 | /*
|
---|
634 | * Create an anonymous session. This is required to run the Guest Additions
|
---|
635 | * update process with administrative rights.
|
---|
636 | */
|
---|
637 | GuestSessionStartupInfo startupInfo;
|
---|
638 | startupInfo.mName = "Updating Guest Additions";
|
---|
639 |
|
---|
640 | GuestCredentials guestCreds;
|
---|
641 |
|
---|
642 | HRESULT hrc;
|
---|
643 | ComObjPtr<GuestSession> pSession;
|
---|
644 | int vrc = i_sessionCreate(startupInfo, guestCreds, pSession);
|
---|
645 | if (RT_SUCCESS(vrc))
|
---|
646 | {
|
---|
647 | Assert(!pSession.isNull());
|
---|
648 |
|
---|
649 | int vrcGuest = VERR_GSTCTL_GUEST_ERROR;
|
---|
650 | vrc = pSession->i_startSession(&vrcGuest);
|
---|
651 | if (RT_SUCCESS(vrc))
|
---|
652 | {
|
---|
653 | /*
|
---|
654 | * Create the update task.
|
---|
655 | */
|
---|
656 | GuestSessionTaskUpdateAdditions *pTask = NULL;
|
---|
657 | try
|
---|
658 | {
|
---|
659 | pTask = new GuestSessionTaskUpdateAdditions(pSession /* GuestSession */, aSource, aArgs, fFlags);
|
---|
660 | hrc = S_OK;
|
---|
661 | }
|
---|
662 | catch (std::bad_alloc &)
|
---|
663 | {
|
---|
664 | hrc = setError(E_OUTOFMEMORY, tr("Failed to create SessionTaskUpdateAdditions object"));
|
---|
665 | }
|
---|
666 | if (SUCCEEDED(hrc))
|
---|
667 | {
|
---|
668 | try
|
---|
669 | {
|
---|
670 | hrc = pTask->Init(Utf8StrFmt(tr("Updating Guest Additions")));
|
---|
671 | }
|
---|
672 | catch (std::bad_alloc &)
|
---|
673 | {
|
---|
674 | hrc = E_OUTOFMEMORY;
|
---|
675 | }
|
---|
676 | if (SUCCEEDED(hrc))
|
---|
677 | {
|
---|
678 | ComPtr<Progress> ptrProgress = pTask->GetProgressObject();
|
---|
679 |
|
---|
680 | /*
|
---|
681 | * Kick off the thread. Note! consumes pTask!
|
---|
682 | */
|
---|
683 | hrc = pTask->createThreadWithType(RTTHREADTYPE_MAIN_HEAVY_WORKER);
|
---|
684 | pTask = NULL;
|
---|
685 | if (SUCCEEDED(hrc))
|
---|
686 | hrc = ptrProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
687 | else
|
---|
688 | hrc = setError(hrc, tr("Starting thread for updating Guest Additions on the guest failed"));
|
---|
689 | }
|
---|
690 | else
|
---|
691 | {
|
---|
692 | hrc = setError(hrc, tr("Failed to initialize SessionTaskUpdateAdditions object"));
|
---|
693 | delete pTask;
|
---|
694 | }
|
---|
695 | }
|
---|
696 | }
|
---|
697 | else
|
---|
698 | {
|
---|
699 | if (vrc == VERR_GSTCTL_GUEST_ERROR)
|
---|
700 | vrc = vrcGuest;
|
---|
701 | hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Could not open guest session: %Rrc"), vrc);
|
---|
702 | }
|
---|
703 | }
|
---|
704 | else
|
---|
705 | {
|
---|
706 | switch (vrc)
|
---|
707 | {
|
---|
708 | case VERR_MAX_PROCS_REACHED:
|
---|
709 | hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Maximum number of concurrent guest sessions (%d) reached"),
|
---|
710 | VBOX_GUESTCTRL_MAX_SESSIONS);
|
---|
711 | break;
|
---|
712 |
|
---|
713 | /** @todo Add more errors here. */
|
---|
714 |
|
---|
715 | default:
|
---|
716 | hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Could not create guest session: %Rrc"), vrc);
|
---|
717 | break;
|
---|
718 | }
|
---|
719 | }
|
---|
720 |
|
---|
721 | LogFlowFunc(("Returning hrc=%Rhrc\n", hrc));
|
---|
722 | return hrc;
|
---|
723 | #endif /* VBOX_WITH_GUEST_CONTROL */
|
---|
724 | }
|
---|
725 |
|
---|