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 | #ifdef LOG_GROUP
|
---|
40 | #undef LOG_GROUP
|
---|
41 | #endif
|
---|
42 | #define LOG_GROUP LOG_GROUP_GUEST_CONTROL
|
---|
43 | #include <VBox/log.h>
|
---|
44 |
|
---|
45 | /** Maximum number of guest sessions a VM can have. */
|
---|
46 | #define VBOX_GUESTCTRL_MAX_SESSIONS 255
|
---|
47 | /** Maximum of guest processes a guest session can have. */
|
---|
48 | #define VBOX_GUESTCTRL_MAX_PROCESSES 255
|
---|
49 | /** Maximum of callback contexts a guest process can have. */
|
---|
50 | #define VBOX_GUESTCTRL_MAX_CONTEXTS _64K - 1
|
---|
51 |
|
---|
52 | /** Builds a context ID out of the session ID, process ID and an
|
---|
53 | * increasing count. */
|
---|
54 | #define VBOX_GUESTCTRL_CONTEXTID_MAKE(uSession, uProcess, uCount) \
|
---|
55 | ( (uint32_t)((uSession) & 0xff) << 24 \
|
---|
56 | | (uint32_t)((uProcess) & 0xff) << 16 \
|
---|
57 | | (uint32_t)((uCount) & 0xffff) \
|
---|
58 | )
|
---|
59 | /** Gets the session ID out of a context ID. */
|
---|
60 | #define VBOX_GUESTCTRL_CONTEXTID_GET_SESSION(uContextID) \
|
---|
61 | ((uContextID) >> 24)
|
---|
62 | /** Gets the process ID out of a context ID. */
|
---|
63 | #define VBOX_GUESTCTRL_CONTEXTID_GET_PROCESS(uContextID) \
|
---|
64 | (((uContextID) >> 16) & 0xff)
|
---|
65 | /** Gets the conext count of a process out of a context ID. */
|
---|
66 | #define VBOX_GUESTCTRL_CONTEXTID_GET_COUNT(uContextID) \
|
---|
67 | ((uContextID) & 0xffff)
|
---|
68 |
|
---|
69 | /** Vector holding a process' CPU affinity. */
|
---|
70 | typedef std::vector <LONG> ProcessAffinity;
|
---|
71 | /** Vector holding process startup arguments. */
|
---|
72 | typedef std::vector <Utf8Str> ProcessArguments;
|
---|
73 |
|
---|
74 | class GuestProcessStreamBlock;
|
---|
75 |
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Generic class for a all guest control callbacks/events.
|
---|
79 | */
|
---|
80 | class GuestCtrlEvent
|
---|
81 | {
|
---|
82 | public:
|
---|
83 |
|
---|
84 | GuestCtrlEvent(void);
|
---|
85 |
|
---|
86 | virtual ~GuestCtrlEvent(void);
|
---|
87 |
|
---|
88 | /** @todo Copy/comparison operator? */
|
---|
89 |
|
---|
90 | public:
|
---|
91 |
|
---|
92 | int Cancel(void);
|
---|
93 |
|
---|
94 | bool Canceled(void);
|
---|
95 |
|
---|
96 | virtual void Destroy(void);
|
---|
97 |
|
---|
98 | int Init(void);
|
---|
99 |
|
---|
100 | virtual int Signal(int rc = VINF_SUCCESS);
|
---|
101 |
|
---|
102 | int GetResultCode(void) { return mRC; }
|
---|
103 |
|
---|
104 | int Wait(ULONG uTimeoutMS);
|
---|
105 |
|
---|
106 | protected:
|
---|
107 |
|
---|
108 | /** Was the callback canceled? */
|
---|
109 | bool fCanceled;
|
---|
110 | /** Did the callback complete? */
|
---|
111 | bool fCompleted;
|
---|
112 | /** The event semaphore for triggering
|
---|
113 | * the actual event. */
|
---|
114 | RTSEMEVENT hEventSem;
|
---|
115 | /** The waiting mutex. */
|
---|
116 | RTSEMMUTEX hEventMutex;
|
---|
117 | /** Overall result code. */
|
---|
118 | int mRC;
|
---|
119 | };
|
---|
120 |
|
---|
121 |
|
---|
122 | /*
|
---|
123 | * Class representing a guest control callback.
|
---|
124 | */
|
---|
125 | class GuestCtrlCallback : public GuestCtrlEvent
|
---|
126 | {
|
---|
127 | public:
|
---|
128 | GuestCtrlCallback(void);
|
---|
129 |
|
---|
130 | GuestCtrlCallback(eVBoxGuestCtrlCallbackType enmType);
|
---|
131 |
|
---|
132 | virtual ~GuestCtrlCallback(void);
|
---|
133 |
|
---|
134 | public:
|
---|
135 |
|
---|
136 | void Destroy(void);
|
---|
137 |
|
---|
138 | int Init(eVBoxGuestCtrlCallbackType enmType);
|
---|
139 |
|
---|
140 | eVBoxGuestCtrlCallbackType GetCallbackType(void) { return mType; }
|
---|
141 |
|
---|
142 | const void* GetDataRaw(void) const { return pvData; }
|
---|
143 |
|
---|
144 | size_t GetDataSize(void) { return cbData; }
|
---|
145 |
|
---|
146 | const void* GetPayloadRaw(void) const { return pvPayload; }
|
---|
147 |
|
---|
148 | size_t GetPayloadSize(void) { return cbPayload; }
|
---|
149 |
|
---|
150 | int SetData(const void *pvCallback, size_t cbCallback);
|
---|
151 |
|
---|
152 | int SetPayload(const void *pvToWrite, size_t cbToWrite);
|
---|
153 |
|
---|
154 | protected:
|
---|
155 |
|
---|
156 | /** Pointer to actual callback data. */
|
---|
157 | void *pvData;
|
---|
158 | /** Size of user-supplied data. */
|
---|
159 | size_t cbData;
|
---|
160 | /** The callback type. */
|
---|
161 | eVBoxGuestCtrlCallbackType mType;
|
---|
162 | /** Callback flags. */
|
---|
163 | uint32_t uFlags;
|
---|
164 | /** Payload which will be available on successful
|
---|
165 | * waiting (optional). */
|
---|
166 | void *pvPayload;
|
---|
167 | /** Size of the payload (optional). */
|
---|
168 | size_t cbPayload;
|
---|
169 | };
|
---|
170 | typedef std::map < uint32_t, GuestCtrlCallback* > GuestCtrlCallbacks;
|
---|
171 |
|
---|
172 | struct GuestProcessWaitResult
|
---|
173 | {
|
---|
174 | GuestProcessWaitResult(void)
|
---|
175 | : mResult(ProcessWaitResult_None),
|
---|
176 | mRC(VINF_SUCCESS) { }
|
---|
177 |
|
---|
178 | /** The wait result when returning from the wait call. */
|
---|
179 | ProcessWaitResult_T mResult;
|
---|
180 | /** Optional rc to this result. */
|
---|
181 | int mRC;
|
---|
182 | };
|
---|
183 |
|
---|
184 |
|
---|
185 | /*
|
---|
186 | * Class representing a guest control process event.
|
---|
187 | */
|
---|
188 | class GuestProcessEvent : public GuestCtrlEvent
|
---|
189 | {
|
---|
190 | public:
|
---|
191 | GuestProcessEvent(void);
|
---|
192 |
|
---|
193 | GuestProcessEvent(uint32_t uWaitFlags);
|
---|
194 |
|
---|
195 | virtual ~GuestProcessEvent(void);
|
---|
196 |
|
---|
197 | public:
|
---|
198 |
|
---|
199 | void Destroy(void);
|
---|
200 |
|
---|
201 | int Init(uint32_t uWaitFlags);
|
---|
202 |
|
---|
203 | uint32_t GetWaitFlags(void) { return ASMAtomicReadU32(&mWaitFlags); }
|
---|
204 |
|
---|
205 | GuestProcessWaitResult GetResult(void) { return mWaitResult; }
|
---|
206 |
|
---|
207 | int Signal(ProcessWaitResult_T enmResult, int rc = VINF_SUCCESS);
|
---|
208 |
|
---|
209 | protected:
|
---|
210 |
|
---|
211 | /** The waiting flag(s). The specifies what to
|
---|
212 | * wait for. */
|
---|
213 | uint32_t mWaitFlags;
|
---|
214 | /** Structure containing the overall result. */
|
---|
215 | GuestProcessWaitResult mWaitResult;
|
---|
216 | };
|
---|
217 |
|
---|
218 |
|
---|
219 | /**
|
---|
220 | * Simple structure mantaining guest credentials.
|
---|
221 | */
|
---|
222 | struct GuestCredentials
|
---|
223 | {
|
---|
224 | Utf8Str mUser;
|
---|
225 | Utf8Str mPassword;
|
---|
226 | Utf8Str mDomain;
|
---|
227 | };
|
---|
228 |
|
---|
229 |
|
---|
230 | typedef std::vector <Utf8Str> GuestEnvironmentArray;
|
---|
231 | class GuestEnvironment
|
---|
232 | {
|
---|
233 | public:
|
---|
234 |
|
---|
235 | int BuildEnvironmentBlock(void **ppvEnv, size_t *pcbEnv, uint32_t *pcEnvVars);
|
---|
236 |
|
---|
237 | void Clear(void);
|
---|
238 |
|
---|
239 | int CopyFrom(const GuestEnvironmentArray &environment);
|
---|
240 |
|
---|
241 | int CopyTo(GuestEnvironmentArray &environment);
|
---|
242 |
|
---|
243 | static void FreeEnvironmentBlock(void *pvEnv);
|
---|
244 |
|
---|
245 | Utf8Str Get(const Utf8Str &strKey);
|
---|
246 |
|
---|
247 | Utf8Str Get(size_t nPos);
|
---|
248 |
|
---|
249 | bool Has(const Utf8Str &strKey);
|
---|
250 |
|
---|
251 | int Set(const Utf8Str &strKey, const Utf8Str &strValue);
|
---|
252 |
|
---|
253 | int Set(const Utf8Str &strPair);
|
---|
254 |
|
---|
255 | size_t Size(void);
|
---|
256 |
|
---|
257 | int Unset(const Utf8Str &strKey);
|
---|
258 |
|
---|
259 | public:
|
---|
260 |
|
---|
261 | GuestEnvironment& operator=(const GuestEnvironmentArray &that);
|
---|
262 |
|
---|
263 | GuestEnvironment& operator=(const GuestEnvironment &that);
|
---|
264 |
|
---|
265 | protected:
|
---|
266 |
|
---|
267 | int appendToEnvBlock(const char *pszEnv, void **ppvList, size_t *pcbList, uint32_t *pcEnvVars);
|
---|
268 |
|
---|
269 | protected:
|
---|
270 |
|
---|
271 | std::map <Utf8Str, Utf8Str> mEnvironment;
|
---|
272 | };
|
---|
273 |
|
---|
274 |
|
---|
275 | /**
|
---|
276 | * Structure representing information of a
|
---|
277 | * file system object.
|
---|
278 | */
|
---|
279 | struct GuestFsObjData
|
---|
280 | {
|
---|
281 | /** Helper function to extract the data from
|
---|
282 | * a certin VBoxService tool's guest stream block. */
|
---|
283 | int FromLs(const GuestProcessStreamBlock &strmBlk);
|
---|
284 | int FromStat(const GuestProcessStreamBlock &strmBlk);
|
---|
285 |
|
---|
286 | int64_t mAccessTime;
|
---|
287 | int64_t mAllocatedSize;
|
---|
288 | int64_t mBirthTime;
|
---|
289 | int64_t mChangeTime;
|
---|
290 | uint32_t mDeviceNumber;
|
---|
291 | Utf8Str mFileAttrs;
|
---|
292 | uint32_t mGenerationID;
|
---|
293 | uint32_t mGID;
|
---|
294 | Utf8Str mGroupName;
|
---|
295 | uint32_t mNumHardLinks;
|
---|
296 | int64_t mModificationTime;
|
---|
297 | Utf8Str mName;
|
---|
298 | int64_t mNodeID;
|
---|
299 | uint32_t mNodeIDDevice;
|
---|
300 | int64_t mObjectSize;
|
---|
301 | FsObjType_T mType;
|
---|
302 | uint32_t mUID;
|
---|
303 | uint32_t mUserFlags;
|
---|
304 | Utf8Str mUserName;
|
---|
305 | Utf8Str mACL;
|
---|
306 | };
|
---|
307 |
|
---|
308 |
|
---|
309 | /**
|
---|
310 | * Structure for keeping all the relevant process
|
---|
311 | * starting parameters around.
|
---|
312 | */
|
---|
313 | class GuestProcessStartupInfo
|
---|
314 | {
|
---|
315 | public:
|
---|
316 |
|
---|
317 | GuestProcessStartupInfo(void)
|
---|
318 | : mFlags(ProcessCreateFlag_None),
|
---|
319 | mTimeoutMS(30 * 1000 /* 30s timeout by default */),
|
---|
320 | mPriority(ProcessPriority_Default) { }
|
---|
321 |
|
---|
322 | /** The process' friendly name. */
|
---|
323 | Utf8Str mName;
|
---|
324 | /** The actual command to execute. */
|
---|
325 | Utf8Str mCommand;
|
---|
326 | ProcessArguments mArguments;
|
---|
327 | GuestEnvironment mEnvironment;
|
---|
328 | uint32_t mFlags;
|
---|
329 | ULONG mTimeoutMS;
|
---|
330 | ProcessPriority_T mPriority;
|
---|
331 | ProcessAffinity mAffinity;
|
---|
332 | };
|
---|
333 |
|
---|
334 |
|
---|
335 | /**
|
---|
336 | * Class representing the "value" side of a "key=value" pair.
|
---|
337 | */
|
---|
338 | class GuestProcessStreamValue
|
---|
339 | {
|
---|
340 | public:
|
---|
341 |
|
---|
342 | GuestProcessStreamValue(void) { }
|
---|
343 | GuestProcessStreamValue(const char *pszValue)
|
---|
344 | : mValue(pszValue) {}
|
---|
345 |
|
---|
346 | GuestProcessStreamValue(const GuestProcessStreamValue& aThat)
|
---|
347 | : mValue(aThat.mValue) { }
|
---|
348 |
|
---|
349 | Utf8Str mValue;
|
---|
350 | };
|
---|
351 |
|
---|
352 | /** Map containing "key=value" pairs of a guest process stream. */
|
---|
353 | typedef std::pair< Utf8Str, GuestProcessStreamValue > GuestCtrlStreamPair;
|
---|
354 | typedef std::map < Utf8Str, GuestProcessStreamValue > GuestCtrlStreamPairMap;
|
---|
355 | typedef std::map < Utf8Str, GuestProcessStreamValue >::iterator GuestCtrlStreamPairMapIter;
|
---|
356 | typedef std::map < Utf8Str, GuestProcessStreamValue >::const_iterator GuestCtrlStreamPairMapIterConst;
|
---|
357 |
|
---|
358 | /**
|
---|
359 | * Class representing a block of stream pairs (key=value). Each block in a raw guest
|
---|
360 | * output stream is separated by "\0\0", each pair is separated by "\0". The overall
|
---|
361 | * end of a guest stream is marked by "\0\0\0\0".
|
---|
362 | */
|
---|
363 | class GuestProcessStreamBlock
|
---|
364 | {
|
---|
365 | public:
|
---|
366 |
|
---|
367 | GuestProcessStreamBlock(void);
|
---|
368 |
|
---|
369 | virtual ~GuestProcessStreamBlock(void);
|
---|
370 |
|
---|
371 | public:
|
---|
372 |
|
---|
373 | void Clear(void);
|
---|
374 |
|
---|
375 | #ifdef DEBUG
|
---|
376 | void DumpToLog(void) const;
|
---|
377 | #endif
|
---|
378 |
|
---|
379 | int GetInt64Ex(const char *pszKey, int64_t *piVal) const;
|
---|
380 |
|
---|
381 | int64_t GetInt64(const char *pszKey) const;
|
---|
382 |
|
---|
383 | size_t GetCount(void) const;
|
---|
384 |
|
---|
385 | const char* GetString(const char *pszKey) const;
|
---|
386 |
|
---|
387 | int GetUInt32Ex(const char *pszKey, uint32_t *puVal) const;
|
---|
388 |
|
---|
389 | uint32_t GetUInt32(const char *pszKey) const;
|
---|
390 |
|
---|
391 | bool IsEmpty(void) { return m_mapPairs.empty(); }
|
---|
392 |
|
---|
393 | int SetValue(const char *pszKey, const char *pszValue);
|
---|
394 |
|
---|
395 | protected:
|
---|
396 |
|
---|
397 | GuestCtrlStreamPairMap m_mapPairs;
|
---|
398 | };
|
---|
399 |
|
---|
400 | /** Vector containing multiple allocated stream pair objects. */
|
---|
401 | typedef std::vector< GuestProcessStreamBlock > GuestCtrlStreamObjects;
|
---|
402 | typedef std::vector< GuestProcessStreamBlock >::iterator GuestCtrlStreamObjectsIter;
|
---|
403 | typedef std::vector< GuestProcessStreamBlock >::const_iterator GuestCtrlStreamObjectsIterConst;
|
---|
404 |
|
---|
405 | /**
|
---|
406 | * Class for parsing machine-readable guest process output by VBoxService'
|
---|
407 | * toolbox commands ("vbox_ls", "vbox_stat" etc), aka "guest stream".
|
---|
408 | */
|
---|
409 | class GuestProcessStream
|
---|
410 | {
|
---|
411 |
|
---|
412 | public:
|
---|
413 |
|
---|
414 | GuestProcessStream();
|
---|
415 |
|
---|
416 | virtual ~GuestProcessStream();
|
---|
417 |
|
---|
418 | public:
|
---|
419 |
|
---|
420 | int AddData(const BYTE *pbData, size_t cbData);
|
---|
421 |
|
---|
422 | void Destroy();
|
---|
423 |
|
---|
424 | #ifdef DEBUG
|
---|
425 | void Dump(const char *pszFile);
|
---|
426 | #endif
|
---|
427 |
|
---|
428 | uint32_t GetOffset();
|
---|
429 |
|
---|
430 | uint32_t GetSize();
|
---|
431 |
|
---|
432 | int ParseBlock(GuestProcessStreamBlock &streamBlock);
|
---|
433 |
|
---|
434 | protected:
|
---|
435 |
|
---|
436 | /** Currently allocated size of internal stream buffer. */
|
---|
437 | uint32_t m_cbAllocated;
|
---|
438 | /** Currently used size of allocated internal stream buffer. */
|
---|
439 | uint32_t m_cbSize;
|
---|
440 | /** Current offset within the internal stream buffer. */
|
---|
441 | uint32_t m_cbOffset;
|
---|
442 | /** Internal stream buffer. */
|
---|
443 | BYTE *m_pbBuffer;
|
---|
444 | };
|
---|
445 |
|
---|
446 | class Guest;
|
---|
447 | class Progress;
|
---|
448 |
|
---|
449 | class GuestTask
|
---|
450 | {
|
---|
451 |
|
---|
452 | public:
|
---|
453 |
|
---|
454 | enum TaskType
|
---|
455 | {
|
---|
456 | /** Copies a file from host to the guest. */
|
---|
457 | TaskType_CopyFileToGuest = 50,
|
---|
458 | /** Copies a file from guest to the host. */
|
---|
459 | TaskType_CopyFileFromGuest = 55,
|
---|
460 | /** Update Guest Additions by directly copying the required installer
|
---|
461 | * off the .ISO file, transfer it to the guest and execute the installer
|
---|
462 | * with system privileges. */
|
---|
463 | TaskType_UpdateGuestAdditions = 100
|
---|
464 | };
|
---|
465 |
|
---|
466 | GuestTask(TaskType aTaskType, Guest *aThat, Progress *aProgress);
|
---|
467 |
|
---|
468 | virtual ~GuestTask();
|
---|
469 |
|
---|
470 | int startThread();
|
---|
471 |
|
---|
472 | static int taskThread(RTTHREAD aThread, void *pvUser);
|
---|
473 | static int uploadProgress(unsigned uPercent, void *pvUser);
|
---|
474 | static HRESULT setProgressSuccess(ComObjPtr<Progress> pProgress);
|
---|
475 | static HRESULT setProgressErrorMsg(HRESULT hr,
|
---|
476 | ComObjPtr<Progress> pProgress, const char * pszText, ...);
|
---|
477 | static HRESULT setProgressErrorParent(HRESULT hr,
|
---|
478 | ComObjPtr<Progress> pProgress, ComObjPtr<Guest> pGuest);
|
---|
479 |
|
---|
480 | TaskType taskType;
|
---|
481 | ComObjPtr<Guest> pGuest;
|
---|
482 | ComObjPtr<Progress> pProgress;
|
---|
483 | HRESULT rc;
|
---|
484 |
|
---|
485 | /* Task data. */
|
---|
486 | Utf8Str strSource;
|
---|
487 | Utf8Str strDest;
|
---|
488 | Utf8Str strUserName;
|
---|
489 | Utf8Str strPassword;
|
---|
490 | ULONG uFlags;
|
---|
491 | };
|
---|
492 | #endif // ____H_GUESTIMPLPRIVATE
|
---|
493 |
|
---|