~ubuntu-branches/ubuntu/trusty/virtualbox-ose/trusty

« back to all changes in this revision

Viewing changes to src/VBox/Frontends/VBoxManage/VBoxManage.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Felix Geyer
  • Date: 2009-12-18 16:44:29 UTC
  • mfrom: (0.3.3 upstream) (0.4.6 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091218164429-jd34ccexpv5na11a
Tags: 3.1.2-dfsg-1ubuntu1
* Merge from Debian unstable (LP: #498219), remaining changes:
  - Disable update action
    - debian/patches/u01-disable-update-action.dpatch
  - VirtualBox should go in Accessories, not in System tools (LP: #288590)
    - debian/virtualbox-ose-qt.files/virtualbox-ose.desktop
  - Add Apport hook
    - debian/virtualbox-ose.files/source_virtualbox-ose.py
    - debian/virtualbox-ose.install
  - Add Launchpad integration
    - debian/control
    - debian/lpi-bug.xpm
    - debian/patches/u02-lp-integration.dpatch
* Fixes the following bugs:
  - Kernel module fails to build with Linux >= 2.6.32 (LP: #474625)
  - X.Org drivers need to be rebuilt against X-Server 1.7 (LP: #495935)
  - The *-source packages try to build the kernel modules even though the
    kernel headers aren't available (LP: #473334)
* Replace *-source packages with transitional packages for *-dkms.
* Adapt u01-disable-update-action.dpatch and u02-lp-integration.dpatch for
  new upstream version.

Show diffs side-by-side

added added

removed removed

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