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 | /** The wait result when returning from the wait call. */
|
---|
175 | ProcessWaitResult_T mResult;
|
---|
176 | int mRC;
|
---|
177 | };
|
---|
178 |
|
---|
179 |
|
---|
180 | /*
|
---|
181 | * Class representing a guest control process event.
|
---|
182 | */
|
---|
183 | class GuestProcessEvent : public GuestCtrlEvent
|
---|
184 | {
|
---|
185 | public:
|
---|
186 | GuestProcessEvent(void);
|
---|
187 |
|
---|
188 | GuestProcessEvent(uint32_t uWaitFlags);
|
---|
189 |
|
---|
190 | virtual ~GuestProcessEvent(void);
|
---|
191 |
|
---|
192 | public:
|
---|
193 |
|
---|
194 | void Destroy(void);
|
---|
195 |
|
---|
196 | int Init(uint32_t uWaitFlags);
|
---|
197 |
|
---|
198 | uint32_t GetWaitFlags(void) { return ASMAtomicReadU32(&mWaitFlags); }
|
---|
199 |
|
---|
200 | GuestProcessWaitResult GetResult(void) { return mWaitResult; }
|
---|
201 |
|
---|
202 | int Signal(ProcessWaitResult_T enmResult, int rc = VINF_SUCCESS);
|
---|
203 |
|
---|
204 | protected:
|
---|
205 |
|
---|
206 | /** The waiting flag(s). The specifies what to
|
---|
207 | * wait for. */
|
---|
208 | uint32_t mWaitFlags;
|
---|
209 | /** Structure containing the overall result. */
|
---|
210 | GuestProcessWaitResult mWaitResult;
|
---|
211 | };
|
---|
212 |
|
---|
213 |
|
---|
214 | /**
|
---|
215 | * Simple structure mantaining guest credentials.
|
---|
216 | */
|
---|
217 | struct GuestCredentials
|
---|
218 | {
|
---|
219 | Utf8Str mUser;
|
---|
220 | Utf8Str mPassword;
|
---|
221 | Utf8Str mDomain;
|
---|
222 | };
|
---|
223 |
|
---|
224 |
|
---|
225 | typedef std::vector <Utf8Str> GuestEnvironmentArray;
|
---|
226 | class GuestEnvironment
|
---|
227 | {
|
---|
228 | public:
|
---|
229 |
|
---|
230 | int BuildEnvironmentBlock(void **ppvEnv, size_t *pcbEnv, uint32_t *pcEnvVars);
|
---|
231 |
|
---|
232 | void Clear(void);
|
---|
233 |
|
---|
234 | int CopyFrom(const GuestEnvironmentArray &environment);
|
---|
235 |
|
---|
236 | int CopyTo(GuestEnvironmentArray &environment);
|
---|
237 |
|
---|
238 | static void FreeEnvironmentBlock(void *pvEnv);
|
---|
239 |
|
---|
240 | Utf8Str Get(const Utf8Str &strKey);
|
---|
241 |
|
---|
242 | Utf8Str Get(size_t nPos);
|
---|
243 |
|
---|
244 | bool Has(const Utf8Str &strKey);
|
---|
245 |
|
---|
246 | int Set(const Utf8Str &strKey, const Utf8Str &strValue);
|
---|
247 |
|
---|
248 | int Set(const Utf8Str &strPair);
|
---|
249 |
|
---|
250 | size_t Size(void);
|
---|
251 |
|
---|
252 | int Unset(const Utf8Str &strKey);
|
---|
253 |
|
---|
254 | public:
|
---|
255 |
|
---|
256 | GuestEnvironment& operator=(const GuestEnvironmentArray &that);
|
---|
257 |
|
---|
258 | GuestEnvironment& operator=(const GuestEnvironment &that);
|
---|
259 |
|
---|
260 | protected:
|
---|
261 |
|
---|
262 | int appendToEnvBlock(const char *pszEnv, void **ppvList, size_t *pcbList, uint32_t *pcEnvVars);
|
---|
263 |
|
---|
264 | protected:
|
---|
265 |
|
---|
266 | std::map <Utf8Str, Utf8Str> mEnvironment;
|
---|
267 | };
|
---|
268 |
|
---|
269 |
|
---|
270 | /**
|
---|
271 | * Structure representing information of a
|
---|
272 | * file system object.
|
---|
273 | */
|
---|
274 | struct GuestFsObjData
|
---|
275 | {
|
---|
276 | /** Helper function to extract the data from
|
---|
277 | * a guest stream block. */
|
---|
278 | int From(const GuestProcessStreamBlock &strmBlk);
|
---|
279 |
|
---|
280 | int64_t mAccessTime;
|
---|
281 | int64_t mAllocatedSize;
|
---|
282 | int64_t mBirthTime;
|
---|
283 | int64_t mChangeTime;
|
---|
284 | uint32_t mDeviceNumber;
|
---|
285 | Utf8Str mFileAttrs;
|
---|
286 | uint32_t mGenerationID;
|
---|
287 | uint32_t mGID;
|
---|
288 | Utf8Str mGroupName;
|
---|
289 | uint32_t mNumHardLinks;
|
---|
290 | int64_t mModificationTime;
|
---|
291 | Utf8Str mName;
|
---|
292 | int64_t mNodeID;
|
---|
293 | uint32_t mNodeIDDevice;
|
---|
294 | int64_t mObjectSize;
|
---|
295 | FsObjType_T mType;
|
---|
296 | uint32_t mUID;
|
---|
297 | uint32_t mUserFlags;
|
---|
298 | Utf8Str mUserName;
|
---|
299 | Utf8Str mACL;
|
---|
300 | };
|
---|
301 |
|
---|
302 |
|
---|
303 | /**
|
---|
304 | * Structure for keeping all the relevant process
|
---|
305 | * starting parameters around.
|
---|
306 | */
|
---|
307 | class GuestProcessStartupInfo
|
---|
308 | {
|
---|
309 | public:
|
---|
310 |
|
---|
311 | GuestProcessStartupInfo(void)
|
---|
312 | : mFlags(ProcessCreateFlag_None),
|
---|
313 | mTimeoutMS(30 * 1000 /* 30s timeout by default */),
|
---|
314 | mPriority(ProcessPriority_Default) { }
|
---|
315 |
|
---|
316 | /** The process' friendly name. */
|
---|
317 | Utf8Str mName;
|
---|
318 | /** The actual command to execute. */
|
---|
319 | Utf8Str mCommand;
|
---|
320 | ProcessArguments mArguments;
|
---|
321 | GuestEnvironment mEnvironment;
|
---|
322 | uint32_t mFlags;
|
---|
323 | ULONG mTimeoutMS;
|
---|
324 | ProcessPriority_T mPriority;
|
---|
325 | ProcessAffinity mAffinity;
|
---|
326 | };
|
---|
327 |
|
---|
328 |
|
---|
329 | /**
|
---|
330 | * Class representing the "value" side of a "key=value" pair.
|
---|
331 | */
|
---|
332 | class GuestProcessStreamValue
|
---|
333 | {
|
---|
334 | public:
|
---|
335 |
|
---|
336 | GuestProcessStreamValue(void) { }
|
---|
337 | GuestProcessStreamValue(const char *pszValue)
|
---|
338 | : mValue(pszValue) {}
|
---|
339 |
|
---|
340 | GuestProcessStreamValue(const GuestProcessStreamValue& aThat)
|
---|
341 | : mValue(aThat.mValue) { }
|
---|
342 |
|
---|
343 | Utf8Str mValue;
|
---|
344 | };
|
---|
345 |
|
---|
346 | /** Map containing "key=value" pairs of a guest process stream. */
|
---|
347 | typedef std::pair< Utf8Str, GuestProcessStreamValue > GuestCtrlStreamPair;
|
---|
348 | typedef std::map < Utf8Str, GuestProcessStreamValue > GuestCtrlStreamPairMap;
|
---|
349 | typedef std::map < Utf8Str, GuestProcessStreamValue >::iterator GuestCtrlStreamPairMapIter;
|
---|
350 | typedef std::map < Utf8Str, GuestProcessStreamValue >::const_iterator GuestCtrlStreamPairMapIterConst;
|
---|
351 |
|
---|
352 | /**
|
---|
353 | * Class representing a block of stream pairs (key=value). Each block in a raw guest
|
---|
354 | * output stream is separated by "\0\0", each pair is separated by "\0". The overall
|
---|
355 | * end of a guest stream is marked by "\0\0\0\0".
|
---|
356 | */
|
---|
357 | class GuestProcessStreamBlock
|
---|
358 | {
|
---|
359 | public:
|
---|
360 |
|
---|
361 | GuestProcessStreamBlock(void);
|
---|
362 |
|
---|
363 | virtual ~GuestProcessStreamBlock(void);
|
---|
364 |
|
---|
365 | public:
|
---|
366 |
|
---|
367 | void Clear();
|
---|
368 |
|
---|
369 | #ifdef DEBUG
|
---|
370 | void Dump();
|
---|
371 | #endif
|
---|
372 |
|
---|
373 | int GetInt64Ex(const char *pszKey, int64_t *piVal) const;
|
---|
374 |
|
---|
375 | int64_t GetInt64(const char *pszKey) const;
|
---|
376 |
|
---|
377 | size_t GetCount(void) const;
|
---|
378 |
|
---|
379 | const char* GetString(const char *pszKey) const;
|
---|
380 |
|
---|
381 | int GetUInt32Ex(const char *pszKey, uint32_t *puVal) const;
|
---|
382 |
|
---|
383 | uint32_t GetUInt32(const char *pszKey) const;
|
---|
384 |
|
---|
385 | int SetValue(const char *pszKey, const char *pszValue);
|
---|
386 |
|
---|
387 | protected:
|
---|
388 |
|
---|
389 | GuestCtrlStreamPairMap m_mapPairs;
|
---|
390 | };
|
---|
391 |
|
---|
392 | /** Vector containing multiple allocated stream pair objects. */
|
---|
393 | typedef std::vector< GuestProcessStreamBlock > GuestCtrlStreamObjects;
|
---|
394 | typedef std::vector< GuestProcessStreamBlock >::iterator GuestCtrlStreamObjectsIter;
|
---|
395 | typedef std::vector< GuestProcessStreamBlock >::const_iterator GuestCtrlStreamObjectsIterConst;
|
---|
396 |
|
---|
397 | /**
|
---|
398 | * Class for parsing machine-readable guest process output by VBoxService'
|
---|
399 | * toolbox commands ("vbox_ls", "vbox_stat" etc), aka "guest stream".
|
---|
400 | */
|
---|
401 | class GuestProcessStream
|
---|
402 | {
|
---|
403 |
|
---|
404 | public:
|
---|
405 |
|
---|
406 | GuestProcessStream();
|
---|
407 |
|
---|
408 | virtual ~GuestProcessStream();
|
---|
409 |
|
---|
410 | public:
|
---|
411 |
|
---|
412 | int AddData(const BYTE *pbData, size_t cbData);
|
---|
413 |
|
---|
414 | void Destroy();
|
---|
415 |
|
---|
416 | #ifdef DEBUG
|
---|
417 | void Dump(const char *pszFile);
|
---|
418 | #endif
|
---|
419 |
|
---|
420 | uint32_t GetOffset();
|
---|
421 |
|
---|
422 | uint32_t GetSize();
|
---|
423 |
|
---|
424 | int ParseBlock(GuestProcessStreamBlock &streamBlock);
|
---|
425 |
|
---|
426 | protected:
|
---|
427 |
|
---|
428 | /** Currently allocated size of internal stream buffer. */
|
---|
429 | uint32_t m_cbAllocated;
|
---|
430 | /** Currently used size of allocated internal stream buffer. */
|
---|
431 | uint32_t m_cbSize;
|
---|
432 | /** Current offset within the internal stream buffer. */
|
---|
433 | uint32_t m_cbOffset;
|
---|
434 | /** Internal stream buffer. */
|
---|
435 | BYTE *m_pbBuffer;
|
---|
436 | };
|
---|
437 |
|
---|
438 | class Guest;
|
---|
439 | class Progress;
|
---|
440 |
|
---|
441 | class GuestTask
|
---|
442 | {
|
---|
443 |
|
---|
444 | public:
|
---|
445 |
|
---|
446 | enum TaskType
|
---|
447 | {
|
---|
448 | /** Copies a file from host to the guest. */
|
---|
449 | TaskType_CopyFileToGuest = 50,
|
---|
450 | /** Copies a file from guest to the host. */
|
---|
451 | TaskType_CopyFileFromGuest = 55,
|
---|
452 | /** Update Guest Additions by directly copying the required installer
|
---|
453 | * off the .ISO file, transfer it to the guest and execute the installer
|
---|
454 | * with system privileges. */
|
---|
455 | TaskType_UpdateGuestAdditions = 100
|
---|
456 | };
|
---|
457 |
|
---|
458 | GuestTask(TaskType aTaskType, Guest *aThat, Progress *aProgress);
|
---|
459 |
|
---|
460 | virtual ~GuestTask();
|
---|
461 |
|
---|
462 | int startThread();
|
---|
463 |
|
---|
464 | static int taskThread(RTTHREAD aThread, void *pvUser);
|
---|
465 | static int uploadProgress(unsigned uPercent, void *pvUser);
|
---|
466 | static HRESULT setProgressSuccess(ComObjPtr<Progress> pProgress);
|
---|
467 | static HRESULT setProgressErrorMsg(HRESULT hr,
|
---|
468 | ComObjPtr<Progress> pProgress, const char * pszText, ...);
|
---|
469 | static HRESULT setProgressErrorParent(HRESULT hr,
|
---|
470 | ComObjPtr<Progress> pProgress, ComObjPtr<Guest> pGuest);
|
---|
471 |
|
---|
472 | TaskType taskType;
|
---|
473 | ComObjPtr<Guest> pGuest;
|
---|
474 | ComObjPtr<Progress> pProgress;
|
---|
475 | HRESULT rc;
|
---|
476 |
|
---|
477 | /* Task data. */
|
---|
478 | Utf8Str strSource;
|
---|
479 | Utf8Str strDest;
|
---|
480 | Utf8Str strUserName;
|
---|
481 | Utf8Str strPassword;
|
---|
482 | ULONG uFlags;
|
---|
483 | };
|
---|
484 | #endif // ____H_GUESTIMPLPRIVATE
|
---|
485 |
|
---|