VirtualBox

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

Last change on this file since 71648 was 71648, checked in by vboxsync, 7 years ago

Guest Control/Main: Added a dedicated header file for guest session tasks.

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