1 | /* $Id: GuestCtrlPrivate.cpp 77586 2019-03-06 16:36:47Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Internal helpers/structures for guest control functionality.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011-2019 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 |
|
---|
19 | /*********************************************************************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *********************************************************************************************************************************/
|
---|
22 | #define LOG_GROUP LOG_GROUP_GUEST_CONTROL
|
---|
23 | #include "LoggingNew.h"
|
---|
24 |
|
---|
25 | #ifndef VBOX_WITH_GUEST_CONTROL
|
---|
26 | # error "VBOX_WITH_GUEST_CONTROL must defined in this file"
|
---|
27 | #endif
|
---|
28 | #include "GuestCtrlImplPrivate.h"
|
---|
29 | #include "GuestSessionImpl.h"
|
---|
30 | #include "VMMDev.h"
|
---|
31 |
|
---|
32 | #include <iprt/asm.h>
|
---|
33 | #include <iprt/cpp/utils.h> /* For unconst(). */
|
---|
34 | #include <iprt/ctype.h>
|
---|
35 | #ifdef DEBUG
|
---|
36 | # include <iprt/file.h>
|
---|
37 | #endif
|
---|
38 | #include <iprt/fs.h>
|
---|
39 | #include <iprt/rand.h>
|
---|
40 | #include <iprt/time.h>
|
---|
41 | #include <VBox/AssertGuest.h>
|
---|
42 |
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Extracts the timespec from a given stream block key.
|
---|
46 | *
|
---|
47 | * @return Pointer to handed-in timespec, or NULL if invalid / not found.
|
---|
48 | * @param strmBlk Stream block to extract timespec from.
|
---|
49 | * @param strKey Key to get timespec for.
|
---|
50 | * @param pTimeSpec Where to store the extracted timespec.
|
---|
51 | */
|
---|
52 | /* static */
|
---|
53 | PRTTIMESPEC GuestFsObjData::TimeSpecFromKey(const GuestProcessStreamBlock &strmBlk, const Utf8Str &strKey, PRTTIMESPEC pTimeSpec)
|
---|
54 | {
|
---|
55 | AssertPtrReturn(pTimeSpec, NULL);
|
---|
56 |
|
---|
57 | Utf8Str strTime = strmBlk.GetString(strKey.c_str());
|
---|
58 | if (strTime.isEmpty())
|
---|
59 | return NULL;
|
---|
60 |
|
---|
61 | if (!RTTimeSpecFromString(pTimeSpec, strTime.c_str()))
|
---|
62 | return NULL;
|
---|
63 |
|
---|
64 | return pTimeSpec;
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Extracts the nanoseconds relative from Unix epoch for a given stream block key.
|
---|
69 | *
|
---|
70 | * @return Nanoseconds relative from Unix epoch, or 0 if invalid / not found.
|
---|
71 | * @param strmBlk Stream block to extract nanoseconds from.
|
---|
72 | * @param strKey Key to get nanoseconds for.
|
---|
73 | */
|
---|
74 | /* static */
|
---|
75 | int64_t GuestFsObjData::UnixEpochNsFromKey(const GuestProcessStreamBlock &strmBlk, const Utf8Str &strKey)
|
---|
76 | {
|
---|
77 | RTTIMESPEC TimeSpec;
|
---|
78 | if (!GuestFsObjData::TimeSpecFromKey(strmBlk, strKey, &TimeSpec))
|
---|
79 | return 0;
|
---|
80 |
|
---|
81 | return TimeSpec.i64NanosecondsRelativeToUnixEpoch;
|
---|
82 | }
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Initializes this object data with a stream block from VBOXSERVICE_TOOL_LS.
|
---|
86 | *
|
---|
87 | * This is also used by FromStat since the output should be identical given that
|
---|
88 | * they use the same output function on the guest side when fLong is true.
|
---|
89 | *
|
---|
90 | * @return VBox status code.
|
---|
91 | * @param strmBlk Stream block to use for initialization.
|
---|
92 | * @param fLong Whether the stream block contains long (detailed) information or not.
|
---|
93 | */
|
---|
94 | int GuestFsObjData::FromLs(const GuestProcessStreamBlock &strmBlk, bool fLong)
|
---|
95 | {
|
---|
96 | LogFlowFunc(("\n"));
|
---|
97 | #ifdef DEBUG
|
---|
98 | strmBlk.DumpToLog();
|
---|
99 | #endif
|
---|
100 |
|
---|
101 | /* Object name. */
|
---|
102 | mName = strmBlk.GetString("name");
|
---|
103 | ASSERT_GUEST_RETURN(mName.isNotEmpty(), VERR_NOT_FOUND);
|
---|
104 |
|
---|
105 | /* Type & attributes. */
|
---|
106 | bool fHaveAttribs = false;
|
---|
107 | char szAttribs[32];
|
---|
108 | memset(szAttribs, '?', sizeof(szAttribs) - 1);
|
---|
109 | mType = FsObjType_Unknown;
|
---|
110 | const char *psz = strmBlk.GetString("ftype");
|
---|
111 | if (psz)
|
---|
112 | {
|
---|
113 | fHaveAttribs = true;
|
---|
114 | szAttribs[0] = *psz;
|
---|
115 | switch (*psz)
|
---|
116 | {
|
---|
117 | case '-': mType = FsObjType_File; break;
|
---|
118 | case 'd': mType = FsObjType_Directory; break;
|
---|
119 | case 'l': mType = FsObjType_Symlink; break;
|
---|
120 | case 'c': mType = FsObjType_DevChar; break;
|
---|
121 | case 'b': mType = FsObjType_DevBlock; break;
|
---|
122 | case 'f': mType = FsObjType_Fifo; break;
|
---|
123 | case 's': mType = FsObjType_Socket; break;
|
---|
124 | case 'w': mType = FsObjType_WhiteOut; break;
|
---|
125 | default:
|
---|
126 | AssertMsgFailed(("%s\n", psz));
|
---|
127 | szAttribs[0] = '?';
|
---|
128 | fHaveAttribs = false;
|
---|
129 | break;
|
---|
130 | }
|
---|
131 | }
|
---|
132 | psz = strmBlk.GetString("owner_mask");
|
---|
133 | if ( psz
|
---|
134 | && (psz[0] == '-' || psz[0] == 'r')
|
---|
135 | && (psz[1] == '-' || psz[1] == 'w')
|
---|
136 | && (psz[2] == '-' || psz[2] == 'x'))
|
---|
137 | {
|
---|
138 | szAttribs[1] = psz[0];
|
---|
139 | szAttribs[2] = psz[1];
|
---|
140 | szAttribs[3] = psz[2];
|
---|
141 | fHaveAttribs = true;
|
---|
142 | }
|
---|
143 | psz = strmBlk.GetString("group_mask");
|
---|
144 | if ( psz
|
---|
145 | && (psz[0] == '-' || psz[0] == 'r')
|
---|
146 | && (psz[1] == '-' || psz[1] == 'w')
|
---|
147 | && (psz[2] == '-' || psz[2] == 'x'))
|
---|
148 | {
|
---|
149 | szAttribs[4] = psz[0];
|
---|
150 | szAttribs[5] = psz[1];
|
---|
151 | szAttribs[6] = psz[2];
|
---|
152 | fHaveAttribs = true;
|
---|
153 | }
|
---|
154 | psz = strmBlk.GetString("other_mask");
|
---|
155 | if ( psz
|
---|
156 | && (psz[0] == '-' || psz[0] == 'r')
|
---|
157 | && (psz[1] == '-' || psz[1] == 'w')
|
---|
158 | && (psz[2] == '-' || psz[2] == 'x'))
|
---|
159 | {
|
---|
160 | szAttribs[7] = psz[0];
|
---|
161 | szAttribs[8] = psz[1];
|
---|
162 | szAttribs[9] = psz[2];
|
---|
163 | fHaveAttribs = true;
|
---|
164 | }
|
---|
165 | szAttribs[10] = ' '; /* Reserve three chars for sticky bits. */
|
---|
166 | szAttribs[11] = ' ';
|
---|
167 | szAttribs[12] = ' ';
|
---|
168 | szAttribs[13] = ' '; /* Separator. */
|
---|
169 | psz = strmBlk.GetString("dos_mask");
|
---|
170 | if ( psz
|
---|
171 | && (psz[ 0] == '-' || psz[ 0] == 'R')
|
---|
172 | && (psz[ 1] == '-' || psz[ 1] == 'H')
|
---|
173 | && (psz[ 2] == '-' || psz[ 2] == 'S')
|
---|
174 | && (psz[ 3] == '-' || psz[ 3] == 'D')
|
---|
175 | && (psz[ 4] == '-' || psz[ 4] == 'A')
|
---|
176 | && (psz[ 5] == '-' || psz[ 5] == 'd')
|
---|
177 | && (psz[ 6] == '-' || psz[ 6] == 'N')
|
---|
178 | && (psz[ 7] == '-' || psz[ 7] == 'T')
|
---|
179 | && (psz[ 8] == '-' || psz[ 8] == 'P')
|
---|
180 | && (psz[ 9] == '-' || psz[ 9] == 'J')
|
---|
181 | && (psz[10] == '-' || psz[10] == 'C')
|
---|
182 | && (psz[11] == '-' || psz[11] == 'O')
|
---|
183 | && (psz[12] == '-' || psz[12] == 'I')
|
---|
184 | && (psz[13] == '-' || psz[13] == 'E'))
|
---|
185 | {
|
---|
186 | memcpy(&szAttribs[14], psz, 14);
|
---|
187 | fHaveAttribs = true;
|
---|
188 | }
|
---|
189 | szAttribs[28] = '\0';
|
---|
190 | if (fHaveAttribs)
|
---|
191 | mFileAttrs = szAttribs;
|
---|
192 |
|
---|
193 | /* Object size. */
|
---|
194 | int rc = strmBlk.GetInt64Ex("st_size", &mObjectSize);
|
---|
195 | ASSERT_GUEST_RC_RETURN(rc, rc);
|
---|
196 | strmBlk.GetInt64Ex("alloc", &mAllocatedSize);
|
---|
197 |
|
---|
198 | /* INode number and device. */
|
---|
199 | psz = strmBlk.GetString("node_id");
|
---|
200 | if (!psz)
|
---|
201 | psz = strmBlk.GetString("cnode_id"); /* copy & past error fixed in 6.0 RC1 */
|
---|
202 | if (psz)
|
---|
203 | mNodeID = RTStrToInt64(psz);
|
---|
204 | mNodeIDDevice = strmBlk.GetUInt32("inode_dev"); /* (Produced by GAs prior to 6.0 RC1.) */
|
---|
205 |
|
---|
206 | if (fLong)
|
---|
207 | {
|
---|
208 | /* Dates. */
|
---|
209 | mAccessTime = GuestFsObjData::UnixEpochNsFromKey(strmBlk, "st_atime");
|
---|
210 | mBirthTime = GuestFsObjData::UnixEpochNsFromKey(strmBlk, "st_birthtime");
|
---|
211 | mChangeTime = GuestFsObjData::UnixEpochNsFromKey(strmBlk, "st_ctime");
|
---|
212 | mModificationTime = GuestFsObjData::UnixEpochNsFromKey(strmBlk, "st_mtime");
|
---|
213 |
|
---|
214 | /* Owner & group. */
|
---|
215 | mUID = strmBlk.GetInt32("uid");
|
---|
216 | psz = strmBlk.GetString("username");
|
---|
217 | if (psz)
|
---|
218 | mUserName = psz;
|
---|
219 | mGID = strmBlk.GetInt32("gid");
|
---|
220 | psz = strmBlk.GetString("groupname");
|
---|
221 | if (psz)
|
---|
222 | mGroupName = psz;
|
---|
223 |
|
---|
224 | /* Misc attributes: */
|
---|
225 | mNumHardLinks = strmBlk.GetUInt32("hlinks", 1);
|
---|
226 | mDeviceNumber = strmBlk.GetUInt32("st_rdev");
|
---|
227 | mGenerationID = strmBlk.GetUInt32("st_gen");
|
---|
228 | mUserFlags = strmBlk.GetUInt32("st_flags");
|
---|
229 |
|
---|
230 | /** @todo ACL */
|
---|
231 | }
|
---|
232 |
|
---|
233 | LogFlowFuncLeave();
|
---|
234 | return VINF_SUCCESS;
|
---|
235 | }
|
---|
236 |
|
---|
237 | /**
|
---|
238 | * Parses stream block output data which came from the 'stat' (vbox_stat)
|
---|
239 | * VBoxService toolbox command. The result will be stored in this object.
|
---|
240 | *
|
---|
241 | * @returns VBox status code.
|
---|
242 | * @param strmBlk Stream block output data to parse.
|
---|
243 | */
|
---|
244 | int GuestFsObjData::FromStat(const GuestProcessStreamBlock &strmBlk)
|
---|
245 | {
|
---|
246 | /* Should be identical output. */
|
---|
247 | return GuestFsObjData::FromLs(strmBlk, true /*fLong*/);
|
---|
248 | }
|
---|
249 |
|
---|
250 | /**
|
---|
251 | * Parses stream block output data which came from the 'mktemp' (vbox_mktemp)
|
---|
252 | * VBoxService toolbox command. The result will be stored in this object.
|
---|
253 | *
|
---|
254 | * @returns VBox status code.
|
---|
255 | * @param strmBlk Stream block output data to parse.
|
---|
256 | */
|
---|
257 | int GuestFsObjData::FromMkTemp(const GuestProcessStreamBlock &strmBlk)
|
---|
258 | {
|
---|
259 | LogFlowFunc(("\n"));
|
---|
260 |
|
---|
261 | #ifdef DEBUG
|
---|
262 | strmBlk.DumpToLog();
|
---|
263 | #endif
|
---|
264 | /* Object name. */
|
---|
265 | mName = strmBlk.GetString("name");
|
---|
266 | ASSERT_GUEST_RETURN(mName.isNotEmpty(), VERR_NOT_FOUND);
|
---|
267 |
|
---|
268 | /* Assign the stream block's rc. */
|
---|
269 | int rc = strmBlk.GetRc();
|
---|
270 |
|
---|
271 | LogFlowFuncLeaveRC(rc);
|
---|
272 | return rc;
|
---|
273 | }
|
---|
274 |
|
---|
275 | /**
|
---|
276 | * Returns the IPRT-compatible file mode.
|
---|
277 | * Note: Only handling RTFS_TYPE_ flags are implemented for now.
|
---|
278 | *
|
---|
279 | * @return IPRT file mode.
|
---|
280 | */
|
---|
281 | RTFMODE GuestFsObjData::GetFileMode(void) const
|
---|
282 | {
|
---|
283 | RTFMODE fMode = 0;
|
---|
284 |
|
---|
285 | switch (mType)
|
---|
286 | {
|
---|
287 | case FsObjType_Directory:
|
---|
288 | fMode |= RTFS_TYPE_DIRECTORY;
|
---|
289 | break;
|
---|
290 |
|
---|
291 | case FsObjType_File:
|
---|
292 | fMode |= RTFS_TYPE_FILE;
|
---|
293 | break;
|
---|
294 |
|
---|
295 | case FsObjType_Symlink:
|
---|
296 | fMode |= RTFS_TYPE_SYMLINK;
|
---|
297 | break;
|
---|
298 |
|
---|
299 | default:
|
---|
300 | break;
|
---|
301 | }
|
---|
302 |
|
---|
303 | /** @todo Implement more stuff. */
|
---|
304 |
|
---|
305 | return fMode;
|
---|
306 | }
|
---|
307 |
|
---|
308 | ///////////////////////////////////////////////////////////////////////////////
|
---|
309 |
|
---|
310 | /** @todo *NOT* thread safe yet! */
|
---|
311 | /** @todo Add exception handling for STL stuff! */
|
---|
312 |
|
---|
313 | GuestProcessStreamBlock::GuestProcessStreamBlock(void)
|
---|
314 | {
|
---|
315 |
|
---|
316 | }
|
---|
317 |
|
---|
318 | GuestProcessStreamBlock::~GuestProcessStreamBlock()
|
---|
319 | {
|
---|
320 | Clear();
|
---|
321 | }
|
---|
322 |
|
---|
323 | /**
|
---|
324 | * Clears (destroys) the currently stored stream pairs.
|
---|
325 | */
|
---|
326 | void GuestProcessStreamBlock::Clear(void)
|
---|
327 | {
|
---|
328 | mPairs.clear();
|
---|
329 | }
|
---|
330 |
|
---|
331 | #ifdef DEBUG
|
---|
332 | /**
|
---|
333 | * Dumps the currently stored stream pairs to the (debug) log.
|
---|
334 | */
|
---|
335 | void GuestProcessStreamBlock::DumpToLog(void) const
|
---|
336 | {
|
---|
337 | LogFlowFunc(("Dumping contents of stream block=0x%p (%ld items):\n",
|
---|
338 | this, mPairs.size()));
|
---|
339 |
|
---|
340 | for (GuestCtrlStreamPairMapIterConst it = mPairs.begin();
|
---|
341 | it != mPairs.end(); ++it)
|
---|
342 | {
|
---|
343 | LogFlowFunc(("\t%s=%s\n", it->first.c_str(), it->second.mValue.c_str()));
|
---|
344 | }
|
---|
345 | }
|
---|
346 | #endif
|
---|
347 |
|
---|
348 | /**
|
---|
349 | * Returns a 64-bit signed integer of a specified key.
|
---|
350 | *
|
---|
351 | * @return VBox status code. VERR_NOT_FOUND if key was not found.
|
---|
352 | * @param pszKey Name of key to get the value for.
|
---|
353 | * @param piVal Pointer to value to return.
|
---|
354 | */
|
---|
355 | int GuestProcessStreamBlock::GetInt64Ex(const char *pszKey, int64_t *piVal) const
|
---|
356 | {
|
---|
357 | AssertPtrReturn(pszKey, VERR_INVALID_POINTER);
|
---|
358 | AssertPtrReturn(piVal, VERR_INVALID_POINTER);
|
---|
359 | const char *pszValue = GetString(pszKey);
|
---|
360 | if (pszValue)
|
---|
361 | {
|
---|
362 | *piVal = RTStrToInt64(pszValue);
|
---|
363 | return VINF_SUCCESS;
|
---|
364 | }
|
---|
365 | return VERR_NOT_FOUND;
|
---|
366 | }
|
---|
367 |
|
---|
368 | /**
|
---|
369 | * Returns a 64-bit integer of a specified key.
|
---|
370 | *
|
---|
371 | * @return int64_t Value to return, 0 if not found / on failure.
|
---|
372 | * @param pszKey Name of key to get the value for.
|
---|
373 | */
|
---|
374 | int64_t GuestProcessStreamBlock::GetInt64(const char *pszKey) const
|
---|
375 | {
|
---|
376 | int64_t iVal;
|
---|
377 | if (RT_SUCCESS(GetInt64Ex(pszKey, &iVal)))
|
---|
378 | return iVal;
|
---|
379 | return 0;
|
---|
380 | }
|
---|
381 |
|
---|
382 | /**
|
---|
383 | * Returns the current number of stream pairs.
|
---|
384 | *
|
---|
385 | * @return uint32_t Current number of stream pairs.
|
---|
386 | */
|
---|
387 | size_t GuestProcessStreamBlock::GetCount(void) const
|
---|
388 | {
|
---|
389 | return mPairs.size();
|
---|
390 | }
|
---|
391 |
|
---|
392 | /**
|
---|
393 | * Gets the return code (name = "rc") of this stream block.
|
---|
394 | *
|
---|
395 | * @return VBox status code.
|
---|
396 | */
|
---|
397 | int GuestProcessStreamBlock::GetRc(void) const
|
---|
398 | {
|
---|
399 | const char *pszValue = GetString("rc");
|
---|
400 | if (pszValue)
|
---|
401 | {
|
---|
402 | return RTStrToInt16(pszValue);
|
---|
403 | }
|
---|
404 | return VERR_NOT_FOUND;
|
---|
405 | }
|
---|
406 |
|
---|
407 | /**
|
---|
408 | * Returns a string value of a specified key.
|
---|
409 | *
|
---|
410 | * @return uint32_t Pointer to string to return, NULL if not found / on failure.
|
---|
411 | * @param pszKey Name of key to get the value for.
|
---|
412 | */
|
---|
413 | const char *GuestProcessStreamBlock::GetString(const char *pszKey) const
|
---|
414 | {
|
---|
415 | AssertPtrReturn(pszKey, NULL);
|
---|
416 |
|
---|
417 | try
|
---|
418 | {
|
---|
419 | GuestCtrlStreamPairMapIterConst itPairs = mPairs.find(pszKey);
|
---|
420 | if (itPairs != mPairs.end())
|
---|
421 | return itPairs->second.mValue.c_str();
|
---|
422 | }
|
---|
423 | catch (const std::exception &ex)
|
---|
424 | {
|
---|
425 | RT_NOREF(ex);
|
---|
426 | }
|
---|
427 | return NULL;
|
---|
428 | }
|
---|
429 |
|
---|
430 | /**
|
---|
431 | * Returns a 32-bit unsigned integer of a specified key.
|
---|
432 | *
|
---|
433 | * @return VBox status code. VERR_NOT_FOUND if key was not found.
|
---|
434 | * @param pszKey Name of key to get the value for.
|
---|
435 | * @param puVal Pointer to value to return.
|
---|
436 | */
|
---|
437 | int GuestProcessStreamBlock::GetUInt32Ex(const char *pszKey, uint32_t *puVal) const
|
---|
438 | {
|
---|
439 | const char *pszValue = GetString(pszKey);
|
---|
440 | if (pszValue)
|
---|
441 | {
|
---|
442 | *puVal = RTStrToUInt32(pszValue);
|
---|
443 | return VINF_SUCCESS;
|
---|
444 | }
|
---|
445 | return VERR_NOT_FOUND;
|
---|
446 | }
|
---|
447 |
|
---|
448 | /**
|
---|
449 | * Returns a 32-bit signed integer of a specified key.
|
---|
450 | *
|
---|
451 | * @returns 32-bit signed value
|
---|
452 | * @param pszKey Name of key to get the value for.
|
---|
453 | * @param iDefault The default to return on error if not found.
|
---|
454 | */
|
---|
455 | int32_t GuestProcessStreamBlock::GetInt32(const char *pszKey, int32_t iDefault) const
|
---|
456 | {
|
---|
457 | const char *pszValue = GetString(pszKey);
|
---|
458 | if (pszValue)
|
---|
459 | {
|
---|
460 | int32_t iRet;
|
---|
461 | int rc = RTStrToInt32Full(pszValue, 0, &iRet);
|
---|
462 | if (RT_SUCCESS(rc))
|
---|
463 | return iRet;
|
---|
464 | ASSERT_GUEST_MSG_FAILED(("%s=%s\n", pszKey, pszValue));
|
---|
465 | }
|
---|
466 | return iDefault;
|
---|
467 | }
|
---|
468 |
|
---|
469 | /**
|
---|
470 | * Returns a 32-bit unsigned integer of a specified key.
|
---|
471 | *
|
---|
472 | * @return uint32_t Value to return, 0 if not found / on failure.
|
---|
473 | * @param pszKey Name of key to get the value for.
|
---|
474 | * @param uDefault The default value to return.
|
---|
475 | */
|
---|
476 | uint32_t GuestProcessStreamBlock::GetUInt32(const char *pszKey, uint32_t uDefault /*= 0*/) const
|
---|
477 | {
|
---|
478 | uint32_t uVal;
|
---|
479 | if (RT_SUCCESS(GetUInt32Ex(pszKey, &uVal)))
|
---|
480 | return uVal;
|
---|
481 | return uDefault;
|
---|
482 | }
|
---|
483 |
|
---|
484 | /**
|
---|
485 | * Sets a value to a key or deletes a key by setting a NULL value.
|
---|
486 | *
|
---|
487 | * @return VBox status code.
|
---|
488 | * @param pszKey Key name to process.
|
---|
489 | * @param pszValue Value to set. Set NULL for deleting the key.
|
---|
490 | */
|
---|
491 | int GuestProcessStreamBlock::SetValue(const char *pszKey, const char *pszValue)
|
---|
492 | {
|
---|
493 | AssertPtrReturn(pszKey, VERR_INVALID_POINTER);
|
---|
494 |
|
---|
495 | int rc = VINF_SUCCESS;
|
---|
496 | try
|
---|
497 | {
|
---|
498 | Utf8Str Utf8Key(pszKey);
|
---|
499 |
|
---|
500 | /* Take a shortcut and prevent crashes on some funny versions
|
---|
501 | * of STL if map is empty initially. */
|
---|
502 | if (!mPairs.empty())
|
---|
503 | {
|
---|
504 | GuestCtrlStreamPairMapIter it = mPairs.find(Utf8Key);
|
---|
505 | if (it != mPairs.end())
|
---|
506 | mPairs.erase(it);
|
---|
507 | }
|
---|
508 |
|
---|
509 | if (pszValue)
|
---|
510 | {
|
---|
511 | GuestProcessStreamValue val(pszValue);
|
---|
512 | mPairs[Utf8Key] = val;
|
---|
513 | }
|
---|
514 | }
|
---|
515 | catch (const std::exception &ex)
|
---|
516 | {
|
---|
517 | RT_NOREF(ex);
|
---|
518 | }
|
---|
519 | return rc;
|
---|
520 | }
|
---|
521 |
|
---|
522 | ///////////////////////////////////////////////////////////////////////////////
|
---|
523 |
|
---|
524 | GuestProcessStream::GuestProcessStream(void)
|
---|
525 | : m_cbAllocated(0),
|
---|
526 | m_cbUsed(0),
|
---|
527 | m_offBuffer(0),
|
---|
528 | m_pbBuffer(NULL)
|
---|
529 | {
|
---|
530 |
|
---|
531 | }
|
---|
532 |
|
---|
533 | GuestProcessStream::~GuestProcessStream(void)
|
---|
534 | {
|
---|
535 | Destroy();
|
---|
536 | }
|
---|
537 |
|
---|
538 | /**
|
---|
539 | * Adds data to the internal parser buffer. Useful if there
|
---|
540 | * are multiple rounds of adding data needed.
|
---|
541 | *
|
---|
542 | * @return VBox status code.
|
---|
543 | * @param pbData Pointer to data to add.
|
---|
544 | * @param cbData Size (in bytes) of data to add.
|
---|
545 | */
|
---|
546 | int GuestProcessStream::AddData(const BYTE *pbData, size_t cbData)
|
---|
547 | {
|
---|
548 | AssertPtrReturn(pbData, VERR_INVALID_POINTER);
|
---|
549 | AssertReturn(cbData, VERR_INVALID_PARAMETER);
|
---|
550 |
|
---|
551 | int rc = VINF_SUCCESS;
|
---|
552 |
|
---|
553 | /* Rewind the buffer if it's empty. */
|
---|
554 | size_t cbInBuf = m_cbUsed - m_offBuffer;
|
---|
555 | bool const fAddToSet = cbInBuf == 0;
|
---|
556 | if (fAddToSet)
|
---|
557 | m_cbUsed = m_offBuffer = 0;
|
---|
558 |
|
---|
559 | /* Try and see if we can simply append the data. */
|
---|
560 | if (cbData + m_cbUsed <= m_cbAllocated)
|
---|
561 | {
|
---|
562 | memcpy(&m_pbBuffer[m_cbUsed], pbData, cbData);
|
---|
563 | m_cbUsed += cbData;
|
---|
564 | }
|
---|
565 | else
|
---|
566 | {
|
---|
567 | /* Move any buffered data to the front. */
|
---|
568 | cbInBuf = m_cbUsed - m_offBuffer;
|
---|
569 | if (cbInBuf == 0)
|
---|
570 | m_cbUsed = m_offBuffer = 0;
|
---|
571 | else if (m_offBuffer) /* Do we have something to move? */
|
---|
572 | {
|
---|
573 | memmove(m_pbBuffer, &m_pbBuffer[m_offBuffer], cbInBuf);
|
---|
574 | m_cbUsed = cbInBuf;
|
---|
575 | m_offBuffer = 0;
|
---|
576 | }
|
---|
577 |
|
---|
578 | /* Do we need to grow the buffer? */
|
---|
579 | if (cbData + m_cbUsed > m_cbAllocated)
|
---|
580 | {
|
---|
581 | /** @todo Put an upper limit on the allocation? */
|
---|
582 | size_t cbAlloc = m_cbUsed + cbData;
|
---|
583 | cbAlloc = RT_ALIGN_Z(cbAlloc, _64K);
|
---|
584 | void *pvNew = RTMemRealloc(m_pbBuffer, cbAlloc);
|
---|
585 | if (pvNew)
|
---|
586 | {
|
---|
587 | m_pbBuffer = (uint8_t *)pvNew;
|
---|
588 | m_cbAllocated = cbAlloc;
|
---|
589 | }
|
---|
590 | else
|
---|
591 | rc = VERR_NO_MEMORY;
|
---|
592 | }
|
---|
593 |
|
---|
594 | /* Finally, copy the data. */
|
---|
595 | if (RT_SUCCESS(rc))
|
---|
596 | {
|
---|
597 | if (cbData + m_cbUsed <= m_cbAllocated)
|
---|
598 | {
|
---|
599 | memcpy(&m_pbBuffer[m_cbUsed], pbData, cbData);
|
---|
600 | m_cbUsed += cbData;
|
---|
601 | }
|
---|
602 | else
|
---|
603 | rc = VERR_BUFFER_OVERFLOW;
|
---|
604 | }
|
---|
605 | }
|
---|
606 |
|
---|
607 | return rc;
|
---|
608 | }
|
---|
609 |
|
---|
610 | /**
|
---|
611 | * Destroys the internal data buffer.
|
---|
612 | */
|
---|
613 | void GuestProcessStream::Destroy(void)
|
---|
614 | {
|
---|
615 | if (m_pbBuffer)
|
---|
616 | {
|
---|
617 | RTMemFree(m_pbBuffer);
|
---|
618 | m_pbBuffer = NULL;
|
---|
619 | }
|
---|
620 |
|
---|
621 | m_cbAllocated = 0;
|
---|
622 | m_cbUsed = 0;
|
---|
623 | m_offBuffer = 0;
|
---|
624 | }
|
---|
625 |
|
---|
626 | #ifdef DEBUG
|
---|
627 | /**
|
---|
628 | * Dumps the raw guest process output to a file on the host.
|
---|
629 | * If the file on the host already exists, it will be overwritten.
|
---|
630 | *
|
---|
631 | * @param pszFile Absolute path to host file to dump the output to.
|
---|
632 | */
|
---|
633 | void GuestProcessStream::Dump(const char *pszFile)
|
---|
634 | {
|
---|
635 | LogFlowFunc(("Dumping contents of stream=0x%p (cbAlloc=%u, cbSize=%u, cbOff=%u) to %s\n",
|
---|
636 | m_pbBuffer, m_cbAllocated, m_cbUsed, m_offBuffer, pszFile));
|
---|
637 |
|
---|
638 | RTFILE hFile;
|
---|
639 | int rc = RTFileOpen(&hFile, pszFile, RTFILE_O_CREATE_REPLACE | RTFILE_O_WRITE | RTFILE_O_DENY_WRITE);
|
---|
640 | if (RT_SUCCESS(rc))
|
---|
641 | {
|
---|
642 | rc = RTFileWrite(hFile, m_pbBuffer, m_cbUsed, NULL /* pcbWritten */);
|
---|
643 | RTFileClose(hFile);
|
---|
644 | }
|
---|
645 | }
|
---|
646 | #endif
|
---|
647 |
|
---|
648 | /**
|
---|
649 | * Tries to parse the next upcoming pair block within the internal
|
---|
650 | * buffer.
|
---|
651 | *
|
---|
652 | * Returns VERR_NO_DATA is no data is in internal buffer or buffer has been
|
---|
653 | * completely parsed already.
|
---|
654 | *
|
---|
655 | * Returns VERR_MORE_DATA if current block was parsed (with zero or more pairs
|
---|
656 | * stored in stream block) but still contains incomplete (unterminated)
|
---|
657 | * data.
|
---|
658 | *
|
---|
659 | * Returns VINF_SUCCESS if current block was parsed until the next upcoming
|
---|
660 | * block (with zero or more pairs stored in stream block).
|
---|
661 | *
|
---|
662 | * @return VBox status code.
|
---|
663 | * @param streamBlock Reference to guest stream block to fill.
|
---|
664 | */
|
---|
665 | int GuestProcessStream::ParseBlock(GuestProcessStreamBlock &streamBlock)
|
---|
666 | {
|
---|
667 | if ( !m_pbBuffer
|
---|
668 | || !m_cbUsed)
|
---|
669 | {
|
---|
670 | return VERR_NO_DATA;
|
---|
671 | }
|
---|
672 |
|
---|
673 | AssertReturn(m_offBuffer <= m_cbUsed, VERR_INVALID_PARAMETER);
|
---|
674 | if (m_offBuffer == m_cbUsed)
|
---|
675 | return VERR_NO_DATA;
|
---|
676 |
|
---|
677 | int rc = VINF_SUCCESS;
|
---|
678 |
|
---|
679 | char *pszOff = (char*)&m_pbBuffer[m_offBuffer];
|
---|
680 | char *pszStart = pszOff;
|
---|
681 | uint32_t uDistance;
|
---|
682 | while (*pszStart)
|
---|
683 | {
|
---|
684 | size_t pairLen = strlen(pszStart);
|
---|
685 | uDistance = (pszStart - pszOff);
|
---|
686 | if (m_offBuffer + uDistance + pairLen + 1 >= m_cbUsed)
|
---|
687 | {
|
---|
688 | rc = VERR_MORE_DATA;
|
---|
689 | break;
|
---|
690 | }
|
---|
691 | else
|
---|
692 | {
|
---|
693 | char *pszSep = strchr(pszStart, '=');
|
---|
694 | char *pszVal = NULL;
|
---|
695 | if (pszSep)
|
---|
696 | pszVal = pszSep + 1;
|
---|
697 | if (!pszSep || !pszVal)
|
---|
698 | {
|
---|
699 | rc = VERR_MORE_DATA;
|
---|
700 | break;
|
---|
701 | }
|
---|
702 |
|
---|
703 | /* Terminate the separator so that we can
|
---|
704 | * use pszStart as our key from now on. */
|
---|
705 | *pszSep = '\0';
|
---|
706 |
|
---|
707 | rc = streamBlock.SetValue(pszStart, pszVal);
|
---|
708 | if (RT_FAILURE(rc))
|
---|
709 | return rc;
|
---|
710 | }
|
---|
711 |
|
---|
712 | /* Next pair. */
|
---|
713 | pszStart += pairLen + 1;
|
---|
714 | }
|
---|
715 |
|
---|
716 | /* If we did not do any movement but we have stuff left
|
---|
717 | * in our buffer just skip the current termination so that
|
---|
718 | * we can try next time. */
|
---|
719 | uDistance = (pszStart - pszOff);
|
---|
720 | if ( !uDistance
|
---|
721 | && *pszStart == '\0'
|
---|
722 | && m_offBuffer < m_cbUsed)
|
---|
723 | {
|
---|
724 | uDistance++;
|
---|
725 | }
|
---|
726 | m_offBuffer += uDistance;
|
---|
727 |
|
---|
728 | return rc;
|
---|
729 | }
|
---|
730 |
|
---|
731 | GuestBase::GuestBase(void)
|
---|
732 | : mConsole(NULL)
|
---|
733 | , mNextContextID(RTRandU32() % VBOX_GUESTCTRL_MAX_CONTEXTS)
|
---|
734 | {
|
---|
735 | }
|
---|
736 |
|
---|
737 | GuestBase::~GuestBase(void)
|
---|
738 | {
|
---|
739 | }
|
---|
740 |
|
---|
741 | /**
|
---|
742 | * Separate initialization function for the base class.
|
---|
743 | *
|
---|
744 | * @returns VBox status code.
|
---|
745 | */
|
---|
746 | int GuestBase::baseInit(void)
|
---|
747 | {
|
---|
748 | int rc = RTCritSectInit(&mWaitEventCritSect);
|
---|
749 |
|
---|
750 | LogFlowFuncLeaveRC(rc);
|
---|
751 | return rc;
|
---|
752 | }
|
---|
753 |
|
---|
754 | /**
|
---|
755 | * Separate uninitialization function for the base class.
|
---|
756 | */
|
---|
757 | void GuestBase::baseUninit(void)
|
---|
758 | {
|
---|
759 | LogFlowThisFuncEnter();
|
---|
760 |
|
---|
761 | /* Make sure to cancel any outstanding wait events. */
|
---|
762 | int rc2 = cancelWaitEvents();
|
---|
763 | AssertRC(rc2);
|
---|
764 |
|
---|
765 | rc2 = RTCritSectDelete(&mWaitEventCritSect);
|
---|
766 | AssertRC(rc2);
|
---|
767 |
|
---|
768 | LogFlowFuncLeaveRC(rc2);
|
---|
769 | /* No return value. */
|
---|
770 | }
|
---|
771 |
|
---|
772 | /**
|
---|
773 | * Cancels all outstanding wait events.
|
---|
774 | *
|
---|
775 | * @returns VBox status code.
|
---|
776 | */
|
---|
777 | int GuestBase::cancelWaitEvents(void)
|
---|
778 | {
|
---|
779 | LogFlowThisFuncEnter();
|
---|
780 |
|
---|
781 | int rc = RTCritSectEnter(&mWaitEventCritSect);
|
---|
782 | if (RT_SUCCESS(rc))
|
---|
783 | {
|
---|
784 | GuestEventGroup::iterator itEventGroups = mWaitEventGroups.begin();
|
---|
785 | while (itEventGroups != mWaitEventGroups.end())
|
---|
786 | {
|
---|
787 | GuestWaitEvents::iterator itEvents = itEventGroups->second.begin();
|
---|
788 | while (itEvents != itEventGroups->second.end())
|
---|
789 | {
|
---|
790 | GuestWaitEvent *pEvent = itEvents->second;
|
---|
791 | AssertPtr(pEvent);
|
---|
792 |
|
---|
793 | /*
|
---|
794 | * Just cancel the event, but don't remove it from the
|
---|
795 | * wait events map. Don't delete it though, this (hopefully)
|
---|
796 | * is done by the caller using unregisterWaitEvent().
|
---|
797 | */
|
---|
798 | int rc2 = pEvent->Cancel();
|
---|
799 | AssertRC(rc2);
|
---|
800 |
|
---|
801 | ++itEvents;
|
---|
802 | }
|
---|
803 |
|
---|
804 | ++itEventGroups;
|
---|
805 | }
|
---|
806 |
|
---|
807 | int rc2 = RTCritSectLeave(&mWaitEventCritSect);
|
---|
808 | if (RT_SUCCESS(rc))
|
---|
809 | rc = rc2;
|
---|
810 | }
|
---|
811 |
|
---|
812 | LogFlowFuncLeaveRC(rc);
|
---|
813 | return rc;
|
---|
814 | }
|
---|
815 |
|
---|
816 | /**
|
---|
817 | * Handles generic messages not bound to a specific object type.
|
---|
818 | *
|
---|
819 | * @return VBox status code. VERR_NOT_FOUND if no handler has been found or VERR_NOT_SUPPORTED
|
---|
820 | * if this class does not support the specified callback.
|
---|
821 | * @param pCtxCb Host callback context.
|
---|
822 | * @param pSvcCb Service callback data.
|
---|
823 | */
|
---|
824 | int GuestBase::dispatchGeneric(PVBOXGUESTCTRLHOSTCBCTX pCtxCb, PVBOXGUESTCTRLHOSTCALLBACK pSvcCb)
|
---|
825 | {
|
---|
826 | LogFlowFunc(("pCtxCb=%p, pSvcCb=%p\n", pCtxCb, pSvcCb));
|
---|
827 |
|
---|
828 | AssertPtrReturn(pCtxCb, VERR_INVALID_POINTER);
|
---|
829 | AssertPtrReturn(pSvcCb, VERR_INVALID_POINTER);
|
---|
830 |
|
---|
831 | int vrc;
|
---|
832 |
|
---|
833 | try
|
---|
834 | {
|
---|
835 | Log2Func(("uFunc=%RU32, cParms=%RU32\n", pCtxCb->uMessage, pSvcCb->mParms));
|
---|
836 |
|
---|
837 | switch (pCtxCb->uMessage)
|
---|
838 | {
|
---|
839 | case GUEST_MSG_PROGRESS_UPDATE:
|
---|
840 | vrc = VINF_SUCCESS;
|
---|
841 | break;
|
---|
842 |
|
---|
843 | case GUEST_MSG_REPLY:
|
---|
844 | {
|
---|
845 | if (pSvcCb->mParms >= 4)
|
---|
846 | {
|
---|
847 | int idx = 1; /* Current parameter index. */
|
---|
848 | CALLBACKDATA_MSG_REPLY dataCb;
|
---|
849 | /* pSvcCb->mpaParms[0] always contains the context ID. */
|
---|
850 | vrc = HGCMSvcGetU32(&pSvcCb->mpaParms[idx++], &dataCb.uType);
|
---|
851 | AssertRCReturn(vrc, vrc);
|
---|
852 | vrc = HGCMSvcGetU32(&pSvcCb->mpaParms[idx++], &dataCb.rc);
|
---|
853 | AssertRCReturn(vrc, vrc);
|
---|
854 | vrc = HGCMSvcGetPv(&pSvcCb->mpaParms[idx++], &dataCb.pvPayload, &dataCb.cbPayload);
|
---|
855 | AssertRCReturn(vrc, vrc);
|
---|
856 |
|
---|
857 | GuestWaitEventPayload evPayload(dataCb.uType, dataCb.pvPayload, dataCb.cbPayload); /* This bugger throws int. */
|
---|
858 | vrc = signalWaitEventInternal(pCtxCb, dataCb.rc, &evPayload);
|
---|
859 | }
|
---|
860 | else
|
---|
861 | vrc = VERR_INVALID_PARAMETER;
|
---|
862 | break;
|
---|
863 | }
|
---|
864 |
|
---|
865 | default:
|
---|
866 | vrc = VERR_NOT_SUPPORTED;
|
---|
867 | break;
|
---|
868 | }
|
---|
869 | }
|
---|
870 | catch (std::bad_alloc &)
|
---|
871 | {
|
---|
872 | vrc = VERR_NO_MEMORY;
|
---|
873 | }
|
---|
874 | catch (int rc)
|
---|
875 | {
|
---|
876 | vrc = rc;
|
---|
877 | }
|
---|
878 |
|
---|
879 | LogFlowFuncLeaveRC(vrc);
|
---|
880 | return vrc;
|
---|
881 | }
|
---|
882 |
|
---|
883 | /**
|
---|
884 | * Generates a context ID (CID) by incrementing the object's count.
|
---|
885 | * A CID consists of a session ID, an object ID and a count.
|
---|
886 | *
|
---|
887 | * Note: This function does not guarantee that the returned CID is unique;
|
---|
888 | * the caller has to take care of that and eventually retry.
|
---|
889 | *
|
---|
890 | * @returns VBox status code.
|
---|
891 | * @param uSessionID Session ID to use for CID generation.
|
---|
892 | * @param uObjectID Object ID to use for CID generation.
|
---|
893 | * @param puContextID Where to store the generated CID on success.
|
---|
894 | */
|
---|
895 | int GuestBase::generateContextID(uint32_t uSessionID, uint32_t uObjectID, uint32_t *puContextID)
|
---|
896 | {
|
---|
897 | AssertPtrReturn(puContextID, VERR_INVALID_POINTER);
|
---|
898 |
|
---|
899 | if ( uSessionID >= VBOX_GUESTCTRL_MAX_SESSIONS
|
---|
900 | || uObjectID >= VBOX_GUESTCTRL_MAX_OBJECTS)
|
---|
901 | return VERR_INVALID_PARAMETER;
|
---|
902 |
|
---|
903 | uint32_t uCount = ASMAtomicIncU32(&mNextContextID);
|
---|
904 | if (uCount >= VBOX_GUESTCTRL_MAX_CONTEXTS)
|
---|
905 | uCount = 0;
|
---|
906 |
|
---|
907 | uint32_t uNewContextID = VBOX_GUESTCTRL_CONTEXTID_MAKE(uSessionID, uObjectID, uCount);
|
---|
908 |
|
---|
909 | *puContextID = uNewContextID;
|
---|
910 |
|
---|
911 | #if 0
|
---|
912 | LogFlowThisFunc(("mNextContextID=%RU32, uSessionID=%RU32, uObjectID=%RU32, uCount=%RU32, uNewContextID=%RU32\n",
|
---|
913 | mNextContextID, uSessionID, uObjectID, uCount, uNewContextID));
|
---|
914 | #endif
|
---|
915 | return VINF_SUCCESS;
|
---|
916 | }
|
---|
917 |
|
---|
918 | /**
|
---|
919 | * Registers (creates) a new wait event based on a given session and object ID.
|
---|
920 | *
|
---|
921 | * From those IDs an unique context ID (CID) will be built, which only can be
|
---|
922 | * around once at a time.
|
---|
923 | *
|
---|
924 | * @returns VBox status code.
|
---|
925 | * @retval VERR_GSTCTL_MAX_CID_COUNT_REACHED if unable to generate a free context ID (CID, the count part (bits 15:0)).
|
---|
926 | * @param uSessionID Session ID to register wait event for.
|
---|
927 | * @param uObjectID Object ID to register wait event for.
|
---|
928 | * @param ppEvent Pointer to registered (created) wait event on success.
|
---|
929 | * Must be destroyed with unregisterWaitEvent().
|
---|
930 | */
|
---|
931 | int GuestBase::registerWaitEvent(uint32_t uSessionID, uint32_t uObjectID, GuestWaitEvent **ppEvent)
|
---|
932 | {
|
---|
933 | GuestEventTypes eventTypesEmpty;
|
---|
934 | return registerWaitEventEx(uSessionID, uObjectID, eventTypesEmpty, ppEvent);
|
---|
935 | }
|
---|
936 |
|
---|
937 | /**
|
---|
938 | * Creates and registers a new wait event object that waits on a set of events
|
---|
939 | * related to a given object within the session.
|
---|
940 | *
|
---|
941 | * From the session ID and object ID a one-time unique context ID (CID) is built
|
---|
942 | * for this wait object. Normally the CID is then passed to the guest along
|
---|
943 | * with a request, and the guest passed the CID back with the reply. The
|
---|
944 | * handler for the reply then emits a signal on the event type associated with
|
---|
945 | * the reply, which includes signalling the object returned by this method and
|
---|
946 | * the waking up the thread waiting on it.
|
---|
947 | *
|
---|
948 | * @returns VBox status code.
|
---|
949 | * @retval VERR_GSTCTL_MAX_CID_COUNT_REACHED if unable to generate a free context ID (CID, the count part (bits 15:0)).
|
---|
950 | * @param uSessionID Session ID to register wait event for.
|
---|
951 | * @param uObjectID Object ID to register wait event for.
|
---|
952 | * @param lstEvents List of events to register the wait event for.
|
---|
953 | * @param ppEvent Pointer to registered (created) wait event on success.
|
---|
954 | * Must be destroyed with unregisterWaitEvent().
|
---|
955 | */
|
---|
956 | int GuestBase::registerWaitEventEx(uint32_t uSessionID, uint32_t uObjectID, const GuestEventTypes &lstEvents,
|
---|
957 | GuestWaitEvent **ppEvent)
|
---|
958 | {
|
---|
959 | AssertPtrReturn(ppEvent, VERR_INVALID_POINTER);
|
---|
960 |
|
---|
961 | uint32_t idContext;
|
---|
962 | int rc = generateContextID(uSessionID, uObjectID, &idContext);
|
---|
963 | AssertRCReturn(rc, rc);
|
---|
964 |
|
---|
965 | GuestWaitEvent *pEvent = new GuestWaitEvent();
|
---|
966 | AssertPtrReturn(pEvent, VERR_NO_MEMORY);
|
---|
967 |
|
---|
968 | rc = pEvent->Init(idContext, lstEvents);
|
---|
969 | AssertRCReturn(rc, rc);
|
---|
970 |
|
---|
971 | LogFlowThisFunc(("New event=%p, CID=%RU32\n", pEvent, idContext));
|
---|
972 |
|
---|
973 | rc = RTCritSectEnter(&mWaitEventCritSect);
|
---|
974 | if (RT_SUCCESS(rc))
|
---|
975 | {
|
---|
976 | /*
|
---|
977 | * Check that we don't have any context ID collisions (should be very unlikely).
|
---|
978 | *
|
---|
979 | * The ASSUMPTION here is that mWaitEvents has all the same events as
|
---|
980 | * mWaitEventGroups, so it suffices to check one of the two.
|
---|
981 | */
|
---|
982 | if (mWaitEvents.find(idContext) != mWaitEvents.end())
|
---|
983 | {
|
---|
984 | uint32_t cTries = 0;
|
---|
985 | do
|
---|
986 | {
|
---|
987 | rc = generateContextID(uSessionID, uObjectID, &idContext);
|
---|
988 | AssertRCBreak(rc);
|
---|
989 | LogFunc(("Found context ID duplicate; trying a different context ID: %#x\n", idContext));
|
---|
990 | if (mWaitEvents.find(idContext) != mWaitEvents.end())
|
---|
991 | rc = VERR_GSTCTL_MAX_CID_COUNT_REACHED;
|
---|
992 | } while (RT_FAILURE_NP(rc) && cTries++ < 10);
|
---|
993 | }
|
---|
994 | if (RT_SUCCESS(rc))
|
---|
995 | {
|
---|
996 | /*
|
---|
997 | * Insert event into matching event group. This is for faster per-group lookup of all events later.
|
---|
998 | */
|
---|
999 | uint32_t cInserts = 0;
|
---|
1000 | for (GuestEventTypes::const_iterator ItType = lstEvents.begin(); ItType != lstEvents.end(); ++ItType)
|
---|
1001 | {
|
---|
1002 | GuestWaitEvents &eventGroup = mWaitEventGroups[*ItType];
|
---|
1003 | if (eventGroup.find(idContext) == eventGroup.end())
|
---|
1004 | {
|
---|
1005 | try
|
---|
1006 | {
|
---|
1007 | eventGroup.insert(std::pair<uint32_t, GuestWaitEvent *>(idContext, pEvent));
|
---|
1008 | cInserts++;
|
---|
1009 | }
|
---|
1010 | catch (std::bad_alloc &)
|
---|
1011 | {
|
---|
1012 | while (ItType != lstEvents.begin())
|
---|
1013 | {
|
---|
1014 | --ItType;
|
---|
1015 | mWaitEventGroups[*ItType].erase(idContext);
|
---|
1016 | }
|
---|
1017 | rc = VERR_NO_MEMORY;
|
---|
1018 | break;
|
---|
1019 | }
|
---|
1020 | }
|
---|
1021 | else
|
---|
1022 | Assert(cInserts > 0); /* else: lstEvents has duplicate entries. */
|
---|
1023 | }
|
---|
1024 | if (RT_SUCCESS(rc))
|
---|
1025 | {
|
---|
1026 | Assert(cInserts > 0 || lstEvents.size() == 0);
|
---|
1027 | RT_NOREF(cInserts);
|
---|
1028 |
|
---|
1029 | /*
|
---|
1030 | * Register event in the regular event list.
|
---|
1031 | */
|
---|
1032 | try
|
---|
1033 | {
|
---|
1034 | mWaitEvents[idContext] = pEvent;
|
---|
1035 | }
|
---|
1036 | catch (std::bad_alloc &)
|
---|
1037 | {
|
---|
1038 | for (GuestEventTypes::const_iterator ItType = lstEvents.begin(); ItType != lstEvents.end(); ++ItType)
|
---|
1039 | mWaitEventGroups[*ItType].erase(idContext);
|
---|
1040 | rc = VERR_NO_MEMORY;
|
---|
1041 | }
|
---|
1042 | }
|
---|
1043 | }
|
---|
1044 |
|
---|
1045 | RTCritSectLeave(&mWaitEventCritSect);
|
---|
1046 | }
|
---|
1047 | if (RT_SUCCESS(rc))
|
---|
1048 | {
|
---|
1049 | *ppEvent = pEvent;
|
---|
1050 | return rc;
|
---|
1051 | }
|
---|
1052 |
|
---|
1053 | if (pEvent)
|
---|
1054 | delete pEvent;
|
---|
1055 |
|
---|
1056 | return rc;
|
---|
1057 | }
|
---|
1058 |
|
---|
1059 | /**
|
---|
1060 | * Signals all wait events of a specific type (if found)
|
---|
1061 | * and notifies external events accordingly.
|
---|
1062 | *
|
---|
1063 | * @returns VBox status code.
|
---|
1064 | * @param aType Event type to signal.
|
---|
1065 | * @param aEvent Which external event to notify.
|
---|
1066 | */
|
---|
1067 | int GuestBase::signalWaitEvent(VBoxEventType_T aType, IEvent *aEvent)
|
---|
1068 | {
|
---|
1069 | int rc = RTCritSectEnter(&mWaitEventCritSect);
|
---|
1070 | #ifdef DEBUG
|
---|
1071 | uint32_t cEvents = 0;
|
---|
1072 | #endif
|
---|
1073 | if (RT_SUCCESS(rc))
|
---|
1074 | {
|
---|
1075 | GuestEventGroup::iterator itGroup = mWaitEventGroups.find(aType);
|
---|
1076 | if (itGroup != mWaitEventGroups.end())
|
---|
1077 | {
|
---|
1078 | /* Signal all events in the group, leaving the group empty afterwards. */
|
---|
1079 | GuestWaitEvents::iterator ItWaitEvt;
|
---|
1080 | while ((ItWaitEvt = itGroup->second.begin()) != itGroup->second.end())
|
---|
1081 | {
|
---|
1082 | LogFlowThisFunc(("Signalling event=%p, type=%ld (CID %#x: Session=%RU32, Object=%RU32, Count=%RU32) ...\n",
|
---|
1083 | ItWaitEvt->second, aType, ItWaitEvt->first, VBOX_GUESTCTRL_CONTEXTID_GET_SESSION(ItWaitEvt->first),
|
---|
1084 | VBOX_GUESTCTRL_CONTEXTID_GET_OBJECT(ItWaitEvt->first), VBOX_GUESTCTRL_CONTEXTID_GET_COUNT(ItWaitEvt->first)));
|
---|
1085 |
|
---|
1086 | int rc2 = ItWaitEvt->second->SignalExternal(aEvent);
|
---|
1087 | AssertRC(rc2);
|
---|
1088 |
|
---|
1089 | /* Take down the wait event object details before we erase it from this list and invalid ItGrpEvt. */
|
---|
1090 | const GuestEventTypes &EvtTypes = ItWaitEvt->second->Types();
|
---|
1091 | uint32_t idContext = ItWaitEvt->first;
|
---|
1092 | itGroup->second.erase(ItWaitEvt);
|
---|
1093 |
|
---|
1094 | for (GuestEventTypes::const_iterator ItType = EvtTypes.begin(); ItType != EvtTypes.end(); ++ItType)
|
---|
1095 | {
|
---|
1096 | GuestEventGroup::iterator EvtTypeGrp = mWaitEventGroups.find(*ItType);
|
---|
1097 | if (EvtTypeGrp != mWaitEventGroups.end())
|
---|
1098 | {
|
---|
1099 | ItWaitEvt = EvtTypeGrp->second.find(idContext);
|
---|
1100 | if (ItWaitEvt != EvtTypeGrp->second.end())
|
---|
1101 | {
|
---|
1102 | LogFlowThisFunc(("Removing event %p (CID %#x) from type %d group\n", ItWaitEvt->second, idContext, *ItType));
|
---|
1103 | EvtTypeGrp->second.erase(ItWaitEvt);
|
---|
1104 | LogFlowThisFunc(("%zu events left for type %d\n", EvtTypeGrp->second.size(), *ItType));
|
---|
1105 | Assert(EvtTypeGrp->second.find(idContext) == EvtTypeGrp->second.end()); /* no duplicates */
|
---|
1106 | }
|
---|
1107 | }
|
---|
1108 | }
|
---|
1109 | }
|
---|
1110 | }
|
---|
1111 |
|
---|
1112 | int rc2 = RTCritSectLeave(&mWaitEventCritSect);
|
---|
1113 | if (RT_SUCCESS(rc))
|
---|
1114 | rc = rc2;
|
---|
1115 | }
|
---|
1116 |
|
---|
1117 | #ifdef DEBUG
|
---|
1118 | LogFlowThisFunc(("Signalled %RU32 events, rc=%Rrc\n", cEvents, rc));
|
---|
1119 | #endif
|
---|
1120 | return rc;
|
---|
1121 | }
|
---|
1122 |
|
---|
1123 | /**
|
---|
1124 | * Signals a wait event which is registered to a specific callback (bound to a context ID (CID)).
|
---|
1125 | *
|
---|
1126 | * @returns VBox status code.
|
---|
1127 | * @param pCbCtx Pointer to host service callback context.
|
---|
1128 | * @param rcGuest Guest return code (rc) to set additionally, if rc is set to VERR_GSTCTL_GUEST_ERROR.
|
---|
1129 | * @param pPayload Additional wait event payload data set set on return. Optional.
|
---|
1130 | */
|
---|
1131 | int GuestBase::signalWaitEventInternal(PVBOXGUESTCTRLHOSTCBCTX pCbCtx,
|
---|
1132 | int rcGuest, const GuestWaitEventPayload *pPayload)
|
---|
1133 | {
|
---|
1134 | if (RT_SUCCESS(rcGuest))
|
---|
1135 | return signalWaitEventInternalEx(pCbCtx, VINF_SUCCESS,
|
---|
1136 | 0 /* Guest rc */, pPayload);
|
---|
1137 |
|
---|
1138 | return signalWaitEventInternalEx(pCbCtx, VERR_GSTCTL_GUEST_ERROR,
|
---|
1139 | rcGuest, pPayload);
|
---|
1140 | }
|
---|
1141 |
|
---|
1142 | /**
|
---|
1143 | * Signals a wait event which is registered to a specific callback (bound to a context ID (CID)).
|
---|
1144 | * Extended version.
|
---|
1145 | *
|
---|
1146 | * @returns VBox status code.
|
---|
1147 | * @param pCbCtx Pointer to host service callback context.
|
---|
1148 | * @param rc Return code (rc) to set as wait result.
|
---|
1149 | * @param rcGuest Guest return code (rc) to set additionally, if rc is set to VERR_GSTCTL_GUEST_ERROR.
|
---|
1150 | * @param pPayload Additional wait event payload data set set on return. Optional.
|
---|
1151 | */
|
---|
1152 | int GuestBase::signalWaitEventInternalEx(PVBOXGUESTCTRLHOSTCBCTX pCbCtx,
|
---|
1153 | int rc, int rcGuest,
|
---|
1154 | const GuestWaitEventPayload *pPayload)
|
---|
1155 | {
|
---|
1156 | AssertPtrReturn(pCbCtx, VERR_INVALID_POINTER);
|
---|
1157 | /* pPayload is optional. */
|
---|
1158 |
|
---|
1159 | int rc2 = RTCritSectEnter(&mWaitEventCritSect);
|
---|
1160 | if (RT_SUCCESS(rc2))
|
---|
1161 | {
|
---|
1162 | GuestWaitEvents::iterator itEvent = mWaitEvents.find(pCbCtx->uContextID);
|
---|
1163 | if (itEvent != mWaitEvents.end())
|
---|
1164 | {
|
---|
1165 | LogFlowThisFunc(("Signalling event=%p (CID %RU32, rc=%Rrc, rcGuest=%Rrc, pPayload=%p) ...\n",
|
---|
1166 | itEvent->second, itEvent->first, rc, rcGuest, pPayload));
|
---|
1167 | GuestWaitEvent *pEvent = itEvent->second;
|
---|
1168 | AssertPtr(pEvent);
|
---|
1169 | rc2 = pEvent->SignalInternal(rc, rcGuest, pPayload);
|
---|
1170 | }
|
---|
1171 | else
|
---|
1172 | rc2 = VERR_NOT_FOUND;
|
---|
1173 |
|
---|
1174 | int rc3 = RTCritSectLeave(&mWaitEventCritSect);
|
---|
1175 | if (RT_SUCCESS(rc2))
|
---|
1176 | rc2 = rc3;
|
---|
1177 | }
|
---|
1178 |
|
---|
1179 | return rc2;
|
---|
1180 | }
|
---|
1181 |
|
---|
1182 | /**
|
---|
1183 | * Unregisters (deletes) a wait event.
|
---|
1184 | *
|
---|
1185 | * After successful unregistration the event will not be valid anymore.
|
---|
1186 | *
|
---|
1187 | * @returns VBox status code.
|
---|
1188 | * @param pWaitEvt Wait event to unregister (delete).
|
---|
1189 | */
|
---|
1190 | int GuestBase::unregisterWaitEvent(GuestWaitEvent *pWaitEvt)
|
---|
1191 | {
|
---|
1192 | if (!pWaitEvt) /* Nothing to unregister. */
|
---|
1193 | return VINF_SUCCESS;
|
---|
1194 |
|
---|
1195 | int rc = RTCritSectEnter(&mWaitEventCritSect);
|
---|
1196 | if (RT_SUCCESS(rc))
|
---|
1197 | {
|
---|
1198 | LogFlowThisFunc(("pWaitEvt=%p\n", pWaitEvt));
|
---|
1199 |
|
---|
1200 | /** @todo r=bird: One way of optimizing this would be to use the pointer
|
---|
1201 | * instead of the context ID as index into the groups, i.e. revert the value
|
---|
1202 | * pair for the GuestWaitEvents type.
|
---|
1203 | *
|
---|
1204 | * An even more efficent way, would be to not use sexy std::xxx containers for
|
---|
1205 | * the types, but iprt/list.h, as that would just be a RTListNodeRemove call for
|
---|
1206 | * each type w/o needing to iterate much at all. I.e. add a struct {
|
---|
1207 | * RTLISTNODE, GuestWaitEvent *pSelf} array to GuestWaitEvent, and change
|
---|
1208 | * GuestEventGroup to std::map<VBoxEventType_T, RTListAnchorClass>
|
---|
1209 | * (RTListAnchorClass == RTLISTANCHOR wrapper with a constructor)).
|
---|
1210 | *
|
---|
1211 | * P.S. the try/catch is now longer needed after I changed pWaitEvt->Types() to
|
---|
1212 | * return a const reference rather than a copy of the type list (and it think it
|
---|
1213 | * is safe to assume iterators are not hitting the heap). Copy vs reference is
|
---|
1214 | * an easy mistake to make in C++.
|
---|
1215 | *
|
---|
1216 | * P.P.S. The mWaitEventGroups optimization is probably just a lot of extra work
|
---|
1217 | * with little payoff.
|
---|
1218 | */
|
---|
1219 | try
|
---|
1220 | {
|
---|
1221 | /* Remove the event from all event type groups. */
|
---|
1222 | const GuestEventTypes &lstTypes = pWaitEvt->Types();
|
---|
1223 | for (GuestEventTypes::const_iterator itType = lstTypes.begin();
|
---|
1224 | itType != lstTypes.end(); ++itType)
|
---|
1225 | {
|
---|
1226 | /** @todo Slow O(n) lookup. Optimize this. */
|
---|
1227 | GuestWaitEvents::iterator itCurEvent = mWaitEventGroups[(*itType)].begin();
|
---|
1228 | while (itCurEvent != mWaitEventGroups[(*itType)].end())
|
---|
1229 | {
|
---|
1230 | if (itCurEvent->second == pWaitEvt)
|
---|
1231 | {
|
---|
1232 | mWaitEventGroups[(*itType)].erase(itCurEvent);
|
---|
1233 | break;
|
---|
1234 | }
|
---|
1235 | ++itCurEvent;
|
---|
1236 | }
|
---|
1237 | }
|
---|
1238 |
|
---|
1239 | /* Remove the event from the general event list as well. */
|
---|
1240 | GuestWaitEvents::iterator itEvent = mWaitEvents.find(pWaitEvt->ContextID());
|
---|
1241 |
|
---|
1242 | Assert(itEvent != mWaitEvents.end());
|
---|
1243 | Assert(itEvent->second == pWaitEvt);
|
---|
1244 |
|
---|
1245 | mWaitEvents.erase(itEvent);
|
---|
1246 |
|
---|
1247 | delete pWaitEvt;
|
---|
1248 | pWaitEvt = NULL;
|
---|
1249 | }
|
---|
1250 | catch (const std::exception &ex)
|
---|
1251 | {
|
---|
1252 | RT_NOREF(ex);
|
---|
1253 | AssertFailedStmt(rc = VERR_NOT_FOUND);
|
---|
1254 | }
|
---|
1255 |
|
---|
1256 | int rc2 = RTCritSectLeave(&mWaitEventCritSect);
|
---|
1257 | if (RT_SUCCESS(rc))
|
---|
1258 | rc = rc2;
|
---|
1259 | }
|
---|
1260 |
|
---|
1261 | return rc;
|
---|
1262 | }
|
---|
1263 |
|
---|
1264 | /**
|
---|
1265 | * Waits for an already registered guest wait event.
|
---|
1266 | *
|
---|
1267 | * @return VBox status code.
|
---|
1268 | * @param pWaitEvt Pointer to event to wait for.
|
---|
1269 | * @param msTimeout Timeout (in ms) for waiting.
|
---|
1270 | * @param pType Event type of following IEvent. Optional.
|
---|
1271 | * @param ppEvent Pointer to IEvent which got triggered for this event. Optional.
|
---|
1272 | */
|
---|
1273 | int GuestBase::waitForEvent(GuestWaitEvent *pWaitEvt, uint32_t msTimeout, VBoxEventType_T *pType, IEvent **ppEvent)
|
---|
1274 | {
|
---|
1275 | AssertPtrReturn(pWaitEvt, VERR_INVALID_POINTER);
|
---|
1276 | /* pType is optional. */
|
---|
1277 | /* ppEvent is optional. */
|
---|
1278 |
|
---|
1279 | int vrc = pWaitEvt->Wait(msTimeout);
|
---|
1280 | if (RT_SUCCESS(vrc))
|
---|
1281 | {
|
---|
1282 | const ComPtr<IEvent> pThisEvent = pWaitEvt->Event();
|
---|
1283 | if (pThisEvent.isNotNull()) /* Make sure that we actually have an event associated. */
|
---|
1284 | {
|
---|
1285 | if (pType)
|
---|
1286 | {
|
---|
1287 | HRESULT hr = pThisEvent->COMGETTER(Type)(pType);
|
---|
1288 | if (FAILED(hr))
|
---|
1289 | vrc = VERR_COM_UNEXPECTED;
|
---|
1290 | }
|
---|
1291 | if ( RT_SUCCESS(vrc)
|
---|
1292 | && ppEvent)
|
---|
1293 | pThisEvent.queryInterfaceTo(ppEvent);
|
---|
1294 |
|
---|
1295 | unconst(pThisEvent).setNull();
|
---|
1296 | }
|
---|
1297 | }
|
---|
1298 |
|
---|
1299 | return vrc;
|
---|
1300 | }
|
---|
1301 |
|
---|
1302 | /**
|
---|
1303 | * Converts RTFMODE to FsObjType_T.
|
---|
1304 | *
|
---|
1305 | * @return Converted FsObjType_T type.
|
---|
1306 | * @param fMode RTFMODE to convert.
|
---|
1307 | */
|
---|
1308 | /* static */
|
---|
1309 | FsObjType_T GuestBase::fileModeToFsObjType(RTFMODE fMode)
|
---|
1310 | {
|
---|
1311 | if (RTFS_IS_FILE(fMode)) return FsObjType_File;
|
---|
1312 | else if (RTFS_IS_DIRECTORY(fMode)) return FsObjType_Directory;
|
---|
1313 | else if (RTFS_IS_SYMLINK(fMode)) return FsObjType_Symlink;
|
---|
1314 |
|
---|
1315 | return FsObjType_Unknown;
|
---|
1316 | }
|
---|
1317 |
|
---|
1318 | GuestObject::GuestObject(void)
|
---|
1319 | : mSession(NULL),
|
---|
1320 | mObjectID(0)
|
---|
1321 | {
|
---|
1322 | }
|
---|
1323 |
|
---|
1324 | GuestObject::~GuestObject(void)
|
---|
1325 | {
|
---|
1326 | }
|
---|
1327 |
|
---|
1328 | /**
|
---|
1329 | * Binds this guest (control) object to a specific guest (control) session.
|
---|
1330 | *
|
---|
1331 | * @returns VBox status code.
|
---|
1332 | * @param pConsole Pointer to console object to use.
|
---|
1333 | * @param pSession Pointer to session to bind this object to.
|
---|
1334 | * @param uObjectID Object ID for this object to use within that specific session.
|
---|
1335 | * Each object ID must be unique per session.
|
---|
1336 | */
|
---|
1337 | int GuestObject::bindToSession(Console *pConsole, GuestSession *pSession, uint32_t uObjectID)
|
---|
1338 | {
|
---|
1339 | AssertPtrReturn(pConsole, VERR_INVALID_POINTER);
|
---|
1340 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
1341 |
|
---|
1342 | mConsole = pConsole;
|
---|
1343 | mSession = pSession;
|
---|
1344 | mObjectID = uObjectID;
|
---|
1345 |
|
---|
1346 | return VINF_SUCCESS;
|
---|
1347 | }
|
---|
1348 |
|
---|
1349 | /**
|
---|
1350 | * Registers (creates) a new wait event.
|
---|
1351 | *
|
---|
1352 | * @returns VBox status code.
|
---|
1353 | * @param lstEvents List of events which the new wait event gets triggered at.
|
---|
1354 | * @param ppEvent Returns the new wait event on success.
|
---|
1355 | */
|
---|
1356 | int GuestObject::registerWaitEvent(const GuestEventTypes &lstEvents,
|
---|
1357 | GuestWaitEvent **ppEvent)
|
---|
1358 | {
|
---|
1359 | AssertPtr(mSession);
|
---|
1360 | return GuestBase::registerWaitEventEx(mSession->i_getId(), mObjectID, lstEvents, ppEvent);
|
---|
1361 | }
|
---|
1362 |
|
---|
1363 | /**
|
---|
1364 | * Sends a HGCM message to the guest (via the guest control host service).
|
---|
1365 | *
|
---|
1366 | * @returns VBox status code.
|
---|
1367 | * @param uMessage Message ID of message to send.
|
---|
1368 | * @param cParms Number of HGCM message parameters to send.
|
---|
1369 | * @param paParms Array of HGCM message parameters to send.
|
---|
1370 | */
|
---|
1371 | int GuestObject::sendMessage(uint32_t uMessage, uint32_t cParms, PVBOXHGCMSVCPARM paParms)
|
---|
1372 | {
|
---|
1373 | #ifndef VBOX_GUESTCTRL_TEST_CASE
|
---|
1374 | ComObjPtr<Console> pConsole = mConsole;
|
---|
1375 | Assert(!pConsole.isNull());
|
---|
1376 |
|
---|
1377 | int vrc = VERR_HGCM_SERVICE_NOT_FOUND;
|
---|
1378 |
|
---|
1379 | /* Forward the information to the VMM device. */
|
---|
1380 | VMMDev *pVMMDev = pConsole->i_getVMMDev();
|
---|
1381 | if (pVMMDev)
|
---|
1382 | {
|
---|
1383 | /* HACK ALERT! We extend the first parameter to 64-bit and use the
|
---|
1384 | two topmost bits for call destination information. */
|
---|
1385 | Assert(paParms[0].type == VBOX_HGCM_SVC_PARM_32BIT);
|
---|
1386 | paParms[0].type = VBOX_HGCM_SVC_PARM_64BIT;
|
---|
1387 | paParms[0].u.uint64 = (uint64_t)paParms[0].u.uint32 | VBOX_GUESTCTRL_DST_SESSION;
|
---|
1388 |
|
---|
1389 | /* Make the call. */
|
---|
1390 | LogFlowThisFunc(("uMessage=%RU32, cParms=%RU32\n", uMessage, cParms));
|
---|
1391 | vrc = pVMMDev->hgcmHostCall(HGCMSERVICE_NAME, uMessage, cParms, paParms);
|
---|
1392 | if (RT_FAILURE(vrc))
|
---|
1393 | {
|
---|
1394 | /** @todo What to do here? */
|
---|
1395 | }
|
---|
1396 | }
|
---|
1397 | #else
|
---|
1398 | LogFlowThisFuncEnter();
|
---|
1399 |
|
---|
1400 | /* Not needed within testcases. */
|
---|
1401 | RT_NOREF(uMessage, cParms, paParms);
|
---|
1402 | int vrc = VINF_SUCCESS;
|
---|
1403 | #endif
|
---|
1404 | return vrc;
|
---|
1405 | }
|
---|
1406 |
|
---|
1407 | GuestWaitEventBase::GuestWaitEventBase(void)
|
---|
1408 | : mfAborted(false),
|
---|
1409 | mCID(0),
|
---|
1410 | mEventSem(NIL_RTSEMEVENT),
|
---|
1411 | mRc(VINF_SUCCESS),
|
---|
1412 | mGuestRc(VINF_SUCCESS)
|
---|
1413 | {
|
---|
1414 | }
|
---|
1415 |
|
---|
1416 | GuestWaitEventBase::~GuestWaitEventBase(void)
|
---|
1417 | {
|
---|
1418 | if (mEventSem != NIL_RTSEMEVENT)
|
---|
1419 | {
|
---|
1420 | RTSemEventDestroy(mEventSem);
|
---|
1421 | mEventSem = NIL_RTSEMEVENT;
|
---|
1422 | }
|
---|
1423 | }
|
---|
1424 |
|
---|
1425 | /**
|
---|
1426 | * Initializes a wait event with a specific context ID (CID).
|
---|
1427 | *
|
---|
1428 | * @returns VBox status code.
|
---|
1429 | * @param uCID Context ID (CID) to initialize wait event with.
|
---|
1430 | */
|
---|
1431 | int GuestWaitEventBase::Init(uint32_t uCID)
|
---|
1432 | {
|
---|
1433 | mCID = uCID;
|
---|
1434 |
|
---|
1435 | return RTSemEventCreate(&mEventSem);
|
---|
1436 | }
|
---|
1437 |
|
---|
1438 | /**
|
---|
1439 | * Signals a wait event.
|
---|
1440 | *
|
---|
1441 | * @returns VBox status code.
|
---|
1442 | * @param rc Return code (rc) to set as wait result.
|
---|
1443 | * @param rcGuest Guest return code (rc) to set additionally, if rc is set to VERR_GSTCTL_GUEST_ERROR.
|
---|
1444 | * @param pPayload Additional wait event payload data set set on return. Optional.
|
---|
1445 | */
|
---|
1446 | int GuestWaitEventBase::SignalInternal(int rc, int rcGuest,
|
---|
1447 | const GuestWaitEventPayload *pPayload)
|
---|
1448 | {
|
---|
1449 | if (mfAborted)
|
---|
1450 | return VERR_CANCELLED;
|
---|
1451 |
|
---|
1452 | #ifdef VBOX_STRICT
|
---|
1453 | if (rc == VERR_GSTCTL_GUEST_ERROR)
|
---|
1454 | AssertMsg(RT_FAILURE(rcGuest), ("Guest error indicated but no actual guest error set (%Rrc)\n", rcGuest));
|
---|
1455 | else
|
---|
1456 | AssertMsg(RT_SUCCESS(rcGuest), ("No guest error indicated but actual guest error set (%Rrc)\n", rcGuest));
|
---|
1457 | #endif
|
---|
1458 |
|
---|
1459 | int rc2;
|
---|
1460 | if (pPayload)
|
---|
1461 | rc2 = mPayload.CopyFromDeep(*pPayload);
|
---|
1462 | else
|
---|
1463 | rc2 = VINF_SUCCESS;
|
---|
1464 | if (RT_SUCCESS(rc2))
|
---|
1465 | {
|
---|
1466 | mRc = rc;
|
---|
1467 | mGuestRc = rcGuest;
|
---|
1468 |
|
---|
1469 | rc2 = RTSemEventSignal(mEventSem);
|
---|
1470 | }
|
---|
1471 |
|
---|
1472 | return rc2;
|
---|
1473 | }
|
---|
1474 |
|
---|
1475 | /**
|
---|
1476 | * Waits for the event to get triggered. Will return success if the
|
---|
1477 | * wait was successufl (e.g. was being triggered), otherwise an error will be returned.
|
---|
1478 | *
|
---|
1479 | * @returns VBox status code.
|
---|
1480 | * @param msTimeout Timeout (in ms) to wait.
|
---|
1481 | * Specifiy 0 to wait indefinitely.
|
---|
1482 | */
|
---|
1483 | int GuestWaitEventBase::Wait(RTMSINTERVAL msTimeout)
|
---|
1484 | {
|
---|
1485 | int rc = VINF_SUCCESS;
|
---|
1486 |
|
---|
1487 | if (mfAborted)
|
---|
1488 | rc = VERR_CANCELLED;
|
---|
1489 |
|
---|
1490 | if (RT_SUCCESS(rc))
|
---|
1491 | {
|
---|
1492 | AssertReturn(mEventSem != NIL_RTSEMEVENT, VERR_CANCELLED);
|
---|
1493 |
|
---|
1494 | rc = RTSemEventWait(mEventSem, msTimeout ? msTimeout : RT_INDEFINITE_WAIT);
|
---|
1495 | if ( RT_SUCCESS(rc)
|
---|
1496 | && mfAborted)
|
---|
1497 | {
|
---|
1498 | rc = VERR_CANCELLED;
|
---|
1499 | }
|
---|
1500 |
|
---|
1501 | if (RT_SUCCESS(rc))
|
---|
1502 | {
|
---|
1503 | /* If waiting succeeded, return the overall
|
---|
1504 | * result code. */
|
---|
1505 | rc = mRc;
|
---|
1506 | }
|
---|
1507 | }
|
---|
1508 |
|
---|
1509 | return rc;
|
---|
1510 | }
|
---|
1511 |
|
---|
1512 | GuestWaitEvent::GuestWaitEvent(void)
|
---|
1513 | {
|
---|
1514 | }
|
---|
1515 |
|
---|
1516 | GuestWaitEvent::~GuestWaitEvent(void)
|
---|
1517 | {
|
---|
1518 |
|
---|
1519 | }
|
---|
1520 |
|
---|
1521 | /**
|
---|
1522 | * Cancels the event.
|
---|
1523 | */
|
---|
1524 | int GuestWaitEvent::Cancel(void)
|
---|
1525 | {
|
---|
1526 | if (mfAborted) /* Already aborted? */
|
---|
1527 | return VINF_SUCCESS;
|
---|
1528 |
|
---|
1529 | mfAborted = true;
|
---|
1530 |
|
---|
1531 | #ifdef DEBUG_andy
|
---|
1532 | LogFlowThisFunc(("Cancelling %p ...\n"));
|
---|
1533 | #endif
|
---|
1534 | return RTSemEventSignal(mEventSem);
|
---|
1535 | }
|
---|
1536 |
|
---|
1537 | /**
|
---|
1538 | * Initializes a wait event with a given context ID (CID).
|
---|
1539 | *
|
---|
1540 | * @returns VBox status code.
|
---|
1541 | * @param uCID Context ID to initialize wait event with.
|
---|
1542 | */
|
---|
1543 | int GuestWaitEvent::Init(uint32_t uCID)
|
---|
1544 | {
|
---|
1545 | return GuestWaitEventBase::Init(uCID);
|
---|
1546 | }
|
---|
1547 |
|
---|
1548 | /**
|
---|
1549 | * Initializes a wait event with a given context ID (CID) and a list of event types to wait for.
|
---|
1550 | *
|
---|
1551 | * @returns VBox status code.
|
---|
1552 | * @param uCID Context ID to initialize wait event with.
|
---|
1553 | * @param lstEvents List of event types to wait for this wait event to get signalled.
|
---|
1554 | */
|
---|
1555 | int GuestWaitEvent::Init(uint32_t uCID, const GuestEventTypes &lstEvents)
|
---|
1556 | {
|
---|
1557 | int rc = GuestWaitEventBase::Init(uCID);
|
---|
1558 | if (RT_SUCCESS(rc))
|
---|
1559 | {
|
---|
1560 | mEventTypes = lstEvents;
|
---|
1561 | }
|
---|
1562 |
|
---|
1563 | return rc;
|
---|
1564 | }
|
---|
1565 |
|
---|
1566 | /**
|
---|
1567 | * Signals the event.
|
---|
1568 | *
|
---|
1569 | * @return VBox status code.
|
---|
1570 | * @param pEvent Public IEvent to associate.
|
---|
1571 | * Optional.
|
---|
1572 | */
|
---|
1573 | int GuestWaitEvent::SignalExternal(IEvent *pEvent)
|
---|
1574 | {
|
---|
1575 | if (pEvent)
|
---|
1576 | mEvent = pEvent;
|
---|
1577 |
|
---|
1578 | return RTSemEventSignal(mEventSem);
|
---|
1579 | }
|
---|
1580 |
|
---|