1 | /* $Id: VBoxManage.cpp 68184 2017-07-31 08:28:23Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxManage - VirtualBox's command-line interface.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2016 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/err.h>
|
---|
35 | #include <VBox/version.h>
|
---|
36 |
|
---|
37 | #include <iprt/asm.h>
|
---|
38 | #include <iprt/buildconfig.h>
|
---|
39 | #include <iprt/ctype.h>
|
---|
40 | #include <iprt/file.h>
|
---|
41 | #include <iprt/getopt.h>
|
---|
42 | #include <iprt/initterm.h>
|
---|
43 | #include <iprt/path.h>
|
---|
44 | #include <iprt/stream.h>
|
---|
45 | #include <iprt/string.h>
|
---|
46 |
|
---|
47 | #include <signal.h>
|
---|
48 |
|
---|
49 | #include "VBoxManage.h"
|
---|
50 |
|
---|
51 |
|
---|
52 | /*********************************************************************************************************************************
|
---|
53 | * Defined Constants And Macros *
|
---|
54 | *********************************************************************************************************************************/
|
---|
55 | /** The command doesn't need the COM stuff. */
|
---|
56 | #define VBMG_CMD_F_NO_COM RT_BIT_32(0)
|
---|
57 |
|
---|
58 | #define VBMG_CMD_TODO HELP_CMD_VBOXMANAGE_INVALID
|
---|
59 |
|
---|
60 |
|
---|
61 | /*********************************************************************************************************************************
|
---|
62 | * Structures and Typedefs *
|
---|
63 | *********************************************************************************************************************************/
|
---|
64 | #ifndef VBOX_ONLY_DOCS
|
---|
65 | /**
|
---|
66 | * VBoxManage command descriptor.
|
---|
67 | */
|
---|
68 | typedef struct VBMGCMD
|
---|
69 | {
|
---|
70 | /** The command. */
|
---|
71 | const char *pszCommand;
|
---|
72 | /** The help category. */
|
---|
73 | USAGECATEGORY enmHelpCat;
|
---|
74 | /** The new help command. */
|
---|
75 | enum HELP_CMD_VBOXMANAGE enmCmdHelp;
|
---|
76 | /** The handler. */
|
---|
77 | RTEXITCODE (*pfnHandler)(HandlerArg *pArg);
|
---|
78 | /** VBMG_CMD_F_XXX, */
|
---|
79 | uint32_t fFlags;
|
---|
80 | } VBMGCMD;
|
---|
81 | /** Pointer to a const VBoxManage command descriptor. */
|
---|
82 | typedef VBMGCMD const *PCVBMGCMD;
|
---|
83 | #endif
|
---|
84 |
|
---|
85 |
|
---|
86 | /*********************************************************************************************************************************
|
---|
87 | * Global Variables *
|
---|
88 | *********************************************************************************************************************************/
|
---|
89 | /*extern*/ bool g_fDetailedProgress = false;
|
---|
90 |
|
---|
91 | #ifndef VBOX_ONLY_DOCS
|
---|
92 | /** Set by the signal handler. */
|
---|
93 | static volatile bool g_fCanceled = false;
|
---|
94 |
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * All registered command handlers
|
---|
98 | */
|
---|
99 | static const VBMGCMD g_aCommands[] =
|
---|
100 | {
|
---|
101 | { "internalcommands", 0, VBMG_CMD_TODO, handleInternalCommands, 0 },
|
---|
102 | { "list", USAGE_LIST, VBMG_CMD_TODO, handleList, 0 },
|
---|
103 | { "showvminfo", USAGE_SHOWVMINFO, VBMG_CMD_TODO, handleShowVMInfo, 0 },
|
---|
104 | { "registervm", USAGE_REGISTERVM, VBMG_CMD_TODO, handleRegisterVM, 0 },
|
---|
105 | { "unregistervm", USAGE_UNREGISTERVM, VBMG_CMD_TODO, handleUnregisterVM, 0 },
|
---|
106 | { "clonevm", USAGE_CLONEVM, VBMG_CMD_TODO, handleCloneVM, 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_UNATTENDEDINSTALL, 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_SNAPSHOT, VBMG_CMD_TODO, 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 | { "getextradata", USAGE_GETEXTRADATA, VBMG_CMD_TODO, handleGetExtraData, 0 },
|
---|
135 | { "setextradata", USAGE_SETEXTRADATA, VBMG_CMD_TODO, handleSetExtraData, 0 },
|
---|
136 | { "setproperty", USAGE_SETPROPERTY, VBMG_CMD_TODO, handleSetProperty, 0 },
|
---|
137 | { "usbfilter", USAGE_USBFILTER, VBMG_CMD_TODO, handleUSBFilter, 0 },
|
---|
138 | { "sharedfolder", USAGE_SHAREDFOLDER, VBMG_CMD_TODO, handleSharedFolder, 0 },
|
---|
139 | #ifdef VBOX_WITH_GUEST_PROPS
|
---|
140 | { "guestproperty", USAGE_GUESTPROPERTY, VBMG_CMD_TODO, handleGuestProperty, 0 },
|
---|
141 | #endif
|
---|
142 | #ifdef VBOX_WITH_GUEST_CONTROL
|
---|
143 | { "guestcontrol", USAGE_GUESTCONTROL, VBMG_CMD_TODO, handleGuestControl, 0 },
|
---|
144 | #endif
|
---|
145 | { "metrics", USAGE_METRICS, VBMG_CMD_TODO, handleMetrics, 0 },
|
---|
146 | { "import", USAGE_IMPORTAPPLIANCE, VBMG_CMD_TODO, handleImportAppliance, 0 },
|
---|
147 | { "export", USAGE_EXPORTAPPLIANCE, VBMG_CMD_TODO, handleExportAppliance, 0 },
|
---|
148 | #ifdef VBOX_WITH_NETFLT
|
---|
149 | { "hostonlyif", USAGE_HOSTONLYIFS, VBMG_CMD_TODO, handleHostonlyIf, 0 },
|
---|
150 | #endif
|
---|
151 | { "dhcpserver", USAGE_DHCPSERVER, VBMG_CMD_TODO, handleDHCPServer, 0 },
|
---|
152 | #ifdef VBOX_WITH_NAT_SERVICE
|
---|
153 | { "natnetwork", USAGE_NATNETWORK, VBMG_CMD_TODO, handleNATNetwork, 0 },
|
---|
154 | #endif
|
---|
155 | { "extpack", USAGE_EXTPACK, HELP_CMD_EXTPACK, handleExtPack, 0 },
|
---|
156 | { "bandwidthctl", USAGE_BANDWIDTHCONTROL, VBMG_CMD_TODO, handleBandwidthControl, 0 },
|
---|
157 | { "debugvm", USAGE_DEBUGVM, HELP_CMD_DEBUGVM, handleDebugVM, 0 },
|
---|
158 | { "convertfromraw", USAGE_CONVERTFROMRAW, VBMG_CMD_TODO, handleConvertFromRaw, VBMG_CMD_F_NO_COM },
|
---|
159 | { "convertdd", USAGE_CONVERTFROMRAW, VBMG_CMD_TODO, handleConvertFromRaw, VBMG_CMD_F_NO_COM },
|
---|
160 | { "usbdevsource", USAGE_USBDEVSOURCE, VBMG_CMD_TODO, handleUSBDevSource, 0 }
|
---|
161 | };
|
---|
162 |
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * Looks up a command by name.
|
---|
166 | *
|
---|
167 | * @returns Pointer to the command structure.
|
---|
168 | * @param pszCommand Name of the command.
|
---|
169 | */
|
---|
170 | static PCVBMGCMD lookupCommand(const char *pszCommand)
|
---|
171 | {
|
---|
172 | if (pszCommand)
|
---|
173 | for (uint32_t i = 0; i < RT_ELEMENTS(g_aCommands); i++)
|
---|
174 | if (!strcmp(g_aCommands[i].pszCommand, pszCommand))
|
---|
175 | return &g_aCommands[i];
|
---|
176 | return NULL;
|
---|
177 | }
|
---|
178 |
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * Signal handler that sets g_fCanceled.
|
---|
182 | *
|
---|
183 | * This can be executed on any thread in the process, on Windows it may even be
|
---|
184 | * a thread dedicated to delivering this signal. Do not doing anything
|
---|
185 | * unnecessary here.
|
---|
186 | */
|
---|
187 | static void showProgressSignalHandler(int iSignal)
|
---|
188 | {
|
---|
189 | NOREF(iSignal);
|
---|
190 | ASMAtomicWriteBool(&g_fCanceled, true);
|
---|
191 | }
|
---|
192 |
|
---|
193 | /**
|
---|
194 | * Print out progress on the console.
|
---|
195 | *
|
---|
196 | * This runs the main event queue every now and then to prevent piling up
|
---|
197 | * unhandled things (which doesn't cause real problems, just makes things
|
---|
198 | * react a little slower than in the ideal case).
|
---|
199 | */
|
---|
200 | HRESULT showProgress(ComPtr<IProgress> progress)
|
---|
201 | {
|
---|
202 | using namespace com;
|
---|
203 |
|
---|
204 | BOOL fCompleted = FALSE;
|
---|
205 | ULONG ulCurrentPercent = 0;
|
---|
206 | ULONG ulLastPercent = 0;
|
---|
207 |
|
---|
208 | ULONG ulLastOperationPercent = (ULONG)-1;
|
---|
209 |
|
---|
210 | ULONG ulLastOperation = (ULONG)-1;
|
---|
211 | Bstr bstrOperationDescription;
|
---|
212 |
|
---|
213 | NativeEventQueue::getMainEventQueue()->processEventQueue(0);
|
---|
214 |
|
---|
215 | ULONG cOperations = 1;
|
---|
216 | HRESULT hrc = progress->COMGETTER(OperationCount)(&cOperations);
|
---|
217 | if (FAILED(hrc))
|
---|
218 | {
|
---|
219 | RTStrmPrintf(g_pStdErr, "Progress object failure: %Rhrc\n", hrc);
|
---|
220 | RTStrmFlush(g_pStdErr);
|
---|
221 | return hrc;
|
---|
222 | }
|
---|
223 |
|
---|
224 | /*
|
---|
225 | * Note: Outputting the progress info to stderr (g_pStdErr) is intentional
|
---|
226 | * to not get intermixed with other (raw) stdout data which might get
|
---|
227 | * written in the meanwhile.
|
---|
228 | */
|
---|
229 |
|
---|
230 | if (!g_fDetailedProgress)
|
---|
231 | {
|
---|
232 | RTStrmPrintf(g_pStdErr, "0%%...");
|
---|
233 | RTStrmFlush(g_pStdErr);
|
---|
234 | }
|
---|
235 |
|
---|
236 | /* setup signal handling if cancelable */
|
---|
237 | bool fCanceledAlready = false;
|
---|
238 | BOOL fCancelable;
|
---|
239 | hrc = progress->COMGETTER(Cancelable)(&fCancelable);
|
---|
240 | if (FAILED(hrc))
|
---|
241 | fCancelable = FALSE;
|
---|
242 | if (fCancelable)
|
---|
243 | {
|
---|
244 | signal(SIGINT, showProgressSignalHandler);
|
---|
245 | signal(SIGTERM, showProgressSignalHandler);
|
---|
246 | #ifdef SIGBREAK
|
---|
247 | signal(SIGBREAK, showProgressSignalHandler);
|
---|
248 | #endif
|
---|
249 | }
|
---|
250 |
|
---|
251 | hrc = progress->COMGETTER(Completed(&fCompleted));
|
---|
252 | while (SUCCEEDED(hrc))
|
---|
253 | {
|
---|
254 | progress->COMGETTER(Percent(&ulCurrentPercent));
|
---|
255 |
|
---|
256 | if (g_fDetailedProgress)
|
---|
257 | {
|
---|
258 | ULONG ulOperation = 1;
|
---|
259 | hrc = progress->COMGETTER(Operation)(&ulOperation);
|
---|
260 | if (FAILED(hrc))
|
---|
261 | break;
|
---|
262 | ULONG ulCurrentOperationPercent = 0;
|
---|
263 | hrc = progress->COMGETTER(OperationPercent(&ulCurrentOperationPercent));
|
---|
264 | if (FAILED(hrc))
|
---|
265 | break;
|
---|
266 |
|
---|
267 | if (ulLastOperation != ulOperation)
|
---|
268 | {
|
---|
269 | hrc = progress->COMGETTER(OperationDescription(bstrOperationDescription.asOutParam()));
|
---|
270 | if (FAILED(hrc))
|
---|
271 | break;
|
---|
272 | ulLastPercent = (ULONG)-1; // force print
|
---|
273 | ulLastOperation = ulOperation;
|
---|
274 | }
|
---|
275 |
|
---|
276 | if ( ulCurrentPercent != ulLastPercent
|
---|
277 | || ulCurrentOperationPercent != ulLastOperationPercent
|
---|
278 | )
|
---|
279 | {
|
---|
280 | LONG lSecsRem = 0;
|
---|
281 | progress->COMGETTER(TimeRemaining)(&lSecsRem);
|
---|
282 |
|
---|
283 | RTStrmPrintf(g_pStdErr, "(%u/%u) %ls %02u%% => %02u%% (%d s remaining)\n", ulOperation + 1, cOperations,
|
---|
284 | bstrOperationDescription.raw(), ulCurrentOperationPercent, ulCurrentPercent, lSecsRem);
|
---|
285 | ulLastPercent = ulCurrentPercent;
|
---|
286 | ulLastOperationPercent = ulCurrentOperationPercent;
|
---|
287 | }
|
---|
288 | }
|
---|
289 | else
|
---|
290 | {
|
---|
291 | /* did we cross a 10% mark? */
|
---|
292 | if (ulCurrentPercent / 10 > ulLastPercent / 10)
|
---|
293 | {
|
---|
294 | /* make sure to also print out missed steps */
|
---|
295 | for (ULONG curVal = (ulLastPercent / 10) * 10 + 10; curVal <= (ulCurrentPercent / 10) * 10; curVal += 10)
|
---|
296 | {
|
---|
297 | if (curVal < 100)
|
---|
298 | {
|
---|
299 | RTStrmPrintf(g_pStdErr, "%u%%...", curVal);
|
---|
300 | RTStrmFlush(g_pStdErr);
|
---|
301 | }
|
---|
302 | }
|
---|
303 | ulLastPercent = (ulCurrentPercent / 10) * 10;
|
---|
304 | }
|
---|
305 | }
|
---|
306 | if (fCompleted)
|
---|
307 | break;
|
---|
308 |
|
---|
309 | /* process async cancelation */
|
---|
310 | if (g_fCanceled && !fCanceledAlready)
|
---|
311 | {
|
---|
312 | hrc = progress->Cancel();
|
---|
313 | if (SUCCEEDED(hrc))
|
---|
314 | fCanceledAlready = true;
|
---|
315 | else
|
---|
316 | g_fCanceled = false;
|
---|
317 | }
|
---|
318 |
|
---|
319 | /* make sure the loop is not too tight */
|
---|
320 | progress->WaitForCompletion(100);
|
---|
321 |
|
---|
322 | NativeEventQueue::getMainEventQueue()->processEventQueue(0);
|
---|
323 | hrc = progress->COMGETTER(Completed(&fCompleted));
|
---|
324 | }
|
---|
325 |
|
---|
326 | /* undo signal handling */
|
---|
327 | if (fCancelable)
|
---|
328 | {
|
---|
329 | signal(SIGINT, SIG_DFL);
|
---|
330 | signal(SIGTERM, SIG_DFL);
|
---|
331 | # ifdef SIGBREAK
|
---|
332 | signal(SIGBREAK, SIG_DFL);
|
---|
333 | # endif
|
---|
334 | }
|
---|
335 |
|
---|
336 | /* complete the line. */
|
---|
337 | LONG iRc = E_FAIL;
|
---|
338 | hrc = progress->COMGETTER(ResultCode)(&iRc);
|
---|
339 | if (SUCCEEDED(hrc))
|
---|
340 | {
|
---|
341 | if (SUCCEEDED(iRc))
|
---|
342 | RTStrmPrintf(g_pStdErr, "100%%\n");
|
---|
343 | else if (g_fCanceled)
|
---|
344 | RTStrmPrintf(g_pStdErr, "CANCELED\n");
|
---|
345 | else
|
---|
346 | {
|
---|
347 | if (!g_fDetailedProgress)
|
---|
348 | RTStrmPrintf(g_pStdErr, "\n");
|
---|
349 | RTStrmPrintf(g_pStdErr, "Progress state: %Rhrc\n", iRc);
|
---|
350 | }
|
---|
351 | hrc = iRc;
|
---|
352 | }
|
---|
353 | else
|
---|
354 | {
|
---|
355 | if (!g_fDetailedProgress)
|
---|
356 | RTStrmPrintf(g_pStdErr, "\n");
|
---|
357 | RTStrmPrintf(g_pStdErr, "Progress object failure: %Rhrc\n", hrc);
|
---|
358 | }
|
---|
359 | RTStrmFlush(g_pStdErr);
|
---|
360 | return hrc;
|
---|
361 | }
|
---|
362 |
|
---|
363 | RTEXITCODE readPasswordFile(const char *pszFilename, com::Utf8Str *pPasswd)
|
---|
364 | {
|
---|
365 | size_t cbFile;
|
---|
366 | char szPasswd[512];
|
---|
367 | int vrc = VINF_SUCCESS;
|
---|
368 | RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
|
---|
369 | bool fStdIn = !strcmp(pszFilename, "stdin");
|
---|
370 | PRTSTREAM pStrm;
|
---|
371 | if (!fStdIn)
|
---|
372 | vrc = RTStrmOpen(pszFilename, "r", &pStrm);
|
---|
373 | else
|
---|
374 | pStrm = g_pStdIn;
|
---|
375 | if (RT_SUCCESS(vrc))
|
---|
376 | {
|
---|
377 | vrc = RTStrmReadEx(pStrm, szPasswd, sizeof(szPasswd)-1, &cbFile);
|
---|
378 | if (RT_SUCCESS(vrc))
|
---|
379 | {
|
---|
380 | if (cbFile >= sizeof(szPasswd)-1)
|
---|
381 | rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Provided password in file '%s' is too long", pszFilename);
|
---|
382 | else
|
---|
383 | {
|
---|
384 | unsigned i;
|
---|
385 | for (i = 0; i < cbFile && !RT_C_IS_CNTRL(szPasswd[i]); i++)
|
---|
386 | ;
|
---|
387 | szPasswd[i] = '\0';
|
---|
388 | *pPasswd = szPasswd;
|
---|
389 | }
|
---|
390 | }
|
---|
391 | else
|
---|
392 | rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Cannot read password from file '%s': %Rrc", pszFilename, vrc);
|
---|
393 | if (!fStdIn)
|
---|
394 | RTStrmClose(pStrm);
|
---|
395 | }
|
---|
396 | else
|
---|
397 | rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Cannot open password file '%s' (%Rrc)", pszFilename, vrc);
|
---|
398 |
|
---|
399 | return rcExit;
|
---|
400 | }
|
---|
401 |
|
---|
402 | static RTEXITCODE settingsPasswordFile(ComPtr<IVirtualBox> virtualBox, const char *pszFilename)
|
---|
403 | {
|
---|
404 | com::Utf8Str passwd;
|
---|
405 | RTEXITCODE rcExit = readPasswordFile(pszFilename, &passwd);
|
---|
406 | if (rcExit == RTEXITCODE_SUCCESS)
|
---|
407 | {
|
---|
408 | CHECK_ERROR2I_STMT(virtualBox, SetSettingsSecret(com::Bstr(passwd).raw()), rcExit = RTEXITCODE_FAILURE);
|
---|
409 | }
|
---|
410 |
|
---|
411 | return rcExit;
|
---|
412 | }
|
---|
413 |
|
---|
414 | RTEXITCODE readPasswordFromConsole(com::Utf8Str *pPassword, const char *pszPrompt, ...)
|
---|
415 | {
|
---|
416 | RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
|
---|
417 | char aszPwdInput[_1K] = { 0 };
|
---|
418 | va_list vaArgs;
|
---|
419 |
|
---|
420 | va_start(vaArgs, pszPrompt);
|
---|
421 | int vrc = RTStrmPrintfV(g_pStdOut, pszPrompt, vaArgs);
|
---|
422 | if (RT_SUCCESS(vrc))
|
---|
423 | {
|
---|
424 | bool fEchoOld = false;
|
---|
425 | vrc = RTStrmInputGetEchoChars(g_pStdIn, &fEchoOld);
|
---|
426 | if (RT_SUCCESS(vrc))
|
---|
427 | {
|
---|
428 | vrc = RTStrmInputSetEchoChars(g_pStdIn, false);
|
---|
429 | if (RT_SUCCESS(vrc))
|
---|
430 | {
|
---|
431 | vrc = RTStrmGetLine(g_pStdIn, &aszPwdInput[0], sizeof(aszPwdInput));
|
---|
432 | if (RT_SUCCESS(vrc))
|
---|
433 | *pPassword = aszPwdInput;
|
---|
434 | else
|
---|
435 | rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed read password from command line (%Rrc)", vrc);
|
---|
436 |
|
---|
437 | int vrc2 = RTStrmInputSetEchoChars(g_pStdIn, fEchoOld);
|
---|
438 | AssertRC(vrc2);
|
---|
439 | }
|
---|
440 | else
|
---|
441 | rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to disable echoing typed characters (%Rrc)", vrc);
|
---|
442 | }
|
---|
443 | else
|
---|
444 | rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to retrieve echo setting (%Rrc)", vrc);
|
---|
445 |
|
---|
446 | RTStrmPutStr(g_pStdOut, "\n");
|
---|
447 | }
|
---|
448 | else
|
---|
449 | rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to print prompt (%Rrc)", vrc);
|
---|
450 | va_end(vaArgs);
|
---|
451 |
|
---|
452 | return rcExit;
|
---|
453 | }
|
---|
454 |
|
---|
455 | #endif /* !VBOX_ONLY_DOCS */
|
---|
456 |
|
---|
457 |
|
---|
458 | int main(int argc, char *argv[])
|
---|
459 | {
|
---|
460 | /*
|
---|
461 | * Before we do anything, init the runtime without loading
|
---|
462 | * the support driver.
|
---|
463 | */
|
---|
464 | int vrc = RTR3InitExe(argc, &argv, 0);
|
---|
465 | if (RT_FAILURE(vrc))
|
---|
466 | return RTMsgInitFailure(vrc);
|
---|
467 | #if defined(RT_OS_WINDOWS) && !defined(VBOX_ONLY_DOCS)
|
---|
468 | ATL::CComModule _Module; /* Required internally by ATL (constructor records instance in global variable). */
|
---|
469 | #endif
|
---|
470 |
|
---|
471 | /*
|
---|
472 | * Parse the global options
|
---|
473 | */
|
---|
474 | bool fShowLogo = false;
|
---|
475 | bool fShowHelp = false;
|
---|
476 | int iCmd = 1;
|
---|
477 | int iCmdArg;
|
---|
478 | const char *pszSettingsPw = NULL;
|
---|
479 | const char *pszSettingsPwFile = NULL;
|
---|
480 | #ifndef VBOX_ONLY_DOCS
|
---|
481 | int cResponseFileArgs = 0;
|
---|
482 | char **papszResponseFileArgs = NULL;
|
---|
483 | char **papszNewArgv = NULL;
|
---|
484 | #endif
|
---|
485 |
|
---|
486 | for (int i = 1; i < argc || argc <= iCmd; i++)
|
---|
487 | {
|
---|
488 | if ( argc <= iCmd
|
---|
489 | || !strcmp(argv[i], "help")
|
---|
490 | || !strcmp(argv[i], "--help")
|
---|
491 | || !strcmp(argv[i], "-?")
|
---|
492 | || !strcmp(argv[i], "-h")
|
---|
493 | || !strcmp(argv[i], "-help"))
|
---|
494 | {
|
---|
495 | if (i >= argc - 1)
|
---|
496 | {
|
---|
497 | showLogo(g_pStdOut);
|
---|
498 | printUsage(USAGE_ALL, ~0U, g_pStdOut);
|
---|
499 | return 0;
|
---|
500 | }
|
---|
501 | fShowLogo = true;
|
---|
502 | fShowHelp = true;
|
---|
503 | iCmd++;
|
---|
504 | continue;
|
---|
505 | }
|
---|
506 |
|
---|
507 | #ifndef VBOX_ONLY_DOCS
|
---|
508 | if ( !strcmp(argv[i], "-V")
|
---|
509 | || !strcmp(argv[i], "--version")
|
---|
510 | || !strcmp(argv[i], "-v") /* deprecated */
|
---|
511 | || !strcmp(argv[i], "-version") /* deprecated */
|
---|
512 | || !strcmp(argv[i], "-Version") /* deprecated */)
|
---|
513 | {
|
---|
514 | /* Print version number, and do nothing else. */
|
---|
515 | RTPrintf("%sr%u\n", VBOX_VERSION_STRING, RTBldCfgRevision());
|
---|
516 | return 0;
|
---|
517 | }
|
---|
518 | #endif
|
---|
519 |
|
---|
520 | if ( !strcmp(argv[i], "--dumpopts")
|
---|
521 | || !strcmp(argv[i], "-dumpopts") /* deprecated */)
|
---|
522 | {
|
---|
523 | /* Special option to dump really all commands,
|
---|
524 | * even the ones not understood on this platform. */
|
---|
525 | printUsage(USAGE_DUMPOPTS, ~0U, g_pStdOut);
|
---|
526 | return 0;
|
---|
527 | }
|
---|
528 |
|
---|
529 | if ( !strcmp(argv[i], "--nologo")
|
---|
530 | || !strcmp(argv[i], "-q")
|
---|
531 | || !strcmp(argv[i], "-nologo") /* deprecated */)
|
---|
532 | {
|
---|
533 | /* suppress the logo */
|
---|
534 | fShowLogo = false;
|
---|
535 | iCmd++;
|
---|
536 | }
|
---|
537 | else if ( !strcmp(argv[i], "--detailed-progress")
|
---|
538 | || !strcmp(argv[i], "-d"))
|
---|
539 | {
|
---|
540 | /* detailed progress report */
|
---|
541 | g_fDetailedProgress = true;
|
---|
542 | iCmd++;
|
---|
543 | }
|
---|
544 | else if (!strcmp(argv[i], "--settingspw"))
|
---|
545 | {
|
---|
546 | if (i >= argc - 1)
|
---|
547 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Password expected");
|
---|
548 | /* password for certain settings */
|
---|
549 | pszSettingsPw = argv[i + 1];
|
---|
550 | iCmd += 2;
|
---|
551 | }
|
---|
552 | else if (!strcmp(argv[i], "--settingspwfile"))
|
---|
553 | {
|
---|
554 | if (i >= argc-1)
|
---|
555 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "No password file specified");
|
---|
556 | pszSettingsPwFile = argv[i+1];
|
---|
557 | iCmd += 2;
|
---|
558 | }
|
---|
559 | #ifndef VBOX_ONLY_DOCS
|
---|
560 | else if (argv[i][0] == '@')
|
---|
561 | {
|
---|
562 | if (papszResponseFileArgs)
|
---|
563 | return RTMsgErrorExitFailure("Only one response file allowed");
|
---|
564 |
|
---|
565 | /* Load response file, making sure it's valid UTF-8. */
|
---|
566 | char *pszResponseFile;
|
---|
567 | size_t cbResponseFile;
|
---|
568 | vrc = RTFileReadAllEx(&argv[i][1], 0, RTFOFF_MAX, RTFILE_RDALL_O_DENY_NONE | RTFILE_RDALL_F_TRAILING_ZERO_BYTE,
|
---|
569 | (void **)&pszResponseFile, &cbResponseFile);
|
---|
570 | if (RT_FAILURE(vrc))
|
---|
571 | return RTMsgErrorExitFailure("Error reading response file '%s': %Rrc", &argv[i][1], vrc);
|
---|
572 | vrc = RTStrValidateEncoding(pszResponseFile);
|
---|
573 | if (RT_FAILURE(vrc))
|
---|
574 | {
|
---|
575 | RTFileReadAllFree(pszResponseFile, cbResponseFile);
|
---|
576 | return RTMsgErrorExitFailure("Invalid response file ('%s') encoding: %Rrc", &argv[i][1], vrc);
|
---|
577 | }
|
---|
578 |
|
---|
579 | /* Parse it. */
|
---|
580 | vrc = RTGetOptArgvFromString(&papszResponseFileArgs, &cResponseFileArgs, pszResponseFile,
|
---|
581 | RTGETOPTARGV_CNV_QUOTE_BOURNE_SH, NULL);
|
---|
582 | RTFileReadAllFree(pszResponseFile, cbResponseFile);
|
---|
583 | if (RT_FAILURE(vrc))
|
---|
584 | return RTMsgErrorExitFailure("Failed to parse response file '%s' (bourne shell style): %Rrc", &argv[i][1], vrc);
|
---|
585 |
|
---|
586 | /* Construct new argv+argc with the response file arguments inserted. */
|
---|
587 | int cNewArgs = argc + cResponseFileArgs;
|
---|
588 | papszNewArgv = (char **)RTMemAllocZ((cNewArgs + 2) * sizeof(papszNewArgv[0]));
|
---|
589 | if (!papszNewArgv)
|
---|
590 | return RTMsgErrorExitFailure("out of memory");
|
---|
591 | memcpy(&papszNewArgv[0], &argv[0], sizeof(argv[0]) * (i + 1));
|
---|
592 | memcpy(&papszNewArgv[i + 1], papszResponseFileArgs, sizeof(argv[0]) * cResponseFileArgs);
|
---|
593 | memcpy(&papszNewArgv[i + 1 + cResponseFileArgs], &argv[i + 1], sizeof(argv[0]) * (argc - i - 1 + 1));
|
---|
594 | argv = papszNewArgv;
|
---|
595 | argc = argc + cResponseFileArgs;
|
---|
596 |
|
---|
597 | iCmd++;
|
---|
598 | }
|
---|
599 | #endif
|
---|
600 | else
|
---|
601 | break;
|
---|
602 | }
|
---|
603 |
|
---|
604 | iCmdArg = iCmd + 1;
|
---|
605 |
|
---|
606 | /*
|
---|
607 | * Show the logo and lookup the command and deal with fShowHelp = true.
|
---|
608 | */
|
---|
609 | if (fShowLogo)
|
---|
610 | showLogo(g_pStdOut);
|
---|
611 |
|
---|
612 | #ifndef VBOX_ONLY_DOCS
|
---|
613 | PCVBMGCMD pCmd = lookupCommand(argv[iCmd]);
|
---|
614 | if (pCmd && pCmd->enmCmdHelp != VBMG_CMD_TODO)
|
---|
615 | setCurrentCommand(pCmd->enmCmdHelp);
|
---|
616 |
|
---|
617 | if ( pCmd
|
---|
618 | && ( fShowHelp
|
---|
619 | || ( argc - iCmdArg == 0
|
---|
620 | && pCmd->enmHelpCat != 0)))
|
---|
621 | {
|
---|
622 | if (pCmd->enmCmdHelp == VBMG_CMD_TODO)
|
---|
623 | printUsage(pCmd->enmHelpCat, ~0U, g_pStdOut);
|
---|
624 | else if (fShowHelp)
|
---|
625 | printHelp(g_pStdOut);
|
---|
626 | else
|
---|
627 | printUsage(g_pStdOut);
|
---|
628 | return RTEXITCODE_FAILURE; /* error */
|
---|
629 | }
|
---|
630 | if (!pCmd)
|
---|
631 | {
|
---|
632 | if (!strcmp(argv[iCmd], "commands"))
|
---|
633 | {
|
---|
634 | RTPrintf("commands:\n");
|
---|
635 | for (unsigned i = 0; i < RT_ELEMENTS(g_aCommands); i++)
|
---|
636 | if ( i == 0 /* skip backwards compatibility entries */
|
---|
637 | || g_aCommands[i].enmHelpCat != g_aCommands[i - 1].enmHelpCat)
|
---|
638 | RTPrintf(" %s\n", g_aCommands[i].pszCommand);
|
---|
639 | return RTEXITCODE_SUCCESS;
|
---|
640 | }
|
---|
641 | return errorSyntax(USAGE_ALL, "Invalid command '%s'", argv[iCmd]);
|
---|
642 | }
|
---|
643 |
|
---|
644 | RTEXITCODE rcExit;
|
---|
645 | if (!(pCmd->fFlags & VBMG_CMD_F_NO_COM))
|
---|
646 | {
|
---|
647 | /*
|
---|
648 | * Initialize COM.
|
---|
649 | */
|
---|
650 | using namespace com;
|
---|
651 | HRESULT hrc = com::Initialize();
|
---|
652 | if (FAILED(hrc))
|
---|
653 | {
|
---|
654 | # ifdef VBOX_WITH_XPCOM
|
---|
655 | if (hrc == NS_ERROR_FILE_ACCESS_DENIED)
|
---|
656 | {
|
---|
657 | char szHome[RTPATH_MAX] = "";
|
---|
658 | com::GetVBoxUserHomeDirectory(szHome, sizeof(szHome));
|
---|
659 | return RTMsgErrorExit(RTEXITCODE_FAILURE,
|
---|
660 | "Failed to initialize COM because the global settings directory '%s' is not accessible!", szHome);
|
---|
661 | }
|
---|
662 | # endif
|
---|
663 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to initialize COM! (hrc=%Rhrc)", hrc);
|
---|
664 | }
|
---|
665 |
|
---|
666 |
|
---|
667 | /*
|
---|
668 | * Get the remote VirtualBox object and create a local session object.
|
---|
669 | */
|
---|
670 | rcExit = RTEXITCODE_FAILURE;
|
---|
671 | ComPtr<IVirtualBoxClient> virtualBoxClient;
|
---|
672 | ComPtr<IVirtualBox> virtualBox;
|
---|
673 | hrc = virtualBoxClient.createInprocObject(CLSID_VirtualBoxClient);
|
---|
674 | if (SUCCEEDED(hrc))
|
---|
675 | hrc = virtualBoxClient->COMGETTER(VirtualBox)(virtualBox.asOutParam());
|
---|
676 | if (SUCCEEDED(hrc))
|
---|
677 | {
|
---|
678 | ComPtr<ISession> session;
|
---|
679 | hrc = session.createInprocObject(CLSID_Session);
|
---|
680 | if (SUCCEEDED(hrc))
|
---|
681 | {
|
---|
682 | /* Session secret. */
|
---|
683 | if (pszSettingsPw)
|
---|
684 | CHECK_ERROR2I_STMT(virtualBox, SetSettingsSecret(Bstr(pszSettingsPw).raw()), rcExit = RTEXITCODE_FAILURE);
|
---|
685 | else if (pszSettingsPwFile)
|
---|
686 | rcExit = settingsPasswordFile(virtualBox, pszSettingsPwFile);
|
---|
687 | else
|
---|
688 | rcExit = RTEXITCODE_SUCCESS;
|
---|
689 | if (rcExit == RTEXITCODE_SUCCESS)
|
---|
690 | {
|
---|
691 | /*
|
---|
692 | * Call the handler.
|
---|
693 | */
|
---|
694 | HandlerArg handlerArg = { argc - iCmdArg, &argv[iCmdArg], virtualBox, session };
|
---|
695 | rcExit = pCmd->pfnHandler(&handlerArg);
|
---|
696 |
|
---|
697 | /* Although all handlers should always close the session if they open it,
|
---|
698 | * we do it here just in case if some of the handlers contains a bug --
|
---|
699 | * leaving the direct session not closed will turn the machine state to
|
---|
700 | * Aborted which may have unwanted side effects like killing the saved
|
---|
701 | * state file (if the machine was in the Saved state before). */
|
---|
702 | session->UnlockMachine();
|
---|
703 | }
|
---|
704 |
|
---|
705 | NativeEventQueue::getMainEventQueue()->processEventQueue(0);
|
---|
706 | }
|
---|
707 | else
|
---|
708 | {
|
---|
709 | com::ErrorInfo info;
|
---|
710 | RTMsgError("Failed to create a session object!");
|
---|
711 | if (!info.isFullAvailable() && !info.isBasicAvailable())
|
---|
712 | com::GluePrintRCMessage(hrc);
|
---|
713 | else
|
---|
714 | com::GluePrintErrorInfo(info);
|
---|
715 | }
|
---|
716 | }
|
---|
717 | else
|
---|
718 | {
|
---|
719 | com::ErrorInfo info;
|
---|
720 | RTMsgError("Failed to create the VirtualBox object!");
|
---|
721 | if (!info.isFullAvailable() && !info.isBasicAvailable())
|
---|
722 | {
|
---|
723 | com::GluePrintRCMessage(hrc);
|
---|
724 | RTMsgError("Most likely, the VirtualBox COM server is not running or failed to start.");
|
---|
725 | }
|
---|
726 | else
|
---|
727 | com::GluePrintErrorInfo(info);
|
---|
728 | }
|
---|
729 |
|
---|
730 | /*
|
---|
731 | * Terminate COM, make sure the virtualBox object has been released.
|
---|
732 | */
|
---|
733 | virtualBox.setNull();
|
---|
734 | virtualBoxClient.setNull();
|
---|
735 | NativeEventQueue::getMainEventQueue()->processEventQueue(0);
|
---|
736 | com::Shutdown();
|
---|
737 | }
|
---|
738 | else
|
---|
739 | {
|
---|
740 | /*
|
---|
741 | * The command needs no COM.
|
---|
742 | */
|
---|
743 | HandlerArg handlerArg;
|
---|
744 | handlerArg.argc = argc - iCmdArg;
|
---|
745 | handlerArg.argv = &argv[iCmdArg];
|
---|
746 | rcExit = pCmd->pfnHandler(&handlerArg);
|
---|
747 | }
|
---|
748 |
|
---|
749 | if (papszResponseFileArgs)
|
---|
750 | {
|
---|
751 | RTGetOptArgvFree(papszResponseFileArgs);
|
---|
752 | RTMemFree(papszNewArgv);
|
---|
753 | }
|
---|
754 |
|
---|
755 | return rcExit;
|
---|
756 | #else /* VBOX_ONLY_DOCS */
|
---|
757 | return RTEXITCODE_SUCCESS;
|
---|
758 | #endif /* VBOX_ONLY_DOCS */
|
---|
759 | }
|
---|