VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/UpdateAgentImpl.cpp@ 94713

Last change on this file since 94713 was 94713, checked in by vboxsync, 3 years ago

Main/Update check: Fixes for event handling. bugref:7983

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 32.4 KB
Line 
1/* $Id: UpdateAgentImpl.cpp 94713 2022-04-27 07:25:52Z vboxsync $ */
2/** @file
3 * IUpdateAgent COM class implementations.
4 */
5
6/*
7 * Copyright (C) 2020-2022 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_UPDATEAGENT
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/err.h>
36#include <VBox/version.h>
37
38#include "HostImpl.h"
39#include "UpdateAgentImpl.h"
40#include "ProgressImpl.h"
41#include "AutoCaller.h"
42#include "LoggingNew.h"
43#include "VirtualBoxImpl.h"
44#include "VBoxEvents.h"
45#include "ThreadTask.h"
46#include "VirtualBoxBase.h"
47
48
49/*********************************************************************************************************************************
50* Update agent task implementation *
51*********************************************************************************************************************************/
52
53/**
54 * Base task class for asynchronous update agent tasks.
55 */
56class UpdateAgentTask : public ThreadTask
57{
58public:
59 UpdateAgentTask(UpdateAgentBase *aThat, Progress *aProgress)
60 : m_pParent(aThat)
61 , m_pProgress(aProgress)
62 {
63 m_strTaskName = "UpdateAgentTask";
64 }
65 virtual ~UpdateAgentTask(void) { }
66
67private:
68 void handler(void);
69
70 /** Weak pointer to parent (update agent). */
71 UpdateAgentBase *m_pParent;
72 /** Smart pointer to the progress object for this job. */
73 ComObjPtr<Progress> m_pProgress;
74
75 friend class UpdateAgent; // allow member functions access to private data
76};
77
78void UpdateAgentTask::handler(void)
79{
80 UpdateAgentBase *pUpdateAgent = this->m_pParent;
81 AssertPtr(pUpdateAgent);
82
83 /** @todo Differentiate tasks once we have more stuff to do (downloading, installing, ++). */
84
85 HRESULT rc = pUpdateAgent->i_checkForUpdateTask(this);
86
87 if (!m_pProgress.isNull())
88 m_pProgress->i_notifyComplete(rc);
89
90 LogFlowFunc(("rc=%Rhrc\n", rc)); RT_NOREF(rc);
91}
92
93
94/*********************************************************************************************************************************
95* Update agent base class implementation *
96*********************************************************************************************************************************/
97
98/**
99 * Returns platform information as a string.
100 *
101 * @returns Platform information as string.
102 */
103/* static */
104Utf8Str UpdateAgentBase::i_getPlatformInfo(void)
105{
106 /* Prepare platform report: */
107 Utf8Str strPlatform;
108
109# if defined (RT_OS_WINDOWS)
110 strPlatform = "win";
111# elif defined (RT_OS_LINUX)
112 strPlatform = "linux";
113# elif defined (RT_OS_DARWIN)
114 strPlatform = "macosx";
115# elif defined (RT_OS_OS2)
116 strPlatform = "os2";
117# elif defined (RT_OS_FREEBSD)
118 strPlatform = "freebsd";
119# elif defined (RT_OS_SOLARIS)
120 strPlatform = "solaris";
121# else
122 strPlatform = "unknown";
123# endif
124
125 /* The format is <system>.<bitness>: */
126 strPlatform.appendPrintf(".%lu", ARCH_BITS);
127
128 /* Add more system information: */
129 int vrc;
130# ifdef RT_OS_LINUX
131 // WORKAROUND:
132 // On Linux we try to generate information using script first of all..
133
134 /* Get script path: */
135 char szAppPrivPath[RTPATH_MAX];
136 vrc = RTPathAppPrivateNoArch(szAppPrivPath, sizeof(szAppPrivPath));
137 AssertRC(vrc);
138 if (RT_SUCCESS(vrc))
139 vrc = RTPathAppend(szAppPrivPath, sizeof(szAppPrivPath), "/VBoxSysInfo.sh");
140 AssertRC(vrc);
141 if (RT_SUCCESS(vrc))
142 {
143 RTPIPE hPipeR;
144 RTHANDLE hStdOutPipe;
145 hStdOutPipe.enmType = RTHANDLETYPE_PIPE;
146 vrc = RTPipeCreate(&hPipeR, &hStdOutPipe.u.hPipe, RTPIPE_C_INHERIT_WRITE);
147 AssertLogRelRC(vrc);
148
149 char const *szAppPrivArgs[2];
150 szAppPrivArgs[0] = szAppPrivPath;
151 szAppPrivArgs[1] = NULL;
152 RTPROCESS hProc = NIL_RTPROCESS;
153
154 /* Run script: */
155 vrc = RTProcCreateEx(szAppPrivPath, szAppPrivArgs, RTENV_DEFAULT, 0 /*fFlags*/, NULL /*phStdin*/, &hStdOutPipe,
156 NULL /*phStderr*/, NULL /*pszAsUser*/, NULL /*pszPassword*/, NULL /*pvExtraData*/, &hProc);
157
158 (void) RTPipeClose(hStdOutPipe.u.hPipe);
159 hStdOutPipe.u.hPipe = NIL_RTPIPE;
160
161 if (RT_SUCCESS(vrc))
162 {
163 RTPROCSTATUS ProcStatus;
164 size_t cbStdOutBuf = 0;
165 size_t offStdOutBuf = 0;
166 char *pszStdOutBuf = NULL;
167 do
168 {
169 if (hPipeR != NIL_RTPIPE)
170 {
171 char achBuf[1024];
172 size_t cbRead;
173 vrc = RTPipeReadBlocking(hPipeR, achBuf, sizeof(achBuf), &cbRead);
174 if (RT_SUCCESS(vrc))
175 {
176 /* grow the buffer? */
177 size_t cbBufReq = offStdOutBuf + cbRead + 1;
178 if ( cbBufReq > cbStdOutBuf
179 && cbBufReq < _256K)
180 {
181 size_t cbNew = RT_ALIGN_Z(cbBufReq, 16); // 1024
182 void *pvNew = RTMemRealloc(pszStdOutBuf, cbNew);
183 if (pvNew)
184 {
185 pszStdOutBuf = (char *)pvNew;
186 cbStdOutBuf = cbNew;
187 }
188 }
189
190 /* append if we've got room. */
191 if (cbBufReq <= cbStdOutBuf)
192 {
193 (void) memcpy(&pszStdOutBuf[offStdOutBuf], achBuf, cbRead);
194 offStdOutBuf = offStdOutBuf + cbRead;
195 pszStdOutBuf[offStdOutBuf] = '\0';
196 }
197 }
198 else
199 {
200 AssertLogRelMsg(vrc == VERR_BROKEN_PIPE, ("%Rrc\n", vrc));
201 RTPipeClose(hPipeR);
202 hPipeR = NIL_RTPIPE;
203 }
204 }
205
206 /*
207 * Service the process. Block if we have no pipe.
208 */
209 if (hProc != NIL_RTPROCESS)
210 {
211 vrc = RTProcWait(hProc,
212 hPipeR == NIL_RTPIPE ? RTPROCWAIT_FLAGS_BLOCK : RTPROCWAIT_FLAGS_NOBLOCK,
213 &ProcStatus);
214 if (RT_SUCCESS(vrc))
215 hProc = NIL_RTPROCESS;
216 else
217 AssertLogRelMsgStmt(vrc == VERR_PROCESS_RUNNING, ("%Rrc\n", vrc), hProc = NIL_RTPROCESS);
218 }
219 } while ( hPipeR != NIL_RTPIPE
220 || hProc != NIL_RTPROCESS);
221
222 if ( ProcStatus.enmReason == RTPROCEXITREASON_NORMAL
223 && ProcStatus.iStatus == 0) {
224 pszStdOutBuf[offStdOutBuf-1] = '\0'; // remove trailing newline
225 Utf8Str pszStdOutBufUTF8(pszStdOutBuf);
226 strPlatform.appendPrintf(" [%s]", pszStdOutBufUTF8.strip().c_str());
227 // For testing, here is some sample output:
228 //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]");
229 }
230 }
231 else
232 vrc = VERR_TRY_AGAIN; /* (take the fallback path) */
233 }
234
235 if (RT_FAILURE(vrc))
236# endif /* RT_OS_LINUX */
237 {
238 /* Use RTSystemQueryOSInfo: */
239 char szTmp[256];
240
241 vrc = RTSystemQueryOSInfo(RTSYSOSINFO_PRODUCT, szTmp, sizeof(szTmp));
242 if ((RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW) && szTmp[0] != '\0')
243 strPlatform.appendPrintf(" [Product: %s", szTmp);
244
245 vrc = RTSystemQueryOSInfo(RTSYSOSINFO_RELEASE, szTmp, sizeof(szTmp));
246 if ((RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW) && szTmp[0] != '\0')
247 strPlatform.appendPrintf(" %sRelease: %s", strlen(szTmp) == 0 ? "[" : "| ", szTmp);
248
249 vrc = RTSystemQueryOSInfo(RTSYSOSINFO_VERSION, szTmp, sizeof(szTmp));
250 if ((RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW) && szTmp[0] != '\0')
251 strPlatform.appendPrintf(" %sVersion: %s", strlen(szTmp) == 0 ? "[" : "| ", szTmp);
252
253 vrc = RTSystemQueryOSInfo(RTSYSOSINFO_SERVICE_PACK, szTmp, sizeof(szTmp));
254 if ((RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW) && szTmp[0] != '\0')
255 strPlatform.appendPrintf(" %sSP: %s]", strlen(szTmp) == 0 ? "[" : "| ", szTmp);
256
257 if (!strPlatform.endsWith("]"))
258 strPlatform.append("]");
259 }
260
261 LogRel2(("UpdateAgent: Platform is '%s'\n", strPlatform.c_str()));
262
263 return strPlatform;
264}
265
266
267/*********************************************************************************************************************************
268* Update agent class implementation *
269*********************************************************************************************************************************/
270UpdateAgent::UpdateAgent()
271{
272}
273
274UpdateAgent::~UpdateAgent()
275{
276}
277
278HRESULT UpdateAgent::FinalConstruct(void)
279{
280 return BaseFinalConstruct();
281}
282
283void UpdateAgent::FinalRelease(void)
284{
285 uninit();
286
287 BaseFinalRelease();
288}
289
290HRESULT UpdateAgent::init(VirtualBox *aVirtualBox)
291{
292 /* Weak reference to a VirtualBox object */
293 unconst(m_VirtualBox) = aVirtualBox;
294
295 HRESULT hr = unconst(m_EventSource).createObject();
296 if (SUCCEEDED(hr))
297 hr = m_EventSource->init();
298
299 return hr;
300}
301
302void UpdateAgent::uninit(void)
303{
304 // Enclose the state transition Ready->InUninit->NotReady.
305 AutoUninitSpan autoUninitSpan(this);
306 if (autoUninitSpan.uninitDone())
307 return;
308
309 unconst(m_EventSource).setNull();
310}
311
312HRESULT UpdateAgent::checkFor(ComPtr<IProgress> &aProgress)
313{
314 RT_NOREF(aProgress);
315
316 return VBOX_E_NOT_SUPPORTED;
317}
318
319HRESULT UpdateAgent::download(ComPtr<IProgress> &aProgress)
320{
321 RT_NOREF(aProgress);
322
323 return VBOX_E_NOT_SUPPORTED;
324}
325
326HRESULT UpdateAgent::install(ComPtr<IProgress> &aProgress)
327{
328 RT_NOREF(aProgress);
329
330 return VBOX_E_NOT_SUPPORTED;
331}
332
333HRESULT UpdateAgent::rollback(void)
334{
335 return VBOX_E_NOT_SUPPORTED;
336}
337
338HRESULT UpdateAgent::getName(com::Utf8Str &aName)
339{
340 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
341
342 aName = mData.m_strName;
343
344 return S_OK;
345}
346
347HRESULT UpdateAgent::getEventSource(ComPtr<IEventSource> &aEventSource)
348{
349 LogFlowThisFuncEnter();
350
351 /* No need to lock - lifetime constant. */
352 m_EventSource.queryInterfaceTo(aEventSource.asOutParam());
353
354 LogFlowFuncLeaveRC(S_OK);
355 return S_OK;
356}
357
358HRESULT UpdateAgent::getOrder(ULONG *aOrder)
359{
360 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
361
362 *aOrder = 0; /* 0 means no order / disabled. */
363
364 return S_OK;
365}
366
367HRESULT UpdateAgent::getDependsOn(std::vector<com::Utf8Str> &aDeps)
368{
369 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
370
371 aDeps.resize(0); /* No dependencies by default. */
372
373 return S_OK;
374}
375
376HRESULT UpdateAgent::getVersion(com::Utf8Str &aVer)
377{
378 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
379
380 aVer = mData.m_lastResult.strVer;
381
382 return S_OK;
383}
384
385HRESULT UpdateAgent::getDownloadUrl(com::Utf8Str &aUrl)
386{
387 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
388
389 aUrl = mData.m_lastResult.strDownloadUrl;
390
391 return S_OK;
392}
393
394
395HRESULT UpdateAgent::getWebUrl(com::Utf8Str &aUrl)
396{
397 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
398
399 aUrl = mData.m_lastResult.strWebUrl;
400
401 return S_OK;
402}
403
404HRESULT UpdateAgent::getReleaseNotes(com::Utf8Str &aRelNotes)
405{
406 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
407
408 aRelNotes = mData.m_lastResult.strReleaseNotes;
409
410 return S_OK;
411}
412
413HRESULT UpdateAgent::getEnabled(BOOL *aEnabled)
414{
415 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
416
417 *aEnabled = m->fEnabled;
418
419 return S_OK;
420}
421
422HRESULT UpdateAgent::setEnabled(const BOOL aEnabled)
423{
424 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
425
426 m->fEnabled = aEnabled;
427
428 return i_commitSettings(alock);
429}
430
431
432HRESULT UpdateAgent::getHidden(BOOL *aHidden)
433{
434 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
435
436 *aHidden = mData.m_fHidden;
437
438 return S_OK;
439}
440
441HRESULT UpdateAgent::getState(UpdateState_T *aState)
442{
443 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
444
445 *aState = mData.m_enmState;
446
447 return S_OK;
448}
449
450HRESULT UpdateAgent::getCheckFrequency(ULONG *aFreqSeconds)
451{
452 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
453
454 *aFreqSeconds = m->uCheckFreqSeconds;
455
456 return S_OK;
457}
458
459HRESULT UpdateAgent::setCheckFrequency(ULONG aFreqSeconds)
460{
461 if (aFreqSeconds < RT_SEC_1DAY) /* Don't allow more frequent checks for now. */
462 return setError(E_INVALIDARG, tr("Frequency too small; one day is the minimum"));
463
464 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
465
466 m->uCheckFreqSeconds = aFreqSeconds;
467
468 return i_commitSettings(alock);
469}
470
471HRESULT UpdateAgent::getChannel(UpdateChannel_T *aChannel)
472{
473 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
474
475 *aChannel = m->enmChannel;
476
477 return S_OK;
478}
479
480HRESULT UpdateAgent::setChannel(UpdateChannel_T aChannel)
481{
482 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
483
484 m->enmChannel = aChannel;
485
486 return i_commitSettings(alock);
487}
488
489HRESULT UpdateAgent::getCheckCount(ULONG *aCount)
490{
491 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
492
493 *aCount = m->uCheckCount;
494
495 return S_OK;
496}
497
498HRESULT UpdateAgent::getRepositoryURL(com::Utf8Str &aRepo)
499{
500 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
501
502 aRepo = m->strRepoUrl;
503
504 return S_OK;
505}
506
507HRESULT UpdateAgent::setRepositoryURL(const com::Utf8Str &aRepo)
508{
509 if (!aRepo.startsWith("https://", com::Utf8Str::CaseInsensitive))
510 return setError(E_INVALIDARG, tr("Invalid URL scheme specified; only https:// is supported."));
511
512 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
513
514 m->strRepoUrl = aRepo;
515
516 return i_commitSettings(alock);
517}
518
519HRESULT UpdateAgent::getProxyMode(ProxyMode_T *aMode)
520{
521 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
522
523 *aMode = m->enmProxyMode;
524
525 return S_OK;
526}
527
528HRESULT UpdateAgent::setProxyMode(ProxyMode_T aMode)
529{
530 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
531
532 m->enmProxyMode = aMode;
533
534 return i_commitSettings(alock);
535}
536
537HRESULT UpdateAgent::getProxyURL(com::Utf8Str &aAddress)
538{
539 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
540
541 aAddress = m->strProxyUrl;
542
543 return S_OK;
544}
545
546HRESULT UpdateAgent::setProxyURL(const com::Utf8Str &aAddress)
547{
548 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
549
550 m->strProxyUrl = aAddress;
551
552 return i_commitSettings(alock);
553}
554
555HRESULT UpdateAgent::getLastCheckDate(com::Utf8Str &aDate)
556{
557 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
558
559 aDate = m->strLastCheckDate;
560
561 return S_OK;
562}
563
564HRESULT UpdateAgent::getIsCheckNeeded(BOOL *aCheckNeeded)
565{
566 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
567
568 /*
569 * Is update checking enabled at all?
570 */
571 if (!m->fEnabled)
572 {
573 *aCheckNeeded = FALSE;
574 return S_OK;
575 }
576
577 /*
578 * When was the last update?
579 */
580 if (m->strLastCheckDate.isEmpty()) /* No prior update check performed -- do so now. */
581 {
582 *aCheckNeeded = TRUE;
583 return S_OK;
584 }
585
586 RTTIMESPEC LastCheckTime;
587 if (!RTTimeSpecFromString(&LastCheckTime, Utf8Str(m->strLastCheckDate).c_str()))
588 {
589 *aCheckNeeded = TRUE; /* Invalid date set or error? Perform check. */
590 return S_OK;
591 }
592
593 /*
594 * Compare last update with how often we are supposed to check for updates.
595 */
596 if ( !m->uCheckFreqSeconds /* Paranoia */
597 || m->uCheckFreqSeconds < RT_SEC_1DAY) /* This is the minimum we currently allow. */
598 {
599 /* Consider config (enable, 0 day interval) as checking once but never again.
600 We've already check since we've got a date. */
601 *aCheckNeeded = FALSE;
602 return S_OK;
603 }
604
605 uint64_t const cCheckFreqDays = m->uCheckFreqSeconds / RT_SEC_1DAY_64;
606
607 RTTIMESPEC TimeDiff;
608 RTTimeSpecSub(RTTimeNow(&TimeDiff), &LastCheckTime);
609
610 int64_t const diffLastCheckSecs = RTTimeSpecGetSeconds(&TimeDiff);
611 int64_t const diffLastCheckDays = diffLastCheckSecs / RT_SEC_1DAY_64;
612
613 /* Be as accurate as possible. */
614 *aCheckNeeded = diffLastCheckSecs >= (int64_t)m->uCheckFreqSeconds ? TRUE : FALSE;
615
616 LogRel2(("Update agent (%s): Last update %RU64 days (%RU64 seconds) ago, check frequency is every %RU64 days (%RU64 seconds) -> Check %s\n",
617 mData.m_strName.c_str(), diffLastCheckDays, diffLastCheckSecs, cCheckFreqDays, m->uCheckFreqSeconds,
618 *aCheckNeeded ? "needed" : "not needed"));
619
620 return S_OK;
621}
622
623
624/*********************************************************************************************************************************
625* Internal helper methods of update agent class *
626*********************************************************************************************************************************/
627
628/**
629 * Loads the settings of the update agent base class.
630 *
631 * @returns HRESULT
632 * @param data Where to load the settings from.
633 */
634HRESULT UpdateAgent::i_loadSettings(const settings::UpdateAgent &data)
635{
636 AutoCaller autoCaller(this);
637 if (FAILED(autoCaller.rc())) return autoCaller.rc();
638
639 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
640
641 m->fEnabled = data.fEnabled;
642 m->enmChannel = data.enmChannel;
643 m->uCheckFreqSeconds = data.uCheckFreqSeconds;
644 m->strRepoUrl = data.strRepoUrl;
645 m->enmProxyMode = data.enmProxyMode;
646 m->strProxyUrl = data.strProxyUrl;
647 m->strLastCheckDate = data.strLastCheckDate;
648 m->uCheckCount = data.uCheckCount;
649
650 return S_OK;
651}
652
653/**
654 * Saves the settings of the update agent base class.
655 *
656 * @returns HRESULT
657 * @param data Where to save the settings to.
658 */
659HRESULT UpdateAgent::i_saveSettings(settings::UpdateAgent &data)
660{
661 AutoCaller autoCaller(this);
662 if (FAILED(autoCaller.rc())) return autoCaller.rc();
663
664 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
665
666 data = *m;
667
668 return S_OK;
669}
670
671/**
672 * Sets the update check count.
673 *
674 * @returns HRESULT
675 * @param aCount Update check count to set.
676 */
677HRESULT UpdateAgent::i_setCheckCount(ULONG aCount)
678{
679 AutoCaller autoCaller(this);
680 if (FAILED(autoCaller.rc())) return autoCaller.rc();
681
682 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
683
684 m->uCheckCount = aCount;
685
686 return i_commitSettings(alock);
687}
688
689/**
690 * Sets the last update check date.
691 *
692 * @returns HRESULT
693 * @param aDate Last update check date to set.
694 * Must be in ISO 8601 format (e.g. 2020-05-11T21:13:39.348416000Z).
695 */
696HRESULT UpdateAgent::i_setLastCheckDate(const com::Utf8Str &aDate)
697{
698 AutoCaller autoCaller(this);
699 if (FAILED(autoCaller.rc())) return autoCaller.rc();
700
701 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
702
703 m->strLastCheckDate = aDate;
704
705 return i_commitSettings(alock);
706}
707
708/**
709 * Internal helper function to commit modified settings.
710 *
711 * @returns HRESULT
712 * @param aLock Write lock to release before committing settings.
713 */
714HRESULT UpdateAgent::i_commitSettings(AutoWriteLock &aLock)
715{
716 aLock.release();
717
718 ::FireUpdateAgentSettingsChangedEvent(m_EventSource, "" /** @todo Include attribute hints */);
719
720 AutoWriteLock vboxLock(m_VirtualBox COMMA_LOCKVAL_SRC_POS);
721 return m_VirtualBox->i_saveSettings();
722}
723
724/**
725 * Reports an error by setting the error info and also information subscribed listeners.
726 *
727 * @returns HRESULT
728 * @param vrc Result code (IPRT-style) to report.
729 * @param pcszMsgFmt Error message to report.
730 * @param ... Format string for \a pcszMsgFmt.
731 */
732HRESULT UpdateAgent::i_reportError(int vrc, const char *pcszMsgFmt, ...)
733{
734 va_list va;
735 va_start(va, pcszMsgFmt);
736
737 char *psz = NULL;
738 if (RTStrAPrintfV(&psz, pcszMsgFmt, va) <= 0)
739 return E_OUTOFMEMORY;
740
741 LogRel(("Update agent (%s): %s\n", mData.m_strName.c_str(), psz));
742
743 ::FireUpdateAgentErrorEvent(m_EventSource, psz, vrc);
744
745 HRESULT const rc = setErrorVrc(VERR_COM_IPRT_ERROR /** @todo Translate HTTP errors to COM? */, psz);
746
747 va_end(va);
748 RTStrFree(psz);
749
750 return rc;
751}
752
753
754/*********************************************************************************************************************************
755* Host update implementation *
756*********************************************************************************************************************************/
757
758HostUpdateAgent::HostUpdateAgent(void)
759{
760}
761
762HostUpdateAgent::~HostUpdateAgent(void)
763{
764}
765
766
767HRESULT HostUpdateAgent::FinalConstruct(void)
768{
769 return BaseFinalConstruct();
770}
771
772void HostUpdateAgent::FinalRelease(void)
773{
774 uninit();
775
776 BaseFinalRelease();
777}
778
779HRESULT HostUpdateAgent::init(VirtualBox *aVirtualBox)
780{
781 // Enclose the state transition NotReady->InInit->Ready.
782 AutoInitSpan autoInitSpan(this);
783 AssertReturn(autoInitSpan.isOk(), E_FAIL);
784
785 /* Initialize the bare minimum to get things going.
786 ** @todo Add more stuff later here. */
787 mData.m_strName = "VirtualBox";
788 mData.m_fHidden = false;
789
790 /* Set default repository. */
791 m->strRepoUrl = "https://update.virtualbox.org";
792
793 HRESULT hr = UpdateAgent::init(aVirtualBox);
794 if (SUCCEEDED(hr))
795 autoInitSpan.setSucceeded();
796
797 return hr;
798}
799
800void HostUpdateAgent::uninit(void)
801{
802 // Enclose the state transition Ready->InUninit->NotReady.
803 AutoUninitSpan autoUninitSpan(this);
804 if (autoUninitSpan.uninitDone())
805 return;
806}
807
808HRESULT HostUpdateAgent::checkFor(ComPtr<IProgress> &aProgress)
809{
810 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
811
812 ComObjPtr<Progress> pProgress;
813 HRESULT rc = pProgress.createObject();
814 if (FAILED(rc))
815 return rc;
816
817 rc = pProgress->init(m_VirtualBox,
818 static_cast<IUpdateAgent*>(this),
819 tr("Checking for update for %s ...", this->mData.m_strName.c_str()),
820 TRUE /* aCancelable */);
821 if (FAILED(rc))
822 return rc;
823
824 /* initialize the worker task */
825 UpdateAgentTask *pTask = new UpdateAgentTask(this, pProgress);
826 rc = pTask->createThread();
827 pTask = NULL;
828 if (FAILED(rc))
829 return rc;
830
831 return pProgress.queryInterfaceTo(aProgress.asOutParam());
832}
833
834
835/*********************************************************************************************************************************
836* Host update internal functions *
837*********************************************************************************************************************************/
838
839/**
840 * Task callback to perform an update check for the VirtualBox host (core).
841 *
842 * @returns HRESULT
843 * @param pTask Associated update agent task to use.
844 */
845DECLCALLBACK(HRESULT) HostUpdateAgent::i_checkForUpdateTask(UpdateAgentTask *pTask)
846{
847 RT_NOREF(pTask);
848
849 // Following the sequence of steps in UIUpdateStepVirtualBox::sltStartStep()
850 // Build up our query URL starting with the configured repository.
851 Utf8Str strUrl;
852 strUrl.appendPrintf("%s/query.php/?", m->strRepoUrl.c_str());
853
854 // Add platform ID.
855 Bstr platform;
856 HRESULT rc = m_VirtualBox->COMGETTER(PackageType)(platform.asOutParam());
857 AssertComRCReturn(rc, rc);
858 strUrl.appendPrintf("platform=%ls", platform.raw()); // e.g. SOLARIS_64BITS_GENERIC
859
860 // Get the complete current version string for the query URL
861 Bstr versionNormalized;
862 rc = m_VirtualBox->COMGETTER(VersionNormalized)(versionNormalized.asOutParam());
863 AssertComRCReturn(rc, rc);
864 strUrl.appendPrintf("&version=%ls", versionNormalized.raw()); // e.g. 6.1.1
865#ifdef DEBUG // Comment out previous line and uncomment this one for testing.
866// strUrl.appendPrintf("&version=6.0.12");
867#endif
868
869 ULONG revision = 0;
870 rc = m_VirtualBox->COMGETTER(Revision)(&revision);
871 AssertComRCReturn(rc, rc);
872 strUrl.appendPrintf("_%u", revision); // e.g. 135618
873
874 // Update the last update check timestamp.
875 RTTIME Time;
876 RTTIMESPEC TimeNow;
877 char szTimeStr[RTTIME_STR_LEN];
878 RTTimeToString(RTTimeExplode(&Time, RTTimeNow(&TimeNow)), szTimeStr, sizeof(szTimeStr));
879 LogRel2(("Update agent (%s): Setting last update check timestamp to '%s'\n", mData.m_strName.c_str(), szTimeStr));
880
881 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
882
883 m->strLastCheckDate = szTimeStr;
884 m->uCheckCount++;
885
886 rc = i_commitSettings(alock);
887 AssertComRCReturn(rc, rc);
888
889 strUrl.appendPrintf("&count=%RU32", m->uCheckCount);
890
891 // Update the query URL (if necessary) with the 'channel' information.
892 switch (m->enmChannel)
893 {
894 case UpdateChannel_All:
895 strUrl.appendPrintf("&branch=allrelease"); // query.php expects 'allrelease' and not 'allreleases'
896 break;
897 case UpdateChannel_WithBetas:
898 strUrl.appendPrintf("&branch=withbetas");
899 break;
900 /** @todo Handle UpdateChannel_WithTesting once implemented on the backend. */
901 case UpdateChannel_Stable:
902 RT_FALL_THROUGH();
903 default:
904 strUrl.appendPrintf("&branch=stable");
905 break;
906 }
907
908 LogRel2(("Update agent (%s): Using URL '%s'\n", mData.m_strName.c_str(), strUrl.c_str()));
909
910 /*
911 * Compose the User-Agent header for the GET request.
912 */
913 Bstr version;
914 rc = m_VirtualBox->COMGETTER(Version)(version.asOutParam()); // e.g. 6.1.0_RC1
915 AssertComRCReturn(rc, rc);
916
917 Utf8StrFmt const strUserAgent("VirtualBox %ls <%s>", version.raw(), UpdateAgent::i_getPlatformInfo().c_str());
918 LogRel2(("Update agent (%s): Using user agent '%s'\n", mData.m_strName.c_str(), strUserAgent.c_str()));
919
920 /*
921 * Create the HTTP client instance and pass it to a inner worker method to
922 * ensure proper cleanup.
923 */
924 RTHTTP hHttp = NIL_RTHTTP;
925 int vrc = RTHttpCreate(&hHttp);
926 if (RT_SUCCESS(vrc))
927 {
928 try
929 {
930 rc = i_checkForUpdateInner(hHttp, strUrl, strUserAgent);
931 }
932 catch (...)
933 {
934 AssertFailed();
935 rc = E_UNEXPECTED;
936 }
937 RTHttpDestroy(hHttp);
938 }
939 else
940 rc = i_reportError(vrc, tr("RTHttpCreate() failed: %Rrc"), vrc);
941
942 return rc;
943}
944
945/**
946 * Inner function of the actual update checking mechanism.
947 *
948 * @returns HRESULT
949 * @param hHttp HTTP client instance to use for checking.
950 * @param strUrl URL of repository to check.
951 * @param strUserAgent HTTP user agent to use for checking.
952 */
953HRESULT HostUpdateAgent::i_checkForUpdateInner(RTHTTP hHttp, Utf8Str const &strUrl, Utf8Str const &strUserAgent)
954{
955 /** @todo Are there any other headers needed to be added first via RTHttpSetHeaders()? */
956 int vrc = RTHttpAddHeader(hHttp, "User-Agent", strUserAgent.c_str(), strUserAgent.length(), RTHTTPADDHDR_F_BACK);
957 if (RT_FAILURE(vrc))
958 return i_reportError(vrc, tr("RTHttpAddHeader() failed: %Rrc (user agent)"), vrc);
959
960 /*
961 * Configure proxying.
962 */
963 if (m->enmProxyMode == ProxyMode_Manual)
964 {
965 vrc = RTHttpSetProxyByUrl(hHttp, m->strProxyUrl.c_str());
966 if (RT_FAILURE(vrc))
967 return i_reportError(vrc, tr("RTHttpSetProxyByUrl() failed: %Rrc"), vrc);
968 }
969 else if (m->enmProxyMode == ProxyMode_System)
970 {
971 vrc = RTHttpUseSystemProxySettings(hHttp);
972 if (RT_FAILURE(vrc))
973 return i_reportError(vrc, tr("RTHttpUseSystemProxySettings() failed: %Rrc"), vrc);
974 }
975 else
976 Assert(m->enmProxyMode == ProxyMode_NoProxy);
977
978 /*
979 * Perform the GET request, returning raw binary stuff.
980 */
981 void *pvResponse = NULL;
982 size_t cbResponse = 0;
983 vrc = RTHttpGetBinary(hHttp, strUrl.c_str(), &pvResponse, &cbResponse);
984 if (RT_FAILURE(vrc))
985 return i_reportError(vrc, tr("RTHttpGetBinary() failed: %Rrc"), vrc);
986
987 /* Note! We can do nothing that might throw exceptions till we call RTHttpFreeResponse! */
988
989 /*
990 * If url is platform=DARWIN_64BITS_GENERIC&version=6.0.12&branch=stable for example, the reply is:
991 * 6.0.14<SPACE>https://download.virtualbox.org/virtualbox/6.0.14/VirtualBox-6.0.14-133895-OSX.dmg
992 * If no update required, 'UPTODATE' is returned.
993 */
994 /* Parse out the two first words of the response, ignoring whatever follows: */
995 const char *pchResponse = (const char *)pvResponse;
996 while (cbResponse > 0 && *pchResponse == ' ')
997 cbResponse--, pchResponse++;
998
999 char ch;
1000 const char *pchWord0 = pchResponse;
1001 while (cbResponse > 0 && (ch = *pchResponse) != ' ' && ch != '\0')
1002 cbResponse--, pchResponse++;
1003 size_t const cchWord0 = (size_t)(pchResponse - pchWord0);
1004
1005 while (cbResponse > 0 && *pchResponse == ' ')
1006 cbResponse--, pchResponse++;
1007 const char *pchWord1 = pchResponse;
1008 while (cbResponse > 0 && (ch = *pchResponse) != ' ' && ch != '\0')
1009 cbResponse--, pchResponse++;
1010 size_t const cchWord1 = (size_t)(pchResponse - pchWord1);
1011
1012 HRESULT rc;
1013
1014 /* Decode the two word: */
1015 static char const s_szUpToDate[] = "UPTODATE";
1016 if ( cchWord0 == sizeof(s_szUpToDate) - 1
1017 && memcmp(pchWord0, s_szUpToDate, sizeof(s_szUpToDate) - 1) == 0)
1018 {
1019 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1020
1021 mData.m_enmState = UpdateState_NotAvailable;
1022 rc = S_OK;
1023
1024 alock.release(); /* Release lock before firing off event. */
1025
1026 ::FireUpdateAgentStateChangedEvent(m_EventSource, UpdateState_NotAvailable);
1027 }
1028 else
1029 {
1030 mData.m_enmState = UpdateState_Error; /* Play safe by default. */
1031
1032 vrc = RTStrValidateEncodingEx(pchWord0, cchWord0, 0 /*fFlags*/);
1033 if (RT_SUCCESS(vrc))
1034 vrc = RTStrValidateEncodingEx(pchWord1, cchWord1, 0 /*fFlags*/);
1035 if (RT_SUCCESS(vrc))
1036 {
1037 /** @todo Any additional sanity checks we could perform here? */
1038 rc = mData.m_lastResult.strVer.assignEx(pchWord0, cchWord0);
1039 if (SUCCEEDED(rc))
1040 rc = mData.m_lastResult.strDownloadUrl.assignEx(pchWord1, cchWord1);
1041
1042 if (SUCCEEDED(rc))
1043 {
1044 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1045
1046 /** @todo Implement this on the backend first.
1047 * We also could do some guessing based on the installed version vs. reported update version? */
1048 mData.m_lastResult.enmSeverity = UpdateSeverity_Invalid;
1049 mData.m_enmState = UpdateState_Available;
1050
1051 alock.release(); /* Release lock before firing off events. */
1052
1053 ::FireUpdateAgentStateChangedEvent(m_EventSource, UpdateState_Available);
1054 ::FireUpdateAgentAvailableEvent(m_EventSource, mData.m_lastResult.strVer, m->enmChannel,
1055 mData.m_lastResult.enmSeverity, mData.m_lastResult.strDownloadUrl,
1056 mData.m_lastResult.strWebUrl, mData.m_lastResult.strReleaseNotes);
1057 }
1058 else
1059 rc = i_reportError(VERR_GENERAL_FAILURE /** @todo Use a better rc */,
1060 tr("Invalid server response [1]: %Rhrc (%.*Rhxs -- %.*Rhxs)"),
1061 rc, cchWord0, pchWord0, cchWord1, pchWord1);
1062
1063 LogRel2(("Update agent (%s): HTTP server replied: %.*s %.*s\n",
1064 mData.m_strName.c_str(), cchWord0, pchWord0, cchWord1, pchWord1));
1065 }
1066 else
1067 rc = i_reportError(vrc, tr("Invalid server response [2]: %Rrc (%.*Rhxs -- %.*Rhxs)"),
1068 vrc, cchWord0, pchWord0, cchWord1, pchWord1);
1069 }
1070
1071 RTHttpFreeResponse(pvResponse);
1072
1073 return rc;
1074}
1075
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