VirtualBox

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

Last change on this file since 45805 was 45572, checked in by vboxsync, 12 years ago

GuestSessionImplTasks.cpp: Better Windows OS version detection for automatic updates.

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

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