VirtualBox

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

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

Main: rework error macros everywhere; make error messages much more readable (esp. with VBoxManage); use shared function to actually print message; reduces size of VBoxManage debug build from 3.4 to 2.3 MB

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 15.5 KB
Line 
1/* $Id: VBoxManageGuestProp.cpp 16530 2009-02-05 16:08:49Z 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.215389.xyz. 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/errorprint2.h>
38
39#include <VBox/com/VirtualBox.h>
40
41#include <VBox/log.h>
42#include <iprt/stream.h>
43#include <iprt/thread.h>
44#include <iprt/time.h>
45
46#ifdef USE_XPCOM_QUEUE
47# include <sys/select.h>
48#endif
49
50using namespace com;
51
52class GuestPropertyCallback : public IVirtualBoxCallback
53{
54public:
55 GuestPropertyCallback(const char *pszPatterns, Guid aUuid)
56 : mSignalled(false), mPatterns(pszPatterns), mUuid(aUuid)
57 {
58#if defined (RT_OS_WINDOWS)
59 refcnt = 0;
60#endif
61 }
62
63 virtual ~GuestPropertyCallback() {}
64
65#ifdef RT_OS_WINDOWS
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
95
96 NS_DECL_ISUPPORTS
97
98 STDMETHOD(OnMachineStateChange)(IN_GUID machineId,
99 MachineState_T state)
100 {
101 return S_OK;
102 }
103
104 STDMETHOD(OnMachineDataChange)(IN_GUID machineId)
105 {
106 return S_OK;
107 }
108
109 STDMETHOD(OnExtraDataCanChange)(IN_GUID 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_GUID machineId, IN_BSTR key,
121 IN_BSTR value)
122 {
123 return S_OK;
124 }
125
126 STDMETHOD(OnMediaRegistered) (IN_GUID 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_GUID machineId, BOOL registered)
136 {
137 return S_OK;
138 }
139
140 STDMETHOD(OnSessionStateChange)(IN_GUID machineId,
141 SessionState_T state)
142 {
143 return S_OK;
144 }
145
146 STDMETHOD(OnSnapshotTaken) (IN_GUID aMachineId,
147 IN_GUID aSnapshotId)
148 {
149 return S_OK;
150 }
151
152 STDMETHOD(OnSnapshotDiscarded) (IN_GUID aMachineId,
153 IN_GUID aSnapshotId)
154 {
155 return S_OK;
156 }
157
158 STDMETHOD(OnSnapshotChange) (IN_GUID aMachineId,
159 IN_GUID aSnapshotId)
160 {
161 return S_OK;
162 }
163
164 STDMETHOD(OnGuestPropertyChange)(IN_GUID machineId,
165 IN_BSTR name, IN_BSTR value,
166 IN_BSTR flags)
167 {
168 int 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#ifdef RT_OS_WINDOWS
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 ((3 == a->argc) && (0 == strcmp(a->argv[2], "-verbose")))
224 verbose = true;
225 else if (a->argc != 2)
226 return errorSyntax(USAGE_GUESTPROPERTY, "Incorrect parameters");
227
228 ComPtr<IMachine> machine;
229 /* assume it's a UUID */
230 rc = a->virtualBox->GetMachine(Guid(a->argv[0]), machine.asOutParam());
231 if (FAILED(rc) || !machine)
232 {
233 /* must be a name */
234 CHECK_ERROR(a->virtualBox, FindMachine(Bstr(a->argv[0]), machine.asOutParam()));
235 }
236 if (machine)
237 {
238 Guid uuid;
239 machine->COMGETTER(Id)(uuid.asOutParam());
240
241 /* open a session for the VM - new or existing */
242 if (FAILED (a->virtualBox->OpenSession(a->session, uuid)))
243 CHECK_ERROR_RET (a->virtualBox, OpenExistingSession(a->session, uuid), 1);
244
245 /* get the mutable session machine */
246 a->session->COMGETTER(Machine)(machine.asOutParam());
247
248 Bstr value;
249 uint64_t u64Timestamp;
250 Bstr flags;
251 CHECK_ERROR(machine, GetGuestProperty(Bstr(a->argv[1]), value.asOutParam(),
252 &u64Timestamp, flags.asOutParam()));
253 if (!value)
254 RTPrintf("No value set!\n");
255 if (value)
256 RTPrintf("Value: %lS\n", value.raw());
257 if (value && verbose)
258 {
259 RTPrintf("Timestamp: %lld\n", u64Timestamp);
260 RTPrintf("Flags: %lS\n", flags.raw());
261 }
262 }
263 return SUCCEEDED(rc) ? 0 : 1;
264}
265
266static int handleSetGuestProperty(HandlerArg *a)
267{
268 HRESULT rc = S_OK;
269
270/*
271 * Check the syntax. We can deduce the correct syntax from the number of
272 * arguments.
273 */
274 bool usageOK = true;
275 const char *pszName = NULL;
276 const char *pszValue = NULL;
277 const char *pszFlags = NULL;
278 if (3 == a->argc)
279 {
280 pszValue = a->argv[2];
281 }
282 else if (4 == a->argc)
283 usageOK = false;
284 else if (5 == a->argc)
285 {
286 pszValue = a->argv[2];
287 if (strcmp(a->argv[3], "-flags") != 0)
288 usageOK = false;
289 pszFlags = a->argv[4];
290 }
291 else if (a->argc != 2)
292 usageOK = false;
293 if (!usageOK)
294 return errorSyntax(USAGE_GUESTPROPERTY, "Incorrect parameters");
295 /* This is always needed. */
296 pszName = a->argv[1];
297
298 ComPtr<IMachine> machine;
299 /* assume it's a UUID */
300 rc = a->virtualBox->GetMachine(Guid(a->argv[0]), machine.asOutParam());
301 if (FAILED(rc) || !machine)
302 {
303 /* must be a name */
304 CHECK_ERROR(a->virtualBox, FindMachine(Bstr(a->argv[0]), machine.asOutParam()));
305 }
306 if (machine)
307 {
308 Guid uuid;
309 machine->COMGETTER(Id)(uuid.asOutParam());
310
311 /* open a session for the VM - new or existing */
312 if (FAILED (a->virtualBox->OpenSession(a->session, uuid)))
313 CHECK_ERROR_RET (a->virtualBox, OpenExistingSession(a->session, uuid), 1);
314
315 /* get the mutable session machine */
316 a->session->COMGETTER(Machine)(machine.asOutParam());
317
318 if ((NULL == pszValue) && (NULL == pszFlags))
319 CHECK_ERROR(machine, SetGuestPropertyValue(Bstr(pszName), NULL));
320 else if (NULL == pszFlags)
321 CHECK_ERROR(machine, SetGuestPropertyValue(Bstr(pszName), Bstr(pszValue)));
322 else
323 CHECK_ERROR(machine, SetGuestProperty(Bstr(pszName), Bstr(pszValue), Bstr(pszFlags)));
324
325 if (SUCCEEDED(rc))
326 CHECK_ERROR(machine, SaveSettings());
327
328 a->session->Close();
329 }
330 return SUCCEEDED(rc) ? 0 : 1;
331}
332
333/**
334 * Enumerates the properties in the guest property store.
335 *
336 * @returns 0 on success, 1 on failure
337 * @note see the command line API description for parameters
338 */
339static int handleEnumGuestProperty(HandlerArg *a)
340{
341/*
342 * Check the syntax. We can deduce the correct syntax from the number of
343 * arguments.
344 */
345 if ((a->argc < 1) || (2 == a->argc) ||
346 ((a->argc > 3) && strcmp(a->argv[1], "-patterns") != 0))
347 return errorSyntax(USAGE_GUESTPROPERTY, "Incorrect parameters");
348
349/*
350 * Pack the patterns
351 */
352 Utf8Str Utf8Patterns(a->argc > 2 ? a->argv[2] : "*");
353 for (ssize_t i = 3; i < a->argc; ++i)
354 Utf8Patterns = Utf8StrFmt ("%s,%s", Utf8Patterns.raw(), a->argv[i]);
355
356/*
357 * Make the actual call to Main.
358 */
359 ComPtr<IMachine> machine;
360 /* assume it's a UUID */
361 HRESULT rc = a->virtualBox->GetMachine(Guid(a->argv[0]), machine.asOutParam());
362 if (FAILED(rc) || !machine)
363 {
364 /* must be a name */
365 CHECK_ERROR(a->virtualBox, FindMachine(Bstr(a->argv[0]), machine.asOutParam()));
366 }
367 if (machine)
368 {
369 Guid uuid;
370 machine->COMGETTER(Id)(uuid.asOutParam());
371
372 /* open a session for the VM - new or existing */
373 if (FAILED (a->virtualBox->OpenSession(a->session, uuid)))
374 CHECK_ERROR_RET (a->virtualBox, OpenExistingSession(a->session, uuid), 1);
375
376 /* get the mutable session machine */
377 a->session->COMGETTER(Machine)(machine.asOutParam());
378
379 com::SafeArray <BSTR> names;
380 com::SafeArray <BSTR> values;
381 com::SafeArray <ULONG64> timestamps;
382 com::SafeArray <BSTR> flags;
383 CHECK_ERROR(machine, EnumerateGuestProperties(Bstr(Utf8Patterns),
384 ComSafeArrayAsOutParam(names),
385 ComSafeArrayAsOutParam(values),
386 ComSafeArrayAsOutParam(timestamps),
387 ComSafeArrayAsOutParam(flags)));
388 if (SUCCEEDED(rc))
389 {
390 if (names.size() == 0)
391 RTPrintf("No properties found.\n");
392 for (unsigned i = 0; i < names.size(); ++i)
393 RTPrintf("Name: %lS, value: %lS, timestamp: %lld, flags: %lS\n",
394 names[i], values[i], timestamps[i], flags[i]);
395 }
396 }
397 return SUCCEEDED(rc) ? 0 : 1;
398}
399
400/**
401 * Enumerates the properties in the guest property store.
402 *
403 * @returns 0 on success, 1 on failure
404 * @note see the command line API description for parameters
405 */
406static int handleWaitGuestProperty(HandlerArg *a)
407{
408
409/*
410 * Handle arguments
411 */
412 const char *pszPatterns = NULL;
413 uint32_t u32Timeout = RT_INDEFINITE_WAIT;
414 bool usageOK = true;
415 if (a->argc < 2)
416 usageOK = false;
417 else
418 pszPatterns = a->argv[1];
419 ComPtr<IMachine> machine;
420 /* assume it's a UUID */
421 HRESULT rc = a->virtualBox->GetMachine(Guid(a->argv[0]), machine.asOutParam());
422 if (FAILED(rc) || !machine)
423 {
424 /* must be a name */
425 CHECK_ERROR(a->virtualBox, FindMachine(Bstr(a->argv[0]), machine.asOutParam()));
426 }
427 if (!machine)
428 usageOK = false;
429 for (int i = 2; usageOK && i < a->argc; ++i)
430 {
431 if (strcmp(a->argv[i], "-timeout") == 0)
432 {
433 if ( i + 1 >= a->argc
434 || RTStrToUInt32Full(a->argv[i + 1], 10, &u32Timeout)
435 != VINF_SUCCESS
436 )
437 usageOK = false;
438 else
439 ++i;
440 }
441 else
442 usageOK = false;
443 }
444 if (!usageOK)
445 return errorSyntax(USAGE_GUESTPROPERTY, "Incorrect parameters");
446
447/*
448 * Set up the callback and wait.
449 */
450 Guid uuid;
451 machine->COMGETTER(Id)(uuid.asOutParam());
452 GuestPropertyCallback *callback = new GuestPropertyCallback(pszPatterns, uuid);
453 callback->AddRef();
454 a->virtualBox->RegisterCallback (callback);
455 bool stop = false;
456#ifdef USE_XPCOM_QUEUE
457 int max_fd = a->eventQ->GetEventQueueSelectFD();
458#endif
459 for (; !stop && u32Timeout > 0; u32Timeout -= RT_MIN(u32Timeout, 1000))
460 {
461#ifdef USE_XPCOM_QUEUE
462 int prc;
463 fd_set fdset;
464 struct timeval tv;
465 FD_ZERO (&fdset);
466 FD_SET(max_fd, &fdset);
467 tv.tv_sec = RT_MIN(u32Timeout, 1000);
468 tv.tv_usec = u32Timeout > 1000 ? 0 : u32Timeout * 1000;
469 RTTIMESPEC TimeNow;
470 uint64_t u64Time = RTTimeSpecGetMilli(RTTimeNow(&TimeNow));
471 prc = select(max_fd + 1, &fdset, NULL, NULL, &tv);
472 if (prc == -1)
473 {
474 RTPrintf("Error waiting for event.\n");
475 stop = true;
476 }
477 else if (prc != 0)
478 {
479 uint64_t u64NextTime = RTTimeSpecGetMilli(RTTimeNow(&TimeNow));
480 u32Timeout += (uint32_t)(u64Time + 1000 - u64NextTime);
481 a->eventQ->ProcessPendingEvents();
482 if (callback->Signalled())
483 stop = true;
484 }
485#else
486 /** @todo Use a semaphore. But I currently don't have a Windows system
487 * running to test on. */
488 RTThreadSleep(RT_MIN(1000, u32Timeout));
489 if (callback->Signalled())
490 stop = true;
491#endif /* USE_XPCOM_QUEUE */
492 }
493
494/*
495 * Clean up the callback.
496 */
497 a->virtualBox->UnregisterCallback (callback);
498 if (!callback->Signalled())
499 RTPrintf("Time out or interruption while waiting for a notification.\n");
500 callback->Release ();
501
502/*
503 * Done.
504 */
505 return 0;
506}
507
508/**
509 * Access the guest property store.
510 *
511 * @returns 0 on success, 1 on failure
512 * @note see the command line API description for parameters
513 */
514int handleGuestProperty(HandlerArg *a)
515{
516 HandlerArg arg = *a;
517 arg.argc = a->argc - 1;
518 arg.argv = a->argv + 1;
519
520 if (0 == a->argc)
521 return errorSyntax(USAGE_GUESTPROPERTY, "Incorrect parameters");
522 if (0 == strcmp(a->argv[0], "get"))
523 return handleGetGuestProperty(&arg);
524 else if (0 == strcmp(a->argv[0], "set"))
525 return handleSetGuestProperty(&arg);
526 else if (0 == strcmp(a->argv[0], "enumerate"))
527 return handleEnumGuestProperty(&arg);
528 else if (0 == strcmp(a->argv[0], "wait"))
529 return handleWaitGuestProperty(&arg);
530 /* else */
531 return errorSyntax(USAGE_GUESTPROPERTY, "Incorrect parameters");
532}
533
Note: See TracBrowser for help on using the repository browser.

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