VirtualBox

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

Last change on this file since 86435 was 86434, checked in by vboxsync, 4 years ago

VBoxManage: Added --dump-build-type so valkit/vbox.py has a better chance of detecting the build type. bugref:9841

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 26.9 KB
Line 
1/* $Id: VBoxManage.cpp 86434 2020-10-02 21:33:45Z vboxsync $ */
2/** @file
3 * VBoxManage - VirtualBox's command-line interface.
4 */
5
6/*
7 * Copyright (C) 2006-2020 Oracle Corporation
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
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#ifndef VBOX_ONLY_DOCS
23# include <VBox/com/com.h>
24# include <VBox/com/string.h>
25# include <VBox/com/Guid.h>
26# include <VBox/com/array.h>
27# include <VBox/com/ErrorInfo.h>
28# include <VBox/com/errorprint.h>
29# include <VBox/com/NativeEventQueue.h>
30
31# include <VBox/com/VirtualBox.h>
32#endif /* !VBOX_ONLY_DOCS */
33
34#include <VBox/version.h>
35
36#include <iprt/asm.h>
37#include <iprt/buildconfig.h>
38#include <iprt/ctype.h>
39#include <iprt/file.h>
40#include <iprt/getopt.h>
41#include <iprt/initterm.h>
42#include <iprt/path.h>
43#include <iprt/stream.h>
44#include <iprt/string.h>
45
46#include <signal.h>
47
48#include "VBoxManage.h"
49
50
51/*********************************************************************************************************************************
52* Defined Constants And Macros *
53*********************************************************************************************************************************/
54/** The command doesn't need the COM stuff. */
55#define VBMG_CMD_F_NO_COM RT_BIT_32(0)
56
57#define VBMG_CMD_TODO HELP_CMD_VBOXMANAGE_INVALID
58
59
60/*********************************************************************************************************************************
61* Structures and Typedefs *
62*********************************************************************************************************************************/
63#ifndef VBOX_ONLY_DOCS
64/**
65 * VBoxManage command descriptor.
66 */
67typedef struct VBMGCMD
68{
69 /** The command. */
70 const char *pszCommand;
71 /** The help category. */
72 USAGECATEGORY enmHelpCat;
73 /** The new help command. */
74 enum HELP_CMD_VBOXMANAGE enmCmdHelp;
75 /** The handler. */
76 RTEXITCODE (*pfnHandler)(HandlerArg *pArg);
77 /** VBMG_CMD_F_XXX, */
78 uint32_t fFlags;
79} VBMGCMD;
80/** Pointer to a const VBoxManage command descriptor. */
81typedef VBMGCMD const *PCVBMGCMD;
82#endif
83
84
85/*********************************************************************************************************************************
86* Global Variables *
87*********************************************************************************************************************************/
88/*extern*/ bool g_fDetailedProgress = false;
89
90#ifndef VBOX_ONLY_DOCS
91/** Set by the signal handler. */
92static volatile bool g_fCanceled = false;
93
94
95/**
96 * All registered command handlers
97 */
98static const VBMGCMD g_aCommands[] =
99{
100 { "internalcommands", USAGE_INVALID, VBMG_CMD_TODO, handleInternalCommands, 0 },
101 { "list", USAGE_LIST, VBMG_CMD_TODO, handleList, 0 },
102 { "showvminfo", USAGE_SHOWVMINFO, VBMG_CMD_TODO, handleShowVMInfo, 0 },
103 { "registervm", USAGE_REGISTERVM, VBMG_CMD_TODO, handleRegisterVM, 0 },
104 { "unregistervm", USAGE_UNREGISTERVM, VBMG_CMD_TODO, handleUnregisterVM, 0 },
105 { "clonevm", USAGE_S_NEWCMD, HELP_CMD_CLONEVM, handleCloneVM, 0 },
106 { "movevm", USAGE_MOVEVM, VBMG_CMD_TODO, handleMoveVM, 0 },
107 { "mediumproperty", USAGE_MEDIUMPROPERTY, VBMG_CMD_TODO, handleMediumProperty, 0 },
108 { "hdproperty", USAGE_MEDIUMPROPERTY, VBMG_CMD_TODO, handleMediumProperty, 0 }, /* backward compatibility */
109 { "createmedium", USAGE_CREATEMEDIUM, VBMG_CMD_TODO, handleCreateMedium, 0 },
110 { "createhd", USAGE_CREATEMEDIUM, VBMG_CMD_TODO, handleCreateMedium, 0 }, /* backward compatibility */
111 { "createvdi", USAGE_CREATEMEDIUM, VBMG_CMD_TODO, handleCreateMedium, 0 }, /* backward compatibility */
112 { "modifymedium", USAGE_MODIFYMEDIUM, VBMG_CMD_TODO, handleModifyMedium, 0 },
113 { "modifyhd", USAGE_MODIFYMEDIUM, VBMG_CMD_TODO, handleModifyMedium, 0 }, /* backward compatibility */
114 { "modifyvdi", USAGE_MODIFYMEDIUM, VBMG_CMD_TODO, handleModifyMedium, 0 }, /* backward compatibility */
115 { "clonemedium", USAGE_CLONEMEDIUM, VBMG_CMD_TODO, handleCloneMedium, 0 },
116 { "clonehd", USAGE_CLONEMEDIUM, VBMG_CMD_TODO, handleCloneMedium, 0 }, /* backward compatibility */
117 { "clonevdi", USAGE_CLONEMEDIUM, VBMG_CMD_TODO, handleCloneMedium, 0 }, /* backward compatibility */
118 { "encryptmedium", USAGE_ENCRYPTMEDIUM, VBMG_CMD_TODO, handleEncryptMedium, 0 },
119 { "checkmediumpwd", USAGE_MEDIUMENCCHKPWD, VBMG_CMD_TODO, handleCheckMediumPassword, 0 },
120 { "createvm", USAGE_CREATEVM, VBMG_CMD_TODO, handleCreateVM, 0 },
121 { "modifyvm", USAGE_MODIFYVM, VBMG_CMD_TODO, handleModifyVM, 0 },
122 { "startvm", USAGE_STARTVM, VBMG_CMD_TODO, handleStartVM, 0 },
123 { "controlvm", USAGE_CONTROLVM, VBMG_CMD_TODO, handleControlVM, 0 },
124 { "unattended", USAGE_S_NEWCMD, HELP_CMD_UNATTENDED, handleUnattended, 0 },
125 { "discardstate", USAGE_DISCARDSTATE, VBMG_CMD_TODO, handleDiscardState, 0 },
126 { "adoptstate", USAGE_ADOPTSTATE, VBMG_CMD_TODO, handleAdoptState, 0 },
127 { "snapshot", USAGE_S_NEWCMD, HELP_CMD_SNAPSHOT, handleSnapshot, 0 },
128 { "closemedium", USAGE_CLOSEMEDIUM, VBMG_CMD_TODO, handleCloseMedium, 0 },
129 { "storageattach", USAGE_STORAGEATTACH, VBMG_CMD_TODO, handleStorageAttach, 0 },
130 { "storagectl", USAGE_STORAGECONTROLLER,VBMG_CMD_TODO, handleStorageController, 0 },
131 { "showmediuminfo", USAGE_SHOWMEDIUMINFO, VBMG_CMD_TODO, handleShowMediumInfo, 0 },
132 { "showhdinfo", USAGE_SHOWMEDIUMINFO, VBMG_CMD_TODO, handleShowMediumInfo, 0 }, /* backward compatibility */
133 { "showvdiinfo", USAGE_SHOWMEDIUMINFO, VBMG_CMD_TODO, handleShowMediumInfo, 0 }, /* backward compatibility */
134 { "mediumio", USAGE_S_NEWCMD, HELP_CMD_MEDIUMIO, handleMediumIO, 0 },
135 { "getextradata", USAGE_GETEXTRADATA, VBMG_CMD_TODO, handleGetExtraData, 0 },
136 { "setextradata", USAGE_SETEXTRADATA, VBMG_CMD_TODO, handleSetExtraData, 0 },
137 { "setproperty", USAGE_SETPROPERTY, VBMG_CMD_TODO, handleSetProperty, 0 },
138 { "usbfilter", USAGE_USBFILTER, VBMG_CMD_TODO, handleUSBFilter, 0 },
139 { "sharedfolder", USAGE_S_NEWCMD, HELP_CMD_SHAREDFOLDER, handleSharedFolder, 0 },
140#ifdef VBOX_WITH_GUEST_PROPS
141 { "guestproperty", USAGE_GUESTPROPERTY, VBMG_CMD_TODO, handleGuestProperty, 0 },
142#endif
143#ifdef VBOX_WITH_GUEST_CONTROL
144 { "guestcontrol", USAGE_GUESTCONTROL, VBMG_CMD_TODO, handleGuestControl, 0 },
145#endif
146 { "metrics", USAGE_METRICS, VBMG_CMD_TODO, handleMetrics, 0 },
147 { "import", USAGE_IMPORTAPPLIANCE, VBMG_CMD_TODO, handleImportAppliance, 0 },
148 { "export", USAGE_EXPORTAPPLIANCE, VBMG_CMD_TODO, handleExportAppliance, 0 },
149 { "signova", USAGE_S_NEWCMD, HELP_CMD_SIGNOVA, handleSignAppliance, VBMG_CMD_F_NO_COM },
150#ifdef VBOX_WITH_NETFLT
151 { "hostonlyif", USAGE_HOSTONLYIFS, VBMG_CMD_TODO, handleHostonlyIf, 0 },
152#endif
153 { "dhcpserver", USAGE_S_NEWCMD, HELP_CMD_DHCPSERVER, handleDHCPServer, 0 },
154#ifdef VBOX_WITH_NAT_SERVICE
155 { "natnetwork", USAGE_NATNETWORK, VBMG_CMD_TODO, handleNATNetwork, 0 },
156#endif
157 { "extpack", USAGE_S_NEWCMD, HELP_CMD_EXTPACK, handleExtPack, 0 },
158 { "bandwidthctl", USAGE_BANDWIDTHCONTROL, VBMG_CMD_TODO, handleBandwidthControl, 0 },
159 { "debugvm", USAGE_S_NEWCMD, HELP_CMD_DEBUGVM, handleDebugVM, 0 },
160 { "convertfromraw", USAGE_CONVERTFROMRAW, VBMG_CMD_TODO, handleConvertFromRaw, VBMG_CMD_F_NO_COM },
161 { "convertdd", USAGE_CONVERTFROMRAW, VBMG_CMD_TODO, handleConvertFromRaw, VBMG_CMD_F_NO_COM },
162 { "usbdevsource", USAGE_USBDEVSOURCE, VBMG_CMD_TODO, handleUSBDevSource, 0 },
163 { "cloudprofile", USAGE_S_NEWCMD, HELP_CMD_CLOUDPROFILE, handleCloudProfile, 0 },
164 { "cloud", USAGE_S_NEWCMD, HELP_CMD_CLOUD, handleCloud, 0 },
165 { "updatecheck", USAGE_S_NEWCMD, HELP_CMD_UPDATECHECK, handleUpdateCheck, 0 }
166};
167
168/**
169 * Looks up a command by name.
170 *
171 * @returns Pointer to the command structure.
172 * @param pszCommand Name of the command.
173 */
174static PCVBMGCMD lookupCommand(const char *pszCommand)
175{
176 if (pszCommand)
177 for (uint32_t i = 0; i < RT_ELEMENTS(g_aCommands); i++)
178 if (!strcmp(g_aCommands[i].pszCommand, pszCommand))
179 return &g_aCommands[i];
180 return NULL;
181}
182
183
184/**
185 * Signal handler that sets g_fCanceled.
186 *
187 * This can be executed on any thread in the process, on Windows it may even be
188 * a thread dedicated to delivering this signal. Do not doing anything
189 * unnecessary here.
190 */
191static void showProgressSignalHandler(int iSignal) RT_NOTHROW_DEF
192{
193 NOREF(iSignal);
194 ASMAtomicWriteBool(&g_fCanceled, true);
195}
196
197/**
198 * Print out progress on the console.
199 *
200 * This runs the main event queue every now and then to prevent piling up
201 * unhandled things (which doesn't cause real problems, just makes things
202 * react a little slower than in the ideal case).
203 */
204HRESULT showProgress(ComPtr<IProgress> progress)
205{
206 using namespace com;
207
208 AssertReturn(progress.isNotNull(), E_FAIL);
209
210 BOOL fCompleted = FALSE;
211 ULONG ulCurrentPercent = 0;
212 ULONG ulLastPercent = 0;
213
214 ULONG ulLastOperationPercent = (ULONG)-1;
215
216 ULONG ulLastOperation = (ULONG)-1;
217 Bstr bstrOperationDescription;
218
219 NativeEventQueue::getMainEventQueue()->processEventQueue(0);
220
221 ULONG cOperations = 1;
222 HRESULT hrc = progress->COMGETTER(OperationCount)(&cOperations);
223 if (FAILED(hrc))
224 {
225 RTStrmPrintf(g_pStdErr, "Progress object failure: %Rhrc\n", hrc);
226 RTStrmFlush(g_pStdErr);
227 return hrc;
228 }
229
230 /*
231 * Note: Outputting the progress info to stderr (g_pStdErr) is intentional
232 * to not get intermixed with other (raw) stdout data which might get
233 * written in the meanwhile.
234 */
235
236 if (!g_fDetailedProgress)
237 {
238 RTStrmPrintf(g_pStdErr, "0%%...");
239 RTStrmFlush(g_pStdErr);
240 }
241
242 /* setup signal handling if cancelable */
243 bool fCanceledAlready = false;
244 BOOL fCancelable;
245 hrc = progress->COMGETTER(Cancelable)(&fCancelable);
246 if (FAILED(hrc))
247 fCancelable = FALSE;
248 if (fCancelable)
249 {
250 signal(SIGINT, showProgressSignalHandler);
251 signal(SIGTERM, showProgressSignalHandler);
252#ifdef SIGBREAK
253 signal(SIGBREAK, showProgressSignalHandler);
254#endif
255 }
256
257 hrc = progress->COMGETTER(Completed(&fCompleted));
258 while (SUCCEEDED(hrc))
259 {
260 progress->COMGETTER(Percent(&ulCurrentPercent));
261
262 if (g_fDetailedProgress)
263 {
264 ULONG ulOperation = 1;
265 hrc = progress->COMGETTER(Operation)(&ulOperation);
266 if (FAILED(hrc))
267 break;
268 ULONG ulCurrentOperationPercent = 0;
269 hrc = progress->COMGETTER(OperationPercent(&ulCurrentOperationPercent));
270 if (FAILED(hrc))
271 break;
272
273 if (ulLastOperation != ulOperation)
274 {
275 hrc = progress->COMGETTER(OperationDescription(bstrOperationDescription.asOutParam()));
276 if (FAILED(hrc))
277 break;
278 ulLastPercent = (ULONG)-1; // force print
279 ulLastOperation = ulOperation;
280 }
281
282 if ( ulCurrentPercent != ulLastPercent
283 || ulCurrentOperationPercent != ulLastOperationPercent
284 )
285 {
286 LONG lSecsRem = 0;
287 progress->COMGETTER(TimeRemaining)(&lSecsRem);
288
289 RTStrmPrintf(g_pStdErr, "(%u/%u) %ls %02u%% => %02u%% (%d s remaining)\n", ulOperation + 1, cOperations,
290 bstrOperationDescription.raw(), ulCurrentOperationPercent, ulCurrentPercent, lSecsRem);
291 ulLastPercent = ulCurrentPercent;
292 ulLastOperationPercent = ulCurrentOperationPercent;
293 }
294 }
295 else
296 {
297 /* did we cross a 10% mark? */
298 if (ulCurrentPercent / 10 > ulLastPercent / 10)
299 {
300 /* make sure to also print out missed steps */
301 for (ULONG curVal = (ulLastPercent / 10) * 10 + 10; curVal <= (ulCurrentPercent / 10) * 10; curVal += 10)
302 {
303 if (curVal < 100)
304 {
305 RTStrmPrintf(g_pStdErr, "%u%%...", curVal);
306 RTStrmFlush(g_pStdErr);
307 }
308 }
309 ulLastPercent = (ulCurrentPercent / 10) * 10;
310 }
311 }
312 if (fCompleted)
313 break;
314
315 /* process async cancelation */
316 if (g_fCanceled && !fCanceledAlready)
317 {
318 hrc = progress->Cancel();
319 if (SUCCEEDED(hrc))
320 fCanceledAlready = true;
321 else
322 g_fCanceled = false;
323 }
324
325 /* make sure the loop is not too tight */
326 progress->WaitForCompletion(100);
327
328 NativeEventQueue::getMainEventQueue()->processEventQueue(0);
329 hrc = progress->COMGETTER(Completed(&fCompleted));
330 }
331
332 /* undo signal handling */
333 if (fCancelable)
334 {
335 signal(SIGINT, SIG_DFL);
336 signal(SIGTERM, SIG_DFL);
337# ifdef SIGBREAK
338 signal(SIGBREAK, SIG_DFL);
339# endif
340 }
341
342 /* complete the line. */
343 LONG iRc = E_FAIL;
344 hrc = progress->COMGETTER(ResultCode)(&iRc);
345 if (SUCCEEDED(hrc))
346 {
347 if (SUCCEEDED(iRc))
348 RTStrmPrintf(g_pStdErr, "100%%\n");
349 else if (g_fCanceled)
350 RTStrmPrintf(g_pStdErr, "CANCELED\n");
351 else
352 {
353 if (!g_fDetailedProgress)
354 RTStrmPrintf(g_pStdErr, "\n");
355 RTStrmPrintf(g_pStdErr, "Progress state: %Rhrc\n", iRc);
356 }
357 hrc = iRc;
358 }
359 else
360 {
361 if (!g_fDetailedProgress)
362 RTStrmPrintf(g_pStdErr, "\n");
363 RTStrmPrintf(g_pStdErr, "Progress object failure: %Rhrc\n", hrc);
364 }
365 RTStrmFlush(g_pStdErr);
366 return hrc;
367}
368
369#endif /* !VBOX_ONLY_DOCS */
370
371
372int main(int argc, char *argv[])
373{
374 /*
375 * Before we do anything, init the runtime without loading
376 * the support driver.
377 */
378 int vrc = RTR3InitExe(argc, &argv, 0);
379 if (RT_FAILURE(vrc))
380 return RTMsgInitFailure(vrc);
381#if defined(RT_OS_WINDOWS) && !defined(VBOX_ONLY_DOCS)
382 ATL::CComModule _Module; /* Required internally by ATL (constructor records instance in global variable). */
383#endif
384
385 /*
386 * Parse the global options
387 */
388 bool fShowLogo = false;
389 bool fShowHelp = false;
390 int iCmd = 1;
391 int iCmdArg;
392 const char *pszSettingsPw = NULL;
393 const char *pszSettingsPwFile = NULL;
394#ifndef VBOX_ONLY_DOCS
395 int cResponseFileArgs = 0;
396 char **papszResponseFileArgs = NULL;
397 char **papszNewArgv = NULL;
398#endif
399
400 for (int i = 1; i < argc || argc <= iCmd; i++)
401 {
402 if ( argc <= iCmd
403 || !strcmp(argv[i], "help")
404 || !strcmp(argv[i], "--help")
405 || !strcmp(argv[i], "-?")
406 || !strcmp(argv[i], "-h")
407 || !strcmp(argv[i], "-help"))
408 {
409 if (i >= argc - 1)
410 {
411 showLogo(g_pStdOut);
412 printUsage(USAGE_S_ALL, RTMSGREFENTRYSTR_SCOPE_GLOBAL, g_pStdOut);
413 return 0;
414 }
415 fShowLogo = true;
416 fShowHelp = true;
417 iCmd++;
418 continue;
419 }
420
421#ifndef VBOX_ONLY_DOCS
422 if ( !strcmp(argv[i], "-V")
423 || !strcmp(argv[i], "--version")
424 || !strcmp(argv[i], "-v") /* deprecated */
425 || !strcmp(argv[i], "-version") /* deprecated */
426 || !strcmp(argv[i], "-Version") /* deprecated */)
427 {
428 /* Print version number, and do nothing else. */
429 RTPrintf("%sr%u\n", VBOX_VERSION_STRING, RTBldCfgRevision());
430 return 0;
431 }
432 if (!strcmp(argv[i], "--dump-build-type"))
433 {
434 /* Print the build type, and do nothing else. (Used by ValKit to detect build type.) */
435 RTPrintf("%s\n", RTBldCfgType());
436 return 0;
437 }
438#endif
439
440 if ( !strcmp(argv[i], "--dumpopts")
441 || !strcmp(argv[i], "-dumpopts") /* deprecated */)
442 {
443 /* Special option to dump really all commands,
444 * even the ones not understood on this platform. */
445 printUsage(USAGE_S_DUMPOPTS, RTMSGREFENTRYSTR_SCOPE_GLOBAL, g_pStdOut);
446 return 0;
447 }
448
449 if ( !strcmp(argv[i], "--nologo")
450 || !strcmp(argv[i], "-q")
451 || !strcmp(argv[i], "-nologo") /* deprecated */)
452 {
453 /* suppress the logo */
454 fShowLogo = false;
455 iCmd++;
456 }
457 else if ( !strcmp(argv[i], "--detailed-progress")
458 || !strcmp(argv[i], "-d"))
459 {
460 /* detailed progress report */
461 g_fDetailedProgress = true;
462 iCmd++;
463 }
464 else if (!strcmp(argv[i], "--settingspw"))
465 {
466 if (i >= argc - 1)
467 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Password expected");
468 /* password for certain settings */
469 pszSettingsPw = argv[i + 1];
470 iCmd += 2;
471 }
472 else if (!strcmp(argv[i], "--settingspwfile"))
473 {
474 if (i >= argc-1)
475 return RTMsgErrorExit(RTEXITCODE_FAILURE, "No password file specified");
476 pszSettingsPwFile = argv[i+1];
477 iCmd += 2;
478 }
479#ifndef VBOX_ONLY_DOCS
480 else if (argv[i][0] == '@')
481 {
482 if (papszResponseFileArgs)
483 return RTMsgErrorExitFailure("Only one response file allowed");
484
485 /* Load response file, making sure it's valid UTF-8. */
486 char *pszResponseFile;
487 size_t cbResponseFile;
488 vrc = RTFileReadAllEx(&argv[i][1], 0, RTFOFF_MAX, RTFILE_RDALL_O_DENY_NONE | RTFILE_RDALL_F_TRAILING_ZERO_BYTE,
489 (void **)&pszResponseFile, &cbResponseFile);
490 if (RT_FAILURE(vrc))
491 return RTMsgErrorExitFailure("Error reading response file '%s': %Rrc", &argv[i][1], vrc);
492 vrc = RTStrValidateEncoding(pszResponseFile);
493 if (RT_FAILURE(vrc))
494 {
495 RTFileReadAllFree(pszResponseFile, cbResponseFile);
496 return RTMsgErrorExitFailure("Invalid response file ('%s') encoding: %Rrc", &argv[i][1], vrc);
497 }
498
499 /* Parse it. */
500 vrc = RTGetOptArgvFromString(&papszResponseFileArgs, &cResponseFileArgs, pszResponseFile,
501 RTGETOPTARGV_CNV_QUOTE_BOURNE_SH, NULL);
502 RTFileReadAllFree(pszResponseFile, cbResponseFile);
503 if (RT_FAILURE(vrc))
504 return RTMsgErrorExitFailure("Failed to parse response file '%s' (bourne shell style): %Rrc", &argv[i][1], vrc);
505
506 /* Construct new argv+argc with the response file arguments inserted. */
507 int cNewArgs = argc + cResponseFileArgs;
508 papszNewArgv = (char **)RTMemAllocZ((cNewArgs + 2) * sizeof(papszNewArgv[0]));
509 if (!papszNewArgv)
510 return RTMsgErrorExitFailure("out of memory");
511 memcpy(&papszNewArgv[0], &argv[0], sizeof(argv[0]) * (i + 1));
512 memcpy(&papszNewArgv[i + 1], papszResponseFileArgs, sizeof(argv[0]) * cResponseFileArgs);
513 memcpy(&papszNewArgv[i + 1 + cResponseFileArgs], &argv[i + 1], sizeof(argv[0]) * (argc - i - 1 + 1));
514 argv = papszNewArgv;
515 argc = argc + cResponseFileArgs;
516
517 iCmd++;
518 }
519#endif
520 else
521 break;
522 }
523
524 iCmdArg = iCmd + 1;
525
526 /*
527 * Show the logo and lookup the command and deal with fShowHelp = true.
528 */
529 if (fShowLogo)
530 showLogo(g_pStdOut);
531
532#ifndef VBOX_ONLY_DOCS
533 PCVBMGCMD pCmd = lookupCommand(argv[iCmd]);
534 if (pCmd && pCmd->enmCmdHelp != VBMG_CMD_TODO)
535 setCurrentCommand(pCmd->enmCmdHelp);
536
537 if ( pCmd
538 && ( fShowHelp
539 || ( argc - iCmdArg == 0
540 && pCmd->enmHelpCat != USAGE_INVALID)))
541 {
542 if (pCmd->enmCmdHelp == VBMG_CMD_TODO)
543 printUsage(pCmd->enmHelpCat, RTMSGREFENTRYSTR_SCOPE_GLOBAL, g_pStdOut);
544 else if (fShowHelp)
545 printHelp(g_pStdOut);
546 else
547 printUsage(g_pStdOut);
548 return RTEXITCODE_FAILURE; /* error */
549 }
550 if (!pCmd)
551 {
552 if (!strcmp(argv[iCmd], "commands"))
553 {
554 RTPrintf("commands:\n");
555 for (unsigned i = 0; i < RT_ELEMENTS(g_aCommands); i++)
556 if ( i == 0 /* skip backwards compatibility entries */
557 || (g_aCommands[i].enmHelpCat != USAGE_S_NEWCMD
558 ? g_aCommands[i].enmHelpCat != g_aCommands[i - 1].enmHelpCat
559 : g_aCommands[i].enmCmdHelp != g_aCommands[i - 1].enmCmdHelp))
560 RTPrintf(" %s\n", g_aCommands[i].pszCommand);
561 return RTEXITCODE_SUCCESS;
562 }
563 return errorSyntax(USAGE_S_ALL, "Invalid command '%s'", argv[iCmd]);
564 }
565
566 RTEXITCODE rcExit;
567 if (!(pCmd->fFlags & VBMG_CMD_F_NO_COM))
568 {
569 /*
570 * Initialize COM.
571 */
572 using namespace com;
573 HRESULT hrc = com::Initialize();
574 if (FAILED(hrc))
575 {
576# ifdef VBOX_WITH_XPCOM
577 if (hrc == NS_ERROR_FILE_ACCESS_DENIED)
578 {
579 char szHome[RTPATH_MAX] = "";
580 com::GetVBoxUserHomeDirectory(szHome, sizeof(szHome));
581 return RTMsgErrorExit(RTEXITCODE_FAILURE,
582 "Failed to initialize COM because the global settings directory '%s' is not accessible!", szHome);
583 }
584# endif
585 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to initialize COM! (hrc=%Rhrc)", hrc);
586 }
587
588
589 /*
590 * Get the remote VirtualBox object and create a local session object.
591 */
592 rcExit = RTEXITCODE_FAILURE;
593 ComPtr<IVirtualBoxClient> virtualBoxClient;
594 ComPtr<IVirtualBox> virtualBox;
595 hrc = virtualBoxClient.createInprocObject(CLSID_VirtualBoxClient);
596 if (SUCCEEDED(hrc))
597 hrc = virtualBoxClient->COMGETTER(VirtualBox)(virtualBox.asOutParam());
598 if (SUCCEEDED(hrc))
599 {
600 ComPtr<ISession> session;
601 hrc = session.createInprocObject(CLSID_Session);
602 if (SUCCEEDED(hrc))
603 {
604 /* Session secret. */
605 if (pszSettingsPw)
606 CHECK_ERROR2I_STMT(virtualBox, SetSettingsSecret(Bstr(pszSettingsPw).raw()), rcExit = RTEXITCODE_FAILURE);
607 else if (pszSettingsPwFile)
608 rcExit = settingsPasswordFile(virtualBox, pszSettingsPwFile);
609 else
610 rcExit = RTEXITCODE_SUCCESS;
611 if (rcExit == RTEXITCODE_SUCCESS)
612 {
613 /*
614 * Call the handler.
615 */
616 HandlerArg handlerArg = { argc - iCmdArg, &argv[iCmdArg], virtualBox, session };
617 rcExit = pCmd->pfnHandler(&handlerArg);
618
619 /* Although all handlers should always close the session if they open it,
620 * we do it here just in case if some of the handlers contains a bug --
621 * leaving the direct session not closed will turn the machine state to
622 * Aborted which may have unwanted side effects like killing the saved
623 * state file (if the machine was in the Saved state before). */
624 session->UnlockMachine();
625 }
626
627 NativeEventQueue::getMainEventQueue()->processEventQueue(0);
628 }
629 else
630 {
631 com::ErrorInfo info;
632 RTMsgError("Failed to create a session object!");
633 if (!info.isFullAvailable() && !info.isBasicAvailable())
634 com::GluePrintRCMessage(hrc);
635 else
636 com::GluePrintErrorInfo(info);
637 }
638 }
639 else
640 {
641 com::ErrorInfo info;
642 RTMsgError("Failed to create the VirtualBox object!");
643 if (!info.isFullAvailable() && !info.isBasicAvailable())
644 {
645 com::GluePrintRCMessage(hrc);
646 RTMsgError("Most likely, the VirtualBox COM server is not running or failed to start.");
647 }
648 else
649 com::GluePrintErrorInfo(info);
650 }
651
652 /*
653 * Terminate COM, make sure the virtualBox object has been released.
654 */
655 virtualBox.setNull();
656 virtualBoxClient.setNull();
657 NativeEventQueue::getMainEventQueue()->processEventQueue(0);
658 com::Shutdown();
659 }
660 else
661 {
662 /*
663 * The command needs no COM.
664 */
665 HandlerArg handlerArg;
666 handlerArg.argc = argc - iCmdArg;
667 handlerArg.argv = &argv[iCmdArg];
668 rcExit = pCmd->pfnHandler(&handlerArg);
669 }
670
671 if (papszResponseFileArgs)
672 {
673 RTGetOptArgvFree(papszResponseFileArgs);
674 RTMemFree(papszNewArgv);
675 }
676
677 return rcExit;
678#else /* VBOX_ONLY_DOCS */
679 return RTEXITCODE_SUCCESS;
680#endif /* VBOX_ONLY_DOCS */
681}
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