VirtualBox

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

Last change on this file was 104178, checked in by vboxsync, 5 weeks ago

Guest Control:

  • Factored out most of the guest process stream handling of GuestToolboxStream (deprecated) into a new generic class GuestProcessOutputStream. That way we can make use of most of that code for other, non-toolbox related functionality.
  • Factoredd out most of the guest process wrapping functionality from GuestProcessToolbox into a new generic class GuestProcessWrapper. Ditto (see above).
  • Make more use of VBOX_WITH_GSTCTL_TOOLBOX_SUPPORT to compile a lot less code if not defined. Toolbox handling is required for supporting older Guest Additions (< 7.1) though (so enabled by default).
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.3 KB
Line 
1/* $Id: GuestProcessImpl.h 104178 2024-04-05 12:23:48Z vboxsync $ */
2/** @file
3 * VirtualBox Main - Guest process handling implementation.
4 */
5
6/*
7 * Copyright (C) 2012-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28#ifndef MAIN_INCLUDED_GuestProcessImpl_h
29#define MAIN_INCLUDED_GuestProcessImpl_h
30#ifndef RT_WITHOUT_PRAGMA_ONCE
31# pragma once
32#endif
33
34#include "GuestCtrlImplPrivate.h"
35#include "GuestProcessWrap.h"
36
37#include <iprt/cpp/utils.h>
38
39class Console;
40class GuestSession;
41class GuestProcessStartTask;
42
43/**
44 * Class for handling a guest process.
45 */
46class ATL_NO_VTABLE GuestProcess :
47 public GuestProcessWrap,
48 public GuestObject
49{
50public:
51 /** @name COM and internal init/term/mapping cruft.
52 * @{ */
53 DECLARE_COMMON_CLASS_METHODS(GuestProcess)
54
55 int init(Console *aConsole, GuestSession *aSession, ULONG aObjectID,
56 const GuestProcessStartupInfo &aProcInfo, const GuestEnvironment *pBaseEnv);
57 void uninit(void);
58 HRESULT FinalConstruct(void);
59 void FinalRelease(void);
60 /** @} */
61
62public:
63 /** @name Implemented virtual methods from GuestObject.
64 * @{ */
65 int i_callbackDispatcher(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCb);
66 int i_onUnregister(void);
67 int i_onSessionStatusChange(GuestSessionStatus_T enmSessionStatus);
68 /** @} */
69
70public:
71 /** @name Public internal methods.
72 * @{ */
73 inline int i_checkPID(uint32_t uPID);
74 ProcessStatus_T i_getStatus(void);
75 int i_readData(uint32_t uHandle, uint32_t uSize, uint32_t uTimeoutMS, void *pvData, size_t cbData, uint32_t *pcbRead, int *pvrcGuest);
76 int i_startProcess(uint32_t cMsTimeout, int *pvrcGuest);
77 int i_startProcessInner(uint32_t cMsTimeout, AutoWriteLock &rLock, GuestWaitEvent *pEvent, int *pvrcGuest);
78 int i_startProcessAsync(void);
79 int i_terminateProcess(uint32_t uTimeoutMS, int *pvrcGuest);
80 ProcessWaitResult_T i_waitFlagsToResult(uint32_t fWaitFlags);
81 int i_waitFor(uint32_t fWaitFlags, ULONG uTimeoutMS, ProcessWaitResult_T &waitResult, int *pvrcGuest);
82 int i_waitForInputNotify(GuestWaitEvent *pEvent, uint32_t uHandle, uint32_t uTimeoutMS, ProcessInputStatus_T *pInputStatus, uint32_t *pcbProcessed);
83 int i_waitForOutput(GuestWaitEvent *pEvent, uint32_t uHandle, uint32_t uTimeoutMS, void* pvData, size_t cbData, uint32_t *pcbRead);
84 int i_waitForStatusChange(GuestWaitEvent *pEvent, uint32_t uTimeoutMS, ProcessStatus_T *pProcessStatus, int *pvrcGuest);
85 int i_writeData(uint32_t uHandle, uint32_t uFlags, void *pvData, size_t cbData, uint32_t uTimeoutMS, uint32_t *puWritten, int *pvrcGuest);
86 /** @} */
87
88 /** @name Static internal methods.
89 * @{ */
90 static Utf8Str i_guestErrorToString(int vrcGuest, const char *pcszWhat);
91 static Utf8Str i_statusToString(ProcessStatus_T enmStatus);
92 static bool i_isGuestError(int guestRc);
93 static ProcessWaitResult_T i_waitFlagsToResultEx(uint32_t fWaitFlags, ProcessStatus_T oldStatus, ProcessStatus_T newStatus, uint32_t uProcFlags, uint32_t uProtocol);
94#if 0 /* unused */
95 static bool i_waitResultImpliesEx(ProcessWaitResult_T waitResult, ProcessStatus_T procStatus, uint32_t uProtocol);
96#endif
97 /** @} */
98
99protected:
100 /** @name Protected internal methods.
101 * @{ */
102 inline bool i_isAlive(void);
103 inline bool i_hasEnded(void);
104 int i_onGuestDisconnected(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData);
105 int i_onProcessInputStatus(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData);
106 int i_onProcessNotifyIO(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData);
107 int i_onProcessStatusChange(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData);
108 int i_onProcessOutput(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData);
109 int i_prepareExecuteEnv(const char *pszEnv, void **ppvList, ULONG *pcbList, ULONG *pcEnvVars);
110 int i_setProcessStatus(ProcessStatus_T procStatus, int vrcProc);
111 static int i_startProcessThreadTask(GuestProcessStartTask *pTask);
112 /** @} */
113
114private:
115 /** Wrapped @name IProcess properties.
116 * @{ */
117 HRESULT getArguments(std::vector<com::Utf8Str> &aArguments);
118 HRESULT getEnvironment(std::vector<com::Utf8Str> &aEnvironment);
119 HRESULT getEventSource(ComPtr<IEventSource> &aEventSource);
120 HRESULT getExecutablePath(com::Utf8Str &aExecutablePath);
121 HRESULT getExitCode(LONG *aExitCode);
122 HRESULT getName(com::Utf8Str &aName);
123 HRESULT getPID(ULONG *aPID);
124 HRESULT getStatus(ProcessStatus_T *aStatus);
125 /** @} */
126
127 /** Wrapped @name IProcess methods.
128 * @{ */
129 HRESULT waitFor(ULONG aWaitFor,
130 ULONG aTimeoutMS,
131 ProcessWaitResult_T *aReason);
132 HRESULT waitForArray(const std::vector<ProcessWaitForFlag_T> &aWaitFor,
133 ULONG aTimeoutMS,
134 ProcessWaitResult_T *aReason);
135 HRESULT read(ULONG aHandle,
136 ULONG aToRead,
137 ULONG aTimeoutMS,
138 std::vector<BYTE> &aData);
139 HRESULT write(ULONG aHandle,
140 ULONG aFlags,
141 const std::vector<BYTE> &aData,
142 ULONG aTimeoutMS,
143 ULONG *aWritten);
144 HRESULT writeArray(ULONG aHandle,
145 const std::vector<ProcessInputFlag_T> &aFlags,
146 const std::vector<BYTE> &aData,
147 ULONG aTimeoutMS,
148 ULONG *aWritten);
149 HRESULT terminate(void);
150 /** @} */
151
152 /**
153 * This can safely be used without holding any locks.
154 * An AutoCaller suffices to prevent it being destroy while in use and
155 * internally there is a lock providing the necessary serialization.
156 */
157 const ComObjPtr<EventSource> mEventSource;
158
159 struct Data
160 {
161 /** The process startup information. */
162 GuestProcessStartupInfo mProcess;
163 /** Reference to the immutable session base environment. NULL if the
164 * environment feature isn't supported.
165 * @remarks If there is proof that the uninit order of GuestSession and
166 * this class is what GuestObjectBase claims, then this isn't
167 * strictly necessary. */
168 GuestEnvironment const *mpSessionBaseEnv;
169 /** Exit code if process has been terminated. */
170 LONG mExitCode;
171 /** PID reported from the guest.
172 * Note: This is *not* the internal object ID! */
173 ULONG mPID;
174 /** The current process status. */
175 ProcessStatus_T mStatus;
176 /** The last returned process status
177 * returned from the guest side. */
178 int mLastError;
179
180 Data(void) : mpSessionBaseEnv(NULL)
181 { }
182 ~Data(void)
183 {
184 if (mpSessionBaseEnv)
185 {
186 mpSessionBaseEnv->releaseConst();
187 mpSessionBaseEnv = NULL;
188 }
189 }
190 } mData;
191
192 friend class GuestProcessStartTask;
193};
194
195/**
196 * Guest process tool wait flags.
197 */
198/** No wait flags specified; wait until process terminates.
199 * The maximum waiting time is set in the process' startup
200 * info. */
201#define GUESTPROCESSTOOL_WAIT_FLAG_NONE 0
202/** Wait until next stream block from stdout has been
203 * read in completely, then return.
204 */
205#define GUESTPROCESSTOOL_WAIT_FLAG_STDOUT_BLOCK RT_BIT(0)
206
207/**
208 * Structure for keeping a VBoxService toolbox tool's error info around.
209 */
210struct GuestProcessToolErrorInfo
211{
212 /** Return (VBox status) code from the guest side for executing the process tool. */
213 int vrcGuest;
214 /** The process tool's returned exit code. */
215 int32_t iExitCode;
216};
217
218/**
219 * Wrapper class for running guest processes.
220 */
221class GuestProcessWrapper
222{
223public:
224 DECLARE_TRANSLATE_METHODS(GuestProcessWrapper)
225
226 GuestProcessWrapper(void);
227
228 virtual ~GuestProcessWrapper(void);
229
230public:
231
232 int init(GuestSession *pGuestSession, const GuestProcessStartupInfo &startupInfo, bool fAsync, int *pvrcGuest);
233
234 void uninit(void);
235
236 /** Returns the stdout output from the guest process. */
237 GuestProcessOutputStream &getStdOut(void) { return mStdOut; }
238
239 /** Returns the stderr output from the guest proces. */
240 GuestProcessOutputStream &getStdErr(void) { return mStdErr; }
241
242 bool isRunning(void);
243
244 bool isTerminatedOk(void);
245
246 int getTerminationStatus(int32_t *piExitCode = NULL);
247
248 int terminate(uint32_t uTimeoutMS, int *pvrcGuest);
249
250protected:
251
252 /** Pointer to session this toolbox object is bound to. */
253 ComObjPtr<GuestSession> pSession;
254 /** Pointer to process object this object is bound to. */
255 ComObjPtr<GuestProcess> pProcess;
256 /** The process startup info. */
257 GuestProcessStartupInfo mStartupInfo;
258 /** Stream object for handling stdout data. */
259 GuestProcessOutputStream mStdOut;
260 /** Stream object for handling stderr data. */
261 GuestProcessOutputStream mStdErr;
262};
263
264#ifdef VBOX_WITH_GSTCTL_TOOLBOX_SUPPORT
265/**
266 * Internal class for handling the BusyBox-like tools built into VBoxService
267 * on the guest side. It's also called the VBoxService Toolbox (tm).
268 *
269 * Those initially were necessary to guarantee execution of commands (like "ls", "cat")
270 * under the behalf of a certain guest user.
271 *
272 * This class essentially helps to wrap all the gory details like process creation,
273 * information extraction and maintaining the overall status.
274 *
275 * Note! When implementing new functionality / commands, do *not* use this approach anymore!
276 * This class has to be kept to guarantee backwards-compatibility.
277 *
278 * Deprecated, do not use anymore.
279 */
280class GuestProcessToolbox : public GuestProcessWrapper
281{
282public:
283 DECLARE_TRANSLATE_METHODS(GuestProcessToolbox)
284
285 GuestProcessToolbox(void);
286
287 virtual ~GuestProcessToolbox(void);
288
289public:
290
291 int wait(uint32_t fToolWaitFlags, int *pvrcGuest);
292
293 int waitEx(uint32_t fToolWaitFlags, GuestToolboxStreamBlock *strmBlockOut, int *pvrcGuest);
294
295 int getCurrentBlock(uint32_t uHandle, GuestToolboxStreamBlock &strmBlock);
296
297 int getRc(void) const;
298
299 /** Returns the stdout output from the guest process tool. */
300 GuestToolboxStream &getStdOut(void) { return mStdOut; }
301
302 /** Returns the stderr output from the guest process tool. */
303 GuestToolboxStream &getStdErr(void) { return mStdErr; }
304
305public:
306
307 /** Wrapped @name Static run methods.
308 * @note Do not call anything 'run' here, it's way too common.
309 * @{ */
310 static int runTool(GuestSession *pGuestSession, const GuestProcessStartupInfo &startupInfo, int *pvrcGuest,
311 GuestCtrlStreamObjects *pLstStdOutObjPairs = NULL, uint32_t cStdOutObjectsToRead = 1);
312
313 static int runToolWithErrorInfo(GuestSession *pGuestSession, const GuestProcessStartupInfo &startupInfo,
314 GuestProcessToolErrorInfo &errorInfo,
315 GuestCtrlStreamObjects *pLstStdOutObjPairs = NULL, uint32_t cStdOutObjectsToRead = 1);
316 /** @} */
317
318 /** Wrapped @name Static exit code conversion methods.
319 * @{ */
320 static int exitCodeToRc(const GuestProcessStartupInfo &startupInfo, int32_t iExitCode);
321 static int exitCodeToRc(const char *pszTool, int32_t iExitCode);
322 /** @} */
323
324 /** Wrapped @name Static guest error conversion methods.
325 * @{ */
326 static Utf8Str guestErrorToString(const char *pszTool, const GuestErrorInfo& guestErrorInfo);
327 /** @} */
328
329protected:
330
331 /** Stream object for handling the toolbox' stdout data. */
332 GuestToolboxStream mStdOut;
333 /** Stream object for handling the toolbox' stderr data. */
334 GuestToolboxStream mStdErr;
335};
336#endif /* VBOX_WITH_GSTCTL_TOOLBOX_SUPPORT */
337
338#endif /* !MAIN_INCLUDED_GuestProcessImpl_h */
339
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use