VirtualBox

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

Last change on this file since 28322 was 28248, checked in by vboxsync, 15 years ago

VBoxManage/Control: More syntax.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.9 KB
Line 
1/* $Id: VBoxManageGuestCtrl.cpp 28248 2010-04-13 12:52:38Z 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/getopt.h>
40#include <iprt/stream.h>
41#include <iprt/string.h>
42#include <iprt/time.h>
43#include <iprt/thread.h>
44
45#ifdef USE_XPCOM_QUEUE
46# include <sys/select.h>
47# include <errno.h>
48#endif
49
50#ifdef RT_OS_DARWIN
51# include <CoreFoundation/CFRunLoop.h>
52#endif
53
54using namespace com;
55
56/**
57 * IVirtualBoxCallback implementation for handling the GuestControlCallback in
58 * relation to the "guestcontrol * wait" command.
59 */
60/** @todo */
61
62void usageGuestControl(void)
63{
64 RTPrintf("VBoxManage guestcontrol execute <vmname>|<uuid>\n"
65 " <path to program> [--arguments \"<arguments>\"] [--environment \"NAME=VALUE NAME=VALUE\"]\n"
66 " [--flags <flags>] [--username <name> [--password <password>]]\n"
67 " [--timeout <msec>] [--verbose] [--wait stdout[,[stderr]]]\n"
68 "\n");
69}
70
71static int handleExecProgram(HandlerArg *a)
72{
73 /*
74 * Check the syntax. We can deduce the correct syntax from the number of
75 * arguments.
76 */
77 if (a->argc < 2) /* At least the command we want to execute in the guest should be present :-). */
78 return errorSyntax(USAGE_GUESTCONTROL, "Incorrect parameters");
79
80 Utf8Str Utf8Cmd(a->argv[1]);
81 uint32_t uFlags = 0;
82 com::SafeArray <BSTR> args;
83 com::SafeArray <BSTR> env;
84 Utf8Str Utf8StdIn;
85 Utf8Str Utf8StdOut;
86 Utf8Str Utf8StdErr;
87 Utf8Str Utf8UserName;
88 Utf8Str Utf8Password;
89 uint32_t uTimeoutMS = RT_INDEFINITE_WAIT;
90 bool waitForOutput = false;
91 bool verbose = false;
92
93 /* Iterate through all possible commands (if available). */
94 bool usageOK = true;
95 for (int i = 2; usageOK && i < a->argc; i++)
96 {
97 if ( !strcmp(a->argv[i], "--arguments")
98 || !strcmp(a->argv[i], "--args")
99 || !strcmp(a->argv[i], "--arg"))
100 {
101 if (i + 1 >= a->argc)
102 usageOK = false;
103 else
104 {
105 char **papszArg;
106 int cArgs;
107
108 int vrc = RTGetOptArgvFromString(&papszArg, &cArgs, a->argv[i + 1], NULL);
109 if (RT_SUCCESS(vrc))
110 {
111 for (int j = 0; j < cArgs; j++)
112 args.push_back(Bstr(papszArg[j]));
113
114 RTGetOptArgvFree(papszArg);
115 }
116 ++i;
117 }
118 }
119 else if ( !strcmp(a->argv[i], "--environment")
120 || !strcmp(a->argv[i], "--env"))
121 {
122 if (i + 1 >= a->argc)
123 usageOK = false;
124 else
125 {
126 char **papszArg;
127 int cArgs;
128
129 int vrc = RTGetOptArgvFromString(&papszArg, &cArgs, a->argv[i + 1], NULL);
130 if (RT_SUCCESS(vrc))
131 {
132 for (int j = 0; j < cArgs; j++)
133 env.push_back(Bstr(papszArg[j]));
134
135 RTGetOptArgvFree(papszArg);
136 }
137 ++i;
138 }
139 }
140 else if (!strcmp(a->argv[i], "--flags"))
141 {
142 if ( i + 1 >= a->argc
143 || RTStrToUInt32Full(a->argv[i + 1], 10, &uFlags) != VINF_SUCCESS)
144 usageOK = false;
145 else
146 ++i;
147 }
148 else if ( !strcmp(a->argv[i], "--username")
149 || !strcmp(a->argv[i], "--user"))
150 {
151 if (i + 1 >= a->argc)
152 usageOK = false;
153 else
154 {
155 Utf8UserName = a->argv[i + 1];
156 ++i;
157 }
158 }
159 else if ( !strcmp(a->argv[i], "--password")
160 || !strcmp(a->argv[i], "--pwd"))
161 {
162 if (i + 1 >= a->argc)
163 usageOK = false;
164 else
165 {
166 Utf8Password = a->argv[i + 1];
167 ++i;
168 }
169 }
170 else if (!strcmp(a->argv[i], "--timeout"))
171 {
172 if ( i + 1 >= a->argc
173 || RTStrToUInt32Full(a->argv[i + 1], 10, &uTimeoutMS) != VINF_SUCCESS)
174 usageOK = false;
175 else
176 ++i;
177 }
178 else if (!strcmp(a->argv[i], "--wait"))
179 {
180 if (i + 1 >= a->argc)
181 usageOK = false;
182 else
183 {
184 /** @todo Check for "stdout" or "stderr"! */
185 waitForOutput = true;
186 ++i;
187 }
188 }
189 else if (!strcmp(a->argv[i], "--verbose"))
190 {
191 verbose = true;
192 }
193 /** @todo Add fancy piping stuff here. */
194 else
195 {
196 return errorSyntax(USAGE_GUESTCONTROL,
197 "Invalid parameter '%s'", Utf8Str(a->argv[i]).raw());
198 }
199 }
200
201 if (!usageOK)
202 return errorSyntax(USAGE_GUESTCONTROL, "Incorrect parameters");
203
204 /* If a password was specified, check if we also got a user name. */
205 if ( !Utf8Password.isEmpty()
206 && Utf8UserName.isEmpty())
207 {
208 return errorSyntax(USAGE_GUESTCONTROL,
209 "No user name for password specified!");
210 }
211
212 /* lookup VM. */
213 ComPtr<IMachine> machine;
214 /* assume it's an UUID */
215 HRESULT rc = a->virtualBox->GetMachine(Bstr(a->argv[0]), machine.asOutParam());
216 if (FAILED(rc) || !machine)
217 {
218 /* must be a name */
219 CHECK_ERROR(a->virtualBox, FindMachine(Bstr(a->argv[0]), machine.asOutParam()));
220 }
221
222 if (machine)
223 {
224 do
225 {
226 Bstr uuid;
227 machine->COMGETTER(Id)(uuid.asOutParam());
228
229 /* open an existing session for VM - so the VM has to be running */
230 CHECK_ERROR_BREAK(a->virtualBox, OpenExistingSession(a->session, uuid));
231
232 /* get the mutable session machine */
233 a->session->COMGETTER(Machine)(machine.asOutParam());
234
235 /* get the associated console */
236 ComPtr<IConsole> console;
237 CHECK_ERROR_BREAK(a->session, COMGETTER(Console)(console.asOutParam()));
238
239 ComPtr<IGuest> guest;
240 CHECK_ERROR_BREAK(console, COMGETTER(Guest)(guest.asOutParam()));
241
242 ComPtr<IProgress> progress;
243 ULONG uPID = 0;
244
245 if (verbose) RTPrintf("Waiting for guest to start process ...\n");
246 CHECK_ERROR_BREAK(guest, ExecuteProcess(Bstr(Utf8Cmd), uFlags,
247 ComSafeArrayAsInParam(args), ComSafeArrayAsInParam(env),
248 Bstr(Utf8StdIn), Bstr(Utf8StdOut), Bstr(Utf8StdErr),
249 Bstr(Utf8UserName), Bstr(Utf8Password), uTimeoutMS,
250 &uPID, progress.asOutParam()));
251 if (verbose) RTPrintf("Process \"%s\" (PID: %u) started.\n", Utf8Cmd.raw(), uPID);
252 if (waitForOutput)
253 {
254 if (verbose) RTPrintf("Waiting for output ...\n");
255 /** @todo */
256 }
257 /** @todo Show some progress here? */
258 a->session->Close();
259 } while (0);
260 }
261 return SUCCEEDED(rc) ? 0 : 1;
262}
263
264/**
265 * Access the guest control store.
266 *
267 * @returns 0 on success, 1 on failure
268 * @note see the command line API description for parameters
269 */
270int handleGuestControl(HandlerArg *a)
271{
272 HandlerArg arg = *a;
273 arg.argc = a->argc - 1;
274 arg.argv = a->argv + 1;
275
276 if (a->argc == 0)
277 return errorSyntax(USAGE_GUESTCONTROL, "Incorrect parameters");
278
279 /* switch (cmd) */
280 if ( strcmp(a->argv[0], "exec") == 0
281 || strcmp(a->argv[0], "execute") == 0)
282 return handleExecProgram(&arg);
283
284 /* default: */
285 return errorSyntax(USAGE_GUESTCONTROL, "Incorrect parameters");
286}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette