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