VirtualBox

source: vbox/trunk/src/VBox/Frontends/VBoxManage/VBoxManageGuestProp.cpp@ 35273

Last change on this file since 35273 was 33294, checked in by vboxsync, 14 years ago

Main: API change, merge IVirtualBox::getMachine() with findMachine()

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 12.7 KB
Line 
1/* $Id: VBoxManageGuestProp.cpp 33294 2010-10-21 10:45:26Z vboxsync $ */
2/** @file
3 * VBoxManage - Implementation of guestproperty command.
4 */
5
6/*
7 * Copyright (C) 2006-2010 Oracle Corporation
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
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#include "VBoxManage.h"
23
24#ifndef VBOX_ONLY_DOCS
25
26#include <VBox/com/com.h>
27#include <VBox/com/string.h>
28#include <VBox/com/array.h>
29#include <VBox/com/ErrorInfo.h>
30#include <VBox/com/errorprint.h>
31
32#include <VBox/com/VirtualBox.h>
33#include <VBox/com/EventQueue.h>
34
35#include <VBox/log.h>
36#include <iprt/asm.h>
37#include <iprt/stream.h>
38#include <iprt/string.h>
39#include <iprt/time.h>
40#include <iprt/thread.h>
41
42#ifdef USE_XPCOM_QUEUE
43# include <sys/select.h>
44# include <errno.h>
45#endif
46
47#ifdef RT_OS_DARWIN
48# include <CoreFoundation/CFRunLoop.h>
49#endif
50
51using namespace com;
52
53#endif /* !VBOX_ONLY_DOCS */
54
55void usageGuestProperty(PRTSTREAM pStrm)
56{
57 RTStrmPrintf(pStrm,
58 "VBoxManage guestproperty get <vmname>|<uuid>\n"
59 " <property> [--verbose]\n"
60 "\n");
61 RTStrmPrintf(pStrm,
62 "VBoxManage guestproperty set <vmname>|<uuid>\n"
63 " <property> [<value> [--flags <flags>]]\n"
64 "\n");
65 RTStrmPrintf(pStrm,
66 "VBoxManage guestproperty enumerate <vmname>|<uuid>\n"
67 " [--patterns <patterns>]\n"
68 "\n");
69 RTStrmPrintf(pStrm,
70 "VBoxManage guestproperty wait <vmname>|<uuid> <patterns>\n"
71 " [--timeout <msec>] [--fail-on-timeout]\n"
72 "\n");
73}
74
75#ifndef VBOX_ONLY_DOCS
76
77static int handleGetGuestProperty(HandlerArg *a)
78{
79 HRESULT rc = S_OK;
80
81 bool verbose = false;
82 if ( a->argc == 3
83 && ( !strcmp(a->argv[2], "--verbose")
84 || !strcmp(a->argv[2], "-verbose")))
85 verbose = true;
86 else if (a->argc != 2)
87 return errorSyntax(USAGE_GUESTPROPERTY, "Incorrect parameters");
88
89 ComPtr<IMachine> machine;
90 CHECK_ERROR(a->virtualBox, FindMachine(Bstr(a->argv[0]).raw(),
91 machine.asOutParam()));
92 if (machine)
93 {
94 /* open a session for the VM - new or existing */
95 CHECK_ERROR_RET(machine, LockMachine(a->session, LockType_Shared), 1);
96
97 /* get the mutable session machine */
98 a->session->COMGETTER(Machine)(machine.asOutParam());
99
100 Bstr value;
101 LONG64 i64Timestamp;
102 Bstr flags;
103 CHECK_ERROR(machine, GetGuestProperty(Bstr(a->argv[1]).raw(),
104 value.asOutParam(),
105 &i64Timestamp, flags.asOutParam()));
106 if (value.isEmpty())
107 RTPrintf("No value set!\n");
108 else
109 RTPrintf("Value: %lS\n", value.raw());
110 if (!value.isEmpty() && verbose)
111 {
112 RTPrintf("Timestamp: %lld\n", i64Timestamp);
113 RTPrintf("Flags: %lS\n", flags.raw());
114 }
115 }
116 return SUCCEEDED(rc) ? 0 : 1;
117}
118
119static int handleSetGuestProperty(HandlerArg *a)
120{
121 HRESULT rc = S_OK;
122
123 /*
124 * Check the syntax. We can deduce the correct syntax from the number of
125 * arguments.
126 */
127 bool usageOK = true;
128 const char *pszName = NULL;
129 const char *pszValue = NULL;
130 const char *pszFlags = NULL;
131 if (a->argc == 3)
132 pszValue = a->argv[2];
133 else if (a->argc == 4)
134 usageOK = false;
135 else if (a->argc == 5)
136 {
137 pszValue = a->argv[2];
138 if ( strcmp(a->argv[3], "--flags")
139 && strcmp(a->argv[3], "-flags"))
140 usageOK = false;
141 pszFlags = a->argv[4];
142 }
143 else if (a->argc != 2)
144 usageOK = false;
145 if (!usageOK)
146 return errorSyntax(USAGE_GUESTPROPERTY, "Incorrect parameters");
147 /* This is always needed. */
148 pszName = a->argv[1];
149
150 ComPtr<IMachine> machine;
151 CHECK_ERROR(a->virtualBox, FindMachine(Bstr(a->argv[0]).raw(),
152 machine.asOutParam()));
153 if (machine)
154 {
155 /* open a session for the VM - new or existing */
156 CHECK_ERROR_RET(machine, LockMachine(a->session, LockType_Shared), 1);
157
158 /* get the mutable session machine */
159 a->session->COMGETTER(Machine)(machine.asOutParam());
160
161 if (!pszValue && !pszFlags)
162 CHECK_ERROR(machine, SetGuestPropertyValue(Bstr(pszName).raw(),
163 Bstr().raw()));
164 else if (!pszFlags)
165 CHECK_ERROR(machine, SetGuestPropertyValue(Bstr(pszName).raw(),
166 Bstr(pszValue).raw()));
167 else
168 CHECK_ERROR(machine, SetGuestProperty(Bstr(pszName).raw(),
169 Bstr(pszValue).raw(),
170 Bstr(pszFlags).raw()));
171
172 if (SUCCEEDED(rc))
173 CHECK_ERROR(machine, SaveSettings());
174
175 a->session->UnlockMachine();
176 }
177 return SUCCEEDED(rc) ? 0 : 1;
178}
179
180/**
181 * Enumerates the properties in the guest property store.
182 *
183 * @returns 0 on success, 1 on failure
184 * @note see the command line API description for parameters
185 */
186static int handleEnumGuestProperty(HandlerArg *a)
187{
188 /*
189 * Check the syntax. We can deduce the correct syntax from the number of
190 * arguments.
191 */
192 if ( a->argc < 1
193 || a->argc == 2
194 || ( a->argc > 3
195 && strcmp(a->argv[1], "--patterns")
196 && strcmp(a->argv[1], "-patterns")))
197 return errorSyntax(USAGE_GUESTPROPERTY, "Incorrect parameters");
198
199 /*
200 * Pack the patterns
201 */
202 Utf8Str Utf8Patterns(a->argc > 2 ? a->argv[2] : "");
203 for (int i = 3; i < a->argc; ++i)
204 Utf8Patterns = Utf8StrFmt ("%s,%s", Utf8Patterns.c_str(), a->argv[i]);
205
206 /*
207 * Make the actual call to Main.
208 */
209 ComPtr<IMachine> machine;
210 HRESULT rc;
211 CHECK_ERROR(a->virtualBox, FindMachine(Bstr(a->argv[0]).raw(),
212 machine.asOutParam()));
213 if (machine)
214 {
215 /* open a session for the VM - new or existing */
216 CHECK_ERROR_RET(machine, LockMachine(a->session, LockType_Shared), 1);
217
218 /* get the mutable session machine */
219 a->session->COMGETTER(Machine)(machine.asOutParam());
220
221 com::SafeArray<BSTR> names;
222 com::SafeArray<BSTR> values;
223 com::SafeArray<LONG64> timestamps;
224 com::SafeArray<BSTR> flags;
225 CHECK_ERROR(machine, EnumerateGuestProperties(Bstr(Utf8Patterns).raw(),
226 ComSafeArrayAsOutParam(names),
227 ComSafeArrayAsOutParam(values),
228 ComSafeArrayAsOutParam(timestamps),
229 ComSafeArrayAsOutParam(flags)));
230 if (SUCCEEDED(rc))
231 {
232 if (names.size() == 0)
233 RTPrintf("No properties found.\n");
234 for (unsigned i = 0; i < names.size(); ++i)
235 RTPrintf("Name: %lS, value: %lS, timestamp: %lld, flags: %lS\n",
236 names[i], values[i], timestamps[i], flags[i]);
237 }
238 }
239 return SUCCEEDED(rc) ? 0 : 1;
240}
241
242/**
243 * Enumerates the properties in the guest property store.
244 *
245 * @returns 0 on success, 1 on failure
246 * @note see the command line API description for parameters
247 */
248static int handleWaitGuestProperty(HandlerArg *a)
249{
250 /*
251 * Handle arguments
252 */
253 bool fFailOnTimeout = false;
254 const char *pszPatterns = NULL;
255 uint32_t cMsTimeout = RT_INDEFINITE_WAIT;
256 bool usageOK = true;
257 if (a->argc < 2)
258 usageOK = false;
259 else
260 pszPatterns = a->argv[1];
261 ComPtr<IMachine> machine;
262 HRESULT rc;
263 CHECK_ERROR(a->virtualBox, FindMachine(Bstr(a->argv[0]).raw(),
264 machine.asOutParam()));
265 if (!machine)
266 usageOK = false;
267 for (int i = 2; usageOK && i < a->argc; ++i)
268 {
269 if ( !strcmp(a->argv[i], "--timeout")
270 || !strcmp(a->argv[i], "-timeout"))
271 {
272 if ( i + 1 >= a->argc
273 || RTStrToUInt32Full(a->argv[i + 1], 10, &cMsTimeout) != VINF_SUCCESS)
274 usageOK = false;
275 else
276 ++i;
277 }
278 else if (!strcmp(a->argv[i], "--fail-on-timeout"))
279 fFailOnTimeout = true;
280 else
281 usageOK = false;
282 }
283 if (!usageOK)
284 return errorSyntax(USAGE_GUESTPROPERTY, "Incorrect parameters");
285
286 /*
287 * Set up the event listener and wait until found match or timeout.
288 */
289 Bstr aMachStrGuid;
290 machine->COMGETTER(Id)(aMachStrGuid.asOutParam());
291 Guid aMachGuid(aMachStrGuid);
292 ComPtr<IEventSource> es;
293 CHECK_ERROR(a->virtualBox, COMGETTER(EventSource)(es.asOutParam()));
294 ComPtr<IEventListener> listener;
295 CHECK_ERROR(es, CreateListener(listener.asOutParam()));
296 com::SafeArray <VBoxEventType_T> eventTypes(1);
297 eventTypes.push_back(VBoxEventType_OnGuestPropertyChanged);
298 CHECK_ERROR(es, RegisterListener(listener, ComSafeArrayAsInParam(eventTypes), false));
299
300 uint64_t u64Started = RTTimeMilliTS();
301 bool fSignalled = false;
302 do
303 {
304 unsigned cMsWait;
305 if (cMsTimeout == RT_INDEFINITE_WAIT)
306 cMsWait = 1000;
307 else
308 {
309 uint64_t cMsElapsed = RTTimeMilliTS() - u64Started;
310 if (cMsElapsed >= cMsTimeout)
311 break; /* timed out */
312 cMsWait = RT_MIN(1000, cMsTimeout - (uint32_t)cMsElapsed);
313 }
314
315 ComPtr<IEvent> ev;
316 rc = es->GetEvent(listener, cMsWait, ev.asOutParam());
317 if (ev)
318 {
319 VBoxEventType_T aType;
320 rc = ev->COMGETTER(Type)(&aType);
321 switch (aType)
322 {
323 case VBoxEventType_OnGuestPropertyChanged:
324 {
325 ComPtr<IGuestPropertyChangedEvent> gpcev = ev;
326 Assert(gpcev);
327 Bstr aNextStrGuid;
328 gpcev->COMGETTER(MachineId)(aNextStrGuid.asOutParam());
329 if (aMachGuid != Guid(aNextStrGuid))
330 continue;
331 Bstr aNextName;
332 gpcev->COMGETTER(Name)(aNextName.asOutParam());
333 if (RTStrSimplePatternMultiMatch(pszPatterns, RTSTR_MAX,
334 Utf8Str(aNextName).c_str(), RTSTR_MAX, NULL))
335 {
336 Bstr aNextValue, aNextFlags;
337 gpcev->COMGETTER(Value)(aNextValue.asOutParam());
338 gpcev->COMGETTER(Flags)(aNextFlags.asOutParam());
339 RTPrintf("Name: %lS, value: %lS, flags: %lS\n",
340 aNextName.raw(), aNextValue.raw(), aNextFlags.raw());
341 fSignalled = true;
342 }
343 break;
344 }
345 default:
346 AssertFailed();
347 }
348 }
349 } while (!fSignalled);
350
351 es->UnregisterListener(listener);
352
353 int rcRet = 0;
354 if (!fSignalled)
355 {
356 RTMsgError("Time out or interruption while waiting for a notification.");
357 if (fFailOnTimeout)
358 rcRet = 2;
359 }
360 return rcRet;
361}
362
363/**
364 * Access the guest property store.
365 *
366 * @returns 0 on success, 1 on failure
367 * @note see the command line API description for parameters
368 */
369int handleGuestProperty(HandlerArg *a)
370{
371 HandlerArg arg = *a;
372 arg.argc = a->argc - 1;
373 arg.argv = a->argv + 1;
374
375 if (a->argc == 0)
376 return errorSyntax(USAGE_GUESTPROPERTY, "Incorrect parameters");
377
378 /* switch (cmd) */
379 if (strcmp(a->argv[0], "get") == 0)
380 return handleGetGuestProperty(&arg);
381 if (strcmp(a->argv[0], "set") == 0)
382 return handleSetGuestProperty(&arg);
383 if (strcmp(a->argv[0], "enumerate") == 0)
384 return handleEnumGuestProperty(&arg);
385 if (strcmp(a->argv[0], "wait") == 0)
386 return handleWaitGuestProperty(&arg);
387
388 /* default: */
389 return errorSyntax(USAGE_GUESTPROPERTY, "Incorrect parameters");
390}
391
392#endif /* !VBOX_ONLY_DOCS */
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use