1 | /** @file
|
---|
2 | *
|
---|
3 | * Internal helpers/structures for guest control functionality.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011-2012 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #ifndef ____H_GUESTIMPLPRIVATE
|
---|
19 | #define ____H_GUESTIMPLPRIVATE
|
---|
20 |
|
---|
21 | #include <iprt/semaphore.h>
|
---|
22 |
|
---|
23 | #include <VBox/com/com.h>
|
---|
24 | #include <VBox/com/ErrorInfo.h>
|
---|
25 | #include <VBox/com/string.h>
|
---|
26 | #include <VBox/com/VirtualBox.h>
|
---|
27 |
|
---|
28 | #include <map>
|
---|
29 | #include <vector>
|
---|
30 |
|
---|
31 | using namespace com;
|
---|
32 |
|
---|
33 | #ifdef VBOX_WITH_GUEST_CONTROL
|
---|
34 | # include <VBox/HostServices/GuestControlSvc.h>
|
---|
35 | using namespace guestControl;
|
---|
36 | #endif
|
---|
37 |
|
---|
38 |
|
---|
39 | /** Maximum number of guest sessions a VM can have. */
|
---|
40 | #define VBOX_GUESTCTRL_MAX_SESSIONS 255
|
---|
41 | /** Maximum of guest processes a guest session can have. */
|
---|
42 | #define VBOX_GUESTCTRL_MAX_PROCESSES 255
|
---|
43 | /** Maximum of callback contexts a guest process can have. */
|
---|
44 | #define VBOX_GUESTCTRL_MAX_CONTEXTS _64K - 1
|
---|
45 |
|
---|
46 | /** Builds a context ID out of the session ID, process ID and an
|
---|
47 | * increasing count. */
|
---|
48 | #define VBOX_GUESTCTRL_CONTEXTID_MAKE(uSession, uProcess, uCount) \
|
---|
49 | ( (uint32_t)((uSession) & 0xff) << 24 \
|
---|
50 | | (uint32_t)((uProcess) & 0xff) << 16 \
|
---|
51 | | (uint32_t)((uCount) & 0xffff) \
|
---|
52 | )
|
---|
53 | /** Gets the session ID out of a context ID. */
|
---|
54 | #define VBOX_GUESTCTRL_CONTEXTID_GET_SESSION(uContextID) \
|
---|
55 | ((uContextID) >> 24)
|
---|
56 | /** Gets the process ID out of a context ID. */
|
---|
57 | #define VBOX_GUESTCTRL_CONTEXTID_GET_PROCESS(uContextID) \
|
---|
58 | (((uContextID) >> 16) & 0xff)
|
---|
59 | /** Gets the conext count of a process out of a context ID. */
|
---|
60 | #define VBOX_GUESTCTRL_CONTEXTID_GET_COUNT(uContextID) \
|
---|
61 | ((uContextID) & 0xffff)
|
---|
62 |
|
---|
63 |
|
---|
64 | typedef std::vector <LONG> ProcessAffinity;
|
---|
65 | typedef std::vector <Utf8Str> ProcessArguments;
|
---|
66 |
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Generic class for a all guest control callbacks.
|
---|
70 | */
|
---|
71 | class GuestCtrlCallback
|
---|
72 | {
|
---|
73 | public:
|
---|
74 |
|
---|
75 | GuestCtrlCallback(void);
|
---|
76 |
|
---|
77 | GuestCtrlCallback(eVBoxGuestCtrlCallbackType enmType);
|
---|
78 |
|
---|
79 | virtual ~GuestCtrlCallback(void);
|
---|
80 |
|
---|
81 | /** @todo Copy/comparison operator? */
|
---|
82 |
|
---|
83 | public:
|
---|
84 |
|
---|
85 | int Cancel(void);
|
---|
86 |
|
---|
87 | bool Canceled(void);
|
---|
88 |
|
---|
89 | int Init(eVBoxGuestCtrlCallbackType enmType);
|
---|
90 |
|
---|
91 | void Destroy(void);
|
---|
92 |
|
---|
93 | int Signal(int rc = VINF_SUCCESS, const Utf8Str &strMsg = "");
|
---|
94 |
|
---|
95 | Utf8Str GetMessage(void) { return mMessage; }
|
---|
96 |
|
---|
97 | eVBoxGuestCtrlCallbackType GetType(void) { return mType; }
|
---|
98 |
|
---|
99 | int Wait(ULONG uTimeoutMS);
|
---|
100 |
|
---|
101 | protected:
|
---|
102 |
|
---|
103 | /** The callback type. */
|
---|
104 | eVBoxGuestCtrlCallbackType mType;
|
---|
105 | /** Callback flags. */
|
---|
106 | uint32_t uFlags;
|
---|
107 | /** Was the callback canceled? */
|
---|
108 | bool fCanceled;
|
---|
109 | /** Pointer to user-supplied data. */
|
---|
110 | void *pvData;
|
---|
111 | /** Size of user-supplied data. */
|
---|
112 | size_t cbData;
|
---|
113 | /** The event semaphore triggering the*/
|
---|
114 | RTSEMEVENT hEventSem;
|
---|
115 | /** Overall result code. */
|
---|
116 | int mRC;
|
---|
117 | /** Error / information message to the
|
---|
118 | * result code. */
|
---|
119 | Utf8Str mMessage;
|
---|
120 | };
|
---|
121 | typedef std::map < uint32_t, GuestCtrlCallback* > GuestCtrlCallbacks;
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * Simple structure mantaining guest credentials.
|
---|
125 | */
|
---|
126 | class GuestCredentials
|
---|
127 | {
|
---|
128 | public:
|
---|
129 |
|
---|
130 |
|
---|
131 | public:
|
---|
132 |
|
---|
133 | Utf8Str mUser;
|
---|
134 | Utf8Str mPassword;
|
---|
135 | Utf8Str mDomain;
|
---|
136 | };
|
---|
137 |
|
---|
138 | typedef std::vector <Utf8Str> GuestEnvironmentArray;
|
---|
139 | class GuestEnvironment
|
---|
140 | {
|
---|
141 | public:
|
---|
142 |
|
---|
143 | int BuildEnvironmentBlock(void **ppvEnv, size_t *pcbEnv, uint32_t *pcEnvVars);
|
---|
144 |
|
---|
145 | void Clear(void);
|
---|
146 |
|
---|
147 | int CopyFrom(const GuestEnvironmentArray &environment);
|
---|
148 |
|
---|
149 | int CopyTo(GuestEnvironmentArray &environment);
|
---|
150 |
|
---|
151 | static void FreeEnvironmentBlock(void *pvEnv);
|
---|
152 |
|
---|
153 | Utf8Str Get(const Utf8Str &strKey);
|
---|
154 |
|
---|
155 | Utf8Str Get(size_t nPos);
|
---|
156 |
|
---|
157 | bool Has(const Utf8Str &strKey);
|
---|
158 |
|
---|
159 | int Set(const Utf8Str &strKey, const Utf8Str &strValue);
|
---|
160 |
|
---|
161 | int Set(const Utf8Str &strPair);
|
---|
162 |
|
---|
163 | size_t Size(void);
|
---|
164 |
|
---|
165 | int Unset(const Utf8Str &strKey);
|
---|
166 |
|
---|
167 | public:
|
---|
168 |
|
---|
169 | GuestEnvironment& operator=(const GuestEnvironmentArray &that);
|
---|
170 |
|
---|
171 | GuestEnvironment& operator=(const GuestEnvironment &that);
|
---|
172 |
|
---|
173 | protected:
|
---|
174 |
|
---|
175 | int appendToEnvBlock(const char *pszEnv, void **ppvList, size_t *pcbList, uint32_t *pcEnvVars);
|
---|
176 |
|
---|
177 | protected:
|
---|
178 |
|
---|
179 | std::map <Utf8Str, Utf8Str> mEnvironment;
|
---|
180 | };
|
---|
181 |
|
---|
182 |
|
---|
183 | /**
|
---|
184 | * Structure for keeping all the relevant process
|
---|
185 | * starting parameters around.
|
---|
186 | */
|
---|
187 | struct GuestProcessInfo
|
---|
188 | {
|
---|
189 | Utf8Str mCommand;
|
---|
190 | ProcessArguments mArguments;
|
---|
191 | GuestEnvironment mEnvironment;
|
---|
192 | uint32_t mFlags;
|
---|
193 | ULONG mTimeoutMS;
|
---|
194 | ProcessPriority_T mPriority;
|
---|
195 | ProcessAffinity mAffinity;
|
---|
196 | };
|
---|
197 |
|
---|
198 | /**
|
---|
199 | * Class representing the "value" side of a "key=value" pair.
|
---|
200 | */
|
---|
201 | class GuestProcessStreamValue
|
---|
202 | {
|
---|
203 | public:
|
---|
204 |
|
---|
205 | GuestProcessStreamValue() { }
|
---|
206 | GuestProcessStreamValue(const char *pszValue)
|
---|
207 | : mValue(pszValue) {}
|
---|
208 |
|
---|
209 | GuestProcessStreamValue(const GuestProcessStreamValue& aThat)
|
---|
210 | : mValue(aThat.mValue) {}
|
---|
211 |
|
---|
212 | Utf8Str mValue;
|
---|
213 | };
|
---|
214 |
|
---|
215 | /** Map containing "key=value" pairs of a guest process stream. */
|
---|
216 | typedef std::pair< Utf8Str, GuestProcessStreamValue > GuestCtrlStreamPair;
|
---|
217 | typedef std::map < Utf8Str, GuestProcessStreamValue > GuestCtrlStreamPairMap;
|
---|
218 | typedef std::map < Utf8Str, GuestProcessStreamValue >::iterator GuestCtrlStreamPairMapIter;
|
---|
219 | typedef std::map < Utf8Str, GuestProcessStreamValue >::const_iterator GuestCtrlStreamPairMapIterConst;
|
---|
220 |
|
---|
221 | /**
|
---|
222 | * Class representing a block of stream pairs (key=value). Each block in a raw guest
|
---|
223 | * output stream is separated by "\0\0", each pair is separated by "\0". The overall
|
---|
224 | * end of a guest stream is marked by "\0\0\0\0".
|
---|
225 | */
|
---|
226 | class GuestProcessStreamBlock
|
---|
227 | {
|
---|
228 | public:
|
---|
229 |
|
---|
230 | GuestProcessStreamBlock();
|
---|
231 |
|
---|
232 | //GuestProcessStreamBlock(GuestProcessStreamBlock &);
|
---|
233 |
|
---|
234 | virtual ~GuestProcessStreamBlock();
|
---|
235 |
|
---|
236 | public:
|
---|
237 |
|
---|
238 | void Clear();
|
---|
239 |
|
---|
240 | #ifdef DEBUG
|
---|
241 | void Dump();
|
---|
242 | #endif
|
---|
243 |
|
---|
244 | int GetInt64Ex(const char *pszKey, int64_t *piVal);
|
---|
245 |
|
---|
246 | int64_t GetInt64(const char *pszKey);
|
---|
247 |
|
---|
248 | size_t GetCount();
|
---|
249 |
|
---|
250 | const char* GetString(const char *pszKey);
|
---|
251 |
|
---|
252 | int GetUInt32Ex(const char *pszKey, uint32_t *puVal);
|
---|
253 |
|
---|
254 | uint32_t GetUInt32(const char *pszKey);
|
---|
255 |
|
---|
256 | int SetValue(const char *pszKey, const char *pszValue);
|
---|
257 |
|
---|
258 | protected:
|
---|
259 |
|
---|
260 | GuestCtrlStreamPairMap m_mapPairs;
|
---|
261 | };
|
---|
262 |
|
---|
263 | /** Vector containing multiple allocated stream pair objects. */
|
---|
264 | typedef std::vector< GuestProcessStreamBlock > GuestCtrlStreamObjects;
|
---|
265 | typedef std::vector< GuestProcessStreamBlock >::iterator GuestCtrlStreamObjectsIter;
|
---|
266 | typedef std::vector< GuestProcessStreamBlock >::const_iterator GuestCtrlStreamObjectsIterConst;
|
---|
267 |
|
---|
268 | /**
|
---|
269 | * Class for parsing machine-readable guest process output by VBoxService'
|
---|
270 | * toolbox commands ("vbox_ls", "vbox_stat" etc), aka "guest stream".
|
---|
271 | */
|
---|
272 | class GuestProcessStream
|
---|
273 | {
|
---|
274 |
|
---|
275 | public:
|
---|
276 |
|
---|
277 | GuestProcessStream();
|
---|
278 |
|
---|
279 | virtual ~GuestProcessStream();
|
---|
280 |
|
---|
281 | public:
|
---|
282 |
|
---|
283 | int AddData(const BYTE *pbData, size_t cbData);
|
---|
284 |
|
---|
285 | void Destroy();
|
---|
286 |
|
---|
287 | #ifdef DEBUG
|
---|
288 | void Dump(const char *pszFile);
|
---|
289 | #endif
|
---|
290 |
|
---|
291 | uint32_t GetOffset();
|
---|
292 |
|
---|
293 | uint32_t GetSize();
|
---|
294 |
|
---|
295 | int ParseBlock(GuestProcessStreamBlock &streamBlock);
|
---|
296 |
|
---|
297 | protected:
|
---|
298 |
|
---|
299 | /** Currently allocated size of internal stream buffer. */
|
---|
300 | uint32_t m_cbAllocated;
|
---|
301 | /** Currently used size of allocated internal stream buffer. */
|
---|
302 | uint32_t m_cbSize;
|
---|
303 | /** Current offset within the internal stream buffer. */
|
---|
304 | uint32_t m_cbOffset;
|
---|
305 | /** Internal stream buffer. */
|
---|
306 | BYTE *m_pbBuffer;
|
---|
307 | };
|
---|
308 |
|
---|
309 | class Guest;
|
---|
310 | class Progress;
|
---|
311 |
|
---|
312 | class GuestTask
|
---|
313 | {
|
---|
314 |
|
---|
315 | public:
|
---|
316 |
|
---|
317 | enum TaskType
|
---|
318 | {
|
---|
319 | /** Copies a file from host to the guest. */
|
---|
320 | TaskType_CopyFileToGuest = 50,
|
---|
321 | /** Copies a file from guest to the host. */
|
---|
322 | TaskType_CopyFileFromGuest = 55,
|
---|
323 | /** Update Guest Additions by directly copying the required installer
|
---|
324 | * off the .ISO file, transfer it to the guest and execute the installer
|
---|
325 | * with system privileges. */
|
---|
326 | TaskType_UpdateGuestAdditions = 100
|
---|
327 | };
|
---|
328 |
|
---|
329 | GuestTask(TaskType aTaskType, Guest *aThat, Progress *aProgress);
|
---|
330 |
|
---|
331 | virtual ~GuestTask();
|
---|
332 |
|
---|
333 | int startThread();
|
---|
334 |
|
---|
335 | static int taskThread(RTTHREAD aThread, void *pvUser);
|
---|
336 | static int uploadProgress(unsigned uPercent, void *pvUser);
|
---|
337 | static HRESULT setProgressSuccess(ComObjPtr<Progress> pProgress);
|
---|
338 | static HRESULT setProgressErrorMsg(HRESULT hr,
|
---|
339 | ComObjPtr<Progress> pProgress, const char * pszText, ...);
|
---|
340 | static HRESULT setProgressErrorParent(HRESULT hr,
|
---|
341 | ComObjPtr<Progress> pProgress, ComObjPtr<Guest> pGuest);
|
---|
342 |
|
---|
343 | TaskType taskType;
|
---|
344 | ComObjPtr<Guest> pGuest;
|
---|
345 | ComObjPtr<Progress> pProgress;
|
---|
346 | HRESULT rc;
|
---|
347 |
|
---|
348 | /* Task data. */
|
---|
349 | Utf8Str strSource;
|
---|
350 | Utf8Str strDest;
|
---|
351 | Utf8Str strUserName;
|
---|
352 | Utf8Str strPassword;
|
---|
353 | ULONG uFlags;
|
---|
354 | };
|
---|
355 | #endif // ____H_GUESTIMPLPRIVATE
|
---|
356 |
|
---|