VirtualBox

source: vbox/trunk/src/VBox/Main/src-client/GuestSessionImpl.cpp@ 45078

Last change on this file since 45078 was 45078, checked in by vboxsync, 12 years ago

VBox/err.h + Main/GuestCtrl: Introduced own error range VERR_GSTCTL_*, solves a lot of @todos using VERR_GENERAL_FAILURE before.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 84.4 KB
Line 
1
2/* $Id: GuestSessionImpl.cpp 45078 2013-03-18 21:09:20Z vboxsync $ */
3/** @file
4 * VirtualBox Main - Guest session handling.
5 */
6
7/*
8 * Copyright (C) 2012-2013 Oracle Corporation
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 */
18
19
20/*******************************************************************************
21* Header Files *
22*******************************************************************************/
23#include "GuestImpl.h"
24#include "GuestSessionImpl.h"
25#include "GuestCtrlImplPrivate.h"
26
27#include "Global.h"
28#include "AutoCaller.h"
29#include "ProgressImpl.h"
30#include "VMMDev.h"
31
32#include <memory> /* For auto_ptr. */
33
34#include <iprt/env.h>
35#include <iprt/file.h> /* For CopyTo/From. */
36
37#include <VBox/com/array.h>
38#include <VBox/version.h>
39
40#ifdef LOG_GROUP
41 #undef LOG_GROUP
42#endif
43#define LOG_GROUP LOG_GROUP_GUEST_CONTROL
44#include <VBox/log.h>
45
46
47/**
48 * Base class representing an internal
49 * asynchronous session task.
50 */
51class GuestSessionTaskInternal
52{
53public:
54
55 GuestSessionTaskInternal(GuestSession *pSession)
56 : mSession(pSession),
57 mRC(VINF_SUCCESS) { }
58
59 virtual ~GuestSessionTaskInternal(void) { }
60
61 int rc(void) const { return mRC; }
62 bool isOk(void) const { return RT_SUCCESS(mRC); }
63 const ComObjPtr<GuestSession> &Session(void) const { return mSession; }
64
65protected:
66
67 const ComObjPtr<GuestSession> mSession;
68 int mRC;
69};
70
71/**
72 * Class for asynchronously opening a guest session.
73 */
74class GuestSessionTaskInternalOpen : public GuestSessionTaskInternal
75{
76public:
77
78 GuestSessionTaskInternalOpen(GuestSession *pSession)
79 : GuestSessionTaskInternal(pSession) { }
80};
81
82// constructor / destructor
83/////////////////////////////////////////////////////////////////////////////
84
85DEFINE_EMPTY_CTOR_DTOR(GuestSession)
86
87HRESULT GuestSession::FinalConstruct(void)
88{
89 LogFlowThisFunc(("\n"));
90
91 mData.mRC = VINF_SUCCESS;
92 mData.mStatus = GuestSessionStatus_Undefined;
93
94 mData.mWaitCount = 0;
95 mData.mWaitEvent = NULL;
96
97 return BaseFinalConstruct();
98}
99
100void GuestSession::FinalRelease(void)
101{
102 LogFlowThisFuncEnter();
103 uninit();
104 BaseFinalRelease();
105 LogFlowThisFuncLeave();
106}
107
108// public initializer/uninitializer for internal purposes only
109/////////////////////////////////////////////////////////////////////////////
110
111/**
112 * Initializes a guest session but does *not* open in on the guest side
113 * yet. This needs to be done via the openSession() / openSessionAsync calls.
114 *
115 * @return IPRT status code.
116 ** @todo Docs!
117 */
118int GuestSession::init(Guest *pGuest, const GuestSessionStartupInfo &ssInfo,
119 const GuestCredentials &guestCreds)
120{
121 LogFlowThisFunc(("pGuest=%p, ssInfo=%p, guestCreds=%p\n",
122 pGuest, &ssInfo, &guestCreds));
123
124 AssertPtrReturn(pGuest, VERR_INVALID_POINTER);
125
126 /* Enclose the state transition NotReady->InInit->Ready. */
127 AutoInitSpan autoInitSpan(this);
128 AssertReturn(autoInitSpan.isOk(), VERR_OBJECT_DESTROYED);
129
130 mData.mParent = pGuest;
131
132 /* Copy over startup info. */
133 /** @todo Use an overloaded copy operator. Later. */
134 mData.mSession.mID = ssInfo.mID;
135 mData.mSession.mIsInternal = ssInfo.mIsInternal;
136 mData.mSession.mName = ssInfo.mName;
137 mData.mSession.mOpenFlags = ssInfo.mOpenFlags;
138 mData.mSession.mOpenTimeoutMS = ssInfo.mOpenTimeoutMS;
139
140 /** @todo Use an overloaded copy operator. Later. */
141 mData.mCredentials.mUser = guestCreds.mUser;
142 mData.mCredentials.mPassword = guestCreds.mPassword;
143 mData.mCredentials.mDomain = guestCreds.mDomain;
144
145 mData.mStatus = GuestSessionStatus_Undefined;
146 mData.mNumObjects = 0;
147
148 int rc = queryInfo();
149 if (RT_SUCCESS(rc))
150 {
151 /* Confirm a successful initialization when it's the case. */
152 autoInitSpan.setSucceeded();
153 }
154 else
155 autoInitSpan.setFailed();
156
157 LogFlowFuncLeaveRC(rc);
158 return rc;
159}
160
161/**
162 * Uninitializes the instance.
163 * Called from FinalRelease().
164 */
165void GuestSession::uninit(void)
166{
167 LogFlowThisFuncEnter();
168
169 /* Enclose the state transition Ready->InUninit->NotReady. */
170 AutoUninitSpan autoUninitSpan(this);
171 if (autoUninitSpan.uninitDone())
172 return;
173
174 int rc = VINF_SUCCESS;
175
176#ifndef VBOX_WITH_GUEST_CONTROL
177 LogFlowThisFunc(("Closing directories (%RU64 total)\n",
178 mData.mDirectories.size()));
179 for (SessionDirectories::iterator itDirs = mData.mDirectories.begin();
180 itDirs != mData.mDirectories.end(); ++itDirs)
181 {
182#ifdef DEBUG
183 ULONG cRefs = (*itDirs)->AddRef();
184 LogFlowThisFunc(("pFile=%p, cRefs=%RU32\n", (*itDirs), cRefs));
185 (*itDirs)->Release();
186#endif
187 (*itDirs)->uninit();
188 }
189 mData.mDirectories.clear();
190
191 LogFlowThisFunc(("Closing files (%RU64 total)\n",
192 mData.mFiles.size()));
193 for (SessionFiles::iterator itFiles = mData.mFiles.begin();
194 itFiles != mData.mFiles.end(); ++itFiles)
195 {
196#ifdef DEBUG
197 ULONG cRefs = (*itFiles)->AddRef();
198 LogFlowThisFunc(("pFile=%p, cRefs=%RU32\n", (*itFiles), cRefs));
199 (*itFiles)->Release();
200#endif
201 (*itFiles)->uninit();
202 }
203 mData.mFiles.clear();
204
205 LogFlowThisFunc(("Closing processes (%RU64 total)\n",
206 mData.mProcesses.size()));
207 for (SessionProcesses::iterator itProcs = mData.mProcesses.begin();
208 itProcs != mData.mProcesses.end(); ++itProcs)
209 {
210#ifdef DEBUG
211 ULONG cRefs = itProcs->second->AddRef();
212 LogFlowThisFunc(("pProcess=%p, cRefs=%RU32\n", itProcs->second, cRefs));
213 itProcs->second->Release();
214#endif
215 itProcs->second->uninit();
216 }
217 mData.mProcesses.clear();
218
219 LogFlowThisFunc(("mNumObjects=%RU32\n", mData.mNumObjects));
220#endif
221 LogFlowFuncLeaveRC(rc);
222}
223
224// implementation of public getters/setters for attributes
225/////////////////////////////////////////////////////////////////////////////
226
227STDMETHODIMP GuestSession::COMGETTER(User)(BSTR *aUser)
228{
229#ifndef VBOX_WITH_GUEST_CONTROL
230 ReturnComNotImplemented();
231#else
232 LogFlowThisFuncEnter();
233
234 CheckComArgOutPointerValid(aUser);
235
236 AutoCaller autoCaller(this);
237 if (FAILED(autoCaller.rc())) return autoCaller.rc();
238
239 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
240
241 mData.mCredentials.mUser.cloneTo(aUser);
242
243 LogFlowFuncLeaveRC(S_OK);
244 return S_OK;
245#endif /* VBOX_WITH_GUEST_CONTROL */
246}
247
248STDMETHODIMP GuestSession::COMGETTER(Domain)(BSTR *aDomain)
249{
250#ifndef VBOX_WITH_GUEST_CONTROL
251 ReturnComNotImplemented();
252#else
253 LogFlowThisFuncEnter();
254
255 CheckComArgOutPointerValid(aDomain);
256
257 AutoCaller autoCaller(this);
258 if (FAILED(autoCaller.rc())) return autoCaller.rc();
259
260 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
261
262 mData.mCredentials.mDomain.cloneTo(aDomain);
263
264 LogFlowFuncLeaveRC(S_OK);
265 return S_OK;
266#endif /* VBOX_WITH_GUEST_CONTROL */
267}
268
269STDMETHODIMP GuestSession::COMGETTER(Name)(BSTR *aName)
270{
271#ifndef VBOX_WITH_GUEST_CONTROL
272 ReturnComNotImplemented();
273#else
274 LogFlowThisFuncEnter();
275
276 CheckComArgOutPointerValid(aName);
277
278 AutoCaller autoCaller(this);
279 if (FAILED(autoCaller.rc())) return autoCaller.rc();
280
281 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
282
283 mData.mSession.mName.cloneTo(aName);
284
285 LogFlowFuncLeaveRC(S_OK);
286 return S_OK;
287#endif /* VBOX_WITH_GUEST_CONTROL */
288}
289
290STDMETHODIMP GuestSession::COMGETTER(Id)(ULONG *aId)
291{
292#ifndef VBOX_WITH_GUEST_CONTROL
293 ReturnComNotImplemented();
294#else
295 LogFlowThisFuncEnter();
296
297 CheckComArgOutPointerValid(aId);
298
299 AutoCaller autoCaller(this);
300 if (FAILED(autoCaller.rc())) return autoCaller.rc();
301
302 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
303
304 *aId = mData.mSession.mID;
305
306 LogFlowFuncLeaveRC(S_OK);
307 return S_OK;
308#endif /* VBOX_WITH_GUEST_CONTROL */
309}
310
311STDMETHODIMP GuestSession::COMGETTER(Status)(GuestSessionStatus_T *aStatus)
312{
313#ifndef VBOX_WITH_GUEST_CONTROL
314 ReturnComNotImplemented();
315#else
316 LogFlowThisFuncEnter();
317
318 CheckComArgOutPointerValid(aStatus);
319
320 AutoCaller autoCaller(this);
321 if (FAILED(autoCaller.rc())) return autoCaller.rc();
322
323 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
324
325 *aStatus = mData.mStatus;
326
327 LogFlowFuncLeaveRC(S_OK);
328 return S_OK;
329#endif /* VBOX_WITH_GUEST_CONTROL */
330}
331
332STDMETHODIMP GuestSession::COMGETTER(Timeout)(ULONG *aTimeout)
333{
334#ifndef VBOX_WITH_GUEST_CONTROL
335 ReturnComNotImplemented();
336#else
337 LogFlowThisFuncEnter();
338
339 CheckComArgOutPointerValid(aTimeout);
340
341 AutoCaller autoCaller(this);
342 if (FAILED(autoCaller.rc())) return autoCaller.rc();
343
344 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
345
346 *aTimeout = mData.mTimeout;
347
348 LogFlowFuncLeaveRC(S_OK);
349 return S_OK;
350#endif /* VBOX_WITH_GUEST_CONTROL */
351}
352
353STDMETHODIMP GuestSession::COMSETTER(Timeout)(ULONG aTimeout)
354{
355#ifndef VBOX_WITH_GUEST_CONTROL
356 ReturnComNotImplemented();
357#else
358 LogFlowThisFuncEnter();
359
360 AutoCaller autoCaller(this);
361 if (FAILED(autoCaller.rc())) return autoCaller.rc();
362
363 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
364
365 mData.mTimeout = aTimeout;
366
367 LogFlowFuncLeaveRC(S_OK);
368 return S_OK;
369#endif /* VBOX_WITH_GUEST_CONTROL */
370}
371
372STDMETHODIMP GuestSession::COMGETTER(Environment)(ComSafeArrayOut(BSTR, aEnvironment))
373{
374#ifndef VBOX_WITH_GUEST_CONTROL
375 ReturnComNotImplemented();
376#else
377 LogFlowThisFuncEnter();
378
379 CheckComArgOutSafeArrayPointerValid(aEnvironment);
380
381 AutoCaller autoCaller(this);
382 if (FAILED(autoCaller.rc())) return autoCaller.rc();
383
384 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
385
386 size_t cEnvVars = mData.mEnvironment.Size();
387 LogFlowThisFunc(("[%s]: cEnvVars=%RU32\n",
388 mData.mSession.mName.c_str(), cEnvVars));
389 com::SafeArray<BSTR> environment(cEnvVars);
390
391 for (size_t i = 0; i < cEnvVars; i++)
392 {
393 Bstr strEnv(mData.mEnvironment.Get(i));
394 strEnv.cloneTo(&environment[i]);
395 }
396 environment.detachTo(ComSafeArrayOutArg(aEnvironment));
397
398 LogFlowFuncLeaveRC(S_OK);
399 return S_OK;
400#endif /* VBOX_WITH_GUEST_CONTROL */
401}
402
403STDMETHODIMP GuestSession::COMSETTER(Environment)(ComSafeArrayIn(IN_BSTR, aValues))
404{
405#ifndef VBOX_WITH_GUEST_CONTROL
406 ReturnComNotImplemented();
407#else
408 LogFlowThisFuncEnter();
409
410 AutoCaller autoCaller(this);
411 if (FAILED(autoCaller.rc())) return autoCaller.rc();
412
413 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
414
415 com::SafeArray<IN_BSTR> environment(ComSafeArrayInArg(aValues));
416
417 int rc = VINF_SUCCESS;
418 for (size_t i = 0; i < environment.size() && RT_SUCCESS(rc); i++)
419 {
420 Utf8Str strEnv(environment[i]);
421 if (!strEnv.isEmpty()) /* Silently skip empty entries. */
422 rc = mData.mEnvironment.Set(strEnv);
423 }
424
425 HRESULT hr = RT_SUCCESS(rc) ? S_OK : VBOX_E_IPRT_ERROR;
426 LogFlowFuncLeaveRC(hr);
427 return hr;
428#endif /* VBOX_WITH_GUEST_CONTROL */
429}
430
431STDMETHODIMP GuestSession::COMGETTER(Processes)(ComSafeArrayOut(IGuestProcess *, aProcesses))
432{
433#ifndef VBOX_WITH_GUEST_CONTROL
434 ReturnComNotImplemented();
435#else
436 LogFlowThisFuncEnter();
437
438 CheckComArgOutSafeArrayPointerValid(aProcesses);
439
440 AutoCaller autoCaller(this);
441 if (FAILED(autoCaller.rc())) return autoCaller.rc();
442
443 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
444
445 SafeIfaceArray<IGuestProcess> collection(mData.mProcesses);
446 collection.detachTo(ComSafeArrayOutArg(aProcesses));
447
448 LogFlowFuncLeaveRC(S_OK);
449 return S_OK;
450#endif /* VBOX_WITH_GUEST_CONTROL */
451}
452
453STDMETHODIMP GuestSession::COMGETTER(Directories)(ComSafeArrayOut(IGuestDirectory *, aDirectories))
454{
455#ifndef VBOX_WITH_GUEST_CONTROL
456 ReturnComNotImplemented();
457#else
458 LogFlowThisFuncEnter();
459
460 CheckComArgOutSafeArrayPointerValid(aDirectories);
461
462 AutoCaller autoCaller(this);
463 if (FAILED(autoCaller.rc())) return autoCaller.rc();
464
465 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
466
467 SafeIfaceArray<IGuestDirectory> collection(mData.mDirectories);
468 collection.detachTo(ComSafeArrayOutArg(aDirectories));
469
470 LogFlowFuncLeaveRC(S_OK);
471 return S_OK;
472#endif /* VBOX_WITH_GUEST_CONTROL */
473}
474
475STDMETHODIMP GuestSession::COMGETTER(Files)(ComSafeArrayOut(IGuestFile *, aFiles))
476{
477#ifndef VBOX_WITH_GUEST_CONTROL
478 ReturnComNotImplemented();
479#else
480 LogFlowThisFuncEnter();
481
482 CheckComArgOutSafeArrayPointerValid(aFiles);
483
484 AutoCaller autoCaller(this);
485 if (FAILED(autoCaller.rc())) return autoCaller.rc();
486
487 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
488
489 SafeIfaceArray<IGuestFile> collection(mData.mFiles);
490 collection.detachTo(ComSafeArrayOutArg(aFiles));
491
492 LogFlowFuncLeaveRC(S_OK);
493 return S_OK;
494#endif /* VBOX_WITH_GUEST_CONTROL */
495}
496
497// private methods
498///////////////////////////////////////////////////////////////////////////////
499
500int GuestSession::closeSession(uint32_t uFlags, uint32_t uTimeoutMS, int *pGuestRc)
501{
502 LogFlowThisFunc(("uFlags=%x, uTimeoutMS=%RU32\n", uFlags, uTimeoutMS));
503
504 /* Legacy Guest Additions don't support opening dedicated
505 guest sessions. */
506 if (mData.mProtocolVersion < 2)
507 {
508 LogFlowThisFunc(("Installed Guest Additions don't support closing dedicated sessions, skipping\n"));
509 return VINF_SUCCESS;
510 }
511
512 /** @todo uFlags validation. */
513
514 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
515
516 /* Destroy a pending callback request. */
517 mData.mCallback.Destroy();
518
519 int vrc = mData.mCallback.Init(CALLBACKTYPE_SESSION_NOTIFY);
520
521 alock.release(); /* Drop the write lock again. */
522
523 if (RT_SUCCESS(vrc))
524 {
525 /* The context ID only contains this session's ID; all other
526 * parameters like object and the count itself are not needed
527 * and therefore 0. */
528 uint32_t uContextID = VBOX_GUESTCTRL_CONTEXTID_MAKE(mData.mSession.mID /* Session ID */,
529 0 /* Object */, 0 /* Count */);
530
531 VBOXHGCMSVCPARM paParms[4];
532
533 int i = 0;
534 paParms[i++].setUInt32(uContextID);
535 paParms[i++].setUInt32(uFlags);
536
537 vrc = sendCommand(HOST_SESSION_CLOSE, i, paParms);
538 }
539
540 if (RT_SUCCESS(vrc))
541 {
542 /*
543 * Let's wait for the process being started.
544 * Note: Be sure not keeping a AutoRead/WriteLock here.
545 */
546 LogFlowThisFunc(("Waiting for callback (%RU32ms) ...\n", uTimeoutMS));
547 vrc = mData.mCallback.Wait(uTimeoutMS);
548 if (RT_SUCCESS(vrc)) /* Wait was successful, check for supplied information. */
549 {
550 int guestRc = mData.mCallback.GetResultCode();
551 if (RT_SUCCESS(guestRc))
552 {
553 /* Nothing to do here right now. */
554 }
555 else
556 vrc = VERR_GSTCTL_GUEST_ERROR;
557
558 if (pGuestRc)
559 *pGuestRc = guestRc;
560 LogFlowThisFunc(("Callback returned rc=%Rrc\n", guestRc));
561 }
562 }
563
564 AutoWriteLock awlock(this COMMA_LOCKVAL_SRC_POS);
565
566 mData.mCallback.Destroy();
567
568 LogFlowFuncLeaveRC(vrc);
569 return vrc;
570}
571
572int GuestSession::directoryRemoveFromList(GuestDirectory *pDirectory)
573{
574 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
575
576 for (SessionDirectories::iterator itDirs = mData.mDirectories.begin();
577 itDirs != mData.mDirectories.end(); ++itDirs)
578 {
579 if (pDirectory == (*itDirs))
580 {
581 Bstr strName;
582 HRESULT hr = (*itDirs)->COMGETTER(DirectoryName)(strName.asOutParam());
583 ComAssertComRC(hr);
584
585 Assert(mData.mDirectories.size());
586 LogFlowFunc(("Removing directory \"%s\" (Session: %RU32) (now total %ld directories)\n",
587 Utf8Str(strName).c_str(), mData.mSession.mID, mData.mDirectories.size() - 1));
588
589 mData.mDirectories.erase(itDirs);
590 return VINF_SUCCESS;
591 }
592 }
593
594 return VERR_NOT_FOUND;
595}
596
597int GuestSession::directoryCreateInternal(const Utf8Str &strPath, uint32_t uMode, uint32_t uFlags, int *pGuestRc)
598{
599 /* pGuestRc is optional. */
600
601 LogFlowThisFunc(("strPath=%s, uMode=%x, uFlags=%x\n",
602 strPath.c_str(), uMode, uFlags));
603
604 GuestProcessStartupInfo procInfo;
605 procInfo.mCommand = Utf8Str(VBOXSERVICE_TOOL_MKDIR);
606 procInfo.mFlags = ProcessCreateFlag_Hidden;
607
608 int vrc = VINF_SUCCESS;
609
610 /* Construct arguments. */
611 if (uFlags & DirectoryCreateFlag_Parents)
612 procInfo.mArguments.push_back(Utf8Str("--parents")); /* We also want to create the parent directories. */
613 if (uMode)
614 {
615 procInfo.mArguments.push_back(Utf8Str("--mode")); /* Set the creation mode. */
616
617 char szMode[16];
618 if (RTStrPrintf(szMode, sizeof(szMode), "%o", uMode))
619 {
620 procInfo.mArguments.push_back(Utf8Str(szMode));
621 }
622 else
623 vrc = VERR_BUFFER_OVERFLOW;
624 }
625 procInfo.mArguments.push_back(strPath); /* The directory we want to create. */
626
627 if (RT_SUCCESS(vrc))
628 {
629 int guestRc;
630 GuestProcessTool procTool;
631 vrc = procTool.Init(this, procInfo, false /* Async */, &guestRc);
632 if (RT_SUCCESS(vrc))
633 {
634 if (RT_SUCCESS(guestRc))
635 vrc = procTool.Wait(GUESTPROCESSTOOL_FLAG_NONE, &guestRc);
636 }
637
638 if (RT_SUCCESS(vrc))
639 {
640 if (RT_SUCCESS(guestRc))
641 guestRc = procTool.TerminatedOk(NULL /* Exit code */);
642 }
643
644 if ( vrc == VERR_GSTCTL_GUEST_ERROR
645 && pGuestRc)
646 {
647 *pGuestRc = guestRc;
648 }
649 }
650
651 LogFlowFuncLeaveRC(vrc);
652 return vrc;
653}
654
655int GuestSession::directoryQueryInfoInternal(const Utf8Str &strPath, GuestFsObjData &objData, int *pGuestRc)
656{
657 LogFlowThisFunc(("strPath=%s\n", strPath.c_str()));
658
659 int vrc = fsQueryInfoInternal(strPath, objData, pGuestRc);
660 if (RT_SUCCESS(vrc))
661 {
662 vrc = objData.mType == FsObjType_Directory
663 ? VINF_SUCCESS : VERR_NOT_A_DIRECTORY;
664 }
665
666 LogFlowFuncLeaveRC(vrc);
667 return vrc;
668}
669
670int GuestSession::objectCreateTempInternal(const Utf8Str &strTemplate, const Utf8Str &strPath,
671 bool fDirectory, const Utf8Str &strName, int *pGuestRc)
672{
673 GuestProcessStartupInfo procInfo;
674 procInfo.mCommand = Utf8Str(VBOXSERVICE_TOOL_MKTEMP);
675 procInfo.mFlags = ProcessCreateFlag_WaitForStdOut;
676 procInfo.mArguments.push_back(Utf8Str("--machinereadable"));
677 if (fDirectory)
678 procInfo.mArguments.push_back(Utf8Str("-d"));
679 if (strPath.length()) /* Otherwise use /tmp or equivalent. */
680 {
681 procInfo.mArguments.push_back(Utf8Str("-t"));
682 procInfo.mArguments.push_back(strPath);
683 }
684 procInfo.mArguments.push_back(strTemplate);
685
686 GuestProcessTool procTool; int guestRc;
687 int vrc = procTool.Init(this, procInfo, false /* Async */, &guestRc);
688 if (RT_SUCCESS(vrc))
689 {
690 if (RT_SUCCESS(guestRc))
691 vrc = procTool.Wait(GUESTPROCESSTOOL_FLAG_NONE, &guestRc);
692 }
693
694 if (RT_SUCCESS(vrc))
695 {
696 if (RT_SUCCESS(guestRc))
697 guestRc = procTool.TerminatedOk(NULL /* Exit code */);
698 }
699
700 if ( vrc == VERR_GSTCTL_GUEST_ERROR
701 && pGuestRc)
702 {
703 *pGuestRc = guestRc;
704 }
705
706 LogFlowFuncLeaveRC(vrc);
707 return vrc;
708}
709
710int GuestSession::directoryOpenInternal(const Utf8Str &strPath, const Utf8Str &strFilter,
711 uint32_t uFlags, ComObjPtr<GuestDirectory> &pDirectory)
712{
713 LogFlowThisFunc(("strPath=%s, strPath=%s, uFlags=%x\n",
714 strPath.c_str(), strFilter.c_str(), uFlags));
715
716 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
717
718 /* Create the directory object. */
719 HRESULT hr = pDirectory.createObject();
720 if (FAILED(hr))
721 return VERR_COM_UNEXPECTED;
722
723 int vrc = pDirectory->init(this /* Parent */,
724 strPath, strFilter, uFlags);
725 if (RT_FAILURE(vrc))
726 return vrc;
727
728 /* Add the created directory to our vector. */
729 mData.mDirectories.push_back(pDirectory);
730
731 LogFlowFunc(("Added new directory \"%s\" (Session: %RU32)\n",
732 strPath.c_str(), mData.mSession.mID));
733
734 LogFlowFuncLeaveRC(vrc);
735 return vrc;
736}
737
738int GuestSession::dispatchToFile(PVBOXGUESTCTRLHOSTCBCTX pCtxCb, PVBOXGUESTCTRLHOSTCALLBACK pSvcCb)
739{
740 LogFlowFunc(("pCtxCb=%p, pSvcCb=%p\n", pCtxCb, pSvcCb));
741
742 AssertPtrReturn(pCtxCb, VERR_INVALID_POINTER);
743 AssertPtrReturn(pSvcCb, VERR_INVALID_POINTER);
744
745 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
746
747 uint32_t uFileID = VBOX_GUESTCTRL_CONTEXTID_GET_OBJECT(pCtxCb->uContextID);
748#ifdef DEBUG
749 LogFlowFunc(("uFileID=%RU32 (%RU32 total)\n",
750 uFileID, mData.mFiles.size()));
751#endif
752 int rc;
753 SessionFiles::const_iterator itFile
754 = mData.mFiles.find(uFileID);
755 if (itFile != mData.mFiles.end())
756 {
757 ComObjPtr<GuestFile> pFile(itFile->second);
758 Assert(!pFile.isNull());
759
760 alock.release();
761
762 rc = pFile->callbackDispatcher(pCtxCb, pSvcCb);
763 }
764 else
765 rc = VERR_NOT_FOUND;
766
767 LogFlowFuncLeaveRC(rc);
768 return rc;
769}
770
771int GuestSession::dispatchToProcess(PVBOXGUESTCTRLHOSTCBCTX pCtxCb, PVBOXGUESTCTRLHOSTCALLBACK pSvcCb)
772{
773 LogFlowFunc(("pCtxCb=%p, pSvcCb=%p\n", pCtxCb, pSvcCb));
774
775 AssertPtrReturn(pCtxCb, VERR_INVALID_POINTER);
776 AssertPtrReturn(pSvcCb, VERR_INVALID_POINTER);
777
778 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
779
780 uint32_t uProcessID = VBOX_GUESTCTRL_CONTEXTID_GET_OBJECT(pCtxCb->uContextID);
781#ifdef DEBUG
782 LogFlowFunc(("uProcessID=%RU32 (%RU32 total)\n",
783 uProcessID, mData.mProcesses.size()));
784#endif
785 int rc;
786 SessionProcesses::const_iterator itProc
787 = mData.mProcesses.find(uProcessID);
788 if (itProc != mData.mProcesses.end())
789 {
790 ComObjPtr<GuestProcess> pProcess(itProc->second);
791 Assert(!pProcess.isNull());
792
793 /* Set protocol version so that pSvcCb can
794 * be interpreted right. */
795 pCtxCb->uProtocol = mData.mProtocolVersion;
796
797 alock.release();
798 rc = pProcess->callbackDispatcher(pCtxCb, pSvcCb);
799 }
800 else
801 rc = VERR_NOT_FOUND;
802
803 LogFlowFuncLeaveRC(rc);
804 return rc;
805}
806
807int GuestSession::dispatchToThis(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCb)
808{
809 AssertPtrReturn(pCbCtx, VERR_INVALID_POINTER);
810 AssertPtrReturn(pSvcCb, VERR_INVALID_POINTER);
811
812 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
813
814#ifdef DEBUG
815 LogFlowThisFunc(("ID=%RU32, uContextID=%RU32, uFunction=%RU32, pSvcCb=%p\n",
816 mData.mSession.mID, pCbCtx->uContextID, pCbCtx->uFunction, pSvcCb));
817#endif
818
819 int rc = VINF_SUCCESS;
820 switch (pCbCtx->uFunction)
821 {
822 case GUEST_DISCONNECTED:
823 /** @todo Handle closing all processes. */
824 break;
825
826 case GUEST_SESSION_NOTIFY:
827 {
828 rc = onSessionStatusChange(pCbCtx,
829 &mData.mCallback, pSvcCb);
830 break;
831 }
832
833 default:
834 /* Silently skip unknown callbacks. */
835 rc = VERR_NOT_SUPPORTED;
836 break;
837 }
838
839 LogFlowFuncLeaveRC(rc);
840 return rc;
841}
842
843inline bool GuestSession::fileExists(uint32_t uFileID, ComObjPtr<GuestFile> *pFile)
844{
845 SessionFiles::const_iterator it = mData.mFiles.find(uFileID);
846 if (it != mData.mFiles.end())
847 {
848 if (pFile)
849 *pFile = it->second;
850 return true;
851 }
852 return false;
853}
854
855int GuestSession::fileRemoveFromList(GuestFile *pFile)
856{
857 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
858
859 for (SessionFiles::iterator itFiles = mData.mFiles.begin();
860 itFiles != mData.mFiles.end(); ++itFiles)
861 {
862 if (pFile == itFiles->second)
863 {
864 GuestFile *pThis = itFiles->second;
865 AssertPtr(pThis);
866
867 Bstr strName;
868 HRESULT hr = pThis->COMGETTER(FileName)(strName.asOutParam());
869 ComAssertComRC(hr);
870
871 Assert(mData.mNumObjects);
872 LogFlowThisFunc(("Removing file \"%s\" (Session: %RU32) (now total %ld files, %ld objects)\n",
873 Utf8Str(strName).c_str(), mData.mSession.mID, mData.mFiles.size() - 1, mData.mNumObjects - 1));
874#ifdef DEBUG
875 ULONG cRefs = pFile->AddRef();
876 LogFlowThisFunc(("pObject=%p, cRefs=%RU32\n", pFile, cRefs));
877 pFile->Release();
878#endif
879 mData.mFiles.erase(itFiles);
880 mData.mNumObjects--;
881 return VINF_SUCCESS;
882 }
883 }
884
885 return VERR_NOT_FOUND;
886}
887
888int GuestSession::fileRemoveInternal(const Utf8Str &strPath, int *pGuestRc)
889{
890 GuestProcessStartupInfo procInfo;
891 GuestProcessStream streamOut;
892
893 procInfo.mCommand = Utf8Str(VBOXSERVICE_TOOL_RM);
894 procInfo.mFlags = ProcessCreateFlag_WaitForStdOut;
895 procInfo.mArguments.push_back(Utf8Str("--machinereadable"));
896 procInfo.mArguments.push_back(strPath); /* The file we want to remove. */
897
898 GuestProcessTool procTool; int guestRc;
899 int vrc = procTool.Init(this, procInfo, false /* Async */, &guestRc);
900 if (RT_SUCCESS(vrc))
901 vrc = procTool.Wait(GUESTPROCESSTOOL_FLAG_NONE, &guestRc);
902
903 if (RT_SUCCESS(vrc))
904 {
905 if (RT_SUCCESS(guestRc))
906 guestRc = procTool.TerminatedOk(NULL /* Exit code */);
907 }
908
909 if ( vrc == VERR_GSTCTL_GUEST_ERROR
910 && pGuestRc)
911 {
912 *pGuestRc = guestRc;
913 }
914
915 LogFlowFuncLeaveRC(vrc);
916 return vrc;
917}
918
919int GuestSession::fileOpenInternal(const GuestFileOpenInfo &openInfo, ComObjPtr<GuestFile> &pFile, int *pGuestRc)
920{
921 LogFlowThisFunc(("strPath=%s, strOpenMode=%s, strDisposition=%s, uCreationMode=%x, iOffset=%RI64\n",
922 openInfo.mFileName.c_str(), openInfo.mOpenMode.c_str(), openInfo.mDisposition.c_str(),
923 openInfo.mCreationMode, openInfo.mInitialOffset));
924
925 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
926
927 int rc = VERR_MAX_PROCS_REACHED;
928 if (mData.mNumObjects >= VBOX_GUESTCTRL_MAX_OBJECTS)
929 return rc;
930
931 /* Create a new (host-based) file ID and assign it. */
932 uint32_t uNewFileID = 0;
933 ULONG uTries = 0;
934
935 for (;;)
936 {
937 /* Is the file ID already used? */
938 if (!fileExists(uNewFileID, NULL /* pProgress */))
939 {
940 /* Callback with context ID was not found. This means
941 * we can use this context ID for our new callback we want
942 * to add below. */
943 rc = VINF_SUCCESS;
944 break;
945 }
946 uNewFileID++;
947 if (uNewFileID == VBOX_GUESTCTRL_MAX_OBJECTS)
948 uNewFileID = 0;
949
950 if (++uTries == UINT32_MAX)
951 break; /* Don't try too hard. */
952 }
953
954 if (RT_FAILURE(rc))
955 return rc;
956
957 /* Create the directory object. */
958 HRESULT hr = pFile.createObject();
959 if (FAILED(hr))
960 return VERR_COM_UNEXPECTED;
961
962 Console *pConsole = mData.mParent->getConsole();
963 AssertPtr(pConsole);
964
965 rc = pFile->init(pConsole, this /* GuestSession */,
966 uNewFileID, openInfo);
967 if (RT_FAILURE(rc))
968 return rc;
969
970 /* Add the created file to our vector. */
971 mData.mFiles[uNewFileID] = pFile;
972 mData.mNumObjects++;
973 Assert(mData.mNumObjects <= VBOX_GUESTCTRL_MAX_OBJECTS);
974
975 LogFlowFunc(("Added new file \"%s\" (Session: %RU32) (now total %ld files, %ld objects)\n",
976 openInfo.mFileName.c_str(), mData.mSession.mID, mData.mFiles.size(), mData.mNumObjects));
977
978 LogFlowFuncLeaveRC(rc);
979 return rc;
980}
981
982int GuestSession::fileQueryInfoInternal(const Utf8Str &strPath, GuestFsObjData &objData, int *pGuestRc)
983{
984 LogFlowThisFunc(("strPath=%s\n", strPath.c_str()));
985
986 int vrc = fsQueryInfoInternal(strPath, objData, pGuestRc);
987 if (RT_SUCCESS(vrc))
988 {
989 vrc = objData.mType == FsObjType_File
990 ? VINF_SUCCESS : VERR_NOT_A_FILE;
991 }
992
993 LogFlowFuncLeaveRC(vrc);
994 return vrc;
995}
996
997int GuestSession::fileQuerySizeInternal(const Utf8Str &strPath, int64_t *pllSize, int *pGuestRc)
998{
999 AssertPtrReturn(pllSize, VERR_INVALID_POINTER);
1000
1001 GuestFsObjData objData;
1002 int vrc = fileQueryInfoInternal(strPath, objData, pGuestRc);
1003 if (RT_SUCCESS(vrc))
1004 *pllSize = objData.mObjectSize;
1005
1006 return vrc;
1007}
1008
1009int GuestSession::fsQueryInfoInternal(const Utf8Str &strPath, GuestFsObjData &objData, int *pGuestRc)
1010{
1011 LogFlowThisFunc(("strPath=%s\n", strPath.c_str()));
1012
1013 /** @todo Merge this with IGuestFile::queryInfo(). */
1014 GuestProcessStartupInfo procInfo;
1015 procInfo.mCommand = Utf8Str(VBOXSERVICE_TOOL_STAT);
1016 procInfo.mFlags = ProcessCreateFlag_WaitForStdOut;
1017
1018 /* Construct arguments. */
1019 procInfo.mArguments.push_back(Utf8Str("--machinereadable"));
1020 procInfo.mArguments.push_back(strPath);
1021
1022 GuestProcessTool procTool; int guestRc;
1023 int vrc = procTool.Init(this, procInfo, false /* Async */, &guestRc);
1024 if (RT_SUCCESS(vrc))
1025 vrc = procTool.Wait(GUESTPROCESSTOOL_FLAG_NONE, &guestRc);
1026 if (RT_SUCCESS(vrc))
1027 {
1028 guestRc = procTool.TerminatedOk(NULL /* Exit code */);
1029 if (RT_SUCCESS(guestRc))
1030 {
1031 GuestProcessStreamBlock curBlock;
1032 vrc = procTool.GetCurrentBlock(OUTPUT_HANDLE_ID_STDOUT, curBlock);
1033 /** @todo Check for more / validate blocks! */
1034 if (RT_SUCCESS(vrc))
1035 vrc = objData.FromStat(curBlock);
1036 }
1037 }
1038
1039 if ( vrc == VERR_GSTCTL_GUEST_ERROR
1040 && pGuestRc)
1041 {
1042 *pGuestRc = guestRc;
1043 }
1044
1045 LogFlowFuncLeaveRC(vrc);
1046 return vrc;
1047}
1048
1049const GuestCredentials& GuestSession::getCredentials(void)
1050{
1051 return mData.mCredentials;
1052}
1053
1054const GuestEnvironment& GuestSession::getEnvironment(void)
1055{
1056 return mData.mEnvironment;
1057}
1058
1059Utf8Str GuestSession::getName(void)
1060{
1061 return mData.mSession.mName;
1062}
1063
1064/* static */
1065Utf8Str GuestSession::guestErrorToString(int guestRc)
1066{
1067 Utf8Str strError;
1068
1069 /** @todo pData->u32Flags: int vs. uint32 -- IPRT errors are *negative* !!! */
1070 switch (guestRc)
1071 {
1072 case VERR_INVALID_VM_HANDLE:
1073 strError += Utf8StrFmt(tr("VMM device is not available (is the VM running?)"));
1074 break;
1075
1076 case VERR_HGCM_SERVICE_NOT_FOUND:
1077 strError += Utf8StrFmt(tr("The guest execution service is not available"));
1078 break;
1079
1080 case VERR_AUTHENTICATION_FAILURE:
1081 strError += Utf8StrFmt(tr("The specified user was not able to logon on guest"));
1082 break;
1083
1084 case VERR_TIMEOUT:
1085 strError += Utf8StrFmt(tr("The guest did not respond within time"));
1086 break;
1087
1088 case VERR_CANCELLED:
1089 strError += Utf8StrFmt(tr("The session operation was canceled"));
1090 break;
1091
1092 case VERR_PERMISSION_DENIED:
1093 strError += Utf8StrFmt(tr("Invalid user/password credentials"));
1094 break;
1095
1096 case VERR_MAX_PROCS_REACHED:
1097 strError += Utf8StrFmt(tr("Maximum number of parallel guest processes has been reached"));
1098 break;
1099
1100 case VERR_NOT_EQUAL: /** @todo Imprecise to the user; can mean anything and all. */
1101 strError += Utf8StrFmt(tr("Unable to retrieve requested information"));
1102 break;
1103
1104 case VERR_NOT_FOUND:
1105 strError += Utf8StrFmt(tr("The guest execution service is not ready (yet)"));
1106 break;
1107
1108 default:
1109 strError += Utf8StrFmt("%Rrc", guestRc);
1110 break;
1111 }
1112
1113 return strError;
1114}
1115
1116/** No locking! */
1117int GuestSession::onSessionStatusChange(PVBOXGUESTCTRLHOSTCBCTX pCbCtx,
1118 GuestCtrlCallback *pCallback, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData)
1119{
1120 /* pCallback is optional. */
1121 AssertPtrReturn(pSvcCbData, VERR_INVALID_POINTER);
1122
1123 if (pSvcCbData->mParms < 3)
1124 return VERR_INVALID_PARAMETER;
1125
1126 CALLBACKDATA_SESSION_NOTIFY dataCb;
1127 /* pSvcCb->mpaParms[0] always contains the context ID. */
1128 pSvcCbData->mpaParms[1].getUInt32(&dataCb.uType);
1129 pSvcCbData->mpaParms[2].getUInt32(&dataCb.uResult);
1130
1131 LogFlowThisFunc(("ID=%RU32, uType=%RU32, rc=%Rrc, pCallback=%p, pData=%p\n",
1132 mData.mSession.mID, dataCb.uType, dataCb.uResult, pCallback, pSvcCbData));
1133
1134 int vrc = VINF_SUCCESS;
1135
1136 int guestRc = dataCb.uResult; /** @todo uint32_t vs. int. */
1137 switch (dataCb.uType)
1138 {
1139 case GUEST_SESSION_NOTIFYTYPE_ERROR:
1140 mData.mStatus = GuestSessionStatus_Error;
1141 break;
1142
1143 case GUEST_SESSION_NOTIFYTYPE_STARTED:
1144 mData.mStatus = GuestSessionStatus_Started;
1145 break;
1146
1147 case GUEST_SESSION_NOTIFYTYPE_TEN:
1148 case GUEST_SESSION_NOTIFYTYPE_TES:
1149 case GUEST_SESSION_NOTIFYTYPE_TEA:
1150 mData.mStatus = GuestSessionStatus_Terminated;
1151 break;
1152
1153 case GUEST_SESSION_NOTIFYTYPE_TOK:
1154 mData.mStatus = GuestSessionStatus_TimedOutKilled;
1155 break;
1156
1157 case GUEST_SESSION_NOTIFYTYPE_TOA:
1158 mData.mStatus = GuestSessionStatus_TimedOutAbnormally;
1159 break;
1160
1161 case GUEST_SESSION_NOTIFYTYPE_DWN:
1162 mData.mStatus = GuestSessionStatus_Down;
1163 break;
1164
1165 default:
1166 vrc = VERR_NOT_SUPPORTED;
1167 break;
1168 }
1169
1170 if (RT_SUCCESS(vrc))
1171 {
1172 if (RT_FAILURE(guestRc))
1173 mData.mStatus = GuestSessionStatus_Error;
1174 }
1175 else if (vrc == VERR_NOT_SUPPORTED)
1176 {
1177 /* Also let the callback know. */
1178 guestRc = VERR_NOT_SUPPORTED;
1179 }
1180
1181 /*
1182 * Now do the signalling stuff.
1183 */
1184 if (pCallback)
1185 {
1186 int rc2 = pCallback->SetData(&dataCb, sizeof(dataCb));
1187 AssertRC(rc2);
1188 rc2 = pCallback->Signal(guestRc);
1189 AssertRC(rc2);
1190 }
1191
1192 LogFlowThisFunc(("ID=%RU32, guestRc=%Rrc\n",
1193 mData.mSession.mID, guestRc));
1194
1195 LogFlowFuncLeaveRC(vrc);
1196 return vrc;
1197}
1198
1199int GuestSession::startSessionIntenal(int *pGuestRc)
1200{
1201 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1202
1203 LogFlowThisFunc(("uProtocolVersion=%RU32, openFlags=%x, openTimeoutMS=%RU32\n",
1204 mData.mProtocolVersion, mData.mSession.mOpenFlags, mData.mSession.mOpenTimeoutMS));
1205
1206 /* Legacy Guest Additions don't support opening dedicated
1207 guest sessions. Simply return success here. */
1208 if (mData.mProtocolVersion < 2)
1209 {
1210 mData.mStatus = GuestSessionStatus_Started;
1211
1212 LogFlowThisFunc(("Installed Guest Additions don't support opening dedicated sessions, skipping\n"));
1213 return VINF_SUCCESS;
1214 }
1215
1216 /** @todo mData.mSession.uFlags validation. */
1217
1218 /* Set current session status. */
1219 mData.mStatus = GuestSessionStatus_Starting;
1220
1221 /* Destroy a pending callback request. */
1222 mData.mCallback.Destroy();
1223
1224 int vrc = mData.mCallback.Init(CALLBACKTYPE_SESSION_NOTIFY);
1225
1226 alock.release(); /* Drop the write lock again. */
1227
1228 if (RT_SUCCESS(vrc))
1229 {
1230 uint32_t uContextID =
1231 VBOX_GUESTCTRL_CONTEXTID_MAKE(mData.mSession.mID /* Session ID */,
1232 0 /* Object */, 0 /* Count */);
1233
1234 VBOXHGCMSVCPARM paParms[8];
1235
1236 int i = 0;
1237 paParms[i++].setUInt32(uContextID);
1238 paParms[i++].setUInt32(mData.mProtocolVersion);
1239 paParms[i++].setPointer((void*)mData.mCredentials.mUser.c_str(),
1240 (ULONG)mData.mCredentials.mUser.length() + 1);
1241 paParms[i++].setPointer((void*)mData.mCredentials.mPassword.c_str(),
1242 (ULONG)mData.mCredentials.mPassword.length() + 1);
1243 paParms[i++].setPointer((void*)mData.mCredentials.mDomain.c_str(),
1244 (ULONG)mData.mCredentials.mDomain.length() + 1);
1245 paParms[i++].setUInt32(mData.mSession.mOpenFlags);
1246
1247 vrc = sendCommand(HOST_SESSION_CREATE, i, paParms);
1248 }
1249
1250 if (RT_SUCCESS(vrc))
1251 {
1252 /*
1253 * Let's wait for the process being started.
1254 * Note: Be sure not keeping a AutoRead/WriteLock here.
1255 */
1256 LogFlowThisFunc(("Waiting for callback (%RU32ms) ...\n", mData.mSession.mOpenTimeoutMS));
1257 vrc = mData.mCallback.Wait(mData.mSession.mOpenTimeoutMS);
1258 if (RT_SUCCESS(vrc)) /* Wait was successful, check for supplied information. */
1259 {
1260 int guestRc = mData.mCallback.GetResultCode();
1261 if (RT_SUCCESS(guestRc))
1262 {
1263 /* Nothing to do here right now. */
1264 }
1265 else
1266 vrc = VERR_GSTCTL_GUEST_ERROR;
1267
1268 if (pGuestRc)
1269 *pGuestRc = guestRc;
1270 LogFlowThisFunc(("Callback returned rc=%Rrc\n", guestRc));
1271 }
1272 }
1273
1274 alock.acquire();
1275
1276 /* Destroy callback. */
1277 mData.mCallback.Destroy();
1278
1279 LogFlowFuncLeaveRC(vrc);
1280 return vrc;
1281}
1282
1283int GuestSession::startSessionAsync(void)
1284{
1285 LogFlowThisFuncEnter();
1286
1287 int vrc;
1288
1289 try
1290 {
1291 /* Asynchronously open the session on the guest by kicking off a
1292 * worker thread. */
1293 std::auto_ptr<GuestSessionTaskInternalOpen> pTask(new GuestSessionTaskInternalOpen(this));
1294 AssertReturn(pTask->isOk(), pTask->rc());
1295
1296 vrc = RTThreadCreate(NULL, GuestSession::startSessionThread,
1297 (void *)pTask.get(), 0,
1298 RTTHREADTYPE_MAIN_WORKER, 0,
1299 "gctlSesStart");
1300 if (RT_SUCCESS(vrc))
1301 {
1302 /* pTask is now owned by openSessionThread(), so release it. */
1303 pTask.release();
1304 }
1305 }
1306 catch(std::bad_alloc &)
1307 {
1308 vrc = VERR_NO_MEMORY;
1309 }
1310
1311 LogFlowFuncLeaveRC(vrc);
1312 return vrc;
1313}
1314
1315/* static */
1316DECLCALLBACK(int) GuestSession::startSessionThread(RTTHREAD Thread, void *pvUser)
1317{
1318 LogFlowFunc(("pvUser=%p\n", pvUser));
1319
1320 std::auto_ptr<GuestSessionTaskInternalOpen> pTask(static_cast<GuestSessionTaskInternalOpen*>(pvUser));
1321 AssertPtr(pTask.get());
1322
1323 const ComObjPtr<GuestSession> pSession(pTask->Session());
1324 Assert(!pSession.isNull());
1325
1326 AutoCaller autoCaller(pSession);
1327 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1328
1329 int vrc = pSession->startSessionIntenal(NULL /* Guest rc, ignored */);
1330 /* Nothing to do here anymore. */
1331
1332 LogFlowFuncLeaveRC(vrc);
1333 return vrc;
1334}
1335
1336int GuestSession::processRemoveFromList(GuestProcess *pProcess)
1337{
1338 LogFlowThisFuncEnter();
1339
1340 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1341
1342 int rc = VERR_NOT_FOUND;
1343
1344 ULONG uPID;
1345 HRESULT hr = pProcess->COMGETTER(PID)(&uPID);
1346
1347 LogFlowFunc(("Closing process (PID=%RU32) ...\n", uPID));
1348
1349 for (SessionProcesses::iterator itProcs = mData.mProcesses.begin();
1350 itProcs != mData.mProcesses.end(); ++itProcs)
1351 {
1352 if (pProcess == itProcs->second)
1353 {
1354 GuestProcess *pCurProc = itProcs->second;
1355 AssertPtr(pCurProc);
1356
1357 hr = pCurProc->COMGETTER(PID)(&uPID);
1358 ComAssertComRC(hr);
1359
1360 Assert(mData.mNumObjects);
1361 LogFlowFunc(("Removing process (Session: %RU32) with process ID=%RU32, guest PID=%RU32 (now total %ld processes, %ld objects)\n",
1362 mData.mSession.mID, pCurProc->getObjectID(), uPID, mData.mProcesses.size() - 1, mData.mNumObjects - 1));
1363
1364 mData.mProcesses.erase(itProcs);
1365 mData.mNumObjects--;
1366
1367 rc = VINF_SUCCESS;
1368 break;
1369 }
1370 }
1371
1372 LogFlowFuncLeaveRC(rc);
1373 return rc;
1374}
1375
1376/**
1377 * Creates but does *not* start the process yet. See GuestProcess::startProcess() or
1378 * GuestProcess::startProcessAsync() for that.
1379 *
1380 * @return IPRT status code.
1381 * @param procInfo
1382 * @param pProcess
1383 */
1384int GuestSession::processCreateExInteral(GuestProcessStartupInfo &procInfo, ComObjPtr<GuestProcess> &pProcess)
1385{
1386 LogFlowFunc(("mCmd=%s, mFlags=%x, mTimeoutMS=%RU32\n",
1387 procInfo.mCommand.c_str(), procInfo.mFlags, procInfo.mTimeoutMS));
1388#ifdef DEBUG
1389 if (procInfo.mArguments.size())
1390 {
1391 LogFlowFunc(("Arguments:"));
1392 ProcessArguments::const_iterator it = procInfo.mArguments.begin();
1393 while (it != procInfo.mArguments.end())
1394 {
1395 LogFlow((" %s", (*it).c_str()));
1396 it++;
1397 }
1398 LogFlow(("\n"));
1399 }
1400#endif
1401
1402 /* Validate flags. */
1403 if (procInfo.mFlags)
1404 {
1405 if ( !(procInfo.mFlags & ProcessCreateFlag_IgnoreOrphanedProcesses)
1406 && !(procInfo.mFlags & ProcessCreateFlag_WaitForProcessStartOnly)
1407 && !(procInfo.mFlags & ProcessCreateFlag_Hidden)
1408 && !(procInfo.mFlags & ProcessCreateFlag_NoProfile)
1409 && !(procInfo.mFlags & ProcessCreateFlag_WaitForStdOut)
1410 && !(procInfo.mFlags & ProcessCreateFlag_WaitForStdErr))
1411 {
1412 return VERR_INVALID_PARAMETER;
1413 }
1414 }
1415
1416 if ( (procInfo.mFlags & ProcessCreateFlag_WaitForProcessStartOnly)
1417 && ( (procInfo.mFlags & ProcessCreateFlag_WaitForStdOut)
1418 || (procInfo.mFlags & ProcessCreateFlag_WaitForStdErr)
1419 )
1420 )
1421 {
1422 return VERR_INVALID_PARAMETER;
1423 }
1424
1425 /* Adjust timeout. If set to 0, we define
1426 * an infinite timeout. */
1427 if (procInfo.mTimeoutMS == 0)
1428 procInfo.mTimeoutMS = UINT32_MAX;
1429
1430 /** @tood Implement process priority + affinity. */
1431
1432 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1433
1434 int rc = VERR_MAX_PROCS_REACHED;
1435 if (mData.mNumObjects >= VBOX_GUESTCTRL_MAX_OBJECTS)
1436 return rc;
1437
1438 /* Create a new (host-based) process ID and assign it. */
1439 uint32_t uNewProcessID = 0;
1440 ULONG uTries = 0;
1441
1442 for (;;)
1443 {
1444 /* Is the context ID already used? */
1445 if (!processExists(uNewProcessID, NULL /* pProgress */))
1446 {
1447 /* Callback with context ID was not found. This means
1448 * we can use this context ID for our new callback we want
1449 * to add below. */
1450 rc = VINF_SUCCESS;
1451 break;
1452 }
1453 uNewProcessID++;
1454 if (uNewProcessID == VBOX_GUESTCTRL_MAX_OBJECTS)
1455 uNewProcessID = 0;
1456
1457 if (++uTries == VBOX_GUESTCTRL_MAX_OBJECTS)
1458 break; /* Don't try too hard. */
1459 }
1460
1461 if (RT_FAILURE(rc))
1462 return rc;
1463
1464 /* Create the process object. */
1465 HRESULT hr = pProcess.createObject();
1466 if (FAILED(hr))
1467 return VERR_COM_UNEXPECTED;
1468
1469 rc = pProcess->init(mData.mParent->getConsole() /* Console */, this /* Session */,
1470 uNewProcessID, procInfo);
1471 if (RT_FAILURE(rc))
1472 return rc;
1473
1474 /* Add the created process to our map. */
1475 mData.mProcesses[uNewProcessID] = pProcess;
1476 mData.mNumObjects++;
1477 Assert(mData.mNumObjects <= VBOX_GUESTCTRL_MAX_OBJECTS);
1478
1479 LogFlowFunc(("Added new process (Session: %RU32) with process ID=%RU32 (now total %ld processes, %ld objects)\n",
1480 mData.mSession.mID, uNewProcessID, mData.mProcesses.size(), mData.mNumObjects));
1481
1482 return rc;
1483}
1484
1485inline bool GuestSession::processExists(uint32_t uProcessID, ComObjPtr<GuestProcess> *pProcess)
1486{
1487 SessionProcesses::const_iterator it = mData.mProcesses.find(uProcessID);
1488 if (it != mData.mProcesses.end())
1489 {
1490 if (pProcess)
1491 *pProcess = it->second;
1492 return true;
1493 }
1494 return false;
1495}
1496
1497inline int GuestSession::processGetByPID(ULONG uPID, ComObjPtr<GuestProcess> *pProcess)
1498{
1499 AssertReturn(uPID, false);
1500 /* pProcess is optional. */
1501
1502 SessionProcesses::iterator itProcs = mData.mProcesses.begin();
1503 for (; itProcs != mData.mProcesses.end(); itProcs++)
1504 {
1505 ComObjPtr<GuestProcess> pCurProc = itProcs->second;
1506 AutoCaller procCaller(pCurProc);
1507 if (procCaller.rc())
1508 return VERR_COM_INVALID_OBJECT_STATE;
1509
1510 ULONG uCurPID;
1511 HRESULT hr = pCurProc->COMGETTER(PID)(&uCurPID);
1512 ComAssertComRC(hr);
1513
1514 if (uCurPID == uPID)
1515 {
1516 if (pProcess)
1517 *pProcess = pCurProc;
1518 return VINF_SUCCESS;
1519 }
1520 }
1521
1522 return VERR_NOT_FOUND;
1523}
1524
1525int GuestSession::sendCommand(uint32_t uFunction,
1526 uint32_t uParms, PVBOXHGCMSVCPARM paParms)
1527{
1528 LogFlowThisFuncEnter();
1529
1530#ifndef VBOX_GUESTCTRL_TEST_CASE
1531 ComObjPtr<Console> pConsole = mData.mParent->getConsole();
1532 Assert(!pConsole.isNull());
1533
1534 /* Forward the information to the VMM device. */
1535 VMMDev *pVMMDev = pConsole->getVMMDev();
1536 AssertPtr(pVMMDev);
1537
1538 LogFlowThisFunc(("uFunction=%RU32, uParms=%RU32\n", uFunction, uParms));
1539 int vrc = pVMMDev->hgcmHostCall(HGCMSERVICE_NAME, uFunction, uParms, paParms);
1540 if (RT_FAILURE(vrc))
1541 {
1542 /** @todo What to do here? */
1543 }
1544#else
1545 /* Not needed within testcases. */
1546 int vrc = VINF_SUCCESS;
1547#endif
1548 LogFlowFuncLeaveRC(vrc);
1549 return vrc;
1550}
1551
1552/* static */
1553HRESULT GuestSession::setErrorExternal(VirtualBoxBase *pInterface, int guestRc)
1554{
1555 AssertPtr(pInterface);
1556 AssertMsg(RT_FAILURE(guestRc), ("Guest rc does not indicate a failure when setting error\n"));
1557
1558 return pInterface->setError(VBOX_E_IPRT_ERROR, GuestSession::guestErrorToString(guestRc).c_str());
1559}
1560
1561int GuestSession::startTaskAsync(const Utf8Str &strTaskDesc,
1562 GuestSessionTask *pTask, ComObjPtr<Progress> &pProgress)
1563{
1564 LogFlowThisFunc(("strTaskDesc=%s, pTask=%p\n", strTaskDesc.c_str(), pTask));
1565
1566 AssertPtrReturn(pTask, VERR_INVALID_POINTER);
1567
1568 /* Create the progress object. */
1569 HRESULT hr = pProgress.createObject();
1570 if (FAILED(hr))
1571 return VERR_COM_UNEXPECTED;
1572
1573 hr = pProgress->init(static_cast<IGuestSession*>(this),
1574 Bstr(strTaskDesc).raw(),
1575 TRUE /* aCancelable */);
1576 if (FAILED(hr))
1577 return VERR_COM_UNEXPECTED;
1578
1579 /* Initialize our worker task. */
1580 std::auto_ptr<GuestSessionTask> task(pTask);
1581
1582 int rc = task->RunAsync(strTaskDesc, pProgress);
1583 if (RT_FAILURE(rc))
1584 return rc;
1585
1586 /* Don't destruct on success. */
1587 task.release();
1588
1589 LogFlowFuncLeaveRC(rc);
1590 return rc;
1591}
1592
1593/**
1594 * Queries/collects information prior to establishing a guest session.
1595 * This is necessary to know which guest control protocol version to use,
1596 * among other things (later).
1597 *
1598 * @return IPRT status code.
1599 */
1600int GuestSession::queryInfo(void)
1601{
1602#ifndef DEBUG_andy
1603 /* Since the new functions are not fully implemented yet, force Main
1604 to use protocol ver 1 so far. */
1605 mData.mProtocolVersion = 1;
1606#else
1607 #if 1
1608 /* For debugging only: Hardcode version. */
1609 mData.mProtocolVersion = 2;
1610 #else
1611 /*
1612 * Try querying the guest control protocol version running on the guest.
1613 * This is done using the Guest Additions version
1614 */
1615 ComObjPtr<Guest> pGuest = mData.mParent;
1616 Assert(!pGuest.isNull());
1617
1618 uint32_t uVerAdditions = pGuest->getAdditionsVersion();
1619 mData.mProtocolVersion = ( VBOX_FULL_VERSION_GET_MAJOR(uVerAdditions) >= 4
1620 && VBOX_FULL_VERSION_GET_MINOR(uVerAdditions) >= 3) /** @todo What's about v5.0 ? */
1621 ? 2 /* Guest control 2.0. */
1622 : 1; /* Legacy guest control (VBox < 4.3). */
1623 /* Build revision is ignored. */
1624
1625 /* Tell the user but don't bitch too often. */
1626 static short s_gctrlLegacyWarning = 0;
1627 if (s_gctrlLegacyWarning++ < 3) /** @todo Find a bit nicer text. */
1628 LogRel((tr("Warning: Guest Additions are older (%ld.%ld) than host capabilities for guest control, please upgrade them. Using protocol version %ld now\n"),
1629 VBOX_FULL_VERSION_GET_MAJOR(uVerAdditions), VBOX_FULL_VERSION_GET_MINOR(uVerAdditions), mData.mProtocolVersion));
1630 #endif
1631#endif
1632 return VINF_SUCCESS;
1633}
1634
1635int GuestSession::waitFor(uint32_t fWaitFlags, ULONG uTimeoutMS, GuestSessionWaitResult_T &waitResult, int *pGuestRc)
1636{
1637 LogFlowThisFuncEnter();
1638
1639 AssertReturn(fWaitFlags, VERR_INVALID_PARAMETER);
1640
1641 LogFlowThisFunc(("fWaitFlags=0x%x, uTimeoutMS=%RU32, mStatus=%RU32, mWaitCount=%RU32, mWaitEvent=%p, pGuestRc=%p\n",
1642 fWaitFlags, uTimeoutMS, mData.mStatus, mData.mWaitCount, mData.mWaitEvent, pGuestRc));
1643
1644 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1645
1646 /* Did some error occur before? Then skip waiting and return. */
1647 if (mData.mStatus == GuestSessionStatus_Error)
1648 {
1649 waitResult = GuestSessionWaitResult_Error;
1650 AssertMsg(RT_FAILURE(mData.mRC), ("No error rc (%Rrc) set when guest session indicated an error\n", mData.mRC));
1651 if (pGuestRc)
1652 *pGuestRc = mData.mRC; /* Return last set error. */
1653 return VERR_GSTCTL_GUEST_ERROR;
1654 }
1655
1656 waitResult = GuestSessionWaitResult_None;
1657 if (fWaitFlags & GuestSessionWaitForFlag_Terminate)
1658 {
1659 switch (mData.mStatus)
1660 {
1661 case GuestSessionStatus_Terminated:
1662 case GuestSessionStatus_Down:
1663 waitResult = GuestSessionWaitResult_Terminate;
1664 break;
1665
1666 case GuestSessionStatus_TimedOutKilled:
1667 case GuestSessionStatus_TimedOutAbnormally:
1668 waitResult = GuestSessionWaitResult_Timeout;
1669 break;
1670
1671 case GuestSessionStatus_Error:
1672 /* Handled above. */
1673 break;
1674
1675 case GuestSessionStatus_Started:
1676 waitResult = GuestSessionWaitResult_Start;
1677 break;
1678
1679 case GuestSessionStatus_Undefined:
1680 case GuestSessionStatus_Starting:
1681 /* Do the waiting below. */
1682 break;
1683
1684 default:
1685 AssertMsgFailed(("Unhandled session status %ld\n", mData.mStatus));
1686 return VERR_NOT_IMPLEMENTED;
1687 }
1688 }
1689 else if (fWaitFlags & GuestSessionWaitForFlag_Start)
1690 {
1691 switch (mData.mStatus)
1692 {
1693 case GuestSessionStatus_Started:
1694 case GuestSessionStatus_Terminating:
1695 case GuestSessionStatus_Terminated:
1696 case GuestSessionStatus_Down:
1697 waitResult = GuestSessionWaitResult_Start;
1698 break;
1699
1700 case GuestSessionStatus_Error:
1701 waitResult = GuestSessionWaitResult_Error;
1702 break;
1703
1704 case GuestSessionStatus_TimedOutKilled:
1705 case GuestSessionStatus_TimedOutAbnormally:
1706 waitResult = GuestSessionWaitResult_Timeout;
1707 break;
1708
1709 case GuestSessionStatus_Undefined:
1710 case GuestSessionStatus_Starting:
1711 /* Do the waiting below. */
1712 break;
1713
1714 default:
1715 AssertMsgFailed(("Unhandled session status %ld\n", mData.mStatus));
1716 return VERR_NOT_IMPLEMENTED;
1717 }
1718 }
1719
1720 LogFlowThisFunc(("sessionStatus=%ld, sessionRc=%Rrc, waitResult=%ld\n",
1721 mData.mStatus, mData.mRC, waitResult));
1722
1723 /* No waiting needed? Return immediately using the last set error. */
1724 if (waitResult != GuestSessionWaitResult_None)
1725 {
1726 if (pGuestRc)
1727 *pGuestRc = mData.mRC; /* Return last set error (if any). */
1728 return RT_SUCCESS(mData.mRC) ? VINF_SUCCESS : VERR_GSTCTL_GUEST_ERROR;
1729 }
1730
1731 if (mData.mWaitCount > 0) /* We only support one waiting caller a time at the moment. */
1732 return VERR_ALREADY_EXISTS;
1733 mData.mWaitCount++;
1734
1735 int vrc = VINF_SUCCESS;
1736 try
1737 {
1738 Assert(mData.mWaitEvent == NULL);
1739 mData.mWaitEvent = new GuestSessionWaitEvent(fWaitFlags);
1740 }
1741 catch(std::bad_alloc &)
1742 {
1743 vrc = VERR_NO_MEMORY;
1744 }
1745
1746 if (RT_SUCCESS(vrc))
1747 {
1748 GuestSessionWaitEvent *pEvent = mData.mWaitEvent;
1749 AssertPtr(pEvent);
1750
1751 alock.release(); /* Release lock before waiting. */
1752
1753 vrc = pEvent->Wait(uTimeoutMS);
1754 LogFlowThisFunc(("Waiting completed with rc=%Rrc\n", vrc));
1755 if (RT_SUCCESS(vrc))
1756 {
1757 waitResult = pEvent->GetWaitResult();
1758 int guestRc = pEvent->GetWaitRc();
1759 if (RT_FAILURE(guestRc))
1760 vrc = VERR_GSTCTL_GUEST_ERROR;
1761
1762 LogFlowThisFunc(("Waiting event returned rc=%Rrc\n", guestRc));
1763
1764 if (pGuestRc)
1765 *pGuestRc = guestRc;
1766 }
1767
1768 alock.acquire(); /* Get the lock again. */
1769
1770 /* Note: The caller always is responsible of deleting the
1771 * stuff it created before. See close() for more information. */
1772 delete mData.mWaitEvent;
1773 mData.mWaitEvent = NULL;
1774 }
1775
1776 Assert(mData.mWaitCount);
1777 mData.mWaitCount--;
1778
1779 LogFlowFuncLeaveRC(vrc);
1780 return vrc;
1781}
1782
1783// implementation of public methods
1784/////////////////////////////////////////////////////////////////////////////
1785
1786STDMETHODIMP GuestSession::Close(void)
1787{
1788#ifndef VBOX_WITH_GUEST_CONTROL
1789 ReturnComNotImplemented();
1790#else
1791 LogFlowThisFuncEnter();
1792
1793 AutoCaller autoCaller(this);
1794 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1795
1796 /* Close session on guest session. */
1797 int guestRc;
1798 int rc = closeSession(0 /* Flags */, 30 * 1000 /* Timeout */,
1799 &guestRc);
1800 /* On failure don't return here, instead do all the cleanup
1801 * work first and then return an error. */
1802
1803 /* Remove ourselves from the session list. */
1804 int rc2 = mData.mParent->sessionRemove(this);
1805 if (RT_SUCCESS(rc))
1806 rc = rc2;
1807
1808 /*
1809 * Release autocaller before calling uninit.
1810 */
1811 autoCaller.release();
1812
1813 uninit();
1814
1815 LogFlowFuncLeaveRC(rc);
1816 if (RT_FAILURE(rc))
1817 {
1818 /** @todo Handle guestRc! */
1819 return setError(VBOX_E_IPRT_ERROR,
1820 tr("Closing guest session failed with %Rrc\n"), rc);
1821 }
1822
1823 return S_OK;
1824#endif /* VBOX_WITH_GUEST_CONTROL */
1825}
1826
1827STDMETHODIMP GuestSession::CopyFrom(IN_BSTR aSource, IN_BSTR aDest, ComSafeArrayIn(CopyFileFlag_T, aFlags), IProgress **aProgress)
1828{
1829#ifndef VBOX_WITH_GUEST_CONTROL
1830 ReturnComNotImplemented();
1831#else
1832 CheckComArgStrNotEmptyOrNull(aSource);
1833 CheckComArgStrNotEmptyOrNull(aDest);
1834 CheckComArgOutPointerValid(aProgress);
1835
1836 LogFlowThisFuncEnter();
1837
1838 if (RT_UNLIKELY((aSource) == NULL || *(aSource) == '\0'))
1839 return setError(E_INVALIDARG, tr("No source specified"));
1840 if (RT_UNLIKELY((aDest) == NULL || *(aDest) == '\0'))
1841 return setError(E_INVALIDARG, tr("No destination specified"));
1842
1843 AutoCaller autoCaller(this);
1844 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1845
1846 uint32_t fFlags = CopyFileFlag_None;
1847 if (aFlags)
1848 {
1849 com::SafeArray<CopyFileFlag_T> flags(ComSafeArrayInArg(aFlags));
1850 for (size_t i = 0; i < flags.size(); i++)
1851 fFlags |= flags[i];
1852 }
1853
1854 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1855
1856 HRESULT hr = S_OK;
1857
1858 try
1859 {
1860 ComObjPtr<Progress> pProgress;
1861 SessionTaskCopyFrom *pTask = new SessionTaskCopyFrom(this /* GuestSession */,
1862 Utf8Str(aSource), Utf8Str(aDest), fFlags);
1863 int rc = startTaskAsync(Utf8StrFmt(tr("Copying \"%ls\" from guest to \"%ls\" on the host"), aSource, aDest),
1864 pTask, pProgress);
1865 if (RT_SUCCESS(rc))
1866 {
1867 /* Return progress to the caller. */
1868 hr = pProgress.queryInterfaceTo(aProgress);
1869 }
1870 else
1871 hr = setError(VBOX_E_IPRT_ERROR,
1872 tr("Starting task for copying file \"%ls\" from guest to \"%ls\" on the host failed: %Rrc"), rc);
1873 }
1874 catch(std::bad_alloc &)
1875 {
1876 hr = E_OUTOFMEMORY;
1877 }
1878
1879 return hr;
1880#endif /* VBOX_WITH_GUEST_CONTROL */
1881}
1882
1883STDMETHODIMP GuestSession::CopyTo(IN_BSTR aSource, IN_BSTR aDest, ComSafeArrayIn(CopyFileFlag_T, aFlags), IProgress **aProgress)
1884{
1885#ifndef VBOX_WITH_GUEST_CONTROL
1886 ReturnComNotImplemented();
1887#else
1888 CheckComArgStrNotEmptyOrNull(aSource);
1889 CheckComArgStrNotEmptyOrNull(aDest);
1890 CheckComArgOutPointerValid(aProgress);
1891
1892 LogFlowThisFuncEnter();
1893
1894 if (RT_UNLIKELY((aSource) == NULL || *(aSource) == '\0'))
1895 return setError(E_INVALIDARG, tr("No source specified"));
1896 if (RT_UNLIKELY((aDest) == NULL || *(aDest) == '\0'))
1897 return setError(E_INVALIDARG, tr("No destination specified"));
1898
1899 AutoCaller autoCaller(this);
1900 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1901
1902 uint32_t fFlags = CopyFileFlag_None;
1903 if (aFlags)
1904 {
1905 com::SafeArray<CopyFileFlag_T> flags(ComSafeArrayInArg(aFlags));
1906 for (size_t i = 0; i < flags.size(); i++)
1907 fFlags |= flags[i];
1908 }
1909
1910 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1911
1912 HRESULT hr = S_OK;
1913
1914 try
1915 {
1916 ComObjPtr<Progress> pProgress;
1917 SessionTaskCopyTo *pTask = new SessionTaskCopyTo(this /* GuestSession */,
1918 Utf8Str(aSource), Utf8Str(aDest), fFlags);
1919 AssertPtrReturn(pTask, E_OUTOFMEMORY);
1920 int rc = startTaskAsync(Utf8StrFmt(tr("Copying \"%ls\" from host to \"%ls\" on the guest"), aSource, aDest),
1921 pTask, pProgress);
1922 if (RT_SUCCESS(rc))
1923 {
1924 /* Return progress to the caller. */
1925 hr = pProgress.queryInterfaceTo(aProgress);
1926 }
1927 else
1928 hr = setError(VBOX_E_IPRT_ERROR,
1929 tr("Starting task for copying file \"%ls\" from host to \"%ls\" on the guest failed: %Rrc"), rc);
1930 }
1931 catch(std::bad_alloc &)
1932 {
1933 hr = E_OUTOFMEMORY;
1934 }
1935
1936 return hr;
1937#endif /* VBOX_WITH_GUEST_CONTROL */
1938}
1939
1940STDMETHODIMP GuestSession::DirectoryCreate(IN_BSTR aPath, ULONG aMode,
1941 ComSafeArrayIn(DirectoryCreateFlag_T, aFlags))
1942{
1943#ifndef VBOX_WITH_GUEST_CONTROL
1944 ReturnComNotImplemented();
1945#else
1946 LogFlowThisFuncEnter();
1947
1948 if (RT_UNLIKELY((aPath) == NULL || *(aPath) == '\0'))
1949 return setError(E_INVALIDARG, tr("No directory to create specified"));
1950
1951 AutoCaller autoCaller(this);
1952 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1953
1954 uint32_t fFlags = DirectoryCreateFlag_None;
1955 if (aFlags)
1956 {
1957 com::SafeArray<DirectoryCreateFlag_T> flags(ComSafeArrayInArg(aFlags));
1958 for (size_t i = 0; i < flags.size(); i++)
1959 fFlags |= flags[i];
1960
1961 if (fFlags)
1962 {
1963 if (!(fFlags & DirectoryCreateFlag_Parents))
1964 return setError(E_INVALIDARG, tr("Unknown flags (%#x)"), fFlags);
1965 }
1966 }
1967
1968 HRESULT hr = S_OK;
1969
1970 ComObjPtr <GuestDirectory> pDirectory; int guestRc;
1971 int rc = directoryCreateInternal(Utf8Str(aPath), (uint32_t)aMode, fFlags, &guestRc);
1972 if (RT_FAILURE(rc))
1973 {
1974 switch (rc)
1975 {
1976 case VERR_GSTCTL_GUEST_ERROR:
1977 hr = GuestProcess::setErrorExternal(this, guestRc);
1978 break;
1979
1980 case VERR_INVALID_PARAMETER:
1981 hr = setError(VBOX_E_IPRT_ERROR, tr("Directory creation failed: Invalid parameters given"));
1982 break;
1983
1984 case VERR_BROKEN_PIPE:
1985 hr = setError(VBOX_E_IPRT_ERROR, tr("Directory creation failed: Unexpectedly aborted"));
1986 break;
1987
1988 case VERR_CANT_CREATE:
1989 hr = setError(VBOX_E_IPRT_ERROR, tr("Directory creation failed: Could not create directory"));
1990 break;
1991
1992 default:
1993 hr = setError(VBOX_E_IPRT_ERROR, tr("Directory creation failed: %Rrc"), rc);
1994 break;
1995 }
1996 }
1997
1998 return hr;
1999#endif /* VBOX_WITH_GUEST_CONTROL */
2000}
2001
2002STDMETHODIMP GuestSession::DirectoryCreateTemp(IN_BSTR aTemplate, ULONG aMode, IN_BSTR aPath, BOOL aSecure, BSTR *aDirectory)
2003{
2004#ifndef VBOX_WITH_GUEST_CONTROL
2005 ReturnComNotImplemented();
2006#else
2007 LogFlowThisFuncEnter();
2008
2009 if (RT_UNLIKELY((aTemplate) == NULL || *(aTemplate) == '\0'))
2010 return setError(E_INVALIDARG, tr("No template specified"));
2011 if (RT_UNLIKELY((aPath) == NULL || *(aPath) == '\0'))
2012 return setError(E_INVALIDARG, tr("No directory name specified"));
2013 CheckComArgOutPointerValid(aDirectory);
2014
2015 AutoCaller autoCaller(this);
2016 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2017
2018 HRESULT hr = S_OK;
2019
2020 Utf8Str strName; int guestRc;
2021 int rc = objectCreateTempInternal(Utf8Str(aTemplate),
2022 Utf8Str(aPath),
2023 true /* Directory */, strName, &guestRc);
2024 if (RT_SUCCESS(rc))
2025 {
2026 strName.cloneTo(aDirectory);
2027 }
2028 else
2029 {
2030 switch (rc)
2031 {
2032 case VERR_GSTCTL_GUEST_ERROR:
2033 hr = GuestProcess::setErrorExternal(this, guestRc);
2034 break;
2035
2036 default:
2037 hr = setError(VBOX_E_IPRT_ERROR, tr("Temporary directory creation \"%s\" with template \"%s\" failed: %Rrc"),
2038 Utf8Str(aPath).c_str(), Utf8Str(aTemplate).c_str(), rc);
2039 break;
2040 }
2041 }
2042
2043 return hr;
2044#endif /* VBOX_WITH_GUEST_CONTROL */
2045}
2046
2047STDMETHODIMP GuestSession::DirectoryExists(IN_BSTR aPath, BOOL *aExists)
2048{
2049#ifndef VBOX_WITH_GUEST_CONTROL
2050 ReturnComNotImplemented();
2051#else
2052 LogFlowThisFuncEnter();
2053
2054 if (RT_UNLIKELY((aPath) == NULL || *(aPath) == '\0'))
2055 return setError(E_INVALIDARG, tr("No directory to check existence for specified"));
2056 CheckComArgOutPointerValid(aExists);
2057
2058 AutoCaller autoCaller(this);
2059 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2060
2061 HRESULT hr = S_OK;
2062
2063 GuestFsObjData objData; int guestRc;
2064 int rc = directoryQueryInfoInternal(Utf8Str(aPath), objData, &guestRc);
2065 if (RT_SUCCESS(rc))
2066 {
2067 *aExists = objData.mType == FsObjType_Directory;
2068 }
2069 else
2070 {
2071 switch (rc)
2072 {
2073 case VERR_GSTCTL_GUEST_ERROR:
2074 hr = GuestProcess::setErrorExternal(this, guestRc);
2075 break;
2076
2077 default:
2078 hr = setError(VBOX_E_IPRT_ERROR, tr("Querying directory existence \"%s\" failed: %Rrc"),
2079 Utf8Str(aPath).c_str(), rc);
2080 break;
2081 }
2082 }
2083
2084 return hr;
2085#endif /* VBOX_WITH_GUEST_CONTROL */
2086}
2087
2088STDMETHODIMP GuestSession::DirectoryOpen(IN_BSTR aPath, IN_BSTR aFilter, ComSafeArrayIn(DirectoryOpenFlag_T, aFlags), IGuestDirectory **aDirectory)
2089{
2090#ifndef VBOX_WITH_GUEST_CONTROL
2091 ReturnComNotImplemented();
2092#else
2093 LogFlowThisFuncEnter();
2094
2095 if (RT_UNLIKELY((aPath) == NULL || *(aPath) == '\0'))
2096 return setError(E_INVALIDARG, tr("No directory to open specified"));
2097 if (RT_UNLIKELY((aFilter) != NULL && *(aFilter) != '\0'))
2098 return setError(E_INVALIDARG, tr("Directory filters are not implemented yet"));
2099 CheckComArgOutPointerValid(aDirectory);
2100
2101 AutoCaller autoCaller(this);
2102 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2103
2104 uint32_t fFlags = DirectoryOpenFlag_None;
2105 if (aFlags)
2106 {
2107 com::SafeArray<DirectoryOpenFlag_T> flags(ComSafeArrayInArg(aFlags));
2108 for (size_t i = 0; i < flags.size(); i++)
2109 fFlags |= flags[i];
2110
2111 if (fFlags)
2112 return setError(E_INVALIDARG, tr("Open flags (%#x) not implemented yet"), fFlags);
2113 }
2114
2115 HRESULT hr = S_OK;
2116
2117 ComObjPtr <GuestDirectory> pDirectory;
2118 int rc = directoryOpenInternal(Utf8Str(aPath), Utf8Str(aFilter), fFlags, pDirectory);
2119 if (RT_SUCCESS(rc))
2120 {
2121 /* Return directory object to the caller. */
2122 hr = pDirectory.queryInterfaceTo(aDirectory);
2123 }
2124 else
2125 {
2126 switch (rc)
2127 {
2128 case VERR_INVALID_PARAMETER:
2129 hr = setError(VBOX_E_IPRT_ERROR, tr("Opening directory \"%s\" failed; invalid parameters given",
2130 Utf8Str(aPath).c_str()));
2131 break;
2132
2133 default:
2134 hr = setError(VBOX_E_IPRT_ERROR, tr("Opening directory \"%s\" failed: %Rrc"),
2135 Utf8Str(aPath).c_str(),rc);
2136 break;
2137 }
2138 }
2139
2140 return hr;
2141#endif /* VBOX_WITH_GUEST_CONTROL */
2142}
2143
2144STDMETHODIMP GuestSession::DirectoryQueryInfo(IN_BSTR aPath, IGuestFsObjInfo **aInfo)
2145{
2146#ifndef VBOX_WITH_GUEST_CONTROL
2147 ReturnComNotImplemented();
2148#else
2149 LogFlowThisFuncEnter();
2150
2151 if (RT_UNLIKELY((aPath) == NULL || *(aPath) == '\0'))
2152 return setError(E_INVALIDARG, tr("No directory to query information for specified"));
2153 CheckComArgOutPointerValid(aInfo);
2154
2155 AutoCaller autoCaller(this);
2156 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2157
2158 HRESULT hr = S_OK;
2159
2160 GuestFsObjData objData; int guestRc;
2161 int vrc = directoryQueryInfoInternal(Utf8Str(aPath), objData, &guestRc);
2162 if (RT_SUCCESS(vrc))
2163 {
2164 if (objData.mType == FsObjType_Directory)
2165 {
2166 ComObjPtr<GuestFsObjInfo> pFsObjInfo;
2167 hr = pFsObjInfo.createObject();
2168 if (FAILED(hr)) return hr;
2169
2170 vrc = pFsObjInfo->init(objData);
2171 if (RT_SUCCESS(vrc))
2172 {
2173 hr = pFsObjInfo.queryInterfaceTo(aInfo);
2174 if (FAILED(hr)) return hr;
2175 }
2176 }
2177 }
2178
2179 if (RT_FAILURE(vrc))
2180 {
2181 switch (vrc)
2182 {
2183 case VERR_GSTCTL_GUEST_ERROR:
2184 hr = GuestProcess::setErrorExternal(this, guestRc);
2185 break;
2186
2187 case VERR_NOT_A_DIRECTORY:
2188 hr = setError(VBOX_E_IPRT_ERROR, tr("Element \"%s\" exists but is not a directory",
2189 Utf8Str(aPath).c_str()));
2190 break;
2191
2192 default:
2193 hr = setError(VBOX_E_IPRT_ERROR, tr("Querying directory information for \"%s\" failed: %Rrc"),
2194 Utf8Str(aPath).c_str(), vrc);
2195 break;
2196 }
2197 }
2198
2199 return hr;
2200#endif /* VBOX_WITH_GUEST_CONTROL */
2201}
2202
2203STDMETHODIMP GuestSession::DirectoryRemove(IN_BSTR aPath)
2204{
2205#ifndef VBOX_WITH_GUEST_CONTROL
2206 ReturnComNotImplemented();
2207#else
2208 LogFlowThisFuncEnter();
2209
2210 AutoCaller autoCaller(this);
2211 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2212
2213 ReturnComNotImplemented();
2214#endif /* VBOX_WITH_GUEST_CONTROL */
2215}
2216
2217STDMETHODIMP GuestSession::DirectoryRemoveRecursive(IN_BSTR aPath, ComSafeArrayIn(DirectoryRemoveRecFlag_T, aFlags), IProgress **aProgress)
2218{
2219#ifndef VBOX_WITH_GUEST_CONTROL
2220 ReturnComNotImplemented();
2221#else
2222 LogFlowThisFuncEnter();
2223
2224 AutoCaller autoCaller(this);
2225 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2226
2227 ReturnComNotImplemented();
2228#endif /* VBOX_WITH_GUEST_CONTROL */
2229}
2230
2231STDMETHODIMP GuestSession::DirectoryRename(IN_BSTR aSource, IN_BSTR aDest, ComSafeArrayIn(PathRenameFlag_T, aFlags))
2232{
2233#ifndef VBOX_WITH_GUEST_CONTROL
2234 ReturnComNotImplemented();
2235#else
2236 LogFlowThisFuncEnter();
2237
2238 AutoCaller autoCaller(this);
2239 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2240
2241 ReturnComNotImplemented();
2242#endif /* VBOX_WITH_GUEST_CONTROL */
2243}
2244
2245STDMETHODIMP GuestSession::DirectorySetACL(IN_BSTR aPath, IN_BSTR aACL)
2246{
2247#ifndef VBOX_WITH_GUEST_CONTROL
2248 ReturnComNotImplemented();
2249#else
2250 LogFlowThisFuncEnter();
2251
2252 AutoCaller autoCaller(this);
2253 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2254
2255 ReturnComNotImplemented();
2256#endif /* VBOX_WITH_GUEST_CONTROL */
2257}
2258
2259STDMETHODIMP GuestSession::EnvironmentClear(void)
2260{
2261#ifndef VBOX_WITH_GUEST_CONTROL
2262 ReturnComNotImplemented();
2263#else
2264 LogFlowThisFuncEnter();
2265
2266 AutoCaller autoCaller(this);
2267 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2268
2269 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2270
2271 mData.mEnvironment.Clear();
2272
2273 LogFlowFuncLeaveRC(S_OK);
2274 return S_OK;
2275#endif /* VBOX_WITH_GUEST_CONTROL */
2276}
2277
2278STDMETHODIMP GuestSession::EnvironmentGet(IN_BSTR aName, BSTR *aValue)
2279{
2280#ifndef VBOX_WITH_GUEST_CONTROL
2281 ReturnComNotImplemented();
2282#else
2283 LogFlowThisFuncEnter();
2284
2285 if (RT_UNLIKELY((aName) == NULL || *(aName) == '\0'))
2286 return setError(E_INVALIDARG, tr("No value name specified"));
2287
2288 CheckComArgOutPointerValid(aValue);
2289
2290 AutoCaller autoCaller(this);
2291 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2292
2293 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2294
2295 Bstr strValue(mData.mEnvironment.Get(Utf8Str(aName)));
2296 strValue.cloneTo(aValue);
2297
2298 LogFlowFuncLeaveRC(S_OK);
2299 return S_OK;
2300#endif /* VBOX_WITH_GUEST_CONTROL */
2301}
2302
2303STDMETHODIMP GuestSession::EnvironmentSet(IN_BSTR aName, IN_BSTR aValue)
2304{
2305#ifndef VBOX_WITH_GUEST_CONTROL
2306 ReturnComNotImplemented();
2307#else
2308 LogFlowThisFuncEnter();
2309
2310 if (RT_UNLIKELY((aName) == NULL || *(aName) == '\0'))
2311 return setError(E_INVALIDARG, tr("No value name specified"));
2312
2313 AutoCaller autoCaller(this);
2314 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2315
2316 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2317
2318 int rc = mData.mEnvironment.Set(Utf8Str(aName), Utf8Str(aValue));
2319
2320 HRESULT hr = RT_SUCCESS(rc) ? S_OK : VBOX_E_IPRT_ERROR;
2321 LogFlowFuncLeaveRC(hr);
2322 return hr;
2323#endif /* VBOX_WITH_GUEST_CONTROL */
2324}
2325
2326STDMETHODIMP GuestSession::EnvironmentUnset(IN_BSTR aName)
2327{
2328#ifndef VBOX_WITH_GUEST_CONTROL
2329 ReturnComNotImplemented();
2330#else
2331 LogFlowThisFuncEnter();
2332
2333 AutoCaller autoCaller(this);
2334 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2335
2336 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2337
2338 mData.mEnvironment.Unset(Utf8Str(aName));
2339
2340 LogFlowFuncLeaveRC(S_OK);
2341 return S_OK;
2342#endif /* VBOX_WITH_GUEST_CONTROL */
2343}
2344
2345STDMETHODIMP GuestSession::FileCreateTemp(IN_BSTR aTemplate, ULONG aMode, IN_BSTR aPath, BOOL aSecure, IGuestFile **aFile)
2346{
2347#ifndef VBOX_WITH_GUEST_CONTROL
2348 ReturnComNotImplemented();
2349#else
2350 LogFlowThisFuncEnter();
2351
2352 AutoCaller autoCaller(this);
2353 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2354
2355 ReturnComNotImplemented();
2356#endif /* VBOX_WITH_GUEST_CONTROL */
2357}
2358
2359STDMETHODIMP GuestSession::FileExists(IN_BSTR aPath, BOOL *aExists)
2360{
2361#ifndef VBOX_WITH_GUEST_CONTROL
2362 ReturnComNotImplemented();
2363#else
2364 LogFlowThisFuncEnter();
2365
2366 if (RT_UNLIKELY((aPath) == NULL || *(aPath) == '\0'))
2367 return setError(E_INVALIDARG, tr("No file to check existence for specified"));
2368 CheckComArgOutPointerValid(aExists);
2369
2370 AutoCaller autoCaller(this);
2371 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2372
2373 GuestFsObjData objData; int guestRc;
2374 int vrc = fileQueryInfoInternal(Utf8Str(aPath), objData, &guestRc);
2375 if (RT_SUCCESS(vrc))
2376 {
2377 *aExists = TRUE;
2378 return S_OK;
2379 }
2380
2381 HRESULT hr = S_OK;
2382
2383 switch (vrc)
2384 {
2385 case VERR_GSTCTL_GUEST_ERROR:
2386 hr = GuestProcess::setErrorExternal(this, guestRc);
2387 break;
2388
2389 case VERR_NOT_A_FILE:
2390 *aExists = FALSE;
2391 break;
2392
2393 default:
2394 hr = setError(VBOX_E_IPRT_ERROR, tr("Querying file information for \"%s\" failed: %Rrc"),
2395 Utf8Str(aPath).c_str(), vrc);
2396 break;
2397 }
2398
2399 return hr;
2400#endif /* VBOX_WITH_GUEST_CONTROL */
2401}
2402
2403STDMETHODIMP GuestSession::FileRemove(IN_BSTR aPath)
2404{
2405#ifndef VBOX_WITH_GUEST_CONTROL
2406 ReturnComNotImplemented();
2407#else
2408 LogFlowThisFuncEnter();
2409
2410 if (RT_UNLIKELY((aPath) == NULL || *(aPath) == '\0'))
2411 return setError(E_INVALIDARG, tr("No file to remove specified"));
2412
2413 AutoCaller autoCaller(this);
2414 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2415
2416 HRESULT hr = S_OK;
2417
2418 int guestRc;
2419 int vrc = fileRemoveInternal(Utf8Str(aPath), &guestRc);
2420 if (RT_FAILURE(vrc))
2421 {
2422 switch (vrc)
2423 {
2424 case VERR_GSTCTL_GUEST_ERROR:
2425 hr = GuestProcess::setErrorExternal(this, guestRc);
2426 break;
2427
2428 default:
2429 hr = setError(VBOX_E_IPRT_ERROR, tr("Removing file \"%s\" failed: %Rrc"),
2430 Utf8Str(aPath).c_str(), vrc);
2431 break;
2432 }
2433 }
2434
2435 return hr;
2436#endif /* VBOX_WITH_GUEST_CONTROL */
2437}
2438
2439STDMETHODIMP GuestSession::FileOpen(IN_BSTR aPath, IN_BSTR aOpenMode, IN_BSTR aDisposition, ULONG aCreationMode, LONG64 aOffset, IGuestFile **aFile)
2440{
2441#ifndef VBOX_WITH_GUEST_CONTROL
2442 ReturnComNotImplemented();
2443#else
2444 LogFlowThisFuncEnter();
2445
2446 if (RT_UNLIKELY((aPath) == NULL || *(aPath) == '\0'))
2447 return setError(E_INVALIDARG, tr("No file to open specified"));
2448 if (RT_UNLIKELY((aOpenMode) == NULL || *(aOpenMode) == '\0'))
2449 return setError(E_INVALIDARG, tr("No open mode specified"));
2450 if (RT_UNLIKELY((aDisposition) == NULL || *(aDisposition) == '\0'))
2451 return setError(E_INVALIDARG, tr("No disposition mode specified"));
2452
2453 CheckComArgOutPointerValid(aFile);
2454
2455 AutoCaller autoCaller(this);
2456 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2457
2458 /** @todo Validate open mode. */
2459 /** @todo Validate disposition mode. */
2460
2461 /** @todo Validate creation mode. */
2462 uint32_t uCreationMode = 0;
2463
2464 HRESULT hr = S_OK;
2465
2466 GuestFileOpenInfo openInfo;
2467 openInfo.mFileName = Utf8Str(aPath);
2468 openInfo.mOpenMode = Utf8Str(aOpenMode);
2469 openInfo.mDisposition = Utf8Str(aDisposition);
2470 openInfo.mCreationMode = aCreationMode;
2471 openInfo.mInitialOffset = aOffset;
2472
2473 ComObjPtr <GuestFile> pFile; int guestRc;
2474 int vrc = fileOpenInternal(openInfo, pFile, &guestRc);
2475 if (RT_SUCCESS(vrc))
2476 {
2477 /* Return directory object to the caller. */
2478 hr = pFile.queryInterfaceTo(aFile);
2479 }
2480 else
2481 {
2482 switch (vrc)
2483 {
2484 case VERR_GSTCTL_GUEST_ERROR:
2485 hr = GuestProcess::setErrorExternal(this, guestRc);
2486 break;
2487
2488 default:
2489 hr = setError(VBOX_E_IPRT_ERROR, tr("Opening file \"%s\" failed: %Rrc"),
2490 Utf8Str(aPath).c_str(), vrc);
2491 break;
2492 }
2493 }
2494
2495 return hr;
2496#endif /* VBOX_WITH_GUEST_CONTROL */
2497}
2498
2499STDMETHODIMP GuestSession::FileQueryInfo(IN_BSTR aPath, IGuestFsObjInfo **aInfo)
2500{
2501#ifndef VBOX_WITH_GUEST_CONTROL
2502 ReturnComNotImplemented();
2503#else
2504 LogFlowThisFuncEnter();
2505
2506 if (RT_UNLIKELY((aPath) == NULL || *(aPath) == '\0'))
2507 return setError(E_INVALIDARG, tr("No file to query information for specified"));
2508 CheckComArgOutPointerValid(aInfo);
2509
2510 AutoCaller autoCaller(this);
2511 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2512
2513 HRESULT hr = S_OK;
2514
2515 GuestFsObjData objData; int guestRc;
2516 int vrc = fileQueryInfoInternal(Utf8Str(aPath), objData, &guestRc);
2517 if (RT_SUCCESS(vrc))
2518 {
2519 ComObjPtr<GuestFsObjInfo> pFsObjInfo;
2520 hr = pFsObjInfo.createObject();
2521 if (FAILED(hr)) return hr;
2522
2523 vrc = pFsObjInfo->init(objData);
2524 if (RT_SUCCESS(vrc))
2525 {
2526 hr = pFsObjInfo.queryInterfaceTo(aInfo);
2527 if (FAILED(hr)) return hr;
2528 }
2529 }
2530
2531 if (RT_FAILURE(vrc))
2532 {
2533 switch (vrc)
2534 {
2535 case VERR_GSTCTL_GUEST_ERROR:
2536 hr = GuestProcess::setErrorExternal(this, guestRc);
2537 break;
2538
2539 case VERR_NOT_A_FILE:
2540 hr = setError(VBOX_E_IPRT_ERROR, tr("Element exists but is not a file"));
2541 break;
2542
2543 default:
2544 hr = setError(VBOX_E_IPRT_ERROR, tr("Querying file information failed: %Rrc"), vrc);
2545 break;
2546 }
2547 }
2548
2549 return hr;
2550#endif /* VBOX_WITH_GUEST_CONTROL */
2551}
2552
2553STDMETHODIMP GuestSession::FileQuerySize(IN_BSTR aPath, LONG64 *aSize)
2554{
2555#ifndef VBOX_WITH_GUEST_CONTROL
2556 ReturnComNotImplemented();
2557#else
2558 LogFlowThisFuncEnter();
2559
2560 if (RT_UNLIKELY((aPath) == NULL || *(aPath) == '\0'))
2561 return setError(E_INVALIDARG, tr("No file to query size for specified"));
2562 CheckComArgOutPointerValid(aSize);
2563
2564 AutoCaller autoCaller(this);
2565 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2566
2567 HRESULT hr = S_OK;
2568
2569 int64_t llSize; int guestRc;
2570 int vrc = fileQuerySizeInternal(Utf8Str(aPath), &llSize, &guestRc);
2571 if (RT_SUCCESS(vrc))
2572 {
2573 *aSize = llSize;
2574 }
2575 else
2576 {
2577 switch (vrc)
2578 {
2579 case VERR_GSTCTL_GUEST_ERROR:
2580 hr = GuestProcess::setErrorExternal(this, guestRc);
2581 break;
2582
2583 default:
2584 hr = setError(VBOX_E_IPRT_ERROR, tr("Querying file size failed: %Rrc"), vrc);
2585 break;
2586 }
2587 }
2588
2589 return hr;
2590#endif /* VBOX_WITH_GUEST_CONTROL */
2591}
2592
2593STDMETHODIMP GuestSession::FileRename(IN_BSTR aSource, IN_BSTR aDest, ComSafeArrayIn(PathRenameFlag_T, aFlags))
2594{
2595#ifndef VBOX_WITH_GUEST_CONTROL
2596 ReturnComNotImplemented();
2597#else
2598 LogFlowThisFuncEnter();
2599
2600 AutoCaller autoCaller(this);
2601 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2602
2603 ReturnComNotImplemented();
2604#endif /* VBOX_WITH_GUEST_CONTROL */
2605}
2606
2607STDMETHODIMP GuestSession::FileSetACL(IN_BSTR aPath, IN_BSTR aACL)
2608{
2609#ifndef VBOX_WITH_GUEST_CONTROL
2610 ReturnComNotImplemented();
2611#else
2612 LogFlowThisFuncEnter();
2613
2614 AutoCaller autoCaller(this);
2615 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2616
2617 ReturnComNotImplemented();
2618#endif /* VBOX_WITH_GUEST_CONTROL */
2619}
2620
2621STDMETHODIMP GuestSession::ProcessCreate(IN_BSTR aCommand, ComSafeArrayIn(IN_BSTR, aArguments), ComSafeArrayIn(IN_BSTR, aEnvironment),
2622 ComSafeArrayIn(ProcessCreateFlag_T, aFlags), ULONG aTimeoutMS, IGuestProcess **aProcess)
2623{
2624#ifndef VBOX_WITH_GUEST_CONTROL
2625 ReturnComNotImplemented();
2626#else
2627 LogFlowThisFuncEnter();
2628
2629 com::SafeArray<LONG> affinity;
2630
2631 return ProcessCreateEx(aCommand, ComSafeArrayInArg(aArguments), ComSafeArrayInArg(aEnvironment),
2632 ComSafeArrayInArg(aFlags), aTimeoutMS, ProcessPriority_Default, ComSafeArrayAsInParam(affinity), aProcess);
2633#endif /* VBOX_WITH_GUEST_CONTROL */
2634}
2635
2636STDMETHODIMP GuestSession::ProcessCreateEx(IN_BSTR aCommand, ComSafeArrayIn(IN_BSTR, aArguments), ComSafeArrayIn(IN_BSTR, aEnvironment),
2637 ComSafeArrayIn(ProcessCreateFlag_T, aFlags), ULONG aTimeoutMS,
2638 ProcessPriority_T aPriority, ComSafeArrayIn(LONG, aAffinity),
2639 IGuestProcess **aProcess)
2640{
2641#ifndef VBOX_WITH_GUEST_CONTROL
2642 ReturnComNotImplemented();
2643#else
2644 LogFlowThisFuncEnter();
2645
2646 if (RT_UNLIKELY((aCommand) == NULL || *(aCommand) == '\0'))
2647 return setError(E_INVALIDARG, tr("No command to execute specified"));
2648 CheckComArgOutPointerValid(aProcess);
2649
2650 AutoCaller autoCaller(this);
2651 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2652
2653 GuestProcessStartupInfo procInfo;
2654 procInfo.mCommand = Utf8Str(aCommand);
2655
2656 if (aArguments)
2657 {
2658 com::SafeArray<IN_BSTR> arguments(ComSafeArrayInArg(aArguments));
2659 for (size_t i = 0; i < arguments.size(); i++)
2660 procInfo.mArguments.push_back(Utf8Str(arguments[i]));
2661 }
2662
2663 int rc = VINF_SUCCESS;
2664
2665 /*
2666 * Create the process environment:
2667 * - Apply the session environment in a first step, and
2668 * - Apply environment variables specified by this call to
2669 * have the chance of overwriting/deleting session entries.
2670 */
2671 procInfo.mEnvironment = mData.mEnvironment; /* Apply original session environment. */
2672
2673 if (aEnvironment)
2674 {
2675 com::SafeArray<IN_BSTR> environment(ComSafeArrayInArg(aEnvironment));
2676 for (size_t i = 0; i < environment.size() && RT_SUCCESS(rc); i++)
2677 rc = procInfo.mEnvironment.Set(Utf8Str(environment[i]));
2678 }
2679
2680 HRESULT hr = S_OK;
2681
2682 if (RT_SUCCESS(rc))
2683 {
2684 if (aFlags)
2685 {
2686 com::SafeArray<ProcessCreateFlag_T> flags(ComSafeArrayInArg(aFlags));
2687 for (size_t i = 0; i < flags.size(); i++)
2688 procInfo.mFlags |= flags[i];
2689 }
2690
2691 procInfo.mTimeoutMS = aTimeoutMS;
2692
2693 if (aAffinity)
2694 {
2695 com::SafeArray<LONG> affinity(ComSafeArrayInArg(aAffinity));
2696 for (size_t i = 0; i < affinity.size(); i++)
2697 {
2698 if (affinity[i])
2699 procInfo.mAffinity |= (uint64_t)1 << i;
2700 }
2701 }
2702
2703 procInfo.mPriority = aPriority;
2704
2705 ComObjPtr<GuestProcess> pProcess;
2706 rc = processCreateExInteral(procInfo, pProcess);
2707 if (RT_SUCCESS(rc))
2708 {
2709 /* Return guest session to the caller. */
2710 HRESULT hr2 = pProcess.queryInterfaceTo(aProcess);
2711 if (FAILED(hr2))
2712 rc = VERR_COM_OBJECT_NOT_FOUND;
2713
2714 if (RT_SUCCESS(rc))
2715 rc = pProcess->startProcessAsync();
2716 }
2717 }
2718
2719 if (RT_FAILURE(rc))
2720 {
2721 switch (rc)
2722 {
2723 case VERR_MAX_PROCS_REACHED:
2724 hr = setError(VBOX_E_IPRT_ERROR, tr("Maximum number of guest objects per session (%ld) reached"),
2725 VBOX_GUESTCTRL_MAX_OBJECTS);
2726 break;
2727
2728 /** @todo Add more errors here. */
2729
2730 default:
2731 hr = setError(VBOX_E_IPRT_ERROR, tr("Could not create guest process, rc=%Rrc"), rc);
2732 break;
2733 }
2734 }
2735
2736 LogFlowFuncLeaveRC(rc);
2737 return hr;
2738#endif /* VBOX_WITH_GUEST_CONTROL */
2739}
2740
2741STDMETHODIMP GuestSession::ProcessGet(ULONG aPID, IGuestProcess **aProcess)
2742{
2743#ifndef VBOX_WITH_GUEST_CONTROL
2744 ReturnComNotImplemented();
2745#else
2746 LogFlowThisFunc(("aPID=%RU32\n", aPID));
2747
2748 CheckComArgOutPointerValid(aProcess);
2749 if (aPID == 0)
2750 return setError(E_INVALIDARG, tr("No valid process ID (PID) specified"));
2751
2752 AutoCaller autoCaller(this);
2753 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2754
2755 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2756
2757 HRESULT hr = S_OK;
2758
2759 ComObjPtr<GuestProcess> pProcess;
2760 int rc = processGetByPID(aPID, &pProcess);
2761 if (RT_FAILURE(rc))
2762 hr = setError(E_INVALIDARG, tr("No process with PID %RU32 found"), aPID);
2763
2764 /* This will set (*aProcess) to NULL if pProgress is NULL. */
2765 HRESULT hr2 = pProcess.queryInterfaceTo(aProcess);
2766 if (SUCCEEDED(hr))
2767 hr = hr2;
2768
2769 LogFlowThisFunc(("aProcess=%p, hr=%Rhrc\n", *aProcess, hr));
2770 return hr;
2771#endif /* VBOX_WITH_GUEST_CONTROL */
2772}
2773
2774STDMETHODIMP GuestSession::SymlinkCreate(IN_BSTR aSource, IN_BSTR aTarget, SymlinkType_T aType)
2775{
2776#ifndef VBOX_WITH_GUEST_CONTROL
2777 ReturnComNotImplemented();
2778#else
2779 LogFlowThisFuncEnter();
2780
2781 AutoCaller autoCaller(this);
2782 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2783
2784 ReturnComNotImplemented();
2785#endif /* VBOX_WITH_GUEST_CONTROL */
2786}
2787
2788STDMETHODIMP GuestSession::SymlinkExists(IN_BSTR aSymlink, BOOL *aExists)
2789{
2790#ifndef VBOX_WITH_GUEST_CONTROL
2791 ReturnComNotImplemented();
2792#else
2793 LogFlowThisFuncEnter();
2794
2795 AutoCaller autoCaller(this);
2796 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2797
2798 ReturnComNotImplemented();
2799#endif /* VBOX_WITH_GUEST_CONTROL */
2800}
2801
2802STDMETHODIMP GuestSession::SymlinkRead(IN_BSTR aSymlink, ComSafeArrayIn(SymlinkReadFlag_T, aFlags), BSTR *aTarget)
2803{
2804#ifndef VBOX_WITH_GUEST_CONTROL
2805 ReturnComNotImplemented();
2806#else
2807 LogFlowThisFuncEnter();
2808
2809 AutoCaller autoCaller(this);
2810 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2811
2812 ReturnComNotImplemented();
2813#endif /* VBOX_WITH_GUEST_CONTROL */
2814}
2815
2816STDMETHODIMP GuestSession::SymlinkRemoveDirectory(IN_BSTR aPath)
2817{
2818#ifndef VBOX_WITH_GUEST_CONTROL
2819 ReturnComNotImplemented();
2820#else
2821 LogFlowThisFuncEnter();
2822
2823 AutoCaller autoCaller(this);
2824 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2825
2826 ReturnComNotImplemented();
2827#endif /* VBOX_WITH_GUEST_CONTROL */
2828}
2829
2830STDMETHODIMP GuestSession::SymlinkRemoveFile(IN_BSTR aFile)
2831{
2832#ifndef VBOX_WITH_GUEST_CONTROL
2833 ReturnComNotImplemented();
2834#else
2835 LogFlowThisFuncEnter();
2836
2837 AutoCaller autoCaller(this);
2838 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2839
2840 ReturnComNotImplemented();
2841#endif /* VBOX_WITH_GUEST_CONTROL */
2842}
2843
2844STDMETHODIMP GuestSession::WaitFor(ULONG aWaitFlags, ULONG aTimeoutMS, GuestSessionWaitResult_T *aReason)
2845{
2846#ifndef VBOX_WITH_GUEST_CONTROL
2847 ReturnComNotImplemented();
2848#else
2849 LogFlowThisFuncEnter();
2850
2851 CheckComArgOutPointerValid(aReason);
2852
2853 AutoCaller autoCaller(this);
2854 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2855
2856 /*
2857 * Note: Do not hold any locks here while waiting!
2858 */
2859 HRESULT hr = S_OK;
2860
2861 int guestRc; GuestSessionWaitResult_T waitResult;
2862 int vrc = waitFor(aWaitFlags, aTimeoutMS, waitResult, &guestRc);
2863 if (RT_SUCCESS(vrc))
2864 {
2865 *aReason = waitResult;
2866 }
2867 else
2868 {
2869 switch (vrc)
2870 {
2871 case VERR_GSTCTL_GUEST_ERROR:
2872 hr = GuestSession::setErrorExternal(this, guestRc);
2873 break;
2874
2875 case VERR_TIMEOUT:
2876 *aReason = GuestSessionWaitResult_Timeout;
2877 break;
2878
2879 default:
2880 {
2881 const char *pszSessionName = mData.mSession.mName.c_str();
2882 hr = setError(VBOX_E_IPRT_ERROR,
2883 tr("Waiting for guest session \"%s\" failed: %Rrc"),
2884 pszSessionName ? pszSessionName : tr("Unnamed"), vrc);
2885 break;
2886 }
2887 }
2888 }
2889
2890 LogFlowFuncLeaveRC(vrc);
2891 return hr;
2892#endif /* VBOX_WITH_GUEST_CONTROL */
2893}
2894
2895STDMETHODIMP GuestSession::WaitForArray(ComSafeArrayIn(GuestSessionWaitForFlag_T, aFlags), ULONG aTimeoutMS, GuestSessionWaitResult_T *aReason)
2896{
2897#ifndef VBOX_WITH_GUEST_CONTROL
2898 ReturnComNotImplemented();
2899#else
2900 LogFlowThisFuncEnter();
2901
2902 CheckComArgOutPointerValid(aReason);
2903
2904 AutoCaller autoCaller(this);
2905 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2906
2907 /*
2908 * Note: Do not hold any locks here while waiting!
2909 */
2910 uint32_t fWaitFor = GuestSessionWaitForFlag_None;
2911 com::SafeArray<GuestSessionWaitForFlag_T> flags(ComSafeArrayInArg(aFlags));
2912 for (size_t i = 0; i < flags.size(); i++)
2913 fWaitFor |= flags[i];
2914
2915 return WaitFor(fWaitFor, aTimeoutMS, aReason);
2916#endif /* VBOX_WITH_GUEST_CONTROL */
2917}
2918
Note: See TracBrowser for help on using the repository browser.

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