VirtualBox

source: vbox/trunk/src/VBox/Main/include/GuestSessionImpl.h@ 45733

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

GuestCtrl: Implemented using (public) VirtualBox events instead of own callback mechanisms. Bugfixes for testcases (still work in progress).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.0 KB
Line 
1
2/* $Id: GuestSessionImpl.h 45415 2013-04-08 21:40:42Z 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#ifndef ____H_GUESTSESSIONIMPL
20#define ____H_GUESTSESSIONIMPL
21
22#include "VirtualBoxBase.h"
23#include "EventImpl.h"
24
25#include "GuestCtrlImplPrivate.h"
26#include "GuestProcessImpl.h"
27#include "GuestDirectoryImpl.h"
28#include "GuestFileImpl.h"
29#include "GuestFsObjInfoImpl.h"
30
31#include <iprt/isofs.h> /* For UpdateAdditions. */
32
33class Guest;
34
35/**
36 * Abstract base class for a lenghtly per-session operation which
37 * runs in a Main worker thread.
38 */
39class GuestSessionTask
40{
41public:
42
43 GuestSessionTask(GuestSession *pSession);
44
45 virtual ~GuestSessionTask(void);
46
47public:
48
49 virtual int Run(void) = 0;
50 virtual int RunAsync(const Utf8Str &strDesc, ComObjPtr<Progress> &pProgress) = 0;
51
52protected:
53
54 int getGuestProperty(const ComObjPtr<Guest> &pGuest,
55 const Utf8Str &strPath, Utf8Str &strValue);
56 int setProgress(ULONG uPercent);
57 int setProgressSuccess(void);
58 HRESULT setProgressErrorMsg(HRESULT hr, const Utf8Str &strMsg);
59
60protected:
61
62 Utf8Str mDesc;
63 GuestSession *mSession;
64 /** Progress object for getting updated when running
65 * asynchronously. Optional. */
66 ComObjPtr<Progress> mProgress;
67};
68
69/**
70 * Task for opening a guest session.
71 */
72class SessionTaskOpen : public GuestSessionTask
73{
74public:
75
76 SessionTaskOpen(GuestSession *pSession,
77 uint32_t uFlags,
78 uint32_t uTimeoutMS);
79
80 virtual ~SessionTaskOpen(void);
81
82public:
83
84 int Run(int *pGuestRc);
85 int RunAsync(const Utf8Str &strDesc, ComObjPtr<Progress> &pProgress);
86 static int taskThread(RTTHREAD Thread, void *pvUser);
87
88protected:
89
90 /** Session creation flags. */
91 uint32_t mFlags;
92 /** Session creation timeout (in ms). */
93 uint32_t mTimeoutMS;
94};
95
96/**
97 * Task for copying files from host to the guest.
98 */
99class SessionTaskCopyTo : public GuestSessionTask
100{
101public:
102
103 SessionTaskCopyTo(GuestSession *pSession,
104 const Utf8Str &strSource, const Utf8Str &strDest, uint32_t uFlags);
105
106 SessionTaskCopyTo(GuestSession *pSession,
107 PRTFILE pSourceFile, size_t cbSourceOffset, uint64_t cbSourceSize,
108 const Utf8Str &strDest, uint32_t uFlags);
109
110 virtual ~SessionTaskCopyTo(void);
111
112public:
113
114 int Run(void);
115 int RunAsync(const Utf8Str &strDesc, ComObjPtr<Progress> &pProgress);
116 static int taskThread(RTTHREAD Thread, void *pvUser);
117
118protected:
119
120 Utf8Str mSource;
121 PRTFILE mSourceFile;
122 size_t mSourceOffset;
123 uint64_t mSourceSize;
124 Utf8Str mDest;
125 uint32_t mCopyFileFlags;
126};
127
128/**
129 * Task for copying files from guest to the host.
130 */
131class SessionTaskCopyFrom : public GuestSessionTask
132{
133public:
134
135 SessionTaskCopyFrom(GuestSession *pSession,
136 const Utf8Str &strSource, const Utf8Str &strDest, uint32_t uFlags);
137
138 virtual ~SessionTaskCopyFrom(void);
139
140public:
141
142 int Run(void);
143 int RunAsync(const Utf8Str &strDesc, ComObjPtr<Progress> &pProgress);
144 static int taskThread(RTTHREAD Thread, void *pvUser);
145
146protected:
147
148 Utf8Str mSource;
149 Utf8Str mDest;
150 uint32_t mFlags;
151};
152
153/**
154 * Task for automatically updating the Guest Additions on the guest.
155 */
156class SessionTaskUpdateAdditions : public GuestSessionTask
157{
158public:
159
160 SessionTaskUpdateAdditions(GuestSession *pSession,
161 const Utf8Str &strSource, uint32_t uFlags);
162
163 virtual ~SessionTaskUpdateAdditions(void);
164
165public:
166
167 int Run(void);
168 int RunAsync(const Utf8Str &strDesc, ComObjPtr<Progress> &pProgress);
169 static int taskThread(RTTHREAD Thread, void *pvUser);
170
171protected:
172
173 /**
174 * Suported OS types for automatic updating.
175 */
176 enum eOSType
177 {
178 eOSType_Unknown = 0,
179 eOSType_Windows = 1,
180 eOSType_Linux = 2,
181 eOSType_Solaris = 3
182 };
183
184 /**
185 * Structure representing a file to
186 * get off the .ISO, copied to the guest.
187 */
188 struct InstallerFile
189 {
190 InstallerFile(const Utf8Str &aSource,
191 const Utf8Str &aDest,
192 uint32_t aFlags = 0)
193 : strSource(aSource),
194 strDest(aDest),
195 fFlags(aFlags) { }
196
197 InstallerFile(const Utf8Str &aSource,
198 const Utf8Str &aDest,
199 uint32_t aFlags,
200 GuestProcessStartupInfo startupInfo)
201 : strSource(aSource),
202 strDest(aDest),
203 fFlags(aFlags),
204 mProcInfo(startupInfo)
205 {
206 mProcInfo.mCommand = strDest;
207 if (mProcInfo.mName.isEmpty())
208 mProcInfo.mName = strDest;
209 }
210
211 /** Source file on .ISO. */
212 Utf8Str strSource;
213 /** Destination file on the guest. */
214 Utf8Str strDest;
215 /** File flags. */
216 uint32_t fFlags;
217 /** Optional arguments if this file needs to be
218 * executed. */
219 GuestProcessStartupInfo mProcInfo;
220 };
221
222 int copyFileToGuest(GuestSession *pSession, PRTISOFSFILE pISO,
223 Utf8Str const &strFileSource, const Utf8Str &strFileDest,
224 bool fOptional, uint32_t *pcbSize);
225 int runFileOnGuest(GuestSession *pSession, GuestProcessStartupInfo &procInfo);
226
227 /** Files to handle. */
228 std::vector<InstallerFile> mFiles;
229 /** The (optionally) specified Guest Additions .ISO on the host
230 * which will be used for the updating process. */
231 Utf8Str mSource;
232 /** Update flags. */
233 uint32_t mFlags;
234};
235
236/**
237 * Guest session implementation.
238 */
239class ATL_NO_VTABLE GuestSession :
240 public VirtualBoxBase,
241 public GuestBase,
242 VBOX_SCRIPTABLE_IMPL(IGuestSession)
243{
244public:
245 /** @name COM and internal init/term/mapping cruft.
246 * @{ */
247 VIRTUALBOXBASE_ADD_ERRORINFO_SUPPORT(GuestSession, IGuestSession)
248 DECLARE_NOT_AGGREGATABLE(GuestSession)
249 DECLARE_PROTECT_FINAL_CONSTRUCT()
250 BEGIN_COM_MAP(GuestSession)
251 VBOX_DEFAULT_INTERFACE_ENTRIES(IGuestSession)
252 END_COM_MAP()
253 DECLARE_EMPTY_CTOR_DTOR(GuestSession)
254
255 int init(Guest *pGuest, const GuestSessionStartupInfo &ssInfo, const GuestCredentials &guestCreds);
256 void uninit(void);
257 HRESULT FinalConstruct(void);
258 void FinalRelease(void);
259 /** @} */
260
261 /** @name IGuestSession properties.
262 * @{ */
263 STDMETHOD(COMGETTER(User))(BSTR *aName);
264 STDMETHOD(COMGETTER(Domain))(BSTR *aDomain);
265 STDMETHOD(COMGETTER(Name))(BSTR *aName);
266 STDMETHOD(COMGETTER(Id))(ULONG *aId);
267 STDMETHOD(COMGETTER(Status))(GuestSessionStatus_T *aStatus);
268 STDMETHOD(COMGETTER(Timeout))(ULONG *aTimeout);
269 STDMETHOD(COMSETTER(Timeout))(ULONG aTimeout);
270 STDMETHOD(COMGETTER(Environment))(ComSafeArrayOut(BSTR, aEnvironment));
271 STDMETHOD(COMSETTER(Environment))(ComSafeArrayIn(IN_BSTR, aEnvironment));
272 STDMETHOD(COMGETTER(Processes))(ComSafeArrayOut(IGuestProcess *, aProcesses));
273 STDMETHOD(COMGETTER(Directories))(ComSafeArrayOut(IGuestDirectory *, aDirectories));
274 STDMETHOD(COMGETTER(Files))(ComSafeArrayOut(IGuestFile *, aFiles));
275 STDMETHOD(COMGETTER(EventSource))(IEventSource ** aEventSource);
276 /** @} */
277
278 /** @name IGuestSession methods.
279 * @{ */
280 STDMETHOD(Close)(void);
281 STDMETHOD(CopyFrom)(IN_BSTR aSource, IN_BSTR aDest, ComSafeArrayIn(CopyFileFlag_T, aFlags), IProgress **aProgress);
282 STDMETHOD(CopyTo)(IN_BSTR aSource, IN_BSTR aDest, ComSafeArrayIn(CopyFileFlag_T, aFlags), IProgress **aProgress);
283 STDMETHOD(DirectoryCreate)(IN_BSTR aPath, ULONG aMode, ComSafeArrayIn(DirectoryCreateFlag_T, aFlags));
284 STDMETHOD(DirectoryCreateTemp)(IN_BSTR aTemplate, ULONG aMode, IN_BSTR aPath, BOOL aSecure, BSTR *aDirectory);
285 STDMETHOD(DirectoryExists)(IN_BSTR aPath, BOOL *aExists);
286 STDMETHOD(DirectoryOpen)(IN_BSTR aPath, IN_BSTR aFilter, ComSafeArrayIn(DirectoryOpenFlag_T, aFlags), IGuestDirectory **aDirectory);
287 STDMETHOD(DirectoryQueryInfo)(IN_BSTR aPath, IGuestFsObjInfo **aInfo);
288 STDMETHOD(DirectoryRemove)(IN_BSTR aPath);
289 STDMETHOD(DirectoryRemoveRecursive)(IN_BSTR aPath, ComSafeArrayIn(DirectoryRemoveRecFlag_T, aFlags), IProgress **aProgress);
290 STDMETHOD(DirectoryRename)(IN_BSTR aSource, IN_BSTR aDest, ComSafeArrayIn(PathRenameFlag_T, aFlags));
291 STDMETHOD(DirectorySetACL)(IN_BSTR aPath, IN_BSTR aACL);
292 STDMETHOD(EnvironmentClear)(void);
293 STDMETHOD(EnvironmentGet)(IN_BSTR aName, BSTR *aValue);
294 STDMETHOD(EnvironmentSet)(IN_BSTR aName, IN_BSTR aValue);
295 STDMETHOD(EnvironmentUnset)(IN_BSTR aName);
296 STDMETHOD(FileCreateTemp)(IN_BSTR aTemplate, ULONG aMode, IN_BSTR aPath, BOOL aSecure, IGuestFile **aFile);
297 STDMETHOD(FileExists)(IN_BSTR aPath, BOOL *aExists);
298 STDMETHOD(FileRemove)(IN_BSTR aPath);
299 STDMETHOD(FileOpen)(IN_BSTR aPath, IN_BSTR aOpenMode, IN_BSTR aDisposition, ULONG aCreationMode, LONG64 aOffset, IGuestFile **aFile);
300 STDMETHOD(FileQueryInfo)(IN_BSTR aPath, IGuestFsObjInfo **aInfo);
301 STDMETHOD(FileQuerySize)(IN_BSTR aPath, LONG64 *aSize);
302 STDMETHOD(FileRename)(IN_BSTR aSource, IN_BSTR aDest, ComSafeArrayIn(PathRenameFlag_T, aFlags));
303 STDMETHOD(FileSetACL)(IN_BSTR aPath, IN_BSTR aACL);
304 STDMETHOD(ProcessCreate)(IN_BSTR aCommand, ComSafeArrayIn(IN_BSTR, aArguments), ComSafeArrayIn(IN_BSTR, aEnvironment),
305 ComSafeArrayIn(ProcessCreateFlag_T, aFlags), ULONG aTimeoutMS, IGuestProcess **aProcess);
306 STDMETHOD(ProcessCreateEx)(IN_BSTR aCommand, ComSafeArrayIn(IN_BSTR, aArguments), ComSafeArrayIn(IN_BSTR, aEnvironment),
307 ComSafeArrayIn(ProcessCreateFlag_T, aFlags), ULONG aTimeoutMS,
308 ProcessPriority_T aPriority, ComSafeArrayIn(LONG, aAffinity),
309 IGuestProcess **aProcess);
310 STDMETHOD(ProcessGet)(ULONG aPID, IGuestProcess **aProcess);
311 STDMETHOD(SymlinkCreate)(IN_BSTR aSource, IN_BSTR aTarget, SymlinkType_T aType);
312 STDMETHOD(SymlinkExists)(IN_BSTR aSymlink, BOOL *aExists);
313 STDMETHOD(SymlinkRead)(IN_BSTR aSymlink, ComSafeArrayIn(SymlinkReadFlag_T, aFlags), BSTR *aTarget);
314 STDMETHOD(SymlinkRemoveDirectory)(IN_BSTR aPath);
315 STDMETHOD(SymlinkRemoveFile)(IN_BSTR aFile);
316 STDMETHOD(WaitFor)(ULONG aWaitFlags, ULONG aTimeoutMS, GuestSessionWaitResult_T *aReason);
317 STDMETHOD(WaitForArray)(ComSafeArrayIn(GuestSessionWaitForFlag_T, aFlags), ULONG aTimeoutMS, GuestSessionWaitResult_T *aReason);
318 /** @} */
319
320private:
321
322 typedef std::vector <ComObjPtr<GuestDirectory> > SessionDirectories;
323 /** Map of guest files. The key specifies the internal file ID. */
324 typedef std::map <uint32_t, ComObjPtr<GuestFile> > SessionFiles;
325 /** Map of guest processes. The key specifies the internal process number.
326 * To retrieve the process' guest PID use the Id() method of the IProcess interface. */
327 typedef std::map <uint32_t, ComObjPtr<GuestProcess> > SessionProcesses;
328
329public:
330 /** @name Public internal methods.
331 * @{ */
332 int closeSession(uint32_t uFlags, uint32_t uTimeoutMS, int *pGuestRc);
333 int directoryRemoveFromList(GuestDirectory *pDirectory);
334 int directoryCreateInternal(const Utf8Str &strPath, uint32_t uMode, uint32_t uFlags, int *pGuestRc);
335 int objectCreateTempInternal(const Utf8Str &strTemplate, const Utf8Str &strPath, bool fDirectory, const Utf8Str &strName, int *pGuestRc);
336 int directoryOpenInternal(const Utf8Str &strPath, const Utf8Str &strFilter, uint32_t uFlags, ComObjPtr<GuestDirectory> &pDirectory);
337 int directoryQueryInfoInternal(const Utf8Str &strPath, GuestFsObjData &objData, int *pGuestRc);
338 int dispatchToFile(PVBOXGUESTCTRLHOSTCBCTX pCtxCb, PVBOXGUESTCTRLHOSTCALLBACK pSvcCb);
339 int dispatchToProcess(PVBOXGUESTCTRLHOSTCBCTX pCtxCb, PVBOXGUESTCTRLHOSTCALLBACK pSvcCb);
340 int dispatchToThis(PVBOXGUESTCTRLHOSTCBCTX pCtxCb, PVBOXGUESTCTRLHOSTCALLBACK pSvcCb);
341 inline bool fileExists(uint32_t uFileID, ComObjPtr<GuestFile> *pFile);
342 int fileRemoveFromList(GuestFile *pFile);
343 int fileRemoveInternal(const Utf8Str &strPath, int *pGuestRc);
344 int fileOpenInternal(const GuestFileOpenInfo &openInfo, ComObjPtr<GuestFile> &pFile, int *pGuestRc);
345 int fileQueryInfoInternal(const Utf8Str &strPath, GuestFsObjData &objData, int *pGuestRc);
346 int fileQuerySizeInternal(const Utf8Str &strPath, int64_t *pllSize, int *pGuestRc);
347 int fsQueryInfoInternal(const Utf8Str &strPath, GuestFsObjData &objData, int *pGuestRc);
348 const GuestCredentials &getCredentials(void);
349 const GuestEnvironment &getEnvironment(void);
350 EventSource *getEventSource(void) { return mEventSource; }
351 Utf8Str getName(void);
352 ULONG getId(void) { return mData.mSession.mID; }
353 static Utf8Str guestErrorToString(int guestRc);
354 HRESULT isReadyExternal(void);
355 int onSessionStatusChange(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData);
356 int startSessionIntenal(int *pGuestRc);
357 int startSessionAsync(void);
358 static DECLCALLBACK(int)
359 startSessionThread(RTTHREAD Thread, void *pvUser);
360 Guest *getParent(void) { return mParent; }
361 uint32_t getProtocolVersion(void) { return mData.mProtocolVersion; }
362 int processRemoveFromList(GuestProcess *pProcess);
363 int processCreateExInteral(GuestProcessStartupInfo &procInfo, ComObjPtr<GuestProcess> &pProgress);
364 inline bool processExists(uint32_t uProcessID, ComObjPtr<GuestProcess> *pProcess);
365 inline int processGetByPID(ULONG uPID, ComObjPtr<GuestProcess> *pProcess);
366 int sendCommand(uint32_t uFunction, uint32_t uParms, PVBOXHGCMSVCPARM paParms);
367 static HRESULT setErrorExternal(VirtualBoxBase *pInterface, int guestRc);
368 int setSessionStatus(GuestSessionStatus_T sessionStatus, int sessionRc);
369 int signalWaiters(GuestSessionWaitResult_T enmWaitResult, int rc /*= VINF_SUCCESS */);
370 int startTaskAsync(const Utf8Str &strTaskDesc, GuestSessionTask *pTask, ComObjPtr<Progress> &pProgress);
371 int queryInfo(void);
372 int waitFor(uint32_t fWaitFlags, ULONG uTimeoutMS, GuestSessionWaitResult_T &waitResult, int *pGuestRc);
373 int waitForStateChange(uint32_t fWaitFlags, uint32_t uTimeoutMS, GuestSessionStatus_T *pSessionStatus, int *pGuestRc);
374 /** @} */
375
376private:
377
378 /** Pointer to the parent (Guest). */
379 Guest *mParent;
380 /**
381 * This can safely be used without holding any locks.
382 * An AutoCaller suffices to prevent it being destroy while in use and
383 * internally there is a lock providing the necessary serialization.
384 */
385 const ComObjPtr<EventSource> mEventSource;
386
387 struct Data
388 {
389 /** The session credentials. */
390 GuestCredentials mCredentials;
391 /** The session's startup info. */
392 GuestSessionStartupInfo mSession;
393 /** The session's current status. */
394 GuestSessionStatus_T mStatus;
395 /** The session's environment block. Can be
396 * overwritten/extended by ProcessCreate(Ex). */
397 GuestEnvironment mEnvironment;
398 /** Directory objects bound to this session. */
399 SessionDirectories mDirectories;
400 /** File objects bound to this session. */
401 SessionFiles mFiles;
402 /** Process objects bound to this session. */
403 SessionProcesses mProcesses;
404 /** Guest control protocol version to be used.
405 * Guest Additions < VBox 4.3 have version 1,
406 * any newer version will have version 2. */
407 uint32_t mProtocolVersion;
408 /** Session timeout (in ms). */
409 uint32_t mTimeout;
410 /** Total number of session objects (processes,
411 * files, ...). */
412 uint32_t mNumObjects;
413 /** The last returned session status
414 * returned from the guest side. */
415 int mRC;
416 } mData;
417};
418
419#endif /* !____H_GUESTSESSIONIMPL */
420
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