VirtualBox

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

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

IPRT, Main: make ministring throw std::bad_alloc on allocation failure; remove isEmpty() and isNull(), change Main code to using length() instead; second try

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 15.8 KB
Line 
1/* $Id: VBoxManageGuestProp.cpp 21404 2009-07-08 15:19:42Z vboxsync $ */
2/** @file
3 * VBoxManage - The 'guestproperty' command.
4 */
5
6/*
7 * Copyright (C) 2006-2009 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#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/errorprint.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 ( 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#ifndef VBOX_WITH_XPCOM
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 ( a->argc == 3
222 && ( !strcmp(a->argv[2], "--verbose")
223 || !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(Bstr(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 Bstr 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")
286 || !strcmp(a->argv[3], "-flags"))
287 usageOK = false;
288 pszFlags = a->argv[4];
289 }
290 else if (a->argc != 2)
291 usageOK = false;
292 if (!usageOK)
293 return errorSyntax(USAGE_GUESTPROPERTY, "Incorrect parameters");
294 /* This is always needed. */
295 pszName = a->argv[1];
296
297 ComPtr<IMachine> machine;
298 /* assume it's a UUID */
299 rc = a->virtualBox->GetMachine(Bstr(a->argv[0]), machine.asOutParam());
300 if (FAILED(rc) || !machine)
301 {
302 /* must be a name */
303 CHECK_ERROR(a->virtualBox, FindMachine(Bstr(a->argv[0]), machine.asOutParam()));
304 }
305 if (machine)
306 {
307 Bstr uuid;
308 machine->COMGETTER(Id)(uuid.asOutParam());
309
310 /* open a session for the VM - new or existing */
311 if (FAILED (a->virtualBox->OpenSession(a->session, uuid)))
312 CHECK_ERROR_RET (a->virtualBox, OpenExistingSession(a->session, uuid), 1);
313
314 /* get the mutable session machine */
315 a->session->COMGETTER(Machine)(machine.asOutParam());
316
317 if (!pszValue && !pszFlags)
318 CHECK_ERROR(machine, SetGuestPropertyValue(Bstr(pszName), NULL));
319 else if (!pszFlags)
320 CHECK_ERROR(machine, SetGuestPropertyValue(Bstr(pszName), Bstr(pszValue)));
321 else
322 CHECK_ERROR(machine, SetGuestProperty(Bstr(pszName), Bstr(pszValue), Bstr(pszFlags)));
323
324 if (SUCCEEDED(rc))
325 CHECK_ERROR(machine, SaveSettings());
326
327 a->session->Close();
328 }
329 return SUCCEEDED(rc) ? 0 : 1;
330}
331
332/**
333 * Enumerates the properties in the guest property store.
334 *
335 * @returns 0 on success, 1 on failure
336 * @note see the command line API description for parameters
337 */
338static int handleEnumGuestProperty(HandlerArg *a)
339{
340 /*
341 * Check the syntax. We can deduce the correct syntax from the number of
342 * arguments.
343 */
344 if ( a->argc < 1
345 || a->argc == 2
346 || ( a->argc > 3
347 && strcmp(a->argv[1], "--patterns")
348 && strcmp(a->argv[1], "-patterns")))
349 return errorSyntax(USAGE_GUESTPROPERTY, "Incorrect parameters");
350
351 /*
352 * Pack the patterns
353 */
354 Utf8Str Utf8Patterns(a->argc > 2 ? a->argv[2] : "*");
355 for (int i = 3; i < a->argc; ++i)
356 Utf8Patterns = Utf8StrFmt ("%s,%s", Utf8Patterns.raw(), a->argv[i]);
357
358 /*
359 * Make the actual call to Main.
360 */
361 ComPtr<IMachine> machine;
362 /* assume it's a UUID */
363 HRESULT rc = a->virtualBox->GetMachine(Bstr(a->argv[0]), machine.asOutParam());
364 if (FAILED(rc) || !machine)
365 {
366 /* must be a name */
367 CHECK_ERROR(a->virtualBox, FindMachine(Bstr(a->argv[0]), machine.asOutParam()));
368 }
369 if (machine)
370 {
371 Bstr uuid;
372 machine->COMGETTER(Id)(uuid.asOutParam());
373
374 /* open a session for the VM - new or existing */
375 if (FAILED(a->virtualBox->OpenSession(a->session, uuid)))
376 CHECK_ERROR_RET (a->virtualBox, OpenExistingSession(a->session, uuid), 1);
377
378 /* get the mutable session machine */
379 a->session->COMGETTER(Machine)(machine.asOutParam());
380
381 com::SafeArray <BSTR> names;
382 com::SafeArray <BSTR> values;
383 com::SafeArray <ULONG64> timestamps;
384 com::SafeArray <BSTR> flags;
385 CHECK_ERROR(machine, EnumerateGuestProperties(Bstr(Utf8Patterns),
386 ComSafeArrayAsOutParam(names),
387 ComSafeArrayAsOutParam(values),
388 ComSafeArrayAsOutParam(timestamps),
389 ComSafeArrayAsOutParam(flags)));
390 if (SUCCEEDED(rc))
391 {
392 if (names.size() == 0)
393 RTPrintf("No properties found.\n");
394 for (unsigned i = 0; i < names.size(); ++i)
395 RTPrintf("Name: %lS, value: %lS, timestamp: %lld, flags: %lS\n",
396 names[i], values[i], timestamps[i], flags[i]);
397 }
398 }
399 return SUCCEEDED(rc) ? 0 : 1;
400}
401
402/**
403 * Enumerates the properties in the guest property store.
404 *
405 * @returns 0 on success, 1 on failure
406 * @note see the command line API description for parameters
407 */
408static int handleWaitGuestProperty(HandlerArg *a)
409{
410 /*
411 * Handle arguments
412 */
413 const char *pszPatterns = NULL;
414 uint32_t u32Timeout = RT_INDEFINITE_WAIT;
415 bool usageOK = true;
416 if (a->argc < 2)
417 usageOK = false;
418 else
419 pszPatterns = a->argv[1];
420 ComPtr<IMachine> machine;
421 /* assume it's a UUID */
422 HRESULT rc = a->virtualBox->GetMachine(Bstr(a->argv[0]), machine.asOutParam());
423 if (FAILED(rc) || !machine)
424 {
425 /* must be a name */
426 CHECK_ERROR(a->virtualBox, FindMachine(Bstr(a->argv[0]), machine.asOutParam()));
427 }
428 if (!machine)
429 usageOK = false;
430 for (int i = 2; usageOK && i < a->argc; ++i)
431 {
432 if ( !strcmp(a->argv[i], "--timeout")
433 || !strcmp(a->argv[i], "-timeout"))
434 {
435 if ( i + 1 >= a->argc
436 || RTStrToUInt32Full(a->argv[i + 1], 10, &u32Timeout)
437 != VINF_SUCCESS
438 )
439 usageOK = false;
440 else
441 ++i;
442 }
443 else
444 usageOK = false;
445 }
446 if (!usageOK)
447 return errorSyntax(USAGE_GUESTPROPERTY, "Incorrect parameters");
448
449 /*
450 * Set up the callback and wait.
451 */
452 Bstr uuid;
453 machine->COMGETTER(Id)(uuid.asOutParam());
454 GuestPropertyCallback *callback = new GuestPropertyCallback(pszPatterns, uuid);
455 callback->AddRef();
456 a->virtualBox->RegisterCallback (callback);
457 bool stop = false;
458#ifdef USE_XPCOM_QUEUE
459 int max_fd = a->eventQ->GetEventQueueSelectFD();
460#endif
461 for (; !stop && u32Timeout > 0; u32Timeout -= RT_MIN(u32Timeout, 1000))
462 {
463#ifdef USE_XPCOM_QUEUE
464 int prc;
465 fd_set fdset;
466 struct timeval tv;
467 FD_ZERO (&fdset);
468 FD_SET(max_fd, &fdset);
469 tv.tv_sec = RT_MIN(u32Timeout, 1000);
470 tv.tv_usec = u32Timeout > 1000 ? 0 : u32Timeout * 1000;
471 RTTIMESPEC TimeNow;
472 uint64_t u64Time = RTTimeSpecGetMilli(RTTimeNow(&TimeNow));
473 prc = select(max_fd + 1, &fdset, NULL, NULL, &tv);
474 if (prc == -1)
475 {
476 RTPrintf("Error waiting for event.\n");
477 stop = true;
478 }
479 else if (prc != 0)
480 {
481 uint64_t u64NextTime = RTTimeSpecGetMilli(RTTimeNow(&TimeNow));
482 u32Timeout += (uint32_t)(u64Time + 1000 - u64NextTime);
483 a->eventQ->ProcessPendingEvents();
484 if (callback->Signalled())
485 stop = true;
486 }
487#else /* !USE_XPCOM_QUEUE */
488 /** @todo Use a semaphore. But I currently don't have a Windows system
489 * running to test on. */
490 /**@todo r=bird: get to it!*/
491 RTThreadSleep(RT_MIN(1000, u32Timeout));
492 if (callback->Signalled())
493 stop = true;
494#endif /* !USE_XPCOM_QUEUE */
495 }
496
497 /*
498 * Clean up the callback.
499 */
500 a->virtualBox->UnregisterCallback(callback);
501 if (!callback->Signalled())
502 RTPrintf("Time out or interruption while waiting for a notification.\n");
503 callback->Release();
504
505 /*
506 * Done.
507 */
508 return 0;
509}
510
511/**
512 * Access the guest property store.
513 *
514 * @returns 0 on success, 1 on failure
515 * @note see the command line API description for parameters
516 */
517int handleGuestProperty(HandlerArg *a)
518{
519 HandlerArg arg = *a;
520 arg.argc = a->argc - 1;
521 arg.argv = a->argv + 1;
522
523 if (a->argc == 0)
524 return errorSyntax(USAGE_GUESTPROPERTY, "Incorrect parameters");
525
526 /* switch (cmd) */
527 if (strcmp(a->argv[0], "get") == 0)
528 return handleGetGuestProperty(&arg);
529 if (strcmp(a->argv[0], "set") == 0)
530 return handleSetGuestProperty(&arg);
531 if (strcmp(a->argv[0], "enumerate") == 0)
532 return handleEnumGuestProperty(&arg);
533 if (strcmp(a->argv[0], "wait") == 0)
534 return handleWaitGuestProperty(&arg);
535
536 /* default: */
537 return errorSyntax(USAGE_GUESTPROPERTY, "Incorrect parameters");
538}
539
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