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