VirtualBox

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

Last change on this file since 43421 was 42460, checked in by vboxsync, 12 years ago

VBoxManage: polished help

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 13.0 KB
Line 
1/* $Id: VBoxManageGuestProp.cpp 42460 2012-07-30 20:37:10Z 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, const char *pcszSep1, const char *pcszSep2)
56{
57 RTStrmPrintf(pStrm,
58 "%s guestproperty %s get <vmname>|<uuid>\n"
59 " <property> [--verbose]\n"
60 "\n", pcszSep1, pcszSep2);
61 RTStrmPrintf(pStrm,
62 "%s guestproperty %s set <vmname>|<uuid>\n"
63 " <property> [<value> [--flags <flags>]]\n"
64 "\n", pcszSep1, pcszSep2);
65 RTStrmPrintf(pStrm,
66 "%s guestproperty %s enumerate <vmname>|<uuid>\n"
67 " [--patterns <patterns>]\n"
68 "\n", pcszSep1, pcszSep2);
69 RTStrmPrintf(pStrm,
70 "%s guestproperty %s wait <vmname>|<uuid> <patterns>\n"
71 " [--timeout <msec>] [--fail-on-timeout]\n"
72 "\n", pcszSep1, pcszSep2);
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, DeleteGuestProperty(Bstr(pszName).raw()));
163 else if (!pszFlags)
164 CHECK_ERROR(machine, SetGuestPropertyValue(Bstr(pszName).raw(),
165 Bstr(pszValue).raw()));
166 else
167 CHECK_ERROR(machine, SetGuestProperty(Bstr(pszName).raw(),
168 Bstr(pszValue).raw(),
169 Bstr(pszFlags).raw()));
170
171 if (SUCCEEDED(rc))
172 CHECK_ERROR(machine, SaveSettings());
173
174 a->session->UnlockMachine();
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 */
185static int handleEnumGuestProperty(HandlerArg *a)
186{
187 /*
188 * Check the syntax. We can deduce the correct syntax from the number of
189 * arguments.
190 */
191 if ( a->argc < 1
192 || a->argc == 2
193 || ( a->argc > 3
194 && strcmp(a->argv[1], "--patterns")
195 && strcmp(a->argv[1], "-patterns")))
196 return errorSyntax(USAGE_GUESTPROPERTY, "Incorrect parameters");
197
198 /*
199 * Pack the patterns
200 */
201 Utf8Str Utf8Patterns(a->argc > 2 ? a->argv[2] : "");
202 for (int i = 3; i < a->argc; ++i)
203 Utf8Patterns = Utf8StrFmt ("%s,%s", Utf8Patterns.c_str(), a->argv[i]);
204
205 /*
206 * Make the actual call to Main.
207 */
208 ComPtr<IMachine> machine;
209 HRESULT rc;
210 CHECK_ERROR(a->virtualBox, FindMachine(Bstr(a->argv[0]).raw(),
211 machine.asOutParam()));
212 if (machine)
213 {
214 /* open a session for the VM - new or existing */
215 CHECK_ERROR_RET(machine, LockMachine(a->session, LockType_Shared), 1);
216
217 /* get the mutable session machine */
218 a->session->COMGETTER(Machine)(machine.asOutParam());
219
220 com::SafeArray<BSTR> names;
221 com::SafeArray<BSTR> values;
222 com::SafeArray<LONG64> timestamps;
223 com::SafeArray<BSTR> flags;
224 CHECK_ERROR(machine, EnumerateGuestProperties(Bstr(Utf8Patterns).raw(),
225 ComSafeArrayAsOutParam(names),
226 ComSafeArrayAsOutParam(values),
227 ComSafeArrayAsOutParam(timestamps),
228 ComSafeArrayAsOutParam(flags)));
229 if (SUCCEEDED(rc))
230 {
231 if (names.size() == 0)
232 RTPrintf("No properties found.\n");
233 for (unsigned i = 0; i < names.size(); ++i)
234 RTPrintf("Name: %ls, value: %ls, timestamp: %lld, flags: %ls\n",
235 names[i], values[i], timestamps[i], flags[i]);
236 }
237 }
238 return SUCCEEDED(rc) ? 0 : 1;
239}
240
241/**
242 * Enumerates the properties in the guest property store.
243 *
244 * @returns 0 on success, 1 on failure
245 * @note see the command line API description for parameters
246 */
247static int handleWaitGuestProperty(HandlerArg *a)
248{
249 /*
250 * Handle arguments
251 */
252 bool fFailOnTimeout = false;
253 const char *pszPatterns = NULL;
254 uint32_t cMsTimeout = RT_INDEFINITE_WAIT;
255 bool usageOK = true;
256 if (a->argc < 2)
257 usageOK = false;
258 else
259 pszPatterns = a->argv[1];
260 ComPtr<IMachine> machine;
261 HRESULT rc;
262 CHECK_ERROR(a->virtualBox, FindMachine(Bstr(a->argv[0]).raw(),
263 machine.asOutParam()));
264 if (!machine)
265 usageOK = false;
266 for (int i = 2; usageOK && i < a->argc; ++i)
267 {
268 if ( !strcmp(a->argv[i], "--timeout")
269 || !strcmp(a->argv[i], "-timeout"))
270 {
271 if ( i + 1 >= a->argc
272 || RTStrToUInt32Full(a->argv[i + 1], 10, &cMsTimeout) != VINF_SUCCESS)
273 usageOK = false;
274 else
275 ++i;
276 }
277 else if (!strcmp(a->argv[i], "--fail-on-timeout"))
278 fFailOnTimeout = true;
279 else
280 usageOK = false;
281 }
282 if (!usageOK)
283 return errorSyntax(USAGE_GUESTPROPERTY, "Incorrect parameters");
284
285 /*
286 * Set up the event listener and wait until found match or timeout.
287 */
288 Bstr aMachStrGuid;
289 machine->COMGETTER(Id)(aMachStrGuid.asOutParam());
290 Guid aMachGuid(aMachStrGuid);
291 ComPtr<IEventSource> es;
292 CHECK_ERROR(a->virtualBox, COMGETTER(EventSource)(es.asOutParam()));
293 ComPtr<IEventListener> listener;
294 CHECK_ERROR(es, CreateListener(listener.asOutParam()));
295 com::SafeArray <VBoxEventType_T> eventTypes(1);
296 eventTypes.push_back(VBoxEventType_OnGuestPropertyChanged);
297 CHECK_ERROR(es, RegisterListener(listener, ComSafeArrayAsInParam(eventTypes), false));
298
299 uint64_t u64Started = RTTimeMilliTS();
300 bool fSignalled = false;
301 do
302 {
303 unsigned cMsWait;
304 if (cMsTimeout == RT_INDEFINITE_WAIT)
305 cMsWait = 1000;
306 else
307 {
308 uint64_t cMsElapsed = RTTimeMilliTS() - u64Started;
309 if (cMsElapsed >= cMsTimeout)
310 break; /* timed out */
311 cMsWait = RT_MIN(1000, cMsTimeout - (uint32_t)cMsElapsed);
312 }
313
314 ComPtr<IEvent> ev;
315 rc = es->GetEvent(listener, cMsWait, ev.asOutParam());
316 if (ev)
317 {
318 VBoxEventType_T aType;
319 rc = ev->COMGETTER(Type)(&aType);
320 switch (aType)
321 {
322 case VBoxEventType_OnGuestPropertyChanged:
323 {
324 ComPtr<IGuestPropertyChangedEvent> gpcev = ev;
325 Assert(gpcev);
326 Bstr aNextStrGuid;
327 gpcev->COMGETTER(MachineId)(aNextStrGuid.asOutParam());
328 if (aMachGuid != Guid(aNextStrGuid))
329 continue;
330 Bstr aNextName;
331 gpcev->COMGETTER(Name)(aNextName.asOutParam());
332 if (RTStrSimplePatternMultiMatch(pszPatterns, RTSTR_MAX,
333 Utf8Str(aNextName).c_str(), RTSTR_MAX, NULL))
334 {
335 Bstr aNextValue, aNextFlags;
336 gpcev->COMGETTER(Value)(aNextValue.asOutParam());
337 gpcev->COMGETTER(Flags)(aNextFlags.asOutParam());
338 RTPrintf("Name: %ls, value: %ls, flags: %ls\n",
339 aNextName.raw(), aNextValue.raw(), aNextFlags.raw());
340 fSignalled = true;
341 }
342 break;
343 }
344 default:
345 AssertFailed();
346 }
347 }
348 } while (!fSignalled);
349
350 es->UnregisterListener(listener);
351
352 int rcRet = 0;
353 if (!fSignalled)
354 {
355 RTMsgError("Time out or interruption while waiting for a notification.");
356 if (fFailOnTimeout)
357 rcRet = 2;
358 }
359 return rcRet;
360}
361
362/**
363 * Access the guest property store.
364 *
365 * @returns 0 on success, 1 on failure
366 * @note see the command line API description for parameters
367 */
368int handleGuestProperty(HandlerArg *a)
369{
370 HandlerArg arg = *a;
371 arg.argc = a->argc - 1;
372 arg.argv = a->argv + 1;
373
374 /** @todo This command does not follow the syntax where the <uuid|vmname>
375 * comes between the command and subcommand. The commands controlvm,
376 * snapshot and debugvm puts it between.
377 */
378
379 if (a->argc == 0)
380 return errorSyntax(USAGE_GUESTPROPERTY, "Incorrect parameters");
381
382 /* switch (cmd) */
383 if (strcmp(a->argv[0], "get") == 0)
384 return handleGetGuestProperty(&arg);
385 if (strcmp(a->argv[0], "set") == 0)
386 return handleSetGuestProperty(&arg);
387 if (strcmp(a->argv[0], "enumerate") == 0)
388 return handleEnumGuestProperty(&arg);
389 if (strcmp(a->argv[0], "wait") == 0)
390 return handleWaitGuestProperty(&arg);
391
392 /* default: */
393 return errorSyntax(USAGE_GUESTPROPERTY, "Incorrect parameters");
394}
395
396#endif /* !VBOX_ONLY_DOCS */
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use