1 | /** @file
|
---|
2 | *
|
---|
3 | * VBox frontends: VBoxManage (command-line interface), Guest Properties
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 |
|
---|
22 |
|
---|
23 | /*******************************************************************************
|
---|
24 | * Header Files *
|
---|
25 | *******************************************************************************/
|
---|
26 |
|
---|
27 | #include <VBox/com/com.h>
|
---|
28 | #include <VBox/com/string.h>
|
---|
29 | #include <VBox/com/array.h>
|
---|
30 | #include <VBox/com/ErrorInfo.h>
|
---|
31 |
|
---|
32 | #include <VBox/com/VirtualBox.h>
|
---|
33 |
|
---|
34 | #include <iprt/stream.h>
|
---|
35 | #include <VBox/log.h>
|
---|
36 |
|
---|
37 | #include "VBoxManage.h"
|
---|
38 |
|
---|
39 | using namespace com;
|
---|
40 |
|
---|
41 | void usageGuestProperty(void)
|
---|
42 | {
|
---|
43 | RTPrintf("VBoxManage guestproperty get <vmname>|<uuid>\n"
|
---|
44 | " <property> [-verbose]\n"
|
---|
45 | "\n");
|
---|
46 | RTPrintf("VBoxManage guestproperty set <vmname>|<uuid>\n"
|
---|
47 | " <property> [<value> [-flags <flags>]]\n"
|
---|
48 | "\n");
|
---|
49 | RTPrintf("VBoxManage guestproperty enumerate <vmname>|<uuid>\n"
|
---|
50 | " [-patterns <patterns>]\n"
|
---|
51 | "\n");
|
---|
52 | }
|
---|
53 |
|
---|
54 | static int handleGetGuestProperty(int argc, char *argv[],
|
---|
55 | ComPtr<IVirtualBox> aVirtualBox,
|
---|
56 | ComPtr<ISession> aSession)
|
---|
57 | {
|
---|
58 | HRESULT rc = S_OK;
|
---|
59 |
|
---|
60 | bool verbose = false;
|
---|
61 | if ((3 == argc) && (0 == strcmp(argv[2], "-verbose")))
|
---|
62 | verbose = true;
|
---|
63 | else if (argc != 2)
|
---|
64 | return errorSyntax(USAGE_GUESTPROPERTY, "Incorrect parameters");
|
---|
65 |
|
---|
66 | ComPtr<IMachine> machine;
|
---|
67 | /* assume it's a UUID */
|
---|
68 | rc = aVirtualBox->GetMachine(Guid(argv[0]), machine.asOutParam());
|
---|
69 | if (FAILED(rc) || !machine)
|
---|
70 | {
|
---|
71 | /* must be a name */
|
---|
72 | CHECK_ERROR(aVirtualBox, FindMachine(Bstr(argv[0]), machine.asOutParam()));
|
---|
73 | }
|
---|
74 | if (machine)
|
---|
75 | {
|
---|
76 | Guid uuid;
|
---|
77 | machine->COMGETTER(Id)(uuid.asOutParam());
|
---|
78 |
|
---|
79 | /* open a session for the VM */
|
---|
80 | CHECK_ERROR_RET (aVirtualBox, OpenSession(aSession, uuid), 1);
|
---|
81 |
|
---|
82 | /* get the mutable session machine */
|
---|
83 | aSession->COMGETTER(Machine)(machine.asOutParam());
|
---|
84 |
|
---|
85 | Bstr value;
|
---|
86 | uint64_t u64Timestamp;
|
---|
87 | Bstr flags;
|
---|
88 | CHECK_ERROR(machine, GetGuestProperty(Bstr(argv[1]), value.asOutParam(),
|
---|
89 | &u64Timestamp, flags.asOutParam()));
|
---|
90 | if (!value)
|
---|
91 | RTPrintf("No value set!\n");
|
---|
92 | if (value)
|
---|
93 | RTPrintf("Value: %lS\n", value.raw());
|
---|
94 | if (value && verbose)
|
---|
95 | {
|
---|
96 | RTPrintf("Timestamp: %lld\n", u64Timestamp);
|
---|
97 | RTPrintf("Flags: %lS\n", flags.raw());
|
---|
98 | }
|
---|
99 | }
|
---|
100 | return SUCCEEDED(rc) ? 0 : 1;
|
---|
101 | }
|
---|
102 |
|
---|
103 | static int handleSetGuestProperty(int argc, char *argv[],
|
---|
104 | ComPtr<IVirtualBox> aVirtualBox,
|
---|
105 | ComPtr<ISession> aSession)
|
---|
106 | {
|
---|
107 | HRESULT rc = S_OK;
|
---|
108 |
|
---|
109 | /*
|
---|
110 | * Check the syntax. We can deduce the correct syntax from the number of
|
---|
111 | * arguments.
|
---|
112 | */
|
---|
113 | bool usageOK = true;
|
---|
114 | const char *pszName = NULL;
|
---|
115 | const char *pszValue = NULL;
|
---|
116 | const char *pszFlags = NULL;
|
---|
117 | if (3 == argc)
|
---|
118 | {
|
---|
119 | pszValue = argv[2];
|
---|
120 | }
|
---|
121 | else if (4 == argc)
|
---|
122 | {
|
---|
123 | if (strcmp(argv[2], "-flags") != 0)
|
---|
124 | usageOK = false;
|
---|
125 | else
|
---|
126 | return errorSyntax(USAGE_GUESTPROPERTY,
|
---|
127 | "You may not specify flags without a value");
|
---|
128 | }
|
---|
129 | else if (5 == argc)
|
---|
130 | {
|
---|
131 | pszValue = argv[2];
|
---|
132 | if (strcmp(argv[3], "-flags") != 0)
|
---|
133 | usageOK = false;
|
---|
134 | pszFlags = argv[4];
|
---|
135 | }
|
---|
136 | else if (argc != 2)
|
---|
137 | usageOK = false;
|
---|
138 | if (!usageOK)
|
---|
139 | return errorSyntax(USAGE_GUESTPROPERTY, "Incorrect parameters");
|
---|
140 | /* This is always needed. */
|
---|
141 | pszName = argv[1];
|
---|
142 |
|
---|
143 | ComPtr<IMachine> machine;
|
---|
144 | /* assume it's a UUID */
|
---|
145 | rc = aVirtualBox->GetMachine(Guid(argv[0]), machine.asOutParam());
|
---|
146 | if (FAILED(rc) || !machine)
|
---|
147 | {
|
---|
148 | /* must be a name */
|
---|
149 | CHECK_ERROR(aVirtualBox, FindMachine(Bstr(argv[0]), machine.asOutParam()));
|
---|
150 | }
|
---|
151 | if (machine)
|
---|
152 | {
|
---|
153 | Guid uuid;
|
---|
154 | machine->COMGETTER(Id)(uuid.asOutParam());
|
---|
155 |
|
---|
156 | /* open a session for the VM */
|
---|
157 | CHECK_ERROR_RET (aVirtualBox, OpenSession(aSession, uuid), 1);
|
---|
158 |
|
---|
159 | /* get the mutable session machine */
|
---|
160 | aSession->COMGETTER(Machine)(machine.asOutParam());
|
---|
161 |
|
---|
162 | if ((NULL == pszValue) && (NULL == pszFlags))
|
---|
163 | CHECK_ERROR(machine, SetGuestPropertyValue(Bstr(pszName), NULL));
|
---|
164 | else if (NULL == pszFlags)
|
---|
165 | CHECK_ERROR(machine, SetGuestPropertyValue(Bstr(pszName), Bstr(pszValue)));
|
---|
166 | else if (NULL == pszValue)
|
---|
167 | CHECK_ERROR(machine, SetGuestProperty(Bstr(pszName), NULL, Bstr(pszFlags)));
|
---|
168 | else
|
---|
169 | CHECK_ERROR(machine, SetGuestProperty(Bstr(pszName), Bstr(pszValue), Bstr(pszFlags)));
|
---|
170 |
|
---|
171 | if (SUCCEEDED(rc))
|
---|
172 | CHECK_ERROR(machine, SaveSettings());
|
---|
173 |
|
---|
174 | aSession->Close();
|
---|
175 | }
|
---|
176 | return SUCCEEDED(rc) ? 0 : 1;
|
---|
177 | }
|
---|
178 |
|
---|
179 | /**
|
---|
180 | * Enumerates the properties in the guest property store.
|
---|
181 | *
|
---|
182 | * @returns 0 on success, 1 on failure
|
---|
183 | * @note see the command line API description for parameters
|
---|
184 | */
|
---|
185 | static int handleEnumGuestProperty(int argc, char *argv[],
|
---|
186 | ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
|
---|
187 | {
|
---|
188 | /*
|
---|
189 | * Check the syntax. We can deduce the correct syntax from the number of
|
---|
190 | * arguments.
|
---|
191 | */
|
---|
192 | if ((argc < 1) || (2 == argc) ||
|
---|
193 | ((argc > 3) && strcmp(argv[1], "-patterns") != 0))
|
---|
194 | return errorSyntax(USAGE_GUESTPROPERTY, "Incorrect parameters");
|
---|
195 |
|
---|
196 | /*
|
---|
197 | * Pack the patterns
|
---|
198 | */
|
---|
199 | Utf8Str Utf8Patterns(argc > 2 ? argv[2] : "");
|
---|
200 | for (ssize_t i = 3; i < argc; ++i)
|
---|
201 | Utf8Patterns = Utf8StrFmt ("%s,%s", Utf8Patterns.raw(), argv[i]);
|
---|
202 |
|
---|
203 | /*
|
---|
204 | * Make the actual call to Main.
|
---|
205 | */
|
---|
206 | ComPtr<IMachine> machine;
|
---|
207 | /* assume it's a UUID */
|
---|
208 | HRESULT rc = aVirtualBox->GetMachine(Guid(argv[0]), machine.asOutParam());
|
---|
209 | if (FAILED(rc) || !machine)
|
---|
210 | {
|
---|
211 | /* must be a name */
|
---|
212 | CHECK_ERROR(aVirtualBox, FindMachine(Bstr(argv[0]), machine.asOutParam()));
|
---|
213 | }
|
---|
214 | if (machine)
|
---|
215 | {
|
---|
216 | Guid uuid;
|
---|
217 | machine->COMGETTER(Id)(uuid.asOutParam());
|
---|
218 |
|
---|
219 | /* open a session for the VM */
|
---|
220 | CHECK_ERROR_RET (aVirtualBox, OpenSession(aSession, uuid), 1);
|
---|
221 |
|
---|
222 | /* get the mutable session machine */
|
---|
223 | aSession->COMGETTER(Machine)(machine.asOutParam());
|
---|
224 |
|
---|
225 | com::SafeArray <BSTR> names;
|
---|
226 | com::SafeArray <BSTR> values;
|
---|
227 | com::SafeArray <ULONG64> timestamps;
|
---|
228 | com::SafeArray <BSTR> flags;
|
---|
229 | CHECK_ERROR(machine, EnumerateGuestProperties(Bstr(Utf8Patterns),
|
---|
230 | ComSafeArrayAsOutParam(names),
|
---|
231 | ComSafeArrayAsOutParam(values),
|
---|
232 | ComSafeArrayAsOutParam(timestamps),
|
---|
233 | ComSafeArrayAsOutParam(flags)));
|
---|
234 | if (SUCCEEDED(rc))
|
---|
235 | {
|
---|
236 | if (names.size() == 0)
|
---|
237 | RTPrintf("No properties found.\n");
|
---|
238 | for (unsigned i = 0; i < names.size(); ++i)
|
---|
239 | RTPrintf("Name: %lS, value: %lS, timestamp: %lld, flags: %lS\n",
|
---|
240 | names[i], values[i], timestamps[i], flags[i]);
|
---|
241 | }
|
---|
242 | }
|
---|
243 | return SUCCEEDED(rc) ? 0 : 1;
|
---|
244 | }
|
---|
245 |
|
---|
246 | /**
|
---|
247 | * Access the guest property store.
|
---|
248 | *
|
---|
249 | * @returns 0 on success, 1 on failure
|
---|
250 | * @note see the command line API description for parameters
|
---|
251 | */
|
---|
252 | int handleGuestProperty(int argc, char *argv[],
|
---|
253 | ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
|
---|
254 | {
|
---|
255 | if (0 == argc)
|
---|
256 | return errorSyntax(USAGE_GUESTPROPERTY, "Incorrect parameters");
|
---|
257 | if (0 == strcmp(argv[0], "get"))
|
---|
258 | return handleGetGuestProperty(argc - 1, argv + 1, aVirtualBox, aSession);
|
---|
259 | else if (0 == strcmp(argv[0], "set"))
|
---|
260 | return handleSetGuestProperty(argc - 1, argv + 1, aVirtualBox, aSession);
|
---|
261 | else if (0 == strcmp(argv[0], "enumerate"))
|
---|
262 | return handleEnumGuestProperty(argc - 1, argv + 1, aVirtualBox, aSession);
|
---|
263 | /* else */
|
---|
264 | return errorSyntax(USAGE_GUESTPROPERTY, "Incorrect parameters");
|
---|
265 | }
|
---|
266 |
|
---|