VirtualBox

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

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

GuestCtrl: More stuff for IGuestFile and IGuestSession handling (work in progress).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 84.6 KB
Line 
1
2/* $Id: GuestSessionImpl.cpp 45109 2013-03-20 16:41:00Z 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 int guestRc;
971 rc = pFile->openFile(&guestRc);
972 if (RT_SUCCESS(rc))
973 {
974 /* Add the created file to our vector. */
975 mData.mFiles[uNewFileID] = pFile;
976 mData.mNumObjects++;
977 Assert(mData.mNumObjects <= VBOX_GUESTCTRL_MAX_OBJECTS);
978
979 LogFlowFunc(("Added new file \"%s\" (Session: %RU32) (now total %ld files, %ld objects)\n",
980 openInfo.mFileName.c_str(), mData.mSession.mID, mData.mFiles.size(), mData.mNumObjects));
981 }
982
983 if (pGuestRc)
984 *pGuestRc = guestRc;
985
986 LogFlowFuncLeaveRC(rc);
987 return rc;
988}
989
990int GuestSession::fileQueryInfoInternal(const Utf8Str &strPath, GuestFsObjData &objData, int *pGuestRc)
991{
992 LogFlowThisFunc(("strPath=%s\n", strPath.c_str()));
993
994 int vrc = fsQueryInfoInternal(strPath, objData, pGuestRc);
995 if (RT_SUCCESS(vrc))
996 {
997 vrc = objData.mType == FsObjType_File
998 ? VINF_SUCCESS : VERR_NOT_A_FILE;
999 }
1000
1001 LogFlowFuncLeaveRC(vrc);
1002 return vrc;
1003}
1004
1005int GuestSession::fileQuerySizeInternal(const Utf8Str &strPath, int64_t *pllSize, int *pGuestRc)
1006{
1007 AssertPtrReturn(pllSize, VERR_INVALID_POINTER);
1008
1009 GuestFsObjData objData;
1010 int vrc = fileQueryInfoInternal(strPath, objData, pGuestRc);
1011 if (RT_SUCCESS(vrc))
1012 *pllSize = objData.mObjectSize;
1013
1014 return vrc;
1015}
1016
1017int GuestSession::fsQueryInfoInternal(const Utf8Str &strPath, GuestFsObjData &objData, int *pGuestRc)
1018{
1019 LogFlowThisFunc(("strPath=%s\n", strPath.c_str()));
1020
1021 /** @todo Merge this with IGuestFile::queryInfo(). */
1022 GuestProcessStartupInfo procInfo;
1023 procInfo.mCommand = Utf8Str(VBOXSERVICE_TOOL_STAT);
1024 procInfo.mFlags = ProcessCreateFlag_WaitForStdOut;
1025
1026 /* Construct arguments. */
1027 procInfo.mArguments.push_back(Utf8Str("--machinereadable"));
1028 procInfo.mArguments.push_back(strPath);
1029
1030 GuestProcessTool procTool; int guestRc;
1031 int vrc = procTool.Init(this, procInfo, false /* Async */, &guestRc);
1032 if (RT_SUCCESS(vrc))
1033 vrc = procTool.Wait(GUESTPROCESSTOOL_FLAG_NONE, &guestRc);
1034 if (RT_SUCCESS(vrc))
1035 {
1036 guestRc = procTool.TerminatedOk(NULL /* Exit code */);
1037 if (RT_SUCCESS(guestRc))
1038 {
1039 GuestProcessStreamBlock curBlock;
1040 vrc = procTool.GetCurrentBlock(OUTPUT_HANDLE_ID_STDOUT, curBlock);
1041 /** @todo Check for more / validate blocks! */
1042 if (RT_SUCCESS(vrc))
1043 vrc = objData.FromStat(curBlock);
1044 }
1045 }
1046
1047 if ( vrc == VERR_GSTCTL_GUEST_ERROR
1048 && pGuestRc)
1049 {
1050 *pGuestRc = guestRc;
1051 }
1052
1053 LogFlowFuncLeaveRC(vrc);
1054 return vrc;
1055}
1056
1057const GuestCredentials& GuestSession::getCredentials(void)
1058{
1059 return mData.mCredentials;
1060}
1061
1062const GuestEnvironment& GuestSession::getEnvironment(void)
1063{
1064 return mData.mEnvironment;
1065}
1066
1067Utf8Str GuestSession::getName(void)
1068{
1069 return mData.mSession.mName;
1070}
1071
1072/* static */
1073Utf8Str GuestSession::guestErrorToString(int guestRc)
1074{
1075 Utf8Str strError;
1076
1077 /** @todo pData->u32Flags: int vs. uint32 -- IPRT errors are *negative* !!! */
1078 switch (guestRc)
1079 {
1080 case VERR_INVALID_VM_HANDLE:
1081 strError += Utf8StrFmt(tr("VMM device is not available (is the VM running?)"));
1082 break;
1083
1084 case VERR_HGCM_SERVICE_NOT_FOUND:
1085 strError += Utf8StrFmt(tr("The guest execution service is not available"));
1086 break;
1087
1088 case VERR_AUTHENTICATION_FAILURE:
1089 strError += Utf8StrFmt(tr("The specified user was not able to logon on guest"));
1090 break;
1091
1092 case VERR_TIMEOUT:
1093 strError += Utf8StrFmt(tr("The guest did not respond within time"));
1094 break;
1095
1096 case VERR_CANCELLED:
1097 strError += Utf8StrFmt(tr("The session operation was canceled"));
1098 break;
1099
1100 case VERR_PERMISSION_DENIED:
1101 strError += Utf8StrFmt(tr("Invalid user/password credentials"));
1102 break;
1103
1104 case VERR_MAX_PROCS_REACHED:
1105 strError += Utf8StrFmt(tr("Maximum number of parallel guest processes has been reached"));
1106 break;
1107
1108 case VERR_NOT_EQUAL: /** @todo Imprecise to the user; can mean anything and all. */
1109 strError += Utf8StrFmt(tr("Unable to retrieve requested information"));
1110 break;
1111
1112 case VERR_NOT_FOUND:
1113 strError += Utf8StrFmt(tr("The guest execution service is not ready (yet)"));
1114 break;
1115
1116 default:
1117 strError += Utf8StrFmt("%Rrc", guestRc);
1118 break;
1119 }
1120
1121 return strError;
1122}
1123
1124/** No locking! */
1125int GuestSession::onSessionStatusChange(PVBOXGUESTCTRLHOSTCBCTX pCbCtx,
1126 GuestCtrlCallback *pCallback, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData)
1127{
1128 /* pCallback is optional. */
1129 AssertPtrReturn(pSvcCbData, VERR_INVALID_POINTER);
1130
1131 if (pSvcCbData->mParms < 3)
1132 return VERR_INVALID_PARAMETER;
1133
1134 CALLBACKDATA_SESSION_NOTIFY dataCb;
1135 /* pSvcCb->mpaParms[0] always contains the context ID. */
1136 pSvcCbData->mpaParms[1].getUInt32(&dataCb.uType);
1137 pSvcCbData->mpaParms[2].getUInt32(&dataCb.uResult);
1138
1139 LogFlowThisFunc(("ID=%RU32, uType=%RU32, rc=%Rrc, pCallback=%p, pData=%p\n",
1140 mData.mSession.mID, dataCb.uType, dataCb.uResult, pCallback, pSvcCbData));
1141
1142 int vrc = VINF_SUCCESS;
1143
1144 int guestRc = dataCb.uResult; /** @todo uint32_t vs. int. */
1145 switch (dataCb.uType)
1146 {
1147 case GUEST_SESSION_NOTIFYTYPE_ERROR:
1148 mData.mStatus = GuestSessionStatus_Error;
1149 break;
1150
1151 case GUEST_SESSION_NOTIFYTYPE_STARTED:
1152 mData.mStatus = GuestSessionStatus_Started;
1153 break;
1154
1155 case GUEST_SESSION_NOTIFYTYPE_TEN:
1156 case GUEST_SESSION_NOTIFYTYPE_TES:
1157 case GUEST_SESSION_NOTIFYTYPE_TEA:
1158 mData.mStatus = GuestSessionStatus_Terminated;
1159 break;
1160
1161 case GUEST_SESSION_NOTIFYTYPE_TOK:
1162 mData.mStatus = GuestSessionStatus_TimedOutKilled;
1163 break;
1164
1165 case GUEST_SESSION_NOTIFYTYPE_TOA:
1166 mData.mStatus = GuestSessionStatus_TimedOutAbnormally;
1167 break;
1168
1169 case GUEST_SESSION_NOTIFYTYPE_DWN:
1170 mData.mStatus = GuestSessionStatus_Down;
1171 break;
1172
1173 default:
1174 vrc = VERR_NOT_SUPPORTED;
1175 break;
1176 }
1177
1178 if (RT_SUCCESS(vrc))
1179 {
1180 if (RT_FAILURE(guestRc))
1181 mData.mStatus = GuestSessionStatus_Error;
1182 }
1183 else if (vrc == VERR_NOT_SUPPORTED)
1184 {
1185 /* Also let the callback know. */
1186 guestRc = VERR_NOT_SUPPORTED;
1187 }
1188
1189 /*
1190 * Now do the signalling stuff.
1191 */
1192 if (pCallback)
1193 {
1194 int rc2 = pCallback->SetData(&dataCb, sizeof(dataCb));
1195 AssertRC(rc2);
1196 rc2 = pCallback->Signal(guestRc);
1197 AssertRC(rc2);
1198 }
1199
1200 LogFlowThisFunc(("ID=%RU32, guestRc=%Rrc\n",
1201 mData.mSession.mID, guestRc));
1202
1203 LogFlowFuncLeaveRC(vrc);
1204 return vrc;
1205}
1206
1207int GuestSession::startSessionIntenal(int *pGuestRc)
1208{
1209 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1210
1211 LogFlowThisFunc(("uProtocolVersion=%RU32, openFlags=%x, openTimeoutMS=%RU32\n",
1212 mData.mProtocolVersion, mData.mSession.mOpenFlags, mData.mSession.mOpenTimeoutMS));
1213
1214 /* Legacy Guest Additions don't support opening dedicated
1215 guest sessions. Simply return success here. */
1216 if (mData.mProtocolVersion < 2)
1217 {
1218 mData.mStatus = GuestSessionStatus_Started;
1219
1220 LogFlowThisFunc(("Installed Guest Additions don't support opening dedicated sessions, skipping\n"));
1221 return VINF_SUCCESS;
1222 }
1223
1224 /** @todo mData.mSession.uFlags validation. */
1225
1226 /* Set current session status. */
1227 mData.mStatus = GuestSessionStatus_Starting;
1228
1229 /* Destroy a pending callback request. */
1230 mData.mCallback.Destroy();
1231
1232 int vrc = mData.mCallback.Init(CALLBACKTYPE_SESSION_NOTIFY);
1233
1234 alock.release(); /* Drop the write lock again. */
1235
1236 if (RT_SUCCESS(vrc))
1237 {
1238 uint32_t uContextID =
1239 VBOX_GUESTCTRL_CONTEXTID_MAKE(mData.mSession.mID /* Session ID */,
1240 0 /* Object */, 0 /* Count */);
1241
1242 VBOXHGCMSVCPARM paParms[8];
1243
1244 int i = 0;
1245 paParms[i++].setUInt32(uContextID);
1246 paParms[i++].setUInt32(mData.mProtocolVersion);
1247 paParms[i++].setPointer((void*)mData.mCredentials.mUser.c_str(),
1248 (ULONG)mData.mCredentials.mUser.length() + 1);
1249 paParms[i++].setPointer((void*)mData.mCredentials.mPassword.c_str(),
1250 (ULONG)mData.mCredentials.mPassword.length() + 1);
1251 paParms[i++].setPointer((void*)mData.mCredentials.mDomain.c_str(),
1252 (ULONG)mData.mCredentials.mDomain.length() + 1);
1253 paParms[i++].setUInt32(mData.mSession.mOpenFlags);
1254
1255 vrc = sendCommand(HOST_SESSION_CREATE, i, paParms);
1256 }
1257
1258 if (RT_SUCCESS(vrc))
1259 {
1260 /*
1261 * Let's wait for the process being started.
1262 * Note: Be sure not keeping a AutoRead/WriteLock here.
1263 */
1264 LogFlowThisFunc(("Waiting for callback (%RU32ms) ...\n", mData.mSession.mOpenTimeoutMS));
1265 vrc = mData.mCallback.Wait(mData.mSession.mOpenTimeoutMS);
1266 if (RT_SUCCESS(vrc)) /* Wait was successful, check for supplied information. */
1267 {
1268 int guestRc = mData.mCallback.GetResultCode();
1269 if (RT_SUCCESS(guestRc))
1270 {
1271 /* Nothing to do here right now. */
1272 }
1273 else
1274 vrc = VERR_GSTCTL_GUEST_ERROR;
1275
1276 if (pGuestRc)
1277 *pGuestRc = guestRc;
1278 LogFlowThisFunc(("Callback returned rc=%Rrc\n", guestRc));
1279 }
1280 }
1281
1282 alock.acquire();
1283
1284 /* Destroy callback. */
1285 mData.mCallback.Destroy();
1286
1287 LogFlowFuncLeaveRC(vrc);
1288 return vrc;
1289}
1290
1291int GuestSession::startSessionAsync(void)
1292{
1293 LogFlowThisFuncEnter();
1294
1295 int vrc;
1296
1297 try
1298 {
1299 /* Asynchronously open the session on the guest by kicking off a
1300 * worker thread. */
1301 std::auto_ptr<GuestSessionTaskInternalOpen> pTask(new GuestSessionTaskInternalOpen(this));
1302 AssertReturn(pTask->isOk(), pTask->rc());
1303
1304 vrc = RTThreadCreate(NULL, GuestSession::startSessionThread,
1305 (void *)pTask.get(), 0,
1306 RTTHREADTYPE_MAIN_WORKER, 0,
1307 "gctlSesStart");
1308 if (RT_SUCCESS(vrc))
1309 {
1310 /* pTask is now owned by openSessionThread(), so release it. */
1311 pTask.release();
1312 }
1313 }
1314 catch(std::bad_alloc &)
1315 {
1316 vrc = VERR_NO_MEMORY;
1317 }
1318
1319 LogFlowFuncLeaveRC(vrc);
1320 return vrc;
1321}
1322
1323/* static */
1324DECLCALLBACK(int) GuestSession::startSessionThread(RTTHREAD Thread, void *pvUser)
1325{
1326 LogFlowFunc(("pvUser=%p\n", pvUser));
1327
1328 std::auto_ptr<GuestSessionTaskInternalOpen> pTask(static_cast<GuestSessionTaskInternalOpen*>(pvUser));
1329 AssertPtr(pTask.get());
1330
1331 const ComObjPtr<GuestSession> pSession(pTask->Session());
1332 Assert(!pSession.isNull());
1333
1334 AutoCaller autoCaller(pSession);
1335 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1336
1337 int vrc = pSession->startSessionIntenal(NULL /* Guest rc, ignored */);
1338 /* Nothing to do here anymore. */
1339
1340 LogFlowFuncLeaveRC(vrc);
1341 return vrc;
1342}
1343
1344int GuestSession::processRemoveFromList(GuestProcess *pProcess)
1345{
1346 LogFlowThisFuncEnter();
1347
1348 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1349
1350 int rc = VERR_NOT_FOUND;
1351
1352 ULONG uPID;
1353 HRESULT hr = pProcess->COMGETTER(PID)(&uPID);
1354
1355 LogFlowFunc(("Closing process (PID=%RU32) ...\n", uPID));
1356
1357 for (SessionProcesses::iterator itProcs = mData.mProcesses.begin();
1358 itProcs != mData.mProcesses.end(); ++itProcs)
1359 {
1360 if (pProcess == itProcs->second)
1361 {
1362 GuestProcess *pCurProc = itProcs->second;
1363 AssertPtr(pCurProc);
1364
1365 hr = pCurProc->COMGETTER(PID)(&uPID);
1366 ComAssertComRC(hr);
1367
1368 Assert(mData.mNumObjects);
1369 LogFlowFunc(("Removing process (Session: %RU32) with process ID=%RU32, guest PID=%RU32 (now total %ld processes, %ld objects)\n",
1370 mData.mSession.mID, pCurProc->getObjectID(), uPID, mData.mProcesses.size() - 1, mData.mNumObjects - 1));
1371
1372 mData.mProcesses.erase(itProcs);
1373 mData.mNumObjects--;
1374
1375 rc = VINF_SUCCESS;
1376 break;
1377 }
1378 }
1379
1380 LogFlowFuncLeaveRC(rc);
1381 return rc;
1382}
1383
1384/**
1385 * Creates but does *not* start the process yet. See GuestProcess::startProcess() or
1386 * GuestProcess::startProcessAsync() for that.
1387 *
1388 * @return IPRT status code.
1389 * @param procInfo
1390 * @param pProcess
1391 */
1392int GuestSession::processCreateExInteral(GuestProcessStartupInfo &procInfo, ComObjPtr<GuestProcess> &pProcess)
1393{
1394 LogFlowFunc(("mCmd=%s, mFlags=%x, mTimeoutMS=%RU32\n",
1395 procInfo.mCommand.c_str(), procInfo.mFlags, procInfo.mTimeoutMS));
1396#ifdef DEBUG
1397 if (procInfo.mArguments.size())
1398 {
1399 LogFlowFunc(("Arguments:"));
1400 ProcessArguments::const_iterator it = procInfo.mArguments.begin();
1401 while (it != procInfo.mArguments.end())
1402 {
1403 LogFlow((" %s", (*it).c_str()));
1404 it++;
1405 }
1406 LogFlow(("\n"));
1407 }
1408#endif
1409
1410 /* Validate flags. */
1411 if (procInfo.mFlags)
1412 {
1413 if ( !(procInfo.mFlags & ProcessCreateFlag_IgnoreOrphanedProcesses)
1414 && !(procInfo.mFlags & ProcessCreateFlag_WaitForProcessStartOnly)
1415 && !(procInfo.mFlags & ProcessCreateFlag_Hidden)
1416 && !(procInfo.mFlags & ProcessCreateFlag_NoProfile)
1417 && !(procInfo.mFlags & ProcessCreateFlag_WaitForStdOut)
1418 && !(procInfo.mFlags & ProcessCreateFlag_WaitForStdErr))
1419 {
1420 return VERR_INVALID_PARAMETER;
1421 }
1422 }
1423
1424 if ( (procInfo.mFlags & ProcessCreateFlag_WaitForProcessStartOnly)
1425 && ( (procInfo.mFlags & ProcessCreateFlag_WaitForStdOut)
1426 || (procInfo.mFlags & ProcessCreateFlag_WaitForStdErr)
1427 )
1428 )
1429 {
1430 return VERR_INVALID_PARAMETER;
1431 }
1432
1433 /* Adjust timeout. If set to 0, we define
1434 * an infinite timeout. */
1435 if (procInfo.mTimeoutMS == 0)
1436 procInfo.mTimeoutMS = UINT32_MAX;
1437
1438 /** @tood Implement process priority + affinity. */
1439
1440 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1441
1442 int rc = VERR_MAX_PROCS_REACHED;
1443 if (mData.mNumObjects >= VBOX_GUESTCTRL_MAX_OBJECTS)
1444 return rc;
1445
1446 /* Create a new (host-based) process ID and assign it. */
1447 uint32_t uNewProcessID = 0;
1448 ULONG uTries = 0;
1449
1450 for (;;)
1451 {
1452 /* Is the context ID already used? */
1453 if (!processExists(uNewProcessID, NULL /* pProgress */))
1454 {
1455 /* Callback with context ID was not found. This means
1456 * we can use this context ID for our new callback we want
1457 * to add below. */
1458 rc = VINF_SUCCESS;
1459 break;
1460 }
1461 uNewProcessID++;
1462 if (uNewProcessID == VBOX_GUESTCTRL_MAX_OBJECTS)
1463 uNewProcessID = 0;
1464
1465 if (++uTries == VBOX_GUESTCTRL_MAX_OBJECTS)
1466 break; /* Don't try too hard. */
1467 }
1468
1469 if (RT_FAILURE(rc))
1470 return rc;
1471
1472 /* Create the process object. */
1473 HRESULT hr = pProcess.createObject();
1474 if (FAILED(hr))
1475 return VERR_COM_UNEXPECTED;
1476
1477 rc = pProcess->init(mData.mParent->getConsole() /* Console */, this /* Session */,
1478 uNewProcessID, procInfo);
1479 if (RT_FAILURE(rc))
1480 return rc;
1481
1482 /* Add the created process to our map. */
1483 mData.mProcesses[uNewProcessID] = pProcess;
1484 mData.mNumObjects++;
1485 Assert(mData.mNumObjects <= VBOX_GUESTCTRL_MAX_OBJECTS);
1486
1487 LogFlowFunc(("Added new process (Session: %RU32) with process ID=%RU32 (now total %ld processes, %ld objects)\n",
1488 mData.mSession.mID, uNewProcessID, mData.mProcesses.size(), mData.mNumObjects));
1489
1490 return rc;
1491}
1492
1493inline bool GuestSession::processExists(uint32_t uProcessID, ComObjPtr<GuestProcess> *pProcess)
1494{
1495 SessionProcesses::const_iterator it = mData.mProcesses.find(uProcessID);
1496 if (it != mData.mProcesses.end())
1497 {
1498 if (pProcess)
1499 *pProcess = it->second;
1500 return true;
1501 }
1502 return false;
1503}
1504
1505inline int GuestSession::processGetByPID(ULONG uPID, ComObjPtr<GuestProcess> *pProcess)
1506{
1507 AssertReturn(uPID, false);
1508 /* pProcess is optional. */
1509
1510 SessionProcesses::iterator itProcs = mData.mProcesses.begin();
1511 for (; itProcs != mData.mProcesses.end(); itProcs++)
1512 {
1513 ComObjPtr<GuestProcess> pCurProc = itProcs->second;
1514 AutoCaller procCaller(pCurProc);
1515 if (procCaller.rc())
1516 return VERR_COM_INVALID_OBJECT_STATE;
1517
1518 ULONG uCurPID;
1519 HRESULT hr = pCurProc->COMGETTER(PID)(&uCurPID);
1520 ComAssertComRC(hr);
1521
1522 if (uCurPID == uPID)
1523 {
1524 if (pProcess)
1525 *pProcess = pCurProc;
1526 return VINF_SUCCESS;
1527 }
1528 }
1529
1530 return VERR_NOT_FOUND;
1531}
1532
1533int GuestSession::sendCommand(uint32_t uFunction,
1534 uint32_t uParms, PVBOXHGCMSVCPARM paParms)
1535{
1536 LogFlowThisFuncEnter();
1537
1538#ifndef VBOX_GUESTCTRL_TEST_CASE
1539 ComObjPtr<Console> pConsole = mData.mParent->getConsole();
1540 Assert(!pConsole.isNull());
1541
1542 /* Forward the information to the VMM device. */
1543 VMMDev *pVMMDev = pConsole->getVMMDev();
1544 AssertPtr(pVMMDev);
1545
1546 LogFlowThisFunc(("uFunction=%RU32, uParms=%RU32\n", uFunction, uParms));
1547 int vrc = pVMMDev->hgcmHostCall(HGCMSERVICE_NAME, uFunction, uParms, paParms);
1548 if (RT_FAILURE(vrc))
1549 {
1550 /** @todo What to do here? */
1551 }
1552#else
1553 /* Not needed within testcases. */
1554 int vrc = VINF_SUCCESS;
1555#endif
1556 LogFlowFuncLeaveRC(vrc);
1557 return vrc;
1558}
1559
1560/* static */
1561HRESULT GuestSession::setErrorExternal(VirtualBoxBase *pInterface, int guestRc)
1562{
1563 AssertPtr(pInterface);
1564 AssertMsg(RT_FAILURE(guestRc), ("Guest rc does not indicate a failure when setting error\n"));
1565
1566 return pInterface->setError(VBOX_E_IPRT_ERROR, GuestSession::guestErrorToString(guestRc).c_str());
1567}
1568
1569int GuestSession::startTaskAsync(const Utf8Str &strTaskDesc,
1570 GuestSessionTask *pTask, ComObjPtr<Progress> &pProgress)
1571{
1572 LogFlowThisFunc(("strTaskDesc=%s, pTask=%p\n", strTaskDesc.c_str(), pTask));
1573
1574 AssertPtrReturn(pTask, VERR_INVALID_POINTER);
1575
1576 /* Create the progress object. */
1577 HRESULT hr = pProgress.createObject();
1578 if (FAILED(hr))
1579 return VERR_COM_UNEXPECTED;
1580
1581 hr = pProgress->init(static_cast<IGuestSession*>(this),
1582 Bstr(strTaskDesc).raw(),
1583 TRUE /* aCancelable */);
1584 if (FAILED(hr))
1585 return VERR_COM_UNEXPECTED;
1586
1587 /* Initialize our worker task. */
1588 std::auto_ptr<GuestSessionTask> task(pTask);
1589
1590 int rc = task->RunAsync(strTaskDesc, pProgress);
1591 if (RT_FAILURE(rc))
1592 return rc;
1593
1594 /* Don't destruct on success. */
1595 task.release();
1596
1597 LogFlowFuncLeaveRC(rc);
1598 return rc;
1599}
1600
1601/**
1602 * Queries/collects information prior to establishing a guest session.
1603 * This is necessary to know which guest control protocol version to use,
1604 * among other things (later).
1605 *
1606 * @return IPRT status code.
1607 */
1608int GuestSession::queryInfo(void)
1609{
1610#ifndef DEBUG_andy
1611 /* Since the new functions are not fully implemented yet, force Main
1612 to use protocol ver 1 so far. */
1613 mData.mProtocolVersion = 1;
1614#else
1615 #if 1
1616 /* For debugging only: Hardcode version. */
1617 mData.mProtocolVersion = 2;
1618 #else
1619 /*
1620 * Try querying the guest control protocol version running on the guest.
1621 * This is done using the Guest Additions version
1622 */
1623 ComObjPtr<Guest> pGuest = mData.mParent;
1624 Assert(!pGuest.isNull());
1625
1626 uint32_t uVerAdditions = pGuest->getAdditionsVersion();
1627 mData.mProtocolVersion = ( VBOX_FULL_VERSION_GET_MAJOR(uVerAdditions) >= 4
1628 && VBOX_FULL_VERSION_GET_MINOR(uVerAdditions) >= 3) /** @todo What's about v5.0 ? */
1629 ? 2 /* Guest control 2.0. */
1630 : 1; /* Legacy guest control (VBox < 4.3). */
1631 /* Build revision is ignored. */
1632
1633 /* Tell the user but don't bitch too often. */
1634 static short s_gctrlLegacyWarning = 0;
1635 if (s_gctrlLegacyWarning++ < 3) /** @todo Find a bit nicer text. */
1636 LogRel((tr("Warning: Guest Additions are older (%ld.%ld) than host capabilities for guest control, please upgrade them. Using protocol version %ld now\n"),
1637 VBOX_FULL_VERSION_GET_MAJOR(uVerAdditions), VBOX_FULL_VERSION_GET_MINOR(uVerAdditions), mData.mProtocolVersion));
1638 #endif
1639#endif
1640 return VINF_SUCCESS;
1641}
1642
1643int GuestSession::waitFor(uint32_t fWaitFlags, ULONG uTimeoutMS, GuestSessionWaitResult_T &waitResult, int *pGuestRc)
1644{
1645 LogFlowThisFuncEnter();
1646
1647 AssertReturn(fWaitFlags, VERR_INVALID_PARAMETER);
1648
1649 LogFlowThisFunc(("fWaitFlags=0x%x, uTimeoutMS=%RU32, mStatus=%RU32, mWaitCount=%RU32, mWaitEvent=%p, pGuestRc=%p\n",
1650 fWaitFlags, uTimeoutMS, mData.mStatus, mData.mWaitCount, mData.mWaitEvent, pGuestRc));
1651
1652 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1653
1654 /* Did some error occur before? Then skip waiting and return. */
1655 if (mData.mStatus == GuestSessionStatus_Error)
1656 {
1657 waitResult = GuestSessionWaitResult_Error;
1658 AssertMsg(RT_FAILURE(mData.mRC), ("No error rc (%Rrc) set when guest session indicated an error\n", mData.mRC));
1659 if (pGuestRc)
1660 *pGuestRc = mData.mRC; /* Return last set error. */
1661 return VERR_GSTCTL_GUEST_ERROR;
1662 }
1663
1664 waitResult = GuestSessionWaitResult_None;
1665 if (fWaitFlags & GuestSessionWaitForFlag_Terminate)
1666 {
1667 switch (mData.mStatus)
1668 {
1669 case GuestSessionStatus_Terminated:
1670 case GuestSessionStatus_Down:
1671 waitResult = GuestSessionWaitResult_Terminate;
1672 break;
1673
1674 case GuestSessionStatus_TimedOutKilled:
1675 case GuestSessionStatus_TimedOutAbnormally:
1676 waitResult = GuestSessionWaitResult_Timeout;
1677 break;
1678
1679 case GuestSessionStatus_Error:
1680 /* Handled above. */
1681 break;
1682
1683 case GuestSessionStatus_Started:
1684 waitResult = GuestSessionWaitResult_Start;
1685 break;
1686
1687 case GuestSessionStatus_Undefined:
1688 case GuestSessionStatus_Starting:
1689 /* Do the waiting below. */
1690 break;
1691
1692 default:
1693 AssertMsgFailed(("Unhandled session status %ld\n", mData.mStatus));
1694 return VERR_NOT_IMPLEMENTED;
1695 }
1696 }
1697 else if (fWaitFlags & GuestSessionWaitForFlag_Start)
1698 {
1699 switch (mData.mStatus)
1700 {
1701 case GuestSessionStatus_Started:
1702 case GuestSessionStatus_Terminating:
1703 case GuestSessionStatus_Terminated:
1704 case GuestSessionStatus_Down:
1705 waitResult = GuestSessionWaitResult_Start;
1706 break;
1707
1708 case GuestSessionStatus_Error:
1709 waitResult = GuestSessionWaitResult_Error;
1710 break;
1711
1712 case GuestSessionStatus_TimedOutKilled:
1713 case GuestSessionStatus_TimedOutAbnormally:
1714 waitResult = GuestSessionWaitResult_Timeout;
1715 break;
1716
1717 case GuestSessionStatus_Undefined:
1718 case GuestSessionStatus_Starting:
1719 /* Do the waiting below. */
1720 break;
1721
1722 default:
1723 AssertMsgFailed(("Unhandled session status %ld\n", mData.mStatus));
1724 return VERR_NOT_IMPLEMENTED;
1725 }
1726 }
1727
1728 LogFlowThisFunc(("sessionStatus=%ld, sessionRc=%Rrc, waitResult=%ld\n",
1729 mData.mStatus, mData.mRC, waitResult));
1730
1731 /* No waiting needed? Return immediately using the last set error. */
1732 if (waitResult != GuestSessionWaitResult_None)
1733 {
1734 if (pGuestRc)
1735 *pGuestRc = mData.mRC; /* Return last set error (if any). */
1736 return RT_SUCCESS(mData.mRC) ? VINF_SUCCESS : VERR_GSTCTL_GUEST_ERROR;
1737 }
1738
1739 if (mData.mWaitCount > 0) /* We only support one waiting caller a time at the moment. */
1740 return VERR_ALREADY_EXISTS;
1741 mData.mWaitCount++;
1742
1743 int vrc = VINF_SUCCESS;
1744 try
1745 {
1746 Assert(mData.mWaitEvent == NULL);
1747 mData.mWaitEvent = new GuestSessionWaitEvent(fWaitFlags);
1748 }
1749 catch(std::bad_alloc &)
1750 {
1751 vrc = VERR_NO_MEMORY;
1752 }
1753
1754 if (RT_SUCCESS(vrc))
1755 {
1756 GuestSessionWaitEvent *pEvent = mData.mWaitEvent;
1757 AssertPtr(pEvent);
1758
1759 alock.release(); /* Release lock before waiting. */
1760
1761 vrc = pEvent->Wait(uTimeoutMS);
1762 LogFlowThisFunc(("Waiting completed with rc=%Rrc\n", vrc));
1763 if (RT_SUCCESS(vrc))
1764 {
1765 waitResult = pEvent->GetWaitResult();
1766 int guestRc = pEvent->GetWaitRc();
1767 if (RT_FAILURE(guestRc))
1768 vrc = VERR_GSTCTL_GUEST_ERROR;
1769
1770 LogFlowThisFunc(("Waiting event returned rc=%Rrc\n", guestRc));
1771
1772 if (pGuestRc)
1773 *pGuestRc = guestRc;
1774 }
1775
1776 alock.acquire(); /* Get the lock again. */
1777
1778 /* Note: The caller always is responsible of deleting the
1779 * stuff it created before. See close() for more information. */
1780 delete mData.mWaitEvent;
1781 mData.mWaitEvent = NULL;
1782 }
1783
1784 Assert(mData.mWaitCount);
1785 mData.mWaitCount--;
1786
1787 LogFlowFuncLeaveRC(vrc);
1788 return vrc;
1789}
1790
1791// implementation of public methods
1792/////////////////////////////////////////////////////////////////////////////
1793
1794STDMETHODIMP GuestSession::Close(void)
1795{
1796#ifndef VBOX_WITH_GUEST_CONTROL
1797 ReturnComNotImplemented();
1798#else
1799 LogFlowThisFuncEnter();
1800
1801 AutoCaller autoCaller(this);
1802 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1803
1804 /* Close session on guest. */
1805 int guestRc;
1806 int rc = closeSession(0 /* Flags */, 30 * 1000 /* Timeout */,
1807 &guestRc);
1808 /* On failure don't return here, instead do all the cleanup
1809 * work first and then return an error. */
1810
1811 /* Remove ourselves from the session list. */
1812 int rc2 = mData.mParent->sessionRemove(this);
1813 if (RT_SUCCESS(rc))
1814 rc = rc2;
1815
1816 /*
1817 * Release autocaller before calling uninit.
1818 */
1819 autoCaller.release();
1820
1821 uninit();
1822
1823 LogFlowFuncLeaveRC(rc);
1824 if (RT_FAILURE(rc))
1825 {
1826 if (rc == VERR_GSTCTL_GUEST_ERROR)
1827 return GuestSession::setErrorExternal(this, guestRc);
1828
1829 return setError(VBOX_E_IPRT_ERROR,
1830 tr("Closing guest session failed with %Rrc\n"), rc);
1831 }
1832
1833 return S_OK;
1834#endif /* VBOX_WITH_GUEST_CONTROL */
1835}
1836
1837STDMETHODIMP GuestSession::CopyFrom(IN_BSTR aSource, IN_BSTR aDest, ComSafeArrayIn(CopyFileFlag_T, aFlags), IProgress **aProgress)
1838{
1839#ifndef VBOX_WITH_GUEST_CONTROL
1840 ReturnComNotImplemented();
1841#else
1842 CheckComArgStrNotEmptyOrNull(aSource);
1843 CheckComArgStrNotEmptyOrNull(aDest);
1844 CheckComArgOutPointerValid(aProgress);
1845
1846 LogFlowThisFuncEnter();
1847
1848 if (RT_UNLIKELY((aSource) == NULL || *(aSource) == '\0'))
1849 return setError(E_INVALIDARG, tr("No source specified"));
1850 if (RT_UNLIKELY((aDest) == NULL || *(aDest) == '\0'))
1851 return setError(E_INVALIDARG, tr("No destination specified"));
1852
1853 AutoCaller autoCaller(this);
1854 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1855
1856 uint32_t fFlags = CopyFileFlag_None;
1857 if (aFlags)
1858 {
1859 com::SafeArray<CopyFileFlag_T> flags(ComSafeArrayInArg(aFlags));
1860 for (size_t i = 0; i < flags.size(); i++)
1861 fFlags |= flags[i];
1862 }
1863
1864 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1865
1866 HRESULT hr = S_OK;
1867
1868 try
1869 {
1870 ComObjPtr<Progress> pProgress;
1871 SessionTaskCopyFrom *pTask = new SessionTaskCopyFrom(this /* GuestSession */,
1872 Utf8Str(aSource), Utf8Str(aDest), fFlags);
1873 int rc = startTaskAsync(Utf8StrFmt(tr("Copying \"%ls\" from guest to \"%ls\" on the host"), aSource, aDest),
1874 pTask, pProgress);
1875 if (RT_SUCCESS(rc))
1876 {
1877 /* Return progress to the caller. */
1878 hr = pProgress.queryInterfaceTo(aProgress);
1879 }
1880 else
1881 hr = setError(VBOX_E_IPRT_ERROR,
1882 tr("Starting task for copying file \"%ls\" from guest to \"%ls\" on the host failed: %Rrc"), rc);
1883 }
1884 catch(std::bad_alloc &)
1885 {
1886 hr = E_OUTOFMEMORY;
1887 }
1888
1889 return hr;
1890#endif /* VBOX_WITH_GUEST_CONTROL */
1891}
1892
1893STDMETHODIMP GuestSession::CopyTo(IN_BSTR aSource, IN_BSTR aDest, ComSafeArrayIn(CopyFileFlag_T, aFlags), IProgress **aProgress)
1894{
1895#ifndef VBOX_WITH_GUEST_CONTROL
1896 ReturnComNotImplemented();
1897#else
1898 CheckComArgStrNotEmptyOrNull(aSource);
1899 CheckComArgStrNotEmptyOrNull(aDest);
1900 CheckComArgOutPointerValid(aProgress);
1901
1902 LogFlowThisFuncEnter();
1903
1904 if (RT_UNLIKELY((aSource) == NULL || *(aSource) == '\0'))
1905 return setError(E_INVALIDARG, tr("No source specified"));
1906 if (RT_UNLIKELY((aDest) == NULL || *(aDest) == '\0'))
1907 return setError(E_INVALIDARG, tr("No destination specified"));
1908
1909 AutoCaller autoCaller(this);
1910 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1911
1912 uint32_t fFlags = CopyFileFlag_None;
1913 if (aFlags)
1914 {
1915 com::SafeArray<CopyFileFlag_T> flags(ComSafeArrayInArg(aFlags));
1916 for (size_t i = 0; i < flags.size(); i++)
1917 fFlags |= flags[i];
1918 }
1919
1920 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1921
1922 HRESULT hr = S_OK;
1923
1924 try
1925 {
1926 ComObjPtr<Progress> pProgress;
1927 SessionTaskCopyTo *pTask = new SessionTaskCopyTo(this /* GuestSession */,
1928 Utf8Str(aSource), Utf8Str(aDest), fFlags);
1929 AssertPtrReturn(pTask, E_OUTOFMEMORY);
1930 int rc = startTaskAsync(Utf8StrFmt(tr("Copying \"%ls\" from host to \"%ls\" on the guest"), aSource, aDest),
1931 pTask, pProgress);
1932 if (RT_SUCCESS(rc))
1933 {
1934 /* Return progress to the caller. */
1935 hr = pProgress.queryInterfaceTo(aProgress);
1936 }
1937 else
1938 hr = setError(VBOX_E_IPRT_ERROR,
1939 tr("Starting task for copying file \"%ls\" from host to \"%ls\" on the guest failed: %Rrc"), rc);
1940 }
1941 catch(std::bad_alloc &)
1942 {
1943 hr = E_OUTOFMEMORY;
1944 }
1945
1946 return hr;
1947#endif /* VBOX_WITH_GUEST_CONTROL */
1948}
1949
1950STDMETHODIMP GuestSession::DirectoryCreate(IN_BSTR aPath, ULONG aMode,
1951 ComSafeArrayIn(DirectoryCreateFlag_T, aFlags))
1952{
1953#ifndef VBOX_WITH_GUEST_CONTROL
1954 ReturnComNotImplemented();
1955#else
1956 LogFlowThisFuncEnter();
1957
1958 if (RT_UNLIKELY((aPath) == NULL || *(aPath) == '\0'))
1959 return setError(E_INVALIDARG, tr("No directory to create specified"));
1960
1961 AutoCaller autoCaller(this);
1962 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1963
1964 uint32_t fFlags = DirectoryCreateFlag_None;
1965 if (aFlags)
1966 {
1967 com::SafeArray<DirectoryCreateFlag_T> flags(ComSafeArrayInArg(aFlags));
1968 for (size_t i = 0; i < flags.size(); i++)
1969 fFlags |= flags[i];
1970
1971 if (fFlags)
1972 {
1973 if (!(fFlags & DirectoryCreateFlag_Parents))
1974 return setError(E_INVALIDARG, tr("Unknown flags (%#x)"), fFlags);
1975 }
1976 }
1977
1978 HRESULT hr = S_OK;
1979
1980 ComObjPtr <GuestDirectory> pDirectory; int guestRc;
1981 int rc = directoryCreateInternal(Utf8Str(aPath), (uint32_t)aMode, fFlags, &guestRc);
1982 if (RT_FAILURE(rc))
1983 {
1984 switch (rc)
1985 {
1986 case VERR_GSTCTL_GUEST_ERROR:
1987 hr = GuestProcess::setErrorExternal(this, guestRc);
1988 break;
1989
1990 case VERR_INVALID_PARAMETER:
1991 hr = setError(VBOX_E_IPRT_ERROR, tr("Directory creation failed: Invalid parameters given"));
1992 break;
1993
1994 case VERR_BROKEN_PIPE:
1995 hr = setError(VBOX_E_IPRT_ERROR, tr("Directory creation failed: Unexpectedly aborted"));
1996 break;
1997
1998 case VERR_CANT_CREATE:
1999 hr = setError(VBOX_E_IPRT_ERROR, tr("Directory creation failed: Could not create directory"));
2000 break;
2001
2002 default:
2003 hr = setError(VBOX_E_IPRT_ERROR, tr("Directory creation failed: %Rrc"), rc);
2004 break;
2005 }
2006 }
2007
2008 return hr;
2009#endif /* VBOX_WITH_GUEST_CONTROL */
2010}
2011
2012STDMETHODIMP GuestSession::DirectoryCreateTemp(IN_BSTR aTemplate, ULONG aMode, IN_BSTR aPath, BOOL aSecure, BSTR *aDirectory)
2013{
2014#ifndef VBOX_WITH_GUEST_CONTROL
2015 ReturnComNotImplemented();
2016#else
2017 LogFlowThisFuncEnter();
2018
2019 if (RT_UNLIKELY((aTemplate) == NULL || *(aTemplate) == '\0'))
2020 return setError(E_INVALIDARG, tr("No template specified"));
2021 if (RT_UNLIKELY((aPath) == NULL || *(aPath) == '\0'))
2022 return setError(E_INVALIDARG, tr("No directory name specified"));
2023 CheckComArgOutPointerValid(aDirectory);
2024
2025 AutoCaller autoCaller(this);
2026 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2027
2028 HRESULT hr = S_OK;
2029
2030 Utf8Str strName; int guestRc;
2031 int rc = objectCreateTempInternal(Utf8Str(aTemplate),
2032 Utf8Str(aPath),
2033 true /* Directory */, strName, &guestRc);
2034 if (RT_SUCCESS(rc))
2035 {
2036 strName.cloneTo(aDirectory);
2037 }
2038 else
2039 {
2040 switch (rc)
2041 {
2042 case VERR_GSTCTL_GUEST_ERROR:
2043 hr = GuestProcess::setErrorExternal(this, guestRc);
2044 break;
2045
2046 default:
2047 hr = setError(VBOX_E_IPRT_ERROR, tr("Temporary directory creation \"%s\" with template \"%s\" failed: %Rrc"),
2048 Utf8Str(aPath).c_str(), Utf8Str(aTemplate).c_str(), rc);
2049 break;
2050 }
2051 }
2052
2053 return hr;
2054#endif /* VBOX_WITH_GUEST_CONTROL */
2055}
2056
2057STDMETHODIMP GuestSession::DirectoryExists(IN_BSTR aPath, BOOL *aExists)
2058{
2059#ifndef VBOX_WITH_GUEST_CONTROL
2060 ReturnComNotImplemented();
2061#else
2062 LogFlowThisFuncEnter();
2063
2064 if (RT_UNLIKELY((aPath) == NULL || *(aPath) == '\0'))
2065 return setError(E_INVALIDARG, tr("No directory to check existence for specified"));
2066 CheckComArgOutPointerValid(aExists);
2067
2068 AutoCaller autoCaller(this);
2069 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2070
2071 HRESULT hr = S_OK;
2072
2073 GuestFsObjData objData; int guestRc;
2074 int rc = directoryQueryInfoInternal(Utf8Str(aPath), objData, &guestRc);
2075 if (RT_SUCCESS(rc))
2076 {
2077 *aExists = objData.mType == FsObjType_Directory;
2078 }
2079 else
2080 {
2081 switch (rc)
2082 {
2083 case VERR_GSTCTL_GUEST_ERROR:
2084 hr = GuestProcess::setErrorExternal(this, guestRc);
2085 break;
2086
2087 default:
2088 hr = setError(VBOX_E_IPRT_ERROR, tr("Querying directory existence \"%s\" failed: %Rrc"),
2089 Utf8Str(aPath).c_str(), rc);
2090 break;
2091 }
2092 }
2093
2094 return hr;
2095#endif /* VBOX_WITH_GUEST_CONTROL */
2096}
2097
2098STDMETHODIMP GuestSession::DirectoryOpen(IN_BSTR aPath, IN_BSTR aFilter, ComSafeArrayIn(DirectoryOpenFlag_T, aFlags), IGuestDirectory **aDirectory)
2099{
2100#ifndef VBOX_WITH_GUEST_CONTROL
2101 ReturnComNotImplemented();
2102#else
2103 LogFlowThisFuncEnter();
2104
2105 if (RT_UNLIKELY((aPath) == NULL || *(aPath) == '\0'))
2106 return setError(E_INVALIDARG, tr("No directory to open specified"));
2107 if (RT_UNLIKELY((aFilter) != NULL && *(aFilter) != '\0'))
2108 return setError(E_INVALIDARG, tr("Directory filters are not implemented yet"));
2109 CheckComArgOutPointerValid(aDirectory);
2110
2111 AutoCaller autoCaller(this);
2112 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2113
2114 uint32_t fFlags = DirectoryOpenFlag_None;
2115 if (aFlags)
2116 {
2117 com::SafeArray<DirectoryOpenFlag_T> flags(ComSafeArrayInArg(aFlags));
2118 for (size_t i = 0; i < flags.size(); i++)
2119 fFlags |= flags[i];
2120
2121 if (fFlags)
2122 return setError(E_INVALIDARG, tr("Open flags (%#x) not implemented yet"), fFlags);
2123 }
2124
2125 HRESULT hr = S_OK;
2126
2127 ComObjPtr <GuestDirectory> pDirectory;
2128 int rc = directoryOpenInternal(Utf8Str(aPath), Utf8Str(aFilter), fFlags, pDirectory);
2129 if (RT_SUCCESS(rc))
2130 {
2131 /* Return directory object to the caller. */
2132 hr = pDirectory.queryInterfaceTo(aDirectory);
2133 }
2134 else
2135 {
2136 switch (rc)
2137 {
2138 case VERR_INVALID_PARAMETER:
2139 hr = setError(VBOX_E_IPRT_ERROR, tr("Opening directory \"%s\" failed; invalid parameters given",
2140 Utf8Str(aPath).c_str()));
2141 break;
2142
2143 default:
2144 hr = setError(VBOX_E_IPRT_ERROR, tr("Opening directory \"%s\" failed: %Rrc"),
2145 Utf8Str(aPath).c_str(),rc);
2146 break;
2147 }
2148 }
2149
2150 return hr;
2151#endif /* VBOX_WITH_GUEST_CONTROL */
2152}
2153
2154STDMETHODIMP GuestSession::DirectoryQueryInfo(IN_BSTR aPath, IGuestFsObjInfo **aInfo)
2155{
2156#ifndef VBOX_WITH_GUEST_CONTROL
2157 ReturnComNotImplemented();
2158#else
2159 LogFlowThisFuncEnter();
2160
2161 if (RT_UNLIKELY((aPath) == NULL || *(aPath) == '\0'))
2162 return setError(E_INVALIDARG, tr("No directory to query information for specified"));
2163 CheckComArgOutPointerValid(aInfo);
2164
2165 AutoCaller autoCaller(this);
2166 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2167
2168 HRESULT hr = S_OK;
2169
2170 GuestFsObjData objData; int guestRc;
2171 int vrc = directoryQueryInfoInternal(Utf8Str(aPath), objData, &guestRc);
2172 if (RT_SUCCESS(vrc))
2173 {
2174 if (objData.mType == FsObjType_Directory)
2175 {
2176 ComObjPtr<GuestFsObjInfo> pFsObjInfo;
2177 hr = pFsObjInfo.createObject();
2178 if (FAILED(hr)) return hr;
2179
2180 vrc = pFsObjInfo->init(objData);
2181 if (RT_SUCCESS(vrc))
2182 {
2183 hr = pFsObjInfo.queryInterfaceTo(aInfo);
2184 if (FAILED(hr)) return hr;
2185 }
2186 }
2187 }
2188
2189 if (RT_FAILURE(vrc))
2190 {
2191 switch (vrc)
2192 {
2193 case VERR_GSTCTL_GUEST_ERROR:
2194 hr = GuestProcess::setErrorExternal(this, guestRc);
2195 break;
2196
2197 case VERR_NOT_A_DIRECTORY:
2198 hr = setError(VBOX_E_IPRT_ERROR, tr("Element \"%s\" exists but is not a directory",
2199 Utf8Str(aPath).c_str()));
2200 break;
2201
2202 default:
2203 hr = setError(VBOX_E_IPRT_ERROR, tr("Querying directory information for \"%s\" failed: %Rrc"),
2204 Utf8Str(aPath).c_str(), vrc);
2205 break;
2206 }
2207 }
2208
2209 return hr;
2210#endif /* VBOX_WITH_GUEST_CONTROL */
2211}
2212
2213STDMETHODIMP GuestSession::DirectoryRemove(IN_BSTR aPath)
2214{
2215#ifndef VBOX_WITH_GUEST_CONTROL
2216 ReturnComNotImplemented();
2217#else
2218 LogFlowThisFuncEnter();
2219
2220 AutoCaller autoCaller(this);
2221 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2222
2223 ReturnComNotImplemented();
2224#endif /* VBOX_WITH_GUEST_CONTROL */
2225}
2226
2227STDMETHODIMP GuestSession::DirectoryRemoveRecursive(IN_BSTR aPath, ComSafeArrayIn(DirectoryRemoveRecFlag_T, aFlags), IProgress **aProgress)
2228{
2229#ifndef VBOX_WITH_GUEST_CONTROL
2230 ReturnComNotImplemented();
2231#else
2232 LogFlowThisFuncEnter();
2233
2234 AutoCaller autoCaller(this);
2235 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2236
2237 ReturnComNotImplemented();
2238#endif /* VBOX_WITH_GUEST_CONTROL */
2239}
2240
2241STDMETHODIMP GuestSession::DirectoryRename(IN_BSTR aSource, IN_BSTR aDest, ComSafeArrayIn(PathRenameFlag_T, aFlags))
2242{
2243#ifndef VBOX_WITH_GUEST_CONTROL
2244 ReturnComNotImplemented();
2245#else
2246 LogFlowThisFuncEnter();
2247
2248 AutoCaller autoCaller(this);
2249 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2250
2251 ReturnComNotImplemented();
2252#endif /* VBOX_WITH_GUEST_CONTROL */
2253}
2254
2255STDMETHODIMP GuestSession::DirectorySetACL(IN_BSTR aPath, IN_BSTR aACL)
2256{
2257#ifndef VBOX_WITH_GUEST_CONTROL
2258 ReturnComNotImplemented();
2259#else
2260 LogFlowThisFuncEnter();
2261
2262 AutoCaller autoCaller(this);
2263 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2264
2265 ReturnComNotImplemented();
2266#endif /* VBOX_WITH_GUEST_CONTROL */
2267}
2268
2269STDMETHODIMP GuestSession::EnvironmentClear(void)
2270{
2271#ifndef VBOX_WITH_GUEST_CONTROL
2272 ReturnComNotImplemented();
2273#else
2274 LogFlowThisFuncEnter();
2275
2276 AutoCaller autoCaller(this);
2277 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2278
2279 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2280
2281 mData.mEnvironment.Clear();
2282
2283 LogFlowFuncLeaveRC(S_OK);
2284 return S_OK;
2285#endif /* VBOX_WITH_GUEST_CONTROL */
2286}
2287
2288STDMETHODIMP GuestSession::EnvironmentGet(IN_BSTR aName, BSTR *aValue)
2289{
2290#ifndef VBOX_WITH_GUEST_CONTROL
2291 ReturnComNotImplemented();
2292#else
2293 LogFlowThisFuncEnter();
2294
2295 if (RT_UNLIKELY((aName) == NULL || *(aName) == '\0'))
2296 return setError(E_INVALIDARG, tr("No value name specified"));
2297
2298 CheckComArgOutPointerValid(aValue);
2299
2300 AutoCaller autoCaller(this);
2301 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2302
2303 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2304
2305 Bstr strValue(mData.mEnvironment.Get(Utf8Str(aName)));
2306 strValue.cloneTo(aValue);
2307
2308 LogFlowFuncLeaveRC(S_OK);
2309 return S_OK;
2310#endif /* VBOX_WITH_GUEST_CONTROL */
2311}
2312
2313STDMETHODIMP GuestSession::EnvironmentSet(IN_BSTR aName, IN_BSTR aValue)
2314{
2315#ifndef VBOX_WITH_GUEST_CONTROL
2316 ReturnComNotImplemented();
2317#else
2318 LogFlowThisFuncEnter();
2319
2320 if (RT_UNLIKELY((aName) == NULL || *(aName) == '\0'))
2321 return setError(E_INVALIDARG, tr("No value name specified"));
2322
2323 AutoCaller autoCaller(this);
2324 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2325
2326 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2327
2328 int rc = mData.mEnvironment.Set(Utf8Str(aName), Utf8Str(aValue));
2329
2330 HRESULT hr = RT_SUCCESS(rc) ? S_OK : VBOX_E_IPRT_ERROR;
2331 LogFlowFuncLeaveRC(hr);
2332 return hr;
2333#endif /* VBOX_WITH_GUEST_CONTROL */
2334}
2335
2336STDMETHODIMP GuestSession::EnvironmentUnset(IN_BSTR aName)
2337{
2338#ifndef VBOX_WITH_GUEST_CONTROL
2339 ReturnComNotImplemented();
2340#else
2341 LogFlowThisFuncEnter();
2342
2343 AutoCaller autoCaller(this);
2344 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2345
2346 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2347
2348 mData.mEnvironment.Unset(Utf8Str(aName));
2349
2350 LogFlowFuncLeaveRC(S_OK);
2351 return S_OK;
2352#endif /* VBOX_WITH_GUEST_CONTROL */
2353}
2354
2355STDMETHODIMP GuestSession::FileCreateTemp(IN_BSTR aTemplate, ULONG aMode, IN_BSTR aPath, BOOL aSecure, IGuestFile **aFile)
2356{
2357#ifndef VBOX_WITH_GUEST_CONTROL
2358 ReturnComNotImplemented();
2359#else
2360 LogFlowThisFuncEnter();
2361
2362 AutoCaller autoCaller(this);
2363 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2364
2365 ReturnComNotImplemented();
2366#endif /* VBOX_WITH_GUEST_CONTROL */
2367}
2368
2369STDMETHODIMP GuestSession::FileExists(IN_BSTR aPath, BOOL *aExists)
2370{
2371#ifndef VBOX_WITH_GUEST_CONTROL
2372 ReturnComNotImplemented();
2373#else
2374 LogFlowThisFuncEnter();
2375
2376 if (RT_UNLIKELY((aPath) == NULL || *(aPath) == '\0'))
2377 return setError(E_INVALIDARG, tr("No file to check existence for specified"));
2378 CheckComArgOutPointerValid(aExists);
2379
2380 AutoCaller autoCaller(this);
2381 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2382
2383 GuestFsObjData objData; int guestRc;
2384 int vrc = fileQueryInfoInternal(Utf8Str(aPath), objData, &guestRc);
2385 if (RT_SUCCESS(vrc))
2386 {
2387 *aExists = TRUE;
2388 return S_OK;
2389 }
2390
2391 HRESULT hr = S_OK;
2392
2393 switch (vrc)
2394 {
2395 case VERR_GSTCTL_GUEST_ERROR:
2396 hr = GuestProcess::setErrorExternal(this, guestRc);
2397 break;
2398
2399 case VERR_NOT_A_FILE:
2400 *aExists = FALSE;
2401 break;
2402
2403 default:
2404 hr = setError(VBOX_E_IPRT_ERROR, tr("Querying file information for \"%s\" failed: %Rrc"),
2405 Utf8Str(aPath).c_str(), vrc);
2406 break;
2407 }
2408
2409 return hr;
2410#endif /* VBOX_WITH_GUEST_CONTROL */
2411}
2412
2413STDMETHODIMP GuestSession::FileRemove(IN_BSTR aPath)
2414{
2415#ifndef VBOX_WITH_GUEST_CONTROL
2416 ReturnComNotImplemented();
2417#else
2418 LogFlowThisFuncEnter();
2419
2420 if (RT_UNLIKELY((aPath) == NULL || *(aPath) == '\0'))
2421 return setError(E_INVALIDARG, tr("No file to remove specified"));
2422
2423 AutoCaller autoCaller(this);
2424 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2425
2426 HRESULT hr = S_OK;
2427
2428 int guestRc;
2429 int vrc = fileRemoveInternal(Utf8Str(aPath), &guestRc);
2430 if (RT_FAILURE(vrc))
2431 {
2432 switch (vrc)
2433 {
2434 case VERR_GSTCTL_GUEST_ERROR:
2435 hr = GuestProcess::setErrorExternal(this, guestRc);
2436 break;
2437
2438 default:
2439 hr = setError(VBOX_E_IPRT_ERROR, tr("Removing file \"%s\" failed: %Rrc"),
2440 Utf8Str(aPath).c_str(), vrc);
2441 break;
2442 }
2443 }
2444
2445 return hr;
2446#endif /* VBOX_WITH_GUEST_CONTROL */
2447}
2448
2449STDMETHODIMP GuestSession::FileOpen(IN_BSTR aPath, IN_BSTR aOpenMode, IN_BSTR aDisposition, ULONG aCreationMode, LONG64 aOffset, IGuestFile **aFile)
2450{
2451#ifndef VBOX_WITH_GUEST_CONTROL
2452 ReturnComNotImplemented();
2453#else
2454 LogFlowThisFuncEnter();
2455
2456 if (RT_UNLIKELY((aPath) == NULL || *(aPath) == '\0'))
2457 return setError(E_INVALIDARG, tr("No file to open specified"));
2458 if (RT_UNLIKELY((aOpenMode) == NULL || *(aOpenMode) == '\0'))
2459 return setError(E_INVALIDARG, tr("No open mode specified"));
2460 if (RT_UNLIKELY((aDisposition) == NULL || *(aDisposition) == '\0'))
2461 return setError(E_INVALIDARG, tr("No disposition mode specified"));
2462
2463 CheckComArgOutPointerValid(aFile);
2464
2465 AutoCaller autoCaller(this);
2466 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2467
2468 /** @todo Validate open mode. */
2469 /** @todo Validate disposition mode. */
2470
2471 /** @todo Validate creation mode. */
2472 uint32_t uCreationMode = 0;
2473
2474 HRESULT hr = S_OK;
2475
2476 GuestFileOpenInfo openInfo;
2477 openInfo.mFileName = Utf8Str(aPath);
2478 openInfo.mOpenMode = Utf8Str(aOpenMode);
2479 openInfo.mDisposition = Utf8Str(aDisposition);
2480 openInfo.mCreationMode = aCreationMode;
2481 openInfo.mInitialOffset = aOffset;
2482
2483 ComObjPtr <GuestFile> pFile; int guestRc;
2484 int vrc = fileOpenInternal(openInfo, pFile, &guestRc);
2485 if (RT_SUCCESS(vrc))
2486 {
2487 /* Return directory object to the caller. */
2488 hr = pFile.queryInterfaceTo(aFile);
2489 }
2490 else
2491 {
2492 switch (vrc)
2493 {
2494 case VERR_GSTCTL_GUEST_ERROR:
2495 hr = GuestFile::setErrorExternal(this, guestRc);
2496 break;
2497
2498 default:
2499 hr = setError(VBOX_E_IPRT_ERROR, tr("Opening guest file \"%s\" failed: %Rrc"),
2500 Utf8Str(aPath).c_str(), vrc);
2501 break;
2502 }
2503 }
2504
2505 return hr;
2506#endif /* VBOX_WITH_GUEST_CONTROL */
2507}
2508
2509STDMETHODIMP GuestSession::FileQueryInfo(IN_BSTR aPath, IGuestFsObjInfo **aInfo)
2510{
2511#ifndef VBOX_WITH_GUEST_CONTROL
2512 ReturnComNotImplemented();
2513#else
2514 LogFlowThisFuncEnter();
2515
2516 if (RT_UNLIKELY((aPath) == NULL || *(aPath) == '\0'))
2517 return setError(E_INVALIDARG, tr("No file to query information for specified"));
2518 CheckComArgOutPointerValid(aInfo);
2519
2520 AutoCaller autoCaller(this);
2521 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2522
2523 HRESULT hr = S_OK;
2524
2525 GuestFsObjData objData; int guestRc;
2526 int vrc = fileQueryInfoInternal(Utf8Str(aPath), objData, &guestRc);
2527 if (RT_SUCCESS(vrc))
2528 {
2529 ComObjPtr<GuestFsObjInfo> pFsObjInfo;
2530 hr = pFsObjInfo.createObject();
2531 if (FAILED(hr)) return hr;
2532
2533 vrc = pFsObjInfo->init(objData);
2534 if (RT_SUCCESS(vrc))
2535 {
2536 hr = pFsObjInfo.queryInterfaceTo(aInfo);
2537 if (FAILED(hr)) return hr;
2538 }
2539 }
2540
2541 if (RT_FAILURE(vrc))
2542 {
2543 switch (vrc)
2544 {
2545 case VERR_GSTCTL_GUEST_ERROR:
2546 hr = GuestProcess::setErrorExternal(this, guestRc);
2547 break;
2548
2549 case VERR_NOT_A_FILE:
2550 hr = setError(VBOX_E_IPRT_ERROR, tr("Element exists but is not a file"));
2551 break;
2552
2553 default:
2554 hr = setError(VBOX_E_IPRT_ERROR, tr("Querying file information failed: %Rrc"), vrc);
2555 break;
2556 }
2557 }
2558
2559 return hr;
2560#endif /* VBOX_WITH_GUEST_CONTROL */
2561}
2562
2563STDMETHODIMP GuestSession::FileQuerySize(IN_BSTR aPath, LONG64 *aSize)
2564{
2565#ifndef VBOX_WITH_GUEST_CONTROL
2566 ReturnComNotImplemented();
2567#else
2568 LogFlowThisFuncEnter();
2569
2570 if (RT_UNLIKELY((aPath) == NULL || *(aPath) == '\0'))
2571 return setError(E_INVALIDARG, tr("No file to query size for specified"));
2572 CheckComArgOutPointerValid(aSize);
2573
2574 AutoCaller autoCaller(this);
2575 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2576
2577 HRESULT hr = S_OK;
2578
2579 int64_t llSize; int guestRc;
2580 int vrc = fileQuerySizeInternal(Utf8Str(aPath), &llSize, &guestRc);
2581 if (RT_SUCCESS(vrc))
2582 {
2583 *aSize = llSize;
2584 }
2585 else
2586 {
2587 switch (vrc)
2588 {
2589 case VERR_GSTCTL_GUEST_ERROR:
2590 hr = GuestProcess::setErrorExternal(this, guestRc);
2591 break;
2592
2593 default:
2594 hr = setError(VBOX_E_IPRT_ERROR, tr("Querying file size failed: %Rrc"), vrc);
2595 break;
2596 }
2597 }
2598
2599 return hr;
2600#endif /* VBOX_WITH_GUEST_CONTROL */
2601}
2602
2603STDMETHODIMP GuestSession::FileRename(IN_BSTR aSource, IN_BSTR aDest, ComSafeArrayIn(PathRenameFlag_T, aFlags))
2604{
2605#ifndef VBOX_WITH_GUEST_CONTROL
2606 ReturnComNotImplemented();
2607#else
2608 LogFlowThisFuncEnter();
2609
2610 AutoCaller autoCaller(this);
2611 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2612
2613 ReturnComNotImplemented();
2614#endif /* VBOX_WITH_GUEST_CONTROL */
2615}
2616
2617STDMETHODIMP GuestSession::FileSetACL(IN_BSTR aPath, IN_BSTR aACL)
2618{
2619#ifndef VBOX_WITH_GUEST_CONTROL
2620 ReturnComNotImplemented();
2621#else
2622 LogFlowThisFuncEnter();
2623
2624 AutoCaller autoCaller(this);
2625 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2626
2627 ReturnComNotImplemented();
2628#endif /* VBOX_WITH_GUEST_CONTROL */
2629}
2630
2631STDMETHODIMP GuestSession::ProcessCreate(IN_BSTR aCommand, ComSafeArrayIn(IN_BSTR, aArguments), ComSafeArrayIn(IN_BSTR, aEnvironment),
2632 ComSafeArrayIn(ProcessCreateFlag_T, aFlags), ULONG aTimeoutMS, IGuestProcess **aProcess)
2633{
2634#ifndef VBOX_WITH_GUEST_CONTROL
2635 ReturnComNotImplemented();
2636#else
2637 LogFlowThisFuncEnter();
2638
2639 com::SafeArray<LONG> affinity;
2640
2641 return ProcessCreateEx(aCommand, ComSafeArrayInArg(aArguments), ComSafeArrayInArg(aEnvironment),
2642 ComSafeArrayInArg(aFlags), aTimeoutMS, ProcessPriority_Default, ComSafeArrayAsInParam(affinity), aProcess);
2643#endif /* VBOX_WITH_GUEST_CONTROL */
2644}
2645
2646STDMETHODIMP GuestSession::ProcessCreateEx(IN_BSTR aCommand, ComSafeArrayIn(IN_BSTR, aArguments), ComSafeArrayIn(IN_BSTR, aEnvironment),
2647 ComSafeArrayIn(ProcessCreateFlag_T, aFlags), ULONG aTimeoutMS,
2648 ProcessPriority_T aPriority, ComSafeArrayIn(LONG, aAffinity),
2649 IGuestProcess **aProcess)
2650{
2651#ifndef VBOX_WITH_GUEST_CONTROL
2652 ReturnComNotImplemented();
2653#else
2654 LogFlowThisFuncEnter();
2655
2656 if (RT_UNLIKELY((aCommand) == NULL || *(aCommand) == '\0'))
2657 return setError(E_INVALIDARG, tr("No command to execute specified"));
2658 CheckComArgOutPointerValid(aProcess);
2659
2660 AutoCaller autoCaller(this);
2661 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2662
2663 GuestProcessStartupInfo procInfo;
2664 procInfo.mCommand = Utf8Str(aCommand);
2665
2666 if (aArguments)
2667 {
2668 com::SafeArray<IN_BSTR> arguments(ComSafeArrayInArg(aArguments));
2669 for (size_t i = 0; i < arguments.size(); i++)
2670 procInfo.mArguments.push_back(Utf8Str(arguments[i]));
2671 }
2672
2673 int rc = VINF_SUCCESS;
2674
2675 /*
2676 * Create the process environment:
2677 * - Apply the session environment in a first step, and
2678 * - Apply environment variables specified by this call to
2679 * have the chance of overwriting/deleting session entries.
2680 */
2681 procInfo.mEnvironment = mData.mEnvironment; /* Apply original session environment. */
2682
2683 if (aEnvironment)
2684 {
2685 com::SafeArray<IN_BSTR> environment(ComSafeArrayInArg(aEnvironment));
2686 for (size_t i = 0; i < environment.size() && RT_SUCCESS(rc); i++)
2687 rc = procInfo.mEnvironment.Set(Utf8Str(environment[i]));
2688 }
2689
2690 HRESULT hr = S_OK;
2691
2692 if (RT_SUCCESS(rc))
2693 {
2694 if (aFlags)
2695 {
2696 com::SafeArray<ProcessCreateFlag_T> flags(ComSafeArrayInArg(aFlags));
2697 for (size_t i = 0; i < flags.size(); i++)
2698 procInfo.mFlags |= flags[i];
2699 }
2700
2701 procInfo.mTimeoutMS = aTimeoutMS;
2702
2703 if (aAffinity)
2704 {
2705 com::SafeArray<LONG> affinity(ComSafeArrayInArg(aAffinity));
2706 for (size_t i = 0; i < affinity.size(); i++)
2707 {
2708 if (affinity[i])
2709 procInfo.mAffinity |= (uint64_t)1 << i;
2710 }
2711 }
2712
2713 procInfo.mPriority = aPriority;
2714
2715 ComObjPtr<GuestProcess> pProcess;
2716 rc = processCreateExInteral(procInfo, pProcess);
2717 if (RT_SUCCESS(rc))
2718 {
2719 /* Return guest session to the caller. */
2720 HRESULT hr2 = pProcess.queryInterfaceTo(aProcess);
2721 if (FAILED(hr2))
2722 rc = VERR_COM_OBJECT_NOT_FOUND;
2723
2724 if (RT_SUCCESS(rc))
2725 rc = pProcess->startProcessAsync();
2726 }
2727 }
2728
2729 if (RT_FAILURE(rc))
2730 {
2731 switch (rc)
2732 {
2733 case VERR_MAX_PROCS_REACHED:
2734 hr = setError(VBOX_E_IPRT_ERROR, tr("Maximum number of guest objects per session (%ld) reached"),
2735 VBOX_GUESTCTRL_MAX_OBJECTS);
2736 break;
2737
2738 /** @todo Add more errors here. */
2739
2740 default:
2741 hr = setError(VBOX_E_IPRT_ERROR, tr("Could not create guest process, rc=%Rrc"), rc);
2742 break;
2743 }
2744 }
2745
2746 LogFlowFuncLeaveRC(rc);
2747 return hr;
2748#endif /* VBOX_WITH_GUEST_CONTROL */
2749}
2750
2751STDMETHODIMP GuestSession::ProcessGet(ULONG aPID, IGuestProcess **aProcess)
2752{
2753#ifndef VBOX_WITH_GUEST_CONTROL
2754 ReturnComNotImplemented();
2755#else
2756 LogFlowThisFunc(("aPID=%RU32\n", aPID));
2757
2758 CheckComArgOutPointerValid(aProcess);
2759 if (aPID == 0)
2760 return setError(E_INVALIDARG, tr("No valid process ID (PID) specified"));
2761
2762 AutoCaller autoCaller(this);
2763 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2764
2765 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2766
2767 HRESULT hr = S_OK;
2768
2769 ComObjPtr<GuestProcess> pProcess;
2770 int rc = processGetByPID(aPID, &pProcess);
2771 if (RT_FAILURE(rc))
2772 hr = setError(E_INVALIDARG, tr("No process with PID %RU32 found"), aPID);
2773
2774 /* This will set (*aProcess) to NULL if pProgress is NULL. */
2775 HRESULT hr2 = pProcess.queryInterfaceTo(aProcess);
2776 if (SUCCEEDED(hr))
2777 hr = hr2;
2778
2779 LogFlowThisFunc(("aProcess=%p, hr=%Rhrc\n", *aProcess, hr));
2780 return hr;
2781#endif /* VBOX_WITH_GUEST_CONTROL */
2782}
2783
2784STDMETHODIMP GuestSession::SymlinkCreate(IN_BSTR aSource, IN_BSTR aTarget, SymlinkType_T aType)
2785{
2786#ifndef VBOX_WITH_GUEST_CONTROL
2787 ReturnComNotImplemented();
2788#else
2789 LogFlowThisFuncEnter();
2790
2791 AutoCaller autoCaller(this);
2792 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2793
2794 ReturnComNotImplemented();
2795#endif /* VBOX_WITH_GUEST_CONTROL */
2796}
2797
2798STDMETHODIMP GuestSession::SymlinkExists(IN_BSTR aSymlink, BOOL *aExists)
2799{
2800#ifndef VBOX_WITH_GUEST_CONTROL
2801 ReturnComNotImplemented();
2802#else
2803 LogFlowThisFuncEnter();
2804
2805 AutoCaller autoCaller(this);
2806 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2807
2808 ReturnComNotImplemented();
2809#endif /* VBOX_WITH_GUEST_CONTROL */
2810}
2811
2812STDMETHODIMP GuestSession::SymlinkRead(IN_BSTR aSymlink, ComSafeArrayIn(SymlinkReadFlag_T, aFlags), BSTR *aTarget)
2813{
2814#ifndef VBOX_WITH_GUEST_CONTROL
2815 ReturnComNotImplemented();
2816#else
2817 LogFlowThisFuncEnter();
2818
2819 AutoCaller autoCaller(this);
2820 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2821
2822 ReturnComNotImplemented();
2823#endif /* VBOX_WITH_GUEST_CONTROL */
2824}
2825
2826STDMETHODIMP GuestSession::SymlinkRemoveDirectory(IN_BSTR aPath)
2827{
2828#ifndef VBOX_WITH_GUEST_CONTROL
2829 ReturnComNotImplemented();
2830#else
2831 LogFlowThisFuncEnter();
2832
2833 AutoCaller autoCaller(this);
2834 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2835
2836 ReturnComNotImplemented();
2837#endif /* VBOX_WITH_GUEST_CONTROL */
2838}
2839
2840STDMETHODIMP GuestSession::SymlinkRemoveFile(IN_BSTR aFile)
2841{
2842#ifndef VBOX_WITH_GUEST_CONTROL
2843 ReturnComNotImplemented();
2844#else
2845 LogFlowThisFuncEnter();
2846
2847 AutoCaller autoCaller(this);
2848 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2849
2850 ReturnComNotImplemented();
2851#endif /* VBOX_WITH_GUEST_CONTROL */
2852}
2853
2854STDMETHODIMP GuestSession::WaitFor(ULONG aWaitFlags, ULONG aTimeoutMS, GuestSessionWaitResult_T *aReason)
2855{
2856#ifndef VBOX_WITH_GUEST_CONTROL
2857 ReturnComNotImplemented();
2858#else
2859 LogFlowThisFuncEnter();
2860
2861 CheckComArgOutPointerValid(aReason);
2862
2863 AutoCaller autoCaller(this);
2864 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2865
2866 /*
2867 * Note: Do not hold any locks here while waiting!
2868 */
2869 HRESULT hr = S_OK;
2870
2871 int guestRc; GuestSessionWaitResult_T waitResult;
2872 int vrc = waitFor(aWaitFlags, aTimeoutMS, waitResult, &guestRc);
2873 if (RT_SUCCESS(vrc))
2874 {
2875 *aReason = waitResult;
2876 }
2877 else
2878 {
2879 switch (vrc)
2880 {
2881 case VERR_GSTCTL_GUEST_ERROR:
2882 hr = GuestSession::setErrorExternal(this, guestRc);
2883 break;
2884
2885 case VERR_TIMEOUT:
2886 *aReason = GuestSessionWaitResult_Timeout;
2887 break;
2888
2889 default:
2890 {
2891 const char *pszSessionName = mData.mSession.mName.c_str();
2892 hr = setError(VBOX_E_IPRT_ERROR,
2893 tr("Waiting for guest session \"%s\" failed: %Rrc"),
2894 pszSessionName ? pszSessionName : tr("Unnamed"), vrc);
2895 break;
2896 }
2897 }
2898 }
2899
2900 LogFlowFuncLeaveRC(vrc);
2901 return hr;
2902#endif /* VBOX_WITH_GUEST_CONTROL */
2903}
2904
2905STDMETHODIMP GuestSession::WaitForArray(ComSafeArrayIn(GuestSessionWaitForFlag_T, aFlags), ULONG aTimeoutMS, GuestSessionWaitResult_T *aReason)
2906{
2907#ifndef VBOX_WITH_GUEST_CONTROL
2908 ReturnComNotImplemented();
2909#else
2910 LogFlowThisFuncEnter();
2911
2912 CheckComArgOutPointerValid(aReason);
2913
2914 AutoCaller autoCaller(this);
2915 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2916
2917 /*
2918 * Note: Do not hold any locks here while waiting!
2919 */
2920 uint32_t fWaitFor = GuestSessionWaitForFlag_None;
2921 com::SafeArray<GuestSessionWaitForFlag_T> flags(ComSafeArrayInArg(aFlags));
2922 for (size_t i = 0; i < flags.size(); i++)
2923 fWaitFor |= flags[i];
2924
2925 return WaitFor(fWaitFor, aTimeoutMS, aReason);
2926#endif /* VBOX_WITH_GUEST_CONTROL */
2927}
2928
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