VirtualBox

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

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

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