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/asm.h>
|
---|
22 | #include <iprt/semaphore.h>
|
---|
23 |
|
---|
24 | #include <VBox/com/com.h>
|
---|
25 | #include <VBox/com/ErrorInfo.h>
|
---|
26 | #include <VBox/com/string.h>
|
---|
27 | #include <VBox/com/VirtualBox.h>
|
---|
28 |
|
---|
29 | #include <map>
|
---|
30 | #include <vector>
|
---|
31 |
|
---|
32 | using namespace com;
|
---|
33 |
|
---|
34 | #ifdef VBOX_WITH_GUEST_CONTROL
|
---|
35 | # include <VBox/HostServices/GuestControlSvc.h>
|
---|
36 | using namespace guestControl;
|
---|
37 | #endif
|
---|
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 | /** Vector holding a process' CPU affinity. */
|
---|
64 | typedef std::vector <LONG> ProcessAffinity;
|
---|
65 | /** Vector holding process startup arguments. */
|
---|
66 | typedef std::vector <Utf8Str> ProcessArguments;
|
---|
67 |
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Generic class for a all guest control callbacks/events.
|
---|
71 | */
|
---|
72 | class GuestCtrlEvent
|
---|
73 | {
|
---|
74 | public:
|
---|
75 |
|
---|
76 | GuestCtrlEvent(void);
|
---|
77 |
|
---|
78 | virtual ~GuestCtrlEvent(void);
|
---|
79 |
|
---|
80 | /** @todo Copy/comparison operator? */
|
---|
81 |
|
---|
82 | public:
|
---|
83 |
|
---|
84 | int Cancel(void);
|
---|
85 |
|
---|
86 | bool Canceled(void);
|
---|
87 |
|
---|
88 | virtual void Destroy(void);
|
---|
89 |
|
---|
90 | int Init(void);
|
---|
91 |
|
---|
92 | virtual int Signal(int rc = VINF_SUCCESS);
|
---|
93 |
|
---|
94 | int GetResultCode(void) { return mRC; }
|
---|
95 |
|
---|
96 | int Wait(ULONG uTimeoutMS);
|
---|
97 |
|
---|
98 | protected:
|
---|
99 |
|
---|
100 | /** Was the callback canceled? */
|
---|
101 | bool fCanceled;
|
---|
102 | /** Did the callback complete? */
|
---|
103 | bool fCompleted;
|
---|
104 | /** The event semaphore for triggering
|
---|
105 | * the actual event. */
|
---|
106 | RTSEMEVENT hEventSem;
|
---|
107 | /** The waiting mutex. */
|
---|
108 | RTSEMMUTEX hEventMutex;
|
---|
109 | /** Overall result code. */
|
---|
110 | int mRC;
|
---|
111 | };
|
---|
112 |
|
---|
113 | /*
|
---|
114 | * Class representing a guest control callback.
|
---|
115 | */
|
---|
116 | class GuestCtrlCallback : public GuestCtrlEvent
|
---|
117 | {
|
---|
118 | public:
|
---|
119 | GuestCtrlCallback(void);
|
---|
120 |
|
---|
121 | GuestCtrlCallback(eVBoxGuestCtrlCallbackType enmType);
|
---|
122 |
|
---|
123 | virtual ~GuestCtrlCallback(void);
|
---|
124 |
|
---|
125 | public:
|
---|
126 |
|
---|
127 | void Destroy(void);
|
---|
128 |
|
---|
129 | int Init(eVBoxGuestCtrlCallbackType enmType);
|
---|
130 |
|
---|
131 | eVBoxGuestCtrlCallbackType GetCallbackType(void) { return mType; }
|
---|
132 |
|
---|
133 | protected:
|
---|
134 |
|
---|
135 | /** Pointer to user-supplied data. */
|
---|
136 | void *pvData;
|
---|
137 | /** Size of user-supplied data. */
|
---|
138 | size_t cbData;
|
---|
139 | /** The callback type. */
|
---|
140 | eVBoxGuestCtrlCallbackType mType;
|
---|
141 | /** Callback flags. */
|
---|
142 | uint32_t uFlags;
|
---|
143 | };
|
---|
144 | typedef std::map < uint32_t, GuestCtrlCallback* > GuestCtrlCallbacks;
|
---|
145 |
|
---|
146 | struct GuestProcessWaitResult
|
---|
147 | {
|
---|
148 | /** The wait result when returning from the wait call. */
|
---|
149 | ProcessWaitResult_T mResult;
|
---|
150 | int mRC;
|
---|
151 | };
|
---|
152 |
|
---|
153 | /*
|
---|
154 | * Class representing a guest control process event.
|
---|
155 | */
|
---|
156 | class GuestProcessEvent : public GuestCtrlEvent
|
---|
157 | {
|
---|
158 | public:
|
---|
159 | GuestProcessEvent(void);
|
---|
160 |
|
---|
161 | GuestProcessEvent(uint32_t uWaitFlags);
|
---|
162 |
|
---|
163 | virtual ~GuestProcessEvent(void);
|
---|
164 |
|
---|
165 | public:
|
---|
166 |
|
---|
167 | void Destroy(void);
|
---|
168 |
|
---|
169 | int Init(uint32_t uWaitFlags);
|
---|
170 |
|
---|
171 | uint32_t GetWaitFlags(void) { return ASMAtomicReadU32(&mWaitFlags); }
|
---|
172 |
|
---|
173 | GuestProcessWaitResult GetResult(void) { return mWaitResult; }
|
---|
174 |
|
---|
175 | int Signal(ProcessWaitResult_T enmResult, int rc = VINF_SUCCESS);
|
---|
176 |
|
---|
177 | protected:
|
---|
178 |
|
---|
179 | /** The waiting flag(s). The specifies what to
|
---|
180 | * wait for. */
|
---|
181 | uint32_t mWaitFlags;
|
---|
182 | /** Structure containing the overall result. */
|
---|
183 | GuestProcessWaitResult mWaitResult;
|
---|
184 | };
|
---|
185 |
|
---|
186 | /**
|
---|
187 | * Simple structure mantaining guest credentials.
|
---|
188 | */
|
---|
189 | struct GuestCredentials
|
---|
190 | {
|
---|
191 | Utf8Str mUser;
|
---|
192 | Utf8Str mPassword;
|
---|
193 | Utf8Str mDomain;
|
---|
194 | };
|
---|
195 |
|
---|
196 | typedef std::vector <Utf8Str> GuestEnvironmentArray;
|
---|
197 | class GuestEnvironment
|
---|
198 | {
|
---|
199 | public:
|
---|
200 |
|
---|
201 | int BuildEnvironmentBlock(void **ppvEnv, size_t *pcbEnv, uint32_t *pcEnvVars);
|
---|
202 |
|
---|
203 | void Clear(void);
|
---|
204 |
|
---|
205 | int CopyFrom(const GuestEnvironmentArray &environment);
|
---|
206 |
|
---|
207 | int CopyTo(GuestEnvironmentArray &environment);
|
---|
208 |
|
---|
209 | static void FreeEnvironmentBlock(void *pvEnv);
|
---|
210 |
|
---|
211 | Utf8Str Get(const Utf8Str &strKey);
|
---|
212 |
|
---|
213 | Utf8Str Get(size_t nPos);
|
---|
214 |
|
---|
215 | bool Has(const Utf8Str &strKey);
|
---|
216 |
|
---|
217 | int Set(const Utf8Str &strKey, const Utf8Str &strValue);
|
---|
218 |
|
---|
219 | int Set(const Utf8Str &strPair);
|
---|
220 |
|
---|
221 | size_t Size(void);
|
---|
222 |
|
---|
223 | int Unset(const Utf8Str &strKey);
|
---|
224 |
|
---|
225 | public:
|
---|
226 |
|
---|
227 | GuestEnvironment& operator=(const GuestEnvironmentArray &that);
|
---|
228 |
|
---|
229 | GuestEnvironment& operator=(const GuestEnvironment &that);
|
---|
230 |
|
---|
231 | protected:
|
---|
232 |
|
---|
233 | int appendToEnvBlock(const char *pszEnv, void **ppvList, size_t *pcbList, uint32_t *pcEnvVars);
|
---|
234 |
|
---|
235 | protected:
|
---|
236 |
|
---|
237 | std::map <Utf8Str, Utf8Str> mEnvironment;
|
---|
238 | };
|
---|
239 |
|
---|
240 |
|
---|
241 | /**
|
---|
242 | * Structure for keeping all the relevant process
|
---|
243 | * starting parameters around.
|
---|
244 | */
|
---|
245 | struct GuestProcessInfo
|
---|
246 | {
|
---|
247 | Utf8Str mCommand;
|
---|
248 | ProcessArguments mArguments;
|
---|
249 | GuestEnvironment mEnvironment;
|
---|
250 | uint32_t mFlags;
|
---|
251 | ULONG mTimeoutMS;
|
---|
252 | ProcessPriority_T mPriority;
|
---|
253 | ProcessAffinity mAffinity;
|
---|
254 | };
|
---|
255 |
|
---|
256 | /**
|
---|
257 | * Class representing the "value" side of a "key=value" pair.
|
---|
258 | */
|
---|
259 | class GuestProcessStreamValue
|
---|
260 | {
|
---|
261 | public:
|
---|
262 |
|
---|
263 | GuestProcessStreamValue() { }
|
---|
264 | GuestProcessStreamValue(const char *pszValue)
|
---|
265 | : mValue(pszValue) {}
|
---|
266 |
|
---|
267 | GuestProcessStreamValue(const GuestProcessStreamValue& aThat)
|
---|
268 | : mValue(aThat.mValue) {}
|
---|
269 |
|
---|
270 | Utf8Str mValue;
|
---|
271 | };
|
---|
272 |
|
---|
273 | /** Map containing "key=value" pairs of a guest process stream. */
|
---|
274 | typedef std::pair< Utf8Str, GuestProcessStreamValue > GuestCtrlStreamPair;
|
---|
275 | typedef std::map < Utf8Str, GuestProcessStreamValue > GuestCtrlStreamPairMap;
|
---|
276 | typedef std::map < Utf8Str, GuestProcessStreamValue >::iterator GuestCtrlStreamPairMapIter;
|
---|
277 | typedef std::map < Utf8Str, GuestProcessStreamValue >::const_iterator GuestCtrlStreamPairMapIterConst;
|
---|
278 |
|
---|
279 | /**
|
---|
280 | * Class representing a block of stream pairs (key=value). Each block in a raw guest
|
---|
281 | * output stream is separated by "\0\0", each pair is separated by "\0". The overall
|
---|
282 | * end of a guest stream is marked by "\0\0\0\0".
|
---|
283 | */
|
---|
284 | class GuestProcessStreamBlock
|
---|
285 | {
|
---|
286 | public:
|
---|
287 |
|
---|
288 | GuestProcessStreamBlock();
|
---|
289 |
|
---|
290 | //GuestProcessStreamBlock(GuestProcessStreamBlock &);
|
---|
291 |
|
---|
292 | virtual ~GuestProcessStreamBlock();
|
---|
293 |
|
---|
294 | public:
|
---|
295 |
|
---|
296 | void Clear();
|
---|
297 |
|
---|
298 | #ifdef DEBUG
|
---|
299 | void Dump();
|
---|
300 | #endif
|
---|
301 |
|
---|
302 | int GetInt64Ex(const char *pszKey, int64_t *piVal);
|
---|
303 |
|
---|
304 | int64_t GetInt64(const char *pszKey);
|
---|
305 |
|
---|
306 | size_t GetCount();
|
---|
307 |
|
---|
308 | const char* GetString(const char *pszKey);
|
---|
309 |
|
---|
310 | int GetUInt32Ex(const char *pszKey, uint32_t *puVal);
|
---|
311 |
|
---|
312 | uint32_t GetUInt32(const char *pszKey);
|
---|
313 |
|
---|
314 | int SetValue(const char *pszKey, const char *pszValue);
|
---|
315 |
|
---|
316 | protected:
|
---|
317 |
|
---|
318 | GuestCtrlStreamPairMap m_mapPairs;
|
---|
319 | };
|
---|
320 |
|
---|
321 | /** Vector containing multiple allocated stream pair objects. */
|
---|
322 | typedef std::vector< GuestProcessStreamBlock > GuestCtrlStreamObjects;
|
---|
323 | typedef std::vector< GuestProcessStreamBlock >::iterator GuestCtrlStreamObjectsIter;
|
---|
324 | typedef std::vector< GuestProcessStreamBlock >::const_iterator GuestCtrlStreamObjectsIterConst;
|
---|
325 |
|
---|
326 | /**
|
---|
327 | * Class for parsing machine-readable guest process output by VBoxService'
|
---|
328 | * toolbox commands ("vbox_ls", "vbox_stat" etc), aka "guest stream".
|
---|
329 | */
|
---|
330 | class GuestProcessStream
|
---|
331 | {
|
---|
332 |
|
---|
333 | public:
|
---|
334 |
|
---|
335 | GuestProcessStream();
|
---|
336 |
|
---|
337 | virtual ~GuestProcessStream();
|
---|
338 |
|
---|
339 | public:
|
---|
340 |
|
---|
341 | int AddData(const BYTE *pbData, size_t cbData);
|
---|
342 |
|
---|
343 | void Destroy();
|
---|
344 |
|
---|
345 | #ifdef DEBUG
|
---|
346 | void Dump(const char *pszFile);
|
---|
347 | #endif
|
---|
348 |
|
---|
349 | uint32_t GetOffset();
|
---|
350 |
|
---|
351 | uint32_t GetSize();
|
---|
352 |
|
---|
353 | int ParseBlock(GuestProcessStreamBlock &streamBlock);
|
---|
354 |
|
---|
355 | protected:
|
---|
356 |
|
---|
357 | /** Currently allocated size of internal stream buffer. */
|
---|
358 | uint32_t m_cbAllocated;
|
---|
359 | /** Currently used size of allocated internal stream buffer. */
|
---|
360 | uint32_t m_cbSize;
|
---|
361 | /** Current offset within the internal stream buffer. */
|
---|
362 | uint32_t m_cbOffset;
|
---|
363 | /** Internal stream buffer. */
|
---|
364 | BYTE *m_pbBuffer;
|
---|
365 | };
|
---|
366 |
|
---|
367 | class Guest;
|
---|
368 | class Progress;
|
---|
369 |
|
---|
370 | class GuestTask
|
---|
371 | {
|
---|
372 |
|
---|
373 | public:
|
---|
374 |
|
---|
375 | enum TaskType
|
---|
376 | {
|
---|
377 | /** Copies a file from host to the guest. */
|
---|
378 | TaskType_CopyFileToGuest = 50,
|
---|
379 | /** Copies a file from guest to the host. */
|
---|
380 | TaskType_CopyFileFromGuest = 55,
|
---|
381 | /** Update Guest Additions by directly copying the required installer
|
---|
382 | * off the .ISO file, transfer it to the guest and execute the installer
|
---|
383 | * with system privileges. */
|
---|
384 | TaskType_UpdateGuestAdditions = 100
|
---|
385 | };
|
---|
386 |
|
---|
387 | GuestTask(TaskType aTaskType, Guest *aThat, Progress *aProgress);
|
---|
388 |
|
---|
389 | virtual ~GuestTask();
|
---|
390 |
|
---|
391 | int startThread();
|
---|
392 |
|
---|
393 | static int taskThread(RTTHREAD aThread, void *pvUser);
|
---|
394 | static int uploadProgress(unsigned uPercent, void *pvUser);
|
---|
395 | static HRESULT setProgressSuccess(ComObjPtr<Progress> pProgress);
|
---|
396 | static HRESULT setProgressErrorMsg(HRESULT hr,
|
---|
397 | ComObjPtr<Progress> pProgress, const char * pszText, ...);
|
---|
398 | static HRESULT setProgressErrorParent(HRESULT hr,
|
---|
399 | ComObjPtr<Progress> pProgress, ComObjPtr<Guest> pGuest);
|
---|
400 |
|
---|
401 | TaskType taskType;
|
---|
402 | ComObjPtr<Guest> pGuest;
|
---|
403 | ComObjPtr<Progress> pProgress;
|
---|
404 | HRESULT rc;
|
---|
405 |
|
---|
406 | /* Task data. */
|
---|
407 | Utf8Str strSource;
|
---|
408 | Utf8Str strDest;
|
---|
409 | Utf8Str strUserName;
|
---|
410 | Utf8Str strPassword;
|
---|
411 | ULONG uFlags;
|
---|
412 | };
|
---|
413 | #endif // ____H_GUESTIMPLPRIVATE
|
---|
414 |
|
---|