1 |
|
---|
2 | /* $Id: GuestSessionImpl.h 42749 2012-08-10 10:19:57Z 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 | int setProgress(ULONG uPercent);
|
---|
52 | int setProgressSuccess(void);
|
---|
53 | HRESULT setProgressErrorMsg(HRESULT hr, const Utf8Str &strMsg);
|
---|
54 |
|
---|
55 | protected:
|
---|
56 |
|
---|
57 | Utf8Str mDesc;
|
---|
58 | GuestSession *mSession;
|
---|
59 | /** Progress object for getting updated when running
|
---|
60 | * asynchronously. Optional. */
|
---|
61 | ComObjPtr<Progress> mProgress;
|
---|
62 | };
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Task for copying files from host to the guest.
|
---|
66 | */
|
---|
67 | class SessionTaskCopyTo : public GuestSessionTask
|
---|
68 | {
|
---|
69 | public:
|
---|
70 |
|
---|
71 | SessionTaskCopyTo(GuestSession *pSession,
|
---|
72 | const Utf8Str &strSource, const Utf8Str &strDest, uint32_t uFlags);
|
---|
73 |
|
---|
74 | SessionTaskCopyTo(GuestSession *pSession,
|
---|
75 | PRTFILE pSourceFile, size_t cbSourceOffset, uint64_t cbSourceSize,
|
---|
76 | const Utf8Str &strDest, uint32_t uFlags);
|
---|
77 |
|
---|
78 | virtual ~SessionTaskCopyTo(void);
|
---|
79 |
|
---|
80 | public:
|
---|
81 |
|
---|
82 | int Run(void);
|
---|
83 | int RunAsync(const Utf8Str &strDesc, ComObjPtr<Progress> &pProgress);
|
---|
84 | static int taskThread(RTTHREAD Thread, void *pvUser);
|
---|
85 |
|
---|
86 | protected:
|
---|
87 |
|
---|
88 | Utf8Str mSource;
|
---|
89 | PRTFILE mSourceFile;
|
---|
90 | size_t mSourceOffset;
|
---|
91 | uint64_t mSourceSize;
|
---|
92 | Utf8Str mDest;
|
---|
93 | uint32_t mCopyFileFlags;
|
---|
94 | };
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Task for copying files from guest to the host.
|
---|
98 | */
|
---|
99 | class SessionTaskCopyFrom : public GuestSessionTask
|
---|
100 | {
|
---|
101 | public:
|
---|
102 |
|
---|
103 | SessionTaskCopyFrom(GuestSession *pSession,
|
---|
104 | const Utf8Str &strSource, const Utf8Str &strDest, uint32_t uFlags);
|
---|
105 |
|
---|
106 | virtual ~SessionTaskCopyFrom(void);
|
---|
107 |
|
---|
108 | public:
|
---|
109 |
|
---|
110 | int Run(void);
|
---|
111 | int RunAsync(const Utf8Str &strDesc, ComObjPtr<Progress> &pProgress);
|
---|
112 | static int taskThread(RTTHREAD Thread, void *pvUser);
|
---|
113 |
|
---|
114 | protected:
|
---|
115 |
|
---|
116 | Utf8Str mSource;
|
---|
117 | Utf8Str mDest;
|
---|
118 | uint32_t mFlags;
|
---|
119 | };
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Task for automatically updating the Guest Additions on the guest.
|
---|
123 | */
|
---|
124 | class SessionTaskUpdateAdditions : public GuestSessionTask
|
---|
125 | {
|
---|
126 | public:
|
---|
127 |
|
---|
128 | SessionTaskUpdateAdditions(GuestSession *pSession,
|
---|
129 | const Utf8Str &strSource, uint32_t uFlags);
|
---|
130 |
|
---|
131 | virtual ~SessionTaskUpdateAdditions(void);
|
---|
132 |
|
---|
133 | public:
|
---|
134 |
|
---|
135 | int Run(void);
|
---|
136 | int RunAsync(const Utf8Str &strDesc, ComObjPtr<Progress> &pProgress);
|
---|
137 | static int taskThread(RTTHREAD Thread, void *pvUser);
|
---|
138 |
|
---|
139 | protected:
|
---|
140 |
|
---|
141 | int copyFileToGuest(GuestSession *pSession, PRTISOFSFILE pISO,
|
---|
142 | Utf8Str const &strFileSource, const Utf8Str &strFileDest,
|
---|
143 | bool fOptional, uint32_t *pcbSize);
|
---|
144 | int runFile(GuestSession *pSession, GuestProcessStartupInfo &procInfo);
|
---|
145 |
|
---|
146 | /** The (optionally) specified Guest Additions .ISO on the host
|
---|
147 | * which will be used for the updating process. */
|
---|
148 | Utf8Str mSource;
|
---|
149 | /** Update flags. */
|
---|
150 | uint32_t mFlags;
|
---|
151 | };
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * Guest session implementation.
|
---|
155 | */
|
---|
156 | class ATL_NO_VTABLE GuestSession :
|
---|
157 | public VirtualBoxBase,
|
---|
158 | VBOX_SCRIPTABLE_IMPL(IGuestSession)
|
---|
159 | {
|
---|
160 | public:
|
---|
161 | /** @name COM and internal init/term/mapping cruft.
|
---|
162 | * @{ */
|
---|
163 | VIRTUALBOXBASE_ADD_ERRORINFO_SUPPORT(GuestSession, IGuestSession)
|
---|
164 | DECLARE_NOT_AGGREGATABLE(GuestSession)
|
---|
165 | DECLARE_PROTECT_FINAL_CONSTRUCT()
|
---|
166 | BEGIN_COM_MAP(GuestSession)
|
---|
167 | VBOX_DEFAULT_INTERFACE_ENTRIES(IGuestSession)
|
---|
168 | END_COM_MAP()
|
---|
169 | DECLARE_EMPTY_CTOR_DTOR(GuestSession)
|
---|
170 |
|
---|
171 | int init(Guest *aGuest, ULONG aSessionID, Utf8Str aUser, Utf8Str aPassword, Utf8Str aDomain, Utf8Str aName);
|
---|
172 | void uninit(void);
|
---|
173 | HRESULT FinalConstruct(void);
|
---|
174 | void FinalRelease(void);
|
---|
175 | /** @} */
|
---|
176 |
|
---|
177 | /** @name IGuestSession properties.
|
---|
178 | * @{ */
|
---|
179 | STDMETHOD(COMGETTER(User))(BSTR *aName);
|
---|
180 | STDMETHOD(COMGETTER(Domain))(BSTR *aDomain);
|
---|
181 | STDMETHOD(COMGETTER(Name))(BSTR *aName);
|
---|
182 | STDMETHOD(COMGETTER(Id))(ULONG *aId);
|
---|
183 | STDMETHOD(COMGETTER(Timeout))(ULONG *aTimeout);
|
---|
184 | STDMETHOD(COMSETTER(Timeout))(ULONG aTimeout);
|
---|
185 | STDMETHOD(COMGETTER(Environment))(ComSafeArrayOut(BSTR, aEnvironment));
|
---|
186 | STDMETHOD(COMSETTER(Environment))(ComSafeArrayIn(IN_BSTR, aEnvironment));
|
---|
187 | STDMETHOD(COMGETTER(Processes))(ComSafeArrayOut(IGuestProcess *, aProcesses));
|
---|
188 | STDMETHOD(COMGETTER(Directories))(ComSafeArrayOut(IGuestDirectory *, aDirectories));
|
---|
189 | STDMETHOD(COMGETTER(Files))(ComSafeArrayOut(IGuestFile *, aFiles));
|
---|
190 | /** @} */
|
---|
191 |
|
---|
192 | /** @name IGuestSession methods.
|
---|
193 | * @{ */
|
---|
194 | STDMETHOD(Close)(void);
|
---|
195 | STDMETHOD(CopyFrom)(IN_BSTR aSource, IN_BSTR aDest, ComSafeArrayIn(CopyFileFlag_T, aFlags), IProgress **aProgress);
|
---|
196 | STDMETHOD(CopyTo)(IN_BSTR aSource, IN_BSTR aDest, ComSafeArrayIn(CopyFileFlag_T, aFlags), IProgress **aProgress);
|
---|
197 | STDMETHOD(DirectoryCreate)(IN_BSTR aPath, ULONG aMode, ComSafeArrayIn(DirectoryCreateFlag_T, aFlags), IGuestDirectory **aDirectory);
|
---|
198 | STDMETHOD(DirectoryCreateTemp)(IN_BSTR aTemplate, ULONG aMode, IN_BSTR aName, IGuestDirectory **aDirectory);
|
---|
199 | STDMETHOD(DirectoryExists)(IN_BSTR aPath, BOOL *aExists);
|
---|
200 | STDMETHOD(DirectoryOpen)(IN_BSTR aPath, IN_BSTR aFilter, ComSafeArrayIn(DirectoryOpenFlag_T, aFlags), IGuestDirectory **aDirectory);
|
---|
201 | STDMETHOD(DirectoryQueryInfo)(IN_BSTR aPath, IGuestFsObjInfo **aInfo);
|
---|
202 | STDMETHOD(DirectoryRemove)(IN_BSTR aPath);
|
---|
203 | STDMETHOD(DirectoryRemoveRecursive)(IN_BSTR aPath, ComSafeArrayIn(DirectoryRemoveRecFlag_T, aFlags), IProgress **aProgress);
|
---|
204 | STDMETHOD(DirectoryRename)(IN_BSTR aSource, IN_BSTR aDest, ComSafeArrayIn(PathRenameFlag_T, aFlags));
|
---|
205 | STDMETHOD(DirectorySetACL)(IN_BSTR aPath, IN_BSTR aACL);
|
---|
206 | STDMETHOD(EnvironmentClear)(void);
|
---|
207 | STDMETHOD(EnvironmentGet)(IN_BSTR aName, BSTR *aValue);
|
---|
208 | STDMETHOD(EnvironmentSet)(IN_BSTR aName, IN_BSTR aValue);
|
---|
209 | STDMETHOD(EnvironmentUnset)(IN_BSTR aName);
|
---|
210 | STDMETHOD(FileCreateTemp)(IN_BSTR aTemplate, ULONG aMode, IN_BSTR aName, IGuestFile **aFile);
|
---|
211 | STDMETHOD(FileExists)(IN_BSTR aPath, BOOL *aExists);
|
---|
212 | STDMETHOD(FileRemove)(IN_BSTR aPath);
|
---|
213 | STDMETHOD(FileOpen)(IN_BSTR aPath, IN_BSTR aOpenMode, IN_BSTR aDisposition, ULONG aCreationMode, LONG64 aOffset, IGuestFile **aFile);
|
---|
214 | STDMETHOD(FileQueryInfo)(IN_BSTR aPath, IGuestFsObjInfo **aInfo);
|
---|
215 | STDMETHOD(FileQuerySize)(IN_BSTR aPath, LONG64 *aSize);
|
---|
216 | STDMETHOD(FileRename)(IN_BSTR aSource, IN_BSTR aDest, ComSafeArrayIn(PathRenameFlag_T, aFlags));
|
---|
217 | STDMETHOD(FileSetACL)(IN_BSTR aPath, IN_BSTR aACL);
|
---|
218 | STDMETHOD(ProcessCreate)(IN_BSTR aCommand, ComSafeArrayIn(IN_BSTR, aArguments), ComSafeArrayIn(IN_BSTR, aEnvironment),
|
---|
219 | ComSafeArrayIn(ProcessCreateFlag_T, aFlags), ULONG aTimeoutMS, IGuestProcess **aProcess);
|
---|
220 | STDMETHOD(ProcessCreateEx)(IN_BSTR aCommand, ComSafeArrayIn(IN_BSTR, aArguments), ComSafeArrayIn(IN_BSTR, aEnvironment),
|
---|
221 | ComSafeArrayIn(ProcessCreateFlag_T, aFlags), ULONG aTimeoutMS,
|
---|
222 | ProcessPriority_T aPriority, ComSafeArrayIn(LONG, aAffinity),
|
---|
223 | IGuestProcess **aProcess);
|
---|
224 | STDMETHOD(ProcessGet)(ULONG aPID, IGuestProcess **aProcess);
|
---|
225 | #if 0
|
---|
226 | STDMETHOD(SetTimeout)(ULONG aTimeoutMS);
|
---|
227 | #endif
|
---|
228 | STDMETHOD(SymlinkCreate)(IN_BSTR aSource, IN_BSTR aTarget, SymlinkType_T aType);
|
---|
229 | STDMETHOD(SymlinkExists)(IN_BSTR aSymlink, BOOL *aExists);
|
---|
230 | STDMETHOD(SymlinkRead)(IN_BSTR aSymlink, ComSafeArrayIn(SymlinkReadFlag_T, aFlags), BSTR *aTarget);
|
---|
231 | STDMETHOD(SymlinkRemoveDirectory)(IN_BSTR aPath);
|
---|
232 | STDMETHOD(SymlinkRemoveFile)(IN_BSTR aFile);
|
---|
233 | /** @} */
|
---|
234 |
|
---|
235 | private:
|
---|
236 |
|
---|
237 | typedef std::vector <ComObjPtr<GuestDirectory> > SessionDirectories;
|
---|
238 | typedef std::vector <ComObjPtr<GuestFile> > SessionFiles;
|
---|
239 | /** Map of guest processes. The key specifies the internal process number.
|
---|
240 | * To retrieve the process' guest PID use the Id() method of the IProcess interface. */
|
---|
241 | typedef std::map <uint32_t, ComObjPtr<GuestProcess> > SessionProcesses;
|
---|
242 |
|
---|
243 | public:
|
---|
244 | /** @name Public internal methods.
|
---|
245 | * @{ */
|
---|
246 | int directoryClose(ComObjPtr<GuestDirectory> pDirectory);
|
---|
247 | int directoryCreateInternal(const Utf8Str &strPath, uint32_t uMode, uint32_t uFlags, ComObjPtr<GuestDirectory> &pDirectory);
|
---|
248 | int directoryOpenInternal(const Utf8Str &strPath, const Utf8Str &strFilter, uint32_t uFlags, ComObjPtr<GuestDirectory> &pDirectory);
|
---|
249 | int dispatchToProcess(uint32_t uContextID, uint32_t uFunction, void *pvData, size_t cbData);
|
---|
250 | int fileClose(ComObjPtr<GuestFile> pFile);
|
---|
251 | int fileRemoveInternal(Utf8Str strPath, int *prc);
|
---|
252 | int fileOpenInternal(const Utf8Str &strPath, const Utf8Str &strOpenMode, const Utf8Str &strDisposition,
|
---|
253 | uint32_t uCreationMode, int64_t iOffset, ComObjPtr<GuestFile> &pFile);
|
---|
254 | int fileQueryInfoInternal(const Utf8Str &strPath, GuestFsObjData &objData);
|
---|
255 | int fileQuerySizeInternal(const Utf8Str &strPath, int64_t *pllSize);
|
---|
256 | const GuestCredentials &getCredentials(void);
|
---|
257 | const GuestEnvironment &getEnvironment(void);
|
---|
258 | Utf8Str getName(void);
|
---|
259 | Guest *getParent(void) { return mData.mParent; }
|
---|
260 | uint32_t getProtocolVersion(void) { return mData.mProtocolVersion; }
|
---|
261 | int processClose(ComObjPtr<GuestProcess> pProcess);
|
---|
262 | int processCreateExInteral(GuestProcessStartupInfo &procInfo, ComObjPtr<GuestProcess> &pProgress);
|
---|
263 | inline bool processExists(uint32_t uProcessID, ComObjPtr<GuestProcess> *pProcess);
|
---|
264 | inline int processGetByPID(ULONG uPID, ComObjPtr<GuestProcess> *pProcess);
|
---|
265 | int startTaskAsync(const Utf8Str &strTaskDesc, GuestSessionTask *pTask, ComObjPtr<Progress> &pProgress);
|
---|
266 | int queryInfo(void);
|
---|
267 | /** @} */
|
---|
268 |
|
---|
269 | private:
|
---|
270 |
|
---|
271 | struct Data
|
---|
272 | {
|
---|
273 | /** Guest control protocol version to be used.
|
---|
274 | * Guest Additions < VBox 4.2 have version 1,
|
---|
275 | * any newer version will have version 2. */
|
---|
276 | uint32_t mProtocolVersion;
|
---|
277 | /** Flag indicating if this is an internal session
|
---|
278 | * or not. Internal session are not accessible by clients. */
|
---|
279 | bool fInternal;
|
---|
280 | /** Pointer to the parent (Guest). */
|
---|
281 | Guest *mParent;
|
---|
282 | /** The session credentials. */
|
---|
283 | GuestCredentials mCredentials;
|
---|
284 | /** The (optional) session name. */
|
---|
285 | Utf8Str mName;
|
---|
286 | /** The session ID. */
|
---|
287 | ULONG mId;
|
---|
288 | /** The session timeout. Default is 30s. */
|
---|
289 | ULONG mTimeout;
|
---|
290 | /** The session's environment block. Can be
|
---|
291 | * overwritten/extended by ProcessCreate(Ex). */
|
---|
292 | GuestEnvironment mEnvironment;
|
---|
293 | /** Directory objects bound to this session. */
|
---|
294 | SessionDirectories mDirectories;
|
---|
295 | /** File objects bound to this session. */
|
---|
296 | SessionFiles mFiles;
|
---|
297 | /** Process objects bound to this session. */
|
---|
298 | SessionProcesses mProcesses;
|
---|
299 | } mData;
|
---|
300 | };
|
---|
301 |
|
---|
302 | #endif /* !____H_GUESTSESSIONIMPL */
|
---|
303 |
|
---|