VirtualBox

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


Ignore:
Timestamp:
Jul 23, 2008 10:08:29 AM (17 years ago)
Author:
vboxsync
Message:

Additions/commom: switched VBoxControl to use the new guest property syntax

File:
1 edited

Legend:

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

    r10797 r10825  
    7777#endif
    7878#ifdef VBOX_WITH_GUEST_PROPS
    79     GET_GUEST_PROP,
    80     SET_GUEST_PROP,
     79    GUEST_PROP,
    8180#endif
    8281    USAGE_ALL = UINT32_MAX
     
    8685{
    8786    RTPrintf("Usage:\n\n");
    88     RTPrintf("%s [-v|--version]       print version number and exit\n", g_pszProgName);
    89     RTPrintf("%s --nologo ...         suppress the logo\n\n", g_pszProgName);
     87    RTPrintf("%s [-v|-version]        print version number and exit\n", g_pszProgName);
     88    RTPrintf("%s -nologo ...          suppress the logo\n\n", g_pszProgName);
    9089
    9190#ifdef RT_OS_WINDOWS
     
    104103#endif
    105104#ifdef VBOX_WITH_GUEST_PROPS
    106     if ((GET_GUEST_PROP == eWhich) || (USAGE_ALL == eWhich))
    107         doUsage("<name>\n", g_pszProgName, "getguestproperty");
    108     if ((SET_GUEST_PROP == eWhich) || (USAGE_ALL == eWhich))
    109         doUsage("<name> [<value>] (no value deletes property)\n", g_pszProgName, "setguestproperty");
     105    if ((GUEST_PROP == eWhich) || (USAGE_ALL == eWhich))
     106    {
     107        doUsage("get <property> [-verbose]\n", g_pszProgName, "guestproperty");
     108        doUsage("set <property> [<value>] [--flags <flags>]\n", g_pszProgName, "guestproperty");
     109    }
    110110#endif
    111111}
     
    866866 *
    867867 * @returns 0 on success, 1 on failure
    868  * @param   name  (string) the name of the property.
     868 * @note see the command line API description for parameters
    869869 */
    870870int getGuestProperty(int argc, char **argv)
     
    872872    using namespace guestProp;
    873873
    874     if (argc != 1)
    875     {
    876         usage(GET_GUEST_PROP);
     874    bool verbose = false;
     875    if ((2 == argc) && (0 == strcmp(argv[1], "-verbose")))
     876        verbose = true;
     877    else if (argc != 1)
     878    {
     879        usage(GUEST_PROP);
    877880        return 1;
    878881    }
    879882
    880     uint32_t u32ClientID = 0;
     883    uint32_t u32ClientId = 0;
    881884    int rc = VINF_SUCCESS;
    882     char *pszName = argv[0];
     885
     886    /* We leave a bit of space here in case the maximum values are raised. */
     887    uint32_t cbBuf = MAX_VALUE_LEN + MAX_FLAGS_LEN + 1024;
     888    void *pvBuf = RTMemAlloc(cbBuf);
     889    if (NULL == pvBuf)
     890    {
     891        rc = VERR_NO_MEMORY;
     892        VBoxControlError("Out of memory\n");
     893    }
     894    if (RT_SUCCESS(rc))
     895    {
     896        rc = VbglR3GuestPropConnect(&u32ClientId);
     897        if (!RT_SUCCESS(rc))
     898            VBoxControlError("Failed to connect to the guest property service, error %Rrc\n", rc);
     899    }
     900
     901/*
     902 * Here we actually retrieve the value from the host.
     903 */
     904    const char *pszName = argv[0];
    883905    char *pszValue = NULL;
    884 
    885     rc = VbglR3GuestPropConnect(&u32ClientID);
    886     if (!RT_SUCCESS(rc))
    887         VBoxControlError("Failed to connect to the guest property service, error %Rrc\n", rc);
     906    uint64_t u64Timestamp = 0;
     907    char *pszFlags = NULL;
    888908    if (RT_SUCCESS(rc))
    889909    {
    890         rc = VbglR3GuestPropReadValueAlloc(u32ClientID, pszName, &pszValue);
    891         if (!RT_SUCCESS(rc) && (rc != VERR_NOT_FOUND))
     910        /* Because there is a race condition between our reading the size of a
     911         * property and the guest updating it, we loop a few times here and
     912         * hope.  Actually this should never go wrong, as we are generous
     913         * enough with buffer space. */
     914        bool finish = false;
     915        for (int i = 0; (i < 10) && !finish; ++i)
     916        {
     917            rc = VbglR3GuestPropRead(u32ClientId, pszName, pvBuf, cbBuf,
     918                                     &pszValue, &u64Timestamp, &pszFlags,
     919                                     &cbBuf);
     920            if (VERR_BUFFER_OVERFLOW == rc)
     921            {
     922                pvBuf = RTMemRealloc(pvBuf, cbBuf);
     923                if (NULL == pvBuf)
     924                    rc = VERR_NO_MEMORY;
     925            }
     926            if (rc != VERR_BUFFER_OVERFLOW)
     927                finish = true;
     928        }
     929        if (VERR_TOO_MUCH_DATA == rc)
     930            VBoxControlError("Temporarily unable to retrieve the property\n");
     931        else if (!RT_SUCCESS(rc) && (rc != VERR_NOT_FOUND))
    892932            VBoxControlError("Failed to retrieve the property value, error %Rrc\n", rc);
    893933    }
     934/*
     935 * And display it on the guest console.
     936 */
    894937    if (VERR_NOT_FOUND == rc)
    895938        RTPrintf("No value set!\n");
    896939    if (RT_SUCCESS(rc))
    897940        RTPrintf("Value: %S\n", pszValue);
    898     if (u32ClientID != 0)
    899         VbglR3GuestPropDisconnect(u32ClientID);
     941    if (RT_SUCCESS(rc) && verbose)
     942    {
     943        RTPrintf("Timestamp: %lld ns\n", u64Timestamp);
     944        RTPrintf("Flags: %S\n", pszFlags);
     945    }
     946
     947    if (u32ClientId != 0)
     948        VbglR3GuestPropDisconnect(u32ClientId);
    900949    VbglR3GuestPropReadValueFree(pszValue);
    901950    return RT_SUCCESS(rc) ? 0 : 1;
     
    908957 *
    909958 * @returns 0 on success, 1 on failure
    910  * @param   name  (string) the name of the property.
    911  * @param   value (string) the value to write.  If empty, the property will be
    912  *                removed.
     959 * @note see the command line API description for parameters
    913960 */
    914961static int setGuestProperty(int argc, char *argv[])
    915962{
    916     if (argc != 1 && argc != 2)
    917     {
    918         usage(SET_GUEST_PROP);
     963/*
     964 * Check the syntax.  We can deduce the correct syntax from the number of
     965 * arguments.
     966 */
     967    bool usageOK = true;
     968    const char *pszName = NULL;
     969    const char *pszValue = NULL;
     970    const char *pszFlags = NULL;
     971    if (2 == argc)
     972    {
     973        pszName = argv[0];
     974        pszValue = argv[1];
     975    }
     976    else if (3 == argc)
     977    {
     978        pszName = argv[0];
     979        if (strcmp(argv[1], "-flags") != 0)
     980            usageOK = false;
     981        pszFlags = argv[2];
     982    }
     983    else if (4 == argc)
     984    {
     985        pszName = argv[0];
     986        pszValue = argv[1];
     987        if (strcmp(argv[2], "-flags") != 0)
     988            usageOK = false;
     989        pszFlags = argv[3];
     990    }
     991    else if (argc != 1)
     992        usageOK = false;
     993    if (!usageOK)
     994    {
     995        usage(GUEST_PROP);
    919996        return 1;
    920997    }
    921998
    922     uint32_t u32ClientID = 0;
     999/*
     1000 * Do the actual setting.
     1001 */
     1002    uint32_t u32ClientId = 0;
    9231003    int rc = VINF_SUCCESS;
    924     char *pszName = argv[0];
    925     char *pszValue = NULL;
    926     if (2 == argc)
    927         pszValue = argv[1];
    928 
    929     rc = VbglR3GuestPropConnect(&u32ClientID);
     1004    rc = VbglR3GuestPropConnect(&u32ClientId);
    9301005    if (!RT_SUCCESS(rc))
    9311006        VBoxControlError("Failed to connect to the guest property service, error %Rrc\n", rc);
    9321007    if (RT_SUCCESS(rc))
    9331008    {
    934         rc = VbglR3GuestPropWriteValue(u32ClientID, pszName, pszValue);
     1009        if (pszFlags != NULL)
     1010            rc = VbglR3GuestPropWrite(u32ClientId, pszName, pszValue, pszFlags);
     1011        else
     1012            rc = VbglR3GuestPropWriteValue(u32ClientId, pszName, pszValue);
    9351013        if (!RT_SUCCESS(rc))
    9361014            VBoxControlError("Failed to store the property value, error %Rrc\n", rc);
    9371015    }
    938     if (u32ClientID != 0)
    939         VbglR3GuestPropDisconnect(u32ClientID);
     1016
     1017    if (u32ClientId != 0)
     1018        VbglR3GuestPropDisconnect(u32ClientId);
    9401019    return RT_SUCCESS(rc) ? 0 : 1;
    9411020}
     1021
     1022
     1023/**
     1024 * Access the guest property store through the "VBoxGuestPropSvc" HGCM
     1025 * service.
     1026 *
     1027 * @returns 0 on success, 1 on failure
     1028 * @note see the command line API description for parameters
     1029 */
     1030static int handleGuestProperty(int argc, char *argv[])
     1031{
     1032    if (0 == argc)
     1033    {
     1034        usage(GUEST_PROP);
     1035        return 1;
     1036    }
     1037    if (0 == strcmp(argv[0], "get"))
     1038        return getGuestProperty(argc - 1, argv + 1);
     1039    else if (0 == strcmp(argv[0], "set"))
     1040        return setGuestProperty(argc - 1, argv + 1);
     1041    /* else */
     1042    usage(GUEST_PROP);
     1043    return 1;
     1044}
     1045
    9421046#endif
    9431047
     
    9621066#endif
    9631067#ifdef VBOX_WITH_GUEST_PROPS
    964     { "getguestproperty", getGuestProperty },
    965     { "setguestproperty", setGuestProperty },
     1068    { "guestproperty", handleGuestProperty },
    9661069#endif
    9671070    { NULL, NULL }  /* terminator */
     
    9791082    /** Should we show the logo text? */
    9801083    bool showlogo = true;
    981     /** Should we print the usage after the logo?  For the --help switch. */
     1084    /** Should we print the usage after the logo?  For the -help switch. */
    9821085    bool dohelp = false;
    9831086    /** Will we be executing a command or just printing information? */
     
    10041107                done = true;
    10051108            }
    1006         else if (0 == strcmp(argv[iArg], "--nologo"))
     1109        else if (0 == strcmp(argv[iArg], "-nologo"))
    10071110            showlogo = false;
    1008         else if (0 == strcmp(argv[iArg], "--help"))
     1111        else if (0 == strcmp(argv[iArg], "-help"))
    10091112        {
    10101113            onlyinfo = true;
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