VirtualBox

source: vbox/trunk/src/VBox/Main/include/GuestCtrlImplPrivate.h@ 79285

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

Main/GuestFileImpl.cpp: Translate FileAccessMode_AppendOnly and AppendRead to 'a' and 'a+' respectively and let the guest side do the invalid parmaeter bit if too old. bugref:9320

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 35.2 KB
Line 
1/* $Id: GuestCtrlImplPrivate.h 79285 2019-06-21 21:52:34Z vboxsync $ */
2/** @file
3 * Internal helpers/structures for guest control functionality.
4 */
5
6/*
7 * Copyright (C) 2011-2019 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 MAIN_INCLUDED_GuestCtrlImplPrivate_h
19#define MAIN_INCLUDED_GuestCtrlImplPrivate_h
20#ifndef RT_WITHOUT_PRAGMA_ONCE
21# pragma once
22#endif
23
24#include "ConsoleImpl.h"
25#include "Global.h"
26
27#include <iprt/asm.h>
28#include <iprt/env.h>
29#include <iprt/semaphore.h>
30#include <iprt/cpp/utils.h>
31
32#include <VBox/com/com.h>
33#include <VBox/com/ErrorInfo.h>
34#include <VBox/com/string.h>
35#include <VBox/com/VirtualBox.h>
36#include <VBox/err.h> /* VERR_GSTCTL_GUEST_ERROR */
37
38#include <map>
39#include <vector>
40
41using namespace com;
42
43#ifdef VBOX_WITH_GUEST_CONTROL
44# include <VBox/GuestHost/GuestControl.h>
45# include <VBox/HostServices/GuestControlSvc.h>
46using namespace guestControl;
47#endif
48
49/** Vector holding a process' CPU affinity. */
50typedef std::vector <LONG> ProcessAffinity;
51/** Vector holding process startup arguments. */
52typedef std::vector <Utf8Str> ProcessArguments;
53
54class GuestProcessStreamBlock;
55class GuestSession;
56
57
58/**
59 * Simple structure mantaining guest credentials.
60 */
61struct GuestCredentials
62{
63 Utf8Str mUser;
64 Utf8Str mPassword;
65 Utf8Str mDomain;
66};
67
68
69/**
70 * Wrapper around the RTEnv API, unusable base class.
71 *
72 * @remarks Feel free to elevate this class to iprt/cpp/env.h as RTCEnv.
73 */
74class GuestEnvironmentBase
75{
76public:
77 /**
78 * Default constructor.
79 *
80 * The user must invoke one of the init methods before using the object.
81 */
82 GuestEnvironmentBase(void)
83 : m_hEnv(NIL_RTENV)
84 , m_cRefs(1)
85 { }
86
87 /**
88 * Destructor.
89 */
90 virtual ~GuestEnvironmentBase(void)
91 {
92 Assert(m_cRefs <= 1);
93 int rc = RTEnvDestroy(m_hEnv); AssertRC(rc);
94 m_hEnv = NIL_RTENV;
95 }
96
97 /**
98 * Retains a reference to this object.
99 * @returns New reference count.
100 * @remarks Sharing an object is currently only safe if no changes are made to
101 * it because RTENV does not yet implement any locking. For the only
102 * purpose we need this, implementing IGuestProcess::environment by
103 * using IGuestSession::environmentBase, that's fine as the session
104 * base environment is immutable.
105 */
106 uint32_t retain(void)
107 {
108 uint32_t cRefs = ASMAtomicIncU32(&m_cRefs);
109 Assert(cRefs > 1); Assert(cRefs < _1M);
110 return cRefs;
111
112 }
113 /** Useful shortcut. */
114 uint32_t retainConst(void) const { return unconst(this)->retain(); }
115
116 /**
117 * Releases a reference to this object, deleting the object when reaching zero.
118 * @returns New reference count.
119 */
120 uint32_t release(void)
121 {
122 uint32_t cRefs = ASMAtomicDecU32(&m_cRefs);
123 Assert(cRefs < _1M);
124 if (cRefs == 0)
125 delete this;
126 return cRefs;
127 }
128
129 /** Useful shortcut. */
130 uint32_t releaseConst(void) const { return unconst(this)->retain(); }
131
132 /**
133 * Checks if the environment has been successfully initialized or not.
134 *
135 * @returns @c true if initialized, @c false if not.
136 */
137 bool isInitialized(void) const
138 {
139 return m_hEnv != NIL_RTENV;
140 }
141
142 /**
143 * Returns the variable count.
144 * @return Number of variables.
145 * @sa RTEnvCountEx
146 */
147 uint32_t count(void) const
148 {
149 return RTEnvCountEx(m_hEnv);
150 }
151
152 /**
153 * Deletes the environment change record entirely.
154 *
155 * The count() method will return zero after this call.
156 *
157 * @sa RTEnvReset
158 */
159 void reset(void)
160 {
161 int rc = RTEnvReset(m_hEnv);
162 AssertRC(rc);
163 }
164
165 /**
166 * Exports the environment change block as an array of putenv style strings.
167 *
168 *
169 * @returns VINF_SUCCESS or VERR_NO_MEMORY.
170 * @param pArray The output array.
171 */
172 int queryPutEnvArray(std::vector<com::Utf8Str> *pArray) const
173 {
174 uint32_t cVars = RTEnvCountEx(m_hEnv);
175 try
176 {
177 pArray->resize(cVars);
178 for (uint32_t iVar = 0; iVar < cVars; iVar++)
179 {
180 const char *psz = RTEnvGetByIndexRawEx(m_hEnv, iVar);
181 AssertReturn(psz, VERR_INTERNAL_ERROR_3); /* someone is racing us! */
182 (*pArray)[iVar] = psz;
183 }
184 return VINF_SUCCESS;
185 }
186 catch (std::bad_alloc &)
187 {
188 return VERR_NO_MEMORY;
189 }
190 }
191
192 /**
193 * Applies an array of putenv style strings.
194 *
195 * @returns IPRT status code.
196 * @param rArray The array with the putenv style strings.
197 * @sa RTEnvPutEnvEx
198 */
199 int applyPutEnvArray(const std::vector<com::Utf8Str> &rArray)
200 {
201 size_t cArray = rArray.size();
202 for (size_t i = 0; i < cArray; i++)
203 {
204 int rc = RTEnvPutEx(m_hEnv, rArray[i].c_str());
205 if (RT_FAILURE(rc))
206 return rc;
207 }
208 return VINF_SUCCESS;
209 }
210
211 /**
212 * Applies the changes from another environment to this.
213 *
214 * @returns IPRT status code.
215 * @param rChanges Reference to an environment which variables will be
216 * imported and, if it's a change record, schedule
217 * variable unsets will be applied.
218 * @sa RTEnvApplyChanges
219 */
220 int applyChanges(const GuestEnvironmentBase &rChanges)
221 {
222 return RTEnvApplyChanges(m_hEnv, rChanges.m_hEnv);
223 }
224
225 /**
226 * See RTEnvQueryUtf8Block for details.
227 * @returns IPRT status code.
228 * @param ppszzBlock Where to return the block pointer.
229 * @param pcbBlock Where to optionally return the block size.
230 * @sa RTEnvQueryUtf8Block
231 */
232 int queryUtf8Block(char **ppszzBlock, size_t *pcbBlock)
233 {
234 return RTEnvQueryUtf8Block(m_hEnv, true /*fSorted*/, ppszzBlock, pcbBlock);
235 }
236
237 /**
238 * Frees what queryUtf8Block returned, NULL ignored.
239 * @sa RTEnvFreeUtf8Block
240 */
241 static void freeUtf8Block(char *pszzBlock)
242 {
243 return RTEnvFreeUtf8Block(pszzBlock);
244 }
245
246 /**
247 * Applies a block on the format returned by queryUtf8Block.
248 *
249 * @returns IPRT status code.
250 * @param pszzBlock Pointer to the block.
251 * @param cbBlock The size of the block.
252 * @param fNoEqualMeansUnset Whether the lack of a '=' (equal) sign in a
253 * string means it should be unset (@c true), or if
254 * it means the variable should be defined with an
255 * empty value (@c false, the default).
256 * @todo move this to RTEnv!
257 */
258 int copyUtf8Block(const char *pszzBlock, size_t cbBlock, bool fNoEqualMeansUnset = false)
259 {
260 int rc = VINF_SUCCESS;
261 while (cbBlock > 0 && *pszzBlock != '\0')
262 {
263 const char *pszEnd = (const char *)memchr(pszzBlock, '\0', cbBlock);
264 if (!pszEnd)
265 return VERR_BUFFER_UNDERFLOW;
266 int rc2;
267 if (fNoEqualMeansUnset || strchr(pszzBlock, '='))
268 rc2 = RTEnvPutEx(m_hEnv, pszzBlock);
269 else
270 rc2 = RTEnvSetEx(m_hEnv, pszzBlock, "");
271 if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
272 rc = rc2;
273
274 /* Advance. */
275 cbBlock -= pszEnd - pszzBlock;
276 if (cbBlock < 2)
277 return VERR_BUFFER_UNDERFLOW;
278 cbBlock--;
279 pszzBlock = pszEnd + 1;
280 }
281
282 /* The remainder must be zero padded. */
283 if (RT_SUCCESS(rc))
284 {
285 if (ASMMemIsZero(pszzBlock, cbBlock))
286 return VINF_SUCCESS;
287 return VERR_TOO_MUCH_DATA;
288 }
289 return rc;
290 }
291
292 /**
293 * Get an environment variable.
294 *
295 * @returns IPRT status code.
296 * @param rName The variable name.
297 * @param pValue Where to return the value.
298 * @sa RTEnvGetEx
299 */
300 int getVariable(const com::Utf8Str &rName, com::Utf8Str *pValue) const
301 {
302 size_t cchNeeded;
303 int rc = RTEnvGetEx(m_hEnv, rName.c_str(), NULL, 0, &cchNeeded);
304 if ( RT_SUCCESS(rc)
305 || rc == VERR_BUFFER_OVERFLOW)
306 {
307 try
308 {
309 pValue->reserve(cchNeeded + 1);
310 rc = RTEnvGetEx(m_hEnv, rName.c_str(), pValue->mutableRaw(), pValue->capacity(), NULL);
311 pValue->jolt();
312 }
313 catch (std::bad_alloc &)
314 {
315 rc = VERR_NO_STR_MEMORY;
316 }
317 }
318 return rc;
319 }
320
321 /**
322 * Checks if the given variable exists.
323 *
324 * @returns @c true if it exists, @c false if not or if it's an scheduled unset
325 * in a environment change record.
326 * @param rName The variable name.
327 * @sa RTEnvExistEx
328 */
329 bool doesVariableExist(const com::Utf8Str &rName) const
330 {
331 return RTEnvExistEx(m_hEnv, rName.c_str());
332 }
333
334 /**
335 * Set an environment variable.
336 *
337 * @returns IPRT status code.
338 * @param rName The variable name.
339 * @param rValue The value of the variable.
340 * @sa RTEnvSetEx
341 */
342 int setVariable(const com::Utf8Str &rName, const com::Utf8Str &rValue)
343 {
344 return RTEnvSetEx(m_hEnv, rName.c_str(), rValue.c_str());
345 }
346
347 /**
348 * Unset an environment variable.
349 *
350 * @returns IPRT status code.
351 * @param rName The variable name.
352 * @sa RTEnvUnsetEx
353 */
354 int unsetVariable(const com::Utf8Str &rName)
355 {
356 return RTEnvUnsetEx(m_hEnv, rName.c_str());
357 }
358
359protected:
360 /**
361 * Copy constructor.
362 * @throws HRESULT
363 */
364 GuestEnvironmentBase(const GuestEnvironmentBase &rThat, bool fChangeRecord)
365 : m_hEnv(NIL_RTENV)
366 , m_cRefs(1)
367 {
368 int rc = cloneCommon(rThat, fChangeRecord);
369 if (RT_FAILURE(rc))
370 throw (Global::vboxStatusCodeToCOM(rc));
371 }
372
373 /**
374 * Common clone/copy method with type conversion abilities.
375 *
376 * @returns IPRT status code.
377 * @param rThat The object to clone.
378 * @param fChangeRecord Whether the this instance is a change record (true)
379 * or normal (false) environment.
380 */
381 int cloneCommon(const GuestEnvironmentBase &rThat, bool fChangeRecord)
382 {
383 int rc = VINF_SUCCESS;
384 RTENV hNewEnv = NIL_RTENV;
385 if (rThat.m_hEnv != NIL_RTENV)
386 {
387 /*
388 * Clone it.
389 */
390 if (RTEnvIsChangeRecord(rThat.m_hEnv) == fChangeRecord)
391 rc = RTEnvClone(&hNewEnv, rThat.m_hEnv);
392 else
393 {
394 /* Need to type convert it. */
395 if (fChangeRecord)
396 rc = RTEnvCreateChangeRecord(&hNewEnv);
397 else
398 rc = RTEnvCreate(&hNewEnv);
399 if (RT_SUCCESS(rc))
400 {
401 rc = RTEnvApplyChanges(hNewEnv, rThat.m_hEnv);
402 if (RT_FAILURE(rc))
403 RTEnvDestroy(hNewEnv);
404 }
405 }
406 }
407 else
408 {
409 /*
410 * Create an empty one so the object works smoothly.
411 * (Relevant for GuestProcessStartupInfo and internal commands.)
412 */
413 if (fChangeRecord)
414 rc = RTEnvCreateChangeRecord(&hNewEnv);
415 else
416 rc = RTEnvCreate(&hNewEnv);
417 }
418 if (RT_SUCCESS(rc))
419 {
420 RTEnvDestroy(m_hEnv);
421 m_hEnv = hNewEnv;
422 }
423 return rc;
424 }
425
426
427 /** The environment change record. */
428 RTENV m_hEnv;
429 /** Reference counter. */
430 uint32_t volatile m_cRefs;
431};
432
433class GuestEnvironmentChanges;
434
435
436/**
437 * Wrapper around the RTEnv API for a normal environment.
438 */
439class GuestEnvironment : public GuestEnvironmentBase
440{
441public:
442 /**
443 * Default constructor.
444 *
445 * The user must invoke one of the init methods before using the object.
446 */
447 GuestEnvironment(void)
448 : GuestEnvironmentBase()
449 { }
450
451 /**
452 * Copy operator.
453 * @param rThat The object to copy.
454 * @throws HRESULT
455 */
456 GuestEnvironment(const GuestEnvironment &rThat)
457 : GuestEnvironmentBase(rThat, false /*fChangeRecord*/)
458 { }
459
460 /**
461 * Copy operator.
462 * @param rThat The object to copy.
463 * @throws HRESULT
464 */
465 GuestEnvironment(const GuestEnvironmentBase &rThat)
466 : GuestEnvironmentBase(rThat, false /*fChangeRecord*/)
467 { }
468
469 /**
470 * Initialize this as a normal environment block.
471 * @returns IPRT status code.
472 */
473 int initNormal(void)
474 {
475 AssertReturn(m_hEnv == NIL_RTENV, VERR_WRONG_ORDER);
476 return RTEnvCreate(&m_hEnv);
477 }
478
479 /**
480 * Replaces this environemnt with that in @a rThat.
481 *
482 * @returns IPRT status code
483 * @param rThat The environment to copy. If it's a different type
484 * we'll convert the data to a normal environment block.
485 */
486 int copy(const GuestEnvironmentBase &rThat)
487 {
488 return cloneCommon(rThat, false /*fChangeRecord*/);
489 }
490
491 /**
492 * @copydoc copy()
493 */
494 GuestEnvironment &operator=(const GuestEnvironmentBase &rThat)
495 {
496 int rc = cloneCommon(rThat, true /*fChangeRecord*/);
497 if (RT_FAILURE(rc))
498 throw (Global::vboxStatusCodeToCOM(rc));
499 return *this;
500 }
501
502 /** @copydoc copy() */
503 GuestEnvironment &operator=(const GuestEnvironment &rThat)
504 { return operator=((const GuestEnvironmentBase &)rThat); }
505
506 /** @copydoc copy() */
507 GuestEnvironment &operator=(const GuestEnvironmentChanges &rThat)
508 { return operator=((const GuestEnvironmentBase &)rThat); }
509
510};
511
512
513/**
514 * Wrapper around the RTEnv API for a environment change record.
515 *
516 * This class is used as a record of changes to be applied to a different
517 * environment block (in VBoxService before launching a new process).
518 */
519class GuestEnvironmentChanges : public GuestEnvironmentBase
520{
521public:
522 /**
523 * Default constructor.
524 *
525 * The user must invoke one of the init methods before using the object.
526 */
527 GuestEnvironmentChanges(void)
528 : GuestEnvironmentBase()
529 { }
530
531 /**
532 * Copy operator.
533 * @param rThat The object to copy.
534 * @throws HRESULT
535 */
536 GuestEnvironmentChanges(const GuestEnvironmentChanges &rThat)
537 : GuestEnvironmentBase(rThat, true /*fChangeRecord*/)
538 { }
539
540 /**
541 * Copy operator.
542 * @param rThat The object to copy.
543 * @throws HRESULT
544 */
545 GuestEnvironmentChanges(const GuestEnvironmentBase &rThat)
546 : GuestEnvironmentBase(rThat, true /*fChangeRecord*/)
547 { }
548
549 /**
550 * Initialize this as a environment change record.
551 * @returns IPRT status code.
552 */
553 int initChangeRecord(void)
554 {
555 AssertReturn(m_hEnv == NIL_RTENV, VERR_WRONG_ORDER);
556 return RTEnvCreateChangeRecord(&m_hEnv);
557 }
558
559 /**
560 * Replaces this environemnt with that in @a rThat.
561 *
562 * @returns IPRT status code
563 * @param rThat The environment to copy. If it's a different type
564 * we'll convert the data to a set of changes.
565 */
566 int copy(const GuestEnvironmentBase &rThat)
567 {
568 return cloneCommon(rThat, true /*fChangeRecord*/);
569 }
570
571 /**
572 * @copydoc copy()
573 */
574 GuestEnvironmentChanges &operator=(const GuestEnvironmentBase &rThat)
575 {
576 int rc = cloneCommon(rThat, true /*fChangeRecord*/);
577 if (RT_FAILURE(rc))
578 throw (Global::vboxStatusCodeToCOM(rc));
579 return *this;
580 }
581
582 /** @copydoc copy() */
583 GuestEnvironmentChanges &operator=(const GuestEnvironmentChanges &rThat)
584 { return operator=((const GuestEnvironmentBase &)rThat); }
585
586 /** @copydoc copy() */
587 GuestEnvironmentChanges &operator=(const GuestEnvironment &rThat)
588 { return operator=((const GuestEnvironmentBase &)rThat); }
589};
590
591
592/**
593 * Structure for keeping all the relevant guest directory
594 * information around.
595 */
596struct GuestDirectoryOpenInfo
597{
598 /** The directory path. */
599 Utf8Str mPath;
600 /** Then open filter. */
601 Utf8Str mFilter;
602 /** Opening flags. */
603 uint32_t mFlags;
604};
605
606
607/**
608 * Structure for keeping all the relevant guest file
609 * information around.
610 */
611struct GuestFileOpenInfo
612{
613 /** The filename. */
614 Utf8Str mFilename;
615 /** The file access mode. */
616 FileAccessMode_T mAccessMode;
617 /** The file open action. */
618 FileOpenAction_T mOpenAction;
619 /** The file sharing mode. */
620 FileSharingMode_T mSharingMode;
621 /** Octal creation mode. */
622 uint32_t mCreationMode;
623 /** Extended open flags (currently none defined). */
624 uint32_t mfOpenEx;
625};
626
627
628/**
629 * Structure representing information of a
630 * file system object.
631 */
632struct GuestFsObjData
633{
634 /** @name Helper functions to extract the data from a certin VBoxService tool's guest stream block.
635 * @{ */
636 int FromLs(const GuestProcessStreamBlock &strmBlk, bool fLong);
637 int FromStat(const GuestProcessStreamBlock &strmBlk);
638 int FromMkTemp(const GuestProcessStreamBlock &strmBlk);
639 /** @} */
640
641 /** @name Static helper functions to work with time from stream block keys.
642 * @{ */
643 static PRTTIMESPEC TimeSpecFromKey(const GuestProcessStreamBlock &strmBlk, const Utf8Str &strKey, PRTTIMESPEC pTimeSpec);
644 static int64_t UnixEpochNsFromKey(const GuestProcessStreamBlock &strmBlk, const Utf8Str &strKey);
645 /** @} */
646
647 /** @name helper functions to work with IPRT stuff.
648 * @{ */
649 RTFMODE GetFileMode(void) const;
650 /** @} */
651
652 Utf8Str mName;
653 FsObjType_T mType;
654 Utf8Str mFileAttrs;
655 int64_t mObjectSize;
656 int64_t mAllocatedSize;
657 int64_t mAccessTime;
658 int64_t mBirthTime;
659 int64_t mChangeTime;
660 int64_t mModificationTime;
661 Utf8Str mUserName;
662 int32_t mUID;
663 int32_t mGID;
664 Utf8Str mGroupName;
665 Utf8Str mACL;
666 int64_t mNodeID;
667 uint32_t mNodeIDDevice;
668 uint32_t mNumHardLinks;
669 uint32_t mDeviceNumber;
670 uint32_t mGenerationID;
671 uint32_t mUserFlags;
672};
673
674
675/**
676 * Structure for keeping all the relevant guest session
677 * startup parameters around.
678 */
679class GuestSessionStartupInfo
680{
681public:
682
683 GuestSessionStartupInfo(void)
684 : mIsInternal(false /* Non-internal session */),
685 mOpenTimeoutMS(30 * 1000 /* 30s opening timeout */),
686 mOpenFlags(0 /* No opening flags set */) { }
687
688 /** The session's friendly name. Optional. */
689 Utf8Str mName;
690 /** The session's unique ID. Used to encode a context ID. */
691 uint32_t mID;
692 /** Flag indicating if this is an internal session
693 * or not. Internal session are not accessible by
694 * public API clients. */
695 bool mIsInternal;
696 /** Timeout (in ms) used for opening the session. */
697 uint32_t mOpenTimeoutMS;
698 /** Session opening flags. */
699 uint32_t mOpenFlags;
700};
701
702
703/**
704 * Structure for keeping all the relevant guest process
705 * startup parameters around.
706 */
707class GuestProcessStartupInfo
708{
709public:
710
711 GuestProcessStartupInfo(void)
712 : mFlags(ProcessCreateFlag_None),
713 mTimeoutMS(UINT32_MAX /* No timeout by default */),
714 mPriority(ProcessPriority_Default) { }
715
716 /** The process' friendly name. */
717 Utf8Str mName;
718 /** The executable. */
719 Utf8Str mExecutable;
720 /** Arguments vector (starting with argument \#0). */
721 ProcessArguments mArguments;
722 /** The process environment change record. */
723 GuestEnvironmentChanges mEnvironmentChanges;
724 /** Process creation flags. */
725 uint32_t mFlags;
726 /** Timeout (in ms) the process is allowed to run.
727 * Specify UINT32_MAX if no timeout (unlimited run time) is given. */
728 ULONG mTimeoutMS;
729 /** Process priority. */
730 ProcessPriority_T mPriority;
731 /** Process affinity. At the moment we
732 * only support 64 VCPUs. API and
733 * guest can do more already! */
734 uint64_t mAffinity;
735};
736
737
738/**
739 * Class representing the "value" side of a "key=value" pair.
740 */
741class GuestProcessStreamValue
742{
743public:
744
745 GuestProcessStreamValue(void) { }
746 GuestProcessStreamValue(const char *pszValue)
747 : mValue(pszValue) {}
748
749 GuestProcessStreamValue(const GuestProcessStreamValue& aThat)
750 : mValue(aThat.mValue) { }
751
752 Utf8Str mValue;
753};
754
755/** Map containing "key=value" pairs of a guest process stream. */
756typedef std::pair< Utf8Str, GuestProcessStreamValue > GuestCtrlStreamPair;
757typedef std::map < Utf8Str, GuestProcessStreamValue > GuestCtrlStreamPairMap;
758typedef std::map < Utf8Str, GuestProcessStreamValue >::iterator GuestCtrlStreamPairMapIter;
759typedef std::map < Utf8Str, GuestProcessStreamValue >::const_iterator GuestCtrlStreamPairMapIterConst;
760
761/**
762 * Class representing a block of stream pairs (key=value). Each block in a raw guest
763 * output stream is separated by "\0\0", each pair is separated by "\0". The overall
764 * end of a guest stream is marked by "\0\0\0\0".
765 */
766class GuestProcessStreamBlock
767{
768public:
769
770 GuestProcessStreamBlock(void);
771
772 virtual ~GuestProcessStreamBlock(void);
773
774public:
775
776 void Clear(void);
777
778#ifdef DEBUG
779 void DumpToLog(void) const;
780#endif
781
782 const char *GetString(const char *pszKey) const;
783 size_t GetCount(void) const;
784 int GetRc(void) const;
785 int GetInt64Ex(const char *pszKey, int64_t *piVal) const;
786 int64_t GetInt64(const char *pszKey) const;
787 int GetUInt32Ex(const char *pszKey, uint32_t *puVal) const;
788 uint32_t GetUInt32(const char *pszKey, uint32_t uDefault = 0) const;
789 int32_t GetInt32(const char *pszKey, int32_t iDefault = 0) const;
790
791 bool IsEmpty(void) { return mPairs.empty(); }
792
793 int SetValue(const char *pszKey, const char *pszValue);
794
795protected:
796
797 GuestCtrlStreamPairMap mPairs;
798};
799
800/** Vector containing multiple allocated stream pair objects. */
801typedef std::vector< GuestProcessStreamBlock > GuestCtrlStreamObjects;
802typedef std::vector< GuestProcessStreamBlock >::iterator GuestCtrlStreamObjectsIter;
803typedef std::vector< GuestProcessStreamBlock >::const_iterator GuestCtrlStreamObjectsIterConst;
804
805/**
806 * Class for parsing machine-readable guest process output by VBoxService'
807 * toolbox commands ("vbox_ls", "vbox_stat" etc), aka "guest stream".
808 */
809class GuestProcessStream
810{
811
812public:
813
814 GuestProcessStream();
815
816 virtual ~GuestProcessStream();
817
818public:
819
820 int AddData(const BYTE *pbData, size_t cbData);
821
822 void Destroy();
823
824#ifdef DEBUG
825 void Dump(const char *pszFile);
826#endif
827
828 size_t GetOffset() { return m_offBuffer; }
829
830 size_t GetSize() { return m_cbUsed; }
831
832 int ParseBlock(GuestProcessStreamBlock &streamBlock);
833
834protected:
835
836 /** Currently allocated size of internal stream buffer. */
837 size_t m_cbAllocated;
838 /** Currently used size at m_offBuffer. */
839 size_t m_cbUsed;
840 /** Current byte offset within the internal stream buffer. */
841 size_t m_offBuffer;
842 /** Internal stream buffer. */
843 BYTE *m_pbBuffer;
844};
845
846class Guest;
847class Progress;
848
849class GuestTask
850{
851
852public:
853
854 enum TaskType
855 {
856 /** Copies a file from host to the guest. */
857 TaskType_CopyFileToGuest = 50,
858 /** Copies a file from guest to the host. */
859 TaskType_CopyFileFromGuest = 55,
860 /** Update Guest Additions by directly copying the required installer
861 * off the .ISO file, transfer it to the guest and execute the installer
862 * with system privileges. */
863 TaskType_UpdateGuestAdditions = 100
864 };
865
866 GuestTask(TaskType aTaskType, Guest *aThat, Progress *aProgress);
867
868 virtual ~GuestTask();
869
870 int startThread();
871
872 static int taskThread(RTTHREAD aThread, void *pvUser);
873 static int uploadProgress(unsigned uPercent, void *pvUser);
874 static HRESULT setProgressSuccess(ComObjPtr<Progress> pProgress);
875 static HRESULT setProgressErrorMsg(HRESULT hr,
876 ComObjPtr<Progress> pProgress, const char * pszText, ...);
877 static HRESULT setProgressErrorParent(HRESULT hr,
878 ComObjPtr<Progress> pProgress, ComObjPtr<Guest> pGuest);
879
880 TaskType taskType;
881 ComObjPtr<Guest> pGuest;
882 ComObjPtr<Progress> pProgress;
883 HRESULT rc;
884
885 /* Task data. */
886 Utf8Str strSource;
887 Utf8Str strDest;
888 Utf8Str strUserName;
889 Utf8Str strPassword;
890 ULONG uFlags;
891};
892
893class GuestWaitEventPayload
894{
895
896public:
897
898 GuestWaitEventPayload(void)
899 : uType(0),
900 cbData(0),
901 pvData(NULL) { }
902
903 GuestWaitEventPayload(uint32_t uTypePayload,
904 const void *pvPayload, uint32_t cbPayload)
905 : uType(0),
906 cbData(0),
907 pvData(NULL)
908 {
909 int rc = copyFrom(uTypePayload, pvPayload, cbPayload);
910 if (RT_FAILURE(rc))
911 throw rc;
912 }
913
914 virtual ~GuestWaitEventPayload(void)
915 {
916 Clear();
917 }
918
919 GuestWaitEventPayload& operator=(const GuestWaitEventPayload &that)
920 {
921 CopyFromDeep(that);
922 return *this;
923 }
924
925public:
926
927 void Clear(void)
928 {
929 if (pvData)
930 {
931 Assert(cbData);
932 RTMemFree(pvData);
933 cbData = 0;
934 pvData = NULL;
935 }
936 uType = 0;
937 }
938
939 int CopyFromDeep(const GuestWaitEventPayload &payload)
940 {
941 return copyFrom(payload.uType, payload.pvData, payload.cbData);
942 }
943
944 const void* Raw(void) const { return pvData; }
945
946 size_t Size(void) const { return cbData; }
947
948 uint32_t Type(void) const { return uType; }
949
950 void* MutableRaw(void) { return pvData; }
951
952 Utf8Str ToString(void)
953 {
954 const char *pszStr = (const char *)pvData;
955 size_t cbStr = cbData;
956
957 if (RT_FAILURE(RTStrValidateEncodingEx(pszStr, cbStr,
958 RTSTR_VALIDATE_ENCODING_ZERO_TERMINATED | RTSTR_VALIDATE_ENCODING_EXACT_LENGTH)))
959 {
960 AssertFailed();
961 return "";
962 }
963
964 return Utf8Str(pszStr, cbStr);
965 }
966
967protected:
968
969 int copyFrom(uint32_t uTypePayload, const void *pvPayload, uint32_t cbPayload)
970 {
971 if (cbPayload > _64K) /* Paranoia. */
972 return VERR_TOO_MUCH_DATA;
973
974 Clear();
975
976 int rc = VINF_SUCCESS;
977
978 if (cbPayload)
979 {
980 pvData = RTMemAlloc(cbPayload);
981 if (pvData)
982 {
983 uType = uTypePayload;
984
985 memcpy(pvData, pvPayload, cbPayload);
986 cbData = cbPayload;
987 }
988 else
989 rc = VERR_NO_MEMORY;
990 }
991 else
992 {
993 uType = uTypePayload;
994
995 pvData = NULL;
996 cbData = 0;
997 }
998
999 return rc;
1000 }
1001
1002protected:
1003
1004 /** Type of payload. */
1005 uint32_t uType;
1006 /** Size (in bytes) of payload. */
1007 uint32_t cbData;
1008 /** Pointer to actual payload data. */
1009 void *pvData;
1010};
1011
1012class GuestWaitEventBase
1013{
1014
1015protected:
1016
1017 GuestWaitEventBase(void);
1018 virtual ~GuestWaitEventBase(void);
1019
1020public:
1021
1022 uint32_t ContextID(void) { return mCID; };
1023 int GuestResult(void) { return mGuestRc; }
1024 int Result(void) { return mRc; }
1025 GuestWaitEventPayload & Payload(void) { return mPayload; }
1026 int SignalInternal(int rc, int guestRc, const GuestWaitEventPayload *pPayload);
1027 int Wait(RTMSINTERVAL uTimeoutMS);
1028
1029protected:
1030
1031 int Init(uint32_t uCID);
1032
1033protected:
1034
1035 /* Shutdown indicator. */
1036 bool mfAborted;
1037 /* Associated context ID (CID). */
1038 uint32_t mCID;
1039 /** The event semaphore for triggering
1040 * the actual event. */
1041 RTSEMEVENT mEventSem;
1042 /** The event's overall result. If
1043 * set to VERR_GSTCTL_GUEST_ERROR,
1044 * mGuestRc will contain the actual
1045 * error code from the guest side. */
1046 int mRc;
1047 /** The event'S overall result from the
1048 * guest side. If used, mRc must be
1049 * set to VERR_GSTCTL_GUEST_ERROR. */
1050 int mGuestRc;
1051 /** The event's payload data. Optional. */
1052 GuestWaitEventPayload mPayload;
1053};
1054
1055/** List of public guest event types. */
1056typedef std::list < VBoxEventType_T > GuestEventTypes;
1057
1058class GuestWaitEvent : public GuestWaitEventBase
1059{
1060
1061public:
1062
1063 GuestWaitEvent(void);
1064 virtual ~GuestWaitEvent(void);
1065
1066public:
1067
1068 int Init(uint32_t uCID);
1069 int Init(uint32_t uCID, const GuestEventTypes &lstEvents);
1070 int Cancel(void);
1071 const ComPtr<IEvent> Event(void) { return mEvent; }
1072 bool HasGuestError(void) const { return mRc == VERR_GSTCTL_GUEST_ERROR; }
1073 int GetGuestError(void) const { return mGuestRc; }
1074 int SignalExternal(IEvent *pEvent);
1075 const GuestEventTypes &Types(void) { return mEventTypes; }
1076 size_t TypeCount(void) { return mEventTypes.size(); }
1077
1078protected:
1079
1080 /** List of public event types this event should
1081 * be signalled on. Optional. */
1082 GuestEventTypes mEventTypes;
1083 /** Pointer to the actual public event, if any. */
1084 ComPtr<IEvent> mEvent;
1085};
1086/** Map of pointers to guest events. The primary key
1087 * contains the context ID. */
1088typedef std::map < uint32_t, GuestWaitEvent* > GuestWaitEvents;
1089/** Map of wait events per public guest event. Nice for
1090 * faster lookups when signalling a whole event group. */
1091typedef std::map < VBoxEventType_T, GuestWaitEvents > GuestEventGroup;
1092
1093class GuestBase
1094{
1095
1096public:
1097
1098 GuestBase(void);
1099 virtual ~GuestBase(void);
1100
1101public:
1102
1103 /** Signals a wait event using a public guest event; also used for
1104 * for external event listeners. */
1105 int signalWaitEvent(VBoxEventType_T aType, IEvent *aEvent);
1106 /** Signals a wait event using a guest rc. */
1107 int signalWaitEventInternal(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, int guestRc, const GuestWaitEventPayload *pPayload);
1108 /** Signals a wait event without letting public guest events know,
1109 * extended director's cut version. */
1110 int signalWaitEventInternalEx(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, int rc, int guestRc, const GuestWaitEventPayload *pPayload);
1111
1112public:
1113
1114 int baseInit(void);
1115 void baseUninit(void);
1116 int cancelWaitEvents(void);
1117 int dispatchGeneric(PVBOXGUESTCTRLHOSTCBCTX pCtxCb, PVBOXGUESTCTRLHOSTCALLBACK pSvcCb);
1118 int generateContextID(uint32_t uSessionID, uint32_t uObjectID, uint32_t *puContextID);
1119 int registerWaitEvent(uint32_t uSessionID, uint32_t uObjectID, GuestWaitEvent **ppEvent);
1120 int registerWaitEventEx(uint32_t uSessionID, uint32_t uObjectID, const GuestEventTypes &lstEvents, GuestWaitEvent **ppEvent);
1121 int unregisterWaitEvent(GuestWaitEvent *pEvent);
1122 int waitForEvent(GuestWaitEvent *pEvent, uint32_t uTimeoutMS, VBoxEventType_T *pType, IEvent **ppEvent);
1123
1124public:
1125
1126 static FsObjType_T fileModeToFsObjType(RTFMODE fMode);
1127
1128protected:
1129
1130 /** Pointer to the console object. Needed
1131 * for HGCM (VMMDev) communication. */
1132 Console *mConsole;
1133 /** The next context ID counter component for this object. */
1134 uint32_t mNextContextID;
1135 /** Local listener for handling the waiting events
1136 * internally. */
1137 ComPtr<IEventListener> mLocalListener;
1138 /** Critical section for wait events access. */
1139 RTCRITSECT mWaitEventCritSect;
1140 /** Map of registered wait events per event group. */
1141 GuestEventGroup mWaitEventGroups;
1142 /** Map of registered wait events. */
1143 GuestWaitEvents mWaitEvents;
1144};
1145
1146/**
1147 * Virtual class (interface) for guest objects (processes, files, ...) --
1148 * contains all per-object callback management.
1149 */
1150class GuestObject : public GuestBase
1151{
1152 friend class GuestSession;
1153
1154public:
1155
1156 GuestObject(void);
1157 virtual ~GuestObject(void);
1158
1159public:
1160
1161 ULONG getObjectID(void) { return mObjectID; }
1162
1163protected:
1164
1165 /**
1166 * Called by IGuestSession when the session status has been changed.
1167 *
1168 * @returns VBox status code.
1169 * @param enmSessionStatus New session status.
1170 */
1171 virtual int i_onSessionStatusChange(GuestSessionStatus_T enmSessionStatus) = 0;
1172
1173 /**
1174 * Called by IGuestSession right before this object gets
1175 * unregistered (removed) from the public object list.
1176 */
1177 virtual int i_onUnregister(void) = 0;
1178
1179 /** Callback dispatcher -- must be implemented by the actual object. */
1180 virtual int i_callbackDispatcher(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCb) = 0;
1181
1182protected:
1183
1184 int bindToSession(Console *pConsole, GuestSession *pSession, uint32_t uObjectID);
1185 int registerWaitEvent(const GuestEventTypes &lstEvents, GuestWaitEvent **ppEvent);
1186 int sendMessage(uint32_t uFunction, uint32_t cParms, PVBOXHGCMSVCPARM paParms);
1187
1188protected:
1189
1190 /** @name Common parameters for all derived objects. They have their own
1191 * mData structure to keep their specific data around.
1192 * @{ */
1193 /** Pointer to parent session. Per definition
1194 * this objects *always* lives shorter than the
1195 * parent.
1196 * @todo r=bird: When wanting to use mSession in the
1197 * IGuestProcess::getEnvironment() implementation I wanted to access
1198 * GuestSession::mData::mpBaseEnvironment. Seeing the comment in
1199 * GuestProcess::terminate() saying:
1200 * "Now only API clients still can hold references to it."
1201 * and recalling seeing similar things in VirtualBox.xidl or some such place,
1202 * I'm wondering how this "per definition" behavior is enforced. Is there any
1203 * GuestProcess:uninit() call or similar magic that invalidates objects that
1204 * GuestSession loses track of in place like GuestProcess::terminate() that I've
1205 * failed to spot?
1206 *
1207 * Please enlighten me.
1208 */
1209 GuestSession *mSession;
1210 /** The object ID -- must be unique for each guest
1211 * object and is encoded into the context ID. Must
1212 * be set manually when initializing the object.
1213 *
1214 * For guest processes this is the internal PID,
1215 * for guest files this is the internal file ID. */
1216 uint32_t mObjectID;
1217 /** @} */
1218};
1219#endif /* !MAIN_INCLUDED_GuestCtrlImplPrivate_h */
1220
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