1 | /* $Id: VBoxManageGuestCtrl.cpp 27788 2010-03-29 12:42:22Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxManage - The 'guestcontrol' command.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 |
|
---|
22 |
|
---|
23 | /*******************************************************************************
|
---|
24 | * Header Files *
|
---|
25 | *******************************************************************************/
|
---|
26 | #include "VBoxManage.h"
|
---|
27 |
|
---|
28 | #include <VBox/com/com.h>
|
---|
29 | #include <VBox/com/string.h>
|
---|
30 | #include <VBox/com/array.h>
|
---|
31 | #include <VBox/com/ErrorInfo.h>
|
---|
32 | #include <VBox/com/errorprint.h>
|
---|
33 |
|
---|
34 | #include <VBox/com/VirtualBox.h>
|
---|
35 | #include <VBox/com/EventQueue.h>
|
---|
36 |
|
---|
37 | #include <VBox/log.h>
|
---|
38 | #include <iprt/asm.h>
|
---|
39 | #include <iprt/stream.h>
|
---|
40 | #include <iprt/string.h>
|
---|
41 | #include <iprt/time.h>
|
---|
42 | #include <iprt/thread.h>
|
---|
43 |
|
---|
44 | #ifdef USE_XPCOM_QUEUE
|
---|
45 | # include <sys/select.h>
|
---|
46 | # include <errno.h>
|
---|
47 | #endif
|
---|
48 |
|
---|
49 | #ifdef RT_OS_DARWIN
|
---|
50 | # include <CoreFoundation/CFRunLoop.h>
|
---|
51 | #endif
|
---|
52 |
|
---|
53 | using namespace com;
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * IVirtualBoxCallback implementation for handling the GuestControlCallback in
|
---|
57 | * relation to the "guestcontrol * wait" command.
|
---|
58 | */
|
---|
59 | /** @todo */
|
---|
60 |
|
---|
61 | void usageGuestControl(void)
|
---|
62 | {
|
---|
63 | RTPrintf("VBoxManage guestcontrol execute <vmname>|<uuid>\n"
|
---|
64 | " <path to program> [--args <arguments>] [--env NAME=VALUE]\n"
|
---|
65 | " [--flags <flags>] [--user <name> [--password <password>]]\n"
|
---|
66 | " [--timeout <milliseconds>]\n"
|
---|
67 | "\n");
|
---|
68 | }
|
---|
69 |
|
---|
70 | static int handleExecProgram(HandlerArg *a)
|
---|
71 | {
|
---|
72 | HRESULT rc = S_OK;
|
---|
73 |
|
---|
74 | /*
|
---|
75 | * Check the syntax. We can deduce the correct syntax from the number of
|
---|
76 | * arguments.
|
---|
77 | */
|
---|
78 | /** @todo */
|
---|
79 | if (a->argc < 2) /* At least the command we want to execute should be present. */
|
---|
80 | return errorSyntax(USAGE_GUESTCONTROL, "Incorrect parameters");
|
---|
81 | Utf8Str Utf8Cmd(a->argv[1]);
|
---|
82 | uint32_t uFlags = 0;
|
---|
83 | Utf8Str Utf8Args;
|
---|
84 | com::SafeArray <BSTR> env;
|
---|
85 | Utf8Str Utf8StdIn;
|
---|
86 | Utf8Str Utf8StdOut;
|
---|
87 | Utf8Str Utf8StdErr;
|
---|
88 | Utf8Str Utf8UserName;
|
---|
89 | Utf8Str Utf8Password;
|
---|
90 | uint32_t uTimeoutMS = 0;
|
---|
91 |
|
---|
92 | /* lookup VM. */
|
---|
93 | ComPtr<IMachine> machine;
|
---|
94 | /* assume it's an UUID */
|
---|
95 | rc = a->virtualBox->GetMachine(Bstr(a->argv[0]), machine.asOutParam());
|
---|
96 | if (FAILED(rc) || !machine)
|
---|
97 | {
|
---|
98 | /* must be a name */
|
---|
99 | CHECK_ERROR(a->virtualBox, FindMachine(Bstr(a->argv[0]), machine.asOutParam()));
|
---|
100 | }
|
---|
101 | if (machine)
|
---|
102 | {
|
---|
103 | do
|
---|
104 | {
|
---|
105 | Bstr uuid;
|
---|
106 | machine->COMGETTER(Id)(uuid.asOutParam());
|
---|
107 |
|
---|
108 | /* open an existing session for VM - so the VM has to be running */
|
---|
109 | CHECK_ERROR_BREAK(a->virtualBox, OpenExistingSession(a->session, uuid));
|
---|
110 |
|
---|
111 | /* get the mutable session machine */
|
---|
112 | a->session->COMGETTER(Machine)(machine.asOutParam());
|
---|
113 |
|
---|
114 | /* get the associated console */
|
---|
115 | ComPtr<IConsole> console;
|
---|
116 | CHECK_ERROR_BREAK(a->session, COMGETTER(Console)(console.asOutParam()));
|
---|
117 |
|
---|
118 | ComPtr<IGuest> guest;
|
---|
119 | CHECK_ERROR_BREAK(console, COMGETTER(Guest)(guest.asOutParam()));
|
---|
120 |
|
---|
121 | ULONG uPID = 0;
|
---|
122 | CHECK_ERROR_BREAK(guest, ExecuteProgram(Bstr(Utf8Cmd), uFlags,
|
---|
123 | Bstr(Utf8Args), ComSafeArrayAsInParam(env),
|
---|
124 | Bstr(Utf8StdIn), Bstr(Utf8StdOut), Bstr(Utf8StdErr),
|
---|
125 | Bstr(Utf8UserName), Bstr(Utf8Password), uTimeoutMS,
|
---|
126 | &uPID));
|
---|
127 | a->session->Close();
|
---|
128 | } while (0);
|
---|
129 | }
|
---|
130 | return SUCCEEDED(rc) ? 0 : 1;
|
---|
131 | }
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * Access the guest control store.
|
---|
135 | *
|
---|
136 | * @returns 0 on success, 1 on failure
|
---|
137 | * @note see the command line API description for parameters
|
---|
138 | */
|
---|
139 | int handleGuestControl(HandlerArg *a)
|
---|
140 | {
|
---|
141 | HandlerArg arg = *a;
|
---|
142 | arg.argc = a->argc - 1;
|
---|
143 | arg.argv = a->argv + 1;
|
---|
144 |
|
---|
145 | if (a->argc == 0)
|
---|
146 | return errorSyntax(USAGE_GUESTCONTROL, "Incorrect parameters");
|
---|
147 |
|
---|
148 | /* switch (cmd) */
|
---|
149 | if (strcmp(a->argv[0], "exec") == 0)
|
---|
150 | return handleExecProgram(&arg);
|
---|
151 |
|
---|
152 | /* default: */
|
---|
153 | return errorSyntax(USAGE_GUESTCONTROL, "Incorrect parameters");
|
---|
154 | }
|
---|