VirtualBox

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

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