1 | /* $Id: GuestProcessImpl.cpp 94915 2022-05-08 19:26:54Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox Main - Guest process handling.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2012-2022 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 | /**
|
---|
19 | * Locking rules:
|
---|
20 | * - When the main dispatcher (callbackDispatcher) is called it takes the
|
---|
21 | * WriteLock while dispatching to the various on* methods.
|
---|
22 | * - All other outer functions (accessible by Main) must not own a lock
|
---|
23 | * while waiting for a callback or for an event.
|
---|
24 | * - Only keep Read/WriteLocks as short as possible and only when necessary.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #define LOG_GROUP LOG_GROUP_MAIN_GUESTPROCESS
|
---|
32 | #include "LoggingNew.h"
|
---|
33 |
|
---|
34 | #ifndef VBOX_WITH_GUEST_CONTROL
|
---|
35 | # error "VBOX_WITH_GUEST_CONTROL must defined in this file"
|
---|
36 | #endif
|
---|
37 | #include "GuestImpl.h"
|
---|
38 | #include "GuestProcessImpl.h"
|
---|
39 | #include "GuestSessionImpl.h"
|
---|
40 | #include "GuestCtrlImplPrivate.h"
|
---|
41 | #include "ConsoleImpl.h"
|
---|
42 | #include "VirtualBoxErrorInfoImpl.h"
|
---|
43 |
|
---|
44 | #include "Global.h"
|
---|
45 | #include "AutoCaller.h"
|
---|
46 | #include "VBoxEvents.h"
|
---|
47 | #include "ThreadTask.h"
|
---|
48 |
|
---|
49 | #include <memory> /* For auto_ptr. */
|
---|
50 |
|
---|
51 | #include <iprt/asm.h>
|
---|
52 | #include <iprt/cpp/utils.h> /* For unconst(). */
|
---|
53 | #include <iprt/getopt.h>
|
---|
54 |
|
---|
55 | #include <VBox/com/listeners.h>
|
---|
56 |
|
---|
57 | #include <VBox/com/array.h>
|
---|
58 |
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Base class for all guest process tasks.
|
---|
62 | */
|
---|
63 | class GuestProcessTask : public ThreadTask
|
---|
64 | {
|
---|
65 | public:
|
---|
66 |
|
---|
67 | GuestProcessTask(GuestProcess *pProcess)
|
---|
68 | : ThreadTask("GenericGuestProcessTask")
|
---|
69 | , mProcess(pProcess)
|
---|
70 | , mRC(VINF_SUCCESS) { }
|
---|
71 |
|
---|
72 | virtual ~GuestProcessTask(void) { }
|
---|
73 |
|
---|
74 | /** Returns the last set result code. */
|
---|
75 | int i_rc(void) const { return mRC; }
|
---|
76 | /** Returns whether the last set result is okay (successful) or not. */
|
---|
77 | bool i_isOk(void) const { return RT_SUCCESS(mRC); }
|
---|
78 | /** Returns the reference of the belonging progress object. */
|
---|
79 | const ComObjPtr<GuestProcess> &i_process(void) const { return mProcess; }
|
---|
80 |
|
---|
81 | protected:
|
---|
82 |
|
---|
83 | /** Progress object this process belongs to. */
|
---|
84 | const ComObjPtr<GuestProcess> mProcess;
|
---|
85 | /** Last set result code. */
|
---|
86 | int mRC;
|
---|
87 | };
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Task to start a process on the guest.
|
---|
91 | */
|
---|
92 | class GuestProcessStartTask : public GuestProcessTask
|
---|
93 | {
|
---|
94 | public:
|
---|
95 |
|
---|
96 | GuestProcessStartTask(GuestProcess *pProcess)
|
---|
97 | : GuestProcessTask(pProcess)
|
---|
98 | {
|
---|
99 | m_strTaskName = "gctlPrcStart";
|
---|
100 | }
|
---|
101 |
|
---|
102 | void handler()
|
---|
103 | {
|
---|
104 | GuestProcess::i_startProcessThreadTask(this);
|
---|
105 | }
|
---|
106 | };
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * Internal listener class to serve events in an
|
---|
110 | * active manner, e.g. without polling delays.
|
---|
111 | */
|
---|
112 | class GuestProcessListener
|
---|
113 | {
|
---|
114 | public:
|
---|
115 |
|
---|
116 | GuestProcessListener(void)
|
---|
117 | {
|
---|
118 | }
|
---|
119 |
|
---|
120 | virtual ~GuestProcessListener(void)
|
---|
121 | {
|
---|
122 | }
|
---|
123 |
|
---|
124 | HRESULT init(GuestProcess *pProcess)
|
---|
125 | {
|
---|
126 | AssertPtrReturn(pProcess, E_POINTER);
|
---|
127 | mProcess = pProcess;
|
---|
128 | return S_OK;
|
---|
129 | }
|
---|
130 |
|
---|
131 | void uninit(void)
|
---|
132 | {
|
---|
133 | mProcess = NULL;
|
---|
134 | }
|
---|
135 |
|
---|
136 | STDMETHOD(HandleEvent)(VBoxEventType_T aType, IEvent *aEvent)
|
---|
137 | {
|
---|
138 | switch (aType)
|
---|
139 | {
|
---|
140 | case VBoxEventType_OnGuestProcessStateChanged:
|
---|
141 | case VBoxEventType_OnGuestProcessInputNotify:
|
---|
142 | case VBoxEventType_OnGuestProcessOutput:
|
---|
143 | {
|
---|
144 | AssertPtrReturn(mProcess, E_POINTER);
|
---|
145 | int vrc2 = mProcess->signalWaitEvent(aType, aEvent);
|
---|
146 | RT_NOREF(vrc2);
|
---|
147 | #ifdef LOG_ENABLED
|
---|
148 | LogFlowThisFunc(("Signalling events of type=%RU32, pProcess=%p resulted in vrc=%Rrc\n",
|
---|
149 | aType, &mProcess, vrc2));
|
---|
150 | #endif
|
---|
151 | break;
|
---|
152 | }
|
---|
153 |
|
---|
154 | default:
|
---|
155 | AssertMsgFailed(("Unhandled event %RU32\n", aType));
|
---|
156 | break;
|
---|
157 | }
|
---|
158 |
|
---|
159 | return S_OK;
|
---|
160 | }
|
---|
161 |
|
---|
162 | private:
|
---|
163 |
|
---|
164 | GuestProcess *mProcess;
|
---|
165 | };
|
---|
166 | typedef ListenerImpl<GuestProcessListener, GuestProcess*> GuestProcessListenerImpl;
|
---|
167 |
|
---|
168 | VBOX_LISTENER_DECLARE(GuestProcessListenerImpl)
|
---|
169 |
|
---|
170 | // constructor / destructor
|
---|
171 | /////////////////////////////////////////////////////////////////////////////
|
---|
172 |
|
---|
173 | DEFINE_EMPTY_CTOR_DTOR(GuestProcess)
|
---|
174 |
|
---|
175 | HRESULT GuestProcess::FinalConstruct(void)
|
---|
176 | {
|
---|
177 | LogFlowThisFuncEnter();
|
---|
178 | return BaseFinalConstruct();
|
---|
179 | }
|
---|
180 |
|
---|
181 | void GuestProcess::FinalRelease(void)
|
---|
182 | {
|
---|
183 | LogFlowThisFuncEnter();
|
---|
184 | uninit();
|
---|
185 | BaseFinalRelease();
|
---|
186 | LogFlowThisFuncLeave();
|
---|
187 | }
|
---|
188 |
|
---|
189 | // public initializer/uninitializer for internal purposes only
|
---|
190 | /////////////////////////////////////////////////////////////////////////////
|
---|
191 |
|
---|
192 | /**
|
---|
193 | * Initialies a guest process object.
|
---|
194 | *
|
---|
195 | * @returns VBox status code.
|
---|
196 | * @param aConsole Console this process is bound to.
|
---|
197 | * @param aSession Guest session this process is bound to.
|
---|
198 | * @param aObjectID Object ID to use for this process object.
|
---|
199 | * @param aProcInfo Process startup information to use.
|
---|
200 | * @param pBaseEnv Guest environment to apply when starting the process on the guest.
|
---|
201 | */
|
---|
202 | int GuestProcess::init(Console *aConsole, GuestSession *aSession, ULONG aObjectID,
|
---|
203 | const GuestProcessStartupInfo &aProcInfo, const GuestEnvironment *pBaseEnv)
|
---|
204 | {
|
---|
205 | LogFlowThisFunc(("aConsole=%p, aSession=%p, aObjectID=%RU32, pBaseEnv=%p\n",
|
---|
206 | aConsole, aSession, aObjectID, pBaseEnv));
|
---|
207 |
|
---|
208 | AssertPtrReturn(aConsole, VERR_INVALID_POINTER);
|
---|
209 | AssertPtrReturn(aSession, VERR_INVALID_POINTER);
|
---|
210 |
|
---|
211 | /* Enclose the state transition NotReady->InInit->Ready. */
|
---|
212 | AutoInitSpan autoInitSpan(this);
|
---|
213 | AssertReturn(autoInitSpan.isOk(), VERR_OBJECT_DESTROYED);
|
---|
214 |
|
---|
215 | HRESULT hr;
|
---|
216 |
|
---|
217 | int vrc = bindToSession(aConsole, aSession, aObjectID);
|
---|
218 | if (RT_SUCCESS(vrc))
|
---|
219 | {
|
---|
220 | hr = unconst(mEventSource).createObject();
|
---|
221 | if (FAILED(hr))
|
---|
222 | vrc = VERR_NO_MEMORY;
|
---|
223 | else
|
---|
224 | {
|
---|
225 | hr = mEventSource->init();
|
---|
226 | if (FAILED(hr))
|
---|
227 | vrc = VERR_COM_UNEXPECTED;
|
---|
228 | }
|
---|
229 | }
|
---|
230 |
|
---|
231 | if (RT_SUCCESS(vrc))
|
---|
232 | {
|
---|
233 | try
|
---|
234 | {
|
---|
235 | GuestProcessListener *pListener = new GuestProcessListener();
|
---|
236 | ComObjPtr<GuestProcessListenerImpl> thisListener;
|
---|
237 | hr = thisListener.createObject();
|
---|
238 | if (SUCCEEDED(hr))
|
---|
239 | hr = thisListener->init(pListener, this);
|
---|
240 |
|
---|
241 | if (SUCCEEDED(hr))
|
---|
242 | {
|
---|
243 | com::SafeArray <VBoxEventType_T> eventTypes;
|
---|
244 | eventTypes.push_back(VBoxEventType_OnGuestProcessStateChanged);
|
---|
245 | eventTypes.push_back(VBoxEventType_OnGuestProcessInputNotify);
|
---|
246 | eventTypes.push_back(VBoxEventType_OnGuestProcessOutput);
|
---|
247 | hr = mEventSource->RegisterListener(thisListener,
|
---|
248 | ComSafeArrayAsInParam(eventTypes),
|
---|
249 | TRUE /* Active listener */);
|
---|
250 | if (SUCCEEDED(hr))
|
---|
251 | {
|
---|
252 | vrc = baseInit();
|
---|
253 | if (RT_SUCCESS(vrc))
|
---|
254 | {
|
---|
255 | mLocalListener = thisListener;
|
---|
256 | }
|
---|
257 | }
|
---|
258 | else
|
---|
259 | vrc = VERR_COM_UNEXPECTED;
|
---|
260 | }
|
---|
261 | else
|
---|
262 | vrc = VERR_COM_UNEXPECTED;
|
---|
263 | }
|
---|
264 | catch(std::bad_alloc &)
|
---|
265 | {
|
---|
266 | vrc = VERR_NO_MEMORY;
|
---|
267 | }
|
---|
268 | }
|
---|
269 |
|
---|
270 | if (RT_SUCCESS(vrc))
|
---|
271 | {
|
---|
272 | mData.mProcess = aProcInfo;
|
---|
273 | mData.mpSessionBaseEnv = pBaseEnv;
|
---|
274 | if (pBaseEnv)
|
---|
275 | pBaseEnv->retainConst();
|
---|
276 | mData.mExitCode = 0;
|
---|
277 | mData.mPID = 0;
|
---|
278 | mData.mLastError = VINF_SUCCESS;
|
---|
279 | mData.mStatus = ProcessStatus_Undefined;
|
---|
280 | /* Everything else will be set by the actual starting routine. */
|
---|
281 |
|
---|
282 | /* Confirm a successful initialization when it's the case. */
|
---|
283 | autoInitSpan.setSucceeded();
|
---|
284 |
|
---|
285 | return vrc;
|
---|
286 | }
|
---|
287 |
|
---|
288 | autoInitSpan.setFailed();
|
---|
289 | return vrc;
|
---|
290 | }
|
---|
291 |
|
---|
292 | /**
|
---|
293 | * Uninitializes the instance.
|
---|
294 | * Called from FinalRelease() or IGuestSession::uninit().
|
---|
295 | */
|
---|
296 | void GuestProcess::uninit(void)
|
---|
297 | {
|
---|
298 | /* Enclose the state transition Ready->InUninit->NotReady. */
|
---|
299 | AutoUninitSpan autoUninitSpan(this);
|
---|
300 | if (autoUninitSpan.uninitDone())
|
---|
301 | return;
|
---|
302 |
|
---|
303 | LogFlowThisFunc(("mExe=%s, PID=%RU32\n", mData.mProcess.mExecutable.c_str(), mData.mPID));
|
---|
304 |
|
---|
305 | if (mData.mpSessionBaseEnv)
|
---|
306 | {
|
---|
307 | mData.mpSessionBaseEnv->releaseConst();
|
---|
308 | mData.mpSessionBaseEnv = NULL;
|
---|
309 | }
|
---|
310 |
|
---|
311 | baseUninit();
|
---|
312 |
|
---|
313 | LogFlowFuncLeave();
|
---|
314 | }
|
---|
315 |
|
---|
316 | // implementation of public getters/setters for attributes
|
---|
317 | /////////////////////////////////////////////////////////////////////////////
|
---|
318 | HRESULT GuestProcess::getArguments(std::vector<com::Utf8Str> &aArguments)
|
---|
319 | {
|
---|
320 | LogFlowThisFuncEnter();
|
---|
321 |
|
---|
322 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
323 | aArguments = mData.mProcess.mArguments;
|
---|
324 | return S_OK;
|
---|
325 | }
|
---|
326 |
|
---|
327 | HRESULT GuestProcess::getEnvironment(std::vector<com::Utf8Str> &aEnvironment)
|
---|
328 | {
|
---|
329 | #ifndef VBOX_WITH_GUEST_CONTROL
|
---|
330 | ReturnComNotImplemented();
|
---|
331 | #else
|
---|
332 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS); /* (Paranoia since both environment objects are immutable.) */
|
---|
333 | HRESULT hrc;
|
---|
334 | if (mData.mpSessionBaseEnv)
|
---|
335 | {
|
---|
336 | int vrc;
|
---|
337 | if (mData.mProcess.mEnvironmentChanges.count() == 0)
|
---|
338 | vrc = mData.mpSessionBaseEnv->queryPutEnvArray(&aEnvironment);
|
---|
339 | else
|
---|
340 | {
|
---|
341 | GuestEnvironment TmpEnv;
|
---|
342 | vrc = TmpEnv.copy(*mData.mpSessionBaseEnv);
|
---|
343 | if (RT_SUCCESS(vrc))
|
---|
344 | {
|
---|
345 | vrc = TmpEnv.applyChanges(mData.mProcess.mEnvironmentChanges);
|
---|
346 | if (RT_SUCCESS(vrc))
|
---|
347 | vrc = TmpEnv.queryPutEnvArray(&aEnvironment);
|
---|
348 | }
|
---|
349 | }
|
---|
350 | hrc = Global::vboxStatusCodeToCOM(vrc);
|
---|
351 | }
|
---|
352 | else
|
---|
353 | hrc = setError(VBOX_E_NOT_SUPPORTED, tr("The base environment feature is not supported by installed Guest Additions"));
|
---|
354 | LogFlowThisFuncLeave();
|
---|
355 | return hrc;
|
---|
356 | #endif
|
---|
357 | }
|
---|
358 |
|
---|
359 | HRESULT GuestProcess::getEventSource(ComPtr<IEventSource> &aEventSource)
|
---|
360 | {
|
---|
361 | LogFlowThisFuncEnter();
|
---|
362 |
|
---|
363 | // no need to lock - lifetime constant
|
---|
364 | mEventSource.queryInterfaceTo(aEventSource.asOutParam());
|
---|
365 |
|
---|
366 | LogFlowThisFuncLeave();
|
---|
367 | return S_OK;
|
---|
368 | }
|
---|
369 |
|
---|
370 | HRESULT GuestProcess::getExecutablePath(com::Utf8Str &aExecutablePath)
|
---|
371 | {
|
---|
372 | LogFlowThisFuncEnter();
|
---|
373 |
|
---|
374 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
375 |
|
---|
376 | aExecutablePath = mData.mProcess.mExecutable;
|
---|
377 |
|
---|
378 | return S_OK;
|
---|
379 | }
|
---|
380 |
|
---|
381 | HRESULT GuestProcess::getExitCode(LONG *aExitCode)
|
---|
382 | {
|
---|
383 | LogFlowThisFuncEnter();
|
---|
384 |
|
---|
385 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
386 |
|
---|
387 | *aExitCode = mData.mExitCode;
|
---|
388 |
|
---|
389 | return S_OK;
|
---|
390 | }
|
---|
391 |
|
---|
392 | HRESULT GuestProcess::getName(com::Utf8Str &aName)
|
---|
393 | {
|
---|
394 | LogFlowThisFuncEnter();
|
---|
395 |
|
---|
396 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
397 |
|
---|
398 | aName = mData.mProcess.mName;
|
---|
399 |
|
---|
400 | return S_OK;
|
---|
401 | }
|
---|
402 |
|
---|
403 | HRESULT GuestProcess::getPID(ULONG *aPID)
|
---|
404 | {
|
---|
405 | LogFlowThisFuncEnter();
|
---|
406 |
|
---|
407 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
408 |
|
---|
409 | *aPID = mData.mPID;
|
---|
410 |
|
---|
411 | return S_OK;
|
---|
412 | }
|
---|
413 |
|
---|
414 | HRESULT GuestProcess::getStatus(ProcessStatus_T *aStatus)
|
---|
415 | {
|
---|
416 | LogFlowThisFuncEnter();
|
---|
417 |
|
---|
418 | *aStatus = i_getStatus();
|
---|
419 |
|
---|
420 | return S_OK;
|
---|
421 | }
|
---|
422 |
|
---|
423 | // private methods
|
---|
424 | /////////////////////////////////////////////////////////////////////////////
|
---|
425 |
|
---|
426 | /**
|
---|
427 | * Entry point for guest side process callbacks.
|
---|
428 | *
|
---|
429 | * @returns VBox status code.
|
---|
430 | * @param pCbCtx Host callback context.
|
---|
431 | * @param pSvcCb Host callback data.
|
---|
432 | */
|
---|
433 | int GuestProcess::i_callbackDispatcher(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCb)
|
---|
434 | {
|
---|
435 | AssertPtrReturn(pCbCtx, VERR_INVALID_POINTER);
|
---|
436 | AssertPtrReturn(pSvcCb, VERR_INVALID_POINTER);
|
---|
437 | #ifdef DEBUG
|
---|
438 | LogFlowThisFunc(("uPID=%RU32, uContextID=%RU32, uMessage=%RU32, pSvcCb=%p\n",
|
---|
439 | mData.mPID, pCbCtx->uContextID, pCbCtx->uMessage, pSvcCb));
|
---|
440 | #endif
|
---|
441 |
|
---|
442 | int vrc;
|
---|
443 | switch (pCbCtx->uMessage)
|
---|
444 | {
|
---|
445 | case GUEST_MSG_DISCONNECTED:
|
---|
446 | {
|
---|
447 | vrc = i_onGuestDisconnected(pCbCtx, pSvcCb);
|
---|
448 | break;
|
---|
449 | }
|
---|
450 |
|
---|
451 | case GUEST_MSG_EXEC_STATUS:
|
---|
452 | {
|
---|
453 | vrc = i_onProcessStatusChange(pCbCtx, pSvcCb);
|
---|
454 | break;
|
---|
455 | }
|
---|
456 |
|
---|
457 | case GUEST_MSG_EXEC_OUTPUT:
|
---|
458 | {
|
---|
459 | vrc = i_onProcessOutput(pCbCtx, pSvcCb);
|
---|
460 | break;
|
---|
461 | }
|
---|
462 |
|
---|
463 | case GUEST_MSG_EXEC_INPUT_STATUS:
|
---|
464 | {
|
---|
465 | vrc = i_onProcessInputStatus(pCbCtx, pSvcCb);
|
---|
466 | break;
|
---|
467 | }
|
---|
468 |
|
---|
469 | default:
|
---|
470 | /* Silently ignore not implemented functions. */
|
---|
471 | vrc = VERR_NOT_SUPPORTED;
|
---|
472 | break;
|
---|
473 | }
|
---|
474 |
|
---|
475 | #ifdef DEBUG
|
---|
476 | LogFlowFuncLeaveRC(vrc);
|
---|
477 | #endif
|
---|
478 | return vrc;
|
---|
479 | }
|
---|
480 |
|
---|
481 | /**
|
---|
482 | * Checks if the current assigned PID matches another PID (from a callback).
|
---|
483 | *
|
---|
484 | * In protocol v1 we don't have the possibility to terminate/kill
|
---|
485 | * processes so it can happen that a formerly started process A
|
---|
486 | * (which has the context ID 0 (session=0, process=0, count=0) will
|
---|
487 | * send a delayed message to the host if this process has already
|
---|
488 | * been discarded there and the same context ID was reused by
|
---|
489 | * a process B. Process B in turn then has a different guest PID.
|
---|
490 | *
|
---|
491 | * Note: This also can happen when restoring from a saved state which
|
---|
492 | * had a guest process running.
|
---|
493 | *
|
---|
494 | * @return IPRT status code.
|
---|
495 | * @param uPID PID to check.
|
---|
496 | */
|
---|
497 | inline int GuestProcess::i_checkPID(uint32_t uPID)
|
---|
498 | {
|
---|
499 | int vrc = VINF_SUCCESS;
|
---|
500 |
|
---|
501 | /* Was there a PID assigned yet? */
|
---|
502 | if (mData.mPID)
|
---|
503 | {
|
---|
504 | if (RT_UNLIKELY(mData.mPID != uPID))
|
---|
505 | {
|
---|
506 | LogFlowFunc(("Stale guest process (PID=%RU32) sent data to a newly started process (pProcesS=%p, PID=%RU32, status=%RU32)\n",
|
---|
507 | uPID, this, mData.mPID, mData.mStatus));
|
---|
508 | vrc = VERR_NOT_FOUND;
|
---|
509 | }
|
---|
510 | }
|
---|
511 |
|
---|
512 | return vrc;
|
---|
513 | }
|
---|
514 |
|
---|
515 | /**
|
---|
516 | * Returns the current process status.
|
---|
517 | *
|
---|
518 | * @returns Current process status.
|
---|
519 | *
|
---|
520 | * @note Takes the read lock.
|
---|
521 | */
|
---|
522 | ProcessStatus_T GuestProcess::i_getStatus(void)
|
---|
523 | {
|
---|
524 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
525 |
|
---|
526 | return mData.mStatus;
|
---|
527 | }
|
---|
528 |
|
---|
529 | /**
|
---|
530 | * Converts a given guest process error to a string.
|
---|
531 | *
|
---|
532 | * @returns Error as a string.
|
---|
533 | * @param rcGuest Guest process error to return string for.
|
---|
534 | * @param pcszWhat Hint of what was involved when the error occurred.
|
---|
535 | */
|
---|
536 | /* static */
|
---|
537 | Utf8Str GuestProcess::i_guestErrorToString(int rcGuest, const char *pcszWhat)
|
---|
538 | {
|
---|
539 | AssertPtrReturn(pcszWhat, "");
|
---|
540 |
|
---|
541 | Utf8Str strErr;
|
---|
542 | switch (rcGuest)
|
---|
543 | {
|
---|
544 | #define CASE_MSG(a_iRc, ...) \
|
---|
545 | case a_iRc: strErr.printf(__VA_ARGS__); break;
|
---|
546 |
|
---|
547 | CASE_MSG(VERR_FILE_NOT_FOUND, tr("No such file or directory \"%s\" on guest"), pcszWhat); /* This is the most likely error. */
|
---|
548 | CASE_MSG(VERR_PATH_NOT_FOUND, tr("No such file or directory \"%s\" on guest"), pcszWhat);
|
---|
549 | CASE_MSG(VERR_INVALID_VM_HANDLE, tr("VMM device is not available (is the VM running?)"));
|
---|
550 | CASE_MSG(VERR_HGCM_SERVICE_NOT_FOUND, tr("The guest execution service is not available"));
|
---|
551 | CASE_MSG(VERR_BAD_EXE_FORMAT, tr("The file \"%s\" is not an executable format on guest"), pcszWhat);
|
---|
552 | CASE_MSG(VERR_AUTHENTICATION_FAILURE, tr("The user \"%s\" was not able to logon on guest"), pcszWhat);
|
---|
553 | CASE_MSG(VERR_INVALID_NAME, tr("The file \"%s\" is an invalid name"), pcszWhat);
|
---|
554 | CASE_MSG(VERR_TIMEOUT, tr("The guest did not respond within time"));
|
---|
555 | CASE_MSG(VERR_CANCELLED, tr("The execution operation for \"%s\" was canceled"), pcszWhat);
|
---|
556 | CASE_MSG(VERR_GSTCTL_MAX_CID_OBJECTS_REACHED, tr("Maximum number of concurrent guest processes has been reached"));
|
---|
557 | CASE_MSG(VERR_NOT_FOUND, tr("The guest execution service is not ready (yet)"));
|
---|
558 | default:
|
---|
559 | strErr.printf(tr("Error %Rrc for guest process \"%s\" occurred\n"), rcGuest, pcszWhat);
|
---|
560 | break;
|
---|
561 | #undef CASE_MSG
|
---|
562 | }
|
---|
563 |
|
---|
564 | return strErr;
|
---|
565 | }
|
---|
566 |
|
---|
567 | /**
|
---|
568 | * Translates a process status to a human readable string.
|
---|
569 | *
|
---|
570 | * @returns Process status as a string.
|
---|
571 | * @param enmStatus Guest process status to return string for.
|
---|
572 | */
|
---|
573 | /* static */
|
---|
574 | Utf8Str GuestProcess::i_statusToString(ProcessStatus_T enmStatus)
|
---|
575 | {
|
---|
576 | switch (enmStatus)
|
---|
577 | {
|
---|
578 | case ProcessStatus_Starting:
|
---|
579 | return "starting";
|
---|
580 | case ProcessStatus_Started:
|
---|
581 | return "started";
|
---|
582 | case ProcessStatus_Paused:
|
---|
583 | return "paused";
|
---|
584 | case ProcessStatus_Terminating:
|
---|
585 | return "terminating";
|
---|
586 | case ProcessStatus_TerminatedNormally:
|
---|
587 | return "successfully terminated";
|
---|
588 | case ProcessStatus_TerminatedSignal:
|
---|
589 | return "terminated by signal";
|
---|
590 | case ProcessStatus_TerminatedAbnormally:
|
---|
591 | return "abnormally aborted";
|
---|
592 | case ProcessStatus_TimedOutKilled:
|
---|
593 | return "timed out";
|
---|
594 | case ProcessStatus_TimedOutAbnormally:
|
---|
595 | return "timed out, hanging";
|
---|
596 | case ProcessStatus_Down:
|
---|
597 | return "killed";
|
---|
598 | case ProcessStatus_Error:
|
---|
599 | return "error";
|
---|
600 | default:
|
---|
601 | break;
|
---|
602 | }
|
---|
603 |
|
---|
604 | AssertFailed(); /* Should never happen! */
|
---|
605 | return "unknown";
|
---|
606 | }
|
---|
607 |
|
---|
608 | /**
|
---|
609 | * Returns @c true if the passed in error code indicates an error which came
|
---|
610 | * from the guest side, or @c false if not.
|
---|
611 | *
|
---|
612 | * @return bool @c true if the passed in error code indicates an error which came
|
---|
613 | * from the guest side, or @c false if not.
|
---|
614 | * @param rc Error code to check.
|
---|
615 | */
|
---|
616 | /* static */
|
---|
617 | bool GuestProcess::i_isGuestError(int rc)
|
---|
618 | {
|
---|
619 | return ( rc == VERR_GSTCTL_GUEST_ERROR
|
---|
620 | || rc == VERR_GSTCTL_PROCESS_EXIT_CODE);
|
---|
621 | }
|
---|
622 |
|
---|
623 | /**
|
---|
624 | * Returns whether the guest process is alive (i.e. running) or not.
|
---|
625 | *
|
---|
626 | * @returns \c true if alive and running, or \c false if not.
|
---|
627 | */
|
---|
628 | inline bool GuestProcess::i_isAlive(void)
|
---|
629 | {
|
---|
630 | return ( mData.mStatus == ProcessStatus_Started
|
---|
631 | || mData.mStatus == ProcessStatus_Paused
|
---|
632 | || mData.mStatus == ProcessStatus_Terminating);
|
---|
633 | }
|
---|
634 |
|
---|
635 | /**
|
---|
636 | * Returns whether the guest process has ended (i.e. terminated) or not.
|
---|
637 | *
|
---|
638 | * @returns \c true if ended, or \c false if not.
|
---|
639 | */
|
---|
640 | inline bool GuestProcess::i_hasEnded(void)
|
---|
641 | {
|
---|
642 | return ( mData.mStatus == ProcessStatus_TerminatedNormally
|
---|
643 | || mData.mStatus == ProcessStatus_TerminatedSignal
|
---|
644 | || mData.mStatus == ProcessStatus_TerminatedAbnormally
|
---|
645 | || mData.mStatus == ProcessStatus_TimedOutKilled
|
---|
646 | || mData.mStatus == ProcessStatus_TimedOutAbnormally
|
---|
647 | || mData.mStatus == ProcessStatus_Down
|
---|
648 | || mData.mStatus == ProcessStatus_Error);
|
---|
649 | }
|
---|
650 |
|
---|
651 | /**
|
---|
652 | * Called when the guest side of the process has been disconnected (closed, terminated, +++).
|
---|
653 | *
|
---|
654 | * @returns VBox status code.
|
---|
655 | * @param pCbCtx Host callback context.
|
---|
656 | * @param pSvcCbData Host callback data.
|
---|
657 | */
|
---|
658 | int GuestProcess::i_onGuestDisconnected(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData)
|
---|
659 | {
|
---|
660 | AssertPtrReturn(pCbCtx, VERR_INVALID_POINTER);
|
---|
661 | AssertPtrReturn(pSvcCbData, VERR_INVALID_POINTER);
|
---|
662 |
|
---|
663 | int vrc = i_setProcessStatus(ProcessStatus_Down, VINF_SUCCESS);
|
---|
664 |
|
---|
665 | LogFlowFuncLeaveRC(vrc);
|
---|
666 | return vrc;
|
---|
667 | }
|
---|
668 |
|
---|
669 | /**
|
---|
670 | * Sets (reports) the current input status of the guest process.
|
---|
671 | *
|
---|
672 | * @returns VBox status code.
|
---|
673 | * @param pCbCtx Host callback context.
|
---|
674 | * @param pSvcCbData Host callback data.
|
---|
675 | *
|
---|
676 | * @note Takes the write lock.
|
---|
677 | */
|
---|
678 | int GuestProcess::i_onProcessInputStatus(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData)
|
---|
679 | {
|
---|
680 | AssertPtrReturn(pCbCtx, VERR_INVALID_POINTER);
|
---|
681 | AssertPtrReturn(pSvcCbData, VERR_INVALID_POINTER);
|
---|
682 | /* pCallback is optional. */
|
---|
683 |
|
---|
684 | if (pSvcCbData->mParms < 5)
|
---|
685 | return VERR_INVALID_PARAMETER;
|
---|
686 |
|
---|
687 | CALLBACKDATA_PROC_INPUT dataCb;
|
---|
688 | /* pSvcCb->mpaParms[0] always contains the context ID. */
|
---|
689 | int vrc = HGCMSvcGetU32(&pSvcCbData->mpaParms[1], &dataCb.uPID);
|
---|
690 | AssertRCReturn(vrc, vrc);
|
---|
691 | vrc = HGCMSvcGetU32(&pSvcCbData->mpaParms[2], &dataCb.uStatus);
|
---|
692 | AssertRCReturn(vrc, vrc);
|
---|
693 | vrc = HGCMSvcGetU32(&pSvcCbData->mpaParms[3], &dataCb.uFlags);
|
---|
694 | AssertRCReturn(vrc, vrc);
|
---|
695 | vrc = HGCMSvcGetU32(&pSvcCbData->mpaParms[4], &dataCb.uProcessed);
|
---|
696 | AssertRCReturn(vrc, vrc);
|
---|
697 |
|
---|
698 | LogFlowThisFunc(("uPID=%RU32, uStatus=%RU32, uFlags=%RI32, cbProcessed=%RU32\n",
|
---|
699 | dataCb.uPID, dataCb.uStatus, dataCb.uFlags, dataCb.uProcessed));
|
---|
700 |
|
---|
701 | vrc = i_checkPID(dataCb.uPID);
|
---|
702 | if (RT_SUCCESS(vrc))
|
---|
703 | {
|
---|
704 | ProcessInputStatus_T inputStatus = ProcessInputStatus_Undefined;
|
---|
705 | switch (dataCb.uStatus)
|
---|
706 | {
|
---|
707 | case INPUT_STS_WRITTEN:
|
---|
708 | inputStatus = ProcessInputStatus_Written;
|
---|
709 | break;
|
---|
710 | case INPUT_STS_ERROR:
|
---|
711 | inputStatus = ProcessInputStatus_Broken;
|
---|
712 | break;
|
---|
713 | case INPUT_STS_TERMINATED:
|
---|
714 | inputStatus = ProcessInputStatus_Broken;
|
---|
715 | break;
|
---|
716 | case INPUT_STS_OVERFLOW:
|
---|
717 | inputStatus = ProcessInputStatus_Overflow;
|
---|
718 | break;
|
---|
719 | case INPUT_STS_UNDEFINED:
|
---|
720 | /* Fall through is intentional. */
|
---|
721 | default:
|
---|
722 | AssertMsg(!dataCb.uProcessed, ("Processed data is not 0 in undefined input state\n"));
|
---|
723 | break;
|
---|
724 | }
|
---|
725 |
|
---|
726 | if (inputStatus != ProcessInputStatus_Undefined)
|
---|
727 | {
|
---|
728 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
729 |
|
---|
730 | /* Copy over necessary data before releasing lock again. */
|
---|
731 | uint32_t uPID = mData.mPID;
|
---|
732 | /** @todo Also handle mSession? */
|
---|
733 |
|
---|
734 | alock.release(); /* Release lock before firing off event. */
|
---|
735 |
|
---|
736 | ::FireGuestProcessInputNotifyEvent(mEventSource, mSession, this, uPID, 0 /* StdIn */, dataCb.uProcessed, inputStatus);
|
---|
737 | }
|
---|
738 | }
|
---|
739 |
|
---|
740 | LogFlowFuncLeaveRC(vrc);
|
---|
741 | return vrc;
|
---|
742 | }
|
---|
743 |
|
---|
744 | /**
|
---|
745 | * Notifies of an I/O operation of the guest process.
|
---|
746 | *
|
---|
747 | * @returns VERR_NOT_IMPLEMENTED -- not implemented yet.
|
---|
748 | * @param pCbCtx Host callback context.
|
---|
749 | * @param pSvcCbData Host callback data.
|
---|
750 | */
|
---|
751 | int GuestProcess::i_onProcessNotifyIO(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData)
|
---|
752 | {
|
---|
753 | AssertPtrReturn(pCbCtx, VERR_INVALID_POINTER);
|
---|
754 | AssertPtrReturn(pSvcCbData, VERR_INVALID_POINTER);
|
---|
755 |
|
---|
756 | return VERR_NOT_IMPLEMENTED;
|
---|
757 | }
|
---|
758 |
|
---|
759 | /**
|
---|
760 | * Sets (reports) the current running status of the guest process.
|
---|
761 | *
|
---|
762 | * @returns VBox status code.
|
---|
763 | * @param pCbCtx Host callback context.
|
---|
764 | * @param pSvcCbData Host callback data.
|
---|
765 | *
|
---|
766 | * @note Takes the write lock.
|
---|
767 | */
|
---|
768 | int GuestProcess::i_onProcessStatusChange(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData)
|
---|
769 | {
|
---|
770 | AssertPtrReturn(pCbCtx, VERR_INVALID_POINTER);
|
---|
771 | AssertPtrReturn(pSvcCbData, VERR_INVALID_POINTER);
|
---|
772 |
|
---|
773 | if (pSvcCbData->mParms < 5)
|
---|
774 | return VERR_INVALID_PARAMETER;
|
---|
775 |
|
---|
776 | CALLBACKDATA_PROC_STATUS dataCb;
|
---|
777 | /* pSvcCb->mpaParms[0] always contains the context ID. */
|
---|
778 | int vrc = HGCMSvcGetU32(&pSvcCbData->mpaParms[1], &dataCb.uPID);
|
---|
779 | AssertRCReturn(vrc, vrc);
|
---|
780 | vrc = HGCMSvcGetU32(&pSvcCbData->mpaParms[2], &dataCb.uStatus);
|
---|
781 | AssertRCReturn(vrc, vrc);
|
---|
782 | vrc = HGCMSvcGetU32(&pSvcCbData->mpaParms[3], &dataCb.uFlags);
|
---|
783 | AssertRCReturn(vrc, vrc);
|
---|
784 | vrc = HGCMSvcGetPv(&pSvcCbData->mpaParms[4], &dataCb.pvData, &dataCb.cbData);
|
---|
785 | AssertRCReturn(vrc, vrc);
|
---|
786 |
|
---|
787 | LogFlowThisFunc(("uPID=%RU32, uStatus=%RU32, uFlags=%RU32\n",
|
---|
788 | dataCb.uPID, dataCb.uStatus, dataCb.uFlags));
|
---|
789 |
|
---|
790 | vrc = i_checkPID(dataCb.uPID);
|
---|
791 | if (RT_SUCCESS(vrc))
|
---|
792 | {
|
---|
793 | ProcessStatus_T procStatus = ProcessStatus_Undefined;
|
---|
794 | int procRc = VINF_SUCCESS;
|
---|
795 |
|
---|
796 | switch (dataCb.uStatus)
|
---|
797 | {
|
---|
798 | case PROC_STS_STARTED:
|
---|
799 | {
|
---|
800 | procStatus = ProcessStatus_Started;
|
---|
801 |
|
---|
802 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
803 | mData.mPID = dataCb.uPID; /* Set the process PID. */
|
---|
804 | break;
|
---|
805 | }
|
---|
806 |
|
---|
807 | case PROC_STS_TEN:
|
---|
808 | {
|
---|
809 | procStatus = ProcessStatus_TerminatedNormally;
|
---|
810 |
|
---|
811 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
812 | mData.mExitCode = dataCb.uFlags; /* Contains the exit code. */
|
---|
813 | break;
|
---|
814 | }
|
---|
815 |
|
---|
816 | case PROC_STS_TES:
|
---|
817 | {
|
---|
818 | procStatus = ProcessStatus_TerminatedSignal;
|
---|
819 |
|
---|
820 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
821 | mData.mExitCode = dataCb.uFlags; /* Contains the signal. */
|
---|
822 | break;
|
---|
823 | }
|
---|
824 |
|
---|
825 | case PROC_STS_TEA:
|
---|
826 | {
|
---|
827 | procStatus = ProcessStatus_TerminatedAbnormally;
|
---|
828 | break;
|
---|
829 | }
|
---|
830 |
|
---|
831 | case PROC_STS_TOK:
|
---|
832 | {
|
---|
833 | procStatus = ProcessStatus_TimedOutKilled;
|
---|
834 | break;
|
---|
835 | }
|
---|
836 |
|
---|
837 | case PROC_STS_TOA:
|
---|
838 | {
|
---|
839 | procStatus = ProcessStatus_TimedOutAbnormally;
|
---|
840 | break;
|
---|
841 | }
|
---|
842 |
|
---|
843 | case PROC_STS_DWN:
|
---|
844 | {
|
---|
845 | procStatus = ProcessStatus_Down;
|
---|
846 | break;
|
---|
847 | }
|
---|
848 |
|
---|
849 | case PROC_STS_ERROR:
|
---|
850 | {
|
---|
851 | procRc = dataCb.uFlags; /* mFlags contains the IPRT error sent from the guest. */
|
---|
852 | procStatus = ProcessStatus_Error;
|
---|
853 | break;
|
---|
854 | }
|
---|
855 |
|
---|
856 | case PROC_STS_UNDEFINED:
|
---|
857 | default:
|
---|
858 | {
|
---|
859 | /* Silently skip this request. */
|
---|
860 | procStatus = ProcessStatus_Undefined;
|
---|
861 | break;
|
---|
862 | }
|
---|
863 | }
|
---|
864 |
|
---|
865 | LogFlowThisFunc(("Got rc=%Rrc, procSts=%RU32, procRc=%Rrc\n",
|
---|
866 | vrc, procStatus, procRc));
|
---|
867 |
|
---|
868 | /* Set the process status. */
|
---|
869 | int vrc2 = i_setProcessStatus(procStatus, procRc);
|
---|
870 | if (RT_SUCCESS(vrc))
|
---|
871 | vrc = vrc2;
|
---|
872 | }
|
---|
873 |
|
---|
874 | LogFlowFuncLeaveRC(vrc);
|
---|
875 | return vrc;
|
---|
876 | }
|
---|
877 |
|
---|
878 | /**
|
---|
879 | * Sets (reports) the current output status of the guest process.
|
---|
880 | *
|
---|
881 | * @returns VBox status code.
|
---|
882 | * @param pCbCtx Host callback context.
|
---|
883 | * @param pSvcCbData Host callback data.
|
---|
884 | */
|
---|
885 | int GuestProcess::i_onProcessOutput(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData)
|
---|
886 | {
|
---|
887 | RT_NOREF(pCbCtx);
|
---|
888 | AssertPtrReturn(pSvcCbData, VERR_INVALID_POINTER);
|
---|
889 |
|
---|
890 | if (pSvcCbData->mParms < 5)
|
---|
891 | return VERR_INVALID_PARAMETER;
|
---|
892 |
|
---|
893 | CALLBACKDATA_PROC_OUTPUT dataCb;
|
---|
894 | /* pSvcCb->mpaParms[0] always contains the context ID. */
|
---|
895 | int vrc = HGCMSvcGetU32(&pSvcCbData->mpaParms[1], &dataCb.uPID);
|
---|
896 | AssertRCReturn(vrc, vrc);
|
---|
897 | vrc = HGCMSvcGetU32(&pSvcCbData->mpaParms[2], &dataCb.uHandle);
|
---|
898 | AssertRCReturn(vrc, vrc);
|
---|
899 | vrc = HGCMSvcGetU32(&pSvcCbData->mpaParms[3], &dataCb.uFlags);
|
---|
900 | AssertRCReturn(vrc, vrc);
|
---|
901 | vrc = HGCMSvcGetPv(&pSvcCbData->mpaParms[4], &dataCb.pvData, &dataCb.cbData);
|
---|
902 | AssertRCReturn(vrc, vrc);
|
---|
903 |
|
---|
904 | LogFlowThisFunc(("uPID=%RU32, uHandle=%RU32, uFlags=%RI32, pvData=%p, cbData=%RU32\n",
|
---|
905 | dataCb.uPID, dataCb.uHandle, dataCb.uFlags, dataCb.pvData, dataCb.cbData));
|
---|
906 |
|
---|
907 | vrc = i_checkPID(dataCb.uPID);
|
---|
908 | if (RT_SUCCESS(vrc))
|
---|
909 | {
|
---|
910 | com::SafeArray<BYTE> data((size_t)dataCb.cbData);
|
---|
911 | if (dataCb.cbData)
|
---|
912 | data.initFrom((BYTE*)dataCb.pvData, dataCb.cbData);
|
---|
913 |
|
---|
914 | ::FireGuestProcessOutputEvent(mEventSource, mSession, this,
|
---|
915 | mData.mPID, dataCb.uHandle, dataCb.cbData, ComSafeArrayAsInParam(data));
|
---|
916 | }
|
---|
917 |
|
---|
918 | LogFlowFuncLeaveRC(vrc);
|
---|
919 | return vrc;
|
---|
920 | }
|
---|
921 |
|
---|
922 | /**
|
---|
923 | * @copydoc GuestObject::i_onUnregister
|
---|
924 | */
|
---|
925 | int GuestProcess::i_onUnregister(void)
|
---|
926 | {
|
---|
927 | LogFlowThisFuncEnter();
|
---|
928 |
|
---|
929 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
930 |
|
---|
931 | int vrc = VINF_SUCCESS;
|
---|
932 |
|
---|
933 | /*
|
---|
934 | * Note: The event source stuff holds references to this object,
|
---|
935 | * so make sure that this is cleaned up *before* calling uninit().
|
---|
936 | */
|
---|
937 | if (!mEventSource.isNull())
|
---|
938 | {
|
---|
939 | mEventSource->UnregisterListener(mLocalListener);
|
---|
940 |
|
---|
941 | mLocalListener.setNull();
|
---|
942 | unconst(mEventSource).setNull();
|
---|
943 | }
|
---|
944 |
|
---|
945 | LogFlowFuncLeaveRC(vrc);
|
---|
946 | return vrc;
|
---|
947 | }
|
---|
948 |
|
---|
949 | /**
|
---|
950 | * @copydoc GuestObject::i_onSessionStatusChange
|
---|
951 | */
|
---|
952 | int GuestProcess::i_onSessionStatusChange(GuestSessionStatus_T enmSessionStatus)
|
---|
953 | {
|
---|
954 | LogFlowThisFuncEnter();
|
---|
955 |
|
---|
956 | int vrc = VINF_SUCCESS;
|
---|
957 |
|
---|
958 | /* If the session now is in a terminated state, set the process status
|
---|
959 | * to "down", as there is not much else we can do now. */
|
---|
960 | if (GuestSession::i_isTerminated(enmSessionStatus))
|
---|
961 | {
|
---|
962 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
963 |
|
---|
964 | vrc = i_setProcessStatus(ProcessStatus_Down, 0 /* rc, ignored */);
|
---|
965 | }
|
---|
966 |
|
---|
967 | LogFlowFuncLeaveRC(vrc);
|
---|
968 | return vrc;
|
---|
969 | }
|
---|
970 |
|
---|
971 | /**
|
---|
972 | * Reads data from a guest file.
|
---|
973 | *
|
---|
974 | * @returns VBox status code.
|
---|
975 | * @retval VERR_GSTCTL_GUEST_ERROR when an error from the guest side has been received.
|
---|
976 | * @param uHandle Internal file handle to use for reading.
|
---|
977 | * @param uSize Size (in bytes) to read.
|
---|
978 | * @param uTimeoutMS Timeout (in ms) to wait.
|
---|
979 | * @param pvData Where to store the read data on success.
|
---|
980 | * @param cbData Size (in bytes) of \a pvData on input.
|
---|
981 | * @param pcbRead Where to return to size (in bytes) read on success.
|
---|
982 | * Optional.
|
---|
983 | * @param prcGuest Where to return the guest error when VERR_GSTCTL_GUEST_ERROR
|
---|
984 | * was returned. Optional.
|
---|
985 | *
|
---|
986 | * @note Takes the write lock.
|
---|
987 | */
|
---|
988 | int GuestProcess::i_readData(uint32_t uHandle, uint32_t uSize, uint32_t uTimeoutMS,
|
---|
989 | void *pvData, size_t cbData, uint32_t *pcbRead, int *prcGuest)
|
---|
990 | {
|
---|
991 | LogFlowThisFunc(("uPID=%RU32, uHandle=%RU32, uSize=%RU32, uTimeoutMS=%RU32, pvData=%p, cbData=%RU32, prcGuest=%p\n",
|
---|
992 | mData.mPID, uHandle, uSize, uTimeoutMS, pvData, cbData, prcGuest));
|
---|
993 | AssertReturn(uSize, VERR_INVALID_PARAMETER);
|
---|
994 | AssertPtrReturn(pvData, VERR_INVALID_POINTER);
|
---|
995 | AssertReturn(cbData >= uSize, VERR_INVALID_PARAMETER);
|
---|
996 | /* pcbRead is optional. */
|
---|
997 |
|
---|
998 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
999 |
|
---|
1000 | if ( mData.mStatus != ProcessStatus_Started
|
---|
1001 | /* Skip reading if the process wasn't started with the appropriate
|
---|
1002 | * flags. */
|
---|
1003 | || ( ( uHandle == GUEST_PROC_OUT_H_STDOUT
|
---|
1004 | || uHandle == GUEST_PROC_OUT_H_STDOUT_DEPRECATED)
|
---|
1005 | && !(mData.mProcess.mFlags & ProcessCreateFlag_WaitForStdOut))
|
---|
1006 | || ( uHandle == GUEST_PROC_OUT_H_STDERR
|
---|
1007 | && !(mData.mProcess.mFlags & ProcessCreateFlag_WaitForStdErr))
|
---|
1008 | )
|
---|
1009 | {
|
---|
1010 | if (pcbRead)
|
---|
1011 | *pcbRead = 0;
|
---|
1012 | if (prcGuest)
|
---|
1013 | *prcGuest = VINF_SUCCESS;
|
---|
1014 | return VINF_SUCCESS; /* Nothing to read anymore. */
|
---|
1015 | }
|
---|
1016 |
|
---|
1017 | int vrc;
|
---|
1018 |
|
---|
1019 | GuestWaitEvent *pEvent = NULL;
|
---|
1020 | GuestEventTypes eventTypes;
|
---|
1021 | try
|
---|
1022 | {
|
---|
1023 | /*
|
---|
1024 | * On Guest Additions < 4.3 there is no guarantee that the process status
|
---|
1025 | * change arrives *after* the output event, e.g. if this was the last output
|
---|
1026 | * block being read and the process will report status "terminate".
|
---|
1027 | * So just skip checking for process status change and only wait for the
|
---|
1028 | * output event.
|
---|
1029 | */
|
---|
1030 | if (mSession->i_getProtocolVersion() >= 2)
|
---|
1031 | eventTypes.push_back(VBoxEventType_OnGuestProcessStateChanged);
|
---|
1032 | eventTypes.push_back(VBoxEventType_OnGuestProcessOutput);
|
---|
1033 |
|
---|
1034 | vrc = registerWaitEvent(eventTypes, &pEvent);
|
---|
1035 | }
|
---|
1036 | catch (std::bad_alloc &)
|
---|
1037 | {
|
---|
1038 | vrc = VERR_NO_MEMORY;
|
---|
1039 | }
|
---|
1040 |
|
---|
1041 | if (RT_FAILURE(vrc))
|
---|
1042 | return vrc;
|
---|
1043 |
|
---|
1044 | if (RT_SUCCESS(vrc))
|
---|
1045 | {
|
---|
1046 | VBOXHGCMSVCPARM paParms[8];
|
---|
1047 | int i = 0;
|
---|
1048 | HGCMSvcSetU32(&paParms[i++], pEvent->ContextID());
|
---|
1049 | HGCMSvcSetU32(&paParms[i++], mData.mPID);
|
---|
1050 | HGCMSvcSetU32(&paParms[i++], uHandle);
|
---|
1051 | HGCMSvcSetU32(&paParms[i++], 0 /* Flags, none set yet. */);
|
---|
1052 |
|
---|
1053 | alock.release(); /* Drop the write lock before sending. */
|
---|
1054 |
|
---|
1055 | vrc = sendMessage(HOST_MSG_EXEC_GET_OUTPUT, i, paParms);
|
---|
1056 | }
|
---|
1057 |
|
---|
1058 | if (RT_SUCCESS(vrc))
|
---|
1059 | vrc = i_waitForOutput(pEvent, uHandle, uTimeoutMS,
|
---|
1060 | pvData, cbData, pcbRead);
|
---|
1061 |
|
---|
1062 | unregisterWaitEvent(pEvent);
|
---|
1063 |
|
---|
1064 | LogFlowFuncLeaveRC(vrc);
|
---|
1065 | return vrc;
|
---|
1066 | }
|
---|
1067 |
|
---|
1068 | /**
|
---|
1069 | * Sets (reports) the current (overall) status of the guest process.
|
---|
1070 | *
|
---|
1071 | * @returns VBox status code.
|
---|
1072 | * @param procStatus Guest process status to set.
|
---|
1073 | * @param procRc Guest process result code to set.
|
---|
1074 | *
|
---|
1075 | * @note Takes the write lock.
|
---|
1076 | */
|
---|
1077 | int GuestProcess::i_setProcessStatus(ProcessStatus_T procStatus, int procRc)
|
---|
1078 | {
|
---|
1079 | LogFlowThisFuncEnter();
|
---|
1080 |
|
---|
1081 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1082 |
|
---|
1083 | LogFlowThisFunc(("oldStatus=%RU32, newStatus=%RU32, procRc=%Rrc\n",
|
---|
1084 | mData.mStatus, procStatus, procRc));
|
---|
1085 |
|
---|
1086 | if (procStatus == ProcessStatus_Error)
|
---|
1087 | {
|
---|
1088 | AssertMsg(RT_FAILURE(procRc), ("Guest rc must be an error (%Rrc)\n", procRc));
|
---|
1089 | /* Do not allow overwriting an already set error. If this happens
|
---|
1090 | * this means we forgot some error checking/locking somewhere. */
|
---|
1091 | AssertMsg(RT_SUCCESS(mData.mLastError), ("Guest rc already set (to %Rrc)\n", mData.mLastError));
|
---|
1092 | }
|
---|
1093 | else
|
---|
1094 | AssertMsg(RT_SUCCESS(procRc), ("Guest rc must not be an error (%Rrc)\n", procRc));
|
---|
1095 |
|
---|
1096 | int vrc = VINF_SUCCESS;
|
---|
1097 |
|
---|
1098 | if (mData.mStatus != procStatus) /* Was there a process status change? */
|
---|
1099 | {
|
---|
1100 | mData.mStatus = procStatus;
|
---|
1101 | mData.mLastError = procRc;
|
---|
1102 |
|
---|
1103 | ComObjPtr<VirtualBoxErrorInfo> errorInfo;
|
---|
1104 | HRESULT hrc = errorInfo.createObject();
|
---|
1105 | ComAssertComRC(hrc);
|
---|
1106 | if (RT_FAILURE(mData.mLastError))
|
---|
1107 | {
|
---|
1108 | hrc = errorInfo->initEx(VBOX_E_IPRT_ERROR, mData.mLastError,
|
---|
1109 | COM_IIDOF(IGuestProcess), getComponentName(),
|
---|
1110 | i_guestErrorToString(mData.mLastError, mData.mProcess.mExecutable.c_str()));
|
---|
1111 | ComAssertComRC(hrc);
|
---|
1112 | }
|
---|
1113 |
|
---|
1114 | /* Copy over necessary data before releasing lock again. */
|
---|
1115 | uint32_t uPID = mData.mPID;
|
---|
1116 | /** @todo Also handle mSession? */
|
---|
1117 |
|
---|
1118 | alock.release(); /* Release lock before firing off event. */
|
---|
1119 |
|
---|
1120 | ::FireGuestProcessStateChangedEvent(mEventSource, mSession, this, uPID, procStatus, errorInfo);
|
---|
1121 | #if 0
|
---|
1122 | /*
|
---|
1123 | * On Guest Additions < 4.3 there is no guarantee that outstanding
|
---|
1124 | * requests will be delivered to the host after the process has ended,
|
---|
1125 | * so just cancel all waiting events here to not let clients run
|
---|
1126 | * into timeouts.
|
---|
1127 | */
|
---|
1128 | if ( mSession->getProtocolVersion() < 2
|
---|
1129 | && hasEnded())
|
---|
1130 | {
|
---|
1131 | LogFlowThisFunc(("Process ended, canceling outstanding wait events ...\n"));
|
---|
1132 | vrc = cancelWaitEvents();
|
---|
1133 | }
|
---|
1134 | #endif
|
---|
1135 | }
|
---|
1136 |
|
---|
1137 | return vrc;
|
---|
1138 | }
|
---|
1139 |
|
---|
1140 | /**
|
---|
1141 | * Starts the process on the guest.
|
---|
1142 | *
|
---|
1143 | * @returns VBox status code.
|
---|
1144 | * @param cMsTimeout Timeout (in ms) to wait for starting the process.
|
---|
1145 | * @param prcGuest Where to return the guest error when VERR_GSTCTL_GUEST_ERROR
|
---|
1146 | * was returned. Optional.
|
---|
1147 | *
|
---|
1148 | * @note Takes the write lock.
|
---|
1149 | */
|
---|
1150 | int GuestProcess::i_startProcess(uint32_t cMsTimeout, int *prcGuest)
|
---|
1151 | {
|
---|
1152 | LogFlowThisFunc(("cMsTimeout=%RU32, procExe=%s, procTimeoutMS=%RU32, procFlags=%x, sessionID=%RU32\n",
|
---|
1153 | cMsTimeout, mData.mProcess.mExecutable.c_str(), mData.mProcess.mTimeoutMS, mData.mProcess.mFlags,
|
---|
1154 | mSession->i_getId()));
|
---|
1155 |
|
---|
1156 | /* Wait until the caller function (if kicked off by a thread)
|
---|
1157 | * has returned and continue operation. */
|
---|
1158 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1159 |
|
---|
1160 | mData.mStatus = ProcessStatus_Starting;
|
---|
1161 |
|
---|
1162 | int vrc;
|
---|
1163 |
|
---|
1164 | GuestWaitEvent *pEvent = NULL;
|
---|
1165 | GuestEventTypes eventTypes;
|
---|
1166 | try
|
---|
1167 | {
|
---|
1168 | eventTypes.push_back(VBoxEventType_OnGuestProcessStateChanged);
|
---|
1169 | vrc = registerWaitEvent(eventTypes, &pEvent);
|
---|
1170 | }
|
---|
1171 | catch (std::bad_alloc &)
|
---|
1172 | {
|
---|
1173 | vrc = VERR_NO_MEMORY;
|
---|
1174 | }
|
---|
1175 | if (RT_FAILURE(vrc))
|
---|
1176 | return vrc;
|
---|
1177 |
|
---|
1178 | vrc = i_startProcessInner(cMsTimeout, alock, pEvent, prcGuest);
|
---|
1179 |
|
---|
1180 | unregisterWaitEvent(pEvent);
|
---|
1181 |
|
---|
1182 | LogFlowFuncLeaveRC(vrc);
|
---|
1183 | return vrc;
|
---|
1184 | }
|
---|
1185 |
|
---|
1186 | /**
|
---|
1187 | * Helper function to start a process on the guest. Do not call directly!
|
---|
1188 | *
|
---|
1189 | * @returns VBox status code.
|
---|
1190 | * @param cMsTimeout Timeout (in ms) to wait for starting the process.
|
---|
1191 | * @param rLock Write lock to use for serialization.
|
---|
1192 | * @param pEvent Event to use for notifying waiters.
|
---|
1193 | * @param prcGuest Where to return the guest error when VERR_GSTCTL_GUEST_ERROR
|
---|
1194 | * was returned. Optional.
|
---|
1195 | */
|
---|
1196 | int GuestProcess::i_startProcessInner(uint32_t cMsTimeout, AutoWriteLock &rLock, GuestWaitEvent *pEvent, int *prcGuest)
|
---|
1197 | {
|
---|
1198 | GuestSession *pSession = mSession;
|
---|
1199 | AssertPtr(pSession);
|
---|
1200 | uint32_t const uProtocol = pSession->i_getProtocolVersion();
|
---|
1201 |
|
---|
1202 | const GuestCredentials &sessionCreds = pSession->i_getCredentials();
|
---|
1203 |
|
---|
1204 | /* Prepare arguments. */
|
---|
1205 | size_t cArgs = mData.mProcess.mArguments.size();
|
---|
1206 | if (cArgs >= 128*1024)
|
---|
1207 | return VERR_BUFFER_OVERFLOW;
|
---|
1208 |
|
---|
1209 | size_t cbArgs = 0;
|
---|
1210 | char *pszArgs = NULL;
|
---|
1211 | int vrc = VINF_SUCCESS;
|
---|
1212 | if (cArgs)
|
---|
1213 | {
|
---|
1214 | char const **papszArgv = (char const **)RTMemAlloc((cArgs + 1) * sizeof(papszArgv[0]));
|
---|
1215 | AssertReturn(papszArgv, VERR_NO_MEMORY);
|
---|
1216 |
|
---|
1217 | for (size_t i = 0; i < cArgs; i++)
|
---|
1218 | {
|
---|
1219 | papszArgv[i] = mData.mProcess.mArguments[i].c_str();
|
---|
1220 | AssertPtr(papszArgv[i]);
|
---|
1221 | }
|
---|
1222 | papszArgv[cArgs] = NULL;
|
---|
1223 |
|
---|
1224 | Guest *pGuest = mSession->i_getParent();
|
---|
1225 | AssertPtr(pGuest);
|
---|
1226 |
|
---|
1227 | const uint64_t fGuestControlFeatures0 = pGuest->i_getGuestControlFeatures0();
|
---|
1228 |
|
---|
1229 | /* If the Guest Additions don't support using argv[0] correctly (< 6.1.x), don't supply it. */
|
---|
1230 | if (!(fGuestControlFeatures0 & VBOX_GUESTCTRL_GF_0_PROCESS_ARGV0))
|
---|
1231 | vrc = RTGetOptArgvToString(&pszArgs, papszArgv + 1, RTGETOPTARGV_CNV_QUOTE_BOURNE_SH);
|
---|
1232 | else /* ... else send the whole argv, including argv[0]. */
|
---|
1233 | vrc = RTGetOptArgvToString(&pszArgs, papszArgv, RTGETOPTARGV_CNV_QUOTE_BOURNE_SH);
|
---|
1234 |
|
---|
1235 | RTMemFree(papszArgv);
|
---|
1236 | if (RT_FAILURE(vrc))
|
---|
1237 | return vrc;
|
---|
1238 |
|
---|
1239 | /* Note! No direct returns after this. */
|
---|
1240 | }
|
---|
1241 |
|
---|
1242 | /* Calculate arguments size (in bytes). */
|
---|
1243 | AssertPtr(pszArgs);
|
---|
1244 | cbArgs = strlen(pszArgs) + 1; /* Include terminating zero. */
|
---|
1245 |
|
---|
1246 | /* Prepare environment. The guest service dislikes the empty string at the end, so drop it. */
|
---|
1247 | size_t cbEnvBlock = 0; /* Shut up MSVC. */
|
---|
1248 | char *pszzEnvBlock = NULL; /* Ditto. */
|
---|
1249 | vrc = mData.mProcess.mEnvironmentChanges.queryUtf8Block(&pszzEnvBlock, &cbEnvBlock);
|
---|
1250 | if (RT_SUCCESS(vrc))
|
---|
1251 | {
|
---|
1252 | Assert(cbEnvBlock > 0);
|
---|
1253 | cbEnvBlock--;
|
---|
1254 | AssertPtr(pszzEnvBlock);
|
---|
1255 |
|
---|
1256 | /* Prepare HGCM call. */
|
---|
1257 | VBOXHGCMSVCPARM paParms[16];
|
---|
1258 | int i = 0;
|
---|
1259 | HGCMSvcSetU32(&paParms[i++], pEvent->ContextID());
|
---|
1260 | HGCMSvcSetRTCStr(&paParms[i++], mData.mProcess.mExecutable);
|
---|
1261 | HGCMSvcSetU32(&paParms[i++], mData.mProcess.mFlags);
|
---|
1262 | HGCMSvcSetU32(&paParms[i++], (uint32_t)mData.mProcess.mArguments.size());
|
---|
1263 | HGCMSvcSetPv(&paParms[i++], pszArgs, (uint32_t)cbArgs);
|
---|
1264 | HGCMSvcSetU32(&paParms[i++], mData.mProcess.mEnvironmentChanges.count());
|
---|
1265 | HGCMSvcSetU32(&paParms[i++], (uint32_t)cbEnvBlock);
|
---|
1266 | HGCMSvcSetPv(&paParms[i++], pszzEnvBlock, (uint32_t)cbEnvBlock);
|
---|
1267 | if (uProtocol < 2)
|
---|
1268 | {
|
---|
1269 | /* In protocol v1 (VBox < 4.3) the credentials were part of the execution
|
---|
1270 | * call. In newer protocols these credentials are part of the opened guest
|
---|
1271 | * session, so not needed anymore here. */
|
---|
1272 | HGCMSvcSetRTCStr(&paParms[i++], sessionCreds.mUser);
|
---|
1273 | HGCMSvcSetRTCStr(&paParms[i++], sessionCreds.mPassword);
|
---|
1274 | }
|
---|
1275 | /*
|
---|
1276 | * If the WaitForProcessStartOnly flag is set, we only want to define and wait for a timeout
|
---|
1277 | * until the process was started - the process itself then gets an infinite timeout for execution.
|
---|
1278 | * This is handy when we want to start a process inside a worker thread within a certain timeout
|
---|
1279 | * but let the started process perform lengthly operations then.
|
---|
1280 | */
|
---|
1281 | if (mData.mProcess.mFlags & ProcessCreateFlag_WaitForProcessStartOnly)
|
---|
1282 | HGCMSvcSetU32(&paParms[i++], UINT32_MAX /* Infinite timeout */);
|
---|
1283 | else
|
---|
1284 | HGCMSvcSetU32(&paParms[i++], mData.mProcess.mTimeoutMS);
|
---|
1285 | if (uProtocol >= 2)
|
---|
1286 | {
|
---|
1287 | HGCMSvcSetU32(&paParms[i++], mData.mProcess.mPriority);
|
---|
1288 | /* CPU affinity: We only support one CPU affinity block at the moment,
|
---|
1289 | * so that makes up to 64 CPUs total. This can be more in the future. */
|
---|
1290 | HGCMSvcSetU32(&paParms[i++], 1);
|
---|
1291 | /* The actual CPU affinity blocks. */
|
---|
1292 | HGCMSvcSetPv(&paParms[i++], (void *)&mData.mProcess.mAffinity, sizeof(mData.mProcess.mAffinity));
|
---|
1293 | }
|
---|
1294 |
|
---|
1295 | rLock.release(); /* Drop the write lock before sending. */
|
---|
1296 |
|
---|
1297 | vrc = sendMessage(HOST_MSG_EXEC_CMD, i, paParms);
|
---|
1298 | if (RT_FAILURE(vrc))
|
---|
1299 | {
|
---|
1300 | int vrc2 = i_setProcessStatus(ProcessStatus_Error, vrc);
|
---|
1301 | AssertRC(vrc2);
|
---|
1302 | }
|
---|
1303 |
|
---|
1304 | mData.mProcess.mEnvironmentChanges.freeUtf8Block(pszzEnvBlock);
|
---|
1305 | }
|
---|
1306 |
|
---|
1307 | RTStrFree(pszArgs);
|
---|
1308 |
|
---|
1309 | if (RT_SUCCESS(vrc))
|
---|
1310 | vrc = i_waitForStatusChange(pEvent, cMsTimeout,
|
---|
1311 | NULL /* Process status */, prcGuest);
|
---|
1312 | return vrc;
|
---|
1313 | }
|
---|
1314 |
|
---|
1315 | /**
|
---|
1316 | * Starts the process asynchronously (via worker thread) on the guest.
|
---|
1317 | *
|
---|
1318 | * @returns VBox status code.
|
---|
1319 | */
|
---|
1320 | int GuestProcess::i_startProcessAsync(void)
|
---|
1321 | {
|
---|
1322 | LogFlowThisFuncEnter();
|
---|
1323 |
|
---|
1324 | /* Create the task: */
|
---|
1325 | GuestProcessStartTask *pTask = NULL;
|
---|
1326 | try
|
---|
1327 | {
|
---|
1328 | pTask = new GuestProcessStartTask(this);
|
---|
1329 | }
|
---|
1330 | catch (std::bad_alloc &)
|
---|
1331 | {
|
---|
1332 | LogFlowThisFunc(("out of memory\n"));
|
---|
1333 | return VERR_NO_MEMORY;
|
---|
1334 | }
|
---|
1335 | AssertReturnStmt(pTask->i_isOk(), delete pTask, E_FAIL); /* cannot fail for GuestProcessStartTask. */
|
---|
1336 | LogFlowThisFunc(("Successfully created GuestProcessStartTask object\n"));
|
---|
1337 |
|
---|
1338 | /* Start the thread (always consumes the task): */
|
---|
1339 | HRESULT hrc = pTask->createThread();
|
---|
1340 | pTask = NULL;
|
---|
1341 | if (SUCCEEDED(hrc))
|
---|
1342 | return VINF_SUCCESS;
|
---|
1343 | LogFlowThisFunc(("Failed to create thread for GuestProcessStartTask\n"));
|
---|
1344 | return VERR_GENERAL_FAILURE;
|
---|
1345 | }
|
---|
1346 |
|
---|
1347 | /**
|
---|
1348 | * Thread task which does the asynchronous starting of a guest process.
|
---|
1349 | *
|
---|
1350 | * @returns VBox status code.
|
---|
1351 | * @param pTask Process start task (context) to process.
|
---|
1352 | */
|
---|
1353 | /* static */
|
---|
1354 | int GuestProcess::i_startProcessThreadTask(GuestProcessStartTask *pTask)
|
---|
1355 | {
|
---|
1356 | LogFlowFunc(("pTask=%p\n", pTask));
|
---|
1357 |
|
---|
1358 | const ComObjPtr<GuestProcess> pProcess(pTask->i_process());
|
---|
1359 | Assert(!pProcess.isNull());
|
---|
1360 |
|
---|
1361 | AutoCaller autoCaller(pProcess);
|
---|
1362 | if (FAILED(autoCaller.rc()))
|
---|
1363 | return VERR_COM_UNEXPECTED;
|
---|
1364 |
|
---|
1365 | int vrc = pProcess->i_startProcess(30 * 1000 /* 30s timeout */, NULL /* Guest rc, ignored */);
|
---|
1366 | /* Nothing to do here anymore. */
|
---|
1367 |
|
---|
1368 | LogFlowFunc(("pProcess=%p, vrc=%Rrc\n", (GuestProcess *)pProcess, vrc));
|
---|
1369 | return vrc;
|
---|
1370 | }
|
---|
1371 |
|
---|
1372 | /**
|
---|
1373 | * Terminates a guest process.
|
---|
1374 | *
|
---|
1375 | * @returns VBox status code.
|
---|
1376 | * @retval VWRN_INVALID_STATE if process not in running state (anymore).
|
---|
1377 | * @retval VERR_NOT_SUPPORTED if process termination is not supported on the guest.
|
---|
1378 | * @param uTimeoutMS Timeout (in ms) to wait for process termination.
|
---|
1379 | * @param prcGuest Where to return the guest error when VERR_GSTCTL_GUEST_ERROR
|
---|
1380 | * was returned. Optional.
|
---|
1381 | *
|
---|
1382 | * @note Takes the write lock.
|
---|
1383 | */
|
---|
1384 | int GuestProcess::i_terminateProcess(uint32_t uTimeoutMS, int *prcGuest)
|
---|
1385 | {
|
---|
1386 | /* prcGuest is optional. */
|
---|
1387 | LogFlowThisFunc(("uTimeoutMS=%RU32\n", uTimeoutMS));
|
---|
1388 |
|
---|
1389 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1390 |
|
---|
1391 | int vrc = VINF_SUCCESS;
|
---|
1392 |
|
---|
1393 | if (mData.mStatus != ProcessStatus_Started)
|
---|
1394 | {
|
---|
1395 | LogFlowThisFunc(("Process not in started state (state is %RU32), skipping termination\n",
|
---|
1396 | mData.mStatus));
|
---|
1397 | vrc = VWRN_INVALID_STATE;
|
---|
1398 | }
|
---|
1399 | else
|
---|
1400 | {
|
---|
1401 | AssertPtr(mSession);
|
---|
1402 | /* Note: VBox < 4.3 (aka protocol version 1) does not
|
---|
1403 | * support this, so just skip. */
|
---|
1404 | if (mSession->i_getProtocolVersion() < 2)
|
---|
1405 | vrc = VERR_NOT_SUPPORTED;
|
---|
1406 |
|
---|
1407 | if (RT_SUCCESS(vrc))
|
---|
1408 | {
|
---|
1409 | GuestWaitEvent *pEvent = NULL;
|
---|
1410 | GuestEventTypes eventTypes;
|
---|
1411 | try
|
---|
1412 | {
|
---|
1413 | eventTypes.push_back(VBoxEventType_OnGuestProcessStateChanged);
|
---|
1414 |
|
---|
1415 | vrc = registerWaitEvent(eventTypes, &pEvent);
|
---|
1416 | }
|
---|
1417 | catch (std::bad_alloc &)
|
---|
1418 | {
|
---|
1419 | vrc = VERR_NO_MEMORY;
|
---|
1420 | }
|
---|
1421 |
|
---|
1422 | if (RT_FAILURE(vrc))
|
---|
1423 | return vrc;
|
---|
1424 |
|
---|
1425 | VBOXHGCMSVCPARM paParms[4];
|
---|
1426 | int i = 0;
|
---|
1427 | HGCMSvcSetU32(&paParms[i++], pEvent->ContextID());
|
---|
1428 | HGCMSvcSetU32(&paParms[i++], mData.mPID);
|
---|
1429 |
|
---|
1430 | alock.release(); /* Drop the write lock before sending. */
|
---|
1431 |
|
---|
1432 | vrc = sendMessage(HOST_MSG_EXEC_TERMINATE, i, paParms);
|
---|
1433 | if (RT_SUCCESS(vrc))
|
---|
1434 | vrc = i_waitForStatusChange(pEvent, uTimeoutMS,
|
---|
1435 | NULL /* ProcessStatus */, prcGuest);
|
---|
1436 | unregisterWaitEvent(pEvent);
|
---|
1437 | }
|
---|
1438 | }
|
---|
1439 |
|
---|
1440 | LogFlowFuncLeaveRC(vrc);
|
---|
1441 | return vrc;
|
---|
1442 | }
|
---|
1443 |
|
---|
1444 | /**
|
---|
1445 | * Converts given process status / flags and wait flag combination
|
---|
1446 | * to an overall process wait result.
|
---|
1447 | *
|
---|
1448 | * @returns Overall process wait result.
|
---|
1449 | * @param fWaitFlags Process wait flags to use for conversion.
|
---|
1450 | * @param oldStatus Old process status to use for conversion.
|
---|
1451 | * @param newStatus New process status to use for conversion.
|
---|
1452 | * @param uProcFlags Process flags to use for conversion.
|
---|
1453 | * @param uProtocol Guest Control protocol version to use for conversion.
|
---|
1454 | */
|
---|
1455 | /* static */
|
---|
1456 | ProcessWaitResult_T GuestProcess::i_waitFlagsToResultEx(uint32_t fWaitFlags,
|
---|
1457 | ProcessStatus_T oldStatus, ProcessStatus_T newStatus,
|
---|
1458 | uint32_t uProcFlags, uint32_t uProtocol)
|
---|
1459 | {
|
---|
1460 | ProcessWaitResult_T waitResult = ProcessWaitResult_None;
|
---|
1461 |
|
---|
1462 | switch (newStatus)
|
---|
1463 | {
|
---|
1464 | case ProcessStatus_TerminatedNormally:
|
---|
1465 | case ProcessStatus_TerminatedSignal:
|
---|
1466 | case ProcessStatus_TerminatedAbnormally:
|
---|
1467 | case ProcessStatus_Down:
|
---|
1468 | /* Nothing to wait for anymore. */
|
---|
1469 | waitResult = ProcessWaitResult_Terminate;
|
---|
1470 | break;
|
---|
1471 |
|
---|
1472 | case ProcessStatus_TimedOutKilled:
|
---|
1473 | case ProcessStatus_TimedOutAbnormally:
|
---|
1474 | /* Dito. */
|
---|
1475 | waitResult = ProcessWaitResult_Timeout;
|
---|
1476 | break;
|
---|
1477 |
|
---|
1478 | case ProcessStatus_Started:
|
---|
1479 | switch (oldStatus)
|
---|
1480 | {
|
---|
1481 | case ProcessStatus_Undefined:
|
---|
1482 | case ProcessStatus_Starting:
|
---|
1483 | /* Also wait for process start. */
|
---|
1484 | if (fWaitFlags & ProcessWaitForFlag_Start)
|
---|
1485 | waitResult = ProcessWaitResult_Start;
|
---|
1486 | else
|
---|
1487 | {
|
---|
1488 | /*
|
---|
1489 | * If ProcessCreateFlag_WaitForProcessStartOnly was specified on process creation the
|
---|
1490 | * caller is not interested in getting further process statuses -- so just don't notify
|
---|
1491 | * anything here anymore and return.
|
---|
1492 | */
|
---|
1493 | if (uProcFlags & ProcessCreateFlag_WaitForProcessStartOnly)
|
---|
1494 | waitResult = ProcessWaitResult_Start;
|
---|
1495 | }
|
---|
1496 | break;
|
---|
1497 |
|
---|
1498 | case ProcessStatus_Started:
|
---|
1499 | /* Only wait for process start. */
|
---|
1500 | if (fWaitFlags & ProcessWaitForFlag_Start)
|
---|
1501 | waitResult = ProcessWaitResult_Start;
|
---|
1502 | break;
|
---|
1503 |
|
---|
1504 | default:
|
---|
1505 | AssertMsgFailed(("Unhandled old status %RU32 before new status 'started'\n",
|
---|
1506 | oldStatus));
|
---|
1507 | if (fWaitFlags & ProcessWaitForFlag_Start)
|
---|
1508 | waitResult = ProcessWaitResult_Start;
|
---|
1509 | break;
|
---|
1510 | }
|
---|
1511 | break;
|
---|
1512 |
|
---|
1513 | case ProcessStatus_Error:
|
---|
1514 | /* Nothing to wait for anymore. */
|
---|
1515 | waitResult = ProcessWaitResult_Error;
|
---|
1516 | break;
|
---|
1517 |
|
---|
1518 | case ProcessStatus_Undefined:
|
---|
1519 | case ProcessStatus_Starting:
|
---|
1520 | case ProcessStatus_Terminating:
|
---|
1521 | case ProcessStatus_Paused:
|
---|
1522 | /* No result available yet, leave wait
|
---|
1523 | * flags untouched. */
|
---|
1524 | break;
|
---|
1525 | #ifdef VBOX_WITH_XPCOM_CPP_ENUM_HACK
|
---|
1526 | case ProcessStatus_32BitHack: AssertFailedBreak(); /* (compiler warnings) */
|
---|
1527 | #endif
|
---|
1528 | }
|
---|
1529 |
|
---|
1530 | if (newStatus == ProcessStatus_Started)
|
---|
1531 | {
|
---|
1532 | /*
|
---|
1533 | * Filter out waits which are *not* supported using
|
---|
1534 | * older guest control Guest Additions.
|
---|
1535 | *
|
---|
1536 | */
|
---|
1537 | /** @todo ProcessWaitForFlag_Std* flags are not implemented yet. */
|
---|
1538 | if (uProtocol < 99) /* See @todo above. */
|
---|
1539 | {
|
---|
1540 | if ( waitResult == ProcessWaitResult_None
|
---|
1541 | /* We don't support waiting for stdin, out + err,
|
---|
1542 | * just skip waiting then. */
|
---|
1543 | && ( (fWaitFlags & ProcessWaitForFlag_StdIn)
|
---|
1544 | || (fWaitFlags & ProcessWaitForFlag_StdOut)
|
---|
1545 | || (fWaitFlags & ProcessWaitForFlag_StdErr)
|
---|
1546 | )
|
---|
1547 | )
|
---|
1548 | {
|
---|
1549 | /* Use _WaitFlagNotSupported because we don't know what to tell the caller. */
|
---|
1550 | waitResult = ProcessWaitResult_WaitFlagNotSupported;
|
---|
1551 | }
|
---|
1552 | }
|
---|
1553 | }
|
---|
1554 |
|
---|
1555 | #ifdef DEBUG
|
---|
1556 | LogFlowFunc(("oldStatus=%RU32, newStatus=%RU32, fWaitFlags=0x%x, waitResult=%RU32\n",
|
---|
1557 | oldStatus, newStatus, fWaitFlags, waitResult));
|
---|
1558 | #endif
|
---|
1559 | return waitResult;
|
---|
1560 | }
|
---|
1561 |
|
---|
1562 | /**
|
---|
1563 | * Converts given wait flags to an overall process wait result.
|
---|
1564 | *
|
---|
1565 | * @returns Overall process wait result.
|
---|
1566 | * @param fWaitFlags Process wait flags to use for conversion.
|
---|
1567 | */
|
---|
1568 | ProcessWaitResult_T GuestProcess::i_waitFlagsToResult(uint32_t fWaitFlags)
|
---|
1569 | {
|
---|
1570 | AssertPtr(mSession);
|
---|
1571 | return GuestProcess::i_waitFlagsToResultEx(fWaitFlags,
|
---|
1572 | mData.mStatus /* oldStatus */, mData.mStatus /* newStatus */,
|
---|
1573 | mData.mProcess.mFlags, mSession->i_getProtocolVersion());
|
---|
1574 | }
|
---|
1575 |
|
---|
1576 | /**
|
---|
1577 | * Waits for certain events of the guest process.
|
---|
1578 | *
|
---|
1579 | * @returns VBox status code.
|
---|
1580 | * @param fWaitFlags Process wait flags to wait for.
|
---|
1581 | * @param uTimeoutMS Timeout (in ms) to wait.
|
---|
1582 | * @param waitResult Where to return the process wait result on success.
|
---|
1583 | * @param prcGuest Where to return the guest error when VERR_GSTCTL_GUEST_ERROR
|
---|
1584 | * was returned. Optional.
|
---|
1585 | * @note Takes the read lock.
|
---|
1586 | */
|
---|
1587 | int GuestProcess::i_waitFor(uint32_t fWaitFlags, ULONG uTimeoutMS,
|
---|
1588 | ProcessWaitResult_T &waitResult, int *prcGuest)
|
---|
1589 | {
|
---|
1590 | AssertReturn(fWaitFlags, VERR_INVALID_PARAMETER);
|
---|
1591 |
|
---|
1592 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1593 |
|
---|
1594 | LogFlowThisFunc(("fWaitFlags=0x%x, uTimeoutMS=%RU32, procStatus=%RU32, procRc=%Rrc, prcGuest=%p\n",
|
---|
1595 | fWaitFlags, uTimeoutMS, mData.mStatus, mData.mLastError, prcGuest));
|
---|
1596 |
|
---|
1597 | /* Did some error occur before? Then skip waiting and return. */
|
---|
1598 | ProcessStatus_T curStatus = mData.mStatus;
|
---|
1599 | if (curStatus == ProcessStatus_Error)
|
---|
1600 | {
|
---|
1601 | waitResult = ProcessWaitResult_Error;
|
---|
1602 | AssertMsg(RT_FAILURE(mData.mLastError),
|
---|
1603 | ("No error rc (%Rrc) set when guest process indicated an error\n", mData.mLastError));
|
---|
1604 | if (prcGuest)
|
---|
1605 | *prcGuest = mData.mLastError; /* Return last set error. */
|
---|
1606 | LogFlowThisFunc(("Process is in error state (rcGuest=%Rrc)\n", mData.mLastError));
|
---|
1607 | return VERR_GSTCTL_GUEST_ERROR;
|
---|
1608 | }
|
---|
1609 |
|
---|
1610 | waitResult = i_waitFlagsToResult(fWaitFlags);
|
---|
1611 |
|
---|
1612 | /* No waiting needed? Return immediately using the last set error. */
|
---|
1613 | if (waitResult != ProcessWaitResult_None)
|
---|
1614 | {
|
---|
1615 | if (prcGuest)
|
---|
1616 | *prcGuest = mData.mLastError; /* Return last set error (if any). */
|
---|
1617 | LogFlowThisFunc(("Nothing to wait for (rcGuest=%Rrc)\n", mData.mLastError));
|
---|
1618 | return RT_SUCCESS(mData.mLastError) ? VINF_SUCCESS : VERR_GSTCTL_GUEST_ERROR;
|
---|
1619 | }
|
---|
1620 |
|
---|
1621 | /* Adjust timeout. Passing 0 means RT_INDEFINITE_WAIT. */
|
---|
1622 | if (!uTimeoutMS)
|
---|
1623 | uTimeoutMS = RT_INDEFINITE_WAIT;
|
---|
1624 |
|
---|
1625 | int vrc;
|
---|
1626 |
|
---|
1627 | GuestWaitEvent *pEvent = NULL;
|
---|
1628 | GuestEventTypes eventTypes;
|
---|
1629 | try
|
---|
1630 | {
|
---|
1631 | eventTypes.push_back(VBoxEventType_OnGuestProcessStateChanged);
|
---|
1632 |
|
---|
1633 | vrc = registerWaitEvent(eventTypes, &pEvent);
|
---|
1634 | }
|
---|
1635 | catch (std::bad_alloc &)
|
---|
1636 | {
|
---|
1637 | vrc = VERR_NO_MEMORY;
|
---|
1638 | }
|
---|
1639 |
|
---|
1640 | if (RT_FAILURE(vrc))
|
---|
1641 | return vrc;
|
---|
1642 |
|
---|
1643 | alock.release(); /* Release lock before waiting. */
|
---|
1644 |
|
---|
1645 | /*
|
---|
1646 | * Do the actual waiting.
|
---|
1647 | */
|
---|
1648 | ProcessStatus_T newStatus = ProcessStatus_Undefined;
|
---|
1649 | uint64_t u64StartMS = RTTimeMilliTS();
|
---|
1650 | for (;;)
|
---|
1651 | {
|
---|
1652 | uint64_t u64ElapsedMS = RTTimeMilliTS() - u64StartMS;
|
---|
1653 | if ( uTimeoutMS != RT_INDEFINITE_WAIT
|
---|
1654 | && u64ElapsedMS >= uTimeoutMS)
|
---|
1655 | {
|
---|
1656 | vrc = VERR_TIMEOUT;
|
---|
1657 | break;
|
---|
1658 | }
|
---|
1659 |
|
---|
1660 | vrc = i_waitForStatusChange(pEvent,
|
---|
1661 | uTimeoutMS == RT_INDEFINITE_WAIT
|
---|
1662 | ? RT_INDEFINITE_WAIT : uTimeoutMS - (uint32_t)u64ElapsedMS,
|
---|
1663 | &newStatus, prcGuest);
|
---|
1664 | if (RT_SUCCESS(vrc))
|
---|
1665 | {
|
---|
1666 | alock.acquire();
|
---|
1667 |
|
---|
1668 | waitResult = i_waitFlagsToResultEx(fWaitFlags, curStatus, newStatus,
|
---|
1669 | mData.mProcess.mFlags, mSession->i_getProtocolVersion());
|
---|
1670 | #ifdef DEBUG
|
---|
1671 | LogFlowThisFunc(("Got new status change: fWaitFlags=0x%x, newStatus=%RU32, waitResult=%RU32\n",
|
---|
1672 | fWaitFlags, newStatus, waitResult));
|
---|
1673 | #endif
|
---|
1674 | if (ProcessWaitResult_None != waitResult) /* We got a waiting result. */
|
---|
1675 | break;
|
---|
1676 | }
|
---|
1677 | else /* Waiting failed, bail out. */
|
---|
1678 | break;
|
---|
1679 |
|
---|
1680 | alock.release(); /* Don't hold lock in next waiting round. */
|
---|
1681 | }
|
---|
1682 |
|
---|
1683 | unregisterWaitEvent(pEvent);
|
---|
1684 |
|
---|
1685 | LogFlowThisFunc(("Returned waitResult=%RU32, newStatus=%RU32, rc=%Rrc\n",
|
---|
1686 | waitResult, newStatus, vrc));
|
---|
1687 | return vrc;
|
---|
1688 | }
|
---|
1689 |
|
---|
1690 | /**
|
---|
1691 | * Waits for a guest process input notification.
|
---|
1692 | *
|
---|
1693 | * @param pEvent Wait event to use for waiting.
|
---|
1694 | * @param uHandle Guest process file handle to wait for.
|
---|
1695 | * @param uTimeoutMS Timeout (in ms) to wait.
|
---|
1696 | * @param pInputStatus Where to return the process input status on success.
|
---|
1697 | * @param pcbProcessed Where to return the processed input (in bytes) on success.
|
---|
1698 | */
|
---|
1699 | int GuestProcess::i_waitForInputNotify(GuestWaitEvent *pEvent, uint32_t uHandle, uint32_t uTimeoutMS,
|
---|
1700 | ProcessInputStatus_T *pInputStatus, uint32_t *pcbProcessed)
|
---|
1701 | {
|
---|
1702 | RT_NOREF(uHandle);
|
---|
1703 | AssertPtrReturn(pEvent, VERR_INVALID_POINTER);
|
---|
1704 |
|
---|
1705 | VBoxEventType_T evtType;
|
---|
1706 | ComPtr<IEvent> pIEvent;
|
---|
1707 | int vrc = waitForEvent(pEvent, uTimeoutMS,
|
---|
1708 | &evtType, pIEvent.asOutParam());
|
---|
1709 | if (RT_SUCCESS(vrc))
|
---|
1710 | {
|
---|
1711 | if (evtType == VBoxEventType_OnGuestProcessInputNotify)
|
---|
1712 | {
|
---|
1713 | ComPtr<IGuestProcessInputNotifyEvent> pProcessEvent = pIEvent;
|
---|
1714 | Assert(!pProcessEvent.isNull());
|
---|
1715 |
|
---|
1716 | if (pInputStatus)
|
---|
1717 | {
|
---|
1718 | HRESULT hr2 = pProcessEvent->COMGETTER(Status)(pInputStatus);
|
---|
1719 | ComAssertComRC(hr2);
|
---|
1720 | }
|
---|
1721 | if (pcbProcessed)
|
---|
1722 | {
|
---|
1723 | HRESULT hr2 = pProcessEvent->COMGETTER(Processed)((ULONG*)pcbProcessed);
|
---|
1724 | ComAssertComRC(hr2);
|
---|
1725 | }
|
---|
1726 | }
|
---|
1727 | else
|
---|
1728 | vrc = VWRN_GSTCTL_OBJECTSTATE_CHANGED;
|
---|
1729 | }
|
---|
1730 |
|
---|
1731 | LogFlowThisFunc(("Returning pEvent=%p, uHandle=%RU32, rc=%Rrc\n",
|
---|
1732 | pEvent, uHandle, vrc));
|
---|
1733 | return vrc;
|
---|
1734 | }
|
---|
1735 |
|
---|
1736 | /**
|
---|
1737 | * Waits for a guest process input notification.
|
---|
1738 | *
|
---|
1739 | * @returns VBox status code.
|
---|
1740 | * @param pEvent Wait event to use for waiting.
|
---|
1741 | * @param uHandle Guest process file handle to wait for.
|
---|
1742 | * @param uTimeoutMS Timeout (in ms) to wait.
|
---|
1743 | * @param pvData Where to store the guest process output on success.
|
---|
1744 | * @param cbData Size (in bytes) of \a pvData.
|
---|
1745 | * @param pcbRead Where to return the size (in bytes) read.
|
---|
1746 | */
|
---|
1747 | int GuestProcess::i_waitForOutput(GuestWaitEvent *pEvent, uint32_t uHandle, uint32_t uTimeoutMS,
|
---|
1748 | void *pvData, size_t cbData, uint32_t *pcbRead)
|
---|
1749 | {
|
---|
1750 | AssertPtrReturn(pEvent, VERR_INVALID_POINTER);
|
---|
1751 | /* pvData is optional. */
|
---|
1752 | /* cbData is optional. */
|
---|
1753 | /* pcbRead is optional. */
|
---|
1754 |
|
---|
1755 | LogFlowThisFunc(("cEventTypes=%zu, pEvent=%p, uHandle=%RU32, uTimeoutMS=%RU32, pvData=%p, cbData=%zu, pcbRead=%p\n",
|
---|
1756 | pEvent->TypeCount(), pEvent, uHandle, uTimeoutMS, pvData, cbData, pcbRead));
|
---|
1757 |
|
---|
1758 | int vrc;
|
---|
1759 |
|
---|
1760 | VBoxEventType_T evtType;
|
---|
1761 | ComPtr<IEvent> pIEvent;
|
---|
1762 | do
|
---|
1763 | {
|
---|
1764 | vrc = waitForEvent(pEvent, uTimeoutMS,
|
---|
1765 | &evtType, pIEvent.asOutParam());
|
---|
1766 | if (RT_SUCCESS(vrc))
|
---|
1767 | {
|
---|
1768 | if (evtType == VBoxEventType_OnGuestProcessOutput)
|
---|
1769 | {
|
---|
1770 | ComPtr<IGuestProcessOutputEvent> pProcessEvent = pIEvent;
|
---|
1771 | Assert(!pProcessEvent.isNull());
|
---|
1772 |
|
---|
1773 | ULONG uHandleEvent;
|
---|
1774 | HRESULT hr = pProcessEvent->COMGETTER(Handle)(&uHandleEvent);
|
---|
1775 | if ( SUCCEEDED(hr)
|
---|
1776 | && uHandleEvent == uHandle)
|
---|
1777 | {
|
---|
1778 | if (pvData)
|
---|
1779 | {
|
---|
1780 | com::SafeArray <BYTE> data;
|
---|
1781 | hr = pProcessEvent->COMGETTER(Data)(ComSafeArrayAsOutParam(data));
|
---|
1782 | ComAssertComRC(hr);
|
---|
1783 | size_t cbRead = data.size();
|
---|
1784 | if (cbRead)
|
---|
1785 | {
|
---|
1786 | if (cbRead <= cbData)
|
---|
1787 | {
|
---|
1788 | /* Copy data from event into our buffer. */
|
---|
1789 | memcpy(pvData, data.raw(), data.size());
|
---|
1790 | }
|
---|
1791 | else
|
---|
1792 | vrc = VERR_BUFFER_OVERFLOW;
|
---|
1793 |
|
---|
1794 | LogFlowThisFunc(("Read %zu bytes (uHandle=%RU32), rc=%Rrc\n",
|
---|
1795 | cbRead, uHandleEvent, vrc));
|
---|
1796 | }
|
---|
1797 | }
|
---|
1798 |
|
---|
1799 | if ( RT_SUCCESS(vrc)
|
---|
1800 | && pcbRead)
|
---|
1801 | {
|
---|
1802 | ULONG cbRead;
|
---|
1803 | hr = pProcessEvent->COMGETTER(Processed)(&cbRead);
|
---|
1804 | ComAssertComRC(hr);
|
---|
1805 | *pcbRead = (uint32_t)cbRead;
|
---|
1806 | }
|
---|
1807 |
|
---|
1808 | break;
|
---|
1809 | }
|
---|
1810 | else if (FAILED(hr))
|
---|
1811 | vrc = VERR_COM_UNEXPECTED;
|
---|
1812 | }
|
---|
1813 | else
|
---|
1814 | vrc = VWRN_GSTCTL_OBJECTSTATE_CHANGED;
|
---|
1815 | }
|
---|
1816 |
|
---|
1817 | } while (vrc == VINF_SUCCESS);
|
---|
1818 |
|
---|
1819 | if ( vrc != VINF_SUCCESS
|
---|
1820 | && pcbRead)
|
---|
1821 | {
|
---|
1822 | *pcbRead = 0;
|
---|
1823 | }
|
---|
1824 |
|
---|
1825 | LogFlowFuncLeaveRC(vrc);
|
---|
1826 | return vrc;
|
---|
1827 | }
|
---|
1828 |
|
---|
1829 | /**
|
---|
1830 | * Waits for a guest process status change.
|
---|
1831 | *
|
---|
1832 | * @returns VBox status code.
|
---|
1833 | * @retval VERR_GSTCTL_GUEST_ERROR when an error from the guest side has been received.
|
---|
1834 | * @param pEvent Guest wait event to wait for.
|
---|
1835 | * @param uTimeoutMS Timeout (in ms) to wait.
|
---|
1836 | * @param pProcessStatus Where to return the process status on success.
|
---|
1837 | * @param prcGuest Where to return the guest error when VERR_GSTCTL_GUEST_ERROR
|
---|
1838 | * was returned.
|
---|
1839 | */
|
---|
1840 | int GuestProcess::i_waitForStatusChange(GuestWaitEvent *pEvent, uint32_t uTimeoutMS,
|
---|
1841 | ProcessStatus_T *pProcessStatus, int *prcGuest)
|
---|
1842 | {
|
---|
1843 | AssertPtrReturn(pEvent, VERR_INVALID_POINTER);
|
---|
1844 | /* pProcessStatus is optional. */
|
---|
1845 | /* prcGuest is optional. */
|
---|
1846 |
|
---|
1847 | VBoxEventType_T evtType;
|
---|
1848 | ComPtr<IEvent> pIEvent;
|
---|
1849 | int vrc = waitForEvent(pEvent, uTimeoutMS,
|
---|
1850 | &evtType, pIEvent.asOutParam());
|
---|
1851 | if (RT_SUCCESS(vrc))
|
---|
1852 | {
|
---|
1853 | Assert(evtType == VBoxEventType_OnGuestProcessStateChanged);
|
---|
1854 | ComPtr<IGuestProcessStateChangedEvent> pProcessEvent = pIEvent;
|
---|
1855 | Assert(!pProcessEvent.isNull());
|
---|
1856 |
|
---|
1857 | ProcessStatus_T procStatus;
|
---|
1858 | HRESULT hr = pProcessEvent->COMGETTER(Status)(&procStatus);
|
---|
1859 | ComAssertComRC(hr);
|
---|
1860 | if (pProcessStatus)
|
---|
1861 | *pProcessStatus = procStatus;
|
---|
1862 |
|
---|
1863 | ComPtr<IVirtualBoxErrorInfo> errorInfo;
|
---|
1864 | hr = pProcessEvent->COMGETTER(Error)(errorInfo.asOutParam());
|
---|
1865 | ComAssertComRC(hr);
|
---|
1866 |
|
---|
1867 | LONG lGuestRc;
|
---|
1868 | hr = errorInfo->COMGETTER(ResultDetail)(&lGuestRc);
|
---|
1869 | ComAssertComRC(hr);
|
---|
1870 |
|
---|
1871 | LogFlowThisFunc(("Got procStatus=%RU32, rcGuest=%RI32 (%Rrc)\n",
|
---|
1872 | procStatus, lGuestRc, lGuestRc));
|
---|
1873 |
|
---|
1874 | if (RT_FAILURE((int)lGuestRc))
|
---|
1875 | vrc = VERR_GSTCTL_GUEST_ERROR;
|
---|
1876 |
|
---|
1877 | if (prcGuest)
|
---|
1878 | *prcGuest = (int)lGuestRc;
|
---|
1879 | }
|
---|
1880 | /* waitForEvent may also return VERR_GSTCTL_GUEST_ERROR like we do above, so make prcGuest is set. */
|
---|
1881 | else if (vrc == VERR_GSTCTL_GUEST_ERROR && prcGuest)
|
---|
1882 | *prcGuest = pEvent->GuestResult();
|
---|
1883 | Assert(vrc != VERR_GSTCTL_GUEST_ERROR || !prcGuest || *prcGuest != (int)0xcccccccc);
|
---|
1884 |
|
---|
1885 | LogFlowFuncLeaveRC(vrc);
|
---|
1886 | return vrc;
|
---|
1887 | }
|
---|
1888 |
|
---|
1889 | #if 0 /* Unused */
|
---|
1890 | /* static */
|
---|
1891 | bool GuestProcess::i_waitResultImpliesEx(ProcessWaitResult_T waitResult, ProcessStatus_T procStatus, uint32_t uProtocol)
|
---|
1892 | {
|
---|
1893 | RT_NOREF(uProtocol);
|
---|
1894 |
|
---|
1895 | bool fImplies;
|
---|
1896 |
|
---|
1897 | switch (waitResult)
|
---|
1898 | {
|
---|
1899 | case ProcessWaitResult_Start:
|
---|
1900 | fImplies = procStatus == ProcessStatus_Started;
|
---|
1901 | break;
|
---|
1902 |
|
---|
1903 | case ProcessWaitResult_Terminate:
|
---|
1904 | fImplies = ( procStatus == ProcessStatus_TerminatedNormally
|
---|
1905 | || procStatus == ProcessStatus_TerminatedSignal
|
---|
1906 | || procStatus == ProcessStatus_TerminatedAbnormally
|
---|
1907 | || procStatus == ProcessStatus_TimedOutKilled
|
---|
1908 | || procStatus == ProcessStatus_TimedOutAbnormally
|
---|
1909 | || procStatus == ProcessStatus_Down
|
---|
1910 | || procStatus == ProcessStatus_Error);
|
---|
1911 | break;
|
---|
1912 |
|
---|
1913 | default:
|
---|
1914 | fImplies = false;
|
---|
1915 | break;
|
---|
1916 | }
|
---|
1917 |
|
---|
1918 | return fImplies;
|
---|
1919 | }
|
---|
1920 | #endif /* unused */
|
---|
1921 |
|
---|
1922 | /**
|
---|
1923 | * Writes input data to a guest process.
|
---|
1924 | *
|
---|
1925 | * @returns VBox status code.
|
---|
1926 | * @retval VERR_GSTCTL_GUEST_ERROR when an error from the guest side has been received.
|
---|
1927 | * @param uHandle Guest process file handle to write to.
|
---|
1928 | * @param uFlags Input flags of type PRocessInputFlag_XXX.
|
---|
1929 | * @param pvData Data to write to the guest process.
|
---|
1930 | * @param cbData Size (in bytes) of \a pvData to write.
|
---|
1931 | * @param uTimeoutMS Timeout (in ms) to wait.
|
---|
1932 | * @param puWritten Where to return the size (in bytes) written. Optional.
|
---|
1933 | * @param prcGuest Where to return the guest error when VERR_GSTCTL_GUEST_ERROR
|
---|
1934 | * was returned. Optional.
|
---|
1935 | *
|
---|
1936 | * @note Takes the write lock.
|
---|
1937 | */
|
---|
1938 | int GuestProcess::i_writeData(uint32_t uHandle, uint32_t uFlags,
|
---|
1939 | void *pvData, size_t cbData, uint32_t uTimeoutMS, uint32_t *puWritten, int *prcGuest)
|
---|
1940 | {
|
---|
1941 | LogFlowThisFunc(("uPID=%RU32, uHandle=%RU32, uFlags=%RU32, pvData=%p, cbData=%RU32, uTimeoutMS=%RU32, puWritten=%p, prcGuest=%p\n",
|
---|
1942 | mData.mPID, uHandle, uFlags, pvData, cbData, uTimeoutMS, puWritten, prcGuest));
|
---|
1943 | /* All is optional. There can be 0 byte writes. */
|
---|
1944 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1945 |
|
---|
1946 | if (mData.mStatus != ProcessStatus_Started)
|
---|
1947 | {
|
---|
1948 | if (puWritten)
|
---|
1949 | *puWritten = 0;
|
---|
1950 | if (prcGuest)
|
---|
1951 | *prcGuest = VINF_SUCCESS;
|
---|
1952 | return VINF_SUCCESS; /* Not available for writing (anymore). */
|
---|
1953 | }
|
---|
1954 |
|
---|
1955 | int vrc;
|
---|
1956 |
|
---|
1957 | GuestWaitEvent *pEvent = NULL;
|
---|
1958 | GuestEventTypes eventTypes;
|
---|
1959 | try
|
---|
1960 | {
|
---|
1961 | /*
|
---|
1962 | * On Guest Additions < 4.3 there is no guarantee that the process status
|
---|
1963 | * change arrives *after* the input event, e.g. if this was the last input
|
---|
1964 | * block being written and the process will report status "terminate".
|
---|
1965 | * So just skip checking for process status change and only wait for the
|
---|
1966 | * input event.
|
---|
1967 | */
|
---|
1968 | if (mSession->i_getProtocolVersion() >= 2)
|
---|
1969 | eventTypes.push_back(VBoxEventType_OnGuestProcessStateChanged);
|
---|
1970 | eventTypes.push_back(VBoxEventType_OnGuestProcessInputNotify);
|
---|
1971 |
|
---|
1972 | vrc = registerWaitEvent(eventTypes, &pEvent);
|
---|
1973 | }
|
---|
1974 | catch (std::bad_alloc &)
|
---|
1975 | {
|
---|
1976 | vrc = VERR_NO_MEMORY;
|
---|
1977 | }
|
---|
1978 |
|
---|
1979 | if (RT_FAILURE(vrc))
|
---|
1980 | return vrc;
|
---|
1981 |
|
---|
1982 | VBOXHGCMSVCPARM paParms[5];
|
---|
1983 | int i = 0;
|
---|
1984 | HGCMSvcSetU32(&paParms[i++], pEvent->ContextID());
|
---|
1985 | HGCMSvcSetU32(&paParms[i++], mData.mPID);
|
---|
1986 | HGCMSvcSetU32(&paParms[i++], uFlags);
|
---|
1987 | HGCMSvcSetPv(&paParms[i++], pvData, (uint32_t)cbData);
|
---|
1988 | HGCMSvcSetU32(&paParms[i++], (uint32_t)cbData);
|
---|
1989 |
|
---|
1990 | alock.release(); /* Drop the write lock before sending. */
|
---|
1991 |
|
---|
1992 | uint32_t cbProcessed = 0;
|
---|
1993 | vrc = sendMessage(HOST_MSG_EXEC_SET_INPUT, i, paParms);
|
---|
1994 | if (RT_SUCCESS(vrc))
|
---|
1995 | {
|
---|
1996 | ProcessInputStatus_T inputStatus;
|
---|
1997 | vrc = i_waitForInputNotify(pEvent, uHandle, uTimeoutMS,
|
---|
1998 | &inputStatus, &cbProcessed);
|
---|
1999 | if (RT_SUCCESS(vrc))
|
---|
2000 | {
|
---|
2001 | /** @todo Set rcGuest. */
|
---|
2002 |
|
---|
2003 | if (puWritten)
|
---|
2004 | *puWritten = cbProcessed;
|
---|
2005 | }
|
---|
2006 | /** @todo Error handling. */
|
---|
2007 | }
|
---|
2008 |
|
---|
2009 | unregisterWaitEvent(pEvent);
|
---|
2010 |
|
---|
2011 | LogFlowThisFunc(("Returning cbProcessed=%RU32, rc=%Rrc\n",
|
---|
2012 | cbProcessed, vrc));
|
---|
2013 | return vrc;
|
---|
2014 | }
|
---|
2015 |
|
---|
2016 | // implementation of public methods
|
---|
2017 | /////////////////////////////////////////////////////////////////////////////
|
---|
2018 |
|
---|
2019 | HRESULT GuestProcess::read(ULONG aHandle, ULONG aToRead, ULONG aTimeoutMS, std::vector<BYTE> &aData)
|
---|
2020 | {
|
---|
2021 | AutoCaller autoCaller(this);
|
---|
2022 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2023 |
|
---|
2024 | if (aToRead == 0)
|
---|
2025 | return setError(E_INVALIDARG, tr("The size to read is zero"));
|
---|
2026 |
|
---|
2027 | LogFlowThisFuncEnter();
|
---|
2028 |
|
---|
2029 | aData.resize(aToRead);
|
---|
2030 |
|
---|
2031 | HRESULT hrc = S_OK;
|
---|
2032 |
|
---|
2033 | uint32_t cbRead;
|
---|
2034 | int vrcGuest = VERR_IPE_UNINITIALIZED_STATUS;
|
---|
2035 | int vrc = i_readData(aHandle, aToRead, aTimeoutMS, &aData.front(), aToRead, &cbRead, &vrcGuest);
|
---|
2036 | if (RT_SUCCESS(vrc))
|
---|
2037 | {
|
---|
2038 | if (aData.size() != cbRead)
|
---|
2039 | aData.resize(cbRead);
|
---|
2040 | }
|
---|
2041 | else
|
---|
2042 | {
|
---|
2043 | aData.resize(0);
|
---|
2044 |
|
---|
2045 | switch (vrc)
|
---|
2046 | {
|
---|
2047 | case VERR_GSTCTL_GUEST_ERROR:
|
---|
2048 | {
|
---|
2049 | GuestErrorInfo ge(GuestErrorInfo::Type_Process, vrcGuest, mData.mProcess.mExecutable.c_str());
|
---|
2050 | hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrcGuest,
|
---|
2051 | tr("Reading %RU32 bytes from guest process handle %RU32 failed: %s", "", aToRead),
|
---|
2052 | aToRead, aHandle, GuestBase::getErrorAsString(ge).c_str());
|
---|
2053 | break;
|
---|
2054 | }
|
---|
2055 | default:
|
---|
2056 | hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Reading from guest process \"%s\" (PID %RU32) failed: %Rrc"),
|
---|
2057 | mData.mProcess.mExecutable.c_str(), mData.mPID, vrc);
|
---|
2058 | break;
|
---|
2059 | }
|
---|
2060 | }
|
---|
2061 |
|
---|
2062 | LogFlowThisFunc(("rc=%Rrc, cbRead=%RU32\n", vrc, cbRead));
|
---|
2063 |
|
---|
2064 | LogFlowFuncLeaveRC(vrc);
|
---|
2065 | return hrc;
|
---|
2066 | }
|
---|
2067 |
|
---|
2068 | HRESULT GuestProcess::terminate()
|
---|
2069 | {
|
---|
2070 | AutoCaller autoCaller(this);
|
---|
2071 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2072 |
|
---|
2073 | LogFlowThisFuncEnter();
|
---|
2074 |
|
---|
2075 | HRESULT hrc = S_OK;
|
---|
2076 |
|
---|
2077 | int vrcGuest = VERR_IPE_UNINITIALIZED_STATUS;
|
---|
2078 | int vrc = i_terminateProcess(30 * 1000 /* Timeout in ms */, &vrcGuest);
|
---|
2079 |
|
---|
2080 | switch (vrc)
|
---|
2081 | {
|
---|
2082 | case VINF_SUCCESS:
|
---|
2083 | /* Nothing to do here, all good. */
|
---|
2084 | break;
|
---|
2085 |
|
---|
2086 | case VWRN_INVALID_STATE:
|
---|
2087 | {
|
---|
2088 | GuestErrorInfo ge(GuestErrorInfo::Type_Process, vrcGuest, mData.mProcess.mExecutable.c_str());
|
---|
2089 | hrc = setErrorBoth(VBOX_E_IPRT_ERROR, VWRN_INVALID_STATE,
|
---|
2090 | tr("Guest process is not in '%s' state anymore (current is in '%s')"),
|
---|
2091 | GuestProcess::i_statusToString(ProcessStatus_Started).c_str(),
|
---|
2092 | GuestProcess::i_statusToString(i_getStatus()).c_str());
|
---|
2093 | break;
|
---|
2094 | }
|
---|
2095 |
|
---|
2096 | case VERR_GSTCTL_GUEST_ERROR:
|
---|
2097 | {
|
---|
2098 | GuestErrorInfo ge(GuestErrorInfo::Type_Process, vrcGuest, mData.mProcess.mExecutable.c_str());
|
---|
2099 | hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrcGuest, tr("Terminating guest process failed: %s"),
|
---|
2100 | GuestBase::getErrorAsString(ge).c_str());
|
---|
2101 | break;
|
---|
2102 | }
|
---|
2103 |
|
---|
2104 | case VERR_NOT_SUPPORTED:
|
---|
2105 | {
|
---|
2106 | hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc,
|
---|
2107 | tr("Terminating guest process \"%s\" (PID %RU32) not supported by installed Guest Additions"),
|
---|
2108 | mData.mProcess.mExecutable.c_str(), mData.mPID);
|
---|
2109 | break;
|
---|
2110 | }
|
---|
2111 |
|
---|
2112 | default:
|
---|
2113 | hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Terminating guest process \"%s\" (PID %RU32) failed: %Rrc"),
|
---|
2114 | mData.mProcess.mExecutable.c_str(), mData.mPID, vrc);
|
---|
2115 | break;
|
---|
2116 | }
|
---|
2117 |
|
---|
2118 | /* Note: Also could be VWRN_INVALID_STATE from i_terminateProcess().
|
---|
2119 | * In such a case we have to keep the process in our list in order to fullfill any upcoming responses / requests. */
|
---|
2120 | if (vrc == VINF_SUCCESS)
|
---|
2121 | {
|
---|
2122 | /* Remove process from guest session list. Now only API clients
|
---|
2123 | * still can hold references to it. */
|
---|
2124 | AssertPtr(mSession);
|
---|
2125 | int vrc2 = mSession->i_processUnregister(this);
|
---|
2126 | if (RT_SUCCESS(vrc))
|
---|
2127 | vrc = vrc2;
|
---|
2128 | }
|
---|
2129 |
|
---|
2130 | LogFlowFuncLeaveRC(vrc);
|
---|
2131 | return hrc;
|
---|
2132 | }
|
---|
2133 |
|
---|
2134 | HRESULT GuestProcess::waitFor(ULONG aWaitFor, ULONG aTimeoutMS, ProcessWaitResult_T *aReason)
|
---|
2135 | {
|
---|
2136 | AutoCaller autoCaller(this);
|
---|
2137 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2138 |
|
---|
2139 | LogFlowThisFuncEnter();
|
---|
2140 |
|
---|
2141 | /* Validate flags: */
|
---|
2142 | static ULONG const s_fValidFlags = ProcessWaitForFlag_None | ProcessWaitForFlag_Start | ProcessWaitForFlag_Terminate
|
---|
2143 | | ProcessWaitForFlag_StdIn | ProcessWaitForFlag_StdOut | ProcessWaitForFlag_StdErr;
|
---|
2144 | if (aWaitFor & ~s_fValidFlags)
|
---|
2145 | return setErrorBoth(E_INVALIDARG, VERR_INVALID_FLAGS, tr("Flags value %#x, invalid: %#x"),
|
---|
2146 | aWaitFor, aWaitFor & ~s_fValidFlags);
|
---|
2147 |
|
---|
2148 | /*
|
---|
2149 | * Note: Do not hold any locks here while waiting!
|
---|
2150 | */
|
---|
2151 | HRESULT hrc = S_OK;
|
---|
2152 |
|
---|
2153 | int vrcGuest = VERR_IPE_UNINITIALIZED_STATUS;
|
---|
2154 | ProcessWaitResult_T waitResult;
|
---|
2155 | int vrc = i_waitFor(aWaitFor, aTimeoutMS, waitResult, &vrcGuest);
|
---|
2156 | if (RT_SUCCESS(vrc))
|
---|
2157 | {
|
---|
2158 | *aReason = waitResult;
|
---|
2159 | }
|
---|
2160 | else
|
---|
2161 | {
|
---|
2162 | switch (vrc)
|
---|
2163 | {
|
---|
2164 | case VERR_GSTCTL_GUEST_ERROR:
|
---|
2165 | {
|
---|
2166 | GuestErrorInfo ge(GuestErrorInfo::Type_Process, vrcGuest, mData.mProcess.mExecutable.c_str());
|
---|
2167 | hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrcGuest, tr("Waiting for guest process (flags %#x) failed: %s"),
|
---|
2168 | aWaitFor, GuestBase::getErrorAsString(ge).c_str());
|
---|
2169 | break;
|
---|
2170 | }
|
---|
2171 | case VERR_TIMEOUT:
|
---|
2172 | *aReason = ProcessWaitResult_Timeout;
|
---|
2173 | break;
|
---|
2174 |
|
---|
2175 | default:
|
---|
2176 | hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Waiting for guest process \"%s\" (PID %RU32) failed: %Rrc"),
|
---|
2177 | mData.mProcess.mExecutable.c_str(), mData.mPID, vrc);
|
---|
2178 | break;
|
---|
2179 | }
|
---|
2180 | }
|
---|
2181 |
|
---|
2182 | LogFlowFuncLeaveRC(vrc);
|
---|
2183 | return hrc;
|
---|
2184 | }
|
---|
2185 |
|
---|
2186 | HRESULT GuestProcess::waitForArray(const std::vector<ProcessWaitForFlag_T> &aWaitFor,
|
---|
2187 | ULONG aTimeoutMS, ProcessWaitResult_T *aReason)
|
---|
2188 | {
|
---|
2189 | uint32_t fWaitFor = ProcessWaitForFlag_None;
|
---|
2190 | for (size_t i = 0; i < aWaitFor.size(); i++)
|
---|
2191 | fWaitFor |= aWaitFor[i];
|
---|
2192 |
|
---|
2193 | return WaitFor(fWaitFor, aTimeoutMS, aReason);
|
---|
2194 | }
|
---|
2195 |
|
---|
2196 | HRESULT GuestProcess::write(ULONG aHandle, ULONG aFlags, const std::vector<BYTE> &aData,
|
---|
2197 | ULONG aTimeoutMS, ULONG *aWritten)
|
---|
2198 | {
|
---|
2199 | static ULONG const s_fValidFlags = ProcessInputFlag_None | ProcessInputFlag_EndOfFile;
|
---|
2200 | if (aFlags & ~s_fValidFlags)
|
---|
2201 | return setErrorBoth(E_INVALIDARG, VERR_INVALID_FLAGS, tr("Flags value %#x, invalid: %#x"),
|
---|
2202 | aFlags, aFlags & ~s_fValidFlags);
|
---|
2203 |
|
---|
2204 | AutoCaller autoCaller(this);
|
---|
2205 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2206 |
|
---|
2207 | LogFlowThisFuncEnter();
|
---|
2208 |
|
---|
2209 | HRESULT hrc = S_OK;
|
---|
2210 |
|
---|
2211 | uint32_t cbWritten;
|
---|
2212 | int vrcGuest = VERR_IPE_UNINITIALIZED_STATUS;
|
---|
2213 | uint32_t cbData = (uint32_t)aData.size();
|
---|
2214 | void *pvData = cbData > 0 ? (void *)&aData.front() : NULL;
|
---|
2215 | int vrc = i_writeData(aHandle, aFlags, pvData, cbData, aTimeoutMS, &cbWritten, &vrcGuest);
|
---|
2216 | if (RT_FAILURE(vrc))
|
---|
2217 | {
|
---|
2218 | switch (vrc)
|
---|
2219 | {
|
---|
2220 | case VERR_GSTCTL_GUEST_ERROR:
|
---|
2221 | {
|
---|
2222 | GuestErrorInfo ge(GuestErrorInfo::Type_Process, vrcGuest, mData.mProcess.mExecutable.c_str());
|
---|
2223 | hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrcGuest,
|
---|
2224 | tr("Writing %RU32 bytes (flags %#x) to guest process failed: %s", "", cbData),
|
---|
2225 | cbData, aFlags, GuestBase::getErrorAsString(ge).c_str());
|
---|
2226 | break;
|
---|
2227 | }
|
---|
2228 | default:
|
---|
2229 | hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Writing to guest process \"%s\" (PID %RU32) failed: %Rrc"),
|
---|
2230 | mData.mProcess.mExecutable.c_str(), mData.mPID, vrc);
|
---|
2231 | break;
|
---|
2232 | }
|
---|
2233 | }
|
---|
2234 |
|
---|
2235 | LogFlowThisFunc(("rc=%Rrc, aWritten=%RU32\n", vrc, cbWritten));
|
---|
2236 |
|
---|
2237 | *aWritten = (ULONG)cbWritten;
|
---|
2238 |
|
---|
2239 | LogFlowFuncLeaveRC(vrc);
|
---|
2240 | return hrc;
|
---|
2241 | }
|
---|
2242 |
|
---|
2243 | HRESULT GuestProcess::writeArray(ULONG aHandle, const std::vector<ProcessInputFlag_T> &aFlags,
|
---|
2244 | const std::vector<BYTE> &aData, ULONG aTimeoutMS, ULONG *aWritten)
|
---|
2245 | {
|
---|
2246 | LogFlowThisFuncEnter();
|
---|
2247 |
|
---|
2248 | ULONG fWrite = ProcessInputFlag_None;
|
---|
2249 | for (size_t i = 0; i < aFlags.size(); i++)
|
---|
2250 | fWrite |= aFlags[i];
|
---|
2251 |
|
---|
2252 | return write(aHandle, fWrite, aData, aTimeoutMS, aWritten);
|
---|
2253 | }
|
---|
2254 |
|
---|
2255 | ///////////////////////////////////////////////////////////////////////////////
|
---|
2256 |
|
---|
2257 | GuestProcessTool::GuestProcessTool(void)
|
---|
2258 | : pSession(NULL),
|
---|
2259 | pProcess(NULL)
|
---|
2260 | {
|
---|
2261 | }
|
---|
2262 |
|
---|
2263 | GuestProcessTool::~GuestProcessTool(void)
|
---|
2264 | {
|
---|
2265 | uninit();
|
---|
2266 | }
|
---|
2267 |
|
---|
2268 | /**
|
---|
2269 | * Initializes and starts a process tool on the guest.
|
---|
2270 | *
|
---|
2271 | * @returns VBox status code.
|
---|
2272 | * @param pGuestSession Guest session the process tools should be started in.
|
---|
2273 | * @param startupInfo Guest process startup info to use for starting.
|
---|
2274 | * @param fAsync Whether to start asynchronously or not.
|
---|
2275 | * @param prcGuest Where to return the guest error when VERR_GSTCTL_GUEST_ERROR
|
---|
2276 | * was returned. Optional.
|
---|
2277 | */
|
---|
2278 | int GuestProcessTool::init(GuestSession *pGuestSession, const GuestProcessStartupInfo &startupInfo,
|
---|
2279 | bool fAsync, int *prcGuest)
|
---|
2280 | {
|
---|
2281 | LogFlowThisFunc(("pGuestSession=%p, exe=%s, fAsync=%RTbool\n",
|
---|
2282 | pGuestSession, startupInfo.mExecutable.c_str(), fAsync));
|
---|
2283 |
|
---|
2284 | AssertPtrReturn(pGuestSession, VERR_INVALID_POINTER);
|
---|
2285 | Assert(startupInfo.mArguments[0] == startupInfo.mExecutable);
|
---|
2286 |
|
---|
2287 | pSession = pGuestSession;
|
---|
2288 | mStartupInfo = startupInfo;
|
---|
2289 |
|
---|
2290 | /* Make sure the process is hidden. */
|
---|
2291 | mStartupInfo.mFlags |= ProcessCreateFlag_Hidden;
|
---|
2292 |
|
---|
2293 | int vrc = pSession->i_processCreateEx(mStartupInfo, pProcess);
|
---|
2294 | if (RT_SUCCESS(vrc))
|
---|
2295 | {
|
---|
2296 | int vrcGuest = VINF_SUCCESS;
|
---|
2297 | vrc = fAsync
|
---|
2298 | ? pProcess->i_startProcessAsync()
|
---|
2299 | : pProcess->i_startProcess(30 * 1000 /* 30s timeout */, &vrcGuest);
|
---|
2300 |
|
---|
2301 | if ( RT_SUCCESS(vrc)
|
---|
2302 | && !fAsync
|
---|
2303 | && RT_FAILURE(vrcGuest)
|
---|
2304 | )
|
---|
2305 | {
|
---|
2306 | vrc = VERR_GSTCTL_GUEST_ERROR;
|
---|
2307 | }
|
---|
2308 |
|
---|
2309 | if (prcGuest)
|
---|
2310 | *prcGuest = vrcGuest;
|
---|
2311 | }
|
---|
2312 |
|
---|
2313 | LogFlowFuncLeaveRC(vrc);
|
---|
2314 | return vrc;
|
---|
2315 | }
|
---|
2316 |
|
---|
2317 | /**
|
---|
2318 | * Unitializes a guest process tool by terminating it on the guest.
|
---|
2319 | */
|
---|
2320 | void GuestProcessTool::uninit(void)
|
---|
2321 | {
|
---|
2322 | /* Make sure the process is terminated and unregistered from the guest session. */
|
---|
2323 | int vrcGuestIgnored;
|
---|
2324 | terminate(30 * 1000 /* 30s timeout */, &vrcGuestIgnored);
|
---|
2325 |
|
---|
2326 | /* Unregister the process from the process (and the session's object) list. */
|
---|
2327 | if ( pSession
|
---|
2328 | && pProcess)
|
---|
2329 | pSession->i_processUnregister(pProcess);
|
---|
2330 |
|
---|
2331 | /* Release references. */
|
---|
2332 | pProcess.setNull();
|
---|
2333 | pSession.setNull();
|
---|
2334 | }
|
---|
2335 |
|
---|
2336 | /**
|
---|
2337 | * Gets the current guest process stream block.
|
---|
2338 | *
|
---|
2339 | * @returns VBox status code.
|
---|
2340 | * @param uHandle Guest process file handle to get current block for.
|
---|
2341 | * @param strmBlock Where to return the stream block on success.
|
---|
2342 | */
|
---|
2343 | int GuestProcessTool::getCurrentBlock(uint32_t uHandle, GuestProcessStreamBlock &strmBlock)
|
---|
2344 | {
|
---|
2345 | const GuestProcessStream *pStream = NULL;
|
---|
2346 | if (uHandle == GUEST_PROC_OUT_H_STDOUT)
|
---|
2347 | pStream = &mStdOut;
|
---|
2348 | else if (uHandle == GUEST_PROC_OUT_H_STDERR)
|
---|
2349 | pStream = &mStdErr;
|
---|
2350 |
|
---|
2351 | if (!pStream)
|
---|
2352 | return VERR_INVALID_PARAMETER;
|
---|
2353 |
|
---|
2354 | /** @todo Why not using pStream down below and hardcode to mStdOut? */
|
---|
2355 |
|
---|
2356 | int vrc;
|
---|
2357 | do
|
---|
2358 | {
|
---|
2359 | /* Try parsing the data to see if the current block is complete. */
|
---|
2360 | vrc = mStdOut.ParseBlock(strmBlock);
|
---|
2361 | if (strmBlock.GetCount())
|
---|
2362 | break;
|
---|
2363 | } while (RT_SUCCESS(vrc));
|
---|
2364 |
|
---|
2365 | LogFlowThisFunc(("rc=%Rrc, %RU64 pairs\n",
|
---|
2366 | vrc, strmBlock.GetCount()));
|
---|
2367 | return vrc;
|
---|
2368 | }
|
---|
2369 |
|
---|
2370 | /**
|
---|
2371 | * Returns the result code from an ended guest process tool.
|
---|
2372 | *
|
---|
2373 | * @returns Result code from guest process tool.
|
---|
2374 | */
|
---|
2375 | int GuestProcessTool::getRc(void) const
|
---|
2376 | {
|
---|
2377 | LONG exitCode = -1;
|
---|
2378 | HRESULT hr = pProcess->COMGETTER(ExitCode(&exitCode));
|
---|
2379 | AssertComRC(hr);
|
---|
2380 |
|
---|
2381 | return GuestProcessTool::exitCodeToRc(mStartupInfo, exitCode);
|
---|
2382 | }
|
---|
2383 |
|
---|
2384 | /**
|
---|
2385 | * Returns whether a guest process tool is still running or not.
|
---|
2386 | *
|
---|
2387 | * @returns \c true if running, or \c false if not.
|
---|
2388 | */
|
---|
2389 | bool GuestProcessTool::isRunning(void)
|
---|
2390 | {
|
---|
2391 | AssertReturn(!pProcess.isNull(), false);
|
---|
2392 |
|
---|
2393 | ProcessStatus_T procStatus = ProcessStatus_Undefined;
|
---|
2394 | HRESULT hr = pProcess->COMGETTER(Status(&procStatus));
|
---|
2395 | AssertComRC(hr);
|
---|
2396 |
|
---|
2397 | if ( procStatus == ProcessStatus_Started
|
---|
2398 | || procStatus == ProcessStatus_Paused
|
---|
2399 | || procStatus == ProcessStatus_Terminating)
|
---|
2400 | {
|
---|
2401 | return true;
|
---|
2402 | }
|
---|
2403 |
|
---|
2404 | return false;
|
---|
2405 | }
|
---|
2406 |
|
---|
2407 | /**
|
---|
2408 | * Returns whether the tool has been run correctly or not, based on it's internal process
|
---|
2409 | * status and reported exit status.
|
---|
2410 | *
|
---|
2411 | * @return @c true if the tool has been run correctly (exit status 0), or @c false if some error
|
---|
2412 | * occurred (exit status <> 0 or wrong process state).
|
---|
2413 | */
|
---|
2414 | bool GuestProcessTool::isTerminatedOk(void)
|
---|
2415 | {
|
---|
2416 | return getTerminationStatus() == VINF_SUCCESS ? true : false;
|
---|
2417 | }
|
---|
2418 |
|
---|
2419 | /**
|
---|
2420 | * Static helper function to start and wait for a certain toolbox tool.
|
---|
2421 | *
|
---|
2422 | * This function most likely is the one you want to use in the first place if you
|
---|
2423 | * want to just use a toolbox tool and wait for its result. See runEx() if you also
|
---|
2424 | * needs its output.
|
---|
2425 | *
|
---|
2426 | * @return VBox status code.
|
---|
2427 | * @param pGuestSession Guest control session to use for starting the toolbox tool in.
|
---|
2428 | * @param startupInfo Startup information about the toolbox tool.
|
---|
2429 | * @param pvrcGuest Where to store the toolbox tool's specific error code in case
|
---|
2430 | * VERR_GSTCTL_GUEST_ERROR is returned.
|
---|
2431 | */
|
---|
2432 | /* static */
|
---|
2433 | int GuestProcessTool::run( GuestSession *pGuestSession,
|
---|
2434 | const GuestProcessStartupInfo &startupInfo,
|
---|
2435 | int *pvrcGuest /* = NULL */)
|
---|
2436 | {
|
---|
2437 | int vrcGuest = VERR_IPE_UNINITIALIZED_STATUS;
|
---|
2438 |
|
---|
2439 | GuestProcessToolErrorInfo errorInfo = { VERR_IPE_UNINITIALIZED_STATUS, INT32_MAX };
|
---|
2440 | int vrc = runErrorInfo(pGuestSession, startupInfo, errorInfo);
|
---|
2441 | if (RT_SUCCESS(vrc))
|
---|
2442 | {
|
---|
2443 | /* Make sure to check the error information we got from the guest tool. */
|
---|
2444 | if (GuestProcess::i_isGuestError(errorInfo.rcGuest))
|
---|
2445 | {
|
---|
2446 | if (errorInfo.rcGuest == VERR_GSTCTL_PROCESS_EXIT_CODE) /* Translate exit code to a meaningful error code. */
|
---|
2447 | vrcGuest = GuestProcessTool::exitCodeToRc(startupInfo, errorInfo.iExitCode);
|
---|
2448 | else /* At least return something. */
|
---|
2449 | vrcGuest = errorInfo.rcGuest;
|
---|
2450 |
|
---|
2451 | if (pvrcGuest)
|
---|
2452 | *pvrcGuest = vrcGuest;
|
---|
2453 |
|
---|
2454 | vrc = VERR_GSTCTL_GUEST_ERROR;
|
---|
2455 | }
|
---|
2456 | }
|
---|
2457 |
|
---|
2458 | LogFlowFunc(("Returned vrc=%Rrc, vrcGuest=%Rrc, iExitCode=%d\n", vrc, errorInfo.rcGuest, errorInfo.iExitCode));
|
---|
2459 | return vrc;
|
---|
2460 | }
|
---|
2461 |
|
---|
2462 | /**
|
---|
2463 | * Static helper function to start and wait for a certain toolbox tool, returning
|
---|
2464 | * extended error information from the guest.
|
---|
2465 | *
|
---|
2466 | * @return VBox status code.
|
---|
2467 | * @param pGuestSession Guest control session to use for starting the toolbox tool in.
|
---|
2468 | * @param startupInfo Startup information about the toolbox tool.
|
---|
2469 | * @param errorInfo Error information returned for error handling.
|
---|
2470 | */
|
---|
2471 | /* static */
|
---|
2472 | int GuestProcessTool::runErrorInfo( GuestSession *pGuestSession,
|
---|
2473 | const GuestProcessStartupInfo &startupInfo,
|
---|
2474 | GuestProcessToolErrorInfo &errorInfo)
|
---|
2475 | {
|
---|
2476 | return runExErrorInfo(pGuestSession, startupInfo,
|
---|
2477 | NULL /* paStrmOutObjects */, 0 /* cStrmOutObjects */, errorInfo);
|
---|
2478 | }
|
---|
2479 |
|
---|
2480 | /**
|
---|
2481 | * Static helper function to start and wait for output of a certain toolbox tool.
|
---|
2482 | *
|
---|
2483 | * @return IPRT status code.
|
---|
2484 | * @param pGuestSession Guest control session to use for starting the toolbox tool in.
|
---|
2485 | * @param startupInfo Startup information about the toolbox tool.
|
---|
2486 | * @param paStrmOutObjects Pointer to stream objects array to use for retrieving the output of the toolbox tool.
|
---|
2487 | * Optional.
|
---|
2488 | * @param cStrmOutObjects Number of stream objects passed in. Optional.
|
---|
2489 | * @param pvrcGuest Error code returned from the guest side if VERR_GSTCTL_GUEST_ERROR is returned. Optional.
|
---|
2490 | */
|
---|
2491 | /* static */
|
---|
2492 | int GuestProcessTool::runEx( GuestSession *pGuestSession,
|
---|
2493 | const GuestProcessStartupInfo &startupInfo,
|
---|
2494 | GuestCtrlStreamObjects *paStrmOutObjects,
|
---|
2495 | uint32_t cStrmOutObjects,
|
---|
2496 | int *pvrcGuest /* = NULL */)
|
---|
2497 | {
|
---|
2498 | int vrcGuest = VERR_IPE_UNINITIALIZED_STATUS;
|
---|
2499 |
|
---|
2500 | GuestProcessToolErrorInfo errorInfo = { VERR_IPE_UNINITIALIZED_STATUS, INT32_MAX };
|
---|
2501 | int vrc = GuestProcessTool::runExErrorInfo(pGuestSession, startupInfo, paStrmOutObjects, cStrmOutObjects, errorInfo);
|
---|
2502 | if (RT_SUCCESS(vrc))
|
---|
2503 | {
|
---|
2504 | /* Make sure to check the error information we got from the guest tool. */
|
---|
2505 | if (GuestProcess::i_isGuestError(errorInfo.rcGuest))
|
---|
2506 | {
|
---|
2507 | if (errorInfo.rcGuest == VERR_GSTCTL_PROCESS_EXIT_CODE) /* Translate exit code to a meaningful error code. */
|
---|
2508 | vrcGuest = GuestProcessTool::exitCodeToRc(startupInfo, errorInfo.iExitCode);
|
---|
2509 | else /* At least return something. */
|
---|
2510 | vrcGuest = errorInfo.rcGuest;
|
---|
2511 |
|
---|
2512 | if (pvrcGuest)
|
---|
2513 | *pvrcGuest = vrcGuest;
|
---|
2514 |
|
---|
2515 | vrc = VERR_GSTCTL_GUEST_ERROR;
|
---|
2516 | }
|
---|
2517 | }
|
---|
2518 |
|
---|
2519 | LogFlowFunc(("Returned vrc=%Rrc, vrcGuest=%Rrc, iExitCode=%d\n", vrc, errorInfo.rcGuest, errorInfo.iExitCode));
|
---|
2520 | return vrc;
|
---|
2521 | }
|
---|
2522 |
|
---|
2523 | /**
|
---|
2524 | * Static helper function to start and wait for output of a certain toolbox tool.
|
---|
2525 | *
|
---|
2526 | * This is the extended version, which addds the possibility of retrieving parsable so-called guest stream
|
---|
2527 | * objects. Those objects are issued on the guest side as part of VBoxService's toolbox tools (think of a BusyBox-like approach)
|
---|
2528 | * on stdout and can be used on the host side to retrieve more information about the actual command issued on the guest side.
|
---|
2529 | *
|
---|
2530 | * @return VBox status code.
|
---|
2531 | * @param pGuestSession Guest control session to use for starting the toolbox tool in.
|
---|
2532 | * @param startupInfo Startup information about the toolbox tool.
|
---|
2533 | * @param paStrmOutObjects Pointer to stream objects array to use for retrieving the output of the toolbox tool.
|
---|
2534 | * Optional.
|
---|
2535 | * @param cStrmOutObjects Number of stream objects passed in. Optional.
|
---|
2536 | * @param errorInfo Error information returned for error handling.
|
---|
2537 | */
|
---|
2538 | /* static */
|
---|
2539 | int GuestProcessTool::runExErrorInfo( GuestSession *pGuestSession,
|
---|
2540 | const GuestProcessStartupInfo &startupInfo,
|
---|
2541 | GuestCtrlStreamObjects *paStrmOutObjects,
|
---|
2542 | uint32_t cStrmOutObjects,
|
---|
2543 | GuestProcessToolErrorInfo &errorInfo)
|
---|
2544 | {
|
---|
2545 | AssertPtrReturn(pGuestSession, VERR_INVALID_POINTER);
|
---|
2546 | /* paStrmOutObjects is optional. */
|
---|
2547 |
|
---|
2548 | /** @todo Check if this is a valid toolbox. */
|
---|
2549 |
|
---|
2550 | GuestProcessTool procTool;
|
---|
2551 | int vrc = procTool.init(pGuestSession, startupInfo, false /* Async */, &errorInfo.rcGuest);
|
---|
2552 | if (RT_SUCCESS(vrc))
|
---|
2553 | {
|
---|
2554 | while (cStrmOutObjects--)
|
---|
2555 | {
|
---|
2556 | try
|
---|
2557 | {
|
---|
2558 | GuestProcessStreamBlock strmBlk;
|
---|
2559 | vrc = procTool.waitEx( paStrmOutObjects
|
---|
2560 | ? GUESTPROCESSTOOL_WAIT_FLAG_STDOUT_BLOCK
|
---|
2561 | : GUESTPROCESSTOOL_WAIT_FLAG_NONE, &strmBlk, &errorInfo.rcGuest);
|
---|
2562 | if (paStrmOutObjects)
|
---|
2563 | paStrmOutObjects->push_back(strmBlk);
|
---|
2564 | }
|
---|
2565 | catch (std::bad_alloc &)
|
---|
2566 | {
|
---|
2567 | vrc = VERR_NO_MEMORY;
|
---|
2568 | }
|
---|
2569 |
|
---|
2570 | if (RT_FAILURE(vrc))
|
---|
2571 | break;
|
---|
2572 | }
|
---|
2573 | }
|
---|
2574 |
|
---|
2575 | if (RT_SUCCESS(vrc))
|
---|
2576 | {
|
---|
2577 | /* Make sure the process runs until completion. */
|
---|
2578 | vrc = procTool.wait(GUESTPROCESSTOOL_WAIT_FLAG_NONE, &errorInfo.rcGuest);
|
---|
2579 | if (RT_SUCCESS(vrc))
|
---|
2580 | errorInfo.rcGuest = procTool.getTerminationStatus(&errorInfo.iExitCode);
|
---|
2581 | }
|
---|
2582 |
|
---|
2583 | LogFlowFunc(("Returned rc=%Rrc, rcGuest=%Rrc, iExitCode=%d\n", vrc, errorInfo.rcGuest, errorInfo.iExitCode));
|
---|
2584 | return vrc;
|
---|
2585 | }
|
---|
2586 |
|
---|
2587 | /**
|
---|
2588 | * Reports if the tool has been run correctly.
|
---|
2589 | *
|
---|
2590 | * @return Will return VERR_GSTCTL_PROCESS_EXIT_CODE if the tool process returned an exit code <> 0,
|
---|
2591 | * VERR_GSTCTL_PROCESS_WRONG_STATE if the tool process is in a wrong state (e.g. still running),
|
---|
2592 | * or VINF_SUCCESS otherwise.
|
---|
2593 | *
|
---|
2594 | * @param piExitCode Exit code of the tool. Optional.
|
---|
2595 | */
|
---|
2596 | int GuestProcessTool::getTerminationStatus(int32_t *piExitCode /* = NULL */)
|
---|
2597 | {
|
---|
2598 | Assert(!pProcess.isNull());
|
---|
2599 | /* pExitCode is optional. */
|
---|
2600 |
|
---|
2601 | int vrc;
|
---|
2602 | if (!isRunning())
|
---|
2603 | {
|
---|
2604 | LONG iExitCode = -1;
|
---|
2605 | HRESULT hr = pProcess->COMGETTER(ExitCode(&iExitCode));
|
---|
2606 | AssertComRC(hr);
|
---|
2607 |
|
---|
2608 | if (piExitCode)
|
---|
2609 | *piExitCode = iExitCode;
|
---|
2610 |
|
---|
2611 | vrc = iExitCode != 0 ? VERR_GSTCTL_PROCESS_EXIT_CODE : VINF_SUCCESS;
|
---|
2612 | }
|
---|
2613 | else
|
---|
2614 | vrc = VERR_GSTCTL_PROCESS_WRONG_STATE;
|
---|
2615 |
|
---|
2616 | LogFlowFuncLeaveRC(vrc);
|
---|
2617 | return vrc;
|
---|
2618 | }
|
---|
2619 |
|
---|
2620 | /**
|
---|
2621 | * Waits for a guest process tool.
|
---|
2622 | *
|
---|
2623 | * @returns VBox status code.
|
---|
2624 | * @param fToolWaitFlags Guest process tool wait flags to use for waiting.
|
---|
2625 | * @param prcGuest Where to return the guest error when VERR_GSTCTL_GUEST_ERROR
|
---|
2626 | * was returned. Optional.
|
---|
2627 | */
|
---|
2628 | int GuestProcessTool::wait(uint32_t fToolWaitFlags, int *prcGuest)
|
---|
2629 | {
|
---|
2630 | return waitEx(fToolWaitFlags, NULL /* pStrmBlkOut */, prcGuest);
|
---|
2631 | }
|
---|
2632 |
|
---|
2633 | /**
|
---|
2634 | * Waits for a guest process tool, also returning process output.
|
---|
2635 | *
|
---|
2636 | * @returns VBox status code.
|
---|
2637 | * @param fToolWaitFlags Guest process tool wait flags to use for waiting.
|
---|
2638 | * @param pStrmBlkOut Where to store the guest process output.
|
---|
2639 | * @param prcGuest Where to return the guest error when VERR_GSTCTL_GUEST_ERROR
|
---|
2640 | * was returned. Optional.
|
---|
2641 | */
|
---|
2642 | int GuestProcessTool::waitEx(uint32_t fToolWaitFlags, GuestProcessStreamBlock *pStrmBlkOut, int *prcGuest)
|
---|
2643 | {
|
---|
2644 | LogFlowThisFunc(("fToolWaitFlags=0x%x, pStreamBlock=%p, prcGuest=%p\n", fToolWaitFlags, pStrmBlkOut, prcGuest));
|
---|
2645 |
|
---|
2646 | /* Can we parse the next block without waiting? */
|
---|
2647 | int vrc;
|
---|
2648 | if (fToolWaitFlags & GUESTPROCESSTOOL_WAIT_FLAG_STDOUT_BLOCK)
|
---|
2649 | {
|
---|
2650 | AssertPtr(pStrmBlkOut);
|
---|
2651 | vrc = getCurrentBlock(GUEST_PROC_OUT_H_STDOUT, *pStrmBlkOut);
|
---|
2652 | if (RT_SUCCESS(vrc))
|
---|
2653 | return vrc;
|
---|
2654 | /* else do the waiting below. */
|
---|
2655 | }
|
---|
2656 |
|
---|
2657 | /* Do the waiting. */
|
---|
2658 | uint32_t fProcWaitForFlags = ProcessWaitForFlag_Terminate;
|
---|
2659 | if (mStartupInfo.mFlags & ProcessCreateFlag_WaitForStdOut)
|
---|
2660 | fProcWaitForFlags |= ProcessWaitForFlag_StdOut;
|
---|
2661 | if (mStartupInfo.mFlags & ProcessCreateFlag_WaitForStdErr)
|
---|
2662 | fProcWaitForFlags |= ProcessWaitForFlag_StdErr;
|
---|
2663 |
|
---|
2664 | /** @todo Decrease timeout while running. */
|
---|
2665 | uint64_t u64StartMS = RTTimeMilliTS();
|
---|
2666 | uint32_t uTimeoutMS = mStartupInfo.mTimeoutMS;
|
---|
2667 |
|
---|
2668 | int vrcGuest = VINF_SUCCESS;
|
---|
2669 | bool fDone = false;
|
---|
2670 |
|
---|
2671 | BYTE byBuf[_64K];
|
---|
2672 | uint32_t cbRead;
|
---|
2673 |
|
---|
2674 | bool fHandleStdOut = false;
|
---|
2675 | bool fHandleStdErr = false;
|
---|
2676 |
|
---|
2677 | /**
|
---|
2678 | * Updates the elapsed time and checks if a
|
---|
2679 | * timeout happened, then breaking out of the loop.
|
---|
2680 | */
|
---|
2681 | #define UPDATE_AND_CHECK_ELAPSED_TIME() \
|
---|
2682 | u64ElapsedMS = RTTimeMilliTS() - u64StartMS; \
|
---|
2683 | if ( uTimeoutMS != RT_INDEFINITE_WAIT \
|
---|
2684 | && u64ElapsedMS >= uTimeoutMS) \
|
---|
2685 | { \
|
---|
2686 | vrc = VERR_TIMEOUT; \
|
---|
2687 | break; \
|
---|
2688 | }
|
---|
2689 |
|
---|
2690 | /**
|
---|
2691 | * Returns the remaining time (in ms).
|
---|
2692 | */
|
---|
2693 | #define GET_REMAINING_TIME \
|
---|
2694 | uTimeoutMS == RT_INDEFINITE_WAIT \
|
---|
2695 | ? RT_INDEFINITE_WAIT : uTimeoutMS - (uint32_t)u64ElapsedMS \
|
---|
2696 |
|
---|
2697 | ProcessWaitResult_T waitRes = ProcessWaitResult_None;
|
---|
2698 | do
|
---|
2699 | {
|
---|
2700 | uint64_t u64ElapsedMS;
|
---|
2701 | UPDATE_AND_CHECK_ELAPSED_TIME();
|
---|
2702 |
|
---|
2703 | vrc = pProcess->i_waitFor(fProcWaitForFlags, GET_REMAINING_TIME, waitRes, &vrcGuest);
|
---|
2704 | if (RT_FAILURE(vrc))
|
---|
2705 | break;
|
---|
2706 |
|
---|
2707 | switch (waitRes)
|
---|
2708 | {
|
---|
2709 | case ProcessWaitResult_StdIn:
|
---|
2710 | vrc = VERR_NOT_IMPLEMENTED;
|
---|
2711 | break;
|
---|
2712 |
|
---|
2713 | case ProcessWaitResult_StdOut:
|
---|
2714 | fHandleStdOut = true;
|
---|
2715 | break;
|
---|
2716 |
|
---|
2717 | case ProcessWaitResult_StdErr:
|
---|
2718 | fHandleStdErr = true;
|
---|
2719 | break;
|
---|
2720 |
|
---|
2721 | case ProcessWaitResult_WaitFlagNotSupported:
|
---|
2722 | if (fProcWaitForFlags & ProcessWaitForFlag_StdOut)
|
---|
2723 | fHandleStdOut = true;
|
---|
2724 | if (fProcWaitForFlags & ProcessWaitForFlag_StdErr)
|
---|
2725 | fHandleStdErr = true;
|
---|
2726 | /* Since waiting for stdout / stderr is not supported by the guest,
|
---|
2727 | * wait a bit to not hog the CPU too much when polling for data. */
|
---|
2728 | RTThreadSleep(1); /* Optional, don't check rc. */
|
---|
2729 | break;
|
---|
2730 |
|
---|
2731 | case ProcessWaitResult_Error:
|
---|
2732 | vrc = VERR_GSTCTL_GUEST_ERROR;
|
---|
2733 | break;
|
---|
2734 |
|
---|
2735 | case ProcessWaitResult_Terminate:
|
---|
2736 | fDone = true;
|
---|
2737 | break;
|
---|
2738 |
|
---|
2739 | case ProcessWaitResult_Timeout:
|
---|
2740 | vrc = VERR_TIMEOUT;
|
---|
2741 | break;
|
---|
2742 |
|
---|
2743 | case ProcessWaitResult_Start:
|
---|
2744 | case ProcessWaitResult_Status:
|
---|
2745 | /* Not used here, just skip. */
|
---|
2746 | break;
|
---|
2747 |
|
---|
2748 | default:
|
---|
2749 | AssertMsgFailed(("Unhandled process wait result %RU32\n", waitRes));
|
---|
2750 | break;
|
---|
2751 | }
|
---|
2752 |
|
---|
2753 | if (RT_FAILURE(vrc))
|
---|
2754 | break;
|
---|
2755 |
|
---|
2756 | if (fHandleStdOut)
|
---|
2757 | {
|
---|
2758 | UPDATE_AND_CHECK_ELAPSED_TIME();
|
---|
2759 |
|
---|
2760 | cbRead = 0;
|
---|
2761 | vrc = pProcess->i_readData(GUEST_PROC_OUT_H_STDOUT, sizeof(byBuf),
|
---|
2762 | GET_REMAINING_TIME,
|
---|
2763 | byBuf, sizeof(byBuf),
|
---|
2764 | &cbRead, &vrcGuest);
|
---|
2765 | if ( RT_FAILURE(vrc)
|
---|
2766 | || vrc == VWRN_GSTCTL_OBJECTSTATE_CHANGED)
|
---|
2767 | break;
|
---|
2768 |
|
---|
2769 | if (cbRead)
|
---|
2770 | {
|
---|
2771 | LogFlowThisFunc(("Received %RU32 bytes from stdout\n", cbRead));
|
---|
2772 | vrc = mStdOut.AddData(byBuf, cbRead);
|
---|
2773 |
|
---|
2774 | if ( RT_SUCCESS(vrc)
|
---|
2775 | && (fToolWaitFlags & GUESTPROCESSTOOL_WAIT_FLAG_STDOUT_BLOCK))
|
---|
2776 | {
|
---|
2777 | AssertPtr(pStrmBlkOut);
|
---|
2778 | vrc = getCurrentBlock(GUEST_PROC_OUT_H_STDOUT, *pStrmBlkOut);
|
---|
2779 |
|
---|
2780 | /* When successful, break out of the loop because we're done
|
---|
2781 | * with reading the first stream block. */
|
---|
2782 | if (RT_SUCCESS(vrc))
|
---|
2783 | fDone = true;
|
---|
2784 | }
|
---|
2785 | }
|
---|
2786 |
|
---|
2787 | fHandleStdOut = false;
|
---|
2788 | }
|
---|
2789 |
|
---|
2790 | if (fHandleStdErr)
|
---|
2791 | {
|
---|
2792 | UPDATE_AND_CHECK_ELAPSED_TIME();
|
---|
2793 |
|
---|
2794 | cbRead = 0;
|
---|
2795 | vrc = pProcess->i_readData(GUEST_PROC_OUT_H_STDERR, sizeof(byBuf),
|
---|
2796 | GET_REMAINING_TIME,
|
---|
2797 | byBuf, sizeof(byBuf),
|
---|
2798 | &cbRead, &vrcGuest);
|
---|
2799 | if ( RT_FAILURE(vrc)
|
---|
2800 | || vrc == VWRN_GSTCTL_OBJECTSTATE_CHANGED)
|
---|
2801 | break;
|
---|
2802 |
|
---|
2803 | if (cbRead)
|
---|
2804 | {
|
---|
2805 | LogFlowThisFunc(("Received %RU32 bytes from stderr\n", cbRead));
|
---|
2806 | vrc = mStdErr.AddData(byBuf, cbRead);
|
---|
2807 | }
|
---|
2808 |
|
---|
2809 | fHandleStdErr = false;
|
---|
2810 | }
|
---|
2811 |
|
---|
2812 | } while (!fDone && RT_SUCCESS(vrc));
|
---|
2813 |
|
---|
2814 | #undef UPDATE_AND_CHECK_ELAPSED_TIME
|
---|
2815 | #undef GET_REMAINING_TIME
|
---|
2816 |
|
---|
2817 | if (RT_FAILURE(vrcGuest))
|
---|
2818 | vrc = VERR_GSTCTL_GUEST_ERROR;
|
---|
2819 |
|
---|
2820 | LogFlowThisFunc(("Loop ended with rc=%Rrc, vrcGuest=%Rrc, waitRes=%RU32\n",
|
---|
2821 | vrc, vrcGuest, waitRes));
|
---|
2822 | if (prcGuest)
|
---|
2823 | *prcGuest = vrcGuest;
|
---|
2824 |
|
---|
2825 | LogFlowFuncLeaveRC(vrc);
|
---|
2826 | return vrc;
|
---|
2827 | }
|
---|
2828 |
|
---|
2829 | /**
|
---|
2830 | * Terminates a guest process tool.
|
---|
2831 | *
|
---|
2832 | * @returns VBox status code.
|
---|
2833 | * @param uTimeoutMS Timeout (in ms) to wait.
|
---|
2834 | * @param prcGuest Where to return the guest error when VERR_GSTCTL_GUEST_ERROR
|
---|
2835 | * was returned. Optional.
|
---|
2836 | */
|
---|
2837 | int GuestProcessTool::terminate(uint32_t uTimeoutMS, int *prcGuest)
|
---|
2838 | {
|
---|
2839 | LogFlowThisFuncEnter();
|
---|
2840 |
|
---|
2841 | int vrc;
|
---|
2842 | if (!pProcess.isNull())
|
---|
2843 | vrc = pProcess->i_terminateProcess(uTimeoutMS, prcGuest);
|
---|
2844 | else
|
---|
2845 | vrc = VERR_NOT_FOUND;
|
---|
2846 |
|
---|
2847 | LogFlowFuncLeaveRC(vrc);
|
---|
2848 | return vrc;
|
---|
2849 | }
|
---|
2850 |
|
---|
2851 | /**
|
---|
2852 | * Converts a toolbox tool's exit code to an IPRT error code.
|
---|
2853 | *
|
---|
2854 | * @returns VBox status code.
|
---|
2855 | * @param startupInfo Startup info of the toolbox tool to lookup error code for.
|
---|
2856 | * @param iExitCode The toolbox tool's exit code to lookup IPRT error for.
|
---|
2857 | */
|
---|
2858 | /* static */
|
---|
2859 | int GuestProcessTool::exitCodeToRc(const GuestProcessStartupInfo &startupInfo, int32_t iExitCode)
|
---|
2860 | {
|
---|
2861 | if (startupInfo.mArguments.size() == 0)
|
---|
2862 | {
|
---|
2863 | AssertFailed();
|
---|
2864 | return VERR_GENERAL_FAILURE; /* Should not happen. */
|
---|
2865 | }
|
---|
2866 |
|
---|
2867 | return exitCodeToRc(startupInfo.mArguments[0].c_str(), iExitCode);
|
---|
2868 | }
|
---|
2869 |
|
---|
2870 | /**
|
---|
2871 | * Converts a toolbox tool's exit code to an IPRT error code.
|
---|
2872 | *
|
---|
2873 | * @returns VBox status code.
|
---|
2874 | * @param pszTool Name of toolbox tool to lookup error code for.
|
---|
2875 | * @param iExitCode The toolbox tool's exit code to lookup IPRT error for.
|
---|
2876 | */
|
---|
2877 | /* static */
|
---|
2878 | int GuestProcessTool::exitCodeToRc(const char *pszTool, int32_t iExitCode)
|
---|
2879 | {
|
---|
2880 | AssertPtrReturn(pszTool, VERR_INVALID_POINTER);
|
---|
2881 |
|
---|
2882 | LogFlowFunc(("%s: %d\n", pszTool, iExitCode));
|
---|
2883 |
|
---|
2884 | if (iExitCode == 0) /* No error? Bail out early. */
|
---|
2885 | return VINF_SUCCESS;
|
---|
2886 |
|
---|
2887 | if (!RTStrICmp(pszTool, VBOXSERVICE_TOOL_CAT))
|
---|
2888 | {
|
---|
2889 | switch (iExitCode)
|
---|
2890 | {
|
---|
2891 | case VBOXSERVICETOOLBOX_CAT_EXITCODE_ACCESS_DENIED: return VERR_ACCESS_DENIED;
|
---|
2892 | case VBOXSERVICETOOLBOX_CAT_EXITCODE_FILE_NOT_FOUND: return VERR_FILE_NOT_FOUND;
|
---|
2893 | case VBOXSERVICETOOLBOX_CAT_EXITCODE_PATH_NOT_FOUND: return VERR_PATH_NOT_FOUND;
|
---|
2894 | case VBOXSERVICETOOLBOX_CAT_EXITCODE_SHARING_VIOLATION: return VERR_SHARING_VIOLATION;
|
---|
2895 | case VBOXSERVICETOOLBOX_CAT_EXITCODE_IS_A_DIRECTORY: return VERR_IS_A_DIRECTORY;
|
---|
2896 | default: break;
|
---|
2897 | }
|
---|
2898 | }
|
---|
2899 | else if (!RTStrICmp(pszTool, VBOXSERVICE_TOOL_LS))
|
---|
2900 | {
|
---|
2901 | switch (iExitCode)
|
---|
2902 | {
|
---|
2903 | /** @todo Handle access denied? */
|
---|
2904 | case RTEXITCODE_FAILURE: return VERR_PATH_NOT_FOUND;
|
---|
2905 | default: break;
|
---|
2906 | }
|
---|
2907 | }
|
---|
2908 | else if (!RTStrICmp(pszTool, VBOXSERVICE_TOOL_STAT))
|
---|
2909 | {
|
---|
2910 | switch (iExitCode)
|
---|
2911 | {
|
---|
2912 | case VBOXSERVICETOOLBOX_STAT_EXITCODE_ACCESS_DENIED: return VERR_ACCESS_DENIED;
|
---|
2913 | case VBOXSERVICETOOLBOX_STAT_EXITCODE_FILE_NOT_FOUND: return VERR_FILE_NOT_FOUND;
|
---|
2914 | case VBOXSERVICETOOLBOX_STAT_EXITCODE_PATH_NOT_FOUND: return VERR_PATH_NOT_FOUND;
|
---|
2915 | case VBOXSERVICETOOLBOX_STAT_EXITCODE_NET_PATH_NOT_FOUND: return VERR_NET_PATH_NOT_FOUND;
|
---|
2916 | default: break;
|
---|
2917 | }
|
---|
2918 | }
|
---|
2919 | else if (!RTStrICmp(pszTool, VBOXSERVICE_TOOL_MKDIR))
|
---|
2920 | {
|
---|
2921 | switch (iExitCode)
|
---|
2922 | {
|
---|
2923 | case RTEXITCODE_FAILURE: return VERR_CANT_CREATE;
|
---|
2924 | default: break;
|
---|
2925 | }
|
---|
2926 | }
|
---|
2927 | else if (!RTStrICmp(pszTool, VBOXSERVICE_TOOL_MKTEMP))
|
---|
2928 | {
|
---|
2929 | switch (iExitCode)
|
---|
2930 | {
|
---|
2931 | case RTEXITCODE_FAILURE: return VERR_CANT_CREATE;
|
---|
2932 | default: break;
|
---|
2933 | }
|
---|
2934 | }
|
---|
2935 | else if (!RTStrICmp(pszTool, VBOXSERVICE_TOOL_RM))
|
---|
2936 | {
|
---|
2937 | switch (iExitCode)
|
---|
2938 | {
|
---|
2939 | case RTEXITCODE_FAILURE: return VERR_FILE_NOT_FOUND;
|
---|
2940 | /** @todo RTPathRmCmd does not yet distinguish between not found and access denied yet. */
|
---|
2941 | default: break;
|
---|
2942 | }
|
---|
2943 | }
|
---|
2944 |
|
---|
2945 | LogFunc(("Warning: Exit code %d not handled for tool '%s', returning VERR_GENERAL_FAILURE\n", iExitCode, pszTool));
|
---|
2946 |
|
---|
2947 | if (iExitCode == RTEXITCODE_SYNTAX)
|
---|
2948 | return VERR_INTERNAL_ERROR_5;
|
---|
2949 | return VERR_GENERAL_FAILURE;
|
---|
2950 | }
|
---|
2951 |
|
---|
2952 | /**
|
---|
2953 | * Returns a stringyfied error of a guest process tool error.
|
---|
2954 | *
|
---|
2955 | * @returns Stringyfied error.
|
---|
2956 | * @param pszTool Toolbox tool name to get stringyfied error for.
|
---|
2957 | * @param guestErrorInfo Guest error info to get stringyfied error for.
|
---|
2958 | */
|
---|
2959 | /* static */
|
---|
2960 | Utf8Str GuestProcessTool::guestErrorToString(const char *pszTool, const GuestErrorInfo &guestErrorInfo)
|
---|
2961 | {
|
---|
2962 | Utf8Str strErr;
|
---|
2963 |
|
---|
2964 | /** @todo pData->u32Flags: int vs. uint32 -- IPRT errors are *negative* !!! */
|
---|
2965 | switch (guestErrorInfo.getRc())
|
---|
2966 | {
|
---|
2967 | case VERR_ACCESS_DENIED:
|
---|
2968 | strErr.printf(tr("Access to \"%s\" denied"), guestErrorInfo.getWhat().c_str());
|
---|
2969 | break;
|
---|
2970 |
|
---|
2971 | case VERR_FILE_NOT_FOUND: /* This is the most likely error. */
|
---|
2972 | RT_FALL_THROUGH();
|
---|
2973 | case VERR_PATH_NOT_FOUND:
|
---|
2974 | strErr.printf(tr("No such file or directory \"%s\""), guestErrorInfo.getWhat().c_str());
|
---|
2975 | break;
|
---|
2976 |
|
---|
2977 | case VERR_INVALID_VM_HANDLE:
|
---|
2978 | strErr.printf(tr("VMM device is not available (is the VM running?)"));
|
---|
2979 | break;
|
---|
2980 |
|
---|
2981 | case VERR_HGCM_SERVICE_NOT_FOUND:
|
---|
2982 | strErr.printf(tr("The guest execution service is not available"));
|
---|
2983 | break;
|
---|
2984 |
|
---|
2985 | case VERR_BAD_EXE_FORMAT:
|
---|
2986 | strErr.printf(tr("The file \"%s\" is not an executable format"), guestErrorInfo.getWhat().c_str());
|
---|
2987 | break;
|
---|
2988 |
|
---|
2989 | case VERR_AUTHENTICATION_FAILURE:
|
---|
2990 | strErr.printf(tr("The user \"%s\" was not able to logon"), guestErrorInfo.getWhat().c_str());
|
---|
2991 | break;
|
---|
2992 |
|
---|
2993 | case VERR_INVALID_NAME:
|
---|
2994 | strErr.printf(tr("The file \"%s\" is an invalid name"), guestErrorInfo.getWhat().c_str());
|
---|
2995 | break;
|
---|
2996 |
|
---|
2997 | case VERR_TIMEOUT:
|
---|
2998 | strErr.printf(tr("The guest did not respond within time"));
|
---|
2999 | break;
|
---|
3000 |
|
---|
3001 | case VERR_CANCELLED:
|
---|
3002 | strErr.printf(tr("The execution operation was canceled"));
|
---|
3003 | break;
|
---|
3004 |
|
---|
3005 | case VERR_GSTCTL_MAX_CID_OBJECTS_REACHED:
|
---|
3006 | strErr.printf(tr("Maximum number of concurrent guest processes has been reached"));
|
---|
3007 | break;
|
---|
3008 |
|
---|
3009 | case VERR_NOT_FOUND:
|
---|
3010 | strErr.printf(tr("The guest execution service is not ready (yet)"));
|
---|
3011 | break;
|
---|
3012 |
|
---|
3013 | default:
|
---|
3014 | strErr.printf(tr("Unhandled error %Rrc for \"%s\" occurred for tool \"%s\" on guest -- please file a bug report"),
|
---|
3015 | guestErrorInfo.getRc(), guestErrorInfo.getWhat().c_str(), pszTool);
|
---|
3016 | break;
|
---|
3017 | }
|
---|
3018 |
|
---|
3019 | return strErr;
|
---|
3020 | }
|
---|
3021 |
|
---|