VirtualBox

source: vbox/trunk/src/VBox/Main/include/GuestProcessImpl.h@ 50762

Last change on this file since 50762 was 50709, checked in by vboxsync, 11 years ago

6813 src-client/GuestProcessImpl.cpp

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.7 KB
Line 
1
2/* $Id: GuestProcessImpl.h 50709 2014-03-05 18:29:52Z vboxsync $ */
3/** @file
4 * VirtualBox Main - Guest process handling.
5 */
6
7/*
8 * Copyright (C) 2012-2013 Oracle Corporation
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 */
18
19#ifndef ____H_GUESTPROCESSIMPL
20#define ____H_GUESTPROCESSIMPL
21
22#include "GuestCtrlImplPrivate.h"
23#include "GuestProcessWrap.h"
24
25class Console;
26class GuestSession;
27
28/**
29 * Class for handling a guest process.
30 */
31class ATL_NO_VTABLE GuestProcess :
32 public GuestProcessWrap,
33 public GuestObject
34{
35public:
36 /** @name COM and internal init/term/mapping cruft.
37 * @{ */
38 DECLARE_EMPTY_CTOR_DTOR(GuestProcess)
39
40 int init(Console *aConsole, GuestSession *aSession, ULONG aProcessID, const GuestProcessStartupInfo &aProcInfo);
41 void uninit(void);
42 HRESULT FinalConstruct(void);
43 void FinalRelease(void);
44 /** @} */
45
46
47public:
48 /** @name Public internal methods.
49 * @{ */
50 int i_callbackDispatcher(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCb);
51 inline int i_checkPID(uint32_t uPID);
52 static Utf8Str i_guestErrorToString(int guestRc);
53 int i_onRemove(void);
54 int i_readData(uint32_t uHandle, uint32_t uSize, uint32_t uTimeoutMS, void *pvData, size_t cbData, uint32_t *pcbRead, int *pGuestRc);
55 static HRESULT i_setErrorExternal(VirtualBoxBase *pInterface, int guestRc);
56 int i_startProcess(uint32_t uTimeoutMS, int *pGuestRc);
57 int i_startProcessAsync(void);
58 int i_terminateProcess(uint32_t uTimeoutMS, int *pGuestRc);
59 static ProcessWaitResult_T i_waitFlagsToResultEx(uint32_t fWaitFlags, ProcessStatus_T oldStatus, ProcessStatus_T newStatus, uint32_t uProcFlags, uint32_t uProtocol);
60 ProcessWaitResult_T i_waitFlagsToResult(uint32_t fWaitFlags);
61 int i_waitFor(uint32_t fWaitFlags, ULONG uTimeoutMS, ProcessWaitResult_T &waitResult, int *pGuestRc);
62 int i_waitForInputNotify(GuestWaitEvent *pEvent, uint32_t uHandle, uint32_t uTimeoutMS, ProcessInputStatus_T *pInputStatus, uint32_t *pcbProcessed);
63 int i_waitForOutput(GuestWaitEvent *pEvent, uint32_t uHandle, uint32_t uTimeoutMS, void* pvData, size_t cbData, uint32_t *pcbRead);
64 int i_waitForStatusChange(GuestWaitEvent *pEvent, uint32_t uTimeoutMS, ProcessStatus_T *pProcessStatus, int *pGuestRc);
65 static bool i_waitResultImpliesEx(ProcessWaitResult_T waitResult, ProcessStatus_T procStatus, uint32_t uProcFlags, uint32_t uProtocol);
66 int i_writeData(uint32_t uHandle, uint32_t uFlags, void *pvData, size_t cbData, uint32_t uTimeoutMS, uint32_t *puWritten, int *pGuestRc);
67 /** @} */
68
69protected:
70 /** @name Protected internal methods.
71 * @{ */
72 inline bool i_isAlive(void);
73 inline bool i_hasEnded(void);
74 int i_onGuestDisconnected(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData);
75 int i_onProcessInputStatus(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData);
76 int i_onProcessNotifyIO(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData);
77 int i_onProcessStatusChange(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData);
78 int i_onProcessOutput(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData);
79 int i_prepareExecuteEnv(const char *pszEnv, void **ppvList, ULONG *pcbList, ULONG *pcEnvVars);
80 int i_setProcessStatus(ProcessStatus_T procStatus, int procRc);
81 static DECLCALLBACK(int) i_startProcessThread(RTTHREAD Thread, void *pvUser);
82 /** @} */
83
84private:
85 /** Wrapped @name IProcess data .
86 * @{ */
87 HRESULT getArguments(std::vector<com::Utf8Str> &aArguments);
88 HRESULT getEnvironment(std::vector<com::Utf8Str> &aEnvironment);
89 HRESULT getEventSource(ComPtr<IEventSource> &aEventSource);
90 HRESULT getExecutablePath(com::Utf8Str &aExecutablePath);
91 HRESULT getExitCode(LONG *aExitCode);
92 HRESULT getName(com::Utf8Str &aName);
93 HRESULT getPID(ULONG *aPID);
94 HRESULT getStatus(ProcessStatus_T *aStatus);
95
96 /** Wrapped @name IProcess methods.
97 * @{ */
98 HRESULT waitFor(ULONG aWaitFor,
99 ULONG aTimeoutMS,
100 ProcessWaitResult_T *aReason);
101 HRESULT waitForArray(const std::vector<ProcessWaitForFlag_T> &aWaitFor,
102 ULONG aTimeoutMS,
103 ProcessWaitResult_T *aReason);
104 HRESULT read(ULONG aHandle,
105 ULONG aToRead,
106 ULONG aTimeoutMS,
107 std::vector<BYTE> &aData);
108 HRESULT write(ULONG aHandle,
109 ULONG aFlags,
110 const std::vector<BYTE> &aData,
111 ULONG aTimeoutMS,
112 ULONG *aWritten);
113 HRESULT writeArray(ULONG aHandle,
114 const std::vector<ProcessInputFlag_T> &aFlags,
115 const std::vector<BYTE> &aData,
116 ULONG aTimeoutMS,
117 ULONG *aWritten);
118 HRESULT terminate();
119
120 /**
121 * This can safely be used without holding any locks.
122 * An AutoCaller suffices to prevent it being destroy while in use and
123 * internally there is a lock providing the necessary serialization.
124 */
125 const ComObjPtr<EventSource> mEventSource;
126
127 struct Data
128 {
129 /** The process startup information. */
130 GuestProcessStartupInfo mProcess;
131 /** Exit code if process has been terminated. */
132 LONG mExitCode;
133 /** PID reported from the guest. */
134 ULONG mPID;
135 /** The current process status. */
136 ProcessStatus_T mStatus;
137 /** The last returned process status
138 * returned from the guest side. */
139 int mLastError;
140 } mData;
141};
142
143/**
144 * Guest process tool flags.
145 */
146/** No flags specified; wait until process terminates.
147 * The maximum waiting time is set in the process' startup
148 * info. */
149#define GUESTPROCESSTOOL_FLAG_NONE 0
150/** Wait until next stream block from stdout has been
151 * read in completely, then return.
152 */
153#define GUESTPROCESSTOOL_FLAG_STDOUT_BLOCK RT_BIT(0)
154
155/**
156 * Internal class for handling a VBoxService tool ("vbox_ls", vbox_stat", ...).
157 */
158class GuestProcessTool
159{
160public:
161
162 GuestProcessTool(void);
163
164 virtual ~GuestProcessTool(void);
165
166public:
167
168 int Init(GuestSession *pGuestSession, const GuestProcessStartupInfo &startupInfo, bool fAsync, int *pGuestRc);
169
170 GuestProcessStream &i_getStdOut(void) { return mStdOut; }
171
172 GuestProcessStream &i_getStdErr(void) { return mStdErr; }
173
174 int i_wait(uint32_t fFlags, int *pGuestRc);
175
176 int i_waitEx(uint32_t fFlags, GuestProcessStreamBlock *pStreamBlock, int *pGuestRc);
177
178 int i_getCurrentBlock(uint32_t uHandle, GuestProcessStreamBlock &strmBlock);
179
180 bool i_isRunning(void);
181
182 static int i_run(GuestSession *pGuestSession, const GuestProcessStartupInfo &startupInfo, int *pGuestRc);
183
184 static int i_runEx(GuestSession *pGuestSession, const GuestProcessStartupInfo &startupInfo, GuestCtrlStreamObjects *pStrmOutObjects,
185 uint32_t cStrmOutObjects, int *pGuestRc);
186
187 int i_terminatedOk(LONG *pExitCode);
188
189 int i_terminate(uint32_t uTimeoutMS, int *pGuestRc);
190
191protected:
192
193 ComObjPtr<GuestSession> pSession;
194 ComObjPtr<GuestProcess> pProcess;
195 GuestProcessStartupInfo mStartupInfo;
196 GuestProcessStream mStdOut;
197 GuestProcessStream mStdErr;
198};
199
200#endif /* !____H_GUESTPROCESSIMPL */
201
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