1 | /* $Id: */
|
---|
2 | /** @file
|
---|
3 | * VBoxGuestR3Lib - Ring-3 Support Library for VirtualBox guest additions, host version check.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-2022 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 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include <stdio.h> /* Required for sscanf */
|
---|
32 | #include <iprt/string.h>
|
---|
33 | #include <VBox/log.h>
|
---|
34 |
|
---|
35 | #ifdef RT_OS_WINDOWS
|
---|
36 | #define WIN32_LEAN_AND_MEAN
|
---|
37 | #include <iprt/win/windows.h>
|
---|
38 | #endif
|
---|
39 |
|
---|
40 | #include "VBoxGuestR3LibInternal.h"
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Checks for a Guest Additions update by comparing the installed version on the
|
---|
44 | * guest and the reported host version.
|
---|
45 | *
|
---|
46 | * @returns VBox status code
|
---|
47 | *
|
---|
48 | * @param idClient The client id returned by
|
---|
49 | * VbglR3InfoSvcConnect().
|
---|
50 | * @param pfUpdate Receives pointer to boolean flag indicating
|
---|
51 | * whether an update was found or not.
|
---|
52 | * @param ppszHostVersion Receives pointer of allocated version string.
|
---|
53 | * The returned pointer must be freed using
|
---|
54 | * VbglR3GuestPropReadValueFree(). Always set to
|
---|
55 | * NULL.
|
---|
56 | * @param ppszGuestVersion Receives pointer of allocated revision string.
|
---|
57 | * The returned pointer must be freed using
|
---|
58 | * VbglR3GuestPropReadValueFree(). Always set to
|
---|
59 | * NULL.
|
---|
60 | */
|
---|
61 | VBGLR3DECL(int) VbglR3HostVersionCheckForUpdate(HGCMCLIENTID idClient, bool *pfUpdate, char **ppszHostVersion, char **ppszGuestVersion)
|
---|
62 | {
|
---|
63 | #ifdef VBOX_WITH_GUEST_PROPS
|
---|
64 | Assert(idClient > 0);
|
---|
65 | AssertPtr(pfUpdate);
|
---|
66 | AssertPtr(ppszHostVersion);
|
---|
67 | AssertPtr(ppszGuestVersion);
|
---|
68 |
|
---|
69 | *ppszHostVersion = NULL;
|
---|
70 | *ppszGuestVersion = NULL;
|
---|
71 |
|
---|
72 | /* We assume we have an update initially.
|
---|
73 | Every block down below is allowed to veto */
|
---|
74 | *pfUpdate = true;
|
---|
75 |
|
---|
76 | /* Do we need to do all this stuff? */
|
---|
77 | char *pszCheckHostVersion;
|
---|
78 | int rc = VbglR3GuestPropReadValueAlloc(idClient, "/VirtualBox/GuestAdd/CheckHostVersion", &pszCheckHostVersion);
|
---|
79 | if (RT_FAILURE(rc))
|
---|
80 | {
|
---|
81 | if (rc == VERR_NOT_FOUND)
|
---|
82 | rc = VINF_SUCCESS; /* If we don't find the value above we do the check by default */
|
---|
83 | else
|
---|
84 | LogFlow(("Could not read check host version flag! rc = %Rrc\n", rc));
|
---|
85 | }
|
---|
86 | else
|
---|
87 | {
|
---|
88 | /* Only don't do the check if we have a valid "0" in it */
|
---|
89 | if (!strcmp(pszCheckHostVersion, "0"))
|
---|
90 | {
|
---|
91 | LogRel(("No host version update check performed (disabled).\n"));
|
---|
92 | *pfUpdate = false;
|
---|
93 | }
|
---|
94 | VbglR3GuestPropReadValueFree(pszCheckHostVersion);
|
---|
95 | }
|
---|
96 |
|
---|
97 | /* Collect all needed information */
|
---|
98 | /* Make sure we only notify the user once by comparing the host version with
|
---|
99 | * the last checked host version (if any) */
|
---|
100 | if (RT_SUCCESS(rc) && *pfUpdate)
|
---|
101 | {
|
---|
102 | /* Look up host version */
|
---|
103 | rc = VbglR3GuestPropReadValueAlloc(idClient, "/VirtualBox/HostInfo/VBoxVer", ppszHostVersion);
|
---|
104 | if (RT_FAILURE(rc))
|
---|
105 | {
|
---|
106 | LogFlow(("Could not read VBox host version! rc = %Rrc\n", rc));
|
---|
107 | }
|
---|
108 | else
|
---|
109 | {
|
---|
110 | LogFlow(("Host version: %s\n", *ppszHostVersion));
|
---|
111 |
|
---|
112 | /* Get last checked host version */
|
---|
113 | char *pszLastCheckedHostVersion;
|
---|
114 | rc = VbglR3HostVersionLastCheckedLoad(idClient, &pszLastCheckedHostVersion);
|
---|
115 | if (RT_SUCCESS(rc))
|
---|
116 | {
|
---|
117 | LogFlow(("Last checked host version: %s\n", pszLastCheckedHostVersion));
|
---|
118 | if (strcmp(*ppszHostVersion, pszLastCheckedHostVersion) == 0)
|
---|
119 | *pfUpdate = false; /* We already notified this version, skip */
|
---|
120 | VbglR3GuestPropReadValueFree(pszLastCheckedHostVersion);
|
---|
121 | }
|
---|
122 | else if (rc == VERR_NOT_FOUND) /* Never wrote a last checked host version before */
|
---|
123 | {
|
---|
124 | LogFlow(("Never checked a host version before.\n"));
|
---|
125 | rc = VINF_SUCCESS;
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | /* Look up guest version */
|
---|
130 | if (RT_SUCCESS(rc))
|
---|
131 | {
|
---|
132 | rc = VbglR3GetAdditionsVersion(ppszGuestVersion, NULL /* Extended version not needed here */,
|
---|
133 | NULL /* Revision not needed here */);
|
---|
134 | if (RT_FAILURE(rc))
|
---|
135 | LogFlow(("Could not read VBox guest version! rc = %Rrc\n", rc));
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | /* Do the actual version comparison (if needed, see block(s) above) */
|
---|
140 | if (RT_SUCCESS(rc) && *pfUpdate)
|
---|
141 | {
|
---|
142 | if (RTStrVersionCompare(*ppszHostVersion, *ppszGuestVersion) > 0) /* Is host version greater than guest add version? */
|
---|
143 | {
|
---|
144 | /* Yay, we have an update! */
|
---|
145 | LogRel(("Guest Additions update found! Please upgrade this machine to the latest Guest Additions.\n"));
|
---|
146 | }
|
---|
147 | else
|
---|
148 | {
|
---|
149 | /* How sad ... */
|
---|
150 | *pfUpdate = false;
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | /* Cleanup on failure */
|
---|
155 | if (RT_FAILURE(rc))
|
---|
156 | {
|
---|
157 | if (*ppszHostVersion)
|
---|
158 | {
|
---|
159 | VbglR3GuestPropReadValueFree(*ppszHostVersion);
|
---|
160 | *ppszHostVersion = NULL;
|
---|
161 | }
|
---|
162 | if (*ppszGuestVersion)
|
---|
163 | {
|
---|
164 | VbglR3GuestPropReadValueFree(*ppszGuestVersion);
|
---|
165 | *ppszGuestVersion = NULL;
|
---|
166 | }
|
---|
167 | }
|
---|
168 | return rc;
|
---|
169 | #else /* !VBOX_WITH_GUEST_PROPS */
|
---|
170 | RT_NOREF(idClient, pfUpdate, ppszHostVersion, ppszGuestVersion);
|
---|
171 | return VERR_NOT_SUPPORTED;
|
---|
172 | #endif
|
---|
173 | }
|
---|
174 |
|
---|
175 |
|
---|
176 | /** Retrieves the last checked host version.
|
---|
177 | *
|
---|
178 | * @returns VBox status code.
|
---|
179 | *
|
---|
180 | * @param idClient The client id returned by VbglR3InfoSvcConnect().
|
---|
181 | * @param ppszVer Receives pointer of allocated version string.
|
---|
182 | * The returned pointer must be freed using RTStrFree() on VINF_SUCCESS.
|
---|
183 | */
|
---|
184 | VBGLR3DECL(int) VbglR3HostVersionLastCheckedLoad(HGCMCLIENTID idClient, char **ppszVer)
|
---|
185 | {
|
---|
186 | #ifdef VBOX_WITH_GUEST_PROPS
|
---|
187 | Assert(idClient > 0);
|
---|
188 | AssertPtr(ppszVer);
|
---|
189 | return VbglR3GuestPropReadValueAlloc(idClient, "/VirtualBox/GuestAdd/HostVerLastChecked", ppszVer);
|
---|
190 | #else /* !VBOX_WITH_GUEST_PROPS */
|
---|
191 | RT_NOREF(idClient, ppszVer);
|
---|
192 | return VERR_NOT_SUPPORTED;
|
---|
193 | #endif
|
---|
194 | }
|
---|
195 |
|
---|
196 |
|
---|
197 | /** Stores the last checked host version for later lookup.
|
---|
198 | * Requires strings in form of "majorVer.minorVer.build".
|
---|
199 | *
|
---|
200 | * @returns VBox status code.
|
---|
201 | *
|
---|
202 | * @param idClient The client id returned by VbglR3InfoSvcConnect().
|
---|
203 | * @param pszVer Pointer to version string to store.
|
---|
204 | */
|
---|
205 | VBGLR3DECL(int) VbglR3HostVersionLastCheckedStore(HGCMCLIENTID idClient, const char *pszVer)
|
---|
206 | {
|
---|
207 | #ifdef VBOX_WITH_GUEST_PROPS
|
---|
208 | Assert(idClient > 0);
|
---|
209 | AssertPtr(pszVer);
|
---|
210 | return VbglR3GuestPropWriteValue(idClient, "/VirtualBox/GuestAdd/HostVerLastChecked", pszVer);
|
---|
211 | #else /* !VBOX_WITH_GUEST_PROPS */
|
---|
212 | RT_NOREF(idClient, pszVer);
|
---|
213 | return VERR_NOT_SUPPORTED;
|
---|
214 | #endif
|
---|
215 | }
|
---|
216 |
|
---|