1 | /* $Id: GuestCtrlPrivate.cpp 45571 2013-04-16 13:10:30Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | *
|
---|
4 | * Internal helpers/structures for guest control functionality.
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2011-2013 Oracle Corporation
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License (GPL) as published by the Free Software
|
---|
14 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
15 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
16 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | */
|
---|
18 |
|
---|
19 | /******************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | ******************************************************************************/
|
---|
22 | #include "GuestCtrlImplPrivate.h"
|
---|
23 | #include "GuestSessionImpl.h"
|
---|
24 | #include "VMMDev.h"
|
---|
25 |
|
---|
26 | #include <iprt/asm.h>
|
---|
27 | #include <iprt/ctype.h>
|
---|
28 | #ifdef DEBUG
|
---|
29 | # include <iprt/file.h>
|
---|
30 | #endif /* DEBUG */
|
---|
31 |
|
---|
32 | #ifdef LOG_GROUP
|
---|
33 | #undef LOG_GROUP
|
---|
34 | #endif
|
---|
35 | #define LOG_GROUP LOG_GROUP_GUEST_CONTROL
|
---|
36 | #include <VBox/log.h>
|
---|
37 |
|
---|
38 | /******************************************************************************
|
---|
39 | * Structures and Typedefs *
|
---|
40 | ******************************************************************************/
|
---|
41 |
|
---|
42 | int GuestEnvironment::BuildEnvironmentBlock(void **ppvEnv, size_t *pcbEnv, uint32_t *pcEnvVars)
|
---|
43 | {
|
---|
44 | AssertPtrReturn(ppvEnv, VERR_INVALID_POINTER);
|
---|
45 | /* Rest is optional. */
|
---|
46 |
|
---|
47 | size_t cbEnv = 0;
|
---|
48 | uint32_t cEnvVars = 0;
|
---|
49 |
|
---|
50 | int rc = VINF_SUCCESS;
|
---|
51 |
|
---|
52 | size_t cEnv = mEnvironment.size();
|
---|
53 | if (cEnv)
|
---|
54 | {
|
---|
55 | std::map<Utf8Str, Utf8Str>::const_iterator itEnv = mEnvironment.begin();
|
---|
56 | for (; itEnv != mEnvironment.end() && RT_SUCCESS(rc); itEnv++)
|
---|
57 | {
|
---|
58 | char *pszEnv;
|
---|
59 | if (!RTStrAPrintf(&pszEnv, "%s=%s", itEnv->first.c_str(), itEnv->second.c_str()))
|
---|
60 | {
|
---|
61 | rc = VERR_NO_MEMORY;
|
---|
62 | break;
|
---|
63 | }
|
---|
64 | AssertPtr(pszEnv);
|
---|
65 | rc = appendToEnvBlock(pszEnv, ppvEnv, &cbEnv, &cEnvVars);
|
---|
66 | RTStrFree(pszEnv);
|
---|
67 | }
|
---|
68 | Assert(cEnv == cEnvVars);
|
---|
69 | }
|
---|
70 |
|
---|
71 | if (pcbEnv)
|
---|
72 | *pcbEnv = cbEnv;
|
---|
73 | if (pcEnvVars)
|
---|
74 | *pcEnvVars = cEnvVars;
|
---|
75 |
|
---|
76 | return rc;
|
---|
77 | }
|
---|
78 |
|
---|
79 | void GuestEnvironment::Clear(void)
|
---|
80 | {
|
---|
81 | mEnvironment.clear();
|
---|
82 | }
|
---|
83 |
|
---|
84 | int GuestEnvironment::CopyFrom(const GuestEnvironmentArray &environment)
|
---|
85 | {
|
---|
86 | int rc = VINF_SUCCESS;
|
---|
87 |
|
---|
88 | for (GuestEnvironmentArray::const_iterator it = environment.begin();
|
---|
89 | it != environment.end() && RT_SUCCESS(rc);
|
---|
90 | ++it)
|
---|
91 | {
|
---|
92 | rc = Set((*it));
|
---|
93 | }
|
---|
94 |
|
---|
95 | return rc;
|
---|
96 | }
|
---|
97 |
|
---|
98 | int GuestEnvironment::CopyTo(GuestEnvironmentArray &environment)
|
---|
99 | {
|
---|
100 | size_t s = 0;
|
---|
101 | for (std::map<Utf8Str, Utf8Str>::const_iterator it = mEnvironment.begin();
|
---|
102 | it != mEnvironment.end();
|
---|
103 | ++it, ++s)
|
---|
104 | {
|
---|
105 | environment[s] = Bstr(it->first + "=" + it->second).raw();
|
---|
106 | }
|
---|
107 |
|
---|
108 | return VINF_SUCCESS;
|
---|
109 | }
|
---|
110 |
|
---|
111 | /* static */
|
---|
112 | void GuestEnvironment::FreeEnvironmentBlock(void *pvEnv)
|
---|
113 | {
|
---|
114 | if (pvEnv)
|
---|
115 | RTMemFree(pvEnv);
|
---|
116 | }
|
---|
117 |
|
---|
118 | Utf8Str GuestEnvironment::Get(size_t nPos)
|
---|
119 | {
|
---|
120 | size_t curPos = 0;
|
---|
121 | std::map<Utf8Str, Utf8Str>::const_iterator it = mEnvironment.begin();
|
---|
122 | for (; it != mEnvironment.end() && curPos < nPos;
|
---|
123 | ++it, ++curPos) { }
|
---|
124 |
|
---|
125 | if (it != mEnvironment.end())
|
---|
126 | return Utf8Str(it->first + "=" + it->second);
|
---|
127 |
|
---|
128 | return Utf8Str("");
|
---|
129 | }
|
---|
130 |
|
---|
131 | Utf8Str GuestEnvironment::Get(const Utf8Str &strKey)
|
---|
132 | {
|
---|
133 | std::map <Utf8Str, Utf8Str>::const_iterator itEnv = mEnvironment.find(strKey);
|
---|
134 | Utf8Str strRet;
|
---|
135 | if (itEnv != mEnvironment.end())
|
---|
136 | strRet = itEnv->second;
|
---|
137 | return strRet;
|
---|
138 | }
|
---|
139 |
|
---|
140 | bool GuestEnvironment::Has(const Utf8Str &strKey)
|
---|
141 | {
|
---|
142 | std::map <Utf8Str, Utf8Str>::const_iterator itEnv = mEnvironment.find(strKey);
|
---|
143 | return (itEnv != mEnvironment.end());
|
---|
144 | }
|
---|
145 |
|
---|
146 | int GuestEnvironment::Set(const Utf8Str &strKey, const Utf8Str &strValue)
|
---|
147 | {
|
---|
148 | /** @todo Do some validation using regex. */
|
---|
149 | if (strKey.isEmpty())
|
---|
150 | return VERR_INVALID_PARAMETER;
|
---|
151 |
|
---|
152 | int rc = VINF_SUCCESS;
|
---|
153 | const char *pszString = strKey.c_str();
|
---|
154 | while (*pszString != '\0' && RT_SUCCESS(rc))
|
---|
155 | {
|
---|
156 | if ( !RT_C_IS_ALNUM(*pszString)
|
---|
157 | && !RT_C_IS_GRAPH(*pszString))
|
---|
158 | rc = VERR_INVALID_PARAMETER;
|
---|
159 | *pszString++;
|
---|
160 | }
|
---|
161 |
|
---|
162 | if (RT_SUCCESS(rc))
|
---|
163 | mEnvironment[strKey] = strValue;
|
---|
164 |
|
---|
165 | return rc;
|
---|
166 | }
|
---|
167 |
|
---|
168 | int GuestEnvironment::Set(const Utf8Str &strPair)
|
---|
169 | {
|
---|
170 | RTCList<RTCString> listPair = strPair.split("=", RTCString::KeepEmptyParts);
|
---|
171 | /* Skip completely empty pairs. Note that we still need pairs with a valid
|
---|
172 | * (set) key and an empty value. */
|
---|
173 | if (listPair.size() <= 1)
|
---|
174 | return VINF_SUCCESS;
|
---|
175 |
|
---|
176 | int rc = VINF_SUCCESS;
|
---|
177 | size_t p = 0;
|
---|
178 | while (p < listPair.size() && RT_SUCCESS(rc))
|
---|
179 | {
|
---|
180 | Utf8Str strKey = listPair.at(p++);
|
---|
181 | if ( strKey.isEmpty()
|
---|
182 | || strKey.equals("=")) /* Skip pairs with empty keys (e.g. "=FOO"). */
|
---|
183 | {
|
---|
184 | break;
|
---|
185 | }
|
---|
186 | Utf8Str strValue;
|
---|
187 | if (p < listPair.size()) /* Does the list also contain a value? */
|
---|
188 | strValue = listPair.at(p++);
|
---|
189 |
|
---|
190 | #ifdef DEBUG
|
---|
191 | LogFlowFunc(("strKey=%s, strValue=%s\n",
|
---|
192 | strKey.c_str(), strValue.c_str()));
|
---|
193 | #endif
|
---|
194 | rc = Set(strKey, strValue);
|
---|
195 | }
|
---|
196 |
|
---|
197 | return rc;
|
---|
198 | }
|
---|
199 |
|
---|
200 | size_t GuestEnvironment::Size(void)
|
---|
201 | {
|
---|
202 | return mEnvironment.size();
|
---|
203 | }
|
---|
204 |
|
---|
205 | int GuestEnvironment::Unset(const Utf8Str &strKey)
|
---|
206 | {
|
---|
207 | std::map <Utf8Str, Utf8Str>::iterator itEnv = mEnvironment.find(strKey);
|
---|
208 | if (itEnv != mEnvironment.end())
|
---|
209 | {
|
---|
210 | mEnvironment.erase(itEnv);
|
---|
211 | return VINF_SUCCESS;
|
---|
212 | }
|
---|
213 |
|
---|
214 | return VERR_NOT_FOUND;
|
---|
215 | }
|
---|
216 |
|
---|
217 | GuestEnvironment& GuestEnvironment::operator=(const GuestEnvironmentArray &that)
|
---|
218 | {
|
---|
219 | CopyFrom(that);
|
---|
220 | return *this;
|
---|
221 | }
|
---|
222 |
|
---|
223 | GuestEnvironment& GuestEnvironment::operator=(const GuestEnvironment &that)
|
---|
224 | {
|
---|
225 | for (std::map<Utf8Str, Utf8Str>::const_iterator it = that.mEnvironment.begin();
|
---|
226 | it != that.mEnvironment.end();
|
---|
227 | ++it)
|
---|
228 | {
|
---|
229 | mEnvironment[it->first] = it->second;
|
---|
230 | }
|
---|
231 |
|
---|
232 | return *this;
|
---|
233 | }
|
---|
234 |
|
---|
235 | /**
|
---|
236 | * Appends environment variables to the environment block.
|
---|
237 | *
|
---|
238 | * Each var=value pair is separated by the null character ('\\0'). The whole
|
---|
239 | * block will be stored in one blob and disassembled on the guest side later to
|
---|
240 | * fit into the HGCM param structure.
|
---|
241 | *
|
---|
242 | * @returns VBox status code.
|
---|
243 | *
|
---|
244 | * @param pszEnvVar The environment variable=value to append to the
|
---|
245 | * environment block.
|
---|
246 | * @param ppvList This is actually a pointer to a char pointer
|
---|
247 | * variable which keeps track of the environment block
|
---|
248 | * that we're constructing.
|
---|
249 | * @param pcbList Pointer to the variable holding the current size of
|
---|
250 | * the environment block. (List is a misnomer, go
|
---|
251 | * ahead a be confused.)
|
---|
252 | * @param pcEnvVars Pointer to the variable holding count of variables
|
---|
253 | * stored in the environment block.
|
---|
254 | */
|
---|
255 | int GuestEnvironment::appendToEnvBlock(const char *pszEnv, void **ppvList, size_t *pcbList, uint32_t *pcEnvVars)
|
---|
256 | {
|
---|
257 | int rc = VINF_SUCCESS;
|
---|
258 | size_t cchEnv = strlen(pszEnv); Assert(cchEnv >= 2);
|
---|
259 | if (*ppvList)
|
---|
260 | {
|
---|
261 | size_t cbNewLen = *pcbList + cchEnv + 1; /* Include zero termination. */
|
---|
262 | char *pvTmp = (char *)RTMemRealloc(*ppvList, cbNewLen);
|
---|
263 | if (pvTmp == NULL)
|
---|
264 | rc = VERR_NO_MEMORY;
|
---|
265 | else
|
---|
266 | {
|
---|
267 | memcpy(pvTmp + *pcbList, pszEnv, cchEnv);
|
---|
268 | pvTmp[cbNewLen - 1] = '\0'; /* Add zero termination. */
|
---|
269 | *ppvList = (void **)pvTmp;
|
---|
270 | }
|
---|
271 | }
|
---|
272 | else
|
---|
273 | {
|
---|
274 | char *pszTmp;
|
---|
275 | if (RTStrAPrintf(&pszTmp, "%s", pszEnv) >= 0)
|
---|
276 | {
|
---|
277 | *ppvList = (void **)pszTmp;
|
---|
278 | /* Reset counters. */
|
---|
279 | *pcEnvVars = 0;
|
---|
280 | *pcbList = 0;
|
---|
281 | }
|
---|
282 | }
|
---|
283 | if (RT_SUCCESS(rc))
|
---|
284 | {
|
---|
285 | *pcbList += cchEnv + 1; /* Include zero termination. */
|
---|
286 | *pcEnvVars += 1; /* Increase env variable count. */
|
---|
287 | }
|
---|
288 | return rc;
|
---|
289 | }
|
---|
290 |
|
---|
291 | int GuestFsObjData::FromLs(const GuestProcessStreamBlock &strmBlk)
|
---|
292 | {
|
---|
293 | LogFlowFunc(("\n"));
|
---|
294 |
|
---|
295 | int rc = VINF_SUCCESS;
|
---|
296 |
|
---|
297 | try
|
---|
298 | {
|
---|
299 | #ifdef DEBUG
|
---|
300 | strmBlk.DumpToLog();
|
---|
301 | #endif
|
---|
302 | /* Object name. */
|
---|
303 | mName = strmBlk.GetString("name");
|
---|
304 | if (mName.isEmpty()) throw VERR_NOT_FOUND;
|
---|
305 | /* Type. */
|
---|
306 | Utf8Str strType(strmBlk.GetString("ftype"));
|
---|
307 | if (strType.equalsIgnoreCase("-"))
|
---|
308 | mType = FsObjType_File;
|
---|
309 | else if (strType.equalsIgnoreCase("d"))
|
---|
310 | mType = FsObjType_Directory;
|
---|
311 | /** @todo Add more types! */
|
---|
312 | else
|
---|
313 | mType = FsObjType_Undefined;
|
---|
314 | /* Object size. */
|
---|
315 | rc = strmBlk.GetInt64Ex("st_size", &mObjectSize);
|
---|
316 | if (RT_FAILURE(rc)) throw rc;
|
---|
317 | /** @todo Add complete ls info! */
|
---|
318 | }
|
---|
319 | catch (int rc2)
|
---|
320 | {
|
---|
321 | rc = rc2;
|
---|
322 | }
|
---|
323 |
|
---|
324 | LogFlowFuncLeaveRC(rc);
|
---|
325 | return rc;
|
---|
326 | }
|
---|
327 |
|
---|
328 | int GuestFsObjData::FromStat(const GuestProcessStreamBlock &strmBlk)
|
---|
329 | {
|
---|
330 | LogFlowFunc(("\n"));
|
---|
331 |
|
---|
332 | int rc = VINF_SUCCESS;
|
---|
333 |
|
---|
334 | try
|
---|
335 | {
|
---|
336 | #ifdef DEBUG
|
---|
337 | strmBlk.DumpToLog();
|
---|
338 | #endif
|
---|
339 | /* Node ID, optional because we don't include this
|
---|
340 | * in older VBoxService (< 4.2) versions. */
|
---|
341 | mNodeID = strmBlk.GetInt64("node_id");
|
---|
342 | /* Object name. */
|
---|
343 | mName = strmBlk.GetString("name");
|
---|
344 | if (mName.isEmpty()) throw VERR_NOT_FOUND;
|
---|
345 | /* Type. */
|
---|
346 | Utf8Str strType(strmBlk.GetString("ftype"));
|
---|
347 | if (strType.equalsIgnoreCase("-"))
|
---|
348 | mType = FsObjType_File;
|
---|
349 | else if (strType.equalsIgnoreCase("d"))
|
---|
350 | mType = FsObjType_Directory;
|
---|
351 | /** @todo Add more types! */
|
---|
352 | else
|
---|
353 | mType = FsObjType_Undefined;
|
---|
354 | /* Object size. */
|
---|
355 | rc = strmBlk.GetInt64Ex("st_size", &mObjectSize);
|
---|
356 | if (RT_FAILURE(rc)) throw rc;
|
---|
357 | /** @todo Add complete stat info! */
|
---|
358 | }
|
---|
359 | catch (int rc2)
|
---|
360 | {
|
---|
361 | rc = rc2;
|
---|
362 | }
|
---|
363 |
|
---|
364 | LogFlowFuncLeaveRC(rc);
|
---|
365 | return rc;
|
---|
366 | }
|
---|
367 |
|
---|
368 | ///////////////////////////////////////////////////////////////////////////////
|
---|
369 |
|
---|
370 | /** @todo *NOT* thread safe yet! */
|
---|
371 | /** @todo Add exception handling for STL stuff! */
|
---|
372 |
|
---|
373 | GuestProcessStreamBlock::GuestProcessStreamBlock(void)
|
---|
374 | {
|
---|
375 |
|
---|
376 | }
|
---|
377 |
|
---|
378 | /*
|
---|
379 | GuestProcessStreamBlock::GuestProcessStreamBlock(const GuestProcessStreamBlock &otherBlock)
|
---|
380 | {
|
---|
381 | for (GuestCtrlStreamPairsIter it = otherBlock.mPairs.begin();
|
---|
382 | it != otherBlock.end(); it++)
|
---|
383 | {
|
---|
384 | mPairs[it->first] = new
|
---|
385 | if (it->second.pszValue)
|
---|
386 | {
|
---|
387 | RTMemFree(it->second.pszValue);
|
---|
388 | it->second.pszValue = NULL;
|
---|
389 | }
|
---|
390 | }
|
---|
391 | }*/
|
---|
392 |
|
---|
393 | GuestProcessStreamBlock::~GuestProcessStreamBlock()
|
---|
394 | {
|
---|
395 | Clear();
|
---|
396 | }
|
---|
397 |
|
---|
398 | /**
|
---|
399 | * Destroys the currently stored stream pairs.
|
---|
400 | *
|
---|
401 | * @return IPRT status code.
|
---|
402 | */
|
---|
403 | void GuestProcessStreamBlock::Clear(void)
|
---|
404 | {
|
---|
405 | mPairs.clear();
|
---|
406 | }
|
---|
407 |
|
---|
408 | #ifdef DEBUG
|
---|
409 | void GuestProcessStreamBlock::DumpToLog(void) const
|
---|
410 | {
|
---|
411 | LogFlowFunc(("Dumping contents of stream block=0x%p (%ld items):\n",
|
---|
412 | this, mPairs.size()));
|
---|
413 |
|
---|
414 | for (GuestCtrlStreamPairMapIterConst it = mPairs.begin();
|
---|
415 | it != mPairs.end(); it++)
|
---|
416 | {
|
---|
417 | LogFlowFunc(("\t%s=%s\n", it->first.c_str(), it->second.mValue.c_str()));
|
---|
418 | }
|
---|
419 | }
|
---|
420 | #endif
|
---|
421 |
|
---|
422 | /**
|
---|
423 | * Returns a 64-bit signed integer of a specified key.
|
---|
424 | *
|
---|
425 | * @return IPRT status code. VERR_NOT_FOUND if key was not found.
|
---|
426 | * @param pszKey Name of key to get the value for.
|
---|
427 | * @param piVal Pointer to value to return.
|
---|
428 | */
|
---|
429 | int GuestProcessStreamBlock::GetInt64Ex(const char *pszKey, int64_t *piVal) const
|
---|
430 | {
|
---|
431 | AssertPtrReturn(pszKey, VERR_INVALID_POINTER);
|
---|
432 | AssertPtrReturn(piVal, VERR_INVALID_POINTER);
|
---|
433 | const char *pszValue = GetString(pszKey);
|
---|
434 | if (pszValue)
|
---|
435 | {
|
---|
436 | *piVal = RTStrToInt64(pszValue);
|
---|
437 | return VINF_SUCCESS;
|
---|
438 | }
|
---|
439 | return VERR_NOT_FOUND;
|
---|
440 | }
|
---|
441 |
|
---|
442 | /**
|
---|
443 | * Returns a 64-bit integer of a specified key.
|
---|
444 | *
|
---|
445 | * @return int64_t Value to return, 0 if not found / on failure.
|
---|
446 | * @param pszKey Name of key to get the value for.
|
---|
447 | */
|
---|
448 | int64_t GuestProcessStreamBlock::GetInt64(const char *pszKey) const
|
---|
449 | {
|
---|
450 | int64_t iVal;
|
---|
451 | if (RT_SUCCESS(GetInt64Ex(pszKey, &iVal)))
|
---|
452 | return iVal;
|
---|
453 | return 0;
|
---|
454 | }
|
---|
455 |
|
---|
456 | /**
|
---|
457 | * Returns the current number of stream pairs.
|
---|
458 | *
|
---|
459 | * @return uint32_t Current number of stream pairs.
|
---|
460 | */
|
---|
461 | size_t GuestProcessStreamBlock::GetCount(void) const
|
---|
462 | {
|
---|
463 | return mPairs.size();
|
---|
464 | }
|
---|
465 |
|
---|
466 | /**
|
---|
467 | * Returns a string value of a specified key.
|
---|
468 | *
|
---|
469 | * @return uint32_t Pointer to string to return, NULL if not found / on failure.
|
---|
470 | * @param pszKey Name of key to get the value for.
|
---|
471 | */
|
---|
472 | const char* GuestProcessStreamBlock::GetString(const char *pszKey) const
|
---|
473 | {
|
---|
474 | AssertPtrReturn(pszKey, NULL);
|
---|
475 |
|
---|
476 | try
|
---|
477 | {
|
---|
478 | GuestCtrlStreamPairMapIterConst itPairs = mPairs.find(Utf8Str(pszKey));
|
---|
479 | if (itPairs != mPairs.end())
|
---|
480 | return itPairs->second.mValue.c_str();
|
---|
481 | }
|
---|
482 | catch (const std::exception &ex)
|
---|
483 | {
|
---|
484 | NOREF(ex);
|
---|
485 | }
|
---|
486 | return NULL;
|
---|
487 | }
|
---|
488 |
|
---|
489 | /**
|
---|
490 | * Returns a 32-bit unsigned integer of a specified key.
|
---|
491 | *
|
---|
492 | * @return IPRT status code. VERR_NOT_FOUND if key was not found.
|
---|
493 | * @param pszKey Name of key to get the value for.
|
---|
494 | * @param puVal Pointer to value to return.
|
---|
495 | */
|
---|
496 | int GuestProcessStreamBlock::GetUInt32Ex(const char *pszKey, uint32_t *puVal) const
|
---|
497 | {
|
---|
498 | AssertPtrReturn(pszKey, VERR_INVALID_POINTER);
|
---|
499 | AssertPtrReturn(puVal, VERR_INVALID_POINTER);
|
---|
500 | const char *pszValue = GetString(pszKey);
|
---|
501 | if (pszValue)
|
---|
502 | {
|
---|
503 | *puVal = RTStrToUInt32(pszValue);
|
---|
504 | return VINF_SUCCESS;
|
---|
505 | }
|
---|
506 | return VERR_NOT_FOUND;
|
---|
507 | }
|
---|
508 |
|
---|
509 | /**
|
---|
510 | * Returns a 32-bit unsigned integer of a specified key.
|
---|
511 | *
|
---|
512 | * @return uint32_t Value to return, 0 if not found / on failure.
|
---|
513 | * @param pszKey Name of key to get the value for.
|
---|
514 | */
|
---|
515 | uint32_t GuestProcessStreamBlock::GetUInt32(const char *pszKey) const
|
---|
516 | {
|
---|
517 | uint32_t uVal;
|
---|
518 | if (RT_SUCCESS(GetUInt32Ex(pszKey, &uVal)))
|
---|
519 | return uVal;
|
---|
520 | return 0;
|
---|
521 | }
|
---|
522 |
|
---|
523 | /**
|
---|
524 | * Sets a value to a key or deletes a key by setting a NULL value.
|
---|
525 | *
|
---|
526 | * @return IPRT status code.
|
---|
527 | * @param pszKey Key name to process.
|
---|
528 | * @param pszValue Value to set. Set NULL for deleting the key.
|
---|
529 | */
|
---|
530 | int GuestProcessStreamBlock::SetValue(const char *pszKey, const char *pszValue)
|
---|
531 | {
|
---|
532 | AssertPtrReturn(pszKey, VERR_INVALID_POINTER);
|
---|
533 |
|
---|
534 | int rc = VINF_SUCCESS;
|
---|
535 | try
|
---|
536 | {
|
---|
537 | Utf8Str Utf8Key(pszKey);
|
---|
538 |
|
---|
539 | /* Take a shortcut and prevent crashes on some funny versions
|
---|
540 | * of STL if map is empty initially. */
|
---|
541 | if (!mPairs.empty())
|
---|
542 | {
|
---|
543 | GuestCtrlStreamPairMapIter it = mPairs.find(Utf8Key);
|
---|
544 | if (it != mPairs.end())
|
---|
545 | mPairs.erase(it);
|
---|
546 | }
|
---|
547 |
|
---|
548 | if (pszValue)
|
---|
549 | {
|
---|
550 | GuestProcessStreamValue val(pszValue);
|
---|
551 | mPairs[Utf8Key] = val;
|
---|
552 | }
|
---|
553 | }
|
---|
554 | catch (const std::exception &ex)
|
---|
555 | {
|
---|
556 | NOREF(ex);
|
---|
557 | }
|
---|
558 | return rc;
|
---|
559 | }
|
---|
560 |
|
---|
561 | ///////////////////////////////////////////////////////////////////////////////
|
---|
562 |
|
---|
563 | GuestProcessStream::GuestProcessStream(void)
|
---|
564 | : m_cbAllocated(0),
|
---|
565 | m_cbSize(0),
|
---|
566 | m_cbOffset(0),
|
---|
567 | m_pbBuffer(NULL)
|
---|
568 | {
|
---|
569 |
|
---|
570 | }
|
---|
571 |
|
---|
572 | GuestProcessStream::~GuestProcessStream(void)
|
---|
573 | {
|
---|
574 | Destroy();
|
---|
575 | }
|
---|
576 |
|
---|
577 | /**
|
---|
578 | * Adds data to the internal parser buffer. Useful if there
|
---|
579 | * are multiple rounds of adding data needed.
|
---|
580 | *
|
---|
581 | * @return IPRT status code.
|
---|
582 | * @param pbData Pointer to data to add.
|
---|
583 | * @param cbData Size (in bytes) of data to add.
|
---|
584 | */
|
---|
585 | int GuestProcessStream::AddData(const BYTE *pbData, size_t cbData)
|
---|
586 | {
|
---|
587 | AssertPtrReturn(pbData, VERR_INVALID_POINTER);
|
---|
588 | AssertReturn(cbData, VERR_INVALID_PARAMETER);
|
---|
589 |
|
---|
590 | int rc = VINF_SUCCESS;
|
---|
591 |
|
---|
592 | /* Rewind the buffer if it's empty. */
|
---|
593 | size_t cbInBuf = m_cbSize - m_cbOffset;
|
---|
594 | bool const fAddToSet = cbInBuf == 0;
|
---|
595 | if (fAddToSet)
|
---|
596 | m_cbSize = m_cbOffset = 0;
|
---|
597 |
|
---|
598 | /* Try and see if we can simply append the data. */
|
---|
599 | if (cbData + m_cbSize <= m_cbAllocated)
|
---|
600 | {
|
---|
601 | memcpy(&m_pbBuffer[m_cbSize], pbData, cbData);
|
---|
602 | m_cbSize += cbData;
|
---|
603 | }
|
---|
604 | else
|
---|
605 | {
|
---|
606 | /* Move any buffered data to the front. */
|
---|
607 | cbInBuf = m_cbSize - m_cbOffset;
|
---|
608 | if (cbInBuf == 0)
|
---|
609 | m_cbSize = m_cbOffset = 0;
|
---|
610 | else if (m_cbOffset) /* Do we have something to move? */
|
---|
611 | {
|
---|
612 | memmove(m_pbBuffer, &m_pbBuffer[m_cbOffset], cbInBuf);
|
---|
613 | m_cbSize = cbInBuf;
|
---|
614 | m_cbOffset = 0;
|
---|
615 | }
|
---|
616 |
|
---|
617 | /* Do we need to grow the buffer? */
|
---|
618 | if (cbData + m_cbSize > m_cbAllocated)
|
---|
619 | {
|
---|
620 | size_t cbAlloc = m_cbSize + cbData;
|
---|
621 | cbAlloc = RT_ALIGN_Z(cbAlloc, _64K);
|
---|
622 | void *pvNew = RTMemRealloc(m_pbBuffer, cbAlloc);
|
---|
623 | if (pvNew)
|
---|
624 | {
|
---|
625 | m_pbBuffer = (uint8_t *)pvNew;
|
---|
626 | m_cbAllocated = cbAlloc;
|
---|
627 | }
|
---|
628 | else
|
---|
629 | rc = VERR_NO_MEMORY;
|
---|
630 | }
|
---|
631 |
|
---|
632 | /* Finally, copy the data. */
|
---|
633 | if (RT_SUCCESS(rc))
|
---|
634 | {
|
---|
635 | if (cbData + m_cbSize <= m_cbAllocated)
|
---|
636 | {
|
---|
637 | memcpy(&m_pbBuffer[m_cbSize], pbData, cbData);
|
---|
638 | m_cbSize += cbData;
|
---|
639 | }
|
---|
640 | else
|
---|
641 | rc = VERR_BUFFER_OVERFLOW;
|
---|
642 | }
|
---|
643 | }
|
---|
644 |
|
---|
645 | return rc;
|
---|
646 | }
|
---|
647 |
|
---|
648 | /**
|
---|
649 | * Destroys the internal data buffer.
|
---|
650 | */
|
---|
651 | void GuestProcessStream::Destroy(void)
|
---|
652 | {
|
---|
653 | if (m_pbBuffer)
|
---|
654 | {
|
---|
655 | RTMemFree(m_pbBuffer);
|
---|
656 | m_pbBuffer = NULL;
|
---|
657 | }
|
---|
658 |
|
---|
659 | m_cbAllocated = 0;
|
---|
660 | m_cbSize = 0;
|
---|
661 | m_cbOffset = 0;
|
---|
662 | }
|
---|
663 |
|
---|
664 | #ifdef DEBUG
|
---|
665 | void GuestProcessStream::Dump(const char *pszFile)
|
---|
666 | {
|
---|
667 | LogFlowFunc(("Dumping contents of stream=0x%p (cbAlloc=%u, cbSize=%u, cbOff=%u) to %s\n",
|
---|
668 | m_pbBuffer, m_cbAllocated, m_cbSize, m_cbOffset, pszFile));
|
---|
669 |
|
---|
670 | RTFILE hFile;
|
---|
671 | int rc = RTFileOpen(&hFile, pszFile, RTFILE_O_CREATE_REPLACE | RTFILE_O_WRITE | RTFILE_O_DENY_WRITE);
|
---|
672 | if (RT_SUCCESS(rc))
|
---|
673 | {
|
---|
674 | rc = RTFileWrite(hFile, m_pbBuffer, m_cbSize, NULL /* pcbWritten */);
|
---|
675 | RTFileClose(hFile);
|
---|
676 | }
|
---|
677 | }
|
---|
678 | #endif
|
---|
679 |
|
---|
680 | /**
|
---|
681 | * Returns the current offset of the parser within
|
---|
682 | * the internal data buffer.
|
---|
683 | *
|
---|
684 | * @return uint32_t Parser offset.
|
---|
685 | */
|
---|
686 | uint32_t GuestProcessStream::GetOffset()
|
---|
687 | {
|
---|
688 | return m_cbOffset;
|
---|
689 | }
|
---|
690 |
|
---|
691 | uint32_t GuestProcessStream::GetSize()
|
---|
692 | {
|
---|
693 | return m_cbSize;
|
---|
694 | }
|
---|
695 |
|
---|
696 | /**
|
---|
697 | * Tries to parse the next upcoming pair block within the internal
|
---|
698 | * buffer.
|
---|
699 | *
|
---|
700 | * Returns VERR_NO_DATA is no data is in internal buffer or buffer has been
|
---|
701 | * completely parsed already.
|
---|
702 | *
|
---|
703 | * Returns VERR_MORE_DATA if current block was parsed (with zero or more pairs
|
---|
704 | * stored in stream block) but still contains incomplete (unterminated)
|
---|
705 | * data.
|
---|
706 | *
|
---|
707 | * Returns VINF_SUCCESS if current block was parsed until the next upcoming
|
---|
708 | * block (with zero or more pairs stored in stream block).
|
---|
709 | *
|
---|
710 | * @return IPRT status code.
|
---|
711 | * @param streamBlock Reference to guest stream block to fill.
|
---|
712 | *
|
---|
713 | */
|
---|
714 | int GuestProcessStream::ParseBlock(GuestProcessStreamBlock &streamBlock)
|
---|
715 | {
|
---|
716 | if ( !m_pbBuffer
|
---|
717 | || !m_cbSize)
|
---|
718 | {
|
---|
719 | return VERR_NO_DATA;
|
---|
720 | }
|
---|
721 |
|
---|
722 | AssertReturn(m_cbOffset <= m_cbSize, VERR_INVALID_PARAMETER);
|
---|
723 | if (m_cbOffset == m_cbSize)
|
---|
724 | return VERR_NO_DATA;
|
---|
725 |
|
---|
726 | int rc = VINF_SUCCESS;
|
---|
727 |
|
---|
728 | char *pszOff = (char*)&m_pbBuffer[m_cbOffset];
|
---|
729 | char *pszStart = pszOff;
|
---|
730 | uint32_t uDistance;
|
---|
731 | while (*pszStart)
|
---|
732 | {
|
---|
733 | size_t pairLen = strlen(pszStart);
|
---|
734 | uDistance = (pszStart - pszOff);
|
---|
735 | if (m_cbOffset + uDistance + pairLen + 1 >= m_cbSize)
|
---|
736 | {
|
---|
737 | rc = VERR_MORE_DATA;
|
---|
738 | break;
|
---|
739 | }
|
---|
740 | else
|
---|
741 | {
|
---|
742 | char *pszSep = strchr(pszStart, '=');
|
---|
743 | char *pszVal = NULL;
|
---|
744 | if (pszSep)
|
---|
745 | pszVal = pszSep + 1;
|
---|
746 | if (!pszSep || !pszVal)
|
---|
747 | {
|
---|
748 | rc = VERR_MORE_DATA;
|
---|
749 | break;
|
---|
750 | }
|
---|
751 |
|
---|
752 | /* Terminate the separator so that we can
|
---|
753 | * use pszStart as our key from now on. */
|
---|
754 | *pszSep = '\0';
|
---|
755 |
|
---|
756 | rc = streamBlock.SetValue(pszStart, pszVal);
|
---|
757 | if (RT_FAILURE(rc))
|
---|
758 | return rc;
|
---|
759 | }
|
---|
760 |
|
---|
761 | /* Next pair. */
|
---|
762 | pszStart += pairLen + 1;
|
---|
763 | }
|
---|
764 |
|
---|
765 | /* If we did not do any movement but we have stuff left
|
---|
766 | * in our buffer just skip the current termination so that
|
---|
767 | * we can try next time. */
|
---|
768 | uDistance = (pszStart - pszOff);
|
---|
769 | if ( !uDistance
|
---|
770 | && *pszStart == '\0'
|
---|
771 | && m_cbOffset < m_cbSize)
|
---|
772 | {
|
---|
773 | uDistance++;
|
---|
774 | }
|
---|
775 | m_cbOffset += uDistance;
|
---|
776 |
|
---|
777 | return rc;
|
---|
778 | }
|
---|
779 |
|
---|
780 | GuestBase::GuestBase(void)
|
---|
781 | : mConsole(NULL),
|
---|
782 | mNextContextID(0)
|
---|
783 | {
|
---|
784 | }
|
---|
785 |
|
---|
786 | GuestBase::~GuestBase(void)
|
---|
787 | {
|
---|
788 | }
|
---|
789 |
|
---|
790 | int GuestBase::generateContextID(uint32_t uSessionID, uint32_t uObjectID, uint32_t *puContextID)
|
---|
791 | {
|
---|
792 | AssertPtrReturn(puContextID, VERR_INVALID_POINTER);
|
---|
793 |
|
---|
794 | uint32_t uCount = mNextContextID++;
|
---|
795 | if (uCount == VBOX_GUESTCTRL_MAX_CONTEXTS)
|
---|
796 | uCount = 0;
|
---|
797 |
|
---|
798 | uint32_t uNewContextID =
|
---|
799 | VBOX_GUESTCTRL_CONTEXTID_MAKE(uSessionID, uObjectID, uCount);
|
---|
800 |
|
---|
801 | *puContextID = uNewContextID;
|
---|
802 |
|
---|
803 | return VINF_SUCCESS;
|
---|
804 | }
|
---|
805 |
|
---|
806 | GuestObject::GuestObject(void)
|
---|
807 | : mSession(NULL),
|
---|
808 | mObjectID(0)
|
---|
809 | {
|
---|
810 | }
|
---|
811 |
|
---|
812 | GuestObject::~GuestObject(void)
|
---|
813 | {
|
---|
814 | }
|
---|
815 |
|
---|
816 | int GuestObject::bindToSession(Console *pConsole, GuestSession *pSession, uint32_t uObjectID)
|
---|
817 | {
|
---|
818 | AssertPtrReturn(pConsole, VERR_INVALID_POINTER);
|
---|
819 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
820 |
|
---|
821 | mConsole = pConsole;
|
---|
822 | mSession = pSession;
|
---|
823 | mObjectID = uObjectID;
|
---|
824 |
|
---|
825 | return VINF_SUCCESS;
|
---|
826 | }
|
---|
827 |
|
---|
828 | int GuestObject::sendCommand(uint32_t uFunction,
|
---|
829 | uint32_t uParms, PVBOXHGCMSVCPARM paParms)
|
---|
830 | {
|
---|
831 | LogFlowThisFuncEnter();
|
---|
832 |
|
---|
833 | #ifndef VBOX_GUESTCTRL_TEST_CASE
|
---|
834 | ComObjPtr<Console> pConsole = mConsole;
|
---|
835 | Assert(!pConsole.isNull());
|
---|
836 |
|
---|
837 | /* Forward the information to the VMM device. */
|
---|
838 | VMMDev *pVMMDev = pConsole->getVMMDev();
|
---|
839 | AssertPtr(pVMMDev);
|
---|
840 |
|
---|
841 | LogFlowThisFunc(("uFunction=%RU32, uParms=%RU32\n", uFunction, uParms));
|
---|
842 | int vrc = pVMMDev->hgcmHostCall(HGCMSERVICE_NAME, uFunction, uParms, paParms);
|
---|
843 | if (RT_FAILURE(vrc))
|
---|
844 | {
|
---|
845 | /** @todo What to do here? */
|
---|
846 | }
|
---|
847 | #else
|
---|
848 | /* Not needed within testcases. */
|
---|
849 | int vrc = VINF_SUCCESS;
|
---|
850 | #endif
|
---|
851 | LogFlowFuncLeaveRC(vrc);
|
---|
852 | return vrc;
|
---|
853 | }
|
---|
854 |
|
---|