VirtualBox

source: vbox/trunk/src/VBox/Main/include/GuestSessionImpl.h@ 75861

Last change on this file since 75861 was 75861, checked in by vboxsync, 6 years ago

Main/Guest: Added a few codereview comments, mainly in the guest base class area. Replaced the object ID management in GuestSession with a classic allocation bitmap approach as that's much less memory intensive than 16 byte structs in a insert-only std::map and an additional map tracking free 32-bit IDs after we run out. bugref:9313

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 21.9 KB
Line 
1/* $Id: GuestSessionImpl.h 75861 2018-12-02 00:26:36Z vboxsync $ */
2/** @file
3 * VirtualBox Main - Guest session handling.
4 */
5
6/*
7 * Copyright (C) 2012-2018 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#ifndef ____H_GUESTSESSIONIMPL
19#define ____H_GUESTSESSIONIMPL
20
21#include "GuestSessionWrap.h"
22#include "EventImpl.h"
23
24#include "GuestCtrlImplPrivate.h"
25#include "GuestProcessImpl.h"
26#include "GuestDirectoryImpl.h"
27#include "GuestFileImpl.h"
28#include "GuestFsObjInfoImpl.h"
29#include "GuestSessionImplTasks.h"
30
31#include <iprt/asm.h> /** @todo r=bird: Needed for ASMBitSet() in GuestSession::Data constructor. Removed when
32 * that is moved into the class implementation file as it should be. */
33#include <deque>
34
35class GuestSessionTaskInternalOpen; /* Needed for i_startSessionThreadTask(). */
36
37/**
38 * Guest session implementation.
39 */
40class ATL_NO_VTABLE GuestSession
41 : public GuestSessionWrap
42 , public GuestBase
43{
44public:
45 /** @name COM and internal init/term/mapping cruft.
46 * @{ */
47 DECLARE_EMPTY_CTOR_DTOR(GuestSession)
48
49 int init(Guest *pGuest, const GuestSessionStartupInfo &ssInfo, const GuestCredentials &guestCreds);
50 void uninit(void);
51 HRESULT FinalConstruct(void);
52 void FinalRelease(void);
53 /** @} */
54
55private:
56
57 /** Wrapped @name IGuestSession properties.
58 * @{ */
59 HRESULT getUser(com::Utf8Str &aUser);
60 HRESULT getDomain(com::Utf8Str &aDomain);
61 HRESULT getName(com::Utf8Str &aName);
62 HRESULT getId(ULONG *aId);
63 HRESULT getTimeout(ULONG *aTimeout);
64 HRESULT setTimeout(ULONG aTimeout);
65 HRESULT getProtocolVersion(ULONG *aProtocolVersion);
66 HRESULT getStatus(GuestSessionStatus_T *aStatus);
67 HRESULT getEnvironmentChanges(std::vector<com::Utf8Str> &aEnvironmentChanges);
68 HRESULT setEnvironmentChanges(const std::vector<com::Utf8Str> &aEnvironmentChanges);
69 HRESULT getEnvironmentBase(std::vector<com::Utf8Str> &aEnvironmentBase);
70 HRESULT getProcesses(std::vector<ComPtr<IGuestProcess> > &aProcesses);
71 HRESULT getPathStyle(PathStyle_T *aPathStyle);
72 HRESULT getCurrentDirectory(com::Utf8Str &aCurrentDirectory);
73 HRESULT setCurrentDirectory(const com::Utf8Str &aCurrentDirectory);
74 HRESULT getUserDocuments(com::Utf8Str &aUserDocuments);
75 HRESULT getUserHome(com::Utf8Str &aUserHome);
76 HRESULT getDirectories(std::vector<ComPtr<IGuestDirectory> > &aDirectories);
77 HRESULT getFiles(std::vector<ComPtr<IGuestFile> > &aFiles);
78 HRESULT getEventSource(ComPtr<IEventSource> &aEventSource);
79 /** @} */
80
81 /** Wrapped @name IGuestSession methods.
82 * @{ */
83 HRESULT close();
84
85 HRESULT copyFromGuest(const std::vector<com::Utf8Str> &aSources,
86 const std::vector<com::Utf8Str> &aFilters,
87 const std::vector<com::Utf8Str> &aFlags,
88 const com::Utf8Str &aDestination,
89 ComPtr<IProgress> &aProgress);
90 HRESULT copyToGuest(const std::vector<com::Utf8Str> &aSources,
91 const std::vector<com::Utf8Str> &aFilters,
92 const std::vector<com::Utf8Str> &aFlags,
93 const com::Utf8Str &aDestination,
94 ComPtr<IProgress> &aProgress);
95
96 HRESULT directoryCopy(const com::Utf8Str &aSource,
97 const com::Utf8Str &aDestination,
98 const std::vector<DirectoryCopyFlag_T> &aFlags,
99 ComPtr<IProgress> &aProgress);
100 HRESULT directoryCopyFromGuest(const com::Utf8Str &aSource,
101 const com::Utf8Str &aDestination,
102 const std::vector<DirectoryCopyFlag_T> &aFlags,
103 ComPtr<IProgress> &aProgress);
104 HRESULT directoryCopyToGuest(const com::Utf8Str &aSource,
105 const com::Utf8Str &aDestination,
106 const std::vector<DirectoryCopyFlag_T> &aFlags,
107 ComPtr<IProgress> &aProgress);
108 HRESULT directoryCreate(const com::Utf8Str &aPath,
109 ULONG aMode,
110 const std::vector<DirectoryCreateFlag_T> &aFlags);
111 HRESULT directoryCreateTemp(const com::Utf8Str &aTemplateName,
112 ULONG aMode,
113 const com::Utf8Str &aPath,
114 BOOL aSecure,
115 com::Utf8Str &aDirectory);
116 HRESULT directoryExists(const com::Utf8Str &aPath,
117 BOOL aFollowSymlinks,
118 BOOL *aExists);
119 HRESULT directoryOpen(const com::Utf8Str &aPath,
120 const com::Utf8Str &aFilter,
121 const std::vector<DirectoryOpenFlag_T> &aFlags,
122 ComPtr<IGuestDirectory> &aDirectory);
123 HRESULT directoryRemove(const com::Utf8Str &aPath);
124 HRESULT directoryRemoveRecursive(const com::Utf8Str &aPath,
125 const std::vector<DirectoryRemoveRecFlag_T> &aFlags,
126 ComPtr<IProgress> &aProgress);
127 HRESULT environmentScheduleSet(const com::Utf8Str &aName,
128 const com::Utf8Str &aValue);
129 HRESULT environmentScheduleUnset(const com::Utf8Str &aName);
130 HRESULT environmentGetBaseVariable(const com::Utf8Str &aName,
131 com::Utf8Str &aValue);
132 HRESULT environmentDoesBaseVariableExist(const com::Utf8Str &aName,
133 BOOL *aExists);
134
135 HRESULT fileCopy(const com::Utf8Str &aSource,
136 const com::Utf8Str &aDestination,
137 const std::vector<FileCopyFlag_T> &aFlags,
138 ComPtr<IProgress> &aProgress);
139 HRESULT fileCopyToGuest(const com::Utf8Str &aSource,
140 const com::Utf8Str &aDestination,
141 const std::vector<FileCopyFlag_T> &aFlags,
142 ComPtr<IProgress> &aProgress);
143 HRESULT fileCopyFromGuest(const com::Utf8Str &aSource,
144 const com::Utf8Str &aDestination,
145 const std::vector<FileCopyFlag_T> &aFlags,
146 ComPtr<IProgress> &aProgress);
147 HRESULT fileCreateTemp(const com::Utf8Str &aTemplateName,
148 ULONG aMode,
149 const com::Utf8Str &aPath,
150 BOOL aSecure,
151 ComPtr<IGuestFile> &aFile);
152 HRESULT fileExists(const com::Utf8Str &aPath,
153 BOOL aFollowSymlinks,
154 BOOL *aExists);
155 HRESULT fileOpen(const com::Utf8Str &aPath,
156 FileAccessMode_T aAccessMode,
157 FileOpenAction_T aOpenAction,
158 ULONG aCreationMode,
159 ComPtr<IGuestFile> &aFile);
160 HRESULT fileOpenEx(const com::Utf8Str &aPath,
161 FileAccessMode_T aAccessMode,
162 FileOpenAction_T aOpenAction,
163 FileSharingMode_T aSharingMode,
164 ULONG aCreationMode,
165 const std::vector<FileOpenExFlag_T> &aFlags,
166 ComPtr<IGuestFile> &aFile);
167 HRESULT fileQuerySize(const com::Utf8Str &aPath,
168 BOOL aFollowSymlinks,
169 LONG64 *aSize);
170 HRESULT fsObjExists(const com::Utf8Str &aPath,
171 BOOL aFollowSymlinks,
172 BOOL *pfExists);
173 HRESULT fsObjQueryInfo(const com::Utf8Str &aPath,
174 BOOL aFollowSymlinks,
175 ComPtr<IGuestFsObjInfo> &aInfo);
176 HRESULT fsObjRemove(const com::Utf8Str &aPath);
177 HRESULT fsObjRemoveArray(const std::vector<com::Utf8Str> &aPaths,
178 ComPtr<IProgress> &aProgress);
179 HRESULT fsObjRename(const com::Utf8Str &aOldPath,
180 const com::Utf8Str &aNewPath,
181 const std::vector<FsObjRenameFlag_T> &aFlags);
182 HRESULT fsObjMove(const com::Utf8Str &aSource,
183 const com::Utf8Str &aDestination,
184 const std::vector<FsObjMoveFlag_T> &aFlags,
185 ComPtr<IProgress> &aProgress);
186 HRESULT fsObjSetACL(const com::Utf8Str &aPath,
187 BOOL aFollowSymlinks,
188 const com::Utf8Str &aAcl,
189 ULONG aMode);
190 HRESULT processCreate(const com::Utf8Str &aCommand,
191 const std::vector<com::Utf8Str> &aArguments,
192 const std::vector<com::Utf8Str> &aEnvironment,
193 const std::vector<ProcessCreateFlag_T> &aFlags,
194 ULONG aTimeoutMS,
195 ComPtr<IGuestProcess> &aGuestProcess);
196 HRESULT processCreateEx(const com::Utf8Str &aCommand,
197 const std::vector<com::Utf8Str> &aArguments,
198 const std::vector<com::Utf8Str> &aEnvironment,
199 const std::vector<ProcessCreateFlag_T> &aFlags,
200 ULONG aTimeoutMS,
201 ProcessPriority_T aPriority,
202 const std::vector<LONG> &aAffinity,
203 ComPtr<IGuestProcess> &aGuestProcess);
204 HRESULT processGet(ULONG aPid,
205 ComPtr<IGuestProcess> &aGuestProcess);
206 HRESULT symlinkCreate(const com::Utf8Str &aSource,
207 const com::Utf8Str &aTarget,
208 SymlinkType_T aType);
209 HRESULT symlinkExists(const com::Utf8Str &aSymlink,
210 BOOL *aExists);
211 HRESULT symlinkRead(const com::Utf8Str &aSymlink,
212 const std::vector<SymlinkReadFlag_T> &aFlags,
213 com::Utf8Str &aTarget);
214 HRESULT waitFor(ULONG aWaitFor,
215 ULONG aTimeoutMS,
216 GuestSessionWaitResult_T *aReason);
217 HRESULT waitForArray(const std::vector<GuestSessionWaitForFlag_T> &aWaitFor,
218 ULONG aTimeoutMS,
219 GuestSessionWaitResult_T *aReason);
220 /** @} */
221
222 /** Map of guest directories. The key specifies the internal directory ID. */
223 typedef std::map <uint32_t, ComObjPtr<GuestDirectory> > SessionDirectories;
224 /** Map of guest files. The key specifies the internal file ID. */
225 typedef std::map <uint32_t, ComObjPtr<GuestFile> > SessionFiles;
226 /** Map of guest processes. The key specifies the internal process number.
227 * To retrieve the process' guest PID use the Id() method of the IProcess interface. */
228 typedef std::map <uint32_t, ComObjPtr<GuestProcess> > SessionProcesses;
229
230 /** Guest session object type enumeration. */
231 enum SESSIONOBJECTTYPE
232 {
233 /** Anonymous object. */
234 SESSIONOBJECTTYPE_ANONYMOUS = 0,
235 /** Session object. */
236 SESSIONOBJECTTYPE_SESSION = 1,
237 /** Directory object. */
238 SESSIONOBJECTTYPE_DIRECTORY = 2,
239 /** File object. */
240 SESSIONOBJECTTYPE_FILE = 3,
241 /** Process object. */
242 SESSIONOBJECTTYPE_PROCESS = 4,
243 /** The usual 32-bit hack. */
244 SESSIONOBJECTTYPE_32BIT_HACK = 0x7fffffff
245 };
246
247 struct SessionObject
248 {
249 /** Creation timestamp (in ms).
250 * @note not used by anyone at the moment. */
251 uint64_t msBirth;
252 /** The object type. */
253 SESSIONOBJECTTYPE enmType;
254 };
255
256 /** Map containing all objects bound to a guest session.
257 * The key specifies the (global) context ID. */
258 typedef std::map <uint32_t, SessionObject> SessionObjects;
259 /** Queue containing context IDs which are no longer in use.
260 * Useful for quickly retrieving a new, unused context ID. */
261 typedef std::deque <uint32_t> SessionObjectsFree;
262
263public:
264 /** @name Public internal methods.
265 * @todo r=bird: Most of these are public for no real reason...
266 * @{ */
267 HRESULT i_copyFromGuest(const GuestSessionFsSourceSet &SourceSet, const com::Utf8Str &strDestination,
268 ComPtr<IProgress> &pProgress);
269 HRESULT i_copyToGuest(const GuestSessionFsSourceSet &SourceSet, const com::Utf8Str &strDestination,
270 ComPtr<IProgress> &pProgress);
271 int i_closeSession(uint32_t uFlags, uint32_t uTimeoutMS, int *pGuestRc);
272 HRESULT i_directoryCopyFlagFromStr(const com::Utf8Str &strFlags, DirectoryCopyFlag_T *pfFlags);
273 inline bool i_directoryExists(uint32_t uDirID, ComObjPtr<GuestDirectory> *pDir);
274 int i_directoryUnregister(GuestDirectory *pDirectory);
275 int i_directoryRemove(const Utf8Str &strPath, uint32_t uFlags, int *pGuestRc);
276 int i_directoryCreate(const Utf8Str &strPath, uint32_t uMode, uint32_t uFlags, int *pGuestRc);
277 int i_directoryOpen(const GuestDirectoryOpenInfo &openInfo,
278 ComObjPtr<GuestDirectory> &pDirectory, int *pGuestRc);
279 int i_directoryQueryInfo(const Utf8Str &strPath, bool fFollowSymlinks, GuestFsObjData &objData, int *pGuestRc);
280 int i_dispatchToObject(PVBOXGUESTCTRLHOSTCBCTX pCtxCb, PVBOXGUESTCTRLHOSTCALLBACK pSvcCb);
281 int i_dispatchToThis(PVBOXGUESTCTRLHOSTCBCTX pCtxCb, PVBOXGUESTCTRLHOSTCALLBACK pSvcCb);
282 HRESULT i_fileCopyFlagFromStr(const com::Utf8Str &strFlags, FileCopyFlag_T *pfFlags);
283 inline bool i_fileExists(uint32_t uFileID, ComObjPtr<GuestFile> *pFile);
284 int i_fileUnregister(GuestFile *pFile);
285 int i_fileRemove(const Utf8Str &strPath, int *pGuestRc);
286 int i_fileOpenEx(const com::Utf8Str &aPath, FileAccessMode_T aAccessMode, FileOpenAction_T aOpenAction,
287 FileSharingMode_T aSharingMode, ULONG aCreationMode,
288 const std::vector<FileOpenExFlag_T> &aFlags,
289 ComObjPtr<GuestFile> &pFile, int *prcGuest);
290 int i_fileOpen(const GuestFileOpenInfo &openInfo, ComObjPtr<GuestFile> &pFile, int *pGuestRc);
291 int i_fileQueryInfo(const Utf8Str &strPath, bool fFollowSymlinks, GuestFsObjData &objData, int *pGuestRc);
292 int i_fileQuerySize(const Utf8Str &strPath, bool fFollowSymlinks, int64_t *pllSize, int *pGuestRc);
293 int i_fsCreateTemp(const Utf8Str &strTemplate, const Utf8Str &strPath, bool fDirectory,
294 Utf8Str &strName, int *pGuestRc);
295 int i_fsQueryInfo(const Utf8Str &strPath, bool fFollowSymlinks, GuestFsObjData &objData, int *pGuestRc);
296 const GuestCredentials &i_getCredentials(void);
297 EventSource *i_getEventSource(void) { return mEventSource; }
298 Utf8Str i_getName(void);
299 ULONG i_getId(void) { return mData.mSession.mID; }
300 static Utf8Str i_guestErrorToString(int guestRc);
301 HRESULT i_isReadyExternal(void);
302 int i_onRemove(void);
303 int i_onSessionStatusChange(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData);
304 PathStyle_T i_getPathStyle(void);
305 int i_startSession(int *pGuestRc);
306 int i_startSessionAsync(void);
307 static void i_startSessionThreadTask(GuestSessionTaskInternalOpen *pTask);
308 Guest *i_getParent(void) { return mParent; }
309 uint32_t i_getProtocolVersion(void) { return mData.mProtocolVersion; }
310 int i_objectRegister(SESSIONOBJECTTYPE enmType, uint32_t *pidObject);
311 int i_objectUnregister(uint32_t uObjectID);
312 int i_pathRename(const Utf8Str &strSource, const Utf8Str &strDest, uint32_t uFlags, int *pGuestRc);
313 int i_pathUserDocuments(Utf8Str &strPath, int *prcGuest);
314 int i_pathUserHome(Utf8Str &strPath, int *prcGuest);
315 int i_processUnregister(GuestProcess *pProcess);
316 int i_processCreateEx(GuestProcessStartupInfo &procInfo, ComObjPtr<GuestProcess> &pProgress);
317 inline bool i_processExists(uint32_t uProcessID, ComObjPtr<GuestProcess> *pProcess);
318 inline int i_processGetByPID(ULONG uPID, ComObjPtr<GuestProcess> *pProcess);
319 int i_sendCommand(uint32_t uFunction, uint32_t uParms, PVBOXHGCMSVCPARM paParms,
320 uint64_t fDst = VBOX_GUESTCTRL_DST_SESSION);
321 static HRESULT i_setErrorExternal(VirtualBoxBase *pInterface, int guestRc);
322 int i_setSessionStatus(GuestSessionStatus_T sessionStatus, int sessionRc);
323 int i_signalWaiters(GuestSessionWaitResult_T enmWaitResult, int rc /*= VINF_SUCCESS */);
324 int i_determineProtocolVersion(void);
325 int i_waitFor(uint32_t fWaitFlags, ULONG uTimeoutMS, GuestSessionWaitResult_T &waitResult, int *pGuestRc);
326 int i_waitForStatusChange(GuestWaitEvent *pEvent, uint32_t fWaitFlags, uint32_t uTimeoutMS,
327 GuestSessionStatus_T *pSessionStatus, int *pGuestRc);
328 /** @} */
329
330private:
331
332 /** Pointer to the parent (Guest). */
333 Guest *mParent;
334 /**
335 * The session's event source. This source is used for
336 * serving the internal listener as well as all other
337 * external listeners that may register to it.
338 *
339 * Note: This can safely be used without holding any locks.
340 * An AutoCaller suffices to prevent it being destroy while in use and
341 * internally there is a lock providing the necessary serialization.
342 */
343 const ComObjPtr<EventSource> mEventSource;
344
345 /** @todo r=bird: One of the core points of the DATA sub-structures in Main is
346 * hinding implementation details and stuff that requires including iprt/asm.h.
347 * The way it's used here totally defeats that purpose. You need to make it
348 * a pointer to a anynmous Data struct and define that structure in
349 * GuestSessionImpl.cpp and allocate it in the Init() function.
350 */
351 struct Data
352 {
353 /** The session credentials. */
354 GuestCredentials mCredentials;
355 /** The session's startup info. */
356 GuestSessionStartupInfo mSession;
357 /** The session's object ID.
358 * Needed for registering wait events which are bound directly to this session. */
359 uint32_t mObjectID;
360 /** The session's current status. */
361 GuestSessionStatus_T mStatus;
362 /** The set of environment changes for the session for use when
363 * creating new guest processes. */
364 GuestEnvironmentChanges mEnvironmentChanges;
365 /** Pointer to the immutable base environment for the session.
366 * @note This is not allocated until the guest reports it to the host. It is
367 * also shared with child processes. */
368 GuestEnvironment const *mpBaseEnvironment;
369 /** Directory objects bound to this session. */
370 SessionDirectories mDirectories;
371 /** File objects bound to this session. */
372 SessionFiles mFiles;
373 /** Process objects bound to this session. */
374 SessionProcesses mProcesses;
375 /** Map of registered session objects (files, directories, ...). */
376 SessionObjects mObjects;
377 /** Guest control protocol version to be used.
378 * Guest Additions < VBox 4.3 have version 1,
379 * any newer version will have version 2. */
380 uint32_t mProtocolVersion;
381 /** Session timeout (in ms). */
382 uint32_t mTimeout;
383 /** The last returned session status
384 * returned from the guest side. */
385 int mRC;
386 /** Object ID allocation bitmap; clear bits are free, set bits are busy. */
387 uint64_t bmObjectIds[VBOX_GUESTCTRL_MAX_OBJECTS / sizeof(uint64_t) / 8];
388
389 Data(void)
390 : mpBaseEnvironment(NULL)
391 {
392 RT_ZERO(bmObjectIds);
393 ASMBitSet(&bmObjectIds, VBOX_GUESTCTRL_MAX_OBJECTS - 1); /* Reserved for the session itself? */
394 ASMBitSet(&bmObjectIds, 0); /* Let's reserve this too. */
395 }
396 Data(const Data &rThat)
397 : mCredentials(rThat.mCredentials)
398 , mSession(rThat.mSession)
399 , mStatus(rThat.mStatus)
400 , mEnvironmentChanges(rThat.mEnvironmentChanges)
401 , mpBaseEnvironment(NULL)
402 , mDirectories(rThat.mDirectories)
403 , mFiles(rThat.mFiles)
404 , mProcesses(rThat.mProcesses)
405 , mObjects(rThat.mObjects)
406 , mProtocolVersion(rThat.mProtocolVersion)
407 , mTimeout(rThat.mTimeout)
408 , mRC(rThat.mRC)
409 {
410 memcpy(&bmObjectIds, &rThat.bmObjectIds, sizeof(bmObjectIds));
411 }
412 ~Data(void)
413 {
414 if (mpBaseEnvironment)
415 {
416 mpBaseEnvironment->releaseConst();
417 mpBaseEnvironment = NULL;
418 }
419 }
420 } mData;
421};
422
423#endif /* !____H_GUESTSESSIONIMPL */
424
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette