VirtualBox

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

Last change on this file since 92710 was 92594, checked in by vboxsync, 3 years ago

Main: bugref:1909: Added translation into VBoxManage excluding built-in docbook help. Fixed translation marks in the VBoxManage

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 152.5 KB
Line 
1/* $Id: VBoxManageGuestCtrl.cpp 92594 2021-11-25 09:05:48Z vboxsync $ */
2/** @file
3 * VBoxManage - Implementation of guestcontrol command.
4 */
5
6/*
7 * Copyright (C) 2010-2020 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#include "VBoxManageGuestCtrl.h"
24
25#ifndef VBOX_ONLY_DOCS
26
27#include <VBox/com/array.h>
28#include <VBox/com/com.h>
29#include <VBox/com/ErrorInfo.h>
30#include <VBox/com/errorprint.h>
31#include <VBox/com/listeners.h>
32#include <VBox/com/NativeEventQueue.h>
33#include <VBox/com/string.h>
34#include <VBox/com/VirtualBox.h>
35
36#include <VBox/err.h>
37#include <VBox/log.h>
38
39#include <iprt/asm.h>
40#include <iprt/dir.h>
41#include <iprt/file.h>
42#include <iprt/getopt.h>
43#include <iprt/list.h>
44#include <iprt/path.h>
45#include <iprt/process.h> /* For RTProcSelf(). */
46#include <iprt/semaphore.h>
47#include <iprt/thread.h>
48#include <iprt/vfs.h>
49
50#include <iprt/cpp/path.h>
51
52#include <map>
53#include <vector>
54
55#ifdef USE_XPCOM_QUEUE
56# include <sys/select.h>
57# include <errno.h>
58#endif
59
60#include <signal.h>
61
62#ifdef RT_OS_DARWIN
63# include <CoreFoundation/CFRunLoop.h>
64#endif
65
66using namespace com;
67
68
69/*********************************************************************************************************************************
70 * Defined Constants And Macros *
71*********************************************************************************************************************************/
72
73#define GCTLCMD_COMMON_OPT_USER 999 /**< The --username option number. */
74#define GCTLCMD_COMMON_OPT_PASSWORD 998 /**< The --password option number. */
75#define GCTLCMD_COMMON_OPT_PASSWORD_FILE 997 /**< The --password-file option number. */
76#define GCTLCMD_COMMON_OPT_DOMAIN 996 /**< The --domain option number. */
77/** Common option definitions. */
78#define GCTLCMD_COMMON_OPTION_DEFS() \
79 { "--user", GCTLCMD_COMMON_OPT_USER, RTGETOPT_REQ_STRING }, \
80 { "--username", GCTLCMD_COMMON_OPT_USER, RTGETOPT_REQ_STRING }, \
81 { "--passwordfile", GCTLCMD_COMMON_OPT_PASSWORD_FILE, RTGETOPT_REQ_STRING }, \
82 { "--password", GCTLCMD_COMMON_OPT_PASSWORD, RTGETOPT_REQ_STRING }, \
83 { "--domain", GCTLCMD_COMMON_OPT_DOMAIN, RTGETOPT_REQ_STRING }, \
84 { "--quiet", 'q', RTGETOPT_REQ_NOTHING }, \
85 { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
86
87/** Handles common options in the typical option parsing switch. */
88#define GCTLCMD_COMMON_OPTION_CASES(a_pCtx, a_ch, a_pValueUnion) \
89 case 'v': \
90 case 'q': \
91 case GCTLCMD_COMMON_OPT_USER: \
92 case GCTLCMD_COMMON_OPT_DOMAIN: \
93 case GCTLCMD_COMMON_OPT_PASSWORD: \
94 case GCTLCMD_COMMON_OPT_PASSWORD_FILE: \
95 { \
96 RTEXITCODE rcExitCommon = gctlCtxSetOption(a_pCtx, a_ch, a_pValueUnion); \
97 if (RT_UNLIKELY(rcExitCommon != RTEXITCODE_SUCCESS)) \
98 return rcExitCommon; \
99 } break
100
101
102/*********************************************************************************************************************************
103* Global Variables *
104*********************************************************************************************************************************/
105/** Set by the signal handler when current guest control
106 * action shall be aborted. */
107static volatile bool g_fGuestCtrlCanceled = false;
108/** Event semaphore used for wait notifications.
109 * Also being used for the listener implementations in VBoxManageGuestCtrlListener.cpp. */
110 RTSEMEVENT g_SemEventGuestCtrlCanceled = NIL_RTSEMEVENT;
111
112
113/*********************************************************************************************************************************
114* Structures and Typedefs *
115*********************************************************************************************************************************/
116/**
117 * Listener declarations.
118 */
119VBOX_LISTENER_DECLARE(GuestFileEventListenerImpl)
120VBOX_LISTENER_DECLARE(GuestProcessEventListenerImpl)
121VBOX_LISTENER_DECLARE(GuestSessionEventListenerImpl)
122VBOX_LISTENER_DECLARE(GuestEventListenerImpl)
123VBOX_LISTENER_DECLARE(GuestAdditionsRunlevelListener)
124
125/**
126 * Definition of a guestcontrol command, with handler and various flags.
127 */
128typedef struct GCTLCMDDEF
129{
130 /** The command name. */
131 const char *pszName;
132
133 /**
134 * Actual command handler callback.
135 *
136 * @param pCtx Pointer to command context to use.
137 */
138 DECLR3CALLBACKMEMBER(RTEXITCODE, pfnHandler, (struct GCTLCMDCTX *pCtx, int argc, char **argv));
139
140 /** The sub-command scope flags. */
141 uint64_t fSubcommandScope;
142 /** Command context flags (GCTLCMDCTX_F_XXX). */
143 uint32_t fCmdCtx;
144} GCTLCMD;
145/** Pointer to a const guest control command definition. */
146typedef GCTLCMDDEF const *PCGCTLCMDDEF;
147
148/** @name GCTLCMDCTX_F_XXX - Command context flags.
149 * @{
150 */
151/** No flags set. */
152#define GCTLCMDCTX_F_NONE 0
153/** Don't install a signal handler (CTRL+C trap). */
154#define GCTLCMDCTX_F_NO_SIGNAL_HANDLER RT_BIT(0)
155/** No guest session needed. */
156#define GCTLCMDCTX_F_SESSION_ANONYMOUS RT_BIT(1)
157/** @} */
158
159/**
160 * Context for handling a specific command.
161 */
162typedef struct GCTLCMDCTX
163{
164 HandlerArg *pArg;
165
166 /** Pointer to the command definition. */
167 PCGCTLCMDDEF pCmdDef;
168 /** The VM name or UUID. */
169 const char *pszVmNameOrUuid;
170
171 /** Whether we've done the post option parsing init already. */
172 bool fPostOptionParsingInited;
173 /** Whether we've locked the VM session. */
174 bool fLockedVmSession;
175 /** Whether to detach (@c true) or close the session. */
176 bool fDetachGuestSession;
177 /** Set if we've installed the signal handler. */
178 bool fInstalledSignalHandler;
179 /** The verbosity level. */
180 uint32_t cVerbose;
181 /** User name. */
182 Utf8Str strUsername;
183 /** Password. */
184 Utf8Str strPassword;
185 /** Domain. */
186 Utf8Str strDomain;
187 /** Pointer to the IGuest interface. */
188 ComPtr<IGuest> pGuest;
189 /** Pointer to the to be used guest session. */
190 ComPtr<IGuestSession> pGuestSession;
191 /** The guest session ID. */
192 ULONG uSessionID;
193
194} GCTLCMDCTX, *PGCTLCMDCTX;
195
196
197/**
198 * An entry for an element which needs to be copied/created to/on the guest.
199 */
200typedef struct DESTFILEENTRY
201{
202 DESTFILEENTRY(Utf8Str strFilename) : mFilename(strFilename) {}
203 Utf8Str mFilename;
204} DESTFILEENTRY, *PDESTFILEENTRY;
205/*
206 * Map for holding destination entries, whereas the key is the destination
207 * directory and the mapped value is a vector holding all elements for this directory.
208 */
209typedef std::map< Utf8Str, std::vector<DESTFILEENTRY> > DESTDIRMAP, *PDESTDIRMAP;
210typedef std::map< Utf8Str, std::vector<DESTFILEENTRY> >::iterator DESTDIRMAPITER, *PDESTDIRMAPITER;
211
212
213enum kStreamTransform
214{
215 kStreamTransform_None = 0,
216 kStreamTransform_Dos2Unix,
217 kStreamTransform_Unix2Dos
218};
219#endif /* VBOX_ONLY_DOCS */
220
221DECLARE_TRANSLATION_CONTEXT(GuestCtrl);
222
223void usageGuestControl(PRTSTREAM pStrm, const char *pcszSep1, const char *pcszSep2, uint64_t fSubcommandScope)
224{
225 const uint64_t fAnonSubCmds = HELP_SCOPE_GSTCTRL_CLOSESESSION
226 | HELP_SCOPE_GSTCTRL_LIST
227 | HELP_SCOPE_GSTCTRL_CLOSEPROCESS
228 | HELP_SCOPE_GSTCTRL_CLOSESESSION
229 | HELP_SCOPE_GSTCTRL_UPDATEGA
230 | HELP_SCOPE_GSTCTRL_WATCH
231 | HELP_SCOPE_GSTCTRL_WAITRUNLEVEL;
232
233 /* 0 1 2 3 4 5 6 7 8XXXXXXXXXX */
234 /* 0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 */
235 if (~fAnonSubCmds & fSubcommandScope)
236 RTStrmPrintf(pStrm,
237 "%s guestcontrol %s <uuid|vmname> [--verbose|-v] [--quiet|-q]\n"
238 " [--user[name] <name>] [--domain <domain>]\n"
239 " [--passwordfile <file> | --password <password>]\n%s",
240 pcszSep1, pcszSep2, (fSubcommandScope & RTMSGREFENTRYSTR_SCOPE_MASK) == RTMSGREFENTRYSTR_SCOPE_GLOBAL ? "\n" : "");
241 if (fSubcommandScope & HELP_SCOPE_GSTCTRL_RUN)
242 RTStrmPrintf(pStrm,
243 " run [common-options]\n"
244 " [--exe <path to executable>] [--timeout <msec>]\n"
245 " [-E|--putenv <NAME>[=<VALUE>]] [--unquoted-args]\n"
246 " [--ignore-operhaned-processes] [--profile]\n"
247 " [--no-wait-stdout|--wait-stdout]\n"
248 " [--no-wait-stderr|--wait-stderr]\n"
249 " [--dos2unix] [--unix2dos]\n"
250 " -- <program/arg0> [argument1] ... [argumentN]]\n"
251 "\n");
252 if (fSubcommandScope & HELP_SCOPE_GSTCTRL_START)
253 RTStrmPrintf(pStrm,
254 " start [common-options]\n"
255 " [--exe <path to executable>] [--timeout <msec>]\n"
256 " [-E|--putenv <NAME>[=<VALUE>]] [--unquoted-args]\n"
257 " [--ignore-operhaned-processes] [--profile]\n"
258 " -- <program/arg0> [argument1] ... [argumentN]]\n"
259 "\n");
260 if (fSubcommandScope & HELP_SCOPE_GSTCTRL_COPYFROM)
261 RTStrmPrintf(pStrm,
262 " copyfrom [common-options]\n"
263 " [--follow] [-R|--recursive]\n"
264 " <guest-src0> [guest-src1 [...]] <host-dst>\n"
265 "\n"
266 " copyfrom [common-options]\n"
267 " [--follow] [-R|--recursive]\n"
268 " [--target-directory <host-dst-dir>]\n"
269 " <guest-src0> [guest-src1 [...]]\n"
270 "\n");
271 if (fSubcommandScope & HELP_SCOPE_GSTCTRL_COPYTO)
272 RTStrmPrintf(pStrm,
273 " copyto [common-options]\n"
274 " [--follow] [-R|--recursive]\n"
275 " <host-src0> [host-src1 [...]] <guest-dst>\n"
276 "\n"
277 " copyto [common-options]\n"
278 " [--follow] [-R|--recursive]\n"
279 " [--target-directory <guest-dst>]\n"
280 " <host-src0> [host-src1 [...]]\n"
281 "\n");
282 if (fSubcommandScope & HELP_SCOPE_GSTCTRL_MKDIR)
283 RTStrmPrintf(pStrm,
284 " mkdir|createdir[ectory] [common-options]\n"
285 " [--parents] [--mode <mode>]\n"
286 " <guest directory> [...]\n"
287 "\n");
288 if (fSubcommandScope & HELP_SCOPE_GSTCTRL_RMDIR)
289 RTStrmPrintf(pStrm,
290 " rmdir|removedir[ectory] [common-options]\n"
291 " [-R|--recursive]\n"
292 " <guest directory> [...]\n"
293 "\n");
294 if (fSubcommandScope & HELP_SCOPE_GSTCTRL_RM)
295 RTStrmPrintf(pStrm,
296 " removefile|rm [common-options] [-f|--force]\n"
297 " <guest file> [...]\n"
298 "\n");
299 if (fSubcommandScope & HELP_SCOPE_GSTCTRL_MV)
300 RTStrmPrintf(pStrm,
301 " mv|move|ren[ame] [common-options]\n"
302 " <source> [source1 [...]] <dest>\n"
303 "\n");
304 if (fSubcommandScope & HELP_SCOPE_GSTCTRL_MKTEMP)
305 RTStrmPrintf(pStrm,
306 " mktemp|createtemp[orary] [common-options]\n"
307 " [--secure] [--mode <mode>] [--tmpdir <directory>]\n"
308 " <template>\n"
309 "\n");
310 if (fSubcommandScope & HELP_SCOPE_GSTCTRL_STAT)
311 RTStrmPrintf(pStrm,
312 " stat [common-options]\n"
313 " <file> [...]\n"
314 "\n");
315
316 /*
317 * Command not requiring authentication.
318 */
319 if (fAnonSubCmds & fSubcommandScope)
320 {
321 /* 0 1 2 3 4 5 6 7 8XXXXXXXXXX */
322 /* 0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 */
323 RTStrmPrintf(pStrm,
324 "%s guestcontrol %s <uuid|vmname> [--verbose|-v] [--quiet|-q]\n%s",
325 pcszSep1, pcszSep2, (fSubcommandScope & RTMSGREFENTRYSTR_SCOPE_MASK) == RTMSGREFENTRYSTR_SCOPE_GLOBAL ? "\n" : "");
326 if (fSubcommandScope & HELP_SCOPE_GSTCTRL_LIST)
327 RTStrmPrintf(pStrm,
328 " list <all|sessions|processes|files> [common-opts]\n"
329 "\n");
330 if (fSubcommandScope & HELP_SCOPE_GSTCTRL_CLOSEPROCESS)
331 RTStrmPrintf(pStrm,
332 " closeprocess [common-options]\n"
333 " < --session-id <ID>\n"
334 " | --session-name <name or pattern>\n"
335 " <PID1> [PID1 [...]]\n"
336 "\n");
337 if (fSubcommandScope & HELP_SCOPE_GSTCTRL_CLOSESESSION)
338 RTStrmPrintf(pStrm,
339 " closesession [common-options]\n"
340 " < --all | --session-id <ID>\n"
341 " | --session-name <name or pattern> >\n"
342 "\n");
343 if (fSubcommandScope & HELP_SCOPE_GSTCTRL_UPDATEGA)
344 RTStrmPrintf(pStrm,
345 " updatega|updateguestadditions|updateadditions\n"
346 " [--source <guest additions .ISO>]\n"
347 " [--wait-start] [common-options]\n"
348 " [-- [<argument1>] ... [<argumentN>]]\n"
349 "\n");
350 if (fSubcommandScope & HELP_SCOPE_GSTCTRL_WATCH)
351 RTStrmPrintf(pStrm,
352 " watch [--timeout <msec>] [common-options]\n"
353 "\n");
354 if (fSubcommandScope & HELP_SCOPE_GSTCTRL_WAITRUNLEVEL)
355 RTStrmPrintf(pStrm,
356 " waitrunlevel [--timeout <msec>] [common-options]\n"
357 " <system|userland|desktop>\n"
358 "\n");
359 }
360}
361
362#ifndef VBOX_ONLY_DOCS
363
364
365#ifdef RT_OS_WINDOWS
366static BOOL WINAPI gctlSignalHandler(DWORD dwCtrlType) RT_NOTHROW_DEF
367{
368 bool fEventHandled = FALSE;
369 switch (dwCtrlType)
370 {
371 /* User pressed CTRL+C or CTRL+BREAK or an external event was sent
372 * via GenerateConsoleCtrlEvent(). */
373 case CTRL_BREAK_EVENT:
374 case CTRL_CLOSE_EVENT:
375 case CTRL_C_EVENT:
376 ASMAtomicWriteBool(&g_fGuestCtrlCanceled, true);
377 fEventHandled = TRUE;
378 break;
379 default:
380 break;
381 /** @todo Add other events here. */
382 }
383
384 return fEventHandled;
385}
386#else /* !RT_OS_WINDOWS */
387/**
388 * Signal handler that sets g_fGuestCtrlCanceled.
389 *
390 * This can be executed on any thread in the process, on Windows it may even be
391 * a thread dedicated to delivering this signal. Don't do anything
392 * unnecessary here.
393 */
394static void gctlSignalHandler(int iSignal) RT_NOTHROW_DEF
395{
396 NOREF(iSignal);
397 ASMAtomicWriteBool(&g_fGuestCtrlCanceled, true);
398}
399#endif
400
401
402/**
403 * Installs a custom signal handler to get notified
404 * whenever the user wants to intercept the program.
405 *
406 * @todo Make this handler available for all VBoxManage modules?
407 */
408static int gctlSignalHandlerInstall(void)
409{
410 g_fGuestCtrlCanceled = false;
411
412 int rc = VINF_SUCCESS;
413#ifdef RT_OS_WINDOWS
414 if (!SetConsoleCtrlHandler((PHANDLER_ROUTINE)gctlSignalHandler, TRUE /* Add handler */))
415 {
416 rc = RTErrConvertFromWin32(GetLastError());
417 RTMsgError(GuestCtrl::tr("Unable to install console control handler, rc=%Rrc\n"), rc);
418 }
419#else
420 signal(SIGINT, gctlSignalHandler);
421 signal(SIGTERM, gctlSignalHandler);
422# ifdef SIGBREAK
423 signal(SIGBREAK, gctlSignalHandler);
424# endif
425#endif
426
427 if (RT_SUCCESS(rc))
428 rc = RTSemEventCreate(&g_SemEventGuestCtrlCanceled);
429
430 return rc;
431}
432
433
434/**
435 * Uninstalls a previously installed signal handler.
436 */
437static int gctlSignalHandlerUninstall(void)
438{
439 int rc = VINF_SUCCESS;
440#ifdef RT_OS_WINDOWS
441 if (!SetConsoleCtrlHandler((PHANDLER_ROUTINE)NULL, FALSE /* Remove handler */))
442 {
443 rc = RTErrConvertFromWin32(GetLastError());
444 RTMsgError(GuestCtrl::tr("Unable to uninstall console control handler, rc=%Rrc\n"), rc);
445 }
446#else
447 signal(SIGINT, SIG_DFL);
448 signal(SIGTERM, SIG_DFL);
449# ifdef SIGBREAK
450 signal(SIGBREAK, SIG_DFL);
451# endif
452#endif
453
454 if (g_SemEventGuestCtrlCanceled != NIL_RTSEMEVENT)
455 {
456 RTSemEventDestroy(g_SemEventGuestCtrlCanceled);
457 g_SemEventGuestCtrlCanceled = NIL_RTSEMEVENT;
458 }
459 return rc;
460}
461
462
463/**
464 * Translates a process status to a human readable string.
465 */
466const char *gctlProcessStatusToText(ProcessStatus_T enmStatus)
467{
468 switch (enmStatus)
469 {
470 case ProcessStatus_Starting:
471 return GuestCtrl::tr("starting");
472 case ProcessStatus_Started:
473 return GuestCtrl::tr("started");
474 case ProcessStatus_Paused:
475 return GuestCtrl::tr("paused");
476 case ProcessStatus_Terminating:
477 return GuestCtrl::tr("terminating");
478 case ProcessStatus_TerminatedNormally:
479 return GuestCtrl::tr("successfully terminated");
480 case ProcessStatus_TerminatedSignal:
481 return GuestCtrl::tr("terminated by signal");
482 case ProcessStatus_TerminatedAbnormally:
483 return GuestCtrl::tr("abnormally aborted");
484 case ProcessStatus_TimedOutKilled:
485 return GuestCtrl::tr("timed out");
486 case ProcessStatus_TimedOutAbnormally:
487 return GuestCtrl::tr("timed out, hanging");
488 case ProcessStatus_Down:
489 return GuestCtrl::tr("killed");
490 case ProcessStatus_Error:
491 return GuestCtrl::tr("error");
492 default:
493 break;
494 }
495 return GuestCtrl::tr("unknown");
496}
497
498/**
499 * Translates a guest process wait result to a human readable string.
500 */
501const char *gctlProcessWaitResultToText(ProcessWaitResult_T enmWaitResult)
502{
503 switch (enmWaitResult)
504 {
505 case ProcessWaitResult_Start:
506 return GuestCtrl::tr("started");
507 case ProcessWaitResult_Terminate:
508 return GuestCtrl::tr("terminated");
509 case ProcessWaitResult_Status:
510 return GuestCtrl::tr("status changed");
511 case ProcessWaitResult_Error:
512 return GuestCtrl::tr("error");
513 case ProcessWaitResult_Timeout:
514 return GuestCtrl::tr("timed out");
515 case ProcessWaitResult_StdIn:
516 return GuestCtrl::tr("stdin ready");
517 case ProcessWaitResult_StdOut:
518 return GuestCtrl::tr("data on stdout");
519 case ProcessWaitResult_StdErr:
520 return GuestCtrl::tr("data on stderr");
521 case ProcessWaitResult_WaitFlagNotSupported:
522 return GuestCtrl::tr("waiting flag not supported");
523 default:
524 break;
525 }
526 return GuestCtrl::tr("unknown");
527}
528
529/**
530 * Translates a guest session status to a human readable string.
531 */
532const char *gctlGuestSessionStatusToText(GuestSessionStatus_T enmStatus)
533{
534 switch (enmStatus)
535 {
536 case GuestSessionStatus_Starting:
537 return GuestCtrl::tr("starting");
538 case GuestSessionStatus_Started:
539 return GuestCtrl::tr("started");
540 case GuestSessionStatus_Terminating:
541 return GuestCtrl::tr("terminating");
542 case GuestSessionStatus_Terminated:
543 return GuestCtrl::tr("terminated");
544 case GuestSessionStatus_TimedOutKilled:
545 return GuestCtrl::tr("timed out");
546 case GuestSessionStatus_TimedOutAbnormally:
547 return GuestCtrl::tr("timed out, hanging");
548 case GuestSessionStatus_Down:
549 return GuestCtrl::tr("killed");
550 case GuestSessionStatus_Error:
551 return GuestCtrl::tr("error");
552 default:
553 break;
554 }
555 return GuestCtrl::tr("unknown");
556}
557
558/**
559 * Translates a guest file status to a human readable string.
560 */
561const char *gctlFileStatusToText(FileStatus_T enmStatus)
562{
563 switch (enmStatus)
564 {
565 case FileStatus_Opening:
566 return GuestCtrl::tr("opening");
567 case FileStatus_Open:
568 return GuestCtrl::tr("open");
569 case FileStatus_Closing:
570 return GuestCtrl::tr("closing");
571 case FileStatus_Closed:
572 return GuestCtrl::tr("closed");
573 case FileStatus_Down:
574 return GuestCtrl::tr("killed");
575 case FileStatus_Error:
576 return GuestCtrl::tr("error");
577 default:
578 break;
579 }
580 return GuestCtrl::tr("unknown");
581}
582
583/**
584 * Translates a file system objec type to a string.
585 */
586const char *gctlFsObjTypeToName(FsObjType_T enmType)
587{
588 switch (enmType)
589 {
590 case FsObjType_Unknown: return GuestCtrl::tr("unknown");
591 case FsObjType_Fifo: return GuestCtrl::tr("fifo");
592 case FsObjType_DevChar: return GuestCtrl::tr("char-device");
593 case FsObjType_Directory: return GuestCtrl::tr("directory");
594 case FsObjType_DevBlock: return GuestCtrl::tr("block-device");
595 case FsObjType_File: return GuestCtrl::tr("file");
596 case FsObjType_Symlink: return GuestCtrl::tr("symlink");
597 case FsObjType_Socket: return GuestCtrl::tr("socket");
598 case FsObjType_WhiteOut: return GuestCtrl::tr("white-out");
599#ifdef VBOX_WITH_XPCOM_CPP_ENUM_HACK
600 case FsObjType_32BitHack: break;
601#endif
602 }
603 return GuestCtrl::tr("unknown");
604}
605
606static int gctlPrintError(com::ErrorInfo &errorInfo)
607{
608 if ( errorInfo.isFullAvailable()
609 || errorInfo.isBasicAvailable())
610 {
611 /* If we got a VBOX_E_IPRT error we handle the error in a more gentle way
612 * because it contains more accurate info about what went wrong. */
613 if (errorInfo.getResultCode() == VBOX_E_IPRT_ERROR)
614 RTMsgError("%ls.", errorInfo.getText().raw());
615 else
616 {
617 RTMsgError(GuestCtrl::tr("Error details:"));
618 GluePrintErrorInfo(errorInfo);
619 }
620 return VERR_GENERAL_FAILURE; /** @todo */
621 }
622 AssertMsgFailedReturn((GuestCtrl::tr("Object has indicated no error (%Rhrc)!?\n"), errorInfo.getResultCode()),
623 VERR_INVALID_PARAMETER);
624}
625
626static int gctlPrintError(IUnknown *pObj, const GUID &aIID)
627{
628 com::ErrorInfo ErrInfo(pObj, aIID);
629 return gctlPrintError(ErrInfo);
630}
631
632static int gctlPrintProgressError(ComPtr<IProgress> pProgress)
633{
634 int vrc = VINF_SUCCESS;
635 HRESULT rc;
636
637 do
638 {
639 BOOL fCanceled;
640 CHECK_ERROR_BREAK(pProgress, COMGETTER(Canceled)(&fCanceled));
641 if (!fCanceled)
642 {
643 LONG rcProc;
644 CHECK_ERROR_BREAK(pProgress, COMGETTER(ResultCode)(&rcProc));
645 if (FAILED(rcProc))
646 {
647 com::ProgressErrorInfo ErrInfo(pProgress);
648 vrc = gctlPrintError(ErrInfo);
649 }
650 }
651
652 } while(0);
653
654 AssertMsgStmt(SUCCEEDED(rc), (GuestCtrl::tr("Could not lookup progress information\n")), vrc = VERR_COM_UNEXPECTED);
655
656 return vrc;
657}
658
659
660
661/*
662 *
663 *
664 * Guest Control Command Context
665 * Guest Control Command Context
666 * Guest Control Command Context
667 * Guest Control Command Context
668 *
669 *
670 *
671 */
672
673
674/**
675 * Initializes a guest control command context structure.
676 *
677 * @returns RTEXITCODE_SUCCESS on success, RTEXITCODE_FAILURE on failure (after
678 * informing the user of course).
679 * @param pCtx The command context to init.
680 * @param pArg The handle argument package.
681 */
682static RTEXITCODE gctrCmdCtxInit(PGCTLCMDCTX pCtx, HandlerArg *pArg)
683{
684 pCtx->pArg = pArg;
685 pCtx->pCmdDef = NULL;
686 pCtx->pszVmNameOrUuid = NULL;
687 pCtx->fPostOptionParsingInited = false;
688 pCtx->fLockedVmSession = false;
689 pCtx->fDetachGuestSession = false;
690 pCtx->fInstalledSignalHandler = false;
691 pCtx->cVerbose = 0;
692 pCtx->strUsername.setNull();
693 pCtx->strPassword.setNull();
694 pCtx->strDomain.setNull();
695 pCtx->pGuest.setNull();
696 pCtx->pGuestSession.setNull();
697 pCtx->uSessionID = 0;
698
699 /*
700 * The user name defaults to the host one, if we can get at it.
701 */
702 char szUser[1024];
703 int rc = RTProcQueryUsername(RTProcSelf(), szUser, sizeof(szUser), NULL);
704 if ( RT_SUCCESS(rc)
705 && RTStrIsValidEncoding(szUser)) /* paranoia was required on posix at some point, not needed any more! */
706 {
707 try
708 {
709 pCtx->strUsername = szUser;
710 }
711 catch (std::bad_alloc &)
712 {
713 return RTMsgErrorExit(RTEXITCODE_FAILURE, GuestCtrl::tr("Out of memory"));
714 }
715 }
716 /* else: ignore this failure. */
717
718 return RTEXITCODE_SUCCESS;
719}
720
721
722/**
723 * Worker for GCTLCMD_COMMON_OPTION_CASES.
724 *
725 * @returns RTEXITCODE_SUCCESS if the option was handled successfully. If not,
726 * an error message is printed and an appropriate failure exit code is
727 * returned.
728 * @param pCtx The guest control command context.
729 * @param ch The option char or ordinal.
730 * @param pValueUnion The option value union.
731 */
732static RTEXITCODE gctlCtxSetOption(PGCTLCMDCTX pCtx, int ch, PRTGETOPTUNION pValueUnion)
733{
734 RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
735 switch (ch)
736 {
737 case GCTLCMD_COMMON_OPT_USER: /* User name */
738 if (!pCtx->pCmdDef || !(pCtx->pCmdDef->fCmdCtx & GCTLCMDCTX_F_SESSION_ANONYMOUS))
739 pCtx->strUsername = pValueUnion->psz;
740 else
741 RTMsgWarning(GuestCtrl::tr("The --username|-u option is ignored by '%s'"), pCtx->pCmdDef->pszName);
742 break;
743
744 case GCTLCMD_COMMON_OPT_PASSWORD: /* Password */
745 if (!pCtx->pCmdDef || !(pCtx->pCmdDef->fCmdCtx & GCTLCMDCTX_F_SESSION_ANONYMOUS))
746 {
747 if (pCtx->strPassword.isNotEmpty())
748 RTMsgWarning(GuestCtrl::tr("Password is given more than once."));
749 pCtx->strPassword = pValueUnion->psz;
750 }
751 else
752 RTMsgWarning(GuestCtrl::tr("The --password option is ignored by '%s'"), pCtx->pCmdDef->pszName);
753 break;
754
755 case GCTLCMD_COMMON_OPT_PASSWORD_FILE: /* Password file */
756 if (!pCtx->pCmdDef || !(pCtx->pCmdDef->fCmdCtx & GCTLCMDCTX_F_SESSION_ANONYMOUS))
757 rcExit = readPasswordFile(pValueUnion->psz, &pCtx->strPassword);
758 else
759 RTMsgWarning(GuestCtrl::tr("The --password-file|-p option is ignored by '%s'"), pCtx->pCmdDef->pszName);
760 break;
761
762 case GCTLCMD_COMMON_OPT_DOMAIN: /* domain */
763 if (!pCtx->pCmdDef || !(pCtx->pCmdDef->fCmdCtx & GCTLCMDCTX_F_SESSION_ANONYMOUS))
764 pCtx->strDomain = pValueUnion->psz;
765 else
766 RTMsgWarning(GuestCtrl::tr("The --domain option is ignored by '%s'"), pCtx->pCmdDef->pszName);
767 break;
768
769 case 'v': /* --verbose */
770 pCtx->cVerbose++;
771 break;
772
773 case 'q': /* --quiet */
774 if (pCtx->cVerbose)
775 pCtx->cVerbose--;
776 break;
777
778 default:
779 AssertFatalMsgFailed(("ch=%d (%c)\n", ch, ch));
780 }
781 return rcExit;
782}
783
784
785/**
786 * Initializes the VM for IGuest operation.
787 *
788 * This opens a shared session to a running VM and gets hold of IGuest.
789 *
790 * @returns RTEXITCODE_SUCCESS on success. RTEXITCODE_FAILURE and user message
791 * on failure.
792 * @param pCtx The guest control command context.
793 * GCTLCMDCTX::pGuest will be set on success.
794 */
795static RTEXITCODE gctlCtxInitVmSession(PGCTLCMDCTX pCtx)
796{
797 HRESULT rc;
798 AssertPtr(pCtx);
799 AssertPtr(pCtx->pArg);
800
801 /*
802 * Find the VM and check if it's running.
803 */
804 ComPtr<IMachine> machine;
805 CHECK_ERROR(pCtx->pArg->virtualBox, FindMachine(Bstr(pCtx->pszVmNameOrUuid).raw(), machine.asOutParam()));
806 if (SUCCEEDED(rc))
807 {
808 MachineState_T enmMachineState;
809 CHECK_ERROR(machine, COMGETTER(State)(&enmMachineState));
810 if ( SUCCEEDED(rc)
811 && enmMachineState == MachineState_Running)
812 {
813 /*
814 * It's running. So, open a session to it and get the IGuest interface.
815 */
816 CHECK_ERROR(machine, LockMachine(pCtx->pArg->session, LockType_Shared));
817 if (SUCCEEDED(rc))
818 {
819 pCtx->fLockedVmSession = true;
820 ComPtr<IConsole> ptrConsole;
821 CHECK_ERROR(pCtx->pArg->session, COMGETTER(Console)(ptrConsole.asOutParam()));
822 if (SUCCEEDED(rc))
823 {
824 if (ptrConsole.isNotNull())
825 {
826 CHECK_ERROR(ptrConsole, COMGETTER(Guest)(pCtx->pGuest.asOutParam()));
827 if (SUCCEEDED(rc))
828 return RTEXITCODE_SUCCESS;
829 }
830 else
831 RTMsgError(GuestCtrl::tr("Failed to get a IConsole pointer for the machine. Is it still running?\n"));
832 }
833 }
834 }
835 else if (SUCCEEDED(rc))
836 RTMsgError(GuestCtrl::tr("Machine \"%s\" is not running (currently %s)!\n"),
837 pCtx->pszVmNameOrUuid, machineStateToName(enmMachineState, false));
838 }
839 return RTEXITCODE_FAILURE;
840}
841
842
843/**
844 * Creates a guest session with the VM.
845 *
846 * @retval RTEXITCODE_SUCCESS on success.
847 * @retval RTEXITCODE_FAILURE and user message on failure.
848 * @param pCtx The guest control command context.
849 * GCTCMDCTX::pGuestSession and GCTLCMDCTX::uSessionID
850 * will be set.
851 */
852static RTEXITCODE gctlCtxInitGuestSession(PGCTLCMDCTX pCtx)
853{
854 HRESULT rc;
855 AssertPtr(pCtx);
856 Assert(!(pCtx->pCmdDef->fCmdCtx & GCTLCMDCTX_F_SESSION_ANONYMOUS));
857 Assert(pCtx->pGuest.isNotNull());
858
859 /*
860 * Build up a reasonable guest session name. Useful for identifying
861 * a specific session when listing / searching for them.
862 */
863 char *pszSessionName;
864 if (RTStrAPrintf(&pszSessionName,
865 GuestCtrl::tr("[%RU32] VBoxManage Guest Control [%s] - %s"),
866 RTProcSelf(), pCtx->pszVmNameOrUuid, pCtx->pCmdDef->pszName) < 0)
867 return RTMsgErrorExit(RTEXITCODE_FAILURE, GuestCtrl::tr("No enough memory for session name"));
868
869 /*
870 * Create a guest session.
871 */
872 if (pCtx->cVerbose)
873 RTPrintf(GuestCtrl::tr("Creating guest session as user '%s'...\n"), pCtx->strUsername.c_str());
874 try
875 {
876 CHECK_ERROR(pCtx->pGuest, CreateSession(Bstr(pCtx->strUsername).raw(),
877 Bstr(pCtx->strPassword).raw(),
878 Bstr(pCtx->strDomain).raw(),
879 Bstr(pszSessionName).raw(),
880 pCtx->pGuestSession.asOutParam()));
881 }
882 catch (std::bad_alloc &)
883 {
884 RTMsgError(GuestCtrl::tr("Out of memory setting up IGuest::CreateSession call"));
885 rc = E_OUTOFMEMORY;
886 }
887 if (SUCCEEDED(rc))
888 {
889 /*
890 * Wait for guest session to start.
891 */
892 if (pCtx->cVerbose)
893 RTPrintf(GuestCtrl::tr("Waiting for guest session to start...\n"));
894 GuestSessionWaitResult_T enmWaitResult = GuestSessionWaitResult_None; /* Shut up MSC */
895 try
896 {
897 com::SafeArray<GuestSessionWaitForFlag_T> aSessionWaitFlags;
898 aSessionWaitFlags.push_back(GuestSessionWaitForFlag_Start);
899 CHECK_ERROR(pCtx->pGuestSession, WaitForArray(ComSafeArrayAsInParam(aSessionWaitFlags),
900 /** @todo Make session handling timeouts configurable. */
901 30 * 1000, &enmWaitResult));
902 }
903 catch (std::bad_alloc &)
904 {
905 RTMsgError(GuestCtrl::tr("Out of memory setting up IGuestSession::WaitForArray call"));
906 rc = E_OUTOFMEMORY;
907 }
908 if (SUCCEEDED(rc))
909 {
910 /* The WaitFlagNotSupported result may happen with GAs older than 4.3. */
911 if ( enmWaitResult == GuestSessionWaitResult_Start
912 || enmWaitResult == GuestSessionWaitResult_WaitFlagNotSupported)
913 {
914 /*
915 * Get the session ID and we're ready to rumble.
916 */
917 CHECK_ERROR(pCtx->pGuestSession, COMGETTER(Id)(&pCtx->uSessionID));
918 if (SUCCEEDED(rc))
919 {
920 if (pCtx->cVerbose)
921 RTPrintf(GuestCtrl::tr("Successfully started guest session (ID %RU32)\n"), pCtx->uSessionID);
922 RTStrFree(pszSessionName);
923 return RTEXITCODE_SUCCESS;
924 }
925 }
926 else
927 {
928 GuestSessionStatus_T enmSessionStatus;
929 CHECK_ERROR(pCtx->pGuestSession, COMGETTER(Status)(&enmSessionStatus));
930 RTMsgError(GuestCtrl::tr("Error starting guest session (current status is: %s)\n"),
931 SUCCEEDED(rc) ? gctlGuestSessionStatusToText(enmSessionStatus) : GuestCtrl::tr("<unknown>"));
932 }
933 }
934 }
935
936 RTStrFree(pszSessionName);
937 return RTEXITCODE_FAILURE;
938}
939
940
941/**
942 * Completes the guest control context initialization after parsing arguments.
943 *
944 * Will validate common arguments, open a VM session, and if requested open a
945 * guest session and install the CTRL-C signal handler.
946 *
947 * It is good to validate all the options and arguments you can before making
948 * this call. However, the VM session, IGuest and IGuestSession interfaces are
949 * not availabe till after this call, so take care.
950 *
951 * @retval RTEXITCODE_SUCCESS on success.
952 * @retval RTEXITCODE_FAILURE and user message on failure.
953 * @param pCtx The guest control command context.
954 * GCTCMDCTX::pGuestSession and GCTLCMDCTX::uSessionID
955 * will be set.
956 * @remarks Can safely be called multiple times, will only do work once.
957 */
958static RTEXITCODE gctlCtxPostOptionParsingInit(PGCTLCMDCTX pCtx)
959{
960 if (pCtx->fPostOptionParsingInited)
961 return RTEXITCODE_SUCCESS;
962
963 /*
964 * Check that the user name isn't empty when we need it.
965 */
966 RTEXITCODE rcExit;
967 if ( (pCtx->pCmdDef->fCmdCtx & GCTLCMDCTX_F_SESSION_ANONYMOUS)
968 || pCtx->strUsername.isNotEmpty())
969 {
970 /*
971 * Open the VM session and if required, a guest session.
972 */
973 rcExit = gctlCtxInitVmSession(pCtx);
974 if ( rcExit == RTEXITCODE_SUCCESS
975 && !(pCtx->pCmdDef->fCmdCtx & GCTLCMDCTX_F_SESSION_ANONYMOUS))
976 rcExit = gctlCtxInitGuestSession(pCtx);
977 if (rcExit == RTEXITCODE_SUCCESS)
978 {
979 /*
980 * Install signal handler if requested (errors are ignored).
981 */
982 if (!(pCtx->pCmdDef->fCmdCtx & GCTLCMDCTX_F_NO_SIGNAL_HANDLER))
983 {
984 int rc = gctlSignalHandlerInstall();
985 pCtx->fInstalledSignalHandler = RT_SUCCESS(rc);
986 }
987 }
988 }
989 else
990 rcExit = errorSyntaxEx(USAGE_GUESTCONTROL, pCtx->pCmdDef->fSubcommandScope, GuestCtrl::tr("No user name specified!"));
991
992 pCtx->fPostOptionParsingInited = rcExit == RTEXITCODE_SUCCESS;
993 return rcExit;
994}
995
996
997/**
998 * Cleans up the context when the command returns.
999 *
1000 * This will close any open guest session, unless the DETACH flag is set.
1001 * It will also close any VM session that may be been established. Any signal
1002 * handlers we've installed will also be removed.
1003 *
1004 * Un-initializes the VM after guest control usage.
1005 * @param pCmdCtx Pointer to command context.
1006 */
1007static void gctlCtxTerm(PGCTLCMDCTX pCtx)
1008{
1009 HRESULT rc;
1010 AssertPtr(pCtx);
1011
1012 /*
1013 * Uninstall signal handler.
1014 */
1015 if (pCtx->fInstalledSignalHandler)
1016 {
1017 gctlSignalHandlerUninstall();
1018 pCtx->fInstalledSignalHandler = false;
1019 }
1020
1021 /*
1022 * Close, or at least release, the guest session.
1023 */
1024 if (pCtx->pGuestSession.isNotNull())
1025 {
1026 if ( !(pCtx->pCmdDef->fCmdCtx & GCTLCMDCTX_F_SESSION_ANONYMOUS)
1027 && !pCtx->fDetachGuestSession)
1028 {
1029 if (pCtx->cVerbose)
1030 RTPrintf(GuestCtrl::tr("Closing guest session ...\n"));
1031
1032 CHECK_ERROR(pCtx->pGuestSession, Close());
1033 }
1034 else if ( pCtx->fDetachGuestSession
1035 && pCtx->cVerbose)
1036 RTPrintf(GuestCtrl::tr("Guest session detached\n"));
1037
1038 pCtx->pGuestSession.setNull();
1039 }
1040
1041 /*
1042 * Close the VM session.
1043 */
1044 if (pCtx->fLockedVmSession)
1045 {
1046 Assert(pCtx->pArg->session.isNotNull());
1047 CHECK_ERROR(pCtx->pArg->session, UnlockMachine());
1048 pCtx->fLockedVmSession = false;
1049 }
1050}
1051
1052
1053
1054
1055
1056/*
1057 *
1058 *
1059 * Guest Control Command Handling.
1060 * Guest Control Command Handling.
1061 * Guest Control Command Handling.
1062 * Guest Control Command Handling.
1063 * Guest Control Command Handling.
1064 *
1065 *
1066 */
1067
1068
1069/** @name EXITCODEEXEC_XXX - Special run exit codes.
1070 *
1071 * Special exit codes for returning errors/information of a started guest
1072 * process to the command line VBoxManage was started from. Useful for e.g.
1073 * scripting.
1074 *
1075 * ASSUMING that all platforms have at least 7-bits for the exit code we can do
1076 * the following mapping:
1077 * - Guest exit code 0 is mapped to 0 on the host.
1078 * - Guest exit codes 1 thru 93 (0x5d) are displaced by 32, so that 1
1079 * becomes 33 (0x21) on the host and 93 becomes 125 (0x7d) on the host.
1080 * - Guest exit codes 94 (0x5e) and above are mapped to 126 (0x5e).
1081 *
1082 * We ASSUME that all VBoxManage status codes are in the range 0 thru 32.
1083 *
1084 * @note These are frozen as of 4.1.0.
1085 * @note The guest exit code mappings was introduced with 5.0 and the 'run'
1086 * command, they are/was not supported by 'exec'.
1087 * @sa gctlRunCalculateExitCode
1088 */
1089/** Process exited normally but with an exit code <> 0. */
1090#define EXITCODEEXEC_CODE ((RTEXITCODE)16)
1091#define EXITCODEEXEC_FAILED ((RTEXITCODE)17)
1092#define EXITCODEEXEC_TERM_SIGNAL ((RTEXITCODE)18)
1093#define EXITCODEEXEC_TERM_ABEND ((RTEXITCODE)19)
1094#define EXITCODEEXEC_TIMEOUT ((RTEXITCODE)20)
1095#define EXITCODEEXEC_DOWN ((RTEXITCODE)21)
1096/** Execution was interrupt by user (ctrl-c). */
1097#define EXITCODEEXEC_CANCELED ((RTEXITCODE)22)
1098/** The first mapped guest (non-zero) exit code. */
1099#define EXITCODEEXEC_MAPPED_FIRST 33
1100/** The last mapped guest (non-zero) exit code value (inclusive). */
1101#define EXITCODEEXEC_MAPPED_LAST 125
1102/** The number of exit codes from EXITCODEEXEC_MAPPED_FIRST to
1103 * EXITCODEEXEC_MAPPED_LAST. This is also the highest guest exit code number
1104 * we're able to map. */
1105#define EXITCODEEXEC_MAPPED_RANGE (93)
1106/** The guest exit code displacement value. */
1107#define EXITCODEEXEC_MAPPED_DISPLACEMENT 32
1108/** The guest exit code was too big to be mapped. */
1109#define EXITCODEEXEC_MAPPED_BIG ((RTEXITCODE)126)
1110/** @} */
1111
1112/**
1113 * Calculates the exit code of VBoxManage.
1114 *
1115 * @returns The exit code to return.
1116 * @param enmStatus The guest process status.
1117 * @param uExitCode The associated guest process exit code (where
1118 * applicable).
1119 * @param fReturnExitCodes Set if we're to use the 32-126 range for guest
1120 * exit codes.
1121 */
1122static RTEXITCODE gctlRunCalculateExitCode(ProcessStatus_T enmStatus, ULONG uExitCode, bool fReturnExitCodes)
1123{
1124 switch (enmStatus)
1125 {
1126 case ProcessStatus_TerminatedNormally:
1127 if (uExitCode == 0)
1128 return RTEXITCODE_SUCCESS;
1129 if (!fReturnExitCodes)
1130 return EXITCODEEXEC_CODE;
1131 if (uExitCode <= EXITCODEEXEC_MAPPED_RANGE)
1132 return (RTEXITCODE) (uExitCode + EXITCODEEXEC_MAPPED_DISPLACEMENT);
1133 return EXITCODEEXEC_MAPPED_BIG;
1134
1135 case ProcessStatus_TerminatedAbnormally:
1136 return EXITCODEEXEC_TERM_ABEND;
1137 case ProcessStatus_TerminatedSignal:
1138 return EXITCODEEXEC_TERM_SIGNAL;
1139
1140#if 0 /* see caller! */
1141 case ProcessStatus_TimedOutKilled:
1142 return EXITCODEEXEC_TIMEOUT;
1143 case ProcessStatus_Down:
1144 return EXITCODEEXEC_DOWN; /* Service/OS is stopping, process was killed. */
1145 case ProcessStatus_Error:
1146 return EXITCODEEXEC_FAILED;
1147
1148 /* The following is probably for detached? */
1149 case ProcessStatus_Starting:
1150 return RTEXITCODE_SUCCESS;
1151 case ProcessStatus_Started:
1152 return RTEXITCODE_SUCCESS;
1153 case ProcessStatus_Paused:
1154 return RTEXITCODE_SUCCESS;
1155 case ProcessStatus_Terminating:
1156 return RTEXITCODE_SUCCESS; /** @todo ???? */
1157#endif
1158
1159 default:
1160 AssertMsgFailed(("Unknown exit status (%u/%u) from guest process returned!\n", enmStatus, uExitCode));
1161 return RTEXITCODE_FAILURE;
1162 }
1163}
1164
1165
1166/**
1167 * Pumps guest output to the host.
1168 *
1169 * @return IPRT status code.
1170 * @param pProcess Pointer to appropriate process object.
1171 * @param hVfsIosDst Where to write the data. Can be the bit bucket or a (valid [std]) handle.
1172 * @param uHandle Handle where to read the data from.
1173 * @param cMsTimeout Timeout (in ms) to wait for the operation to
1174 * complete.
1175 */
1176static int gctlRunPumpOutput(IProcess *pProcess, RTVFSIOSTREAM hVfsIosDst, ULONG uHandle, RTMSINTERVAL cMsTimeout)
1177{
1178 AssertPtrReturn(pProcess, VERR_INVALID_POINTER);
1179 Assert(hVfsIosDst != NIL_RTVFSIOSTREAM);
1180
1181 int vrc;
1182
1183 SafeArray<BYTE> aOutputData;
1184 HRESULT hrc = pProcess->Read(uHandle, _64K, RT_MAX(cMsTimeout, 1), ComSafeArrayAsOutParam(aOutputData));
1185 if (SUCCEEDED(hrc))
1186 {
1187 size_t cbOutputData = aOutputData.size();
1188 if (cbOutputData == 0)
1189 vrc = VINF_SUCCESS;
1190 else
1191 {
1192 BYTE const *pbBuf = aOutputData.raw();
1193 AssertPtr(pbBuf);
1194
1195 vrc = RTVfsIoStrmWrite(hVfsIosDst, pbBuf, cbOutputData, true /*fBlocking*/, NULL);
1196 if (RT_FAILURE(vrc))
1197 RTMsgError(GuestCtrl::tr("Unable to write output, rc=%Rrc\n"), vrc);
1198 }
1199 }
1200 else
1201 vrc = gctlPrintError(pProcess, COM_IIDOF(IProcess));
1202 return vrc;
1203}
1204
1205
1206/**
1207 * Configures a host handle for pumping guest bits.
1208 *
1209 * @returns true if enabled and we successfully configured it.
1210 * @param fEnabled Whether pumping this pipe is configured to std handles,
1211 * or going to the bit bucket instead.
1212 * @param enmHandle The IPRT standard handle designation.
1213 * @param pszName The name for user messages.
1214 * @param enmTransformation The transformation to apply.
1215 * @param phVfsIos Where to return the resulting I/O stream handle.
1216 */
1217static bool gctlRunSetupHandle(bool fEnabled, RTHANDLESTD enmHandle, const char *pszName,
1218 kStreamTransform enmTransformation, PRTVFSIOSTREAM phVfsIos)
1219{
1220 if (fEnabled)
1221 {
1222 int vrc = RTVfsIoStrmFromStdHandle(enmHandle, 0, true /*fLeaveOpen*/, phVfsIos);
1223 if (RT_SUCCESS(vrc))
1224 {
1225 if (enmTransformation != kStreamTransform_None)
1226 {
1227 RTMsgWarning(GuestCtrl::tr("Unsupported %s line ending conversion"), pszName);
1228 /** @todo Implement dos2unix and unix2dos stream filters. */
1229 }
1230 return true;
1231 }
1232 RTMsgWarning(GuestCtrl::tr("Error getting %s handle: %Rrc"), pszName, vrc);
1233 }
1234 else /* If disabled, all goes to / gets fed to/from the bit bucket. */
1235 {
1236 RTFILE hFile;
1237 int vrc = RTFileOpenBitBucket(&hFile, enmHandle == RTHANDLESTD_INPUT ? RTFILE_O_READ : RTFILE_O_WRITE);
1238 if (RT_SUCCESS(vrc))
1239 {
1240 vrc = RTVfsIoStrmFromRTFile(hFile, 0 /* fOpen */, false /* fLeaveOpen */, phVfsIos);
1241 if (RT_SUCCESS(vrc))
1242 return true;
1243 }
1244 }
1245
1246 return false;
1247}
1248
1249
1250/**
1251 * Returns the remaining time (in ms) based on the start time and a set
1252 * timeout value. Returns RT_INDEFINITE_WAIT if no timeout was specified.
1253 *
1254 * @return RTMSINTERVAL Time left (in ms).
1255 * @param u64StartMs Start time (in ms).
1256 * @param cMsTimeout Timeout value (in ms).
1257 */
1258static RTMSINTERVAL gctlRunGetRemainingTime(uint64_t u64StartMs, RTMSINTERVAL cMsTimeout)
1259{
1260 if (!cMsTimeout || cMsTimeout == RT_INDEFINITE_WAIT) /* If no timeout specified, wait forever. */
1261 return RT_INDEFINITE_WAIT;
1262
1263 uint64_t u64ElapsedMs = RTTimeMilliTS() - u64StartMs;
1264 if (u64ElapsedMs >= cMsTimeout)
1265 return 0;
1266
1267 return cMsTimeout - (RTMSINTERVAL)u64ElapsedMs;
1268}
1269
1270/**
1271 * Common handler for the 'run' and 'start' commands.
1272 *
1273 * @returns Command exit code.
1274 * @param pCtx Guest session context.
1275 * @param argc The argument count.
1276 * @param argv The argument vector for this command.
1277 * @param fRunCmd Set if it's 'run' clear if 'start'.
1278 * @param fHelp The help flag for the command.
1279 */
1280static RTEXITCODE gctlHandleRunCommon(PGCTLCMDCTX pCtx, int argc, char **argv, bool fRunCmd, uint32_t fHelp)
1281{
1282 RT_NOREF(fHelp);
1283 AssertPtrReturn(pCtx, RTEXITCODE_FAILURE);
1284
1285 /*
1286 * Parse arguments.
1287 */
1288 enum kGstCtrlRunOpt
1289 {
1290 kGstCtrlRunOpt_IgnoreOrphanedProcesses = 1000,
1291 kGstCtrlRunOpt_NoProfile, /** @todo Deprecated and will be removed soon; use kGstCtrlRunOpt_Profile instead, if needed. */
1292 kGstCtrlRunOpt_Profile,
1293 kGstCtrlRunOpt_Dos2Unix,
1294 kGstCtrlRunOpt_Unix2Dos,
1295 kGstCtrlRunOpt_WaitForStdOut,
1296 kGstCtrlRunOpt_NoWaitForStdOut,
1297 kGstCtrlRunOpt_WaitForStdErr,
1298 kGstCtrlRunOpt_NoWaitForStdErr
1299 };
1300 static const RTGETOPTDEF s_aOptions[] =
1301 {
1302 GCTLCMD_COMMON_OPTION_DEFS()
1303 { "--putenv", 'E', RTGETOPT_REQ_STRING },
1304 { "--exe", 'e', RTGETOPT_REQ_STRING },
1305 { "--timeout", 't', RTGETOPT_REQ_UINT32 },
1306 { "--unquoted-args", 'u', RTGETOPT_REQ_NOTHING },
1307 { "--ignore-operhaned-processes", kGstCtrlRunOpt_IgnoreOrphanedProcesses, RTGETOPT_REQ_NOTHING },
1308 { "--no-profile", kGstCtrlRunOpt_NoProfile, RTGETOPT_REQ_NOTHING }, /** @todo Deprecated. */
1309 { "--profile", kGstCtrlRunOpt_Profile, RTGETOPT_REQ_NOTHING },
1310 /* run only: 6 - options */
1311 { "--dos2unix", kGstCtrlRunOpt_Dos2Unix, RTGETOPT_REQ_NOTHING },
1312 { "--unix2dos", kGstCtrlRunOpt_Unix2Dos, RTGETOPT_REQ_NOTHING },
1313 { "--no-wait-stdout", kGstCtrlRunOpt_NoWaitForStdOut, RTGETOPT_REQ_NOTHING },
1314 { "--wait-stdout", kGstCtrlRunOpt_WaitForStdOut, RTGETOPT_REQ_NOTHING },
1315 { "--no-wait-stderr", kGstCtrlRunOpt_NoWaitForStdErr, RTGETOPT_REQ_NOTHING },
1316 { "--wait-stderr", kGstCtrlRunOpt_WaitForStdErr, RTGETOPT_REQ_NOTHING },
1317 };
1318
1319 /** @todo stdin handling. */
1320
1321 int ch;
1322 RTGETOPTUNION ValueUnion;
1323 RTGETOPTSTATE GetState;
1324 int vrc = RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions) - (fRunCmd ? 0 : 6),
1325 1, RTGETOPTINIT_FLAGS_OPTS_FIRST);
1326 AssertRC(vrc);
1327
1328 com::SafeArray<ProcessCreateFlag_T> aCreateFlags;
1329 com::SafeArray<ProcessWaitForFlag_T> aWaitFlags;
1330 com::SafeArray<IN_BSTR> aArgs;
1331 com::SafeArray<IN_BSTR> aEnv;
1332 const char * pszImage = NULL;
1333 bool fWaitForStdOut = fRunCmd;
1334 bool fWaitForStdErr = fRunCmd;
1335 RTVFSIOSTREAM hVfsStdOut = NIL_RTVFSIOSTREAM;
1336 RTVFSIOSTREAM hVfsStdErr = NIL_RTVFSIOSTREAM;
1337 enum kStreamTransform enmStdOutTransform = kStreamTransform_None;
1338 enum kStreamTransform enmStdErrTransform = kStreamTransform_None;
1339 RTMSINTERVAL cMsTimeout = 0;
1340
1341 try
1342 {
1343 /* Wait for process start in any case. This is useful for scripting VBoxManage
1344 * when relying on its overall exit code. */
1345 aWaitFlags.push_back(ProcessWaitForFlag_Start);
1346
1347 while ((ch = RTGetOpt(&GetState, &ValueUnion)) != 0)
1348 {
1349 /* For options that require an argument, ValueUnion has received the value. */
1350 switch (ch)
1351 {
1352 GCTLCMD_COMMON_OPTION_CASES(pCtx, ch, &ValueUnion);
1353
1354 case 'E':
1355 if ( ValueUnion.psz[0] == '\0'
1356 || ValueUnion.psz[0] == '=')
1357 return errorSyntaxEx(USAGE_GUESTCONTROL, HELP_SCOPE_GSTCTRL_RUN,
1358 GuestCtrl::tr("Invalid argument variable[=value]: '%s'"), ValueUnion.psz);
1359 aEnv.push_back(Bstr(ValueUnion.psz).raw());
1360 break;
1361
1362 case kGstCtrlRunOpt_IgnoreOrphanedProcesses:
1363 aCreateFlags.push_back(ProcessCreateFlag_IgnoreOrphanedProcesses);
1364 break;
1365
1366 case kGstCtrlRunOpt_NoProfile:
1367 /** @todo Deprecated, will be removed. */
1368 RTPrintf(GuestCtrl::tr("Warning: Deprecated option \"--no-profile\" specified\n"));
1369 break;
1370
1371 case kGstCtrlRunOpt_Profile:
1372 aCreateFlags.push_back(ProcessCreateFlag_Profile);
1373 break;
1374
1375 case 'e':
1376 pszImage = ValueUnion.psz;
1377 break;
1378
1379 case 'u':
1380 aCreateFlags.push_back(ProcessCreateFlag_UnquotedArguments);
1381 break;
1382
1383 /** @todo Add a hidden flag. */
1384
1385 case 't': /* Timeout */
1386 cMsTimeout = ValueUnion.u32;
1387 break;
1388
1389 /* run only options: */
1390 case kGstCtrlRunOpt_Dos2Unix:
1391 Assert(fRunCmd);
1392 enmStdErrTransform = enmStdOutTransform = kStreamTransform_Dos2Unix;
1393 break;
1394 case kGstCtrlRunOpt_Unix2Dos:
1395 Assert(fRunCmd);
1396 enmStdErrTransform = enmStdOutTransform = kStreamTransform_Unix2Dos;
1397 break;
1398
1399 case kGstCtrlRunOpt_WaitForStdOut:
1400 Assert(fRunCmd);
1401 fWaitForStdOut = true;
1402 break;
1403 case kGstCtrlRunOpt_NoWaitForStdOut:
1404 Assert(fRunCmd);
1405 fWaitForStdOut = false;
1406 break;
1407
1408 case kGstCtrlRunOpt_WaitForStdErr:
1409 Assert(fRunCmd);
1410 fWaitForStdErr = true;
1411 break;
1412 case kGstCtrlRunOpt_NoWaitForStdErr:
1413 Assert(fRunCmd);
1414 fWaitForStdErr = false;
1415 break;
1416
1417 case VINF_GETOPT_NOT_OPTION:
1418 aArgs.push_back(Bstr(ValueUnion.psz).raw());
1419 if (!pszImage)
1420 {
1421 Assert(aArgs.size() == 1);
1422 pszImage = ValueUnion.psz;
1423 }
1424 break;
1425
1426 default:
1427 return errorGetOptEx(USAGE_GUESTCONTROL, HELP_SCOPE_GSTCTRL_RUN, ch, &ValueUnion);
1428
1429 } /* switch */
1430 } /* while RTGetOpt */
1431
1432 /* Must have something to execute. */
1433 if (!pszImage || !*pszImage)
1434 return errorSyntaxEx(USAGE_GUESTCONTROL, HELP_SCOPE_GSTCTRL_RUN, GuestCtrl::tr("No executable specified!"));
1435
1436 /*
1437 * Finalize process creation and wait flags and input/output streams.
1438 */
1439 if (!fRunCmd)
1440 {
1441 aCreateFlags.push_back(ProcessCreateFlag_WaitForProcessStartOnly);
1442 Assert(!fWaitForStdOut);
1443 Assert(!fWaitForStdErr);
1444 }
1445 else
1446 {
1447 aWaitFlags.push_back(ProcessWaitForFlag_Terminate);
1448 fWaitForStdOut = gctlRunSetupHandle(fWaitForStdOut, RTHANDLESTD_OUTPUT, "stdout", enmStdOutTransform, &hVfsStdOut);
1449 if (fWaitForStdOut)
1450 {
1451 aCreateFlags.push_back(ProcessCreateFlag_WaitForStdOut);
1452 aWaitFlags.push_back(ProcessWaitForFlag_StdOut);
1453 }
1454 fWaitForStdErr = gctlRunSetupHandle(fWaitForStdErr, RTHANDLESTD_ERROR, "stderr", enmStdErrTransform, &hVfsStdErr);
1455 if (fWaitForStdErr)
1456 {
1457 aCreateFlags.push_back(ProcessCreateFlag_WaitForStdErr);
1458 aWaitFlags.push_back(ProcessWaitForFlag_StdErr);
1459 }
1460 }
1461 }
1462 catch (std::bad_alloc &)
1463 {
1464 return RTMsgErrorExit(RTEXITCODE_FAILURE, "VERR_NO_MEMORY\n");
1465 }
1466
1467 RTEXITCODE rcExit = gctlCtxPostOptionParsingInit(pCtx);
1468 if (rcExit != RTEXITCODE_SUCCESS)
1469 return rcExit;
1470
1471 HRESULT rc;
1472
1473 try
1474 {
1475 do
1476 {
1477 /* Get current time stamp to later calculate rest of timeout left. */
1478 uint64_t msStart = RTTimeMilliTS();
1479
1480 /*
1481 * Create the process.
1482 */
1483 if (pCtx->cVerbose)
1484 {
1485 if (cMsTimeout == 0)
1486 RTPrintf(GuestCtrl::tr("Starting guest process ...\n"));
1487 else
1488 RTPrintf(GuestCtrl::tr("Starting guest process (within %ums)\n"), cMsTimeout);
1489 }
1490 ComPtr<IGuestProcess> pProcess;
1491 CHECK_ERROR_BREAK(pCtx->pGuestSession, ProcessCreate(Bstr(pszImage).raw(),
1492 ComSafeArrayAsInParam(aArgs),
1493 ComSafeArrayAsInParam(aEnv),
1494 ComSafeArrayAsInParam(aCreateFlags),
1495 gctlRunGetRemainingTime(msStart, cMsTimeout),
1496 pProcess.asOutParam()));
1497
1498 /*
1499 * Explicitly wait for the guest process to be in a started state.
1500 */
1501 com::SafeArray<ProcessWaitForFlag_T> aWaitStartFlags;
1502 aWaitStartFlags.push_back(ProcessWaitForFlag_Start);
1503 ProcessWaitResult_T waitResult;
1504 CHECK_ERROR_BREAK(pProcess, WaitForArray(ComSafeArrayAsInParam(aWaitStartFlags),
1505 gctlRunGetRemainingTime(msStart, cMsTimeout), &waitResult));
1506
1507 ULONG uPID = 0;
1508 CHECK_ERROR_BREAK(pProcess, COMGETTER(PID)(&uPID));
1509 if (fRunCmd && pCtx->cVerbose)
1510 RTPrintf(GuestCtrl::tr("Process '%s' (PID %RU32) started\n"), pszImage, uPID);
1511 else if (!fRunCmd && pCtx->cVerbose)
1512 {
1513 /* Just print plain PID to make it easier for scripts
1514 * invoking VBoxManage. */
1515 RTPrintf(GuestCtrl::tr("[%RU32 - Session %RU32]\n"), uPID, pCtx->uSessionID);
1516 }
1517
1518 /*
1519 * Wait for process to exit/start...
1520 */
1521 RTMSINTERVAL cMsTimeLeft = 1; /* Will be calculated. */
1522 bool fReadStdOut = false;
1523 bool fReadStdErr = false;
1524 bool fCompleted = false;
1525 bool fCompletedStartCmd = false;
1526
1527 vrc = VINF_SUCCESS;
1528 while ( !fCompleted
1529 && cMsTimeLeft > 0)
1530 {
1531 cMsTimeLeft = gctlRunGetRemainingTime(msStart, cMsTimeout);
1532 CHECK_ERROR_BREAK(pProcess, WaitForArray(ComSafeArrayAsInParam(aWaitFlags),
1533 RT_MIN(500 /*ms*/, RT_MAX(cMsTimeLeft, 1 /*ms*/)),
1534 &waitResult));
1535 if (pCtx->cVerbose)
1536 RTPrintf(GuestCtrl::tr("waitResult: %d\n"), waitResult);
1537 switch (waitResult)
1538 {
1539 case ProcessWaitResult_Start: /** @todo you always wait for 'start', */
1540 fCompletedStartCmd = fCompleted = !fRunCmd; /* Only wait for startup if the 'start' command. */
1541 if (!fCompleted && aWaitFlags[0] == ProcessWaitForFlag_Start)
1542 aWaitFlags[0] = ProcessWaitForFlag_Terminate;
1543 break;
1544 case ProcessWaitResult_StdOut:
1545 fReadStdOut = true;
1546 break;
1547 case ProcessWaitResult_StdErr:
1548 fReadStdErr = true;
1549 break;
1550 case ProcessWaitResult_Terminate:
1551 if (pCtx->cVerbose)
1552 RTPrintf(GuestCtrl::tr("Process terminated\n"));
1553 /* Process terminated, we're done. */
1554 fCompleted = true;
1555 break;
1556 case ProcessWaitResult_WaitFlagNotSupported:
1557 /* The guest does not support waiting for stdout/err, so
1558 * yield to reduce the CPU load due to busy waiting. */
1559 RTThreadYield();
1560 fReadStdOut = fReadStdErr = true;
1561 /* Note: In case the user specified explicitly not wanting to wait for stdout / stderr,
1562 * the configured VFS handle goes to / will be fed from the bit bucket. */
1563 break;
1564 case ProcessWaitResult_Timeout:
1565 {
1566 /** @todo It is really unclear whether we will get stuck with the timeout
1567 * result here if the guest side times out the process and fails to
1568 * kill the process... To be on the save side, double the IPC and
1569 * check the process status every time we time out. */
1570 ProcessStatus_T enmProcStatus;
1571 CHECK_ERROR_BREAK(pProcess, COMGETTER(Status)(&enmProcStatus));
1572 if ( enmProcStatus == ProcessStatus_TimedOutKilled
1573 || enmProcStatus == ProcessStatus_TimedOutAbnormally)
1574 fCompleted = true;
1575 fReadStdOut = fReadStdErr = true;
1576 break;
1577 }
1578 case ProcessWaitResult_Status:
1579 /* ignore. */
1580 break;
1581 case ProcessWaitResult_Error:
1582 /* waitFor is dead in the water, I think, so better leave the loop. */
1583 vrc = VERR_CALLBACK_RETURN;
1584 break;
1585
1586 case ProcessWaitResult_StdIn: AssertFailed(); /* did ask for this! */ break;
1587 case ProcessWaitResult_None: AssertFailed(); /* used. */ break;
1588 default: AssertFailed(); /* huh? */ break;
1589 }
1590
1591 if (g_fGuestCtrlCanceled)
1592 break;
1593
1594 /*
1595 * Pump output as needed.
1596 */
1597 if (fReadStdOut)
1598 {
1599 cMsTimeLeft = gctlRunGetRemainingTime(msStart, cMsTimeout);
1600 int vrc2 = gctlRunPumpOutput(pProcess, hVfsStdOut, 1 /* StdOut */, cMsTimeLeft);
1601 if (RT_FAILURE(vrc2) && RT_SUCCESS(vrc))
1602 vrc = vrc2;
1603 fReadStdOut = false;
1604 }
1605 if (fReadStdErr)
1606 {
1607 cMsTimeLeft = gctlRunGetRemainingTime(msStart, cMsTimeout);
1608 int vrc2 = gctlRunPumpOutput(pProcess, hVfsStdErr, 2 /* StdErr */, cMsTimeLeft);
1609 if (RT_FAILURE(vrc2) && RT_SUCCESS(vrc))
1610 vrc = vrc2;
1611 fReadStdErr = false;
1612 }
1613 if ( RT_FAILURE(vrc)
1614 || g_fGuestCtrlCanceled)
1615 break;
1616
1617 /*
1618 * Process events before looping.
1619 */
1620 NativeEventQueue::getMainEventQueue()->processEventQueue(0);
1621 } /* while */
1622
1623 /*
1624 * Report status back to the user.
1625 */
1626 if (g_fGuestCtrlCanceled)
1627 {
1628 if (pCtx->cVerbose)
1629 RTPrintf(GuestCtrl::tr("Process execution aborted!\n"));
1630 rcExit = EXITCODEEXEC_CANCELED;
1631 }
1632 else if (fCompletedStartCmd)
1633 {
1634 if (pCtx->cVerbose)
1635 RTPrintf(GuestCtrl::tr("Process successfully started!\n"));
1636 rcExit = RTEXITCODE_SUCCESS;
1637 }
1638 else if (fCompleted)
1639 {
1640 ProcessStatus_T procStatus;
1641 CHECK_ERROR_BREAK(pProcess, COMGETTER(Status)(&procStatus));
1642 if ( procStatus == ProcessStatus_TerminatedNormally
1643 || procStatus == ProcessStatus_TerminatedAbnormally
1644 || procStatus == ProcessStatus_TerminatedSignal)
1645 {
1646 LONG lExitCode;
1647 CHECK_ERROR_BREAK(pProcess, COMGETTER(ExitCode)(&lExitCode));
1648 if (pCtx->cVerbose)
1649 RTPrintf(GuestCtrl::tr("Exit code=%u (Status=%u [%s])\n"),
1650 lExitCode, procStatus, gctlProcessStatusToText(procStatus));
1651
1652 rcExit = gctlRunCalculateExitCode(procStatus, lExitCode, true /*fReturnExitCodes*/);
1653 }
1654 else if ( procStatus == ProcessStatus_TimedOutKilled
1655 || procStatus == ProcessStatus_TimedOutAbnormally)
1656 {
1657 if (pCtx->cVerbose)
1658 RTPrintf(GuestCtrl::tr("Process timed out (guest side) and %s\n"),
1659 procStatus == ProcessStatus_TimedOutAbnormally
1660 ? GuestCtrl::tr("failed to terminate so far") : GuestCtrl::tr("was terminated"));
1661 rcExit = EXITCODEEXEC_TIMEOUT;
1662 }
1663 else
1664 {
1665 if (pCtx->cVerbose)
1666 RTPrintf(GuestCtrl::tr("Process now is in status [%s] (unexpected)\n"),
1667 gctlProcessStatusToText(procStatus));
1668 rcExit = RTEXITCODE_FAILURE;
1669 }
1670 }
1671 else if (RT_FAILURE_NP(vrc))
1672 {
1673 if (pCtx->cVerbose)
1674 RTPrintf(GuestCtrl::tr("Process monitor loop quit with vrc=%Rrc\n"), vrc);
1675 rcExit = RTEXITCODE_FAILURE;
1676 }
1677 else
1678 {
1679 if (pCtx->cVerbose)
1680 RTPrintf(GuestCtrl::tr("Process monitor loop timed out\n"));
1681 rcExit = EXITCODEEXEC_TIMEOUT;
1682 }
1683
1684 } while (0);
1685 }
1686 catch (std::bad_alloc &)
1687 {
1688 rc = E_OUTOFMEMORY;
1689 }
1690
1691 /*
1692 * Decide what to do with the guest session.
1693 *
1694 * If it's the 'start' command where detach the guest process after
1695 * starting, don't close the guest session it is part of, except on
1696 * failure or ctrl-c.
1697 *
1698 * For the 'run' command the guest process quits with us.
1699 */
1700 if (!fRunCmd && SUCCEEDED(rc) && !g_fGuestCtrlCanceled)
1701 pCtx->fDetachGuestSession = true;
1702
1703 /* Make sure we return failure on failure. */
1704 if (FAILED(rc) && rcExit == RTEXITCODE_SUCCESS)
1705 rcExit = RTEXITCODE_FAILURE;
1706 return rcExit;
1707}
1708
1709
1710static DECLCALLBACK(RTEXITCODE) gctlHandleRun(PGCTLCMDCTX pCtx, int argc, char **argv)
1711{
1712 return gctlHandleRunCommon(pCtx, argc, argv, true /*fRunCmd*/, HELP_SCOPE_GSTCTRL_RUN);
1713}
1714
1715
1716static DECLCALLBACK(RTEXITCODE) gctlHandleStart(PGCTLCMDCTX pCtx, int argc, char **argv)
1717{
1718 return gctlHandleRunCommon(pCtx, argc, argv, false /*fRunCmd*/, HELP_SCOPE_GSTCTRL_START);
1719}
1720
1721
1722static RTEXITCODE gctlHandleCopy(PGCTLCMDCTX pCtx, int argc, char **argv, bool fHostToGuest)
1723{
1724 AssertPtrReturn(pCtx, RTEXITCODE_FAILURE);
1725
1726 /** @todo r=bird: This command isn't very unix friendly in general. mkdir
1727 * is much better (partly because it is much simpler of course). The main
1728 * arguments against this is that (1) all but two options conflicts with
1729 * what 'man cp' tells me on a GNU/Linux system, (2) wildchar matching is
1730 * done windows CMD style (though not in a 100% compatible way), and (3)
1731 * that only one source is allowed - efficiently sabotaging default
1732 * wildcard expansion by a unix shell. The best solution here would be
1733 * two different variant, one windowsy (xcopy) and one unixy (gnu cp). */
1734
1735 /*
1736 * IGuest::CopyToGuest is kept as simple as possible to let the developer choose
1737 * what and how to implement the file enumeration/recursive lookup, like VBoxManage
1738 * does in here.
1739 */
1740 enum GETOPTDEF_COPY
1741 {
1742 GETOPTDEF_COPY_FOLLOW = 1000,
1743 GETOPTDEF_COPY_TARGETDIR
1744 };
1745 static const RTGETOPTDEF s_aOptions[] =
1746 {
1747 GCTLCMD_COMMON_OPTION_DEFS()
1748 { "--follow", GETOPTDEF_COPY_FOLLOW, RTGETOPT_REQ_NOTHING },
1749 { "--recursive", 'R', RTGETOPT_REQ_NOTHING },
1750 { "--target-directory", GETOPTDEF_COPY_TARGETDIR, RTGETOPT_REQ_STRING }
1751 };
1752
1753 int ch;
1754 RTGETOPTUNION ValueUnion;
1755 RTGETOPTSTATE GetState;
1756 RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, RTGETOPTINIT_FLAGS_OPTS_FIRST);
1757
1758 bool fDstMustBeDir = false;
1759 const char *pszDst = NULL;
1760 bool fFollow = false;
1761 bool fRecursive = false;
1762 uint64_t uUsage = fHostToGuest ? HELP_SCOPE_GSTCTRL_COPYTO : HELP_SCOPE_GSTCTRL_COPYFROM;
1763
1764 int vrc = VINF_SUCCESS;
1765 while ( (ch = RTGetOpt(&GetState, &ValueUnion)) != 0
1766 && ch != VINF_GETOPT_NOT_OPTION)
1767 {
1768 /* For options that require an argument, ValueUnion has received the value. */
1769 switch (ch)
1770 {
1771 GCTLCMD_COMMON_OPTION_CASES(pCtx, ch, &ValueUnion);
1772
1773 case GETOPTDEF_COPY_FOLLOW:
1774 fFollow = true;
1775 break;
1776
1777 case 'R': /* Recursive processing */
1778 fRecursive = true;
1779 break;
1780
1781 case GETOPTDEF_COPY_TARGETDIR:
1782 pszDst = ValueUnion.psz;
1783 fDstMustBeDir = true;
1784 break;
1785
1786 default:
1787 return errorGetOptEx(USAGE_GUESTCONTROL, uUsage, ch, &ValueUnion);
1788 }
1789 }
1790
1791 char **papszSources = RTGetOptNonOptionArrayPtr(&GetState);
1792 size_t cSources = &argv[argc] - papszSources;
1793
1794 if (!cSources)
1795 return errorSyntaxEx(USAGE_GUESTCONTROL, uUsage, GuestCtrl::tr("No sources specified!"));
1796
1797 /* Unless a --target-directory is given, the last argument is the destination, so
1798 bump it from the source list. */
1799 if (pszDst == NULL && cSources >= 2)
1800 pszDst = papszSources[--cSources];
1801
1802 if (pszDst == NULL)
1803 return errorSyntaxEx(USAGE_GUESTCONTROL, uUsage, GuestCtrl::tr("No destination specified!"));
1804
1805 char szAbsDst[RTPATH_MAX];
1806 if (!fHostToGuest)
1807 {
1808 vrc = RTPathAbs(pszDst, szAbsDst, sizeof(szAbsDst));
1809 if (RT_SUCCESS(vrc))
1810 pszDst = szAbsDst;
1811 else
1812 return RTMsgErrorExitFailure(GuestCtrl::tr("RTPathAbs failed on '%s': %Rrc"), pszDst, vrc);
1813 }
1814
1815 RTEXITCODE rcExit = gctlCtxPostOptionParsingInit(pCtx);
1816 if (rcExit != RTEXITCODE_SUCCESS)
1817 return rcExit;
1818
1819 /*
1820 * Done parsing arguments, do some more preparations.
1821 */
1822 if (pCtx->cVerbose)
1823 {
1824 if (fHostToGuest)
1825 RTPrintf(GuestCtrl::tr("Copying from host to guest ...\n"));
1826 else
1827 RTPrintf(GuestCtrl::tr("Copying from guest to host ...\n"));
1828 }
1829
1830 HRESULT rc = S_OK;
1831 ComPtr<IProgress> pProgress;
1832/** @todo r=bird: This codes does nothing to handle the case where there are
1833 * multiple sources. You need to do serveral thing before thats handled
1834 * correctly. For starters the progress object handling needs to be moved
1835 * inside the loop. Next you need to check what the destination is, because you
1836 * can only copy multiple source files/directories to another directory. You
1837 * actually need to check whether the target exists and is a directory
1838 * regardless of you have 1 or 10 source files/dirs.
1839 *
1840 * Btw. the original approach to error handling here was APPALING. If some file
1841 * couldn't be stat'ed or if it was a file/directory, you only spat out messages
1842 * in verbose mode and never set the status code.
1843 *
1844 * The handling of the wildcard filtering expressions in sources was also just
1845 * skipped. I've corrected this, but you still need to make up your mind wrt
1846 * wildcards or not.
1847 *
1848 * Update: I've kicked out the whole SourceFileEntry/vecSources stuff as we can
1849 * use argv directly without any unnecessary copying. You just have to
1850 * look for the wildcards inside this loop instead.
1851 */
1852 NOREF(fDstMustBeDir);
1853 if (cSources != 1)
1854 return RTMsgErrorExitFailure(GuestCtrl::tr("Only one source file or directory at the moment."));
1855 for (size_t iSrc = 0; iSrc < cSources; iSrc++)
1856 {
1857 const char *pszSource = papszSources[iSrc];
1858
1859 /* Check if the source contains any wilecards in the last component, if so we
1860 don't know how to deal with it yet and refuse. */
1861 const char *pszSrcFinalComp = RTPathFilename(pszSource);
1862 if (pszSrcFinalComp && strpbrk(pszSrcFinalComp, "*?"))
1863 rcExit = RTMsgErrorExitFailure(GuestCtrl::tr("Skipping '%s' because wildcard expansion isn't implemented yet\n"),
1864 pszSource);
1865 else if (fHostToGuest)
1866 {
1867 /*
1868 * Source is host, destination guest.
1869 */
1870 char szAbsSrc[RTPATH_MAX];
1871 vrc = RTPathAbs(pszSource, szAbsSrc, sizeof(szAbsSrc));
1872 if (RT_SUCCESS(vrc))
1873 {
1874 RTFSOBJINFO ObjInfo;
1875 vrc = RTPathQueryInfo(szAbsSrc, &ObjInfo, RTFSOBJATTRADD_NOTHING);
1876 if (RT_SUCCESS(vrc))
1877 {
1878 if (RTFS_IS_FILE(ObjInfo.Attr.fMode))
1879 {
1880 if (pCtx->cVerbose)
1881 RTPrintf(GuestCtrl::tr("File '%s' -> '%s'\n"), szAbsSrc, pszDst);
1882
1883 SafeArray<FileCopyFlag_T> copyFlags;
1884 rc = pCtx->pGuestSession->FileCopyToGuest(Bstr(szAbsSrc).raw(), Bstr(pszDst).raw(),
1885 ComSafeArrayAsInParam(copyFlags), pProgress.asOutParam());
1886 }
1887 else if (RTFS_IS_DIRECTORY(ObjInfo.Attr.fMode))
1888 {
1889 if (pCtx->cVerbose)
1890 RTPrintf(GuestCtrl::tr("Directory '%s' -> '%s'\n"), szAbsSrc, pszDst);
1891
1892 SafeArray<DirectoryCopyFlag_T> copyFlags;
1893 copyFlags.push_back(DirectoryCopyFlag_CopyIntoExisting);
1894 rc = pCtx->pGuestSession->DirectoryCopyToGuest(Bstr(szAbsSrc).raw(), Bstr(pszDst).raw(),
1895 ComSafeArrayAsInParam(copyFlags), pProgress.asOutParam());
1896 }
1897 else
1898 rcExit = RTMsgErrorExitFailure(GuestCtrl::tr("Not a file or directory: %s\n"), szAbsSrc);
1899 }
1900 else
1901 rcExit = RTMsgErrorExitFailure(GuestCtrl::tr("RTPathQueryInfo failed on '%s': %Rrc"), szAbsSrc, vrc);
1902 }
1903 else
1904 rcExit = RTMsgErrorExitFailure(GuestCtrl::tr("RTPathAbs failed on '%s': %Rrc"), pszSource, vrc);
1905 }
1906 else
1907 {
1908 /*
1909 * Source guest, destination host.
1910 */
1911 /* We need to query the source type on the guest first in order to know which copy flavor we need. */
1912 ComPtr<IGuestFsObjInfo> pFsObjInfo;
1913 rc = pCtx->pGuestSession->FsObjQueryInfo(Bstr(pszSource).raw(), TRUE /* fFollowSymlinks */, pFsObjInfo.asOutParam());
1914 if (SUCCEEDED(rc))
1915 {
1916 FsObjType_T enmObjType;
1917 CHECK_ERROR(pFsObjInfo,COMGETTER(Type)(&enmObjType));
1918 if (SUCCEEDED(rc))
1919 {
1920 /* Take action according to source file. */
1921 if (enmObjType == FsObjType_Directory)
1922 {
1923 if (pCtx->cVerbose)
1924 RTPrintf(GuestCtrl::tr("Directory '%s' -> '%s'\n"), pszSource, pszDst);
1925
1926 SafeArray<DirectoryCopyFlag_T> aCopyFlags;
1927 aCopyFlags.push_back(DirectoryCopyFlag_CopyIntoExisting);
1928 rc = pCtx->pGuestSession->DirectoryCopyFromGuest(Bstr(pszSource).raw(), Bstr(pszDst).raw(),
1929 ComSafeArrayAsInParam(aCopyFlags), pProgress.asOutParam());
1930 }
1931 else if (enmObjType == FsObjType_File)
1932 {
1933 if (pCtx->cVerbose)
1934 RTPrintf(GuestCtrl::tr("File '%s' -> '%s'\n"), pszSource, pszDst);
1935
1936 SafeArray<FileCopyFlag_T> aCopyFlags;
1937 rc = pCtx->pGuestSession->FileCopyFromGuest(Bstr(pszSource).raw(), Bstr(pszDst).raw(),
1938 ComSafeArrayAsInParam(aCopyFlags), pProgress.asOutParam());
1939 }
1940 else
1941 rcExit = RTMsgErrorExitFailure(GuestCtrl::tr("Not a file or directory: %s\n"), pszSource);
1942 }
1943 else
1944 rcExit = RTEXITCODE_FAILURE;
1945 }
1946 else
1947 rcExit = RTMsgErrorExitFailure(GuestCtrl::tr("FsObjQueryInfo failed on '%s': %Rhrc"), pszSource, rc);
1948 }
1949 }
1950
1951 if (FAILED(rc))
1952 {
1953 vrc = gctlPrintError(pCtx->pGuestSession, COM_IIDOF(IGuestSession));
1954 }
1955 else if (pProgress.isNotNull())
1956 {
1957 if (pCtx->cVerbose)
1958 rc = showProgress(pProgress);
1959 else
1960 rc = pProgress->WaitForCompletion(-1 /* No timeout */);
1961 if (SUCCEEDED(rc))
1962 CHECK_PROGRESS_ERROR(pProgress, (GuestCtrl::tr("File copy failed")));
1963 vrc = gctlPrintProgressError(pProgress);
1964 }
1965 if (RT_FAILURE(vrc))
1966 rcExit = RTEXITCODE_FAILURE;
1967
1968 return rcExit;
1969}
1970
1971static DECLCALLBACK(RTEXITCODE) gctlHandleCopyFrom(PGCTLCMDCTX pCtx, int argc, char **argv)
1972{
1973 return gctlHandleCopy(pCtx, argc, argv, false /* Guest to host */);
1974}
1975
1976static DECLCALLBACK(RTEXITCODE) gctlHandleCopyTo(PGCTLCMDCTX pCtx, int argc, char **argv)
1977{
1978 return gctlHandleCopy(pCtx, argc, argv, true /* Host to guest */);
1979}
1980
1981static DECLCALLBACK(RTEXITCODE) gctrlHandleMkDir(PGCTLCMDCTX pCtx, int argc, char **argv)
1982{
1983 AssertPtrReturn(pCtx, RTEXITCODE_FAILURE);
1984
1985 static const RTGETOPTDEF s_aOptions[] =
1986 {
1987 GCTLCMD_COMMON_OPTION_DEFS()
1988 { "--mode", 'm', RTGETOPT_REQ_UINT32 },
1989 { "--parents", 'P', RTGETOPT_REQ_NOTHING }
1990 };
1991
1992 int ch;
1993 RTGETOPTUNION ValueUnion;
1994 RTGETOPTSTATE GetState;
1995 RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, RTGETOPTINIT_FLAGS_OPTS_FIRST);
1996
1997 SafeArray<DirectoryCreateFlag_T> aDirCreateFlags;
1998 uint32_t fDirMode = 0; /* Default mode. */
1999 uint32_t cDirsCreated = 0;
2000 RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
2001
2002 while ((ch = RTGetOpt(&GetState, &ValueUnion)) != 0)
2003 {
2004 /* For options that require an argument, ValueUnion has received the value. */
2005 switch (ch)
2006 {
2007 GCTLCMD_COMMON_OPTION_CASES(pCtx, ch, &ValueUnion);
2008
2009 case 'm': /* Mode */
2010 fDirMode = ValueUnion.u32;
2011 break;
2012
2013 case 'P': /* Create parents */
2014 aDirCreateFlags.push_back(DirectoryCreateFlag_Parents);
2015 break;
2016
2017 case VINF_GETOPT_NOT_OPTION:
2018 if (cDirsCreated == 0)
2019 {
2020 /*
2021 * First non-option - no more options now.
2022 */
2023 rcExit = gctlCtxPostOptionParsingInit(pCtx);
2024 if (rcExit != RTEXITCODE_SUCCESS)
2025 return rcExit;
2026 if (pCtx->cVerbose)
2027 RTPrintf(GuestCtrl::tr("Creating %RU32 directories...\n", "", argc - GetState.iNext + 1),
2028 argc - GetState.iNext + 1);
2029 }
2030 if (g_fGuestCtrlCanceled)
2031 return RTMsgErrorExit(RTEXITCODE_FAILURE, GuestCtrl::tr("mkdir was interrupted by Ctrl-C (%u left)\n"),
2032 argc - GetState.iNext + 1);
2033
2034 /*
2035 * Create the specified directory.
2036 *
2037 * On failure we'll change the exit status to failure and
2038 * continue with the next directory that needs creating. We do
2039 * this because we only create new things, and because this is
2040 * how /bin/mkdir works on unix.
2041 */
2042 cDirsCreated++;
2043 if (pCtx->cVerbose)
2044 RTPrintf(GuestCtrl::tr("Creating directory \"%s\" ...\n"), ValueUnion.psz);
2045 try
2046 {
2047 HRESULT rc;
2048 CHECK_ERROR(pCtx->pGuestSession, DirectoryCreate(Bstr(ValueUnion.psz).raw(),
2049 fDirMode, ComSafeArrayAsInParam(aDirCreateFlags)));
2050 if (FAILED(rc))
2051 rcExit = RTEXITCODE_FAILURE;
2052 }
2053 catch (std::bad_alloc &)
2054 {
2055 return RTMsgErrorExit(RTEXITCODE_FAILURE, GuestCtrl::tr("Out of memory\n"));
2056 }
2057 break;
2058
2059 default:
2060 return errorGetOptEx(USAGE_GUESTCONTROL, HELP_SCOPE_GSTCTRL_MKDIR, ch, &ValueUnion);
2061 }
2062 }
2063
2064 if (!cDirsCreated)
2065 return errorSyntaxEx(USAGE_GUESTCONTROL, HELP_SCOPE_GSTCTRL_MKDIR, GuestCtrl::tr("No directory to create specified!"));
2066 return rcExit;
2067}
2068
2069
2070static DECLCALLBACK(RTEXITCODE) gctlHandleRmDir(PGCTLCMDCTX pCtx, int argc, char **argv)
2071{
2072 AssertPtrReturn(pCtx, RTEXITCODE_FAILURE);
2073
2074 static const RTGETOPTDEF s_aOptions[] =
2075 {
2076 GCTLCMD_COMMON_OPTION_DEFS()
2077 { "--recursive", 'R', RTGETOPT_REQ_NOTHING },
2078 };
2079
2080 int ch;
2081 RTGETOPTUNION ValueUnion;
2082 RTGETOPTSTATE GetState;
2083 RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, RTGETOPTINIT_FLAGS_OPTS_FIRST);
2084
2085 bool fRecursive = false;
2086 uint32_t cDirRemoved = 0;
2087 RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
2088
2089 while ((ch = RTGetOpt(&GetState, &ValueUnion)) != 0)
2090 {
2091 /* For options that require an argument, ValueUnion has received the value. */
2092 switch (ch)
2093 {
2094 GCTLCMD_COMMON_OPTION_CASES(pCtx, ch, &ValueUnion);
2095
2096 case 'R':
2097 fRecursive = true;
2098 break;
2099
2100 case VINF_GETOPT_NOT_OPTION:
2101 {
2102 if (cDirRemoved == 0)
2103 {
2104 /*
2105 * First non-option - no more options now.
2106 */
2107 rcExit = gctlCtxPostOptionParsingInit(pCtx);
2108 if (rcExit != RTEXITCODE_SUCCESS)
2109 return rcExit;
2110 if (pCtx->cVerbose)
2111 {
2112 if (fRecursive)
2113 RTPrintf(GuestCtrl::tr("Removing %RU32 directory tree(s)...\n", "", argc - GetState.iNext + 1),
2114 argc - GetState.iNext + 1);
2115 else
2116 RTPrintf(GuestCtrl::tr("Removing %RU32 directorie(s)...\n", "", argc - GetState.iNext + 1),
2117 argc - GetState.iNext + 1);
2118 }
2119 }
2120 if (g_fGuestCtrlCanceled)
2121 return RTMsgErrorExit(RTEXITCODE_FAILURE, GuestCtrl::tr("rmdir was interrupted by Ctrl-C (%u left)\n"),
2122 argc - GetState.iNext + 1);
2123
2124 cDirRemoved++;
2125 HRESULT rc;
2126 if (!fRecursive)
2127 {
2128 /*
2129 * Remove exactly one directory.
2130 */
2131 if (pCtx->cVerbose)
2132 RTPrintf(GuestCtrl::tr("Removing directory \"%s\" ...\n"), ValueUnion.psz);
2133 try
2134 {
2135 CHECK_ERROR(pCtx->pGuestSession, DirectoryRemove(Bstr(ValueUnion.psz).raw()));
2136 }
2137 catch (std::bad_alloc &)
2138 {
2139 return RTMsgErrorExit(RTEXITCODE_FAILURE, GuestCtrl::tr("Out of memory\n"));
2140 }
2141 }
2142 else
2143 {
2144 /*
2145 * Remove the directory and anything under it, that means files
2146 * and everything. This is in the tradition of the Windows NT
2147 * CMD.EXE "rmdir /s" operation, a tradition which jpsoft's TCC
2148 * strongly warns against (and half-ways questions the sense of).
2149 */
2150 if (pCtx->cVerbose)
2151 RTPrintf(GuestCtrl::tr("Recursively removing directory \"%s\" ...\n"), ValueUnion.psz);
2152 try
2153 {
2154 /** @todo Make flags configurable. */
2155 com::SafeArray<DirectoryRemoveRecFlag_T> aRemRecFlags;
2156 aRemRecFlags.push_back(DirectoryRemoveRecFlag_ContentAndDir);
2157
2158 ComPtr<IProgress> ptrProgress;
2159 CHECK_ERROR(pCtx->pGuestSession, DirectoryRemoveRecursive(Bstr(ValueUnion.psz).raw(),
2160 ComSafeArrayAsInParam(aRemRecFlags),
2161 ptrProgress.asOutParam()));
2162 if (SUCCEEDED(rc))
2163 {
2164 if (pCtx->cVerbose)
2165 rc = showProgress(ptrProgress);
2166 else
2167 rc = ptrProgress->WaitForCompletion(-1 /* indefinitely */);
2168 if (SUCCEEDED(rc))
2169 CHECK_PROGRESS_ERROR(ptrProgress, (GuestCtrl::tr("Directory deletion failed")));
2170 ptrProgress.setNull();
2171 }
2172 }
2173 catch (std::bad_alloc &)
2174 {
2175 return RTMsgErrorExit(RTEXITCODE_FAILURE, GuestCtrl::tr("Out of memory during recursive rmdir\n"));
2176 }
2177 }
2178
2179 /*
2180 * This command returns immediately on failure since it's destructive in nature.
2181 */
2182 if (FAILED(rc))
2183 return RTEXITCODE_FAILURE;
2184 break;
2185 }
2186
2187 default:
2188 return errorGetOptEx(USAGE_GUESTCONTROL, HELP_SCOPE_GSTCTRL_RMDIR, ch, &ValueUnion);
2189 }
2190 }
2191
2192 if (!cDirRemoved)
2193 return errorSyntaxEx(USAGE_GUESTCONTROL, HELP_SCOPE_GSTCTRL_RMDIR, GuestCtrl::tr("No directory to remove specified!"));
2194 return rcExit;
2195}
2196
2197static DECLCALLBACK(RTEXITCODE) gctlHandleRm(PGCTLCMDCTX pCtx, int argc, char **argv)
2198{
2199 AssertPtrReturn(pCtx, RTEXITCODE_FAILURE);
2200
2201 static const RTGETOPTDEF s_aOptions[] =
2202 {
2203 GCTLCMD_COMMON_OPTION_DEFS()
2204 { "--force", 'f', RTGETOPT_REQ_NOTHING, },
2205 };
2206
2207 int ch;
2208 RTGETOPTUNION ValueUnion;
2209 RTGETOPTSTATE GetState;
2210 RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, RTGETOPTINIT_FLAGS_OPTS_FIRST);
2211
2212 uint32_t cFilesDeleted = 0;
2213 RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
2214 bool fForce = true;
2215
2216 while ((ch = RTGetOpt(&GetState, &ValueUnion)) != 0)
2217 {
2218 /* For options that require an argument, ValueUnion has received the value. */
2219 switch (ch)
2220 {
2221 GCTLCMD_COMMON_OPTION_CASES(pCtx, ch, &ValueUnion);
2222
2223 case VINF_GETOPT_NOT_OPTION:
2224 if (cFilesDeleted == 0)
2225 {
2226 /*
2227 * First non-option - no more options now.
2228 */
2229 rcExit = gctlCtxPostOptionParsingInit(pCtx);
2230 if (rcExit != RTEXITCODE_SUCCESS)
2231 return rcExit;
2232 if (pCtx->cVerbose)
2233 RTPrintf(GuestCtrl::tr("Removing %RU32 file(s)...\n", "", argc - GetState.iNext + 1),
2234 argc - GetState.iNext + 1);
2235 }
2236 if (g_fGuestCtrlCanceled)
2237 return RTMsgErrorExit(RTEXITCODE_FAILURE, GuestCtrl::tr("rm was interrupted by Ctrl-C (%u left)\n"),
2238 argc - GetState.iNext + 1);
2239
2240 /*
2241 * Remove the specified file.
2242 *
2243 * On failure we will by default stop, however, the force option will
2244 * by unix traditions force us to ignore errors and continue.
2245 */
2246 cFilesDeleted++;
2247 if (pCtx->cVerbose)
2248 RTPrintf(GuestCtrl::tr("Removing file \"%s\" ...\n", ValueUnion.psz));
2249 try
2250 {
2251 /** @todo How does IGuestSession::FsObjRemove work with read-only files? Do we
2252 * need to do some chmod or whatever to better emulate the --force flag? */
2253 HRESULT rc;
2254 CHECK_ERROR(pCtx->pGuestSession, FsObjRemove(Bstr(ValueUnion.psz).raw()));
2255 if (FAILED(rc) && !fForce)
2256 return RTEXITCODE_FAILURE;
2257 }
2258 catch (std::bad_alloc &)
2259 {
2260 return RTMsgErrorExit(RTEXITCODE_FAILURE, GuestCtrl::tr("Out of memory\n"));
2261 }
2262 break;
2263
2264 default:
2265 return errorGetOptEx(USAGE_GUESTCONTROL, HELP_SCOPE_GSTCTRL_RM, ch, &ValueUnion);
2266 }
2267 }
2268
2269 if (!cFilesDeleted && !fForce)
2270 return errorSyntaxEx(USAGE_GUESTCONTROL, HELP_SCOPE_GSTCTRL_RM, GuestCtrl::tr("No file to remove specified!"));
2271 return rcExit;
2272}
2273
2274static DECLCALLBACK(RTEXITCODE) gctlHandleMv(PGCTLCMDCTX pCtx, int argc, char **argv)
2275{
2276 AssertPtrReturn(pCtx, RTEXITCODE_FAILURE);
2277
2278 static const RTGETOPTDEF s_aOptions[] =
2279 {
2280 GCTLCMD_COMMON_OPTION_DEFS()
2281/** @todo Missing --force/-f flag. */
2282 };
2283
2284 int ch;
2285 RTGETOPTUNION ValueUnion;
2286 RTGETOPTSTATE GetState;
2287 RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, RTGETOPTINIT_FLAGS_OPTS_FIRST);
2288
2289 int vrc = VINF_SUCCESS;
2290
2291 bool fDryrun = false;
2292 std::vector< Utf8Str > vecSources;
2293 const char *pszDst = NULL;
2294 com::SafeArray<FsObjRenameFlag_T> aRenameFlags;
2295
2296 try
2297 {
2298 /** @todo Make flags configurable. */
2299 aRenameFlags.push_back(FsObjRenameFlag_NoReplace);
2300
2301 while ( (ch = RTGetOpt(&GetState, &ValueUnion))
2302 && RT_SUCCESS(vrc))
2303 {
2304 /* For options that require an argument, ValueUnion has received the value. */
2305 switch (ch)
2306 {
2307 GCTLCMD_COMMON_OPTION_CASES(pCtx, ch, &ValueUnion);
2308
2309 /** @todo Implement a --dryrun command. */
2310 /** @todo Implement rename flags. */
2311
2312 case VINF_GETOPT_NOT_OPTION:
2313 vecSources.push_back(Utf8Str(ValueUnion.psz));
2314 pszDst = ValueUnion.psz;
2315 break;
2316
2317 default:
2318 return errorGetOptEx(USAGE_GUESTCONTROL, HELP_SCOPE_GSTCTRL_MV, ch, &ValueUnion);
2319 }
2320 }
2321 }
2322 catch (std::bad_alloc &)
2323 {
2324 vrc = VERR_NO_MEMORY;
2325 }
2326
2327 if (RT_FAILURE(vrc))
2328 return RTMsgErrorExit(RTEXITCODE_FAILURE, GuestCtrl::tr("Failed to initialize, rc=%Rrc\n"), vrc);
2329
2330 size_t cSources = vecSources.size();
2331 if (!cSources)
2332 return errorSyntaxEx(USAGE_GUESTCONTROL, HELP_SCOPE_GSTCTRL_MV,
2333 GuestCtrl::tr("No source(s) to move specified!"));
2334 if (cSources < 2)
2335 return errorSyntaxEx(USAGE_GUESTCONTROL, HELP_SCOPE_GSTCTRL_MV,
2336 GuestCtrl::tr("No destination specified!"));
2337
2338 RTEXITCODE rcExit = gctlCtxPostOptionParsingInit(pCtx);
2339 if (rcExit != RTEXITCODE_SUCCESS)
2340 return rcExit;
2341
2342 /* Delete last element, which now is the destination. */
2343 vecSources.pop_back();
2344 cSources = vecSources.size();
2345
2346 HRESULT rc = S_OK;
2347
2348 /* Destination must be a directory when specifying multiple sources. */
2349 if (cSources > 1)
2350 {
2351 ComPtr<IGuestFsObjInfo> pFsObjInfo;
2352 rc = pCtx->pGuestSession->FsObjQueryInfo(Bstr(pszDst).raw(), FALSE /*followSymlinks*/, pFsObjInfo.asOutParam());
2353 if (FAILED(rc))
2354 {
2355 return RTMsgErrorExit(RTEXITCODE_FAILURE, GuestCtrl::tr("Destination does not exist\n"));
2356 }
2357 else
2358 {
2359 FsObjType_T enmObjType = FsObjType_Unknown; /* Shut up MSC */
2360 rc = pFsObjInfo->COMGETTER(Type)(&enmObjType);
2361 if (SUCCEEDED(rc))
2362 {
2363 if (enmObjType != FsObjType_Directory)
2364 return RTMsgErrorExit(RTEXITCODE_FAILURE,
2365 GuestCtrl::tr("Destination must be a directory when specifying multiple sources\n"));
2366 }
2367 else
2368 return RTMsgErrorExit(RTEXITCODE_FAILURE,
2369 GuestCtrl::tr("Unable to determine destination type: %Rhrc\n"),
2370 rc);
2371 }
2372 }
2373
2374 /*
2375 * Rename (move) the entries.
2376 */
2377 if (pCtx->cVerbose)
2378 RTPrintf(GuestCtrl::tr("Renaming %RU32 %s ...\n"), cSources,
2379 cSources > 1 ? GuestCtrl::tr("sources", "", cSources) : GuestCtrl::tr("source"));
2380
2381 std::vector< Utf8Str >::iterator it = vecSources.begin();
2382 while ( it != vecSources.end()
2383 && !g_fGuestCtrlCanceled)
2384 {
2385 Utf8Str strSrcCur = (*it);
2386
2387 ComPtr<IGuestFsObjInfo> pFsObjInfo;
2388 FsObjType_T enmObjType = FsObjType_Unknown; /* Shut up MSC */
2389 rc = pCtx->pGuestSession->FsObjQueryInfo(Bstr(strSrcCur).raw(), FALSE /*followSymlinks*/, pFsObjInfo.asOutParam());
2390 if (SUCCEEDED(rc))
2391 rc = pFsObjInfo->COMGETTER(Type)(&enmObjType);
2392 if (FAILED(rc))
2393 {
2394 RTPrintf(GuestCtrl::tr("Cannot stat \"%s\": No such file or directory\n"), strSrcCur.c_str());
2395 ++it;
2396 continue; /* Skip. */
2397 }
2398
2399 char *pszDstCur = NULL;
2400
2401 if (cSources > 1)
2402 {
2403 pszDstCur = RTPathJoinA(pszDst, RTPathFilename(strSrcCur.c_str()));
2404 }
2405 else
2406 pszDstCur = RTStrDup(pszDst);
2407
2408 AssertPtrBreakStmt(pszDstCur, VERR_NO_MEMORY);
2409
2410 if (pCtx->cVerbose)
2411 RTPrintf(GuestCtrl::tr("Renaming %s \"%s\" to \"%s\" ...\n"),
2412 enmObjType == FsObjType_Directory ? GuestCtrl::tr("directory", "object") : GuestCtrl::tr("file","object"),
2413 strSrcCur.c_str(), pszDstCur);
2414
2415 if (!fDryrun)
2416 {
2417 CHECK_ERROR(pCtx->pGuestSession, FsObjRename(Bstr(strSrcCur).raw(),
2418 Bstr(pszDstCur).raw(),
2419 ComSafeArrayAsInParam(aRenameFlags)));
2420 /* Keep going with next item in case of errors. */
2421 }
2422
2423 RTStrFree(pszDstCur);
2424
2425 ++it;
2426 }
2427
2428 if ( (it != vecSources.end())
2429 && pCtx->cVerbose)
2430 {
2431 RTPrintf(GuestCtrl::tr("Warning: Not all sources were renamed\n"));
2432 }
2433
2434 return FAILED(rc) ? RTEXITCODE_FAILURE : RTEXITCODE_SUCCESS;
2435}
2436
2437static DECLCALLBACK(RTEXITCODE) gctlHandleMkTemp(PGCTLCMDCTX pCtx, int argc, char **argv)
2438{
2439 AssertPtrReturn(pCtx, RTEXITCODE_FAILURE);
2440
2441 static const RTGETOPTDEF s_aOptions[] =
2442 {
2443 GCTLCMD_COMMON_OPTION_DEFS()
2444 { "--mode", 'm', RTGETOPT_REQ_UINT32 },
2445 { "--directory", 'D', RTGETOPT_REQ_NOTHING },
2446 { "--secure", 's', RTGETOPT_REQ_NOTHING },
2447 { "--tmpdir", 't', RTGETOPT_REQ_STRING }
2448 };
2449
2450 int ch;
2451 RTGETOPTUNION ValueUnion;
2452 RTGETOPTSTATE GetState;
2453 RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, RTGETOPTINIT_FLAGS_OPTS_FIRST);
2454
2455 Utf8Str strTemplate;
2456 uint32_t fMode = 0; /* Default mode. */
2457 bool fDirectory = false;
2458 bool fSecure = false;
2459 Utf8Str strTempDir;
2460
2461 DESTDIRMAP mapDirs;
2462
2463 while ((ch = RTGetOpt(&GetState, &ValueUnion)) != 0)
2464 {
2465 /* For options that require an argument, ValueUnion has received the value. */
2466 switch (ch)
2467 {
2468 GCTLCMD_COMMON_OPTION_CASES(pCtx, ch, &ValueUnion);
2469
2470 case 'm': /* Mode */
2471 fMode = ValueUnion.u32;
2472 break;
2473
2474 case 'D': /* Create directory */
2475 fDirectory = true;
2476 break;
2477
2478 case 's': /* Secure */
2479 fSecure = true;
2480 break;
2481
2482 case 't': /* Temp directory */
2483 strTempDir = ValueUnion.psz;
2484 break;
2485
2486 case VINF_GETOPT_NOT_OPTION:
2487 if (strTemplate.isEmpty())
2488 strTemplate = ValueUnion.psz;
2489 else
2490 return errorSyntaxEx(USAGE_GUESTCONTROL, HELP_SCOPE_GSTCTRL_MKTEMP,
2491 GuestCtrl::tr("More than one template specified!\n"));
2492 break;
2493
2494 default:
2495 return errorGetOptEx(USAGE_GUESTCONTROL, HELP_SCOPE_GSTCTRL_MKTEMP, ch, &ValueUnion);
2496 }
2497 }
2498
2499 if (strTemplate.isEmpty())
2500 return errorSyntaxEx(USAGE_GUESTCONTROL, HELP_SCOPE_GSTCTRL_MKTEMP,
2501 GuestCtrl::tr("No template specified!"));
2502
2503 if (!fDirectory)
2504 return errorSyntaxEx(USAGE_GUESTCONTROL, HELP_SCOPE_GSTCTRL_MKTEMP,
2505 GuestCtrl::tr("Creating temporary files is currently not supported!"));
2506
2507 RTEXITCODE rcExit = gctlCtxPostOptionParsingInit(pCtx);
2508 if (rcExit != RTEXITCODE_SUCCESS)
2509 return rcExit;
2510
2511 /*
2512 * Create the directories.
2513 */
2514 if (pCtx->cVerbose)
2515 {
2516 if (fDirectory && !strTempDir.isEmpty())
2517 RTPrintf(GuestCtrl::tr("Creating temporary directory from template '%s' in directory '%s' ...\n"),
2518 strTemplate.c_str(), strTempDir.c_str());
2519 else if (fDirectory)
2520 RTPrintf(GuestCtrl::tr("Creating temporary directory from template '%s' in default temporary directory ...\n"),
2521 strTemplate.c_str());
2522 else if (!fDirectory && !strTempDir.isEmpty())
2523 RTPrintf(GuestCtrl::tr("Creating temporary file from template '%s' in directory '%s' ...\n"),
2524 strTemplate.c_str(), strTempDir.c_str());
2525 else if (!fDirectory)
2526 RTPrintf(GuestCtrl::tr("Creating temporary file from template '%s' in default temporary directory ...\n"),
2527 strTemplate.c_str());
2528 }
2529
2530 HRESULT rc = S_OK;
2531 if (fDirectory)
2532 {
2533 Bstr bstrDirectory;
2534 CHECK_ERROR(pCtx->pGuestSession, DirectoryCreateTemp(Bstr(strTemplate).raw(),
2535 fMode, Bstr(strTempDir).raw(),
2536 fSecure,
2537 bstrDirectory.asOutParam()));
2538 if (SUCCEEDED(rc))
2539 RTPrintf(GuestCtrl::tr("Directory name: %ls\n"), bstrDirectory.raw());
2540 }
2541 else
2542 {
2543 // else - temporary file not yet implemented
2544 /** @todo implement temporary file creation (we fend it off above, no
2545 * worries). */
2546 rc = E_FAIL;
2547 }
2548
2549 return FAILED(rc) ? RTEXITCODE_FAILURE : RTEXITCODE_SUCCESS;
2550}
2551
2552static DECLCALLBACK(RTEXITCODE) gctlHandleStat(PGCTLCMDCTX pCtx, int argc, char **argv)
2553{
2554 AssertPtrReturn(pCtx, RTEXITCODE_FAILURE);
2555
2556 static const RTGETOPTDEF s_aOptions[] =
2557 {
2558 GCTLCMD_COMMON_OPTION_DEFS()
2559 { "--dereference", 'L', RTGETOPT_REQ_NOTHING },
2560 { "--file-system", 'f', RTGETOPT_REQ_NOTHING },
2561 { "--format", 'c', RTGETOPT_REQ_STRING },
2562 { "--terse", 't', RTGETOPT_REQ_NOTHING }
2563 };
2564
2565 int ch;
2566 RTGETOPTUNION ValueUnion;
2567 RTGETOPTSTATE GetState;
2568 RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, RTGETOPTINIT_FLAGS_OPTS_FIRST);
2569
2570 while ( (ch = RTGetOpt(&GetState, &ValueUnion)) != 0
2571 && ch != VINF_GETOPT_NOT_OPTION)
2572 {
2573 /* For options that require an argument, ValueUnion has received the value. */
2574 switch (ch)
2575 {
2576 GCTLCMD_COMMON_OPTION_CASES(pCtx, ch, &ValueUnion);
2577
2578 case 'L': /* Dereference */
2579 case 'f': /* File-system */
2580 case 'c': /* Format */
2581 case 't': /* Terse */
2582 return errorSyntaxEx(USAGE_GUESTCONTROL, HELP_SCOPE_GSTCTRL_STAT,
2583 GuestCtrl::tr("Command \"%s\" not implemented yet!"), ValueUnion.psz);
2584
2585 default:
2586 return errorGetOptEx(USAGE_GUESTCONTROL, HELP_SCOPE_GSTCTRL_STAT, ch, &ValueUnion);
2587 }
2588 }
2589
2590 if (ch != VINF_GETOPT_NOT_OPTION)
2591 return errorSyntaxEx(USAGE_GUESTCONTROL, HELP_SCOPE_GSTCTRL_STAT, GuestCtrl::tr("Nothing to stat!"));
2592
2593 RTEXITCODE rcExit = gctlCtxPostOptionParsingInit(pCtx);
2594 if (rcExit != RTEXITCODE_SUCCESS)
2595 return rcExit;
2596
2597
2598 /*
2599 * Do the file stat'ing.
2600 */
2601 while (ch == VINF_GETOPT_NOT_OPTION)
2602 {
2603 if (pCtx->cVerbose)
2604 RTPrintf(GuestCtrl::tr("Checking for element \"%s\" ...\n"), ValueUnion.psz);
2605
2606 ComPtr<IGuestFsObjInfo> pFsObjInfo;
2607 HRESULT hrc = pCtx->pGuestSession->FsObjQueryInfo(Bstr(ValueUnion.psz).raw(), FALSE /*followSymlinks*/,
2608 pFsObjInfo.asOutParam());
2609 if (FAILED(hrc))
2610 {
2611 /** @todo r=bird: There might be other reasons why we end up here than
2612 * non-existing "element" (object or file, please, nobody calls it elements). */
2613 if (pCtx->cVerbose)
2614 RTPrintf(GuestCtrl::tr("Failed to stat '%s': No such file\n"), ValueUnion.psz);
2615 rcExit = RTEXITCODE_FAILURE;
2616 }
2617 else
2618 {
2619 RTPrintf(GuestCtrl::tr(" File: '%s'\n"), ValueUnion.psz); /** @todo escape this name. */
2620
2621 FsObjType_T enmType = FsObjType_Unknown;
2622 CHECK_ERROR2I(pFsObjInfo, COMGETTER(Type)(&enmType));
2623 LONG64 cbObject = 0;
2624 CHECK_ERROR2I(pFsObjInfo, COMGETTER(ObjectSize)(&cbObject));
2625 LONG64 cbAllocated = 0;
2626 CHECK_ERROR2I(pFsObjInfo, COMGETTER(AllocatedSize)(&cbAllocated));
2627 LONG uid = 0;
2628 CHECK_ERROR2I(pFsObjInfo, COMGETTER(UID)(&uid));
2629 LONG gid = 0;
2630 CHECK_ERROR2I(pFsObjInfo, COMGETTER(GID)(&gid));
2631 Bstr bstrUsername;
2632 CHECK_ERROR2I(pFsObjInfo, COMGETTER(UserName)(bstrUsername.asOutParam()));
2633 Bstr bstrGroupName;
2634 CHECK_ERROR2I(pFsObjInfo, COMGETTER(GroupName)(bstrGroupName.asOutParam()));
2635 Bstr bstrAttribs;
2636 CHECK_ERROR2I(pFsObjInfo, COMGETTER(FileAttributes)(bstrAttribs.asOutParam()));
2637 LONG64 idNode = 0;
2638 CHECK_ERROR2I(pFsObjInfo, COMGETTER(NodeId)(&idNode));
2639 ULONG uDevNode = 0;
2640 CHECK_ERROR2I(pFsObjInfo, COMGETTER(NodeIdDevice)(&uDevNode));
2641 ULONG uDeviceNo = 0;
2642 CHECK_ERROR2I(pFsObjInfo, COMGETTER(DeviceNumber)(&uDeviceNo));
2643 ULONG cHardLinks = 1;
2644 CHECK_ERROR2I(pFsObjInfo, COMGETTER(HardLinks)(&cHardLinks));
2645 LONG64 nsBirthTime = 0;
2646 CHECK_ERROR2I(pFsObjInfo, COMGETTER(BirthTime)(&nsBirthTime));
2647 LONG64 nsChangeTime = 0;
2648 CHECK_ERROR2I(pFsObjInfo, COMGETTER(ChangeTime)(&nsChangeTime));
2649 LONG64 nsModificationTime = 0;
2650 CHECK_ERROR2I(pFsObjInfo, COMGETTER(ModificationTime)(&nsModificationTime));
2651 LONG64 nsAccessTime = 0;
2652 CHECK_ERROR2I(pFsObjInfo, COMGETTER(AccessTime)(&nsAccessTime));
2653
2654 RTPrintf(GuestCtrl::tr(" Size: %-17RU64 Alloc: %-19RU64 Type: %s\n"),
2655 cbObject, cbAllocated, gctlFsObjTypeToName(enmType));
2656 RTPrintf(GuestCtrl::tr("Device: %#-17RX32 INode: %-18RU64 Links: %u\n"), uDevNode, idNode, cHardLinks);
2657
2658 Utf8Str strAttrib(bstrAttribs);
2659 char *pszMode = strAttrib.mutableRaw();
2660 char *pszAttribs = strchr(pszMode, ' ');
2661 if (pszAttribs)
2662 do *pszAttribs++ = '\0';
2663 while (*pszAttribs == ' ');
2664 else
2665 pszAttribs = strchr(pszMode, '\0');
2666 if (uDeviceNo != 0)
2667 RTPrintf(GuestCtrl::tr(" Mode: %-16s Attrib: %-17s Dev ID: %#RX32\n"), pszMode, pszAttribs, uDeviceNo);
2668 else
2669 RTPrintf(GuestCtrl::tr(" Mode: %-16s Attrib: %s\n"), pszMode, pszAttribs);
2670
2671 RTPrintf(GuestCtrl::tr(" Owner: %4d/%-12ls Group: %4d/%ls\n"), uid, bstrUsername.raw(), gid, bstrGroupName.raw());
2672
2673 RTTIMESPEC TimeSpec;
2674 char szTmp[RTTIME_STR_LEN];
2675 RTPrintf(GuestCtrl::tr(" Birth: %s\n"), RTTimeSpecToString(RTTimeSpecSetNano(&TimeSpec, nsBirthTime),
2676 szTmp, sizeof(szTmp)));
2677 RTPrintf(GuestCtrl::tr("Change: %s\n"), RTTimeSpecToString(RTTimeSpecSetNano(&TimeSpec, nsChangeTime),
2678 szTmp, sizeof(szTmp)));
2679 RTPrintf(GuestCtrl::tr("Modify: %s\n"), RTTimeSpecToString(RTTimeSpecSetNano(&TimeSpec, nsModificationTime),
2680 szTmp, sizeof(szTmp)));
2681 RTPrintf(GuestCtrl::tr("Access: %s\n"), RTTimeSpecToString(RTTimeSpecSetNano(&TimeSpec, nsAccessTime),
2682 szTmp, sizeof(szTmp)));
2683
2684 /* Skiping: Generation ID - only the ISO9660 VFS sets this. FreeBSD user flags. */
2685 }
2686
2687 /* Next file. */
2688 ch = RTGetOpt(&GetState, &ValueUnion);
2689 }
2690
2691 return rcExit;
2692}
2693
2694/**
2695 * Waits for a Guest Additions run level being reached.
2696 *
2697 * @returns VBox status code.
2698 * Returns VERR_CANCELLED if waiting for cancelled due to signal handling, e.g. when CTRL+C or some sort was pressed.
2699 * @param pCtx The guest control command context.
2700 * @param enmRunLevel Run level to wait for.
2701 * @param cMsTimeout Timeout (in ms) for waiting.
2702 */
2703static int gctlWaitForRunLevel(PGCTLCMDCTX pCtx, AdditionsRunLevelType_T enmRunLevel, RTMSINTERVAL cMsTimeout)
2704{
2705 int vrc = VINF_SUCCESS; /* Shut up MSVC. */
2706
2707 try
2708 {
2709 HRESULT rc = S_OK;
2710 /** Whether we need to actually wait for the run level or if we already reached it. */
2711 bool fWait = false;
2712
2713 /* Install an event handler first to catch any runlevel changes. */
2714 ComObjPtr<GuestAdditionsRunlevelListenerImpl> pGuestListener;
2715 do
2716 {
2717 /* Listener creation. */
2718 pGuestListener.createObject();
2719 pGuestListener->init(new GuestAdditionsRunlevelListener(enmRunLevel));
2720
2721 /* Register for IGuest events. */
2722 ComPtr<IEventSource> es;
2723 CHECK_ERROR_BREAK(pCtx->pGuest, COMGETTER(EventSource)(es.asOutParam()));
2724 com::SafeArray<VBoxEventType_T> eventTypes;
2725 eventTypes.push_back(VBoxEventType_OnGuestAdditionsStatusChanged);
2726 CHECK_ERROR_BREAK(es, RegisterListener(pGuestListener, ComSafeArrayAsInParam(eventTypes),
2727 true /* Active listener */));
2728
2729 AdditionsRunLevelType_T enmRunLevelCur = AdditionsRunLevelType_None;
2730 CHECK_ERROR_BREAK(pCtx->pGuest, COMGETTER(AdditionsRunLevel)(&enmRunLevelCur));
2731 fWait = enmRunLevelCur != enmRunLevel;
2732
2733 if (pCtx->cVerbose)
2734 RTPrintf(GuestCtrl::tr("Current run level is %RU32\n"), enmRunLevelCur);
2735
2736 } while (0);
2737
2738 if (fWait)
2739 {
2740 if (pCtx->cVerbose)
2741 RTPrintf(GuestCtrl::tr("Waiting for run level %RU32 ...\n"), enmRunLevel);
2742
2743 RTMSINTERVAL tsStart = RTTimeMilliTS();
2744 while (RTTimeMilliTS() - tsStart < cMsTimeout)
2745 {
2746 /* Wait for the global signal semaphore getting signalled. */
2747 vrc = RTSemEventWait(g_SemEventGuestCtrlCanceled, 100 /* ms */);
2748 if (RT_FAILURE(vrc))
2749 {
2750 if (vrc == VERR_TIMEOUT)
2751 continue;
2752 else
2753 {
2754 RTPrintf(GuestCtrl::tr("Waiting failed with %Rrc\n"), vrc);
2755 break;
2756 }
2757 }
2758 else if (pCtx->cVerbose)
2759 {
2760 RTPrintf(GuestCtrl::tr("Run level %RU32 reached\n"), enmRunLevel);
2761 break;
2762 }
2763
2764 NativeEventQueue::getMainEventQueue()->processEventQueue(0);
2765 }
2766
2767 if ( vrc == VERR_TIMEOUT
2768 && pCtx->cVerbose)
2769 RTPrintf(GuestCtrl::tr("Run level %RU32 not reached within time\n"), enmRunLevel);
2770 }
2771
2772 if (!pGuestListener.isNull())
2773 {
2774 /* Guest callback unregistration. */
2775 ComPtr<IEventSource> pES;
2776 CHECK_ERROR(pCtx->pGuest, COMGETTER(EventSource)(pES.asOutParam()));
2777 if (!pES.isNull())
2778 CHECK_ERROR(pES, UnregisterListener(pGuestListener));
2779 pGuestListener.setNull();
2780 }
2781
2782 if (g_fGuestCtrlCanceled)
2783 vrc = VERR_CANCELLED;
2784 }
2785 catch (std::bad_alloc &)
2786 {
2787 vrc = VERR_NO_MEMORY;
2788 }
2789
2790 return vrc;
2791}
2792
2793static DECLCALLBACK(RTEXITCODE) gctlHandleUpdateAdditions(PGCTLCMDCTX pCtx, int argc, char **argv)
2794{
2795 AssertPtrReturn(pCtx, RTEXITCODE_FAILURE);
2796
2797 /** Timeout to wait for the whole updating procedure to complete. */
2798 uint32_t cMsTimeout = RT_INDEFINITE_WAIT;
2799 /** Source path to .ISO Guest Additions file to use. */
2800 Utf8Str strSource;
2801 com::SafeArray<IN_BSTR> aArgs;
2802 /** Whether to reboot the guest automatically when the update process has finished successfully. */
2803 bool fRebootOnFinish = false;
2804 /** Whether to only wait for getting the update process started instead of waiting until it finishes. */
2805 bool fWaitStartOnly = false;
2806 /** Whether to wait for the VM being ready to start the update. Needs Guest Additions facility reporting. */
2807 bool fWaitReady = false;
2808 /** Whether to verify if the Guest Additions were successfully updated on the guest. */
2809 bool fVerify = false;
2810
2811 /*
2812 * Parse arguments.
2813 */
2814 enum KGSTCTRLUPDATEADDITIONSOPT
2815 {
2816 KGSTCTRLUPDATEADDITIONSOPT_REBOOT = 1000,
2817 KGSTCTRLUPDATEADDITIONSOPT_SOURCE,
2818 KGSTCTRLUPDATEADDITIONSOPT_TIMEOUT,
2819 KGSTCTRLUPDATEADDITIONSOPT_VERIFY,
2820 KGSTCTRLUPDATEADDITIONSOPT_WAITREADY,
2821 KGSTCTRLUPDATEADDITIONSOPT_WAITSTART
2822 };
2823
2824 static const RTGETOPTDEF s_aOptions[] =
2825 {
2826 GCTLCMD_COMMON_OPTION_DEFS()
2827 { "--reboot", KGSTCTRLUPDATEADDITIONSOPT_REBOOT, RTGETOPT_REQ_NOTHING },
2828 { "--source", KGSTCTRLUPDATEADDITIONSOPT_SOURCE, RTGETOPT_REQ_STRING },
2829 { "--timeout", KGSTCTRLUPDATEADDITIONSOPT_TIMEOUT, RTGETOPT_REQ_UINT32 },
2830 { "--verify", KGSTCTRLUPDATEADDITIONSOPT_VERIFY, RTGETOPT_REQ_NOTHING },
2831 { "--wait-ready", KGSTCTRLUPDATEADDITIONSOPT_WAITREADY, RTGETOPT_REQ_NOTHING },
2832 { "--wait-start", KGSTCTRLUPDATEADDITIONSOPT_WAITSTART, RTGETOPT_REQ_NOTHING }
2833 };
2834
2835 int ch;
2836 RTGETOPTUNION ValueUnion;
2837 RTGETOPTSTATE GetState;
2838 RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, RTGETOPTINIT_FLAGS_OPTS_FIRST);
2839
2840 int vrc = VINF_SUCCESS;
2841 while ( (ch = RTGetOpt(&GetState, &ValueUnion))
2842 && RT_SUCCESS(vrc))
2843 {
2844 switch (ch)
2845 {
2846 GCTLCMD_COMMON_OPTION_CASES(pCtx, ch, &ValueUnion);
2847
2848 case KGSTCTRLUPDATEADDITIONSOPT_REBOOT:
2849 fRebootOnFinish = true;
2850 break;
2851
2852 case KGSTCTRLUPDATEADDITIONSOPT_SOURCE:
2853 vrc = RTPathAbsCxx(strSource, ValueUnion.psz);
2854 if (RT_FAILURE(vrc))
2855 return RTMsgErrorExitFailure(GuestCtrl::tr("RTPathAbsCxx failed on '%s': %Rrc"), ValueUnion.psz, vrc);
2856 break;
2857
2858 case KGSTCTRLUPDATEADDITIONSOPT_WAITSTART:
2859 fWaitStartOnly = true;
2860 break;
2861
2862 case KGSTCTRLUPDATEADDITIONSOPT_WAITREADY:
2863 fWaitReady = true;
2864 break;
2865
2866 case KGSTCTRLUPDATEADDITIONSOPT_VERIFY:
2867 fVerify = true;
2868 fRebootOnFinish = true; /* Verification needs a mandatory reboot after successful update. */
2869 break;
2870
2871 case VINF_GETOPT_NOT_OPTION:
2872 if (aArgs.size() == 0 && strSource.isEmpty())
2873 strSource = ValueUnion.psz;
2874 else
2875 aArgs.push_back(Bstr(ValueUnion.psz).raw());
2876 break;
2877
2878 default:
2879 return errorGetOptEx(USAGE_GUESTCONTROL, HELP_SCOPE_GSTCTRL_UPDATEGA, ch, &ValueUnion);
2880 }
2881 }
2882
2883 if (pCtx->cVerbose)
2884 RTPrintf(GuestCtrl::tr("Updating Guest Additions ...\n"));
2885
2886 HRESULT rc = S_OK;
2887 while (strSource.isEmpty())
2888 {
2889 ComPtr<ISystemProperties> pProperties;
2890 CHECK_ERROR_BREAK(pCtx->pArg->virtualBox, COMGETTER(SystemProperties)(pProperties.asOutParam()));
2891 Bstr strISO;
2892 CHECK_ERROR_BREAK(pProperties, COMGETTER(DefaultAdditionsISO)(strISO.asOutParam()));
2893 strSource = strISO;
2894 break;
2895 }
2896
2897 /* Determine source if not set yet. */
2898 if (strSource.isEmpty())
2899 {
2900 RTMsgError(GuestCtrl::tr("No Guest Additions source found or specified, aborting\n"));
2901 vrc = VERR_FILE_NOT_FOUND;
2902 }
2903 else if (!RTFileExists(strSource.c_str()))
2904 {
2905 RTMsgError(GuestCtrl::tr("Source \"%s\" does not exist!\n"), strSource.c_str());
2906 vrc = VERR_FILE_NOT_FOUND;
2907 }
2908
2909
2910
2911#if 0
2912 ComPtr<IGuest> guest;
2913 rc = pConsole->COMGETTER(Guest)(guest.asOutParam());
2914 if (SUCCEEDED(rc) && !guest.isNull())
2915 {
2916 SHOW_STRING_PROP_NOT_EMPTY(guest, OSTypeId, "GuestOSType", GuestCtrl::tr("OS type:"));
2917
2918 AdditionsRunLevelType_T guestRunLevel; /** @todo Add a runlevel-to-string (e.g. 0 = "None") method? */
2919 rc = guest->COMGETTER(AdditionsRunLevel)(&guestRunLevel);
2920 if (SUCCEEDED(rc))
2921 SHOW_ULONG_VALUE("GuestAdditionsRunLevel", GuestCtrl::tr("Additions run level:"), (ULONG)guestRunLevel, "");
2922
2923 Bstr guestString;
2924 rc = guest->COMGETTER(AdditionsVersion)(guestString.asOutParam());
2925 if ( SUCCEEDED(rc)
2926 && !guestString.isEmpty())
2927 {
2928 ULONG uRevision;
2929 rc = guest->COMGETTER(AdditionsRevision)(&uRevision);
2930 if (FAILED(rc))
2931 uRevision = 0;
2932 RTStrPrintf(szValue, sizeof(szValue), "%ls r%u", guestString.raw(), uRevision);
2933 SHOW_UTF8_STRING("GuestAdditionsVersion", GuestCtrl::tr("Additions version:"), szValue);
2934 }
2935 }
2936#endif
2937
2938 if (RT_SUCCESS(vrc))
2939 {
2940 if (pCtx->cVerbose)
2941 RTPrintf(GuestCtrl::tr("Using source: %s\n"), strSource.c_str());
2942
2943 RTEXITCODE rcExit = gctlCtxPostOptionParsingInit(pCtx);
2944 if (rcExit != RTEXITCODE_SUCCESS)
2945 return rcExit;
2946
2947 if (fWaitReady)
2948 {
2949 if (pCtx->cVerbose)
2950 RTPrintf(GuestCtrl::tr("Waiting for current Guest Additions inside VM getting ready for updating ...\n"));
2951
2952 const uint64_t uTsStart = RTTimeMilliTS();
2953 vrc = gctlWaitForRunLevel(pCtx, AdditionsRunLevelType_Userland, cMsTimeout);
2954 if (RT_SUCCESS(vrc))
2955 cMsTimeout = cMsTimeout != RT_INDEFINITE_WAIT ? cMsTimeout - (RTTimeMilliTS() - uTsStart) : cMsTimeout;
2956 }
2957
2958 if (RT_SUCCESS(vrc))
2959 {
2960 /* Get current Guest Additions version / revision. */
2961 Bstr strGstVerCur;
2962 ULONG uGstRevCur = 0;
2963 rc = pCtx->pGuest->COMGETTER(AdditionsVersion)(strGstVerCur.asOutParam());
2964 if ( SUCCEEDED(rc)
2965 && !strGstVerCur.isEmpty())
2966 {
2967 rc = pCtx->pGuest->COMGETTER(AdditionsRevision)(&uGstRevCur);
2968 if (SUCCEEDED(rc))
2969 {
2970 if (pCtx->cVerbose)
2971 RTPrintf(GuestCtrl::tr("Guest Additions %lsr%RU64 currently installed, waiting for Guest Additions installer to start ...\n"),
2972 strGstVerCur.raw(), uGstRevCur);
2973 }
2974 }
2975
2976 com::SafeArray<AdditionsUpdateFlag_T> aUpdateFlags;
2977 if (fWaitStartOnly)
2978 aUpdateFlags.push_back(AdditionsUpdateFlag_WaitForUpdateStartOnly);
2979
2980 ComPtr<IProgress> pProgress;
2981 CHECK_ERROR(pCtx->pGuest, UpdateGuestAdditions(Bstr(strSource).raw(),
2982 ComSafeArrayAsInParam(aArgs),
2983 ComSafeArrayAsInParam(aUpdateFlags),
2984 pProgress.asOutParam()));
2985 if (FAILED(rc))
2986 vrc = gctlPrintError(pCtx->pGuest, COM_IIDOF(IGuest));
2987 else
2988 {
2989 if (pCtx->cVerbose)
2990 rc = showProgress(pProgress);
2991 else
2992 rc = pProgress->WaitForCompletion((int32_t)cMsTimeout);
2993
2994 if (SUCCEEDED(rc))
2995 CHECK_PROGRESS_ERROR(pProgress, (GuestCtrl::tr("Guest Additions update failed")));
2996 vrc = gctlPrintProgressError(pProgress);
2997 if (RT_SUCCESS(vrc))
2998 {
2999 if (pCtx->cVerbose)
3000 RTPrintf(GuestCtrl::tr("Guest Additions update successful.\n"));
3001
3002 if (fRebootOnFinish)
3003 {
3004 if (pCtx->cVerbose)
3005 RTPrintf(GuestCtrl::tr("Rebooting guest ...\n"));
3006 com::SafeArray<GuestShutdownFlag_T> aShutdownFlags;
3007 aShutdownFlags.push_back(GuestShutdownFlag_Reboot);
3008 CHECK_ERROR(pCtx->pGuest, Shutdown(ComSafeArrayAsInParam(aShutdownFlags)));
3009 if (FAILED(rc))
3010 {
3011 if (rc == VBOX_E_NOT_SUPPORTED)
3012 {
3013 RTPrintf(GuestCtrl::tr("Current installed Guest Additions don't support automatic rebooting. "
3014 "Please reboot manually.\n"));
3015 vrc = VERR_NOT_SUPPORTED;
3016 }
3017 else
3018 vrc = gctlPrintError(pCtx->pGuest, COM_IIDOF(IGuest));
3019 }
3020 else
3021 {
3022 if (fWaitReady)
3023 {
3024 if (pCtx->cVerbose)
3025 RTPrintf(GuestCtrl::tr("Waiting for new Guest Additions inside VM getting ready ...\n"));
3026
3027 vrc = gctlWaitForRunLevel(pCtx, AdditionsRunLevelType_Userland, cMsTimeout);
3028 if (RT_SUCCESS(vrc))
3029 {
3030 if (fVerify)
3031 {
3032 if (pCtx->cVerbose)
3033 RTPrintf(GuestCtrl::tr("Verifying Guest Additions update ...\n"));
3034
3035 /* Get new Guest Additions version / revision. */
3036 Bstr strGstVerNew;
3037 ULONG uGstRevNew = 0;
3038 rc = pCtx->pGuest->COMGETTER(AdditionsVersion)(strGstVerNew.asOutParam());
3039 if ( SUCCEEDED(rc)
3040 && !strGstVerNew.isEmpty())
3041 {
3042 rc = pCtx->pGuest->COMGETTER(AdditionsRevision)(&uGstRevNew);
3043 if (FAILED(rc))
3044 uGstRevNew = 0;
3045 }
3046
3047 /** @todo Do more verification here. */
3048 vrc = uGstRevNew > uGstRevCur ? VINF_SUCCESS : VERR_NO_CHANGE;
3049
3050 if (pCtx->cVerbose)
3051 {
3052 RTPrintf(GuestCtrl::tr("Old Guest Additions: %ls%RU64\n"), strGstVerCur.raw(),
3053 uGstRevCur);
3054 RTPrintf(GuestCtrl::tr("New Guest Additions: %ls%RU64\n"), strGstVerNew.raw(),
3055 uGstRevNew);
3056
3057 if (RT_FAILURE(vrc))
3058 {
3059 RTPrintf(GuestCtrl::tr("\nError updating Guest Additions, please check guest installer log\n"));
3060 }
3061 else
3062 {
3063 if (uGstRevNew < uGstRevCur)
3064 RTPrintf(GuestCtrl::tr("\nWARNING: Guest Additions were downgraded\n"));
3065 }
3066 }
3067 }
3068 }
3069 }
3070 else if (pCtx->cVerbose)
3071 RTPrintf(GuestCtrl::tr("The guest needs to be restarted in order to make use of the updated Guest Additions.\n"));
3072 }
3073 }
3074 }
3075 }
3076 }
3077 }
3078
3079 return RT_SUCCESS(vrc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
3080}
3081
3082/**
3083 * Returns a Guest Additions run level from a string.
3084 *
3085 * @returns Run level if found, or AdditionsRunLevelType_None if not found / invalid.
3086 * @param pcszStr String to return run level for.
3087 */
3088static AdditionsRunLevelType_T gctlGetRunLevelFromStr(const char *pcszStr)
3089{
3090 AssertPtrReturn(pcszStr, AdditionsRunLevelType_None);
3091
3092 if (RTStrICmp(pcszStr, "system") == 0) return AdditionsRunLevelType_System;
3093 else if (RTStrICmp(pcszStr, "userland") == 0) return AdditionsRunLevelType_Userland;
3094 else if (RTStrICmp(pcszStr, "desktop") == 0) return AdditionsRunLevelType_Desktop;
3095
3096 return AdditionsRunLevelType_None;
3097}
3098
3099static DECLCALLBACK(RTEXITCODE) gctlHandleWaitRunLevel(PGCTLCMDCTX pCtx, int argc, char **argv)
3100{
3101 AssertPtrReturn(pCtx, RTEXITCODE_FAILURE);
3102
3103 /** Timeout to wait for run level being reached.
3104 * By default we wait until it's reached. */
3105 uint32_t cMsTimeout = RT_INDEFINITE_WAIT;
3106
3107 /*
3108 * Parse arguments.
3109 */
3110 enum KGSTCTRLWAITRUNLEVELOPT
3111 {
3112 KGSTCTRLWAITRUNLEVELOPT_TIMEOUT = 1000
3113 };
3114
3115 static const RTGETOPTDEF s_aOptions[] =
3116 {
3117 GCTLCMD_COMMON_OPTION_DEFS()
3118 { "--timeout", KGSTCTRLWAITRUNLEVELOPT_TIMEOUT, RTGETOPT_REQ_UINT32 }
3119 };
3120
3121 int ch;
3122 RTGETOPTUNION ValueUnion;
3123 RTGETOPTSTATE GetState;
3124 RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, RTGETOPTINIT_FLAGS_OPTS_FIRST);
3125
3126 AdditionsRunLevelType_T enmRunLevel = AdditionsRunLevelType_None;
3127
3128 int vrc = VINF_SUCCESS;
3129 while ( (ch = RTGetOpt(&GetState, &ValueUnion))
3130 && RT_SUCCESS(vrc))
3131 {
3132 switch (ch)
3133 {
3134 GCTLCMD_COMMON_OPTION_CASES(pCtx, ch, &ValueUnion);
3135
3136 case KGSTCTRLWAITRUNLEVELOPT_TIMEOUT:
3137 cMsTimeout = ValueUnion.u32;
3138 break;
3139
3140 case VINF_GETOPT_NOT_OPTION:
3141 {
3142 enmRunLevel = gctlGetRunLevelFromStr(ValueUnion.psz);
3143 if (enmRunLevel == AdditionsRunLevelType_None)
3144 return errorSyntaxEx(USAGE_GUESTCONTROL, HELP_SCOPE_GSTCTRL_WAITRUNLEVEL,
3145 GuestCtrl::tr("Invalid run level specified. Valid values are: system, userland, desktop"));
3146 break;
3147 }
3148
3149 default:
3150 return errorGetOptEx(USAGE_GUESTCONTROL, HELP_SCOPE_GSTCTRL_WAITRUNLEVEL, ch, &ValueUnion);
3151 }
3152 }
3153
3154 RTEXITCODE rcExit = gctlCtxPostOptionParsingInit(pCtx);
3155 if (rcExit != RTEXITCODE_SUCCESS)
3156 return rcExit;
3157
3158 if (enmRunLevel == AdditionsRunLevelType_None)
3159 return errorSyntaxEx(USAGE_GUESTCONTROL, HELP_SCOPE_GSTCTRL_WAITRUNLEVEL, GuestCtrl::tr("Missing run level to wait for"));
3160
3161 vrc = gctlWaitForRunLevel(pCtx, enmRunLevel, cMsTimeout);
3162
3163 return RT_SUCCESS(vrc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
3164}
3165
3166static DECLCALLBACK(RTEXITCODE) gctlHandleList(PGCTLCMDCTX pCtx, int argc, char **argv)
3167{
3168 AssertPtrReturn(pCtx, RTEXITCODE_FAILURE);
3169
3170 static const RTGETOPTDEF s_aOptions[] =
3171 {
3172 GCTLCMD_COMMON_OPTION_DEFS()
3173 };
3174
3175 int ch;
3176 RTGETOPTUNION ValueUnion;
3177 RTGETOPTSTATE GetState;
3178 RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, RTGETOPTINIT_FLAGS_OPTS_FIRST);
3179
3180 bool fSeenListArg = false;
3181 bool fListAll = false;
3182 bool fListSessions = false;
3183 bool fListProcesses = false;
3184 bool fListFiles = false;
3185
3186 int vrc = VINF_SUCCESS;
3187 while ( (ch = RTGetOpt(&GetState, &ValueUnion))
3188 && RT_SUCCESS(vrc))
3189 {
3190 switch (ch)
3191 {
3192 GCTLCMD_COMMON_OPTION_CASES(pCtx, ch, &ValueUnion);
3193
3194 case VINF_GETOPT_NOT_OPTION:
3195 if ( !RTStrICmp(ValueUnion.psz, "sessions")
3196 || !RTStrICmp(ValueUnion.psz, "sess"))
3197 fListSessions = true;
3198 else if ( !RTStrICmp(ValueUnion.psz, "processes")
3199 || !RTStrICmp(ValueUnion.psz, "procs"))
3200 fListSessions = fListProcesses = true; /* Showing processes implies showing sessions. */
3201 else if (!RTStrICmp(ValueUnion.psz, "files"))
3202 fListSessions = fListFiles = true; /* Showing files implies showing sessions. */
3203 else if (!RTStrICmp(ValueUnion.psz, "all"))
3204 fListAll = true;
3205 else
3206 return errorSyntaxEx(USAGE_GUESTCONTROL, HELP_SCOPE_GSTCTRL_LIST,
3207 GuestCtrl::tr("Unknown list: '%s'"), ValueUnion.psz);
3208 fSeenListArg = true;
3209 break;
3210
3211 default:
3212 return errorGetOptEx(USAGE_GUESTCONTROL, HELP_SCOPE_GSTCTRL_UPDATEGA, ch, &ValueUnion);
3213 }
3214 }
3215
3216 if (!fSeenListArg)
3217 return errorSyntaxEx(USAGE_GUESTCONTROL, HELP_SCOPE_GSTCTRL_LIST, GuestCtrl::tr("Missing list name"));
3218 Assert(fListAll || fListSessions);
3219
3220 RTEXITCODE rcExit = gctlCtxPostOptionParsingInit(pCtx);
3221 if (rcExit != RTEXITCODE_SUCCESS)
3222 return rcExit;
3223
3224
3225 /** @todo Do we need a machine-readable output here as well? */
3226
3227 HRESULT rc;
3228 size_t cTotalProcs = 0;
3229 size_t cTotalFiles = 0;
3230
3231 SafeIfaceArray <IGuestSession> collSessions;
3232 CHECK_ERROR(pCtx->pGuest, COMGETTER(Sessions)(ComSafeArrayAsOutParam(collSessions)));
3233 if (SUCCEEDED(rc))
3234 {
3235 size_t const cSessions = collSessions.size();
3236 if (cSessions)
3237 {
3238 RTPrintf(GuestCtrl::tr("Active guest sessions:\n"));
3239
3240 /** @todo Make this output a bit prettier. No time now. */
3241
3242 for (size_t i = 0; i < cSessions; i++)
3243 {
3244 ComPtr<IGuestSession> pCurSession = collSessions[i];
3245 if (!pCurSession.isNull())
3246 {
3247 do
3248 {
3249 ULONG uID;
3250 CHECK_ERROR_BREAK(pCurSession, COMGETTER(Id)(&uID));
3251 Bstr strName;
3252 CHECK_ERROR_BREAK(pCurSession, COMGETTER(Name)(strName.asOutParam()));
3253 Bstr strUser;
3254 CHECK_ERROR_BREAK(pCurSession, COMGETTER(User)(strUser.asOutParam()));
3255 GuestSessionStatus_T sessionStatus;
3256 CHECK_ERROR_BREAK(pCurSession, COMGETTER(Status)(&sessionStatus));
3257 RTPrintf(GuestCtrl::tr("\n\tSession #%-3zu ID=%-3RU32 User=%-16ls Status=[%s] Name=%ls"),
3258 i, uID, strUser.raw(), gctlGuestSessionStatusToText(sessionStatus), strName.raw());
3259 } while (0);
3260
3261 if ( fListAll
3262 || fListProcesses)
3263 {
3264 SafeIfaceArray <IGuestProcess> collProcesses;
3265 CHECK_ERROR_BREAK(pCurSession, COMGETTER(Processes)(ComSafeArrayAsOutParam(collProcesses)));
3266 for (size_t a = 0; a < collProcesses.size(); a++)
3267 {
3268 ComPtr<IGuestProcess> pCurProcess = collProcesses[a];
3269 if (!pCurProcess.isNull())
3270 {
3271 do
3272 {
3273 ULONG uPID;
3274 CHECK_ERROR_BREAK(pCurProcess, COMGETTER(PID)(&uPID));
3275 Bstr strExecPath;
3276 CHECK_ERROR_BREAK(pCurProcess, COMGETTER(ExecutablePath)(strExecPath.asOutParam()));
3277 ProcessStatus_T procStatus;
3278 CHECK_ERROR_BREAK(pCurProcess, COMGETTER(Status)(&procStatus));
3279
3280 RTPrintf(GuestCtrl::tr("\n\t\tProcess #%-03zu PID=%-6RU32 Status=[%s] Command=%ls"),
3281 a, uPID, gctlProcessStatusToText(procStatus), strExecPath.raw());
3282 } while (0);
3283 }
3284 }
3285
3286 cTotalProcs += collProcesses.size();
3287 }
3288
3289 if ( fListAll
3290 || fListFiles)
3291 {
3292 SafeIfaceArray <IGuestFile> collFiles;
3293 CHECK_ERROR_BREAK(pCurSession, COMGETTER(Files)(ComSafeArrayAsOutParam(collFiles)));
3294 for (size_t a = 0; a < collFiles.size(); a++)
3295 {
3296 ComPtr<IGuestFile> pCurFile = collFiles[a];
3297 if (!pCurFile.isNull())
3298 {
3299 do
3300 {
3301 ULONG idFile;
3302 CHECK_ERROR_BREAK(pCurFile, COMGETTER(Id)(&idFile));
3303 Bstr strName;
3304 CHECK_ERROR_BREAK(pCurFile, COMGETTER(Filename)(strName.asOutParam()));
3305 FileStatus_T fileStatus;
3306 CHECK_ERROR_BREAK(pCurFile, COMGETTER(Status)(&fileStatus));
3307
3308 RTPrintf(GuestCtrl::tr("\n\t\tFile #%-03zu ID=%-6RU32 Status=[%s] Name=%ls"),
3309 a, idFile, gctlFileStatusToText(fileStatus), strName.raw());
3310 } while (0);
3311 }
3312 }
3313
3314 cTotalFiles += collFiles.size();
3315 }
3316 }
3317 }
3318
3319 RTPrintf(GuestCtrl::tr("\n\nTotal guest sessions: %zu\n"), collSessions.size());
3320 if (fListAll || fListProcesses)
3321 RTPrintf(GuestCtrl::tr("Total guest processes: %zu\n"), cTotalProcs);
3322 if (fListAll || fListFiles)
3323 RTPrintf(GuestCtrl::tr("Total guest files: %zu\n"), cTotalFiles);
3324 }
3325 else
3326 RTPrintf(GuestCtrl::tr("No active guest sessions found\n"));
3327 }
3328
3329 if (FAILED(rc)) /** @todo yeah, right... Only the last error? */
3330 rcExit = RTEXITCODE_FAILURE;
3331
3332 return rcExit;
3333}
3334
3335static DECLCALLBACK(RTEXITCODE) gctlHandleCloseProcess(PGCTLCMDCTX pCtx, int argc, char **argv)
3336{
3337 AssertPtrReturn(pCtx, RTEXITCODE_FAILURE);
3338
3339 static const RTGETOPTDEF s_aOptions[] =
3340 {
3341 GCTLCMD_COMMON_OPTION_DEFS()
3342 { "--session-id", 'i', RTGETOPT_REQ_UINT32 },
3343 { "--session-name", 'n', RTGETOPT_REQ_STRING }
3344 };
3345
3346 int ch;
3347 RTGETOPTUNION ValueUnion;
3348 RTGETOPTSTATE GetState;
3349 RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, RTGETOPTINIT_FLAGS_OPTS_FIRST);
3350
3351 std::vector < uint32_t > vecPID;
3352 ULONG idSession = UINT32_MAX;
3353 Utf8Str strSessionName;
3354
3355 while ((ch = RTGetOpt(&GetState, &ValueUnion)) != 0)
3356 {
3357 /* For options that require an argument, ValueUnion has received the value. */
3358 switch (ch)
3359 {
3360 GCTLCMD_COMMON_OPTION_CASES(pCtx, ch, &ValueUnion);
3361
3362 case 'n': /* Session name (or pattern) */
3363 strSessionName = ValueUnion.psz;
3364 break;
3365
3366 case 'i': /* Session ID */
3367 idSession = ValueUnion.u32;
3368 break;
3369
3370 case VINF_GETOPT_NOT_OPTION:
3371 {
3372 /* Treat every else specified as a PID to kill. */
3373 uint32_t uPid;
3374 int rc = RTStrToUInt32Ex(ValueUnion.psz, NULL, 0, &uPid);
3375 if ( RT_SUCCESS(rc)
3376 && rc != VWRN_TRAILING_CHARS
3377 && rc != VWRN_NUMBER_TOO_BIG
3378 && rc != VWRN_NEGATIVE_UNSIGNED)
3379 {
3380 if (uPid != 0)
3381 {
3382 try
3383 {
3384 vecPID.push_back(uPid);
3385 }
3386 catch (std::bad_alloc &)
3387 {
3388 return RTMsgErrorExit(RTEXITCODE_FAILURE, GuestCtrl::tr("Out of memory"));
3389 }
3390 }
3391 else
3392 return errorSyntaxEx(USAGE_GUESTCONTROL, HELP_SCOPE_GSTCTRL_CLOSEPROCESS,
3393 GuestCtrl::tr("Invalid PID value: 0"));
3394 }
3395 else
3396 return errorSyntaxEx(USAGE_GUESTCONTROL, HELP_SCOPE_GSTCTRL_CLOSEPROCESS,
3397 GuestCtrl::tr("Error parsing PID value: %Rrc"), rc);
3398 break;
3399 }
3400
3401 default:
3402 return errorGetOptEx(USAGE_GUESTCONTROL, HELP_SCOPE_GSTCTRL_CLOSEPROCESS, ch, &ValueUnion);
3403 }
3404 }
3405
3406 if (vecPID.empty())
3407 return errorSyntaxEx(USAGE_GUESTCONTROL, HELP_SCOPE_GSTCTRL_CLOSEPROCESS,
3408 GuestCtrl::tr("At least one PID must be specified to kill!"));
3409
3410 if ( strSessionName.isEmpty()
3411 && idSession == UINT32_MAX)
3412 return errorSyntaxEx(USAGE_GUESTCONTROL, HELP_SCOPE_GSTCTRL_CLOSEPROCESS, GuestCtrl::tr("No session ID specified!"));
3413
3414 if ( strSessionName.isNotEmpty()
3415 && idSession != UINT32_MAX)
3416 return errorSyntaxEx(USAGE_GUESTCONTROL, HELP_SCOPE_GSTCTRL_CLOSEPROCESS,
3417 GuestCtrl::tr("Either session ID or name (pattern) must be specified"));
3418
3419 RTEXITCODE rcExit = gctlCtxPostOptionParsingInit(pCtx);
3420 if (rcExit != RTEXITCODE_SUCCESS)
3421 return rcExit;
3422
3423 HRESULT rc = S_OK;
3424
3425 ComPtr<IGuestSession> pSession;
3426 ComPtr<IGuestProcess> pProcess;
3427 do
3428 {
3429 uint32_t uProcsTerminated = 0;
3430
3431 SafeIfaceArray <IGuestSession> collSessions;
3432 CHECK_ERROR_BREAK(pCtx->pGuest, COMGETTER(Sessions)(ComSafeArrayAsOutParam(collSessions)));
3433 size_t cSessions = collSessions.size();
3434
3435 uint32_t cSessionsHandled = 0;
3436 for (size_t i = 0; i < cSessions; i++)
3437 {
3438 pSession = collSessions[i];
3439 Assert(!pSession.isNull());
3440
3441 ULONG uID; /* Session ID */
3442 CHECK_ERROR_BREAK(pSession, COMGETTER(Id)(&uID));
3443 Bstr strName;
3444 CHECK_ERROR_BREAK(pSession, COMGETTER(Name)(strName.asOutParam()));
3445 Utf8Str strNameUtf8(strName); /* Session name */
3446
3447 bool fSessionFound;
3448 if (strSessionName.isEmpty()) /* Search by ID. Slow lookup. */
3449 fSessionFound = uID == idSession;
3450 else /* ... or by naming pattern. */
3451 fSessionFound = RTStrSimplePatternMatch(strSessionName.c_str(), strNameUtf8.c_str());
3452 if (fSessionFound)
3453 {
3454 AssertStmt(!pSession.isNull(), break);
3455 cSessionsHandled++;
3456
3457 SafeIfaceArray <IGuestProcess> collProcs;
3458 CHECK_ERROR_BREAK(pSession, COMGETTER(Processes)(ComSafeArrayAsOutParam(collProcs)));
3459
3460 size_t cProcs = collProcs.size();
3461 for (size_t p = 0; p < cProcs; p++)
3462 {
3463 pProcess = collProcs[p];
3464 Assert(!pProcess.isNull());
3465
3466 ULONG uPID; /* Process ID */
3467 CHECK_ERROR_BREAK(pProcess, COMGETTER(PID)(&uPID));
3468
3469 bool fProcFound = false;
3470 for (size_t a = 0; a < vecPID.size(); a++) /* Slow, but works. */
3471 {
3472 fProcFound = vecPID[a] == uPID;
3473 if (fProcFound)
3474 break;
3475 }
3476
3477 if (fProcFound)
3478 {
3479 if (pCtx->cVerbose)
3480 RTPrintf(GuestCtrl::tr("Terminating process (PID %RU32) (session ID %RU32) ...\n"),
3481 uPID, uID);
3482 CHECK_ERROR_BREAK(pProcess, Terminate());
3483 uProcsTerminated++;
3484 }
3485 else
3486 {
3487 if (idSession != UINT32_MAX)
3488 RTPrintf(GuestCtrl::tr("No matching process(es) for session ID %RU32 found\n"),
3489 idSession);
3490 }
3491
3492 pProcess.setNull();
3493 }
3494
3495 pSession.setNull();
3496 }
3497 }
3498
3499 if (!cSessionsHandled)
3500 RTPrintf(GuestCtrl::tr("No matching session(s) found\n"));
3501
3502 if (uProcsTerminated)
3503 RTPrintf(GuestCtrl::tr("%RU32 process(es) terminated\n", "", uProcsTerminated), uProcsTerminated);
3504
3505 } while (0);
3506
3507 pProcess.setNull();
3508 pSession.setNull();
3509
3510 return SUCCEEDED(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
3511}
3512
3513
3514static DECLCALLBACK(RTEXITCODE) gctlHandleCloseSession(PGCTLCMDCTX pCtx, int argc, char **argv)
3515{
3516 AssertPtrReturn(pCtx, RTEXITCODE_FAILURE);
3517
3518 enum GETOPTDEF_SESSIONCLOSE
3519 {
3520 GETOPTDEF_SESSIONCLOSE_ALL = 2000
3521 };
3522 static const RTGETOPTDEF s_aOptions[] =
3523 {
3524 GCTLCMD_COMMON_OPTION_DEFS()
3525 { "--all", GETOPTDEF_SESSIONCLOSE_ALL, RTGETOPT_REQ_NOTHING },
3526 { "--session-id", 'i', RTGETOPT_REQ_UINT32 },
3527 { "--session-name", 'n', RTGETOPT_REQ_STRING }
3528 };
3529
3530 int ch;
3531 RTGETOPTUNION ValueUnion;
3532 RTGETOPTSTATE GetState;
3533 RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, RTGETOPTINIT_FLAGS_OPTS_FIRST);
3534
3535 ULONG idSession = UINT32_MAX;
3536 Utf8Str strSessionName;
3537
3538 while ((ch = RTGetOpt(&GetState, &ValueUnion)) != 0)
3539 {
3540 /* For options that require an argument, ValueUnion has received the value. */
3541 switch (ch)
3542 {
3543 GCTLCMD_COMMON_OPTION_CASES(pCtx, ch, &ValueUnion);
3544
3545 case 'n': /* Session name pattern */
3546 strSessionName = ValueUnion.psz;
3547 break;
3548
3549 case 'i': /* Session ID */
3550 idSession = ValueUnion.u32;
3551 break;
3552
3553 case GETOPTDEF_SESSIONCLOSE_ALL:
3554 strSessionName = "*";
3555 break;
3556
3557 case VINF_GETOPT_NOT_OPTION:
3558 /** @todo Supply a CSV list of IDs or patterns to close?
3559 * break; */
3560 default:
3561 return errorGetOptEx(USAGE_GUESTCONTROL, HELP_SCOPE_GSTCTRL_CLOSESESSION, ch, &ValueUnion);
3562 }
3563 }
3564
3565 if ( strSessionName.isEmpty()
3566 && idSession == UINT32_MAX)
3567 return errorSyntaxEx(USAGE_GUESTCONTROL, HELP_SCOPE_GSTCTRL_CLOSESESSION,
3568 GuestCtrl::tr("No session ID specified!"));
3569
3570 if ( !strSessionName.isEmpty()
3571 && idSession != UINT32_MAX)
3572 return errorSyntaxEx(USAGE_GUESTCONTROL, HELP_SCOPE_GSTCTRL_CLOSESESSION,
3573 GuestCtrl::tr("Either session ID or name (pattern) must be specified"));
3574
3575 RTEXITCODE rcExit = gctlCtxPostOptionParsingInit(pCtx);
3576 if (rcExit != RTEXITCODE_SUCCESS)
3577 return rcExit;
3578
3579 HRESULT rc = S_OK;
3580
3581 do
3582 {
3583 size_t cSessionsHandled = 0;
3584
3585 SafeIfaceArray <IGuestSession> collSessions;
3586 CHECK_ERROR_BREAK(pCtx->pGuest, COMGETTER(Sessions)(ComSafeArrayAsOutParam(collSessions)));
3587 size_t cSessions = collSessions.size();
3588
3589 for (size_t i = 0; i < cSessions; i++)
3590 {
3591 ComPtr<IGuestSession> pSession = collSessions[i];
3592 Assert(!pSession.isNull());
3593
3594 ULONG uID; /* Session ID */
3595 CHECK_ERROR_BREAK(pSession, COMGETTER(Id)(&uID));
3596 Bstr strName;
3597 CHECK_ERROR_BREAK(pSession, COMGETTER(Name)(strName.asOutParam()));
3598 Utf8Str strNameUtf8(strName); /* Session name */
3599
3600 bool fSessionFound;
3601 if (strSessionName.isEmpty()) /* Search by ID. Slow lookup. */
3602 fSessionFound = uID == idSession;
3603 else /* ... or by naming pattern. */
3604 fSessionFound = RTStrSimplePatternMatch(strSessionName.c_str(), strNameUtf8.c_str());
3605 if (fSessionFound)
3606 {
3607 cSessionsHandled++;
3608
3609 Assert(!pSession.isNull());
3610 if (pCtx->cVerbose)
3611 RTPrintf(GuestCtrl::tr("Closing guest session ID=#%RU32 \"%s\" ...\n"),
3612 uID, strNameUtf8.c_str());
3613 CHECK_ERROR_BREAK(pSession, Close());
3614 if (pCtx->cVerbose)
3615 RTPrintf(GuestCtrl::tr("Guest session successfully closed\n"));
3616
3617 pSession.setNull();
3618 }
3619 }
3620
3621 if (!cSessionsHandled)
3622 {
3623 RTPrintf(GuestCtrl::tr("No guest session(s) found\n"));
3624 rc = E_ABORT; /* To set exit code accordingly. */
3625 }
3626
3627 } while (0);
3628
3629 return SUCCEEDED(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
3630}
3631
3632
3633static DECLCALLBACK(RTEXITCODE) gctlHandleWatch(PGCTLCMDCTX pCtx, int argc, char **argv)
3634{
3635 AssertPtrReturn(pCtx, RTEXITCODE_FAILURE);
3636
3637 /*
3638 * Parse arguments.
3639 */
3640 static const RTGETOPTDEF s_aOptions[] =
3641 {
3642 GCTLCMD_COMMON_OPTION_DEFS()
3643 { "--timeout", 't', RTGETOPT_REQ_UINT32 }
3644 };
3645
3646 uint32_t cMsTimeout = RT_INDEFINITE_WAIT;
3647
3648 int ch;
3649 RTGETOPTUNION ValueUnion;
3650 RTGETOPTSTATE GetState;
3651 RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, RTGETOPTINIT_FLAGS_OPTS_FIRST);
3652
3653 while ((ch = RTGetOpt(&GetState, &ValueUnion)) != 0)
3654 {
3655 /* For options that require an argument, ValueUnion has received the value. */
3656 switch (ch)
3657 {
3658 GCTLCMD_COMMON_OPTION_CASES(pCtx, ch, &ValueUnion);
3659
3660 case 't': /* Timeout */
3661 cMsTimeout = ValueUnion.u32;
3662 break;
3663
3664 case VINF_GETOPT_NOT_OPTION:
3665 default:
3666 return errorGetOptEx(USAGE_GUESTCONTROL, HELP_SCOPE_GSTCTRL_WATCH, ch, &ValueUnion);
3667 }
3668 }
3669
3670 /** @todo Specify categories to watch for. */
3671 /** @todo Specify a --timeout for waiting only for a certain amount of time? */
3672
3673 RTEXITCODE rcExit = gctlCtxPostOptionParsingInit(pCtx);
3674 if (rcExit != RTEXITCODE_SUCCESS)
3675 return rcExit;
3676
3677 HRESULT rc;
3678
3679 try
3680 {
3681 ComObjPtr<GuestEventListenerImpl> pGuestListener;
3682 do
3683 {
3684 /* Listener creation. */
3685 pGuestListener.createObject();
3686 pGuestListener->init(new GuestEventListener());
3687
3688 /* Register for IGuest events. */
3689 ComPtr<IEventSource> es;
3690 CHECK_ERROR_BREAK(pCtx->pGuest, COMGETTER(EventSource)(es.asOutParam()));
3691 com::SafeArray<VBoxEventType_T> eventTypes;
3692 eventTypes.push_back(VBoxEventType_OnGuestSessionRegistered);
3693 /** @todo Also register for VBoxEventType_OnGuestUserStateChanged on demand? */
3694 CHECK_ERROR_BREAK(es, RegisterListener(pGuestListener, ComSafeArrayAsInParam(eventTypes),
3695 true /* Active listener */));
3696 /* Note: All other guest control events have to be registered
3697 * as their corresponding objects appear. */
3698
3699 } while (0);
3700
3701 if (pCtx->cVerbose)
3702 RTPrintf(GuestCtrl::tr("Waiting for events ...\n"));
3703
3704 /* Wait for the global signal semaphore getting signalled. */
3705 int vrc = RTSemEventWait(g_SemEventGuestCtrlCanceled, cMsTimeout);
3706 if (vrc == VERR_TIMEOUT)
3707 {
3708 if (pCtx->cVerbose)
3709 RTPrintf(GuestCtrl::tr("Waiting done\n"));
3710 }
3711 else if (RT_FAILURE(vrc))
3712 RTPrintf(GuestCtrl::tr("Waiting failed with %Rrc\n"), vrc);
3713
3714 if (!pGuestListener.isNull())
3715 {
3716 /* Guest callback unregistration. */
3717 ComPtr<IEventSource> pES;
3718 CHECK_ERROR(pCtx->pGuest, COMGETTER(EventSource)(pES.asOutParam()));
3719 if (!pES.isNull())
3720 CHECK_ERROR(pES, UnregisterListener(pGuestListener));
3721 pGuestListener.setNull();
3722 }
3723 }
3724 catch (std::bad_alloc &)
3725 {
3726 rc = E_OUTOFMEMORY;
3727 }
3728
3729 return SUCCEEDED(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
3730}
3731
3732/**
3733 * Access the guest control store.
3734 *
3735 * @returns program exit code.
3736 * @note see the command line API description for parameters
3737 */
3738RTEXITCODE handleGuestControl(HandlerArg *pArg)
3739{
3740 AssertPtr(pArg);
3741
3742#ifdef DEBUG_andy_disabled
3743 if (RT_FAILURE(tstTranslatePath()))
3744 return RTEXITCODE_FAILURE;
3745#endif
3746
3747 /*
3748 * Command definitions.
3749 */
3750 static const GCTLCMDDEF s_aCmdDefs[] =
3751 {
3752 { "run", gctlHandleRun, HELP_SCOPE_GSTCTRL_RUN, 0 },
3753 { "start", gctlHandleStart, HELP_SCOPE_GSTCTRL_START, 0 },
3754 { "copyfrom", gctlHandleCopyFrom, HELP_SCOPE_GSTCTRL_COPYFROM, 0 },
3755 { "copyto", gctlHandleCopyTo, HELP_SCOPE_GSTCTRL_COPYTO, 0 },
3756
3757 { "mkdir", gctrlHandleMkDir, HELP_SCOPE_GSTCTRL_MKDIR, 0 },
3758 { "md", gctrlHandleMkDir, HELP_SCOPE_GSTCTRL_MKDIR, 0 },
3759 { "createdirectory", gctrlHandleMkDir, HELP_SCOPE_GSTCTRL_MKDIR, 0 },
3760 { "createdir", gctrlHandleMkDir, HELP_SCOPE_GSTCTRL_MKDIR, 0 },
3761
3762 { "rmdir", gctlHandleRmDir, HELP_SCOPE_GSTCTRL_RMDIR, 0 },
3763 { "removedir", gctlHandleRmDir, HELP_SCOPE_GSTCTRL_RMDIR, 0 },
3764 { "removedirectory", gctlHandleRmDir, HELP_SCOPE_GSTCTRL_RMDIR, 0 },
3765
3766 { "rm", gctlHandleRm, HELP_SCOPE_GSTCTRL_RM, 0 },
3767 { "removefile", gctlHandleRm, HELP_SCOPE_GSTCTRL_RM, 0 },
3768 { "erase", gctlHandleRm, HELP_SCOPE_GSTCTRL_RM, 0 },
3769 { "del", gctlHandleRm, HELP_SCOPE_GSTCTRL_RM, 0 },
3770 { "delete", gctlHandleRm, HELP_SCOPE_GSTCTRL_RM, 0 },
3771
3772 { "mv", gctlHandleMv, HELP_SCOPE_GSTCTRL_MV, 0 },
3773 { "move", gctlHandleMv, HELP_SCOPE_GSTCTRL_MV, 0 },
3774 { "ren", gctlHandleMv, HELP_SCOPE_GSTCTRL_MV, 0 },
3775 { "rename", gctlHandleMv, HELP_SCOPE_GSTCTRL_MV, 0 },
3776
3777 { "mktemp", gctlHandleMkTemp, HELP_SCOPE_GSTCTRL_MKTEMP, 0 },
3778 { "createtemp", gctlHandleMkTemp, HELP_SCOPE_GSTCTRL_MKTEMP, 0 },
3779 { "createtemporary", gctlHandleMkTemp, HELP_SCOPE_GSTCTRL_MKTEMP, 0 },
3780
3781 { "stat", gctlHandleStat, HELP_SCOPE_GSTCTRL_STAT, 0 },
3782
3783 { "closeprocess", gctlHandleCloseProcess, HELP_SCOPE_GSTCTRL_CLOSEPROCESS, GCTLCMDCTX_F_SESSION_ANONYMOUS | GCTLCMDCTX_F_NO_SIGNAL_HANDLER },
3784 { "closesession", gctlHandleCloseSession, HELP_SCOPE_GSTCTRL_CLOSESESSION, GCTLCMDCTX_F_SESSION_ANONYMOUS | GCTLCMDCTX_F_NO_SIGNAL_HANDLER },
3785 { "list", gctlHandleList, HELP_SCOPE_GSTCTRL_LIST, GCTLCMDCTX_F_SESSION_ANONYMOUS | GCTLCMDCTX_F_NO_SIGNAL_HANDLER },
3786 { "watch", gctlHandleWatch, HELP_SCOPE_GSTCTRL_WATCH, GCTLCMDCTX_F_SESSION_ANONYMOUS | GCTLCMDCTX_F_NO_SIGNAL_HANDLER },
3787
3788 {"updateguestadditions",gctlHandleUpdateAdditions, HELP_SCOPE_GSTCTRL_UPDATEGA, GCTLCMDCTX_F_SESSION_ANONYMOUS },
3789 { "updateadditions", gctlHandleUpdateAdditions, HELP_SCOPE_GSTCTRL_UPDATEGA, GCTLCMDCTX_F_SESSION_ANONYMOUS },
3790 { "updatega", gctlHandleUpdateAdditions, HELP_SCOPE_GSTCTRL_UPDATEGA, GCTLCMDCTX_F_SESSION_ANONYMOUS },
3791
3792 { "waitrunlevel", gctlHandleWaitRunLevel, HELP_SCOPE_GSTCTRL_WAITRUNLEVEL, GCTLCMDCTX_F_SESSION_ANONYMOUS },
3793 { "waitforrunlevel", gctlHandleWaitRunLevel, HELP_SCOPE_GSTCTRL_WAITRUNLEVEL, GCTLCMDCTX_F_SESSION_ANONYMOUS },
3794 };
3795
3796 /*
3797 * VBoxManage guestcontrol [common-options] <VM> [common-options] <sub-command> ...
3798 *
3799 * Parse common options and VM name until we find a sub-command. Allowing
3800 * the user to put the user and password related options before the
3801 * sub-command makes it easier to edit the command line when doing several
3802 * operations with the same guest user account. (Accidentally, it also
3803 * makes the syntax diagram shorter and easier to read.)
3804 */
3805 GCTLCMDCTX CmdCtx;
3806 RTEXITCODE rcExit = gctrCmdCtxInit(&CmdCtx, pArg);
3807 if (rcExit == RTEXITCODE_SUCCESS)
3808 {
3809 static const RTGETOPTDEF s_CommonOptions[] = { GCTLCMD_COMMON_OPTION_DEFS() };
3810
3811 int ch;
3812 RTGETOPTUNION ValueUnion;
3813 RTGETOPTSTATE GetState;
3814 RTGetOptInit(&GetState, pArg->argc, pArg->argv, s_CommonOptions, RT_ELEMENTS(s_CommonOptions), 0, 0 /* No sorting! */);
3815
3816 while ((ch = RTGetOpt(&GetState, &ValueUnion)) != 0)
3817 {
3818 switch (ch)
3819 {
3820 GCTLCMD_COMMON_OPTION_CASES(&CmdCtx, ch, &ValueUnion);
3821
3822 case VINF_GETOPT_NOT_OPTION:
3823 /* First comes the VM name or UUID. */
3824 if (!CmdCtx.pszVmNameOrUuid)
3825 CmdCtx.pszVmNameOrUuid = ValueUnion.psz;
3826 /*
3827 * The sub-command is next. Look it up and invoke it.
3828 * Note! Currently no warnings about user/password options (like we'll do later on)
3829 * for GCTLCMDCTX_F_SESSION_ANONYMOUS commands. No reason to be too pedantic.
3830 */
3831 else
3832 {
3833 const char *pszCmd = ValueUnion.psz;
3834 uint32_t iCmd;
3835 for (iCmd = 0; iCmd < RT_ELEMENTS(s_aCmdDefs); iCmd++)
3836 if (strcmp(s_aCmdDefs[iCmd].pszName, pszCmd) == 0)
3837 {
3838 CmdCtx.pCmdDef = &s_aCmdDefs[iCmd];
3839
3840 rcExit = s_aCmdDefs[iCmd].pfnHandler(&CmdCtx, pArg->argc - GetState.iNext + 1,
3841 &pArg->argv[GetState.iNext - 1]);
3842
3843 gctlCtxTerm(&CmdCtx);
3844 return rcExit;
3845 }
3846 return errorSyntax(USAGE_GUESTCONTROL, GuestCtrl::tr("Unknown sub-command: '%s'"), pszCmd);
3847 }
3848 break;
3849
3850 default:
3851 return errorGetOpt(USAGE_GUESTCONTROL, ch, &ValueUnion);
3852 }
3853 }
3854 if (CmdCtx.pszVmNameOrUuid)
3855 rcExit = errorSyntax(USAGE_GUESTCONTROL, GuestCtrl::tr("Missing sub-command"));
3856 else
3857 rcExit = errorSyntax(USAGE_GUESTCONTROL, GuestCtrl::tr("Missing VM name and sub-command"));
3858 }
3859 return rcExit;
3860}
3861#endif /* !VBOX_ONLY_DOCS */
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