VirtualBox

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

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

Main/IGuest::UpdateGuestAdditions: Implemented support for passing optional command line arguments to the performing installer on the guest (untested).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.3 KB
Line 
1
2/* $Id: GuestSessionImpl.h 46524 2013-06-13 12:10:23Z 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, const ProcessArguments &aArguments,
162 uint32_t uFlags);
163
164 virtual ~SessionTaskUpdateAdditions(void);
165
166public:
167
168 int Run(void);
169 int RunAsync(const Utf8Str &strDesc, ComObjPtr<Progress> &pProgress);
170 static int taskThread(RTTHREAD Thread, void *pvUser);
171
172protected:
173
174 /**
175 * Suported OS types for automatic updating.
176 */
177 enum eOSType
178 {
179 eOSType_Unknown = 0,
180 eOSType_Windows = 1,
181 eOSType_Linux = 2,
182 eOSType_Solaris = 3
183 };
184
185 /**
186 * Structure representing a file to
187 * get off the .ISO, copied to the guest.
188 */
189 struct InstallerFile
190 {
191 InstallerFile(const Utf8Str &aSource,
192 const Utf8Str &aDest,
193 uint32_t aFlags = 0)
194 : strSource(aSource),
195 strDest(aDest),
196 fFlags(aFlags) { }
197
198 InstallerFile(const Utf8Str &aSource,
199 const Utf8Str &aDest,
200 uint32_t aFlags,
201 GuestProcessStartupInfo startupInfo)
202 : strSource(aSource),
203 strDest(aDest),
204 fFlags(aFlags),
205 mProcInfo(startupInfo)
206 {
207 mProcInfo.mCommand = strDest;
208 if (mProcInfo.mName.isEmpty())
209 mProcInfo.mName = strDest;
210 }
211
212 /** Source file on .ISO. */
213 Utf8Str strSource;
214 /** Destination file on the guest. */
215 Utf8Str strDest;
216 /** File flags. */
217 uint32_t fFlags;
218 /** Optional arguments if this file needs to be
219 * executed. */
220 GuestProcessStartupInfo mProcInfo;
221 };
222
223 int addProcessArguments(ProcessArguments &aArgumentsDest,
224 const ProcessArguments &aArgumentsSource);
225 int copyFileToGuest(GuestSession *pSession, PRTISOFSFILE pISO,
226 Utf8Str const &strFileSource, const Utf8Str &strFileDest,
227 bool fOptional, uint32_t *pcbSize);
228 int runFileOnGuest(GuestSession *pSession, GuestProcessStartupInfo &procInfo);
229
230 /** Files to handle. */
231 std::vector<InstallerFile> mFiles;
232 /** The (optionally) specified Guest Additions .ISO on the host
233 * which will be used for the updating process. */
234 Utf8Str mSource;
235 /** (Optional) installer command line arguments. */
236 ProcessArguments mArguments;
237 /** Update flags. */
238 uint32_t mFlags;
239};
240
241/**
242 * Guest session implementation.
243 */
244class ATL_NO_VTABLE GuestSession :
245 public VirtualBoxBase,
246 public GuestBase,
247 VBOX_SCRIPTABLE_IMPL(IGuestSession)
248{
249public:
250 /** @name COM and internal init/term/mapping cruft.
251 * @{ */
252 VIRTUALBOXBASE_ADD_ERRORINFO_SUPPORT(GuestSession, IGuestSession)
253 DECLARE_NOT_AGGREGATABLE(GuestSession)
254 DECLARE_PROTECT_FINAL_CONSTRUCT()
255 BEGIN_COM_MAP(GuestSession)
256 VBOX_DEFAULT_INTERFACE_ENTRIES(IGuestSession)
257 END_COM_MAP()
258 DECLARE_EMPTY_CTOR_DTOR(GuestSession)
259
260 int init(Guest *pGuest, const GuestSessionStartupInfo &ssInfo, const GuestCredentials &guestCreds);
261 void uninit(void);
262 HRESULT FinalConstruct(void);
263 void FinalRelease(void);
264 /** @} */
265
266 /** @name IGuestSession properties.
267 * @{ */
268 STDMETHOD(COMGETTER(User))(BSTR *aName);
269 STDMETHOD(COMGETTER(Domain))(BSTR *aDomain);
270 STDMETHOD(COMGETTER(Name))(BSTR *aName);
271 STDMETHOD(COMGETTER(Id))(ULONG *aId);
272 STDMETHOD(COMGETTER(Status))(GuestSessionStatus_T *aStatus);
273 STDMETHOD(COMGETTER(Timeout))(ULONG *aTimeout);
274 STDMETHOD(COMSETTER(Timeout))(ULONG aTimeout);
275 STDMETHOD(COMGETTER(Environment))(ComSafeArrayOut(BSTR, aEnvironment));
276 STDMETHOD(COMSETTER(Environment))(ComSafeArrayIn(IN_BSTR, aEnvironment));
277 STDMETHOD(COMGETTER(Processes))(ComSafeArrayOut(IGuestProcess *, aProcesses));
278 STDMETHOD(COMGETTER(Directories))(ComSafeArrayOut(IGuestDirectory *, aDirectories));
279 STDMETHOD(COMGETTER(Files))(ComSafeArrayOut(IGuestFile *, aFiles));
280 STDMETHOD(COMGETTER(EventSource))(IEventSource ** aEventSource);
281 /** @} */
282
283 /** @name IGuestSession methods.
284 * @{ */
285 STDMETHOD(Close)(void);
286 STDMETHOD(CopyFrom)(IN_BSTR aSource, IN_BSTR aDest, ComSafeArrayIn(CopyFileFlag_T, aFlags), IProgress **aProgress);
287 STDMETHOD(CopyTo)(IN_BSTR aSource, IN_BSTR aDest, ComSafeArrayIn(CopyFileFlag_T, aFlags), IProgress **aProgress);
288 STDMETHOD(DirectoryCreate)(IN_BSTR aPath, ULONG aMode, ComSafeArrayIn(DirectoryCreateFlag_T, aFlags));
289 STDMETHOD(DirectoryCreateTemp)(IN_BSTR aTemplate, ULONG aMode, IN_BSTR aPath, BOOL aSecure, BSTR *aDirectory);
290 STDMETHOD(DirectoryExists)(IN_BSTR aPath, BOOL *aExists);
291 STDMETHOD(DirectoryOpen)(IN_BSTR aPath, IN_BSTR aFilter, ComSafeArrayIn(DirectoryOpenFlag_T, aFlags), IGuestDirectory **aDirectory);
292 STDMETHOD(DirectoryQueryInfo)(IN_BSTR aPath, IGuestFsObjInfo **aInfo);
293 STDMETHOD(DirectoryRemove)(IN_BSTR aPath);
294 STDMETHOD(DirectoryRemoveRecursive)(IN_BSTR aPath, ComSafeArrayIn(DirectoryRemoveRecFlag_T, aFlags), IProgress **aProgress);
295 STDMETHOD(DirectoryRename)(IN_BSTR aSource, IN_BSTR aDest, ComSafeArrayIn(PathRenameFlag_T, aFlags));
296 STDMETHOD(DirectorySetACL)(IN_BSTR aPath, IN_BSTR aACL);
297 STDMETHOD(EnvironmentClear)(void);
298 STDMETHOD(EnvironmentGet)(IN_BSTR aName, BSTR *aValue);
299 STDMETHOD(EnvironmentSet)(IN_BSTR aName, IN_BSTR aValue);
300 STDMETHOD(EnvironmentUnset)(IN_BSTR aName);
301 STDMETHOD(FileCreateTemp)(IN_BSTR aTemplate, ULONG aMode, IN_BSTR aPath, BOOL aSecure, IGuestFile **aFile);
302 STDMETHOD(FileExists)(IN_BSTR aPath, BOOL *aExists);
303 STDMETHOD(FileRemove)(IN_BSTR aPath);
304 STDMETHOD(FileOpen)(IN_BSTR aPath, IN_BSTR aOpenMode, IN_BSTR aDisposition, ULONG aCreationMode, LONG64 aOffset, IGuestFile **aFile);
305 STDMETHOD(FileQueryInfo)(IN_BSTR aPath, IGuestFsObjInfo **aInfo);
306 STDMETHOD(FileQuerySize)(IN_BSTR aPath, LONG64 *aSize);
307 STDMETHOD(FileRename)(IN_BSTR aSource, IN_BSTR aDest, ComSafeArrayIn(PathRenameFlag_T, aFlags));
308 STDMETHOD(FileSetACL)(IN_BSTR aPath, IN_BSTR aACL);
309 STDMETHOD(ProcessCreate)(IN_BSTR aCommand, ComSafeArrayIn(IN_BSTR, aArguments), ComSafeArrayIn(IN_BSTR, aEnvironment),
310 ComSafeArrayIn(ProcessCreateFlag_T, aFlags), ULONG aTimeoutMS, IGuestProcess **aProcess);
311 STDMETHOD(ProcessCreateEx)(IN_BSTR aCommand, ComSafeArrayIn(IN_BSTR, aArguments), ComSafeArrayIn(IN_BSTR, aEnvironment),
312 ComSafeArrayIn(ProcessCreateFlag_T, aFlags), ULONG aTimeoutMS,
313 ProcessPriority_T aPriority, ComSafeArrayIn(LONG, aAffinity),
314 IGuestProcess **aProcess);
315 STDMETHOD(ProcessGet)(ULONG aPID, IGuestProcess **aProcess);
316 STDMETHOD(SymlinkCreate)(IN_BSTR aSource, IN_BSTR aTarget, SymlinkType_T aType);
317 STDMETHOD(SymlinkExists)(IN_BSTR aSymlink, BOOL *aExists);
318 STDMETHOD(SymlinkRead)(IN_BSTR aSymlink, ComSafeArrayIn(SymlinkReadFlag_T, aFlags), BSTR *aTarget);
319 STDMETHOD(SymlinkRemoveDirectory)(IN_BSTR aPath);
320 STDMETHOD(SymlinkRemoveFile)(IN_BSTR aFile);
321 STDMETHOD(WaitFor)(ULONG aWaitFlags, ULONG aTimeoutMS, GuestSessionWaitResult_T *aReason);
322 STDMETHOD(WaitForArray)(ComSafeArrayIn(GuestSessionWaitForFlag_T, aFlags), ULONG aTimeoutMS, GuestSessionWaitResult_T *aReason);
323 /** @} */
324
325private:
326
327 typedef std::vector <ComObjPtr<GuestDirectory> > SessionDirectories;
328 /** Map of guest files. The key specifies the internal file ID. */
329 typedef std::map <uint32_t, ComObjPtr<GuestFile> > SessionFiles;
330 /** Map of guest processes. The key specifies the internal process number.
331 * To retrieve the process' guest PID use the Id() method of the IProcess interface. */
332 typedef std::map <uint32_t, ComObjPtr<GuestProcess> > SessionProcesses;
333
334public:
335 /** @name Public internal methods.
336 * @{ */
337 int closeSession(uint32_t uFlags, uint32_t uTimeoutMS, int *pGuestRc);
338 int directoryRemoveFromList(GuestDirectory *pDirectory);
339 int directoryCreateInternal(const Utf8Str &strPath, uint32_t uMode, uint32_t uFlags, int *pGuestRc);
340 int objectCreateTempInternal(const Utf8Str &strTemplate, const Utf8Str &strPath, bool fDirectory, const Utf8Str &strName, int *pGuestRc);
341 int directoryOpenInternal(const Utf8Str &strPath, const Utf8Str &strFilter, uint32_t uFlags, ComObjPtr<GuestDirectory> &pDirectory);
342 int directoryQueryInfoInternal(const Utf8Str &strPath, GuestFsObjData &objData, int *pGuestRc);
343 int dispatchToFile(PVBOXGUESTCTRLHOSTCBCTX pCtxCb, PVBOXGUESTCTRLHOSTCALLBACK pSvcCb);
344 int dispatchToProcess(PVBOXGUESTCTRLHOSTCBCTX pCtxCb, PVBOXGUESTCTRLHOSTCALLBACK pSvcCb);
345 int dispatchToThis(PVBOXGUESTCTRLHOSTCBCTX pCtxCb, PVBOXGUESTCTRLHOSTCALLBACK pSvcCb);
346 inline bool fileExists(uint32_t uFileID, ComObjPtr<GuestFile> *pFile);
347 int fileRemoveFromList(GuestFile *pFile);
348 int fileRemoveInternal(const Utf8Str &strPath, int *pGuestRc);
349 int fileOpenInternal(const GuestFileOpenInfo &openInfo, ComObjPtr<GuestFile> &pFile, int *pGuestRc);
350 int fileQueryInfoInternal(const Utf8Str &strPath, GuestFsObjData &objData, int *pGuestRc);
351 int fileQuerySizeInternal(const Utf8Str &strPath, int64_t *pllSize, int *pGuestRc);
352 int fsQueryInfoInternal(const Utf8Str &strPath, GuestFsObjData &objData, int *pGuestRc);
353 const GuestCredentials &getCredentials(void);
354 const GuestEnvironment &getEnvironment(void);
355 EventSource *getEventSource(void) { return mEventSource; }
356 Utf8Str getName(void);
357 ULONG getId(void) { return mData.mSession.mID; }
358 static Utf8Str guestErrorToString(int guestRc);
359 HRESULT isReadyExternal(void);
360 int onSessionStatusChange(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData);
361 int startSessionIntenal(int *pGuestRc);
362 int startSessionAsync(void);
363 static DECLCALLBACK(int)
364 startSessionThread(RTTHREAD Thread, void *pvUser);
365 Guest *getParent(void) { return mParent; }
366 uint32_t getProtocolVersion(void) { return mData.mProtocolVersion; }
367 int processRemoveFromList(GuestProcess *pProcess);
368 int processCreateExInteral(GuestProcessStartupInfo &procInfo, ComObjPtr<GuestProcess> &pProgress);
369 inline bool processExists(uint32_t uProcessID, ComObjPtr<GuestProcess> *pProcess);
370 inline int processGetByPID(ULONG uPID, ComObjPtr<GuestProcess> *pProcess);
371 int sendCommand(uint32_t uFunction, uint32_t uParms, PVBOXHGCMSVCPARM paParms);
372 static HRESULT setErrorExternal(VirtualBoxBase *pInterface, int guestRc);
373 int setSessionStatus(GuestSessionStatus_T sessionStatus, int sessionRc);
374 int signalWaiters(GuestSessionWaitResult_T enmWaitResult, int rc /*= VINF_SUCCESS */);
375 int startTaskAsync(const Utf8Str &strTaskDesc, GuestSessionTask *pTask, ComObjPtr<Progress> &pProgress);
376 int queryInfo(void);
377 int waitFor(uint32_t fWaitFlags, ULONG uTimeoutMS, GuestSessionWaitResult_T &waitResult, int *pGuestRc);
378 int waitForStatusChange(GuestWaitEvent *pEvent, uint32_t fWaitFlags, uint32_t uTimeoutMS, GuestSessionStatus_T *pSessionStatus, int *pGuestRc);
379 /** @} */
380
381private:
382
383 /** Pointer to the parent (Guest). */
384 Guest *mParent;
385 /**
386 * This can safely be used without holding any locks.
387 * An AutoCaller suffices to prevent it being destroy while in use and
388 * internally there is a lock providing the necessary serialization.
389 */
390 const ComObjPtr<EventSource> mEventSource;
391
392 struct Data
393 {
394 /** The session credentials. */
395 GuestCredentials mCredentials;
396 /** The session's startup info. */
397 GuestSessionStartupInfo mSession;
398 /** The session's current status. */
399 GuestSessionStatus_T mStatus;
400 /** The session's environment block. Can be
401 * overwritten/extended by ProcessCreate(Ex). */
402 GuestEnvironment mEnvironment;
403 /** Directory objects bound to this session. */
404 SessionDirectories mDirectories;
405 /** File objects bound to this session. */
406 SessionFiles mFiles;
407 /** Process objects bound to this session. */
408 SessionProcesses mProcesses;
409 /** Guest control protocol version to be used.
410 * Guest Additions < VBox 4.3 have version 1,
411 * any newer version will have version 2. */
412 uint32_t mProtocolVersion;
413 /** Session timeout (in ms). */
414 uint32_t mTimeout;
415 /** Total number of session objects (processes,
416 * files, ...). */
417 uint32_t mNumObjects;
418 /** The last returned session status
419 * returned from the guest side. */
420 int mRC;
421 } mData;
422};
423
424#endif /* !____H_GUESTSESSIONIMPL */
425
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