1 |
|
---|
2 | /* $Id: GuestSessionImpl.h 42948 2012-08-23 12:12:09Z vboxsync $ */
|
---|
3 | /** @file
|
---|
4 | * VirtualBox Main - XXX.
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2012 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 |
|
---|
32 | class Guest;
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Abstract base class for a lenghtly per-session operation which
|
---|
36 | * runs in a Main worker thread.
|
---|
37 | */
|
---|
38 | class GuestSessionTask
|
---|
39 | {
|
---|
40 | public:
|
---|
41 |
|
---|
42 | GuestSessionTask(GuestSession *pSession);
|
---|
43 |
|
---|
44 | virtual ~GuestSessionTask(void);
|
---|
45 |
|
---|
46 | public:
|
---|
47 |
|
---|
48 | virtual int Run(void) = 0;
|
---|
49 | virtual int RunAsync(const Utf8Str &strDesc, ComObjPtr<Progress> &pProgress) = 0;
|
---|
50 |
|
---|
51 | protected:
|
---|
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 |
|
---|
59 | protected:
|
---|
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 copying files from host to the guest.
|
---|
70 | */
|
---|
71 | class SessionTaskCopyTo : public GuestSessionTask
|
---|
72 | {
|
---|
73 | public:
|
---|
74 |
|
---|
75 | SessionTaskCopyTo(GuestSession *pSession,
|
---|
76 | const Utf8Str &strSource, const Utf8Str &strDest, uint32_t uFlags);
|
---|
77 |
|
---|
78 | SessionTaskCopyTo(GuestSession *pSession,
|
---|
79 | PRTFILE pSourceFile, size_t cbSourceOffset, uint64_t cbSourceSize,
|
---|
80 | const Utf8Str &strDest, uint32_t uFlags);
|
---|
81 |
|
---|
82 | virtual ~SessionTaskCopyTo(void);
|
---|
83 |
|
---|
84 | public:
|
---|
85 |
|
---|
86 | int Run(void);
|
---|
87 | int RunAsync(const Utf8Str &strDesc, ComObjPtr<Progress> &pProgress);
|
---|
88 | static int taskThread(RTTHREAD Thread, void *pvUser);
|
---|
89 |
|
---|
90 | protected:
|
---|
91 |
|
---|
92 | Utf8Str mSource;
|
---|
93 | PRTFILE mSourceFile;
|
---|
94 | size_t mSourceOffset;
|
---|
95 | uint64_t mSourceSize;
|
---|
96 | Utf8Str mDest;
|
---|
97 | uint32_t mCopyFileFlags;
|
---|
98 | };
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Task for copying files from guest to the host.
|
---|
102 | */
|
---|
103 | class SessionTaskCopyFrom : public GuestSessionTask
|
---|
104 | {
|
---|
105 | public:
|
---|
106 |
|
---|
107 | SessionTaskCopyFrom(GuestSession *pSession,
|
---|
108 | const Utf8Str &strSource, const Utf8Str &strDest, uint32_t uFlags);
|
---|
109 |
|
---|
110 | virtual ~SessionTaskCopyFrom(void);
|
---|
111 |
|
---|
112 | public:
|
---|
113 |
|
---|
114 | int Run(void);
|
---|
115 | int RunAsync(const Utf8Str &strDesc, ComObjPtr<Progress> &pProgress);
|
---|
116 | static int taskThread(RTTHREAD Thread, void *pvUser);
|
---|
117 |
|
---|
118 | protected:
|
---|
119 |
|
---|
120 | Utf8Str mSource;
|
---|
121 | Utf8Str mDest;
|
---|
122 | uint32_t mFlags;
|
---|
123 | };
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * Task for automatically updating the Guest Additions on the guest.
|
---|
127 | */
|
---|
128 | class SessionTaskUpdateAdditions : public GuestSessionTask
|
---|
129 | {
|
---|
130 | public:
|
---|
131 |
|
---|
132 | SessionTaskUpdateAdditions(GuestSession *pSession,
|
---|
133 | const Utf8Str &strSource, uint32_t uFlags);
|
---|
134 |
|
---|
135 | virtual ~SessionTaskUpdateAdditions(void);
|
---|
136 |
|
---|
137 | public:
|
---|
138 |
|
---|
139 | int Run(void);
|
---|
140 | int RunAsync(const Utf8Str &strDesc, ComObjPtr<Progress> &pProgress);
|
---|
141 | static int taskThread(RTTHREAD Thread, void *pvUser);
|
---|
142 |
|
---|
143 | protected:
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * Suported OS types for automatic updating.
|
---|
147 | */
|
---|
148 | enum eOSType
|
---|
149 | {
|
---|
150 | eOSType_Unknown = 0,
|
---|
151 | eOSType_Windows = 1,
|
---|
152 | eOSType_Linux = 2,
|
---|
153 | eOSType_Solaris = 3
|
---|
154 | };
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * Structure representing a file to
|
---|
158 | * get off the .ISO, copied to the guest.
|
---|
159 | */
|
---|
160 | struct InstallerFile
|
---|
161 | {
|
---|
162 | InstallerFile(const Utf8Str &aSource,
|
---|
163 | const Utf8Str &aDest,
|
---|
164 | uint32_t aFlags = 0)
|
---|
165 | : strSource(aSource),
|
---|
166 | strDest(aDest),
|
---|
167 | fFlags(aFlags) { }
|
---|
168 |
|
---|
169 | InstallerFile(const Utf8Str &aSource,
|
---|
170 | const Utf8Str &aDest,
|
---|
171 | uint32_t aFlags,
|
---|
172 | GuestProcessStartupInfo startupInfo)
|
---|
173 | : strSource(aSource),
|
---|
174 | strDest(aDest),
|
---|
175 | fFlags(aFlags),
|
---|
176 | mProcInfo(startupInfo)
|
---|
177 | {
|
---|
178 | mProcInfo.mCommand = strDest;
|
---|
179 | mProcInfo.mName = strDest;
|
---|
180 | }
|
---|
181 |
|
---|
182 | /** Source file on .ISO. */
|
---|
183 | Utf8Str strSource;
|
---|
184 | /** Destination file on the guest. */
|
---|
185 | Utf8Str strDest;
|
---|
186 | /** File flags. */
|
---|
187 | uint32_t fFlags;
|
---|
188 | /** Optional arguments if this file needs to be
|
---|
189 | * executed. */
|
---|
190 | GuestProcessStartupInfo mProcInfo;
|
---|
191 | };
|
---|
192 |
|
---|
193 | int copyFileToGuest(GuestSession *pSession, PRTISOFSFILE pISO,
|
---|
194 | Utf8Str const &strFileSource, const Utf8Str &strFileDest,
|
---|
195 | bool fOptional, uint32_t *pcbSize);
|
---|
196 | int runFile(GuestSession *pSession, GuestProcessStartupInfo &procInfo);
|
---|
197 |
|
---|
198 | /** Files to handle. */
|
---|
199 | std::vector<InstallerFile> mFiles;
|
---|
200 | /** The (optionally) specified Guest Additions .ISO on the host
|
---|
201 | * which will be used for the updating process. */
|
---|
202 | Utf8Str mSource;
|
---|
203 | /** Update flags. */
|
---|
204 | uint32_t mFlags;
|
---|
205 | };
|
---|
206 |
|
---|
207 | /**
|
---|
208 | * Guest session implementation.
|
---|
209 | */
|
---|
210 | class ATL_NO_VTABLE GuestSession :
|
---|
211 | public VirtualBoxBase,
|
---|
212 | VBOX_SCRIPTABLE_IMPL(IGuestSession)
|
---|
213 | {
|
---|
214 | public:
|
---|
215 | /** @name COM and internal init/term/mapping cruft.
|
---|
216 | * @{ */
|
---|
217 | VIRTUALBOXBASE_ADD_ERRORINFO_SUPPORT(GuestSession, IGuestSession)
|
---|
218 | DECLARE_NOT_AGGREGATABLE(GuestSession)
|
---|
219 | DECLARE_PROTECT_FINAL_CONSTRUCT()
|
---|
220 | BEGIN_COM_MAP(GuestSession)
|
---|
221 | VBOX_DEFAULT_INTERFACE_ENTRIES(IGuestSession)
|
---|
222 | END_COM_MAP()
|
---|
223 | DECLARE_EMPTY_CTOR_DTOR(GuestSession)
|
---|
224 |
|
---|
225 | int init(Guest *aGuest, ULONG aSessionID, Utf8Str aUser, Utf8Str aPassword, Utf8Str aDomain, Utf8Str aName);
|
---|
226 | void uninit(void);
|
---|
227 | HRESULT FinalConstruct(void);
|
---|
228 | void FinalRelease(void);
|
---|
229 | /** @} */
|
---|
230 |
|
---|
231 | /** @name IGuestSession properties.
|
---|
232 | * @{ */
|
---|
233 | STDMETHOD(COMGETTER(User))(BSTR *aName);
|
---|
234 | STDMETHOD(COMGETTER(Domain))(BSTR *aDomain);
|
---|
235 | STDMETHOD(COMGETTER(Name))(BSTR *aName);
|
---|
236 | STDMETHOD(COMGETTER(Id))(ULONG *aId);
|
---|
237 | STDMETHOD(COMGETTER(Timeout))(ULONG *aTimeout);
|
---|
238 | STDMETHOD(COMSETTER(Timeout))(ULONG aTimeout);
|
---|
239 | STDMETHOD(COMGETTER(Environment))(ComSafeArrayOut(BSTR, aEnvironment));
|
---|
240 | STDMETHOD(COMSETTER(Environment))(ComSafeArrayIn(IN_BSTR, aEnvironment));
|
---|
241 | STDMETHOD(COMGETTER(Processes))(ComSafeArrayOut(IGuestProcess *, aProcesses));
|
---|
242 | STDMETHOD(COMGETTER(Directories))(ComSafeArrayOut(IGuestDirectory *, aDirectories));
|
---|
243 | STDMETHOD(COMGETTER(Files))(ComSafeArrayOut(IGuestFile *, aFiles));
|
---|
244 | /** @} */
|
---|
245 |
|
---|
246 | /** @name IGuestSession methods.
|
---|
247 | * @{ */
|
---|
248 | STDMETHOD(Close)(void);
|
---|
249 | STDMETHOD(CopyFrom)(IN_BSTR aSource, IN_BSTR aDest, ComSafeArrayIn(CopyFileFlag_T, aFlags), IProgress **aProgress);
|
---|
250 | STDMETHOD(CopyTo)(IN_BSTR aSource, IN_BSTR aDest, ComSafeArrayIn(CopyFileFlag_T, aFlags), IProgress **aProgress);
|
---|
251 | STDMETHOD(DirectoryCreate)(IN_BSTR aPath, ULONG aMode, ComSafeArrayIn(DirectoryCreateFlag_T, aFlags));
|
---|
252 | STDMETHOD(DirectoryCreateTemp)(IN_BSTR aTemplate, ULONG aMode, IN_BSTR aPath, BOOL aSecure, BSTR *aDirectory);
|
---|
253 | STDMETHOD(DirectoryExists)(IN_BSTR aPath, BOOL *aExists);
|
---|
254 | STDMETHOD(DirectoryOpen)(IN_BSTR aPath, IN_BSTR aFilter, ComSafeArrayIn(DirectoryOpenFlag_T, aFlags), IGuestDirectory **aDirectory);
|
---|
255 | STDMETHOD(DirectoryQueryInfo)(IN_BSTR aPath, IGuestFsObjInfo **aInfo);
|
---|
256 | STDMETHOD(DirectoryRemove)(IN_BSTR aPath);
|
---|
257 | STDMETHOD(DirectoryRemoveRecursive)(IN_BSTR aPath, ComSafeArrayIn(DirectoryRemoveRecFlag_T, aFlags), IProgress **aProgress);
|
---|
258 | STDMETHOD(DirectoryRename)(IN_BSTR aSource, IN_BSTR aDest, ComSafeArrayIn(PathRenameFlag_T, aFlags));
|
---|
259 | STDMETHOD(DirectorySetACL)(IN_BSTR aPath, IN_BSTR aACL);
|
---|
260 | STDMETHOD(EnvironmentClear)(void);
|
---|
261 | STDMETHOD(EnvironmentGet)(IN_BSTR aName, BSTR *aValue);
|
---|
262 | STDMETHOD(EnvironmentSet)(IN_BSTR aName, IN_BSTR aValue);
|
---|
263 | STDMETHOD(EnvironmentUnset)(IN_BSTR aName);
|
---|
264 | STDMETHOD(FileCreateTemp)(IN_BSTR aTemplate, ULONG aMode, IN_BSTR aPath, BOOL aSecure, IGuestFile **aFile);
|
---|
265 | STDMETHOD(FileExists)(IN_BSTR aPath, BOOL *aExists);
|
---|
266 | STDMETHOD(FileRemove)(IN_BSTR aPath);
|
---|
267 | STDMETHOD(FileOpen)(IN_BSTR aPath, IN_BSTR aOpenMode, IN_BSTR aDisposition, ULONG aCreationMode, LONG64 aOffset, IGuestFile **aFile);
|
---|
268 | STDMETHOD(FileQueryInfo)(IN_BSTR aPath, IGuestFsObjInfo **aInfo);
|
---|
269 | STDMETHOD(FileQuerySize)(IN_BSTR aPath, LONG64 *aSize);
|
---|
270 | STDMETHOD(FileRename)(IN_BSTR aSource, IN_BSTR aDest, ComSafeArrayIn(PathRenameFlag_T, aFlags));
|
---|
271 | STDMETHOD(FileSetACL)(IN_BSTR aPath, IN_BSTR aACL);
|
---|
272 | STDMETHOD(ProcessCreate)(IN_BSTR aCommand, ComSafeArrayIn(IN_BSTR, aArguments), ComSafeArrayIn(IN_BSTR, aEnvironment),
|
---|
273 | ComSafeArrayIn(ProcessCreateFlag_T, aFlags), ULONG aTimeoutMS, IGuestProcess **aProcess);
|
---|
274 | STDMETHOD(ProcessCreateEx)(IN_BSTR aCommand, ComSafeArrayIn(IN_BSTR, aArguments), ComSafeArrayIn(IN_BSTR, aEnvironment),
|
---|
275 | ComSafeArrayIn(ProcessCreateFlag_T, aFlags), ULONG aTimeoutMS,
|
---|
276 | ProcessPriority_T aPriority, ComSafeArrayIn(LONG, aAffinity),
|
---|
277 | IGuestProcess **aProcess);
|
---|
278 | STDMETHOD(ProcessGet)(ULONG aPID, IGuestProcess **aProcess);
|
---|
279 | STDMETHOD(SymlinkCreate)(IN_BSTR aSource, IN_BSTR aTarget, SymlinkType_T aType);
|
---|
280 | STDMETHOD(SymlinkExists)(IN_BSTR aSymlink, BOOL *aExists);
|
---|
281 | STDMETHOD(SymlinkRead)(IN_BSTR aSymlink, ComSafeArrayIn(SymlinkReadFlag_T, aFlags), BSTR *aTarget);
|
---|
282 | STDMETHOD(SymlinkRemoveDirectory)(IN_BSTR aPath);
|
---|
283 | STDMETHOD(SymlinkRemoveFile)(IN_BSTR aFile);
|
---|
284 | /** @} */
|
---|
285 |
|
---|
286 | private:
|
---|
287 |
|
---|
288 | typedef std::vector <ComObjPtr<GuestDirectory> > SessionDirectories;
|
---|
289 | typedef std::vector <ComObjPtr<GuestFile> > SessionFiles;
|
---|
290 | /** Map of guest processes. The key specifies the internal process number.
|
---|
291 | * To retrieve the process' guest PID use the Id() method of the IProcess interface. */
|
---|
292 | typedef std::map <uint32_t, ComObjPtr<GuestProcess> > SessionProcesses;
|
---|
293 |
|
---|
294 | public:
|
---|
295 | /** @name Public internal methods.
|
---|
296 | * @{ */
|
---|
297 | int directoryRemoveFromList(GuestDirectory *pDirectory);
|
---|
298 | int directoryCreateInternal(const Utf8Str &strPath, uint32_t uMode, uint32_t uFlags);
|
---|
299 | int objectCreateTempInternal(Utf8Str strTemplate,
|
---|
300 | Utf8Str strPath,
|
---|
301 | bool fDirectory,
|
---|
302 | Utf8Str &strName,
|
---|
303 | int *prc);
|
---|
304 | int directoryOpenInternal(const Utf8Str &strPath, const Utf8Str &strFilter, uint32_t uFlags, ComObjPtr<GuestDirectory> &pDirectory);
|
---|
305 | int directoryQueryInfoInternal(const Utf8Str &strPath, GuestFsObjData &objData);
|
---|
306 | int dispatchToProcess(uint32_t uContextID, uint32_t uFunction, void *pvData, size_t cbData);
|
---|
307 | int fileRemoveFromList(GuestFile *pFile);
|
---|
308 | int fileRemoveInternal(Utf8Str strPath, int *prc);
|
---|
309 | int fileOpenInternal(const Utf8Str &strPath, const Utf8Str &strOpenMode, const Utf8Str &strDisposition,
|
---|
310 | uint32_t uCreationMode, int64_t iOffset, ComObjPtr<GuestFile> &pFile);
|
---|
311 | int fileQueryInfoInternal(const Utf8Str &strPath, GuestFsObjData &objData);
|
---|
312 | int fileQuerySizeInternal(const Utf8Str &strPath, int64_t *pllSize);
|
---|
313 | int fsQueryInfoInternal(const Utf8Str &strPath, GuestFsObjData &objData);
|
---|
314 | const GuestCredentials &getCredentials(void);
|
---|
315 | const GuestEnvironment &getEnvironment(void);
|
---|
316 | Utf8Str getName(void);
|
---|
317 | ULONG getId(void) { return mData.mId; }
|
---|
318 | Guest *getParent(void) { return mData.mParent; }
|
---|
319 | uint32_t getProtocolVersion(void) { return mData.mProtocolVersion; }
|
---|
320 | int processRemoveFromList(GuestProcess *pProcess);
|
---|
321 | int processCreateExInteral(GuestProcessStartupInfo &procInfo, ComObjPtr<GuestProcess> &pProgress);
|
---|
322 | inline bool processExists(uint32_t uProcessID, ComObjPtr<GuestProcess> *pProcess);
|
---|
323 | inline int processGetByPID(ULONG uPID, ComObjPtr<GuestProcess> *pProcess);
|
---|
324 | int startTaskAsync(const Utf8Str &strTaskDesc, GuestSessionTask *pTask, ComObjPtr<Progress> &pProgress);
|
---|
325 | int queryInfo(void);
|
---|
326 | /** @} */
|
---|
327 |
|
---|
328 | private:
|
---|
329 |
|
---|
330 | struct Data
|
---|
331 | {
|
---|
332 | /** Guest control protocol version to be used.
|
---|
333 | * Guest Additions < VBox 4.2 have version 1,
|
---|
334 | * any newer version will have version 2. */
|
---|
335 | uint32_t mProtocolVersion;
|
---|
336 | /** Flag indicating if this is an internal session
|
---|
337 | * or not. Internal session are not accessible by clients. */
|
---|
338 | bool fInternal;
|
---|
339 | /** Pointer to the parent (Guest). */
|
---|
340 | Guest *mParent;
|
---|
341 | /** The session credentials. */
|
---|
342 | GuestCredentials mCredentials;
|
---|
343 | /** The (optional) session name. */
|
---|
344 | Utf8Str mName;
|
---|
345 | /** The session ID. */
|
---|
346 | ULONG mId;
|
---|
347 | /** The session timeout. Default is 30s. */
|
---|
348 | ULONG mTimeout;
|
---|
349 | /** The session's environment block. Can be
|
---|
350 | * overwritten/extended by ProcessCreate(Ex). */
|
---|
351 | GuestEnvironment mEnvironment;
|
---|
352 | /** Directory objects bound to this session. */
|
---|
353 | SessionDirectories mDirectories;
|
---|
354 | /** File objects bound to this session. */
|
---|
355 | SessionFiles mFiles;
|
---|
356 | /** Process objects bound to this session. */
|
---|
357 | SessionProcesses mProcesses;
|
---|
358 | /** Total number of session objects (processes,
|
---|
359 | * files, ...). */
|
---|
360 | uint32_t mNumObjects;
|
---|
361 | } mData;
|
---|
362 | };
|
---|
363 |
|
---|
364 | #endif /* !____H_GUESTSESSIONIMPL */
|
---|
365 |
|
---|