1 | /** @file
|
---|
2 | *
|
---|
3 | * Internal helpers/structures for guest control functionality.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011-2013 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 "ConsoleImpl.h"
|
---|
22 |
|
---|
23 | #include <iprt/asm.h>
|
---|
24 | #include <iprt/semaphore.h>
|
---|
25 |
|
---|
26 | #include <VBox/com/com.h>
|
---|
27 | #include <VBox/com/ErrorInfo.h>
|
---|
28 | #include <VBox/com/string.h>
|
---|
29 | #include <VBox/com/VirtualBox.h>
|
---|
30 |
|
---|
31 | #include <map>
|
---|
32 | #include <vector>
|
---|
33 |
|
---|
34 | using namespace com;
|
---|
35 |
|
---|
36 | #ifdef VBOX_WITH_GUEST_CONTROL
|
---|
37 | # include <VBox/HostServices/GuestControlSvc.h>
|
---|
38 | using namespace guestControl;
|
---|
39 | #endif
|
---|
40 |
|
---|
41 | /** Vector holding a process' CPU affinity. */
|
---|
42 | typedef std::vector <LONG> ProcessAffinity;
|
---|
43 | /** Vector holding process startup arguments. */
|
---|
44 | typedef std::vector <Utf8Str> ProcessArguments;
|
---|
45 |
|
---|
46 | class GuestProcessStreamBlock;
|
---|
47 | class GuestSession;
|
---|
48 |
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Simple structure mantaining guest credentials.
|
---|
52 | */
|
---|
53 | struct GuestCredentials
|
---|
54 | {
|
---|
55 | Utf8Str mUser;
|
---|
56 | Utf8Str mPassword;
|
---|
57 | Utf8Str mDomain;
|
---|
58 | };
|
---|
59 |
|
---|
60 |
|
---|
61 | typedef std::vector <Utf8Str> GuestEnvironmentArray;
|
---|
62 | class GuestEnvironment
|
---|
63 | {
|
---|
64 | public:
|
---|
65 |
|
---|
66 | int BuildEnvironmentBlock(void **ppvEnv, size_t *pcbEnv, uint32_t *pcEnvVars);
|
---|
67 |
|
---|
68 | void Clear(void);
|
---|
69 |
|
---|
70 | int CopyFrom(const GuestEnvironmentArray &environment);
|
---|
71 |
|
---|
72 | int CopyTo(GuestEnvironmentArray &environment);
|
---|
73 |
|
---|
74 | static void FreeEnvironmentBlock(void *pvEnv);
|
---|
75 |
|
---|
76 | Utf8Str Get(const Utf8Str &strKey);
|
---|
77 |
|
---|
78 | Utf8Str Get(size_t nPos);
|
---|
79 |
|
---|
80 | bool Has(const Utf8Str &strKey);
|
---|
81 |
|
---|
82 | int Set(const Utf8Str &strKey, const Utf8Str &strValue);
|
---|
83 |
|
---|
84 | int Set(const Utf8Str &strPair);
|
---|
85 |
|
---|
86 | size_t Size(void);
|
---|
87 |
|
---|
88 | int Unset(const Utf8Str &strKey);
|
---|
89 |
|
---|
90 | public:
|
---|
91 |
|
---|
92 | GuestEnvironment& operator=(const GuestEnvironmentArray &that);
|
---|
93 |
|
---|
94 | GuestEnvironment& operator=(const GuestEnvironment &that);
|
---|
95 |
|
---|
96 | protected:
|
---|
97 |
|
---|
98 | int appendToEnvBlock(const char *pszEnv, void **ppvList, size_t *pcbList, uint32_t *pcEnvVars);
|
---|
99 |
|
---|
100 | protected:
|
---|
101 |
|
---|
102 | std::map <Utf8Str, Utf8Str> mEnvironment;
|
---|
103 | };
|
---|
104 |
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * Structure for keeping all the relevant guest file
|
---|
108 | * information around.
|
---|
109 | */
|
---|
110 | struct GuestFileOpenInfo
|
---|
111 | {
|
---|
112 | /** The filename. */
|
---|
113 | Utf8Str mFileName;
|
---|
114 | /** Then file's opening mode. */
|
---|
115 | Utf8Str mOpenMode;
|
---|
116 | /** The file's disposition mode. */
|
---|
117 | Utf8Str mDisposition;
|
---|
118 | /** The file's sharing mode.
|
---|
119 | **@todo Not implemented yet.*/
|
---|
120 | Utf8Str mSharingMode;
|
---|
121 | /** Octal creation mode. */
|
---|
122 | uint32_t mCreationMode;
|
---|
123 | /** The initial offset on open. */
|
---|
124 | uint64_t mInitialOffset;
|
---|
125 | };
|
---|
126 |
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * Structure representing information of a
|
---|
130 | * file system object.
|
---|
131 | */
|
---|
132 | struct GuestFsObjData
|
---|
133 | {
|
---|
134 | /** Helper function to extract the data from
|
---|
135 | * a certin VBoxService tool's guest stream block. */
|
---|
136 | int FromLs(const GuestProcessStreamBlock &strmBlk);
|
---|
137 | int FromStat(const GuestProcessStreamBlock &strmBlk);
|
---|
138 |
|
---|
139 | int64_t mAccessTime;
|
---|
140 | int64_t mAllocatedSize;
|
---|
141 | int64_t mBirthTime;
|
---|
142 | int64_t mChangeTime;
|
---|
143 | uint32_t mDeviceNumber;
|
---|
144 | Utf8Str mFileAttrs;
|
---|
145 | uint32_t mGenerationID;
|
---|
146 | uint32_t mGID;
|
---|
147 | Utf8Str mGroupName;
|
---|
148 | uint32_t mNumHardLinks;
|
---|
149 | int64_t mModificationTime;
|
---|
150 | Utf8Str mName;
|
---|
151 | int64_t mNodeID;
|
---|
152 | uint32_t mNodeIDDevice;
|
---|
153 | int64_t mObjectSize;
|
---|
154 | FsObjType_T mType;
|
---|
155 | uint32_t mUID;
|
---|
156 | uint32_t mUserFlags;
|
---|
157 | Utf8Str mUserName;
|
---|
158 | Utf8Str mACL;
|
---|
159 | };
|
---|
160 |
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * Structure for keeping all the relevant guest session
|
---|
164 | * startup parameters around.
|
---|
165 | */
|
---|
166 | class GuestSessionStartupInfo
|
---|
167 | {
|
---|
168 | public:
|
---|
169 |
|
---|
170 | GuestSessionStartupInfo(void)
|
---|
171 | : mIsInternal(false /* Non-internal session */),
|
---|
172 | mOpenTimeoutMS(30 * 1000 /* 30s opening timeout */),
|
---|
173 | mOpenFlags(0 /* No opening flags set */) { }
|
---|
174 |
|
---|
175 | /** The session's friendly name. Optional. */
|
---|
176 | Utf8Str mName;
|
---|
177 | /** The session's unique ID. Used to encode
|
---|
178 | * a context ID. */
|
---|
179 | uint32_t mID;
|
---|
180 | /** Flag indicating if this is an internal session
|
---|
181 | * or not. Internal session are not accessible by
|
---|
182 | * public API clients. */
|
---|
183 | bool mIsInternal;
|
---|
184 | /** Timeout (in ms) used for opening the session. */
|
---|
185 | uint32_t mOpenTimeoutMS;
|
---|
186 | /** Session opening flags. */
|
---|
187 | uint32_t mOpenFlags;
|
---|
188 | };
|
---|
189 |
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * Structure for keeping all the relevant guest process
|
---|
193 | * startup parameters around.
|
---|
194 | */
|
---|
195 | class GuestProcessStartupInfo
|
---|
196 | {
|
---|
197 | public:
|
---|
198 |
|
---|
199 | GuestProcessStartupInfo(void)
|
---|
200 | : mFlags(ProcessCreateFlag_None),
|
---|
201 | mTimeoutMS(30 * 1000 /* 30s timeout by default */),
|
---|
202 | mPriority(ProcessPriority_Default) { }
|
---|
203 |
|
---|
204 | /** The process' friendly name. */
|
---|
205 | Utf8Str mName;
|
---|
206 | /** The actual command to execute. */
|
---|
207 | Utf8Str mCommand;
|
---|
208 | ProcessArguments mArguments;
|
---|
209 | GuestEnvironment mEnvironment;
|
---|
210 | /** Process creation flags. */
|
---|
211 | uint32_t mFlags;
|
---|
212 | ULONG mTimeoutMS;
|
---|
213 | /** Process priority. */
|
---|
214 | ProcessPriority_T mPriority;
|
---|
215 | /** Process affinity. At the moment we
|
---|
216 | * only support 64 VCPUs. API and
|
---|
217 | * guest can do more already! */
|
---|
218 | uint64_t mAffinity;
|
---|
219 | };
|
---|
220 |
|
---|
221 |
|
---|
222 | /**
|
---|
223 | * Class representing the "value" side of a "key=value" pair.
|
---|
224 | */
|
---|
225 | class GuestProcessStreamValue
|
---|
226 | {
|
---|
227 | public:
|
---|
228 |
|
---|
229 | GuestProcessStreamValue(void) { }
|
---|
230 | GuestProcessStreamValue(const char *pszValue)
|
---|
231 | : mValue(pszValue) {}
|
---|
232 |
|
---|
233 | GuestProcessStreamValue(const GuestProcessStreamValue& aThat)
|
---|
234 | : mValue(aThat.mValue) { }
|
---|
235 |
|
---|
236 | Utf8Str mValue;
|
---|
237 | };
|
---|
238 |
|
---|
239 | /** Map containing "key=value" pairs of a guest process stream. */
|
---|
240 | typedef std::pair< Utf8Str, GuestProcessStreamValue > GuestCtrlStreamPair;
|
---|
241 | typedef std::map < Utf8Str, GuestProcessStreamValue > GuestCtrlStreamPairMap;
|
---|
242 | typedef std::map < Utf8Str, GuestProcessStreamValue >::iterator GuestCtrlStreamPairMapIter;
|
---|
243 | typedef std::map < Utf8Str, GuestProcessStreamValue >::const_iterator GuestCtrlStreamPairMapIterConst;
|
---|
244 |
|
---|
245 | /**
|
---|
246 | * Class representing a block of stream pairs (key=value). Each block in a raw guest
|
---|
247 | * output stream is separated by "\0\0", each pair is separated by "\0". The overall
|
---|
248 | * end of a guest stream is marked by "\0\0\0\0".
|
---|
249 | */
|
---|
250 | class GuestProcessStreamBlock
|
---|
251 | {
|
---|
252 | public:
|
---|
253 |
|
---|
254 | GuestProcessStreamBlock(void);
|
---|
255 |
|
---|
256 | virtual ~GuestProcessStreamBlock(void);
|
---|
257 |
|
---|
258 | public:
|
---|
259 |
|
---|
260 | void Clear(void);
|
---|
261 |
|
---|
262 | #ifdef DEBUG
|
---|
263 | void DumpToLog(void) const;
|
---|
264 | #endif
|
---|
265 |
|
---|
266 | int GetInt64Ex(const char *pszKey, int64_t *piVal) const;
|
---|
267 |
|
---|
268 | int64_t GetInt64(const char *pszKey) const;
|
---|
269 |
|
---|
270 | size_t GetCount(void) const;
|
---|
271 |
|
---|
272 | const char* GetString(const char *pszKey) const;
|
---|
273 |
|
---|
274 | int GetUInt32Ex(const char *pszKey, uint32_t *puVal) const;
|
---|
275 |
|
---|
276 | uint32_t GetUInt32(const char *pszKey) const;
|
---|
277 |
|
---|
278 | bool IsEmpty(void) { return mPairs.empty(); }
|
---|
279 |
|
---|
280 | int SetValue(const char *pszKey, const char *pszValue);
|
---|
281 |
|
---|
282 | protected:
|
---|
283 |
|
---|
284 | GuestCtrlStreamPairMap mPairs;
|
---|
285 | };
|
---|
286 |
|
---|
287 | /** Vector containing multiple allocated stream pair objects. */
|
---|
288 | typedef std::vector< GuestProcessStreamBlock > GuestCtrlStreamObjects;
|
---|
289 | typedef std::vector< GuestProcessStreamBlock >::iterator GuestCtrlStreamObjectsIter;
|
---|
290 | typedef std::vector< GuestProcessStreamBlock >::const_iterator GuestCtrlStreamObjectsIterConst;
|
---|
291 |
|
---|
292 | /**
|
---|
293 | * Class for parsing machine-readable guest process output by VBoxService'
|
---|
294 | * toolbox commands ("vbox_ls", "vbox_stat" etc), aka "guest stream".
|
---|
295 | */
|
---|
296 | class GuestProcessStream
|
---|
297 | {
|
---|
298 |
|
---|
299 | public:
|
---|
300 |
|
---|
301 | GuestProcessStream();
|
---|
302 |
|
---|
303 | virtual ~GuestProcessStream();
|
---|
304 |
|
---|
305 | public:
|
---|
306 |
|
---|
307 | int AddData(const BYTE *pbData, size_t cbData);
|
---|
308 |
|
---|
309 | void Destroy();
|
---|
310 |
|
---|
311 | #ifdef DEBUG
|
---|
312 | void Dump(const char *pszFile);
|
---|
313 | #endif
|
---|
314 |
|
---|
315 | uint32_t GetOffset();
|
---|
316 |
|
---|
317 | uint32_t GetSize();
|
---|
318 |
|
---|
319 | int ParseBlock(GuestProcessStreamBlock &streamBlock);
|
---|
320 |
|
---|
321 | protected:
|
---|
322 |
|
---|
323 | /** Currently allocated size of internal stream buffer. */
|
---|
324 | uint32_t m_cbAllocated;
|
---|
325 | /** Currently used size of allocated internal stream buffer. */
|
---|
326 | uint32_t m_cbSize;
|
---|
327 | /** Current offset within the internal stream buffer. */
|
---|
328 | uint32_t m_cbOffset;
|
---|
329 | /** Internal stream buffer. */
|
---|
330 | BYTE *m_pbBuffer;
|
---|
331 | };
|
---|
332 |
|
---|
333 | class Guest;
|
---|
334 | class Progress;
|
---|
335 |
|
---|
336 | class GuestTask
|
---|
337 | {
|
---|
338 |
|
---|
339 | public:
|
---|
340 |
|
---|
341 | enum TaskType
|
---|
342 | {
|
---|
343 | /** Copies a file from host to the guest. */
|
---|
344 | TaskType_CopyFileToGuest = 50,
|
---|
345 | /** Copies a file from guest to the host. */
|
---|
346 | TaskType_CopyFileFromGuest = 55,
|
---|
347 | /** Update Guest Additions by directly copying the required installer
|
---|
348 | * off the .ISO file, transfer it to the guest and execute the installer
|
---|
349 | * with system privileges. */
|
---|
350 | TaskType_UpdateGuestAdditions = 100
|
---|
351 | };
|
---|
352 |
|
---|
353 | GuestTask(TaskType aTaskType, Guest *aThat, Progress *aProgress);
|
---|
354 |
|
---|
355 | virtual ~GuestTask();
|
---|
356 |
|
---|
357 | int startThread();
|
---|
358 |
|
---|
359 | static int taskThread(RTTHREAD aThread, void *pvUser);
|
---|
360 | static int uploadProgress(unsigned uPercent, void *pvUser);
|
---|
361 | static HRESULT setProgressSuccess(ComObjPtr<Progress> pProgress);
|
---|
362 | static HRESULT setProgressErrorMsg(HRESULT hr,
|
---|
363 | ComObjPtr<Progress> pProgress, const char * pszText, ...);
|
---|
364 | static HRESULT setProgressErrorParent(HRESULT hr,
|
---|
365 | ComObjPtr<Progress> pProgress, ComObjPtr<Guest> pGuest);
|
---|
366 |
|
---|
367 | TaskType taskType;
|
---|
368 | ComObjPtr<Guest> pGuest;
|
---|
369 | ComObjPtr<Progress> pProgress;
|
---|
370 | HRESULT rc;
|
---|
371 |
|
---|
372 | /* Task data. */
|
---|
373 | Utf8Str strSource;
|
---|
374 | Utf8Str strDest;
|
---|
375 | Utf8Str strUserName;
|
---|
376 | Utf8Str strPassword;
|
---|
377 | ULONG uFlags;
|
---|
378 | };
|
---|
379 |
|
---|
380 | class GuestWaitEvent
|
---|
381 | {
|
---|
382 |
|
---|
383 | public:
|
---|
384 |
|
---|
385 | GuestWaitEvent(uint32_t mCID, const std::list<VBoxEventType_T> &lstEvents);
|
---|
386 | virtual ~GuestWaitEvent(void);
|
---|
387 |
|
---|
388 | public:
|
---|
389 |
|
---|
390 | uint32_t ContextID(void) { return mCID; };
|
---|
391 | const ComPtr<IEvent> Event(void) { return mEvent; };
|
---|
392 | const std::list<VBoxEventType_T> Types(void) { return mEventTypes; };
|
---|
393 | size_t TypeCount(void) { return mEventTypes.size(); }
|
---|
394 | virtual int Signal(IEvent *pEvent);
|
---|
395 | int Wait(RTMSINTERVAL uTimeoutMS);
|
---|
396 |
|
---|
397 | protected:
|
---|
398 |
|
---|
399 | /* Shutdown indicator. */
|
---|
400 | bool fAborted;
|
---|
401 | /* Associated context ID (CID). */
|
---|
402 | uint32_t mCID;
|
---|
403 | /** List of event types this event should
|
---|
404 | * be signalled on. */
|
---|
405 | std::list<VBoxEventType_T> mEventTypes;
|
---|
406 | /** The event semaphore for triggering
|
---|
407 | * the actual event. */
|
---|
408 | RTSEMEVENT mEventSem;
|
---|
409 | /** Pointer to the actual event. */
|
---|
410 | ComPtr<IEvent> mEvent;
|
---|
411 | };
|
---|
412 | typedef std::list < GuestWaitEvent* > GuestWaitEvents;
|
---|
413 | typedef std::map < VBoxEventType_T, GuestWaitEvents > GuestWaitEventTypes;
|
---|
414 |
|
---|
415 | class GuestBase
|
---|
416 | {
|
---|
417 |
|
---|
418 | public:
|
---|
419 |
|
---|
420 | GuestBase(void);
|
---|
421 | virtual ~GuestBase(void);
|
---|
422 |
|
---|
423 | public:
|
---|
424 |
|
---|
425 | /** For external event listeners. */
|
---|
426 | int signalWaitEvents(VBoxEventType_T aType, IEvent *aEvent);
|
---|
427 |
|
---|
428 | public:
|
---|
429 |
|
---|
430 | int baseInit(void);
|
---|
431 | void baseUninit(void);
|
---|
432 | int cancelWaitEvents(void);
|
---|
433 | int generateContextID(uint32_t uSessionID, uint32_t uObjectID, uint32_t *puContextID);
|
---|
434 | int registerWaitEvent(uint32_t uSessionID, uint32_t uObjectID, const std::list<VBoxEventType_T> &lstEvents, GuestWaitEvent **ppEvent);
|
---|
435 | void unregisterWaitEvent(GuestWaitEvent *pEvent);
|
---|
436 | int waitForEvent(GuestWaitEvent *pEvent, uint32_t uTimeoutMS, VBoxEventType_T *pType, IEvent **ppEvent);
|
---|
437 |
|
---|
438 | protected:
|
---|
439 |
|
---|
440 | /** Pointer to the console object. Needed
|
---|
441 | * for HGCM (VMMDev) communication. */
|
---|
442 | Console *mConsole;
|
---|
443 | /** The next upcoming context ID for this object. */
|
---|
444 | uint32_t mNextContextID;
|
---|
445 | /** Local listener for handling the waiting events
|
---|
446 | * internally. */
|
---|
447 | ComPtr<IEventListener> mLocalListener;
|
---|
448 | /** Critical section for wait events access. */
|
---|
449 | RTCRITSECT mWaitEventCritSect;
|
---|
450 | /** Map of internal events to wait for. */
|
---|
451 | GuestWaitEventTypes mWaitEvents;
|
---|
452 | };
|
---|
453 |
|
---|
454 | /**
|
---|
455 | * Virtual class (interface) for guest objects (processes, files, ...) --
|
---|
456 | * contains all per-object callback management.
|
---|
457 | */
|
---|
458 | class GuestObject : public GuestBase
|
---|
459 | {
|
---|
460 |
|
---|
461 | public:
|
---|
462 |
|
---|
463 | GuestObject(void);
|
---|
464 | virtual ~GuestObject(void);
|
---|
465 |
|
---|
466 | public:
|
---|
467 |
|
---|
468 | ULONG getObjectID(void) { return mObjectID; }
|
---|
469 |
|
---|
470 | protected:
|
---|
471 |
|
---|
472 | /** Callback dispatcher -- must be implemented by the actual object. */
|
---|
473 | virtual int callbackDispatcher(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCb) = 0;
|
---|
474 |
|
---|
475 | protected:
|
---|
476 |
|
---|
477 | int bindToSession(Console *pConsole, GuestSession *pSession, uint32_t uObjectID);
|
---|
478 | int registerWaitEvent(const std::list<VBoxEventType_T> &lstEvents, GuestWaitEvent **ppEvent);
|
---|
479 | int sendCommand(uint32_t uFunction, uint32_t uParms, PVBOXHGCMSVCPARM paParms);
|
---|
480 |
|
---|
481 | protected:
|
---|
482 |
|
---|
483 | /**
|
---|
484 | * Commom parameters for all derived objects, when then have
|
---|
485 | * an own mData structure to keep their specific data around.
|
---|
486 | */
|
---|
487 |
|
---|
488 | /** Pointer to parent session. Per definition
|
---|
489 | * this objects *always* lives shorter than the
|
---|
490 | * parent. */
|
---|
491 | GuestSession *mSession;
|
---|
492 | /** The object ID -- must be unique for each guest
|
---|
493 | * object and is encoded into the context ID. Must
|
---|
494 | * be set manually when initializing the object.
|
---|
495 | *
|
---|
496 | * For guest processes this is the internal PID,
|
---|
497 | * for guest files this is the internal file ID. */
|
---|
498 | uint32_t mObjectID;
|
---|
499 | };
|
---|
500 | #endif // ____H_GUESTIMPLPRIVATE
|
---|
501 |
|
---|