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 FillData(const void *pvToWrite, size_t cbToWrite);
|
---|
130 |
|
---|
131 | int Init(eVBoxGuestCtrlCallbackType enmType);
|
---|
132 |
|
---|
133 | eVBoxGuestCtrlCallbackType GetCallbackType(void) { return mType; }
|
---|
134 |
|
---|
135 | const void* GetPayloadRaw(void) const { return pvPayload; }
|
---|
136 |
|
---|
137 | size_t GetPayloadSize(void) { return cbPayload; }
|
---|
138 |
|
---|
139 | protected:
|
---|
140 |
|
---|
141 | /** Pointer to actual callback data. */
|
---|
142 | void *pvData;
|
---|
143 | /** Size of user-supplied data. */
|
---|
144 | size_t cbData;
|
---|
145 | /** The callback type. */
|
---|
146 | eVBoxGuestCtrlCallbackType mType;
|
---|
147 | /** Callback flags. */
|
---|
148 | uint32_t uFlags;
|
---|
149 | /** Payload which will be available on successful
|
---|
150 | * waiting (optional). */
|
---|
151 | void *pvPayload;
|
---|
152 | /** Size of the payload. */
|
---|
153 | size_t cbPayload;
|
---|
154 | };
|
---|
155 | typedef std::map < uint32_t, GuestCtrlCallback* > GuestCtrlCallbacks;
|
---|
156 |
|
---|
157 | struct GuestProcessWaitResult
|
---|
158 | {
|
---|
159 | /** The wait result when returning from the wait call. */
|
---|
160 | ProcessWaitResult_T mResult;
|
---|
161 | int mRC;
|
---|
162 | };
|
---|
163 |
|
---|
164 | /*
|
---|
165 | * Class representing a guest control process event.
|
---|
166 | */
|
---|
167 | class GuestProcessEvent : public GuestCtrlEvent
|
---|
168 | {
|
---|
169 | public:
|
---|
170 | GuestProcessEvent(void);
|
---|
171 |
|
---|
172 | GuestProcessEvent(uint32_t uWaitFlags);
|
---|
173 |
|
---|
174 | virtual ~GuestProcessEvent(void);
|
---|
175 |
|
---|
176 | public:
|
---|
177 |
|
---|
178 | void Destroy(void);
|
---|
179 |
|
---|
180 | int Init(uint32_t uWaitFlags);
|
---|
181 |
|
---|
182 | uint32_t GetWaitFlags(void) { return ASMAtomicReadU32(&mWaitFlags); }
|
---|
183 |
|
---|
184 | GuestProcessWaitResult GetResult(void) { return mWaitResult; }
|
---|
185 |
|
---|
186 | int Signal(ProcessWaitResult_T enmResult, int rc = VINF_SUCCESS);
|
---|
187 |
|
---|
188 | protected:
|
---|
189 |
|
---|
190 | /** The waiting flag(s). The specifies what to
|
---|
191 | * wait for. */
|
---|
192 | uint32_t mWaitFlags;
|
---|
193 | /** Structure containing the overall result. */
|
---|
194 | GuestProcessWaitResult mWaitResult;
|
---|
195 | };
|
---|
196 |
|
---|
197 | /**
|
---|
198 | * Simple structure mantaining guest credentials.
|
---|
199 | */
|
---|
200 | struct GuestCredentials
|
---|
201 | {
|
---|
202 | Utf8Str mUser;
|
---|
203 | Utf8Str mPassword;
|
---|
204 | Utf8Str mDomain;
|
---|
205 | };
|
---|
206 |
|
---|
207 | typedef std::vector <Utf8Str> GuestEnvironmentArray;
|
---|
208 | class GuestEnvironment
|
---|
209 | {
|
---|
210 | public:
|
---|
211 |
|
---|
212 | int BuildEnvironmentBlock(void **ppvEnv, size_t *pcbEnv, uint32_t *pcEnvVars);
|
---|
213 |
|
---|
214 | void Clear(void);
|
---|
215 |
|
---|
216 | int CopyFrom(const GuestEnvironmentArray &environment);
|
---|
217 |
|
---|
218 | int CopyTo(GuestEnvironmentArray &environment);
|
---|
219 |
|
---|
220 | static void FreeEnvironmentBlock(void *pvEnv);
|
---|
221 |
|
---|
222 | Utf8Str Get(const Utf8Str &strKey);
|
---|
223 |
|
---|
224 | Utf8Str Get(size_t nPos);
|
---|
225 |
|
---|
226 | bool Has(const Utf8Str &strKey);
|
---|
227 |
|
---|
228 | int Set(const Utf8Str &strKey, const Utf8Str &strValue);
|
---|
229 |
|
---|
230 | int Set(const Utf8Str &strPair);
|
---|
231 |
|
---|
232 | size_t Size(void);
|
---|
233 |
|
---|
234 | int Unset(const Utf8Str &strKey);
|
---|
235 |
|
---|
236 | public:
|
---|
237 |
|
---|
238 | GuestEnvironment& operator=(const GuestEnvironmentArray &that);
|
---|
239 |
|
---|
240 | GuestEnvironment& operator=(const GuestEnvironment &that);
|
---|
241 |
|
---|
242 | protected:
|
---|
243 |
|
---|
244 | int appendToEnvBlock(const char *pszEnv, void **ppvList, size_t *pcbList, uint32_t *pcEnvVars);
|
---|
245 |
|
---|
246 | protected:
|
---|
247 |
|
---|
248 | std::map <Utf8Str, Utf8Str> mEnvironment;
|
---|
249 | };
|
---|
250 |
|
---|
251 |
|
---|
252 | /**
|
---|
253 | * Structure for keeping all the relevant process
|
---|
254 | * starting parameters around.
|
---|
255 | */
|
---|
256 | struct GuestProcessInfo
|
---|
257 | {
|
---|
258 | Utf8Str mCommand;
|
---|
259 | ProcessArguments mArguments;
|
---|
260 | GuestEnvironment mEnvironment;
|
---|
261 | uint32_t mFlags;
|
---|
262 | ULONG mTimeoutMS;
|
---|
263 | ProcessPriority_T mPriority;
|
---|
264 | ProcessAffinity mAffinity;
|
---|
265 | };
|
---|
266 |
|
---|
267 | /**
|
---|
268 | * Class representing the "value" side of a "key=value" pair.
|
---|
269 | */
|
---|
270 | class GuestProcessStreamValue
|
---|
271 | {
|
---|
272 | public:
|
---|
273 |
|
---|
274 | GuestProcessStreamValue() { }
|
---|
275 | GuestProcessStreamValue(const char *pszValue)
|
---|
276 | : mValue(pszValue) {}
|
---|
277 |
|
---|
278 | GuestProcessStreamValue(const GuestProcessStreamValue& aThat)
|
---|
279 | : mValue(aThat.mValue) {}
|
---|
280 |
|
---|
281 | Utf8Str mValue;
|
---|
282 | };
|
---|
283 |
|
---|
284 | /** Map containing "key=value" pairs of a guest process stream. */
|
---|
285 | typedef std::pair< Utf8Str, GuestProcessStreamValue > GuestCtrlStreamPair;
|
---|
286 | typedef std::map < Utf8Str, GuestProcessStreamValue > GuestCtrlStreamPairMap;
|
---|
287 | typedef std::map < Utf8Str, GuestProcessStreamValue >::iterator GuestCtrlStreamPairMapIter;
|
---|
288 | typedef std::map < Utf8Str, GuestProcessStreamValue >::const_iterator GuestCtrlStreamPairMapIterConst;
|
---|
289 |
|
---|
290 | /**
|
---|
291 | * Class representing a block of stream pairs (key=value). Each block in a raw guest
|
---|
292 | * output stream is separated by "\0\0", each pair is separated by "\0". The overall
|
---|
293 | * end of a guest stream is marked by "\0\0\0\0".
|
---|
294 | */
|
---|
295 | class GuestProcessStreamBlock
|
---|
296 | {
|
---|
297 | public:
|
---|
298 |
|
---|
299 | GuestProcessStreamBlock();
|
---|
300 |
|
---|
301 | //GuestProcessStreamBlock(GuestProcessStreamBlock &);
|
---|
302 |
|
---|
303 | virtual ~GuestProcessStreamBlock();
|
---|
304 |
|
---|
305 | public:
|
---|
306 |
|
---|
307 | void Clear();
|
---|
308 |
|
---|
309 | #ifdef DEBUG
|
---|
310 | void Dump();
|
---|
311 | #endif
|
---|
312 |
|
---|
313 | int GetInt64Ex(const char *pszKey, int64_t *piVal);
|
---|
314 |
|
---|
315 | int64_t GetInt64(const char *pszKey);
|
---|
316 |
|
---|
317 | size_t GetCount();
|
---|
318 |
|
---|
319 | const char* GetString(const char *pszKey);
|
---|
320 |
|
---|
321 | int GetUInt32Ex(const char *pszKey, uint32_t *puVal);
|
---|
322 |
|
---|
323 | uint32_t GetUInt32(const char *pszKey);
|
---|
324 |
|
---|
325 | int SetValue(const char *pszKey, const char *pszValue);
|
---|
326 |
|
---|
327 | protected:
|
---|
328 |
|
---|
329 | GuestCtrlStreamPairMap m_mapPairs;
|
---|
330 | };
|
---|
331 |
|
---|
332 | /** Vector containing multiple allocated stream pair objects. */
|
---|
333 | typedef std::vector< GuestProcessStreamBlock > GuestCtrlStreamObjects;
|
---|
334 | typedef std::vector< GuestProcessStreamBlock >::iterator GuestCtrlStreamObjectsIter;
|
---|
335 | typedef std::vector< GuestProcessStreamBlock >::const_iterator GuestCtrlStreamObjectsIterConst;
|
---|
336 |
|
---|
337 | /**
|
---|
338 | * Class for parsing machine-readable guest process output by VBoxService'
|
---|
339 | * toolbox commands ("vbox_ls", "vbox_stat" etc), aka "guest stream".
|
---|
340 | */
|
---|
341 | class GuestProcessStream
|
---|
342 | {
|
---|
343 |
|
---|
344 | public:
|
---|
345 |
|
---|
346 | GuestProcessStream();
|
---|
347 |
|
---|
348 | virtual ~GuestProcessStream();
|
---|
349 |
|
---|
350 | public:
|
---|
351 |
|
---|
352 | int AddData(const BYTE *pbData, size_t cbData);
|
---|
353 |
|
---|
354 | void Destroy();
|
---|
355 |
|
---|
356 | #ifdef DEBUG
|
---|
357 | void Dump(const char *pszFile);
|
---|
358 | #endif
|
---|
359 |
|
---|
360 | uint32_t GetOffset();
|
---|
361 |
|
---|
362 | uint32_t GetSize();
|
---|
363 |
|
---|
364 | int ParseBlock(GuestProcessStreamBlock &streamBlock);
|
---|
365 |
|
---|
366 | protected:
|
---|
367 |
|
---|
368 | /** Currently allocated size of internal stream buffer. */
|
---|
369 | uint32_t m_cbAllocated;
|
---|
370 | /** Currently used size of allocated internal stream buffer. */
|
---|
371 | uint32_t m_cbSize;
|
---|
372 | /** Current offset within the internal stream buffer. */
|
---|
373 | uint32_t m_cbOffset;
|
---|
374 | /** Internal stream buffer. */
|
---|
375 | BYTE *m_pbBuffer;
|
---|
376 | };
|
---|
377 |
|
---|
378 | class Guest;
|
---|
379 | class Progress;
|
---|
380 |
|
---|
381 | class GuestTask
|
---|
382 | {
|
---|
383 |
|
---|
384 | public:
|
---|
385 |
|
---|
386 | enum TaskType
|
---|
387 | {
|
---|
388 | /** Copies a file from host to the guest. */
|
---|
389 | TaskType_CopyFileToGuest = 50,
|
---|
390 | /** Copies a file from guest to the host. */
|
---|
391 | TaskType_CopyFileFromGuest = 55,
|
---|
392 | /** Update Guest Additions by directly copying the required installer
|
---|
393 | * off the .ISO file, transfer it to the guest and execute the installer
|
---|
394 | * with system privileges. */
|
---|
395 | TaskType_UpdateGuestAdditions = 100
|
---|
396 | };
|
---|
397 |
|
---|
398 | GuestTask(TaskType aTaskType, Guest *aThat, Progress *aProgress);
|
---|
399 |
|
---|
400 | virtual ~GuestTask();
|
---|
401 |
|
---|
402 | int startThread();
|
---|
403 |
|
---|
404 | static int taskThread(RTTHREAD aThread, void *pvUser);
|
---|
405 | static int uploadProgress(unsigned uPercent, void *pvUser);
|
---|
406 | static HRESULT setProgressSuccess(ComObjPtr<Progress> pProgress);
|
---|
407 | static HRESULT setProgressErrorMsg(HRESULT hr,
|
---|
408 | ComObjPtr<Progress> pProgress, const char * pszText, ...);
|
---|
409 | static HRESULT setProgressErrorParent(HRESULT hr,
|
---|
410 | ComObjPtr<Progress> pProgress, ComObjPtr<Guest> pGuest);
|
---|
411 |
|
---|
412 | TaskType taskType;
|
---|
413 | ComObjPtr<Guest> pGuest;
|
---|
414 | ComObjPtr<Progress> pProgress;
|
---|
415 | HRESULT rc;
|
---|
416 |
|
---|
417 | /* Task data. */
|
---|
418 | Utf8Str strSource;
|
---|
419 | Utf8Str strDest;
|
---|
420 | Utf8Str strUserName;
|
---|
421 | Utf8Str strPassword;
|
---|
422 | ULONG uFlags;
|
---|
423 | };
|
---|
424 | #endif // ____H_GUESTIMPLPRIVATE
|
---|
425 |
|
---|