VirtualBox

source: vbox/trunk/src/VBox/Main/src-client/GuestSessionImplTasks.cpp@ 70772

Last change on this file since 70772 was 69500, checked in by vboxsync, 7 years ago

*: scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 65.9 KB
RevLine 
[42084]1/* $Id: GuestSessionImplTasks.cpp 69500 2017-10-28 15:14:05Z vboxsync $ */
2/** @file
[44869]3 * VirtualBox Main - Guest session tasks.
[42084]4 */
5
6/*
[69500]7 * Copyright (C) 2012-2017 Oracle Corporation
[42084]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
[57358]19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
[67914]22#define LOG_GROUP LOG_GROUP_GUEST_CONTROL //LOG_GROUP_MAIN_GUESTSESSION
23#include "LoggingNew.h"
24
[42105]25#include "GuestImpl.h"
[55644]26#ifndef VBOX_WITH_GUEST_CONTROL
27# error "VBOX_WITH_GUEST_CONTROL must defined in this file"
28#endif
[42084]29#include "GuestSessionImpl.h"
[42478]30#include "GuestCtrlImplPrivate.h"
[42084]31
32#include "Global.h"
33#include "AutoCaller.h"
[42923]34#include "ConsoleImpl.h"
[42566]35#include "ProgressImpl.h"
[42084]36
[42567]37#include <memory> /* For auto_ptr. */
38
[42171]39#include <iprt/env.h>
[42573]40#include <iprt/file.h> /* For CopyTo/From. */
[60629]41#include <iprt/path.h>
[42171]42
[42084]43
[57358]44/*********************************************************************************************************************************
45* Defines *
46*********************************************************************************************************************************/
[42923]47
48/**
49 * Update file flags.
[42808]50 */
[43162]51#define UPDATEFILE_FLAG_NONE 0
[43001]52/** Copy over the file from host to the
53 * guest. */
[43162]54#define UPDATEFILE_FLAG_COPY_FROM_ISO RT_BIT(0)
[43001]55/** Execute file on the guest after it has
56 * been successfully transfered. */
57#define UPDATEFILE_FLAG_EXECUTE RT_BIT(7)
[42923]58/** File is optional, does not have to be
59 * existent on the .ISO. */
[43001]60#define UPDATEFILE_FLAG_OPTIONAL RT_BIT(8)
[42749]61
[42923]62
[42566]63// session task classes
64/////////////////////////////////////////////////////////////////////////////
65
[63186]66GuestSessionTask::GuestSessionTask(GuestSession *pSession)
67 : ThreadTask("GenericGuestSessionTask")
[42566]68{
69 mSession = pSession;
70}
71
72GuestSessionTask::~GuestSessionTask(void)
73{
74}
75
[58521]76HRESULT GuestSessionTask::createAndSetProgressObject()
77{
78 LogFlowThisFunc(("Task Description = %s, pTask=%p\n", mDesc.c_str(), this));
79
80 ComObjPtr<Progress> pProgress;
81 HRESULT hr = S_OK;
82 /* Create the progress object. */
83 hr = pProgress.createObject();
84 if (FAILED(hr))
85 return VERR_COM_UNEXPECTED;
86
87 hr = pProgress->init(static_cast<IGuestSession*>(mSession),
88 Bstr(mDesc).raw(),
89 TRUE /* aCancelable */);
90 if (FAILED(hr))
91 return VERR_COM_UNEXPECTED;
92
93 mProgress = pProgress;
94
95 LogFlowFuncLeave();
96
97 return hr;
98}
99
[63186]100int GuestSessionTask::RunAsync(const Utf8Str &strDesc, ComObjPtr<Progress> &pProgress)
101{
102 LogFlowThisFunc(("strDesc=%s\n", strDesc.c_str()));
103
104 mDesc = strDesc;
105 mProgress = pProgress;
106 HRESULT hrc = createThreadWithType(RTTHREADTYPE_MAIN_HEAVY_WORKER);
107
108 LogFlowThisFunc(("Returning hrc=%Rhrc\n", hrc));
109 return Global::vboxStatusCodeToCOM(hrc);
110}
111
112
[42923]113int GuestSessionTask::getGuestProperty(const ComObjPtr<Guest> &pGuest,
114 const Utf8Str &strPath, Utf8Str &strValue)
115{
[52082]116 ComObjPtr<Console> pConsole = pGuest->i_getConsole();
[51612]117 const ComPtr<IMachine> pMachine = pConsole->i_machine();
[42923]118
119 Assert(!pMachine.isNull());
120 Bstr strTemp, strFlags;
121 LONG64 i64Timestamp;
122 HRESULT hr = pMachine->GetGuestProperty(Bstr(strPath).raw(),
123 strTemp.asOutParam(),
124 &i64Timestamp, strFlags.asOutParam());
125 if (SUCCEEDED(hr))
126 {
127 strValue = strTemp;
128 return VINF_SUCCESS;
129 }
130 return VERR_NOT_FOUND;
131}
132
[42634]133int GuestSessionTask::setProgress(ULONG uPercent)
[42566]134{
[42634]135 if (mProgress.isNull()) /* Progress is optional. */
136 return VINF_SUCCESS;
137
[42566]138 BOOL fCanceled;
[42634]139 if ( SUCCEEDED(mProgress->COMGETTER(Canceled(&fCanceled)))
140 && fCanceled)
[42566]141 return VERR_CANCELLED;
[42634]142 BOOL fCompleted;
143 if ( SUCCEEDED(mProgress->COMGETTER(Completed(&fCompleted)))
[42937]144 && fCompleted)
145 {
146 AssertMsgFailed(("Setting value of an already completed progress\n"));
[42634]147 return VINF_SUCCESS;
[42937]148 }
[42634]149 HRESULT hr = mProgress->SetCurrentOperationProgress(uPercent);
[42636]150 if (FAILED(hr))
151 return VERR_COM_UNEXPECTED;
[42566]152
153 return VINF_SUCCESS;
154}
155
156int GuestSessionTask::setProgressSuccess(void)
157{
[42634]158 if (mProgress.isNull()) /* Progress is optional. */
159 return VINF_SUCCESS;
160
[42566]161 BOOL fCanceled;
162 BOOL fCompleted;
163 if ( SUCCEEDED(mProgress->COMGETTER(Canceled(&fCanceled)))
164 && !fCanceled
165 && SUCCEEDED(mProgress->COMGETTER(Completed(&fCompleted)))
166 && !fCompleted)
167 {
[50874]168 HRESULT hr = mProgress->i_notifyComplete(S_OK);
[42567]169 if (FAILED(hr))
170 return VERR_COM_UNEXPECTED; /** @todo Find a better rc. */
[42566]171 }
172
173 return VINF_SUCCESS;
174}
175
[42693]176HRESULT GuestSessionTask::setProgressErrorMsg(HRESULT hr, const Utf8Str &strMsg)
[42566]177{
[44869]178 LogFlowFunc(("hr=%Rhrc, strMsg=%s\n",
179 hr, strMsg.c_str()));
[44863]180
[42634]181 if (mProgress.isNull()) /* Progress is optional. */
[42693]182 return hr; /* Return original rc. */
[42634]183
[42566]184 BOOL fCanceled;
185 BOOL fCompleted;
186 if ( SUCCEEDED(mProgress->COMGETTER(Canceled(&fCanceled)))
187 && !fCanceled
188 && SUCCEEDED(mProgress->COMGETTER(Completed(&fCompleted)))
189 && !fCompleted)
190 {
[50874]191 HRESULT hr2 = mProgress->i_notifyComplete(hr,
192 COM_IIDOF(IGuestSession),
193 GuestSession::getStaticComponentName(),
194 strMsg.c_str());
[42567]195 if (FAILED(hr2))
[42693]196 return hr2;
[42566]197 }
[42693]198 return hr; /* Return original rc. */
[42566]199}
200
[44863]201SessionTaskOpen::SessionTaskOpen(GuestSession *pSession,
202 uint32_t uFlags,
203 uint32_t uTimeoutMS)
204 : GuestSessionTask(pSession),
205 mFlags(uFlags),
206 mTimeoutMS(uTimeoutMS)
207{
[58521]208 m_strTaskName = "gctlSesOpen";
[44863]209}
210
211SessionTaskOpen::~SessionTaskOpen(void)
212{
213
214}
215
[63186]216int SessionTaskOpen::Run(void)
[44863]217{
218 LogFlowThisFuncEnter();
219
220 ComObjPtr<GuestSession> pSession = mSession;
221 Assert(!pSession.isNull());
222
223 AutoCaller autoCaller(pSession);
224 if (FAILED(autoCaller.rc())) return autoCaller.rc();
225
[63186]226 int vrc = pSession->i_startSessionInternal(NULL /*pvrcGuest*/);
[44863]227 /* Nothing to do here anymore. */
228
229 LogFlowFuncLeaveRC(vrc);
230 return vrc;
231}
232
[42634]233SessionTaskCopyTo::SessionTaskCopyTo(GuestSession *pSession,
[42566]234 const Utf8Str &strSource, const Utf8Str &strDest, uint32_t uFlags)
[42918]235 : GuestSessionTask(pSession),
236 mSource(strSource),
[42702]237 mSourceFile(NULL),
[42693]238 mSourceOffset(0),
239 mSourceSize(0),
[42918]240 mDest(strDest)
[42566]241{
[42634]242 mCopyFileFlags = uFlags;
[58521]243 m_strTaskName = "gctlCpyTo";
[42566]244}
245
[42716]246/** @todo Merge this and the above call and let the above call do the open/close file handling so that the
247 * inner code only has to deal with file handles. No time now ... */
[42693]248SessionTaskCopyTo::SessionTaskCopyTo(GuestSession *pSession,
[42695]249 PRTFILE pSourceFile, size_t cbSourceOffset, uint64_t cbSourceSize,
[42693]250 const Utf8Str &strDest, uint32_t uFlags)
251 : GuestSessionTask(pSession)
252{
253 mSourceFile = pSourceFile;
254 mSourceOffset = cbSourceOffset;
255 mSourceSize = cbSourceSize;
256 mDest = strDest;
257 mCopyFileFlags = uFlags;
[58521]258 m_strTaskName = "gctlCpyTo";
[42693]259}
260
[42566]261SessionTaskCopyTo::~SessionTaskCopyTo(void)
262{
263
264}
265
266int SessionTaskCopyTo::Run(void)
267{
[42573]268 LogFlowThisFuncEnter();
269
270 ComObjPtr<GuestSession> pSession = mSession;
271 Assert(!pSession.isNull());
272
273 AutoCaller autoCaller(pSession);
274 if (FAILED(autoCaller.rc())) return autoCaller.rc();
275
[42634]276 if (mCopyFileFlags)
[42693]277 {
278 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
279 Utf8StrFmt(GuestSession::tr("Copy flags (%#x) not implemented yet"),
280 mCopyFileFlags));
281 return VERR_INVALID_PARAMETER;
282 }
283
[42573]284 int rc;
285
[60622]286 RTMSINTERVAL msTimeout = 30 * 1000; /** @todo 30s timeout for all actions. Make this configurable? */
287
[42693]288 RTFILE fileLocal;
289 PRTFILE pFile = &fileLocal;
[42573]290
[42693]291 if (!mSourceFile)
[42573]292 {
293 /* Does our source file exist? */
294 if (!RTFileExists(mSource.c_str()))
295 {
296 rc = setProgressErrorMsg(VBOX_E_IPRT_ERROR,
297 Utf8StrFmt(GuestSession::tr("Source file \"%s\" does not exist or is not a file"),
[42611]298 mSource.c_str()));
[42573]299 }
300 else
301 {
[42693]302 rc = RTFileOpen(pFile, mSource.c_str(),
[42573]303 RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_WRITE);
304 if (RT_FAILURE(rc))
305 {
306 rc = setProgressErrorMsg(VBOX_E_IPRT_ERROR,
[42693]307 Utf8StrFmt(GuestSession::tr("Could not open source file \"%s\" for reading: %Rrc"),
[42611]308 mSource.c_str(), rc));
[42573]309 }
310 else
311 {
[42693]312 rc = RTFileGetSize(*pFile, &mSourceSize);
[42573]313 if (RT_FAILURE(rc))
314 {
315 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
[42611]316 Utf8StrFmt(GuestSession::tr("Could not query file size of \"%s\": %Rrc"),
317 mSource.c_str(), rc));
[42573]318 }
[42693]319 }
320 }
321 }
322 else
323 {
[49501]324 rc = VINF_SUCCESS;
[42693]325 pFile = mSourceFile;
326 /* Size + offset are optional. */
327 }
[42573]328
[60622]329 /*
330 * Query information about our destination first.
331 */
[63147]332 int guestRc = VERR_IPE_UNINITIALIZED_STATUS;
[60622]333 if (RT_SUCCESS(rc))
334 {
335 GuestFsObjData objData;
336 rc = pSession->i_directoryQueryInfoInternal(mDest, true /* fFollowSymlinks */, objData, &guestRc);
337 if (RT_SUCCESS(rc))
338 {
339 mDest = Utf8StrFmt("%s/%s", mDest.c_str(), RTPathFilename(mSource.c_str()));
340 }
341 else if (rc == VERR_NOT_A_DIRECTORY)
342 {
343 rc = VINF_SUCCESS;
344 }
345 }
346
347 /** @todo Implement sparse file support? */
348
349 /*
350 * Start the actual copying process by cat'ing the source file to the
351 * destination file on the guest.
352 */
[42693]353 GuestProcessStartupInfo procInfo;
[55535]354 procInfo.mExecutable = Utf8Str(VBOXSERVICE_TOOL_CAT);
355 procInfo.mFlags = ProcessCreateFlag_Hidden;
[42573]356
[42693]357 /* Set arguments.*/
[60494]358 procInfo.mArguments.push_back(procInfo.mExecutable); /* Set argv0. */
[42693]359 procInfo.mArguments.push_back(Utf8StrFmt("--output=%s", mDest.c_str())); /** @todo Do we need path conversion? */
360
361 /* Startup process. */
[60622]362 ComObjPtr<GuestProcess> pProcess;
[42693]363 if (RT_SUCCESS(rc))
[55535]364 rc = pSession->i_processCreateExInternal(procInfo, pProcess);
[49440]365 if (RT_SUCCESS(rc))
366 {
367 Assert(!pProcess.isNull());
[60622]368 rc = pProcess->i_startProcess(msTimeout, &guestRc);
[49440]369 }
370
[42693]371 if (RT_FAILURE(rc))
372 {
[43162]373 switch (rc)
374 {
[45078]375 case VERR_GSTCTL_GUEST_ERROR:
[43162]376 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
[50709]377 GuestProcess::i_guestErrorToString(guestRc));
[43162]378 break;
379
380 default:
381 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
[51612]382 Utf8StrFmt(GuestSession::tr(
383 "Error while creating guest process for copying file \"%s\" from guest to host: %Rrc"),
[60622]384 mSource.c_str(), rc));
[43162]385 break;
386 }
[42693]387 }
[43162]388
389 if (RT_SUCCESS(rc))
[42693]390 {
[43162]391 ProcessWaitResult_T waitRes;
[42693]392 BYTE byBuf[_64K];
393
394 BOOL fCanceled = FALSE;
395 uint64_t cbWrittenTotal = 0;
396 uint64_t cbToRead = mSourceSize;
397
398 for (;;)
399 {
[60622]400 rc = pProcess->i_waitFor(ProcessWaitForFlag_StdIn, msTimeout, waitRes, &guestRc);
[42693]401 if ( RT_FAILURE(rc)
[43162]402 || ( waitRes != ProcessWaitResult_StdIn
403 && waitRes != ProcessWaitResult_WaitFlagNotSupported))
[42693]404 {
405 break;
406 }
407
[42810]408 /* If the guest does not support waiting for stdin, we now yield in
409 * order to reduce the CPU load due to busy waiting. */
[43162]410 if (waitRes == ProcessWaitResult_WaitFlagNotSupported)
[47627]411 RTThreadYield(); /* Optional, don't check rc. */
[42810]412
[42693]413 size_t cbRead = 0;
414 if (mSourceSize) /* If we have nothing to write, take a shortcut. */
415 {
416 /** @todo Not very efficient, but works for now. */
417 rc = RTFileSeek(*pFile, mSourceOffset + cbWrittenTotal,
418 RTFILE_SEEK_BEGIN, NULL /* poffActual */);
419 if (RT_SUCCESS(rc))
420 {
421 rc = RTFileRead(*pFile, (uint8_t*)byBuf,
[47905]422 RT_MIN((size_t)cbToRead, sizeof(byBuf)), &cbRead);
[42693]423 /*
424 * Some other error occured? There might be a chance that RTFileRead
425 * could not resolve/map the native error code to an IPRT code, so just
426 * print a generic error.
427 */
[42611]428 if (RT_FAILURE(rc))
[42573]429 {
[42611]430 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
[60622]431 Utf8StrFmt(GuestSession::tr("Could not read from host file \"%s\" (%Rrc)"),
[42693]432 mSource.c_str(), rc));
433 break;
[42611]434 }
[42693]435 }
436 else
437 {
438 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
[60622]439 Utf8StrFmt(GuestSession::tr("Seeking host file \"%s\" to offset %RU64 failed: %Rrc"),
[42693]440 mSource.c_str(), cbWrittenTotal, rc));
441 break;
442 }
443 }
[42573]444
[42693]445 uint32_t fFlags = ProcessInputFlag_None;
[42573]446
[42693]447 /* Did we reach the end of the content we want to transfer (last chunk)? */
448 if ( (cbRead < sizeof(byBuf))
449 /* Did we reach the last block which is exactly _64K? */
450 || (cbToRead - cbRead == 0)
451 /* ... or does the user want to cancel? */
[43162]452 || ( !mProgress.isNull()
453 && SUCCEEDED(mProgress->COMGETTER(Canceled(&fCanceled)))
[42693]454 && fCanceled)
455 )
456 {
[43162]457 LogFlowThisFunc(("Writing last chunk cbRead=%RU64\n", cbRead));
[42693]458 fFlags |= ProcessInputFlag_EndOfFile;
459 }
[42573]460
[42693]461 uint32_t cbWritten;
462 Assert(sizeof(byBuf) >= cbRead);
[50709]463 rc = pProcess->i_writeData(0 /* StdIn */, fFlags,
464 byBuf, cbRead,
[60622]465 msTimeout, &cbWritten, &guestRc);
[42693]466 if (RT_FAILURE(rc))
467 {
[43162]468 switch (rc)
469 {
[45078]470 case VERR_GSTCTL_GUEST_ERROR:
[43162]471 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
[50709]472 GuestProcess::i_guestErrorToString(guestRc));
[43162]473 break;
474
475 default:
476 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
[60622]477 Utf8StrFmt(GuestSession::tr("Writing to guest file \"%s\" (offset %RU64) failed: %Rrc"),
[43162]478 mDest.c_str(), cbWrittenTotal, rc));
479 break;
480 }
481
[42693]482 break;
483 }
[42758]484
[42693]485 /* Only subtract bytes reported written by the guest. */
486 Assert(cbToRead >= cbWritten);
487 cbToRead -= cbWritten;
[42573]488
[42693]489 /* Update total bytes written to the guest. */
490 cbWrittenTotal += cbWritten;
491 Assert(cbWrittenTotal <= mSourceSize);
[42573]492
[43162]493 LogFlowThisFunc(("rc=%Rrc, cbWritten=%RU32, cbToRead=%RU64, cbWrittenTotal=%RU64, cbFileSize=%RU64\n",
494 rc, cbWritten, cbToRead, cbWrittenTotal, mSourceSize));
495
[42693]496 /* Did the user cancel the operation above? */
497 if (fCanceled)
498 break;
[42573]499
[42693]500 /* Update the progress.
501 * Watch out for division by zero. */
502 mSourceSize > 0
503 ? rc = setProgress((ULONG)(cbWrittenTotal * 100 / mSourceSize))
504 : rc = setProgress(100);
505 if (RT_FAILURE(rc))
506 break;
[42573]507
[42693]508 /* End of file reached? */
509 if (!cbToRead)
510 break;
511 } /* for */
[42573]512
[49629]513 LogFlowThisFunc(("Copy loop ended with rc=%Rrc, cbToRead=%RU64, cbWrittenTotal=%RU64, cbFileSize=%RU64\n",
514 rc, cbToRead, cbWrittenTotal, mSourceSize));
[43162]515
[60622]516 /*
517 * Wait on termination of guest process until it completed all operations.
518 */
[42693]519 if ( !fCanceled
520 || RT_SUCCESS(rc))
521 {
[60622]522 rc = pProcess->i_waitFor(ProcessWaitForFlag_Terminate, msTimeout, waitRes, &guestRc);
523 if ( RT_FAILURE(rc)
524 || waitRes != ProcessWaitResult_Terminate)
525 {
526 if (RT_FAILURE(rc))
527 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
528 Utf8StrFmt(
529 GuestSession::tr("Waiting on termination for copying file \"%s\" to guest failed: %Rrc"),
530 mSource.c_str(), rc));
531 else
532 {
533 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
534 Utf8StrFmt(GuestSession::tr(
535 "Waiting on termination for copying file \"%s\" to guest failed with wait result %ld"),
536 mSource.c_str(), waitRes));
537 rc = VERR_GENERAL_FAILURE; /* Fudge. */
538 }
539 }
540 }
541
542 if (RT_SUCCESS(rc))
543 {
[42693]544 /*
[60622]545 * Newer VBoxService toolbox versions report what went wrong via exit code.
546 * So handle this first.
547 */
[63152]548 /** @todo This code sequence is duplicated in CopyFrom... */
549 ProcessStatus_T procStatus = ProcessStatus_TerminatedAbnormally;
550 HRESULT hrc = pProcess->COMGETTER(Status(&procStatus));
551 if (!SUCCEEDED(hrc))
552 procStatus = ProcessStatus_TerminatedAbnormally;
553
554 LONG exitCode = 42424242;
555 hrc = pProcess->COMGETTER(ExitCode(&exitCode));
556 if (!SUCCEEDED(hrc))
557 exitCode = 42424242;
558
559 if ( procStatus != ProcessStatus_TerminatedNormally
560 || exitCode != 0)
[60622]561 {
[63152]562 LogFlowThisFunc(("procStatus=%d, exitCode=%d\n", procStatus, exitCode));
563 if (procStatus == ProcessStatus_TerminatedNormally)
564 rc = GuestProcessTool::i_exitCodeToRc(procInfo, exitCode);
565 else
566 rc = VERR_GENERAL_FAILURE;
[60622]567 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
568 Utf8StrFmt(GuestSession::tr("Copying file \"%s\" to guest failed: %Rrc"),
569 mSource.c_str(), rc));
570 }
571 /*
[42693]572 * Even if we succeeded until here make sure to check whether we really transfered
573 * everything.
574 */
[60622]575 else if ( mSourceSize > 0
576 && cbWrittenTotal == 0)
[42693]577 {
578 /* If nothing was transfered but the file size was > 0 then "vbox_cat" wasn't able to write
579 * to the destination -> access denied. */
[43162]580 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
[60622]581 Utf8StrFmt(GuestSession::tr("Access denied when copying file \"%s\" to guest \"%s\""),
[43162]582 mSource.c_str(), mDest.c_str()));
[60622]583 rc = VERR_ACCESS_DENIED;
[42693]584 }
585 else if (cbWrittenTotal < mSourceSize)
586 {
587 /* If we did not copy all let the user know. */
[43162]588 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
[60622]589 Utf8StrFmt(GuestSession::tr("Copying file \"%s\" to guest failed (%RU64/%RU64 bytes transfered)"),
[43162]590 mSource.c_str(), cbWrittenTotal, mSourceSize));
[60622]591 rc = VERR_INTERRUPTED;
[42693]592 }
[63152]593 else
[60622]594 rc = setProgressSuccess();
[42693]595 }
596 } /* processCreateExInteral */
[42573]597
[42693]598 if (!mSourceFile) /* Only close locally opened files. */
599 RTFileClose(*pFile);
[42573]600
601 LogFlowFuncLeaveRC(rc);
602 return rc;
[42566]603}
604
[42634]605SessionTaskCopyFrom::SessionTaskCopyFrom(GuestSession *pSession,
[42566]606 const Utf8Str &strSource, const Utf8Str &strDest, uint32_t uFlags)
[42634]607 : GuestSessionTask(pSession)
[42566]608{
609 mSource = strSource;
[42634]610 mDest = strDest;
[42566]611 mFlags = uFlags;
[58521]612 m_strTaskName = "gctlCpyFrom";
[42566]613}
614
615SessionTaskCopyFrom::~SessionTaskCopyFrom(void)
616{
617
618}
619
620int SessionTaskCopyFrom::Run(void)
621{
[42611]622 LogFlowThisFuncEnter();
623
624 ComObjPtr<GuestSession> pSession = mSession;
625 Assert(!pSession.isNull());
626
627 AutoCaller autoCaller(pSession);
628 if (FAILED(autoCaller.rc())) return autoCaller.rc();
629
[60622]630 RTMSINTERVAL msTimeout = 30 * 1000; /** @todo 30s timeout for all actions. Make this configurable? */
631
[42693]632 /*
633 * Note: There will be races between querying file size + reading the guest file's
634 * content because we currently *do not* lock down the guest file when doing the
635 * actual operations.
[49630]636 ** @todo Use the IGuestFile API for locking down the file on the guest!
[42693]637 */
[43162]638 GuestFsObjData objData; int guestRc;
[55613]639 int rc = pSession->i_fileQueryInfoInternal(Utf8Str(mSource), false /*fFollowSymlinks*/, objData, &guestRc);
[42693]640 if (RT_FAILURE(rc))
641 {
642 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
643 Utf8StrFmt(GuestSession::tr("Querying guest file information for \"%s\" failed: %Rrc"),
[51092]644 mSource.c_str(), rc));
[42693]645 }
646 else if (objData.mType != FsObjType_File) /* Only single files are supported at the moment. */
647 {
[43170]648 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
649 Utf8StrFmt(GuestSession::tr("Object \"%s\" on the guest is not a file"), mSource.c_str()));
[60622]650 rc = VERR_NOT_A_FILE;
[42693]651 }
[42611]652
[42693]653 if (RT_SUCCESS(rc))
[42611]654 {
[42693]655 RTFILE fileDest;
656 rc = RTFileOpen(&fileDest, mDest.c_str(),
657 RTFILE_O_WRITE | RTFILE_O_OPEN_CREATE | RTFILE_O_DENY_WRITE); /** @todo Use the correct open modes! */
[42611]658 if (RT_FAILURE(rc))
659 {
660 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
[60622]661 Utf8StrFmt(GuestSession::tr("Opening/creating destination file on host \"%s\" failed: %Rrc"),
[42693]662 mDest.c_str(), rc));
[42611]663 }
[42693]664 else
[42611]665 {
[42693]666 GuestProcessStartupInfo procInfo;
[51092]667 procInfo.mName = Utf8StrFmt(GuestSession::tr("Copying file \"%s\" from guest to the host to \"%s\" (%RI64 bytes)"),
668 mSource.c_str(), mDest.c_str(), objData.mObjectSize);
[55535]669 procInfo.mExecutable = Utf8Str(VBOXSERVICE_TOOL_CAT);
670 procInfo.mFlags = ProcessCreateFlag_Hidden | ProcessCreateFlag_WaitForStdOut;
[42611]671
[42693]672 /* Set arguments.*/
[60494]673 procInfo.mArguments.push_back(procInfo.mExecutable); /* Set argv0. */
674 procInfo.mArguments.push_back(mSource); /* Which file to output? */
[42693]675
676 /* Startup process. */
[43170]677 ComObjPtr<GuestProcess> pProcess;
[55535]678 rc = pSession->i_processCreateExInternal(procInfo, pProcess);
[42693]679 if (RT_SUCCESS(rc))
[60622]680 rc = pProcess->i_startProcess(msTimeout, &guestRc);
[42611]681 if (RT_FAILURE(rc))
682 {
[43162]683 switch (rc)
684 {
[45078]685 case VERR_GSTCTL_GUEST_ERROR:
[60622]686 setProgressErrorMsg(VBOX_E_IPRT_ERROR, GuestProcess::i_guestErrorToString(guestRc));
[43162]687 break;
688
689 default:
690 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
[51612]691 Utf8StrFmt(GuestSession::tr(
692 "Error while creating guest process for copying file \"%s\" from guest to host: %Rrc"),
[60622]693 mSource.c_str(), rc));
[43162]694 break;
695 }
[42611]696 }
697 else
698 {
[43162]699 ProcessWaitResult_T waitRes;
[42693]700 BYTE byBuf[_64K];
[42611]701
[42693]702 BOOL fCanceled = FALSE;
703 uint64_t cbWrittenTotal = 0;
704 uint64_t cbToRead = objData.mObjectSize;
[42611]705
[42693]706 for (;;)
[42611]707 {
[60622]708 rc = pProcess->i_waitFor(ProcessWaitForFlag_StdOut, msTimeout, waitRes, &guestRc);
[43162]709 if (RT_FAILURE(rc))
[42693]710 {
[43162]711 switch (rc)
712 {
[45078]713 case VERR_GSTCTL_GUEST_ERROR:
[43162]714 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
[50709]715 GuestProcess::i_guestErrorToString(guestRc));
[43162]716 break;
717
718 default:
719 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
[51441]720 Utf8StrFmt(GuestSession::tr("Error while creating guest process for copying file \"%s\" from guest to host: %Rrc"),
[43162]721 mSource.c_str(), rc));
722 break;
723 }
724
725 break;
726 }
727
728 if ( waitRes == ProcessWaitResult_StdOut
729 || waitRes == ProcessWaitResult_WaitFlagNotSupported)
730 {
[42810]731 /* If the guest does not support waiting for stdin, we now yield in
732 * order to reduce the CPU load due to busy waiting. */
[43162]733 if (waitRes == ProcessWaitResult_WaitFlagNotSupported)
[47627]734 RTThreadYield(); /* Optional, don't check rc. */
[42810]735
[47627]736 uint32_t cbRead = 0; /* readData can return with VWRN_GSTCTL_OBJECTSTATE_CHANGED. */
[50709]737 rc = pProcess->i_readData(OUTPUT_HANDLE_ID_STDOUT, sizeof(byBuf),
[60622]738 msTimeout, byBuf, sizeof(byBuf),
[50709]739 &cbRead, &guestRc);
[42611]740 if (RT_FAILURE(rc))
741 {
[43162]742 switch (rc)
743 {
[45078]744 case VERR_GSTCTL_GUEST_ERROR:
[43162]745 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
[50709]746 GuestProcess::i_guestErrorToString(guestRc));
[43162]747 break;
748
749 default:
750 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
[60622]751 Utf8StrFmt(GuestSession::tr("Reading from guest file \"%s\" (offset %RU64) failed: %Rrc"),
[43162]752 mSource.c_str(), cbWrittenTotal, rc));
753 break;
754 }
755
[42611]756 break;
757 }
758
[42759]759 if (cbRead)
760 {
761 rc = RTFileWrite(fileDest, byBuf, cbRead, NULL /* No partial writes */);
762 if (RT_FAILURE(rc))
763 {
764 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
[60622]765 Utf8StrFmt(GuestSession::tr("Writing to host file \"%s\" (%RU64 bytes left) failed: %Rrc"),
[51092]766 mDest.c_str(), cbToRead, rc));
[42759]767 break;
768 }
[42611]769
[42759]770 /* Only subtract bytes reported written by the guest. */
771 Assert(cbToRead >= cbRead);
772 cbToRead -= cbRead;
[42611]773
[42759]774 /* Update total bytes written to the guest. */
775 cbWrittenTotal += cbRead;
776 Assert(cbWrittenTotal <= (uint64_t)objData.mObjectSize);
[42611]777
[42759]778 /* Did the user cancel the operation above? */
779 if ( SUCCEEDED(mProgress->COMGETTER(Canceled(&fCanceled)))
780 && fCanceled)
781 break;
[42611]782
[42759]783 rc = setProgress((ULONG)(cbWrittenTotal / ((uint64_t)objData.mObjectSize / 100.0)));
784 if (RT_FAILURE(rc))
785 break;
786 }
[42693]787 }
[43162]788 else
[42759]789 {
790 break;
791 }
[43162]792
[42693]793 } /* for */
[42611]794
[43162]795 LogFlowThisFunc(("rc=%Rrc, guestrc=%Rrc, waitRes=%ld, cbWrittenTotal=%RU64, cbSize=%RI64, cbToRead=%RU64\n",
796 rc, guestRc, waitRes, cbWrittenTotal, objData.mObjectSize, cbToRead));
[42759]797
[60622]798 /*
799 * Wait on termination of guest process until it completed all operations.
800 */
[42693]801 if ( !fCanceled
802 || RT_SUCCESS(rc))
803 {
[60622]804 rc = pProcess->i_waitFor(ProcessWaitForFlag_Terminate, msTimeout, waitRes, &guestRc);
805 if ( RT_FAILURE(rc)
806 || waitRes != ProcessWaitResult_Terminate)
807 {
808 if (RT_FAILURE(rc))
809 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
810 Utf8StrFmt(
811 GuestSession::tr("Waiting on termination for copying file \"%s\" from guest failed: %Rrc"),
812 mSource.c_str(), rc));
813 else
814 {
815 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
816 Utf8StrFmt(GuestSession::tr(
817 "Waiting on termination for copying file \"%s\" from guest failed with wait result %ld"),
818 mSource.c_str(), waitRes));
819 rc = VERR_GENERAL_FAILURE; /* Fudge. */
820 }
821 }
822 }
823
824 if (RT_SUCCESS(rc))
825 {
[63152]826 /** @todo this code sequence is duplicated in CopyTo */
827 ProcessStatus_T procStatus = ProcessStatus_TerminatedAbnormally;
828 HRESULT hrc = pProcess->COMGETTER(Status(&procStatus));
829 if (!SUCCEEDED(hrc))
830 procStatus = ProcessStatus_TerminatedAbnormally;
831
832 LONG exitCode = 42424242;
833 hrc = pProcess->COMGETTER(ExitCode(&exitCode));
834 if (!SUCCEEDED(hrc))
835 exitCode = 42424242;
836
837 if ( procStatus != ProcessStatus_TerminatedNormally
838 || exitCode != 0)
[60622]839 {
[63152]840 LogFlowThisFunc(("procStatus=%d, exitCode=%d\n", procStatus, exitCode));
841 if (procStatus == ProcessStatus_TerminatedNormally)
842 rc = GuestProcessTool::i_exitCodeToRc(procInfo, exitCode);
843 else
844 rc = VERR_GENERAL_FAILURE;
[60622]845 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
846 Utf8StrFmt(GuestSession::tr("Copying file \"%s\" to host failed: %Rrc"),
847 mSource.c_str(), rc));
848 }
[42693]849 /*
850 * Even if we succeeded until here make sure to check whether we really transfered
851 * everything.
852 */
[60622]853 else if ( objData.mObjectSize > 0
854 && cbWrittenTotal == 0)
[42611]855 {
[42693]856 /* If nothing was transfered but the file size was > 0 then "vbox_cat" wasn't able to write
857 * to the destination -> access denied. */
[42759]858 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
[60622]859 Utf8StrFmt(GuestSession::tr("Writing guest file \"%s\" to host to \"%s\" failed: Access denied"),
860 mSource.c_str(), mDest.c_str()));
861 rc = VERR_ACCESS_DENIED;
[42693]862 }
863 else if (cbWrittenTotal < (uint64_t)objData.mObjectSize)
864 {
865 /* If we did not copy all let the user know. */
[42759]866 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
[60622]867 Utf8StrFmt(GuestSession::tr("Copying guest file \"%s\" to host to \"%s\" failed (%RU64/%RI64 bytes transfered)"),
868 mSource.c_str(), mDest.c_str(), cbWrittenTotal, objData.mObjectSize));
869 rc = VERR_INTERRUPTED;
[42693]870 }
[63152]871 else
[60622]872 rc = setProgressSuccess();
[42611]873 }
874 }
[42693]875
876 RTFileClose(fileDest);
[42611]877 }
878 }
879
880 LogFlowFuncLeaveRC(rc);
881 return rc;
[42566]882}
883
[42693]884SessionTaskUpdateAdditions::SessionTaskUpdateAdditions(GuestSession *pSession,
[46524]885 const Utf8Str &strSource,
886 const ProcessArguments &aArguments,
887 uint32_t uFlags)
[42693]888 : GuestSessionTask(pSession)
889{
890 mSource = strSource;
[46524]891 mArguments = aArguments;
[42693]892 mFlags = uFlags;
[58521]893 m_strTaskName = "gctlUpGA";
[42693]894}
895
896SessionTaskUpdateAdditions::~SessionTaskUpdateAdditions(void)
897{
898
899}
900
[50727]901int SessionTaskUpdateAdditions::i_addProcessArguments(ProcessArguments &aArgumentsDest,
902 const ProcessArguments &aArgumentsSource)
[46524]903{
904 int rc = VINF_SUCCESS;
905
906 try
907 {
908 /* Filter out arguments which already are in the destination to
909 * not end up having them specified twice. Not the fastest method on the
910 * planet but does the job. */
911 ProcessArguments::const_iterator itSource = aArgumentsSource.begin();
912 while (itSource != aArgumentsSource.end())
913 {
914 bool fFound = false;
915 ProcessArguments::iterator itDest = aArgumentsDest.begin();
916 while (itDest != aArgumentsDest.end())
917 {
918 if ((*itDest).equalsIgnoreCase((*itSource)))
919 {
920 fFound = true;
921 break;
922 }
[56030]923 ++itDest;
[46524]924 }
925
926 if (!fFound)
927 aArgumentsDest.push_back((*itSource));
928
[56030]929 ++itSource;
[46524]930 }
931 }
932 catch(std::bad_alloc &)
933 {
934 return VERR_NO_MEMORY;
935 }
936
937 return rc;
938}
939
[50727]940int SessionTaskUpdateAdditions::i_copyFileToGuest(GuestSession *pSession, PRTISOFSFILE pISO,
941 Utf8Str const &strFileSource, const Utf8Str &strFileDest,
942 bool fOptional, uint32_t *pcbSize)
[42749]943{
944 AssertPtrReturn(pSession, VERR_INVALID_POINTER);
945 AssertPtrReturn(pISO, VERR_INVALID_POINTER);
946 /* pcbSize is optional. */
947
948 uint32_t cbOffset;
949 size_t cbSize;
950
951 int rc = RTIsoFsGetFileInfo(pISO, strFileSource.c_str(), &cbOffset, &cbSize);
952 if (RT_FAILURE(rc))
953 {
954 if (fOptional)
955 return VINF_SUCCESS;
956
957 return rc;
958 }
959
960 Assert(cbOffset);
961 Assert(cbSize);
962 rc = RTFileSeek(pISO->file, cbOffset, RTFILE_SEEK_BEGIN, NULL);
963
[58521]964 HRESULT hr = S_OK;
[42749]965 /* Copy over the Guest Additions file to the guest. */
966 if (RT_SUCCESS(rc))
967 {
[44863]968 LogRel(("Copying Guest Additions installer file \"%s\" to \"%s\" on guest ...\n",
969 strFileSource.c_str(), strFileDest.c_str()));
[42749]970
971 if (RT_SUCCESS(rc))
972 {
[58521]973 SessionTaskCopyTo *pTask = NULL;
[42749]974 ComObjPtr<Progress> pProgressCopyTo;
[58521]975 try
[42749]976 {
[58521]977 try
978 {
979 pTask = new SessionTaskCopyTo(pSession /* GuestSession */,
980 &pISO->file, cbOffset, cbSize,
981 strFileDest, FileCopyFlag_None);
982 }
983 catch(...)
984 {
[60494]985 hr = setProgressErrorMsg(VBOX_E_IPRT_ERROR,
[58521]986 GuestSession::tr("Failed to create SessionTaskCopyTo object "));
987 throw;
988 }
989
990 hr = pTask->Init(Utf8StrFmt(GuestSession::tr("Copying Guest Additions installer file \"%s\" to \"%s\" on guest"),
991 mSource.c_str(), strFileDest.c_str()));
992 if (FAILED(hr))
993 {
994 delete pTask;
995 hr = setProgressErrorMsg(VBOX_E_IPRT_ERROR,
996 GuestSession::tr("Creating progress object for SessionTaskCopyTo object failed"));
997 throw hr;
998 }
999
[63182]1000 hr = pTask->createThreadWithType(RTTHREADTYPE_MAIN_HEAVY_WORKER);
[58521]1001
1002 if (SUCCEEDED(hr))
1003 {
1004 /* Return progress to the caller. */
1005 pProgressCopyTo = pTask->GetProgressObject();
1006 }
1007 else
[60494]1008 hr = setProgressErrorMsg(VBOX_E_IPRT_ERROR,
[58521]1009 GuestSession::tr("Starting thread for updating additions failed "));
1010 }
1011 catch(std::bad_alloc &)
1012 {
1013 hr = E_OUTOFMEMORY;
1014 }
1015 catch(HRESULT eHR)
1016 {
1017 hr = eHR;
1018 LogFlowThisFunc(("Exception was caught in the function \n"));
1019 }
1020
1021 if (SUCCEEDED(hr))
1022 {
[42749]1023 BOOL fCanceled = FALSE;
[58521]1024 hr = pProgressCopyTo->WaitForCompletion(-1);
[42749]1025 if ( SUCCEEDED(pProgressCopyTo->COMGETTER(Canceled)(&fCanceled))
1026 && fCanceled)
1027 {
1028 rc = VERR_GENERAL_FAILURE; /* Fudge. */
1029 }
1030 else if (FAILED(hr))
1031 {
1032 Assert(FAILED(hr));
1033 rc = VERR_GENERAL_FAILURE; /* Fudge. */
1034 }
1035 }
1036 }
1037 }
1038
1039 /** @todo Note: Since there is no file locking involved at the moment, there can be modifications
1040 * between finished copying, the verification and the actual execution. */
1041
1042 /* Determine where the installer image ended up and if it has the correct size. */
1043 if (RT_SUCCESS(rc))
1044 {
[44863]1045 LogRel(("Verifying Guest Additions installer file \"%s\" ...\n",
1046 strFileDest.c_str()));
[42749]1047
1048 GuestFsObjData objData;
[43162]1049 int64_t cbSizeOnGuest; int guestRc;
[55631]1050 rc = pSession->i_fileQuerySizeInternal(strFileDest, false /*fFollowSymlinks*/, &cbSizeOnGuest, &guestRc);
[42749]1051 if ( RT_SUCCESS(rc)
1052 && cbSize == (uint64_t)cbSizeOnGuest)
1053 {
[42937]1054 LogFlowThisFunc(("Guest Additions installer file \"%s\" successfully verified\n",
1055 strFileDest.c_str()));
[42749]1056 }
1057 else
1058 {
1059 if (RT_SUCCESS(rc)) /* Size does not match. */
[42923]1060 {
[44863]1061 LogRel(("Size of Guest Additions installer file \"%s\" does not match: %RI64 bytes copied, %RU64 bytes expected\n",
1062 strFileDest.c_str(), cbSizeOnGuest, cbSize));
[42923]1063 rc = VERR_BROKEN_PIPE; /** @todo Find a better error. */
1064 }
1065 else
[43162]1066 {
1067 switch (rc)
1068 {
[45078]1069 case VERR_GSTCTL_GUEST_ERROR:
[43162]1070 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
[50709]1071 GuestProcess::i_guestErrorToString(guestRc));
[43162]1072 break;
1073
1074 default:
1075 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
1076 Utf8StrFmt(GuestSession::tr("Error while querying size for file \"%s\": %Rrc"),
1077 strFileDest.c_str(), rc));
1078 break;
1079 }
1080 }
[42749]1081 }
1082
1083 if (RT_SUCCESS(rc))
1084 {
1085 if (pcbSize)
[47905]1086 *pcbSize = (uint32_t)cbSizeOnGuest;
[42749]1087 }
1088 }
1089
1090 return rc;
1091}
1092
[50727]1093int SessionTaskUpdateAdditions::i_runFileOnGuest(GuestSession *pSession, GuestProcessStartupInfo &procInfo)
[42749]1094{
1095 AssertPtrReturn(pSession, VERR_INVALID_POINTER);
1096
[43162]1097 LogRel(("Running %s ...\n", procInfo.mName.c_str()));
[42749]1098
[43162]1099 GuestProcessTool procTool; int guestRc;
1100 int vrc = procTool.Init(pSession, procInfo, false /* Async */, &guestRc);
1101 if (RT_SUCCESS(vrc))
[42749]1102 {
[43162]1103 if (RT_SUCCESS(guestRc))
[50709]1104 vrc = procTool.i_wait(GUESTPROCESSTOOL_FLAG_NONE, &guestRc);
[43162]1105 if (RT_SUCCESS(vrc))
[60622]1106 vrc = procTool.i_terminatedOk();
[43162]1107 }
[42749]1108
[43162]1109 if (RT_FAILURE(vrc))
1110 {
1111 switch (vrc)
[42749]1112 {
[60622]1113 case VWRN_GSTCTL_PROCESS_EXIT_CODE:
[42749]1114 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
[60622]1115 Utf8StrFmt(GuestSession::tr("Running update file \"%s\" on guest failed: %Rrc"),
1116 procInfo.mExecutable.c_str(), procTool.i_getRc()));
[43162]1117 break;
1118
[45078]1119 case VERR_GSTCTL_GUEST_ERROR:
[42749]1120 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
[50709]1121 GuestProcess::i_guestErrorToString(guestRc));
[43162]1122 break;
1123
[43299]1124 case VERR_INVALID_STATE: /** @todo Special guest control rc needed! */
1125 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
[51441]1126 Utf8StrFmt(GuestSession::tr("Update file \"%s\" reported invalid running state"),
[55535]1127 procInfo.mExecutable.c_str()));
[43299]1128 break;
1129
[43162]1130 default:
1131 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
[51441]1132 Utf8StrFmt(GuestSession::tr("Error while running update file \"%s\" on guest: %Rrc"),
[55535]1133 procInfo.mExecutable.c_str(), vrc));
[43162]1134 break;
[42749]1135 }
1136 }
1137
[43162]1138 return vrc;
[42749]1139}
1140
[42693]1141int SessionTaskUpdateAdditions::Run(void)
1142{
1143 LogFlowThisFuncEnter();
1144
1145 ComObjPtr<GuestSession> pSession = mSession;
1146 Assert(!pSession.isNull());
1147
1148 AutoCaller autoCaller(pSession);
1149 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1150
1151 int rc = setProgress(10);
1152 if (RT_FAILURE(rc))
1153 return rc;
1154
1155 HRESULT hr = S_OK;
1156
1157 LogRel(("Automatic update of Guest Additions started, using \"%s\"\n", mSource.c_str()));
1158
[50727]1159 ComObjPtr<Guest> pGuest(mSession->i_getParent());
[43030]1160#if 0
[42693]1161 /*
[43001]1162 * Wait for the guest being ready within 30 seconds.
[42693]1163 */
[43001]1164 AdditionsRunLevelType_T addsRunLevel;
1165 uint64_t tsStart = RTTimeSystemMilliTS();
1166 while ( SUCCEEDED(hr = pGuest->COMGETTER(AdditionsRunLevel)(&addsRunLevel))
1167 && ( addsRunLevel != AdditionsRunLevelType_Userland
1168 && addsRunLevel != AdditionsRunLevelType_Desktop))
1169 {
1170 if ((RTTimeSystemMilliTS() - tsStart) > 30 * 1000)
1171 {
1172 rc = VERR_TIMEOUT;
1173 break;
1174 }
[42693]1175
[43001]1176 RTThreadSleep(100); /* Wait a bit. */
1177 }
1178
1179 if (FAILED(hr)) rc = VERR_TIMEOUT;
1180 if (rc == VERR_TIMEOUT)
1181 hr = setProgressErrorMsg(VBOX_E_NOT_SUPPORTED,
1182 Utf8StrFmt(GuestSession::tr("Guest Additions were not ready within time, giving up")));
[43030]1183#else
1184 /*
1185 * For use with the GUI we don't want to wait, just return so that the manual .ISO mounting
1186 * can continue.
1187 */
1188 AdditionsRunLevelType_T addsRunLevel;
[43034]1189 if ( FAILED(hr = pGuest->COMGETTER(AdditionsRunLevel)(&addsRunLevel))
[43030]1190 || ( addsRunLevel != AdditionsRunLevelType_Userland
1191 && addsRunLevel != AdditionsRunLevelType_Desktop))
1192 {
[43034]1193 if (addsRunLevel == AdditionsRunLevelType_System)
1194 hr = setProgressErrorMsg(VBOX_E_NOT_SUPPORTED,
[51441]1195 Utf8StrFmt(GuestSession::tr("Guest Additions are installed but not fully loaded yet, aborting automatic update")));
[43034]1196 else
1197 hr = setProgressErrorMsg(VBOX_E_NOT_SUPPORTED,
[51441]1198 Utf8StrFmt(GuestSession::tr("Guest Additions not installed or ready, aborting automatic update")));
[43030]1199 rc = VERR_NOT_SUPPORTED;
1200 }
1201#endif
[43001]1202
1203 if (RT_SUCCESS(rc))
[42693]1204 {
[43001]1205 /*
1206 * Determine if we are able to update automatically. This only works
1207 * if there are recent Guest Additions installed already.
1208 */
1209 Utf8Str strAddsVer;
1210 rc = getGuestProperty(pGuest, "/VirtualBox/GuestAdd/Version", strAddsVer);
1211 if ( RT_SUCCESS(rc)
1212 && RTStrVersionCompare(strAddsVer.c_str(), "4.1") < 0)
1213 {
1214 hr = setProgressErrorMsg(VBOX_E_NOT_SUPPORTED,
1215 Utf8StrFmt(GuestSession::tr("Guest has too old Guest Additions (%s) installed for automatic updating, please update manually"),
1216 strAddsVer.c_str()));
1217 rc = VERR_NOT_SUPPORTED;
1218 }
[43030]1219 }
[42923]1220
[43053]1221 Utf8Str strOSVer;
[43493]1222 eOSType osType = eOSType_Unknown;
[43030]1223 if (RT_SUCCESS(rc))
1224 {
[43001]1225 /*
1226 * Determine guest OS type and the required installer image.
1227 */
1228 Utf8Str strOSType;
1229 rc = getGuestProperty(pGuest, "/VirtualBox/GuestInfo/OS/Product", strOSType);
1230 if (RT_SUCCESS(rc))
1231 {
1232 if ( strOSType.contains("Microsoft", Utf8Str::CaseInsensitive)
1233 || strOSType.contains("Windows", Utf8Str::CaseInsensitive))
1234 {
[43002]1235 osType = eOSType_Windows;
[43053]1236
1237 /*
1238 * Determine guest OS version.
1239 */
1240 rc = getGuestProperty(pGuest, "/VirtualBox/GuestInfo/OS/Release", strOSVer);
1241 if (RT_FAILURE(rc))
1242 {
1243 hr = setProgressErrorMsg(VBOX_E_NOT_SUPPORTED,
[51092]1244 Utf8StrFmt(GuestSession::tr("Unable to detected guest OS version, please update manually")));
[43053]1245 rc = VERR_NOT_SUPPORTED;
1246 }
1247
1248 /* Because Windows 2000 + XP and is bitching with WHQL popups even if we have signed drivers we
1249 * can't do automated updates here. */
[43060]1250 /* Windows XP 64-bit (5.2) is a Windows 2003 Server actually, so skip this here. */
[43053]1251 if ( RT_SUCCESS(rc)
[45572]1252 && RTStrVersionCompare(strOSVer.c_str(), "5.0") >= 0)
[43053]1253 {
[45572]1254 if ( strOSVer.startsWith("5.0") /* Exclude the build number. */
1255 || strOSVer.startsWith("5.1") /* Exclude the build number. */)
[43060]1256 {
[45572]1257 /* If we don't have AdditionsUpdateFlag_WaitForUpdateStartOnly set we can't continue
1258 * because the Windows Guest Additions installer will fail because of WHQL popups. If the
1259 * flag is set this update routine ends successfully as soon as the installer was started
1260 * (and the user has to deal with it in the guest). */
1261 if (!(mFlags & AdditionsUpdateFlag_WaitForUpdateStartOnly))
1262 {
1263 hr = setProgressErrorMsg(VBOX_E_NOT_SUPPORTED,
[51441]1264 Utf8StrFmt(GuestSession::tr("Windows 2000 and XP are not supported for automatic updating due to WHQL interaction, please update manually")));
[45572]1265 rc = VERR_NOT_SUPPORTED;
1266 }
[43060]1267 }
[43053]1268 }
[45572]1269 else
1270 {
1271 hr = setProgressErrorMsg(VBOX_E_NOT_SUPPORTED,
[51441]1272 Utf8StrFmt(GuestSession::tr("%s (%s) not supported for automatic updating, please update manually"),
[45572]1273 strOSType.c_str(), strOSVer.c_str()));
1274 rc = VERR_NOT_SUPPORTED;
1275 }
[43001]1276 }
1277 else if (strOSType.contains("Solaris", Utf8Str::CaseInsensitive))
1278 {
[43002]1279 osType = eOSType_Solaris;
[43001]1280 }
1281 else /* Everything else hopefully means Linux :-). */
[43002]1282 osType = eOSType_Linux;
[43001]1283
[42923]1284#if 1 /* Only Windows is supported (and tested) at the moment. */
[45572]1285 if ( RT_SUCCESS(rc)
1286 && osType != eOSType_Windows)
[43001]1287 {
1288 hr = setProgressErrorMsg(VBOX_E_NOT_SUPPORTED,
1289 Utf8StrFmt(GuestSession::tr("Detected guest OS (%s) does not support automatic Guest Additions updating, please update manually"),
1290 strOSType.c_str()));
1291 rc = VERR_NOT_SUPPORTED;
1292 }
1293#endif
1294 }
[42693]1295 }
1296
1297 RTISOFSFILE iso;
1298 if (RT_SUCCESS(rc))
1299 {
1300 /*
[42923]1301 * Try to open the .ISO file to extract all needed files.
[42693]1302 */
1303 rc = RTIsoFsOpen(&iso, mSource.c_str());
1304 if (RT_FAILURE(rc))
1305 {
1306 hr = setProgressErrorMsg(VBOX_E_IPRT_ERROR,
[42716]1307 Utf8StrFmt(GuestSession::tr("Unable to open Guest Additions .ISO file \"%s\": %Rrc"),
[42693]1308 mSource.c_str(), rc));
1309 }
1310 else
1311 {
[42923]1312 /* Set default installation directories. */
[43001]1313 Utf8Str strUpdateDir = "/tmp/";
[43002]1314 if (osType == eOSType_Windows)
[43001]1315 strUpdateDir = "C:\\Temp\\";
[42923]1316
[42749]1317 rc = setProgress(5);
[42693]1318
[42923]1319 /* Try looking up the Guest Additions installation directory. */
[42693]1320 if (RT_SUCCESS(rc))
1321 {
[43001]1322 /* Try getting the installed Guest Additions version to know whether we
1323 * can install our temporary Guest Addition data into the original installation
1324 * directory.
1325 *
1326 * Because versions prior to 4.2 had bugs wrt spaces in paths we have to choose
1327 * a different location then.
1328 */
1329 bool fUseInstallDir = false;
1330
1331 Utf8Str strAddsVer;
1332 rc = getGuestProperty(pGuest, "/VirtualBox/GuestAdd/Version", strAddsVer);
1333 if ( RT_SUCCESS(rc)
[43162]1334 && RTStrVersionCompare(strAddsVer.c_str(), "4.2r80329") > 0)
[42923]1335 {
[43001]1336 fUseInstallDir = true;
1337 }
1338
1339 if (fUseInstallDir)
1340 {
1341 if (RT_SUCCESS(rc))
1342 rc = getGuestProperty(pGuest, "/VirtualBox/GuestAdd/InstallDir", strUpdateDir);
1343 if (RT_SUCCESS(rc))
[42693]1344 {
[43002]1345 if (osType == eOSType_Windows)
[43001]1346 {
1347 strUpdateDir.findReplace('/', '\\');
1348 strUpdateDir.append("\\Update\\");
1349 }
1350 else
1351 strUpdateDir.append("/update/");
[42693]1352 }
1353 }
1354 }
1355
[43001]1356 if (RT_SUCCESS(rc))
1357 LogRel(("Guest Additions update directory is: %s\n",
1358 strUpdateDir.c_str()));
[42923]1359
[42937]1360 /* Create the installation directory. */
[43162]1361 int guestRc;
[50727]1362 rc = pSession->i_directoryCreateInternal(strUpdateDir,
1363 755 /* Mode */, DirectoryCreateFlag_Parents, &guestRc);
[42937]1364 if (RT_FAILURE(rc))
[43162]1365 {
1366 switch (rc)
1367 {
[45078]1368 case VERR_GSTCTL_GUEST_ERROR:
[44863]1369 hr = setProgressErrorMsg(VBOX_E_IPRT_ERROR,
[50709]1370 GuestProcess::i_guestErrorToString(guestRc));
[43162]1371 break;
1372
1373 default:
[44863]1374 hr = setProgressErrorMsg(VBOX_E_IPRT_ERROR,
[51441]1375 Utf8StrFmt(GuestSession::tr("Error creating installation directory \"%s\" on the guest: %Rrc"),
[51092]1376 strUpdateDir.c_str(), rc));
[43162]1377 break;
1378 }
1379 }
[42937]1380 if (RT_SUCCESS(rc))
1381 rc = setProgress(10);
1382
1383 if (RT_SUCCESS(rc))
[42693]1384 {
[42937]1385 /* Prepare the file(s) we want to copy over to the guest and
1386 * (maybe) want to run. */
[43002]1387 switch (osType)
[42923]1388 {
[42937]1389 case eOSType_Windows:
1390 {
[43001]1391 /* Do we need to install our certificates? We do this for W2K and up. */
1392 bool fInstallCert = false;
1393
[43053]1394 /* Only Windows 2000 and up need certificates to be installed. */
1395 if (RTStrVersionCompare(strOSVer.c_str(), "5.0") >= 0)
[43001]1396 {
1397 fInstallCert = true;
1398 LogRel(("Certificates for auto updating WHQL drivers will be installed\n"));
1399 }
1400 else
1401 LogRel(("Skipping installation of certificates for WHQL drivers\n"));
1402
1403 if (fInstallCert)
1404 {
[64939]1405 static struct { const char *pszDst, *pszIso; } const s_aCertFiles[] =
1406 {
1407 { "vbox.cer", "CERT/VBOX.CER" },
1408 { "vbox-sha1.cer", "CERT/VBOX_SHA1.CER" },
1409 { "vbox-sha256.cer", "CERT/VBOX_SHA256.CER" },
1410 { "vbox-sha256-r3.cer", "CERT/VBOX_SHA256_R3.CER" },
1411 { "oracle-vbox.cer", "CERT/ORACLE_VBOX.CER" },
1412 };
1413 uint32_t fCopyCertUtil = UPDATEFILE_FLAG_COPY_FROM_ISO;
1414 for (uint32_t i = 0; i < RT_ELEMENTS(s_aCertFiles); i++)
1415 {
1416 /* Skip if not present on the ISO. */
1417 uint32_t offIgn;
1418 size_t cbIgn;
1419 rc = RTIsoFsGetFileInfo(&iso, s_aCertFiles[i].pszIso, &offIgn, &cbIgn);
1420 if (RT_FAILURE(rc))
1421 continue;
1422
1423 /* Copy the certificate certificate. */
1424 Utf8Str const strDstCert(strUpdateDir + s_aCertFiles[i].pszDst);
1425 mFiles.push_back(InstallerFile(s_aCertFiles[i].pszIso,
1426 strDstCert,
1427 UPDATEFILE_FLAG_COPY_FROM_ISO | UPDATEFILE_FLAG_OPTIONAL));
1428
1429 /* Out certificate installation utility. */
1430 /* First pass: Copy over the file (first time only) + execute it to remove any
1431 * existing VBox certificates. */
1432 GuestProcessStartupInfo siCertUtilRem;
1433 siCertUtilRem.mName = "VirtualBox Certificate Utility, removing old VirtualBox certificates";
1434 siCertUtilRem.mArguments.push_back(Utf8Str("remove-trusted-publisher"));
1435 siCertUtilRem.mArguments.push_back(Utf8Str("--root")); /* Add root certificate as well. */
1436 siCertUtilRem.mArguments.push_back(strDstCert);
1437 siCertUtilRem.mArguments.push_back(strDstCert);
1438 mFiles.push_back(InstallerFile("CERT/VBOXCERTUTIL.EXE",
1439 strUpdateDir + "VBoxCertUtil.exe",
1440 fCopyCertUtil | UPDATEFILE_FLAG_EXECUTE | UPDATEFILE_FLAG_OPTIONAL,
1441 siCertUtilRem));
1442 fCopyCertUtil = 0;
1443 /* Second pass: Only execute (but don't copy) again, this time installng the
1444 * recent certificates just copied over. */
1445 GuestProcessStartupInfo siCertUtilAdd;
1446 siCertUtilAdd.mName = "VirtualBox Certificate Utility, installing VirtualBox certificates";
1447 siCertUtilAdd.mArguments.push_back(Utf8Str("add-trusted-publisher"));
1448 siCertUtilAdd.mArguments.push_back(Utf8Str("--root")); /* Add root certificate as well. */
1449 siCertUtilAdd.mArguments.push_back(strDstCert);
1450 siCertUtilAdd.mArguments.push_back(strDstCert);
1451 mFiles.push_back(InstallerFile("CERT/VBOXCERTUTIL.EXE",
1452 strUpdateDir + "VBoxCertUtil.exe",
1453 UPDATEFILE_FLAG_EXECUTE | UPDATEFILE_FLAG_OPTIONAL,
1454 siCertUtilAdd));
1455 }
[43001]1456 }
1457 /* The installers in different flavors, as we don't know (and can't assume)
1458 * the guest's bitness. */
[42937]1459 mFiles.push_back(InstallerFile("VBOXWINDOWSADDITIONS_X86.EXE",
[43001]1460 strUpdateDir + "VBoxWindowsAdditions-x86.exe",
1461 UPDATEFILE_FLAG_COPY_FROM_ISO));
[42937]1462 mFiles.push_back(InstallerFile("VBOXWINDOWSADDITIONS_AMD64.EXE",
[43001]1463 strUpdateDir + "VBoxWindowsAdditions-amd64.exe",
1464 UPDATEFILE_FLAG_COPY_FROM_ISO));
1465 /* The stub loader which decides which flavor to run. */
[42937]1466 GuestProcessStartupInfo siInstaller;
[43001]1467 siInstaller.mName = "VirtualBox Windows Guest Additions Installer";
[43052]1468 /* Set a running timeout of 5 minutes -- the Windows Guest Additions
1469 * setup can take quite a while, so be on the safe side. */
1470 siInstaller.mTimeoutMS = 5 * 60 * 1000;
[42937]1471 siInstaller.mArguments.push_back(Utf8Str("/S")); /* We want to install in silent mode. */
1472 siInstaller.mArguments.push_back(Utf8Str("/l")); /* ... and logging enabled. */
1473 /* Don't quit VBoxService during upgrade because it still is used for this
1474 * piece of code we're in right now (that is, here!) ... */
1475 siInstaller.mArguments.push_back(Utf8Str("/no_vboxservice_exit"));
1476 /* Tell the installer to report its current installation status
1477 * using a running VBoxTray instance via balloon messages in the
1478 * Windows taskbar. */
1479 siInstaller.mArguments.push_back(Utf8Str("/post_installstatus"));
[46524]1480 /* Add optional installer command line arguments from the API to the
1481 * installer's startup info. */
[50727]1482 rc = i_addProcessArguments(siInstaller.mArguments, mArguments);
[46524]1483 AssertRC(rc);
[42937]1484 /* If the caller does not want to wait for out guest update process to end,
1485 * complete the progress object now so that the caller can do other work. */
1486 if (mFlags & AdditionsUpdateFlag_WaitForUpdateStartOnly)
1487 siInstaller.mFlags |= ProcessCreateFlag_WaitForProcessStartOnly;
1488 mFiles.push_back(InstallerFile("VBOXWINDOWSADDITIONS.EXE",
[43001]1489 strUpdateDir + "VBoxWindowsAdditions.exe",
1490 UPDATEFILE_FLAG_COPY_FROM_ISO | UPDATEFILE_FLAG_EXECUTE, siInstaller));
[42937]1491 break;
1492 }
1493 case eOSType_Linux:
1494 /** @todo Add Linux support. */
1495 break;
1496 case eOSType_Solaris:
1497 /** @todo Add Solaris support. */
1498 break;
1499 default:
[43002]1500 AssertReleaseMsgFailed(("Unsupported guest type: %d\n", osType));
[42937]1501 break;
[42923]1502 }
[42693]1503 }
1504
1505 if (RT_SUCCESS(rc))
[42923]1506 {
[42937]1507 /* We want to spend 40% total for all copying operations. So roughly
1508 * calculate the specific percentage step of each copied file. */
1509 uint8_t uOffset = 20; /* Start at 20%. */
[55535]1510 uint8_t uStep = 40 / (uint8_t)mFiles.size(); Assert(mFiles.size() <= 10);
[42937]1511
1512 LogRel(("Copying over Guest Additions update files to the guest ...\n"));
1513
[42923]1514 std::vector<InstallerFile>::const_iterator itFiles = mFiles.begin();
1515 while (itFiles != mFiles.end())
1516 {
[43001]1517 if (itFiles->fFlags & UPDATEFILE_FLAG_COPY_FROM_ISO)
[42923]1518 {
[43001]1519 bool fOptional = false;
1520 if (itFiles->fFlags & UPDATEFILE_FLAG_OPTIONAL)
1521 fOptional = true;
[50727]1522 rc = i_copyFileToGuest(pSession, &iso, itFiles->strSource, itFiles->strDest,
1523 fOptional, NULL /* cbSize */);
[43001]1524 if (RT_FAILURE(rc))
1525 {
1526 hr = setProgressErrorMsg(VBOX_E_IPRT_ERROR,
[51441]1527 Utf8StrFmt(GuestSession::tr("Error while copying file \"%s\" to \"%s\" on the guest: %Rrc"),
[43052]1528 itFiles->strSource.c_str(), itFiles->strDest.c_str(), rc));
[43001]1529 break;
1530 }
[42923]1531 }
[42937]1532
1533 rc = setProgress(uOffset);
1534 if (RT_FAILURE(rc))
1535 break;
1536 uOffset += uStep;
1537
[56030]1538 ++itFiles;
[42923]1539 }
1540 }
[42693]1541
[43001]1542 /* Done copying, close .ISO file. */
1543 RTIsoFsClose(&iso);
1544
[42693]1545 if (RT_SUCCESS(rc))
1546 {
[42937]1547 /* We want to spend 35% total for all copying operations. So roughly
1548 * calculate the specific percentage step of each copied file. */
1549 uint8_t uOffset = 60; /* Start at 60%. */
[55535]1550 uint8_t uStep = 35 / (uint8_t)mFiles.size(); Assert(mFiles.size() <= 10);
[42937]1551
1552 LogRel(("Executing Guest Additions update files ...\n"));
1553
[42923]1554 std::vector<InstallerFile>::iterator itFiles = mFiles.begin();
1555 while (itFiles != mFiles.end())
1556 {
[42937]1557 if (itFiles->fFlags & UPDATEFILE_FLAG_EXECUTE)
1558 {
[50727]1559 rc = i_runFileOnGuest(pSession, itFiles->mProcInfo);
[42937]1560 if (RT_FAILURE(rc))
1561 break;
1562 }
1563
1564 rc = setProgress(uOffset);
[42923]1565 if (RT_FAILURE(rc))
1566 break;
[42937]1567 uOffset += uStep;
1568
[56030]1569 ++itFiles;
[42923]1570 }
1571 }
[42716]1572
[42923]1573 if (RT_SUCCESS(rc))
[42937]1574 {
1575 LogRel(("Automatic update of Guest Additions succeeded\n"));
1576 rc = setProgressSuccess();
1577 }
[43034]1578 }
1579 }
1580
1581 if (RT_FAILURE(rc))
1582 {
1583 if (rc == VERR_CANCELLED)
1584 {
1585 LogRel(("Automatic update of Guest Additions was canceled\n"));
1586
1587 hr = setProgressErrorMsg(VBOX_E_IPRT_ERROR,
1588 Utf8StrFmt(GuestSession::tr("Installation was canceled")));
1589 }
1590 else
1591 {
1592 Utf8Str strError = Utf8StrFmt("No further error information available (%Rrc)", rc);
1593 if (!mProgress.isNull()) /* Progress object is optional. */
[42937]1594 {
[44863]1595 com::ProgressErrorInfo errorInfo(mProgress);
1596 if ( errorInfo.isFullAvailable()
1597 || errorInfo.isBasicAvailable())
[42937]1598 {
[44863]1599 strError = errorInfo.getText();
[42937]1600 }
[43034]1601 }
[42693]1602
[44863]1603 LogRel(("Automatic update of Guest Additions failed: %s (%Rhrc)\n",
1604 strError.c_str(), hr));
[42693]1605 }
[43034]1606
1607 LogRel(("Please install Guest Additions manually\n"));
[42693]1608 }
1609
[44863]1610 /** @todo Clean up copied / left over installation files. */
1611
[42693]1612 LogFlowFuncLeaveRC(rc);
1613 return rc;
1614}
1615
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use