VirtualBox

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

Last change on this file since 28381 was 28358, checked in by vboxsync, 15 years ago

Guest Control: Update.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.3 KB
Line 
1/* $Id: VBoxManageGuestCtrl.cpp 28358 2010-04-15 13:26:07Z 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>] [--timeout <msec>] [--username <name> [--password <password>]]\n"
67 " [--verbose] [--wait-for exit]\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 = 0;
90 bool waitForExit = false;
91 bool waitForStdOut = false;
92 bool waitForStdErr = false;
93 bool verbose = false;
94
95 /* Iterate through all possible commands (if available). */
96 bool usageOK = true;
97 for (int i = 2; usageOK && i < a->argc; i++)
98 {
99 if ( !strcmp(a->argv[i], "--arguments")
100 || !strcmp(a->argv[i], "--args")
101 || !strcmp(a->argv[i], "--arg"))
102 {
103 if (i + 1 >= a->argc)
104 usageOK = false;
105 else
106 {
107 char **papszArg;
108 int cArgs;
109
110 int vrc = RTGetOptArgvFromString(&papszArg, &cArgs, a->argv[i + 1], NULL);
111 if (RT_SUCCESS(vrc))
112 {
113 for (int j = 0; j < cArgs; j++)
114 args.push_back(Bstr(papszArg[j]));
115
116 RTGetOptArgvFree(papszArg);
117 }
118 ++i;
119 }
120 }
121 else if ( !strcmp(a->argv[i], "--environment")
122 || !strcmp(a->argv[i], "--env"))
123 {
124 if (i + 1 >= a->argc)
125 usageOK = false;
126 else
127 {
128 char **papszArg;
129 int cArgs;
130
131 int vrc = RTGetOptArgvFromString(&papszArg, &cArgs, a->argv[i + 1], NULL);
132 if (RT_SUCCESS(vrc))
133 {
134 for (int j = 0; j < cArgs; j++)
135 env.push_back(Bstr(papszArg[j]));
136
137 RTGetOptArgvFree(papszArg);
138 }
139 ++i;
140 }
141 }
142 else if (!strcmp(a->argv[i], "--flags"))
143 {
144 if ( i + 1 >= a->argc
145 || RTStrToUInt32Full(a->argv[i + 1], 10, &uFlags) != VINF_SUCCESS)
146 usageOK = false;
147 else
148 ++i;
149 }
150 else if ( !strcmp(a->argv[i], "--username")
151 || !strcmp(a->argv[i], "--user"))
152 {
153 if (i + 1 >= a->argc)
154 usageOK = false;
155 else
156 {
157 Utf8UserName = a->argv[i + 1];
158 ++i;
159 }
160 }
161 else if ( !strcmp(a->argv[i], "--password")
162 || !strcmp(a->argv[i], "--pwd"))
163 {
164 if (i + 1 >= a->argc)
165 usageOK = false;
166 else
167 {
168 Utf8Password = a->argv[i + 1];
169 ++i;
170 }
171 }
172 else if (!strcmp(a->argv[i], "--timeout"))
173 {
174 if ( i + 1 >= a->argc
175 || RTStrToUInt32Full(a->argv[i + 1], 10, &uTimeoutMS) != VINF_SUCCESS)
176 usageOK = false;
177 else
178 ++i;
179 }
180 else if (!strcmp(a->argv[i], "--wait-for"))
181 {
182 if (i + 1 >= a->argc)
183 usageOK = false;
184 else
185 {
186 if (!strcmp(a->argv[i + 1], "exit"))
187 waitForExit = true;
188 else if (!strcmp(a->argv[i + 1], "stdout"))
189 waitForStdOut = true;
190 else if (!strcmp(a->argv[i + 1], "stderr"))
191 waitForStdErr = true;
192 else
193 usageOK = false;
194 ++i;
195 }
196 }
197 else if (!strcmp(a->argv[i], "--verbose"))
198 {
199 verbose = true;
200 }
201 /** @todo Add fancy piping stuff here. */
202 else
203 {
204 return errorSyntax(USAGE_GUESTCONTROL,
205 "Invalid parameter '%s'", Utf8Str(a->argv[i]).raw());
206 }
207 }
208
209 if (!usageOK)
210 return errorSyntax(USAGE_GUESTCONTROL, "Incorrect parameters");
211
212 /* If a password was specified, check if we also got a user name. */
213 if ( !Utf8Password.isEmpty()
214 && Utf8UserName.isEmpty())
215 {
216 return errorSyntax(USAGE_GUESTCONTROL,
217 "No user name for password specified!");
218 }
219
220 /* lookup VM. */
221 ComPtr<IMachine> machine;
222 /* assume it's an UUID */
223 HRESULT rc = a->virtualBox->GetMachine(Bstr(a->argv[0]), machine.asOutParam());
224 if (FAILED(rc) || !machine)
225 {
226 /* must be a name */
227 CHECK_ERROR(a->virtualBox, FindMachine(Bstr(a->argv[0]), machine.asOutParam()));
228 }
229
230 if (machine)
231 {
232 do
233 {
234 Bstr uuid;
235 machine->COMGETTER(Id)(uuid.asOutParam());
236
237 /* open an existing session for VM - so the VM has to be running */
238 CHECK_ERROR_BREAK(a->virtualBox, OpenExistingSession(a->session, uuid));
239
240 /* get the mutable session machine */
241 a->session->COMGETTER(Machine)(machine.asOutParam());
242
243 /* get the associated console */
244 ComPtr<IConsole> console;
245 CHECK_ERROR_BREAK(a->session, COMGETTER(Console)(console.asOutParam()));
246
247 ComPtr<IGuest> guest;
248 CHECK_ERROR_BREAK(console, COMGETTER(Guest)(guest.asOutParam()));
249
250 ComPtr<IProgress> progress;
251 ULONG uPID = 0;
252
253 if (verbose)
254 {
255 if (uTimeoutMS == 0)
256 RTPrintf("Waiting for guest to start process ...\n");
257 else
258 RTPrintf("Waiting for guest to start process (within %ums)\n", uTimeoutMS);
259 }
260
261 /* Get current time stamp to later calculate rest of timeout left. */
262 uint32_t uStartMS = RTTimeMilliTS();
263
264 /* Execute the process. */
265 CHECK_ERROR_BREAK(guest, ExecuteProcess(Bstr(Utf8Cmd), uFlags,
266 ComSafeArrayAsInParam(args), ComSafeArrayAsInParam(env),
267 Bstr(Utf8StdIn), Bstr(Utf8StdOut), Bstr(Utf8StdErr),
268 Bstr(Utf8UserName), Bstr(Utf8Password), uTimeoutMS,
269 &uPID, progress.asOutParam()));
270 if (verbose) RTPrintf("Process '%s' (PID: %u) started.\n", Utf8Cmd.raw(), uPID);
271 if (waitForExit)
272 {
273 /* Calculate timeout value left after process has been started. */
274 if (uTimeoutMS)
275 uTimeoutMS = uTimeoutMS - (RTTimeMilliTS() - uStartMS);
276 if (verbose)
277 {
278 if (uTimeoutMS == 0)
279 RTPrintf("Waiting for process to exit ...\n");
280 else
281 RTPrintf("Waiting for process to exit (%ums left) ...\n", uTimeoutMS);
282 }
283
284 /* Wait for process to exit ... */
285 ASSERT(progress);
286 rc = progress->WaitForCompletion(uTimeoutMS == 0 ? -1 /* Wait forever */ : uTimeoutMS);
287 if (FAILED(rc))
288 {
289 if (uTimeoutMS)
290 RTStrmPrintf(g_pStdErr, "Process '%s' (PID: %u) did not end within %ums! Wait aborted.\n",
291 Utf8Cmd.raw(), uPID, uTimeoutMS);
292 break;
293 }
294 else
295 {
296 BOOL completed;
297 CHECK_ERROR_RET(progress, COMGETTER(Completed)(&completed), rc);
298 ASSERT(completed);
299
300 LONG iRc;
301 CHECK_ERROR_RET(progress, COMGETTER(ResultCode)(&iRc), rc);
302
303 if (verbose)
304 RTPrintf("Process completed.\n");
305
306 /* Print output if wanted. */
307 if ( waitForStdOut
308 || waitForStdErr)
309 {
310 Bstr strOutput;
311 CHECK_ERROR_BREAK(guest, GetProcessOutput(uPID, 0 /* @todo */, strOutput.asOutParam()));
312 if (verbose)
313 RTPrintf("Output is:\n");
314 //RTPrintf(strOutput.raw());
315 }
316 }
317 }
318 a->session->Close();
319 } while (0);
320 }
321 return SUCCEEDED(rc) ? 0 : 1;
322}
323
324/**
325 * Access the guest control store.
326 *
327 * @returns 0 on success, 1 on failure
328 * @note see the command line API description for parameters
329 */
330int handleGuestControl(HandlerArg *a)
331{
332 HandlerArg arg = *a;
333 arg.argc = a->argc - 1;
334 arg.argv = a->argv + 1;
335
336 if (a->argc == 0)
337 return errorSyntax(USAGE_GUESTCONTROL, "Incorrect parameters");
338
339 /* switch (cmd) */
340 if ( strcmp(a->argv[0], "exec") == 0
341 || strcmp(a->argv[0], "execute") == 0)
342 return handleExecProgram(&arg);
343
344 /* default: */
345 return errorSyntax(USAGE_GUESTCONTROL, "Incorrect parameters");
346}
Note: See TracBrowser for help on using the repository browser.

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