VirtualBox

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

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

VBoxGuestControl: Optimizing message handling - part 1. bugref:9313

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