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

© 2023 Oracle
ContactPrivacy policyTerms of Use