VirtualBox

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

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