VirtualBox

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

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

Main/HostUpdateImpl.cpp: Another Bstr -> Utf8Str. ULONG != unsigned long - so do not use %lu/%ld for formatting them. bugref:7983

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 23.1 KB
Line 
1/* $Id: HostUpdateImpl.cpp 85734 2020-08-12 20:38:51Z 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.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#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 Utf8Str strUrl("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 strUrl.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 strUrl.appendPrintf("&version=%ls", versionNormalized.raw()); // e.g. 6.1.1
278 // strUrl.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 strUrl.appendPrintf("_%u", 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 strUrl.appendPrintf("&count=%u", 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 strUrl.appendPrintf("&branch=allrelease"); // query.php expects 'allrelease' and not 'allreleases'
326 break;
327 case VBoxUpdateTarget_WithBetas:
328 strUrl.appendPrintf("&branch=withbetas");
329 break;
330 case VBoxUpdateTarget_Stable:
331 default:
332 strUrl.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", strUrl.c_str()));
341
342 /*
343 * Setup the User-Agent headers for the GET request
344 */
345 Bstr version;
346 rc = mVirtualBox->COMGETTER(Version)(version.asOutParam()); // e.g. 6.1.0_RC1
347 if (FAILED(rc))
348 return setErrorVrc(rc, tr("%s: IVirtualBox::version() failed: %Rrc"), __FUNCTION__, rc);
349
350 Utf8StrFmt const strUserAgent("VirtualBox %ls <%s>", version.raw(), HostUpdate::i_platformInfo().c_str());
351 LogRelFunc(("userAgent = %s\n", strUserAgent.c_str()));
352
353 RTHTTP hHttp = NIL_RTHTTP;
354 int vrc = RTHttpCreate(&hHttp);
355 if (RT_FAILURE(vrc))
356 return setErrorVrc(vrc, tr("%s: RTHttpCreate() failed: %Rrc"), __FUNCTION__, vrc);
357
358 /// @todo Are there any other headers needed to be added first via RTHttpSetHeaders()?
359 vrc = RTHttpAddHeader(hHttp, "User-Agent", strUserAgent.c_str(), strUserAgent.length(), RTHTTPADDHDR_F_BACK);
360 if (RT_FAILURE(vrc))
361 return setErrorVrc(vrc, tr("%s: RTHttpAddHeader() failed: %Rrc (on User-Agent)"), __FUNCTION__, vrc);
362
363 /*
364 * Configure proxying.
365 */
366 ProxyMode_T enmProxyMode;
367 rc = pSystemProperties->COMGETTER(ProxyMode)(&enmProxyMode);
368 if (FAILED(rc))
369 return setErrorVrc(rc, tr("%s: ISystemProperties::proxyMode() failed: %Rrc"), __FUNCTION__, rc);
370
371 if (enmProxyMode == ProxyMode_Manual)
372 {
373 Bstr strProxyURL;
374 rc = pSystemProperties->COMGETTER(ProxyURL)(strProxyURL.asOutParam());
375 if (FAILED(rc))
376 return setErrorVrc(rc, tr("%s: ISystemProperties::proxyURL() failed: %Rrc"), __FUNCTION__, rc);
377 vrc = RTHttpSetProxyByUrl(hHttp, Utf8Str(strProxyURL).c_str());
378 if (RT_FAILURE(vrc))
379 return setErrorVrc(vrc, tr("%s: RTHttpSetProxyByUrl() failed: %Rrc"), __FUNCTION__, vrc);
380 }
381 else if (enmProxyMode == ProxyMode_System)
382 {
383 vrc = RTHttpUseSystemProxySettings(hHttp);
384 if (RT_FAILURE(vrc))
385 return setErrorVrc(vrc, tr("%s: RTHttpUseSystemProxySettings() failed: %Rrc"), __FUNCTION__, vrc);
386 }
387
388 /*
389 * Perform the GET request, returning raw binary stuff.
390 */
391 void *pvResponse = NULL;
392 size_t cbResponse = 0;
393 vrc = RTHttpGetBinary(hHttp, strUrl.c_str(), &pvResponse, &cbResponse);
394 if (RT_FAILURE(vrc))
395 return setErrorVrc(vrc, tr("%s: RTHttpGetBinary() failed: %Rrc"), __FUNCTION__, vrc);
396
397 RTCList<RTCString> lstHttpReply = RTCString((char *)pvResponse, (size_t)cbResponse).split(" ", RTCString::RemoveEmptyParts);
398 RTHttpFreeResponse(pvResponse);
399
400 // If url is platform=DARWIN_64BITS_GENERIC&version=6.0.12&branch=stable for example, the reply is:
401 // reply[0] = 6.0.14
402 // reply[1] = https://download.virtualbox.org/virtualbox/6.0.14/VirtualBox-6.0.14-133895-OSX.dmg
403 // If no update required, 'UPTODATE' is returned.
404 if (strcmp(lstHttpReply.at(0).c_str(), "UPTODATE") == 0)
405 {
406 m_updateNeeded = FALSE;
407 }
408 else
409 {
410 /** @todo r=bird: trusting the server reply too much here! */
411 m_updateNeeded = TRUE;
412 m_updateVersion = lstHttpReply.at(0).c_str();
413 m_updateURL = lstHttpReply.at(1).c_str();
414 LogRelFunc(("HTTP server reply = %s %s\n", lstHttpReply.at(0).c_str(), lstHttpReply.at(1).c_str()));
415 }
416
417 // clean-up HTTP request paperwork
418 /** @todo r=bird: There is no chance that this would be NIL here unless
419 * you've got stack corruption. Besides, RTHttpDestruct ignores NIL. */
420 if (hHttp != NIL_RTHTTP)
421 RTHttpDestroy(hHttp);
422
423 return S_OK;
424}
425
426HRESULT HostUpdate::i_updateCheckTask(UpdateCheckTask *pTask)
427{
428 LogFlowFuncEnter();
429 AutoCaller autoCaller(this);
430 HRESULT hrc = autoCaller.rc();
431 if (SUCCEEDED(hrc))
432 {
433 try
434 {
435 switch (pTask->m_checkType)
436 {
437 case UpdateCheckType_VirtualBox:
438 hrc = i_checkForVBoxUpdate();
439 break;
440#if 0
441 case UpdateCheckType_ExtensionPack:
442 hrc = i_checkForExtPackUpdate();
443 break;
444
445 case UpdateCheckType_GuestAdditions:
446 hrc = i_checkForGuestAdditionsUpdate();
447 break;
448#endif
449 default:
450 hrc = setError(E_FAIL, tr("Update check type %d is not implemented"), pTask->m_checkType);
451 break;
452 }
453 }
454 catch (...)
455 {
456 AssertFailed();
457 hrc = E_UNEXPECTED;
458 }
459 }
460
461 if (!pTask->m_ptrProgress.isNull())
462 pTask->m_ptrProgress->i_notifyComplete(hrc);
463
464 LogFlowFunc(("rc=%Rhrc\n", hrc));
465 LogFlowFuncLeave();
466 return hrc;
467}
468
469////////////////////////////////////////////////////////////////////////////////
470//
471// HostUpdate constructor / destructor
472//
473// ////////////////////////////////////////////////////////////////////////////////
474HostUpdate::HostUpdate()
475 : mVirtualBox(NULL)
476{
477}
478
479HostUpdate::~HostUpdate()
480{
481}
482
483
484HRESULT HostUpdate::FinalConstruct()
485{
486 return BaseFinalConstruct();
487}
488
489void HostUpdate::FinalRelease()
490{
491 uninit();
492
493 BaseFinalRelease();
494}
495
496HRESULT HostUpdate::init(VirtualBox *aVirtualBox)
497{
498 // Enclose the state transition NotReady->InInit->Ready.
499 AutoInitSpan autoInitSpan(this);
500 AssertReturn(autoInitSpan.isOk(), E_FAIL);
501
502 /* Weak reference to a VirtualBox object */
503 unconst(mVirtualBox) = aVirtualBox;
504
505 autoInitSpan.setSucceeded();
506 return S_OK;
507}
508
509void HostUpdate::uninit()
510{
511 // Enclose the state transition Ready->InUninit->NotReady.
512 AutoUninitSpan autoUninitSpan(this);
513 if (autoUninitSpan.uninitDone())
514 return;
515}
516
517HRESULT HostUpdate::updateCheck(UpdateCheckType_T aCheckType,
518 ComPtr<IProgress> &aProgress)
519{
520 /* Validate input */
521 switch (aCheckType)
522 {
523 case UpdateCheckType_VirtualBox:
524 break;
525 case UpdateCheckType_ExtensionPack:
526 return setError(E_NOTIMPL, tr("UpdateCheckType::ExtensionPack is not implemented"));
527 case UpdateCheckType_GuestAdditions:
528 return setError(E_NOTIMPL, tr("UpdateCheckType::GuestAdditions is not implemented"));
529 default:
530 return setError(E_INVALIDARG, tr("Invalid aCheckType value %d"), aCheckType);
531 }
532
533 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
534
535 // Check whether VirtualBox updates have been disabled before spawning the task thread.
536 ComPtr<ISystemProperties> pSystemProperties;
537 HRESULT rc = mVirtualBox->COMGETTER(SystemProperties)(pSystemProperties.asOutParam());
538 if (FAILED(rc))
539 return setErrorVrc(rc, tr("%s: IVirtualBox::systemProperties() failed: %Rrc"), __FUNCTION__, rc);
540
541 BOOL fVBoxUpdateEnabled = true;
542 rc = pSystemProperties->COMGETTER(VBoxUpdateEnabled)(&fVBoxUpdateEnabled);
543 if (FAILED(rc))
544 return setErrorVrc(rc, tr("%s: retrieving ISystemProperties::VBoxUpdateEnabled failed: %Rrc"), __FUNCTION__, rc);
545
546 /** @todo r=bird: Not sure if this makes sense, it should at least have a
547 * better status code and a proper error message. Also, isn't this really
548 * something the caller should check? Presumably the caller already check
549 * whther this was a good time to perform an update check (i.e. the configured
550 * time has elapsed since last check) ...
551 *
552 * It would make sense to allow performing a one-off update check even if the
553 * automatic update checking is disabled, wouldn't it? */
554 if (!fVBoxUpdateEnabled)
555 return E_NOTIMPL;
556
557 ComObjPtr<Progress> pProgress;
558 rc = pProgress.createObject();
559 if (FAILED(rc))
560 return rc;
561
562 rc = pProgress->init(mVirtualBox,
563 static_cast<IHostUpdate*>(this),
564 tr("Checking for software update..."),
565 TRUE /* aCancelable */);
566 if (FAILED(rc))
567 return rc;
568
569 /* initialize the worker task */
570 UpdateCheckTask *pTask = new UpdateCheckTask(aCheckType, this, pProgress);
571 rc = pTask->createThread();
572 pTask = NULL;
573 if (FAILED(rc))
574 return rc;
575
576 rc = pProgress.queryInterfaceTo(aProgress.asOutParam());
577
578 return rc;
579}
580
581HRESULT HostUpdate::getUpdateVersion(com::Utf8Str &aUpdateVersion)
582{
583 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
584
585 aUpdateVersion = m_updateVersion;
586
587 return S_OK;
588}
589
590HRESULT HostUpdate::getUpdateURL(com::Utf8Str &aUpdateURL)
591{
592 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
593
594 aUpdateURL = m_updateURL;
595
596 return S_OK;
597}
598
599HRESULT HostUpdate::getUpdateResponse(BOOL *aUpdateNeeded)
600{
601 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
602
603 *aUpdateNeeded = m_updateNeeded;
604
605 return S_OK;
606}
607
608HRESULT HostUpdate::getUpdateCheckNeeded(BOOL *aUpdateCheckNeeded)
609{
610 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
611
612 HRESULT rc;
613 ComPtr<ISystemProperties> pSystemProperties;
614 rc = mVirtualBox->COMGETTER(SystemProperties)(pSystemProperties.asOutParam());
615 if (FAILED(rc))
616 return rc;
617
618 /*
619 * Is update checking enabled?
620 */
621 BOOL fVBoxUpdateEnabled;
622 rc = pSystemProperties->COMGETTER(VBoxUpdateEnabled)(&fVBoxUpdateEnabled);
623 if (FAILED(rc))
624 return rc;
625
626 if (!fVBoxUpdateEnabled)
627 {
628 *aUpdateCheckNeeded = false;
629 return S_OK;
630 }
631
632 /*
633 * When was the last update?
634 */
635 Bstr strVBoxUpdateLastCheckDate;
636 rc = pSystemProperties->COMGETTER(VBoxUpdateLastCheckDate)(strVBoxUpdateLastCheckDate.asOutParam());
637 if (FAILED(rc))
638 return rc;
639
640 // No prior update check performed so do so now
641 if (strVBoxUpdateLastCheckDate.isEmpty())
642 {
643 *aUpdateCheckNeeded = true;
644 return S_OK;
645 }
646
647 // convert stored timestamp to time spec
648 RTTIMESPEC LastCheckTime;
649 if (!RTTimeSpecFromString(&LastCheckTime, Utf8Str(strVBoxUpdateLastCheckDate).c_str()))
650 {
651 *aUpdateCheckNeeded = true;
652 return S_OK;
653 }
654
655 /*
656 * Compare last update with how often we are supposed to check for updates.
657 */
658 ULONG uVBoxUpdateFrequency = 0; // value in days
659 rc = pSystemProperties->COMGETTER(VBoxUpdateFrequency)(&uVBoxUpdateFrequency);
660 if (FAILED(rc))
661 return rc;
662
663 if (!uVBoxUpdateFrequency)
664 {
665 /* Consider config (enable, 0 day interval) as checking once but never again.
666 We've already check since we've got a date. */
667 *aUpdateCheckNeeded = false;
668 return S_OK;
669 }
670 uint64_t const cSecsInXDays = uVBoxUpdateFrequency * RT_SEC_1DAY_64;
671
672 RTTIMESPEC TimeDiff;
673 RTTimeSpecSub(RTTimeNow(&TimeDiff), &LastCheckTime);
674
675 LogRelFunc(("Checking if seconds since last check (%lld) >= Number of seconds in %lu day%s (%lld)\n",
676 RTTimeSpecGetSeconds(&TimeDiff), uVBoxUpdateFrequency, uVBoxUpdateFrequency > 1 ? "s" : "", cSecsInXDays));
677
678 if (RTTimeSpecGetSeconds(&TimeDiff) >= (int64_t)cSecsInXDays)
679 *aUpdateCheckNeeded = true;
680
681 return S_OK;
682}
683
684/* 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