Changeset 10825 in vbox for trunk/src/VBox/Additions/common
- Timestamp:
- Jul 23, 2008 10:08:29 AM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Additions/common/VBoxControl/VBoxControl.cpp
r10797 r10825 77 77 #endif 78 78 #ifdef VBOX_WITH_GUEST_PROPS 79 GET_GUEST_PROP, 80 SET_GUEST_PROP, 79 GUEST_PROP, 81 80 #endif 82 81 USAGE_ALL = UINT32_MAX … … 86 85 { 87 86 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); 90 89 91 90 #ifdef RT_OS_WINDOWS … … 104 103 #endif 105 104 #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 } 110 110 #endif 111 111 } … … 866 866 * 867 867 * @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 869 869 */ 870 870 int getGuestProperty(int argc, char **argv) … … 872 872 using namespace guestProp; 873 873 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); 877 880 return 1; 878 881 } 879 882 880 uint32_t u32ClientI D= 0;883 uint32_t u32ClientId = 0; 881 884 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]; 883 905 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; 888 908 if (RT_SUCCESS(rc)) 889 909 { 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)) 892 932 VBoxControlError("Failed to retrieve the property value, error %Rrc\n", rc); 893 933 } 934 /* 935 * And display it on the guest console. 936 */ 894 937 if (VERR_NOT_FOUND == rc) 895 938 RTPrintf("No value set!\n"); 896 939 if (RT_SUCCESS(rc)) 897 940 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); 900 949 VbglR3GuestPropReadValueFree(pszValue); 901 950 return RT_SUCCESS(rc) ? 0 : 1; … … 908 957 * 909 958 * @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 913 960 */ 914 961 static int setGuestProperty(int argc, char *argv[]) 915 962 { 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); 919 996 return 1; 920 997 } 921 998 922 uint32_t u32ClientID = 0; 999 /* 1000 * Do the actual setting. 1001 */ 1002 uint32_t u32ClientId = 0; 923 1003 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); 930 1005 if (!RT_SUCCESS(rc)) 931 1006 VBoxControlError("Failed to connect to the guest property service, error %Rrc\n", rc); 932 1007 if (RT_SUCCESS(rc)) 933 1008 { 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); 935 1013 if (!RT_SUCCESS(rc)) 936 1014 VBoxControlError("Failed to store the property value, error %Rrc\n", rc); 937 1015 } 938 if (u32ClientID != 0) 939 VbglR3GuestPropDisconnect(u32ClientID); 1016 1017 if (u32ClientId != 0) 1018 VbglR3GuestPropDisconnect(u32ClientId); 940 1019 return RT_SUCCESS(rc) ? 0 : 1; 941 1020 } 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 */ 1030 static 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 942 1046 #endif 943 1047 … … 962 1066 #endif 963 1067 #ifdef VBOX_WITH_GUEST_PROPS 964 { "getguestproperty", getGuestProperty }, 965 { "setguestproperty", setGuestProperty }, 1068 { "guestproperty", handleGuestProperty }, 966 1069 #endif 967 1070 { NULL, NULL } /* terminator */ … … 979 1082 /** Should we show the logo text? */ 980 1083 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. */ 982 1085 bool dohelp = false; 983 1086 /** Will we be executing a command or just printing information? */ … … 1004 1107 done = true; 1005 1108 } 1006 else if (0 == strcmp(argv[iArg], "- -nologo"))1109 else if (0 == strcmp(argv[iArg], "-nologo")) 1007 1110 showlogo = false; 1008 else if (0 == strcmp(argv[iArg], "- -help"))1111 else if (0 == strcmp(argv[iArg], "-help")) 1009 1112 { 1010 1113 onlyinfo = true;
Note:
See TracChangeset
for help on using the changeset viewer.