VirtualBox

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

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