VirtualBox

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

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

VBoxManage/guestproperty: double-dash command line options

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 15.9 KB
Line 
1/* $Id: VBoxManageGuestProp.cpp 18772 2009-04-06 15:09:26Z 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")
224 || !strcmp(a->argv[2], "-verbose")))
225 verbose = true;
226 else if (a->argc != 2)
227 return errorSyntax(USAGE_GUESTPROPERTY, "Incorrect parameters");
228
229 ComPtr<IMachine> machine;
230 /* assume it's a UUID */
231 rc = a->virtualBox->GetMachine(Guid(a->argv[0]), machine.asOutParam());
232 if (FAILED(rc) || !machine)
233 {
234 /* must be a name */
235 CHECK_ERROR(a->virtualBox, FindMachine(Bstr(a->argv[0]), machine.asOutParam()));
236 }
237 if (machine)
238 {
239 Guid uuid;
240 machine->COMGETTER(Id)(uuid.asOutParam());
241
242 /* open a session for the VM - new or existing */
243 if (FAILED (a->virtualBox->OpenSession(a->session, uuid)))
244 CHECK_ERROR_RET(a->virtualBox, OpenExistingSession(a->session, uuid), 1);
245
246 /* get the mutable session machine */
247 a->session->COMGETTER(Machine)(machine.asOutParam());
248
249 Bstr value;
250 ULONG64 u64Timestamp;
251 Bstr flags;
252 CHECK_ERROR(machine, GetGuestProperty(Bstr(a->argv[1]), value.asOutParam(),
253 &u64Timestamp, flags.asOutParam()));
254 if (!value)
255 RTPrintf("No value set!\n");
256 if (value)
257 RTPrintf("Value: %lS\n", value.raw());
258 if (value && verbose)
259 {
260 RTPrintf("Timestamp: %lld\n", u64Timestamp);
261 RTPrintf("Flags: %lS\n", flags.raw());
262 }
263 }
264 return SUCCEEDED(rc) ? 0 : 1;
265}
266
267static int handleSetGuestProperty(HandlerArg *a)
268{
269 HRESULT rc = S_OK;
270
271 /*
272 * Check the syntax. We can deduce the correct syntax from the number of
273 * arguments.
274 */
275 bool usageOK = true;
276 const char *pszName = NULL;
277 const char *pszValue = NULL;
278 const char *pszFlags = NULL;
279 if (a->argc == 3)
280 pszValue = a->argv[2];
281 else if (a->argc == 4)
282 usageOK = false;
283 else if (a->argc == 5)
284 {
285 pszValue = a->argv[2];
286 if ( !strcmp(a->argv[3], "--flags")
287 || !strcmp(a->argv[3], "-flags"))
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 (!pszValue && !pszFlags)
319 CHECK_ERROR(machine, SetGuestPropertyValue(Bstr(pszName), NULL));
320 else if (!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
346 || a->argc == 2
347 || ( a->argc > 3
348 && strcmp(a->argv[1], "--patterns")
349 && strcmp(a->argv[1], "-patterns")))
350 return errorSyntax(USAGE_GUESTPROPERTY, "Incorrect parameters");
351
352 /*
353 * Pack the patterns
354 */
355 Utf8Str Utf8Patterns(a->argc > 2 ? a->argv[2] : "*");
356 for (int i = 3; i < a->argc; ++i)
357 Utf8Patterns = Utf8StrFmt ("%s,%s", Utf8Patterns.raw(), a->argv[i]);
358
359 /*
360 * Make the actual call to Main.
361 */
362 ComPtr<IMachine> machine;
363 /* assume it's a UUID */
364 HRESULT rc = a->virtualBox->GetMachine(Guid(a->argv[0]), machine.asOutParam());
365 if (FAILED(rc) || !machine)
366 {
367 /* must be a name */
368 CHECK_ERROR(a->virtualBox, FindMachine(Bstr(a->argv[0]), machine.asOutParam()));
369 }
370 if (machine)
371 {
372 Guid uuid;
373 machine->COMGETTER(Id)(uuid.asOutParam());
374
375 /* open a session for the VM - new or existing */
376 if (FAILED(a->virtualBox->OpenSession(a->session, uuid)))
377 CHECK_ERROR_RET (a->virtualBox, OpenExistingSession(a->session, uuid), 1);
378
379 /* get the mutable session machine */
380 a->session->COMGETTER(Machine)(machine.asOutParam());
381
382 com::SafeArray <BSTR> names;
383 com::SafeArray <BSTR> values;
384 com::SafeArray <ULONG64> timestamps;
385 com::SafeArray <BSTR> flags;
386 CHECK_ERROR(machine, EnumerateGuestProperties(Bstr(Utf8Patterns),
387 ComSafeArrayAsOutParam(names),
388 ComSafeArrayAsOutParam(values),
389 ComSafeArrayAsOutParam(timestamps),
390 ComSafeArrayAsOutParam(flags)));
391 if (SUCCEEDED(rc))
392 {
393 if (names.size() == 0)
394 RTPrintf("No properties found.\n");
395 for (unsigned i = 0; i < names.size(); ++i)
396 RTPrintf("Name: %lS, value: %lS, timestamp: %lld, flags: %lS\n",
397 names[i], values[i], timestamps[i], flags[i]);
398 }
399 }
400 return SUCCEEDED(rc) ? 0 : 1;
401}
402
403/**
404 * Enumerates the properties in the guest property store.
405 *
406 * @returns 0 on success, 1 on failure
407 * @note see the command line API description for parameters
408 */
409static int handleWaitGuestProperty(HandlerArg *a)
410{
411 /*
412 * Handle arguments
413 */
414 const char *pszPatterns = NULL;
415 uint32_t u32Timeout = RT_INDEFINITE_WAIT;
416 bool usageOK = true;
417 if (a->argc < 2)
418 usageOK = false;
419 else
420 pszPatterns = a->argv[1];
421 ComPtr<IMachine> machine;
422 /* assume it's a UUID */
423 HRESULT rc = a->virtualBox->GetMachine(Guid(a->argv[0]), machine.asOutParam());
424 if (FAILED(rc) || !machine)
425 {
426 /* must be a name */
427 CHECK_ERROR(a->virtualBox, FindMachine(Bstr(a->argv[0]), machine.asOutParam()));
428 }
429 if (!machine)
430 usageOK = false;
431 for (int i = 2; usageOK && i < a->argc; ++i)
432 {
433 if ( !strcmp(a->argv[i], "--timeout")
434 || !strcmp(a->argv[i], "-timeout"))
435 {
436 if ( i + 1 >= a->argc
437 || RTStrToUInt32Full(a->argv[i + 1], 10, &u32Timeout)
438 != VINF_SUCCESS
439 )
440 usageOK = false;
441 else
442 ++i;
443 }
444 else
445 usageOK = false;
446 }
447 if (!usageOK)
448 return errorSyntax(USAGE_GUESTPROPERTY, "Incorrect parameters");
449
450 /*
451 * Set up the callback and wait.
452 */
453 Guid uuid;
454 machine->COMGETTER(Id)(uuid.asOutParam());
455 GuestPropertyCallback *callback = new GuestPropertyCallback(pszPatterns, uuid);
456 callback->AddRef();
457 a->virtualBox->RegisterCallback (callback);
458 bool stop = false;
459#ifdef USE_XPCOM_QUEUE
460 int max_fd = a->eventQ->GetEventQueueSelectFD();
461#endif
462 for (; !stop && u32Timeout > 0; u32Timeout -= RT_MIN(u32Timeout, 1000))
463 {
464#ifdef USE_XPCOM_QUEUE
465 int prc;
466 fd_set fdset;
467 struct timeval tv;
468 FD_ZERO (&fdset);
469 FD_SET(max_fd, &fdset);
470 tv.tv_sec = RT_MIN(u32Timeout, 1000);
471 tv.tv_usec = u32Timeout > 1000 ? 0 : u32Timeout * 1000;
472 RTTIMESPEC TimeNow;
473 uint64_t u64Time = RTTimeSpecGetMilli(RTTimeNow(&TimeNow));
474 prc = select(max_fd + 1, &fdset, NULL, NULL, &tv);
475 if (prc == -1)
476 {
477 RTPrintf("Error waiting for event.\n");
478 stop = true;
479 }
480 else if (prc != 0)
481 {
482 uint64_t u64NextTime = RTTimeSpecGetMilli(RTTimeNow(&TimeNow));
483 u32Timeout += (uint32_t)(u64Time + 1000 - u64NextTime);
484 a->eventQ->ProcessPendingEvents();
485 if (callback->Signalled())
486 stop = true;
487 }
488#else /* !USE_XPCOM_QUEUE */
489 /** @todo Use a semaphore. But I currently don't have a Windows system
490 * running to test on. */
491 /**@todo r=bird: get to it!*/
492 RTThreadSleep(RT_MIN(1000, u32Timeout));
493 if (callback->Signalled())
494 stop = true;
495#endif /* !USE_XPCOM_QUEUE */
496 }
497
498 /*
499 * Clean up the callback.
500 */
501 a->virtualBox->UnregisterCallback(callback);
502 if (!callback->Signalled())
503 RTPrintf("Time out or interruption while waiting for a notification.\n");
504 callback->Release();
505
506 /*
507 * Done.
508 */
509 return 0;
510}
511
512/**
513 * Access the guest property store.
514 *
515 * @returns 0 on success, 1 on failure
516 * @note see the command line API description for parameters
517 */
518int handleGuestProperty(HandlerArg *a)
519{
520 HandlerArg arg = *a;
521 arg.argc = a->argc - 1;
522 arg.argv = a->argv + 1;
523
524 if (a->argc == 0)
525 return errorSyntax(USAGE_GUESTPROPERTY, "Incorrect parameters");
526
527 /* switch (cmd) */
528 if (strcmp(a->argv[0], "get") == 0)
529 return handleGetGuestProperty(&arg);
530 if (strcmp(a->argv[0], "set") == 0)
531 return handleSetGuestProperty(&arg);
532 if (strcmp(a->argv[0], "enumerate") == 0)
533 return handleEnumGuestProperty(&arg);
534 if (strcmp(a->argv[0], "wait") == 0)
535 return handleWaitGuestProperty(&arg);
536
537 /* default: */
538 return errorSyntax(USAGE_GUESTPROPERTY, "Incorrect parameters");
539}
540
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