VirtualBox

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

Last change on this file since 31599 was 31539, checked in by vboxsync, 14 years ago

Main: use settings struct for machine user data; remove iprt::MiniString::raw() and change all occurences to c_str()

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 19.2 KB
Line 
1/* $Id: VBoxManageGuestCtrl.cpp 31539 2010-08-10 15:40:18Z 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/HostServices/GuestControlSvc.h> /* for PROC_STS_XXX */
34
35#include <VBox/log.h>
36#include <iprt/asm.h>
37#include <iprt/getopt.h>
38#include <iprt/stream.h>
39#include <iprt/string.h>
40#include <iprt/time.h>
41#include <iprt/thread.h>
42
43#ifdef USE_XPCOM_QUEUE
44# include <sys/select.h>
45# include <errno.h>
46#endif
47
48#include <signal.h>
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
62/** Set by the signal handler. */
63static volatile bool g_fExecCanceled = false;
64
65void usageGuestControl(void)
66{
67 RTPrintf("VBoxManage guestcontrol execute <vmname>|<uuid>\n"
68 " <path to program>\n"
69 " --username <name> --password <password>\n"
70 " [--arguments \"<arguments>\"]\n"
71 " [--environment \"<NAME>=<VALUE> [<NAME>=<VALUE>]\"]\n"
72 " [--flags <flags>] [--timeout <msec>]\n"
73 " [--verbose] [--wait-for exit,stdout,stderr||]\n"
74 "\n");
75}
76
77/**
78 * Signal handler that sets g_fCanceled.
79 *
80 * This can be executed on any thread in the process, on Windows it may even be
81 * a thread dedicated to delivering this signal. Do not doing anything
82 * unnecessary here.
83 */
84static void execProcessSignalHandler(int iSignal)
85{
86 NOREF(iSignal);
87 ASMAtomicWriteBool(&g_fExecCanceled, true);
88}
89
90static const char *getStatus(ULONG uStatus)
91{
92 switch (uStatus)
93 {
94 case guestControl::PROC_STS_STARTED:
95 return "started";
96 case guestControl::PROC_STS_TEN:
97 return "successfully terminated";
98 case guestControl::PROC_STS_TES:
99 return "terminated by signal";
100 case guestControl::PROC_STS_TEA:
101 return "abnormally aborted";
102 case guestControl::PROC_STS_TOK:
103 return "timed out";
104 case guestControl::PROC_STS_TOA:
105 return "timed out, hanging";
106 case guestControl::PROC_STS_DWN:
107 return "killed";
108 case guestControl::PROC_STS_ERROR:
109 return "error";
110 default:
111 return "unknown";
112 }
113}
114
115static int handleExecProgram(HandlerArg *a)
116{
117 /*
118 * Check the syntax. We can deduce the correct syntax from the number of
119 * arguments.
120 */
121 if (a->argc < 2) /* At least the command we want to execute in the guest should be present :-). */
122 return errorSyntax(USAGE_GUESTCONTROL, "Incorrect parameters");
123
124 Utf8Str Utf8Cmd(a->argv[1]);
125 uint32_t uFlags = 0;
126 com::SafeArray <BSTR> args;
127 com::SafeArray <BSTR> env;
128 Utf8Str Utf8UserName;
129 Utf8Str Utf8Password;
130 uint32_t u32TimeoutMS = 0;
131 bool fWaitForExit = false;
132 bool fWaitForStdOut = false;
133 bool fWaitForStdErr = false;
134 bool fVerbose = false;
135 bool fTimeout = false;
136
137 /* Always use the actual command line as argv[0]. */
138 args.push_back(Bstr(Utf8Cmd));
139
140 /* Iterate through all possible commands (if available). */
141 bool usageOK = true;
142 for (int i = 2; usageOK && i < a->argc; i++)
143 {
144 if ( !strcmp(a->argv[i], "--arguments")
145 || !strcmp(a->argv[i], "--args")
146 || !strcmp(a->argv[i], "--arg"))
147 {
148 if (i + 1 >= a->argc)
149 usageOK = false;
150 else
151 {
152 char **papszArg;
153 int cArgs;
154
155 int vrc = RTGetOptArgvFromString(&papszArg, &cArgs, a->argv[i + 1], NULL);
156 if (RT_SUCCESS(vrc))
157 {
158 for (int j = 0; j < cArgs; j++)
159 args.push_back(Bstr(papszArg[j]));
160
161 RTGetOptArgvFree(papszArg);
162 }
163 ++i;
164 }
165 }
166 else if ( !strcmp(a->argv[i], "--environment")
167 || !strcmp(a->argv[i], "--env"))
168 {
169 if (i + 1 >= a->argc)
170 usageOK = false;
171 else
172 {
173 char **papszArg;
174 int cArgs;
175
176 int vrc = RTGetOptArgvFromString(&papszArg, &cArgs, a->argv[i + 1], NULL);
177 if (RT_SUCCESS(vrc))
178 {
179 for (int j = 0; j < cArgs; j++)
180 env.push_back(Bstr(papszArg[j]));
181
182 RTGetOptArgvFree(papszArg);
183 }
184 ++i;
185 }
186 }
187 else if (!strcmp(a->argv[i], "--flags"))
188 {
189 if ( i + 1 >= a->argc
190 || RTStrToUInt32Full(a->argv[i + 1], 10, &uFlags) != VINF_SUCCESS)
191 usageOK = false;
192 else
193 ++i;
194 }
195 else if ( !strcmp(a->argv[i], "--username")
196 || !strcmp(a->argv[i], "--user"))
197 {
198 if (i + 1 >= a->argc)
199 usageOK = false;
200 else
201 {
202 Utf8UserName = a->argv[i + 1];
203 ++i;
204 }
205 }
206 else if ( !strcmp(a->argv[i], "--password")
207 || !strcmp(a->argv[i], "--pwd"))
208 {
209 if (i + 1 >= a->argc)
210 usageOK = false;
211 else
212 {
213 Utf8Password = a->argv[i + 1];
214 ++i;
215 }
216 }
217 else if (!strcmp(a->argv[i], "--timeout"))
218 {
219 if ( i + 1 >= a->argc
220 || RTStrToUInt32Full(a->argv[i + 1], 10, &u32TimeoutMS) != VINF_SUCCESS
221 || u32TimeoutMS == 0)
222 {
223 usageOK = false;
224 }
225 else
226 {
227 fTimeout = true;
228 ++i;
229 }
230 }
231 else if (!strcmp(a->argv[i], "--wait-for"))
232 {
233 if (i + 1 >= a->argc)
234 usageOK = false;
235 else
236 {
237 if (!strcmp(a->argv[i + 1], "exit"))
238 fWaitForExit = true;
239 else if (!strcmp(a->argv[i + 1], "stdout"))
240 {
241 fWaitForExit = true;
242 fWaitForStdOut = true;
243 }
244 else if (!strcmp(a->argv[i + 1], "stderr"))
245 {
246 fWaitForExit = true;
247 fWaitForStdErr = true;
248 }
249 else
250 usageOK = false;
251 ++i;
252 }
253 }
254 else if (!strcmp(a->argv[i], "--verbose"))
255 fVerbose = true;
256 /** @todo Add fancy piping stuff here. */
257 else
258 return errorSyntax(USAGE_GUESTCONTROL,
259 "Invalid parameter '%s'", Utf8Str(a->argv[i]).c_str());
260 }
261
262 if (!usageOK)
263 return errorSyntax(USAGE_GUESTCONTROL, "Incorrect parameters");
264
265 if (Utf8UserName.isEmpty())
266 return errorSyntax(USAGE_GUESTCONTROL,
267 "No user name specified!");
268
269 /* lookup VM. */
270 ComPtr<IMachine> machine;
271 /* assume it's an UUID */
272 HRESULT rc = a->virtualBox->GetMachine(Bstr(a->argv[0]), machine.asOutParam());
273 if (FAILED(rc) || !machine)
274 {
275 /* must be a name */
276 CHECK_ERROR(a->virtualBox, FindMachine(Bstr(a->argv[0]), machine.asOutParam()));
277 }
278
279 if (machine)
280 {
281 do
282 {
283 /* open an existing session for VM */
284 CHECK_ERROR_BREAK(machine, LockMachine(a->session, LockType_Shared));
285 // @todo r=dj assert that it's an existing session
286
287 /* get the mutable session machine */
288 a->session->COMGETTER(Machine)(machine.asOutParam());
289
290 /* get the associated console */
291 ComPtr<IConsole> console;
292 CHECK_ERROR_BREAK(a->session, COMGETTER(Console)(console.asOutParam()));
293
294 ComPtr<IGuest> guest;
295 CHECK_ERROR_BREAK(console, COMGETTER(Guest)(guest.asOutParam()));
296
297 ComPtr<IProgress> progress;
298 ULONG uPID = 0;
299
300 if (fVerbose)
301 {
302 if (u32TimeoutMS == 0)
303 RTPrintf("Waiting for guest to start process ...\n");
304 else
305 RTPrintf("Waiting for guest to start process (within %ums)\n", u32TimeoutMS);
306 }
307
308 /* Get current time stamp to later calculate rest of timeout left. */
309 uint64_t u64StartMS = RTTimeMilliTS();
310
311 /* Execute the process. */
312 rc = guest->ExecuteProcess(Bstr(Utf8Cmd), uFlags,
313 ComSafeArrayAsInParam(args), ComSafeArrayAsInParam(env),
314 Bstr(Utf8UserName), Bstr(Utf8Password), u32TimeoutMS,
315 &uPID, progress.asOutParam());
316 if (FAILED(rc))
317 {
318 /* If we got a VBOX_E_IPRT error we handle the error in a more gentle way
319 * because it contains more accurate info about what went wrong. */
320 ErrorInfo info(guest, COM_IIDOF(IGuest));
321 if (info.isFullAvailable())
322 {
323 if (rc == VBOX_E_IPRT_ERROR)
324 RTPrintf("%ls.\n", info.getText().raw());
325 else
326 RTPrintf("ERROR: %ls (%Rhrc).\n", info.getText().raw(), info.getResultCode());
327 }
328 break;
329 }
330 if (fVerbose)
331 RTPrintf("Process '%s' (PID: %u) started\n", Utf8Cmd.c_str(), uPID);
332 if (fWaitForExit)
333 {
334 if (fTimeout)
335 {
336 /* Calculate timeout value left after process has been started. */
337 uint64_t u64Elapsed = RTTimeMilliTS() - u64StartMS;
338 /* Is timeout still bigger than current difference? */
339 if (u32TimeoutMS > u64Elapsed)
340 {
341 u32TimeoutMS -= (uint32_t)u64Elapsed;
342 if (fVerbose)
343 RTPrintf("Waiting for process to exit (%ums left) ...\n", u32TimeoutMS);
344 }
345 else
346 {
347 if (fVerbose)
348 RTPrintf("No time left to wait for process!\n");
349 }
350 }
351 else if (fVerbose)
352 RTPrintf("Waiting for process to exit ...\n");
353
354 /* setup signal handling if cancelable */
355 ASSERT(progress);
356 bool fCanceledAlready = false;
357 BOOL fCancelable;
358 HRESULT hrc = progress->COMGETTER(Cancelable)(&fCancelable);
359 if (FAILED(hrc))
360 fCancelable = FALSE;
361 if (fCancelable)
362 {
363 signal(SIGINT, execProcessSignalHandler);
364 #ifdef SIGBREAK
365 signal(SIGBREAK, execProcessSignalHandler);
366 #endif
367 }
368
369 /* Wait for process to exit ... */
370 BOOL fCompleted = FALSE;
371 BOOL fCanceled = FALSE;
372 while (SUCCEEDED(progress->COMGETTER(Completed(&fCompleted))))
373 {
374 SafeArray<BYTE> aOutputData;
375 ULONG cbOutputData = 0;
376
377 /*
378 * Some data left to output?
379 */
380 if ( fWaitForStdOut
381 || fWaitForStdErr)
382 {
383
384 rc = guest->GetProcessOutput(uPID, 0 /* aFlags */,
385 u32TimeoutMS, _64K, ComSafeArrayAsOutParam(aOutputData));
386 if (FAILED(rc))
387 {
388 /* If we got a VBOX_E_IPRT error we handle the error in a more gentle way
389 * because it contains more accurate info about what went wrong. */
390 ErrorInfo info(guest, COM_IIDOF(IGuest));
391 if (info.isFullAvailable())
392 {
393 if (rc == VBOX_E_IPRT_ERROR)
394 {
395 RTPrintf("%ls.\n", info.getText().raw());
396 }
397 else
398 {
399 RTPrintf("ERROR: %ls (%Rhrc).\n", info.getText().raw(), info.getResultCode());
400 }
401 }
402 cbOutputData = 0;
403 }
404 else
405 {
406 cbOutputData = aOutputData.size();
407 if (cbOutputData > 0)
408 {
409 /* aOutputData has a platform dependent line ending, standardize on
410 * Unix style, as RTStrmWrite does the LF -> CR/LF replacement on
411 * Windows. Otherwise we end up with CR/CR/LF on Windows. */
412 ULONG cbOutputDataPrint = cbOutputData;
413 for (BYTE *s = aOutputData.raw(), *d = s;
414 s - aOutputData.raw() < (ssize_t)cbOutputData;
415 s++, d++)
416 {
417 if (*s == '\r')
418 {
419 /* skip over CR, adjust destination */
420 d--;
421 cbOutputDataPrint--;
422 }
423 else if (s != d)
424 *d = *s;
425 }
426 RTStrmWrite(g_pStdOut, aOutputData.raw(), cbOutputDataPrint);
427 }
428 }
429 }
430
431 if (cbOutputData <= 0) /* No more output data left? */
432 {
433 if (fCompleted)
434 break;
435
436 if ( fTimeout
437 && RTTimeMilliTS() - u64StartMS > u32TimeoutMS + 5000)
438 {
439 progress->Cancel();
440 break;
441 }
442 }
443
444 /* Process async cancelation */
445 if (g_fExecCanceled && !fCanceledAlready)
446 {
447 hrc = progress->Cancel();
448 if (SUCCEEDED(hrc))
449 fCanceledAlready = TRUE;
450 else
451 g_fExecCanceled = false;
452 }
453
454 /* Progress canceled by Main API? */
455 if ( SUCCEEDED(progress->COMGETTER(Canceled(&fCanceled)))
456 && fCanceled)
457 {
458 break;
459 }
460 }
461
462 /* Undo signal handling */
463 if (fCancelable)
464 {
465 signal(SIGINT, SIG_DFL);
466 #ifdef SIGBREAK
467 signal(SIGBREAK, SIG_DFL);
468 #endif
469 }
470
471 if (fCanceled)
472 {
473 if (fVerbose)
474 RTPrintf("Process execution canceled!\n");
475 }
476 else if ( fCompleted
477 && SUCCEEDED(rc))
478 {
479 LONG iRc = false;
480 CHECK_ERROR_RET(progress, COMGETTER(ResultCode)(&iRc), rc);
481 if (FAILED(iRc))
482 {
483 ComPtr<IVirtualBoxErrorInfo> execError;
484 rc = progress->COMGETTER(ErrorInfo)(execError.asOutParam());
485 com::ErrorInfo info(execError, COM_IIDOF(IVirtualBoxErrorInfo));
486 if (SUCCEEDED(rc) && info.isFullAvailable())
487 {
488 /* If we got a VBOX_E_IPRT error we handle the error in a more gentle way
489 * because it contains more accurate info about what went wrong. */
490 if (info.getResultCode() == VBOX_E_IPRT_ERROR)
491 {
492 RTPrintf("%ls.\n", info.getText().raw());
493 }
494 else
495 {
496 RTPrintf("\n\nProcess error details:\n");
497 GluePrintErrorInfo(info);
498 RTPrintf("\n");
499 }
500 }
501 else
502 AssertMsgFailed(("Full error description is missing!\n"));
503 }
504 else if (fVerbose)
505 {
506 ULONG uRetStatus, uRetExitCode, uRetFlags;
507 rc = guest->GetProcessStatus(uPID, &uRetExitCode, &uRetFlags, &uRetStatus);
508 if (SUCCEEDED(rc))
509 RTPrintf("Exit code=%u (Status=%u [%s], Flags=%u)\n", uRetExitCode, uRetStatus, getStatus(uRetStatus), uRetFlags);
510 }
511 }
512 else
513 {
514 if (fVerbose)
515 RTPrintf("Process execution aborted!\n");
516 }
517 }
518 a->session->UnlockMachine();
519 } while (0);
520 }
521 return SUCCEEDED(rc) ? 0 : 1;
522}
523
524/**
525 * Access the guest control store.
526 *
527 * @returns 0 on success, 1 on failure
528 * @note see the command line API description for parameters
529 */
530int handleGuestControl(HandlerArg *a)
531{
532 HandlerArg arg = *a;
533 arg.argc = a->argc - 1;
534 arg.argv = a->argv + 1;
535
536 if (a->argc == 0)
537 return errorSyntax(USAGE_GUESTCONTROL, "Incorrect parameters");
538
539 /* switch (cmd) */
540 if ( strcmp(a->argv[0], "exec") == 0
541 || strcmp(a->argv[0], "execute") == 0)
542 return handleExecProgram(&arg);
543
544 /* default: */
545 return errorSyntax(USAGE_GUESTCONTROL, "Incorrect parameters");
546}
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