VirtualBox

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

Last change on this file since 38237 was 38237, checked in by vboxsync, 14 years ago

Build fix.

  • 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->waitForProcessStatusChange(uPID, &retStatus, &uRetExitCode, 10 * 1000 /* 10s timeout. */);
359 if (FAILED(rc))
360 {
361 rc = GuestTask::setProgressErrorInfo(rc, aTask->progress, pGuest);
362 }
363 else
364 {
365 if ( uRetExitCode != 0
366 || retStatus != ExecuteProcessStatus_TerminatedNormally)
367 {
368 rc = GuestTask::setProgressErrorInfo(VBOX_E_IPRT_ERROR, aTask->progress,
369 Guest::tr("Guest reported error %u while copying file \"%s\" to \"%s\""),
370 uRetExitCode, aTask->strSource.c_str(), aTask->strDest.c_str());
371 }
372 }
373 }
374
375 if (SUCCEEDED(rc))
376 {
377 if (fCanceled)
378 {
379 /*
380 * In order to make the progress object to behave nicely, we also have to
381 * notify the object with a complete event when it's canceled.
382 */
383 aTask->progress->notifyComplete(VBOX_E_IPRT_ERROR,
384 COM_IIDOF(IGuest),
385 Guest::getStaticComponentName(),
386 Guest::tr("Copying file \"%s\" canceled"), aTask->strSource.c_str());
387 }
388 else
389 {
390 /*
391 * Even if we succeeded until here make sure to check whether we really transfered
392 * everything.
393 */
394 if ( cbSize > 0
395 && cbTransfered == 0)
396 {
397 /* If nothing was transfered but the file size was > 0 then "vbox_cat" wasn't able to write
398 * to the destination -> access denied. */
399 rc = GuestTask::setProgressErrorInfo(VBOX_E_IPRT_ERROR, aTask->progress,
400 Guest::tr("Access denied when copying file \"%s\" to \"%s\""),
401 aTask->strSource.c_str(), aTask->strDest.c_str());
402 }
403 else if (cbTransfered < cbSize)
404 {
405 /* If we did not copy all let the user know. */
406 rc = GuestTask::setProgressErrorInfo(VBOX_E_IPRT_ERROR, aTask->progress,
407 Guest::tr("Copying file \"%s\" failed (%u/%u bytes transfered)"),
408 aTask->strSource.c_str(), cbTransfered, cbSize);
409 }
410 else /* Yay, all went fine! */
411 aTask->progress->notifyComplete(S_OK);
412 }
413 }
414 }
415 }
416 RTFileClose(fileSource);
417 }
418 }
419 }
420 catch (HRESULT aRC)
421 {
422 rc = aRC;
423 }
424
425 /* Clean up */
426 aTask->rc = rc;
427
428 LogFlowFunc(("rc=%Rhrc\n", rc));
429 LogFlowFuncLeave();
430
431 return VINF_SUCCESS;
432}
433
434HRESULT Guest::taskCopyFileFromGuest(GuestTask *aTask)
435{
436 LogFlowFuncEnter();
437
438 AutoCaller autoCaller(this);
439 if (FAILED(autoCaller.rc())) return autoCaller.rc();
440
441 /*
442 * Do *not* take a write lock here since we don't (and won't)
443 * touch any class-specific data (of IGuest) here - only the member functions
444 * which get called here can do that.
445 */
446
447 HRESULT rc = S_OK;
448
449 try
450 {
451 Guest *pGuest = aTask->pGuest;
452 AssertPtr(pGuest);
453
454 /* Does our source file exist? */
455 BOOL fFileExists;
456 rc = pGuest->FileExists(Bstr(aTask->strSource).raw(),
457 Bstr(aTask->strUserName).raw(), Bstr(aTask->strPassword).raw(),
458 &fFileExists);
459 if (SUCCEEDED(rc))
460 {
461 if (!fFileExists)
462 rc = GuestTask::setProgressErrorInfo(VBOX_E_IPRT_ERROR, aTask->progress,
463 Guest::tr("Source file \"%s\" does not exist, or is not a file"),
464 aTask->strSource.c_str());
465 }
466
467 /* Query file size to make an estimate for our progress object. */
468 if (SUCCEEDED(rc))
469 {
470 LONG64 lFileSize;
471 rc = pGuest->FileQuerySize(Bstr(aTask->strSource).raw(),
472 Bstr(aTask->strUserName).raw(), Bstr(aTask->strPassword).raw(),
473 &lFileSize);
474 if (FAILED(rc))
475 rc = GuestTask::setProgressErrorInfo(rc, aTask->progress, pGuest);
476
477 com::SafeArray<IN_BSTR> args;
478 com::SafeArray<IN_BSTR> env;
479
480 if (SUCCEEDED(rc))
481 {
482 /*
483 * Prepare tool command line.
484 */
485 char szSource[RTPATH_MAX];
486 if (RTStrPrintf(szSource, sizeof(szSource), "%s", aTask->strSource.c_str()) <= sizeof(szSource) - 1)
487 {
488 /*
489 * Normalize path slashes, based on the detected guest.
490 */
491 Utf8Str osType = mData.mOSTypeId;
492 if ( osType.contains("Microsoft", Utf8Str::CaseInsensitive)
493 || osType.contains("Windows", Utf8Str::CaseInsensitive))
494 {
495 /* We have a Windows guest. */
496 RTPathChangeToDosSlashes(szSource, true /* Force conversion. */);
497 }
498 else /* ... or something which isn't from Redmond ... */
499 {
500 RTPathChangeToUnixSlashes(szSource, true /* Force conversion. */);
501 }
502
503 args.push_back(Bstr(szSource).raw()); /* Tell our cat tool which file to output. */
504 }
505 else
506 rc = GuestTask::setProgressErrorInfo(VBOX_E_IPRT_ERROR, aTask->progress,
507 Guest::tr("Error preparing command line"));
508 }
509
510 ComPtr<IProgress> execProgress;
511 ULONG uPID;
512 if (SUCCEEDED(rc))
513 {
514 LogRel(("Copying file \"%s\" to host \"%s\" (%u bytes) ...\n",
515 aTask->strSource.c_str(), aTask->strDest.c_str(), lFileSize));
516
517 /*
518 * Okay, since we gathered all stuff we need until now to start the
519 * actual copying, start the guest part now.
520 */
521 rc = pGuest->ExecuteProcess(Bstr(VBOXSERVICE_TOOL_CAT).raw(),
522 ExecuteProcessFlag_Hidden,
523 ComSafeArrayAsInParam(args),
524 ComSafeArrayAsInParam(env),
525 Bstr(aTask->strUserName).raw(),
526 Bstr(aTask->strPassword).raw(),
527 5 * 1000 /* Wait 5s for getting the process started. */,
528 &uPID, execProgress.asOutParam());
529 if (FAILED(rc))
530 rc = GuestTask::setProgressErrorInfo(rc, aTask->progress, pGuest);
531 }
532
533 if (SUCCEEDED(rc))
534 {
535 BOOL fCompleted = FALSE;
536 BOOL fCanceled = FALSE;
537
538 RTFILE hFileDest;
539 int vrc = RTFileOpen(&hFileDest, aTask->strDest.c_str(),
540 RTFILE_O_READWRITE | RTFILE_O_OPEN_CREATE | RTFILE_O_DENY_WRITE);
541 if (RT_FAILURE(vrc))
542 rc = GuestTask::setProgressErrorInfo(VBOX_E_IPRT_ERROR, aTask->progress,
543 Guest::tr("Unable to create/open destination file \"%s\", rc=%Rrc"),
544 aTask->strDest.c_str(), vrc);
545 else
546 {
547 size_t cbToRead = lFileSize;
548 size_t cbTransfered = 0;
549 SafeArray<BYTE> aOutputData(_64K);
550 while (SUCCEEDED(execProgress->COMGETTER(Completed(&fCompleted))))
551 {
552 rc = this->GetProcessOutput(uPID, ProcessOutputFlag_None,
553 10 * 1000 /* Timeout in ms */,
554 _64K, ComSafeArrayAsOutParam(aOutputData));
555 if (SUCCEEDED(rc))
556 {
557 if (!aOutputData.size())
558 {
559 if (cbToRead)
560 rc = GuestTask::setProgressErrorInfo(VBOX_E_IPRT_ERROR, aTask->progress,
561 Guest::tr("Unexpected end of file \"%s\" (%u bytes left)"),
562 aTask->strSource.c_str(), cbToRead);
563 break;
564 }
565
566 vrc = RTFileWrite(hFileDest, aOutputData.raw(), aOutputData.size(), NULL /* No partial writes */);
567 if (RT_FAILURE(vrc))
568 {
569 rc = GuestTask::setProgressErrorInfo(VBOX_E_IPRT_ERROR, aTask->progress,
570 Guest::tr("Error writing to file \"%s\" (%u bytes left), rc=%Rrc"),
571 aTask->strSource.c_str(), cbToRead, vrc);
572 break;
573 }
574
575 cbToRead -= aOutputData.size();
576 cbTransfered += aOutputData.size();
577
578 aTask->progress->SetCurrentOperationProgress(cbTransfered / (lFileSize / 100.0));
579 }
580 else
581 {
582 rc = GuestTask::setProgressErrorInfo(rc, aTask->progress, pGuest);
583 break;
584 }
585 }
586
587 if (SUCCEEDED(rc))
588 aTask->progress->notifyComplete(S_OK);
589
590 RTFileClose(hFileDest);
591 }
592 }
593 }
594 }
595 catch (HRESULT aRC)
596 {
597 rc = aRC;
598 }
599
600 /* Clean up */
601 aTask->rc = rc;
602
603 LogFlowFunc(("rc=%Rhrc\n", rc));
604 LogFlowFuncLeave();
605
606 return VINF_SUCCESS;
607}
608
609HRESULT Guest::taskUpdateGuestAdditions(GuestTask *aTask)
610{
611 LogFlowFuncEnter();
612
613 AutoCaller autoCaller(this);
614 if (FAILED(autoCaller.rc())) return autoCaller.rc();
615
616 /*
617 * Do *not* take a write lock here since we don't (and won't)
618 * touch any class-specific data (of IGuest) here - only the member functions
619 * which get called here can do that.
620 */
621
622 HRESULT rc = S_OK;
623 BOOL fCompleted;
624 BOOL fCanceled;
625
626 try
627 {
628 Guest *pGuest = aTask->pGuest;
629 AssertPtr(pGuest);
630
631 aTask->progress->SetCurrentOperationProgress(10);
632
633 /*
634 * Determine guest OS type and the required installer image.
635 * At the moment only Windows guests are supported.
636 */
637 Utf8Str installerImage;
638 Bstr osTypeId;
639 if ( SUCCEEDED(pGuest->COMGETTER(OSTypeId(osTypeId.asOutParam())))
640 && !osTypeId.isEmpty())
641 {
642 Utf8Str osTypeIdUtf8(osTypeId); /* Needed for .contains(). */
643 if ( osTypeIdUtf8.contains("Microsoft", Utf8Str::CaseInsensitive)
644 || osTypeIdUtf8.contains("Windows", Utf8Str::CaseInsensitive))
645 {
646 if (osTypeIdUtf8.contains("64", Utf8Str::CaseInsensitive))
647 installerImage = "VBOXWINDOWSADDITIONS_AMD64.EXE";
648 else
649 installerImage = "VBOXWINDOWSADDITIONS_X86.EXE";
650 /* Since the installers are located in the root directory,
651 * no further path processing needs to be done (yet). */
652 }
653 else /* Everything else is not supported (yet). */
654 throw GuestTask::setProgressErrorInfo(VBOX_E_NOT_SUPPORTED, aTask->progress,
655 Guest::tr("Detected guest OS (%s) does not support automatic Guest Additions updating, please update manually"),
656 osTypeIdUtf8.c_str());
657 }
658 else
659 throw GuestTask::setProgressErrorInfo(VBOX_E_NOT_SUPPORTED, aTask->progress,
660 Guest::tr("Could not detected guest OS type/version, please update manually"));
661 Assert(!installerImage.isEmpty());
662
663 /*
664 * Try to open the .ISO file and locate the specified installer.
665 */
666 RTISOFSFILE iso;
667 int vrc = RTIsoFsOpen(&iso, aTask->strSource.c_str());
668 if (RT_FAILURE(vrc))
669 {
670 rc = GuestTask::setProgressErrorInfo(VBOX_E_FILE_ERROR, aTask->progress,
671 Guest::tr("Invalid installation medium detected: \"%s\""),
672 aTask->strSource.c_str());
673 }
674 else
675 {
676 uint32_t cbOffset;
677 size_t cbLength;
678 vrc = RTIsoFsGetFileInfo(&iso, installerImage.c_str(), &cbOffset, &cbLength);
679 if ( RT_SUCCESS(vrc)
680 && cbOffset
681 && cbLength)
682 {
683 vrc = RTFileSeek(iso.file, cbOffset, RTFILE_SEEK_BEGIN, NULL);
684 if (RT_FAILURE(vrc))
685 rc = GuestTask::setProgressErrorInfo(VBOX_E_IPRT_ERROR, aTask->progress,
686 Guest::tr("Could not seek to setup file on installation medium \"%s\" (%Rrc)"),
687 aTask->strSource.c_str(), vrc);
688 }
689 else
690 {
691 switch (vrc)
692 {
693 case VERR_FILE_NOT_FOUND:
694 rc = GuestTask::setProgressErrorInfo(VBOX_E_IPRT_ERROR, aTask->progress,
695 Guest::tr("Setup file was not found on installation medium \"%s\""),
696 aTask->strSource.c_str());
697 break;
698
699 default:
700 rc = GuestTask::setProgressErrorInfo(VBOX_E_IPRT_ERROR, aTask->progress,
701 Guest::tr("An unknown error (%Rrc) occured while retrieving information of setup file on installation medium \"%s\""),
702 vrc, aTask->strSource.c_str());
703 break;
704 }
705 }
706
707 /* Specify the ouput path on the guest side. */
708 Utf8Str strInstallerPath = "%TEMP%\\VBoxWindowsAdditions.exe";
709
710 if (RT_SUCCESS(vrc))
711 {
712 /* Okay, we're ready to start our copy routine on the guest! */
713 aTask->progress->SetCurrentOperationProgress(15);
714
715 /* Prepare command line args. */
716 com::SafeArray<IN_BSTR> args;
717 com::SafeArray<IN_BSTR> env;
718
719 args.push_back(Bstr("--output").raw()); /* We want to write a file ... */
720 args.push_back(Bstr(strInstallerPath.c_str()).raw()); /* ... with this path. */
721
722 if (SUCCEEDED(rc))
723 {
724 ComPtr<IProgress> progressCat;
725 ULONG uPID;
726
727 /*
728 * Start built-in "vbox_cat" tool (inside VBoxService) to
729 * copy over/pipe the data into a file on the guest (with
730 * system rights, no username/password specified).
731 */
732 rc = pGuest->executeProcessInternal(Bstr(VBOXSERVICE_TOOL_CAT).raw(),
733 ExecuteProcessFlag_Hidden
734 | ExecuteProcessFlag_WaitForProcessStartOnly,
735 ComSafeArrayAsInParam(args),
736 ComSafeArrayAsInParam(env),
737 Bstr("").raw() /* Username. */,
738 Bstr("").raw() /* Password */,
739 5 * 1000 /* Wait 5s for getting the process started. */,
740 &uPID, progressCat.asOutParam(), &vrc);
741 if (FAILED(rc))
742 {
743 /* Errors which return VBOX_E_NOT_SUPPORTED can be safely skipped by the caller
744 * to silently fall back to "normal" (old) .ISO mounting. */
745
746 /* Due to a very limited COM error range we use vrc for a more detailed error
747 * lookup to figure out what went wrong. */
748 switch (vrc)
749 {
750 /* Guest execution service is not (yet) ready. This basically means that either VBoxService
751 * is not running (yet) or that the Guest Additions are too old (because VBoxService does not
752 * support the guest execution feature in this version). */
753 case VERR_NOT_FOUND:
754 LogRel(("Guest Additions seem not to be installed yet\n"));
755 rc = GuestTask::setProgressErrorInfo(VBOX_E_NOT_SUPPORTED, aTask->progress,
756 Guest::tr("Guest Additions seem not to be installed or are not ready to update yet"));
757 break;
758
759 /* Getting back a VERR_INVALID_PARAMETER indicates that the installed Guest Additions are supporting the guest
760 * execution but not the built-in "vbox_cat" tool of VBoxService (< 4.0). */
761 case VERR_INVALID_PARAMETER:
762 LogRel(("Guest Additions are installed but don't supported automatic updating\n"));
763 rc = GuestTask::setProgressErrorInfo(VBOX_E_NOT_SUPPORTED, aTask->progress,
764 Guest::tr("Installed Guest Additions do not support automatic updating"));
765 break;
766
767 case VERR_TIMEOUT:
768 LogRel(("Guest was unable to start copying the Guest Additions setup within time\n"));
769 rc = GuestTask::setProgressErrorInfo(E_FAIL, aTask->progress,
770 Guest::tr("Guest was unable to start copying the Guest Additions setup within time"));
771 break;
772
773 default:
774 rc = GuestTask::setProgressErrorInfo(E_FAIL, aTask->progress,
775 Guest::tr("Error copying Guest Additions setup file to guest path \"%s\" (%Rrc)"),
776 strInstallerPath.c_str(), vrc);
777 break;
778 }
779 }
780 else
781 {
782 LogRel(("Automatic update of Guest Additions started, using \"%s\"\n", aTask->strSource.c_str()));
783 LogRel(("Copying Guest Additions installer \"%s\" to \"%s\" on guest ...\n",
784 installerImage.c_str(), strInstallerPath.c_str()));
785 aTask->progress->SetCurrentOperationProgress(20);
786
787 /* Wait for process to exit ... */
788 SafeArray<BYTE> aInputData(_64K);
789 while ( SUCCEEDED(progressCat->COMGETTER(Completed(&fCompleted)))
790 && !fCompleted)
791 {
792 size_t cbRead;
793 /* cbLength contains remaining bytes of our installer file
794 * opened above to read. */
795 size_t cbToRead = RT_MIN(cbLength, _64K);
796 if (cbToRead)
797 {
798 vrc = RTFileRead(iso.file, (uint8_t*)aInputData.raw(), cbToRead, &cbRead);
799 if ( cbRead
800 && RT_SUCCESS(vrc))
801 {
802 /* Resize buffer to reflect amount we just have read. */
803 if (cbRead > 0)
804 aInputData.resize(cbRead);
805
806 /* Did we reach the end of the content we want to transfer (last chunk)? */
807 ULONG uFlags = ProcessInputFlag_None;
808 if ( (cbRead < _64K)
809 /* Did we reach the last block which is exactly _64K? */
810 || (cbToRead - cbRead == 0)
811 /* ... or does the user want to cancel? */
812 || ( SUCCEEDED(aTask->progress->COMGETTER(Canceled(&fCanceled)))
813 && fCanceled)
814 )
815 {
816 uFlags |= ProcessInputFlag_EndOfFile;
817 }
818
819 /* Transfer the current chunk ... */
820 #ifdef DEBUG_andy
821 LogRel(("Copying Guest Additions (%u bytes left) ...\n", cbLength));
822 #endif
823 ULONG uBytesWritten;
824 rc = pGuest->SetProcessInput(uPID, uFlags,
825 10 * 1000 /* Wait 10s for getting the input data transfered. */,
826 ComSafeArrayAsInParam(aInputData), &uBytesWritten);
827 if (FAILED(rc))
828 {
829 rc = GuestTask::setProgressErrorInfo(rc, aTask->progress, pGuest);
830 break;
831 }
832
833 /* If task was canceled above also cancel the process execution. */
834 if (fCanceled)
835 progressCat->Cancel();
836
837 #ifdef DEBUG_andy
838 LogRel(("Copying Guest Additions (%u bytes written) ...\n", uBytesWritten));
839 #endif
840 Assert(cbLength >= uBytesWritten);
841 cbLength -= uBytesWritten;
842 }
843 else if (RT_FAILURE(vrc))
844 {
845 rc = GuestTask::setProgressErrorInfo(VBOX_E_IPRT_ERROR, aTask->progress,
846 Guest::tr("Error while reading setup file \"%s\" (To read: %u, Size: %u) from installation medium (%Rrc)"),
847 installerImage.c_str(), cbToRead, cbLength, vrc);
848 }
849 }
850
851 /* Internal progress canceled? */
852 if ( SUCCEEDED(progressCat->COMGETTER(Canceled(&fCanceled)))
853 && fCanceled)
854 {
855 aTask->progress->Cancel();
856 break;
857 }
858 }
859 }
860 }
861 }
862 RTIsoFsClose(&iso);
863
864 if ( SUCCEEDED(rc)
865 && ( SUCCEEDED(aTask->progress->COMGETTER(Canceled(&fCanceled)))
866 && !fCanceled
867 )
868 )
869 {
870 /*
871 * Installer was transferred successfully, so let's start it
872 * (with system rights).
873 */
874 LogRel(("Preparing to execute Guest Additions update ...\n"));
875 aTask->progress->SetCurrentOperationProgress(66);
876
877 /* Prepare command line args for installer. */
878 com::SafeArray<IN_BSTR> installerArgs;
879 com::SafeArray<IN_BSTR> installerEnv;
880
881 /** @todo Only Windows! */
882 installerArgs.push_back(Bstr(strInstallerPath).raw()); /* The actual (internal) installer image (as argv[0]). */
883 /* Note that starting at Windows Vista the lovely session 0 separation applies:
884 * This means that if we run an application with the profile/security context
885 * of VBoxService (system rights!) we're not able to show any UI. */
886 installerArgs.push_back(Bstr("/S").raw()); /* We want to install in silent mode. */
887 installerArgs.push_back(Bstr("/l").raw()); /* ... and logging enabled. */
888 /* Don't quit VBoxService during upgrade because it still is used for this
889 * piece of code we're in right now (that is, here!) ... */
890 installerArgs.push_back(Bstr("/no_vboxservice_exit").raw());
891 /* Tell the installer to report its current installation status
892 * using a running VBoxTray instance via balloon messages in the
893 * Windows taskbar. */
894 installerArgs.push_back(Bstr("/post_installstatus").raw());
895
896 /*
897 * Start the just copied over installer with system rights
898 * in silent mode on the guest. Don't use the hidden flag since there
899 * may be pop ups the user has to process.
900 */
901 ComPtr<IProgress> progressInstaller;
902 ULONG uPID;
903 rc = pGuest->executeProcessInternal(Bstr(strInstallerPath).raw(),
904 ExecuteProcessFlag_WaitForProcessStartOnly,
905 ComSafeArrayAsInParam(installerArgs),
906 ComSafeArrayAsInParam(installerEnv),
907 Bstr("").raw() /* Username */,
908 Bstr("").raw() /* Password */,
909 10 * 1000 /* Wait 10s for getting the process started */,
910 &uPID, progressInstaller.asOutParam(), &vrc);
911 if (SUCCEEDED(rc))
912 {
913 LogRel(("Guest Additions update is running ...\n"));
914
915 /* If the caller does not want to wait for out guest update process to end,
916 * complete the progress object now so that the caller can do other work. */
917 if (aTask->uFlags & AdditionsUpdateFlag_WaitForUpdateStartOnly)
918 aTask->progress->notifyComplete(S_OK);
919 else
920 aTask->progress->SetCurrentOperationProgress(70);
921
922 /* Wait until the Guest Additions installer finishes ... */
923 while ( SUCCEEDED(progressInstaller->COMGETTER(Completed(&fCompleted)))
924 && !fCompleted)
925 {
926 if ( SUCCEEDED(aTask->progress->COMGETTER(Canceled(&fCanceled)))
927 && fCanceled)
928 {
929 progressInstaller->Cancel();
930 break;
931 }
932 /* Progress canceled by Main API? */
933 if ( SUCCEEDED(progressInstaller->COMGETTER(Canceled(&fCanceled)))
934 && fCanceled)
935 {
936 break;
937 }
938 RTThreadSleep(100);
939 }
940
941 ExecuteProcessStatus_T retStatus;
942 ULONG uRetExitCode, uRetFlags;
943 rc = pGuest->GetProcessStatus(uPID, &uRetExitCode, &uRetFlags, &retStatus);
944 if (SUCCEEDED(rc))
945 {
946 if (fCompleted)
947 {
948 if (uRetExitCode == 0)
949 {
950 LogRel(("Guest Additions update successful!\n"));
951 if ( SUCCEEDED(aTask->progress->COMGETTER(Completed(&fCompleted)))
952 && !fCompleted)
953 aTask->progress->notifyComplete(S_OK);
954 }
955 else
956 {
957 LogRel(("Guest Additions update failed (Exit code=%u, Status=%u, Flags=%u)\n",
958 uRetExitCode, retStatus, uRetFlags));
959 rc = GuestTask::setProgressErrorInfo(VBOX_E_IPRT_ERROR, aTask->progress,
960 Guest::tr("Guest Additions update failed with exit code=%u (status=%u, flags=%u)"),
961 uRetExitCode, retStatus, uRetFlags);
962 }
963 }
964 else if ( SUCCEEDED(progressInstaller->COMGETTER(Canceled(&fCanceled)))
965 && fCanceled)
966 {
967 LogRel(("Guest Additions update was canceled\n"));
968 rc = GuestTask::setProgressErrorInfo(VBOX_E_IPRT_ERROR, aTask->progress,
969 Guest::tr("Guest Additions update was canceled by the guest with exit code=%u (status=%u, flags=%u)"),
970 uRetExitCode, retStatus, uRetFlags);
971 }
972 else
973 {
974 LogRel(("Guest Additions update was canceled by the user\n"));
975 }
976 }
977 else
978 rc = GuestTask::setProgressErrorInfo(rc, aTask->progress, pGuest);
979 }
980 else
981 rc = GuestTask::setProgressErrorInfo(rc, aTask->progress, pGuest);
982 }
983 }
984 }
985 catch (HRESULT aRC)
986 {
987 rc = aRC;
988 }
989
990 /* Clean up */
991 aTask->rc = rc;
992
993 LogFlowFunc(("rc=%Rhrc\n", rc));
994 LogFlowFuncLeave();
995
996 return VINF_SUCCESS;
997}
998#endif
999
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