VirtualBox

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

Last change on this file since 16107 was 16104, checked in by vboxsync, 16 years ago

VBoxManage typo

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