VirtualBox

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

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

VBoxManageGuestProp.cpp: Fixed warning. Unified the style a bit and added a @todo escalation :-).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 15.7 KB
Line 
1/* $Id: VBoxManageGuestProp.cpp 16724 2009-02-13 01:43:01Z 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 : public IVirtualBoxCallback
52{
53public:
54 GuestPropertyCallback(const char *pszPatterns, Guid aUuid)
55 : mSignalled(false), mPatterns(pszPatterns), mUuid(aUuid)
56 {
57#ifndef VBOX_WITH_XPCOM
58 refcnt = 0;
59#endif
60 }
61
62 virtual ~GuestPropertyCallback() {}
63
64#ifndef VBOX_WITH_XPCOM
65 STDMETHOD_(ULONG, AddRef)()
66 {
67 return ::InterlockedIncrement(&refcnt);
68 }
69 STDMETHOD_(ULONG, Release)()
70 {
71 long cnt = ::InterlockedDecrement(&refcnt);
72 if (cnt == 0)
73 delete this;
74 return cnt;
75 }
76 STDMETHOD(QueryInterface)(REFIID riid , void **ppObj)
77 {
78 if (riid == IID_IUnknown)
79 {
80 *ppObj = this;
81 AddRef();
82 return S_OK;
83 }
84 if (riid == IID_IVirtualBoxCallback)
85 {
86 *ppObj = this;
87 AddRef();
88 return S_OK;
89 }
90 *ppObj = NULL;
91 return E_NOINTERFACE;
92 }
93#endif /* !VBOX_WITH_XPCOM */
94
95 NS_DECL_ISUPPORTS
96
97 STDMETHOD(OnMachineStateChange)(IN_GUID machineId,
98 MachineState_T state)
99 {
100 return S_OK;
101 }
102
103 STDMETHOD(OnMachineDataChange)(IN_GUID machineId)
104 {
105 return S_OK;
106 }
107
108 STDMETHOD(OnExtraDataCanChange)(IN_GUID machineId, IN_BSTR key,
109 IN_BSTR value, BSTR *error,
110 BOOL *changeAllowed)
111 {
112 /* we never disagree */
113 if (!changeAllowed)
114 return E_INVALIDARG;
115 *changeAllowed = TRUE;
116 return S_OK;
117 }
118
119 STDMETHOD(OnExtraDataChange)(IN_GUID machineId, IN_BSTR key,
120 IN_BSTR value)
121 {
122 return S_OK;
123 }
124
125 STDMETHOD(OnMediaRegistered)(IN_GUID mediaId,
126 DeviceType_T mediaType, BOOL registered)
127 {
128 NOREF(mediaId);
129 NOREF(mediaType);
130 NOREF(registered);
131 return S_OK;
132 }
133
134 STDMETHOD(OnMachineRegistered)(IN_GUID machineId, BOOL registered)
135 {
136 return S_OK;
137 }
138
139 STDMETHOD(OnSessionStateChange)(IN_GUID machineId,
140 SessionState_T state)
141 {
142 return S_OK;
143 }
144
145 STDMETHOD(OnSnapshotTaken)(IN_GUID aMachineId,
146 IN_GUID aSnapshotId)
147 {
148 return S_OK;
149 }
150
151 STDMETHOD(OnSnapshotDiscarded)(IN_GUID aMachineId,
152 IN_GUID aSnapshotId)
153 {
154 return S_OK;
155 }
156
157 STDMETHOD(OnSnapshotChange)(IN_GUID aMachineId,
158 IN_GUID aSnapshotId)
159 {
160 return S_OK;
161 }
162
163 STDMETHOD(OnGuestPropertyChange)(IN_GUID machineId,
164 IN_BSTR name, IN_BSTR value,
165 IN_BSTR flags)
166 {
167 HRESULT rc = S_OK;
168 Utf8Str utf8Name (name);
169 Guid uuid(machineId);
170 if (utf8Name.isNull())
171 rc = E_OUTOFMEMORY;
172 if ( SUCCEEDED (rc)
173 && uuid == mUuid
174 && RTStrSimplePatternMultiMatch(mPatterns, RTSTR_MAX,
175 utf8Name.raw(), RTSTR_MAX, NULL))
176 {
177 RTPrintf("Name: %lS, value: %lS, flags: %lS\n", name, value,
178 flags);
179 mSignalled = true;
180 }
181 return rc;
182 }
183
184 bool Signalled(void) { return mSignalled; }
185
186private:
187 bool mSignalled;
188 const char *mPatterns;
189 Guid mUuid;
190#ifndef VBOX_WITH_XPCOM
191 long refcnt;
192#endif
193
194};
195
196#ifdef VBOX_WITH_XPCOM
197NS_DECL_CLASSINFO(GuestPropertyCallback)
198NS_IMPL_ISUPPORTS1_CI(GuestPropertyCallback, IVirtualBoxCallback)
199#endif /* VBOX_WITH_XPCOM */
200
201void usageGuestProperty(void)
202{
203 RTPrintf("VBoxManage guestproperty get <vmname>|<uuid>\n"
204 " <property> [-verbose]\n"
205 "\n");
206 RTPrintf("VBoxManage guestproperty set <vmname>|<uuid>\n"
207 " <property> [<value> [-flags <flags>]]\n"
208 "\n");
209 RTPrintf("VBoxManage guestproperty enumerate <vmname>|<uuid>\n"
210 " [-patterns <patterns>]\n"
211 "\n");
212 RTPrintf("VBoxManage guestproperty wait <vmname>|<uuid> <patterns>\n"
213 " [-timeout <timeout>]\n"
214 "\n");
215}
216
217static int handleGetGuestProperty(HandlerArg *a)
218{
219 HRESULT rc = S_OK;
220
221 bool verbose = false;
222 if ( a->argc == 3
223 && strcmp(a->argv[2], "-verbose") == 0)
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 ULONG64 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 (a->argc == 3)
279 pszValue = a->argv[2];
280 else if (a->argc == 4)
281 usageOK = false;
282 else if (a->argc == 5)
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 (!pszValue && !pszFlags)
317 CHECK_ERROR(machine, SetGuestPropertyValue(Bstr(pszName), NULL));
318 else if (!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
344 || a->argc == 2
345 || ( a->argc > 3
346 && 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 (int 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 * Handle arguments
410 */
411 const char *pszPatterns = NULL;
412 uint32_t u32Timeout = RT_INDEFINITE_WAIT;
413 bool usageOK = true;
414 if (a->argc < 2)
415 usageOK = false;
416 else
417 pszPatterns = a->argv[1];
418 ComPtr<IMachine> machine;
419 /* assume it's a UUID */
420 HRESULT rc = a->virtualBox->GetMachine(Guid(a->argv[0]), machine.asOutParam());
421 if (FAILED(rc) || !machine)
422 {
423 /* must be a name */
424 CHECK_ERROR(a->virtualBox, FindMachine(Bstr(a->argv[0]), machine.asOutParam()));
425 }
426 if (!machine)
427 usageOK = false;
428 for (int i = 2; usageOK && i < a->argc; ++i)
429 {
430 if (strcmp(a->argv[i], "-timeout") == 0)
431 {
432 if ( i + 1 >= a->argc
433 || RTStrToUInt32Full(a->argv[i + 1], 10, &u32Timeout)
434 != VINF_SUCCESS
435 )
436 usageOK = false;
437 else
438 ++i;
439 }
440 else
441 usageOK = false;
442 }
443 if (!usageOK)
444 return errorSyntax(USAGE_GUESTPROPERTY, "Incorrect parameters");
445
446 /*
447 * Set up the callback and wait.
448 */
449 Guid uuid;
450 machine->COMGETTER(Id)(uuid.asOutParam());
451 GuestPropertyCallback *callback = new GuestPropertyCallback(pszPatterns, uuid);
452 callback->AddRef();
453 a->virtualBox->RegisterCallback (callback);
454 bool stop = false;
455#ifdef USE_XPCOM_QUEUE
456 int max_fd = a->eventQ->GetEventQueueSelectFD();
457#endif
458 for (; !stop && u32Timeout > 0; u32Timeout -= RT_MIN(u32Timeout, 1000))
459 {
460#ifdef USE_XPCOM_QUEUE
461 int prc;
462 fd_set fdset;
463 struct timeval tv;
464 FD_ZERO (&fdset);
465 FD_SET(max_fd, &fdset);
466 tv.tv_sec = RT_MIN(u32Timeout, 1000);
467 tv.tv_usec = u32Timeout > 1000 ? 0 : u32Timeout * 1000;
468 RTTIMESPEC TimeNow;
469 uint64_t u64Time = RTTimeSpecGetMilli(RTTimeNow(&TimeNow));
470 prc = select(max_fd + 1, &fdset, NULL, NULL, &tv);
471 if (prc == -1)
472 {
473 RTPrintf("Error waiting for event.\n");
474 stop = true;
475 }
476 else if (prc != 0)
477 {
478 uint64_t u64NextTime = RTTimeSpecGetMilli(RTTimeNow(&TimeNow));
479 u32Timeout += (uint32_t)(u64Time + 1000 - u64NextTime);
480 a->eventQ->ProcessPendingEvents();
481 if (callback->Signalled())
482 stop = true;
483 }
484#else /* !USE_XPCOM_QUEUE */
485 /** @todo Use a semaphore. But I currently don't have a Windows system
486 * running to test on. */
487 /**@todo r=bird: get to it!*/
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 (a->argc == 0)
521 return errorSyntax(USAGE_GUESTPROPERTY, "Incorrect parameters");
522
523 /* switch (cmd) */
524 if (strcmp(a->argv[0], "get") == 0)
525 return handleGetGuestProperty(&arg);
526 if (strcmp(a->argv[0], "set") == 0)
527 return handleSetGuestProperty(&arg);
528 if (strcmp(a->argv[0], "enumerate") == 0)
529 return handleEnumGuestProperty(&arg);
530 if (strcmp(a->argv[0], "wait") == 0)
531 return handleWaitGuestProperty(&arg);
532
533 /* default: */
534 return errorSyntax(USAGE_GUESTPROPERTY, "Incorrect parameters");
535}
536
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