VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/HostUpdateImpl.cpp@ 85733

Last change on this file since 85733 was 85733, checked in by vboxsync, 5 years ago

Main/HostUpdateImpl.cpp: updateCheck() must validate the aCheckType argument and the i_updateCheckTask() method must indicate failure if and unsupported type for some reason ends up there. i_updateCheckTask must take extreme care to make sure pTask->m_ptrProgress->i_notifyComplete is always called or the client could get stuck forever. Removed HostUpdate::UpdateCheckTask::m_rc as it has no obvious purpose. bugref:7983

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 22.8 KB
Line 
1/* $Id: HostUpdateImpl.cpp 85733 2020-08-12 20:31:09Z vboxsync $ */
2/** @file
3 * IHostUpdate COM class implementations.
4 */
5
6/*
7 * Copyright (C) 2020 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. 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#define LOG_GROUP LOG_GROUP_MAIN_HOSTUPDATE
20
21#include <iprt/cpp/utils.h>
22#include <iprt/param.h>
23#include <iprt/path.h>
24#include <iprt/http.h>
25#include <iprt/system.h>
26#include <iprt/message.h>
27#include <iprt/pipe.h>
28#include <iprt/env.h>
29#include <iprt/process.h>
30#include <iprt/assert.h>
31#include <iprt/err.h>
32#include <iprt/stream.h>
33#include <iprt/time.h>
34#include <VBox/com/defs.h>
35#include <VBox/version.h>
36
37#include "HostImpl.h"
38#include "HostUpdateImpl.h"
39#include "ProgressImpl.h"
40#include "AutoCaller.h"
41#include "LoggingNew.h"
42#include "VirtualBoxImpl.h"
43#include "ThreadTask.h"
44#include "SystemPropertiesImpl.h"
45#include "VirtualBoxBase.h"
46
47
48////////////////////////////////////////////////////////////////////////////////
49//
50// HostUpdate private data definition
51//
52////////////////////////////////////////////////////////////////////////////////
53
54
55class HostUpdate::UpdateCheckTask : public ThreadTask
56{
57public:
58 UpdateCheckTask(UpdateCheckType_T aCheckType, HostUpdate *aThat, Progress *aProgress)
59 : m_checkType(aCheckType)
60 , m_pHostUpdate(aThat)
61 , m_ptrProgress(aProgress)
62 {
63 m_strTaskName = "UpdateCheckTask";
64 }
65 ~UpdateCheckTask() { }
66
67private:
68 void handler();
69
70 UpdateCheckType_T m_checkType;
71 HostUpdate *m_pHostUpdate;
72
73 /** Smart pointer to the progress object for this job. */
74 ComObjPtr<Progress> m_ptrProgress;
75
76 friend class HostUpdate; // allow member functions access to private data
77};
78
79void HostUpdate::UpdateCheckTask::handler()
80{
81 HostUpdate *pHostUpdate = this->m_pHostUpdate;
82
83 LogFlowFuncEnter();
84 LogFlowFunc(("HostUpdate %p\n", pHostUpdate));
85
86 HRESULT rc = pHostUpdate->i_updateCheckTask(this);
87
88 LogFlowFunc(("rc=%Rhrc\n", rc)); NOREF(rc);
89 LogFlowFuncLeave();
90}
91
92Utf8Str HostUpdate::i_platformInfo()
93{
94 /* Prepare platform report: */
95 Utf8Str strPlatform;
96
97#if defined (RT_OS_WINDOWS)
98 strPlatform = "win";
99#elif defined (RT_OS_LINUX)
100 strPlatform = "linux";
101#elif defined (RT_OS_DARWIN)
102 strPlatform = "macosx";
103#elif defined (RT_OS_OS2)
104 strPlatform = "os2";
105#elif defined (RT_OS_FREEBSD)
106 strPlatform = "freebsd";
107#elif defined (RT_OS_SOLARIS)
108 strPlatform = "solaris";
109#else
110 strPlatform = "unknown";
111#endif
112
113 /* The format is <system>.<bitness>: */
114 strPlatform.appendPrintf(".%lu", ARCH_BITS);
115
116 /* Add more system information: */
117 int vrc;
118#ifdef RT_OS_LINUX
119 // WORKAROUND:
120 // On Linux we try to generate information using script first of all..
121
122 /* Get script path: */
123 char szAppPrivPath[RTPATH_MAX];
124 vrc = RTPathAppPrivateNoArch(szAppPrivPath, sizeof(szAppPrivPath));
125 AssertRC(vrc);
126 if (RT_SUCCESS(vrc))
127 vrc = RTPathAppend(szAppPrivPath, sizeof(szAppPrivPath), "/VBoxSysInfo.sh");
128 AssertRC(vrc);
129 if (RT_SUCCESS(vrc))
130 {
131 RTPIPE hPipeR;
132 RTHANDLE hStdOutPipe;
133 hStdOutPipe.enmType = RTHANDLETYPE_PIPE;
134 vrc = RTPipeCreate(&hPipeR, &hStdOutPipe.u.hPipe, RTPIPE_C_INHERIT_WRITE);
135 AssertLogRelRC(vrc);
136
137 char const *szAppPrivArgs[2];
138 szAppPrivArgs[0] = szAppPrivPath;
139 szAppPrivArgs[1] = NULL;
140 RTPROCESS hProc = NIL_RTPROCESS;
141
142 /* Run script: */
143 vrc = RTProcCreateEx(szAppPrivPath, szAppPrivArgs, RTENV_DEFAULT, 0 /*fFlags*/, NULL /*phStdin*/, &hStdOutPipe,
144 NULL /*phStderr*/, NULL /*pszAsUser*/, NULL /*pszPassword*/, NULL /*pvExtraData*/, &hProc);
145
146 (void) RTPipeClose(hStdOutPipe.u.hPipe);
147 hStdOutPipe.u.hPipe = NIL_RTPIPE;
148
149 if (RT_SUCCESS(vrc))
150 {
151 RTPROCSTATUS ProcStatus;
152 size_t cbStdOutBuf = 0;
153 size_t offStdOutBuf = 0;
154 char *pszStdOutBuf = NULL;
155 do
156 {
157 if (hPipeR != NIL_RTPIPE)
158 {
159 char achBuf[1024];
160 size_t cbRead;
161 vrc = RTPipeReadBlocking(hPipeR, achBuf, sizeof(achBuf), &cbRead);
162 if (RT_SUCCESS(vrc))
163 {
164 /* grow the buffer? */
165 size_t cbBufReq = offStdOutBuf + cbRead + 1;
166 if ( cbBufReq > cbStdOutBuf
167 && cbBufReq < _256K)
168 {
169 size_t cbNew = RT_ALIGN_Z(cbBufReq, 16); // 1024
170 void *pvNew = RTMemRealloc(pszStdOutBuf, cbNew);
171 if (pvNew)
172 {
173 pszStdOutBuf = (char *)pvNew;
174 cbStdOutBuf = cbNew;
175 }
176 }
177
178 /* append if we've got room. */
179 if (cbBufReq <= cbStdOutBuf)
180 {
181 (void) memcpy(&pszStdOutBuf[offStdOutBuf], achBuf, cbRead);
182 offStdOutBuf = offStdOutBuf + cbRead;
183 pszStdOutBuf[offStdOutBuf] = '\0';
184 }
185 }
186 else
187 {
188 AssertLogRelMsg(vrc == VERR_BROKEN_PIPE, ("%Rrc\n", vrc));
189 RTPipeClose(hPipeR);
190 hPipeR = NIL_RTPIPE;
191 }
192 }
193
194 /*
195 * Service the process. Block if we have no pipe.
196 */
197 if (hProc != NIL_RTPROCESS)
198 {
199 vrc = RTProcWait(hProc,
200 hPipeR == NIL_RTPIPE ? RTPROCWAIT_FLAGS_BLOCK : RTPROCWAIT_FLAGS_NOBLOCK,
201 &ProcStatus);
202 if (RT_SUCCESS(vrc))
203 hProc = NIL_RTPROCESS;
204 else
205 AssertLogRelMsgStmt(vrc == VERR_PROCESS_RUNNING, ("%Rrc\n", vrc), hProc = NIL_RTPROCESS);
206 }
207 } while ( hPipeR != NIL_RTPIPE
208 || hProc != NIL_RTPROCESS);
209
210 if ( ProcStatus.enmReason == RTPROCEXITREASON_NORMAL
211 && ProcStatus.iStatus == 0) {
212 pszStdOutBuf[offStdOutBuf-1] = '\0'; // remove trailing newline
213 Utf8Str pszStdOutBufUTF8(pszStdOutBuf);
214 strPlatform.appendPrintf(" [%s]", pszStdOutBufUTF8.strip().c_str());
215 // For testing, here is some sample output:
216 //strPlatform.appendPrintf(" [Distribution: Redhat | Version: 7.6.1810 | Kernel: Linux version 3.10.0-952.27.2.el7.x86_64 (gcc version 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) ) #1 SMP Mon Jul 29 17:46:05 UTC 2019]");
217 }
218 }
219 else
220 vrc = VERR_TRY_AGAIN; /* (take the fallback path) */
221 }
222
223 LogRelFunc(("strPlatform (Linux) = %s\n", strPlatform.c_str()));
224
225 if (RT_FAILURE(vrc))
226#endif /* RT_OS_LINUX */
227 {
228 /* Use RTSystemQueryOSInfo: */
229 char szTmp[256];
230
231 vrc = RTSystemQueryOSInfo(RTSYSOSINFO_PRODUCT, szTmp, sizeof(szTmp));
232 if ((RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW) && szTmp[0] != '\0')
233 strPlatform.appendPrintf(" [Product: %s", szTmp);
234
235 vrc = RTSystemQueryOSInfo(RTSYSOSINFO_RELEASE, szTmp, sizeof(szTmp));
236 if ((RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW) && szTmp[0] != '\0')
237 strPlatform.appendPrintf(" %sRelease: %s", strlen(szTmp) == 0 ? "[" : "| ", szTmp);
238
239 vrc = RTSystemQueryOSInfo(RTSYSOSINFO_VERSION, szTmp, sizeof(szTmp));
240 if ((RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW) && szTmp[0] != '\0')
241 strPlatform.appendPrintf(" %sVersion: %s", strlen(szTmp) == 0 ? "[" : "| ", szTmp);
242
243 vrc = RTSystemQueryOSInfo(RTSYSOSINFO_SERVICE_PACK, szTmp, sizeof(szTmp));
244 if ((RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW) && szTmp[0] != '\0')
245 strPlatform.appendPrintf(" %sSP: %s]", strlen(szTmp) == 0 ? "[" : "| ", szTmp);
246
247 if (!strPlatform.endsWith("]"))
248 strPlatform.append("]");
249
250 LogRelFunc(("strPlatform = %s\n", strPlatform.c_str()));
251 }
252
253 return strPlatform;
254}
255
256HRESULT HostUpdate::i_checkForVBoxUpdate()
257{
258 HRESULT rc;
259
260 // Default to no update required
261 m_updateNeeded = FALSE;
262
263 // Following the sequence of steps in UIUpdateStepVirtualBox::sltStartStep()
264 // Build up our query URL starting with the URL basename
265 Bstr url("https://update.virtualbox.org/query.php/?");
266 Bstr platform;
267 rc = mVirtualBox->COMGETTER(PackageType)(platform.asOutParam());
268 if (FAILED(rc))
269 return setErrorVrc(rc, tr("%s: IVirtualBox::packageType() failed: %Rrc"), __FUNCTION__, rc);
270 url.appendPrintf("platform=%ls", platform.raw()); // e.g. SOLARIS_64BITS_GENERIC
271
272 // Get the complete current version string for the query URL
273 Bstr versionNormalized;
274 rc = mVirtualBox->COMGETTER(VersionNormalized)(versionNormalized.asOutParam());
275 if (FAILED(rc))
276 return setErrorVrc(rc, tr("%s: IVirtualBox::versionNormalized() failed: %Rrc"), __FUNCTION__, rc);
277 url.appendPrintf("&version=%ls", versionNormalized.raw()); // e.g. 6.1.1
278 // url.appendPrintf("&version=6.0.12"); // comment out previous line and uncomment this one for testing
279
280 ULONG revision;
281 rc = mVirtualBox->COMGETTER(Revision)(&revision);
282 if (FAILED(rc))
283 return setErrorVrc(rc, tr("%s: IVirtualBox::revision() failed: %Rrc"), __FUNCTION__, rc);
284 url.appendPrintf("_%ld", revision); // e.g. 135618
285
286 // acquire the System Properties interface
287 ComPtr<ISystemProperties> pSystemProperties;
288 rc = mVirtualBox->COMGETTER(SystemProperties)(pSystemProperties.asOutParam());
289 if (FAILED(rc))
290 return setErrorVrc(rc, tr("%s: IVirtualBox::systemProperties() failed: %Rrc"), __FUNCTION__, rc);
291
292 // Update the VBoxUpdate setting 'VBoxUpdateLastCheckDate'
293 RTTIME Time;
294 RTTIMESPEC TimeNow;
295 char szTimeStr[RTTIME_STR_LEN];
296
297 RTTimeToString(RTTimeExplode(&Time, RTTimeNow(&TimeNow)), szTimeStr, sizeof(szTimeStr));
298 LogRelFunc(("VBox updating UpdateDate with TimeString = %s\n", szTimeStr));
299 rc = pSystemProperties->COMSETTER(VBoxUpdateLastCheckDate)(Bstr(szTimeStr).raw());
300 if (FAILED(rc))
301 return rc; // ISystemProperties::setLastCheckDate calls setError() on failure
302
303 // Update the queryURL and the VBoxUpdate setting 'VBoxUpdateCount'
304 ULONG cVBoxUpdateCount = 0;
305 rc = pSystemProperties->COMGETTER(VBoxUpdateCount)(&cVBoxUpdateCount);
306 if (FAILED(rc))
307 return setErrorVrc(rc, tr("%s: retrieving ISystemProperties::VBoxUpdateCount failed: %Rrc"), __FUNCTION__, rc);
308
309 cVBoxUpdateCount++;
310
311 rc = pSystemProperties->COMSETTER(VBoxUpdateCount)(cVBoxUpdateCount);
312 if (FAILED(rc))
313 return rc; // ISystemProperties::setVBoxUpdateCount calls setError() on failure
314 url.appendPrintf("&count=%lu", cVBoxUpdateCount);
315
316 // Update the query URL and the VBoxUpdate settings (if necessary) with the 'Target' information.
317 VBoxUpdateTarget_T enmTarget = VBoxUpdateTarget_Stable; // default branch is 'stable'
318 rc = pSystemProperties->COMGETTER(VBoxUpdateTarget)(&enmTarget);
319 if (FAILED(rc))
320 return setErrorVrc(rc, tr("%s: retrieving ISystemProperties::Target failed: %Rrc"), __FUNCTION__, rc);
321
322 switch (enmTarget)
323 {
324 case VBoxUpdateTarget_AllReleases:
325 url.appendPrintf("&branch=allrelease"); // query.php expects 'allrelease' and not 'allreleases'
326 break;
327 case VBoxUpdateTarget_WithBetas:
328 url.appendPrintf("&branch=withbetas");
329 break;
330 case VBoxUpdateTarget_Stable:
331 default:
332 url.appendPrintf("&branch=stable");
333 break;
334 }
335
336 rc = pSystemProperties->COMSETTER(VBoxUpdateTarget)(enmTarget);
337 if (FAILED(rc))
338 return rc; // ISystemProperties::setTarget calls setError() on failure
339
340 LogRelFunc(("VBox update URL = %s\n", Utf8Str(url).c_str()));
341
342 // Setup the User-Agent headers for the GET request
343 Bstr version;
344 rc = mVirtualBox->COMGETTER(Version)(version.asOutParam()); // e.g. 6.1.0_RC1
345 if (FAILED(rc))
346 return setErrorVrc(rc, tr("%s: IVirtualBox::version() failed: %Rrc"), __FUNCTION__, rc);
347
348 Utf8StrFmt const strUserAgent("VirtualBox %ls <%s>", version.raw(), HostUpdate::i_platformInfo().c_str());
349 LogRelFunc(("userAgent = %s\n", strUserAgent.c_str()));
350
351 RTHTTP hHttp = NIL_RTHTTP;
352 int vrc = RTHttpCreate(&hHttp);
353 if (RT_FAILURE(vrc))
354 return setErrorVrc(vrc, tr("%s: RTHttpCreate() failed: %Rrc"), __FUNCTION__, vrc);
355
356 /// @todo Are there any other headers needed to be added first via RTHttpSetHeaders()?
357 vrc = RTHttpAddHeader(hHttp, "User-Agent", strUserAgent.c_str(), strUserAgent.length(), RTHTTPADDHDR_F_BACK);
358 if (RT_FAILURE(vrc))
359 return setErrorVrc(vrc, tr("%s: RTHttpAddHeader() failed: %Rrc (on User-Agent)"), __FUNCTION__, vrc);
360
361 ProxyMode_T enmProxyMode;
362 rc = pSystemProperties->COMGETTER(ProxyMode)(&enmProxyMode);
363 if (FAILED(rc))
364 return setErrorVrc(rc, tr("%s: ISystemProperties::proxyMode() failed: %Rrc"), __FUNCTION__, rc);
365
366 if (enmProxyMode == ProxyMode_Manual)
367 {
368 Bstr strProxyURL;
369
370 rc = pSystemProperties->COMGETTER(ProxyURL)(strProxyURL.asOutParam());
371 if (FAILED(rc))
372 return setErrorVrc(rc, tr("%s: ISystemProperties::proxyURL() failed: %Rrc"), __FUNCTION__, rc);
373 vrc = RTHttpSetProxyByUrl(hHttp, Utf8Str(strProxyURL).c_str());
374 if (RT_FAILURE(vrc))
375 return setErrorVrc(vrc, tr("%s: RTHttpSetProxyByUrl() failed: %Rrc"), __FUNCTION__, vrc);
376 }
377 else if (enmProxyMode == ProxyMode_System)
378 {
379 vrc = RTHttpUseSystemProxySettings(hHttp);
380 if (RT_FAILURE(vrc))
381 return setErrorVrc(vrc, tr("%s: RTHttpUseSystemProxySettings() failed: %Rrc"), __FUNCTION__, vrc);
382 }
383
384 void *pvResponse = 0;
385 size_t cbResponse = 0;
386 vrc = RTHttpGetBinary(hHttp, Utf8Str(url).c_str(), &pvResponse, &cbResponse);
387 if (RT_FAILURE(vrc))
388 return setErrorVrc(vrc, tr("%s: RTHttpGetBinary() failed: %Rrc"), __FUNCTION__, vrc);
389
390 RTCList<RTCString> lstHttpReply = RTCString((char *)pvResponse, (size_t)cbResponse).split(" ", RTCString::RemoveEmptyParts);
391 RTHttpFreeResponse(pvResponse);
392
393 // If url is platform=DARWIN_64BITS_GENERIC&version=6.0.12&branch=stable for example, the reply is:
394 // reply[0] = 6.0.14
395 // reply[1] = https://download.virtualbox.org/virtualbox/6.0.14/VirtualBox-6.0.14-133895-OSX.dmg
396 // If no update required, 'UPTODATE' is returned.
397 if (strcmp(lstHttpReply.at(0).c_str(), "UPTODATE") == 0)
398 {
399 m_updateNeeded = FALSE;
400 }
401 else
402 {
403 /** @todo r=bird: trusting the server reply too much here! */
404 m_updateNeeded = TRUE;
405 m_updateVersion = lstHttpReply.at(0).c_str();
406 m_updateURL = lstHttpReply.at(1).c_str();
407 LogRelFunc(("HTTP server reply = %s %s\n", lstHttpReply.at(0).c_str(), lstHttpReply.at(1).c_str()));
408 }
409
410 // clean-up HTTP request paperwork
411 if (hHttp != NIL_RTHTTP)
412 RTHttpDestroy(hHttp);
413
414 return S_OK;
415}
416
417HRESULT HostUpdate::i_updateCheckTask(UpdateCheckTask *pTask)
418{
419 LogFlowFuncEnter();
420 AutoCaller autoCaller(this);
421 HRESULT hrc = autoCaller.rc();
422 if (SUCCEEDED(hrc))
423 {
424 try
425 {
426 switch (pTask->m_checkType)
427 {
428 case UpdateCheckType_VirtualBox:
429 hrc = i_checkForVBoxUpdate();
430 break;
431#if 0
432 case UpdateCheckType_ExtensionPack:
433 hrc = i_checkForExtPackUpdate();
434 break;
435
436 case UpdateCheckType_GuestAdditions:
437 hrc = i_checkForGuestAdditionsUpdate();
438 break;
439#endif
440 default:
441 hrc = setError(E_FAIL, tr("Update check type %d is not implemented"), pTask->m_checkType);
442 break;
443 }
444 }
445 catch (...)
446 {
447 AssertFailed();
448 hrc = E_UNEXPECTED;
449 }
450 }
451
452 if (!pTask->m_ptrProgress.isNull())
453 pTask->m_ptrProgress->i_notifyComplete(hrc);
454
455 LogFlowFunc(("rc=%Rhrc\n", hrc));
456 LogFlowFuncLeave();
457 return hrc;
458}
459
460////////////////////////////////////////////////////////////////////////////////
461//
462// HostUpdate constructor / destructor
463//
464// ////////////////////////////////////////////////////////////////////////////////
465HostUpdate::HostUpdate()
466 : mVirtualBox(NULL)
467{
468}
469
470HostUpdate::~HostUpdate()
471{
472}
473
474
475HRESULT HostUpdate::FinalConstruct()
476{
477 return BaseFinalConstruct();
478}
479
480void HostUpdate::FinalRelease()
481{
482 uninit();
483
484 BaseFinalRelease();
485}
486
487HRESULT HostUpdate::init(VirtualBox *aVirtualBox)
488{
489 // Enclose the state transition NotReady->InInit->Ready.
490 AutoInitSpan autoInitSpan(this);
491 AssertReturn(autoInitSpan.isOk(), E_FAIL);
492
493 /* Weak reference to a VirtualBox object */
494 unconst(mVirtualBox) = aVirtualBox;
495
496 autoInitSpan.setSucceeded();
497 return S_OK;
498}
499
500void HostUpdate::uninit()
501{
502 // Enclose the state transition Ready->InUninit->NotReady.
503 AutoUninitSpan autoUninitSpan(this);
504 if (autoUninitSpan.uninitDone())
505 return;
506}
507
508HRESULT HostUpdate::updateCheck(UpdateCheckType_T aCheckType,
509 ComPtr<IProgress> &aProgress)
510{
511 /* Validate input */
512 switch (aCheckType)
513 {
514 case UpdateCheckType_VirtualBox:
515 break;
516 case UpdateCheckType_ExtensionPack:
517 return setError(E_NOTIMPL, tr("UpdateCheckType::ExtensionPack is not implemented"));
518 case UpdateCheckType_GuestAdditions:
519 return setError(E_NOTIMPL, tr("UpdateCheckType::GuestAdditions is not implemented"));
520 default:
521 return setError(E_INVALIDARG, tr("Invalid aCheckType value %d"), aCheckType);
522 }
523
524 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
525
526 // Check whether VirtualBox updates have been disabled before spawning the task thread.
527 ComPtr<ISystemProperties> pSystemProperties;
528 HRESULT rc = mVirtualBox->COMGETTER(SystemProperties)(pSystemProperties.asOutParam());
529 if (FAILED(rc))
530 return setErrorVrc(rc, tr("%s: IVirtualBox::systemProperties() failed: %Rrc"), __FUNCTION__, rc);
531
532 BOOL fVBoxUpdateEnabled = true;
533 rc = pSystemProperties->COMGETTER(VBoxUpdateEnabled)(&fVBoxUpdateEnabled);
534 if (FAILED(rc))
535 return setErrorVrc(rc, tr("%s: retrieving ISystemProperties::VBoxUpdateEnabled failed: %Rrc"), __FUNCTION__, rc);
536
537 /** @todo r=bird: Not sure if this makes sense, it should at least have a
538 * better status code and a proper error message. Also, isn't this really
539 * something the caller should check? Presumably the caller already check
540 * whther this was a good time to perform an update check (i.e. the configured
541 * time has elapsed since last check) ...
542 *
543 * It would make sense to allow performing a one-off update check even if the
544 * automatic update checking is disabled, wouldn't it? */
545 if (!fVBoxUpdateEnabled)
546 return E_NOTIMPL;
547
548 ComObjPtr<Progress> pProgress;
549 rc = pProgress.createObject();
550 if (FAILED(rc))
551 return rc;
552
553 rc = pProgress->init(mVirtualBox,
554 static_cast<IHostUpdate*>(this),
555 tr("Checking for software update..."),
556 TRUE /* aCancelable */);
557 if (FAILED(rc))
558 return rc;
559
560 /* initialize the worker task */
561 UpdateCheckTask *pTask = new UpdateCheckTask(aCheckType, this, pProgress);
562 rc = pTask->createThread();
563 pTask = NULL;
564 if (FAILED(rc))
565 return rc;
566
567 rc = pProgress.queryInterfaceTo(aProgress.asOutParam());
568
569 return rc;
570}
571
572HRESULT HostUpdate::getUpdateVersion(com::Utf8Str &aUpdateVersion)
573{
574 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
575
576 aUpdateVersion = m_updateVersion;
577
578 return S_OK;
579}
580
581HRESULT HostUpdate::getUpdateURL(com::Utf8Str &aUpdateURL)
582{
583 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
584
585 aUpdateURL = m_updateURL;
586
587 return S_OK;
588}
589
590HRESULT HostUpdate::getUpdateResponse(BOOL *aUpdateNeeded)
591{
592 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
593
594 *aUpdateNeeded = m_updateNeeded;
595
596 return S_OK;
597}
598
599HRESULT HostUpdate::getUpdateCheckNeeded(BOOL *aUpdateCheckNeeded)
600{
601 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
602
603 HRESULT rc;
604 ComPtr<ISystemProperties> pSystemProperties;
605 rc = mVirtualBox->COMGETTER(SystemProperties)(pSystemProperties.asOutParam());
606 if (FAILED(rc))
607 return rc;
608
609 /*
610 * Is update checking enabled?
611 */
612 BOOL fVBoxUpdateEnabled;
613 rc = pSystemProperties->COMGETTER(VBoxUpdateEnabled)(&fVBoxUpdateEnabled);
614 if (FAILED(rc))
615 return rc;
616
617 if (!fVBoxUpdateEnabled)
618 {
619 *aUpdateCheckNeeded = false;
620 return S_OK;
621 }
622
623 /*
624 * When was the last update?
625 */
626 Bstr strVBoxUpdateLastCheckDate;
627 rc = pSystemProperties->COMGETTER(VBoxUpdateLastCheckDate)(strVBoxUpdateLastCheckDate.asOutParam());
628 if (FAILED(rc))
629 return rc;
630
631 // No prior update check performed so do so now
632 if (strVBoxUpdateLastCheckDate.isEmpty())
633 {
634 *aUpdateCheckNeeded = true;
635 return S_OK;
636 }
637
638 // convert stored timestamp to time spec
639 RTTIMESPEC LastCheckTime;
640 if (!RTTimeSpecFromString(&LastCheckTime, Utf8Str(strVBoxUpdateLastCheckDate).c_str()))
641 {
642 *aUpdateCheckNeeded = true;
643 return S_OK;
644 }
645
646 /*
647 * Compare last update with how often we are supposed to check for updates.
648 */
649 ULONG uVBoxUpdateFrequency = 0; // value in days
650 rc = pSystemProperties->COMGETTER(VBoxUpdateFrequency)(&uVBoxUpdateFrequency);
651 if (FAILED(rc))
652 return rc;
653
654 if (!uVBoxUpdateFrequency)
655 {
656 /* Consider config (enable, 0 day interval) as checking once but never again.
657 We've already check since we've got a date. */
658 *aUpdateCheckNeeded = false;
659 return S_OK;
660 }
661 uint64_t const cSecsInXDays = uVBoxUpdateFrequency * RT_SEC_1DAY_64;
662
663 RTTIMESPEC TimeDiff;
664 RTTimeSpecSub(RTTimeNow(&TimeDiff), &LastCheckTime);
665
666 LogRelFunc(("Checking if seconds since last check (%lld) >= Number of seconds in %lu day%s (%lld)\n",
667 RTTimeSpecGetSeconds(&TimeDiff), uVBoxUpdateFrequency, uVBoxUpdateFrequency > 1 ? "s" : "", cSecsInXDays));
668
669 if (RTTimeSpecGetSeconds(&TimeDiff) >= (int64_t)cSecsInXDays)
670 *aUpdateCheckNeeded = true;
671
672 return S_OK;
673}
674
675/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Note: See TracBrowser for help on using the repository browser.

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