VirtualBox

Ignore:
Timestamp:
Oct 19, 2009 4:24:26 PM (15 years ago)
Author:
vboxsync
Message:

VbglR3/VBoxTray/VBoxClient: Common code to check for host updates (not tested on non-Windows yet).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR3LibHostVersion.cpp

    • Property svn:executable deleted
    • Property svn:keywords set to Author Date Id Revision
    r23857 r23876  
    3737
    3838/** Compares two VirtualBox version strings and returns the result.
    39  *  Requires strings in form of "majorVer.minorVer.build"
     39 *  Requires strings in form of "majorVer.minorVer.build".
    4040 *
    4141 * @returns 0 if equal, 1 if Ver1 is greater, 2 if Ver2 is greater.
     
    6464
    6565
    66 /** @todo Docs */
    67 VBGLR3DECL(bool) VbglR3HostVersionCheckForUpdate(char **ppszHostVersion, char **ppszGuestVersion)
     66/** Checks for a Guest Additions update by comparing the installed version on
     67 *  the guest and the reported host version.
     68 *
     69 * @returns VBox status code
     70 *
     71 * @param   u32ClientId          The client id returned by VbglR3InfoSvcConnect().
     72 * @param   bUpdate              Receives pointer to boolean flag indicating whether
     73                                 an update was found or not.
     74 * @param   ppszHostVersion      Receives pointer of allocated version string.
     75 *                               The returned pointer must be freed using RTStrFree().
     76 * @param   ppszGuestVersion     Receives pointer of allocated revision string.
     77 *                               The returned pointer must be freed using RTStrFree().
     78 */
     79VBGLR3DECL(int) VbglR3HostVersionCheckForUpdate(uint32_t u32ClientId, bool *bUpdate, char **ppszHostVersion, char **ppszGuestVersion)
    6880{
    6981    int rc;
    70     uint32_t uGuestPropSvcClientID;
    71 
    72     rc = VbglR3GuestPropConnect(&uGuestPropSvcClientID);
    73     if (RT_SUCCESS(rc))
    74         LogFlow(("Property Service Client ID: %ld\n", uGuestPropSvcClientID));
    75     else
    76         LogFlow(("Failed to connect to the guest property service! Error: %d\n", rc));
     82
     83    Assert(u32ClientId > 0);
     84    Assert(bUpdate);
     85    Assert(ppszHostVersion);
     86    Assert(ppszGuestVersion);
     87
     88    /* We assume we have an update initially.
     89       Every block down below is allowed to veto */
     90    *bUpdate = true;
    7791
    7892    /* Do we need to do all this stuff? */
    7993    char *pszCheckHostVersion;
    80     rc = VbglR3GuestPropReadValueAlloc(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/CheckHostVersion", &pszCheckHostVersion);
     94    rc = VbglR3GuestPropReadValueAlloc(u32ClientId, "/VirtualBox/GuestAdd/CheckHostVersion", &pszCheckHostVersion);
    8195    if (RT_FAILURE(rc))
    8296    {
     
    92106            && strlen(pszCheckHostVersion))
    93107        {
    94             rc = VERR_NOT_SUPPORTED;
     108            LogRel(("No host version update check performed (disabled)."));
     109            *bUpdate = false;
    95110        }
    96111        VbglR3GuestPropReadValueFree(pszCheckHostVersion);
    97     }
    98     if (rc == VERR_NOT_SUPPORTED)
    99         LogRel(("No host version check performed (disabled)."));
    100 
    101     if (RT_SUCCESS(rc))
    102     {
    103         /* Look up host version (revision) */
    104         rc = VbglR3GuestPropReadValueAlloc(uGuestPropSvcClientID, "/VirtualBox/HostInfo/VBoxVer", ppszHostVersion);
     112    }   
     113
     114    /* Make sure we only notify the user once by comparing the host version with
     115     * the last checked host version (if any) */
     116    if (RT_SUCCESS(rc) && *bUpdate)
     117    {
     118        /* Look up host version */
     119        rc = VbglR3GuestPropReadValueAlloc(u32ClientId, "/VirtualBox/HostInfo/VBoxVer", ppszHostVersion);
    105120        if (RT_FAILURE(rc))
    106121        {
     
    111126            LogFlow(("Host version: %s\n", *ppszHostVersion));
    112127
    113             /* Look up guest version */
    114             rc = VbglR3GetAdditionsVersion(ppszGuestVersion, NULL /* Revision */);
     128            /* Get last checked host version */
     129            char *pszLastCheckedHostVersion;
     130            rc = VbglR3HostVersionLastCheckedLoad(u32ClientId, &pszLastCheckedHostVersion);
    115131            if (RT_SUCCESS(rc))
    116132            {
    117                 LogFlow(("Additions version: %s\n", *ppszGuestVersion));
    118 
    119                 /* Look up last informed host version */
    120                 char szVBoxHostVerLastChecked[32];
    121 #ifdef RT_OS_WINDOWS
    122                 HKEY hKey;
    123                 LONG lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Sun\\VirtualBox Guest Additions", 0, KEY_READ, &hKey);
    124                 if (lRet == ERROR_SUCCESS)
    125                 {
    126                     DWORD dwType;
    127                     DWORD dwSize = sizeof(szVBoxHostVerLastChecked);
    128                     lRet = RegQueryValueEx(hKey, "HostVerLastChecked", NULL, &dwType, (BYTE*)szVBoxHostVerLastChecked, &dwSize);
    129                     if (lRet != ERROR_SUCCESS && lRet != ERROR_FILE_NOT_FOUND)
    130                         LogFlow(("Could not read HostVerLastChecked! Error = %ld\n", lRet));
    131                     RegCloseKey(hKey);
    132                 }
    133                 else if (lRet != ERROR_FILE_NOT_FOUND)
    134                 {
    135                     LogFlow(("Could not open registry key! Error = %ld\n", lRet));
    136                     rc = RTErrConvertFromWin32(lRet);
    137                 }
    138 #else
    139 
    140 #endif
    141                 /* Compare both versions and prepare message */
    142                 if (   RT_SUCCESS(rc)
    143                     && strcmp(*ppszHostVersion, szVBoxHostVerLastChecked) != 0        /* Make sure we did not process this host version already */
    144                     && VbglR3HostVersionCompare(*ppszHostVersion, *ppszGuestVersion) == 1)     /* Is host version greater than guest add version? */
    145                 {
    146                     LogFlow(("Update found."));
    147                 }
    148                 else rc = VERR_VERSION_MISMATCH; /* No update found */
     133                LogFlow(("Last checked host version: %s\n", pszLastCheckedHostVersion));
     134                if (strcmp(*ppszHostVersion, pszLastCheckedHostVersion) == 0)
     135                    *bUpdate = false; /* We already notified this version, skip */
     136                VbglR3GuestPropReadValueFree(pszLastCheckedHostVersion);
    149137            }
    150         }
    151     }
    152 
     138            else if (rc == VERR_NOT_FOUND) /* Never wrote a last checked host version before */
     139            {
     140                LogFlow(("Never checked a host version before."));
     141                rc = VINF_SUCCESS;
     142            }
     143        }
     144
     145        if (RT_SUCCESS(rc))
     146        {
     147            /* Look up guest version */
     148            rc = VbglR3GetAdditionsVersion(ppszGuestVersion, NULL /* Revision not needed here */);
     149        }
     150    }
     151     
     152    /* Do the actual version comparison (if needed, see block(s) above) */
     153    if (RT_SUCCESS(rc) && *bUpdate)
     154    {
     155        if (VbglR3HostVersionCompare(*ppszHostVersion, *ppszGuestVersion) == 1) /* Is host version greater than guest add version? */
     156        {
     157            /* Yay, we have an update! */
     158            LogRel(("Guest Additions update found! Please upgrade this machine to the latest Guest Additions."));
     159        }
     160        else
     161        {
     162            /* How sad ... */
     163            *bUpdate = false;
     164        }
     165    }
     166
     167    /* Cleanup on failure */
    153168    if (RT_FAILURE(rc))
    154169    {
    155         if (*ppszHostVersion)
    156             VbglR3GuestPropReadValueFree(*ppszHostVersion);
    157         if (*ppszGuestVersion)
    158             VbglR3GuestPropReadValueFree(*ppszGuestVersion);
    159     }
    160     return rc == VINF_SUCCESS ? true : false;
    161 }
    162 
    163 
    164 /** @todo Docs */
    165 VBGLR3DECL(int) VbglR3HostVersionStore(const char* pszVer)
    166 {
    167     int rc;
    168     uint32_t uGuestPropSvcClientID;
    169 
    170     rc = VbglR3GuestPropConnect(&uGuestPropSvcClientID);
    171     if (RT_FAILURE(rc))
    172     {
    173         LogFlow(("Failed to connect to the guest property service! Error: %d\n", rc));
    174     }
    175     else
    176     {
    177 #ifdef RT_OS_WINDOWS
    178         HKEY hKey;
    179         long lRet;
    180         lRet = RegCreateKeyEx (HKEY_LOCAL_MACHINE,
    181                                "SOFTWARE\\Sun\\VirtualBox Guest Additions",
    182                                0,           /* Reserved */
    183                                NULL,        /* lpClass [in, optional] */
    184                                0,           /* dwOptions [in] */
    185                                KEY_WRITE,
    186                                NULL,        /* lpSecurityAttributes [in, optional] */
    187                                &hKey,
    188                                NULL);       /* lpdwDisposition [out, optional] */
    189         if (lRet == ERROR_SUCCESS)
    190         {
    191             lRet = RegSetValueEx(hKey, "HostVerLastChecked", 0, REG_SZ, (BYTE*)pszVer, (DWORD)(strlen(pszVer)*sizeof(char)));
    192             if (lRet != ERROR_SUCCESS)
    193                 LogFlow(("Could not write HostVerLastChecked! Error = %ld\n", lRet));
    194             RegCloseKey(hKey);
    195         }
    196         else
    197             LogFlow(("Could not open registry key! Error = %ld\n", lRet));
    198    
    199         if (lRet != ERROR_SUCCESS)
    200             rc = RTErrConvertFromWin32(lRet);
    201 #else
    202 #endif
     170        VbglR3GuestPropReadValueFree(*ppszHostVersion);
     171        VbglR3GuestPropReadValueFree(*ppszGuestVersion);
    203172    }
    204173    return rc;
    205174}
     175
     176
     177/** Retrieves the last checked host version.
     178 *
     179 * @returns VBox status code.
     180 *
     181 * @param   u32ClientId     The client id returned by VbglR3InfoSvcConnect().
     182 * @param   ppszVer         Receives pointer of allocated version string.
     183 *                          The returned pointer must be freed using RTStrFree() on VINF_SUCCESS.
     184 */
     185VBGLR3DECL(int) VbglR3HostVersionLastCheckedLoad(uint32_t u32ClientId, char **ppszVer)
     186{
     187    Assert(u32ClientId > 0);
     188    Assert(ppszVer);
     189    return VbglR3GuestPropReadValueAlloc(u32ClientId, "/VirtualBox/GuestAdd/HostVerLastChecked", ppszVer);
     190}
     191
     192
     193/** Stores the last checked host version for later lookup.
     194 *  Requires strings in form of "majorVer.minorVer.build".
     195 *
     196 * @returns VBox status code.
     197 *
     198 * @param   u32ClientId     The client id returned by VbglR3InfoSvcConnect().
     199 * @param   pszVer          Pointer to version string to store.
     200 */
     201VBGLR3DECL(int) VbglR3HostVersionLastCheckedStore(uint32_t u32ClientId, const char *pszVer)
     202{
     203    Assert(u32ClientId > 0);
     204    Assert(pszVer); 
     205    return VbglR3GuestPropWriteValue(u32ClientId, "/VirtualBox/GuestAdd/HostVerLastChecked", pszVer);
     206}
Note: See TracChangeset for help on using the changeset viewer.

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