VirtualBox

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

Last change on this file since 84554 was 82968, checked in by vboxsync, 5 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.7 KB
Line 
1/* $Id: GuestProcessImpl.h 82968 2020-02-04 10:35:17Z vboxsync $ */
2/** @file
3 * VirtualBox Main - Guest process handling implementation.
4 */
5
6/*
7 * Copyright (C) 2012-2020 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
29class Console;
30class GuestSession;
31class GuestProcessStartTask;
32
33/**
34 * Class for handling a guest process.
35 */
36class ATL_NO_VTABLE GuestProcess :
37 public GuestProcessWrap,
38 public GuestObject
39{
40public:
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
52public:
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
60public:
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#if 0 /* unused */
84 static bool i_waitResultImpliesEx(ProcessWaitResult_T waitResult, ProcessStatus_T procStatus, uint32_t uProtocol);
85#endif
86 /** @} */
87
88protected:
89 /** @name Protected internal methods.
90 * @{ */
91 inline bool i_isAlive(void);
92 inline bool i_hasEnded(void);
93 int i_onGuestDisconnected(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData);
94 int i_onProcessInputStatus(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData);
95 int i_onProcessNotifyIO(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData);
96 int i_onProcessStatusChange(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData);
97 int i_onProcessOutput(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData);
98 int i_prepareExecuteEnv(const char *pszEnv, void **ppvList, ULONG *pcbList, ULONG *pcEnvVars);
99 int i_setProcessStatus(ProcessStatus_T procStatus, int procRc);
100 static int i_startProcessThreadTask(GuestProcessStartTask *pTask);
101 /** @} */
102
103private:
104 /** Wrapped @name IProcess properties.
105 * @{ */
106 HRESULT getArguments(std::vector<com::Utf8Str> &aArguments);
107 HRESULT getEnvironment(std::vector<com::Utf8Str> &aEnvironment);
108 HRESULT getEventSource(ComPtr<IEventSource> &aEventSource);
109 HRESULT getExecutablePath(com::Utf8Str &aExecutablePath);
110 HRESULT getExitCode(LONG *aExitCode);
111 HRESULT getName(com::Utf8Str &aName);
112 HRESULT getPID(ULONG *aPID);
113 HRESULT getStatus(ProcessStatus_T *aStatus);
114 /** @} */
115
116 /** Wrapped @name IProcess methods.
117 * @{ */
118 HRESULT waitFor(ULONG aWaitFor,
119 ULONG aTimeoutMS,
120 ProcessWaitResult_T *aReason);
121 HRESULT waitForArray(const std::vector<ProcessWaitForFlag_T> &aWaitFor,
122 ULONG aTimeoutMS,
123 ProcessWaitResult_T *aReason);
124 HRESULT read(ULONG aHandle,
125 ULONG aToRead,
126 ULONG aTimeoutMS,
127 std::vector<BYTE> &aData);
128 HRESULT write(ULONG aHandle,
129 ULONG aFlags,
130 const std::vector<BYTE> &aData,
131 ULONG aTimeoutMS,
132 ULONG *aWritten);
133 HRESULT writeArray(ULONG aHandle,
134 const std::vector<ProcessInputFlag_T> &aFlags,
135 const std::vector<BYTE> &aData,
136 ULONG aTimeoutMS,
137 ULONG *aWritten);
138 HRESULT terminate(void);
139 /** @} */
140
141 /**
142 * This can safely be used without holding any locks.
143 * An AutoCaller suffices to prevent it being destroy while in use and
144 * internally there is a lock providing the necessary serialization.
145 */
146 const ComObjPtr<EventSource> mEventSource;
147
148 struct Data
149 {
150 /** The process startup information. */
151 GuestProcessStartupInfo mProcess;
152 /** Reference to the immutable session base environment. NULL if the
153 * environment feature isn't supported.
154 * @remarks If there is proof that the uninit order of GuestSession and
155 * this class is what GuestObjectBase claims, then this isn't
156 * strictly necessary. */
157 GuestEnvironment const *mpSessionBaseEnv;
158 /** Exit code if process has been terminated. */
159 LONG mExitCode;
160 /** PID reported from the guest.
161 * Note: This is *not* the internal object ID! */
162 ULONG mPID;
163 /** The current process status. */
164 ProcessStatus_T mStatus;
165 /** The last returned process status
166 * returned from the guest side. */
167 int mLastError;
168
169 Data(void) : mpSessionBaseEnv(NULL)
170 { }
171 ~Data(void)
172 {
173 if (mpSessionBaseEnv)
174 {
175 mpSessionBaseEnv->releaseConst();
176 mpSessionBaseEnv = NULL;
177 }
178 }
179 } mData;
180
181 friend class GuestProcessStartTask;
182};
183
184/**
185 * Guest process tool wait flags.
186 */
187/** No wait flags specified; wait until process terminates.
188 * The maximum waiting time is set in the process' startup
189 * info. */
190#define GUESTPROCESSTOOL_WAIT_FLAG_NONE 0
191/** Wait until next stream block from stdout has been
192 * read in completely, then return.
193 */
194#define GUESTPROCESSTOOL_WAIT_FLAG_STDOUT_BLOCK RT_BIT(0)
195
196/**
197 * Structure for keeping a VBoxService toolbox tool's error info around.
198 */
199struct GuestProcessToolErrorInfo
200{
201 /** Return code from the guest side for executing the process tool. */
202 int rcGuest;
203 /** The process tool's returned exit code. */
204 int32_t iExitCode;
205};
206
207/**
208 * Internal class for handling the BusyBox-like tools built into VBoxService
209 * on the guest side. It's also called the VBoxService Toolbox (tm).
210 *
211 * Those initially were necessary to guarantee execution of commands (like "ls", "cat")
212 * under the behalf of a certain guest user.
213 *
214 * This class essentially helps to wrap all the gory details like process creation,
215 * information extraction and maintaining the overall status.
216 */
217class GuestProcessTool
218{
219public:
220
221 GuestProcessTool(void);
222
223 virtual ~GuestProcessTool(void);
224
225public:
226
227 int init(GuestSession *pGuestSession, const GuestProcessStartupInfo &startupInfo, bool fAsync, int *pGuestRc);
228
229 void uninit(void);
230
231 int getCurrentBlock(uint32_t uHandle, GuestProcessStreamBlock &strmBlock);
232
233 int getRc(void) const;
234
235 GuestProcessStream &getStdOut(void) { return mStdOut; }
236
237 GuestProcessStream &getStdErr(void) { return mStdErr; }
238
239 int wait(uint32_t fToolWaitFlags, int *pGuestRc);
240
241 int waitEx(uint32_t fToolWaitFlags, GuestProcessStreamBlock *pStreamBlock, int *pGuestRc);
242
243 bool isRunning(void);
244
245 bool isTerminatedOk(void);
246
247 int getTerminationStatus(int32_t *piExitCode = NULL);
248
249 int terminate(uint32_t uTimeoutMS, int *pGuestRc);
250
251public:
252
253 /** Wrapped @name Static run methods.
254 * @{ */
255 static int run(GuestSession *pGuestSession, const GuestProcessStartupInfo &startupInfo, int *pGuestRc);
256
257 static int runErrorInfo(GuestSession *pGuestSession, const GuestProcessStartupInfo &startupInfo, GuestProcessToolErrorInfo &errorInfo);
258
259 static int runEx(GuestSession *pGuestSession, const GuestProcessStartupInfo &startupInfo,
260 GuestCtrlStreamObjects *pStrmOutObjects, uint32_t cStrmOutObjects, int *pGuestRc);
261
262 static int runExErrorInfo(GuestSession *pGuestSession, const GuestProcessStartupInfo &startupInfo,
263 GuestCtrlStreamObjects *pStrmOutObjects, uint32_t cStrmOutObjects, GuestProcessToolErrorInfo &errorInfo);
264 /** @} */
265
266 /** Wrapped @name Static exit code conversion methods.
267 * @{ */
268 static int exitCodeToRc(const GuestProcessStartupInfo &startupInfo, int32_t iExitCode);
269
270 static int exitCodeToRc(const char *pszTool, int32_t iExitCode);
271 /** @} */
272
273protected:
274
275 /** Pointer to session this toolbox object is bound to. */
276 ComObjPtr<GuestSession> pSession;
277 /** Pointer to process object this toolbox object is bound to. */
278 ComObjPtr<GuestProcess> pProcess;
279 /** The toolbox' startup info. */
280 GuestProcessStartupInfo mStartupInfo;
281 /** Stream object for handling the toolbox' stdout data. */
282 GuestProcessStream mStdOut;
283 /** Stream object for handling the toolbox' stderr data. */
284 GuestProcessStream mStdErr;
285};
286
287#endif /* !MAIN_INCLUDED_GuestProcessImpl_h */
288
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