1 | /* $Id: VBoxManageGuestCtrl.cpp 28926 2010-04-30 10:21:00Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxManage - The 'guestcontrol' command.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /*******************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *******************************************************************************/
|
---|
22 | #include "VBoxManage.h"
|
---|
23 |
|
---|
24 | #include <VBox/com/com.h>
|
---|
25 | #include <VBox/com/string.h>
|
---|
26 | #include <VBox/com/array.h>
|
---|
27 | #include <VBox/com/ErrorInfo.h>
|
---|
28 | #include <VBox/com/errorprint.h>
|
---|
29 |
|
---|
30 | #include <VBox/com/VirtualBox.h>
|
---|
31 | #include <VBox/com/EventQueue.h>
|
---|
32 |
|
---|
33 | #include <VBox/log.h>
|
---|
34 | #include <iprt/asm.h>
|
---|
35 | #include <iprt/getopt.h>
|
---|
36 | #include <iprt/stream.h>
|
---|
37 | #include <iprt/string.h>
|
---|
38 | #include <iprt/time.h>
|
---|
39 | #include <iprt/thread.h>
|
---|
40 |
|
---|
41 | #ifdef USE_XPCOM_QUEUE
|
---|
42 | # include <sys/select.h>
|
---|
43 | # include <errno.h>
|
---|
44 | #endif
|
---|
45 |
|
---|
46 | #include <signal.h>
|
---|
47 |
|
---|
48 | #ifdef RT_OS_DARWIN
|
---|
49 | # include <CoreFoundation/CFRunLoop.h>
|
---|
50 | #endif
|
---|
51 |
|
---|
52 | using namespace com;
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * IVirtualBoxCallback implementation for handling the GuestControlCallback in
|
---|
56 | * relation to the "guestcontrol * wait" command.
|
---|
57 | */
|
---|
58 | /** @todo */
|
---|
59 |
|
---|
60 | /** Set by the signal handler. */
|
---|
61 | static volatile bool g_fExecCanceled = false;
|
---|
62 |
|
---|
63 | void usageGuestControl(void)
|
---|
64 | {
|
---|
65 | RTPrintf("VBoxManage guestcontrol execute <vmname>|<uuid>\n"
|
---|
66 | " <path to program> [--arguments \"<arguments>\"]\n"
|
---|
67 | " [--environment \"<NAME>=<VALUE> [<NAME>=<VALUE>]\"]\n"
|
---|
68 | " [--flags <flags>] [--timeout <msec>]\n"
|
---|
69 | " [--username <name> [--password <password>]]\n"
|
---|
70 | " [--verbose] [--wait-for exit,stdout,stderr||]\n"
|
---|
71 | "\n");
|
---|
72 | }
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Signal handler that sets g_fCanceled.
|
---|
76 | *
|
---|
77 | * This can be executed on any thread in the process, on Windows it may even be
|
---|
78 | * a thread dedicated to delivering this signal. Do not doing anything
|
---|
79 | * unnecessary here.
|
---|
80 | */
|
---|
81 | static void execProcessSignalHandler(int iSignal)
|
---|
82 | {
|
---|
83 | NOREF(iSignal);
|
---|
84 | ASMAtomicWriteBool(&g_fExecCanceled, true);
|
---|
85 | }
|
---|
86 |
|
---|
87 | static int handleExecProgram(HandlerArg *a)
|
---|
88 | {
|
---|
89 | /*
|
---|
90 | * Check the syntax. We can deduce the correct syntax from the number of
|
---|
91 | * arguments.
|
---|
92 | */
|
---|
93 | if (a->argc < 2) /* At least the command we want to execute in the guest should be present :-). */
|
---|
94 | return errorSyntax(USAGE_GUESTCONTROL, "Incorrect parameters");
|
---|
95 |
|
---|
96 | Utf8Str Utf8Cmd(a->argv[1]);
|
---|
97 | uint32_t uFlags = 0;
|
---|
98 | com::SafeArray <BSTR> args;
|
---|
99 | com::SafeArray <BSTR> env;
|
---|
100 | Utf8Str Utf8StdIn;
|
---|
101 | Utf8Str Utf8StdOut;
|
---|
102 | Utf8Str Utf8StdErr;
|
---|
103 | Utf8Str Utf8UserName;
|
---|
104 | Utf8Str Utf8Password;
|
---|
105 | uint32_t u32TimeoutMS = 0;
|
---|
106 | bool waitForExit = false;
|
---|
107 | bool waitForStdOut = false;
|
---|
108 | bool waitForStdErr = false;
|
---|
109 | bool verbose = false;
|
---|
110 | bool have_timeout = false;
|
---|
111 |
|
---|
112 | /* Always use the actual command line as argv[0]. */
|
---|
113 | args.push_back(Bstr(Utf8Cmd));
|
---|
114 |
|
---|
115 | /* Iterate through all possible commands (if available). */
|
---|
116 | bool usageOK = true;
|
---|
117 | for (int i = 2; usageOK && i < a->argc; i++)
|
---|
118 | {
|
---|
119 | if ( !strcmp(a->argv[i], "--arguments")
|
---|
120 | || !strcmp(a->argv[i], "--args")
|
---|
121 | || !strcmp(a->argv[i], "--arg"))
|
---|
122 | {
|
---|
123 | if (i + 1 >= a->argc)
|
---|
124 | usageOK = false;
|
---|
125 | else
|
---|
126 | {
|
---|
127 | char **papszArg;
|
---|
128 | int cArgs;
|
---|
129 |
|
---|
130 | int vrc = RTGetOptArgvFromString(&papszArg, &cArgs, a->argv[i + 1], NULL);
|
---|
131 | if (RT_SUCCESS(vrc))
|
---|
132 | {
|
---|
133 | for (int j = 0; j < cArgs; j++)
|
---|
134 | args.push_back(Bstr(papszArg[j]));
|
---|
135 |
|
---|
136 | RTGetOptArgvFree(papszArg);
|
---|
137 | }
|
---|
138 | ++i;
|
---|
139 | }
|
---|
140 | }
|
---|
141 | else if ( !strcmp(a->argv[i], "--environment")
|
---|
142 | || !strcmp(a->argv[i], "--env"))
|
---|
143 | {
|
---|
144 | if (i + 1 >= a->argc)
|
---|
145 | usageOK = false;
|
---|
146 | else
|
---|
147 | {
|
---|
148 | char **papszArg;
|
---|
149 | int cArgs;
|
---|
150 |
|
---|
151 | int vrc = RTGetOptArgvFromString(&papszArg, &cArgs, a->argv[i + 1], NULL);
|
---|
152 | if (RT_SUCCESS(vrc))
|
---|
153 | {
|
---|
154 | for (int j = 0; j < cArgs; j++)
|
---|
155 | env.push_back(Bstr(papszArg[j]));
|
---|
156 |
|
---|
157 | RTGetOptArgvFree(papszArg);
|
---|
158 | }
|
---|
159 | ++i;
|
---|
160 | }
|
---|
161 | }
|
---|
162 | else if (!strcmp(a->argv[i], "--flags"))
|
---|
163 | {
|
---|
164 | if ( i + 1 >= a->argc
|
---|
165 | || RTStrToUInt32Full(a->argv[i + 1], 10, &uFlags) != VINF_SUCCESS)
|
---|
166 | usageOK = false;
|
---|
167 | else
|
---|
168 | ++i;
|
---|
169 | }
|
---|
170 | else if ( !strcmp(a->argv[i], "--username")
|
---|
171 | || !strcmp(a->argv[i], "--user"))
|
---|
172 | {
|
---|
173 | if (i + 1 >= a->argc)
|
---|
174 | usageOK = false;
|
---|
175 | else
|
---|
176 | {
|
---|
177 | Utf8UserName = a->argv[i + 1];
|
---|
178 | ++i;
|
---|
179 | }
|
---|
180 | }
|
---|
181 | else if ( !strcmp(a->argv[i], "--password")
|
---|
182 | || !strcmp(a->argv[i], "--pwd"))
|
---|
183 | {
|
---|
184 | if (i + 1 >= a->argc)
|
---|
185 | usageOK = false;
|
---|
186 | else
|
---|
187 | {
|
---|
188 | Utf8Password = a->argv[i + 1];
|
---|
189 | ++i;
|
---|
190 | }
|
---|
191 | }
|
---|
192 | else if (!strcmp(a->argv[i], "--timeout"))
|
---|
193 | {
|
---|
194 | if ( i + 1 >= a->argc
|
---|
195 | || RTStrToUInt32Full(a->argv[i + 1], 10, &u32TimeoutMS) != VINF_SUCCESS
|
---|
196 | || u32TimeoutMS == 0)
|
---|
197 | {
|
---|
198 | usageOK = false;
|
---|
199 | }
|
---|
200 | else
|
---|
201 | {
|
---|
202 | have_timeout = true;
|
---|
203 | ++i;
|
---|
204 | }
|
---|
205 | }
|
---|
206 | else if (!strcmp(a->argv[i], "--wait-for"))
|
---|
207 | {
|
---|
208 | if (i + 1 >= a->argc)
|
---|
209 | usageOK = false;
|
---|
210 | else
|
---|
211 | {
|
---|
212 | if (!strcmp(a->argv[i + 1], "exit"))
|
---|
213 | waitForExit = true;
|
---|
214 | else if (!strcmp(a->argv[i + 1], "stdout"))
|
---|
215 | {
|
---|
216 | waitForExit = true;
|
---|
217 | waitForStdOut = true;
|
---|
218 | }
|
---|
219 | else if (!strcmp(a->argv[i + 1], "stderr"))
|
---|
220 | {
|
---|
221 | waitForExit = true;
|
---|
222 | waitForStdErr = true;
|
---|
223 | }
|
---|
224 | else
|
---|
225 | usageOK = false;
|
---|
226 | ++i;
|
---|
227 | }
|
---|
228 | }
|
---|
229 | else if (!strcmp(a->argv[i], "--verbose"))
|
---|
230 | {
|
---|
231 | verbose = true;
|
---|
232 | }
|
---|
233 | /** @todo Add fancy piping stuff here. */
|
---|
234 | else
|
---|
235 | {
|
---|
236 | return errorSyntax(USAGE_GUESTCONTROL,
|
---|
237 | "Invalid parameter '%s'", Utf8Str(a->argv[i]).raw());
|
---|
238 | }
|
---|
239 | }
|
---|
240 |
|
---|
241 | if (!usageOK)
|
---|
242 | return errorSyntax(USAGE_GUESTCONTROL, "Incorrect parameters");
|
---|
243 |
|
---|
244 | /* If a password was specified, check if we also got a user name. */
|
---|
245 | if ( !Utf8Password.isEmpty()
|
---|
246 | && Utf8UserName.isEmpty())
|
---|
247 | {
|
---|
248 | return errorSyntax(USAGE_GUESTCONTROL,
|
---|
249 | "No user name for password specified!");
|
---|
250 | }
|
---|
251 |
|
---|
252 | /* lookup VM. */
|
---|
253 | ComPtr<IMachine> machine;
|
---|
254 | /* assume it's an UUID */
|
---|
255 | HRESULT rc = a->virtualBox->GetMachine(Bstr(a->argv[0]), machine.asOutParam());
|
---|
256 | if (FAILED(rc) || !machine)
|
---|
257 | {
|
---|
258 | /* must be a name */
|
---|
259 | CHECK_ERROR(a->virtualBox, FindMachine(Bstr(a->argv[0]), machine.asOutParam()));
|
---|
260 | }
|
---|
261 |
|
---|
262 | if (machine)
|
---|
263 | {
|
---|
264 | do
|
---|
265 | {
|
---|
266 | Bstr uuid;
|
---|
267 | machine->COMGETTER(Id)(uuid.asOutParam());
|
---|
268 |
|
---|
269 | /* open an existing session for VM - so the VM has to be running */
|
---|
270 | CHECK_ERROR_BREAK(a->virtualBox, OpenExistingSession(a->session, uuid));
|
---|
271 |
|
---|
272 | /* get the mutable session machine */
|
---|
273 | a->session->COMGETTER(Machine)(machine.asOutParam());
|
---|
274 |
|
---|
275 | /* get the associated console */
|
---|
276 | ComPtr<IConsole> console;
|
---|
277 | CHECK_ERROR_BREAK(a->session, COMGETTER(Console)(console.asOutParam()));
|
---|
278 |
|
---|
279 | ComPtr<IGuest> guest;
|
---|
280 | CHECK_ERROR_BREAK(console, COMGETTER(Guest)(guest.asOutParam()));
|
---|
281 |
|
---|
282 | ComPtr<IProgress> progress;
|
---|
283 | ULONG uPID = 0;
|
---|
284 |
|
---|
285 | if (verbose)
|
---|
286 | {
|
---|
287 | if (u32TimeoutMS == 0)
|
---|
288 | RTPrintf("Waiting for guest to start process ...\n");
|
---|
289 | else
|
---|
290 | RTPrintf("Waiting for guest to start process (within %ums)\n", u32TimeoutMS);
|
---|
291 | }
|
---|
292 |
|
---|
293 | /* Get current time stamp to later calculate rest of timeout left. */
|
---|
294 | uint64_t u64StartMS = RTTimeMilliTS();
|
---|
295 |
|
---|
296 | /* Execute the process. */
|
---|
297 | CHECK_ERROR_BREAK(guest, ExecuteProcess(Bstr(Utf8Cmd), uFlags,
|
---|
298 | ComSafeArrayAsInParam(args), ComSafeArrayAsInParam(env),
|
---|
299 | Bstr(Utf8StdIn), Bstr(Utf8StdOut), Bstr(Utf8StdErr),
|
---|
300 | Bstr(Utf8UserName), Bstr(Utf8Password), u32TimeoutMS,
|
---|
301 | &uPID, progress.asOutParam()));
|
---|
302 | if (verbose) RTPrintf("Process '%s' (PID: %u) started\n", Utf8Cmd.raw(), uPID);
|
---|
303 | if (waitForExit)
|
---|
304 | {
|
---|
305 | if (have_timeout)
|
---|
306 | {
|
---|
307 | /* Calculate timeout value left after process has been started. */
|
---|
308 | uint64_t u64Diff = RTTimeMilliTS() - u64StartMS;
|
---|
309 | /** @todo Check for uint64_t vs. uint32_t here! */
|
---|
310 | if (u32TimeoutMS > u64Diff) /* Is timeout still bigger than current difference? */
|
---|
311 | {
|
---|
312 | u32TimeoutMS = u32TimeoutMS - u64Diff;
|
---|
313 | if (verbose)
|
---|
314 | RTPrintf("Waiting for process to exit (%ums left) ...\n", u32TimeoutMS);
|
---|
315 | }
|
---|
316 | else
|
---|
317 | {
|
---|
318 | if (verbose)
|
---|
319 | RTPrintf("No time left to wait for process!\n");
|
---|
320 | }
|
---|
321 | }
|
---|
322 | else if (verbose)
|
---|
323 | RTPrintf("Waiting for process to exit ...\n");
|
---|
324 |
|
---|
325 | /* setup signal handling if cancelable */
|
---|
326 | ASSERT(progress);
|
---|
327 | bool fCanceledAlready = false;
|
---|
328 | BOOL fCancelable;
|
---|
329 | HRESULT hrc = progress->COMGETTER(Cancelable)(&fCancelable);
|
---|
330 | if (FAILED(hrc))
|
---|
331 | fCancelable = FALSE;
|
---|
332 | if (fCancelable)
|
---|
333 | {
|
---|
334 | signal(SIGINT, execProcessSignalHandler);
|
---|
335 | #ifdef SIGBREAK
|
---|
336 | signal(SIGBREAK, execProcessSignalHandler);
|
---|
337 | #endif
|
---|
338 | }
|
---|
339 |
|
---|
340 | /* Wait for process to exit ... */
|
---|
341 | BOOL fCompleted = false;
|
---|
342 | ULONG cbOutputData = 0;
|
---|
343 | SafeArray<BYTE> aOutputData;
|
---|
344 | while (SUCCEEDED(progress->COMGETTER(Completed(&fCompleted))))
|
---|
345 | {
|
---|
346 | if (cbOutputData <= 0)
|
---|
347 | {
|
---|
348 | if (fCompleted)
|
---|
349 | break;
|
---|
350 |
|
---|
351 | if ( have_timeout
|
---|
352 | && RTTimeMilliTS() - u64StartMS > u32TimeoutMS + 5000)
|
---|
353 | {
|
---|
354 | progress->Cancel();
|
---|
355 | break;
|
---|
356 | }
|
---|
357 | }
|
---|
358 |
|
---|
359 | CHECK_ERROR_BREAK(guest, GetProcessOutput(uPID, 0 /* aFlags */,
|
---|
360 | u32TimeoutMS, _64K, ComSafeArrayAsOutParam(aOutputData)));
|
---|
361 | cbOutputData = aOutputData.size();
|
---|
362 | if (cbOutputData > 0)
|
---|
363 | {
|
---|
364 | /* aOutputData has a platform dependent line ending, standardize on
|
---|
365 | * Unix style, as RTStrmWrite does the LF -> CR/LF replacement on
|
---|
366 | * Windows. Otherwise we end up with CR/CR/LF on Windows. */
|
---|
367 | ULONG cbOutputDataPrint = cbOutputData;
|
---|
368 | for (BYTE *s = aOutputData.raw(), *d = s;
|
---|
369 | s - aOutputData.raw() < (ssize_t)cbOutputData;
|
---|
370 | s++, d++)
|
---|
371 | {
|
---|
372 | if (*s == '\r')
|
---|
373 | {
|
---|
374 | /* skip over CR, adjust destination */
|
---|
375 | d--;
|
---|
376 | cbOutputDataPrint--;
|
---|
377 | }
|
---|
378 | else if (s != d)
|
---|
379 | *d = *s;
|
---|
380 | }
|
---|
381 | RTStrmWrite(g_pStdOut, aOutputData.raw(), cbOutputDataPrint);
|
---|
382 | }
|
---|
383 |
|
---|
384 | /* process async cancelation. */
|
---|
385 | if (g_fExecCanceled && !fCanceledAlready)
|
---|
386 | {
|
---|
387 | hrc = progress->Cancel();
|
---|
388 | if (SUCCEEDED(hrc))
|
---|
389 | fCanceledAlready = true;
|
---|
390 | else
|
---|
391 | g_fExecCanceled = false;
|
---|
392 | }
|
---|
393 |
|
---|
394 | progress->WaitForCompletion(100);
|
---|
395 | }
|
---|
396 |
|
---|
397 | /* undo signal handling */
|
---|
398 | if (fCancelable)
|
---|
399 | {
|
---|
400 | signal(SIGINT, SIG_DFL);
|
---|
401 | #ifdef SIGBREAK
|
---|
402 | signal(SIGBREAK, SIG_DFL);
|
---|
403 | #endif
|
---|
404 | }
|
---|
405 |
|
---|
406 | /* Not completed yet? -> Timeout */
|
---|
407 | if (fCompleted)
|
---|
408 | {
|
---|
409 | LONG iRc = false;
|
---|
410 | CHECK_ERROR_RET(progress, COMGETTER(ResultCode)(&iRc), rc);
|
---|
411 | if (FAILED(iRc))
|
---|
412 | {
|
---|
413 | ComPtr<IVirtualBoxErrorInfo> execError;
|
---|
414 | rc = progress->COMGETTER(ErrorInfo)(execError.asOutParam());
|
---|
415 | com::ErrorInfo info (execError);
|
---|
416 | RTPrintf("\n\nProcess error details:\n");
|
---|
417 | GluePrintErrorInfo(info);
|
---|
418 | RTPrintf("\n");
|
---|
419 | }
|
---|
420 | }
|
---|
421 | else
|
---|
422 | {
|
---|
423 | RTPrintf("Process timed out!\n");
|
---|
424 | }
|
---|
425 |
|
---|
426 | if (verbose)
|
---|
427 | {
|
---|
428 | ULONG uRetStatus, uRetExitCode, uRetFlags;
|
---|
429 | CHECK_ERROR_BREAK(guest, GetProcessStatus(uPID, &uRetExitCode, &uRetFlags, &uRetStatus));
|
---|
430 | RTPrintf("Exit code=%u (Status=%u, Flags=%u)\n", uRetExitCode, uRetStatus, uRetFlags);
|
---|
431 | }
|
---|
432 | }
|
---|
433 | a->session->Close();
|
---|
434 | } while (0);
|
---|
435 | }
|
---|
436 | return SUCCEEDED(rc) ? 0 : 1;
|
---|
437 | }
|
---|
438 |
|
---|
439 | /**
|
---|
440 | * Access the guest control store.
|
---|
441 | *
|
---|
442 | * @returns 0 on success, 1 on failure
|
---|
443 | * @note see the command line API description for parameters
|
---|
444 | */
|
---|
445 | int handleGuestControl(HandlerArg *a)
|
---|
446 | {
|
---|
447 | HandlerArg arg = *a;
|
---|
448 | arg.argc = a->argc - 1;
|
---|
449 | arg.argv = a->argv + 1;
|
---|
450 |
|
---|
451 | if (a->argc == 0)
|
---|
452 | return errorSyntax(USAGE_GUESTCONTROL, "Incorrect parameters");
|
---|
453 |
|
---|
454 | /* switch (cmd) */
|
---|
455 | if ( strcmp(a->argv[0], "exec") == 0
|
---|
456 | || strcmp(a->argv[0], "execute") == 0)
|
---|
457 | return handleExecProgram(&arg);
|
---|
458 |
|
---|
459 | /* default: */
|
---|
460 | return errorSyntax(USAGE_GUESTCONTROL, "Incorrect parameters");
|
---|
461 | }
|
---|