VirtualBox

Changeset 86876 in vbox for trunk/src/VBox/Additions/common


Ignore:
Timestamp:
Nov 12, 2020 4:38:00 PM (4 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
141332
Message:

Guest Additions/VbglR3: Condensed / removed duplicate guest property code by adding VbglR3GuestPropExist() + VbglR3GuestPropReadEx().

Location:
trunk/src/VBox/Additions/common
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Additions/common/VBoxGuest/lib/VBoxGuestR3LibGuestProp.cpp

    r82968 r86876  
    3838#endif
    3939#include <iprt/assert.h>
     40#include <iprt/mem.h>
    4041#include <iprt/stdarg.h>
    4142#include <VBox/err.h>
     
    150151
    151152/**
     153 * Checks if @a pszPropName exists.
     154 *
     155 * @returns \c true if the guest property exists, \c false if not.
     156 * @param   idClient            The HGCM client ID for the guest property session.
     157 * @param   pszPropName         The property name.
     158 */
     159VBGLR3DECL(bool) VbglR3GuestPropExist(uint32_t idClient, const char *pszPropName)
     160{
     161    return RT_SUCCESS(VbglR3GuestPropReadEx(idClient, pszPropName, NULL /*ppszValue*/, NULL /* ppszFlags */, NULL /* puTimestamp */));
     162}
     163
     164
     165/**
    152166 * Write a property value.
    153167 *
     
    352366
    353367    return VINF_SUCCESS;
     368}
     369
     370/**
     371 * Reads a guest property by returning allocated values.
     372 *
     373 * @returns VBox status code, fully bitched.
     374 *
     375 * @param   idClient            The HGCM client ID for the guest property session.
     376 * @param   pszPropName         The property name.
     377 * @param   ppszValue           Where to return the value.  This is always set
     378 *                              to NULL.  Needs to be free'd using RTStrFree().  Optional.
     379 * @param   ppszFlags           Where to return the value flags.
     380 *                              Needs to be free'd using RTStrFree().  Optional.
     381 * @param   puTimestamp         Where to return the timestamp.  This is only set
     382 *                              on success.  Optional.
     383 */
     384VBGLR3DECL(int) VbglR3GuestPropReadEx(uint32_t idClient,
     385                                      const char *pszPropName, char **ppszValue, char **ppszFlags, uint64_t *puTimestamp)
     386{
     387    AssertPtrReturn(pszPropName, VERR_INVALID_POINTER);
     388
     389    uint32_t    cbBuf = _1K;
     390    void       *pvBuf = NULL;
     391    int         rc    = VINF_SUCCESS;  /* MSC can't figure out the loop */
     392
     393    if (ppszValue)
     394        *ppszValue = NULL;
     395
     396    for (unsigned cTries = 0; cTries < 10; cTries++)
     397    {
     398        /*
     399         * (Re-)Allocate the buffer and try read the property.
     400         */
     401        RTMemFree(pvBuf);
     402        pvBuf = RTMemAlloc(cbBuf);
     403        if (!pvBuf)
     404        {
     405            rc = VERR_NO_MEMORY;
     406            break;
     407        }
     408        char    *pszValue;
     409        char    *pszFlags;
     410        uint64_t uTimestamp;
     411        rc = VbglR3GuestPropRead(idClient, pszPropName, pvBuf, cbBuf, &pszValue, &uTimestamp, &pszFlags, NULL);
     412        if (RT_FAILURE(rc))
     413        {
     414            if (rc == VERR_BUFFER_OVERFLOW)
     415            {
     416                /* try again with a bigger buffer. */
     417                cbBuf *= 2;
     418                continue;
     419            }
     420            break;
     421        }
     422
     423        if (ppszValue)
     424        {
     425            *ppszValue = RTStrDup(pszValue);
     426            if (!*ppszValue)
     427            {
     428                rc = VERR_NO_MEMORY;
     429                break;
     430            }
     431        }
     432
     433        if (puTimestamp)
     434            *puTimestamp = uTimestamp;
     435        if (ppszFlags)
     436            *ppszFlags = RTStrDup(pszFlags);
     437        break; /* done */
     438    }
     439
     440    if (pvBuf)
     441        RTMemFree(pvBuf);
     442    return rc;
    354443}
    355444
  • trunk/src/VBox/Additions/common/VBoxService/VBoxService.cpp

    r86823 r86876  
    905905    if (RT_SUCCESS(rc))
    906906    {
    907         rc = VGSvcCheckPropExist(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/DRMResize");
     907        rc = VbglR3GuestPropExist(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/DRMResize");
    908908        if (RT_SUCCESS(rc))
    909909        {
  • trunk/src/VBox/Additions/common/VBoxService/VBoxServiceTimeSync.cpp

    r83974 r86876  
    210210            rc = VGSvcReadPropUInt32(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold",
    211211                                     &g_TimeSyncSetThreshold, 0, 7*24*60*60*1000 /* a week */);
    212         if (   RT_SUCCESS(rc)
    213             || rc == VERR_NOT_FOUND)
    214         {
    215             rc = VGSvcCheckPropExist(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/VBoxService/--timesync-set-start");
    216             if (RT_SUCCESS(rc))
    217                 g_fTimeSyncSetOnStart = true;
    218         }
    219         if (   RT_SUCCESS(rc)
    220             || rc == VERR_NOT_FOUND)
    221         {
    222             rc = VGSvcCheckPropExist(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/VBoxService/--timesync-no-set-start");
    223             if (RT_SUCCESS(rc))
    224                 g_fTimeSyncSetOnStart = false;
    225         }
    226         if (   RT_SUCCESS(rc)
    227             || rc == VERR_NOT_FOUND)
    228         {
    229             rc = VGSvcCheckPropExist(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/VBoxService/--timesync-set-on-restore");
    230             if (RT_SUCCESS(rc))
    231                 g_fTimeSyncSetOnRestore = true;
    232         }
    233         if (   RT_SUCCESS(rc)
    234             || rc == VERR_NOT_FOUND)
    235         {
    236             rc = VGSvcCheckPropExist(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/VBoxService/--timesync-no-set-on-restore");
    237             if (RT_SUCCESS(rc))
    238                 g_fTimeSyncSetOnRestore = false;
    239         }
    240         if (   RT_SUCCESS(rc)
    241             || rc == VERR_NOT_FOUND)
    242         {
    243             uint32_t uValue;
    244             rc = VGSvcReadPropUInt32(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/VBoxService/--timesync-verbosity",
    245                                      &uValue, 0 /*uMin*/, 255 /*uMax*/);
    246             if (RT_SUCCESS(rc))
    247                 g_cTimeSyncVerbosity = uValue;
    248         }
     212
     213        if (VbglR3GuestPropExist(uGuestPropSvcClientID,
     214                                 "/VirtualBox/GuestAdd/VBoxService/--timesync-set-start"))
     215            g_fTimeSyncSetOnStart = true;
     216
     217        if (VbglR3GuestPropExist(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/VBoxService/--timesync-no-set-start"))
     218            g_fTimeSyncSetOnStart = false;
     219
     220
     221        if (VbglR3GuestPropExist(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/VBoxService/--timesync-set-on-restore"))
     222            g_fTimeSyncSetOnRestore = true;
     223
     224        if (VbglR3GuestPropExist(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/VBoxService/--timesync-no-set-on-restore"))
     225            g_fTimeSyncSetOnRestore = false;
     226
     227        uint32_t uValue;
     228        rc = VGSvcReadPropUInt32(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/VBoxService/--timesync-verbosity",
     229                                 &uValue, 0 /*uMin*/, 255 /*uMax*/);
     230        if (RT_SUCCESS(rc))
     231            g_cTimeSyncVerbosity = uValue;
     232
    249233        VbglR3GuestPropDisconnect(uGuestPropSvcClientID);
    250234    }
  • trunk/src/VBox/Additions/common/VBoxService/VBoxServiceUtils.cpp

    r82968 r86876  
    3434
    3535#ifdef VBOX_WITH_GUEST_PROPS
    36 
    37 /**
    38  * Reads a guest property.
    39  *
    40  * @returns VBox status code, fully bitched.
    41  *
    42  * @param   u32ClientId         The HGCM client ID for the guest property session.
    43  * @param   pszPropName         The property name.
    44  * @param   ppszValue           Where to return the value.  This is always set
    45  *                              to NULL.  Free it using RTStrFree().  Optional.
    46  * @param   ppszFlags           Where to return the value flags. Free it
    47  *                              using RTStrFree().  Optional.
    48  * @param   puTimestamp         Where to return the timestamp.  This is only set
    49  *                              on success.  Optional.
    50  */
    51 int VGSvcReadProp(uint32_t u32ClientId, const char *pszPropName, char **ppszValue, char **ppszFlags, uint64_t *puTimestamp)
    52 {
    53     AssertPtrReturn(pszPropName, VERR_INVALID_POINTER);
    54 
    55     uint32_t    cbBuf = _1K;
    56     void       *pvBuf = NULL;
    57     int         rc    = VINF_SUCCESS;  /* MSC can't figure out the loop */
    58 
    59     if (ppszValue)
    60         *ppszValue = NULL;
    61 
    62     for (unsigned cTries = 0; cTries < 10; cTries++)
    63     {
    64         /*
    65          * (Re-)Allocate the buffer and try read the property.
    66          */
    67         RTMemFree(pvBuf);
    68         pvBuf = RTMemAlloc(cbBuf);
    69         if (!pvBuf)
    70         {
    71             VGSvcError("Guest Property: Failed to allocate %zu bytes\n", cbBuf);
    72             rc = VERR_NO_MEMORY;
    73             break;
    74         }
    75         char    *pszValue;
    76         char    *pszFlags;
    77         uint64_t uTimestamp;
    78         rc = VbglR3GuestPropRead(u32ClientId, pszPropName, pvBuf, cbBuf, &pszValue, &uTimestamp, &pszFlags, NULL);
    79         if (RT_FAILURE(rc))
    80         {
    81             if (rc == VERR_BUFFER_OVERFLOW)
    82             {
    83                 /* try again with a bigger buffer. */
    84                 cbBuf *= 2;
    85                 continue;
    86             }
    87             if (rc == VERR_NOT_FOUND)
    88                 VGSvcVerbose(2, "Guest Property: %s not found\n", pszPropName);
    89             else
    90                 VGSvcError("Guest Property: Failed to query '%s': %Rrc\n", pszPropName, rc);
    91             break;
    92         }
    93 
    94         VGSvcVerbose(2, "Guest Property: Read '%s' = '%s', timestamp %RU64n\n", pszPropName, pszValue, uTimestamp);
    95         if (ppszValue)
    96         {
    97             *ppszValue = RTStrDup(pszValue);
    98             if (!*ppszValue)
    99             {
    100                 VGSvcError("Guest Property: RTStrDup failed for '%s'\n", pszValue);
    101                 rc = VERR_NO_MEMORY;
    102                 break;
    103             }
    104         }
    105 
    106         if (puTimestamp)
    107             *puTimestamp = uTimestamp;
    108         if (ppszFlags)
    109             *ppszFlags = RTStrDup(pszFlags);
    110         break; /* done */
    111     }
    112 
    113     if (pvBuf)
    114         RTMemFree(pvBuf);
    115     return rc;
    116 }
    117 
    118 
    11936/**
    12037 * Reads a guest property as a 32-bit value.
     
    13047{
    13148    char *pszValue;
    132     int rc = VGSvcReadProp(u32ClientId, pszPropName, &pszValue, NULL /* ppszFlags */, NULL /* puTimestamp */);
     49    int rc = VbglR3GuestPropReadEx(u32ClientId, pszPropName, &pszValue, NULL /* ppszFlags */, NULL /* puTimestamp */);
    13350    if (RT_SUCCESS(rc))
    13451    {
     
    14360    return rc;
    14461}
    145 
    146 /**
    147  * Checks if @a pszPropName exists.
    148  *
    149  * @returns VBox status code.
    150  * @retval  VINF_SUCCESS if it exists.
    151  * @retval  VERR_NOT_FOUND if not found.
    152  *
    153  * @param   u32ClientId         The HGCM client ID for the guest property session.
    154  * @param   pszPropName         The property name.
    155  */
    156 int VGSvcCheckPropExist(uint32_t u32ClientId, const char *pszPropName)
    157 {
    158     return VGSvcReadProp(u32ClientId, pszPropName, NULL /*ppszValue*/, NULL /* ppszFlags */, NULL /* puTimestamp */);
    159 }
    160 
    16162
    16263/**
     
    18384    char *pszValue = NULL;
    18485    char *pszFlags = NULL;
    185     int rc = VGSvcReadProp(u32ClientId, pszPropName, &pszValue, &pszFlags, puTimestamp);
     86    int rc = VbglR3GuestPropReadEx(u32ClientId, pszPropName, &pszValue, &pszFlags, puTimestamp);
    18687    if (RT_SUCCESS(rc))
    18788    {
  • trunk/src/VBox/Additions/common/VBoxService/VBoxServiceUtils.h

    r82968 r86876  
    2727int VGSvcReadProp(uint32_t u32ClientId, const char *pszPropName, char **ppszValue, char **ppszFlags, uint64_t *puTimestamp);
    2828int VGSvcReadPropUInt32(uint32_t u32ClientId, const char *pszPropName, uint32_t *pu32, uint32_t u32Min, uint32_t u32Max);
    29 int VGSvcCheckPropExist(uint32_t u32ClientId, const char *pszPropName);
    3029int VGSvcReadHostProp(uint32_t u32ClientId, const char *pszPropName, bool fReadOnly, char **ppszValue, char **ppszFlags,
    3130                      uint64_t *puTimestamp);
Note: See TracChangeset for help on using the changeset viewer.

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