VirtualBox

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

Last change on this file since 20221 was 19239, checked in by vboxsync, 16 years ago

Main: support for using VBox from Python on Windows (still certain limitation apply, such as enum visibility)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 15.9 KB
Line 
1/* $Id: VBoxManageGuestProp.cpp 19239 2009-04-28 13:19:14Z vboxsync $ */
2/** @file
3 * VBoxManage - The 'guestproperty' command.
4 */
5
6/*
7 * Copyright (C) 2006-2008 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#include "VBoxManage.h"
27
28#include <VBox/com/com.h>
29#include <VBox/com/string.h>
30#include <VBox/com/array.h>
31#include <VBox/com/ErrorInfo.h>
32#include <VBox/com/errorprint2.h>
33
34#include <VBox/com/VirtualBox.h>
35
36#include <VBox/log.h>
37#include <iprt/stream.h>
38#include <iprt/thread.h>
39#include <iprt/time.h>
40
41#ifdef USE_XPCOM_QUEUE
42# include <sys/select.h>
43#endif
44
45using namespace com;
46
47/**
48 * IVirtualBoxCallback implementation for handling the GuestPropertyCallback in
49 * relation to the "guestproperty wait" command.
50 */
51class GuestPropertyCallback :
52 VBOX_SCRIPTABLE_IMPL(IVirtualBoxCallback)
53{
54public:
55 GuestPropertyCallback(const char *pszPatterns, Guid aUuid)
56 : mSignalled(false), mPatterns(pszPatterns), mUuid(aUuid)
57 {
58#ifndef VBOX_WITH_XPCOM
59 refcnt = 0;
60#endif
61 }
62
63 virtual ~GuestPropertyCallback() {}
64
65#ifndef VBOX_WITH_XPCOM
66 STDMETHOD_(ULONG, AddRef)()
67 {
68 return ::InterlockedIncrement(&refcnt);
69 }
70 STDMETHOD_(ULONG, Release)()
71 {
72 long cnt = ::InterlockedDecrement(&refcnt);
73 if (cnt == 0)
74 delete this;
75 return cnt;
76 }
77 STDMETHOD(QueryInterface)(REFIID riid , void **ppObj)
78 {
79 if (riid == IID_IUnknown)
80 {
81 *ppObj = this;
82 AddRef();
83 return S_OK;
84 }
85 if (riid == IID_IVirtualBoxCallback)
86 {
87 *ppObj = this;
88 AddRef();
89 return S_OK;
90 }
91 *ppObj = NULL;
92 return E_NOINTERFACE;
93 }
94#endif /* !VBOX_WITH_XPCOM */
95
96 NS_DECL_ISUPPORTS
97
98 STDMETHOD(OnMachineStateChange)(IN_BSTR machineId,
99 MachineState_T state)
100 {
101 return S_OK;
102 }
103
104 STDMETHOD(OnMachineDataChange)(IN_BSTR machineId)
105 {
106 return S_OK;
107 }
108
109 STDMETHOD(OnExtraDataCanChange)(IN_BSTR machineId, IN_BSTR key,
110 IN_BSTR value, BSTR *error,
111 BOOL *changeAllowed)
112 {
113 /* we never disagree */
114 if (!changeAllowed)
115 return E_INVALIDARG;
116 *changeAllowed = TRUE;
117 return S_OK;
118 }
119
120 STDMETHOD(OnExtraDataChange)(IN_BSTR machineId, IN_BSTR key,
121 IN_BSTR value)
122 {
123 return S_OK;
124 }
125
126 STDMETHOD(OnMediaRegistered)(IN_BSTR mediaId,
127 DeviceType_T mediaType, BOOL registered)
128 {
129 NOREF(mediaId);
130 NOREF(mediaType);
131 NOREF(registered);
132 return S_OK;
133 }
134
135 STDMETHOD(OnMachineRegistered)(IN_BSTR machineId, BOOL registered)
136 {
137 return S_OK;
138 }
139
140 STDMETHOD(OnSessionStateChange)(IN_BSTR machineId,
141 SessionState_T state)
142 {
143 return S_OK;
144 }
145
146 STDMETHOD(OnSnapshotTaken)(IN_BSTR aMachineId,
147 IN_BSTR aSnapshotId)
148 {
149 return S_OK;
150 }
151
152 STDMETHOD(OnSnapshotDiscarded)(IN_BSTR aMachineId,
153 IN_BSTR aSnapshotId)
154 {
155 return S_OK;
156 }
157
158 STDMETHOD(OnSnapshotChange)(IN_BSTR aMachineId,
159 IN_BSTR aSnapshotId)
160 {
161 return S_OK;
162 }
163
164 STDMETHOD(OnGuestPropertyChange)(IN_BSTR machineId,
165 IN_BSTR name, IN_BSTR value,
166 IN_BSTR flags)
167 {
168 HRESULT rc = S_OK;
169 Utf8Str utf8Name (name);
170 Guid uuid(machineId);
171 if (utf8Name.isNull())
172 rc = E_OUTOFMEMORY;
173 if ( SUCCEEDED (rc)
174 && uuid == mUuid
175 && RTStrSimplePatternMultiMatch(mPatterns, RTSTR_MAX,
176 utf8Name.raw(), RTSTR_MAX, NULL))
177 {
178 RTPrintf("Name: %lS, value: %lS, flags: %lS\n", name, value,
179 flags);
180 mSignalled = true;
181 }
182 return rc;
183 }
184
185 bool Signalled(void) { return mSignalled; }
186
187private:
188 bool mSignalled;
189 const char *mPatterns;
190 Guid mUuid;
191#ifndef VBOX_WITH_XPCOM
192 long refcnt;
193#endif
194
195};
196
197#ifdef VBOX_WITH_XPCOM
198NS_DECL_CLASSINFO(GuestPropertyCallback)
199NS_IMPL_ISUPPORTS1_CI(GuestPropertyCallback, IVirtualBoxCallback)
200#endif /* VBOX_WITH_XPCOM */
201
202void usageGuestProperty(void)
203{
204 RTPrintf("VBoxManage guestproperty get <vmname>|<uuid>\n"
205 " <property> [--verbose]\n"
206 "\n");
207 RTPrintf("VBoxManage guestproperty set <vmname>|<uuid>\n"
208 " <property> [<value> [--flags <flags>]]\n"
209 "\n");
210 RTPrintf("VBoxManage guestproperty enumerate <vmname>|<uuid>\n"
211 " [--patterns <patterns>]\n"
212 "\n");
213 RTPrintf("VBoxManage guestproperty wait <vmname>|<uuid> <patterns>\n"
214 " [--timeout <timeout>]\n"
215 "\n");
216}
217
218static int handleGetGuestProperty(HandlerArg *a)
219{
220 HRESULT rc = S_OK;
221
222 bool verbose = false;
223 if ( a->argc == 3
224 && ( !strcmp(a->argv[2], "--verbose")
225 || !strcmp(a->argv[2], "-verbose")))
226 verbose = true;
227 else if (a->argc != 2)
228 return errorSyntax(USAGE_GUESTPROPERTY, "Incorrect parameters");
229
230 ComPtr<IMachine> machine;
231 /* assume it's a UUID */
232 rc = a->virtualBox->GetMachine(Bstr(a->argv[0]), machine.asOutParam());
233 if (FAILED(rc) || !machine)
234 {
235 /* must be a name */
236 CHECK_ERROR(a->virtualBox, FindMachine(Bstr(a->argv[0]), machine.asOutParam()));
237 }
238 if (machine)
239 {
240 Bstr uuid;
241 machine->COMGETTER(Id)(uuid.asOutParam());
242
243 /* open a session for the VM - new or existing */
244 if (FAILED (a->virtualBox->OpenSession(a->session, uuid)))
245 CHECK_ERROR_RET(a->virtualBox, OpenExistingSession(a->session, uuid), 1);
246
247 /* get the mutable session machine */
248 a->session->COMGETTER(Machine)(machine.asOutParam());
249
250 Bstr value;
251 ULONG64 u64Timestamp;
252 Bstr flags;
253 CHECK_ERROR(machine, GetGuestProperty(Bstr(a->argv[1]), value.asOutParam(),
254 &u64Timestamp, flags.asOutParam()));
255 if (!value)
256 RTPrintf("No value set!\n");
257 if (value)
258 RTPrintf("Value: %lS\n", value.raw());
259 if (value && verbose)
260 {
261 RTPrintf("Timestamp: %lld\n", u64Timestamp);
262 RTPrintf("Flags: %lS\n", flags.raw());
263 }
264 }
265 return SUCCEEDED(rc) ? 0 : 1;
266}
267
268static int handleSetGuestProperty(HandlerArg *a)
269{
270 HRESULT rc = S_OK;
271
272 /*
273 * Check the syntax. We can deduce the correct syntax from the number of
274 * arguments.
275 */
276 bool usageOK = true;
277 const char *pszName = NULL;
278 const char *pszValue = NULL;
279 const char *pszFlags = NULL;
280 if (a->argc == 3)
281 pszValue = a->argv[2];
282 else if (a->argc == 4)
283 usageOK = false;
284 else if (a->argc == 5)
285 {
286 pszValue = a->argv[2];
287 if ( !strcmp(a->argv[3], "--flags")
288 || !strcmp(a->argv[3], "-flags"))
289 usageOK = false;
290 pszFlags = a->argv[4];
291 }
292 else if (a->argc != 2)
293 usageOK = false;
294 if (!usageOK)
295 return errorSyntax(USAGE_GUESTPROPERTY, "Incorrect parameters");
296 /* This is always needed. */
297 pszName = a->argv[1];
298
299 ComPtr<IMachine> machine;
300 /* assume it's a UUID */
301 rc = a->virtualBox->GetMachine(Bstr(a->argv[0]), machine.asOutParam());
302 if (FAILED(rc) || !machine)
303 {
304 /* must be a name */
305 CHECK_ERROR(a->virtualBox, FindMachine(Bstr(a->argv[0]), machine.asOutParam()));
306 }
307 if (machine)
308 {
309 Bstr uuid;
310 machine->COMGETTER(Id)(uuid.asOutParam());
311
312 /* open a session for the VM - new or existing */
313 if (FAILED (a->virtualBox->OpenSession(a->session, uuid)))
314 CHECK_ERROR_RET (a->virtualBox, OpenExistingSession(a->session, uuid), 1);
315
316 /* get the mutable session machine */
317 a->session->COMGETTER(Machine)(machine.asOutParam());
318
319 if (!pszValue && !pszFlags)
320 CHECK_ERROR(machine, SetGuestPropertyValue(Bstr(pszName), NULL));
321 else if (!pszFlags)
322 CHECK_ERROR(machine, SetGuestPropertyValue(Bstr(pszName), Bstr(pszValue)));
323 else
324 CHECK_ERROR(machine, SetGuestProperty(Bstr(pszName), Bstr(pszValue), Bstr(pszFlags)));
325
326 if (SUCCEEDED(rc))
327 CHECK_ERROR(machine, SaveSettings());
328
329 a->session->Close();
330 }
331 return SUCCEEDED(rc) ? 0 : 1;
332}
333
334/**
335 * Enumerates the properties in the guest property store.
336 *
337 * @returns 0 on success, 1 on failure
338 * @note see the command line API description for parameters
339 */
340static int handleEnumGuestProperty(HandlerArg *a)
341{
342 /*
343 * Check the syntax. We can deduce the correct syntax from the number of
344 * arguments.
345 */
346 if ( a->argc < 1
347 || a->argc == 2
348 || ( a->argc > 3
349 && strcmp(a->argv[1], "--patterns")
350 && strcmp(a->argv[1], "-patterns")))
351 return errorSyntax(USAGE_GUESTPROPERTY, "Incorrect parameters");
352
353 /*
354 * Pack the patterns
355 */
356 Utf8Str Utf8Patterns(a->argc > 2 ? a->argv[2] : "*");
357 for (int i = 3; i < a->argc; ++i)
358 Utf8Patterns = Utf8StrFmt ("%s,%s", Utf8Patterns.raw(), a->argv[i]);
359
360 /*
361 * Make the actual call to Main.
362 */
363 ComPtr<IMachine> machine;
364 /* assume it's a UUID */
365 HRESULT rc = a->virtualBox->GetMachine(Bstr(a->argv[0]), machine.asOutParam());
366 if (FAILED(rc) || !machine)
367 {
368 /* must be a name */
369 CHECK_ERROR(a->virtualBox, FindMachine(Bstr(a->argv[0]), machine.asOutParam()));
370 }
371 if (machine)
372 {
373 Bstr uuid;
374 machine->COMGETTER(Id)(uuid.asOutParam());
375
376 /* open a session for the VM - new or existing */
377 if (FAILED(a->virtualBox->OpenSession(a->session, uuid)))
378 CHECK_ERROR_RET (a->virtualBox, OpenExistingSession(a->session, uuid), 1);
379
380 /* get the mutable session machine */
381 a->session->COMGETTER(Machine)(machine.asOutParam());
382
383 com::SafeArray <BSTR> names;
384 com::SafeArray <BSTR> values;
385 com::SafeArray <ULONG64> timestamps;
386 com::SafeArray <BSTR> flags;
387 CHECK_ERROR(machine, EnumerateGuestProperties(Bstr(Utf8Patterns),
388 ComSafeArrayAsOutParam(names),
389 ComSafeArrayAsOutParam(values),
390 ComSafeArrayAsOutParam(timestamps),
391 ComSafeArrayAsOutParam(flags)));
392 if (SUCCEEDED(rc))
393 {
394 if (names.size() == 0)
395 RTPrintf("No properties found.\n");
396 for (unsigned i = 0; i < names.size(); ++i)
397 RTPrintf("Name: %lS, value: %lS, timestamp: %lld, flags: %lS\n",
398 names[i], values[i], timestamps[i], flags[i]);
399 }
400 }
401 return SUCCEEDED(rc) ? 0 : 1;
402}
403
404/**
405 * Enumerates the properties in the guest property store.
406 *
407 * @returns 0 on success, 1 on failure
408 * @note see the command line API description for parameters
409 */
410static int handleWaitGuestProperty(HandlerArg *a)
411{
412 /*
413 * Handle arguments
414 */
415 const char *pszPatterns = NULL;
416 uint32_t u32Timeout = RT_INDEFINITE_WAIT;
417 bool usageOK = true;
418 if (a->argc < 2)
419 usageOK = false;
420 else
421 pszPatterns = a->argv[1];
422 ComPtr<IMachine> machine;
423 /* assume it's a UUID */
424 HRESULT rc = a->virtualBox->GetMachine(Bstr(a->argv[0]), machine.asOutParam());
425 if (FAILED(rc) || !machine)
426 {
427 /* must be a name */
428 CHECK_ERROR(a->virtualBox, FindMachine(Bstr(a->argv[0]), machine.asOutParam()));
429 }
430 if (!machine)
431 usageOK = false;
432 for (int i = 2; usageOK && i < a->argc; ++i)
433 {
434 if ( !strcmp(a->argv[i], "--timeout")
435 || !strcmp(a->argv[i], "-timeout"))
436 {
437 if ( i + 1 >= a->argc
438 || RTStrToUInt32Full(a->argv[i + 1], 10, &u32Timeout)
439 != VINF_SUCCESS
440 )
441 usageOK = false;
442 else
443 ++i;
444 }
445 else
446 usageOK = false;
447 }
448 if (!usageOK)
449 return errorSyntax(USAGE_GUESTPROPERTY, "Incorrect parameters");
450
451 /*
452 * Set up the callback and wait.
453 */
454 Bstr uuid;
455 machine->COMGETTER(Id)(uuid.asOutParam());
456 GuestPropertyCallback *callback = new GuestPropertyCallback(pszPatterns, uuid);
457 callback->AddRef();
458 a->virtualBox->RegisterCallback (callback);
459 bool stop = false;
460#ifdef USE_XPCOM_QUEUE
461 int max_fd = a->eventQ->GetEventQueueSelectFD();
462#endif
463 for (; !stop && u32Timeout > 0; u32Timeout -= RT_MIN(u32Timeout, 1000))
464 {
465#ifdef USE_XPCOM_QUEUE
466 int prc;
467 fd_set fdset;
468 struct timeval tv;
469 FD_ZERO (&fdset);
470 FD_SET(max_fd, &fdset);
471 tv.tv_sec = RT_MIN(u32Timeout, 1000);
472 tv.tv_usec = u32Timeout > 1000 ? 0 : u32Timeout * 1000;
473 RTTIMESPEC TimeNow;
474 uint64_t u64Time = RTTimeSpecGetMilli(RTTimeNow(&TimeNow));
475 prc = select(max_fd + 1, &fdset, NULL, NULL, &tv);
476 if (prc == -1)
477 {
478 RTPrintf("Error waiting for event.\n");
479 stop = true;
480 }
481 else if (prc != 0)
482 {
483 uint64_t u64NextTime = RTTimeSpecGetMilli(RTTimeNow(&TimeNow));
484 u32Timeout += (uint32_t)(u64Time + 1000 - u64NextTime);
485 a->eventQ->ProcessPendingEvents();
486 if (callback->Signalled())
487 stop = true;
488 }
489#else /* !USE_XPCOM_QUEUE */
490 /** @todo Use a semaphore. But I currently don't have a Windows system
491 * running to test on. */
492 /**@todo r=bird: get to it!*/
493 RTThreadSleep(RT_MIN(1000, u32Timeout));
494 if (callback->Signalled())
495 stop = true;
496#endif /* !USE_XPCOM_QUEUE */
497 }
498
499 /*
500 * Clean up the callback.
501 */
502 a->virtualBox->UnregisterCallback(callback);
503 if (!callback->Signalled())
504 RTPrintf("Time out or interruption while waiting for a notification.\n");
505 callback->Release();
506
507 /*
508 * Done.
509 */
510 return 0;
511}
512
513/**
514 * Access the guest property store.
515 *
516 * @returns 0 on success, 1 on failure
517 * @note see the command line API description for parameters
518 */
519int handleGuestProperty(HandlerArg *a)
520{
521 HandlerArg arg = *a;
522 arg.argc = a->argc - 1;
523 arg.argv = a->argv + 1;
524
525 if (a->argc == 0)
526 return errorSyntax(USAGE_GUESTPROPERTY, "Incorrect parameters");
527
528 /* switch (cmd) */
529 if (strcmp(a->argv[0], "get") == 0)
530 return handleGetGuestProperty(&arg);
531 if (strcmp(a->argv[0], "set") == 0)
532 return handleSetGuestProperty(&arg);
533 if (strcmp(a->argv[0], "enumerate") == 0)
534 return handleEnumGuestProperty(&arg);
535 if (strcmp(a->argv[0], "wait") == 0)
536 return handleWaitGuestProperty(&arg);
537
538 /* default: */
539 return errorSyntax(USAGE_GUESTPROPERTY, "Incorrect parameters");
540}
541
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette