VirtualBox

source: vbox/trunk/src/VBox/Main/include/GuestCtrlImplPrivate.h@ 42611

Last change on this file since 42611 was 42611, checked in by vboxsync, 13 years ago

Guest Control 2.0: Update.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.3 KB
Line 
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
32using namespace com;
33
34#ifdef VBOX_WITH_GUEST_CONTROL
35# include <VBox/HostServices/GuestControlSvc.h>
36using 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. */
70typedef std::vector <LONG> ProcessAffinity;
71/** Vector holding process startup arguments. */
72typedef std::vector <Utf8Str> ProcessArguments;
73
74class GuestProcessStreamBlock;
75
76
77/**
78 * Generic class for a all guest control callbacks/events.
79 */
80class GuestCtrlEvent
81{
82public:
83
84 GuestCtrlEvent(void);
85
86 virtual ~GuestCtrlEvent(void);
87
88 /** @todo Copy/comparison operator? */
89
90public:
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
106protected:
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 */
125class GuestCtrlCallback : public GuestCtrlEvent
126{
127public:
128 GuestCtrlCallback(void);
129
130 GuestCtrlCallback(eVBoxGuestCtrlCallbackType enmType);
131
132 virtual ~GuestCtrlCallback(void);
133
134public:
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
154protected:
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};
170typedef std::map < uint32_t, GuestCtrlCallback* > GuestCtrlCallbacks;
171
172struct 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 */
183class GuestProcessEvent : public GuestCtrlEvent
184{
185public:
186 GuestProcessEvent(void);
187
188 GuestProcessEvent(uint32_t uWaitFlags);
189
190 virtual ~GuestProcessEvent(void);
191
192public:
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
204protected:
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 */
217struct GuestCredentials
218{
219 Utf8Str mUser;
220 Utf8Str mPassword;
221 Utf8Str mDomain;
222};
223
224
225typedef std::vector <Utf8Str> GuestEnvironmentArray;
226class GuestEnvironment
227{
228public:
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
254public:
255
256 GuestEnvironment& operator=(const GuestEnvironmentArray &that);
257
258 GuestEnvironment& operator=(const GuestEnvironment &that);
259
260protected:
261
262 int appendToEnvBlock(const char *pszEnv, void **ppvList, size_t *pcbList, uint32_t *pcEnvVars);
263
264protected:
265
266 std::map <Utf8Str, Utf8Str> mEnvironment;
267};
268
269
270/**
271 * Structure representing information of a
272 * file system object.
273 */
274struct 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 */
307struct GuestProcessStartupInfo
308{
309 GuestProcessStartupInfo(void)
310 : mFlags(ProcessCreateFlag_None),
311 mPriority(ProcessPriority_Default),
312 mTimeoutMS(30 * 1000 /* 30s timeout by default */) { }
313
314 /** The process' friendly name. */
315 Utf8Str mName;
316 /** The actual command to execute. */
317 Utf8Str mCommand;
318 ProcessArguments mArguments;
319 GuestEnvironment mEnvironment;
320 uint32_t mFlags;
321 ULONG mTimeoutMS;
322 ProcessPriority_T mPriority;
323 ProcessAffinity mAffinity;
324};
325
326
327/**
328 * Class representing the "value" side of a "key=value" pair.
329 */
330class GuestProcessStreamValue
331{
332public:
333
334 GuestProcessStreamValue() { }
335 GuestProcessStreamValue(const char *pszValue)
336 : mValue(pszValue) {}
337
338 GuestProcessStreamValue(const GuestProcessStreamValue& aThat)
339 : mValue(aThat.mValue) {}
340
341 Utf8Str mValue;
342};
343
344/** Map containing "key=value" pairs of a guest process stream. */
345typedef std::pair< Utf8Str, GuestProcessStreamValue > GuestCtrlStreamPair;
346typedef std::map < Utf8Str, GuestProcessStreamValue > GuestCtrlStreamPairMap;
347typedef std::map < Utf8Str, GuestProcessStreamValue >::iterator GuestCtrlStreamPairMapIter;
348typedef std::map < Utf8Str, GuestProcessStreamValue >::const_iterator GuestCtrlStreamPairMapIterConst;
349
350/**
351 * Class representing a block of stream pairs (key=value). Each block in a raw guest
352 * output stream is separated by "\0\0", each pair is separated by "\0". The overall
353 * end of a guest stream is marked by "\0\0\0\0".
354 */
355class GuestProcessStreamBlock
356{
357public:
358
359 GuestProcessStreamBlock(void);
360
361 virtual ~GuestProcessStreamBlock(void);
362
363public:
364
365 void Clear();
366
367#ifdef DEBUG
368 void Dump();
369#endif
370
371 int GetInt64Ex(const char *pszKey, int64_t *piVal) const;
372
373 int64_t GetInt64(const char *pszKey) const;
374
375 size_t GetCount(void) const;
376
377 const char* GetString(const char *pszKey) const;
378
379 int GetUInt32Ex(const char *pszKey, uint32_t *puVal) const;
380
381 uint32_t GetUInt32(const char *pszKey) const;
382
383 int SetValue(const char *pszKey, const char *pszValue);
384
385protected:
386
387 GuestCtrlStreamPairMap m_mapPairs;
388};
389
390/** Vector containing multiple allocated stream pair objects. */
391typedef std::vector< GuestProcessStreamBlock > GuestCtrlStreamObjects;
392typedef std::vector< GuestProcessStreamBlock >::iterator GuestCtrlStreamObjectsIter;
393typedef std::vector< GuestProcessStreamBlock >::const_iterator GuestCtrlStreamObjectsIterConst;
394
395/**
396 * Class for parsing machine-readable guest process output by VBoxService'
397 * toolbox commands ("vbox_ls", "vbox_stat" etc), aka "guest stream".
398 */
399class GuestProcessStream
400{
401
402public:
403
404 GuestProcessStream();
405
406 virtual ~GuestProcessStream();
407
408public:
409
410 int AddData(const BYTE *pbData, size_t cbData);
411
412 void Destroy();
413
414#ifdef DEBUG
415 void Dump(const char *pszFile);
416#endif
417
418 uint32_t GetOffset();
419
420 uint32_t GetSize();
421
422 int ParseBlock(GuestProcessStreamBlock &streamBlock);
423
424protected:
425
426 /** Currently allocated size of internal stream buffer. */
427 uint32_t m_cbAllocated;
428 /** Currently used size of allocated internal stream buffer. */
429 uint32_t m_cbSize;
430 /** Current offset within the internal stream buffer. */
431 uint32_t m_cbOffset;
432 /** Internal stream buffer. */
433 BYTE *m_pbBuffer;
434};
435
436class Guest;
437class Progress;
438
439class GuestTask
440{
441
442public:
443
444 enum TaskType
445 {
446 /** Copies a file from host to the guest. */
447 TaskType_CopyFileToGuest = 50,
448 /** Copies a file from guest to the host. */
449 TaskType_CopyFileFromGuest = 55,
450 /** Update Guest Additions by directly copying the required installer
451 * off the .ISO file, transfer it to the guest and execute the installer
452 * with system privileges. */
453 TaskType_UpdateGuestAdditions = 100
454 };
455
456 GuestTask(TaskType aTaskType, Guest *aThat, Progress *aProgress);
457
458 virtual ~GuestTask();
459
460 int startThread();
461
462 static int taskThread(RTTHREAD aThread, void *pvUser);
463 static int uploadProgress(unsigned uPercent, void *pvUser);
464 static HRESULT setProgressSuccess(ComObjPtr<Progress> pProgress);
465 static HRESULT setProgressErrorMsg(HRESULT hr,
466 ComObjPtr<Progress> pProgress, const char * pszText, ...);
467 static HRESULT setProgressErrorParent(HRESULT hr,
468 ComObjPtr<Progress> pProgress, ComObjPtr<Guest> pGuest);
469
470 TaskType taskType;
471 ComObjPtr<Guest> pGuest;
472 ComObjPtr<Progress> pProgress;
473 HRESULT rc;
474
475 /* Task data. */
476 Utf8Str strSource;
477 Utf8Str strDest;
478 Utf8Str strUserName;
479 Utf8Str strPassword;
480 ULONG uFlags;
481};
482#endif // ____H_GUESTIMPLPRIVATE
483
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette