VirtualBox

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

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

Implemented 'VBoxManage controlvm vrdpport'

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 65.7 KB
Line 
1/* $Id: VBoxManage.cpp 23129 2009-09-18 12:57:37Z vboxsync $ */
2/** @file
3 * VBoxManage - VirtualBox's command-line interface.
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.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#ifndef VBOX_ONLY_DOCS
27#include <VBox/com/com.h>
28#include <VBox/com/string.h>
29#include <VBox/com/Guid.h>
30#include <VBox/com/array.h>
31#include <VBox/com/ErrorInfo.h>
32#include <VBox/com/errorprint.h>
33#include <VBox/com/EventQueue.h>
34
35#include <VBox/com/VirtualBox.h>
36
37#include <vector>
38#include <list>
39#endif /* !VBOX_ONLY_DOCS */
40
41#include <iprt/asm.h>
42#include <iprt/buildconfig.h>
43#include <iprt/cidr.h>
44#include <iprt/ctype.h>
45#include <iprt/dir.h>
46#include <iprt/env.h>
47#include <VBox/err.h>
48#include <iprt/file.h>
49#include <iprt/initterm.h>
50#include <iprt/param.h>
51#include <iprt/path.h>
52#include <iprt/stream.h>
53#include <iprt/string.h>
54#include <iprt/stdarg.h>
55#include <iprt/thread.h>
56#include <iprt/uuid.h>
57#include <iprt/getopt.h>
58#include <iprt/ctype.h>
59#include <VBox/version.h>
60#include <VBox/log.h>
61
62#include "VBoxManage.h"
63
64#ifndef VBOX_ONLY_DOCS
65using namespace com;
66
67/** command handler type */
68typedef int (*PFNHANDLER)(HandlerArg *a);
69
70#endif /* !VBOX_ONLY_DOCS */
71
72////////////////////////////////////////////////////////////////////////////////
73//
74// global variables
75//
76////////////////////////////////////////////////////////////////////////////////
77
78/*extern*/ bool g_fDetailedProgress = false;
79
80////////////////////////////////////////////////////////////////////////////////
81//
82// functions
83//
84////////////////////////////////////////////////////////////////////////////////
85
86#ifndef VBOX_ONLY_DOCS
87/**
88 * Print out progress on the console
89 */
90LONG showProgress(ComPtr<IProgress> progress)
91{
92 BOOL fCompleted;
93 ULONG ulCurrentPercent;
94 ULONG ulLastPercent = 0;
95
96 ULONG ulCurrentOperationPercent;
97 ULONG ulLastOperationPercent = (ULONG)-1;
98
99 ULONG ulLastOperation = (ULONG)-1;
100 Bstr bstrOperationDescription;
101
102 ULONG cOperations;
103 progress->COMGETTER(OperationCount)(&cOperations);
104
105 if (!g_fDetailedProgress)
106 {
107 RTPrintf("0%%...");
108 RTStrmFlush(g_pStdOut);
109 }
110
111 while (SUCCEEDED(progress->COMGETTER(Completed(&fCompleted))))
112 {
113 ULONG ulOperation;
114 progress->COMGETTER(Operation)(&ulOperation);
115
116 progress->COMGETTER(Percent(&ulCurrentPercent));
117 progress->COMGETTER(OperationPercent(&ulCurrentOperationPercent));
118
119 if (g_fDetailedProgress)
120 {
121 if (ulLastOperation != ulOperation)
122 {
123 progress->COMGETTER(OperationDescription(bstrOperationDescription.asOutParam()));
124 ulLastPercent = (ULONG)-1; // force print
125 ulLastOperation = ulOperation;
126 }
127
128 if ( (ulCurrentPercent != ulLastPercent)
129 || (ulCurrentOperationPercent != ulLastOperationPercent)
130 )
131 {
132 LONG lSecsRem;
133 progress->COMGETTER(TimeRemaining)(&lSecsRem);
134
135 RTPrintf("(%ld/%ld) %ls %ld%% => %ld%% (%d s remaining)\n", ulOperation + 1, cOperations, bstrOperationDescription.raw(), ulCurrentOperationPercent, ulCurrentPercent, lSecsRem);
136 ulLastPercent = ulCurrentPercent;
137 ulLastOperationPercent = ulCurrentOperationPercent;
138 }
139 }
140 else
141 {
142 /* did we cross a 10% mark? */
143 if (((ulCurrentPercent / 10) > (ulLastPercent / 10)))
144 {
145 /* make sure to also print out missed steps */
146 for (ULONG curVal = (ulLastPercent / 10) * 10 + 10; curVal <= (ulCurrentPercent / 10) * 10; curVal += 10)
147 {
148 if (curVal < 100)
149 {
150 RTPrintf("%ld%%...", curVal);
151 RTStrmFlush(g_pStdOut);
152 }
153 }
154 ulLastPercent = (ulCurrentPercent / 10) * 10;
155 }
156 }
157 if (fCompleted)
158 break;
159
160 /* make sure the loop is not too tight */
161 progress->WaitForCompletion(100);
162 }
163
164 /* complete the line. */
165 LONG iRc;
166 if (SUCCEEDED(progress->COMGETTER(ResultCode)(&iRc)))
167 {
168 if (SUCCEEDED(iRc))
169 RTPrintf("100%%\n");
170 else
171 RTPrintf("FAILED\n");
172 }
173 else
174 RTPrintf("\n");
175 RTStrmFlush(g_pStdOut);
176 return iRc;
177}
178#endif /* !VBOX_ONLY_DOCS */
179
180void showLogo(void)
181{
182 static bool fShown; /* show only once */
183
184 if (!fShown)
185 {
186 RTPrintf("VirtualBox Command Line Management Interface Version "
187 VBOX_VERSION_STRING "\n"
188 "(C) 2005-2009 Sun Microsystems, Inc.\n"
189 "All rights reserved.\n"
190 "\n");
191 fShown = true;
192 }
193}
194
195#ifndef VBOX_ONLY_DOCS
196
197static int handleRegisterVM(HandlerArg *a)
198{
199 HRESULT rc;
200
201 if (a->argc != 1)
202 return errorSyntax(USAGE_REGISTERVM, "Incorrect number of parameters");
203
204 ComPtr<IMachine> machine;
205 /** @todo Ugly hack to get both the API interpretation of relative paths
206 * and the client's interpretation of relative paths. Remove after the API
207 * has been redesigned. */
208 rc = a->virtualBox->OpenMachine(Bstr(a->argv[0]), machine.asOutParam());
209 if (rc == VBOX_E_FILE_ERROR)
210 {
211 char szVMFileAbs[RTPATH_MAX] = "";
212 int vrc = RTPathAbs(a->argv[0], szVMFileAbs, sizeof(szVMFileAbs));
213 if (RT_FAILURE(vrc))
214 {
215 RTPrintf("Cannot convert filename \"%s\" to absolute path\n", a->argv[0]);
216 return 1;
217 }
218 CHECK_ERROR(a->virtualBox, OpenMachine(Bstr(szVMFileAbs), machine.asOutParam()));
219 }
220 else if (FAILED(rc))
221 CHECK_ERROR(a->virtualBox, OpenMachine(Bstr(a->argv[0]), machine.asOutParam()));
222 if (SUCCEEDED(rc))
223 {
224 ASSERT(machine);
225 CHECK_ERROR(a->virtualBox, RegisterMachine(machine));
226 }
227 return SUCCEEDED(rc) ? 0 : 1;
228}
229
230static const RTGETOPTDEF g_aUnregisterVMOptions[] =
231{
232 { "--delete", 'd', RTGETOPT_REQ_NOTHING },
233 { "-delete", 'd', RTGETOPT_REQ_NOTHING }, // deprecated
234};
235
236static int handleUnregisterVM(HandlerArg *a)
237{
238 HRESULT rc;
239 const char *VMName = NULL;
240 bool fDelete = false;
241
242 int c;
243 RTGETOPTUNION ValueUnion;
244 RTGETOPTSTATE GetState;
245 // start at 0 because main() has hacked both the argc and argv given to us
246 RTGetOptInit(&GetState, a->argc, a->argv, g_aUnregisterVMOptions, RT_ELEMENTS(g_aUnregisterVMOptions), 0, 0 /* fFlags */);
247 while ((c = RTGetOpt(&GetState, &ValueUnion)))
248 {
249 switch (c)
250 {
251 case 'd': // --delete
252 fDelete = true;
253 break;
254
255 case VINF_GETOPT_NOT_OPTION:
256 if (!VMName)
257 VMName = ValueUnion.psz;
258 else
259 return errorSyntax(USAGE_UNREGISTERVM, "Invalid parameter '%s'", ValueUnion.psz);
260 break;
261
262 default:
263 if (c > 0)
264 {
265 if (RT_C_IS_PRINT(c))
266 return errorSyntax(USAGE_UNREGISTERVM, "Invalid option -%c", c);
267 else
268 return errorSyntax(USAGE_UNREGISTERVM, "Invalid option case %i", c);
269 }
270 else if (c == VERR_GETOPT_UNKNOWN_OPTION)
271 return errorSyntax(USAGE_UNREGISTERVM, "unknown option: %s\n", ValueUnion.psz);
272 else if (ValueUnion.pDef)
273 return errorSyntax(USAGE_UNREGISTERVM, "%s: %Rrs", ValueUnion.pDef->pszLong, c);
274 else
275 return errorSyntax(USAGE_UNREGISTERVM, "error: %Rrs", c);
276 }
277 }
278
279 /* check for required options */
280 if (!VMName)
281 return errorSyntax(USAGE_UNREGISTERVM, "VM name required");
282
283 ComPtr<IMachine> machine;
284 /* assume it's a UUID */
285 rc = a->virtualBox->GetMachine(Guid(VMName).toUtf16(), machine.asOutParam());
286 if (FAILED(rc) || !machine)
287 {
288 /* must be a name */
289 CHECK_ERROR(a->virtualBox, FindMachine(Bstr(VMName), machine.asOutParam()));
290 }
291 if (machine)
292 {
293 Bstr uuid;
294 machine->COMGETTER(Id)(uuid.asOutParam());
295 machine = NULL;
296 CHECK_ERROR(a->virtualBox, UnregisterMachine(uuid, machine.asOutParam()));
297 if (SUCCEEDED(rc) && machine && fDelete)
298 CHECK_ERROR(machine, DeleteSettings());
299 }
300 return SUCCEEDED(rc) ? 0 : 1;
301}
302
303static int handleCreateVM(HandlerArg *a)
304{
305 HRESULT rc;
306 Bstr baseFolder;
307 Bstr settingsFile;
308 Bstr name;
309 Bstr osTypeId;
310 RTUUID id;
311 bool fRegister = false;
312
313 RTUuidClear(&id);
314 for (int i = 0; i < a->argc; i++)
315 {
316 if ( !strcmp(a->argv[i], "--basefolder")
317 || !strcmp(a->argv[i], "-basefolder"))
318 {
319 if (a->argc <= i + 1)
320 return errorArgument("Missing argument to '%s'", a->argv[i]);
321 i++;
322 baseFolder = a->argv[i];
323 }
324 else if ( !strcmp(a->argv[i], "--settingsfile")
325 || !strcmp(a->argv[i], "-settingsfile"))
326 {
327 if (a->argc <= i + 1)
328 return errorArgument("Missing argument to '%s'", a->argv[i]);
329 i++;
330 settingsFile = a->argv[i];
331 }
332 else if ( !strcmp(a->argv[i], "--name")
333 || !strcmp(a->argv[i], "-name"))
334 {
335 if (a->argc <= i + 1)
336 return errorArgument("Missing argument to '%s'", a->argv[i]);
337 i++;
338 name = a->argv[i];
339 }
340 else if ( !strcmp(a->argv[i], "--ostype")
341 || !strcmp(a->argv[i], "-ostype"))
342 {
343 if (a->argc <= i + 1)
344 return errorArgument("Missing argument to '%s'", a->argv[i]);
345 i++;
346 osTypeId = a->argv[i];
347 }
348 else if ( !strcmp(a->argv[i], "--uuid")
349 || !strcmp(a->argv[i], "-uuid"))
350 {
351 if (a->argc <= i + 1)
352 return errorArgument("Missing argument to '%s'", a->argv[i]);
353 i++;
354 if (RT_FAILURE(RTUuidFromStr(&id, a->argv[i])))
355 return errorArgument("Invalid UUID format %s\n", a->argv[i]);
356 }
357 else if ( !strcmp(a->argv[i], "--register")
358 || !strcmp(a->argv[i], "-register"))
359 {
360 fRegister = true;
361 }
362 else
363 return errorSyntax(USAGE_CREATEVM, "Invalid parameter '%s'", Utf8Str(a->argv[i]).raw());
364 }
365 if (!name)
366 return errorSyntax(USAGE_CREATEVM, "Parameter --name is required");
367
368 if (!!baseFolder && !!settingsFile)
369 return errorSyntax(USAGE_CREATEVM, "Either --basefolder or --settingsfile must be specified");
370
371 do
372 {
373 ComPtr<IMachine> machine;
374
375 if (!settingsFile)
376 CHECK_ERROR_BREAK(a->virtualBox,
377 CreateMachine(name, osTypeId, baseFolder, Guid(id).toUtf16(), machine.asOutParam()));
378 else
379 CHECK_ERROR_BREAK(a->virtualBox,
380 CreateLegacyMachine(name, osTypeId, settingsFile, Guid(id).toUtf16(), machine.asOutParam()));
381
382 CHECK_ERROR_BREAK(machine, SaveSettings());
383 if (fRegister)
384 {
385 CHECK_ERROR_BREAK(a->virtualBox, RegisterMachine(machine));
386 }
387 Bstr uuid;
388 CHECK_ERROR_BREAK(machine, COMGETTER(Id)(uuid.asOutParam()));
389 CHECK_ERROR_BREAK(machine, COMGETTER(SettingsFilePath)(settingsFile.asOutParam()));
390 RTPrintf("Virtual machine '%ls' is created%s.\n"
391 "UUID: %s\n"
392 "Settings file: '%ls'\n",
393 name.raw(), fRegister ? " and registered" : "",
394 Utf8Str(uuid).raw(), settingsFile.raw());
395 }
396 while (0);
397
398 return SUCCEEDED(rc) ? 0 : 1;
399}
400
401/**
402 * Parses a number.
403 *
404 * @returns Valid number on success.
405 * @returns 0 if invalid number. All necesary bitching has been done.
406 * @param psz Pointer to the nic number.
407 */
408unsigned parseNum(const char *psz, unsigned cMaxNum, const char *name)
409{
410 uint32_t u32;
411 char *pszNext;
412 int rc = RTStrToUInt32Ex(psz, &pszNext, 10, &u32);
413 if ( RT_SUCCESS(rc)
414 && *pszNext == '\0'
415 && u32 >= 1
416 && u32 <= cMaxNum)
417 return (unsigned)u32;
418 errorArgument("Invalid %s number '%s'", name, psz);
419 return 0;
420}
421
422
423/** @todo refine this after HDD changes; MSC 8.0/64 has trouble with handleModifyVM. */
424#if defined(_MSC_VER)
425# pragma optimize("", on)
426#endif
427
428static const RTGETOPTDEF g_aStartVMOptions[] =
429{
430 { "--type", 't', RTGETOPT_REQ_STRING },
431 { "-type", 't', RTGETOPT_REQ_STRING }, // deprecated
432};
433
434static int handleStartVM(HandlerArg *a)
435{
436 HRESULT rc;
437 const char *VMName = NULL;
438 Bstr sessionType = "gui";
439
440 int c;
441 RTGETOPTUNION ValueUnion;
442 RTGETOPTSTATE GetState;
443 // start at 0 because main() has hacked both the argc and argv given to us
444 RTGetOptInit(&GetState, a->argc, a->argv, g_aStartVMOptions, RT_ELEMENTS(g_aStartVMOptions), 0, 0 /* fFlags */);
445 while ((c = RTGetOpt(&GetState, &ValueUnion)))
446 {
447 switch (c)
448 {
449 case 't': // --type
450 if (!RTStrICmp(ValueUnion.psz, "gui"))
451 {
452 sessionType = "gui";
453 }
454#ifdef VBOX_WITH_VBOXSDL
455 else if (!RTStrICmp(ValueUnion.psz, "sdl"))
456 {
457 sessionType = "sdl";
458 }
459#endif
460#ifdef VBOX_WITH_VRDP
461 else if (!RTStrICmp(ValueUnion.psz, "vrdp"))
462 {
463 sessionType = "vrdp";
464 }
465#endif
466#ifdef VBOX_WITH_HEADLESS
467 else if (!RTStrICmp(ValueUnion.psz, "capture"))
468 {
469 sessionType = "capture";
470 }
471 else if (!RTStrICmp(ValueUnion.psz, "headless"))
472 {
473 sessionType = "headless";
474 }
475#endif
476 else
477 return errorArgument("Invalid session type '%s'", ValueUnion.psz);
478 break;
479
480 case VINF_GETOPT_NOT_OPTION:
481 if (!VMName)
482 VMName = ValueUnion.psz;
483 else
484 return errorSyntax(USAGE_STARTVM, "Invalid parameter '%s'", ValueUnion.psz);
485 break;
486
487 default:
488 if (c > 0)
489 {
490 if (RT_C_IS_PRINT(c))
491 return errorSyntax(USAGE_STARTVM, "Invalid option -%c", c);
492 else
493 return errorSyntax(USAGE_STARTVM, "Invalid option case %i", c);
494 }
495 else if (c == VERR_GETOPT_UNKNOWN_OPTION)
496 return errorSyntax(USAGE_STARTVM, "unknown option: %s\n", ValueUnion.psz);
497 else if (ValueUnion.pDef)
498 return errorSyntax(USAGE_STARTVM, "%s: %Rrs", ValueUnion.pDef->pszLong, c);
499 else
500 return errorSyntax(USAGE_STARTVM, "error: %Rrs", c);
501 }
502 }
503
504 /* check for required options */
505 if (!VMName)
506 return errorSyntax(USAGE_STARTVM, "VM name required");
507
508 ComPtr<IMachine> machine;
509 /* assume it's a UUID */
510 rc = a->virtualBox->GetMachine(Guid(VMName).toUtf16(), machine.asOutParam());
511 if (FAILED(rc) || !machine)
512 {
513 /* must be a name */
514 CHECK_ERROR(a->virtualBox, FindMachine(Bstr(VMName), machine.asOutParam()));
515 }
516 if (machine)
517 {
518 Bstr uuid;
519 machine->COMGETTER(Id)(uuid.asOutParam());
520
521
522 Bstr env;
523#if defined(RT_OS_LINUX) || defined(RT_OS_SOLARIS)
524 /* make sure the VM process will start on the same display as VBoxManage */
525 {
526 const char *display = RTEnvGet ("DISPLAY");
527 if (display)
528 env = Utf8StrFmt ("DISPLAY=%s", display);
529 }
530#endif
531 ComPtr<IProgress> progress;
532 CHECK_ERROR_RET(a->virtualBox, OpenRemoteSession(a->session, uuid, sessionType,
533 env, progress.asOutParam()), rc);
534 RTPrintf("Waiting for the remote session to open...\n");
535 CHECK_ERROR_RET(progress, WaitForCompletion (-1), 1);
536
537 BOOL completed;
538 CHECK_ERROR_RET(progress, COMGETTER(Completed)(&completed), rc);
539 ASSERT(completed);
540
541 LONG iRc;
542 CHECK_ERROR_RET(progress, COMGETTER(ResultCode)(&iRc), rc);
543 if (FAILED(iRc))
544 {
545 ComPtr <IVirtualBoxErrorInfo> errorInfo;
546 CHECK_ERROR_RET(progress, COMGETTER(ErrorInfo)(errorInfo.asOutParam()), 1);
547 ErrorInfo info (errorInfo);
548 com::GluePrintErrorInfo(info);
549 }
550 else
551 {
552 RTPrintf("Remote session has been successfully opened.\n");
553 }
554 }
555
556 /* it's important to always close sessions */
557 a->session->Close();
558
559 return SUCCEEDED(rc) ? 0 : 1;
560}
561
562static int handleControlVM(HandlerArg *a)
563{
564 HRESULT rc;
565
566 if (a->argc < 2)
567 return errorSyntax(USAGE_CONTROLVM, "Not enough parameters");
568
569 /* try to find the given machine */
570 ComPtr <IMachine> machine;
571 Bstr uuid (a->argv[0]);
572 if (!Guid(uuid).isEmpty())
573 {
574 CHECK_ERROR (a->virtualBox, GetMachine (uuid, machine.asOutParam()));
575 }
576 else
577 {
578 CHECK_ERROR (a->virtualBox, FindMachine (uuid, machine.asOutParam()));
579 if (SUCCEEDED (rc))
580 machine->COMGETTER(Id) (uuid.asOutParam());
581 }
582 if (FAILED (rc))
583 return 1;
584
585 /* open a session for the VM */
586 CHECK_ERROR_RET (a->virtualBox, OpenExistingSession (a->session, uuid), 1);
587
588 do
589 {
590 /* get the associated console */
591 ComPtr<IConsole> console;
592 CHECK_ERROR_BREAK (a->session, COMGETTER(Console)(console.asOutParam()));
593 /* ... and session machine */
594 ComPtr<IMachine> sessionMachine;
595 CHECK_ERROR_BREAK (a->session, COMGETTER(Machine)(sessionMachine.asOutParam()));
596
597 /* which command? */
598 if (!strcmp(a->argv[1], "pause"))
599 {
600 CHECK_ERROR_BREAK (console, Pause());
601 }
602 else if (!strcmp(a->argv[1], "resume"))
603 {
604 CHECK_ERROR_BREAK (console, Resume());
605 }
606 else if (!strcmp(a->argv[1], "reset"))
607 {
608 CHECK_ERROR_BREAK (console, Reset());
609 }
610 else if (!strcmp(a->argv[1], "poweroff"))
611 {
612 ComPtr<IProgress> progress;
613 CHECK_ERROR_BREAK (console, PowerDown(progress.asOutParam()));
614
615 showProgress(progress);
616
617 LONG iRc;
618 progress->COMGETTER(ResultCode)(&iRc);
619 if (FAILED(iRc))
620 {
621 com::ProgressErrorInfo info(progress);
622 if (info.isBasicAvailable())
623 {
624 RTPrintf("Error: failed to power off machine. Error message: %lS\n", info.getText().raw());
625 }
626 else
627 {
628 RTPrintf("Error: failed to power off machine. No error message available!\n");
629 }
630 }
631 }
632 else if (!strcmp(a->argv[1], "savestate"))
633 {
634 ComPtr<IProgress> progress;
635 CHECK_ERROR_BREAK (console, SaveState(progress.asOutParam()));
636
637 showProgress(progress);
638
639 LONG iRc;
640 progress->COMGETTER(ResultCode)(&iRc);
641 if (FAILED(iRc))
642 {
643 com::ProgressErrorInfo info(progress);
644 if (info.isBasicAvailable())
645 {
646 RTPrintf("Error: failed to save machine state. Error message: %lS\n", info.getText().raw());
647 }
648 else
649 {
650 RTPrintf("Error: failed to save machine state. No error message available!\n");
651 }
652 }
653 }
654 else if (!strcmp(a->argv[1], "acpipowerbutton"))
655 {
656 CHECK_ERROR_BREAK (console, PowerButton());
657 }
658 else if (!strcmp(a->argv[1], "acpisleepbutton"))
659 {
660 CHECK_ERROR_BREAK (console, SleepButton());
661 }
662 else if (!strcmp(a->argv[1], "injectnmi"))
663 {
664 /* get the machine debugger. */
665 ComPtr <IMachineDebugger> debugger;
666 CHECK_ERROR_BREAK(console, COMGETTER(Debugger)(debugger.asOutParam()));
667 CHECK_ERROR_BREAK(debugger, InjectNMI());
668 }
669 else if (!strcmp(a->argv[1], "keyboardputscancode"))
670 {
671 ComPtr<IKeyboard> keyboard;
672 CHECK_ERROR_BREAK(console, COMGETTER(Keyboard)(keyboard.asOutParam()));
673
674 if (a->argc <= 1 + 1)
675 {
676 errorArgument("Missing argument to '%s'. Expected IBM PC AT set 2 keyboard scancode(s) as hex byte(s).", a->argv[1]);
677 rc = E_FAIL;
678 break;
679 }
680
681 /* Arbitrary restrict the length of a sequence of scancodes to 1024. */
682 LONG alScancodes[1024];
683 int cScancodes = 0;
684
685 /* Process the command line. */
686 int i;
687 for (i = 1 + 1; i < a->argc && cScancodes < (int)RT_ELEMENTS(alScancodes); i++, cScancodes++)
688 {
689 if ( RT_C_IS_XDIGIT (a->argv[i][0])
690 && RT_C_IS_XDIGIT (a->argv[i][1])
691 && a->argv[i][2] == 0)
692 {
693 uint8_t u8Scancode;
694 int rc = RTStrToUInt8Ex(a->argv[i], NULL, 16, &u8Scancode);
695 if (RT_FAILURE (rc))
696 {
697 RTPrintf("Error: converting '%s' returned %Rrc!\n", a->argv[i], rc);
698 rc = E_FAIL;
699 break;
700 }
701
702 alScancodes[cScancodes] = u8Scancode;
703 }
704 else
705 {
706 RTPrintf("Error: '%s' is not a hex byte!\n", a->argv[i]);
707 rc = E_FAIL;
708 break;
709 }
710 }
711
712 if (FAILED(rc))
713 break;
714
715 if ( cScancodes == RT_ELEMENTS(alScancodes)
716 && i < a->argc)
717 {
718 RTPrintf("Error: too many scancodes, maximum %d allowed!\n", RT_ELEMENTS(alScancodes));
719 rc = E_FAIL;
720 break;
721 }
722
723 /* Send scancodes to the VM.
724 * Note: 'PutScancodes' did not work here. Only the first scancode was transmitted.
725 */
726 for (i = 0; i < cScancodes; i++)
727 {
728 CHECK_ERROR_BREAK(keyboard, PutScancode(alScancodes[i]));
729 RTPrintf("Scancode[%d]: 0x%02X\n", i, alScancodes[i]);
730 }
731 }
732 else if (!strncmp(a->argv[1], "setlinkstate", 12))
733 {
734 /* Get the number of network adapters */
735 ULONG NetworkAdapterCount = 0;
736 ComPtr <ISystemProperties> info;
737 CHECK_ERROR_BREAK (a->virtualBox, COMGETTER(SystemProperties) (info.asOutParam()));
738 CHECK_ERROR_BREAK (info, COMGETTER(NetworkAdapterCount) (&NetworkAdapterCount));
739
740 unsigned n = parseNum(&a->argv[1][12], NetworkAdapterCount, "NIC");
741 if (!n)
742 {
743 rc = E_FAIL;
744 break;
745 }
746 if (a->argc <= 1 + 1)
747 {
748 errorArgument("Missing argument to '%s'", a->argv[1]);
749 rc = E_FAIL;
750 break;
751 }
752 /* get the corresponding network adapter */
753 ComPtr<INetworkAdapter> adapter;
754 CHECK_ERROR_BREAK (sessionMachine, GetNetworkAdapter(n - 1, adapter.asOutParam()));
755 if (adapter)
756 {
757 if (!strcmp(a->argv[2], "on"))
758 {
759 CHECK_ERROR_BREAK (adapter, COMSETTER(CableConnected)(TRUE));
760 }
761 else if (!strcmp(a->argv[2], "off"))
762 {
763 CHECK_ERROR_BREAK (adapter, COMSETTER(CableConnected)(FALSE));
764 }
765 else
766 {
767 errorArgument("Invalid link state '%s'", Utf8Str(a->argv[2]).raw());
768 rc = E_FAIL;
769 break;
770 }
771 }
772 }
773#ifdef VBOX_DYNAMIC_NET_ATTACH
774 else if (!strncmp(a->argv[1], "nic", 3))
775 {
776 /* Get the number of network adapters */
777 ULONG NetworkAdapterCount = 0;
778 ComPtr <ISystemProperties> info;
779 CHECK_ERROR_BREAK (a->virtualBox, COMGETTER(SystemProperties) (info.asOutParam()));
780 CHECK_ERROR_BREAK (info, COMGETTER(NetworkAdapterCount) (&NetworkAdapterCount));
781
782 unsigned n = parseNum(&a->argv[1][3], NetworkAdapterCount, "NIC");
783 if (!n)
784 {
785 rc = E_FAIL;
786 break;
787 }
788 if (a->argc <= 2)
789 {
790 errorArgument("Missing argument to '%s'", a->argv[1]);
791 rc = E_FAIL;
792 break;
793 }
794
795 /* get the corresponding network adapter */
796 ComPtr<INetworkAdapter> adapter;
797 CHECK_ERROR_BREAK (sessionMachine, GetNetworkAdapter(n - 1, adapter.asOutParam()));
798 if (adapter)
799 {
800 BOOL fEnabled;
801 adapter->COMGETTER(Enabled)(&fEnabled);
802 if (fEnabled)
803 {
804 if (!strcmp(a->argv[2], "null"))
805 {
806 CHECK_ERROR_RET(adapter, COMSETTER(Enabled) (TRUE), 1);
807 CHECK_ERROR_RET(adapter, Detach(), 1);
808 }
809 else if (!strcmp(a->argv[2], "nat"))
810 {
811 CHECK_ERROR_RET(adapter, COMSETTER(Enabled) (TRUE), 1);
812 if (a->argc == 4)
813 CHECK_ERROR_RET(adapter, COMSETTER(NATNetwork)(Bstr(a->argv[3])), 1);
814 CHECK_ERROR_RET(adapter, AttachToNAT(), 1);
815 }
816 else if ( !strcmp(a->argv[2], "bridged")
817 || !strcmp(a->argv[2], "hostif")) /* backward compatibility */
818 {
819 if (a->argc <= 3)
820 {
821 errorArgument("Missing argument to '%s'", a->argv[2]);
822 rc = E_FAIL;
823 break;
824 }
825 CHECK_ERROR_RET(adapter, COMSETTER(Enabled) (TRUE), 1);
826 CHECK_ERROR_RET(adapter, COMSETTER(HostInterface)(Bstr(a->argv[3])), 1);
827 CHECK_ERROR_RET(adapter, AttachToBridgedInterface(), 1);
828 }
829 else if (!strcmp(a->argv[2], "intnet"))
830 {
831 if (a->argc <= 3)
832 {
833 errorArgument("Missing argument to '%s'", a->argv[2]);
834 rc = E_FAIL;
835 break;
836 }
837 CHECK_ERROR_RET(adapter, COMSETTER(Enabled) (TRUE), 1);
838 CHECK_ERROR_RET(adapter, COMSETTER(InternalNetwork)(Bstr(a->argv[3])), 1);
839 CHECK_ERROR_RET(adapter, AttachToInternalNetwork(), 1);
840 }
841#if defined(VBOX_WITH_NETFLT)
842 else if (!strcmp(a->argv[2], "hostonly"))
843 {
844 if (a->argc <= 3)
845 {
846 errorArgument("Missing argument to '%s'", a->argv[2]);
847 rc = E_FAIL;
848 break;
849 }
850 CHECK_ERROR_RET(adapter, COMSETTER(Enabled) (TRUE), 1);
851 CHECK_ERROR_RET(adapter, COMSETTER(HostInterface)(Bstr(a->argv[3])), 1);
852 CHECK_ERROR_RET(adapter, AttachToHostOnlyInterface(), 1);
853 }
854#endif
855 else
856 {
857 errorArgument("Invalid type '%s' specfied for NIC %lu", Utf8Str(a->argv[2]).raw(), n);
858 rc = E_FAIL;
859 break;
860 }
861 }
862 else
863 {
864 RTPrintf("The NIC %d is currently disabled and thus can't change its attachment type\n", n);
865 }
866 }
867 }
868#endif /* VBOX_DYNAMIC_NET_ATTACH */
869#ifdef VBOX_WITH_VRDP
870 else if (!strcmp(a->argv[1], "vrdp"))
871 {
872 if (a->argc <= 1 + 1)
873 {
874 errorArgument("Missing argument to '%s'", a->argv[1]);
875 rc = E_FAIL;
876 break;
877 }
878 /* get the corresponding VRDP server */
879 ComPtr<IVRDPServer> vrdpServer;
880 sessionMachine->COMGETTER(VRDPServer)(vrdpServer.asOutParam());
881 ASSERT(vrdpServer);
882 if (vrdpServer)
883 {
884 if (!strcmp(a->argv[2], "on"))
885 {
886 CHECK_ERROR_BREAK (vrdpServer, COMSETTER(Enabled)(TRUE));
887 }
888 else if (!strcmp(a->argv[2], "off"))
889 {
890 CHECK_ERROR_BREAK (vrdpServer, COMSETTER(Enabled)(FALSE));
891 }
892 else
893 {
894 errorArgument("Invalid vrdp server state '%s'", Utf8Str(a->argv[2]).raw());
895 rc = E_FAIL;
896 break;
897 }
898 }
899 }
900 else if (!strcmp(a->argv[1], "vrdpport"))
901 {
902 if (a->argc <= 1 + 1)
903 {
904 errorArgument("Missing argument to '%s'", a->argv[1]);
905 rc = E_FAIL;
906 break;
907 }
908 /* get the corresponding VRDP server */
909 ComPtr<IVRDPServer> vrdpServer;
910 sessionMachine->COMGETTER(VRDPServer)(vrdpServer.asOutParam());
911 ASSERT(vrdpServer);
912 if (vrdpServer)
913 {
914 uint16_t vrdpport;
915
916 if (!strcmp(a->argv[2], "default"))
917 {
918 vrdpport = 0;
919 }
920 else
921 {
922 int vrc = RTStrToUInt16Full(a->argv[2], 0, &vrdpport);
923
924 if (vrc != VINF_SUCCESS)
925 {
926 vrdpport = UINT16_MAX;
927 }
928 }
929
930 if (vrdpport != UINT16_MAX)
931 {
932 CHECK_ERROR_BREAK(vrdpServer, COMSETTER(Port)(vrdpport));
933 }
934 else
935 {
936 errorArgument("Invalid vrdp server port '%s'", Utf8Str(a->argv[2]).raw());
937 rc = E_FAIL;
938 break;
939 }
940 }
941 }
942#endif /* VBOX_WITH_VRDP */
943 else if ( !strcmp (a->argv[1], "usbattach")
944 || !strcmp (a->argv[1], "usbdetach"))
945 {
946 if (a->argc < 3)
947 {
948 errorSyntax(USAGE_CONTROLVM, "Not enough parameters");
949 rc = E_FAIL;
950 break;
951 }
952
953 bool attach = !strcmp(a->argv[1], "usbattach");
954
955 Bstr usbId = a->argv [2];
956 if (Guid(usbId).isEmpty())
957 {
958 // assume address
959 if (attach)
960 {
961 ComPtr <IHost> host;
962 CHECK_ERROR_BREAK (a->virtualBox, COMGETTER(Host) (host.asOutParam()));
963 SafeIfaceArray <IHostUSBDevice> coll;
964 CHECK_ERROR_BREAK (host, COMGETTER(USBDevices) (ComSafeArrayAsOutParam(coll)));
965 ComPtr <IHostUSBDevice> dev;
966 CHECK_ERROR_BREAK (host, FindUSBDeviceByAddress (Bstr (a->argv [2]), dev.asOutParam()));
967 CHECK_ERROR_BREAK (dev, COMGETTER(Id) (usbId.asOutParam()));
968 }
969 else
970 {
971 SafeIfaceArray <IUSBDevice> coll;
972 CHECK_ERROR_BREAK (console, COMGETTER(USBDevices)(ComSafeArrayAsOutParam(coll)));
973 ComPtr <IUSBDevice> dev;
974 CHECK_ERROR_BREAK (console, FindUSBDeviceByAddress (Bstr (a->argv [2]),
975 dev.asOutParam()));
976 CHECK_ERROR_BREAK (dev, COMGETTER(Id) (usbId.asOutParam()));
977 }
978 }
979
980 if (attach)
981 CHECK_ERROR_BREAK (console, AttachUSBDevice (usbId));
982 else
983 {
984 ComPtr <IUSBDevice> dev;
985 CHECK_ERROR_BREAK (console, DetachUSBDevice (usbId, dev.asOutParam()));
986 }
987 }
988 else if (!strcmp(a->argv[1], "setvideomodehint"))
989 {
990 if (a->argc != 5 && a->argc != 6)
991 {
992 errorSyntax(USAGE_CONTROLVM, "Incorrect number of parameters");
993 rc = E_FAIL;
994 break;
995 }
996 uint32_t xres = RTStrToUInt32(a->argv[2]);
997 uint32_t yres = RTStrToUInt32(a->argv[3]);
998 uint32_t bpp = RTStrToUInt32(a->argv[4]);
999 uint32_t displayIdx = 0;
1000 if (a->argc == 6)
1001 displayIdx = RTStrToUInt32(a->argv[5]);
1002
1003 ComPtr<IDisplay> display;
1004 CHECK_ERROR_BREAK(console, COMGETTER(Display)(display.asOutParam()));
1005 CHECK_ERROR_BREAK(display, SetVideoModeHint(xres, yres, bpp, displayIdx));
1006 }
1007 else if (!strcmp(a->argv[1], "setcredentials"))
1008 {
1009 bool fAllowLocalLogon = true;
1010 if (a->argc == 7)
1011 {
1012 if ( strcmp(a->argv[5], "--allowlocallogon")
1013 && strcmp(a->argv[5], "-allowlocallogon"))
1014 {
1015 errorArgument("Invalid parameter '%s'", a->argv[5]);
1016 rc = E_FAIL;
1017 break;
1018 }
1019 if (!strcmp(a->argv[6], "no"))
1020 fAllowLocalLogon = false;
1021 }
1022 else if (a->argc != 5)
1023 {
1024 errorSyntax(USAGE_CONTROLVM, "Incorrect number of parameters");
1025 rc = E_FAIL;
1026 break;
1027 }
1028
1029 ComPtr<IGuest> guest;
1030 CHECK_ERROR_BREAK(console, COMGETTER(Guest)(guest.asOutParam()));
1031 CHECK_ERROR_BREAK(guest, SetCredentials(Bstr(a->argv[2]), Bstr(a->argv[3]), Bstr(a->argv[4]), fAllowLocalLogon));
1032 }
1033 else if (!strcmp(a->argv[1], "dvdattach"))
1034 {
1035 if (a->argc != 3)
1036 {
1037 errorSyntax(USAGE_CONTROLVM, "Incorrect number of parameters");
1038 rc = E_FAIL;
1039 break;
1040 }
1041 ComPtr<IDVDDrive> dvdDrive;
1042 sessionMachine->COMGETTER(DVDDrive)(dvdDrive.asOutParam());
1043 ASSERT(dvdDrive);
1044
1045 /* unmount? */
1046 if (!strcmp(a->argv[2], "none"))
1047 {
1048 CHECK_ERROR(dvdDrive, Unmount());
1049 }
1050 /* host drive? */
1051 else if (!strncmp(a->argv[2], "host:", 5))
1052 {
1053 ComPtr<IHost> host;
1054 CHECK_ERROR(a->virtualBox, COMGETTER(Host)(host.asOutParam()));
1055 com::SafeIfaceArray <IHostDVDDrive> hostDVDs;
1056 rc = host->COMGETTER(DVDDrives)(ComSafeArrayAsOutParam(hostDVDs));
1057
1058 ComPtr<IHostDVDDrive> hostDVDDrive;
1059 rc = host->FindHostDVDDrive(Bstr(a->argv[2] + 5), hostDVDDrive.asOutParam());
1060 if (!hostDVDDrive)
1061 {
1062 errorArgument("Invalid host DVD drive name");
1063 rc = E_FAIL;
1064 break;
1065 }
1066 CHECK_ERROR(dvdDrive, CaptureHostDrive(hostDVDDrive));
1067 }
1068 else
1069 {
1070 /* first assume it's a UUID */
1071 Bstr uuid(a->argv[2]);
1072 ComPtr<IDVDImage> dvdImage;
1073 rc = a->virtualBox->GetDVDImage(uuid, dvdImage.asOutParam());
1074 if (FAILED(rc) || !dvdImage)
1075 {
1076 /* must be a filename, check if it's in the collection */
1077 rc = a->virtualBox->FindDVDImage(Bstr(a->argv[2]), dvdImage.asOutParam());
1078 /* not registered, do that on the fly */
1079 if (!dvdImage)
1080 {
1081 Bstr emptyUUID;
1082 CHECK_ERROR(a->virtualBox, OpenDVDImage(Bstr(a->argv[2]), emptyUUID, dvdImage.asOutParam()));
1083 }
1084 }
1085 if (!dvdImage)
1086 {
1087 rc = E_FAIL;
1088 break;
1089 }
1090 dvdImage->COMGETTER(Id)(uuid.asOutParam());
1091 CHECK_ERROR(dvdDrive, MountImage(uuid));
1092 }
1093 }
1094 else if (!strcmp(a->argv[1], "floppyattach"))
1095 {
1096 if (a->argc != 3)
1097 {
1098 errorSyntax(USAGE_CONTROLVM, "Incorrect number of parameters");
1099 rc = E_FAIL;
1100 break;
1101 }
1102
1103 ComPtr<IFloppyDrive> floppyDrive;
1104 sessionMachine->COMGETTER(FloppyDrive)(floppyDrive.asOutParam());
1105 ASSERT(floppyDrive);
1106
1107 /* unmount? */
1108 if (!strcmp(a->argv[2], "none"))
1109 {
1110 CHECK_ERROR(floppyDrive, Unmount());
1111 }
1112 /* host drive? */
1113 else if (!strncmp(a->argv[2], "host:", 5))
1114 {
1115 ComPtr<IHost> host;
1116 CHECK_ERROR(a->virtualBox, COMGETTER(Host)(host.asOutParam()));
1117 com::SafeIfaceArray <IHostFloppyDrive> hostFloppies;
1118 rc = host->COMGETTER(FloppyDrives)(ComSafeArrayAsOutParam(hostFloppies));
1119 CheckComRCReturnRC (rc);
1120 ComPtr<IHostFloppyDrive> hostFloppyDrive;
1121 host->FindHostFloppyDrive(Bstr(a->argv[2] + 5), hostFloppyDrive.asOutParam());
1122 if (!hostFloppyDrive)
1123 {
1124 errorArgument("Invalid host floppy drive name");
1125 rc = E_FAIL;
1126 break;
1127 }
1128 CHECK_ERROR(floppyDrive, CaptureHostDrive(hostFloppyDrive));
1129 }
1130 else
1131 {
1132 /* first assume it's a UUID */
1133 Bstr uuid(a->argv[2]);
1134 ComPtr<IFloppyImage> floppyImage;
1135 rc = a->virtualBox->GetFloppyImage(uuid, floppyImage.asOutParam());
1136 if (FAILED(rc) || !floppyImage)
1137 {
1138 /* must be a filename, check if it's in the collection */
1139 rc = a->virtualBox->FindFloppyImage(Bstr(a->argv[2]), floppyImage.asOutParam());
1140 /* not registered, do that on the fly */
1141 if (!floppyImage)
1142 {
1143 Bstr emptyUUID;
1144 CHECK_ERROR(a->virtualBox, OpenFloppyImage(Bstr(a->argv[2]), emptyUUID, floppyImage.asOutParam()));
1145 }
1146 }
1147 if (!floppyImage)
1148 {
1149 rc = E_FAIL;
1150 break;
1151 }
1152 floppyImage->COMGETTER(Id)(uuid.asOutParam());
1153 CHECK_ERROR(floppyDrive, MountImage(uuid));
1154 }
1155 }
1156#ifdef VBOX_WITH_MEM_BALLOONING
1157 else if ( !strcmp(a->argv[1], "--guestmemoryballoon")
1158 || !strcmp(a->argv[1], "-guestmemoryballoon"))
1159 {
1160 if (a->argc != 3)
1161 {
1162 errorSyntax(USAGE_CONTROLVM, "Incorrect number of parameters");
1163 rc = E_FAIL;
1164 break;
1165 }
1166 uint32_t uVal;
1167 int vrc;
1168 vrc = RTStrToUInt32Ex(a->argv[2], NULL, 0, &uVal);
1169 if (vrc != VINF_SUCCESS)
1170 {
1171 errorArgument("Error parsing guest memory balloon size '%s'", a->argv[2]);
1172 rc = E_FAIL;
1173 break;
1174 }
1175
1176 /* guest is running; update IGuest */
1177 ComPtr <IGuest> guest;
1178
1179 rc = console->COMGETTER(Guest)(guest.asOutParam());
1180 if (SUCCEEDED(rc))
1181 CHECK_ERROR(guest, COMSETTER(MemoryBalloonSize)(uVal));
1182 }
1183#endif
1184 else if ( !strcmp(a->argv[1], "--gueststatisticsinterval")
1185 || !strcmp(a->argv[1], "-gueststatisticsinterval"))
1186 {
1187 if (a->argc != 3)
1188 {
1189 errorSyntax(USAGE_CONTROLVM, "Incorrect number of parameters");
1190 rc = E_FAIL;
1191 break;
1192 }
1193 uint32_t uVal;
1194 int vrc;
1195 vrc = RTStrToUInt32Ex(a->argv[2], NULL, 0, &uVal);
1196 if (vrc != VINF_SUCCESS)
1197 {
1198 errorArgument("Error parsing guest statistics interval '%s'", a->argv[2]);
1199 rc = E_FAIL;
1200 break;
1201 }
1202
1203 /* guest is running; update IGuest */
1204 ComPtr <IGuest> guest;
1205
1206 rc = console->COMGETTER(Guest)(guest.asOutParam());
1207 if (SUCCEEDED(rc))
1208 CHECK_ERROR(guest, COMSETTER(StatisticsUpdateInterval)(uVal));
1209 }
1210 else
1211 {
1212 errorSyntax(USAGE_CONTROLVM, "Invalid parameter '%s'", Utf8Str(a->argv[1]).raw());
1213 rc = E_FAIL;
1214 }
1215 }
1216 while (0);
1217
1218 a->session->Close();
1219
1220 return SUCCEEDED (rc) ? 0 : 1;
1221}
1222
1223static int handleDiscardState(HandlerArg *a)
1224{
1225 HRESULT rc;
1226
1227 if (a->argc != 1)
1228 return errorSyntax(USAGE_DISCARDSTATE, "Incorrect number of parameters");
1229
1230 ComPtr<IMachine> machine;
1231 /* assume it's a UUID */
1232 rc = a->virtualBox->GetMachine(Bstr(a->argv[0]), machine.asOutParam());
1233 if (FAILED(rc) || !machine)
1234 {
1235 /* must be a name */
1236 CHECK_ERROR(a->virtualBox, FindMachine(Bstr(a->argv[0]), machine.asOutParam()));
1237 }
1238 if (machine)
1239 {
1240 do
1241 {
1242 /* we have to open a session for this task */
1243 Bstr guid;
1244 machine->COMGETTER(Id)(guid.asOutParam());
1245 CHECK_ERROR_BREAK(a->virtualBox, OpenSession(a->session, guid));
1246 do
1247 {
1248 ComPtr<IConsole> console;
1249 CHECK_ERROR_BREAK(a->session, COMGETTER(Console)(console.asOutParam()));
1250 CHECK_ERROR_BREAK(console, ForgetSavedState(true));
1251 }
1252 while (0);
1253 CHECK_ERROR_BREAK(a->session, Close());
1254 }
1255 while (0);
1256 }
1257
1258 return SUCCEEDED(rc) ? 0 : 1;
1259}
1260
1261static int handleAdoptdState(HandlerArg *a)
1262{
1263 HRESULT rc;
1264
1265 if (a->argc != 2)
1266 return errorSyntax(USAGE_ADOPTSTATE, "Incorrect number of parameters");
1267
1268 ComPtr<IMachine> machine;
1269 /* assume it's a UUID */
1270 rc = a->virtualBox->GetMachine(Bstr(a->argv[0]), machine.asOutParam());
1271 if (FAILED(rc) || !machine)
1272 {
1273 /* must be a name */
1274 CHECK_ERROR(a->virtualBox, FindMachine(Bstr(a->argv[0]), machine.asOutParam()));
1275 }
1276 if (machine)
1277 {
1278 do
1279 {
1280 /* we have to open a session for this task */
1281 Bstr guid;
1282 machine->COMGETTER(Id)(guid.asOutParam());
1283 CHECK_ERROR_BREAK(a->virtualBox, OpenSession(a->session, guid));
1284 do
1285 {
1286 ComPtr<IConsole> console;
1287 CHECK_ERROR_BREAK(a->session, COMGETTER(Console)(console.asOutParam()));
1288 CHECK_ERROR_BREAK(console, AdoptSavedState (Bstr (a->argv[1])));
1289 }
1290 while (0);
1291 CHECK_ERROR_BREAK(a->session, Close());
1292 }
1293 while (0);
1294 }
1295
1296 return SUCCEEDED(rc) ? 0 : 1;
1297}
1298
1299static int handleGetExtraData(HandlerArg *a)
1300{
1301 HRESULT rc = S_OK;
1302
1303 if (a->argc != 2)
1304 return errorSyntax(USAGE_GETEXTRADATA, "Incorrect number of parameters");
1305
1306 /* global data? */
1307 if (!strcmp(a->argv[0], "global"))
1308 {
1309 /* enumeration? */
1310 if (!strcmp(a->argv[1], "enumerate"))
1311 {
1312 SafeArray<BSTR> aKeys;
1313 CHECK_ERROR(a->virtualBox, GetExtraDataKeys(ComSafeArrayAsOutParam(aKeys)));
1314
1315 for (size_t i = 0;
1316 i < aKeys.size();
1317 ++i)
1318 {
1319 Bstr bstrKey(aKeys[i]);
1320 Bstr bstrValue;
1321 CHECK_ERROR(a->virtualBox, GetExtraData(bstrKey, bstrValue.asOutParam()));
1322
1323 RTPrintf("Key: %lS, Value: %lS\n", bstrKey.raw(), bstrValue.raw());
1324 }
1325 }
1326 else
1327 {
1328 Bstr value;
1329 CHECK_ERROR(a->virtualBox, GetExtraData(Bstr(a->argv[1]), value.asOutParam()));
1330 if (!value.isEmpty())
1331 RTPrintf("Value: %lS\n", value.raw());
1332 else
1333 RTPrintf("No value set!\n");
1334 }
1335 }
1336 else
1337 {
1338 ComPtr<IMachine> machine;
1339 /* assume it's a UUID */
1340 rc = a->virtualBox->GetMachine(Bstr(a->argv[0]), machine.asOutParam());
1341 if (FAILED(rc) || !machine)
1342 {
1343 /* must be a name */
1344 CHECK_ERROR(a->virtualBox, FindMachine(Bstr(a->argv[0]), machine.asOutParam()));
1345 }
1346 if (machine)
1347 {
1348 /* enumeration? */
1349 if (!strcmp(a->argv[1], "enumerate"))
1350 {
1351 SafeArray<BSTR> aKeys;
1352 CHECK_ERROR(machine, GetExtraDataKeys(ComSafeArrayAsOutParam(aKeys)));
1353
1354 for (size_t i = 0;
1355 i < aKeys.size();
1356 ++i)
1357 {
1358 Bstr bstrKey(aKeys[i]);
1359 Bstr bstrValue;
1360 CHECK_ERROR(machine, GetExtraData(bstrKey, bstrValue.asOutParam()));
1361
1362 RTPrintf("Key: %lS, Value: %lS\n", bstrKey.raw(), bstrValue.raw());
1363 }
1364 }
1365 else
1366 {
1367 Bstr value;
1368 CHECK_ERROR(machine, GetExtraData(Bstr(a->argv[1]), value.asOutParam()));
1369 if (!value.isEmpty())
1370 RTPrintf("Value: %lS\n", value.raw());
1371 else
1372 RTPrintf("No value set!\n");
1373 }
1374 }
1375 }
1376 return SUCCEEDED(rc) ? 0 : 1;
1377}
1378
1379static int handleSetExtraData(HandlerArg *a)
1380{
1381 HRESULT rc = S_OK;
1382
1383 if (a->argc < 2)
1384 return errorSyntax(USAGE_SETEXTRADATA, "Not enough parameters");
1385
1386 /* global data? */
1387 if (!strcmp(a->argv[0], "global"))
1388 {
1389 if (a->argc < 3)
1390 CHECK_ERROR(a->virtualBox, SetExtraData(Bstr(a->argv[1]), NULL));
1391 else if (a->argc == 3)
1392 CHECK_ERROR(a->virtualBox, SetExtraData(Bstr(a->argv[1]), Bstr(a->argv[2])));
1393 else
1394 return errorSyntax(USAGE_SETEXTRADATA, "Too many parameters");
1395 }
1396 else
1397 {
1398 ComPtr<IMachine> machine;
1399 /* assume it's a UUID */
1400 rc = a->virtualBox->GetMachine(Bstr(a->argv[0]), machine.asOutParam());
1401 if (FAILED(rc) || !machine)
1402 {
1403 /* must be a name */
1404 CHECK_ERROR(a->virtualBox, FindMachine(Bstr(a->argv[0]), machine.asOutParam()));
1405 }
1406 if (machine)
1407 {
1408 if (a->argc < 3)
1409 CHECK_ERROR(machine, SetExtraData(Bstr(a->argv[1]), NULL));
1410 else if (a->argc == 3)
1411 CHECK_ERROR(machine, SetExtraData(Bstr(a->argv[1]), Bstr(a->argv[2])));
1412 else
1413 return errorSyntax(USAGE_SETEXTRADATA, "Too many parameters");
1414 }
1415 }
1416 return SUCCEEDED(rc) ? 0 : 1;
1417}
1418
1419static int handleSetProperty(HandlerArg *a)
1420{
1421 HRESULT rc;
1422
1423 /* there must be two arguments: property name and value */
1424 if (a->argc != 2)
1425 return errorSyntax(USAGE_SETPROPERTY, "Incorrect number of parameters");
1426
1427 ComPtr<ISystemProperties> systemProperties;
1428 a->virtualBox->COMGETTER(SystemProperties)(systemProperties.asOutParam());
1429
1430 if (!strcmp(a->argv[0], "hdfolder"))
1431 {
1432 /* reset to default? */
1433 if (!strcmp(a->argv[1], "default"))
1434 CHECK_ERROR(systemProperties, COMSETTER(DefaultHardDiskFolder)(NULL));
1435 else
1436 CHECK_ERROR(systemProperties, COMSETTER(DefaultHardDiskFolder)(Bstr(a->argv[1])));
1437 }
1438 else if (!strcmp(a->argv[0], "machinefolder"))
1439 {
1440 /* reset to default? */
1441 if (!strcmp(a->argv[1], "default"))
1442 CHECK_ERROR(systemProperties, COMSETTER(DefaultMachineFolder)(NULL));
1443 else
1444 CHECK_ERROR(systemProperties, COMSETTER(DefaultMachineFolder)(Bstr(a->argv[1])));
1445 }
1446 else if (!strcmp(a->argv[0], "vrdpauthlibrary"))
1447 {
1448 /* reset to default? */
1449 if (!strcmp(a->argv[1], "default"))
1450 CHECK_ERROR(systemProperties, COMSETTER(RemoteDisplayAuthLibrary)(NULL));
1451 else
1452 CHECK_ERROR(systemProperties, COMSETTER(RemoteDisplayAuthLibrary)(Bstr(a->argv[1])));
1453 }
1454 else if (!strcmp(a->argv[0], "websrvauthlibrary"))
1455 {
1456 /* reset to default? */
1457 if (!strcmp(a->argv[1], "default"))
1458 CHECK_ERROR(systemProperties, COMSETTER(WebServiceAuthLibrary)(NULL));
1459 else
1460 CHECK_ERROR(systemProperties, COMSETTER(WebServiceAuthLibrary)(Bstr(a->argv[1])));
1461 }
1462 else if (!strcmp(a->argv[0], "loghistorycount"))
1463 {
1464 uint32_t uVal;
1465 int vrc;
1466 vrc = RTStrToUInt32Ex(a->argv[1], NULL, 0, &uVal);
1467 if (vrc != VINF_SUCCESS)
1468 return errorArgument("Error parsing Log history count '%s'", a->argv[1]);
1469 CHECK_ERROR(systemProperties, COMSETTER(LogHistoryCount)(uVal));
1470 }
1471 else
1472 return errorSyntax(USAGE_SETPROPERTY, "Invalid parameter '%s'", a->argv[0]);
1473
1474 return SUCCEEDED(rc) ? 0 : 1;
1475}
1476
1477static int handleSharedFolder (HandlerArg *a)
1478{
1479 HRESULT rc;
1480
1481 /* we need at least a command and target */
1482 if (a->argc < 2)
1483 return errorSyntax(USAGE_SHAREDFOLDER, "Not enough parameters");
1484
1485 ComPtr<IMachine> machine;
1486 /* assume it's a UUID */
1487 rc = a->virtualBox->GetMachine(Bstr(a->argv[1]), machine.asOutParam());
1488 if (FAILED(rc) || !machine)
1489 {
1490 /* must be a name */
1491 CHECK_ERROR(a->virtualBox, FindMachine(Bstr(a->argv[1]), machine.asOutParam()));
1492 }
1493 if (!machine)
1494 return 1;
1495 Bstr uuid;
1496 machine->COMGETTER(Id)(uuid.asOutParam());
1497
1498 if (!strcmp(a->argv[0], "add"))
1499 {
1500 /* we need at least four more parameters */
1501 if (a->argc < 5)
1502 return errorSyntax(USAGE_SHAREDFOLDER_ADD, "Not enough parameters");
1503
1504 char *name = NULL;
1505 char *hostpath = NULL;
1506 bool fTransient = false;
1507 bool fWritable = true;
1508
1509 for (int i = 2; i < a->argc; i++)
1510 {
1511 if ( !strcmp(a->argv[i], "--name")
1512 || !strcmp(a->argv[i], "-name"))
1513 {
1514 if (a->argc <= i + 1 || !*a->argv[i+1])
1515 return errorArgument("Missing argument to '%s'", a->argv[i]);
1516 i++;
1517 name = a->argv[i];
1518 }
1519 else if ( !strcmp(a->argv[i], "--hostpath")
1520 || !strcmp(a->argv[i], "-hostpath"))
1521 {
1522 if (a->argc <= i + 1 || !*a->argv[i+1])
1523 return errorArgument("Missing argument to '%s'", a->argv[i]);
1524 i++;
1525 hostpath = a->argv[i];
1526 }
1527 else if ( !strcmp(a->argv[i], "--readonly")
1528 || !strcmp(a->argv[i], "-readonly"))
1529 {
1530 fWritable = false;
1531 }
1532 else if ( !strcmp(a->argv[i], "--transient")
1533 || !strcmp(a->argv[i], "-transient"))
1534 {
1535 fTransient = true;
1536 }
1537 else
1538 return errorSyntax(USAGE_SHAREDFOLDER_ADD, "Invalid parameter '%s'", Utf8Str(a->argv[i]).raw());
1539 }
1540
1541 if (NULL != strstr(name, " "))
1542 return errorSyntax(USAGE_SHAREDFOLDER_ADD, "No spaces allowed in parameter '-name'!");
1543
1544 /* required arguments */
1545 if (!name || !hostpath)
1546 {
1547 return errorSyntax(USAGE_SHAREDFOLDER_ADD, "Parameters --name and --hostpath are required");
1548 }
1549
1550 if (fTransient)
1551 {
1552 ComPtr <IConsole> console;
1553
1554 /* open an existing session for the VM */
1555 CHECK_ERROR_RET(a->virtualBox, OpenExistingSession (a->session, uuid), 1);
1556 /* get the session machine */
1557 CHECK_ERROR_RET(a->session, COMGETTER(Machine)(machine.asOutParam()), 1);
1558 /* get the session console */
1559 CHECK_ERROR_RET(a->session, COMGETTER(Console)(console.asOutParam()), 1);
1560
1561 CHECK_ERROR(console, CreateSharedFolder(Bstr(name), Bstr(hostpath), fWritable));
1562
1563 if (console)
1564 a->session->Close();
1565 }
1566 else
1567 {
1568 /* open a session for the VM */
1569 CHECK_ERROR_RET (a->virtualBox, OpenSession(a->session, uuid), 1);
1570
1571 /* get the mutable session machine */
1572 a->session->COMGETTER(Machine)(machine.asOutParam());
1573
1574 CHECK_ERROR(machine, CreateSharedFolder(Bstr(name), Bstr(hostpath), fWritable));
1575
1576 if (SUCCEEDED(rc))
1577 CHECK_ERROR(machine, SaveSettings());
1578
1579 a->session->Close();
1580 }
1581 }
1582 else if (!strcmp(a->argv[0], "remove"))
1583 {
1584 /* we need at least two more parameters */
1585 if (a->argc < 3)
1586 return errorSyntax(USAGE_SHAREDFOLDER_REMOVE, "Not enough parameters");
1587
1588 char *name = NULL;
1589 bool fTransient = false;
1590
1591 for (int i = 2; i < a->argc; i++)
1592 {
1593 if ( !strcmp(a->argv[i], "--name")
1594 || !strcmp(a->argv[i], "-name"))
1595 {
1596 if (a->argc <= i + 1 || !*a->argv[i+1])
1597 return errorArgument("Missing argument to '%s'", a->argv[i]);
1598 i++;
1599 name = a->argv[i];
1600 }
1601 else if ( !strcmp(a->argv[i], "--transient")
1602 || !strcmp(a->argv[i], "-transient"))
1603 {
1604 fTransient = true;
1605 }
1606 else
1607 return errorSyntax(USAGE_SHAREDFOLDER_REMOVE, "Invalid parameter '%s'", Utf8Str(a->argv[i]).raw());
1608 }
1609
1610 /* required arguments */
1611 if (!name)
1612 return errorSyntax(USAGE_SHAREDFOLDER_REMOVE, "Parameter --name is required");
1613
1614 if (fTransient)
1615 {
1616 ComPtr <IConsole> console;
1617
1618 /* open an existing session for the VM */
1619 CHECK_ERROR_RET(a->virtualBox, OpenExistingSession (a->session, uuid), 1);
1620 /* get the session machine */
1621 CHECK_ERROR_RET(a->session, COMGETTER(Machine)(machine.asOutParam()), 1);
1622 /* get the session console */
1623 CHECK_ERROR_RET(a->session, COMGETTER(Console)(console.asOutParam()), 1);
1624
1625 CHECK_ERROR(console, RemoveSharedFolder(Bstr(name)));
1626
1627 if (console)
1628 a->session->Close();
1629 }
1630 else
1631 {
1632 /* open a session for the VM */
1633 CHECK_ERROR_RET (a->virtualBox, OpenSession(a->session, uuid), 1);
1634
1635 /* get the mutable session machine */
1636 a->session->COMGETTER(Machine)(machine.asOutParam());
1637
1638 CHECK_ERROR(machine, RemoveSharedFolder(Bstr(name)));
1639
1640 /* commit and close the session */
1641 CHECK_ERROR(machine, SaveSettings());
1642 a->session->Close();
1643 }
1644 }
1645 else
1646 return errorSyntax(USAGE_SETPROPERTY, "Invalid parameter '%s'", Utf8Str(a->argv[0]).raw());
1647
1648 return 0;
1649}
1650
1651static int handleVMStatistics(HandlerArg *a)
1652{
1653 HRESULT rc;
1654
1655 /* at least one option: the UUID or name of the VM */
1656 if (a->argc < 1)
1657 return errorSyntax(USAGE_VM_STATISTICS, "Incorrect number of parameters");
1658
1659 /* try to find the given machine */
1660 ComPtr <IMachine> machine;
1661 Bstr uuid (a->argv[0]);
1662 if (!Guid (a->argv[0]).isEmpty())
1663 CHECK_ERROR(a->virtualBox, GetMachine(uuid, machine.asOutParam()));
1664 else
1665 {
1666 CHECK_ERROR(a->virtualBox, FindMachine(Bstr(a->argv[0]), machine.asOutParam()));
1667 if (SUCCEEDED (rc))
1668 machine->COMGETTER(Id)(uuid.asOutParam());
1669 }
1670 if (FAILED(rc))
1671 return 1;
1672
1673 /* parse arguments. */
1674 bool fReset = false;
1675 bool fWithDescriptions = false;
1676 const char *pszPattern = NULL; /* all */
1677 for (int i = 1; i < a->argc; i++)
1678 {
1679 if ( !strcmp(a->argv[i], "--pattern")
1680 || !strcmp(a->argv[i], "-pattern"))
1681 {
1682 if (pszPattern)
1683 return errorSyntax(USAGE_VM_STATISTICS, "Multiple --patterns options is not permitted");
1684 if (i + 1 >= a->argc)
1685 return errorArgument("Missing argument to '%s'", a->argv[i]);
1686 pszPattern = a->argv[++i];
1687 }
1688 else if ( !strcmp(a->argv[i], "--descriptions")
1689 || !strcmp(a->argv[i], "-descriptions"))
1690 fWithDescriptions = true;
1691 /* add: --file <filename> and --formatted */
1692 else if ( !strcmp(a->argv[i], "--reset")
1693 || !strcmp(a->argv[i], "-reset"))
1694 fReset = true;
1695 else
1696 return errorSyntax(USAGE_VM_STATISTICS, "Unknown option '%s'", a->argv[i]);
1697 }
1698 if (fReset && fWithDescriptions)
1699 return errorSyntax(USAGE_VM_STATISTICS, "The --reset and --descriptions options does not mix");
1700
1701
1702 /* open an existing session for the VM. */
1703 CHECK_ERROR(a->virtualBox, OpenExistingSession(a->session, uuid));
1704 if (SUCCEEDED(rc))
1705 {
1706 /* get the session console. */
1707 ComPtr <IConsole> console;
1708 CHECK_ERROR(a->session, COMGETTER(Console)(console.asOutParam()));
1709 if (SUCCEEDED(rc))
1710 {
1711 /* get the machine debugger. */
1712 ComPtr <IMachineDebugger> debugger;
1713 CHECK_ERROR(console, COMGETTER(Debugger)(debugger.asOutParam()));
1714 if (SUCCEEDED(rc))
1715 {
1716 if (fReset)
1717 CHECK_ERROR(debugger, ResetStats(Bstr(pszPattern)));
1718 else
1719 {
1720 Bstr stats;
1721 CHECK_ERROR(debugger, GetStats(Bstr(pszPattern), fWithDescriptions, stats.asOutParam()));
1722 if (SUCCEEDED(rc))
1723 {
1724 /* if (fFormatted)
1725 { big mess }
1726 else
1727 */
1728 RTPrintf("%ls\n", stats.raw());
1729 }
1730 }
1731 }
1732 a->session->Close();
1733 }
1734 }
1735
1736 return SUCCEEDED(rc) ? 0 : 1;
1737}
1738#endif /* !VBOX_ONLY_DOCS */
1739
1740// main
1741///////////////////////////////////////////////////////////////////////////////
1742
1743int main(int argc, char *argv[])
1744{
1745 /*
1746 * Before we do anything, init the runtime without loading
1747 * the support driver.
1748 */
1749 RTR3Init();
1750
1751 bool fShowLogo = true;
1752 int iCmd = 1;
1753 int iCmdArg;
1754
1755 /* global options */
1756 for (int i = 1; i < argc || argc <= iCmd; i++)
1757 {
1758 if ( argc <= iCmd
1759 || !strcmp(argv[i], "help")
1760 || !strcmp(argv[i], "-?")
1761 || !strcmp(argv[i], "-h")
1762 || !strcmp(argv[i], "-help")
1763 || !strcmp(argv[i], "--help"))
1764 {
1765 showLogo();
1766 printUsage(USAGE_ALL);
1767 return 0;
1768 }
1769 else if ( !strcmp(argv[i], "-v")
1770 || !strcmp(argv[i], "-version")
1771 || !strcmp(argv[i], "-Version")
1772 || !strcmp(argv[i], "--version"))
1773 {
1774 /* Print version number, and do nothing else. */
1775 RTPrintf("%sr%d\n", VBOX_VERSION_STRING, RTBldCfgRevision());
1776 return 0;
1777 }
1778 else if ( !strcmp(argv[i], "--dumpopts")
1779 || !strcmp(argv[i], "-dumpopts"))
1780 {
1781 /* Special option to dump really all commands,
1782 * even the ones not understood on this platform. */
1783 printUsage(USAGE_DUMPOPTS);
1784 return 0;
1785 }
1786 else if ( !strcmp(argv[i], "--nologo")
1787 || !strcmp(argv[i], "-nologo")
1788 || !strcmp(argv[i], "-q"))
1789 {
1790 /* suppress the logo */
1791 fShowLogo = false;
1792 iCmd++;
1793 }
1794 else
1795 {
1796 break;
1797 }
1798 }
1799
1800 iCmdArg = iCmd + 1;
1801
1802 if (fShowLogo)
1803 showLogo();
1804
1805
1806#ifdef VBOX_ONLY_DOCS
1807 int rc = 0;
1808#else /* !VBOX_ONLY_DOCS */
1809 HRESULT rc = 0;
1810
1811 rc = com::Initialize();
1812 if (FAILED(rc))
1813 {
1814 RTPrintf("ERROR: failed to initialize COM!\n");
1815 return rc;
1816 }
1817
1818 /*
1819 * The input is in the host OS'es codepage (NT guarantees ACP).
1820 * For VBox we use UTF-8 and convert to UCS-2 when calling (XP)COM APIs.
1821 * For simplicity, just convert the argv[] array here.
1822 */
1823 for (int i = iCmdArg; i < argc; i++)
1824 {
1825 char *converted;
1826 RTStrCurrentCPToUtf8(&converted, argv[i]);
1827 argv[i] = converted;
1828 }
1829
1830 do
1831 {
1832 // scopes all the stuff till shutdown
1833 ////////////////////////////////////////////////////////////////////////////
1834
1835 /* convertfromraw: does not need a VirtualBox instantiation. */
1836 if (argc >= iCmdArg && ( !strcmp(argv[iCmd], "convertfromraw")
1837 || !strcmp(argv[iCmd], "convertdd")))
1838 {
1839 rc = handleConvertFromRaw(argc - iCmdArg, argv + iCmdArg);
1840 break;
1841 }
1842
1843 ComPtr<IVirtualBox> virtualBox;
1844 ComPtr<ISession> session;
1845
1846 rc = virtualBox.createLocalObject(CLSID_VirtualBox);
1847 if (FAILED(rc))
1848 RTPrintf("ERROR: failed to create the VirtualBox object!\n");
1849 else
1850 {
1851 rc = session.createInprocObject(CLSID_Session);
1852 if (FAILED(rc))
1853 RTPrintf("ERROR: failed to create a session object!\n");
1854 }
1855
1856 if (FAILED(rc))
1857 {
1858 com::ErrorInfo info;
1859 if (!info.isFullAvailable() && !info.isBasicAvailable())
1860 {
1861 com::GluePrintRCMessage(rc);
1862 RTPrintf("Most likely, the VirtualBox COM server is not running or failed to start.\n");
1863 }
1864 else
1865 com::GluePrintErrorInfo(info);
1866 break;
1867 }
1868
1869 HandlerArg handlerArg = { 0, NULL, virtualBox, session };
1870
1871 /*
1872 * All registered command handlers
1873 */
1874 struct
1875 {
1876 const char *command;
1877 PFNHANDLER handler;
1878 } commandHandlers[] =
1879 {
1880 { "internalcommands", handleInternalCommands },
1881 { "list", handleList },
1882 { "showvminfo", handleShowVMInfo },
1883 { "registervm", handleRegisterVM },
1884 { "unregistervm", handleUnregisterVM },
1885 { "createhd", handleCreateHardDisk },
1886 { "createvdi", handleCreateHardDisk }, /* backward compatiblity */
1887 { "modifyhd", handleModifyHardDisk },
1888 { "modifyvdi", handleModifyHardDisk }, /* backward compatiblity */
1889 { "clonehd", handleCloneHardDisk },
1890 { "clonevdi", handleCloneHardDisk }, /* backward compatiblity */
1891 { "addiscsidisk", handleAddiSCSIDisk },
1892 { "createvm", handleCreateVM },
1893 { "modifyvm", handleModifyVM },
1894 { "startvm", handleStartVM },
1895 { "controlvm", handleControlVM },
1896 { "discardstate", handleDiscardState },
1897 { "adoptstate", handleAdoptdState },
1898 { "snapshot", handleSnapshot },
1899 { "openmedium", handleOpenMedium },
1900 { "registerimage", handleOpenMedium }, /* backward compatiblity */
1901 { "closemedium", handleCloseMedium },
1902 { "unregisterimage", handleCloseMedium }, /* backward compatiblity */
1903 { "showhdinfo", handleShowHardDiskInfo },
1904 { "showvdiinfo", handleShowHardDiskInfo }, /* backward compatiblity */
1905 { "getextradata", handleGetExtraData },
1906 { "setextradata", handleSetExtraData },
1907 { "setproperty", handleSetProperty },
1908 { "usbfilter", handleUSBFilter },
1909 { "sharedfolder", handleSharedFolder },
1910 { "vmstatistics", handleVMStatistics },
1911#ifdef VBOX_WITH_GUEST_PROPS
1912 { "guestproperty", handleGuestProperty },
1913#endif /* VBOX_WITH_GUEST_PROPS defined */
1914 { "metrics", handleMetrics },
1915 { "import", handleImportAppliance },
1916 { "export", handleExportAppliance },
1917#if defined(VBOX_WITH_NETFLT)
1918 { "hostonlyif", handleHostonlyIf },
1919#endif
1920 { "dhcpserver", handleDHCPServer},
1921 { NULL, NULL }
1922 };
1923
1924 int commandIndex;
1925 for (commandIndex = 0; commandHandlers[commandIndex].command != NULL; commandIndex++)
1926 {
1927 if (!strcmp(commandHandlers[commandIndex].command, argv[iCmd]))
1928 {
1929 handlerArg.argc = argc - iCmdArg;
1930 handlerArg.argv = &argv[iCmdArg];
1931
1932 rc = commandHandlers[commandIndex].handler(&handlerArg);
1933 break;
1934 }
1935 }
1936 if (!commandHandlers[commandIndex].command)
1937 {
1938 rc = errorSyntax(USAGE_ALL, "Invalid command '%s'", Utf8Str(argv[iCmd]).raw());
1939 }
1940
1941 /* Although all handlers should always close the session if they open it,
1942 * we do it here just in case if some of the handlers contains a bug --
1943 * leaving the direct session not closed will turn the machine state to
1944 * Aborted which may have unwanted side effects like killing the saved
1945 * state file (if the machine was in the Saved state before). */
1946 session->Close();
1947
1948 EventQueue::getMainEventQueue()->processEventQueue(0);
1949 // end "all-stuff" scope
1950 ////////////////////////////////////////////////////////////////////////////
1951 }
1952 while (0);
1953
1954 com::Shutdown();
1955#endif /* !VBOX_ONLY_DOCS */
1956
1957 /*
1958 * Free converted argument vector
1959 */
1960 for (int i = iCmdArg; i < argc; i++)
1961 RTStrFree(argv[i]);
1962
1963 return rc != 0;
1964}
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