1 | /* $Id: GuestProcessImpl.cpp 60642 2016-04-22 08:02:20Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox Main - Guest process handling.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2012-2016 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | /**
|
---|
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 | #ifndef VBOX_WITH_GUEST_CONTROL
|
---|
32 | # error "VBOX_WITH_GUEST_CONTROL must defined in this file"
|
---|
33 | #endif
|
---|
34 | #include "GuestProcessImpl.h"
|
---|
35 | #include "GuestSessionImpl.h"
|
---|
36 | #include "GuestCtrlImplPrivate.h"
|
---|
37 | #include "ConsoleImpl.h"
|
---|
38 | #include "VirtualBoxErrorInfoImpl.h"
|
---|
39 |
|
---|
40 | #include "Global.h"
|
---|
41 | #include "AutoCaller.h"
|
---|
42 | #include "VBoxEvents.h"
|
---|
43 | #include "ThreadTask.h"
|
---|
44 |
|
---|
45 | #include <memory> /* For auto_ptr. */
|
---|
46 |
|
---|
47 | #include <iprt/asm.h>
|
---|
48 | #include <iprt/cpp/utils.h> /* For unconst(). */
|
---|
49 | #include <iprt/getopt.h>
|
---|
50 |
|
---|
51 | #include <VBox/com/listeners.h>
|
---|
52 |
|
---|
53 | #include <VBox/com/array.h>
|
---|
54 |
|
---|
55 | #ifdef LOG_GROUP
|
---|
56 | #undef LOG_GROUP
|
---|
57 | #endif
|
---|
58 | #define LOG_GROUP LOG_GROUP_GUEST_CONTROL
|
---|
59 | #include <VBox/log.h>
|
---|
60 |
|
---|
61 |
|
---|
62 | class GuestProcessTask : public ThreadTask
|
---|
63 | {
|
---|
64 | public:
|
---|
65 |
|
---|
66 | GuestProcessTask(GuestProcess *pProcess)
|
---|
67 | : ThreadTask("GenericGuestProcessTask")
|
---|
68 | , mProcess(pProcess)
|
---|
69 | , mRC(VINF_SUCCESS) { }
|
---|
70 |
|
---|
71 | virtual ~GuestProcessTask(void) { }
|
---|
72 |
|
---|
73 | int i_rc(void) const { return mRC; }
|
---|
74 | bool i_isOk(void) const { return RT_SUCCESS(mRC); }
|
---|
75 | const ComObjPtr<GuestProcess> &i_process(void) const { return mProcess; }
|
---|
76 |
|
---|
77 | protected:
|
---|
78 |
|
---|
79 | const ComObjPtr<GuestProcess> mProcess;
|
---|
80 | int mRC;
|
---|
81 | };
|
---|
82 |
|
---|
83 | class GuestProcessStartTask : public GuestProcessTask
|
---|
84 | {
|
---|
85 | public:
|
---|
86 |
|
---|
87 | GuestProcessStartTask(GuestProcess *pProcess)
|
---|
88 | : GuestProcessTask(pProcess)
|
---|
89 | {
|
---|
90 | m_strTaskName = "gctlPrcStart";
|
---|
91 | }
|
---|
92 |
|
---|
93 | void handler()
|
---|
94 | {
|
---|
95 | int vrc = GuestProcess::i_startProcessThread(NULL, this);
|
---|
96 | }
|
---|
97 | };
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Internal listener class to serve events in an
|
---|
101 | * active manner, e.g. without polling delays.
|
---|
102 | */
|
---|
103 | class GuestProcessListener
|
---|
104 | {
|
---|
105 | public:
|
---|
106 |
|
---|
107 | GuestProcessListener(void)
|
---|
108 | {
|
---|
109 | }
|
---|
110 |
|
---|
111 | HRESULT init(GuestProcess *pProcess)
|
---|
112 | {
|
---|
113 | AssertPtrReturn(pProcess, E_POINTER);
|
---|
114 | mProcess = pProcess;
|
---|
115 | return S_OK;
|
---|
116 | }
|
---|
117 |
|
---|
118 | void uninit(void)
|
---|
119 | {
|
---|
120 | mProcess = NULL;
|
---|
121 | }
|
---|
122 |
|
---|
123 | STDMETHOD(HandleEvent)(VBoxEventType_T aType, IEvent *aEvent)
|
---|
124 | {
|
---|
125 | switch (aType)
|
---|
126 | {
|
---|
127 | case VBoxEventType_OnGuestProcessStateChanged:
|
---|
128 | case VBoxEventType_OnGuestProcessInputNotify:
|
---|
129 | case VBoxEventType_OnGuestProcessOutput:
|
---|
130 | {
|
---|
131 | AssertPtrReturn(mProcess, E_POINTER);
|
---|
132 | int rc2 = mProcess->signalWaitEvent(aType, aEvent);
|
---|
133 | #ifdef DEBUG
|
---|
134 | LogFlowThisFunc(("Signalling events of type=%RU32, pProcess=%p resulted in rc=%Rrc\n",
|
---|
135 | aType, &mProcess, rc2));
|
---|
136 | #endif
|
---|
137 | break;
|
---|
138 | }
|
---|
139 |
|
---|
140 | default:
|
---|
141 | AssertMsgFailed(("Unhandled event %RU32\n", aType));
|
---|
142 | break;
|
---|
143 | }
|
---|
144 |
|
---|
145 | return S_OK;
|
---|
146 | }
|
---|
147 |
|
---|
148 | private:
|
---|
149 |
|
---|
150 | GuestProcess *mProcess;
|
---|
151 | };
|
---|
152 | typedef ListenerImpl<GuestProcessListener, GuestProcess*> GuestProcessListenerImpl;
|
---|
153 |
|
---|
154 | VBOX_LISTENER_DECLARE(GuestProcessListenerImpl)
|
---|
155 |
|
---|
156 | // constructor / destructor
|
---|
157 | /////////////////////////////////////////////////////////////////////////////
|
---|
158 |
|
---|
159 | DEFINE_EMPTY_CTOR_DTOR(GuestProcess)
|
---|
160 |
|
---|
161 | HRESULT GuestProcess::FinalConstruct(void)
|
---|
162 | {
|
---|
163 | LogFlowThisFuncEnter();
|
---|
164 | return BaseFinalConstruct();
|
---|
165 | }
|
---|
166 |
|
---|
167 | void GuestProcess::FinalRelease(void)
|
---|
168 | {
|
---|
169 | LogFlowThisFuncEnter();
|
---|
170 | uninit();
|
---|
171 | BaseFinalRelease();
|
---|
172 | LogFlowThisFuncLeave();
|
---|
173 | }
|
---|
174 |
|
---|
175 | // public initializer/uninitializer for internal purposes only
|
---|
176 | /////////////////////////////////////////////////////////////////////////////
|
---|
177 |
|
---|
178 | int GuestProcess::init(Console *aConsole, GuestSession *aSession, ULONG aProcessID,
|
---|
179 | const GuestProcessStartupInfo &aProcInfo, const GuestEnvironment *pBaseEnv)
|
---|
180 | {
|
---|
181 | LogFlowThisFunc(("aConsole=%p, aSession=%p, aProcessID=%RU32 pBaseEnv=%p\n",
|
---|
182 | aConsole, aSession, aProcessID, pBaseEnv));
|
---|
183 |
|
---|
184 | AssertPtrReturn(aConsole, VERR_INVALID_POINTER);
|
---|
185 | AssertPtrReturn(aSession, VERR_INVALID_POINTER);
|
---|
186 |
|
---|
187 | /* Enclose the state transition NotReady->InInit->Ready. */
|
---|
188 | AutoInitSpan autoInitSpan(this);
|
---|
189 | AssertReturn(autoInitSpan.isOk(), VERR_OBJECT_DESTROYED);
|
---|
190 |
|
---|
191 | HRESULT hr;
|
---|
192 |
|
---|
193 | int vrc = bindToSession(aConsole, aSession, aProcessID /* Object ID */);
|
---|
194 | if (RT_SUCCESS(vrc))
|
---|
195 | {
|
---|
196 | hr = unconst(mEventSource).createObject();
|
---|
197 | if (FAILED(hr))
|
---|
198 | vrc = VERR_NO_MEMORY;
|
---|
199 | else
|
---|
200 | {
|
---|
201 | hr = mEventSource->init();
|
---|
202 | if (FAILED(hr))
|
---|
203 | vrc = VERR_COM_UNEXPECTED;
|
---|
204 | }
|
---|
205 | }
|
---|
206 |
|
---|
207 | if (RT_SUCCESS(vrc))
|
---|
208 | {
|
---|
209 | try
|
---|
210 | {
|
---|
211 | GuestProcessListener *pListener = new GuestProcessListener();
|
---|
212 | ComObjPtr<GuestProcessListenerImpl> thisListener;
|
---|
213 | hr = thisListener.createObject();
|
---|
214 | if (SUCCEEDED(hr))
|
---|
215 | hr = thisListener->init(pListener, this);
|
---|
216 |
|
---|
217 | if (SUCCEEDED(hr))
|
---|
218 | {
|
---|
219 | com::SafeArray <VBoxEventType_T> eventTypes;
|
---|
220 | eventTypes.push_back(VBoxEventType_OnGuestProcessStateChanged);
|
---|
221 | eventTypes.push_back(VBoxEventType_OnGuestProcessInputNotify);
|
---|
222 | eventTypes.push_back(VBoxEventType_OnGuestProcessOutput);
|
---|
223 | hr = mEventSource->RegisterListener(thisListener,
|
---|
224 | ComSafeArrayAsInParam(eventTypes),
|
---|
225 | TRUE /* Active listener */);
|
---|
226 | if (SUCCEEDED(hr))
|
---|
227 | {
|
---|
228 | vrc = baseInit();
|
---|
229 | if (RT_SUCCESS(vrc))
|
---|
230 | {
|
---|
231 | mLocalListener = thisListener;
|
---|
232 | }
|
---|
233 | }
|
---|
234 | else
|
---|
235 | vrc = VERR_COM_UNEXPECTED;
|
---|
236 | }
|
---|
237 | else
|
---|
238 | vrc = VERR_COM_UNEXPECTED;
|
---|
239 | }
|
---|
240 | catch(std::bad_alloc &)
|
---|
241 | {
|
---|
242 | vrc = VERR_NO_MEMORY;
|
---|
243 | }
|
---|
244 | }
|
---|
245 |
|
---|
246 | if (RT_SUCCESS(vrc))
|
---|
247 | {
|
---|
248 | mData.mProcess = aProcInfo;
|
---|
249 | mData.mpSessionBaseEnv = pBaseEnv;
|
---|
250 | if (pBaseEnv)
|
---|
251 | pBaseEnv->retainConst();
|
---|
252 | mData.mExitCode = 0;
|
---|
253 | mData.mPID = 0;
|
---|
254 | mData.mLastError = VINF_SUCCESS;
|
---|
255 | mData.mStatus = ProcessStatus_Undefined;
|
---|
256 | /* Everything else will be set by the actual starting routine. */
|
---|
257 |
|
---|
258 | /* Confirm a successful initialization when it's the case. */
|
---|
259 | autoInitSpan.setSucceeded();
|
---|
260 |
|
---|
261 | return vrc;
|
---|
262 | }
|
---|
263 |
|
---|
264 | autoInitSpan.setFailed();
|
---|
265 | return vrc;
|
---|
266 | }
|
---|
267 |
|
---|
268 | /**
|
---|
269 | * Uninitializes the instance.
|
---|
270 | * Called from FinalRelease() or IGuestSession::uninit().
|
---|
271 | */
|
---|
272 | void GuestProcess::uninit(void)
|
---|
273 | {
|
---|
274 | /* Enclose the state transition Ready->InUninit->NotReady. */
|
---|
275 | AutoUninitSpan autoUninitSpan(this);
|
---|
276 | if (autoUninitSpan.uninitDone())
|
---|
277 | return;
|
---|
278 |
|
---|
279 | LogFlowThisFunc(("mExe=%s, PID=%RU32\n", mData.mProcess.mExecutable.c_str(), mData.mPID));
|
---|
280 |
|
---|
281 | /* Terminate process if not already done yet. */
|
---|
282 | int guestRc = VINF_SUCCESS;
|
---|
283 | int vrc = i_terminateProcess(30 * 1000, &guestRc); /** @todo Make timeouts configurable. */
|
---|
284 | /* Note: Don't return here yet; first uninit all other stuff in
|
---|
285 | * case of failure. */
|
---|
286 |
|
---|
287 | if (mData.mpSessionBaseEnv)
|
---|
288 | {
|
---|
289 | mData.mpSessionBaseEnv->releaseConst();
|
---|
290 | mData.mpSessionBaseEnv = NULL;
|
---|
291 | }
|
---|
292 |
|
---|
293 | baseUninit();
|
---|
294 |
|
---|
295 | LogFlowThisFunc(("Returning rc=%Rrc, guestRc=%Rrc\n",
|
---|
296 | vrc, guestRc));
|
---|
297 | }
|
---|
298 |
|
---|
299 | // implementation of public getters/setters for attributes
|
---|
300 | /////////////////////////////////////////////////////////////////////////////
|
---|
301 | HRESULT GuestProcess::getArguments(std::vector<com::Utf8Str> &aArguments)
|
---|
302 | {
|
---|
303 | LogFlowThisFuncEnter();
|
---|
304 |
|
---|
305 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
306 | aArguments = mData.mProcess.mArguments;
|
---|
307 | return S_OK;
|
---|
308 | }
|
---|
309 |
|
---|
310 | HRESULT GuestProcess::getEnvironment(std::vector<com::Utf8Str> &aEnvironment)
|
---|
311 | {
|
---|
312 | #ifndef VBOX_WTIH_GUEST_CONTROL
|
---|
313 | ReturnComNotImplemented();
|
---|
314 | #else
|
---|
315 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS); /* (Paranoia since both environment objects are immutable.) */
|
---|
316 | HRESULT hrc;
|
---|
317 | if (mData.mpSessionBaseEnv)
|
---|
318 | {
|
---|
319 | int vrc;
|
---|
320 | if (mData.mProcess.mEnvironmentChanges.count() == 0)
|
---|
321 | vrc = mData.mpSessionBaseEnv->queryPutEnvArray(&aEnvironment);
|
---|
322 | else
|
---|
323 | {
|
---|
324 | GuestEnvironment TmpEnv;
|
---|
325 | vrc = TmpEnv.copy(*mData.mpSessionBaseEnv);
|
---|
326 | if (RT_SUCCESS(vrc))
|
---|
327 | {
|
---|
328 | vrc = TmpEnv.applyChanges(mData.mProcess.mEnvironmentChanges);
|
---|
329 | if (RT_SUCCESS(rc))
|
---|
330 | vrc = TmpEnv.queryPutEnvArray(&aEnvironment);
|
---|
331 | }
|
---|
332 | }
|
---|
333 | hrc = Global::vboxStatusCodeToCOM(vrc);
|
---|
334 | }
|
---|
335 | else
|
---|
336 | hrc = setError(VBOX_E_NOT_SUPPORTED, tr("The base environment feature is not supported by the guest additions"));
|
---|
337 | LogFlowThisFuncLeave();
|
---|
338 | return hrc;
|
---|
339 | #endif
|
---|
340 | }
|
---|
341 |
|
---|
342 | HRESULT GuestProcess::getEventSource(ComPtr<IEventSource> &aEventSource)
|
---|
343 | {
|
---|
344 | LogFlowThisFuncEnter();
|
---|
345 |
|
---|
346 | // no need to lock - lifetime constant
|
---|
347 | mEventSource.queryInterfaceTo(aEventSource.asOutParam());
|
---|
348 |
|
---|
349 | LogFlowThisFuncLeave();
|
---|
350 | return S_OK;
|
---|
351 | }
|
---|
352 |
|
---|
353 | HRESULT GuestProcess::getExecutablePath(com::Utf8Str &aExecutablePath)
|
---|
354 | {
|
---|
355 | LogFlowThisFuncEnter();
|
---|
356 |
|
---|
357 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
358 |
|
---|
359 | aExecutablePath = mData.mProcess.mExecutable;
|
---|
360 |
|
---|
361 | return S_OK;
|
---|
362 | }
|
---|
363 |
|
---|
364 | HRESULT GuestProcess::getExitCode(LONG *aExitCode)
|
---|
365 | {
|
---|
366 | LogFlowThisFuncEnter();
|
---|
367 |
|
---|
368 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
369 |
|
---|
370 | *aExitCode = mData.mExitCode;
|
---|
371 |
|
---|
372 | return S_OK;
|
---|
373 | }
|
---|
374 |
|
---|
375 | HRESULT GuestProcess::getName(com::Utf8Str &aName)
|
---|
376 | {
|
---|
377 | LogFlowThisFuncEnter();
|
---|
378 |
|
---|
379 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
380 |
|
---|
381 | aName = mData.mProcess.mName;
|
---|
382 |
|
---|
383 | return S_OK;
|
---|
384 | }
|
---|
385 |
|
---|
386 | HRESULT GuestProcess::getPID(ULONG *aPID)
|
---|
387 | {
|
---|
388 | LogFlowThisFuncEnter();
|
---|
389 |
|
---|
390 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
391 |
|
---|
392 | *aPID = mData.mPID;
|
---|
393 |
|
---|
394 | return S_OK;
|
---|
395 | }
|
---|
396 |
|
---|
397 | HRESULT GuestProcess::getStatus(ProcessStatus_T *aStatus)
|
---|
398 | {
|
---|
399 | LogFlowThisFuncEnter();
|
---|
400 |
|
---|
401 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
402 |
|
---|
403 | *aStatus = mData.mStatus;
|
---|
404 |
|
---|
405 | return S_OK;
|
---|
406 | }
|
---|
407 |
|
---|
408 | // private methods
|
---|
409 | /////////////////////////////////////////////////////////////////////////////
|
---|
410 |
|
---|
411 | int GuestProcess::i_callbackDispatcher(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCb)
|
---|
412 | {
|
---|
413 | AssertPtrReturn(pCbCtx, VERR_INVALID_POINTER);
|
---|
414 | AssertPtrReturn(pSvcCb, VERR_INVALID_POINTER);
|
---|
415 | #ifdef DEBUG
|
---|
416 | LogFlowThisFunc(("uPID=%RU32, uContextID=%RU32, uFunction=%RU32, pSvcCb=%p\n",
|
---|
417 | mData.mPID, pCbCtx->uContextID, pCbCtx->uFunction, pSvcCb));
|
---|
418 | #endif
|
---|
419 |
|
---|
420 | int vrc;
|
---|
421 | switch (pCbCtx->uFunction)
|
---|
422 | {
|
---|
423 | case GUEST_DISCONNECTED:
|
---|
424 | {
|
---|
425 | vrc = i_onGuestDisconnected(pCbCtx, pSvcCb);
|
---|
426 | break;
|
---|
427 | }
|
---|
428 |
|
---|
429 | case GUEST_EXEC_STATUS:
|
---|
430 | {
|
---|
431 | vrc = i_onProcessStatusChange(pCbCtx, pSvcCb);
|
---|
432 | break;
|
---|
433 | }
|
---|
434 |
|
---|
435 | case GUEST_EXEC_OUTPUT:
|
---|
436 | {
|
---|
437 | vrc = i_onProcessOutput(pCbCtx, pSvcCb);
|
---|
438 | break;
|
---|
439 | }
|
---|
440 |
|
---|
441 | case GUEST_EXEC_INPUT_STATUS:
|
---|
442 | {
|
---|
443 | vrc = i_onProcessInputStatus(pCbCtx, pSvcCb);
|
---|
444 | break;
|
---|
445 | }
|
---|
446 |
|
---|
447 | default:
|
---|
448 | /* Silently ignore not implemented functions. */
|
---|
449 | vrc = VERR_NOT_SUPPORTED;
|
---|
450 | break;
|
---|
451 | }
|
---|
452 |
|
---|
453 | #ifdef DEBUG
|
---|
454 | LogFlowFuncLeaveRC(vrc);
|
---|
455 | #endif
|
---|
456 | return vrc;
|
---|
457 | }
|
---|
458 |
|
---|
459 | /**
|
---|
460 | * Checks if the current assigned PID matches another PID (from a callback).
|
---|
461 | *
|
---|
462 | * In protocol v1 we don't have the possibility to terminate/kill
|
---|
463 | * processes so it can happen that a formerly started process A
|
---|
464 | * (which has the context ID 0 (session=0, process=0, count=0) will
|
---|
465 | * send a delayed message to the host if this process has already
|
---|
466 | * been discarded there and the same context ID was reused by
|
---|
467 | * a process B. Process B in turn then has a different guest PID.
|
---|
468 | *
|
---|
469 | * Note: This also can happen when restoring from a saved state which
|
---|
470 | * had a guest process running.
|
---|
471 | *
|
---|
472 | * @return IPRT status code.
|
---|
473 | * @param uPID PID to check.
|
---|
474 | */
|
---|
475 | inline int GuestProcess::i_checkPID(uint32_t uPID)
|
---|
476 | {
|
---|
477 | int rc = VINF_SUCCESS;
|
---|
478 |
|
---|
479 | /* Was there a PID assigned yet? */
|
---|
480 | if (mData.mPID)
|
---|
481 | {
|
---|
482 | if (RT_UNLIKELY(mData.mPID != uPID))
|
---|
483 | {
|
---|
484 | LogFlowFunc(("Stale guest process (PID=%RU32) sent data to a newly started process (pProcesS=%p, PID=%RU32, status=%RU32)\n",
|
---|
485 | uPID, this, mData.mPID, mData.mStatus));
|
---|
486 | rc = VERR_NOT_FOUND;
|
---|
487 | }
|
---|
488 | }
|
---|
489 |
|
---|
490 | return rc;
|
---|
491 | }
|
---|
492 |
|
---|
493 | /* static */
|
---|
494 | Utf8Str GuestProcess::i_guestErrorToString(int guestRc)
|
---|
495 | {
|
---|
496 | Utf8Str strError;
|
---|
497 |
|
---|
498 | /** @todo pData->u32Flags: int vs. uint32 -- IPRT errors are *negative* !!! */
|
---|
499 | switch (guestRc)
|
---|
500 | {
|
---|
501 | case VERR_FILE_NOT_FOUND: /* This is the most likely error. */
|
---|
502 | strError += Utf8StrFmt(tr("The specified file was not found on guest"));
|
---|
503 | break;
|
---|
504 |
|
---|
505 | case VERR_INVALID_VM_HANDLE:
|
---|
506 | strError += Utf8StrFmt(tr("VMM device is not available (is the VM running?)"));
|
---|
507 | break;
|
---|
508 |
|
---|
509 | case VERR_HGCM_SERVICE_NOT_FOUND:
|
---|
510 | strError += Utf8StrFmt(tr("The guest execution service is not available"));
|
---|
511 | break;
|
---|
512 |
|
---|
513 | case VERR_PATH_NOT_FOUND:
|
---|
514 | strError += Utf8StrFmt(tr("Could not resolve path to specified file was not found on guest"));
|
---|
515 | break;
|
---|
516 |
|
---|
517 | case VERR_BAD_EXE_FORMAT:
|
---|
518 | strError += Utf8StrFmt(tr("The specified file is not an executable format on guest"));
|
---|
519 | break;
|
---|
520 |
|
---|
521 | case VERR_AUTHENTICATION_FAILURE:
|
---|
522 | strError += Utf8StrFmt(tr("The specified user was not able to logon on guest"));
|
---|
523 | break;
|
---|
524 |
|
---|
525 | case VERR_INVALID_NAME:
|
---|
526 | strError += Utf8StrFmt(tr("The specified file is an invalid name"));
|
---|
527 | break;
|
---|
528 |
|
---|
529 | case VERR_TIMEOUT:
|
---|
530 | strError += Utf8StrFmt(tr("The guest did not respond within time"));
|
---|
531 | break;
|
---|
532 |
|
---|
533 | case VERR_CANCELLED:
|
---|
534 | strError += Utf8StrFmt(tr("The execution operation was canceled"));
|
---|
535 | break;
|
---|
536 |
|
---|
537 | case VERR_PERMISSION_DENIED: /** @todo r=bird: This is probably completely and utterly misleading. VERR_AUTHENTICATION_FAILURE could have this message. */
|
---|
538 | strError += Utf8StrFmt(tr("Invalid user/password credentials"));
|
---|
539 | break;
|
---|
540 |
|
---|
541 | case VERR_MAX_PROCS_REACHED:
|
---|
542 | strError += Utf8StrFmt(tr("Maximum number of concurrent guest processes has been reached"));
|
---|
543 | break;
|
---|
544 |
|
---|
545 | case VERR_NOT_FOUND:
|
---|
546 | strError += Utf8StrFmt(tr("The guest execution service is not ready (yet)"));
|
---|
547 | break;
|
---|
548 |
|
---|
549 | default:
|
---|
550 | strError += Utf8StrFmt("%Rrc", guestRc);
|
---|
551 | break;
|
---|
552 | }
|
---|
553 |
|
---|
554 | return strError;
|
---|
555 | }
|
---|
556 |
|
---|
557 | inline bool GuestProcess::i_isAlive(void)
|
---|
558 | {
|
---|
559 | return ( mData.mStatus == ProcessStatus_Started
|
---|
560 | || mData.mStatus == ProcessStatus_Paused
|
---|
561 | || mData.mStatus == ProcessStatus_Terminating);
|
---|
562 | }
|
---|
563 |
|
---|
564 | inline bool GuestProcess::i_hasEnded(void)
|
---|
565 | {
|
---|
566 | return ( mData.mStatus == ProcessStatus_TerminatedNormally
|
---|
567 | || mData.mStatus == ProcessStatus_TerminatedSignal
|
---|
568 | || mData.mStatus == ProcessStatus_TerminatedAbnormally
|
---|
569 | || mData.mStatus == ProcessStatus_TimedOutKilled
|
---|
570 | || mData.mStatus == ProcessStatus_TimedOutAbnormally
|
---|
571 | || mData.mStatus == ProcessStatus_Down
|
---|
572 | || mData.mStatus == ProcessStatus_Error);
|
---|
573 | }
|
---|
574 |
|
---|
575 | int GuestProcess::i_onGuestDisconnected(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData)
|
---|
576 | {
|
---|
577 | AssertPtrReturn(pCbCtx, VERR_INVALID_POINTER);
|
---|
578 | AssertPtrReturn(pSvcCbData, VERR_INVALID_POINTER);
|
---|
579 |
|
---|
580 | int vrc = i_setProcessStatus(ProcessStatus_Down, VINF_SUCCESS);
|
---|
581 |
|
---|
582 | LogFlowFuncLeaveRC(vrc);
|
---|
583 | return vrc;
|
---|
584 | }
|
---|
585 |
|
---|
586 | int GuestProcess::i_onProcessInputStatus(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData)
|
---|
587 | {
|
---|
588 | AssertPtrReturn(pCbCtx, VERR_INVALID_POINTER);
|
---|
589 | AssertPtrReturn(pSvcCbData, VERR_INVALID_POINTER);
|
---|
590 | /* pCallback is optional. */
|
---|
591 |
|
---|
592 | if (pSvcCbData->mParms < 5)
|
---|
593 | return VERR_INVALID_PARAMETER;
|
---|
594 |
|
---|
595 | CALLBACKDATA_PROC_INPUT dataCb;
|
---|
596 | /* pSvcCb->mpaParms[0] always contains the context ID. */
|
---|
597 | int vrc = pSvcCbData->mpaParms[1].getUInt32(&dataCb.uPID);
|
---|
598 | AssertRCReturn(vrc, vrc);
|
---|
599 | vrc = pSvcCbData->mpaParms[2].getUInt32(&dataCb.uStatus);
|
---|
600 | AssertRCReturn(vrc, vrc);
|
---|
601 | vrc = pSvcCbData->mpaParms[3].getUInt32(&dataCb.uFlags);
|
---|
602 | AssertRCReturn(vrc, vrc);
|
---|
603 | vrc = pSvcCbData->mpaParms[4].getUInt32(&dataCb.uProcessed);
|
---|
604 | AssertRCReturn(vrc, vrc);
|
---|
605 |
|
---|
606 | LogFlowThisFunc(("uPID=%RU32, uStatus=%RU32, uFlags=%RI32, cbProcessed=%RU32\n",
|
---|
607 | dataCb.uPID, dataCb.uStatus, dataCb.uFlags, dataCb.uProcessed));
|
---|
608 |
|
---|
609 | vrc = i_checkPID(dataCb.uPID);
|
---|
610 | if (RT_SUCCESS(vrc))
|
---|
611 | {
|
---|
612 | ProcessInputStatus_T inputStatus = ProcessInputStatus_Undefined;
|
---|
613 | switch (dataCb.uStatus)
|
---|
614 | {
|
---|
615 | case INPUT_STS_WRITTEN:
|
---|
616 | inputStatus = ProcessInputStatus_Written;
|
---|
617 | break;
|
---|
618 | case INPUT_STS_ERROR:
|
---|
619 | inputStatus = ProcessInputStatus_Broken;
|
---|
620 | break;
|
---|
621 | case INPUT_STS_TERMINATED:
|
---|
622 | inputStatus = ProcessInputStatus_Broken;
|
---|
623 | break;
|
---|
624 | case INPUT_STS_OVERFLOW:
|
---|
625 | inputStatus = ProcessInputStatus_Overflow;
|
---|
626 | break;
|
---|
627 | case INPUT_STS_UNDEFINED:
|
---|
628 | /* Fall through is intentional. */
|
---|
629 | default:
|
---|
630 | AssertMsg(!dataCb.uProcessed, ("Processed data is not 0 in undefined input state\n"));
|
---|
631 | break;
|
---|
632 | }
|
---|
633 |
|
---|
634 | if (inputStatus != ProcessInputStatus_Undefined)
|
---|
635 | {
|
---|
636 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
637 |
|
---|
638 | /* Copy over necessary data before releasing lock again. */
|
---|
639 | uint32_t uPID = mData.mPID;
|
---|
640 | /** @todo Also handle mSession? */
|
---|
641 |
|
---|
642 | alock.release(); /* Release lock before firing off event. */
|
---|
643 |
|
---|
644 | fireGuestProcessInputNotifyEvent(mEventSource, mSession, this,
|
---|
645 | uPID, 0 /* StdIn */, dataCb.uProcessed, inputStatus);
|
---|
646 | }
|
---|
647 | }
|
---|
648 |
|
---|
649 | LogFlowFuncLeaveRC(vrc);
|
---|
650 | return vrc;
|
---|
651 | }
|
---|
652 |
|
---|
653 | int GuestProcess::i_onProcessNotifyIO(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData)
|
---|
654 | {
|
---|
655 | AssertPtrReturn(pCbCtx, VERR_INVALID_POINTER);
|
---|
656 | AssertPtrReturn(pSvcCbData, VERR_INVALID_POINTER);
|
---|
657 |
|
---|
658 | return VERR_NOT_IMPLEMENTED;
|
---|
659 | }
|
---|
660 |
|
---|
661 | int GuestProcess::i_onProcessStatusChange(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData)
|
---|
662 | {
|
---|
663 | AssertPtrReturn(pCbCtx, VERR_INVALID_POINTER);
|
---|
664 | AssertPtrReturn(pSvcCbData, VERR_INVALID_POINTER);
|
---|
665 |
|
---|
666 | if (pSvcCbData->mParms < 5)
|
---|
667 | return VERR_INVALID_PARAMETER;
|
---|
668 |
|
---|
669 | CALLBACKDATA_PROC_STATUS dataCb;
|
---|
670 | /* pSvcCb->mpaParms[0] always contains the context ID. */
|
---|
671 | int vrc = pSvcCbData->mpaParms[1].getUInt32(&dataCb.uPID);
|
---|
672 | AssertRCReturn(vrc, vrc);
|
---|
673 | vrc = pSvcCbData->mpaParms[2].getUInt32(&dataCb.uStatus);
|
---|
674 | AssertRCReturn(vrc, vrc);
|
---|
675 | vrc = pSvcCbData->mpaParms[3].getUInt32(&dataCb.uFlags);
|
---|
676 | AssertRCReturn(vrc, vrc);
|
---|
677 | vrc = pSvcCbData->mpaParms[4].getPointer(&dataCb.pvData, &dataCb.cbData);
|
---|
678 | AssertRCReturn(vrc, vrc);
|
---|
679 |
|
---|
680 | LogFlowThisFunc(("uPID=%RU32, uStatus=%RU32, uFlags=%RU32\n",
|
---|
681 | dataCb.uPID, dataCb.uStatus, dataCb.uFlags));
|
---|
682 |
|
---|
683 | vrc = i_checkPID(dataCb.uPID);
|
---|
684 | if (RT_SUCCESS(vrc))
|
---|
685 | {
|
---|
686 | ProcessStatus_T procStatus = ProcessStatus_Undefined;
|
---|
687 | int procRc = VINF_SUCCESS;
|
---|
688 |
|
---|
689 | switch (dataCb.uStatus)
|
---|
690 | {
|
---|
691 | case PROC_STS_STARTED:
|
---|
692 | {
|
---|
693 | procStatus = ProcessStatus_Started;
|
---|
694 |
|
---|
695 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
696 | mData.mPID = dataCb.uPID; /* Set the process PID. */
|
---|
697 | break;
|
---|
698 | }
|
---|
699 |
|
---|
700 | case PROC_STS_TEN:
|
---|
701 | {
|
---|
702 | procStatus = ProcessStatus_TerminatedNormally;
|
---|
703 |
|
---|
704 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
705 | mData.mExitCode = dataCb.uFlags; /* Contains the exit code. */
|
---|
706 | break;
|
---|
707 | }
|
---|
708 |
|
---|
709 | case PROC_STS_TES:
|
---|
710 | {
|
---|
711 | procStatus = ProcessStatus_TerminatedSignal;
|
---|
712 |
|
---|
713 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
714 | mData.mExitCode = dataCb.uFlags; /* Contains the signal. */
|
---|
715 | break;
|
---|
716 | }
|
---|
717 |
|
---|
718 | case PROC_STS_TEA:
|
---|
719 | {
|
---|
720 | procStatus = ProcessStatus_TerminatedAbnormally;
|
---|
721 | break;
|
---|
722 | }
|
---|
723 |
|
---|
724 | case PROC_STS_TOK:
|
---|
725 | {
|
---|
726 | procStatus = ProcessStatus_TimedOutKilled;
|
---|
727 | break;
|
---|
728 | }
|
---|
729 |
|
---|
730 | case PROC_STS_TOA:
|
---|
731 | {
|
---|
732 | procStatus = ProcessStatus_TimedOutAbnormally;
|
---|
733 | break;
|
---|
734 | }
|
---|
735 |
|
---|
736 | case PROC_STS_DWN:
|
---|
737 | {
|
---|
738 | procStatus = ProcessStatus_Down;
|
---|
739 | break;
|
---|
740 | }
|
---|
741 |
|
---|
742 | case PROC_STS_ERROR:
|
---|
743 | {
|
---|
744 | procRc = dataCb.uFlags; /* mFlags contains the IPRT error sent from the guest. */
|
---|
745 | procStatus = ProcessStatus_Error;
|
---|
746 | break;
|
---|
747 | }
|
---|
748 |
|
---|
749 | case PROC_STS_UNDEFINED:
|
---|
750 | default:
|
---|
751 | {
|
---|
752 | /* Silently skip this request. */
|
---|
753 | procStatus = ProcessStatus_Undefined;
|
---|
754 | break;
|
---|
755 | }
|
---|
756 | }
|
---|
757 |
|
---|
758 | LogFlowThisFunc(("Got rc=%Rrc, procSts=%RU32, procRc=%Rrc\n",
|
---|
759 | vrc, procStatus, procRc));
|
---|
760 |
|
---|
761 | /* Set the process status. */
|
---|
762 | int rc2 = i_setProcessStatus(procStatus, procRc);
|
---|
763 | if (RT_SUCCESS(vrc))
|
---|
764 | vrc = rc2;
|
---|
765 | }
|
---|
766 |
|
---|
767 | LogFlowFuncLeaveRC(vrc);
|
---|
768 | return vrc;
|
---|
769 | }
|
---|
770 |
|
---|
771 | int GuestProcess::i_onProcessOutput(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData)
|
---|
772 | {
|
---|
773 | AssertPtrReturn(pSvcCbData, VERR_INVALID_POINTER);
|
---|
774 |
|
---|
775 | if (pSvcCbData->mParms < 5)
|
---|
776 | return VERR_INVALID_PARAMETER;
|
---|
777 |
|
---|
778 | CALLBACKDATA_PROC_OUTPUT dataCb;
|
---|
779 | /* pSvcCb->mpaParms[0] always contains the context ID. */
|
---|
780 | int vrc = pSvcCbData->mpaParms[1].getUInt32(&dataCb.uPID);
|
---|
781 | AssertRCReturn(vrc, vrc);
|
---|
782 | vrc = pSvcCbData->mpaParms[2].getUInt32(&dataCb.uHandle);
|
---|
783 | AssertRCReturn(vrc, vrc);
|
---|
784 | vrc = pSvcCbData->mpaParms[3].getUInt32(&dataCb.uFlags);
|
---|
785 | AssertRCReturn(vrc, vrc);
|
---|
786 | vrc = pSvcCbData->mpaParms[4].getPointer(&dataCb.pvData, &dataCb.cbData);
|
---|
787 | AssertRCReturn(vrc, vrc);
|
---|
788 |
|
---|
789 | LogFlowThisFunc(("uPID=%RU32, uHandle=%RU32, uFlags=%RI32, pvData=%p, cbData=%RU32\n",
|
---|
790 | dataCb.uPID, dataCb.uHandle, dataCb.uFlags, dataCb.pvData, dataCb.cbData));
|
---|
791 |
|
---|
792 | vrc = i_checkPID(dataCb.uPID);
|
---|
793 | if (RT_SUCCESS(vrc))
|
---|
794 | {
|
---|
795 | com::SafeArray<BYTE> data((size_t)dataCb.cbData);
|
---|
796 | if (dataCb.cbData)
|
---|
797 | data.initFrom((BYTE*)dataCb.pvData, dataCb.cbData);
|
---|
798 |
|
---|
799 | fireGuestProcessOutputEvent(mEventSource, mSession, this,
|
---|
800 | mData.mPID, dataCb.uHandle, dataCb.cbData, ComSafeArrayAsInParam(data));
|
---|
801 | }
|
---|
802 |
|
---|
803 | LogFlowFuncLeaveRC(vrc);
|
---|
804 | return vrc;
|
---|
805 | }
|
---|
806 |
|
---|
807 | /**
|
---|
808 | * Called by IGuestSession right before this process gets
|
---|
809 | * removed from the public process list.
|
---|
810 | */
|
---|
811 | int GuestProcess::i_onRemove(void)
|
---|
812 | {
|
---|
813 | LogFlowThisFuncEnter();
|
---|
814 |
|
---|
815 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
816 |
|
---|
817 | int vrc = VINF_SUCCESS;
|
---|
818 |
|
---|
819 | /*
|
---|
820 | * Note: The event source stuff holds references to this object,
|
---|
821 | * so make sure that this is cleaned up *before* calling uninit().
|
---|
822 | */
|
---|
823 | if (!mEventSource.isNull())
|
---|
824 | {
|
---|
825 | mEventSource->UnregisterListener(mLocalListener);
|
---|
826 |
|
---|
827 | mLocalListener.setNull();
|
---|
828 | unconst(mEventSource).setNull();
|
---|
829 | }
|
---|
830 |
|
---|
831 | LogFlowFuncLeaveRC(vrc);
|
---|
832 | return vrc;
|
---|
833 | }
|
---|
834 |
|
---|
835 | int GuestProcess::i_readData(uint32_t uHandle, uint32_t uSize, uint32_t uTimeoutMS,
|
---|
836 | void *pvData, size_t cbData, uint32_t *pcbRead, int *pGuestRc)
|
---|
837 | {
|
---|
838 | LogFlowThisFunc(("uPID=%RU32, uHandle=%RU32, uSize=%RU32, uTimeoutMS=%RU32, pvData=%p, cbData=%RU32, pGuestRc=%p\n",
|
---|
839 | mData.mPID, uHandle, uSize, uTimeoutMS, pvData, cbData, pGuestRc));
|
---|
840 | AssertReturn(uSize, VERR_INVALID_PARAMETER);
|
---|
841 | AssertPtrReturn(pvData, VERR_INVALID_POINTER);
|
---|
842 | AssertReturn(cbData >= uSize, VERR_INVALID_PARAMETER);
|
---|
843 | /* pcbRead is optional. */
|
---|
844 |
|
---|
845 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
846 |
|
---|
847 | if ( mData.mStatus != ProcessStatus_Started
|
---|
848 | /* Skip reading if the process wasn't started with the appropriate
|
---|
849 | * flags. */
|
---|
850 | || ( ( uHandle == OUTPUT_HANDLE_ID_STDOUT
|
---|
851 | || uHandle == OUTPUT_HANDLE_ID_STDOUT_DEPRECATED)
|
---|
852 | && !(mData.mProcess.mFlags & ProcessCreateFlag_WaitForStdOut))
|
---|
853 | || ( uHandle == OUTPUT_HANDLE_ID_STDERR
|
---|
854 | && !(mData.mProcess.mFlags & ProcessCreateFlag_WaitForStdErr))
|
---|
855 | )
|
---|
856 | {
|
---|
857 | if (pcbRead)
|
---|
858 | *pcbRead = 0;
|
---|
859 | if (pGuestRc)
|
---|
860 | *pGuestRc = VINF_SUCCESS;
|
---|
861 | return VINF_SUCCESS; /* Nothing to read anymore. */
|
---|
862 | }
|
---|
863 |
|
---|
864 | int vrc;
|
---|
865 |
|
---|
866 | GuestWaitEvent *pEvent = NULL;
|
---|
867 | GuestEventTypes eventTypes;
|
---|
868 | try
|
---|
869 | {
|
---|
870 | /*
|
---|
871 | * On Guest Additions < 4.3 there is no guarantee that the process status
|
---|
872 | * change arrives *after* the output event, e.g. if this was the last output
|
---|
873 | * block being read and the process will report status "terminate".
|
---|
874 | * So just skip checking for process status change and only wait for the
|
---|
875 | * output event.
|
---|
876 | */
|
---|
877 | if (mSession->i_getProtocolVersion() >= 2)
|
---|
878 | eventTypes.push_back(VBoxEventType_OnGuestProcessStateChanged);
|
---|
879 | eventTypes.push_back(VBoxEventType_OnGuestProcessOutput);
|
---|
880 |
|
---|
881 | vrc = registerWaitEvent(eventTypes, &pEvent);
|
---|
882 | }
|
---|
883 | catch (std::bad_alloc)
|
---|
884 | {
|
---|
885 | vrc = VERR_NO_MEMORY;
|
---|
886 | }
|
---|
887 |
|
---|
888 | if (RT_FAILURE(vrc))
|
---|
889 | return vrc;
|
---|
890 |
|
---|
891 | if (RT_SUCCESS(vrc))
|
---|
892 | {
|
---|
893 | VBOXHGCMSVCPARM paParms[8];
|
---|
894 | int i = 0;
|
---|
895 | paParms[i++].setUInt32(pEvent->ContextID());
|
---|
896 | paParms[i++].setUInt32(mData.mPID);
|
---|
897 | paParms[i++].setUInt32(uHandle);
|
---|
898 | paParms[i++].setUInt32(0 /* Flags, none set yet. */);
|
---|
899 |
|
---|
900 | alock.release(); /* Drop the write lock before sending. */
|
---|
901 |
|
---|
902 | vrc = sendCommand(HOST_EXEC_GET_OUTPUT, i, paParms);
|
---|
903 | }
|
---|
904 |
|
---|
905 | if (RT_SUCCESS(vrc))
|
---|
906 | vrc = i_waitForOutput(pEvent, uHandle, uTimeoutMS,
|
---|
907 | pvData, cbData, pcbRead);
|
---|
908 |
|
---|
909 | unregisterWaitEvent(pEvent);
|
---|
910 |
|
---|
911 | LogFlowFuncLeaveRC(vrc);
|
---|
912 | return vrc;
|
---|
913 | }
|
---|
914 |
|
---|
915 | /* Does not do locking; caller is responsible for that! */
|
---|
916 | int GuestProcess::i_setProcessStatus(ProcessStatus_T procStatus, int procRc)
|
---|
917 | {
|
---|
918 | LogFlowThisFuncEnter();
|
---|
919 |
|
---|
920 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
921 |
|
---|
922 | LogFlowThisFunc(("oldStatus=%RU32, newStatus=%RU32, procRc=%Rrc\n",
|
---|
923 | mData.mStatus, procStatus, procRc));
|
---|
924 |
|
---|
925 | if (procStatus == ProcessStatus_Error)
|
---|
926 | {
|
---|
927 | AssertMsg(RT_FAILURE(procRc), ("Guest rc must be an error (%Rrc)\n", procRc));
|
---|
928 | /* Do not allow overwriting an already set error. If this happens
|
---|
929 | * this means we forgot some error checking/locking somewhere. */
|
---|
930 | AssertMsg(RT_SUCCESS(mData.mLastError), ("Guest rc already set (to %Rrc)\n", mData.mLastError));
|
---|
931 | }
|
---|
932 | else
|
---|
933 | AssertMsg(RT_SUCCESS(procRc), ("Guest rc must not be an error (%Rrc)\n", procRc));
|
---|
934 |
|
---|
935 | int rc = VINF_SUCCESS;
|
---|
936 |
|
---|
937 | if (mData.mStatus != procStatus) /* Was there a process status change? */
|
---|
938 | {
|
---|
939 | mData.mStatus = procStatus;
|
---|
940 | mData.mLastError = procRc;
|
---|
941 |
|
---|
942 | ComObjPtr<VirtualBoxErrorInfo> errorInfo;
|
---|
943 | HRESULT hr = errorInfo.createObject();
|
---|
944 | ComAssertComRC(hr);
|
---|
945 | if (RT_FAILURE(mData.mLastError))
|
---|
946 | {
|
---|
947 | hr = errorInfo->initEx(VBOX_E_IPRT_ERROR, mData.mLastError,
|
---|
948 | COM_IIDOF(IGuestProcess), getComponentName(),
|
---|
949 | i_guestErrorToString(mData.mLastError));
|
---|
950 | ComAssertComRC(hr);
|
---|
951 | }
|
---|
952 |
|
---|
953 | /* Copy over necessary data before releasing lock again. */
|
---|
954 | uint32_t uPID = mData.mPID;
|
---|
955 | /** @todo Also handle mSession? */
|
---|
956 |
|
---|
957 | alock.release(); /* Release lock before firing off event. */
|
---|
958 |
|
---|
959 | fireGuestProcessStateChangedEvent(mEventSource, mSession, this,
|
---|
960 | uPID, procStatus, errorInfo);
|
---|
961 | #if 0
|
---|
962 | /*
|
---|
963 | * On Guest Additions < 4.3 there is no guarantee that outstanding
|
---|
964 | * requests will be delivered to the host after the process has ended,
|
---|
965 | * so just cancel all waiting events here to not let clients run
|
---|
966 | * into timeouts.
|
---|
967 | */
|
---|
968 | if ( mSession->getProtocolVersion() < 2
|
---|
969 | && hasEnded())
|
---|
970 | {
|
---|
971 | LogFlowThisFunc(("Process ended, canceling outstanding wait events ...\n"));
|
---|
972 | rc = cancelWaitEvents();
|
---|
973 | }
|
---|
974 | #endif
|
---|
975 | }
|
---|
976 |
|
---|
977 | return rc;
|
---|
978 | }
|
---|
979 |
|
---|
980 | /* static */
|
---|
981 | HRESULT GuestProcess::i_setErrorExternal(VirtualBoxBase *pInterface, int guestRc)
|
---|
982 | {
|
---|
983 | AssertPtr(pInterface);
|
---|
984 | AssertMsg(RT_FAILURE(guestRc), ("Guest rc does not indicate a failure when setting error\n"));
|
---|
985 |
|
---|
986 | return pInterface->setError(VBOX_E_IPRT_ERROR, GuestProcess::i_guestErrorToString(guestRc).c_str());
|
---|
987 | }
|
---|
988 |
|
---|
989 | int GuestProcess::i_startProcess(uint32_t uTimeoutMS, int *pGuestRc)
|
---|
990 | {
|
---|
991 | LogFlowThisFunc(("uTimeoutMS=%RU32, procExe=%s, procTimeoutMS=%RU32, procFlags=%x, sessionID=%RU32\n",
|
---|
992 | uTimeoutMS, mData.mProcess.mExecutable.c_str(), mData.mProcess.mTimeoutMS, mData.mProcess.mFlags,
|
---|
993 | mSession->i_getId()));
|
---|
994 |
|
---|
995 | /* Wait until the caller function (if kicked off by a thread)
|
---|
996 | * has returned and continue operation. */
|
---|
997 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
998 |
|
---|
999 | mData.mStatus = ProcessStatus_Starting;
|
---|
1000 |
|
---|
1001 | int vrc;
|
---|
1002 |
|
---|
1003 | GuestWaitEvent *pEvent = NULL;
|
---|
1004 | GuestEventTypes eventTypes;
|
---|
1005 | try
|
---|
1006 | {
|
---|
1007 | eventTypes.push_back(VBoxEventType_OnGuestProcessStateChanged);
|
---|
1008 |
|
---|
1009 | vrc = registerWaitEvent(eventTypes, &pEvent);
|
---|
1010 | }
|
---|
1011 | catch (std::bad_alloc)
|
---|
1012 | {
|
---|
1013 | vrc = VERR_NO_MEMORY;
|
---|
1014 | }
|
---|
1015 |
|
---|
1016 | if (RT_FAILURE(vrc))
|
---|
1017 | return vrc;
|
---|
1018 |
|
---|
1019 | GuestSession *pSession = mSession;
|
---|
1020 | AssertPtr(pSession);
|
---|
1021 | uint32_t const uProtocol = pSession->i_getProtocolVersion();
|
---|
1022 |
|
---|
1023 | const GuestCredentials &sessionCreds = pSession->i_getCredentials();
|
---|
1024 |
|
---|
1025 |
|
---|
1026 | /* Prepare arguments. */
|
---|
1027 | char *pszArgs = NULL;
|
---|
1028 | size_t cArgs = mData.mProcess.mArguments.size();
|
---|
1029 | if (cArgs >= UINT32_MAX)
|
---|
1030 | vrc = VERR_BUFFER_OVERFLOW;
|
---|
1031 |
|
---|
1032 | if ( RT_SUCCESS(vrc)
|
---|
1033 | && cArgs)
|
---|
1034 | {
|
---|
1035 | char const **papszArgv = (char const **)RTMemAlloc((cArgs + 1) * sizeof(papszArgv[0]));
|
---|
1036 | AssertReturn(papszArgv, VERR_NO_MEMORY);
|
---|
1037 |
|
---|
1038 | for (size_t i = 0; i < cArgs; i++)
|
---|
1039 | {
|
---|
1040 | papszArgv[i] = mData.mProcess.mArguments[i].c_str();
|
---|
1041 | AssertPtr(papszArgv[i]);
|
---|
1042 | }
|
---|
1043 | papszArgv[cArgs] = NULL;
|
---|
1044 |
|
---|
1045 | if (uProtocol < UINT32_C(0xdeadbeef) ) /** @todo implement a way of sending argv[0], best idea is a new command. */
|
---|
1046 | vrc = RTGetOptArgvToString(&pszArgs, papszArgv + 1, RTGETOPTARGV_CNV_QUOTE_BOURNE_SH);
|
---|
1047 | else
|
---|
1048 | vrc = RTGetOptArgvToString(&pszArgs, papszArgv, RTGETOPTARGV_CNV_QUOTE_BOURNE_SH);
|
---|
1049 |
|
---|
1050 | RTMemFree(papszArgv);
|
---|
1051 | }
|
---|
1052 |
|
---|
1053 | /* Calculate arguments size (in bytes). */
|
---|
1054 | size_t cbArgs = 0;
|
---|
1055 | if (RT_SUCCESS(vrc))
|
---|
1056 | cbArgs = pszArgs ? strlen(pszArgs) + 1 : 0; /* Include terminating zero. */
|
---|
1057 |
|
---|
1058 | /* Prepare environment. The guest service dislikes the empty string at the end, so drop it. */
|
---|
1059 | size_t cbEnvBlock;
|
---|
1060 | char *pszzEnvBlock;
|
---|
1061 | if (RT_SUCCESS(vrc))
|
---|
1062 | vrc = mData.mProcess.mEnvironmentChanges.queryUtf8Block(&pszzEnvBlock, &cbEnvBlock);
|
---|
1063 | if (RT_SUCCESS(vrc))
|
---|
1064 | {
|
---|
1065 | Assert(cbEnvBlock > 0);
|
---|
1066 | cbEnvBlock--;
|
---|
1067 |
|
---|
1068 | /* Prepare HGCM call. */
|
---|
1069 | VBOXHGCMSVCPARM paParms[16];
|
---|
1070 | int i = 0;
|
---|
1071 | paParms[i++].setUInt32(pEvent->ContextID());
|
---|
1072 | paParms[i++].setCppString(mData.mProcess.mExecutable);
|
---|
1073 | paParms[i++].setUInt32(mData.mProcess.mFlags);
|
---|
1074 | paParms[i++].setUInt32((uint32_t)mData.mProcess.mArguments.size());
|
---|
1075 | paParms[i++].setPointer(pszArgs, (uint32_t)cbArgs);
|
---|
1076 | paParms[i++].setUInt32(mData.mProcess.mEnvironmentChanges.count());
|
---|
1077 | paParms[i++].setUInt32((uint32_t)cbEnvBlock);
|
---|
1078 | paParms[i++].setPointer(pszzEnvBlock, (uint32_t)cbEnvBlock);
|
---|
1079 | if (uProtocol < 2)
|
---|
1080 | {
|
---|
1081 | /* In protocol v1 (VBox < 4.3) the credentials were part of the execution
|
---|
1082 | * call. In newer protocols these credentials are part of the opened guest
|
---|
1083 | * session, so not needed anymore here. */
|
---|
1084 | paParms[i++].setCppString(sessionCreds.mUser);
|
---|
1085 | paParms[i++].setCppString(sessionCreds.mPassword);
|
---|
1086 | }
|
---|
1087 | /*
|
---|
1088 | * If the WaitForProcessStartOnly flag is set, we only want to define and wait for a timeout
|
---|
1089 | * until the process was started - the process itself then gets an infinite timeout for execution.
|
---|
1090 | * This is handy when we want to start a process inside a worker thread within a certain timeout
|
---|
1091 | * but let the started process perform lengthly operations then.
|
---|
1092 | */
|
---|
1093 | if (mData.mProcess.mFlags & ProcessCreateFlag_WaitForProcessStartOnly)
|
---|
1094 | paParms[i++].setUInt32(UINT32_MAX /* Infinite timeout */);
|
---|
1095 | else
|
---|
1096 | paParms[i++].setUInt32(mData.mProcess.mTimeoutMS);
|
---|
1097 | if (uProtocol >= 2)
|
---|
1098 | {
|
---|
1099 | paParms[i++].setUInt32(mData.mProcess.mPriority);
|
---|
1100 | /* CPU affinity: We only support one CPU affinity block at the moment,
|
---|
1101 | * so that makes up to 64 CPUs total. This can be more in the future. */
|
---|
1102 | paParms[i++].setUInt32(1);
|
---|
1103 | /* The actual CPU affinity blocks. */
|
---|
1104 | paParms[i++].setPointer((void *)&mData.mProcess.mAffinity, sizeof(mData.mProcess.mAffinity));
|
---|
1105 | }
|
---|
1106 |
|
---|
1107 | alock.release(); /* Drop the write lock before sending. */
|
---|
1108 |
|
---|
1109 | vrc = sendCommand(HOST_EXEC_CMD, i, paParms);
|
---|
1110 | if (RT_FAILURE(vrc))
|
---|
1111 | {
|
---|
1112 | int rc2 = i_setProcessStatus(ProcessStatus_Error, vrc);
|
---|
1113 | AssertRC(rc2);
|
---|
1114 | }
|
---|
1115 |
|
---|
1116 | mData.mProcess.mEnvironmentChanges.freeUtf8Block(pszzEnvBlock);
|
---|
1117 | }
|
---|
1118 |
|
---|
1119 | if (pszArgs)
|
---|
1120 | RTStrFree(pszArgs);
|
---|
1121 |
|
---|
1122 | if (RT_SUCCESS(vrc))
|
---|
1123 | vrc = i_waitForStatusChange(pEvent, uTimeoutMS,
|
---|
1124 | NULL /* Process status */, pGuestRc);
|
---|
1125 | unregisterWaitEvent(pEvent);
|
---|
1126 |
|
---|
1127 | LogFlowFuncLeaveRC(vrc);
|
---|
1128 | return vrc;
|
---|
1129 | }
|
---|
1130 |
|
---|
1131 | int GuestProcess::i_startProcessAsync(void)
|
---|
1132 | {
|
---|
1133 | LogFlowThisFuncEnter();
|
---|
1134 |
|
---|
1135 | int vrc = VINF_SUCCESS;
|
---|
1136 | HRESULT hr = S_OK;
|
---|
1137 |
|
---|
1138 | GuestProcessStartTask* pTask = NULL;
|
---|
1139 | try
|
---|
1140 | {
|
---|
1141 | pTask = new GuestProcessStartTask(this);
|
---|
1142 | if (!pTask->i_isOk())
|
---|
1143 | {
|
---|
1144 | delete pTask;
|
---|
1145 | LogFlow(("GuestProcess: Could not create GuestProcessStartTask object \n"));
|
---|
1146 | throw VERR_MEMOBJ_INIT_FAILED;
|
---|
1147 | }
|
---|
1148 | LogFlow(("GuestProcess: Successfully created GuestProcessStartTask object \n"));
|
---|
1149 | //this function delete pTask in case of exceptions, so there is no need in the call of delete operator
|
---|
1150 | hr = pTask->createThread();
|
---|
1151 | }
|
---|
1152 | catch(std::bad_alloc &)
|
---|
1153 | {
|
---|
1154 | vrc = VERR_NO_MEMORY;
|
---|
1155 | }
|
---|
1156 | catch(int eVRC)
|
---|
1157 | {
|
---|
1158 | vrc = eVRC;
|
---|
1159 | LogFlow(("GuestSession: Could not create thread for GuestProcessStartTask task %Rrc\n", vrc));
|
---|
1160 | }
|
---|
1161 |
|
---|
1162 | LogFlowFuncLeaveRC(vrc);
|
---|
1163 | return vrc;
|
---|
1164 | }
|
---|
1165 |
|
---|
1166 | /* static */
|
---|
1167 | DECLCALLBACK(int) GuestProcess::i_startProcessThread(RTTHREAD Thread, void *pvUser)
|
---|
1168 | {
|
---|
1169 | LogFlowFunc(("pvUser=%p\n", pvUser));
|
---|
1170 |
|
---|
1171 | GuestProcessStartTask* pTask = static_cast<GuestProcessStartTask*>(pvUser);
|
---|
1172 |
|
---|
1173 | const ComObjPtr<GuestProcess> pProcess(pTask->i_process());
|
---|
1174 | Assert(!pProcess.isNull());
|
---|
1175 |
|
---|
1176 | AutoCaller autoCaller(pProcess);
|
---|
1177 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1178 |
|
---|
1179 | int vrc = pProcess->i_startProcess(30 * 1000 /* 30s timeout */,
|
---|
1180 | NULL /* Guest rc, ignored */);
|
---|
1181 | /* Nothing to do here anymore. */
|
---|
1182 |
|
---|
1183 | LogFlowFunc(("pProcess=%p returning rc=%Rrc\n", (GuestProcess *)pProcess, vrc));
|
---|
1184 | return vrc;
|
---|
1185 | }
|
---|
1186 |
|
---|
1187 | int GuestProcess::i_terminateProcess(uint32_t uTimeoutMS, int *pGuestRc)
|
---|
1188 | {
|
---|
1189 | /* pGuestRc is optional. */
|
---|
1190 | LogFlowThisFunc(("uTimeoutMS=%RU32\n", uTimeoutMS));
|
---|
1191 |
|
---|
1192 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1193 |
|
---|
1194 | int vrc = VINF_SUCCESS;
|
---|
1195 |
|
---|
1196 | if (mData.mStatus != ProcessStatus_Started)
|
---|
1197 | {
|
---|
1198 | LogFlowThisFunc(("Process not in started state (state is %RU32), skipping termination\n",
|
---|
1199 | mData.mStatus));
|
---|
1200 | }
|
---|
1201 | else
|
---|
1202 | {
|
---|
1203 | AssertPtr(mSession);
|
---|
1204 | /* Note: VBox < 4.3 (aka protocol version 1) does not
|
---|
1205 | * support this, so just skip. */
|
---|
1206 | if (mSession->i_getProtocolVersion() < 2)
|
---|
1207 | vrc = VERR_NOT_SUPPORTED;
|
---|
1208 |
|
---|
1209 | if (RT_SUCCESS(vrc))
|
---|
1210 | {
|
---|
1211 | GuestWaitEvent *pEvent = NULL;
|
---|
1212 | GuestEventTypes eventTypes;
|
---|
1213 | try
|
---|
1214 | {
|
---|
1215 | eventTypes.push_back(VBoxEventType_OnGuestProcessStateChanged);
|
---|
1216 |
|
---|
1217 | vrc = registerWaitEvent(eventTypes, &pEvent);
|
---|
1218 | }
|
---|
1219 | catch (std::bad_alloc)
|
---|
1220 | {
|
---|
1221 | vrc = VERR_NO_MEMORY;
|
---|
1222 | }
|
---|
1223 |
|
---|
1224 | if (RT_FAILURE(vrc))
|
---|
1225 | return vrc;
|
---|
1226 |
|
---|
1227 | VBOXHGCMSVCPARM paParms[4];
|
---|
1228 | int i = 0;
|
---|
1229 | paParms[i++].setUInt32(pEvent->ContextID());
|
---|
1230 | paParms[i++].setUInt32(mData.mPID);
|
---|
1231 |
|
---|
1232 | alock.release(); /* Drop the write lock before sending. */
|
---|
1233 |
|
---|
1234 | vrc = sendCommand(HOST_EXEC_TERMINATE, i, paParms);
|
---|
1235 | if (RT_SUCCESS(vrc))
|
---|
1236 | vrc = i_waitForStatusChange(pEvent, uTimeoutMS,
|
---|
1237 | NULL /* ProcessStatus */, pGuestRc);
|
---|
1238 | unregisterWaitEvent(pEvent);
|
---|
1239 | }
|
---|
1240 | }
|
---|
1241 |
|
---|
1242 | LogFlowFuncLeaveRC(vrc);
|
---|
1243 | return vrc;
|
---|
1244 | }
|
---|
1245 |
|
---|
1246 | /* static */
|
---|
1247 | ProcessWaitResult_T GuestProcess::i_waitFlagsToResultEx(uint32_t fWaitFlags,
|
---|
1248 | ProcessStatus_T oldStatus, ProcessStatus_T newStatus,
|
---|
1249 | uint32_t uProcFlags, uint32_t uProtocol)
|
---|
1250 | {
|
---|
1251 | ProcessWaitResult_T waitResult = ProcessWaitResult_None;
|
---|
1252 |
|
---|
1253 | switch (newStatus)
|
---|
1254 | {
|
---|
1255 | case ProcessStatus_TerminatedNormally:
|
---|
1256 | case ProcessStatus_TerminatedSignal:
|
---|
1257 | case ProcessStatus_TerminatedAbnormally:
|
---|
1258 | case ProcessStatus_Down:
|
---|
1259 | /* Nothing to wait for anymore. */
|
---|
1260 | waitResult = ProcessWaitResult_Terminate;
|
---|
1261 | break;
|
---|
1262 |
|
---|
1263 | case ProcessStatus_TimedOutKilled:
|
---|
1264 | case ProcessStatus_TimedOutAbnormally:
|
---|
1265 | /* Dito. */
|
---|
1266 | waitResult = ProcessWaitResult_Timeout;
|
---|
1267 | break;
|
---|
1268 |
|
---|
1269 | case ProcessStatus_Started:
|
---|
1270 | switch (oldStatus)
|
---|
1271 | {
|
---|
1272 | case ProcessStatus_Undefined:
|
---|
1273 | case ProcessStatus_Starting:
|
---|
1274 | /* Also wait for process start. */
|
---|
1275 | if (fWaitFlags & ProcessWaitForFlag_Start)
|
---|
1276 | waitResult = ProcessWaitResult_Start;
|
---|
1277 | else
|
---|
1278 | {
|
---|
1279 | /*
|
---|
1280 | * If ProcessCreateFlag_WaitForProcessStartOnly was specified on process creation the
|
---|
1281 | * caller is not interested in getting further process statuses -- so just don't notify
|
---|
1282 | * anything here anymore and return.
|
---|
1283 | */
|
---|
1284 | if (uProcFlags & ProcessCreateFlag_WaitForProcessStartOnly)
|
---|
1285 | waitResult = ProcessWaitResult_Start;
|
---|
1286 | }
|
---|
1287 | break;
|
---|
1288 |
|
---|
1289 | case ProcessStatus_Started:
|
---|
1290 | /* Only wait for process start. */
|
---|
1291 | if (fWaitFlags == ProcessWaitForFlag_Start)
|
---|
1292 | waitResult = ProcessWaitResult_Start;
|
---|
1293 | break;
|
---|
1294 |
|
---|
1295 | default:
|
---|
1296 | AssertMsgFailed(("Unhandled old status %RU32 before new status 'started'\n",
|
---|
1297 | oldStatus));
|
---|
1298 | waitResult = ProcessWaitResult_Start;
|
---|
1299 | break;
|
---|
1300 | }
|
---|
1301 | break;
|
---|
1302 |
|
---|
1303 | case ProcessStatus_Error:
|
---|
1304 | /* Nothing to wait for anymore. */
|
---|
1305 | waitResult = ProcessWaitResult_Error;
|
---|
1306 | break;
|
---|
1307 |
|
---|
1308 | case ProcessStatus_Undefined:
|
---|
1309 | case ProcessStatus_Starting:
|
---|
1310 | /* No result available yet, leave wait
|
---|
1311 | * flags untouched. */
|
---|
1312 | break;
|
---|
1313 | }
|
---|
1314 |
|
---|
1315 | if (newStatus == ProcessStatus_Started)
|
---|
1316 | {
|
---|
1317 | /**
|
---|
1318 | * Filter out waits which are *not* supported using
|
---|
1319 | * older guest control Guest Additions.
|
---|
1320 | *
|
---|
1321 | ** @todo ProcessWaitForFlag_Std* flags are not implemented yet.
|
---|
1322 | */
|
---|
1323 | if (uProtocol < 99) /* See @todo above. */
|
---|
1324 | {
|
---|
1325 | if ( waitResult == ProcessWaitResult_None
|
---|
1326 | /* We don't support waiting for stdin, out + err,
|
---|
1327 | * just skip waiting then. */
|
---|
1328 | && ( (fWaitFlags & ProcessWaitForFlag_StdIn)
|
---|
1329 | || (fWaitFlags & ProcessWaitForFlag_StdOut)
|
---|
1330 | || (fWaitFlags & ProcessWaitForFlag_StdErr)
|
---|
1331 | )
|
---|
1332 | )
|
---|
1333 | {
|
---|
1334 | /* Use _WaitFlagNotSupported because we don't know what to tell the caller. */
|
---|
1335 | waitResult = ProcessWaitResult_WaitFlagNotSupported;
|
---|
1336 | }
|
---|
1337 | }
|
---|
1338 | }
|
---|
1339 |
|
---|
1340 | #ifdef DEBUG
|
---|
1341 | LogFlowFunc(("oldStatus=%RU32, newStatus=%RU32, fWaitFlags=0x%x, waitResult=%RU32\n",
|
---|
1342 | oldStatus, newStatus, fWaitFlags, waitResult));
|
---|
1343 | #endif
|
---|
1344 | return waitResult;
|
---|
1345 | }
|
---|
1346 |
|
---|
1347 | ProcessWaitResult_T GuestProcess::i_waitFlagsToResult(uint32_t fWaitFlags)
|
---|
1348 | {
|
---|
1349 | AssertPtr(mSession);
|
---|
1350 | return GuestProcess::i_waitFlagsToResultEx(fWaitFlags,
|
---|
1351 | mData.mStatus /* curStatus */, mData.mStatus /* newStatus */,
|
---|
1352 | mData.mProcess.mFlags, mSession->i_getProtocolVersion());
|
---|
1353 | }
|
---|
1354 |
|
---|
1355 | int GuestProcess::i_waitFor(uint32_t fWaitFlags, ULONG uTimeoutMS,
|
---|
1356 | ProcessWaitResult_T &waitResult, int *pGuestRc)
|
---|
1357 | {
|
---|
1358 | AssertReturn(fWaitFlags, VERR_INVALID_PARAMETER);
|
---|
1359 |
|
---|
1360 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1361 |
|
---|
1362 | LogFlowThisFunc(("fWaitFlags=0x%x, uTimeoutMS=%RU32, procStatus=%RU32, procRc=%Rrc, pGuestRc=%p\n",
|
---|
1363 | fWaitFlags, uTimeoutMS, mData.mStatus, mData.mLastError, pGuestRc));
|
---|
1364 |
|
---|
1365 | /* Did some error occur before? Then skip waiting and return. */
|
---|
1366 | ProcessStatus_T curStatus = mData.mStatus;
|
---|
1367 | if (curStatus == ProcessStatus_Error)
|
---|
1368 | {
|
---|
1369 | waitResult = ProcessWaitResult_Error;
|
---|
1370 | AssertMsg(RT_FAILURE(mData.mLastError),
|
---|
1371 | ("No error rc (%Rrc) set when guest process indicated an error\n", mData.mLastError));
|
---|
1372 | if (pGuestRc)
|
---|
1373 | *pGuestRc = mData.mLastError; /* Return last set error. */
|
---|
1374 | LogFlowThisFunc(("Process is in error state (guestRc=%Rrc)\n", mData.mLastError));
|
---|
1375 | return VERR_GSTCTL_GUEST_ERROR;
|
---|
1376 | }
|
---|
1377 |
|
---|
1378 | waitResult = i_waitFlagsToResult(fWaitFlags);
|
---|
1379 |
|
---|
1380 | /* No waiting needed? Return immediately using the last set error. */
|
---|
1381 | if (waitResult != ProcessWaitResult_None)
|
---|
1382 | {
|
---|
1383 | if (pGuestRc)
|
---|
1384 | *pGuestRc = mData.mLastError; /* Return last set error (if any). */
|
---|
1385 | LogFlowThisFunc(("Nothing to wait for (guestRc=%Rrc)\n", mData.mLastError));
|
---|
1386 | return RT_SUCCESS(mData.mLastError) ? VINF_SUCCESS : VERR_GSTCTL_GUEST_ERROR;
|
---|
1387 | }
|
---|
1388 |
|
---|
1389 | /* Adjust timeout. Passing 0 means RT_INDEFINITE_WAIT. */
|
---|
1390 | if (!uTimeoutMS)
|
---|
1391 | uTimeoutMS = RT_INDEFINITE_WAIT;
|
---|
1392 |
|
---|
1393 | int vrc;
|
---|
1394 |
|
---|
1395 | GuestWaitEvent *pEvent = NULL;
|
---|
1396 | GuestEventTypes eventTypes;
|
---|
1397 | try
|
---|
1398 | {
|
---|
1399 | eventTypes.push_back(VBoxEventType_OnGuestProcessStateChanged);
|
---|
1400 |
|
---|
1401 | vrc = registerWaitEvent(eventTypes, &pEvent);
|
---|
1402 | }
|
---|
1403 | catch (std::bad_alloc)
|
---|
1404 | {
|
---|
1405 | vrc = VERR_NO_MEMORY;
|
---|
1406 | }
|
---|
1407 |
|
---|
1408 | if (RT_FAILURE(vrc))
|
---|
1409 | return vrc;
|
---|
1410 |
|
---|
1411 | alock.release(); /* Release lock before waiting. */
|
---|
1412 |
|
---|
1413 | /*
|
---|
1414 | * Do the actual waiting.
|
---|
1415 | */
|
---|
1416 | ProcessStatus_T newStatus = ProcessStatus_Undefined;
|
---|
1417 | uint64_t u64StartMS = RTTimeMilliTS();
|
---|
1418 | for (;;)
|
---|
1419 | {
|
---|
1420 | uint64_t u64ElapsedMS = RTTimeMilliTS() - u64StartMS;
|
---|
1421 | if ( uTimeoutMS != RT_INDEFINITE_WAIT
|
---|
1422 | && u64ElapsedMS >= uTimeoutMS)
|
---|
1423 | {
|
---|
1424 | vrc = VERR_TIMEOUT;
|
---|
1425 | break;
|
---|
1426 | }
|
---|
1427 |
|
---|
1428 | vrc = i_waitForStatusChange(pEvent,
|
---|
1429 | uTimeoutMS == RT_INDEFINITE_WAIT
|
---|
1430 | ? RT_INDEFINITE_WAIT : uTimeoutMS - (uint32_t)u64ElapsedMS,
|
---|
1431 | &newStatus, pGuestRc);
|
---|
1432 | if (RT_SUCCESS(vrc))
|
---|
1433 | {
|
---|
1434 | alock.acquire();
|
---|
1435 |
|
---|
1436 | waitResult = i_waitFlagsToResultEx(fWaitFlags, curStatus, newStatus,
|
---|
1437 | mData.mProcess.mFlags, mSession->i_getProtocolVersion());
|
---|
1438 | #ifdef DEBUG
|
---|
1439 | LogFlowThisFunc(("Got new status change: fWaitFlags=0x%x, newStatus=%RU32, waitResult=%RU32\n",
|
---|
1440 | fWaitFlags, newStatus, waitResult));
|
---|
1441 | #endif
|
---|
1442 | if (ProcessWaitResult_None != waitResult) /* We got a waiting result. */
|
---|
1443 | break;
|
---|
1444 | }
|
---|
1445 | else /* Waiting failed, bail out. */
|
---|
1446 | break;
|
---|
1447 |
|
---|
1448 | alock.release(); /* Don't hold lock in next waiting round. */
|
---|
1449 | }
|
---|
1450 |
|
---|
1451 | unregisterWaitEvent(pEvent);
|
---|
1452 |
|
---|
1453 | LogFlowThisFunc(("Returned waitResult=%RU32, newStatus=%RU32, rc=%Rrc\n",
|
---|
1454 | waitResult, newStatus, vrc));
|
---|
1455 | return vrc;
|
---|
1456 | }
|
---|
1457 |
|
---|
1458 | int GuestProcess::i_waitForInputNotify(GuestWaitEvent *pEvent, uint32_t uHandle, uint32_t uTimeoutMS,
|
---|
1459 | ProcessInputStatus_T *pInputStatus, uint32_t *pcbProcessed)
|
---|
1460 | {
|
---|
1461 | AssertPtrReturn(pEvent, VERR_INVALID_POINTER);
|
---|
1462 |
|
---|
1463 | VBoxEventType_T evtType;
|
---|
1464 | ComPtr<IEvent> pIEvent;
|
---|
1465 | int vrc = waitForEvent(pEvent, uTimeoutMS,
|
---|
1466 | &evtType, pIEvent.asOutParam());
|
---|
1467 | if (RT_SUCCESS(vrc))
|
---|
1468 | {
|
---|
1469 | if (evtType == VBoxEventType_OnGuestProcessInputNotify)
|
---|
1470 | {
|
---|
1471 | ComPtr<IGuestProcessInputNotifyEvent> pProcessEvent = pIEvent;
|
---|
1472 | Assert(!pProcessEvent.isNull());
|
---|
1473 |
|
---|
1474 | if (pInputStatus)
|
---|
1475 | {
|
---|
1476 | HRESULT hr2 = pProcessEvent->COMGETTER(Status)(pInputStatus);
|
---|
1477 | ComAssertComRC(hr2);
|
---|
1478 | }
|
---|
1479 | if (pcbProcessed)
|
---|
1480 | {
|
---|
1481 | HRESULT hr2 = pProcessEvent->COMGETTER(Processed)((ULONG*)pcbProcessed);
|
---|
1482 | ComAssertComRC(hr2);
|
---|
1483 | }
|
---|
1484 | }
|
---|
1485 | else
|
---|
1486 | vrc = VWRN_GSTCTL_OBJECTSTATE_CHANGED;
|
---|
1487 | }
|
---|
1488 |
|
---|
1489 | LogFlowThisFunc(("Returning pEvent=%p, uHandle=%RU32, rc=%Rrc\n",
|
---|
1490 | pEvent, uHandle, vrc));
|
---|
1491 | return vrc;
|
---|
1492 | }
|
---|
1493 |
|
---|
1494 | int GuestProcess::i_waitForOutput(GuestWaitEvent *pEvent, uint32_t uHandle, uint32_t uTimeoutMS,
|
---|
1495 | void *pvData, size_t cbData, uint32_t *pcbRead)
|
---|
1496 | {
|
---|
1497 | AssertPtrReturn(pEvent, VERR_INVALID_POINTER);
|
---|
1498 | /* pvData is optional. */
|
---|
1499 | /* cbData is optional. */
|
---|
1500 | /* pcbRead is optional. */
|
---|
1501 |
|
---|
1502 | LogFlowThisFunc(("cEventTypes=%zu, pEvent=%p, uHandle=%RU32, uTimeoutMS=%RU32, pvData=%p, cbData=%zu, pcbRead=%p\n",
|
---|
1503 | pEvent->TypeCount(), pEvent, uHandle, uTimeoutMS, pvData, cbData, pcbRead));
|
---|
1504 |
|
---|
1505 | int vrc;
|
---|
1506 |
|
---|
1507 | VBoxEventType_T evtType;
|
---|
1508 | ComPtr<IEvent> pIEvent;
|
---|
1509 | do
|
---|
1510 | {
|
---|
1511 | vrc = waitForEvent(pEvent, uTimeoutMS,
|
---|
1512 | &evtType, pIEvent.asOutParam());
|
---|
1513 | if (RT_SUCCESS(vrc))
|
---|
1514 | {
|
---|
1515 | if (evtType == VBoxEventType_OnGuestProcessOutput)
|
---|
1516 | {
|
---|
1517 | ComPtr<IGuestProcessOutputEvent> pProcessEvent = pIEvent;
|
---|
1518 | Assert(!pProcessEvent.isNull());
|
---|
1519 |
|
---|
1520 | ULONG uHandleEvent;
|
---|
1521 | HRESULT hr = pProcessEvent->COMGETTER(Handle)(&uHandleEvent);
|
---|
1522 | if ( SUCCEEDED(hr)
|
---|
1523 | && uHandleEvent == uHandle)
|
---|
1524 | {
|
---|
1525 | if (pvData)
|
---|
1526 | {
|
---|
1527 | com::SafeArray <BYTE> data;
|
---|
1528 | hr = pProcessEvent->COMGETTER(Data)(ComSafeArrayAsOutParam(data));
|
---|
1529 | ComAssertComRC(hr);
|
---|
1530 | size_t cbRead = data.size();
|
---|
1531 | if (cbRead)
|
---|
1532 | {
|
---|
1533 | if (cbRead <= cbData)
|
---|
1534 | {
|
---|
1535 | /* Copy data from event into our buffer. */
|
---|
1536 | memcpy(pvData, data.raw(), data.size());
|
---|
1537 | }
|
---|
1538 | else
|
---|
1539 | vrc = VERR_BUFFER_OVERFLOW;
|
---|
1540 |
|
---|
1541 | LogFlowThisFunc(("Read %zu bytes (uHandle=%RU32), rc=%Rrc\n",
|
---|
1542 | cbRead, uHandleEvent, vrc));
|
---|
1543 | }
|
---|
1544 | }
|
---|
1545 |
|
---|
1546 | if ( RT_SUCCESS(vrc)
|
---|
1547 | && pcbRead)
|
---|
1548 | {
|
---|
1549 | ULONG cbRead;
|
---|
1550 | hr = pProcessEvent->COMGETTER(Processed)(&cbRead);
|
---|
1551 | ComAssertComRC(hr);
|
---|
1552 | *pcbRead = (uint32_t)cbRead;
|
---|
1553 | }
|
---|
1554 |
|
---|
1555 | break;
|
---|
1556 | }
|
---|
1557 | else if (FAILED(hr))
|
---|
1558 | vrc = VERR_COM_UNEXPECTED;
|
---|
1559 | }
|
---|
1560 | else
|
---|
1561 | vrc = VWRN_GSTCTL_OBJECTSTATE_CHANGED;
|
---|
1562 | }
|
---|
1563 |
|
---|
1564 | } while (vrc == VINF_SUCCESS);
|
---|
1565 |
|
---|
1566 | if ( vrc != VINF_SUCCESS
|
---|
1567 | && pcbRead)
|
---|
1568 | {
|
---|
1569 | *pcbRead = 0;
|
---|
1570 | }
|
---|
1571 |
|
---|
1572 | LogFlowFuncLeaveRC(vrc);
|
---|
1573 | return vrc;
|
---|
1574 | }
|
---|
1575 |
|
---|
1576 | int GuestProcess::i_waitForStatusChange(GuestWaitEvent *pEvent, uint32_t uTimeoutMS,
|
---|
1577 | ProcessStatus_T *pProcessStatus, int *pGuestRc)
|
---|
1578 | {
|
---|
1579 | AssertPtrReturn(pEvent, VERR_INVALID_POINTER);
|
---|
1580 | /* pProcessStatus is optional. */
|
---|
1581 | /* pGuestRc is optional. */
|
---|
1582 |
|
---|
1583 | VBoxEventType_T evtType;
|
---|
1584 | ComPtr<IEvent> pIEvent;
|
---|
1585 | int vrc = waitForEvent(pEvent, uTimeoutMS,
|
---|
1586 | &evtType, pIEvent.asOutParam());
|
---|
1587 | if (RT_SUCCESS(vrc))
|
---|
1588 | {
|
---|
1589 | Assert(evtType == VBoxEventType_OnGuestProcessStateChanged);
|
---|
1590 | ComPtr<IGuestProcessStateChangedEvent> pProcessEvent = pIEvent;
|
---|
1591 | Assert(!pProcessEvent.isNull());
|
---|
1592 |
|
---|
1593 | ProcessStatus_T procStatus;
|
---|
1594 | HRESULT hr = pProcessEvent->COMGETTER(Status)(&procStatus);
|
---|
1595 | ComAssertComRC(hr);
|
---|
1596 | if (pProcessStatus)
|
---|
1597 | *pProcessStatus = procStatus;
|
---|
1598 |
|
---|
1599 | ComPtr<IVirtualBoxErrorInfo> errorInfo;
|
---|
1600 | hr = pProcessEvent->COMGETTER(Error)(errorInfo.asOutParam());
|
---|
1601 | ComAssertComRC(hr);
|
---|
1602 |
|
---|
1603 | LONG lGuestRc;
|
---|
1604 | hr = errorInfo->COMGETTER(ResultDetail)(&lGuestRc);
|
---|
1605 | ComAssertComRC(hr);
|
---|
1606 |
|
---|
1607 | LogFlowThisFunc(("Got procStatus=%RU32, guestRc=%RI32 (%Rrc)\n",
|
---|
1608 | procStatus, lGuestRc, lGuestRc));
|
---|
1609 |
|
---|
1610 | if (RT_FAILURE((int)lGuestRc))
|
---|
1611 | vrc = VERR_GSTCTL_GUEST_ERROR;
|
---|
1612 |
|
---|
1613 | if (pGuestRc)
|
---|
1614 | *pGuestRc = (int)lGuestRc;
|
---|
1615 | }
|
---|
1616 |
|
---|
1617 | LogFlowFuncLeaveRC(vrc);
|
---|
1618 | return vrc;
|
---|
1619 | }
|
---|
1620 |
|
---|
1621 | /* static */
|
---|
1622 | bool GuestProcess::i_waitResultImpliesEx(ProcessWaitResult_T waitResult,
|
---|
1623 | ProcessStatus_T procStatus, uint32_t uProcFlags,
|
---|
1624 | uint32_t uProtocol)
|
---|
1625 | {
|
---|
1626 | bool fImplies;
|
---|
1627 |
|
---|
1628 | switch (waitResult)
|
---|
1629 | {
|
---|
1630 | case ProcessWaitResult_Start:
|
---|
1631 | fImplies = procStatus == ProcessStatus_Started;
|
---|
1632 | break;
|
---|
1633 |
|
---|
1634 | case ProcessWaitResult_Terminate:
|
---|
1635 | fImplies = ( procStatus == ProcessStatus_TerminatedNormally
|
---|
1636 | || procStatus == ProcessStatus_TerminatedSignal
|
---|
1637 | || procStatus == ProcessStatus_TerminatedAbnormally
|
---|
1638 | || procStatus == ProcessStatus_TimedOutKilled
|
---|
1639 | || procStatus == ProcessStatus_TimedOutAbnormally
|
---|
1640 | || procStatus == ProcessStatus_Down
|
---|
1641 | || procStatus == ProcessStatus_Error);
|
---|
1642 | break;
|
---|
1643 |
|
---|
1644 | default:
|
---|
1645 | fImplies = false;
|
---|
1646 | break;
|
---|
1647 | }
|
---|
1648 |
|
---|
1649 | return fImplies;
|
---|
1650 | }
|
---|
1651 |
|
---|
1652 | int GuestProcess::i_writeData(uint32_t uHandle, uint32_t uFlags,
|
---|
1653 | void *pvData, size_t cbData, uint32_t uTimeoutMS, uint32_t *puWritten, int *pGuestRc)
|
---|
1654 | {
|
---|
1655 | LogFlowThisFunc(("uPID=%RU32, uHandle=%RU32, uFlags=%RU32, pvData=%p, cbData=%RU32, uTimeoutMS=%RU32, puWritten=%p, pGuestRc=%p\n",
|
---|
1656 | mData.mPID, uHandle, uFlags, pvData, cbData, uTimeoutMS, puWritten, pGuestRc));
|
---|
1657 | /* All is optional. There can be 0 byte writes. */
|
---|
1658 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1659 |
|
---|
1660 | if (mData.mStatus != ProcessStatus_Started)
|
---|
1661 | {
|
---|
1662 | if (puWritten)
|
---|
1663 | *puWritten = 0;
|
---|
1664 | if (pGuestRc)
|
---|
1665 | *pGuestRc = VINF_SUCCESS;
|
---|
1666 | return VINF_SUCCESS; /* Not available for writing (anymore). */
|
---|
1667 | }
|
---|
1668 |
|
---|
1669 | int vrc;
|
---|
1670 |
|
---|
1671 | GuestWaitEvent *pEvent = NULL;
|
---|
1672 | GuestEventTypes eventTypes;
|
---|
1673 | try
|
---|
1674 | {
|
---|
1675 | /*
|
---|
1676 | * On Guest Additions < 4.3 there is no guarantee that the process status
|
---|
1677 | * change arrives *after* the input event, e.g. if this was the last input
|
---|
1678 | * block being written and the process will report status "terminate".
|
---|
1679 | * So just skip checking for process status change and only wait for the
|
---|
1680 | * input event.
|
---|
1681 | */
|
---|
1682 | if (mSession->i_getProtocolVersion() >= 2)
|
---|
1683 | eventTypes.push_back(VBoxEventType_OnGuestProcessStateChanged);
|
---|
1684 | eventTypes.push_back(VBoxEventType_OnGuestProcessInputNotify);
|
---|
1685 |
|
---|
1686 | vrc = registerWaitEvent(eventTypes, &pEvent);
|
---|
1687 | }
|
---|
1688 | catch (std::bad_alloc)
|
---|
1689 | {
|
---|
1690 | vrc = VERR_NO_MEMORY;
|
---|
1691 | }
|
---|
1692 |
|
---|
1693 | if (RT_FAILURE(vrc))
|
---|
1694 | return vrc;
|
---|
1695 |
|
---|
1696 | VBOXHGCMSVCPARM paParms[5];
|
---|
1697 | int i = 0;
|
---|
1698 | paParms[i++].setUInt32(pEvent->ContextID());
|
---|
1699 | paParms[i++].setUInt32(mData.mPID);
|
---|
1700 | paParms[i++].setUInt32(uFlags);
|
---|
1701 | paParms[i++].setPointer(pvData, (uint32_t)cbData);
|
---|
1702 | paParms[i++].setUInt32((uint32_t)cbData);
|
---|
1703 |
|
---|
1704 | alock.release(); /* Drop the write lock before sending. */
|
---|
1705 |
|
---|
1706 | uint32_t cbProcessed = 0;
|
---|
1707 | vrc = sendCommand(HOST_EXEC_SET_INPUT, i, paParms);
|
---|
1708 | if (RT_SUCCESS(vrc))
|
---|
1709 | {
|
---|
1710 | ProcessInputStatus_T inputStatus;
|
---|
1711 | vrc = i_waitForInputNotify(pEvent, uHandle, uTimeoutMS,
|
---|
1712 | &inputStatus, &cbProcessed);
|
---|
1713 | if (RT_SUCCESS(vrc))
|
---|
1714 | {
|
---|
1715 | /** @todo Set guestRc. */
|
---|
1716 |
|
---|
1717 | if (puWritten)
|
---|
1718 | *puWritten = cbProcessed;
|
---|
1719 | }
|
---|
1720 | /** @todo Error handling. */
|
---|
1721 | }
|
---|
1722 |
|
---|
1723 | unregisterWaitEvent(pEvent);
|
---|
1724 |
|
---|
1725 | LogFlowThisFunc(("Returning cbProcessed=%RU32, rc=%Rrc\n",
|
---|
1726 | cbProcessed, vrc));
|
---|
1727 | return vrc;
|
---|
1728 | }
|
---|
1729 |
|
---|
1730 | // implementation of public methods
|
---|
1731 | /////////////////////////////////////////////////////////////////////////////
|
---|
1732 |
|
---|
1733 | HRESULT GuestProcess::read(ULONG aHandle, ULONG aToRead, ULONG aTimeoutMS, std::vector<BYTE> &aData)
|
---|
1734 | {
|
---|
1735 | LogFlowThisFuncEnter();
|
---|
1736 |
|
---|
1737 | if (aToRead == 0)
|
---|
1738 | return setError(E_INVALIDARG, tr("The size to read is zero"));
|
---|
1739 |
|
---|
1740 | aData.resize(aToRead);
|
---|
1741 |
|
---|
1742 | HRESULT hr = S_OK;
|
---|
1743 |
|
---|
1744 | uint32_t cbRead; int guestRc;
|
---|
1745 | int vrc = i_readData(aHandle, aToRead, aTimeoutMS, &aData.front(), aToRead, &cbRead, &guestRc);
|
---|
1746 | if (RT_SUCCESS(vrc))
|
---|
1747 | {
|
---|
1748 | if (aData.size() != cbRead)
|
---|
1749 | aData.resize(cbRead);
|
---|
1750 | }
|
---|
1751 | else
|
---|
1752 | {
|
---|
1753 | aData.resize(0);
|
---|
1754 |
|
---|
1755 | switch (vrc)
|
---|
1756 | {
|
---|
1757 | case VERR_GSTCTL_GUEST_ERROR:
|
---|
1758 | hr = GuestProcess::i_setErrorExternal(this, guestRc);
|
---|
1759 | break;
|
---|
1760 |
|
---|
1761 | default:
|
---|
1762 | hr = setError(VBOX_E_IPRT_ERROR,
|
---|
1763 | tr("Reading from process \"%s\" (PID %RU32) failed: %Rrc"),
|
---|
1764 | mData.mProcess.mExecutable.c_str(), mData.mPID, vrc);
|
---|
1765 | break;
|
---|
1766 | }
|
---|
1767 | }
|
---|
1768 |
|
---|
1769 | LogFlowThisFunc(("rc=%Rrc, cbRead=%RU32\n", vrc, cbRead));
|
---|
1770 |
|
---|
1771 | LogFlowFuncLeaveRC(vrc);
|
---|
1772 | return hr;
|
---|
1773 | }
|
---|
1774 |
|
---|
1775 | HRESULT GuestProcess::terminate()
|
---|
1776 | {
|
---|
1777 | HRESULT hr = S_OK;
|
---|
1778 |
|
---|
1779 | int guestRc;
|
---|
1780 | int vrc = i_terminateProcess(30 * 1000 /* Timeout in ms */,
|
---|
1781 | &guestRc);
|
---|
1782 | if (RT_FAILURE(vrc))
|
---|
1783 | {
|
---|
1784 | switch (vrc)
|
---|
1785 | {
|
---|
1786 | case VERR_GSTCTL_GUEST_ERROR:
|
---|
1787 | hr = GuestProcess::i_setErrorExternal(this, guestRc);
|
---|
1788 | break;
|
---|
1789 |
|
---|
1790 | case VERR_NOT_SUPPORTED:
|
---|
1791 | hr = setError(VBOX_E_IPRT_ERROR,
|
---|
1792 | tr("Terminating process \"%s\" (PID %RU32) not supported by installed Guest Additions"),
|
---|
1793 | mData.mProcess.mExecutable.c_str(), mData.mPID);
|
---|
1794 | break;
|
---|
1795 |
|
---|
1796 | default:
|
---|
1797 | hr = setError(VBOX_E_IPRT_ERROR,
|
---|
1798 | tr("Terminating process \"%s\" (PID %RU32) failed: %Rrc"),
|
---|
1799 | mData.mProcess.mExecutable.c_str(), mData.mPID, vrc);
|
---|
1800 | break;
|
---|
1801 | }
|
---|
1802 | }
|
---|
1803 |
|
---|
1804 | /* Remove process from guest session list. Now only API clients
|
---|
1805 | * still can hold references to it. */
|
---|
1806 | AssertPtr(mSession);
|
---|
1807 | int rc2 = mSession->i_processRemoveFromList(this);
|
---|
1808 | if (RT_SUCCESS(vrc))
|
---|
1809 | vrc = rc2;
|
---|
1810 |
|
---|
1811 | LogFlowFuncLeaveRC(vrc);
|
---|
1812 | return hr;
|
---|
1813 | }
|
---|
1814 |
|
---|
1815 | HRESULT GuestProcess::waitFor(ULONG aWaitFor,
|
---|
1816 | ULONG aTimeoutMS,
|
---|
1817 | ProcessWaitResult_T *aReason)
|
---|
1818 | {
|
---|
1819 | /*
|
---|
1820 | * Note: Do not hold any locks here while waiting!
|
---|
1821 | */
|
---|
1822 | HRESULT hr = S_OK;
|
---|
1823 |
|
---|
1824 | int guestRc;
|
---|
1825 | ProcessWaitResult_T waitResult;
|
---|
1826 | int vrc = i_waitFor(aWaitFor, aTimeoutMS, waitResult, &guestRc);
|
---|
1827 | if (RT_SUCCESS(vrc))
|
---|
1828 | {
|
---|
1829 | *aReason = waitResult;
|
---|
1830 | }
|
---|
1831 | else
|
---|
1832 | {
|
---|
1833 | switch (vrc)
|
---|
1834 | {
|
---|
1835 | case VERR_GSTCTL_GUEST_ERROR:
|
---|
1836 | hr = GuestProcess::i_setErrorExternal(this, guestRc);
|
---|
1837 | break;
|
---|
1838 |
|
---|
1839 | case VERR_TIMEOUT:
|
---|
1840 | *aReason = ProcessWaitResult_Timeout;
|
---|
1841 | break;
|
---|
1842 |
|
---|
1843 | default:
|
---|
1844 | hr = setError(VBOX_E_IPRT_ERROR,
|
---|
1845 | tr("Waiting for process \"%s\" (PID %RU32) failed: %Rrc"),
|
---|
1846 | mData.mProcess.mExecutable.c_str(), mData.mPID, vrc);
|
---|
1847 | break;
|
---|
1848 | }
|
---|
1849 | }
|
---|
1850 |
|
---|
1851 | LogFlowFuncLeaveRC(vrc);
|
---|
1852 | return hr;
|
---|
1853 | }
|
---|
1854 |
|
---|
1855 | HRESULT GuestProcess::waitForArray(const std::vector<ProcessWaitForFlag_T> &aWaitFor,
|
---|
1856 | ULONG aTimeoutMS, ProcessWaitResult_T *aReason)
|
---|
1857 | {
|
---|
1858 | /*
|
---|
1859 | * Note: Do not hold any locks here while waiting!
|
---|
1860 | */
|
---|
1861 | uint32_t fWaitFor = ProcessWaitForFlag_None;
|
---|
1862 | for (size_t i = 0; i < aWaitFor.size(); i++)
|
---|
1863 | fWaitFor |= aWaitFor[i];
|
---|
1864 |
|
---|
1865 | return WaitFor(fWaitFor, aTimeoutMS, aReason);
|
---|
1866 | }
|
---|
1867 |
|
---|
1868 | HRESULT GuestProcess::write(ULONG aHandle, ULONG aFlags, const std::vector<BYTE> &aData,
|
---|
1869 | ULONG aTimeoutMS, ULONG *aWritten)
|
---|
1870 | {
|
---|
1871 | LogFlowThisFuncEnter();
|
---|
1872 |
|
---|
1873 | HRESULT hr = S_OK;
|
---|
1874 |
|
---|
1875 | uint32_t cbWritten; int guestRc;
|
---|
1876 | uint32_t cbData = (uint32_t)aData.size();
|
---|
1877 | void *pvData = cbData > 0? (void *)&aData.front(): NULL;
|
---|
1878 | int vrc = i_writeData(aHandle, aFlags, pvData, cbData, aTimeoutMS, &cbWritten, &guestRc);
|
---|
1879 | if (RT_FAILURE(vrc))
|
---|
1880 | {
|
---|
1881 | switch (vrc)
|
---|
1882 | {
|
---|
1883 | case VERR_GSTCTL_GUEST_ERROR:
|
---|
1884 | hr = GuestProcess::i_setErrorExternal(this, guestRc);
|
---|
1885 | break;
|
---|
1886 |
|
---|
1887 | default:
|
---|
1888 | hr = setError(VBOX_E_IPRT_ERROR,
|
---|
1889 | tr("Writing to process \"%s\" (PID %RU32) failed: %Rrc"),
|
---|
1890 | mData.mProcess.mExecutable.c_str(), mData.mPID, vrc);
|
---|
1891 | break;
|
---|
1892 | }
|
---|
1893 | }
|
---|
1894 |
|
---|
1895 | LogFlowThisFunc(("rc=%Rrc, aWritten=%RU32\n", vrc, cbWritten));
|
---|
1896 |
|
---|
1897 | *aWritten = (ULONG)cbWritten;
|
---|
1898 |
|
---|
1899 | LogFlowFuncLeaveRC(vrc);
|
---|
1900 | return hr;
|
---|
1901 | }
|
---|
1902 |
|
---|
1903 | HRESULT GuestProcess::writeArray(ULONG aHandle, const std::vector<ProcessInputFlag_T> &aFlags,
|
---|
1904 | const std::vector<BYTE> &aData, ULONG aTimeoutMS, ULONG *aWritten)
|
---|
1905 | {
|
---|
1906 | LogFlowThisFuncEnter();
|
---|
1907 |
|
---|
1908 | /*
|
---|
1909 | * Note: Do not hold any locks here while writing!
|
---|
1910 | */
|
---|
1911 | ULONG fWrite = ProcessInputFlag_None;
|
---|
1912 | for (size_t i = 0; i < aFlags.size(); i++)
|
---|
1913 | fWrite |= aFlags[i];
|
---|
1914 |
|
---|
1915 | return write(aHandle, fWrite, aData, aTimeoutMS, aWritten);
|
---|
1916 | }
|
---|
1917 |
|
---|
1918 | ///////////////////////////////////////////////////////////////////////////////
|
---|
1919 |
|
---|
1920 | GuestProcessTool::GuestProcessTool(void)
|
---|
1921 | : pSession(NULL),
|
---|
1922 | pProcess(NULL)
|
---|
1923 | {
|
---|
1924 | }
|
---|
1925 |
|
---|
1926 | GuestProcessTool::~GuestProcessTool(void)
|
---|
1927 | {
|
---|
1928 | i_terminate(30 * 1000, NULL /* pGuestRc */);
|
---|
1929 | }
|
---|
1930 |
|
---|
1931 | int GuestProcessTool::Init(GuestSession *pGuestSession, const GuestProcessStartupInfo &startupInfo,
|
---|
1932 | bool fAsync, int *pGuestRc)
|
---|
1933 | {
|
---|
1934 | LogFlowThisFunc(("pGuestSession=%p, exe=%s, fAsync=%RTbool\n",
|
---|
1935 | pGuestSession, startupInfo.mExecutable.c_str(), fAsync));
|
---|
1936 |
|
---|
1937 | AssertPtrReturn(pGuestSession, VERR_INVALID_POINTER);
|
---|
1938 | Assert(startupInfo.mArguments[0] == startupInfo.mExecutable);
|
---|
1939 |
|
---|
1940 | pSession = pGuestSession;
|
---|
1941 | mStartupInfo = startupInfo;
|
---|
1942 |
|
---|
1943 | /* Make sure the process is hidden. */
|
---|
1944 | mStartupInfo.mFlags |= ProcessCreateFlag_Hidden;
|
---|
1945 |
|
---|
1946 | int vrc = pSession->i_processCreateExInternal(mStartupInfo, pProcess);
|
---|
1947 | if (RT_SUCCESS(vrc))
|
---|
1948 | {
|
---|
1949 | int guestRc;
|
---|
1950 | vrc = fAsync
|
---|
1951 | ? pProcess->i_startProcessAsync()
|
---|
1952 | : pProcess->i_startProcess(30 * 1000 /* 30s timeout */, &guestRc);
|
---|
1953 |
|
---|
1954 | if ( RT_SUCCESS(vrc)
|
---|
1955 | && !fAsync
|
---|
1956 | && RT_FAILURE(guestRc)
|
---|
1957 | )
|
---|
1958 | {
|
---|
1959 | if (pGuestRc)
|
---|
1960 | *pGuestRc = guestRc;
|
---|
1961 | vrc = VERR_GSTCTL_GUEST_ERROR;
|
---|
1962 | }
|
---|
1963 | }
|
---|
1964 |
|
---|
1965 | LogFlowFuncLeaveRC(vrc);
|
---|
1966 | return vrc;
|
---|
1967 | }
|
---|
1968 |
|
---|
1969 | int GuestProcessTool::i_getCurrentBlock(uint32_t uHandle, GuestProcessStreamBlock &strmBlock)
|
---|
1970 | {
|
---|
1971 | const GuestProcessStream *pStream = NULL;
|
---|
1972 | if (uHandle == OUTPUT_HANDLE_ID_STDOUT)
|
---|
1973 | pStream = &mStdOut;
|
---|
1974 | else if (uHandle == OUTPUT_HANDLE_ID_STDERR)
|
---|
1975 | pStream = &mStdErr;
|
---|
1976 |
|
---|
1977 | if (!pStream)
|
---|
1978 | return VERR_INVALID_PARAMETER;
|
---|
1979 |
|
---|
1980 | int vrc;
|
---|
1981 | do
|
---|
1982 | {
|
---|
1983 | /* Try parsing the data to see if the current block is complete. */
|
---|
1984 | vrc = mStdOut.ParseBlock(strmBlock);
|
---|
1985 | if (strmBlock.GetCount())
|
---|
1986 | break;
|
---|
1987 | } while (RT_SUCCESS(vrc));
|
---|
1988 |
|
---|
1989 | LogFlowThisFunc(("rc=%Rrc, %RU64 pairs\n",
|
---|
1990 | vrc, strmBlock.GetCount()));
|
---|
1991 | return vrc;
|
---|
1992 | }
|
---|
1993 |
|
---|
1994 | int GuestProcessTool::i_getRc(void) const
|
---|
1995 | {
|
---|
1996 | LONG exitCode;
|
---|
1997 | HRESULT hr = pProcess->COMGETTER(ExitCode(&exitCode));
|
---|
1998 | Assert(SUCCEEDED(hr));
|
---|
1999 |
|
---|
2000 | return GuestProcessTool::i_exitCodeToRc(mStartupInfo, exitCode);
|
---|
2001 | }
|
---|
2002 |
|
---|
2003 | bool GuestProcessTool::i_isRunning(void)
|
---|
2004 | {
|
---|
2005 | AssertReturn(!pProcess.isNull(), false);
|
---|
2006 |
|
---|
2007 | ProcessStatus_T procStatus = ProcessStatus_Undefined;
|
---|
2008 | HRESULT hr = pProcess->COMGETTER(Status(&procStatus));
|
---|
2009 | Assert(SUCCEEDED(hr));
|
---|
2010 |
|
---|
2011 | if ( procStatus == ProcessStatus_Started
|
---|
2012 | || procStatus == ProcessStatus_Paused
|
---|
2013 | || procStatus == ProcessStatus_Terminating)
|
---|
2014 | {
|
---|
2015 | return true;
|
---|
2016 | }
|
---|
2017 |
|
---|
2018 | return false;
|
---|
2019 | }
|
---|
2020 |
|
---|
2021 | /* static */
|
---|
2022 | int GuestProcessTool::i_run( GuestSession *pGuestSession,
|
---|
2023 | const GuestProcessStartupInfo &startupInfo,
|
---|
2024 | int *pGuestRc /* = NULL */)
|
---|
2025 | {
|
---|
2026 | int guestRc;
|
---|
2027 |
|
---|
2028 | GuestProcessToolErrorInfo errorInfo;
|
---|
2029 | int vrc = i_runErrorInfo(pGuestSession, startupInfo, errorInfo);
|
---|
2030 | if (RT_SUCCESS(vrc))
|
---|
2031 | {
|
---|
2032 | if (errorInfo.guestRc == VWRN_GSTCTL_PROCESS_EXIT_CODE)
|
---|
2033 | {
|
---|
2034 | guestRc = GuestProcessTool::i_exitCodeToRc(startupInfo, errorInfo.lExitCode);
|
---|
2035 | }
|
---|
2036 | else
|
---|
2037 | guestRc = errorInfo.guestRc;
|
---|
2038 |
|
---|
2039 | if (pGuestRc)
|
---|
2040 | *pGuestRc = guestRc;
|
---|
2041 | }
|
---|
2042 |
|
---|
2043 | return vrc;
|
---|
2044 | }
|
---|
2045 |
|
---|
2046 | /* static */
|
---|
2047 | int GuestProcessTool::i_runErrorInfo( GuestSession *pGuestSession,
|
---|
2048 | const GuestProcessStartupInfo &startupInfo,
|
---|
2049 | GuestProcessToolErrorInfo &errorInfo)
|
---|
2050 | {
|
---|
2051 | return i_runExErrorInfo(pGuestSession, startupInfo,
|
---|
2052 | NULL /* pStrmOutObjects */, 0 /* cStrmOutObjects */,
|
---|
2053 | errorInfo);
|
---|
2054 | }
|
---|
2055 |
|
---|
2056 | /* static */
|
---|
2057 | int GuestProcessTool::i_runEx( GuestSession *pGuestSession,
|
---|
2058 | const GuestProcessStartupInfo &startupInfo,
|
---|
2059 | GuestCtrlStreamObjects *pStrmOutObjects,
|
---|
2060 | uint32_t cStrmOutObjects,
|
---|
2061 | int *pGuestRc /* = NULL */)
|
---|
2062 | {
|
---|
2063 | int guestRc;
|
---|
2064 |
|
---|
2065 | GuestProcessToolErrorInfo errorInfo;
|
---|
2066 | int vrc = GuestProcessTool::i_runExErrorInfo(pGuestSession, startupInfo, pStrmOutObjects, cStrmOutObjects, errorInfo);
|
---|
2067 | if (RT_SUCCESS(vrc))
|
---|
2068 | {
|
---|
2069 | if (errorInfo.guestRc == VWRN_GSTCTL_PROCESS_EXIT_CODE)
|
---|
2070 | {
|
---|
2071 | guestRc = GuestProcessTool::i_exitCodeToRc(startupInfo, errorInfo.lExitCode);
|
---|
2072 | }
|
---|
2073 | else
|
---|
2074 | guestRc = errorInfo.guestRc;
|
---|
2075 |
|
---|
2076 | if (pGuestRc)
|
---|
2077 | *pGuestRc = guestRc;
|
---|
2078 | }
|
---|
2079 |
|
---|
2080 | return vrc;
|
---|
2081 | }
|
---|
2082 |
|
---|
2083 | /**
|
---|
2084 | * Static helper function to start and wait for output of a certain toolbox tool.
|
---|
2085 | *
|
---|
2086 | * This is the extended version, which addds the possibility of retrieving parsable so-called guest stream
|
---|
2087 | * objects. Those objects are issued on the guest side as part of VBoxService's toolbox tools (think of a BusyBox-like approach)
|
---|
2088 | * on stdout and can be used on the host side to retrieve more information about the actual command issued on the guest side.
|
---|
2089 | *
|
---|
2090 | * @return IPRT status code.
|
---|
2091 | * @param pGuestSession Guest control session to use for starting the toolbox tool in.
|
---|
2092 | * @param startupInfo Startup information about the toolbox tool.
|
---|
2093 | * @param pStrmOutObjects Pointer to stream objects array to use for retrieving the output of the toolbox tool.
|
---|
2094 | * Optional.
|
---|
2095 | * @param cStrmOutObjects Number of stream objects passed in. Optional.
|
---|
2096 | * @param errorInfo Error information returned for error handling.
|
---|
2097 | */
|
---|
2098 | /* static */
|
---|
2099 | int GuestProcessTool::i_runExErrorInfo( GuestSession *pGuestSession,
|
---|
2100 | const GuestProcessStartupInfo &startupInfo,
|
---|
2101 | GuestCtrlStreamObjects *paStrmOutObjects,
|
---|
2102 | uint32_t cStrmOutObjects,
|
---|
2103 | GuestProcessToolErrorInfo &errorInfo)
|
---|
2104 | {
|
---|
2105 | AssertPtrReturn(pGuestSession, VERR_INVALID_POINTER);
|
---|
2106 | /* paStrmOutObjects is optional. */
|
---|
2107 |
|
---|
2108 | /** @todo Check if this is a valid toolbox. */
|
---|
2109 |
|
---|
2110 | GuestProcessTool procTool;
|
---|
2111 | int vrc = procTool.Init(pGuestSession, startupInfo, false /* Async */, &errorInfo.guestRc);
|
---|
2112 | if (RT_SUCCESS(vrc))
|
---|
2113 | {
|
---|
2114 | while (cStrmOutObjects--)
|
---|
2115 | {
|
---|
2116 | try
|
---|
2117 | {
|
---|
2118 | GuestProcessStreamBlock strmBlk;
|
---|
2119 | vrc = procTool.i_waitEx( paStrmOutObjects
|
---|
2120 | ? GUESTPROCESSTOOL_FLAG_STDOUT_BLOCK
|
---|
2121 | : GUESTPROCESSTOOL_FLAG_NONE, &strmBlk, &errorInfo.guestRc);
|
---|
2122 | if (paStrmOutObjects)
|
---|
2123 | paStrmOutObjects->push_back(strmBlk);
|
---|
2124 | }
|
---|
2125 | catch (std::bad_alloc)
|
---|
2126 | {
|
---|
2127 | vrc = VERR_NO_MEMORY;
|
---|
2128 | }
|
---|
2129 | }
|
---|
2130 | }
|
---|
2131 |
|
---|
2132 | if (RT_SUCCESS(vrc))
|
---|
2133 | {
|
---|
2134 | /* Make sure the process runs until completion. */
|
---|
2135 | vrc = procTool.i_wait(GUESTPROCESSTOOL_FLAG_NONE, &errorInfo.guestRc);
|
---|
2136 | if (RT_SUCCESS(vrc))
|
---|
2137 | errorInfo.guestRc = procTool.i_terminatedOk(&errorInfo.lExitCode);
|
---|
2138 | }
|
---|
2139 |
|
---|
2140 | LogFlowFunc(("Returned rc=%Rrc, guestRc=%Rrc, exitCode=%ld\n", vrc, errorInfo.guestRc, errorInfo.lExitCode));
|
---|
2141 | return vrc;
|
---|
2142 | }
|
---|
2143 |
|
---|
2144 | /**
|
---|
2145 | * Reports if the tool has been run correctly.
|
---|
2146 | *
|
---|
2147 | * @return Will return VWRN_GSTCTL_PROCESS_EXIT_CODE if the tool process returned an exit code <> 0,
|
---|
2148 | * VERR_GSTCTL_PROCESS_WRONG_STATE if the tool process is in a wrong state (e.g. still running),
|
---|
2149 | * or VINF_SUCCESS otherwise.
|
---|
2150 | *
|
---|
2151 | * @param plExitCode Exit code of the tool. Optional.
|
---|
2152 | */
|
---|
2153 | int GuestProcessTool::i_terminatedOk(LONG *plExitCode /* = NULL */)
|
---|
2154 | {
|
---|
2155 | Assert(!pProcess.isNull());
|
---|
2156 | /* pExitCode is optional. */
|
---|
2157 |
|
---|
2158 | int vrc;
|
---|
2159 | if (!i_isRunning())
|
---|
2160 | {
|
---|
2161 | LONG lExitCode;
|
---|
2162 | HRESULT hr = pProcess->COMGETTER(ExitCode(&lExitCode));
|
---|
2163 | Assert(SUCCEEDED(hr));
|
---|
2164 |
|
---|
2165 | if (plExitCode)
|
---|
2166 | *plExitCode = lExitCode;
|
---|
2167 |
|
---|
2168 | vrc = (lExitCode != 0)
|
---|
2169 | ? VWRN_GSTCTL_PROCESS_EXIT_CODE : VINF_SUCCESS;
|
---|
2170 | }
|
---|
2171 | else
|
---|
2172 | vrc = VERR_GSTCTL_PROCESS_WRONG_STATE;
|
---|
2173 |
|
---|
2174 | LogFlowFuncLeaveRC(vrc);
|
---|
2175 | return vrc;
|
---|
2176 | }
|
---|
2177 |
|
---|
2178 | int GuestProcessTool::i_wait(uint32_t fFlags, int *pGuestRc)
|
---|
2179 | {
|
---|
2180 | return i_waitEx(fFlags, NULL /* pStrmBlkOut */, pGuestRc);
|
---|
2181 | }
|
---|
2182 |
|
---|
2183 | int GuestProcessTool::i_waitEx(uint32_t fFlags, GuestProcessStreamBlock *pStrmBlkOut, int *pGuestRc)
|
---|
2184 | {
|
---|
2185 | LogFlowThisFunc(("fFlags=0x%x, pStreamBlock=%p, pGuestRc=%p\n",
|
---|
2186 | fFlags, pStrmBlkOut, pGuestRc));
|
---|
2187 |
|
---|
2188 | /* Can we parse the next block without waiting? */
|
---|
2189 | int vrc;
|
---|
2190 | if (fFlags & GUESTPROCESSTOOL_FLAG_STDOUT_BLOCK)
|
---|
2191 | {
|
---|
2192 | AssertPtr(pStrmBlkOut);
|
---|
2193 | vrc = i_getCurrentBlock(OUTPUT_HANDLE_ID_STDOUT, *pStrmBlkOut);
|
---|
2194 | if (RT_SUCCESS(vrc))
|
---|
2195 | return vrc;
|
---|
2196 | /* else do the the waiting below. */
|
---|
2197 | }
|
---|
2198 |
|
---|
2199 | /* Do the waiting. */
|
---|
2200 | uint32_t fWaitFlags = ProcessWaitForFlag_Terminate;
|
---|
2201 | if (mStartupInfo.mFlags & ProcessCreateFlag_WaitForStdOut)
|
---|
2202 | fWaitFlags |= ProcessWaitForFlag_StdOut;
|
---|
2203 | if (mStartupInfo.mFlags & ProcessCreateFlag_WaitForStdErr)
|
---|
2204 | fWaitFlags |= ProcessWaitForFlag_StdErr;
|
---|
2205 |
|
---|
2206 | /** @todo Decrease timeout while running. */
|
---|
2207 | uint64_t u64StartMS = RTTimeMilliTS();
|
---|
2208 | uint32_t uTimeoutMS = mStartupInfo.mTimeoutMS;
|
---|
2209 |
|
---|
2210 | int guestRc;
|
---|
2211 | bool fDone = false;
|
---|
2212 |
|
---|
2213 | BYTE byBuf[_64K];
|
---|
2214 | uint32_t cbRead;
|
---|
2215 |
|
---|
2216 | bool fHandleStdOut = false;
|
---|
2217 | bool fHandleStdErr = false;
|
---|
2218 |
|
---|
2219 | /**
|
---|
2220 | * Updates the elapsed time and checks if a
|
---|
2221 | * timeout happened, then breaking out of the loop.
|
---|
2222 | */
|
---|
2223 | #define UPDATE_AND_CHECK_ELAPSED_TIME() \
|
---|
2224 | u64ElapsedMS = RTTimeMilliTS() - u64StartMS; \
|
---|
2225 | if ( uTimeoutMS != RT_INDEFINITE_WAIT \
|
---|
2226 | && u64ElapsedMS >= uTimeoutMS) \
|
---|
2227 | { \
|
---|
2228 | vrc = VERR_TIMEOUT; \
|
---|
2229 | break; \
|
---|
2230 | }
|
---|
2231 |
|
---|
2232 | /**
|
---|
2233 | * Returns the remaining time (in ms).
|
---|
2234 | */
|
---|
2235 | #define GET_REMAINING_TIME \
|
---|
2236 | uTimeoutMS == RT_INDEFINITE_WAIT \
|
---|
2237 | ? RT_INDEFINITE_WAIT : uTimeoutMS - (uint32_t)u64ElapsedMS \
|
---|
2238 |
|
---|
2239 | ProcessWaitResult_T waitRes;
|
---|
2240 | do
|
---|
2241 | {
|
---|
2242 | uint64_t u64ElapsedMS;
|
---|
2243 | UPDATE_AND_CHECK_ELAPSED_TIME();
|
---|
2244 |
|
---|
2245 | vrc = pProcess->i_waitFor(fWaitFlags, GET_REMAINING_TIME,
|
---|
2246 | waitRes, &guestRc);
|
---|
2247 | if (RT_FAILURE(vrc))
|
---|
2248 | break;
|
---|
2249 |
|
---|
2250 | switch (waitRes)
|
---|
2251 | {
|
---|
2252 | case ProcessWaitResult_StdIn:
|
---|
2253 | vrc = VERR_NOT_IMPLEMENTED;
|
---|
2254 | break;
|
---|
2255 |
|
---|
2256 | case ProcessWaitResult_StdOut:
|
---|
2257 | fHandleStdOut = true;
|
---|
2258 | break;
|
---|
2259 |
|
---|
2260 | case ProcessWaitResult_StdErr:
|
---|
2261 | fHandleStdErr = true;
|
---|
2262 | break;
|
---|
2263 |
|
---|
2264 | case ProcessWaitResult_WaitFlagNotSupported:
|
---|
2265 | if (fWaitFlags & ProcessWaitForFlag_StdOut)
|
---|
2266 | fHandleStdOut = true;
|
---|
2267 | if (fWaitFlags & ProcessWaitForFlag_StdErr)
|
---|
2268 | fHandleStdErr = true;
|
---|
2269 | /* Since waiting for stdout / stderr is not supported by the guest,
|
---|
2270 | * wait a bit to not hog the CPU too much when polling for data. */
|
---|
2271 | RTThreadSleep(1); /* Optional, don't check rc. */
|
---|
2272 | break;
|
---|
2273 |
|
---|
2274 | case ProcessWaitResult_Error:
|
---|
2275 | vrc = VERR_GSTCTL_GUEST_ERROR;
|
---|
2276 | break;
|
---|
2277 |
|
---|
2278 | case ProcessWaitResult_Terminate:
|
---|
2279 | fDone = true;
|
---|
2280 | break;
|
---|
2281 |
|
---|
2282 | case ProcessWaitResult_Timeout:
|
---|
2283 | vrc = VERR_TIMEOUT;
|
---|
2284 | break;
|
---|
2285 |
|
---|
2286 | case ProcessWaitResult_Start:
|
---|
2287 | case ProcessWaitResult_Status:
|
---|
2288 | /* Not used here, just skip. */
|
---|
2289 | break;
|
---|
2290 |
|
---|
2291 | default:
|
---|
2292 | AssertMsgFailed(("Unhandled process wait result %RU32\n", waitRes));
|
---|
2293 | break;
|
---|
2294 | }
|
---|
2295 |
|
---|
2296 | if (RT_FAILURE(vrc))
|
---|
2297 | break;
|
---|
2298 |
|
---|
2299 | if (fHandleStdOut)
|
---|
2300 | {
|
---|
2301 | UPDATE_AND_CHECK_ELAPSED_TIME();
|
---|
2302 |
|
---|
2303 | cbRead = 0;
|
---|
2304 | vrc = pProcess->i_readData(OUTPUT_HANDLE_ID_STDOUT, sizeof(byBuf),
|
---|
2305 | GET_REMAINING_TIME,
|
---|
2306 | byBuf, sizeof(byBuf),
|
---|
2307 | &cbRead, &guestRc);
|
---|
2308 | if ( RT_FAILURE(vrc)
|
---|
2309 | || vrc == VWRN_GSTCTL_OBJECTSTATE_CHANGED)
|
---|
2310 | break;
|
---|
2311 |
|
---|
2312 | if (cbRead)
|
---|
2313 | {
|
---|
2314 | LogFlowThisFunc(("Received %RU32 bytes from stdout\n", cbRead));
|
---|
2315 | vrc = mStdOut.AddData(byBuf, cbRead);
|
---|
2316 |
|
---|
2317 | if ( RT_SUCCESS(vrc)
|
---|
2318 | && (fFlags & GUESTPROCESSTOOL_FLAG_STDOUT_BLOCK))
|
---|
2319 | {
|
---|
2320 | AssertPtr(pStrmBlkOut);
|
---|
2321 | vrc = i_getCurrentBlock(OUTPUT_HANDLE_ID_STDOUT, *pStrmBlkOut);
|
---|
2322 |
|
---|
2323 | /* When successful, break out of the loop because we're done
|
---|
2324 | * with reading the first stream block. */
|
---|
2325 | if (RT_SUCCESS(vrc))
|
---|
2326 | fDone = true;
|
---|
2327 | }
|
---|
2328 | }
|
---|
2329 |
|
---|
2330 | fHandleStdOut = false;
|
---|
2331 | }
|
---|
2332 |
|
---|
2333 | if (fHandleStdErr)
|
---|
2334 | {
|
---|
2335 | UPDATE_AND_CHECK_ELAPSED_TIME();
|
---|
2336 |
|
---|
2337 | cbRead = 0;
|
---|
2338 | vrc = pProcess->i_readData(OUTPUT_HANDLE_ID_STDERR, sizeof(byBuf),
|
---|
2339 | GET_REMAINING_TIME,
|
---|
2340 | byBuf, sizeof(byBuf),
|
---|
2341 | &cbRead, &guestRc);
|
---|
2342 | if ( RT_FAILURE(vrc)
|
---|
2343 | || vrc == VWRN_GSTCTL_OBJECTSTATE_CHANGED)
|
---|
2344 | break;
|
---|
2345 |
|
---|
2346 | if (cbRead)
|
---|
2347 | {
|
---|
2348 | LogFlowThisFunc(("Received %RU32 bytes from stderr\n", cbRead));
|
---|
2349 | vrc = mStdErr.AddData(byBuf, cbRead);
|
---|
2350 | }
|
---|
2351 |
|
---|
2352 | fHandleStdErr = false;
|
---|
2353 | }
|
---|
2354 |
|
---|
2355 | } while (!fDone && RT_SUCCESS(vrc));
|
---|
2356 |
|
---|
2357 | #undef UPDATE_AND_CHECK_ELAPSED_TIME
|
---|
2358 | #undef GET_REMAINING_TIME
|
---|
2359 |
|
---|
2360 | if (RT_FAILURE(guestRc))
|
---|
2361 | vrc = VERR_GSTCTL_GUEST_ERROR;
|
---|
2362 |
|
---|
2363 | LogFlowThisFunc(("Loop ended with rc=%Rrc, guestRc=%Rrc, waitRes=%RU32\n",
|
---|
2364 | vrc, guestRc, waitRes));
|
---|
2365 | if (pGuestRc)
|
---|
2366 | *pGuestRc = guestRc;
|
---|
2367 |
|
---|
2368 | LogFlowFuncLeaveRC(vrc);
|
---|
2369 | return vrc;
|
---|
2370 | }
|
---|
2371 |
|
---|
2372 | int GuestProcessTool::i_terminate(uint32_t uTimeoutMS, int *pGuestRc)
|
---|
2373 | {
|
---|
2374 | LogFlowThisFuncEnter();
|
---|
2375 |
|
---|
2376 | int rc = VINF_SUCCESS;
|
---|
2377 | if (!pProcess.isNull())
|
---|
2378 | {
|
---|
2379 | rc = pProcess->i_terminateProcess(uTimeoutMS, pGuestRc);
|
---|
2380 | pProcess.setNull();
|
---|
2381 | }
|
---|
2382 | else
|
---|
2383 | rc = VERR_NOT_FOUND;
|
---|
2384 |
|
---|
2385 | LogFlowFuncLeaveRC(rc);
|
---|
2386 | return rc;
|
---|
2387 | }
|
---|
2388 |
|
---|
2389 | /**
|
---|
2390 | * Converts a toolbox tool's exit code to an IPRT error code.
|
---|
2391 | *
|
---|
2392 | * @return int Returned IPRT error for the particular tool.
|
---|
2393 | * @param startupInfo Startup info of the toolbox tool to lookup error code for.
|
---|
2394 | * @param lExitCode The toolbox tool's exit code to lookup IPRT error for.
|
---|
2395 | */
|
---|
2396 | /* static */
|
---|
2397 | int GuestProcessTool::i_exitCodeToRc(const GuestProcessStartupInfo &startupInfo, LONG lExitCode)
|
---|
2398 | {
|
---|
2399 | if (startupInfo.mArguments.size() == 0)
|
---|
2400 | {
|
---|
2401 | AssertFailed();
|
---|
2402 | return VERR_GENERAL_FAILURE; /* Should not happen. */
|
---|
2403 | }
|
---|
2404 |
|
---|
2405 | return i_exitCodeToRc(startupInfo.mArguments[0].c_str(), lExitCode);
|
---|
2406 | }
|
---|
2407 |
|
---|
2408 | /**
|
---|
2409 | * Converts a toolbox tool's exit code to an IPRT error code.
|
---|
2410 | *
|
---|
2411 | * @return int Returned IPRT error for the particular tool.
|
---|
2412 | * @param pszTool Name of toolbox tool to lookup error code for.
|
---|
2413 | * @param rcExit The toolbox tool's exit code to lookup IPRT error for.
|
---|
2414 | */
|
---|
2415 | /* static */
|
---|
2416 | int GuestProcessTool::i_exitCodeToRc(const char *pszTool, LONG lExitCode)
|
---|
2417 | {
|
---|
2418 | AssertPtrReturn(pszTool, VERR_INVALID_POINTER);
|
---|
2419 |
|
---|
2420 | LogFlowFunc(("%s: %ld\n", pszTool, lExitCode));
|
---|
2421 |
|
---|
2422 | if (lExitCode == 0) /* No error? Bail out early. */
|
---|
2423 | return VINF_SUCCESS;
|
---|
2424 |
|
---|
2425 | if (!RTStrICmp(pszTool, VBOXSERVICE_TOOL_CAT))
|
---|
2426 | {
|
---|
2427 | switch (lExitCode)
|
---|
2428 | {
|
---|
2429 | case VBOXSERVICETOOLBOX_CAT_EXITCODE_ACCESS_DENIED: return VERR_ACCESS_DENIED;
|
---|
2430 | case VBOXSERVICETOOLBOX_CAT_EXITCODE_FILE_NOT_FOUND: return VERR_FILE_NOT_FOUND;
|
---|
2431 | case VBOXSERVICETOOLBOX_CAT_EXITCODE_PATH_NOT_FOUND: return VERR_PATH_NOT_FOUND;
|
---|
2432 | case VBOXSERVICETOOLBOX_CAT_EXITCODE_SHARING_VIOLATION: return VERR_SHARING_VIOLATION;
|
---|
2433 | default:
|
---|
2434 | break;
|
---|
2435 | }
|
---|
2436 | }
|
---|
2437 | else if (!RTStrICmp(pszTool, VBOXSERVICE_TOOL_STAT))
|
---|
2438 | {
|
---|
2439 | switch (lExitCode)
|
---|
2440 | {
|
---|
2441 | case VBOXSERVICETOOLBOX_STAT_EXITCODE_ACCESS_DENIED: return VERR_ACCESS_DENIED;
|
---|
2442 | case VBOXSERVICETOOLBOX_STAT_EXITCODE_FILE_NOT_FOUND: return VERR_FILE_NOT_FOUND;
|
---|
2443 | case VBOXSERVICETOOLBOX_STAT_EXITCODE_PATH_NOT_FOUND: return VERR_PATH_NOT_FOUND;
|
---|
2444 | default:
|
---|
2445 | break;
|
---|
2446 | }
|
---|
2447 | }
|
---|
2448 |
|
---|
2449 | AssertMsgFailed(("Error code %ld for tool '%s' not handled\n", lExitCode, pszTool));
|
---|
2450 | return VERR_GENERAL_FAILURE;
|
---|
2451 | }
|
---|
2452 |
|
---|