VirtualBox

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

Last change on this file since 42538 was 42530, 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.1 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 GuestProcessInfo
308{
309 /** The process' friendly name. */
310 Utf8Str mName;
311 /** The actual command to execute. */
312 Utf8Str mCommand;
313 ProcessArguments mArguments;
314 GuestEnvironment mEnvironment;
315 uint32_t mFlags;
316 ULONG mTimeoutMS;
317 ProcessPriority_T mPriority;
318 ProcessAffinity mAffinity;
319
320};
321
322
323/**
324 * Class representing the "value" side of a "key=value" pair.
325 */
326class GuestProcessStreamValue
327{
328public:
329
330 GuestProcessStreamValue() { }
331 GuestProcessStreamValue(const char *pszValue)
332 : mValue(pszValue) {}
333
334 GuestProcessStreamValue(const GuestProcessStreamValue& aThat)
335 : mValue(aThat.mValue) {}
336
337 Utf8Str mValue;
338};
339
340/** Map containing "key=value" pairs of a guest process stream. */
341typedef std::pair< Utf8Str, GuestProcessStreamValue > GuestCtrlStreamPair;
342typedef std::map < Utf8Str, GuestProcessStreamValue > GuestCtrlStreamPairMap;
343typedef std::map < Utf8Str, GuestProcessStreamValue >::iterator GuestCtrlStreamPairMapIter;
344typedef std::map < Utf8Str, GuestProcessStreamValue >::const_iterator GuestCtrlStreamPairMapIterConst;
345
346/**
347 * Class representing a block of stream pairs (key=value). Each block in a raw guest
348 * output stream is separated by "\0\0", each pair is separated by "\0". The overall
349 * end of a guest stream is marked by "\0\0\0\0".
350 */
351class GuestProcessStreamBlock
352{
353public:
354
355 GuestProcessStreamBlock(void);
356
357 virtual ~GuestProcessStreamBlock(void);
358
359public:
360
361 void Clear();
362
363#ifdef DEBUG
364 void Dump();
365#endif
366
367 int GetInt64Ex(const char *pszKey, int64_t *piVal) const;
368
369 int64_t GetInt64(const char *pszKey) const;
370
371 size_t GetCount(void) const;
372
373 const char* GetString(const char *pszKey) const;
374
375 int GetUInt32Ex(const char *pszKey, uint32_t *puVal) const;
376
377 uint32_t GetUInt32(const char *pszKey) const;
378
379 int SetValue(const char *pszKey, const char *pszValue);
380
381protected:
382
383 GuestCtrlStreamPairMap m_mapPairs;
384};
385
386/** Vector containing multiple allocated stream pair objects. */
387typedef std::vector< GuestProcessStreamBlock > GuestCtrlStreamObjects;
388typedef std::vector< GuestProcessStreamBlock >::iterator GuestCtrlStreamObjectsIter;
389typedef std::vector< GuestProcessStreamBlock >::const_iterator GuestCtrlStreamObjectsIterConst;
390
391/**
392 * Class for parsing machine-readable guest process output by VBoxService'
393 * toolbox commands ("vbox_ls", "vbox_stat" etc), aka "guest stream".
394 */
395class GuestProcessStream
396{
397
398public:
399
400 GuestProcessStream();
401
402 virtual ~GuestProcessStream();
403
404public:
405
406 int AddData(const BYTE *pbData, size_t cbData);
407
408 void Destroy();
409
410#ifdef DEBUG
411 void Dump(const char *pszFile);
412#endif
413
414 uint32_t GetOffset();
415
416 uint32_t GetSize();
417
418 int ParseBlock(GuestProcessStreamBlock &streamBlock);
419
420protected:
421
422 /** Currently allocated size of internal stream buffer. */
423 uint32_t m_cbAllocated;
424 /** Currently used size of allocated internal stream buffer. */
425 uint32_t m_cbSize;
426 /** Current offset within the internal stream buffer. */
427 uint32_t m_cbOffset;
428 /** Internal stream buffer. */
429 BYTE *m_pbBuffer;
430};
431
432class Guest;
433class Progress;
434
435class GuestTask
436{
437
438public:
439
440 enum TaskType
441 {
442 /** Copies a file from host to the guest. */
443 TaskType_CopyFileToGuest = 50,
444 /** Copies a file from guest to the host. */
445 TaskType_CopyFileFromGuest = 55,
446 /** Update Guest Additions by directly copying the required installer
447 * off the .ISO file, transfer it to the guest and execute the installer
448 * with system privileges. */
449 TaskType_UpdateGuestAdditions = 100
450 };
451
452 GuestTask(TaskType aTaskType, Guest *aThat, Progress *aProgress);
453
454 virtual ~GuestTask();
455
456 int startThread();
457
458 static int taskThread(RTTHREAD aThread, void *pvUser);
459 static int uploadProgress(unsigned uPercent, void *pvUser);
460 static HRESULT setProgressSuccess(ComObjPtr<Progress> pProgress);
461 static HRESULT setProgressErrorMsg(HRESULT hr,
462 ComObjPtr<Progress> pProgress, const char * pszText, ...);
463 static HRESULT setProgressErrorParent(HRESULT hr,
464 ComObjPtr<Progress> pProgress, ComObjPtr<Guest> pGuest);
465
466 TaskType taskType;
467 ComObjPtr<Guest> pGuest;
468 ComObjPtr<Progress> pProgress;
469 HRESULT rc;
470
471 /* Task data. */
472 Utf8Str strSource;
473 Utf8Str strDest;
474 Utf8Str strUserName;
475 Utf8Str strPassword;
476 ULONG uFlags;
477};
478#endif // ____H_GUESTIMPLPRIVATE
479
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