VirtualBox

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

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

GuestCtrl: More code for guest session infrastructure handling (untested, work in progress).

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

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