1 | /* $Id: GuestCtrlPrivate.cpp 107684 2025-01-10 16:26:43Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Internal helpers/structures for guest control functionality.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011-2024 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 |
|
---|
29 | /*********************************************************************************************************************************
|
---|
30 | * Header Files *
|
---|
31 | *********************************************************************************************************************************/
|
---|
32 | #define LOG_GROUP LOG_GROUP_GUEST_CONTROL
|
---|
33 | #include "LoggingNew.h"
|
---|
34 |
|
---|
35 | #ifndef VBOX_WITH_GUEST_CONTROL
|
---|
36 | # error "VBOX_WITH_GUEST_CONTROL must defined in this file"
|
---|
37 | #endif
|
---|
38 | #include "GuestCtrlImplPrivate.h"
|
---|
39 | #include "GuestSessionImpl.h"
|
---|
40 | #include "VMMDev.h"
|
---|
41 |
|
---|
42 | #include <iprt/asm.h>
|
---|
43 | #include <iprt/cpp/utils.h> /* For unconst(). */
|
---|
44 | #include <iprt/ctype.h>
|
---|
45 | #ifdef DEBUG
|
---|
46 | # include <iprt/file.h>
|
---|
47 | #endif
|
---|
48 | #include <iprt/fs.h>
|
---|
49 | #include <iprt/path.h>
|
---|
50 | #include <iprt/rand.h>
|
---|
51 | #include <iprt/time.h>
|
---|
52 | #include <VBox/AssertGuest.h>
|
---|
53 |
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Returns a stringyfied error of a guest fs error.
|
---|
57 | *
|
---|
58 | * @returns Stringyfied error.
|
---|
59 | * @param guestErrorInfo Guest error info to get stringyfied error for.
|
---|
60 | */
|
---|
61 | /* static */
|
---|
62 | Utf8Str GuestFs::guestErrorToString(const GuestErrorInfo &guestErrorInfo)
|
---|
63 | {
|
---|
64 | Utf8Str strErr;
|
---|
65 |
|
---|
66 | /** @todo pData->u32Flags: int vs. uint32 -- IPRT errors are *negative* !!! */
|
---|
67 | switch (guestErrorInfo.getVrc())
|
---|
68 | {
|
---|
69 | case VERR_ACCESS_DENIED:
|
---|
70 | strErr.printf(tr("Access to \"%s\" denied"), guestErrorInfo.getWhat().c_str());
|
---|
71 | break;
|
---|
72 |
|
---|
73 | case VERR_FILE_NOT_FOUND: /* This is the most likely error. */
|
---|
74 | RT_FALL_THROUGH();
|
---|
75 | case VERR_PATH_NOT_FOUND:
|
---|
76 | strErr.printf(tr("No such file or directory \"%s\""), guestErrorInfo.getWhat().c_str());
|
---|
77 | break;
|
---|
78 |
|
---|
79 | case VERR_CANT_CREATE:
|
---|
80 | strErr.printf(tr("File or directory \"%s\" can't be created"), guestErrorInfo.getWhat().c_str());
|
---|
81 | break;
|
---|
82 |
|
---|
83 | case VERR_INVALID_PARAMETER:
|
---|
84 | strErr.printf(tr("Invalid parameter specified"));
|
---|
85 | break;
|
---|
86 |
|
---|
87 | case VERR_INVALID_VM_HANDLE:
|
---|
88 | strErr.printf(tr("VMM device is not available (is the VM running?)"));
|
---|
89 | break;
|
---|
90 |
|
---|
91 | case VERR_HGCM_SERVICE_NOT_FOUND:
|
---|
92 | strErr.printf(tr("The guest execution service is not available"));
|
---|
93 | break;
|
---|
94 |
|
---|
95 | case VERR_BAD_EXE_FORMAT:
|
---|
96 | strErr.printf(tr("The file \"%s\" is not an executable format"), guestErrorInfo.getWhat().c_str());
|
---|
97 | break;
|
---|
98 |
|
---|
99 | case VERR_AUTHENTICATION_FAILURE:
|
---|
100 | strErr.printf(tr("The user \"%s\" was not able to logon"), guestErrorInfo.getWhat().c_str());
|
---|
101 | break;
|
---|
102 |
|
---|
103 | case VERR_INVALID_NAME:
|
---|
104 | strErr.printf(tr("The file \"%s\" is an invalid name"), guestErrorInfo.getWhat().c_str());
|
---|
105 | break;
|
---|
106 |
|
---|
107 | case VERR_TIMEOUT:
|
---|
108 | strErr.printf(tr("The guest did not respond within time"));
|
---|
109 | break;
|
---|
110 |
|
---|
111 | case VERR_CANCELLED:
|
---|
112 | strErr.printf(tr("The execution operation was canceled"));
|
---|
113 | break;
|
---|
114 |
|
---|
115 | case VERR_GSTCTL_MAX_CID_OBJECTS_REACHED:
|
---|
116 | strErr.printf(tr("Maximum number of concurrent guest processes has been reached"));
|
---|
117 | break;
|
---|
118 |
|
---|
119 | case VERR_NOT_FOUND:
|
---|
120 | strErr.printf(tr("The guest execution service is not ready (yet)"));
|
---|
121 | break;
|
---|
122 |
|
---|
123 | case VERR_NOT_SUPPORTED:
|
---|
124 | strErr.printf(tr("Specified mode or flag is not supported on the guest"));
|
---|
125 | break;
|
---|
126 |
|
---|
127 | default:
|
---|
128 | strErr.printf(tr("Unhandled error %Rrc for \"%s\" occurred on guest -- please file a bug report"),
|
---|
129 | guestErrorInfo.getVrc(), guestErrorInfo.getWhat().c_str());
|
---|
130 | break;
|
---|
131 | }
|
---|
132 |
|
---|
133 | return strErr;
|
---|
134 | }
|
---|
135 |
|
---|
136 | #ifdef VBOX_WITH_GSTCTL_TOOLBOX_AS_CMDS
|
---|
137 | /**
|
---|
138 | * Sets the file system object data from a given GSTCTLDIRENTRYEX struct.
|
---|
139 | *
|
---|
140 | * @returns VBox status code.
|
---|
141 | * @param pDirEntryEx Pointer to GSTCTLDIRENTRYEX struct to use.
|
---|
142 | * @param strUser Resolved user name owning the object on the guest.
|
---|
143 | * @param strGroups Resolved user group(s) the object on the guest is associated with.
|
---|
144 | * On Windows there can be multiple groups assigned. The groups are separated with ";"
|
---|
145 | * The first group found is always the primary group.
|
---|
146 | */
|
---|
147 | int GuestFsObjData::FromGuestDirEntryEx(PCGSTCTLDIRENTRYEX pDirEntryEx, const Utf8Str &strUser /* = "" */, const Utf8Str &strGroups /* = "" */)
|
---|
148 | {
|
---|
149 | mName = pDirEntryEx->szName;
|
---|
150 |
|
---|
151 | return FromGuestFsObjInfo(&pDirEntryEx->Info, strUser, strGroups);
|
---|
152 | }
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * Sets the file system object data from a given GSTCTLFSOBJINFO struct.
|
---|
156 | *
|
---|
157 | * @returns VBox status code.
|
---|
158 | * @param pFsObjInfo Pointer to GSTCTLFSOBJINFO struct to use.
|
---|
159 | * @param strUser Resolved user name owning the object on the guest.
|
---|
160 | * @param strGroups Resolved user group(s) the object on the guest is associated with.
|
---|
161 | * On Windows there can be multiple groups assigned. The groups are separated with ";"
|
---|
162 | * The first group found is always the primary group.
|
---|
163 | */
|
---|
164 | int GuestFsObjData::FromGuestFsObjInfo(PCGSTCTLFSOBJINFO pFsObjInfo,
|
---|
165 | const Utf8Str &strUser /* = "" */, const Utf8Str &strGroups /* = "" */)
|
---|
166 | {
|
---|
167 | int vrc = VINF_SUCCESS;
|
---|
168 |
|
---|
169 | mType = GuestBase::fileModeToFsObjType(pFsObjInfo->Attr.fMode);
|
---|
170 |
|
---|
171 | mFileAttrs = "";
|
---|
172 | switch (mType)
|
---|
173 | {
|
---|
174 | case FsObjType_File: mFileAttrs += '-'; break;
|
---|
175 | case FsObjType_Directory: mFileAttrs += 'd'; break;
|
---|
176 | case FsObjType_Symlink: mFileAttrs += 'l'; break;
|
---|
177 | case FsObjType_DevChar: mFileAttrs += 'c'; break;
|
---|
178 | case FsObjType_DevBlock: mFileAttrs += 'b'; break;
|
---|
179 | case FsObjType_Fifo: mFileAttrs += 'f'; break;
|
---|
180 | case FsObjType_Socket: mFileAttrs += 's'; break;
|
---|
181 | case FsObjType_WhiteOut: mFileAttrs += 'w'; break;
|
---|
182 | default:
|
---|
183 | mFileAttrs += '?';
|
---|
184 | AssertFailedStmt(vrc = VERR_NOT_SUPPORTED);
|
---|
185 | break;
|
---|
186 | }
|
---|
187 |
|
---|
188 | #define ADD_ATTR(a_Flag, a_Set, a_Clear) \
|
---|
189 | mFileAttrs += pFsObjInfo->Attr.fMode & a_Flag ? a_Set : a_Clear
|
---|
190 |
|
---|
191 | ADD_ATTR(RTFS_UNIX_IRUSR, 'r', '-');
|
---|
192 | ADD_ATTR(RTFS_UNIX_IWUSR, 'w', '-');
|
---|
193 | ADD_ATTR(RTFS_UNIX_IXUSR, 'x', '-');
|
---|
194 |
|
---|
195 | ADD_ATTR(RTFS_UNIX_IRGRP, 'r', '-');
|
---|
196 | ADD_ATTR(RTFS_UNIX_IWGRP, 'w', '-');
|
---|
197 | ADD_ATTR(RTFS_UNIX_IXGRP, 'x', '-');
|
---|
198 |
|
---|
199 | ADD_ATTR(RTFS_UNIX_IROTH, 'r', '-');
|
---|
200 | ADD_ATTR(RTFS_UNIX_IWOTH, 'w', '-');
|
---|
201 | ADD_ATTR(RTFS_UNIX_IXOTH, 'x', '-');
|
---|
202 |
|
---|
203 | /** @todo Implement sticky bits. */
|
---|
204 | mFileAttrs += " "; /* Reserve 3 chars for sticky bits. */
|
---|
205 |
|
---|
206 | mFileAttrs += " "; /* Separator. */
|
---|
207 |
|
---|
208 | ADD_ATTR(RTFS_DOS_READONLY , 'R', '-');
|
---|
209 | ADD_ATTR(RTFS_DOS_HIDDEN , 'H', '-');
|
---|
210 | ADD_ATTR(RTFS_DOS_SYSTEM , 'S', '-');
|
---|
211 | ADD_ATTR(RTFS_DOS_DIRECTORY , 'D', '-');
|
---|
212 | ADD_ATTR(RTFS_DOS_ARCHIVED , 'A', '-');
|
---|
213 | ADD_ATTR(RTFS_DOS_NT_DEVICE , 'd', '-');
|
---|
214 | ADD_ATTR(RTFS_DOS_NT_NORMAL , 'N', '-');
|
---|
215 | ADD_ATTR(RTFS_DOS_NT_TEMPORARY , 'T', '-');
|
---|
216 | ADD_ATTR(RTFS_DOS_NT_SPARSE_FILE , 'P', '-');
|
---|
217 | ADD_ATTR(RTFS_DOS_NT_REPARSE_POINT , 'J', '-');
|
---|
218 | ADD_ATTR(RTFS_DOS_NT_COMPRESSED , 'C', '-');
|
---|
219 | ADD_ATTR(RTFS_DOS_NT_OFFLINE , 'O', '-');
|
---|
220 | ADD_ATTR(RTFS_DOS_NT_NOT_CONTENT_INDEXED, 'I', '-');
|
---|
221 | ADD_ATTR(RTFS_DOS_NT_ENCRYPTED , 'E', '-');
|
---|
222 |
|
---|
223 | #undef ADD_ATTR
|
---|
224 |
|
---|
225 | mObjectSize = pFsObjInfo->cbObject;
|
---|
226 | mAllocatedSize = pFsObjInfo->cbAllocated;
|
---|
227 | mAccessTime = pFsObjInfo->AccessTime.i64NanosecondsRelativeToUnixEpoch;
|
---|
228 | mBirthTime = pFsObjInfo->BirthTime.i64NanosecondsRelativeToUnixEpoch;
|
---|
229 | mChangeTime = pFsObjInfo->ChangeTime.i64NanosecondsRelativeToUnixEpoch;
|
---|
230 | mModificationTime = pFsObjInfo->ModificationTime.i64NanosecondsRelativeToUnixEpoch;
|
---|
231 | mUserName = strUser;
|
---|
232 | mUID = pFsObjInfo->Attr.u.Unix.uid;
|
---|
233 | mGID = pFsObjInfo->Attr.u.Unix.gid;
|
---|
234 | mGroupName = strGroups; /** @todo Separate multiple group. */
|
---|
235 | mNumHardLinks = pFsObjInfo->Attr.u.Unix.cHardlinks;
|
---|
236 | mNodeIDDevice = pFsObjInfo->Attr.u.Unix.INodeIdDevice;
|
---|
237 | mNodeID = pFsObjInfo->Attr.u.Unix.INodeId;
|
---|
238 | mDeviceNumber = RTFS_IS_DEV_BLOCK(pFsObjInfo->Attr.fMode)
|
---|
239 | || RTFS_IS_DEV_CHAR (pFsObjInfo->Attr.fMode) ? pFsObjInfo->Attr.u.Unix.Device : 0;
|
---|
240 | mGenerationID = pFsObjInfo->Attr.u.Unix.GenerationId;
|
---|
241 | mUserFlags = 0;
|
---|
242 |
|
---|
243 | mACL = ""; /** @todo Implement ACL handling. */
|
---|
244 |
|
---|
245 | return vrc;
|
---|
246 | }
|
---|
247 | #endif /* VBOX_WITH_GSTCTL_TOOLBOX_AS_CMDS */
|
---|
248 |
|
---|
249 | #ifdef VBOX_WITH_GSTCTL_TOOLBOX_SUPPORT
|
---|
250 | /**
|
---|
251 | * Extracts the timespec from a given stream block key.
|
---|
252 | *
|
---|
253 | * @return Pointer to handed-in timespec, or NULL if invalid / not found.
|
---|
254 | * @param strmBlk Stream block to extract timespec from.
|
---|
255 | * @param strKey Key to get timespec for.
|
---|
256 | * @param pTimeSpec Where to store the extracted timespec.
|
---|
257 | */
|
---|
258 | /* static */
|
---|
259 | PRTTIMESPEC GuestFsObjData::TimeSpecFromKey(const GuestToolboxStreamBlock &strmBlk, const Utf8Str &strKey, PRTTIMESPEC pTimeSpec)
|
---|
260 | {
|
---|
261 | AssertPtrReturn(pTimeSpec, NULL);
|
---|
262 |
|
---|
263 | Utf8Str strTime = strmBlk.GetString(strKey.c_str());
|
---|
264 | if (strTime.isEmpty())
|
---|
265 | return NULL;
|
---|
266 |
|
---|
267 | if (!RTTimeSpecFromString(pTimeSpec, strTime.c_str()))
|
---|
268 | return NULL;
|
---|
269 |
|
---|
270 | return pTimeSpec;
|
---|
271 | }
|
---|
272 |
|
---|
273 | /**
|
---|
274 | * Extracts the nanoseconds relative from Unix epoch for a given stream block key.
|
---|
275 | *
|
---|
276 | * @return Nanoseconds relative from Unix epoch, or 0 if invalid / not found.
|
---|
277 | * @param strmBlk Stream block to extract nanoseconds from.
|
---|
278 | * @param strKey Key to get nanoseconds for.
|
---|
279 | */
|
---|
280 | /* static */
|
---|
281 | int64_t GuestFsObjData::UnixEpochNsFromKey(const GuestToolboxStreamBlock &strmBlk, const Utf8Str &strKey)
|
---|
282 | {
|
---|
283 | RTTIMESPEC TimeSpec;
|
---|
284 | if (!GuestFsObjData::TimeSpecFromKey(strmBlk, strKey, &TimeSpec))
|
---|
285 | return 0;
|
---|
286 |
|
---|
287 | return TimeSpec.i64NanosecondsRelativeToUnixEpoch;
|
---|
288 | }
|
---|
289 |
|
---|
290 | /**
|
---|
291 | * Initializes this object data with a stream block from VBOXSERVICE_TOOL_LS.
|
---|
292 | *
|
---|
293 | * This is also used by FromStat since the output should be identical given that
|
---|
294 | * they use the same output function on the guest side when fLong is true.
|
---|
295 | *
|
---|
296 | * @return VBox status code.
|
---|
297 | * @param strmBlk Stream block to use for initialization.
|
---|
298 | * @param fLong Whether the stream block contains long (detailed) information or not.
|
---|
299 | */
|
---|
300 | int GuestFsObjData::FromToolboxLs(const GuestToolboxStreamBlock &strmBlk, bool fLong)
|
---|
301 | {
|
---|
302 | LogFlowFunc(("\n"));
|
---|
303 | #ifdef DEBUG
|
---|
304 | strmBlk.DumpToLog();
|
---|
305 | #endif
|
---|
306 |
|
---|
307 | /* Object name. */
|
---|
308 | mName = strmBlk.GetString("name");
|
---|
309 | ASSERT_GUEST_RETURN(mName.isNotEmpty(), VERR_NOT_FOUND);
|
---|
310 |
|
---|
311 | /* Type & attributes. */
|
---|
312 | bool fHaveAttribs = false;
|
---|
313 | char szAttribs[32];
|
---|
314 | memset(szAttribs, '?', sizeof(szAttribs) - 1);
|
---|
315 | mType = FsObjType_Unknown;
|
---|
316 | const char *psz = strmBlk.GetString("ftype");
|
---|
317 | if (psz)
|
---|
318 | {
|
---|
319 | fHaveAttribs = true;
|
---|
320 | szAttribs[0] = *psz;
|
---|
321 | switch (*psz)
|
---|
322 | {
|
---|
323 | case '-': mType = FsObjType_File; break;
|
---|
324 | case 'd': mType = FsObjType_Directory; break;
|
---|
325 | case 'l': mType = FsObjType_Symlink; break;
|
---|
326 | case 'c': mType = FsObjType_DevChar; break;
|
---|
327 | case 'b': mType = FsObjType_DevBlock; break;
|
---|
328 | case 'f': mType = FsObjType_Fifo; break;
|
---|
329 | case 's': mType = FsObjType_Socket; break;
|
---|
330 | case 'w': mType = FsObjType_WhiteOut; break;
|
---|
331 | default:
|
---|
332 | AssertMsgFailed(("%s\n", psz));
|
---|
333 | szAttribs[0] = '?';
|
---|
334 | fHaveAttribs = false;
|
---|
335 | break;
|
---|
336 | }
|
---|
337 | }
|
---|
338 | psz = strmBlk.GetString("owner_mask");
|
---|
339 | if ( psz
|
---|
340 | && (psz[0] == '-' || psz[0] == 'r')
|
---|
341 | && (psz[1] == '-' || psz[1] == 'w')
|
---|
342 | && (psz[2] == '-' || psz[2] == 'x'))
|
---|
343 | {
|
---|
344 | szAttribs[1] = psz[0];
|
---|
345 | szAttribs[2] = psz[1];
|
---|
346 | szAttribs[3] = psz[2];
|
---|
347 | fHaveAttribs = true;
|
---|
348 | }
|
---|
349 | psz = strmBlk.GetString("group_mask");
|
---|
350 | if ( psz
|
---|
351 | && (psz[0] == '-' || psz[0] == 'r')
|
---|
352 | && (psz[1] == '-' || psz[1] == 'w')
|
---|
353 | && (psz[2] == '-' || psz[2] == 'x'))
|
---|
354 | {
|
---|
355 | szAttribs[4] = psz[0];
|
---|
356 | szAttribs[5] = psz[1];
|
---|
357 | szAttribs[6] = psz[2];
|
---|
358 | fHaveAttribs = true;
|
---|
359 | }
|
---|
360 | psz = strmBlk.GetString("other_mask");
|
---|
361 | if ( psz
|
---|
362 | && (psz[0] == '-' || psz[0] == 'r')
|
---|
363 | && (psz[1] == '-' || psz[1] == 'w')
|
---|
364 | && (psz[2] == '-' || psz[2] == 'x'))
|
---|
365 | {
|
---|
366 | szAttribs[7] = psz[0];
|
---|
367 | szAttribs[8] = psz[1];
|
---|
368 | szAttribs[9] = psz[2];
|
---|
369 | fHaveAttribs = true;
|
---|
370 | }
|
---|
371 | szAttribs[10] = ' '; /* Reserve three chars for sticky bits. */
|
---|
372 | szAttribs[11] = ' ';
|
---|
373 | szAttribs[12] = ' ';
|
---|
374 | szAttribs[13] = ' '; /* Separator. */
|
---|
375 | psz = strmBlk.GetString("dos_mask");
|
---|
376 | if ( psz
|
---|
377 | && (psz[ 0] == '-' || psz[ 0] == 'R')
|
---|
378 | && (psz[ 1] == '-' || psz[ 1] == 'H')
|
---|
379 | && (psz[ 2] == '-' || psz[ 2] == 'S')
|
---|
380 | && (psz[ 3] == '-' || psz[ 3] == 'D')
|
---|
381 | && (psz[ 4] == '-' || psz[ 4] == 'A')
|
---|
382 | && (psz[ 5] == '-' || psz[ 5] == 'd')
|
---|
383 | && (psz[ 6] == '-' || psz[ 6] == 'N')
|
---|
384 | && (psz[ 7] == '-' || psz[ 7] == 'T')
|
---|
385 | && (psz[ 8] == '-' || psz[ 8] == 'P')
|
---|
386 | && (psz[ 9] == '-' || psz[ 9] == 'J')
|
---|
387 | && (psz[10] == '-' || psz[10] == 'C')
|
---|
388 | && (psz[11] == '-' || psz[11] == 'O')
|
---|
389 | && (psz[12] == '-' || psz[12] == 'I')
|
---|
390 | && (psz[13] == '-' || psz[13] == 'E'))
|
---|
391 | {
|
---|
392 | memcpy(&szAttribs[14], psz, 14);
|
---|
393 | fHaveAttribs = true;
|
---|
394 | }
|
---|
395 | szAttribs[28] = '\0';
|
---|
396 | if (fHaveAttribs)
|
---|
397 | mFileAttrs = szAttribs;
|
---|
398 |
|
---|
399 | /* Object size. */
|
---|
400 | int vrc = strmBlk.GetInt64Ex("st_size", &mObjectSize);
|
---|
401 | ASSERT_GUEST_RC_RETURN(vrc, vrc);
|
---|
402 | strmBlk.GetInt64Ex("alloc", &mAllocatedSize);
|
---|
403 |
|
---|
404 | /* INode number and device. */
|
---|
405 | psz = strmBlk.GetString("node_id");
|
---|
406 | if (!psz)
|
---|
407 | psz = strmBlk.GetString("cnode_id"); /* copy & past error fixed in 6.0 RC1 */
|
---|
408 | if (psz)
|
---|
409 | mNodeID = RTStrToInt64(psz);
|
---|
410 | mNodeIDDevice = strmBlk.GetUInt32("inode_dev"); /* (Produced by GAs prior to 6.0 RC1.) */
|
---|
411 |
|
---|
412 | if (fLong)
|
---|
413 | {
|
---|
414 | /* Dates. */
|
---|
415 | mAccessTime = GuestFsObjData::UnixEpochNsFromKey(strmBlk, "st_atime");
|
---|
416 | mBirthTime = GuestFsObjData::UnixEpochNsFromKey(strmBlk, "st_birthtime");
|
---|
417 | mChangeTime = GuestFsObjData::UnixEpochNsFromKey(strmBlk, "st_ctime");
|
---|
418 | mModificationTime = GuestFsObjData::UnixEpochNsFromKey(strmBlk, "st_mtime");
|
---|
419 |
|
---|
420 | /* Owner & group. */
|
---|
421 | mUID = strmBlk.GetInt32("uid");
|
---|
422 | psz = strmBlk.GetString("username");
|
---|
423 | if (psz)
|
---|
424 | mUserName = psz;
|
---|
425 | mGID = strmBlk.GetInt32("gid");
|
---|
426 | psz = strmBlk.GetString("groupname");
|
---|
427 | if (psz)
|
---|
428 | mGroupName = psz;
|
---|
429 |
|
---|
430 | /* Misc attributes: */
|
---|
431 | mNumHardLinks = strmBlk.GetUInt32("hlinks", 1);
|
---|
432 | mDeviceNumber = strmBlk.GetUInt32("st_rdev");
|
---|
433 | mGenerationID = strmBlk.GetUInt32("st_gen");
|
---|
434 | mUserFlags = strmBlk.GetUInt32("st_flags");
|
---|
435 |
|
---|
436 | /** @todo ACL */
|
---|
437 | }
|
---|
438 |
|
---|
439 | LogFlowFuncLeave();
|
---|
440 | return VINF_SUCCESS;
|
---|
441 | }
|
---|
442 |
|
---|
443 | /**
|
---|
444 | * Parses stream block output data which came from the 'rm' (vbox_rm)
|
---|
445 | * VBoxService toolbox command. The result will be stored in this object.
|
---|
446 | *
|
---|
447 | * @returns VBox status code.
|
---|
448 | * @param strmBlk Stream block output data to parse.
|
---|
449 | */
|
---|
450 | int GuestFsObjData::FromToolboxRm(const GuestToolboxStreamBlock &strmBlk)
|
---|
451 | {
|
---|
452 | #ifdef DEBUG
|
---|
453 | strmBlk.DumpToLog();
|
---|
454 | #endif
|
---|
455 | /* Object name. */
|
---|
456 | mName = strmBlk.GetString("fname"); /* Note: RTPathRmCmd() only sets this on failure. */
|
---|
457 |
|
---|
458 | /* Return the stream block's vrc. */
|
---|
459 | return strmBlk.GetVrc(true /* fSucceedIfNotFound */);
|
---|
460 | }
|
---|
461 |
|
---|
462 | /**
|
---|
463 | * Parses stream block output data which came from the 'stat' (vbox_stat)
|
---|
464 | * VBoxService toolbox command. The result will be stored in this object.
|
---|
465 | *
|
---|
466 | * @returns VBox status code.
|
---|
467 | * @param strmBlk Stream block output data to parse.
|
---|
468 | */
|
---|
469 | int GuestFsObjData::FromToolboxStat(const GuestToolboxStreamBlock &strmBlk)
|
---|
470 | {
|
---|
471 | /* Should be identical output. */
|
---|
472 | return GuestFsObjData::FromToolboxLs(strmBlk, true /*fLong*/);
|
---|
473 | }
|
---|
474 |
|
---|
475 | /**
|
---|
476 | * Parses stream block output data which came from the 'mktemp' (vbox_mktemp)
|
---|
477 | * VBoxService toolbox command. The result will be stored in this object.
|
---|
478 | *
|
---|
479 | * @returns VBox status code.
|
---|
480 | * @param strmBlk Stream block output data to parse.
|
---|
481 | */
|
---|
482 | int GuestFsObjData::FromToolboxMkTemp(const GuestToolboxStreamBlock &strmBlk)
|
---|
483 | {
|
---|
484 | LogFlowFunc(("\n"));
|
---|
485 |
|
---|
486 | #ifdef DEBUG
|
---|
487 | strmBlk.DumpToLog();
|
---|
488 | #endif
|
---|
489 | /* Object name. */
|
---|
490 | mName = strmBlk.GetString("name");
|
---|
491 | ASSERT_GUEST_RETURN(mName.isNotEmpty(), VERR_NOT_FOUND);
|
---|
492 |
|
---|
493 | /* Assign the stream block's vrc. */
|
---|
494 | int const vrc = strmBlk.GetVrc();
|
---|
495 | LogFlowFuncLeaveRC(vrc);
|
---|
496 | return vrc;
|
---|
497 | }
|
---|
498 |
|
---|
499 | #endif /* VBOX_WITH_GSTCTL_TOOLBOX_SUPPORT */
|
---|
500 |
|
---|
501 | /**
|
---|
502 | * Returns the IPRT-compatible file mode.
|
---|
503 | * Note: Only handling RTFS_TYPE_ flags are implemented for now.
|
---|
504 | *
|
---|
505 | * @return IPRT file mode.
|
---|
506 | */
|
---|
507 | RTFMODE GuestFsObjData::GetFileMode(void) const
|
---|
508 | {
|
---|
509 | RTFMODE fMode = 0;
|
---|
510 |
|
---|
511 | switch (mType)
|
---|
512 | {
|
---|
513 | case FsObjType_Directory:
|
---|
514 | fMode |= RTFS_TYPE_DIRECTORY;
|
---|
515 | break;
|
---|
516 |
|
---|
517 | case FsObjType_File:
|
---|
518 | fMode |= RTFS_TYPE_FILE;
|
---|
519 | break;
|
---|
520 |
|
---|
521 | case FsObjType_Symlink:
|
---|
522 | fMode |= RTFS_TYPE_SYMLINK;
|
---|
523 | break;
|
---|
524 |
|
---|
525 | default:
|
---|
526 | break;
|
---|
527 | }
|
---|
528 |
|
---|
529 | /** @todo Implement more stuff. */
|
---|
530 |
|
---|
531 | return fMode;
|
---|
532 | }
|
---|
533 |
|
---|
534 | ///////////////////////////////////////////////////////////////////////////////
|
---|
535 |
|
---|
536 | GuestProcessOutputStream::GuestProcessOutputStream(void)
|
---|
537 | : m_cbMax(_32M)
|
---|
538 | , m_cbAllocated(0)
|
---|
539 | , m_cbUsed(0)
|
---|
540 | , m_offBuf(0)
|
---|
541 | , m_pbBuffer(NULL) { }
|
---|
542 |
|
---|
543 | GuestProcessOutputStream::~GuestProcessOutputStream(void)
|
---|
544 | {
|
---|
545 | Destroy();
|
---|
546 | }
|
---|
547 |
|
---|
548 | /**
|
---|
549 | * Adds data to the internal parser buffer. Useful if there
|
---|
550 | * are multiple rounds of adding data needed.
|
---|
551 | *
|
---|
552 | * @return VBox status code. Will return VERR_TOO_MUCH_DATA if the buffer's maximum (limit) has been reached.
|
---|
553 | * @param pbData Pointer to data to add.
|
---|
554 | * @param cbData Size (in bytes) of data to add.
|
---|
555 | */
|
---|
556 | int GuestProcessOutputStream::AddData(const BYTE *pbData, size_t cbData)
|
---|
557 | {
|
---|
558 | AssertPtrReturn(pbData, VERR_INVALID_POINTER);
|
---|
559 | AssertReturn(cbData, VERR_INVALID_PARAMETER);
|
---|
560 |
|
---|
561 | int vrc = VINF_SUCCESS;
|
---|
562 |
|
---|
563 | /* Rewind the buffer if it's empty. */
|
---|
564 | size_t cbInBuf = m_cbUsed - m_offBuf;
|
---|
565 | bool const fAddToSet = cbInBuf == 0;
|
---|
566 | if (fAddToSet)
|
---|
567 | m_cbUsed = m_offBuf = 0;
|
---|
568 |
|
---|
569 | /* Try and see if we can simply append the data. */
|
---|
570 | if (cbData + m_cbUsed <= m_cbAllocated)
|
---|
571 | {
|
---|
572 | memcpy(&m_pbBuffer[m_cbUsed], pbData, cbData);
|
---|
573 | m_cbUsed += cbData;
|
---|
574 | }
|
---|
575 | else
|
---|
576 | {
|
---|
577 | /* Move any buffered data to the front. */
|
---|
578 | cbInBuf = m_cbUsed - m_offBuf;
|
---|
579 | if (cbInBuf == 0)
|
---|
580 | m_cbUsed = m_offBuf = 0;
|
---|
581 | else if (m_offBuf) /* Do we have something to move? */
|
---|
582 | {
|
---|
583 | memmove(m_pbBuffer, &m_pbBuffer[m_offBuf], cbInBuf);
|
---|
584 | m_cbUsed = cbInBuf;
|
---|
585 | m_offBuf = 0;
|
---|
586 | }
|
---|
587 |
|
---|
588 | /* Do we need to grow the buffer? */
|
---|
589 | if (cbData + m_cbUsed > m_cbAllocated)
|
---|
590 | {
|
---|
591 | size_t cbAlloc = m_cbUsed + cbData;
|
---|
592 | if (cbAlloc <= m_cbMax)
|
---|
593 | {
|
---|
594 | cbAlloc = RT_ALIGN_Z(cbAlloc, _64K);
|
---|
595 | void *pvNew = RTMemRealloc(m_pbBuffer, cbAlloc);
|
---|
596 | if (pvNew)
|
---|
597 | {
|
---|
598 | m_pbBuffer = (uint8_t *)pvNew;
|
---|
599 | m_cbAllocated = cbAlloc;
|
---|
600 | }
|
---|
601 | else
|
---|
602 | vrc = VERR_NO_MEMORY;
|
---|
603 | }
|
---|
604 | else
|
---|
605 | vrc = VERR_TOO_MUCH_DATA;
|
---|
606 | }
|
---|
607 |
|
---|
608 | /* Finally, copy the data. */
|
---|
609 | if (RT_SUCCESS(vrc))
|
---|
610 | {
|
---|
611 | if (cbData + m_cbUsed <= m_cbAllocated)
|
---|
612 | {
|
---|
613 | memcpy(&m_pbBuffer[m_cbUsed], pbData, cbData);
|
---|
614 | m_cbUsed += cbData;
|
---|
615 | }
|
---|
616 | else
|
---|
617 | vrc = VERR_BUFFER_OVERFLOW;
|
---|
618 | }
|
---|
619 | }
|
---|
620 |
|
---|
621 | return vrc;
|
---|
622 | }
|
---|
623 |
|
---|
624 | /**
|
---|
625 | * Destroys the internal data buffer.
|
---|
626 | */
|
---|
627 | void GuestProcessOutputStream::Destroy(void)
|
---|
628 | {
|
---|
629 | if (m_pbBuffer)
|
---|
630 | {
|
---|
631 | RTMemFree(m_pbBuffer);
|
---|
632 | m_pbBuffer = NULL;
|
---|
633 | }
|
---|
634 |
|
---|
635 | m_cbAllocated = 0;
|
---|
636 | m_cbUsed = 0;
|
---|
637 | m_offBuf = 0;
|
---|
638 | }
|
---|
639 |
|
---|
640 | #ifdef DEBUG
|
---|
641 | /**
|
---|
642 | * Dumps the raw guest process output to a file on the host.
|
---|
643 | * If the file on the host already exists, it will be overwritten.
|
---|
644 | *
|
---|
645 | * @param pszFile Absolute path to host file to dump the output to.
|
---|
646 | */
|
---|
647 | void GuestProcessOutputStream::Dump(const char *pszFile)
|
---|
648 | {
|
---|
649 | LogFlowFunc(("Dumping contents of stream=0x%p (cbAlloc=%u, cbSize=%u, cbOff=%u) to %s\n",
|
---|
650 | m_pbBuffer, m_cbAllocated, m_cbUsed, m_offBuf, pszFile));
|
---|
651 |
|
---|
652 | RTFILE hFile;
|
---|
653 | int vrc = RTFileOpen(&hFile, pszFile, RTFILE_O_CREATE_REPLACE | RTFILE_O_WRITE | RTFILE_O_DENY_WRITE);
|
---|
654 | if (RT_SUCCESS(vrc))
|
---|
655 | {
|
---|
656 | vrc = RTFileWrite(hFile, m_pbBuffer, m_cbUsed, NULL /* pcbWritten */);
|
---|
657 | RTFileClose(hFile);
|
---|
658 | }
|
---|
659 | }
|
---|
660 | #endif /* DEBUG */
|
---|
661 |
|
---|
662 | ///////////////////////////////////////////////////////////////////////////////
|
---|
663 |
|
---|
664 | #ifdef VBOX_WITH_GSTCTL_TOOLBOX_SUPPORT
|
---|
665 | /** @todo *NOT* thread safe yet! */
|
---|
666 | /** @todo Add exception handling for STL stuff! */
|
---|
667 |
|
---|
668 | GuestToolboxStreamBlock::GuestToolboxStreamBlock(void)
|
---|
669 | : m_fComplete(false) { }
|
---|
670 |
|
---|
671 | GuestToolboxStreamBlock::~GuestToolboxStreamBlock()
|
---|
672 | {
|
---|
673 | Clear();
|
---|
674 | }
|
---|
675 |
|
---|
676 | /**
|
---|
677 | * Clears (destroys) the currently stored stream pairs.
|
---|
678 | */
|
---|
679 | void GuestToolboxStreamBlock::Clear(void)
|
---|
680 | {
|
---|
681 | m_fComplete = false;
|
---|
682 | m_mapPairs.clear();
|
---|
683 | }
|
---|
684 |
|
---|
685 | #ifdef DEBUG
|
---|
686 | /**
|
---|
687 | * Dumps the currently stored stream pairs to the (debug) log.
|
---|
688 | */
|
---|
689 | void GuestToolboxStreamBlock::DumpToLog(void) const
|
---|
690 | {
|
---|
691 | LogFlowFunc(("Dumping contents of stream block=0x%p (%ld items, fComplete=%RTbool):\n",
|
---|
692 | this, m_mapPairs.size(), m_fComplete));
|
---|
693 |
|
---|
694 | for (GuestCtrlStreamPairMapIterConst it = m_mapPairs.begin();
|
---|
695 | it != m_mapPairs.end(); ++it)
|
---|
696 | {
|
---|
697 | LogFlowFunc(("\t%s=%s\n", it->first.c_str(), it->second.mValue.c_str()));
|
---|
698 | }
|
---|
699 | }
|
---|
700 | #endif
|
---|
701 |
|
---|
702 | /**
|
---|
703 | * Returns a 64-bit signed integer of a specified key.
|
---|
704 | *
|
---|
705 | * @return VBox status code. VERR_NOT_FOUND if key was not found.
|
---|
706 | * @param pszKey Name of key to get the value for.
|
---|
707 | * @param piVal Pointer to value to return.
|
---|
708 | */
|
---|
709 | int GuestToolboxStreamBlock::GetInt64Ex(const char *pszKey, int64_t *piVal) const
|
---|
710 | {
|
---|
711 | AssertPtrReturn(pszKey, VERR_INVALID_POINTER);
|
---|
712 | AssertPtrReturn(piVal, VERR_INVALID_POINTER);
|
---|
713 | const char *pszValue = GetString(pszKey);
|
---|
714 | if (pszValue)
|
---|
715 | {
|
---|
716 | *piVal = RTStrToInt64(pszValue);
|
---|
717 | return VINF_SUCCESS;
|
---|
718 | }
|
---|
719 | return VERR_NOT_FOUND;
|
---|
720 | }
|
---|
721 |
|
---|
722 | /**
|
---|
723 | * Returns a 64-bit integer of a specified key.
|
---|
724 | *
|
---|
725 | * @return int64_t Value to return, 0 if not found / on failure.
|
---|
726 | * @param pszKey Name of key to get the value for.
|
---|
727 | */
|
---|
728 | int64_t GuestToolboxStreamBlock::GetInt64(const char *pszKey) const
|
---|
729 | {
|
---|
730 | int64_t iVal;
|
---|
731 | if (RT_SUCCESS(GetInt64Ex(pszKey, &iVal)))
|
---|
732 | return iVal;
|
---|
733 | return 0;
|
---|
734 | }
|
---|
735 |
|
---|
736 | /**
|
---|
737 | * Returns the current number of stream pairs.
|
---|
738 | *
|
---|
739 | * @return uint32_t Current number of stream pairs.
|
---|
740 | */
|
---|
741 | size_t GuestToolboxStreamBlock::GetCount(void) const
|
---|
742 | {
|
---|
743 | return m_mapPairs.size();
|
---|
744 | }
|
---|
745 |
|
---|
746 | /**
|
---|
747 | * Gets the return code (name = "rc") of this stream block.
|
---|
748 | *
|
---|
749 | * @return VBox status code.
|
---|
750 | * @retval VERR_NOT_FOUND if the return code string ("rc") was not found.
|
---|
751 | * @param fSucceedIfNotFound When set to @c true, this reports back VINF_SUCCESS when the key ("rc") is not found.
|
---|
752 | * This can happen with some (older) IPRT-provided tools such as RTPathRmCmd(), which only outputs
|
---|
753 | * rc on failure but not on success. Defaults to @c false.
|
---|
754 | */
|
---|
755 | int GuestToolboxStreamBlock::GetVrc(bool fSucceedIfNotFound /* = false */) const
|
---|
756 | {
|
---|
757 | const char *pszValue = GetString("rc");
|
---|
758 | if (pszValue)
|
---|
759 | return RTStrToInt16(pszValue);
|
---|
760 | if (fSucceedIfNotFound)
|
---|
761 | return VINF_SUCCESS;
|
---|
762 | /** @todo We probably should have a dedicated error for that, VERR_GSTCTL_GUEST_TOOLBOX_whatever. */
|
---|
763 | return VERR_NOT_FOUND;
|
---|
764 | }
|
---|
765 |
|
---|
766 | /**
|
---|
767 | * Returns a string value of a specified key.
|
---|
768 | *
|
---|
769 | * @return uint32_t Pointer to string to return, NULL if not found / on failure.
|
---|
770 | * @param pszKey Name of key to get the value for.
|
---|
771 | */
|
---|
772 | const char *GuestToolboxStreamBlock::GetString(const char *pszKey) const
|
---|
773 | {
|
---|
774 | AssertPtrReturn(pszKey, NULL);
|
---|
775 |
|
---|
776 | try
|
---|
777 | {
|
---|
778 | GuestCtrlStreamPairMapIterConst itPairs = m_mapPairs.find(pszKey);
|
---|
779 | if (itPairs != m_mapPairs.end())
|
---|
780 | return itPairs->second.mValue.c_str();
|
---|
781 | }
|
---|
782 | catch (const std::exception &)
|
---|
783 | {
|
---|
784 | }
|
---|
785 | return NULL;
|
---|
786 | }
|
---|
787 |
|
---|
788 | /**
|
---|
789 | * Returns a 32-bit unsigned integer of a specified key.
|
---|
790 | *
|
---|
791 | * @return VBox status code. VERR_NOT_FOUND if key was not found.
|
---|
792 | * @param pszKey Name of key to get the value for.
|
---|
793 | * @param puVal Pointer to value to return.
|
---|
794 | */
|
---|
795 | int GuestToolboxStreamBlock::GetUInt32Ex(const char *pszKey, uint32_t *puVal) const
|
---|
796 | {
|
---|
797 | const char *pszValue = GetString(pszKey);
|
---|
798 | if (pszValue)
|
---|
799 | {
|
---|
800 | *puVal = RTStrToUInt32(pszValue);
|
---|
801 | return VINF_SUCCESS;
|
---|
802 | }
|
---|
803 | return VERR_NOT_FOUND;
|
---|
804 | }
|
---|
805 |
|
---|
806 | /**
|
---|
807 | * Returns a 32-bit signed integer of a specified key.
|
---|
808 | *
|
---|
809 | * @returns 32-bit signed value
|
---|
810 | * @param pszKey Name of key to get the value for.
|
---|
811 | * @param iDefault The default to return on error if not found.
|
---|
812 | */
|
---|
813 | int32_t GuestToolboxStreamBlock::GetInt32(const char *pszKey, int32_t iDefault) const
|
---|
814 | {
|
---|
815 | const char *pszValue = GetString(pszKey);
|
---|
816 | if (pszValue)
|
---|
817 | {
|
---|
818 | int32_t iRet;
|
---|
819 | int vrc = RTStrToInt32Full(pszValue, 0, &iRet);
|
---|
820 | if (RT_SUCCESS(vrc))
|
---|
821 | return iRet;
|
---|
822 | ASSERT_GUEST_MSG_FAILED(("%s=%s\n", pszKey, pszValue));
|
---|
823 | }
|
---|
824 | return iDefault;
|
---|
825 | }
|
---|
826 |
|
---|
827 | /**
|
---|
828 | * Returns a 32-bit unsigned integer of a specified key.
|
---|
829 | *
|
---|
830 | * @return uint32_t Value to return, 0 if not found / on failure.
|
---|
831 | * @param pszKey Name of key to get the value for.
|
---|
832 | * @param uDefault The default value to return.
|
---|
833 | */
|
---|
834 | uint32_t GuestToolboxStreamBlock::GetUInt32(const char *pszKey, uint32_t uDefault /*= 0*/) const
|
---|
835 | {
|
---|
836 | uint32_t uVal;
|
---|
837 | if (RT_SUCCESS(GetUInt32Ex(pszKey, &uVal)))
|
---|
838 | return uVal;
|
---|
839 | return uDefault;
|
---|
840 | }
|
---|
841 |
|
---|
842 | /**
|
---|
843 | * Sets a value to a key or deletes a key by setting a NULL value. Extended version.
|
---|
844 | *
|
---|
845 | * @return VBox status code.
|
---|
846 | * @param pszKey Key name to process.
|
---|
847 | * @param cwcKey Maximum characters of \a pszKey to process.
|
---|
848 | * @param pszValue Value to set. Set NULL for deleting the key.
|
---|
849 | * @param cwcValue Maximum characters of \a pszValue to process.
|
---|
850 | * @param fOverwrite Whether a key can be overwritten with a new value if it already exists. Will assert otherwise.
|
---|
851 | */
|
---|
852 | int GuestToolboxStreamBlock::SetValueEx(const char *pszKey, size_t cwcKey, const char *pszValue, size_t cwcValue,
|
---|
853 | bool fOverwrite /* = false */)
|
---|
854 | {
|
---|
855 | AssertPtrReturn(pszKey, VERR_INVALID_POINTER);
|
---|
856 | AssertReturn(cwcKey, VERR_INVALID_PARAMETER);
|
---|
857 |
|
---|
858 | int vrc = VINF_SUCCESS;
|
---|
859 | try
|
---|
860 | {
|
---|
861 | Utf8Str const strKey(pszKey, cwcKey);
|
---|
862 |
|
---|
863 | /* Take a shortcut and prevent crashes on some funny versions
|
---|
864 | * of STL if map is empty initially. */
|
---|
865 | if (!m_mapPairs.empty())
|
---|
866 | {
|
---|
867 | GuestCtrlStreamPairMapIter it = m_mapPairs.find(strKey);
|
---|
868 | if (it != m_mapPairs.end())
|
---|
869 | {
|
---|
870 | if (pszValue == NULL)
|
---|
871 | m_mapPairs.erase(it);
|
---|
872 | else if (!fOverwrite)
|
---|
873 | AssertMsgFailedReturn(("Key '%*s' already exists! Value is '%s'\n", cwcKey, pszKey, m_mapPairs[strKey].mValue.c_str()),
|
---|
874 | VERR_ALREADY_EXISTS);
|
---|
875 | }
|
---|
876 | }
|
---|
877 |
|
---|
878 | if (pszValue)
|
---|
879 | {
|
---|
880 | GuestToolboxStreamValue val(pszValue, cwcValue);
|
---|
881 | Log3Func(("strKey='%s', strValue='%s'\n", strKey.c_str(), val.mValue.c_str()));
|
---|
882 | m_mapPairs[strKey] = val;
|
---|
883 | }
|
---|
884 | }
|
---|
885 | catch (const std::exception &)
|
---|
886 | {
|
---|
887 | /** @todo set vrc? */
|
---|
888 | }
|
---|
889 | return vrc;
|
---|
890 | }
|
---|
891 |
|
---|
892 | /**
|
---|
893 | * Sets a value to a key or deletes a key by setting a NULL value.
|
---|
894 | *
|
---|
895 | * @return VBox status code.
|
---|
896 | * @param pszKey Key name to process.
|
---|
897 | * @param pszValue Value to set. Set NULL for deleting the key.
|
---|
898 | */
|
---|
899 | int GuestToolboxStreamBlock::SetValue(const char *pszKey, const char *pszValue)
|
---|
900 | {
|
---|
901 | return SetValueEx(pszKey, RTSTR_MAX, pszValue, RTSTR_MAX);
|
---|
902 | }
|
---|
903 |
|
---|
904 | ///////////////////////////////////////////////////////////////////////////////
|
---|
905 |
|
---|
906 | GuestToolboxStream::GuestToolboxStream(void)
|
---|
907 | : m_cBlocks(0)
|
---|
908 | {
|
---|
909 | }
|
---|
910 |
|
---|
911 | GuestToolboxStream::~GuestToolboxStream(void)
|
---|
912 | {
|
---|
913 | }
|
---|
914 |
|
---|
915 | /**
|
---|
916 | * Tries to parse the next upcoming pair block within the internal buffer.
|
---|
917 | *
|
---|
918 | * Parsing behavior:
|
---|
919 | * - A stream can contain one or multiple blocks and is terminated by four (4) "\0".
|
---|
920 | * - A block (or "object") contains one or multiple key=value pairs and is terminated with two (2) "\0".
|
---|
921 | * - Each key=value pair is terminated by a single (1) "\0".
|
---|
922 | *
|
---|
923 | * As new data can arrive at a later time eventually completing a pair / block / stream,
|
---|
924 | * the algorithm needs to be careful not intepreting its current data too early. So only skip termination
|
---|
925 | * sequences if we really know that the termination sequence is complete. See comments down below.
|
---|
926 | *
|
---|
927 | * No locking done.
|
---|
928 | *
|
---|
929 | * @return VBox status code.
|
---|
930 | * @retval VINF_EOF if the stream reached its end.
|
---|
931 | * @param streamBlock Reference to guest stream block to fill
|
---|
932 | */
|
---|
933 | int GuestToolboxStream::ParseBlock(GuestToolboxStreamBlock &streamBlock)
|
---|
934 | {
|
---|
935 | AssertMsgReturn(streamBlock.m_fComplete == false, ("Block object already marked as being completed\n"), VERR_WRONG_ORDER);
|
---|
936 |
|
---|
937 | if ( !m_pbBuffer
|
---|
938 | || !m_cbUsed)
|
---|
939 | return VINF_EOF;
|
---|
940 |
|
---|
941 | AssertReturn(m_offBuf <= m_cbUsed, VERR_INVALID_PARAMETER);
|
---|
942 | if (m_offBuf == m_cbUsed)
|
---|
943 | return VINF_EOF;
|
---|
944 |
|
---|
945 | char * const pszStart = (char *)&m_pbBuffer[m_offBuf];
|
---|
946 |
|
---|
947 | size_t cbLeftParsed = m_offBuf < m_cbUsed ? m_cbUsed - m_offBuf : 0;
|
---|
948 | size_t cbLeftLookAhead = cbLeftParsed;
|
---|
949 |
|
---|
950 | char *pszLookAhead = pszStart; /* Look ahead pointer to count terminators. */
|
---|
951 | char *pszParsed = pszStart; /* Points to data considered as being parsed already. */
|
---|
952 |
|
---|
953 | Log4Func(("Current @ %zu/%zu:\n%.*Rhxd\n", m_offBuf, m_cbUsed, RT_MIN(cbLeftParsed, _1K), pszStart));
|
---|
954 |
|
---|
955 | size_t cTerm = 0;
|
---|
956 |
|
---|
957 | /*
|
---|
958 | * We have to be careful when handling single terminators ('\0') here, as we might not know yet
|
---|
959 | * if it's part of a multi-terminator seqeuence.
|
---|
960 | *
|
---|
961 | * So handle and skip those *only* when we hit a non-terminator char again.
|
---|
962 | */
|
---|
963 | int vrc = VINF_SUCCESS;
|
---|
964 | while (cbLeftLookAhead)
|
---|
965 | {
|
---|
966 | /* Count consequtive terminators. */
|
---|
967 | if (*pszLookAhead == GUESTTOOLBOX_STRM_TERM)
|
---|
968 | {
|
---|
969 | cTerm++;
|
---|
970 | pszLookAhead++;
|
---|
971 | cbLeftLookAhead--;
|
---|
972 | continue;
|
---|
973 | }
|
---|
974 |
|
---|
975 | pszParsed = pszLookAhead;
|
---|
976 | cbLeftParsed = cbLeftLookAhead;
|
---|
977 |
|
---|
978 | /* We hit a non-terminator (again); now interpret where we are, and
|
---|
979 | * bail out if we need to. */
|
---|
980 | if (cTerm >= 2)
|
---|
981 | {
|
---|
982 | Log2Func(("Hit end of termination sequence (%zu)\n", cTerm));
|
---|
983 | break;
|
---|
984 | }
|
---|
985 |
|
---|
986 | cTerm = 0; /* Reset consequtive counter. */
|
---|
987 |
|
---|
988 | char * const pszPairEnd = RTStrEnd(pszParsed, cbLeftParsed);
|
---|
989 | if (!pszPairEnd) /* No zero terminator found (yet), try next time. */
|
---|
990 | break;
|
---|
991 |
|
---|
992 | Log3Func(("Pair '%s' (%u)\n", pszParsed, strlen(pszParsed)));
|
---|
993 |
|
---|
994 | Assert(pszPairEnd != pszParsed);
|
---|
995 | size_t const cbPair = (size_t)(pszPairEnd - pszParsed);
|
---|
996 | Assert(cbPair);
|
---|
997 | const char *pszSep = (const char *)memchr(pszParsed, '=', cbPair);
|
---|
998 | if (!pszSep) /* No separator found (yet), try next time. */
|
---|
999 | break;
|
---|
1000 |
|
---|
1001 | /* Skip the separator so that pszSep points to the actual value. */
|
---|
1002 | pszSep++;
|
---|
1003 |
|
---|
1004 | char const * const pszKey = pszParsed;
|
---|
1005 | char const * const pszVal = pszSep;
|
---|
1006 |
|
---|
1007 | vrc = streamBlock.SetValueEx(pszKey, pszSep - pszKey - 1, pszVal, pszPairEnd - pszVal);
|
---|
1008 | if (RT_FAILURE(vrc))
|
---|
1009 | return vrc;
|
---|
1010 |
|
---|
1011 | if (cbPair >= cbLeftParsed)
|
---|
1012 | break;
|
---|
1013 |
|
---|
1014 | /* Accounting for next iteration. */
|
---|
1015 | pszParsed = pszPairEnd;
|
---|
1016 | Assert(cbLeftParsed >= cbPair);
|
---|
1017 | cbLeftParsed -= cbPair;
|
---|
1018 |
|
---|
1019 | pszLookAhead = pszPairEnd;
|
---|
1020 | cbLeftLookAhead = cbLeftParsed;
|
---|
1021 |
|
---|
1022 | if (cbLeftParsed)
|
---|
1023 | Log4Func(("Next iteration @ %zu:\n%.*Rhxd\n", pszParsed - pszStart, cbLeftParsed, pszParsed));
|
---|
1024 | }
|
---|
1025 |
|
---|
1026 | if (cbLeftParsed)
|
---|
1027 | Log4Func(("Done @ %zu:\n%.*Rhxd\n", pszParsed - pszStart, cbLeftParsed, pszParsed));
|
---|
1028 |
|
---|
1029 | m_offBuf += pszParsed - pszStart; /* Only account really parsed content. */
|
---|
1030 | Assert(m_offBuf <= m_cbUsed);
|
---|
1031 |
|
---|
1032 | /* Did we hit a block or stream termination sequence? */
|
---|
1033 | if (cTerm >= GUESTTOOLBOX_STRM_BLK_TERM_CNT)
|
---|
1034 | {
|
---|
1035 | if (!streamBlock.IsEmpty()) /* Only account and complete blocks which have values in it. */
|
---|
1036 | {
|
---|
1037 | m_cBlocks++;
|
---|
1038 | streamBlock.m_fComplete = true;
|
---|
1039 | #ifdef DEBUG
|
---|
1040 | streamBlock.DumpToLog();
|
---|
1041 | #endif
|
---|
1042 | }
|
---|
1043 |
|
---|
1044 | if (cTerm >= GUESTTOOLBOX_STRM_TERM_CNT)
|
---|
1045 | {
|
---|
1046 | m_offBuf = m_cbUsed;
|
---|
1047 | vrc = VINF_EOF;
|
---|
1048 | }
|
---|
1049 | }
|
---|
1050 |
|
---|
1051 | LogFlowThisFunc(("cbLeft=%zu, offBuffer=%zu / cbUsed=%zu, cBlocks=%zu, cTerm=%zu -> current block has %RU64 pairs (complete = %RTbool), rc=%Rrc\n",
|
---|
1052 | cbLeftParsed, m_offBuf, m_cbUsed, m_cBlocks, cTerm, streamBlock.GetCount(), streamBlock.IsComplete(), vrc));
|
---|
1053 |
|
---|
1054 | return vrc;
|
---|
1055 | }
|
---|
1056 | #endif /* VBOX_WITH_GSTCTL_TOOLBOX_SUPPORT */
|
---|
1057 |
|
---|
1058 | GuestBase::GuestBase(void)
|
---|
1059 | : mConsole(NULL)
|
---|
1060 | , mNextContextID(RTRandU32() % VBOX_GUESTCTRL_MAX_CONTEXTS)
|
---|
1061 | {
|
---|
1062 | }
|
---|
1063 |
|
---|
1064 | GuestBase::~GuestBase(void)
|
---|
1065 | {
|
---|
1066 | }
|
---|
1067 |
|
---|
1068 | /**
|
---|
1069 | * Separate initialization function for the base class.
|
---|
1070 | *
|
---|
1071 | * @returns VBox status code.
|
---|
1072 | */
|
---|
1073 | int GuestBase::baseInit(void)
|
---|
1074 | {
|
---|
1075 | int const vrc = RTCritSectInit(&mWaitEventCritSect);
|
---|
1076 | LogFlowFuncLeaveRC(vrc);
|
---|
1077 | return vrc;
|
---|
1078 | }
|
---|
1079 |
|
---|
1080 | /**
|
---|
1081 | * Separate uninitialization function for the base class.
|
---|
1082 | */
|
---|
1083 | void GuestBase::baseUninit(void)
|
---|
1084 | {
|
---|
1085 | LogFlowThisFuncEnter();
|
---|
1086 |
|
---|
1087 | /* Make sure to cancel any outstanding wait events. */
|
---|
1088 | int vrc2 = cancelWaitEvents();
|
---|
1089 | AssertRC(vrc2);
|
---|
1090 |
|
---|
1091 | vrc2 = RTCritSectDelete(&mWaitEventCritSect);
|
---|
1092 | AssertRC(vrc2);
|
---|
1093 |
|
---|
1094 | LogFlowFuncLeaveRC(vrc2);
|
---|
1095 | /* No return value. */
|
---|
1096 | }
|
---|
1097 |
|
---|
1098 | /**
|
---|
1099 | * Cancels all outstanding wait events.
|
---|
1100 | *
|
---|
1101 | * @returns VBox status code.
|
---|
1102 | */
|
---|
1103 | int GuestBase::cancelWaitEvents(void)
|
---|
1104 | {
|
---|
1105 | LogFlowThisFuncEnter();
|
---|
1106 |
|
---|
1107 | int vrc = RTCritSectEnter(&mWaitEventCritSect);
|
---|
1108 | if (RT_SUCCESS(vrc))
|
---|
1109 | {
|
---|
1110 | GuestEventGroup::iterator itEventGroups = mWaitEventGroups.begin();
|
---|
1111 | while (itEventGroups != mWaitEventGroups.end())
|
---|
1112 | {
|
---|
1113 | GuestWaitEvents::iterator itEvents = itEventGroups->second.begin();
|
---|
1114 | while (itEvents != itEventGroups->second.end())
|
---|
1115 | {
|
---|
1116 | GuestWaitEvent *pEvent = itEvents->second;
|
---|
1117 | AssertPtr(pEvent);
|
---|
1118 |
|
---|
1119 | /*
|
---|
1120 | * Just cancel the event, but don't remove it from the
|
---|
1121 | * wait events map. Don't delete it though, this (hopefully)
|
---|
1122 | * is done by the caller using unregisterWaitEvent().
|
---|
1123 | */
|
---|
1124 | int vrc2 = pEvent->Cancel();
|
---|
1125 | AssertRC(vrc2);
|
---|
1126 |
|
---|
1127 | ++itEvents;
|
---|
1128 | }
|
---|
1129 |
|
---|
1130 | ++itEventGroups;
|
---|
1131 | }
|
---|
1132 |
|
---|
1133 | int vrc2 = RTCritSectLeave(&mWaitEventCritSect);
|
---|
1134 | if (RT_SUCCESS(vrc))
|
---|
1135 | vrc = vrc2;
|
---|
1136 | }
|
---|
1137 |
|
---|
1138 | LogFlowFuncLeaveRC(vrc);
|
---|
1139 | return vrc;
|
---|
1140 | }
|
---|
1141 |
|
---|
1142 | /**
|
---|
1143 | * Handles generic messages not bound to a specific object type.
|
---|
1144 | *
|
---|
1145 | * @return VBox status code. VERR_NOT_FOUND if no handler has been found or VERR_NOT_SUPPORTED
|
---|
1146 | * if this class does not support the specified callback.
|
---|
1147 | * @param pCtxCb Host callback context.
|
---|
1148 | * @param pSvcCb Service callback data.
|
---|
1149 | */
|
---|
1150 | int GuestBase::dispatchGeneric(PVBOXGUESTCTRLHOSTCBCTX pCtxCb, PVBOXGUESTCTRLHOSTCALLBACK pSvcCb)
|
---|
1151 | {
|
---|
1152 | LogFlowFunc(("pCtxCb=%p, pSvcCb=%p\n", pCtxCb, pSvcCb));
|
---|
1153 |
|
---|
1154 | AssertPtrReturn(pCtxCb, VERR_INVALID_POINTER);
|
---|
1155 | AssertPtrReturn(pSvcCb, VERR_INVALID_POINTER);
|
---|
1156 |
|
---|
1157 | int vrc;
|
---|
1158 |
|
---|
1159 | try
|
---|
1160 | {
|
---|
1161 | Log2Func(("uFunc=%RU32, cParms=%RU32\n", pCtxCb->uMessage, pSvcCb->mParms));
|
---|
1162 |
|
---|
1163 | switch (pCtxCb->uMessage)
|
---|
1164 | {
|
---|
1165 | case GUEST_MSG_PROGRESS_UPDATE:
|
---|
1166 | vrc = VINF_SUCCESS;
|
---|
1167 | break;
|
---|
1168 |
|
---|
1169 | case GUEST_MSG_REPLY:
|
---|
1170 | {
|
---|
1171 | if (pSvcCb->mParms >= 4)
|
---|
1172 | {
|
---|
1173 | int idx = 1; /* Current parameter index. */
|
---|
1174 | CALLBACKDATA_MSG_REPLY dataCb;
|
---|
1175 | /* pSvcCb->mpaParms[0] always contains the context ID. */
|
---|
1176 | vrc = HGCMSvcGetU32(&pSvcCb->mpaParms[idx++], &dataCb.uType);
|
---|
1177 | AssertRCReturn(vrc, vrc);
|
---|
1178 | vrc = HGCMSvcGetU32(&pSvcCb->mpaParms[idx++], &dataCb.rc);
|
---|
1179 | AssertRCReturn(vrc, vrc);
|
---|
1180 | vrc = HGCMSvcGetPv(&pSvcCb->mpaParms[idx++], &dataCb.pvPayload, &dataCb.cbPayload);
|
---|
1181 | AssertRCReturn(vrc, vrc);
|
---|
1182 |
|
---|
1183 | try
|
---|
1184 | {
|
---|
1185 | GuestWaitEventPayload evPayload(dataCb.uType, dataCb.pvPayload, dataCb.cbPayload);
|
---|
1186 | vrc = signalWaitEventInternal(pCtxCb, dataCb.rc, &evPayload);
|
---|
1187 | }
|
---|
1188 | catch (int vrcEx) /* Thrown by GuestWaitEventPayload constructor. */
|
---|
1189 | {
|
---|
1190 | vrc = vrcEx;
|
---|
1191 | }
|
---|
1192 | }
|
---|
1193 | else
|
---|
1194 | vrc = VERR_INVALID_PARAMETER;
|
---|
1195 | break;
|
---|
1196 | }
|
---|
1197 |
|
---|
1198 | default:
|
---|
1199 | vrc = VERR_NOT_SUPPORTED;
|
---|
1200 | break;
|
---|
1201 | }
|
---|
1202 | }
|
---|
1203 | catch (std::bad_alloc &)
|
---|
1204 | {
|
---|
1205 | vrc = VERR_NO_MEMORY;
|
---|
1206 | }
|
---|
1207 | catch (int vrcCatch)
|
---|
1208 | {
|
---|
1209 | vrc = vrcCatch;
|
---|
1210 | }
|
---|
1211 |
|
---|
1212 | LogFlowFuncLeaveRC(vrc);
|
---|
1213 | return vrc;
|
---|
1214 | }
|
---|
1215 |
|
---|
1216 | /**
|
---|
1217 | * Generates a context ID (CID) by incrementing the object's count.
|
---|
1218 | * A CID consists of a session ID, an object ID and a count.
|
---|
1219 | *
|
---|
1220 | * Note: This function does not guarantee that the returned CID is unique;
|
---|
1221 | * the caller has to take care of that and eventually retry.
|
---|
1222 | *
|
---|
1223 | * @returns VBox status code.
|
---|
1224 | * @param uSessionID Session ID to use for CID generation.
|
---|
1225 | * @param uObjectID Object ID to use for CID generation.
|
---|
1226 | * @param puContextID Where to store the generated CID on success.
|
---|
1227 | */
|
---|
1228 | int GuestBase::generateContextID(uint32_t uSessionID, uint32_t uObjectID, uint32_t *puContextID)
|
---|
1229 | {
|
---|
1230 | AssertPtrReturn(puContextID, VERR_INVALID_POINTER);
|
---|
1231 |
|
---|
1232 | if ( uSessionID >= VBOX_GUESTCTRL_MAX_SESSIONS
|
---|
1233 | || uObjectID >= VBOX_GUESTCTRL_MAX_OBJECTS)
|
---|
1234 | return VERR_INVALID_PARAMETER;
|
---|
1235 |
|
---|
1236 | uint32_t uCount = ASMAtomicIncU32(&mNextContextID);
|
---|
1237 | uCount %= VBOX_GUESTCTRL_MAX_CONTEXTS;
|
---|
1238 |
|
---|
1239 | uint32_t uNewContextID = VBOX_GUESTCTRL_CONTEXTID_MAKE(uSessionID, uObjectID, uCount);
|
---|
1240 |
|
---|
1241 | *puContextID = uNewContextID;
|
---|
1242 |
|
---|
1243 | #if 0
|
---|
1244 | LogFlowThisFunc(("mNextContextID=%RU32, uSessionID=%RU32, uObjectID=%RU32, uCount=%RU32, uNewContextID=%RU32\n",
|
---|
1245 | mNextContextID, uSessionID, uObjectID, uCount, uNewContextID));
|
---|
1246 | #endif
|
---|
1247 | return VINF_SUCCESS;
|
---|
1248 | }
|
---|
1249 |
|
---|
1250 | /**
|
---|
1251 | * Registers (creates) a new wait event based on a given session and object ID.
|
---|
1252 | *
|
---|
1253 | * From those IDs an unique context ID (CID) will be built, which only can be
|
---|
1254 | * around once at a time.
|
---|
1255 | *
|
---|
1256 | * @returns VBox status code.
|
---|
1257 | * @retval VERR_GSTCTL_MAX_CID_COUNT_REACHED if unable to generate a free context ID (CID, the count part (bits 15:0)).
|
---|
1258 | * @param uSessionID Session ID to register wait event for.
|
---|
1259 | * @param uObjectID Object ID to register wait event for.
|
---|
1260 | * @param ppEvent Pointer to registered (created) wait event on success.
|
---|
1261 | * Must be destroyed with unregisterWaitEvent().
|
---|
1262 | */
|
---|
1263 | int GuestBase::registerWaitEvent(uint32_t uSessionID, uint32_t uObjectID, GuestWaitEvent **ppEvent)
|
---|
1264 | {
|
---|
1265 | GuestEventTypes eventTypesEmpty;
|
---|
1266 | return registerWaitEventEx(uSessionID, uObjectID, eventTypesEmpty, ppEvent);
|
---|
1267 | }
|
---|
1268 |
|
---|
1269 | /**
|
---|
1270 | * Creates and registers a new wait event object that waits on a set of events
|
---|
1271 | * related to a given object within the session.
|
---|
1272 | *
|
---|
1273 | * From the session ID and object ID a one-time unique context ID (CID) is built
|
---|
1274 | * for this wait object. Normally the CID is then passed to the guest along
|
---|
1275 | * with a request, and the guest passed the CID back with the reply. The
|
---|
1276 | * handler for the reply then emits a signal on the event type associated with
|
---|
1277 | * the reply, which includes signalling the object returned by this method and
|
---|
1278 | * the waking up the thread waiting on it.
|
---|
1279 | *
|
---|
1280 | * @returns VBox status code.
|
---|
1281 | * @retval VERR_GSTCTL_MAX_CID_COUNT_REACHED if unable to generate a free context ID (CID, the count part (bits 15:0)).
|
---|
1282 | * @param uSessionID Session ID to register wait event for.
|
---|
1283 | * @param uObjectID Object ID to register wait event for.
|
---|
1284 | * @param lstEvents List of events to register the wait event for.
|
---|
1285 | * @param ppEvent Pointer to registered (created) wait event on success.
|
---|
1286 | * Must be destroyed with unregisterWaitEvent().
|
---|
1287 | */
|
---|
1288 | int GuestBase::registerWaitEventEx(uint32_t uSessionID, uint32_t uObjectID, const GuestEventTypes &lstEvents,
|
---|
1289 | GuestWaitEvent **ppEvent)
|
---|
1290 | {
|
---|
1291 | AssertPtrReturn(ppEvent, VERR_INVALID_POINTER);
|
---|
1292 |
|
---|
1293 | uint32_t idContext;
|
---|
1294 | int vrc = generateContextID(uSessionID, uObjectID, &idContext);
|
---|
1295 | AssertRCReturn(vrc, vrc);
|
---|
1296 |
|
---|
1297 | GuestWaitEvent *pEvent = new GuestWaitEvent();
|
---|
1298 | AssertPtrReturn(pEvent, VERR_NO_MEMORY);
|
---|
1299 |
|
---|
1300 | vrc = pEvent->Init(idContext, lstEvents);
|
---|
1301 | AssertRCReturn(vrc, vrc);
|
---|
1302 |
|
---|
1303 | LogFlowThisFunc(("New event=%p, CID=%RU32\n", pEvent, idContext));
|
---|
1304 |
|
---|
1305 | vrc = RTCritSectEnter(&mWaitEventCritSect);
|
---|
1306 | if (RT_SUCCESS(vrc))
|
---|
1307 | {
|
---|
1308 | /*
|
---|
1309 | * Check that we don't have any context ID collisions (should be very unlikely).
|
---|
1310 | *
|
---|
1311 | * The ASSUMPTION here is that mWaitEvents has all the same events as
|
---|
1312 | * mWaitEventGroups, so it suffices to check one of the two.
|
---|
1313 | */
|
---|
1314 | if (mWaitEvents.find(idContext) != mWaitEvents.end())
|
---|
1315 | {
|
---|
1316 | uint32_t cTries = 0;
|
---|
1317 | do
|
---|
1318 | {
|
---|
1319 | vrc = generateContextID(uSessionID, uObjectID, &idContext);
|
---|
1320 | AssertRCBreak(vrc);
|
---|
1321 | LogFunc(("Found context ID duplicate; trying a different context ID: %#x\n", idContext));
|
---|
1322 | if (mWaitEvents.find(idContext) != mWaitEvents.end())
|
---|
1323 | vrc = VERR_GSTCTL_MAX_CID_COUNT_REACHED;
|
---|
1324 | } while (RT_FAILURE_NP(vrc) && cTries++ < 10);
|
---|
1325 | }
|
---|
1326 | if (RT_SUCCESS(vrc))
|
---|
1327 | {
|
---|
1328 | /*
|
---|
1329 | * Insert event into matching event group. This is for faster per-group lookup of all events later.
|
---|
1330 | */
|
---|
1331 | uint32_t cInserts = 0;
|
---|
1332 | for (GuestEventTypes::const_iterator ItType = lstEvents.begin(); ItType != lstEvents.end(); ++ItType)
|
---|
1333 | {
|
---|
1334 | GuestWaitEvents &eventGroup = mWaitEventGroups[*ItType];
|
---|
1335 | if (eventGroup.find(idContext) == eventGroup.end())
|
---|
1336 | {
|
---|
1337 | try
|
---|
1338 | {
|
---|
1339 | eventGroup.insert(std::pair<uint32_t, GuestWaitEvent *>(idContext, pEvent));
|
---|
1340 | cInserts++;
|
---|
1341 | }
|
---|
1342 | catch (std::bad_alloc &)
|
---|
1343 | {
|
---|
1344 | while (ItType != lstEvents.begin())
|
---|
1345 | {
|
---|
1346 | --ItType;
|
---|
1347 | mWaitEventGroups[*ItType].erase(idContext);
|
---|
1348 | }
|
---|
1349 | vrc = VERR_NO_MEMORY;
|
---|
1350 | break;
|
---|
1351 | }
|
---|
1352 | }
|
---|
1353 | else
|
---|
1354 | Assert(cInserts > 0); /* else: lstEvents has duplicate entries. */
|
---|
1355 | }
|
---|
1356 | if (RT_SUCCESS(vrc))
|
---|
1357 | {
|
---|
1358 | Assert(cInserts > 0 || lstEvents.size() == 0);
|
---|
1359 | RT_NOREF(cInserts);
|
---|
1360 |
|
---|
1361 | /*
|
---|
1362 | * Register event in the regular event list.
|
---|
1363 | */
|
---|
1364 | try
|
---|
1365 | {
|
---|
1366 | mWaitEvents[idContext] = pEvent;
|
---|
1367 | }
|
---|
1368 | catch (std::bad_alloc &)
|
---|
1369 | {
|
---|
1370 | for (GuestEventTypes::const_iterator ItType = lstEvents.begin(); ItType != lstEvents.end(); ++ItType)
|
---|
1371 | mWaitEventGroups[*ItType].erase(idContext);
|
---|
1372 | vrc = VERR_NO_MEMORY;
|
---|
1373 | }
|
---|
1374 | }
|
---|
1375 | }
|
---|
1376 |
|
---|
1377 | RTCritSectLeave(&mWaitEventCritSect);
|
---|
1378 | }
|
---|
1379 | if (RT_SUCCESS(vrc))
|
---|
1380 | {
|
---|
1381 | *ppEvent = pEvent;
|
---|
1382 | return vrc;
|
---|
1383 | }
|
---|
1384 |
|
---|
1385 | if (pEvent)
|
---|
1386 | delete pEvent;
|
---|
1387 |
|
---|
1388 | return vrc;
|
---|
1389 | }
|
---|
1390 |
|
---|
1391 | /**
|
---|
1392 | * Signals all wait events of a specific type (if found)
|
---|
1393 | * and notifies external events accordingly.
|
---|
1394 | *
|
---|
1395 | * @returns VBox status code.
|
---|
1396 | * @param aType Event type to signal.
|
---|
1397 | * @param aEvent Which external event to notify.
|
---|
1398 | */
|
---|
1399 | int GuestBase::signalWaitEvent(VBoxEventType_T aType, IEvent *aEvent)
|
---|
1400 | {
|
---|
1401 | int vrc = RTCritSectEnter(&mWaitEventCritSect);
|
---|
1402 | #ifdef DEBUG
|
---|
1403 | uint32_t cEvents = 0;
|
---|
1404 | #endif
|
---|
1405 | if (RT_SUCCESS(vrc))
|
---|
1406 | {
|
---|
1407 | GuestEventGroup::iterator itGroup = mWaitEventGroups.find(aType);
|
---|
1408 | if (itGroup != mWaitEventGroups.end())
|
---|
1409 | {
|
---|
1410 | /* Signal all events in the group, leaving the group empty afterwards. */
|
---|
1411 | GuestWaitEvents::iterator ItWaitEvt;
|
---|
1412 | while ((ItWaitEvt = itGroup->second.begin()) != itGroup->second.end())
|
---|
1413 | {
|
---|
1414 | LogFlowThisFunc(("Signalling event=%p, type=%ld (CID %#x: Session=%RU32, Object=%RU32, Count=%RU32) ...\n",
|
---|
1415 | ItWaitEvt->second, aType, ItWaitEvt->first, VBOX_GUESTCTRL_CONTEXTID_GET_SESSION(ItWaitEvt->first),
|
---|
1416 | VBOX_GUESTCTRL_CONTEXTID_GET_OBJECT(ItWaitEvt->first), VBOX_GUESTCTRL_CONTEXTID_GET_COUNT(ItWaitEvt->first)));
|
---|
1417 |
|
---|
1418 | int vrc2 = ItWaitEvt->second->SignalExternal(aEvent);
|
---|
1419 | AssertRC(vrc2);
|
---|
1420 |
|
---|
1421 | /* Take down the wait event object details before we erase it from this list and invalid ItGrpEvt. */
|
---|
1422 | const GuestEventTypes &EvtTypes = ItWaitEvt->second->Types();
|
---|
1423 | uint32_t idContext = ItWaitEvt->first;
|
---|
1424 | itGroup->second.erase(ItWaitEvt);
|
---|
1425 |
|
---|
1426 | for (GuestEventTypes::const_iterator ItType = EvtTypes.begin(); ItType != EvtTypes.end(); ++ItType)
|
---|
1427 | {
|
---|
1428 | GuestEventGroup::iterator EvtTypeGrp = mWaitEventGroups.find(*ItType);
|
---|
1429 | if (EvtTypeGrp != mWaitEventGroups.end())
|
---|
1430 | {
|
---|
1431 | ItWaitEvt = EvtTypeGrp->second.find(idContext);
|
---|
1432 | if (ItWaitEvt != EvtTypeGrp->second.end())
|
---|
1433 | {
|
---|
1434 | LogFlowThisFunc(("Removing event %p (CID %#x) from type %d group\n", ItWaitEvt->second, idContext, *ItType));
|
---|
1435 | EvtTypeGrp->second.erase(ItWaitEvt);
|
---|
1436 | LogFlowThisFunc(("%zu events left for type %d\n", EvtTypeGrp->second.size(), *ItType));
|
---|
1437 | Assert(EvtTypeGrp->second.find(idContext) == EvtTypeGrp->second.end()); /* no duplicates */
|
---|
1438 | }
|
---|
1439 | }
|
---|
1440 | }
|
---|
1441 | }
|
---|
1442 | }
|
---|
1443 |
|
---|
1444 | int vrc2 = RTCritSectLeave(&mWaitEventCritSect);
|
---|
1445 | if (RT_SUCCESS(vrc))
|
---|
1446 | vrc = vrc2;
|
---|
1447 | }
|
---|
1448 |
|
---|
1449 | #ifdef DEBUG
|
---|
1450 | LogFlowThisFunc(("Signalled %RU32 events, vrc=%Rrc\n", cEvents, vrc));
|
---|
1451 | #endif
|
---|
1452 | return vrc;
|
---|
1453 | }
|
---|
1454 |
|
---|
1455 | /**
|
---|
1456 | * Signals a wait event which is registered to a specific callback (bound to a context ID (CID)).
|
---|
1457 | *
|
---|
1458 | * @returns VBox status code.
|
---|
1459 | * @param pCbCtx Pointer to host service callback context.
|
---|
1460 | * @param vrcGuest Guest return VBox status code to set.
|
---|
1461 | * @param pPayload Additional wait event payload data set set on return. Optional.
|
---|
1462 | */
|
---|
1463 | int GuestBase::signalWaitEventInternal(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, int vrcGuest, const GuestWaitEventPayload *pPayload)
|
---|
1464 | {
|
---|
1465 | if (RT_SUCCESS(vrcGuest))
|
---|
1466 | return signalWaitEventInternalEx(pCbCtx, VINF_SUCCESS, VINF_SUCCESS /* vrcGuest */, pPayload);
|
---|
1467 |
|
---|
1468 | return signalWaitEventInternalEx(pCbCtx, VERR_GSTCTL_GUEST_ERROR, vrcGuest, pPayload);
|
---|
1469 | }
|
---|
1470 |
|
---|
1471 | /**
|
---|
1472 | * Signals a wait event which is registered to a specific callback (bound to a context ID (CID)).
|
---|
1473 | * Extended version.
|
---|
1474 | *
|
---|
1475 | * @returns VBox status code.
|
---|
1476 | * @param pCbCtx Pointer to host service callback context.
|
---|
1477 | * @param vrc Return VBox status code to set as wait result.
|
---|
1478 | * @param vrcGuest Guest return VBox status code to set additionally, if
|
---|
1479 | * vrc is set to VERR_GSTCTL_GUEST_ERROR.
|
---|
1480 | * @param pPayload Additional wait event payload data set set on return. Optional.
|
---|
1481 | */
|
---|
1482 | int GuestBase::signalWaitEventInternalEx(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, int vrc, int vrcGuest,
|
---|
1483 | const GuestWaitEventPayload *pPayload)
|
---|
1484 | {
|
---|
1485 | AssertPtrReturn(pCbCtx, VERR_INVALID_POINTER);
|
---|
1486 | /* pPayload is optional. */
|
---|
1487 |
|
---|
1488 | int vrc2 = RTCritSectEnter(&mWaitEventCritSect);
|
---|
1489 | if (RT_SUCCESS(vrc2))
|
---|
1490 | {
|
---|
1491 | GuestWaitEvents::iterator itEvent = mWaitEvents.find(pCbCtx->uContextID);
|
---|
1492 | if (itEvent != mWaitEvents.end())
|
---|
1493 | {
|
---|
1494 | LogFlowThisFunc(("Signalling event=%p (CID %RU32, vrc=%Rrc, vrcGuest=%Rrc, pPayload=%p) ...\n",
|
---|
1495 | itEvent->second, itEvent->first, vrc, vrcGuest, pPayload));
|
---|
1496 | GuestWaitEvent *pEvent = itEvent->second;
|
---|
1497 | AssertPtr(pEvent);
|
---|
1498 | vrc2 = pEvent->SignalInternal(vrc, vrcGuest, pPayload);
|
---|
1499 | }
|
---|
1500 | else
|
---|
1501 | vrc2 = VERR_NOT_FOUND;
|
---|
1502 |
|
---|
1503 | int vrc3 = RTCritSectLeave(&mWaitEventCritSect);
|
---|
1504 | if (RT_SUCCESS(vrc2))
|
---|
1505 | vrc2 = vrc3;
|
---|
1506 | }
|
---|
1507 |
|
---|
1508 | return vrc2;
|
---|
1509 | }
|
---|
1510 |
|
---|
1511 | /**
|
---|
1512 | * Unregisters (deletes) a wait event.
|
---|
1513 | *
|
---|
1514 | * After successful unregistration the event will not be valid anymore.
|
---|
1515 | *
|
---|
1516 | * @returns VBox status code.
|
---|
1517 | * @param pWaitEvt Wait event to unregister (delete).
|
---|
1518 | */
|
---|
1519 | int GuestBase::unregisterWaitEvent(GuestWaitEvent *pWaitEvt)
|
---|
1520 | {
|
---|
1521 | if (!pWaitEvt) /* Nothing to unregister. */
|
---|
1522 | return VINF_SUCCESS;
|
---|
1523 |
|
---|
1524 | int vrc = RTCritSectEnter(&mWaitEventCritSect);
|
---|
1525 | if (RT_SUCCESS(vrc))
|
---|
1526 | {
|
---|
1527 | LogFlowThisFunc(("pWaitEvt=%p\n", pWaitEvt));
|
---|
1528 |
|
---|
1529 | /** @todo r=bird: One way of optimizing this would be to use the pointer
|
---|
1530 | * instead of the context ID as index into the groups, i.e. revert the value
|
---|
1531 | * pair for the GuestWaitEvents type.
|
---|
1532 | *
|
---|
1533 | * An even more efficent way, would be to not use sexy std::xxx containers for
|
---|
1534 | * the types, but iprt/list.h, as that would just be a RTListNodeRemove call for
|
---|
1535 | * each type w/o needing to iterate much at all. I.e. add a struct {
|
---|
1536 | * RTLISTNODE, GuestWaitEvent *pSelf} array to GuestWaitEvent, and change
|
---|
1537 | * GuestEventGroup to std::map<VBoxEventType_T, RTListAnchorClass>
|
---|
1538 | * (RTListAnchorClass == RTLISTANCHOR wrapper with a constructor)).
|
---|
1539 | *
|
---|
1540 | * P.S. the try/catch is now longer needed after I changed pWaitEvt->Types() to
|
---|
1541 | * return a const reference rather than a copy of the type list (and it think it
|
---|
1542 | * is safe to assume iterators are not hitting the heap). Copy vs reference is
|
---|
1543 | * an easy mistake to make in C++.
|
---|
1544 | *
|
---|
1545 | * P.P.S. The mWaitEventGroups optimization is probably just a lot of extra work
|
---|
1546 | * with little payoff.
|
---|
1547 | */
|
---|
1548 | try
|
---|
1549 | {
|
---|
1550 | /* Remove the event from all event type groups. */
|
---|
1551 | const GuestEventTypes &lstTypes = pWaitEvt->Types();
|
---|
1552 | for (GuestEventTypes::const_iterator itType = lstTypes.begin();
|
---|
1553 | itType != lstTypes.end(); ++itType)
|
---|
1554 | {
|
---|
1555 | /** @todo Slow O(n) lookup. Optimize this. */
|
---|
1556 | GuestWaitEvents::iterator itCurEvent = mWaitEventGroups[(*itType)].begin();
|
---|
1557 | while (itCurEvent != mWaitEventGroups[(*itType)].end())
|
---|
1558 | {
|
---|
1559 | if (itCurEvent->second == pWaitEvt)
|
---|
1560 | {
|
---|
1561 | mWaitEventGroups[(*itType)].erase(itCurEvent);
|
---|
1562 | break;
|
---|
1563 | }
|
---|
1564 | ++itCurEvent;
|
---|
1565 | }
|
---|
1566 | }
|
---|
1567 |
|
---|
1568 | /* Remove the event from the general event list as well. */
|
---|
1569 | GuestWaitEvents::iterator itEvent = mWaitEvents.find(pWaitEvt->ContextID());
|
---|
1570 |
|
---|
1571 | Assert(itEvent != mWaitEvents.end());
|
---|
1572 | Assert(itEvent->second == pWaitEvt);
|
---|
1573 |
|
---|
1574 | mWaitEvents.erase(itEvent);
|
---|
1575 |
|
---|
1576 | delete pWaitEvt;
|
---|
1577 | pWaitEvt = NULL;
|
---|
1578 | }
|
---|
1579 | catch (const std::exception &)
|
---|
1580 | {
|
---|
1581 | AssertFailedStmt(vrc = VERR_NOT_FOUND);
|
---|
1582 | }
|
---|
1583 |
|
---|
1584 | int vrc2 = RTCritSectLeave(&mWaitEventCritSect);
|
---|
1585 | if (RT_SUCCESS(vrc))
|
---|
1586 | vrc = vrc2;
|
---|
1587 | }
|
---|
1588 |
|
---|
1589 | return vrc;
|
---|
1590 | }
|
---|
1591 |
|
---|
1592 | /**
|
---|
1593 | * Waits for an already registered guest wait event.
|
---|
1594 | *
|
---|
1595 | * @return VBox status code.
|
---|
1596 | * @retval VERR_GSTCTL_GUEST_ERROR may be returned, call GuestResult() to get
|
---|
1597 | * the actual result.
|
---|
1598 | *
|
---|
1599 | * @param pWaitEvt Pointer to event to wait for.
|
---|
1600 | * @param msTimeout Timeout (in ms) for waiting.
|
---|
1601 | * @param pType Event type of following IEvent. Optional.
|
---|
1602 | * @param ppEvent Pointer to IEvent which got triggered for this event. Optional.
|
---|
1603 | */
|
---|
1604 | int GuestBase::waitForEvent(GuestWaitEvent *pWaitEvt, uint32_t msTimeout, VBoxEventType_T *pType, IEvent **ppEvent)
|
---|
1605 | {
|
---|
1606 | AssertPtrReturn(pWaitEvt, VERR_INVALID_POINTER);
|
---|
1607 | /* pType is optional. */
|
---|
1608 | /* ppEvent is optional. */
|
---|
1609 |
|
---|
1610 | int vrc = pWaitEvt->Wait(msTimeout);
|
---|
1611 | if (RT_SUCCESS(vrc))
|
---|
1612 | {
|
---|
1613 | const ComPtr<IEvent> pThisEvent = pWaitEvt->Event();
|
---|
1614 | if (pThisEvent.isNotNull()) /* Make sure that we actually have an event associated. */
|
---|
1615 | {
|
---|
1616 | if (pType)
|
---|
1617 | {
|
---|
1618 | HRESULT hrc = pThisEvent->COMGETTER(Type)(pType);
|
---|
1619 | if (FAILED(hrc))
|
---|
1620 | vrc = VERR_COM_UNEXPECTED;
|
---|
1621 | }
|
---|
1622 | if ( RT_SUCCESS(vrc)
|
---|
1623 | && ppEvent)
|
---|
1624 | pThisEvent.queryInterfaceTo(ppEvent);
|
---|
1625 |
|
---|
1626 | unconst(pThisEvent).setNull();
|
---|
1627 | }
|
---|
1628 | }
|
---|
1629 |
|
---|
1630 | return vrc;
|
---|
1631 | }
|
---|
1632 |
|
---|
1633 | #ifndef VBOX_GUESTCTRL_TEST_CASE
|
---|
1634 | /**
|
---|
1635 | * Convenience function to return a pre-formatted string using an action description and a guest error information.
|
---|
1636 | *
|
---|
1637 | * @returns Pre-formatted string with a user-friendly error string.
|
---|
1638 | * @param strAction Action of when the error occurred.
|
---|
1639 | * @param guestErrorInfo Related guest error information to use.
|
---|
1640 | */
|
---|
1641 | /* static */ Utf8Str GuestBase::getErrorAsString(const Utf8Str& strAction, const GuestErrorInfo& guestErrorInfo)
|
---|
1642 | {
|
---|
1643 | Assert(strAction.isNotEmpty());
|
---|
1644 | return Utf8StrFmt("%s: %s", strAction.c_str(), getErrorAsString(guestErrorInfo).c_str());
|
---|
1645 | }
|
---|
1646 |
|
---|
1647 | /**
|
---|
1648 | * Returns a user-friendly error message from a given GuestErrorInfo object.
|
---|
1649 | *
|
---|
1650 | * @returns Error message string.
|
---|
1651 | * @param guestErrorInfo Guest error info to return error message for.
|
---|
1652 | */
|
---|
1653 | /* static */ Utf8Str GuestBase::getErrorAsString(const GuestErrorInfo& guestErrorInfo)
|
---|
1654 | {
|
---|
1655 | AssertMsg(RT_FAILURE(guestErrorInfo.getVrc()), ("Guest vrc does not indicate a failure\n"));
|
---|
1656 |
|
---|
1657 | Utf8Str strErr;
|
---|
1658 |
|
---|
1659 | #ifdef VBOX_WITH_GSTCTL_TOOLBOX_SUPPORT
|
---|
1660 | # define CASE_TOOL_ERROR(a_eType, a_strTool) \
|
---|
1661 | case a_eType: \
|
---|
1662 | { \
|
---|
1663 | strErr = GuestProcessToolbox::guestErrorToString(a_strTool, guestErrorInfo); \
|
---|
1664 | break; \
|
---|
1665 | }
|
---|
1666 | #endif /* VBOX_WITH_GSTCTL_TOOLBOX_SUPPORT */
|
---|
1667 |
|
---|
1668 | switch (guestErrorInfo.getType())
|
---|
1669 | {
|
---|
1670 | case GuestErrorInfo::Type_Session:
|
---|
1671 | strErr = GuestSession::i_guestErrorToString(guestErrorInfo.getVrc());
|
---|
1672 | break;
|
---|
1673 |
|
---|
1674 | case GuestErrorInfo::Type_Process:
|
---|
1675 | strErr = GuestProcess::i_guestErrorToString(guestErrorInfo.getVrc(), guestErrorInfo.getWhat().c_str());
|
---|
1676 | break;
|
---|
1677 |
|
---|
1678 | case GuestErrorInfo::Type_File:
|
---|
1679 | strErr = GuestFile::i_guestErrorToString(guestErrorInfo.getVrc(), guestErrorInfo.getWhat().c_str());
|
---|
1680 | break;
|
---|
1681 |
|
---|
1682 | case GuestErrorInfo::Type_Directory:
|
---|
1683 | strErr = GuestDirectory::i_guestErrorToString(guestErrorInfo.getVrc(), guestErrorInfo.getWhat().c_str());
|
---|
1684 | break;
|
---|
1685 |
|
---|
1686 | case GuestErrorInfo::Type_Fs:
|
---|
1687 | strErr = GuestFs::guestErrorToString(guestErrorInfo);
|
---|
1688 | break;
|
---|
1689 |
|
---|
1690 | #ifdef VBOX_WITH_GSTCTL_TOOLBOX_SUPPORT
|
---|
1691 | CASE_TOOL_ERROR(GuestErrorInfo::Type_ToolLs, VBOXSERVICE_TOOL_LS);
|
---|
1692 | CASE_TOOL_ERROR(GuestErrorInfo::Type_ToolMkDir, VBOXSERVICE_TOOL_MKDIR);
|
---|
1693 | CASE_TOOL_ERROR(GuestErrorInfo::Type_ToolMkTemp, VBOXSERVICE_TOOL_MKTEMP);
|
---|
1694 | CASE_TOOL_ERROR(GuestErrorInfo::Type_ToolRm, VBOXSERVICE_TOOL_RM);
|
---|
1695 | CASE_TOOL_ERROR(GuestErrorInfo::Type_ToolStat, VBOXSERVICE_TOOL_STAT);
|
---|
1696 | #endif /* VBOX_WITH_GSTCTL_TOOLBOX_SUPPORT */
|
---|
1697 | default:
|
---|
1698 | AssertMsgFailed(("Type not implemented (type=%RU32, vrc=%Rrc)\n", guestErrorInfo.getType(), guestErrorInfo.getVrc()));
|
---|
1699 | strErr = Utf8StrFmt("Unknown / Not implemented -- Please file a bug report (type=%RU32, vrc=%Rrc)\n",
|
---|
1700 | guestErrorInfo.getType(), guestErrorInfo.getVrc());
|
---|
1701 | break;
|
---|
1702 | }
|
---|
1703 |
|
---|
1704 | return strErr;
|
---|
1705 | }
|
---|
1706 |
|
---|
1707 | #endif /* VBOX_GUESTCTRL_TEST_CASE */
|
---|
1708 |
|
---|
1709 | /**
|
---|
1710 | * Converts RTFMODE to FsObjType_T.
|
---|
1711 | *
|
---|
1712 | * @return Converted FsObjType_T type.
|
---|
1713 | * @param fMode RTFMODE to convert.
|
---|
1714 | */
|
---|
1715 | /* static */
|
---|
1716 | FsObjType_T GuestBase::fileModeToFsObjType(RTFMODE fMode)
|
---|
1717 | {
|
---|
1718 | if (RTFS_IS_FIFO(fMode)) return FsObjType_Fifo;
|
---|
1719 | else if (RTFS_IS_DEV_CHAR(fMode)) return FsObjType_DevChar;
|
---|
1720 | else if (RTFS_IS_DIRECTORY(fMode)) return FsObjType_Directory;
|
---|
1721 | else if (RTFS_IS_DEV_BLOCK(fMode)) return FsObjType_DevBlock;
|
---|
1722 | else if (RTFS_IS_FILE(fMode)) return FsObjType_File;
|
---|
1723 | else if (RTFS_IS_SYMLINK(fMode)) return FsObjType_Symlink;
|
---|
1724 | else if (RTFS_IS_SOCKET(fMode)) return FsObjType_Socket;
|
---|
1725 | else if (RTFS_IS_WHITEOUT(fMode)) return FsObjType_WhiteOut;
|
---|
1726 |
|
---|
1727 | return FsObjType_Unknown;
|
---|
1728 | }
|
---|
1729 |
|
---|
1730 | /**
|
---|
1731 | * Converts a FsObjType_T to a human-readable string.
|
---|
1732 | *
|
---|
1733 | * @returns Human-readable string of FsObjType_T.
|
---|
1734 | * @param enmType FsObjType_T to convert.
|
---|
1735 | */
|
---|
1736 | /* static */
|
---|
1737 | const char *GuestBase::fsObjTypeToStr(FsObjType_T enmType)
|
---|
1738 | {
|
---|
1739 | switch (enmType)
|
---|
1740 | {
|
---|
1741 | case FsObjType_Directory: return "directory";
|
---|
1742 | case FsObjType_Symlink: return "symbolic link";
|
---|
1743 | case FsObjType_File: return "file";
|
---|
1744 | default: break;
|
---|
1745 | }
|
---|
1746 |
|
---|
1747 | return "unknown";
|
---|
1748 | }
|
---|
1749 |
|
---|
1750 | /**
|
---|
1751 | * Converts a PathStyle_T to a human-readable string.
|
---|
1752 | *
|
---|
1753 | * @returns Human-readable string of PathStyle_T.
|
---|
1754 | * @param enmPathStyle PathStyle_T to convert.
|
---|
1755 | */
|
---|
1756 | /* static */
|
---|
1757 | const char *GuestBase::pathStyleToStr(PathStyle_T enmPathStyle)
|
---|
1758 | {
|
---|
1759 | switch (enmPathStyle)
|
---|
1760 | {
|
---|
1761 | case PathStyle_DOS: return "DOS";
|
---|
1762 | case PathStyle_UNIX: return "UNIX";
|
---|
1763 | case PathStyle_Unknown: return "Unknown";
|
---|
1764 | default: break;
|
---|
1765 | }
|
---|
1766 |
|
---|
1767 | return "<invalid>";
|
---|
1768 | }
|
---|
1769 |
|
---|
1770 | GuestObject::GuestObject(void)
|
---|
1771 | : mSession(NULL),
|
---|
1772 | mObjectID(0)
|
---|
1773 | {
|
---|
1774 | }
|
---|
1775 |
|
---|
1776 | GuestObject::~GuestObject(void)
|
---|
1777 | {
|
---|
1778 | }
|
---|
1779 |
|
---|
1780 | /**
|
---|
1781 | * Binds this guest (control) object to a specific guest (control) session.
|
---|
1782 | *
|
---|
1783 | * @returns VBox status code.
|
---|
1784 | * @param pConsole Pointer to console object to use.
|
---|
1785 | * @param pSession Pointer to session to bind this object to.
|
---|
1786 | * @param uObjectID Object ID for this object to use within that specific session.
|
---|
1787 | * Each object ID must be unique per session.
|
---|
1788 | */
|
---|
1789 | int GuestObject::bindToSession(Console *pConsole, GuestSession *pSession, uint32_t uObjectID)
|
---|
1790 | {
|
---|
1791 | AssertPtrReturn(pConsole, VERR_INVALID_POINTER);
|
---|
1792 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
1793 |
|
---|
1794 | mConsole = pConsole;
|
---|
1795 | mSession = pSession;
|
---|
1796 | mObjectID = uObjectID;
|
---|
1797 |
|
---|
1798 | return VINF_SUCCESS;
|
---|
1799 | }
|
---|
1800 |
|
---|
1801 | /**
|
---|
1802 | * Registers (creates) a new wait event.
|
---|
1803 | *
|
---|
1804 | * @returns VBox status code.
|
---|
1805 | * @param lstEvents List of events which the new wait event gets triggered at.
|
---|
1806 | * @param ppEvent Returns the new wait event on success.
|
---|
1807 | */
|
---|
1808 | int GuestObject::registerWaitEvent(const GuestEventTypes &lstEvents,
|
---|
1809 | GuestWaitEvent **ppEvent)
|
---|
1810 | {
|
---|
1811 | AssertPtr(mSession);
|
---|
1812 | return GuestBase::registerWaitEventEx(mSession->i_getId(), mObjectID, lstEvents, ppEvent);
|
---|
1813 | }
|
---|
1814 |
|
---|
1815 | /**
|
---|
1816 | * Sends a HGCM message to the guest (via the guest control host service).
|
---|
1817 | *
|
---|
1818 | * @returns VBox status code.
|
---|
1819 | * @param uMessage Message ID of message to send.
|
---|
1820 | * @param cParms Number of HGCM message parameters to send.
|
---|
1821 | * @param paParms Array of HGCM message parameters to send.
|
---|
1822 | */
|
---|
1823 | int GuestObject::sendMessage(uint32_t uMessage, uint32_t cParms, PVBOXHGCMSVCPARM paParms)
|
---|
1824 | {
|
---|
1825 | #ifndef VBOX_GUESTCTRL_TEST_CASE
|
---|
1826 | ComObjPtr<Console> pConsole = mConsole;
|
---|
1827 | Assert(!pConsole.isNull());
|
---|
1828 |
|
---|
1829 | int vrc = VERR_HGCM_SERVICE_NOT_FOUND;
|
---|
1830 |
|
---|
1831 | /* Forward the information to the VMM device. */
|
---|
1832 | VMMDev *pVMMDev = pConsole->i_getVMMDev();
|
---|
1833 | if (pVMMDev)
|
---|
1834 | {
|
---|
1835 | /* HACK ALERT! We extend the first parameter to 64-bit and use the
|
---|
1836 | two topmost bits for call destination information. */
|
---|
1837 | Assert(paParms[0].type == VBOX_HGCM_SVC_PARM_32BIT);
|
---|
1838 | paParms[0].type = VBOX_HGCM_SVC_PARM_64BIT;
|
---|
1839 | paParms[0].u.uint64 = (uint64_t)paParms[0].u.uint32 | VBOX_GUESTCTRL_DST_SESSION;
|
---|
1840 |
|
---|
1841 | /* Make the call. */
|
---|
1842 | LogFlowThisFunc(("uMessage=%RU32, cParms=%RU32\n", uMessage, cParms));
|
---|
1843 | vrc = pVMMDev->hgcmHostCall(HGCMSERVICE_NAME, uMessage, cParms, paParms);
|
---|
1844 | if (RT_FAILURE(vrc))
|
---|
1845 | {
|
---|
1846 | /** @todo What to do here? */
|
---|
1847 | }
|
---|
1848 | }
|
---|
1849 | #else
|
---|
1850 | LogFlowThisFuncEnter();
|
---|
1851 |
|
---|
1852 | /* Not needed within testcases. */
|
---|
1853 | RT_NOREF(uMessage, cParms, paParms);
|
---|
1854 | int vrc = VINF_SUCCESS;
|
---|
1855 | #endif
|
---|
1856 | return vrc;
|
---|
1857 | }
|
---|
1858 |
|
---|
1859 | GuestWaitEventBase::GuestWaitEventBase(void)
|
---|
1860 | : mfAborted(false),
|
---|
1861 | mCID(0),
|
---|
1862 | mEventSem(NIL_RTSEMEVENT),
|
---|
1863 | mVrc(VINF_SUCCESS),
|
---|
1864 | mGuestRc(VINF_SUCCESS)
|
---|
1865 | {
|
---|
1866 | }
|
---|
1867 |
|
---|
1868 | GuestWaitEventBase::~GuestWaitEventBase(void)
|
---|
1869 | {
|
---|
1870 | if (mEventSem != NIL_RTSEMEVENT)
|
---|
1871 | {
|
---|
1872 | RTSemEventDestroy(mEventSem);
|
---|
1873 | mEventSem = NIL_RTSEMEVENT;
|
---|
1874 | }
|
---|
1875 | }
|
---|
1876 |
|
---|
1877 | /**
|
---|
1878 | * Initializes a wait event with a specific context ID (CID).
|
---|
1879 | *
|
---|
1880 | * @returns VBox status code.
|
---|
1881 | * @param uCID Context ID (CID) to initialize wait event with.
|
---|
1882 | */
|
---|
1883 | int GuestWaitEventBase::Init(uint32_t uCID)
|
---|
1884 | {
|
---|
1885 | mCID = uCID;
|
---|
1886 |
|
---|
1887 | return RTSemEventCreate(&mEventSem);
|
---|
1888 | }
|
---|
1889 |
|
---|
1890 | /**
|
---|
1891 | * Signals a wait event.
|
---|
1892 | *
|
---|
1893 | * @returns VBox status code.
|
---|
1894 | * @param vrc Return VBox status code to set as wait result.
|
---|
1895 | * @param vrcGuest Guest return VBox status code to set additionally, if
|
---|
1896 | * @a vrc is set to VERR_GSTCTL_GUEST_ERROR.
|
---|
1897 | * @param pPayload Additional wait event payload data set set on return. Optional.
|
---|
1898 | */
|
---|
1899 | int GuestWaitEventBase::SignalInternal(int vrc, int vrcGuest, const GuestWaitEventPayload *pPayload)
|
---|
1900 | {
|
---|
1901 | if (mfAborted)
|
---|
1902 | return VERR_CANCELLED;
|
---|
1903 |
|
---|
1904 | #ifdef VBOX_STRICT
|
---|
1905 | if (vrc == VERR_GSTCTL_GUEST_ERROR)
|
---|
1906 | AssertMsg(RT_FAILURE(vrcGuest), ("Guest error indicated but no actual guest error set (%Rrc)\n", vrcGuest));
|
---|
1907 | else
|
---|
1908 | AssertMsg(RT_SUCCESS(vrcGuest), ("No guest error indicated but actual guest error set (%Rrc)\n", vrcGuest));
|
---|
1909 | #endif
|
---|
1910 |
|
---|
1911 | int vrc2;
|
---|
1912 | if (pPayload)
|
---|
1913 | vrc2 = mPayload.CopyFromDeep(*pPayload);
|
---|
1914 | else
|
---|
1915 | vrc2 = VINF_SUCCESS;
|
---|
1916 | if (RT_SUCCESS(vrc2))
|
---|
1917 | {
|
---|
1918 | mVrc = vrc;
|
---|
1919 | mGuestRc = vrcGuest;
|
---|
1920 |
|
---|
1921 | vrc2 = RTSemEventSignal(mEventSem);
|
---|
1922 | }
|
---|
1923 |
|
---|
1924 | return vrc2;
|
---|
1925 | }
|
---|
1926 |
|
---|
1927 | /**
|
---|
1928 | * Waits for the event to get triggered. Will return success if the
|
---|
1929 | * wait was successufl (e.g. was being triggered), otherwise an error will be returned.
|
---|
1930 | *
|
---|
1931 | * @returns VBox status code.
|
---|
1932 | * @retval VERR_GSTCTL_GUEST_ERROR may be returned, call GuestResult() to get
|
---|
1933 | * the actual result from the guest side.
|
---|
1934 | *
|
---|
1935 | * @param msTimeout Timeout (in ms) to wait.
|
---|
1936 | * Specifiy 0 to wait indefinitely.
|
---|
1937 | */
|
---|
1938 | int GuestWaitEventBase::Wait(RTMSINTERVAL msTimeout)
|
---|
1939 | {
|
---|
1940 | int vrc;
|
---|
1941 | if (!mfAborted)
|
---|
1942 | {
|
---|
1943 | AssertReturn(mEventSem != NIL_RTSEMEVENT, VERR_CANCELLED);
|
---|
1944 |
|
---|
1945 | vrc = RTSemEventWait(mEventSem, msTimeout ? msTimeout : RT_INDEFINITE_WAIT);
|
---|
1946 | if ( RT_SUCCESS(vrc)
|
---|
1947 | && mfAborted)
|
---|
1948 | vrc = VERR_CANCELLED;
|
---|
1949 |
|
---|
1950 | if (RT_SUCCESS(vrc))
|
---|
1951 | {
|
---|
1952 | /* If waiting succeeded, return the overall
|
---|
1953 | * result code. */
|
---|
1954 | vrc = mVrc;
|
---|
1955 | }
|
---|
1956 | }
|
---|
1957 | else
|
---|
1958 | vrc = VERR_CANCELLED;
|
---|
1959 | return vrc;
|
---|
1960 | }
|
---|
1961 |
|
---|
1962 | GuestWaitEvent::GuestWaitEvent(void)
|
---|
1963 | {
|
---|
1964 | }
|
---|
1965 |
|
---|
1966 | GuestWaitEvent::~GuestWaitEvent(void)
|
---|
1967 | {
|
---|
1968 |
|
---|
1969 | }
|
---|
1970 |
|
---|
1971 | /**
|
---|
1972 | * Cancels the event.
|
---|
1973 | */
|
---|
1974 | int GuestWaitEvent::Cancel(void)
|
---|
1975 | {
|
---|
1976 | if (mfAborted) /* Already aborted? */
|
---|
1977 | return VINF_SUCCESS;
|
---|
1978 |
|
---|
1979 | mfAborted = true;
|
---|
1980 |
|
---|
1981 | #ifdef DEBUG_andy
|
---|
1982 | LogFlowThisFunc(("Cancelling %p ...\n"));
|
---|
1983 | #endif
|
---|
1984 | return RTSemEventSignal(mEventSem);
|
---|
1985 | }
|
---|
1986 |
|
---|
1987 | /**
|
---|
1988 | * Initializes a wait event with a given context ID (CID).
|
---|
1989 | *
|
---|
1990 | * @returns VBox status code.
|
---|
1991 | * @param uCID Context ID to initialize wait event with.
|
---|
1992 | */
|
---|
1993 | int GuestWaitEvent::Init(uint32_t uCID)
|
---|
1994 | {
|
---|
1995 | return GuestWaitEventBase::Init(uCID);
|
---|
1996 | }
|
---|
1997 |
|
---|
1998 | /**
|
---|
1999 | * Initializes a wait event with a given context ID (CID) and a list of event types to wait for.
|
---|
2000 | *
|
---|
2001 | * @returns VBox status code.
|
---|
2002 | * @param uCID Context ID to initialize wait event with.
|
---|
2003 | * @param lstEvents List of event types to wait for this wait event to get signalled.
|
---|
2004 | */
|
---|
2005 | int GuestWaitEvent::Init(uint32_t uCID, const GuestEventTypes &lstEvents)
|
---|
2006 | {
|
---|
2007 | int vrc = GuestWaitEventBase::Init(uCID);
|
---|
2008 | if (RT_SUCCESS(vrc))
|
---|
2009 | mEventTypes = lstEvents;
|
---|
2010 |
|
---|
2011 | return vrc;
|
---|
2012 | }
|
---|
2013 |
|
---|
2014 | /**
|
---|
2015 | * Signals the event.
|
---|
2016 | *
|
---|
2017 | * @return VBox status code.
|
---|
2018 | * @param pEvent Public IEvent to associate.
|
---|
2019 | * Optional.
|
---|
2020 | */
|
---|
2021 | int GuestWaitEvent::SignalExternal(IEvent *pEvent)
|
---|
2022 | {
|
---|
2023 | if (pEvent)
|
---|
2024 | mEvent = pEvent;
|
---|
2025 |
|
---|
2026 | return RTSemEventSignal(mEventSem);
|
---|
2027 | }
|
---|
2028 |
|
---|
2029 |
|
---|
2030 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
---|
2031 | // GuestPath
|
---|
2032 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
---|
2033 |
|
---|
2034 | /**
|
---|
2035 | * Builds a (final) destination path from a given source + destination path.
|
---|
2036 | *
|
---|
2037 | * This does not utilize any file system access whatsoever. Used for guest and host paths.
|
---|
2038 | *
|
---|
2039 | * @returns VBox status code.
|
---|
2040 | * @param strSrcPath Source path to build destination path for.
|
---|
2041 | * @param enmSrcPathStyle Path style the source path is in.
|
---|
2042 | * @param strDstPath Destination path to use for building the (final) destination path.
|
---|
2043 | * @param enmDstPathStyle Path style the destination path is in.
|
---|
2044 | *
|
---|
2045 | * @note See rules within the function.
|
---|
2046 | */
|
---|
2047 | /* static */
|
---|
2048 | int GuestPath::BuildDestinationPath(const Utf8Str &strSrcPath, PathStyle_T enmSrcPathStyle,
|
---|
2049 | Utf8Str &strDstPath, PathStyle_T enmDstPathStyle)
|
---|
2050 | {
|
---|
2051 | /*
|
---|
2052 | * Rules:
|
---|
2053 | *
|
---|
2054 | * # source dest final dest remarks
|
---|
2055 | *
|
---|
2056 | * 1 /src/path1/ /dst/path2/ /dst/path2/<contents of path1> Just copies contents of <contents of path1>, not the path1 itself.
|
---|
2057 | * 2 /src/path1 /dst/path2/ /dst/path2/path1 Copies path1 into path2.
|
---|
2058 | * 3 /src/path1 /dst/path2 /dst/path2 Overwrites stuff from path2 with stuff from path1.
|
---|
2059 | * 4 Dotdot ("..") directories are forbidden for security reasons.
|
---|
2060 | */
|
---|
2061 | const char *pszSrcName = RTPathFilenameEx(strSrcPath.c_str(),
|
---|
2062 | enmSrcPathStyle == PathStyle_DOS
|
---|
2063 | ? RTPATH_STR_F_STYLE_DOS : RTPATH_STR_F_STYLE_UNIX);
|
---|
2064 |
|
---|
2065 | const char *pszDstName = RTPathFilenameEx(strDstPath.c_str(),
|
---|
2066 | enmDstPathStyle == PathStyle_DOS
|
---|
2067 | ? RTPATH_STR_F_STYLE_DOS : RTPATH_STR_F_STYLE_UNIX);
|
---|
2068 |
|
---|
2069 | if ( (!pszSrcName && !pszDstName) /* #1 */
|
---|
2070 | || ( pszSrcName && pszDstName)) /* #3 */
|
---|
2071 | {
|
---|
2072 | /* Note: Must have DirectoryFlag_CopyIntoExisting + FileFlag_NoReplace *not* set. */
|
---|
2073 | }
|
---|
2074 | else if (pszSrcName && !pszDstName) /* #2 */
|
---|
2075 | {
|
---|
2076 | if (!strDstPath.endsWith(PATH_STYLE_SEP_STR(enmDstPathStyle)))
|
---|
2077 | strDstPath += PATH_STYLE_SEP_STR(enmDstPathStyle);
|
---|
2078 | strDstPath += pszSrcName;
|
---|
2079 | }
|
---|
2080 |
|
---|
2081 | /* Translate the built destination path to a path compatible with the destination. */
|
---|
2082 | int vrc = GuestPath::Translate(strDstPath, enmSrcPathStyle, enmDstPathStyle);
|
---|
2083 | if (RT_SUCCESS(vrc))
|
---|
2084 | {
|
---|
2085 | union
|
---|
2086 | {
|
---|
2087 | RTPATHPARSED Parsed;
|
---|
2088 | RTPATHSPLIT Split;
|
---|
2089 | uint8_t ab[4096];
|
---|
2090 | } u;
|
---|
2091 | vrc = RTPathParse(strDstPath.c_str(), &u.Parsed, sizeof(u),
|
---|
2092 | enmDstPathStyle == PathStyle_DOS ? RTPATH_STR_F_STYLE_DOS : RTPATH_STR_F_STYLE_UNIX);
|
---|
2093 | if (RT_SUCCESS(vrc))
|
---|
2094 | {
|
---|
2095 | if (u.Parsed.fProps & RTPATH_PROP_DOTDOT_REFS) /* #4 */
|
---|
2096 | vrc = VERR_INVALID_PARAMETER;
|
---|
2097 | }
|
---|
2098 | }
|
---|
2099 |
|
---|
2100 | LogRel2(("Guest Control: Building destination path for '%s' (%s) -> '%s' (%s): %Rrc\n",
|
---|
2101 | strSrcPath.c_str(), GuestBase::pathStyleToStr(enmSrcPathStyle),
|
---|
2102 | strDstPath.c_str(), GuestBase::pathStyleToStr(enmDstPathStyle), vrc));
|
---|
2103 |
|
---|
2104 | return vrc;
|
---|
2105 | }
|
---|
2106 |
|
---|
2107 | /**
|
---|
2108 | * Translates a path from a specific path style into another.
|
---|
2109 | *
|
---|
2110 | * @returns VBox status code.
|
---|
2111 | * @retval VERR_NOT_SUPPORTED if a conversion is not supported.
|
---|
2112 | * @retval VERR_NOT_IMPLEMENTED if path style conversion is not implemented yet.
|
---|
2113 | * @param strPath Path to translate. Will contain the translated path on success. UTF-8 only.
|
---|
2114 | * @param enmSrcPathStyle Source path style \a strPath is expected in.
|
---|
2115 | * @param enmDstPathStyle Destination path style to convert to.
|
---|
2116 | * @param fForce Whether to force the translation to the destination path style or not.
|
---|
2117 | *
|
---|
2118 | * @note This does NOT remove any trailing slashes and/or perform file system lookups!
|
---|
2119 | */
|
---|
2120 | /* static */
|
---|
2121 | int GuestPath::Translate(Utf8Str &strPath, PathStyle_T enmSrcPathStyle, PathStyle_T enmDstPathStyle, bool fForce /* = false */)
|
---|
2122 | {
|
---|
2123 | if (strPath.isEmpty())
|
---|
2124 | return VINF_SUCCESS;
|
---|
2125 |
|
---|
2126 | AssertReturn(RTStrIsValidEncoding(strPath.c_str()), VERR_INVALID_PARAMETER);
|
---|
2127 |
|
---|
2128 | int vrc = VINF_SUCCESS;
|
---|
2129 |
|
---|
2130 | Utf8Str strTranslated;
|
---|
2131 |
|
---|
2132 | if ( ( enmSrcPathStyle == PathStyle_DOS
|
---|
2133 | && enmDstPathStyle == PathStyle_UNIX)
|
---|
2134 | || (fForce && enmDstPathStyle == PathStyle_UNIX))
|
---|
2135 | {
|
---|
2136 | strTranslated = strPath;
|
---|
2137 | RTPathChangeToUnixSlashes(strTranslated.mutableRaw(), true /* fForce */);
|
---|
2138 | }
|
---|
2139 | else if ( ( enmSrcPathStyle == PathStyle_UNIX
|
---|
2140 | && enmDstPathStyle == PathStyle_DOS)
|
---|
2141 | || (fForce && enmDstPathStyle == PathStyle_DOS))
|
---|
2142 |
|
---|
2143 | {
|
---|
2144 | strTranslated = strPath;
|
---|
2145 | RTPathChangeToDosSlashes(strTranslated.mutableRaw(), true /* fForce */);
|
---|
2146 | }
|
---|
2147 |
|
---|
2148 | if ( strTranslated.isEmpty() /* Not forced. */
|
---|
2149 | && enmSrcPathStyle == enmDstPathStyle)
|
---|
2150 | {
|
---|
2151 | strTranslated = strPath;
|
---|
2152 | }
|
---|
2153 |
|
---|
2154 | if (RT_FAILURE(vrc))
|
---|
2155 | {
|
---|
2156 | LogRel(("Guest Control: Translating path '%s' (%s) -> '%s' (%s) failed, vrc=%Rrc\n",
|
---|
2157 | strPath.c_str(), GuestBase::pathStyleToStr(enmSrcPathStyle),
|
---|
2158 | strTranslated.c_str(), GuestBase::pathStyleToStr(enmDstPathStyle), vrc));
|
---|
2159 | return vrc;
|
---|
2160 | }
|
---|
2161 |
|
---|
2162 | /* Cleanup. */
|
---|
2163 | const char *psz = strTranslated.mutableRaw();
|
---|
2164 | size_t const cch = strTranslated.length();
|
---|
2165 | size_t off = 0;
|
---|
2166 | while (off < cch)
|
---|
2167 | {
|
---|
2168 | if (off + 1 > cch)
|
---|
2169 | break;
|
---|
2170 | /* Remove double back slashes (DOS only). */
|
---|
2171 | if ( enmDstPathStyle == PathStyle_DOS
|
---|
2172 | && psz[off] == '\\'
|
---|
2173 | && psz[off + 1] == '\\')
|
---|
2174 | {
|
---|
2175 | strTranslated.erase(off + 1, 1);
|
---|
2176 | off++;
|
---|
2177 | }
|
---|
2178 | /* Remove double forward slashes (UNIX only). */
|
---|
2179 | if ( enmDstPathStyle == PathStyle_UNIX
|
---|
2180 | && psz[off] == '/'
|
---|
2181 | && psz[off + 1] == '/')
|
---|
2182 | {
|
---|
2183 | strTranslated.erase(off + 1, 1);
|
---|
2184 | off++;
|
---|
2185 | }
|
---|
2186 | off++;
|
---|
2187 | }
|
---|
2188 |
|
---|
2189 | /* Note: Do not trim() paths here, as technically it's possible to create paths with trailing spaces. */
|
---|
2190 |
|
---|
2191 | strTranslated.jolt();
|
---|
2192 |
|
---|
2193 | LogRel2(("Guest Control: Translating '%s' (%s) -> '%s' (%s): %Rrc\n",
|
---|
2194 | strPath.c_str(), GuestBase::pathStyleToStr(enmSrcPathStyle),
|
---|
2195 | strTranslated.c_str(), GuestBase::pathStyleToStr(enmDstPathStyle), vrc));
|
---|
2196 |
|
---|
2197 | if (RT_SUCCESS(vrc))
|
---|
2198 | strPath = strTranslated;
|
---|
2199 |
|
---|
2200 | return vrc;
|
---|
2201 | }
|
---|
2202 |
|
---|