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