1 | /* $Id: GuestProcessImpl.h 60622 2016-04-21 13:00:20Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox Main - Guest process handling implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2012-2016 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_GUESTPROCESSIMPL
|
---|
19 | #define ____H_GUESTPROCESSIMPL
|
---|
20 |
|
---|
21 | #include "GuestCtrlImplPrivate.h"
|
---|
22 | #include "GuestProcessWrap.h"
|
---|
23 |
|
---|
24 | #include <iprt/cpp/utils.h>
|
---|
25 |
|
---|
26 | class Console;
|
---|
27 | class GuestSession;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Class for handling a guest process.
|
---|
31 | */
|
---|
32 | class ATL_NO_VTABLE GuestProcess :
|
---|
33 | public GuestProcessWrap,
|
---|
34 | public GuestObject
|
---|
35 | {
|
---|
36 | public:
|
---|
37 | /** @name COM and internal init/term/mapping cruft.
|
---|
38 | * @{ */
|
---|
39 | DECLARE_EMPTY_CTOR_DTOR(GuestProcess)
|
---|
40 |
|
---|
41 | int init(Console *aConsole, GuestSession *aSession, ULONG aProcessID,
|
---|
42 | const GuestProcessStartupInfo &aProcInfo, const GuestEnvironment *pBaseEnv);
|
---|
43 | void uninit(void);
|
---|
44 | HRESULT FinalConstruct(void);
|
---|
45 | void FinalRelease(void);
|
---|
46 | /** @} */
|
---|
47 |
|
---|
48 | public:
|
---|
49 | /** @name Public internal methods.
|
---|
50 | * @{ */
|
---|
51 | int i_callbackDispatcher(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCb);
|
---|
52 | inline int i_checkPID(uint32_t uPID);
|
---|
53 | static Utf8Str i_guestErrorToString(int guestRc);
|
---|
54 | int i_onRemove(void);
|
---|
55 | int i_readData(uint32_t uHandle, uint32_t uSize, uint32_t uTimeoutMS, void *pvData,
|
---|
56 | size_t cbData, uint32_t *pcbRead, int *pGuestRc);
|
---|
57 | static HRESULT i_setErrorExternal(VirtualBoxBase *pInterface, int guestRc);
|
---|
58 | int i_startProcess(uint32_t uTimeoutMS, int *pGuestRc);
|
---|
59 | int i_startProcessAsync(void);
|
---|
60 | int i_terminateProcess(uint32_t uTimeoutMS, int *pGuestRc);
|
---|
61 | static ProcessWaitResult_T i_waitFlagsToResultEx(uint32_t fWaitFlags, ProcessStatus_T oldStatus,
|
---|
62 | ProcessStatus_T newStatus, uint32_t uProcFlags,
|
---|
63 | uint32_t uProtocol);
|
---|
64 | ProcessWaitResult_T i_waitFlagsToResult(uint32_t fWaitFlags);
|
---|
65 | int i_waitFor(uint32_t fWaitFlags, ULONG uTimeoutMS, ProcessWaitResult_T &waitResult, int *pGuestRc);
|
---|
66 | int i_waitForInputNotify(GuestWaitEvent *pEvent, uint32_t uHandle, uint32_t uTimeoutMS,
|
---|
67 | ProcessInputStatus_T *pInputStatus, uint32_t *pcbProcessed);
|
---|
68 | int i_waitForOutput(GuestWaitEvent *pEvent, uint32_t uHandle, uint32_t uTimeoutMS,
|
---|
69 | void* pvData, size_t cbData, uint32_t *pcbRead);
|
---|
70 | int i_waitForStatusChange(GuestWaitEvent *pEvent, uint32_t uTimeoutMS,
|
---|
71 | ProcessStatus_T *pProcessStatus, int *pGuestRc);
|
---|
72 | static bool i_waitResultImpliesEx(ProcessWaitResult_T waitResult, ProcessStatus_T procStatus,
|
---|
73 | uint32_t uProcFlags, uint32_t uProtocol);
|
---|
74 | int i_writeData(uint32_t uHandle, uint32_t uFlags, void *pvData, size_t cbData,
|
---|
75 | uint32_t uTimeoutMS, uint32_t *puWritten, int *pGuestRc);
|
---|
76 | /** @} */
|
---|
77 |
|
---|
78 | protected:
|
---|
79 | /** @name Protected internal methods.
|
---|
80 | * @{ */
|
---|
81 | inline bool i_isAlive(void);
|
---|
82 | inline bool i_hasEnded(void);
|
---|
83 | int i_onGuestDisconnected(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData);
|
---|
84 | int i_onProcessInputStatus(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData);
|
---|
85 | int i_onProcessNotifyIO(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData);
|
---|
86 | int i_onProcessStatusChange(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData);
|
---|
87 | int i_onProcessOutput(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData);
|
---|
88 | int i_prepareExecuteEnv(const char *pszEnv, void **ppvList, ULONG *pcbList, ULONG *pcEnvVars);
|
---|
89 | int i_setProcessStatus(ProcessStatus_T procStatus, int procRc);
|
---|
90 | static DECLCALLBACK(int) i_startProcessThread(RTTHREAD Thread, void *pvUser);
|
---|
91 | /** @} */
|
---|
92 |
|
---|
93 | private:
|
---|
94 | /** Wrapped @name IProcess properties.
|
---|
95 | * @{ */
|
---|
96 | HRESULT getArguments(std::vector<com::Utf8Str> &aArguments);
|
---|
97 | HRESULT getEnvironment(std::vector<com::Utf8Str> &aEnvironment);
|
---|
98 | HRESULT getEventSource(ComPtr<IEventSource> &aEventSource);
|
---|
99 | HRESULT getExecutablePath(com::Utf8Str &aExecutablePath);
|
---|
100 | HRESULT getExitCode(LONG *aExitCode);
|
---|
101 | HRESULT getName(com::Utf8Str &aName);
|
---|
102 | HRESULT getPID(ULONG *aPID);
|
---|
103 | HRESULT getStatus(ProcessStatus_T *aStatus);
|
---|
104 | /** @} */
|
---|
105 |
|
---|
106 | /** Wrapped @name IProcess methods.
|
---|
107 | * @{ */
|
---|
108 | HRESULT waitFor(ULONG aWaitFor,
|
---|
109 | ULONG aTimeoutMS,
|
---|
110 | ProcessWaitResult_T *aReason);
|
---|
111 | HRESULT waitForArray(const std::vector<ProcessWaitForFlag_T> &aWaitFor,
|
---|
112 | ULONG aTimeoutMS,
|
---|
113 | ProcessWaitResult_T *aReason);
|
---|
114 | HRESULT read(ULONG aHandle,
|
---|
115 | ULONG aToRead,
|
---|
116 | ULONG aTimeoutMS,
|
---|
117 | std::vector<BYTE> &aData);
|
---|
118 | HRESULT write(ULONG aHandle,
|
---|
119 | ULONG aFlags,
|
---|
120 | const std::vector<BYTE> &aData,
|
---|
121 | ULONG aTimeoutMS,
|
---|
122 | ULONG *aWritten);
|
---|
123 | HRESULT writeArray(ULONG aHandle,
|
---|
124 | const std::vector<ProcessInputFlag_T> &aFlags,
|
---|
125 | const std::vector<BYTE> &aData,
|
---|
126 | ULONG aTimeoutMS,
|
---|
127 | ULONG *aWritten);
|
---|
128 | HRESULT terminate(void);
|
---|
129 | /** @} */
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * This can safely be used without holding any locks.
|
---|
133 | * An AutoCaller suffices to prevent it being destroy while in use and
|
---|
134 | * internally there is a lock providing the necessary serialization.
|
---|
135 | */
|
---|
136 | const ComObjPtr<EventSource> mEventSource;
|
---|
137 |
|
---|
138 | struct Data
|
---|
139 | {
|
---|
140 | /** The process startup information. */
|
---|
141 | GuestProcessStartupInfo mProcess;
|
---|
142 | /** Reference to the immutable session base environment. NULL if the
|
---|
143 | * environment feature isn't supported.
|
---|
144 | * @remarks If there is proof that the uninit order of GuestSession and
|
---|
145 | * this class is what GuestObjectBase claims, then this isn't
|
---|
146 | * strictly necessary. */
|
---|
147 | GuestEnvironment const *mpSessionBaseEnv;
|
---|
148 | /** Exit code if process has been terminated. */
|
---|
149 | LONG mExitCode;
|
---|
150 | /** PID reported from the guest. */
|
---|
151 | ULONG mPID;
|
---|
152 | /** The current process status. */
|
---|
153 | ProcessStatus_T mStatus;
|
---|
154 | /** The last returned process status
|
---|
155 | * returned from the guest side. */
|
---|
156 | int mLastError;
|
---|
157 |
|
---|
158 | Data(void) : mpSessionBaseEnv(NULL)
|
---|
159 | { }
|
---|
160 | ~Data(void)
|
---|
161 | {
|
---|
162 | if (mpSessionBaseEnv)
|
---|
163 | {
|
---|
164 | mpSessionBaseEnv->releaseConst();
|
---|
165 | mpSessionBaseEnv = NULL;
|
---|
166 | }
|
---|
167 | }
|
---|
168 | } mData;
|
---|
169 |
|
---|
170 | friend class GuestProcessStartTask;
|
---|
171 | };
|
---|
172 |
|
---|
173 | /**
|
---|
174 | * Guest process tool flags.
|
---|
175 | */
|
---|
176 | /** No flags specified; wait until process terminates.
|
---|
177 | * The maximum waiting time is set in the process' startup
|
---|
178 | * info. */
|
---|
179 | #define GUESTPROCESSTOOL_FLAG_NONE 0
|
---|
180 | /** Wait until next stream block from stdout has been
|
---|
181 | * read in completely, then return.
|
---|
182 | */
|
---|
183 | #define GUESTPROCESSTOOL_FLAG_STDOUT_BLOCK RT_BIT(0)
|
---|
184 |
|
---|
185 | /**
|
---|
186 | * Structure for keeping a VBoxService toolbox tool's error info around.
|
---|
187 | */
|
---|
188 | struct GuestProcessToolErrorInfo
|
---|
189 | {
|
---|
190 | /** Return code from the guest side for executing the process tool. */
|
---|
191 | int guestRc;
|
---|
192 | /** The process tool's returned exit code. */
|
---|
193 | LONG lExitCode;
|
---|
194 | };
|
---|
195 |
|
---|
196 | /**
|
---|
197 | * Internal class for handling the BusyBox-like tools built into VBoxService
|
---|
198 | * on the guest side. It's also called the VBoxService Toolbox (tm).
|
---|
199 | *
|
---|
200 | * Those initially were necessary to guarantee execution of commands (like "ls", "cat")
|
---|
201 | * under the behalf of a certain guest user.
|
---|
202 | *
|
---|
203 | * This class essentially helps to wrap all the gory details like process creation,
|
---|
204 | * information extraction and maintaining the overall status.
|
---|
205 | */
|
---|
206 | class GuestProcessTool
|
---|
207 | {
|
---|
208 | public:
|
---|
209 |
|
---|
210 | GuestProcessTool(void);
|
---|
211 |
|
---|
212 | virtual ~GuestProcessTool(void);
|
---|
213 |
|
---|
214 | public:
|
---|
215 |
|
---|
216 | int Init(GuestSession *pGuestSession, const GuestProcessStartupInfo &startupInfo, bool fAsync, int *pGuestRc);
|
---|
217 |
|
---|
218 | int i_getCurrentBlock(uint32_t uHandle, GuestProcessStreamBlock &strmBlock);
|
---|
219 |
|
---|
220 | int i_getRc(void) const;
|
---|
221 |
|
---|
222 | GuestProcessStream &i_getStdOut(void) { return mStdOut; }
|
---|
223 |
|
---|
224 | GuestProcessStream &i_getStdErr(void) { return mStdErr; }
|
---|
225 |
|
---|
226 | int i_wait(uint32_t fFlags, int *pGuestRc);
|
---|
227 |
|
---|
228 | int i_waitEx(uint32_t fFlags, GuestProcessStreamBlock *pStreamBlock, int *pGuestRc);
|
---|
229 |
|
---|
230 | bool i_isRunning(void);
|
---|
231 |
|
---|
232 | int i_terminatedOk(LONG *plExitCode = NULL);
|
---|
233 |
|
---|
234 | int i_terminate(uint32_t uTimeoutMS, int *pGuestRc);
|
---|
235 |
|
---|
236 | public:
|
---|
237 |
|
---|
238 | static int i_run(GuestSession *pGuestSession, const GuestProcessStartupInfo &startupInfo, int *pGuestRc);
|
---|
239 |
|
---|
240 | static int i_runErrorInfo(GuestSession *pGuestSession, const GuestProcessStartupInfo &startupInfo, GuestProcessToolErrorInfo &errorInfo);
|
---|
241 |
|
---|
242 | static int i_runEx(GuestSession *pGuestSession, const GuestProcessStartupInfo &startupInfo,
|
---|
243 | GuestCtrlStreamObjects *pStrmOutObjects, uint32_t cStrmOutObjects, int *pGuestRc);
|
---|
244 |
|
---|
245 | static int i_runExErrorInfo(GuestSession *pGuestSession, const GuestProcessStartupInfo &startupInfo,
|
---|
246 | GuestCtrlStreamObjects *pStrmOutObjects, uint32_t cStrmOutObjects, GuestProcessToolErrorInfo &errorInfo);
|
---|
247 |
|
---|
248 | static int i_exitCodeToRc(const GuestProcessStartupInfo &startupInfo, LONG lExitCode);
|
---|
249 |
|
---|
250 | static int i_exitCodeToRc(const char *pszTool, LONG lExitCode);
|
---|
251 |
|
---|
252 | protected:
|
---|
253 |
|
---|
254 | ComObjPtr<GuestSession> pSession;
|
---|
255 | ComObjPtr<GuestProcess> pProcess;
|
---|
256 | GuestProcessStartupInfo mStartupInfo;
|
---|
257 | GuestProcessStream mStdOut;
|
---|
258 | GuestProcessStream mStdErr;
|
---|
259 |
|
---|
260 | };
|
---|
261 |
|
---|
262 | #endif /* !____H_GUESTPROCESSIMPL */
|
---|
263 |
|
---|