VirtualBox

source: vbox/trunk/src/VBox/Main/src-client/GuestCtrlImplTasks.cpp@ 38290

Last change on this file since 38290 was 38290, checked in by vboxsync, 13 years ago

GuestCtrl: Update.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 46.2 KB
Line 
1/* $Id: */
2/** @file
3 * VirtualBox Guest Control - Threaded operations (tasks).
4 */
5
6/*
7 * Copyright (C) 2011 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#include <memory>
19
20#include "GuestImpl.h"
21#include "GuestCtrlImplPrivate.h"
22
23#include "Global.h"
24#include "ConsoleImpl.h"
25#include "ProgressImpl.h"
26#include "VMMDev.h"
27
28#include "AutoCaller.h"
29#include "Logging.h"
30
31#include <VBox/VMMDev.h>
32#ifdef VBOX_WITH_GUEST_CONTROL
33# include <VBox/com/array.h>
34# include <VBox/com/ErrorInfo.h>
35#endif
36
37#include <iprt/file.h>
38#include <iprt/isofs.h>
39#include <iprt/list.h>
40#include <iprt/path.h>
41
42GuestTask::GuestTask(TaskType aTaskType, Guest *aThat, Progress *aProgress)
43 : taskType(aTaskType),
44 pGuest(aThat),
45 progress(aProgress),
46 rc(S_OK)
47{
48
49}
50
51GuestTask::~GuestTask()
52{
53
54}
55
56int GuestTask::startThread()
57{
58 return RTThreadCreate(NULL, GuestTask::taskThread, this,
59 0, RTTHREADTYPE_MAIN_HEAVY_WORKER, 0,
60 "GuestTask");
61}
62
63/* static */
64DECLCALLBACK(int) GuestTask::taskThread(RTTHREAD /* aThread */, void *pvUser)
65{
66 std::auto_ptr<GuestTask> task(static_cast<GuestTask*>(pvUser));
67 AssertReturn(task.get(), VERR_GENERAL_FAILURE);
68
69 Guest *pGuest = task->pGuest;
70
71 LogFlowFuncEnter();
72 LogFlowFunc(("Guest %p\n", pGuest));
73
74 HRESULT rc = S_OK;
75
76 switch (task->taskType)
77 {
78#ifdef VBOX_WITH_GUEST_CONTROL
79 case TaskType_CopyFileToGuest:
80 {
81 rc = pGuest->taskCopyFileToGuest(task.get());
82 break;
83 }
84 case TaskType_CopyFileFromGuest:
85 {
86 rc = pGuest->taskCopyFileFromGuest(task.get());
87 break;
88 }
89 case TaskType_UpdateGuestAdditions:
90 {
91 rc = pGuest->taskUpdateGuestAdditions(task.get());
92 break;
93 }
94#endif
95 default:
96 AssertMsgFailed(("Invalid task type %u specified!\n", task->taskType));
97 break;
98 }
99
100 LogFlowFunc(("rc=%Rhrc\n", rc));
101 LogFlowFuncLeave();
102
103 return VINF_SUCCESS;
104}
105
106/* static */
107int GuestTask::uploadProgress(unsigned uPercent, void *pvUser)
108{
109 GuestTask *pTask = *(GuestTask**)pvUser;
110
111 if ( pTask
112 && !pTask->progress.isNull())
113 {
114 BOOL fCanceled;
115 pTask->progress->COMGETTER(Canceled)(&fCanceled);
116 if (fCanceled)
117 return -1;
118 pTask->progress->SetCurrentOperationProgress(uPercent);
119 }
120 return VINF_SUCCESS;
121}
122
123/* static */
124HRESULT GuestTask::setProgressErrorInfo(HRESULT hr, ComObjPtr<Progress> pProgress,
125 const char *pszText, ...)
126{
127 BOOL fCanceled;
128 BOOL fCompleted;
129 if ( SUCCEEDED(pProgress->COMGETTER(Canceled(&fCanceled)))
130 && !fCanceled
131 && SUCCEEDED(pProgress->COMGETTER(Completed(&fCompleted)))
132 && !fCompleted)
133 {
134 va_list va;
135 va_start(va, pszText);
136 HRESULT hr2 = pProgress->notifyCompleteV(hr,
137 COM_IIDOF(IGuest),
138 Guest::getStaticComponentName(),
139 pszText,
140 va);
141 va_end(va);
142 if (hr2 == S_OK) /* If unable to retrieve error, return input error. */
143 hr2 = hr;
144 return hr2;
145 }
146 return S_OK;
147}
148
149/* static */
150HRESULT GuestTask::setProgressErrorInfo(HRESULT hr,
151 ComObjPtr<Progress> pProgress, ComObjPtr<Guest> pGuest)
152{
153 return setProgressErrorInfo(hr, pProgress,
154 Utf8Str(com::ErrorInfo((IGuest*)pGuest, COM_IIDOF(IGuest)).getText()).c_str());
155}
156
157#ifdef VBOX_WITH_GUEST_CONTROL
158HRESULT Guest::taskCopyFileToGuest(GuestTask *aTask)
159{
160 LogFlowFuncEnter();
161
162 AutoCaller autoCaller(this);
163 if (FAILED(autoCaller.rc())) return autoCaller.rc();
164
165 /*
166 * Do *not* take a write lock here since we don't (and won't)
167 * touch any class-specific data (of IGuest) here - only the member functions
168 * which get called here can do that.
169 */
170
171 HRESULT rc = S_OK;
172
173 try
174 {
175 Guest *pGuest = aTask->pGuest;
176 AssertPtr(pGuest);
177
178 /* Does our source file exist? */
179 if (!RTFileExists(aTask->strSource.c_str()))
180 {
181 rc = GuestTask::setProgressErrorInfo(VBOX_E_IPRT_ERROR, aTask->progress,
182 Guest::tr("Source file \"%s\" does not exist, or is not a file"),
183 aTask->strSource.c_str());
184 }
185 else
186 {
187 RTFILE fileSource;
188 int vrc = RTFileOpen(&fileSource, aTask->strSource.c_str(),
189 RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_WRITE);
190 if (RT_FAILURE(vrc))
191 {
192 rc = GuestTask::setProgressErrorInfo(VBOX_E_IPRT_ERROR, aTask->progress,
193 Guest::tr("Could not open source file \"%s\" for reading (%Rrc)"),
194 aTask->strSource.c_str(), vrc);
195 }
196 else
197 {
198 uint64_t cbSize;
199 vrc = RTFileGetSize(fileSource, &cbSize);
200 if (RT_FAILURE(vrc))
201 {
202 rc = GuestTask::setProgressErrorInfo(VBOX_E_IPRT_ERROR, aTask->progress,
203 Guest::tr("Could not query file size of \"%s\" (%Rrc)"),
204 aTask->strSource.c_str(), vrc);
205 }
206 else
207 {
208 com::SafeArray<IN_BSTR> args;
209 com::SafeArray<IN_BSTR> env;
210
211 /*
212 * Prepare tool command line.
213 */
214 char szOutput[RTPATH_MAX];
215 if (RTStrPrintf(szOutput, sizeof(szOutput), "--output=%s", aTask->strDest.c_str()) <= sizeof(szOutput) - 1)
216 {
217 /*
218 * Normalize path slashes, based on the detected guest.
219 */
220 Utf8Str osType = mData.mOSTypeId;
221 if ( osType.contains("Microsoft", Utf8Str::CaseInsensitive)
222 || osType.contains("Windows", Utf8Str::CaseInsensitive))
223 {
224 /* We have a Windows guest. */
225 RTPathChangeToDosSlashes(szOutput, true /* Force conversion. */);
226 }
227 else /* ... or something which isn't from Redmond ... */
228 {
229 RTPathChangeToUnixSlashes(szOutput, true /* Force conversion. */);
230 }
231
232 args.push_back(Bstr(szOutput).raw()); /* We want to write a file ... */
233 }
234 else
235 {
236 rc = GuestTask::setProgressErrorInfo(VBOX_E_IPRT_ERROR, aTask->progress,
237 Guest::tr("Error preparing command line"));
238 }
239
240 ComPtr<IProgress> execProgress;
241 ULONG uPID;
242 if (SUCCEEDED(rc))
243 {
244 LogRel(("Copying file \"%s\" to guest \"%s\" (%u bytes) ...\n",
245 aTask->strSource.c_str(), aTask->strDest.c_str(), cbSize));
246 /*
247 * Okay, since we gathered all stuff we need until now to start the
248 * actual copying, start the guest part now.
249 */
250 rc = pGuest->ExecuteProcess(Bstr(VBOXSERVICE_TOOL_CAT).raw(),
251 ExecuteProcessFlag_Hidden
252 | ExecuteProcessFlag_WaitForProcessStartOnly,
253 ComSafeArrayAsInParam(args),
254 ComSafeArrayAsInParam(env),
255 Bstr(aTask->strUserName).raw(),
256 Bstr(aTask->strPassword).raw(),
257 5 * 1000 /* Wait 5s for getting the process started. */,
258 &uPID, execProgress.asOutParam());
259 if (FAILED(rc))
260 rc = GuestTask::setProgressErrorInfo(rc, aTask->progress, pGuest);
261 }
262
263 if (SUCCEEDED(rc))
264 {
265 BOOL fCompleted = FALSE;
266 BOOL fCanceled = FALSE;
267
268 size_t cbToRead = cbSize;
269 size_t cbTransfered = 0;
270 size_t cbRead;
271 SafeArray<BYTE> aInputData(_64K);
272 while ( SUCCEEDED(execProgress->COMGETTER(Completed(&fCompleted)))
273 && !fCompleted)
274 {
275 if (!cbToRead)
276 cbRead = 0;
277 else
278 {
279 vrc = RTFileRead(fileSource, (uint8_t*)aInputData.raw(),
280 RT_MIN(cbToRead, _64K), &cbRead);
281 /*
282 * Some other error occured? There might be a chance that RTFileRead
283 * could not resolve/map the native error code to an IPRT code, so just
284 * print a generic error.
285 */
286 if (RT_FAILURE(vrc))
287 {
288 rc = GuestTask::setProgressErrorInfo(VBOX_E_IPRT_ERROR, aTask->progress,
289 Guest::tr("Could not read from file \"%s\" (%Rrc)"),
290 aTask->strSource.c_str(), vrc);
291 break;
292 }
293 }
294
295 /* Resize buffer to reflect amount we just have read.
296 * Size 0 is allowed! */
297 aInputData.resize(cbRead);
298
299 ULONG uFlags = ProcessInputFlag_None;
300 /* Did we reach the end of the content we want to transfer (last chunk)? */
301 if ( (cbRead < _64K)
302 /* Did we reach the last block which is exactly _64K? */
303 || (cbToRead - cbRead == 0)
304 /* ... or does the user want to cancel? */
305 || ( SUCCEEDED(aTask->progress->COMGETTER(Canceled(&fCanceled)))
306 && fCanceled)
307 )
308 {
309 uFlags |= ProcessInputFlag_EndOfFile;
310 }
311
312 /* Transfer the current chunk ... */
313 ULONG uBytesWritten;
314 rc = pGuest->SetProcessInput(uPID, uFlags,
315 10 * 1000 /* Wait 10s for getting the input data transfered. */,
316 ComSafeArrayAsInParam(aInputData), &uBytesWritten);
317 if (FAILED(rc))
318 {
319 rc = GuestTask::setProgressErrorInfo(rc, aTask->progress, pGuest);
320 break;
321 }
322
323 Assert(cbRead <= cbToRead);
324 Assert(cbToRead >= cbRead);
325 cbToRead -= cbRead;
326
327 cbTransfered += uBytesWritten;
328 Assert(cbTransfered <= cbSize);
329 aTask->progress->SetCurrentOperationProgress(cbTransfered / (cbSize / 100.0));
330
331 /* End of file reached? */
332 if (cbToRead == 0)
333 break;
334
335 /* Did the user cancel the operation above? */
336 if (fCanceled)
337 break;
338
339 /* Progress canceled by Main API? */
340 if ( SUCCEEDED(execProgress->COMGETTER(Canceled(&fCanceled)))
341 && fCanceled)
342 {
343 rc = GuestTask::setProgressErrorInfo(VBOX_E_IPRT_ERROR, aTask->progress,
344 Guest::tr("Copy operation of file \"%s\" was canceled on guest side"),
345 aTask->strSource.c_str());
346 break;
347 }
348 }
349
350 if (SUCCEEDED(rc))
351 {
352 /*
353 * If we got here this means the started process either was completed,
354 * canceled or we simply got all stuff transferred.
355 */
356 ExecuteProcessStatus_T retStatus;
357 ULONG uRetExitCode;
358 rc = pGuest->executeWaitForStatusChange(uPID, 10 * 1000 /* 10s timeout. */,
359 &retStatus, &uRetExitCode);
360 if (FAILED(rc))
361 {
362 rc = GuestTask::setProgressErrorInfo(rc, aTask->progress, pGuest);
363 }
364 else
365 {
366 if ( uRetExitCode != 0
367 || retStatus != ExecuteProcessStatus_TerminatedNormally)
368 {
369 rc = GuestTask::setProgressErrorInfo(VBOX_E_IPRT_ERROR, aTask->progress,
370 Guest::tr("Guest reported error %u while copying file \"%s\" to \"%s\""),
371 uRetExitCode, aTask->strSource.c_str(), aTask->strDest.c_str());
372 }
373 }
374 }
375
376 if (SUCCEEDED(rc))
377 {
378 if (fCanceled)
379 {
380 /*
381 * In order to make the progress object to behave nicely, we also have to
382 * notify the object with a complete event when it's canceled.
383 */
384 aTask->progress->notifyComplete(VBOX_E_IPRT_ERROR,
385 COM_IIDOF(IGuest),
386 Guest::getStaticComponentName(),
387 Guest::tr("Copying file \"%s\" canceled"), aTask->strSource.c_str());
388 }
389 else
390 {
391 /*
392 * Even if we succeeded until here make sure to check whether we really transfered
393 * everything.
394 */
395 if ( cbSize > 0
396 && cbTransfered == 0)
397 {
398 /* If nothing was transfered but the file size was > 0 then "vbox_cat" wasn't able to write
399 * to the destination -> access denied. */
400 rc = GuestTask::setProgressErrorInfo(VBOX_E_IPRT_ERROR, aTask->progress,
401 Guest::tr("Access denied when copying file \"%s\" to \"%s\""),
402 aTask->strSource.c_str(), aTask->strDest.c_str());
403 }
404 else if (cbTransfered < cbSize)
405 {
406 /* If we did not copy all let the user know. */
407 rc = GuestTask::setProgressErrorInfo(VBOX_E_IPRT_ERROR, aTask->progress,
408 Guest::tr("Copying file \"%s\" failed (%u/%u bytes transfered)"),
409 aTask->strSource.c_str(), cbTransfered, cbSize);
410 }
411 else /* Yay, all went fine! */
412 aTask->progress->notifyComplete(S_OK);
413 }
414 }
415 }
416 }
417 RTFileClose(fileSource);
418 }
419 }
420 }
421 catch (HRESULT aRC)
422 {
423 rc = aRC;
424 }
425
426 /* Clean up */
427 aTask->rc = rc;
428
429 LogFlowFunc(("rc=%Rhrc\n", rc));
430 LogFlowFuncLeave();
431
432 return VINF_SUCCESS;
433}
434
435HRESULT Guest::taskCopyFileFromGuest(GuestTask *aTask)
436{
437 LogFlowFuncEnter();
438
439 AutoCaller autoCaller(this);
440 if (FAILED(autoCaller.rc())) return autoCaller.rc();
441
442 /*
443 * Do *not* take a write lock here since we don't (and won't)
444 * touch any class-specific data (of IGuest) here - only the member functions
445 * which get called here can do that.
446 */
447
448 HRESULT rc = S_OK;
449
450 try
451 {
452 Guest *pGuest = aTask->pGuest;
453 AssertPtr(pGuest);
454
455 /* Does our source file exist? */
456 BOOL fFileExists;
457 rc = pGuest->FileExists(Bstr(aTask->strSource).raw(),
458 Bstr(aTask->strUserName).raw(), Bstr(aTask->strPassword).raw(),
459 &fFileExists);
460 if (SUCCEEDED(rc))
461 {
462 if (!fFileExists)
463 rc = GuestTask::setProgressErrorInfo(VBOX_E_IPRT_ERROR, aTask->progress,
464 Guest::tr("Source file \"%s\" does not exist, or is not a file"),
465 aTask->strSource.c_str());
466 }
467
468 /* Query file size to make an estimate for our progress object. */
469 if (SUCCEEDED(rc))
470 {
471 LONG64 lFileSize;
472 rc = pGuest->FileQuerySize(Bstr(aTask->strSource).raw(),
473 Bstr(aTask->strUserName).raw(), Bstr(aTask->strPassword).raw(),
474 &lFileSize);
475 if (FAILED(rc))
476 rc = GuestTask::setProgressErrorInfo(rc, aTask->progress, pGuest);
477
478 com::SafeArray<IN_BSTR> args;
479 com::SafeArray<IN_BSTR> env;
480
481 if (SUCCEEDED(rc))
482 {
483 /*
484 * Prepare tool command line.
485 */
486 char szSource[RTPATH_MAX];
487 if (RTStrPrintf(szSource, sizeof(szSource), "%s", aTask->strSource.c_str()) <= sizeof(szSource) - 1)
488 {
489 /*
490 * Normalize path slashes, based on the detected guest.
491 */
492 Utf8Str osType = mData.mOSTypeId;
493 if ( osType.contains("Microsoft", Utf8Str::CaseInsensitive)
494 || osType.contains("Windows", Utf8Str::CaseInsensitive))
495 {
496 /* We have a Windows guest. */
497 RTPathChangeToDosSlashes(szSource, true /* Force conversion. */);
498 }
499 else /* ... or something which isn't from Redmond ... */
500 {
501 RTPathChangeToUnixSlashes(szSource, true /* Force conversion. */);
502 }
503
504 args.push_back(Bstr(szSource).raw()); /* Tell our cat tool which file to output. */
505 }
506 else
507 rc = GuestTask::setProgressErrorInfo(VBOX_E_IPRT_ERROR, aTask->progress,
508 Guest::tr("Error preparing command line"));
509 }
510
511 ComPtr<IProgress> execProgress;
512 ULONG uPID;
513 if (SUCCEEDED(rc))
514 {
515 LogRel(("Copying file \"%s\" to host \"%s\" (%u bytes) ...\n",
516 aTask->strSource.c_str(), aTask->strDest.c_str(), lFileSize));
517
518 /*
519 * Okay, since we gathered all stuff we need until now to start the
520 * actual copying, start the guest part now.
521 */
522 rc = pGuest->ExecuteProcess(Bstr(VBOXSERVICE_TOOL_CAT).raw(),
523 ExecuteProcessFlag_Hidden,
524 ComSafeArrayAsInParam(args),
525 ComSafeArrayAsInParam(env),
526 Bstr(aTask->strUserName).raw(),
527 Bstr(aTask->strPassword).raw(),
528 5 * 1000 /* Wait 5s for getting the process started. */,
529 &uPID, execProgress.asOutParam());
530 if (FAILED(rc))
531 rc = GuestTask::setProgressErrorInfo(rc, aTask->progress, pGuest);
532 }
533
534 if (SUCCEEDED(rc))
535 {
536 BOOL fCompleted = FALSE;
537 BOOL fCanceled = FALSE;
538
539 RTFILE hFileDest;
540 int vrc = RTFileOpen(&hFileDest, aTask->strDest.c_str(),
541 RTFILE_O_READWRITE | RTFILE_O_OPEN_CREATE | RTFILE_O_DENY_WRITE);
542 if (RT_FAILURE(vrc))
543 rc = GuestTask::setProgressErrorInfo(VBOX_E_IPRT_ERROR, aTask->progress,
544 Guest::tr("Unable to create/open destination file \"%s\", rc=%Rrc"),
545 aTask->strDest.c_str(), vrc);
546 else
547 {
548 size_t cbToRead = lFileSize;
549 size_t cbTransfered = 0;
550 SafeArray<BYTE> aOutputData(_64K);
551 while (SUCCEEDED(execProgress->COMGETTER(Completed(&fCompleted))))
552 {
553 rc = this->GetProcessOutput(uPID, ProcessOutputFlag_None,
554 10 * 1000 /* Timeout in ms */,
555 _64K, ComSafeArrayAsOutParam(aOutputData));
556 if (SUCCEEDED(rc))
557 {
558 if (!aOutputData.size())
559 {
560 if (cbToRead)
561 rc = GuestTask::setProgressErrorInfo(VBOX_E_IPRT_ERROR, aTask->progress,
562 Guest::tr("Unexpected end of file \"%s\" (%u bytes left)"),
563 aTask->strSource.c_str(), cbToRead);
564 break;
565 }
566
567 vrc = RTFileWrite(hFileDest, aOutputData.raw(), aOutputData.size(), NULL /* No partial writes */);
568 if (RT_FAILURE(vrc))
569 {
570 rc = GuestTask::setProgressErrorInfo(VBOX_E_IPRT_ERROR, aTask->progress,
571 Guest::tr("Error writing to file \"%s\" (%u bytes left), rc=%Rrc"),
572 aTask->strSource.c_str(), cbToRead, vrc);
573 break;
574 }
575
576 cbToRead -= aOutputData.size();
577 cbTransfered += aOutputData.size();
578
579 aTask->progress->SetCurrentOperationProgress(cbTransfered / (lFileSize / 100.0));
580 }
581 else
582 {
583 rc = GuestTask::setProgressErrorInfo(rc, aTask->progress, pGuest);
584 break;
585 }
586 }
587
588 if (SUCCEEDED(rc))
589 aTask->progress->notifyComplete(S_OK);
590
591 RTFileClose(hFileDest);
592 }
593 }
594 }
595 }
596 catch (HRESULT aRC)
597 {
598 rc = aRC;
599 }
600
601 /* Clean up */
602 aTask->rc = rc;
603
604 LogFlowFunc(("rc=%Rhrc\n", rc));
605 LogFlowFuncLeave();
606
607 return VINF_SUCCESS;
608}
609
610HRESULT Guest::taskUpdateGuestAdditions(GuestTask *aTask)
611{
612 LogFlowFuncEnter();
613
614 AutoCaller autoCaller(this);
615 if (FAILED(autoCaller.rc())) return autoCaller.rc();
616
617 /*
618 * Do *not* take a write lock here since we don't (and won't)
619 * touch any class-specific data (of IGuest) here - only the member functions
620 * which get called here can do that.
621 */
622
623 HRESULT rc = S_OK;
624 BOOL fCompleted;
625 BOOL fCanceled;
626
627 try
628 {
629 Guest *pGuest = aTask->pGuest;
630 AssertPtr(pGuest);
631
632 aTask->progress->SetCurrentOperationProgress(10);
633
634 /*
635 * Determine guest OS type and the required installer image.
636 * At the moment only Windows guests are supported.
637 */
638 Utf8Str installerImage;
639 Bstr osTypeId;
640 if ( SUCCEEDED(pGuest->COMGETTER(OSTypeId(osTypeId.asOutParam())))
641 && !osTypeId.isEmpty())
642 {
643 Utf8Str osTypeIdUtf8(osTypeId); /* Needed for .contains(). */
644 if ( osTypeIdUtf8.contains("Microsoft", Utf8Str::CaseInsensitive)
645 || osTypeIdUtf8.contains("Windows", Utf8Str::CaseInsensitive))
646 {
647 if (osTypeIdUtf8.contains("64", Utf8Str::CaseInsensitive))
648 installerImage = "VBOXWINDOWSADDITIONS_AMD64.EXE";
649 else
650 installerImage = "VBOXWINDOWSADDITIONS_X86.EXE";
651 /* Since the installers are located in the root directory,
652 * no further path processing needs to be done (yet). */
653 }
654 else /* Everything else is not supported (yet). */
655 throw GuestTask::setProgressErrorInfo(VBOX_E_NOT_SUPPORTED, aTask->progress,
656 Guest::tr("Detected guest OS (%s) does not support automatic Guest Additions updating, please update manually"),
657 osTypeIdUtf8.c_str());
658 }
659 else
660 throw GuestTask::setProgressErrorInfo(VBOX_E_NOT_SUPPORTED, aTask->progress,
661 Guest::tr("Could not detected guest OS type/version, please update manually"));
662 Assert(!installerImage.isEmpty());
663
664 /*
665 * Try to open the .ISO file and locate the specified installer.
666 */
667 RTISOFSFILE iso;
668 int vrc = RTIsoFsOpen(&iso, aTask->strSource.c_str());
669 if (RT_FAILURE(vrc))
670 {
671 rc = GuestTask::setProgressErrorInfo(VBOX_E_FILE_ERROR, aTask->progress,
672 Guest::tr("Invalid installation medium detected: \"%s\""),
673 aTask->strSource.c_str());
674 }
675 else
676 {
677 uint32_t cbOffset;
678 size_t cbLength;
679 vrc = RTIsoFsGetFileInfo(&iso, installerImage.c_str(), &cbOffset, &cbLength);
680 if ( RT_SUCCESS(vrc)
681 && cbOffset
682 && cbLength)
683 {
684 vrc = RTFileSeek(iso.file, cbOffset, RTFILE_SEEK_BEGIN, NULL);
685 if (RT_FAILURE(vrc))
686 rc = GuestTask::setProgressErrorInfo(VBOX_E_IPRT_ERROR, aTask->progress,
687 Guest::tr("Could not seek to setup file on installation medium \"%s\" (%Rrc)"),
688 aTask->strSource.c_str(), vrc);
689 }
690 else
691 {
692 switch (vrc)
693 {
694 case VERR_FILE_NOT_FOUND:
695 rc = GuestTask::setProgressErrorInfo(VBOX_E_IPRT_ERROR, aTask->progress,
696 Guest::tr("Setup file was not found on installation medium \"%s\""),
697 aTask->strSource.c_str());
698 break;
699
700 default:
701 rc = GuestTask::setProgressErrorInfo(VBOX_E_IPRT_ERROR, aTask->progress,
702 Guest::tr("An unknown error (%Rrc) occured while retrieving information of setup file on installation medium \"%s\""),
703 vrc, aTask->strSource.c_str());
704 break;
705 }
706 }
707
708 /* Specify the ouput path on the guest side. */
709 Utf8Str strInstallerPath = "%TEMP%\\VBoxWindowsAdditions.exe";
710
711 if (RT_SUCCESS(vrc))
712 {
713 /* Okay, we're ready to start our copy routine on the guest! */
714 aTask->progress->SetCurrentOperationProgress(15);
715
716 /* Prepare command line args. */
717 com::SafeArray<IN_BSTR> args;
718 com::SafeArray<IN_BSTR> env;
719
720 args.push_back(Bstr("--output").raw()); /* We want to write a file ... */
721 args.push_back(Bstr(strInstallerPath.c_str()).raw()); /* ... with this path. */
722
723 if (SUCCEEDED(rc))
724 {
725 ComPtr<IProgress> progressCat;
726 ULONG uPID;
727
728 /*
729 * Start built-in "vbox_cat" tool (inside VBoxService) to
730 * copy over/pipe the data into a file on the guest (with
731 * system rights, no username/password specified).
732 */
733 rc = pGuest->executeProcessInternal(Bstr(VBOXSERVICE_TOOL_CAT).raw(),
734 ExecuteProcessFlag_Hidden
735 | ExecuteProcessFlag_WaitForProcessStartOnly,
736 ComSafeArrayAsInParam(args),
737 ComSafeArrayAsInParam(env),
738 Bstr("").raw() /* Username. */,
739 Bstr("").raw() /* Password */,
740 5 * 1000 /* Wait 5s for getting the process started. */,
741 &uPID, progressCat.asOutParam(), &vrc);
742 if (FAILED(rc))
743 {
744 /* Errors which return VBOX_E_NOT_SUPPORTED can be safely skipped by the caller
745 * to silently fall back to "normal" (old) .ISO mounting. */
746
747 /* Due to a very limited COM error range we use vrc for a more detailed error
748 * lookup to figure out what went wrong. */
749 switch (vrc)
750 {
751 /* Guest execution service is not (yet) ready. This basically means that either VBoxService
752 * is not running (yet) or that the Guest Additions are too old (because VBoxService does not
753 * support the guest execution feature in this version). */
754 case VERR_NOT_FOUND:
755 LogRel(("Guest Additions seem not to be installed yet\n"));
756 rc = GuestTask::setProgressErrorInfo(VBOX_E_NOT_SUPPORTED, aTask->progress,
757 Guest::tr("Guest Additions seem not to be installed or are not ready to update yet"));
758 break;
759
760 /* Getting back a VERR_INVALID_PARAMETER indicates that the installed Guest Additions are supporting the guest
761 * execution but not the built-in "vbox_cat" tool of VBoxService (< 4.0). */
762 case VERR_INVALID_PARAMETER:
763 LogRel(("Guest Additions are installed but don't supported automatic updating\n"));
764 rc = GuestTask::setProgressErrorInfo(VBOX_E_NOT_SUPPORTED, aTask->progress,
765 Guest::tr("Installed Guest Additions do not support automatic updating"));
766 break;
767
768 case VERR_TIMEOUT:
769 LogRel(("Guest was unable to start copying the Guest Additions setup within time\n"));
770 rc = GuestTask::setProgressErrorInfo(E_FAIL, aTask->progress,
771 Guest::tr("Guest was unable to start copying the Guest Additions setup within time"));
772 break;
773
774 default:
775 rc = GuestTask::setProgressErrorInfo(E_FAIL, aTask->progress,
776 Guest::tr("Error copying Guest Additions setup file to guest path \"%s\" (%Rrc)"),
777 strInstallerPath.c_str(), vrc);
778 break;
779 }
780 }
781 else
782 {
783 LogRel(("Automatic update of Guest Additions started, using \"%s\"\n", aTask->strSource.c_str()));
784 LogRel(("Copying Guest Additions installer \"%s\" to \"%s\" on guest ...\n",
785 installerImage.c_str(), strInstallerPath.c_str()));
786 aTask->progress->SetCurrentOperationProgress(20);
787
788 /* Wait for process to exit ... */
789 SafeArray<BYTE> aInputData(_64K);
790 while ( SUCCEEDED(progressCat->COMGETTER(Completed(&fCompleted)))
791 && !fCompleted)
792 {
793 size_t cbRead;
794 /* cbLength contains remaining bytes of our installer file
795 * opened above to read. */
796 size_t cbToRead = RT_MIN(cbLength, _64K);
797 if (cbToRead)
798 {
799 vrc = RTFileRead(iso.file, (uint8_t*)aInputData.raw(), cbToRead, &cbRead);
800 if ( cbRead
801 && RT_SUCCESS(vrc))
802 {
803 /* Resize buffer to reflect amount we just have read. */
804 if (cbRead > 0)
805 aInputData.resize(cbRead);
806
807 /* Did we reach the end of the content we want to transfer (last chunk)? */
808 ULONG uFlags = ProcessInputFlag_None;
809 if ( (cbRead < _64K)
810 /* Did we reach the last block which is exactly _64K? */
811 || (cbToRead - cbRead == 0)
812 /* ... or does the user want to cancel? */
813 || ( SUCCEEDED(aTask->progress->COMGETTER(Canceled(&fCanceled)))
814 && fCanceled)
815 )
816 {
817 uFlags |= ProcessInputFlag_EndOfFile;
818 }
819
820 /* Transfer the current chunk ... */
821 #ifdef DEBUG_andy
822 LogRel(("Copying Guest Additions (%u bytes left) ...\n", cbLength));
823 #endif
824 ULONG uBytesWritten;
825 rc = pGuest->SetProcessInput(uPID, uFlags,
826 10 * 1000 /* Wait 10s for getting the input data transfered. */,
827 ComSafeArrayAsInParam(aInputData), &uBytesWritten);
828 if (FAILED(rc))
829 {
830 rc = GuestTask::setProgressErrorInfo(rc, aTask->progress, pGuest);
831 break;
832 }
833
834 /* If task was canceled above also cancel the process execution. */
835 if (fCanceled)
836 progressCat->Cancel();
837
838 #ifdef DEBUG_andy
839 LogRel(("Copying Guest Additions (%u bytes written) ...\n", uBytesWritten));
840 #endif
841 Assert(cbLength >= uBytesWritten);
842 cbLength -= uBytesWritten;
843 }
844 else if (RT_FAILURE(vrc))
845 {
846 rc = GuestTask::setProgressErrorInfo(VBOX_E_IPRT_ERROR, aTask->progress,
847 Guest::tr("Error while reading setup file \"%s\" (To read: %u, Size: %u) from installation medium (%Rrc)"),
848 installerImage.c_str(), cbToRead, cbLength, vrc);
849 }
850 }
851
852 /* Internal progress canceled? */
853 if ( SUCCEEDED(progressCat->COMGETTER(Canceled(&fCanceled)))
854 && fCanceled)
855 {
856 aTask->progress->Cancel();
857 break;
858 }
859 }
860 }
861 }
862 }
863 RTIsoFsClose(&iso);
864
865 if ( SUCCEEDED(rc)
866 && ( SUCCEEDED(aTask->progress->COMGETTER(Canceled(&fCanceled)))
867 && !fCanceled
868 )
869 )
870 {
871 /*
872 * Installer was transferred successfully, so let's start it
873 * (with system rights).
874 */
875 LogRel(("Preparing to execute Guest Additions update ...\n"));
876 aTask->progress->SetCurrentOperationProgress(66);
877
878 /* Prepare command line args for installer. */
879 com::SafeArray<IN_BSTR> installerArgs;
880 com::SafeArray<IN_BSTR> installerEnv;
881
882 /** @todo Only Windows! */
883 installerArgs.push_back(Bstr(strInstallerPath).raw()); /* The actual (internal) installer image (as argv[0]). */
884 /* Note that starting at Windows Vista the lovely session 0 separation applies:
885 * This means that if we run an application with the profile/security context
886 * of VBoxService (system rights!) we're not able to show any UI. */
887 installerArgs.push_back(Bstr("/S").raw()); /* We want to install in silent mode. */
888 installerArgs.push_back(Bstr("/l").raw()); /* ... and logging enabled. */
889 /* Don't quit VBoxService during upgrade because it still is used for this
890 * piece of code we're in right now (that is, here!) ... */
891 installerArgs.push_back(Bstr("/no_vboxservice_exit").raw());
892 /* Tell the installer to report its current installation status
893 * using a running VBoxTray instance via balloon messages in the
894 * Windows taskbar. */
895 installerArgs.push_back(Bstr("/post_installstatus").raw());
896
897 /*
898 * Start the just copied over installer with system rights
899 * in silent mode on the guest. Don't use the hidden flag since there
900 * may be pop ups the user has to process.
901 */
902 ComPtr<IProgress> progressInstaller;
903 ULONG uPID;
904 rc = pGuest->executeProcessInternal(Bstr(strInstallerPath).raw(),
905 ExecuteProcessFlag_WaitForProcessStartOnly,
906 ComSafeArrayAsInParam(installerArgs),
907 ComSafeArrayAsInParam(installerEnv),
908 Bstr("").raw() /* Username */,
909 Bstr("").raw() /* Password */,
910 10 * 1000 /* Wait 10s for getting the process started */,
911 &uPID, progressInstaller.asOutParam(), &vrc);
912 if (SUCCEEDED(rc))
913 {
914 LogRel(("Guest Additions update is running ...\n"));
915
916 /* If the caller does not want to wait for out guest update process to end,
917 * complete the progress object now so that the caller can do other work. */
918 if (aTask->uFlags & AdditionsUpdateFlag_WaitForUpdateStartOnly)
919 aTask->progress->notifyComplete(S_OK);
920 else
921 aTask->progress->SetCurrentOperationProgress(70);
922
923 /* Wait until the Guest Additions installer finishes ... */
924 while ( SUCCEEDED(progressInstaller->COMGETTER(Completed(&fCompleted)))
925 && !fCompleted)
926 {
927 if ( SUCCEEDED(aTask->progress->COMGETTER(Canceled(&fCanceled)))
928 && fCanceled)
929 {
930 progressInstaller->Cancel();
931 break;
932 }
933 /* Progress canceled by Main API? */
934 if ( SUCCEEDED(progressInstaller->COMGETTER(Canceled(&fCanceled)))
935 && fCanceled)
936 {
937 break;
938 }
939 RTThreadSleep(100);
940 }
941
942 ExecuteProcessStatus_T retStatus;
943 ULONG uRetExitCode, uRetFlags;
944 rc = pGuest->GetProcessStatus(uPID, &uRetExitCode, &uRetFlags, &retStatus);
945 if (SUCCEEDED(rc))
946 {
947 if (fCompleted)
948 {
949 if (uRetExitCode == 0)
950 {
951 LogRel(("Guest Additions update successful!\n"));
952 if ( SUCCEEDED(aTask->progress->COMGETTER(Completed(&fCompleted)))
953 && !fCompleted)
954 aTask->progress->notifyComplete(S_OK);
955 }
956 else
957 {
958 LogRel(("Guest Additions update failed (Exit code=%u, Status=%u, Flags=%u)\n",
959 uRetExitCode, retStatus, uRetFlags));
960 rc = GuestTask::setProgressErrorInfo(VBOX_E_IPRT_ERROR, aTask->progress,
961 Guest::tr("Guest Additions update failed with exit code=%u (status=%u, flags=%u)"),
962 uRetExitCode, retStatus, uRetFlags);
963 }
964 }
965 else if ( SUCCEEDED(progressInstaller->COMGETTER(Canceled(&fCanceled)))
966 && fCanceled)
967 {
968 LogRel(("Guest Additions update was canceled\n"));
969 rc = GuestTask::setProgressErrorInfo(VBOX_E_IPRT_ERROR, aTask->progress,
970 Guest::tr("Guest Additions update was canceled by the guest with exit code=%u (status=%u, flags=%u)"),
971 uRetExitCode, retStatus, uRetFlags);
972 }
973 else
974 {
975 LogRel(("Guest Additions update was canceled by the user\n"));
976 }
977 }
978 else
979 rc = GuestTask::setProgressErrorInfo(rc, aTask->progress, pGuest);
980 }
981 else
982 rc = GuestTask::setProgressErrorInfo(rc, aTask->progress, pGuest);
983 }
984 }
985 }
986 catch (HRESULT aRC)
987 {
988 rc = aRC;
989 }
990
991 /* Clean up */
992 aTask->rc = rc;
993
994 LogFlowFunc(("rc=%Rhrc\n", rc));
995 LogFlowFuncLeave();
996
997 return VINF_SUCCESS;
998}
999#endif
1000
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