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 - new or existing */
|
---|
80 | if (FAILED (aVirtualBox->OpenSession(aSession, uuid)))
|
---|
81 | CHECK_ERROR_RET (aVirtualBox, OpenExistingSession(aSession, uuid), 1);
|
---|
82 |
|
---|
83 | /* get the mutable session machine */
|
---|
84 | aSession->COMGETTER(Machine)(machine.asOutParam());
|
---|
85 |
|
---|
86 | Bstr value;
|
---|
87 | uint64_t u64Timestamp;
|
---|
88 | Bstr flags;
|
---|
89 | CHECK_ERROR(machine, GetGuestProperty(Bstr(argv[1]), value.asOutParam(),
|
---|
90 | &u64Timestamp, flags.asOutParam()));
|
---|
91 | if (!value)
|
---|
92 | RTPrintf("No value set!\n");
|
---|
93 | if (value)
|
---|
94 | RTPrintf("Value: %lS\n", value.raw());
|
---|
95 | if (value && verbose)
|
---|
96 | {
|
---|
97 | RTPrintf("Timestamp: %lld\n", u64Timestamp);
|
---|
98 | RTPrintf("Flags: %lS\n", flags.raw());
|
---|
99 | }
|
---|
100 | }
|
---|
101 | return SUCCEEDED(rc) ? 0 : 1;
|
---|
102 | }
|
---|
103 |
|
---|
104 | static int handleSetGuestProperty(int argc, char *argv[],
|
---|
105 | ComPtr<IVirtualBox> aVirtualBox,
|
---|
106 | ComPtr<ISession> aSession)
|
---|
107 | {
|
---|
108 | HRESULT rc = S_OK;
|
---|
109 |
|
---|
110 | /*
|
---|
111 | * Check the syntax. We can deduce the correct syntax from the number of
|
---|
112 | * arguments.
|
---|
113 | */
|
---|
114 | bool usageOK = true;
|
---|
115 | const char *pszName = NULL;
|
---|
116 | const char *pszValue = NULL;
|
---|
117 | const char *pszFlags = NULL;
|
---|
118 | if (3 == argc)
|
---|
119 | {
|
---|
120 | pszValue = argv[2];
|
---|
121 | }
|
---|
122 | else if (4 == argc)
|
---|
123 | {
|
---|
124 | if (strcmp(argv[2], "-flags") != 0)
|
---|
125 | usageOK = false;
|
---|
126 | else
|
---|
127 | return errorSyntax(USAGE_GUESTPROPERTY,
|
---|
128 | "You may not specify flags without a value");
|
---|
129 | }
|
---|
130 | else if (5 == argc)
|
---|
131 | {
|
---|
132 | pszValue = argv[2];
|
---|
133 | if (strcmp(argv[3], "-flags") != 0)
|
---|
134 | usageOK = false;
|
---|
135 | pszFlags = argv[4];
|
---|
136 | }
|
---|
137 | else if (argc != 2)
|
---|
138 | usageOK = false;
|
---|
139 | if (!usageOK)
|
---|
140 | return errorSyntax(USAGE_GUESTPROPERTY, "Incorrect parameters");
|
---|
141 | /* This is always needed. */
|
---|
142 | pszName = argv[1];
|
---|
143 |
|
---|
144 | ComPtr<IMachine> machine;
|
---|
145 | /* assume it's a UUID */
|
---|
146 | rc = aVirtualBox->GetMachine(Guid(argv[0]), machine.asOutParam());
|
---|
147 | if (FAILED(rc) || !machine)
|
---|
148 | {
|
---|
149 | /* must be a name */
|
---|
150 | CHECK_ERROR(aVirtualBox, FindMachine(Bstr(argv[0]), machine.asOutParam()));
|
---|
151 | }
|
---|
152 | if (machine)
|
---|
153 | {
|
---|
154 | Guid uuid;
|
---|
155 | machine->COMGETTER(Id)(uuid.asOutParam());
|
---|
156 |
|
---|
157 | /* open a session for the VM - new or existing */
|
---|
158 | if (FAILED (aVirtualBox->OpenSession(aSession, uuid)))
|
---|
159 | CHECK_ERROR_RET (aVirtualBox, OpenExistingSession(aSession, uuid), 1);
|
---|
160 |
|
---|
161 | /* get the mutable session machine */
|
---|
162 | aSession->COMGETTER(Machine)(machine.asOutParam());
|
---|
163 |
|
---|
164 | if ((NULL == pszValue) && (NULL == pszFlags))
|
---|
165 | CHECK_ERROR(machine, SetGuestPropertyValue(Bstr(pszName), NULL));
|
---|
166 | else if (NULL == pszFlags)
|
---|
167 | CHECK_ERROR(machine, SetGuestPropertyValue(Bstr(pszName), Bstr(pszValue)));
|
---|
168 | else if (NULL == pszValue)
|
---|
169 | CHECK_ERROR(machine, SetGuestProperty(Bstr(pszName), NULL, Bstr(pszFlags)));
|
---|
170 | else
|
---|
171 | CHECK_ERROR(machine, SetGuestProperty(Bstr(pszName), Bstr(pszValue), Bstr(pszFlags)));
|
---|
172 |
|
---|
173 | if (SUCCEEDED(rc))
|
---|
174 | CHECK_ERROR(machine, SaveSettings());
|
---|
175 |
|
---|
176 | aSession->Close();
|
---|
177 | }
|
---|
178 | return SUCCEEDED(rc) ? 0 : 1;
|
---|
179 | }
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * Enumerates the properties in the guest property store.
|
---|
183 | *
|
---|
184 | * @returns 0 on success, 1 on failure
|
---|
185 | * @note see the command line API description for parameters
|
---|
186 | */
|
---|
187 | static int handleEnumGuestProperty(int argc, char *argv[],
|
---|
188 | ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
|
---|
189 | {
|
---|
190 | /*
|
---|
191 | * Check the syntax. We can deduce the correct syntax from the number of
|
---|
192 | * arguments.
|
---|
193 | */
|
---|
194 | if ((argc < 1) || (2 == argc) ||
|
---|
195 | ((argc > 3) && strcmp(argv[1], "-patterns") != 0))
|
---|
196 | return errorSyntax(USAGE_GUESTPROPERTY, "Incorrect parameters");
|
---|
197 |
|
---|
198 | /*
|
---|
199 | * Pack the patterns
|
---|
200 | */
|
---|
201 | Utf8Str Utf8Patterns(argc > 2 ? argv[2] : "");
|
---|
202 | for (ssize_t i = 3; i < argc; ++i)
|
---|
203 | Utf8Patterns = Utf8StrFmt ("%s,%s", Utf8Patterns.raw(), argv[i]);
|
---|
204 |
|
---|
205 | /*
|
---|
206 | * Make the actual call to Main.
|
---|
207 | */
|
---|
208 | ComPtr<IMachine> machine;
|
---|
209 | /* assume it's a UUID */
|
---|
210 | HRESULT rc = aVirtualBox->GetMachine(Guid(argv[0]), machine.asOutParam());
|
---|
211 | if (FAILED(rc) || !machine)
|
---|
212 | {
|
---|
213 | /* must be a name */
|
---|
214 | CHECK_ERROR(aVirtualBox, FindMachine(Bstr(argv[0]), machine.asOutParam()));
|
---|
215 | }
|
---|
216 | if (machine)
|
---|
217 | {
|
---|
218 | Guid uuid;
|
---|
219 | machine->COMGETTER(Id)(uuid.asOutParam());
|
---|
220 |
|
---|
221 | /* open a session for the VM - new or existing */
|
---|
222 | if (FAILED (aVirtualBox->OpenSession(aSession, uuid)))
|
---|
223 | CHECK_ERROR_RET (aVirtualBox, OpenExistingSession(aSession, uuid), 1);
|
---|
224 |
|
---|
225 | /* get the mutable session machine */
|
---|
226 | aSession->COMGETTER(Machine)(machine.asOutParam());
|
---|
227 |
|
---|
228 | com::SafeArray <BSTR> names;
|
---|
229 | com::SafeArray <BSTR> values;
|
---|
230 | com::SafeArray <ULONG64> timestamps;
|
---|
231 | com::SafeArray <BSTR> flags;
|
---|
232 | CHECK_ERROR(machine, EnumerateGuestProperties(Bstr(Utf8Patterns),
|
---|
233 | ComSafeArrayAsOutParam(names),
|
---|
234 | ComSafeArrayAsOutParam(values),
|
---|
235 | ComSafeArrayAsOutParam(timestamps),
|
---|
236 | ComSafeArrayAsOutParam(flags)));
|
---|
237 | if (SUCCEEDED(rc))
|
---|
238 | {
|
---|
239 | if (names.size() == 0)
|
---|
240 | RTPrintf("No properties found.\n");
|
---|
241 | for (unsigned i = 0; i < names.size(); ++i)
|
---|
242 | RTPrintf("Name: %lS, value: %lS, timestamp: %lld, flags: %lS\n",
|
---|
243 | names[i], values[i], timestamps[i], flags[i]);
|
---|
244 | }
|
---|
245 | }
|
---|
246 | return SUCCEEDED(rc) ? 0 : 1;
|
---|
247 | }
|
---|
248 |
|
---|
249 | /**
|
---|
250 | * Access the guest property store.
|
---|
251 | *
|
---|
252 | * @returns 0 on success, 1 on failure
|
---|
253 | * @note see the command line API description for parameters
|
---|
254 | */
|
---|
255 | int handleGuestProperty(int argc, char *argv[],
|
---|
256 | ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
|
---|
257 | {
|
---|
258 | if (0 == argc)
|
---|
259 | return errorSyntax(USAGE_GUESTPROPERTY, "Incorrect parameters");
|
---|
260 | if (0 == strcmp(argv[0], "get"))
|
---|
261 | return handleGetGuestProperty(argc - 1, argv + 1, aVirtualBox, aSession);
|
---|
262 | else if (0 == strcmp(argv[0], "set"))
|
---|
263 | return handleSetGuestProperty(argc - 1, argv + 1, aVirtualBox, aSession);
|
---|
264 | else if (0 == strcmp(argv[0], "enumerate"))
|
---|
265 | return handleEnumGuestProperty(argc - 1, argv + 1, aVirtualBox, aSession);
|
---|
266 | /* else */
|
---|
267 | return errorSyntax(USAGE_GUESTPROPERTY, "Incorrect parameters");
|
---|
268 | }
|
---|
269 |
|
---|