VirtualBox

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

Last change on this file since 44870 was 44869, checked in by vboxsync, 12 years ago

Build fix.

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette