VirtualBox

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

Last change on this file since 52312 was 51628, checked in by vboxsync, 10 years ago

Frontend/VBoxManage: fix gcc compiler warning in VBoxManageGuestCtrl.cpp

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 142.5 KB
Line 
1/* $Id: VBoxManageGuestCtrl.cpp 51628 2014-06-17 11:47:28Z vboxsync $ */
2/** @file
3 * VBoxManage - Implementation of guestcontrol command.
4 */
5
6/*
7 * Copyright (C) 2010-2013 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/isofs.h>
43#include <iprt/getopt.h>
44#include <iprt/list.h>
45#include <iprt/path.h>
46#include <iprt/process.h> /* For RTProcSelf(). */
47#include <iprt/thread.h>
48
49#include <map>
50#include <vector>
51
52#ifdef USE_XPCOM_QUEUE
53# include <sys/select.h>
54# include <errno.h>
55#endif
56
57#include <signal.h>
58
59#ifdef RT_OS_DARWIN
60# include <CoreFoundation/CFRunLoop.h>
61#endif
62
63using namespace com;
64
65/** Set by the signal handler when current guest control
66 * action shall be aborted. */
67static volatile bool g_fGuestCtrlCanceled = false;
68
69/**
70 * Listener declarations.
71 */
72VBOX_LISTENER_DECLARE(GuestFileEventListenerImpl)
73VBOX_LISTENER_DECLARE(GuestProcessEventListenerImpl)
74VBOX_LISTENER_DECLARE(GuestSessionEventListenerImpl)
75VBOX_LISTENER_DECLARE(GuestEventListenerImpl)
76
77/**
78 * Command context flags.
79 */
80/** No flags set. */
81#define CTLCMDCTX_FLAGS_NONE 0
82/** Don't install a signal handler (CTRL+C trap). */
83#define CTLCMDCTX_FLAGS_NO_SIGNAL_HANDLER RT_BIT(0)
84/** No guest session needed. */
85#define CTLCMDCTX_FLAGS_SESSION_ANONYMOUS RT_BIT(1)
86/** Detach the guest session. That is, don't close the
87 * guest session automatically on exit. */
88#define CTLCMDCTX_FLAGS_SESSION_DETACH RT_BIT(2)
89
90/**
91 * Context for handling a specific command.
92 */
93typedef struct GCTLCMDCTX
94{
95 HandlerArg handlerArg;
96 /** Command-specific argument count. */
97 int iArgc;
98 /** Command-specific argument vector. */
99 char **ppaArgv;
100 /** First argv to start parsing with. */
101 int iFirstArgc;
102 /** Command context flags. */
103 uint32_t uFlags;
104 /** Verbose flag. */
105 bool fVerbose;
106 /** User name. */
107 Utf8Str strUsername;
108 /** Password. */
109 Utf8Str strPassword;
110 /** Domain. */
111 Utf8Str strDomain;
112 /** Pointer to the IGuest interface. */
113 ComPtr<IGuest> pGuest;
114 /** Pointer to the to be used guest session. */
115 ComPtr<IGuestSession> pGuestSession;
116 /** The guest session ID. */
117 ULONG uSessionID;
118
119} GCTLCMDCTX, *PGCTLCMDCTX;
120
121typedef struct GCTLCMD
122{
123 /**
124 * Actual command handler callback.
125 *
126 * @param pCtx Pointer to command context to use.
127 */
128 DECLR3CALLBACKMEMBER(RTEXITCODE, pfnHandler, (PGCTLCMDCTX pCtx));
129
130} GCTLCMD, *PGCTLCMD;
131
132typedef struct COPYCONTEXT
133{
134 COPYCONTEXT()
135 : fDryRun(false),
136 fHostToGuest(false)
137 {
138 }
139
140 PGCTLCMDCTX pCmdCtx;
141 bool fDryRun;
142 bool fHostToGuest;
143
144} COPYCONTEXT, *PCOPYCONTEXT;
145
146/**
147 * An entry for a source element, including an optional DOS-like wildcard (*,?).
148 */
149class SOURCEFILEENTRY
150{
151 public:
152
153 SOURCEFILEENTRY(const char *pszSource, const char *pszFilter)
154 : mSource(pszSource),
155 mFilter(pszFilter) {}
156
157 SOURCEFILEENTRY(const char *pszSource)
158 : mSource(pszSource)
159 {
160 Parse(pszSource);
161 }
162
163 const char* GetSource() const
164 {
165 return mSource.c_str();
166 }
167
168 const char* GetFilter() const
169 {
170 return mFilter.c_str();
171 }
172
173 private:
174
175 int Parse(const char *pszPath)
176 {
177 AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
178
179 if ( !RTFileExists(pszPath)
180 && !RTDirExists(pszPath))
181 {
182 /* No file and no directory -- maybe a filter? */
183 char *pszFilename = RTPathFilename(pszPath);
184 if ( pszFilename
185 && strpbrk(pszFilename, "*?"))
186 {
187 /* Yep, get the actual filter part. */
188 mFilter = RTPathFilename(pszPath);
189 /* Remove the filter from actual sourcec directory name. */
190 RTPathStripFilename(mSource.mutableRaw());
191 mSource.jolt();
192 }
193 }
194
195 return VINF_SUCCESS; /* @todo */
196 }
197
198 private:
199
200 Utf8Str mSource;
201 Utf8Str mFilter;
202};
203typedef std::vector<SOURCEFILEENTRY> SOURCEVEC, *PSOURCEVEC;
204
205/**
206 * An entry for an element which needs to be copied/created to/on the guest.
207 */
208typedef struct DESTFILEENTRY
209{
210 DESTFILEENTRY(Utf8Str strFileName) : mFileName(strFileName) {}
211 Utf8Str mFileName;
212} DESTFILEENTRY, *PDESTFILEENTRY;
213/*
214 * Map for holding destination entires, whereas the key is the destination
215 * directory and the mapped value is a vector holding all elements for this directoy.
216 */
217typedef std::map< Utf8Str, std::vector<DESTFILEENTRY> > DESTDIRMAP, *PDESTDIRMAP;
218typedef std::map< Utf8Str, std::vector<DESTFILEENTRY> >::iterator DESTDIRMAPITER, *PDESTDIRMAPITER;
219
220/**
221 * Special exit codes for returning errors/information of a
222 * started guest process to the command line VBoxManage was started from.
223 * Useful for e.g. scripting.
224 *
225 * @note These are frozen as of 4.1.0.
226 */
227enum EXITCODEEXEC
228{
229 EXITCODEEXEC_SUCCESS = RTEXITCODE_SUCCESS,
230 /* Process exited normally but with an exit code <> 0. */
231 EXITCODEEXEC_CODE = 16,
232 EXITCODEEXEC_FAILED = 17,
233 EXITCODEEXEC_TERM_SIGNAL = 18,
234 EXITCODEEXEC_TERM_ABEND = 19,
235 EXITCODEEXEC_TIMEOUT = 20,
236 EXITCODEEXEC_DOWN = 21,
237 EXITCODEEXEC_CANCELED = 22
238};
239
240/*
241 * Common getopt definitions, starting at 1000.
242 * Specific command definitions will start all at 2000.
243 */
244enum GETOPTDEF_COMMON
245{
246 GETOPTDEF_COMMON_PASSWORD = 1000
247};
248
249/**
250 * RTGetOpt-IDs for the guest execution control command line.
251 */
252enum GETOPTDEF_EXEC
253{
254 GETOPTDEF_EXEC_IGNOREORPHANEDPROCESSES = 1000,
255 GETOPTDEF_EXEC_NO_PROFILE,
256 GETOPTDEF_EXEC_OUTPUTFORMAT,
257 GETOPTDEF_EXEC_DOS2UNIX,
258 GETOPTDEF_EXEC_UNIX2DOS,
259 GETOPTDEF_EXEC_WAITFOREXIT,
260 GETOPTDEF_EXEC_WAITFORSTDOUT,
261 GETOPTDEF_EXEC_WAITFORSTDERR
262};
263
264enum GETOPTDEF_COPY
265{
266 GETOPTDEF_COPY_DRYRUN = 1000,
267 GETOPTDEF_COPY_FOLLOW,
268 GETOPTDEF_COPY_TARGETDIR
269};
270
271enum GETOPTDEF_MKDIR
272{
273};
274
275enum GETOPTDEF_RM
276{
277};
278
279enum GETOPTDEF_RMDIR
280{
281};
282
283enum GETOPTDEF_SESSIONCLOSE
284{
285 GETOPTDEF_SESSIONCLOSE_ALL = 2000
286};
287
288enum GETOPTDEF_STAT
289{
290};
291
292enum OUTPUTTYPE
293{
294 OUTPUTTYPE_UNDEFINED = 0,
295 OUTPUTTYPE_DOS2UNIX = 10,
296 OUTPUTTYPE_UNIX2DOS = 20
297};
298
299static int ctrlCopyDirExists(PCOPYCONTEXT pContext, bool bGuest, const char *pszDir, bool *fExists);
300
301#endif /* VBOX_ONLY_DOCS */
302
303void usageGuestControl(PRTSTREAM pStrm, const char *pcszSep1, const char *pcszSep2, uint32_t uSubCmd)
304{
305 RTStrmPrintf(pStrm,
306 "%s guestcontrol %s <uuid|vmname>\n%s",
307 pcszSep1, pcszSep2,
308 uSubCmd == ~0U ? "\n" : "");
309 if (uSubCmd & USAGE_GSTCTRL_EXEC)
310 RTStrmPrintf(pStrm,
311 " exec[ute]\n"
312 " --image <path to program> --username <name>\n"
313 " [--passwordfile <file> | --password <password>]\n"
314 " [--domain <domain>] [--verbose] [--timeout <msec>]\n"
315 " [--environment \"<NAME>=<VALUE> [<NAME>=<VALUE>]\"]\n"
316 " [--wait-exit] [--wait-stdout] [--wait-stderr]\n"
317 " [--dos2unix] [--unix2dos]\n"
318 " [-- [<argument1>] ... [<argumentN>]]\n"
319 "\n");
320 if (uSubCmd & USAGE_GSTCTRL_COPYFROM)
321 RTStrmPrintf(pStrm,
322 " copyfrom\n"
323 " <guest source> <host dest> --username <name>\n"
324 " [--passwordfile <file> | --password <password>]\n"
325 " [--domain <domain>] [--verbose]\n"
326 " [--dryrun] [--follow] [--recursive]\n"
327 "\n");
328 if (uSubCmd & USAGE_GSTCTRL_COPYTO)
329 RTStrmPrintf(pStrm,
330 " copyto|cp\n"
331 " <host source> <guest dest> --username <name>\n"
332 " [--passwordfile <file> | --password <password>]\n"
333 " [--domain <domain>] [--verbose]\n"
334 " [--dryrun] [--follow] [--recursive]\n"
335 "\n");
336 if (uSubCmd & USAGE_GSTCTRL_CREATEDIR)
337 RTStrmPrintf(pStrm,
338 " createdir[ectory]|mkdir|md\n"
339 " <guest directory>... --username <name>\n"
340 " [--passwordfile <file> | --password <password>]\n"
341 " [--domain <domain>] [--verbose]\n"
342 " [--parents] [--mode <mode>]\n"
343 "\n");
344 if (uSubCmd & USAGE_GSTCTRL_REMOVEDIR)
345 RTStrmPrintf(pStrm,
346 " removedir[ectory]|rmdir\n"
347 " <guest directory>... --username <name>\n"
348 " [--passwordfile <file> | --password <password>]\n"
349 " [--domain <domain>] [--verbose]\n"
350 " [--recursive|-R|-r]\n"
351 "\n");
352 if (uSubCmd & USAGE_GSTCTRL_REMOVEFILE)
353 RTStrmPrintf(pStrm,
354 " removefile|rm\n"
355 " <guest file>... --username <name>\n"
356 " [--passwordfile <file> | --password <password>]\n"
357 " [--domain <domain>] [--verbose]\n"
358 "\n");
359 if (uSubCmd & USAGE_GSTCTRL_RENAME)
360 RTStrmPrintf(pStrm,
361 " ren[ame]|mv\n"
362 " <source>... <dest> --username <name>\n"
363 " [--passwordfile <file> | --password <password>]\n"
364 " [--domain <domain>] [--verbose]\n"
365 "\n");
366 if (uSubCmd & USAGE_GSTCTRL_CREATETEMP)
367 RTStrmPrintf(pStrm,
368 " createtemp[orary]|mktemp\n"
369 " <template> --username <name>\n"
370 " [--passwordfile <file> | --password <password>]\n"
371 " [--directory] [--secure] [--tmpdir <directory>]\n"
372 " [--domain <domain>] [--mode <mode>] [--verbose]\n"
373 "\n");
374 if (uSubCmd & USAGE_GSTCTRL_LIST)
375 RTStrmPrintf(pStrm,
376 " list <all|sessions|processes|files> [--verbose]\n"
377 "\n");
378 /** @todo Add an own help group for "session" and "process" sub commands. */
379 if (uSubCmd & USAGE_GSTCTRL_PROCESS)
380 RTStrmPrintf(pStrm,
381 " process kill --session-id <ID>\n"
382 " | --session-name <name or pattern>\n"
383 " [--verbose]\n"
384 " <PID> ... <PID n>\n"
385 "\n");
386 if (uSubCmd & USAGE_GSTCTRL_KILL)
387 RTStrmPrintf(pStrm,
388 " [p[s]]kill --session-id <ID>\n"
389 " | --session-name <name or pattern>\n"
390 " [--verbose]\n"
391 " <PID> ... <PID n>\n"
392 "\n");
393 if (uSubCmd & USAGE_GSTCTRL_SESSION)
394 RTStrmPrintf(pStrm,
395 " session close --session-id <ID>\n"
396 " | --session-name <name or pattern>\n"
397 " | --all\n"
398 " [--verbose]\n"
399 "\n");
400 if (uSubCmd & USAGE_GSTCTRL_STAT)
401 RTStrmPrintf(pStrm,
402 " stat\n"
403 " <file>... --username <name>\n"
404 " [--passwordfile <file> | --password <password>]\n"
405 " [--domain <domain>] [--verbose]\n"
406 "\n");
407 if (uSubCmd & USAGE_GSTCTRL_UPDATEADDS)
408 RTStrmPrintf(pStrm,
409 " updateadditions\n"
410 " [--source <guest additions .ISO>] [--verbose]\n"
411 " [--wait-start]\n"
412 " [-- [<argument1>] ... [<argumentN>]]\n"
413 "\n");
414 if (uSubCmd & USAGE_GSTCTRL_WATCH)
415 RTStrmPrintf(pStrm,
416 " watch [--verbose]\n"
417 "\n");
418}
419
420#ifndef VBOX_ONLY_DOCS
421
422#ifdef RT_OS_WINDOWS
423static BOOL WINAPI guestCtrlSignalHandler(DWORD dwCtrlType)
424{
425 bool fEventHandled = FALSE;
426 switch (dwCtrlType)
427 {
428 /* User pressed CTRL+C or CTRL+BREAK or an external event was sent
429 * via GenerateConsoleCtrlEvent(). */
430 case CTRL_BREAK_EVENT:
431 case CTRL_CLOSE_EVENT:
432 case CTRL_C_EVENT:
433 ASMAtomicWriteBool(&g_fGuestCtrlCanceled, true);
434 fEventHandled = TRUE;
435 break;
436 default:
437 break;
438 /** @todo Add other events here. */
439 }
440
441 return fEventHandled;
442}
443#else /* !RT_OS_WINDOWS */
444/**
445 * Signal handler that sets g_fGuestCtrlCanceled.
446 *
447 * This can be executed on any thread in the process, on Windows it may even be
448 * a thread dedicated to delivering this signal. Don't do anything
449 * unnecessary here.
450 */
451static void guestCtrlSignalHandler(int iSignal)
452{
453 NOREF(iSignal);
454 ASMAtomicWriteBool(&g_fGuestCtrlCanceled, true);
455}
456#endif
457
458/**
459 * Installs a custom signal handler to get notified
460 * whenever the user wants to intercept the program.
461 *
462 ** @todo Make this handler available for all VBoxManage modules?
463 */
464static int ctrlSignalHandlerInstall(void)
465{
466 int rc = VINF_SUCCESS;
467#ifdef RT_OS_WINDOWS
468 if (!SetConsoleCtrlHandler((PHANDLER_ROUTINE)guestCtrlSignalHandler, TRUE /* Add handler */))
469 {
470 rc = RTErrConvertFromWin32(GetLastError());
471 RTMsgError("Unable to install console control handler, rc=%Rrc\n", rc);
472 }
473#else
474 signal(SIGINT, guestCtrlSignalHandler);
475# ifdef SIGBREAK
476 signal(SIGBREAK, guestCtrlSignalHandler);
477# endif
478#endif
479 return rc;
480}
481
482/**
483 * Uninstalls a previously installed signal handler.
484 */
485static int ctrlSignalHandlerUninstall(void)
486{
487 int rc = VINF_SUCCESS;
488#ifdef RT_OS_WINDOWS
489 if (!SetConsoleCtrlHandler((PHANDLER_ROUTINE)NULL, FALSE /* Remove handler */))
490 {
491 rc = RTErrConvertFromWin32(GetLastError());
492 RTMsgError("Unable to uninstall console control handler, rc=%Rrc\n", rc);
493 }
494#else
495 signal(SIGINT, SIG_DFL);
496# ifdef SIGBREAK
497 signal(SIGBREAK, SIG_DFL);
498# endif
499#endif
500 return rc;
501}
502
503/**
504 * Translates a process status to a human readable
505 * string.
506 */
507const char *ctrlProcessStatusToText(ProcessStatus_T enmStatus)
508{
509 switch (enmStatus)
510 {
511 case ProcessStatus_Starting:
512 return "starting";
513 case ProcessStatus_Started:
514 return "started";
515 case ProcessStatus_Paused:
516 return "paused";
517 case ProcessStatus_Terminating:
518 return "terminating";
519 case ProcessStatus_TerminatedNormally:
520 return "successfully terminated";
521 case ProcessStatus_TerminatedSignal:
522 return "terminated by signal";
523 case ProcessStatus_TerminatedAbnormally:
524 return "abnormally aborted";
525 case ProcessStatus_TimedOutKilled:
526 return "timed out";
527 case ProcessStatus_TimedOutAbnormally:
528 return "timed out, hanging";
529 case ProcessStatus_Down:
530 return "killed";
531 case ProcessStatus_Error:
532 return "error";
533 default:
534 break;
535 }
536 return "unknown";
537}
538
539static int ctrlExecProcessStatusToExitCode(ProcessStatus_T enmStatus, ULONG uExitCode)
540{
541 int vrc = EXITCODEEXEC_SUCCESS;
542 switch (enmStatus)
543 {
544 case ProcessStatus_Starting:
545 vrc = EXITCODEEXEC_SUCCESS;
546 break;
547 case ProcessStatus_Started:
548 vrc = EXITCODEEXEC_SUCCESS;
549 break;
550 case ProcessStatus_Paused:
551 vrc = EXITCODEEXEC_SUCCESS;
552 break;
553 case ProcessStatus_Terminating:
554 vrc = EXITCODEEXEC_SUCCESS;
555 break;
556 case ProcessStatus_TerminatedNormally:
557 vrc = !uExitCode ? EXITCODEEXEC_SUCCESS : EXITCODEEXEC_CODE;
558 break;
559 case ProcessStatus_TerminatedSignal:
560 vrc = EXITCODEEXEC_TERM_SIGNAL;
561 break;
562 case ProcessStatus_TerminatedAbnormally:
563 vrc = EXITCODEEXEC_TERM_ABEND;
564 break;
565 case ProcessStatus_TimedOutKilled:
566 vrc = EXITCODEEXEC_TIMEOUT;
567 break;
568 case ProcessStatus_TimedOutAbnormally:
569 vrc = EXITCODEEXEC_TIMEOUT;
570 break;
571 case ProcessStatus_Down:
572 /* Service/OS is stopping, process was killed, so
573 * not exactly an error of the started process ... */
574 vrc = EXITCODEEXEC_DOWN;
575 break;
576 case ProcessStatus_Error:
577 vrc = EXITCODEEXEC_FAILED;
578 break;
579 default:
580 AssertMsgFailed(("Unknown exit code (%u) from guest process returned!\n", enmStatus));
581 break;
582 }
583 return vrc;
584}
585
586const char *ctrlProcessWaitResultToText(ProcessWaitResult_T enmWaitResult)
587{
588 switch (enmWaitResult)
589 {
590 case ProcessWaitResult_Start:
591 return "started";
592 case ProcessWaitResult_Terminate:
593 return "terminated";
594 case ProcessWaitResult_Status:
595 return "status changed";
596 case ProcessWaitResult_Error:
597 return "error";
598 case ProcessWaitResult_Timeout:
599 return "timed out";
600 case ProcessWaitResult_StdIn:
601 return "stdin ready";
602 case ProcessWaitResult_StdOut:
603 return "data on stdout";
604 case ProcessWaitResult_StdErr:
605 return "data on stderr";
606 case ProcessWaitResult_WaitFlagNotSupported:
607 return "waiting flag not supported";
608 default:
609 break;
610 }
611 return "unknown";
612}
613
614/**
615 * Translates a guest session status to a human readable
616 * string.
617 */
618const char *ctrlSessionStatusToText(GuestSessionStatus_T enmStatus)
619{
620 switch (enmStatus)
621 {
622 case GuestSessionStatus_Starting:
623 return "starting";
624 case GuestSessionStatus_Started:
625 return "started";
626 case GuestSessionStatus_Terminating:
627 return "terminating";
628 case GuestSessionStatus_Terminated:
629 return "terminated";
630 case GuestSessionStatus_TimedOutKilled:
631 return "timed out";
632 case GuestSessionStatus_TimedOutAbnormally:
633 return "timed out, hanging";
634 case GuestSessionStatus_Down:
635 return "killed";
636 case GuestSessionStatus_Error:
637 return "error";
638 default:
639 break;
640 }
641 return "unknown";
642}
643
644/**
645 * Translates a guest file status to a human readable
646 * string.
647 */
648const char *ctrlFileStatusToText(FileStatus_T enmStatus)
649{
650 switch (enmStatus)
651 {
652 case FileStatus_Opening:
653 return "opening";
654 case FileStatus_Open:
655 return "open";
656 case FileStatus_Closing:
657 return "closing";
658 case FileStatus_Closed:
659 return "closed";
660 case FileStatus_Down:
661 return "killed";
662 case FileStatus_Error:
663 return "error";
664 default:
665 break;
666 }
667 return "unknown";
668}
669
670static int ctrlPrintError(com::ErrorInfo &errorInfo)
671{
672 if ( errorInfo.isFullAvailable()
673 || errorInfo.isBasicAvailable())
674 {
675 /* If we got a VBOX_E_IPRT error we handle the error in a more gentle way
676 * because it contains more accurate info about what went wrong. */
677 if (errorInfo.getResultCode() == VBOX_E_IPRT_ERROR)
678 RTMsgError("%ls.", errorInfo.getText().raw());
679 else
680 {
681 RTMsgError("Error details:");
682 GluePrintErrorInfo(errorInfo);
683 }
684 return VERR_GENERAL_FAILURE; /** @todo */
685 }
686 AssertMsgFailedReturn(("Object has indicated no error (%Rhrc)!?\n", errorInfo.getResultCode()),
687 VERR_INVALID_PARAMETER);
688}
689
690static int ctrlPrintError(IUnknown *pObj, const GUID &aIID)
691{
692 com::ErrorInfo ErrInfo(pObj, aIID);
693 return ctrlPrintError(ErrInfo);
694}
695
696static int ctrlPrintProgressError(ComPtr<IProgress> pProgress)
697{
698 int vrc = VINF_SUCCESS;
699 HRESULT rc;
700
701 do
702 {
703 BOOL fCanceled;
704 CHECK_ERROR_BREAK(pProgress, COMGETTER(Canceled)(&fCanceled));
705 if (!fCanceled)
706 {
707 LONG rcProc;
708 CHECK_ERROR_BREAK(pProgress, COMGETTER(ResultCode)(&rcProc));
709 if (FAILED(rcProc))
710 {
711 com::ProgressErrorInfo ErrInfo(pProgress);
712 vrc = ctrlPrintError(ErrInfo);
713 }
714 }
715
716 } while(0);
717
718 AssertMsgStmt(SUCCEEDED(rc), ("Could not lookup progress information\n"), vrc = VERR_COM_UNEXPECTED);
719
720 return vrc;
721}
722
723/**
724 * Un-initializes the VM after guest control usage.
725 * @param pCmdCtx Pointer to command context.
726 * @param uFlags Command context flags.
727 */
728static void ctrlUninitVM(PGCTLCMDCTX pCtx, uint32_t uFlags)
729{
730 AssertPtrReturnVoid(pCtx);
731
732 if (!(pCtx->uFlags & CTLCMDCTX_FLAGS_NO_SIGNAL_HANDLER))
733 ctrlSignalHandlerUninstall();
734
735 HRESULT rc;
736
737 do
738 {
739 if (!pCtx->pGuestSession.isNull())
740 {
741 if ( !(pCtx->uFlags & CTLCMDCTX_FLAGS_SESSION_ANONYMOUS)
742 && !(pCtx->uFlags & CTLCMDCTX_FLAGS_SESSION_DETACH))
743 {
744 if (pCtx->fVerbose)
745 RTPrintf("Closing guest session ...\n");
746
747 CHECK_ERROR(pCtx->pGuestSession, Close());
748 /* Keep going - don't break here. Try to unlock the
749 * machine down below. */
750 }
751 else if ( (pCtx->uFlags & CTLCMDCTX_FLAGS_SESSION_DETACH)
752 && pCtx->fVerbose)
753 RTPrintf("Guest session detached\n");
754
755 pCtx->pGuestSession.setNull();
756 }
757
758 if (pCtx->handlerArg.session)
759 CHECK_ERROR(pCtx->handlerArg.session, UnlockMachine());
760
761 } while (0);
762
763 for (int i = 0; i < pCtx->iArgc; i++)
764 RTStrFree(pCtx->ppaArgv[i]);
765 RTMemFree(pCtx->ppaArgv);
766 pCtx->iArgc = 0;
767}
768
769/**
770 * Initializes the VM for IGuest operations.
771 *
772 * That is, checks whether it's up and running, if it can be locked (shared
773 * only) and returns a valid IGuest pointer on success. Also, it does some
774 * basic command line processing and opens a guest session, if required.
775 *
776 * @return RTEXITCODE status code.
777 * @param pArg Pointer to command line argument structure.
778 * @param pCmdCtx Pointer to command context.
779 * @param uFlags Command context flags.
780 */
781static RTEXITCODE ctrlInitVM(HandlerArg *pArg,
782 PGCTLCMDCTX pCtx, uint32_t uFlags, uint32_t uUsage)
783{
784 AssertPtrReturn(pArg, RTEXITCODE_FAILURE);
785 AssertReturn(pArg->argc > 1, RTEXITCODE_FAILURE);
786 AssertPtrReturn(pCtx, RTEXITCODE_FAILURE);
787
788#ifdef DEBUG_andy
789 RTPrintf("Original argv:\n");
790 for (int i=0; i<pArg->argc;i++)
791 RTPrintf("\targv[%d]=%s\n", i, pArg->argv[i]);
792#endif
793
794 RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
795
796 const char *pszNameOrId = pArg->argv[0];
797 const char *pszCmd = pArg->argv[1];
798
799 /* Lookup VM. */
800 ComPtr<IMachine> machine;
801 /* Assume it's an UUID. */
802 HRESULT rc;
803 CHECK_ERROR(pArg->virtualBox, FindMachine(Bstr(pszNameOrId).raw(),
804 machine.asOutParam()));
805 if (SUCCEEDED(rc))
806 {
807 /* Machine is running? */
808 MachineState_T machineState;
809 CHECK_ERROR(machine, COMGETTER(State)(&machineState));
810 if ( SUCCEEDED(rc)
811 && (machineState != MachineState_Running))
812 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Machine \"%s\" is not running (currently %s)!\n",
813 pszNameOrId, machineStateToName(machineState, false));
814 }
815 else
816 rcExit = RTEXITCODE_FAILURE;
817
818 if (rcExit == RTEXITCODE_SUCCESS)
819 {
820 /*
821 * Process standard options which are served by all commands.
822 */
823 static const RTGETOPTDEF s_aOptions[] =
824 {
825 { "--username", 'u', RTGETOPT_REQ_STRING },
826 { "--passwordfile", 'p', RTGETOPT_REQ_STRING },
827 { "--password", GETOPTDEF_COMMON_PASSWORD, RTGETOPT_REQ_STRING },
828 { "--domain", 'd', RTGETOPT_REQ_STRING },
829 { "--verbose", 'v', RTGETOPT_REQ_NOTHING }
830 };
831
832 /*
833 * Allocate per-command argv. This then only contains the specific arguments
834 * the command needs.
835 */
836 pCtx->ppaArgv = (char**)RTMemAlloc(pArg->argc * sizeof(char*) + 1);
837 if (!pCtx->ppaArgv)
838 {
839 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Not enough memory for per-command argv\n");
840 }
841 else
842 {
843 pCtx->iArgc = 0;
844
845 int iArgIdx = 2; /* Skip VM name and guest control command */
846 int ch;
847 RTGETOPTUNION ValueUnion;
848 RTGETOPTSTATE GetState;
849 RTGetOptInit(&GetState, pArg->argc, pArg->argv,
850 s_aOptions, RT_ELEMENTS(s_aOptions),
851 iArgIdx, 0);
852
853 while ( (ch = RTGetOpt(&GetState, &ValueUnion))
854 && (rcExit == RTEXITCODE_SUCCESS))
855 {
856 /* For options that require an argument, ValueUnion has received the value. */
857 switch (ch)
858 {
859 case 'u': /* User name */
860 if (!(uFlags & CTLCMDCTX_FLAGS_SESSION_ANONYMOUS))
861 pCtx->strUsername = ValueUnion.psz;
862 iArgIdx = GetState.iNext;
863 break;
864
865 case GETOPTDEF_COMMON_PASSWORD: /* Password */
866 if (!(uFlags & CTLCMDCTX_FLAGS_SESSION_ANONYMOUS))
867 {
868 if (pCtx->strPassword.isEmpty())
869 pCtx->strPassword = ValueUnion.psz;
870 }
871 iArgIdx = GetState.iNext;
872 break;
873
874 case 'p': /* Password file */
875 {
876 if (!(uFlags & CTLCMDCTX_FLAGS_SESSION_ANONYMOUS))
877 rcExit = readPasswordFile(ValueUnion.psz, &pCtx->strPassword);
878 iArgIdx = GetState.iNext;
879 break;
880 }
881
882 case 'd': /* domain */
883 if (!(uFlags & CTLCMDCTX_FLAGS_SESSION_ANONYMOUS))
884 pCtx->strDomain = ValueUnion.psz;
885 iArgIdx = GetState.iNext;
886 break;
887
888 case 'v': /* Verbose */
889 pCtx->fVerbose = true;
890 iArgIdx = GetState.iNext;
891 break;
892
893 case 'h': /* Help */
894 errorGetOptEx(USAGE_GUESTCONTROL, uUsage, ch, &ValueUnion);
895 return RTEXITCODE_SYNTAX;
896
897 default:
898 /* Simply skip; might be handled in a specific command
899 * handler later. */
900 break;
901
902 } /* switch */
903
904 int iArgDiff = GetState.iNext - iArgIdx;
905 if (iArgDiff)
906 {
907#ifdef DEBUG_andy
908 RTPrintf("Not handled (iNext=%d, iArgsCur=%d):\n", GetState.iNext, iArgIdx);
909#endif
910 for (int i = iArgIdx; i < GetState.iNext; i++)
911 {
912 char *pszArg = RTStrDup(pArg->argv[i]);
913 if (!pszArg)
914 {
915 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE,
916 "Not enough memory for command line handling\n");
917 break;
918 }
919 pCtx->ppaArgv[pCtx->iArgc] = pszArg;
920 pCtx->iArgc++;
921#ifdef DEBUG_andy
922 RTPrintf("\targv[%d]=%s\n", i, pArg->argv[i]);
923#endif
924 }
925
926 iArgIdx = GetState.iNext;
927 }
928
929 } /* while RTGetOpt */
930 }
931 }
932
933 /*
934 * Check for mandatory stuff.
935 */
936 if (rcExit == RTEXITCODE_SUCCESS)
937 {
938#if 0
939 RTPrintf("argc=%d\n", pCtx->iArgc);
940 for (int i = 0; i < pCtx->iArgc; i++)
941 RTPrintf("argv[%d]=%s\n", i, pCtx->ppaArgv[i]);
942#endif
943 if (!(uFlags & CTLCMDCTX_FLAGS_SESSION_ANONYMOUS))
944 {
945 if (pCtx->strUsername.isEmpty())
946 rcExit = errorSyntaxEx(USAGE_GUESTCONTROL, uUsage, "No user name specified!");
947 }
948 }
949
950 if (rcExit == RTEXITCODE_SUCCESS)
951 {
952 /*
953 * Build up a reasonable guest session name. Useful for identifying
954 * a specific session when listing / searching for them.
955 */
956 char *pszSessionName;
957 if (0 >= RTStrAPrintf(&pszSessionName,
958 "[%RU32] VBoxManage Guest Control [%s] - %s",
959 RTProcSelf(), pszNameOrId, pszCmd))
960 return RTMsgErrorExit(RTEXITCODE_FAILURE, "No enough memory for session name\n");
961
962 do
963 {
964 /* Open a session for the VM. */
965 CHECK_ERROR_BREAK(machine, LockMachine(pArg->session, LockType_Shared));
966 /* Get the associated console. */
967 ComPtr<IConsole> console;
968 CHECK_ERROR_BREAK(pArg->session, COMGETTER(Console)(console.asOutParam()));
969 /* ... and session machine. */
970 ComPtr<IMachine> sessionMachine;
971 CHECK_ERROR_BREAK(pArg->session, COMGETTER(Machine)(sessionMachine.asOutParam()));
972 /* Get IGuest interface. */
973 CHECK_ERROR_BREAK(console, COMGETTER(Guest)(pCtx->pGuest.asOutParam()));
974 if (!(uFlags & CTLCMDCTX_FLAGS_SESSION_ANONYMOUS))
975 {
976 if (pCtx->fVerbose)
977 RTPrintf("Opening guest session as user '%s' ...\n", pCtx->strUsername.c_str());
978
979 /* Open a guest session. */
980 Assert(!pCtx->pGuest.isNull());
981 CHECK_ERROR_BREAK(pCtx->pGuest, CreateSession(Bstr(pCtx->strUsername).raw(),
982 Bstr(pCtx->strPassword).raw(),
983 Bstr(pCtx->strDomain).raw(),
984 Bstr(pszSessionName).raw(),
985 pCtx->pGuestSession.asOutParam()));
986
987 /*
988 * Wait for guest session to start.
989 */
990 if (pCtx->fVerbose)
991 RTPrintf("Waiting for guest session to start ...\n");
992
993 com::SafeArray<GuestSessionWaitForFlag_T> aSessionWaitFlags;
994 aSessionWaitFlags.push_back(GuestSessionWaitForFlag_Start);
995 GuestSessionWaitResult_T sessionWaitResult;
996 CHECK_ERROR_BREAK(pCtx->pGuestSession, WaitForArray(ComSafeArrayAsInParam(aSessionWaitFlags),
997 /** @todo Make session handling timeouts configurable. */
998 30 * 1000, &sessionWaitResult));
999
1000 if ( sessionWaitResult == GuestSessionWaitResult_Start
1001 /* Note: This might happen when Guest Additions < 4.3 are installed which don't
1002 * support dedicated guest sessions. */
1003 || sessionWaitResult == GuestSessionWaitResult_WaitFlagNotSupported)
1004 {
1005 CHECK_ERROR_BREAK(pCtx->pGuestSession, COMGETTER(Id)(&pCtx->uSessionID));
1006 if (pCtx->fVerbose)
1007 RTPrintf("Guest session (ID %RU32) has been started\n", pCtx->uSessionID);
1008 }
1009 else
1010 {
1011 GuestSessionStatus_T sessionStatus;
1012 CHECK_ERROR_BREAK(pCtx->pGuestSession, COMGETTER(Status)(&sessionStatus));
1013 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Error starting guest session (current status is: %s)\n",
1014 ctrlSessionStatusToText(sessionStatus));
1015 break;
1016 }
1017 }
1018
1019 if ( SUCCEEDED(rc)
1020 && !(uFlags & CTLCMDCTX_FLAGS_NO_SIGNAL_HANDLER))
1021 {
1022 ctrlSignalHandlerInstall();
1023 }
1024
1025 } while (0);
1026
1027 if (FAILED(rc))
1028 rcExit = RTEXITCODE_FAILURE;
1029
1030 RTStrFree(pszSessionName);
1031 }
1032
1033 if (rcExit == RTEXITCODE_SUCCESS)
1034 {
1035 pCtx->handlerArg = *pArg;
1036 pCtx->uFlags = uFlags;
1037 }
1038 else /* Clean up on failure. */
1039 ctrlUninitVM(pCtx, uFlags);
1040
1041 return rcExit;
1042}
1043
1044/**
1045 * Prints the desired guest output to a stream.
1046 *
1047 * @return IPRT status code.
1048 * @param pProcess Pointer to appropriate process object.
1049 * @param pStrmOutput Where to write the data.
1050 * @param uHandle Handle where to read the data from.
1051 * @param uTimeoutMS Timeout (in ms) to wait for the operation to complete.
1052 */
1053static int ctrlExecPrintOutput(IProcess *pProcess, PRTSTREAM pStrmOutput,
1054 ULONG uHandle, ULONG uTimeoutMS)
1055{
1056 AssertPtrReturn(pProcess, VERR_INVALID_POINTER);
1057 AssertPtrReturn(pStrmOutput, VERR_INVALID_POINTER);
1058
1059 int vrc = VINF_SUCCESS;
1060
1061 SafeArray<BYTE> aOutputData;
1062 HRESULT rc = pProcess->Read(uHandle, _64K, uTimeoutMS,
1063 ComSafeArrayAsOutParam(aOutputData));
1064 if (FAILED(rc))
1065 vrc = ctrlPrintError(pProcess, COM_IIDOF(IProcess));
1066 else
1067 {
1068 size_t cbOutputData = aOutputData.size();
1069 if (cbOutputData > 0)
1070 {
1071 BYTE *pBuf = aOutputData.raw();
1072 AssertPtr(pBuf);
1073 pBuf[cbOutputData - 1] = 0; /* Properly terminate buffer. */
1074
1075 /** @todo implement the dos2unix/unix2dos conversions */
1076
1077 /*
1078 * If aOutputData is text data from the guest process' stdout or stderr,
1079 * it has a platform dependent line ending. So standardize on
1080 * Unix style, as RTStrmWrite does the LF -> CR/LF replacement on
1081 * Windows. Otherwise we end up with CR/CR/LF on Windows.
1082 */
1083
1084 char *pszBufUTF8;
1085 vrc = RTStrCurrentCPToUtf8(&pszBufUTF8, (const char*)aOutputData.raw());
1086 if (RT_SUCCESS(vrc))
1087 {
1088 cbOutputData = strlen(pszBufUTF8);
1089
1090 ULONG cbOutputDataPrint = cbOutputData;
1091 for (char *s = pszBufUTF8, *d = s;
1092 s - pszBufUTF8 < (ssize_t)cbOutputData;
1093 s++, d++)
1094 {
1095 if (*s == '\r')
1096 {
1097 /* skip over CR, adjust destination */
1098 d--;
1099 cbOutputDataPrint--;
1100 }
1101 else if (s != d)
1102 *d = *s;
1103 }
1104
1105 vrc = RTStrmWrite(pStrmOutput, pszBufUTF8, cbOutputDataPrint);
1106 if (RT_FAILURE(vrc))
1107 RTMsgError("Unable to write output, rc=%Rrc\n", vrc);
1108
1109 RTStrFree(pszBufUTF8);
1110 }
1111 else
1112 RTMsgError("Unable to convert output, rc=%Rrc\n", vrc);
1113 }
1114 }
1115
1116 return vrc;
1117}
1118
1119/**
1120 * Returns the remaining time (in ms) based on the start time and a set
1121 * timeout value. Returns RT_INDEFINITE_WAIT if no timeout was specified.
1122 *
1123 * @return RTMSINTERVAL Time left (in ms).
1124 * @param u64StartMs Start time (in ms).
1125 * @param cMsTimeout Timeout value (in ms).
1126 */
1127inline RTMSINTERVAL ctrlExecGetRemainingTime(uint64_t u64StartMs, RTMSINTERVAL cMsTimeout)
1128{
1129 if (!cMsTimeout || cMsTimeout == RT_INDEFINITE_WAIT) /* If no timeout specified, wait forever. */
1130 return RT_INDEFINITE_WAIT;
1131
1132 uint64_t u64ElapsedMs = RTTimeMilliTS() - u64StartMs;
1133 if (u64ElapsedMs >= cMsTimeout)
1134 return 0;
1135
1136 return cMsTimeout - (RTMSINTERVAL)u64ElapsedMs;
1137}
1138
1139static DECLCALLBACK(RTEXITCODE) handleCtrlProcessExec(PGCTLCMDCTX pCtx)
1140{
1141 AssertPtrReturn(pCtx, RTEXITCODE_FAILURE);
1142
1143 /*
1144 * Parse arguments.
1145 */
1146 static const RTGETOPTDEF s_aOptions[] =
1147 {
1148 { "--dos2unix", GETOPTDEF_EXEC_DOS2UNIX, RTGETOPT_REQ_NOTHING },
1149 { "--environment", 'e', RTGETOPT_REQ_STRING },
1150 { "--flags", 'f', RTGETOPT_REQ_STRING },
1151 { "--ignore-operhaned-processes", GETOPTDEF_EXEC_IGNOREORPHANEDPROCESSES, RTGETOPT_REQ_NOTHING },
1152 { "--image", 'i', RTGETOPT_REQ_STRING },
1153 { "--no-profile", GETOPTDEF_EXEC_NO_PROFILE, RTGETOPT_REQ_NOTHING },
1154 { "--timeout", 't', RTGETOPT_REQ_UINT32 },
1155 { "--unix2dos", GETOPTDEF_EXEC_UNIX2DOS, RTGETOPT_REQ_NOTHING },
1156 { "--wait-exit", GETOPTDEF_EXEC_WAITFOREXIT, RTGETOPT_REQ_NOTHING },
1157 { "--wait-stdout", GETOPTDEF_EXEC_WAITFORSTDOUT, RTGETOPT_REQ_NOTHING },
1158 { "--wait-stderr", GETOPTDEF_EXEC_WAITFORSTDERR, RTGETOPT_REQ_NOTHING }
1159 };
1160
1161#ifdef DEBUG_andy
1162 RTPrintf("first=%d\n", pCtx->iFirstArgc);
1163 for (int i=0; i<pCtx->iArgc;i++)
1164 RTPrintf("\targv[%d]=%s\n", i, pCtx->ppaArgv[i]);
1165#endif
1166
1167 int ch;
1168 RTGETOPTUNION ValueUnion;
1169 RTGETOPTSTATE GetState;
1170 RTGetOptInit(&GetState, pCtx->iArgc, pCtx->ppaArgv, s_aOptions, RT_ELEMENTS(s_aOptions),
1171 pCtx->iFirstArgc, RTGETOPTINIT_FLAGS_OPTS_FIRST);
1172
1173 Utf8Str strCmd;
1174 com::SafeArray<ProcessCreateFlag_T> aCreateFlags;
1175 com::SafeArray<ProcessWaitForFlag_T> aWaitFlags;
1176 com::SafeArray<IN_BSTR> aArgs;
1177 com::SafeArray<IN_BSTR> aEnv;
1178 RTMSINTERVAL cMsTimeout = 0;
1179 OUTPUTTYPE eOutputType = OUTPUTTYPE_UNDEFINED;
1180 bool fDetached = true;
1181 int vrc = VINF_SUCCESS;
1182
1183 try
1184 {
1185 /* Wait for process start in any case. This is useful for scripting VBoxManage
1186 * when relying on its overall exit code. */
1187 aWaitFlags.push_back(ProcessWaitForFlag_Start);
1188
1189 while ( (ch = RTGetOpt(&GetState, &ValueUnion))
1190 && RT_SUCCESS(vrc))
1191 {
1192 /* For options that require an argument, ValueUnion has received the value. */
1193 switch (ch)
1194 {
1195 case GETOPTDEF_EXEC_DOS2UNIX:
1196 if (eOutputType != OUTPUTTYPE_UNDEFINED)
1197 return errorSyntaxEx(USAGE_GUESTCONTROL, USAGE_GSTCTRL_EXEC,
1198 "More than one output type (dos2unix/unix2dos) specified!");
1199 eOutputType = OUTPUTTYPE_DOS2UNIX;
1200 break;
1201
1202 case 'e': /* Environment */
1203 {
1204 char **papszArg;
1205 int cArgs;
1206
1207 vrc = RTGetOptArgvFromString(&papszArg, &cArgs, ValueUnion.psz, NULL);
1208 if (RT_FAILURE(vrc))
1209 return errorSyntaxEx(USAGE_GUESTCONTROL, USAGE_GSTCTRL_EXEC,
1210 "Failed to parse environment value, rc=%Rrc", vrc);
1211 for (int j = 0; j < cArgs; j++)
1212 aEnv.push_back(Bstr(papszArg[j]).raw());
1213
1214 RTGetOptArgvFree(papszArg);
1215 break;
1216 }
1217
1218 case GETOPTDEF_EXEC_IGNOREORPHANEDPROCESSES:
1219 aCreateFlags.push_back(ProcessCreateFlag_IgnoreOrphanedProcesses);
1220 break;
1221
1222 case GETOPTDEF_EXEC_NO_PROFILE:
1223 aCreateFlags.push_back(ProcessCreateFlag_NoProfile);
1224 break;
1225
1226 case 'i':
1227 strCmd = ValueUnion.psz;
1228 break;
1229
1230 /** @todo Add a hidden flag. */
1231
1232 case 't': /* Timeout */
1233 cMsTimeout = ValueUnion.u32;
1234 break;
1235
1236 case GETOPTDEF_EXEC_UNIX2DOS:
1237 if (eOutputType != OUTPUTTYPE_UNDEFINED)
1238 return errorSyntaxEx(USAGE_GUESTCONTROL, USAGE_GSTCTRL_EXEC,
1239 "More than one output type (dos2unix/unix2dos) specified!");
1240 eOutputType = OUTPUTTYPE_UNIX2DOS;
1241 break;
1242
1243 case GETOPTDEF_EXEC_WAITFOREXIT:
1244 aWaitFlags.push_back(ProcessWaitForFlag_Terminate);
1245 fDetached = false;
1246 break;
1247
1248 case GETOPTDEF_EXEC_WAITFORSTDOUT:
1249 aCreateFlags.push_back(ProcessCreateFlag_WaitForStdOut);
1250 aWaitFlags.push_back(ProcessWaitForFlag_StdOut);
1251 fDetached = false;
1252 break;
1253
1254 case GETOPTDEF_EXEC_WAITFORSTDERR:
1255 aCreateFlags.push_back(ProcessCreateFlag_WaitForStdErr);
1256 aWaitFlags.push_back(ProcessWaitForFlag_StdErr);
1257 fDetached = false;
1258 break;
1259
1260 case VINF_GETOPT_NOT_OPTION:
1261 if (aArgs.size() == 0 && strCmd.isEmpty())
1262 strCmd = ValueUnion.psz;
1263 else
1264 aArgs.push_back(Bstr(ValueUnion.psz).raw());
1265 break;
1266
1267 default:
1268 /* Note: Necessary for handling non-options (after --) which
1269 * contain a single dash, e.g. "-- foo.exe -s". */
1270 if (GetState.argc == GetState.iNext)
1271 aArgs.push_back(Bstr(ValueUnion.psz).raw());
1272 else
1273 return errorGetOptEx(USAGE_GUESTCONTROL, USAGE_GSTCTRL_EXEC, ch, &ValueUnion);
1274 break;
1275
1276 } /* switch */
1277 } /* while RTGetOpt */
1278 }
1279 catch (std::bad_alloc &)
1280 {
1281 vrc = VERR_NO_MEMORY;
1282 }
1283
1284 if (RT_FAILURE(vrc))
1285 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to initialize, rc=%Rrc\n", vrc);
1286
1287 if (strCmd.isEmpty())
1288 return errorSyntaxEx(USAGE_GUESTCONTROL, USAGE_GSTCTRL_EXEC,
1289 "No command to execute specified!");
1290
1291 /** @todo Any output conversion not supported yet! */
1292 if (eOutputType != OUTPUTTYPE_UNDEFINED)
1293 return errorSyntaxEx(USAGE_GUESTCONTROL, USAGE_GSTCTRL_EXEC,
1294 "Output conversion not implemented yet!");
1295
1296 RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
1297 HRESULT rc;
1298
1299 try
1300 {
1301 do
1302 {
1303 /* Adjust process creation flags if we don't want to wait for process termination. */
1304 if (fDetached)
1305 aCreateFlags.push_back(ProcessCreateFlag_WaitForProcessStartOnly);
1306
1307 /* Get current time stamp to later calculate rest of timeout left. */
1308 uint64_t u64StartMS = RTTimeMilliTS();
1309
1310 if (pCtx->fVerbose)
1311 {
1312 if (cMsTimeout == 0)
1313 RTPrintf("Starting guest process ...\n");
1314 else
1315 RTPrintf("Starting guest process (within %ums)\n", cMsTimeout);
1316 }
1317
1318 /*
1319 * Execute the process.
1320 */
1321 ComPtr<IGuestProcess> pProcess;
1322 CHECK_ERROR_BREAK(pCtx->pGuestSession, ProcessCreate(Bstr(strCmd).raw(),
1323 ComSafeArrayAsInParam(aArgs),
1324 ComSafeArrayAsInParam(aEnv),
1325 ComSafeArrayAsInParam(aCreateFlags),
1326 ctrlExecGetRemainingTime(u64StartMS, cMsTimeout),
1327 pProcess.asOutParam()));
1328
1329 /*
1330 * Explicitly wait for the guest process to be in a started
1331 * state.
1332 */
1333 com::SafeArray<ProcessWaitForFlag_T> aWaitStartFlags;
1334 aWaitStartFlags.push_back(ProcessWaitForFlag_Start);
1335 ProcessWaitResult_T waitResult;
1336 CHECK_ERROR_BREAK(pProcess, WaitForArray(ComSafeArrayAsInParam(aWaitStartFlags),
1337 ctrlExecGetRemainingTime(u64StartMS, cMsTimeout), &waitResult));
1338 bool fCompleted = false;
1339
1340 ULONG uPID = 0;
1341 CHECK_ERROR_BREAK(pProcess, COMGETTER(PID)(&uPID));
1342 if (!fDetached && pCtx->fVerbose)
1343 {
1344 RTPrintf("Process '%s' (PID %RU32) started\n",
1345 strCmd.c_str(), uPID);
1346 }
1347 else if (fDetached) /** @todo Introduce a --quiet option for not printing this. */
1348 {
1349 /* Just print plain PID to make it easier for scripts
1350 * invoking VBoxManage. */
1351 RTPrintf("[%RU32 - Session %RU32]\n", uPID, pCtx->uSessionID);
1352 }
1353
1354 vrc = RTStrmSetMode(g_pStdOut, 1 /* Binary mode */, -1 /* Code set, unchanged */);
1355 if (RT_FAILURE(vrc))
1356 RTMsgError("Unable to set stdout's binary mode, rc=%Rrc\n", vrc);
1357 vrc = RTStrmSetMode(g_pStdErr, 1 /* Binary mode */, -1 /* Code set, unchanged */);
1358 if (RT_FAILURE(vrc))
1359 RTMsgError("Unable to set stderr's binary mode, rc=%Rrc\n", vrc);
1360
1361 /* Wait for process to exit ... */
1362 RTMSINTERVAL cMsTimeLeft = 1; /* Will be calculated. */
1363 bool fReadStdOut, fReadStdErr;
1364 fReadStdOut = fReadStdErr = false;
1365
1366 while ( !fCompleted
1367 && !fDetached
1368 && cMsTimeLeft != 0)
1369 {
1370 cMsTimeLeft = ctrlExecGetRemainingTime(u64StartMS, cMsTimeout);
1371 CHECK_ERROR_BREAK(pProcess, WaitForArray(ComSafeArrayAsInParam(aWaitFlags),
1372 500 /* ms */, &waitResult));
1373 switch (waitResult)
1374 {
1375 case ProcessWaitResult_Start:
1376 {
1377 /* We're done here if we don't want to wait for termination. */
1378 if (fDetached)
1379 fCompleted = true;
1380
1381 break;
1382 }
1383 case ProcessWaitResult_StdOut:
1384 fReadStdOut = true;
1385 break;
1386 case ProcessWaitResult_StdErr:
1387 fReadStdErr = true;
1388 break;
1389 case ProcessWaitResult_Terminate:
1390 if (pCtx->fVerbose)
1391 RTPrintf("Process terminated\n");
1392 /* Process terminated, we're done. */
1393 fCompleted = true;
1394 break;
1395 case ProcessWaitResult_WaitFlagNotSupported:
1396 {
1397 /* The guest does not support waiting for stdout/err, so
1398 * yield to reduce the CPU load due to busy waiting. */
1399 RTThreadYield(); /* Optional, don't check rc. */
1400
1401 /* Try both, stdout + stderr. */
1402 fReadStdOut = fReadStdErr = true;
1403 break;
1404 }
1405 case ProcessWaitResult_Timeout:
1406 /* Fall through is intentional. */
1407 default:
1408 /* Ignore all other results, let the timeout expire */
1409 break;
1410 }
1411
1412 if (g_fGuestCtrlCanceled)
1413 break;
1414
1415 if (fReadStdOut) /* Do we need to fetch stdout data? */
1416 {
1417 cMsTimeLeft = ctrlExecGetRemainingTime(u64StartMS, cMsTimeout);
1418 vrc = ctrlExecPrintOutput(pProcess, g_pStdOut,
1419 1 /* StdOut */, cMsTimeLeft);
1420 fReadStdOut = false;
1421 }
1422
1423 if (fReadStdErr) /* Do we need to fetch stdout data? */
1424 {
1425 cMsTimeLeft = ctrlExecGetRemainingTime(u64StartMS, cMsTimeout);
1426 vrc = ctrlExecPrintOutput(pProcess, g_pStdErr,
1427 2 /* StdErr */, cMsTimeLeft);
1428 fReadStdErr = false;
1429 }
1430
1431 if ( RT_FAILURE(vrc)
1432 || g_fGuestCtrlCanceled)
1433 break;
1434
1435 /* Did we run out of time? */
1436 if ( cMsTimeout
1437 && RTTimeMilliTS() - u64StartMS > cMsTimeout)
1438 break;
1439
1440 NativeEventQueue::getMainEventQueue()->processEventQueue(0);
1441
1442 } /* while */
1443
1444 if (!fDetached)
1445 {
1446 /* Report status back to the user. */
1447 if ( fCompleted
1448 && !g_fGuestCtrlCanceled)
1449 {
1450
1451 {
1452 ProcessStatus_T procStatus;
1453 CHECK_ERROR_BREAK(pProcess, COMGETTER(Status)(&procStatus));
1454 if ( procStatus == ProcessStatus_TerminatedNormally
1455 || procStatus == ProcessStatus_TerminatedAbnormally
1456 || procStatus == ProcessStatus_TerminatedSignal)
1457 {
1458 LONG exitCode;
1459 CHECK_ERROR_BREAK(pProcess, COMGETTER(ExitCode)(&exitCode));
1460 if (pCtx->fVerbose)
1461 RTPrintf("Exit code=%u (Status=%u [%s])\n",
1462 exitCode, procStatus, ctrlProcessStatusToText(procStatus));
1463
1464 rcExit = (RTEXITCODE)ctrlExecProcessStatusToExitCode(procStatus, exitCode);
1465 }
1466 else if (pCtx->fVerbose)
1467 RTPrintf("Process now is in status [%s]\n", ctrlProcessStatusToText(procStatus));
1468 }
1469 }
1470 else
1471 {
1472 if (pCtx->fVerbose)
1473 RTPrintf("Process execution aborted!\n");
1474
1475 rcExit = (RTEXITCODE)EXITCODEEXEC_TERM_ABEND;
1476 }
1477 }
1478
1479 } while (0);
1480 }
1481 catch (std::bad_alloc)
1482 {
1483 rc = E_OUTOFMEMORY;
1484 }
1485
1486 /*
1487 * Decide what to do with the guest session. If we started a
1488 * detached guest process (that is, without waiting for it to exit),
1489 * don't close the guest session it is part of.
1490 */
1491 bool fCloseSession = false;
1492 if (SUCCEEDED(rc))
1493 {
1494 /*
1495 * Only close the guest session if we waited for the guest
1496 * process to exit. Otherwise we wouldn't have any chance to
1497 * access and/or kill detached guest process lateron.
1498 */
1499 fCloseSession = !fDetached;
1500
1501 /*
1502 * If execution was aborted from the host side (signal handler),
1503 * close the guest session in any case.
1504 */
1505 if (g_fGuestCtrlCanceled)
1506 fCloseSession = true;
1507 }
1508 else /* Close session on error. */
1509 fCloseSession = true;
1510
1511 if (!fCloseSession)
1512 pCtx->uFlags |= CTLCMDCTX_FLAGS_SESSION_DETACH;
1513
1514 if ( rcExit == RTEXITCODE_SUCCESS
1515 && FAILED(rc))
1516 {
1517 /* Make sure an appropriate exit code is set on error. */
1518 rcExit = RTEXITCODE_FAILURE;
1519 }
1520
1521 return rcExit;
1522}
1523
1524/**
1525 * Creates a copy context structure which then can be used with various
1526 * guest control copy functions. Needs to be free'd with ctrlCopyContextFree().
1527 *
1528 * @return IPRT status code.
1529 * @param pCtx Pointer to command context.
1530 * @param fDryRun Flag indicating if we want to run a dry run only.
1531 * @param fHostToGuest Flag indicating if we want to copy from host to guest
1532 * or vice versa.
1533 * @param strSessionName Session name (only for identification purposes).
1534 * @param ppContext Pointer which receives the allocated copy context.
1535 */
1536static int ctrlCopyContextCreate(PGCTLCMDCTX pCtx, bool fDryRun, bool fHostToGuest,
1537 const Utf8Str &strSessionName,
1538 PCOPYCONTEXT *ppContext)
1539{
1540 AssertPtrReturn(pCtx, VERR_INVALID_POINTER);
1541
1542 int vrc = VINF_SUCCESS;
1543 try
1544 {
1545 PCOPYCONTEXT pContext = new COPYCONTEXT();
1546
1547 pContext->pCmdCtx = pCtx;
1548 pContext->fDryRun = fDryRun;
1549 pContext->fHostToGuest = fHostToGuest;
1550
1551 *ppContext = pContext;
1552 }
1553 catch (std::bad_alloc)
1554 {
1555 vrc = VERR_NO_MEMORY;
1556 }
1557
1558 return vrc;
1559}
1560
1561/**
1562 * Frees are previously allocated copy context structure.
1563 *
1564 * @param pContext Pointer to copy context to free.
1565 */
1566static void ctrlCopyContextFree(PCOPYCONTEXT pContext)
1567{
1568 if (pContext)
1569 delete pContext;
1570}
1571
1572/**
1573 * Translates a source path to a destination path (can be both sides,
1574 * either host or guest). The source root is needed to determine the start
1575 * of the relative source path which also needs to present in the destination
1576 * path.
1577 *
1578 * @return IPRT status code.
1579 * @param pszSourceRoot Source root path. No trailing directory slash!
1580 * @param pszSource Actual source to transform. Must begin with
1581 * the source root path!
1582 * @param pszDest Destination path.
1583 * @param ppszTranslated Pointer to the allocated, translated destination
1584 * path. Must be free'd with RTStrFree().
1585 */
1586static int ctrlCopyTranslatePath(const char *pszSourceRoot, const char *pszSource,
1587 const char *pszDest, char **ppszTranslated)
1588{
1589 AssertPtrReturn(pszSourceRoot, VERR_INVALID_POINTER);
1590 AssertPtrReturn(pszSource, VERR_INVALID_POINTER);
1591 AssertPtrReturn(pszDest, VERR_INVALID_POINTER);
1592 AssertPtrReturn(ppszTranslated, VERR_INVALID_POINTER);
1593#if 0 /** @todo r=bird: It does not make sense to apply host path parsing semantics onto guest paths. I hope this code isn't mixing host/guest paths in the same way anywhere else... @bugref{6344} */
1594 AssertReturn(RTPathStartsWith(pszSource, pszSourceRoot), VERR_INVALID_PARAMETER);
1595#endif
1596
1597 /* Construct the relative dest destination path by "subtracting" the
1598 * source from the source root, e.g.
1599 *
1600 * source root path = "e:\foo\", source = "e:\foo\bar"
1601 * dest = "d:\baz\"
1602 * translated = "d:\baz\bar\"
1603 */
1604 char szTranslated[RTPATH_MAX];
1605 size_t srcOff = strlen(pszSourceRoot);
1606 AssertReturn(srcOff, VERR_INVALID_PARAMETER);
1607
1608 char *pszDestPath = RTStrDup(pszDest);
1609 AssertPtrReturn(pszDestPath, VERR_NO_MEMORY);
1610
1611 int vrc;
1612 if (!RTPathFilename(pszDestPath))
1613 {
1614 vrc = RTPathJoin(szTranslated, sizeof(szTranslated),
1615 pszDestPath, &pszSource[srcOff]);
1616 }
1617 else
1618 {
1619 char *pszDestFileName = RTStrDup(RTPathFilename(pszDestPath));
1620 if (pszDestFileName)
1621 {
1622 RTPathStripFilename(pszDestPath);
1623 vrc = RTPathJoin(szTranslated, sizeof(szTranslated),
1624 pszDestPath, pszDestFileName);
1625 RTStrFree(pszDestFileName);
1626 }
1627 else
1628 vrc = VERR_NO_MEMORY;
1629 }
1630 RTStrFree(pszDestPath);
1631
1632 if (RT_SUCCESS(vrc))
1633 {
1634 *ppszTranslated = RTStrDup(szTranslated);
1635#if 0
1636 RTPrintf("Root: %s, Source: %s, Dest: %s, Translated: %s\n",
1637 pszSourceRoot, pszSource, pszDest, *ppszTranslated);
1638#endif
1639 }
1640 return vrc;
1641}
1642
1643#ifdef DEBUG_andy
1644static int tstTranslatePath()
1645{
1646 RTAssertSetMayPanic(false /* Do not freak out, please. */);
1647
1648 static struct
1649 {
1650 const char *pszSourceRoot;
1651 const char *pszSource;
1652 const char *pszDest;
1653 const char *pszTranslated;
1654 int iResult;
1655 } aTests[] =
1656 {
1657 /* Invalid stuff. */
1658 { NULL, NULL, NULL, NULL, VERR_INVALID_POINTER },
1659#ifdef RT_OS_WINDOWS
1660 /* Windows paths. */
1661 { "c:\\foo", "c:\\foo\\bar.txt", "c:\\test", "c:\\test\\bar.txt", VINF_SUCCESS },
1662 { "c:\\foo", "c:\\foo\\baz\\bar.txt", "c:\\test", "c:\\test\\baz\\bar.txt", VINF_SUCCESS },
1663#else /* RT_OS_WINDOWS */
1664 { "/home/test/foo", "/home/test/foo/bar.txt", "/opt/test", "/opt/test/bar.txt", VINF_SUCCESS },
1665 { "/home/test/foo", "/home/test/foo/baz/bar.txt", "/opt/test", "/opt/test/baz/bar.txt", VINF_SUCCESS },
1666#endif /* !RT_OS_WINDOWS */
1667 /* Mixed paths*/
1668 /** @todo */
1669 { NULL }
1670 };
1671
1672 size_t iTest = 0;
1673 for (iTest; iTest < RT_ELEMENTS(aTests); iTest++)
1674 {
1675 RTPrintf("=> Test %d\n", iTest);
1676 RTPrintf("\tSourceRoot=%s, Source=%s, Dest=%s\n",
1677 aTests[iTest].pszSourceRoot, aTests[iTest].pszSource, aTests[iTest].pszDest);
1678
1679 char *pszTranslated = NULL;
1680 int iResult = ctrlCopyTranslatePath(aTests[iTest].pszSourceRoot, aTests[iTest].pszSource,
1681 aTests[iTest].pszDest, &pszTranslated);
1682 if (iResult != aTests[iTest].iResult)
1683 {
1684 RTPrintf("\tReturned %Rrc, expected %Rrc\n",
1685 iResult, aTests[iTest].iResult);
1686 }
1687 else if ( pszTranslated
1688 && strcmp(pszTranslated, aTests[iTest].pszTranslated))
1689 {
1690 RTPrintf("\tReturned translated path %s, expected %s\n",
1691 pszTranslated, aTests[iTest].pszTranslated);
1692 }
1693
1694 if (pszTranslated)
1695 {
1696 RTPrintf("\tTranslated=%s\n", pszTranslated);
1697 RTStrFree(pszTranslated);
1698 }
1699 }
1700
1701 return VINF_SUCCESS; /* @todo */
1702}
1703#endif
1704
1705/**
1706 * Creates a directory on the destination, based on the current copy
1707 * context.
1708 *
1709 * @return IPRT status code.
1710 * @param pContext Pointer to current copy control context.
1711 * @param pszDir Directory to create.
1712 */
1713static int ctrlCopyDirCreate(PCOPYCONTEXT pContext, const char *pszDir)
1714{
1715 AssertPtrReturn(pContext, VERR_INVALID_POINTER);
1716 AssertPtrReturn(pszDir, VERR_INVALID_POINTER);
1717
1718 bool fDirExists;
1719 int vrc = ctrlCopyDirExists(pContext, pContext->fHostToGuest, pszDir, &fDirExists);
1720 if ( RT_SUCCESS(vrc)
1721 && fDirExists)
1722 {
1723 if (pContext->pCmdCtx->fVerbose)
1724 RTPrintf("Directory \"%s\" already exists\n", pszDir);
1725 return VINF_SUCCESS;
1726 }
1727
1728 /* If querying for a directory existence fails there's no point of even trying
1729 * to create such a directory. */
1730 if (RT_FAILURE(vrc))
1731 return vrc;
1732
1733 if (pContext->pCmdCtx->fVerbose)
1734 RTPrintf("Creating directory \"%s\" ...\n", pszDir);
1735
1736 if (pContext->fDryRun)
1737 return VINF_SUCCESS;
1738
1739 if (pContext->fHostToGuest) /* We want to create directories on the guest. */
1740 {
1741 SafeArray<DirectoryCreateFlag_T> dirCreateFlags;
1742 dirCreateFlags.push_back(DirectoryCreateFlag_Parents);
1743 HRESULT rc = pContext->pCmdCtx->pGuestSession->DirectoryCreate(Bstr(pszDir).raw(),
1744 0700, ComSafeArrayAsInParam(dirCreateFlags));
1745 if (FAILED(rc))
1746 vrc = ctrlPrintError(pContext->pCmdCtx->pGuestSession, COM_IIDOF(IGuestSession));
1747 }
1748 else /* ... or on the host. */
1749 {
1750 vrc = RTDirCreateFullPath(pszDir, 0700);
1751 if (vrc == VERR_ALREADY_EXISTS)
1752 vrc = VINF_SUCCESS;
1753 }
1754 return vrc;
1755}
1756
1757/**
1758 * Checks whether a specific host/guest directory exists.
1759 *
1760 * @return IPRT status code.
1761 * @param pContext Pointer to current copy control context.
1762 * @param fOnGuest true if directory needs to be checked on the guest
1763 * or false if on the host.
1764 * @param pszDir Actual directory to check.
1765 * @param fExists Pointer which receives the result if the
1766 * given directory exists or not.
1767 */
1768static int ctrlCopyDirExists(PCOPYCONTEXT pContext, bool fOnGuest,
1769 const char *pszDir, bool *fExists)
1770{
1771 AssertPtrReturn(pContext, false);
1772 AssertPtrReturn(pszDir, false);
1773 AssertPtrReturn(fExists, false);
1774
1775 int vrc = VINF_SUCCESS;
1776 if (fOnGuest)
1777 {
1778 BOOL fDirExists = FALSE;
1779 HRESULT rc = pContext->pCmdCtx->pGuestSession->DirectoryExists(Bstr(pszDir).raw(), &fDirExists);
1780 if (FAILED(rc))
1781 vrc = ctrlPrintError(pContext->pCmdCtx->pGuestSession, COM_IIDOF(IGuestSession));
1782 else
1783 *fExists = fDirExists ? true : false;
1784 }
1785 else
1786 *fExists = RTDirExists(pszDir);
1787 return vrc;
1788}
1789
1790/**
1791 * Checks whether a specific directory exists on the destination, based
1792 * on the current copy context.
1793 *
1794 * @return IPRT status code.
1795 * @param pContext Pointer to current copy control context.
1796 * @param pszDir Actual directory to check.
1797 * @param fExists Pointer which receives the result if the
1798 * given directory exists or not.
1799 */
1800static int ctrlCopyDirExistsOnDest(PCOPYCONTEXT pContext, const char *pszDir,
1801 bool *fExists)
1802{
1803 return ctrlCopyDirExists(pContext, pContext->fHostToGuest,
1804 pszDir, fExists);
1805}
1806
1807/**
1808 * Checks whether a specific directory exists on the source, based
1809 * on the current copy context.
1810 *
1811 * @return IPRT status code.
1812 * @param pContext Pointer to current copy control context.
1813 * @param pszDir Actual directory to check.
1814 * @param fExists Pointer which receives the result if the
1815 * given directory exists or not.
1816 */
1817static int ctrlCopyDirExistsOnSource(PCOPYCONTEXT pContext, const char *pszDir,
1818 bool *fExists)
1819{
1820 return ctrlCopyDirExists(pContext, !pContext->fHostToGuest,
1821 pszDir, fExists);
1822}
1823
1824/**
1825 * Checks whether a specific host/guest file exists.
1826 *
1827 * @return IPRT status code.
1828 * @param pContext Pointer to current copy control context.
1829 * @param bGuest true if file needs to be checked on the guest
1830 * or false if on the host.
1831 * @param pszFile Actual file to check.
1832 * @param fExists Pointer which receives the result if the
1833 * given file exists or not.
1834 */
1835static int ctrlCopyFileExists(PCOPYCONTEXT pContext, bool bOnGuest,
1836 const char *pszFile, bool *fExists)
1837{
1838 AssertPtrReturn(pContext, false);
1839 AssertPtrReturn(pszFile, false);
1840 AssertPtrReturn(fExists, false);
1841
1842 int vrc = VINF_SUCCESS;
1843 if (bOnGuest)
1844 {
1845 BOOL fFileExists = FALSE;
1846 HRESULT rc = pContext->pCmdCtx->pGuestSession->FileExists(Bstr(pszFile).raw(), &fFileExists);
1847 if (FAILED(rc))
1848 vrc = ctrlPrintError(pContext->pCmdCtx->pGuestSession, COM_IIDOF(IGuestSession));
1849 else
1850 *fExists = fFileExists ? true : false;
1851 }
1852 else
1853 *fExists = RTFileExists(pszFile);
1854 return vrc;
1855}
1856
1857/**
1858 * Checks whether a specific file exists on the destination, based on the
1859 * current copy context.
1860 *
1861 * @return IPRT status code.
1862 * @param pContext Pointer to current copy control context.
1863 * @param pszFile Actual file to check.
1864 * @param fExists Pointer which receives the result if the
1865 * given file exists or not.
1866 */
1867static int ctrlCopyFileExistsOnDest(PCOPYCONTEXT pContext, const char *pszFile,
1868 bool *fExists)
1869{
1870 return ctrlCopyFileExists(pContext, pContext->fHostToGuest,
1871 pszFile, fExists);
1872}
1873
1874/**
1875 * Checks whether a specific file exists on the source, based on the
1876 * current copy context.
1877 *
1878 * @return IPRT status code.
1879 * @param pContext Pointer to current copy control context.
1880 * @param pszFile Actual file to check.
1881 * @param fExists Pointer which receives the result if the
1882 * given file exists or not.
1883 */
1884static int ctrlCopyFileExistsOnSource(PCOPYCONTEXT pContext, const char *pszFile,
1885 bool *fExists)
1886{
1887 return ctrlCopyFileExists(pContext, !pContext->fHostToGuest,
1888 pszFile, fExists);
1889}
1890
1891/**
1892 * Copies a source file to the destination.
1893 *
1894 * @return IPRT status code.
1895 * @param pContext Pointer to current copy control context.
1896 * @param pszFileSource Source file to copy to the destination.
1897 * @param pszFileDest Name of copied file on the destination.
1898 * @param fFlags Copy flags. No supported at the moment and needs
1899 * to be set to 0.
1900 */
1901static int ctrlCopyFileToDest(PCOPYCONTEXT pContext, const char *pszFileSource,
1902 const char *pszFileDest, uint32_t fFlags)
1903{
1904 AssertPtrReturn(pContext, VERR_INVALID_POINTER);
1905 AssertPtrReturn(pszFileSource, VERR_INVALID_POINTER);
1906 AssertPtrReturn(pszFileDest, VERR_INVALID_POINTER);
1907 AssertReturn(!fFlags, VERR_INVALID_POINTER); /* No flags supported yet. */
1908
1909 if (pContext->pCmdCtx->fVerbose)
1910 RTPrintf("Copying \"%s\" to \"%s\" ...\n",
1911 pszFileSource, pszFileDest);
1912
1913 if (pContext->fDryRun)
1914 return VINF_SUCCESS;
1915
1916 int vrc = VINF_SUCCESS;
1917 ComPtr<IProgress> pProgress;
1918 HRESULT rc;
1919 if (pContext->fHostToGuest)
1920 {
1921 SafeArray<CopyFileFlag_T> copyFlags;
1922 rc = pContext->pCmdCtx->pGuestSession->CopyTo(Bstr(pszFileSource).raw(), Bstr(pszFileDest).raw(),
1923 ComSafeArrayAsInParam(copyFlags),
1924 pProgress.asOutParam());
1925 }
1926 else
1927 {
1928 SafeArray<CopyFileFlag_T> copyFlags;
1929 rc = pContext->pCmdCtx->pGuestSession->CopyFrom(Bstr(pszFileSource).raw(), Bstr(pszFileDest).raw(),
1930 ComSafeArrayAsInParam(copyFlags),
1931 pProgress.asOutParam());
1932 }
1933
1934 if (FAILED(rc))
1935 {
1936 vrc = ctrlPrintError(pContext->pCmdCtx->pGuestSession, COM_IIDOF(IGuestSession));
1937 }
1938 else
1939 {
1940 if (pContext->pCmdCtx->fVerbose)
1941 rc = showProgress(pProgress);
1942 else
1943 rc = pProgress->WaitForCompletion(-1 /* No timeout */);
1944 if (SUCCEEDED(rc))
1945 CHECK_PROGRESS_ERROR(pProgress, ("File copy failed"));
1946 vrc = ctrlPrintProgressError(pProgress);
1947 }
1948
1949 return vrc;
1950}
1951
1952/**
1953 * Copys a directory (tree) from host to the guest.
1954 *
1955 * @return IPRT status code.
1956 * @param pContext Pointer to current copy control context.
1957 * @param pszSource Source directory on the host to copy to the guest.
1958 * @param pszFilter DOS-style wildcard filter (?, *). Optional.
1959 * @param pszDest Destination directory on the guest.
1960 * @param fFlags Copy flags, such as recursive copying.
1961 * @param pszSubDir Current sub directory to handle. Needs to NULL and only
1962 * is needed for recursion.
1963 */
1964static int ctrlCopyDirToGuest(PCOPYCONTEXT pContext,
1965 const char *pszSource, const char *pszFilter,
1966 const char *pszDest, uint32_t fFlags,
1967 const char *pszSubDir /* For recursion. */)
1968{
1969 AssertPtrReturn(pContext, VERR_INVALID_POINTER);
1970 AssertPtrReturn(pszSource, VERR_INVALID_POINTER);
1971 /* Filter is optional. */
1972 AssertPtrReturn(pszDest, VERR_INVALID_POINTER);
1973 /* Sub directory is optional. */
1974
1975 /*
1976 * Construct current path.
1977 */
1978 char szCurDir[RTPATH_MAX];
1979 int vrc = RTStrCopy(szCurDir, sizeof(szCurDir), pszSource);
1980 if (RT_SUCCESS(vrc) && pszSubDir)
1981 vrc = RTPathAppend(szCurDir, sizeof(szCurDir), pszSubDir);
1982
1983 if (pContext->pCmdCtx->fVerbose)
1984 RTPrintf("Processing host directory: %s\n", szCurDir);
1985
1986 /* Flag indicating whether the current directory was created on the
1987 * target or not. */
1988 bool fDirCreated = false;
1989
1990 /*
1991 * Open directory without a filter - RTDirOpenFiltered unfortunately
1992 * cannot handle sub directories so we have to do the filtering ourselves.
1993 */
1994 PRTDIR pDir = NULL;
1995 if (RT_SUCCESS(vrc))
1996 {
1997 vrc = RTDirOpen(&pDir, szCurDir);
1998 if (RT_FAILURE(vrc))
1999 pDir = NULL;
2000 }
2001 if (RT_SUCCESS(vrc))
2002 {
2003 /*
2004 * Enumerate the directory tree.
2005 */
2006 while (RT_SUCCESS(vrc))
2007 {
2008 RTDIRENTRY DirEntry;
2009 vrc = RTDirRead(pDir, &DirEntry, NULL);
2010 if (RT_FAILURE(vrc))
2011 {
2012 if (vrc == VERR_NO_MORE_FILES)
2013 vrc = VINF_SUCCESS;
2014 break;
2015 }
2016 /** @todo r=bird: This ain't gonna work on most UNIX file systems because
2017 * enmType is RTDIRENTRYTYPE_UNKNOWN. This is clearly documented in
2018 * RTDIRENTRY::enmType. For trunk, RTDirQueryUnknownType can be used. */
2019 switch (DirEntry.enmType)
2020 {
2021 case RTDIRENTRYTYPE_DIRECTORY:
2022 {
2023 /* Skip "." and ".." entries. */
2024 if ( !strcmp(DirEntry.szName, ".")
2025 || !strcmp(DirEntry.szName, ".."))
2026 break;
2027
2028 if (pContext->pCmdCtx->fVerbose)
2029 RTPrintf("Directory: %s\n", DirEntry.szName);
2030
2031 if (fFlags & CopyFileFlag_Recursive)
2032 {
2033 char *pszNewSub = NULL;
2034 if (pszSubDir)
2035 pszNewSub = RTPathJoinA(pszSubDir, DirEntry.szName);
2036 else
2037 {
2038 pszNewSub = RTStrDup(DirEntry.szName);
2039 RTPathStripTrailingSlash(pszNewSub);
2040 }
2041
2042 if (pszNewSub)
2043 {
2044 vrc = ctrlCopyDirToGuest(pContext,
2045 pszSource, pszFilter,
2046 pszDest, fFlags, pszNewSub);
2047 RTStrFree(pszNewSub);
2048 }
2049 else
2050 vrc = VERR_NO_MEMORY;
2051 }
2052 break;
2053 }
2054
2055 case RTDIRENTRYTYPE_SYMLINK:
2056 if ( (fFlags & CopyFileFlag_Recursive)
2057 && (fFlags & CopyFileFlag_FollowLinks))
2058 {
2059 /* Fall through to next case is intentional. */
2060 }
2061 else
2062 break;
2063
2064 case RTDIRENTRYTYPE_FILE:
2065 {
2066 if ( pszFilter
2067 && !RTStrSimplePatternMatch(pszFilter, DirEntry.szName))
2068 {
2069 break; /* Filter does not match. */
2070 }
2071
2072 if (pContext->pCmdCtx->fVerbose)
2073 RTPrintf("File: %s\n", DirEntry.szName);
2074
2075 if (!fDirCreated)
2076 {
2077 char *pszDestDir;
2078 vrc = ctrlCopyTranslatePath(pszSource, szCurDir,
2079 pszDest, &pszDestDir);
2080 if (RT_SUCCESS(vrc))
2081 {
2082 vrc = ctrlCopyDirCreate(pContext, pszDestDir);
2083 RTStrFree(pszDestDir);
2084
2085 fDirCreated = true;
2086 }
2087 }
2088
2089 if (RT_SUCCESS(vrc))
2090 {
2091 char *pszFileSource = RTPathJoinA(szCurDir, DirEntry.szName);
2092 if (pszFileSource)
2093 {
2094 char *pszFileDest;
2095 vrc = ctrlCopyTranslatePath(pszSource, pszFileSource,
2096 pszDest, &pszFileDest);
2097 if (RT_SUCCESS(vrc))
2098 {
2099 vrc = ctrlCopyFileToDest(pContext, pszFileSource,
2100 pszFileDest, 0 /* Flags */);
2101 RTStrFree(pszFileDest);
2102 }
2103 RTStrFree(pszFileSource);
2104 }
2105 }
2106 break;
2107 }
2108
2109 default:
2110 break;
2111 }
2112 if (RT_FAILURE(vrc))
2113 break;
2114 }
2115
2116 RTDirClose(pDir);
2117 }
2118 return vrc;
2119}
2120
2121/**
2122 * Copys a directory (tree) from guest to the host.
2123 *
2124 * @return IPRT status code.
2125 * @param pContext Pointer to current copy control context.
2126 * @param pszSource Source directory on the guest to copy to the host.
2127 * @param pszFilter DOS-style wildcard filter (?, *). Optional.
2128 * @param pszDest Destination directory on the host.
2129 * @param fFlags Copy flags, such as recursive copying.
2130 * @param pszSubDir Current sub directory to handle. Needs to NULL and only
2131 * is needed for recursion.
2132 */
2133static int ctrlCopyDirToHost(PCOPYCONTEXT pContext,
2134 const char *pszSource, const char *pszFilter,
2135 const char *pszDest, uint32_t fFlags,
2136 const char *pszSubDir /* For recursion. */)
2137{
2138 AssertPtrReturn(pContext, VERR_INVALID_POINTER);
2139 AssertPtrReturn(pszSource, VERR_INVALID_POINTER);
2140 /* Filter is optional. */
2141 AssertPtrReturn(pszDest, VERR_INVALID_POINTER);
2142 /* Sub directory is optional. */
2143
2144 /*
2145 * Construct current path.
2146 */
2147 char szCurDir[RTPATH_MAX];
2148 int vrc = RTStrCopy(szCurDir, sizeof(szCurDir), pszSource);
2149 if (RT_SUCCESS(vrc) && pszSubDir)
2150 vrc = RTPathAppend(szCurDir, sizeof(szCurDir), pszSubDir);
2151
2152 if (RT_FAILURE(vrc))
2153 return vrc;
2154
2155 if (pContext->pCmdCtx->fVerbose)
2156 RTPrintf("Processing guest directory: %s\n", szCurDir);
2157
2158 /* Flag indicating whether the current directory was created on the
2159 * target or not. */
2160 bool fDirCreated = false;
2161 SafeArray<DirectoryOpenFlag_T> dirOpenFlags; /* No flags supported yet. */
2162 ComPtr<IGuestDirectory> pDirectory;
2163 HRESULT rc = pContext->pCmdCtx->pGuestSession->DirectoryOpen(Bstr(szCurDir).raw(), Bstr(pszFilter).raw(),
2164 ComSafeArrayAsInParam(dirOpenFlags),
2165 pDirectory.asOutParam());
2166 if (FAILED(rc))
2167 return ctrlPrintError(pContext->pCmdCtx->pGuestSession, COM_IIDOF(IGuestSession));
2168 ComPtr<IFsObjInfo> dirEntry;
2169 while (true)
2170 {
2171 rc = pDirectory->Read(dirEntry.asOutParam());
2172 if (FAILED(rc))
2173 break;
2174
2175 FsObjType_T enmType;
2176 dirEntry->COMGETTER(Type)(&enmType);
2177
2178 Bstr strName;
2179 dirEntry->COMGETTER(Name)(strName.asOutParam());
2180
2181 switch (enmType)
2182 {
2183 case FsObjType_Directory:
2184 {
2185 Assert(!strName.isEmpty());
2186
2187 /* Skip "." and ".." entries. */
2188 if ( !strName.compare(Bstr("."))
2189 || !strName.compare(Bstr("..")))
2190 break;
2191
2192 if (pContext->pCmdCtx->fVerbose)
2193 {
2194 Utf8Str strDir(strName);
2195 RTPrintf("Directory: %s\n", strDir.c_str());
2196 }
2197
2198 if (fFlags & CopyFileFlag_Recursive)
2199 {
2200 Utf8Str strDir(strName);
2201 char *pszNewSub = NULL;
2202 if (pszSubDir)
2203 pszNewSub = RTPathJoinA(pszSubDir, strDir.c_str());
2204 else
2205 {
2206 pszNewSub = RTStrDup(strDir.c_str());
2207 RTPathStripTrailingSlash(pszNewSub);
2208 }
2209 if (pszNewSub)
2210 {
2211 vrc = ctrlCopyDirToHost(pContext,
2212 pszSource, pszFilter,
2213 pszDest, fFlags, pszNewSub);
2214 RTStrFree(pszNewSub);
2215 }
2216 else
2217 vrc = VERR_NO_MEMORY;
2218 }
2219 break;
2220 }
2221
2222 case FsObjType_Symlink:
2223 if ( (fFlags & CopyFileFlag_Recursive)
2224 && (fFlags & CopyFileFlag_FollowLinks))
2225 {
2226 /* Fall through to next case is intentional. */
2227 }
2228 else
2229 break;
2230
2231 case FsObjType_File:
2232 {
2233 Assert(!strName.isEmpty());
2234
2235 Utf8Str strFile(strName);
2236 if ( pszFilter
2237 && !RTStrSimplePatternMatch(pszFilter, strFile.c_str()))
2238 {
2239 break; /* Filter does not match. */
2240 }
2241
2242 if (pContext->pCmdCtx->fVerbose)
2243 RTPrintf("File: %s\n", strFile.c_str());
2244
2245 if (!fDirCreated)
2246 {
2247 char *pszDestDir;
2248 vrc = ctrlCopyTranslatePath(pszSource, szCurDir,
2249 pszDest, &pszDestDir);
2250 if (RT_SUCCESS(vrc))
2251 {
2252 vrc = ctrlCopyDirCreate(pContext, pszDestDir);
2253 RTStrFree(pszDestDir);
2254
2255 fDirCreated = true;
2256 }
2257 }
2258
2259 if (RT_SUCCESS(vrc))
2260 {
2261 char *pszFileSource = RTPathJoinA(szCurDir, strFile.c_str());
2262 if (pszFileSource)
2263 {
2264 char *pszFileDest;
2265 vrc = ctrlCopyTranslatePath(pszSource, pszFileSource,
2266 pszDest, &pszFileDest);
2267 if (RT_SUCCESS(vrc))
2268 {
2269 vrc = ctrlCopyFileToDest(pContext, pszFileSource,
2270 pszFileDest, 0 /* Flags */);
2271 RTStrFree(pszFileDest);
2272 }
2273 RTStrFree(pszFileSource);
2274 }
2275 else
2276 vrc = VERR_NO_MEMORY;
2277 }
2278 break;
2279 }
2280
2281 default:
2282 RTPrintf("Warning: Directory entry of type %ld not handled, skipping ...\n",
2283 enmType);
2284 break;
2285 }
2286
2287 if (RT_FAILURE(vrc))
2288 break;
2289 }
2290
2291 if (RT_UNLIKELY(FAILED(rc)))
2292 {
2293 switch (rc)
2294 {
2295 case E_ABORT: /* No more directory entries left to process. */
2296 break;
2297
2298 case VBOX_E_FILE_ERROR: /* Current entry cannot be accessed to
2299 to missing rights. */
2300 {
2301 RTPrintf("Warning: Cannot access \"%s\", skipping ...\n",
2302 szCurDir);
2303 break;
2304 }
2305
2306 default:
2307 vrc = ctrlPrintError(pDirectory, COM_IIDOF(IGuestDirectory));
2308 break;
2309 }
2310 }
2311
2312 HRESULT rc2 = pDirectory->Close();
2313 if (FAILED(rc2))
2314 {
2315 int vrc2 = ctrlPrintError(pDirectory, COM_IIDOF(IGuestDirectory));
2316 if (RT_SUCCESS(vrc))
2317 vrc = vrc2;
2318 }
2319 else if (SUCCEEDED(rc))
2320 rc = rc2;
2321
2322 return vrc;
2323}
2324
2325/**
2326 * Copys a directory (tree) to the destination, based on the current copy
2327 * context.
2328 *
2329 * @return IPRT status code.
2330 * @param pContext Pointer to current copy control context.
2331 * @param pszSource Source directory to copy to the destination.
2332 * @param pszFilter DOS-style wildcard filter (?, *). Optional.
2333 * @param pszDest Destination directory where to copy in the source
2334 * source directory.
2335 * @param fFlags Copy flags, such as recursive copying.
2336 */
2337static int ctrlCopyDirToDest(PCOPYCONTEXT pContext,
2338 const char *pszSource, const char *pszFilter,
2339 const char *pszDest, uint32_t fFlags)
2340{
2341 if (pContext->fHostToGuest)
2342 return ctrlCopyDirToGuest(pContext, pszSource, pszFilter,
2343 pszDest, fFlags, NULL /* Sub directory, only for recursion. */);
2344 return ctrlCopyDirToHost(pContext, pszSource, pszFilter,
2345 pszDest, fFlags, NULL /* Sub directory, only for recursion. */);
2346}
2347
2348/**
2349 * Creates a source root by stripping file names or filters of the specified source.
2350 *
2351 * @return IPRT status code.
2352 * @param pszSource Source to create source root for.
2353 * @param ppszSourceRoot Pointer that receives the allocated source root. Needs
2354 * to be free'd with ctrlCopyFreeSourceRoot().
2355 */
2356static int ctrlCopyCreateSourceRoot(const char *pszSource, char **ppszSourceRoot)
2357{
2358 AssertPtrReturn(pszSource, VERR_INVALID_POINTER);
2359 AssertPtrReturn(ppszSourceRoot, VERR_INVALID_POINTER);
2360
2361 char *pszNewRoot = RTStrDup(pszSource);
2362 AssertPtrReturn(pszNewRoot, VERR_NO_MEMORY);
2363
2364 size_t lenRoot = strlen(pszNewRoot);
2365 if ( lenRoot
2366 && pszNewRoot[lenRoot - 1] == '/'
2367 && pszNewRoot[lenRoot - 1] == '\\'
2368 && lenRoot > 1
2369 && pszNewRoot[lenRoot - 2] == '/'
2370 && pszNewRoot[lenRoot - 2] == '\\')
2371 {
2372 *ppszSourceRoot = pszNewRoot;
2373 if (lenRoot > 1)
2374 *ppszSourceRoot[lenRoot - 2] = '\0';
2375 *ppszSourceRoot[lenRoot - 1] = '\0';
2376 }
2377 else
2378 {
2379 /* If there's anything (like a file name or a filter),
2380 * strip it! */
2381 RTPathStripFilename(pszNewRoot);
2382 *ppszSourceRoot = pszNewRoot;
2383 }
2384
2385 return VINF_SUCCESS;
2386}
2387
2388/**
2389 * Frees a previously allocated source root.
2390 *
2391 * @return IPRT status code.
2392 * @param pszSourceRoot Source root to free.
2393 */
2394static void ctrlCopyFreeSourceRoot(char *pszSourceRoot)
2395{
2396 RTStrFree(pszSourceRoot);
2397}
2398
2399static RTEXITCODE handleCtrlCopy(PGCTLCMDCTX pCtx, bool fHostToGuest)
2400{
2401 AssertPtrReturn(pCtx, RTEXITCODE_FAILURE);
2402
2403 /** @todo r=bird: This command isn't very unix friendly in general. mkdir
2404 * is much better (partly because it is much simpler of course). The main
2405 * arguments against this is that (1) all but two options conflicts with
2406 * what 'man cp' tells me on a GNU/Linux system, (2) wildchar matching is
2407 * done windows CMD style (though not in a 100% compatible way), and (3)
2408 * that only one source is allowed - efficiently sabotaging default
2409 * wildcard expansion by a unix shell. The best solution here would be
2410 * two different variant, one windowsy (xcopy) and one unixy (gnu cp). */
2411
2412 /*
2413 * IGuest::CopyToGuest is kept as simple as possible to let the developer choose
2414 * what and how to implement the file enumeration/recursive lookup, like VBoxManage
2415 * does in here.
2416 */
2417 static const RTGETOPTDEF s_aOptions[] =
2418 {
2419 { "--dryrun", GETOPTDEF_COPY_DRYRUN, RTGETOPT_REQ_NOTHING },
2420 { "--follow", GETOPTDEF_COPY_FOLLOW, RTGETOPT_REQ_NOTHING },
2421 { "--recursive", 'R', RTGETOPT_REQ_NOTHING },
2422 { "--target-directory", GETOPTDEF_COPY_TARGETDIR, RTGETOPT_REQ_STRING }
2423 };
2424
2425 int ch;
2426 RTGETOPTUNION ValueUnion;
2427 RTGETOPTSTATE GetState;
2428 RTGetOptInit(&GetState, pCtx->iArgc, pCtx->ppaArgv,
2429 s_aOptions, RT_ELEMENTS(s_aOptions), pCtx->iFirstArgc, RTGETOPTINIT_FLAGS_OPTS_FIRST);
2430
2431 Utf8Str strSource;
2432 Utf8Str strDest;
2433 uint32_t fFlags = CopyFileFlag_None;
2434 bool fCopyRecursive = false;
2435 bool fDryRun = false;
2436 uint32_t uUsage = fHostToGuest ? USAGE_GSTCTRL_COPYTO : USAGE_GSTCTRL_COPYFROM;
2437
2438 SOURCEVEC vecSources;
2439
2440 int vrc = VINF_SUCCESS;
2441 while ((ch = RTGetOpt(&GetState, &ValueUnion)))
2442 {
2443 /* For options that require an argument, ValueUnion has received the value. */
2444 switch (ch)
2445 {
2446 case GETOPTDEF_COPY_DRYRUN:
2447 fDryRun = true;
2448 break;
2449
2450 case GETOPTDEF_COPY_FOLLOW:
2451 fFlags |= CopyFileFlag_FollowLinks;
2452 break;
2453
2454 case 'R': /* Recursive processing */
2455 fFlags |= CopyFileFlag_Recursive;
2456 break;
2457
2458 case GETOPTDEF_COPY_TARGETDIR:
2459 strDest = ValueUnion.psz;
2460 break;
2461
2462 case VINF_GETOPT_NOT_OPTION:
2463 {
2464 /* Last argument and no destination specified with
2465 * --target-directory yet? Then use the current
2466 * (= last) argument as destination. */
2467 if ( pCtx->iArgc == GetState.iNext
2468 && strDest.isEmpty())
2469 {
2470 strDest = ValueUnion.psz;
2471 }
2472 else
2473 {
2474 /* Save the source directory. */
2475 vecSources.push_back(SOURCEFILEENTRY(ValueUnion.psz));
2476 }
2477 break;
2478 }
2479
2480 default:
2481 return errorGetOptEx(USAGE_GUESTCONTROL, uUsage, ch, &ValueUnion);
2482 }
2483 }
2484
2485 if (!vecSources.size())
2486 return errorSyntaxEx(USAGE_GUESTCONTROL, uUsage,
2487 "No source(s) specified!");
2488
2489 if (strDest.isEmpty())
2490 return errorSyntaxEx(USAGE_GUESTCONTROL, uUsage,
2491 "No destination specified!");
2492
2493 /*
2494 * Done parsing arguments, do some more preparations.
2495 */
2496 if (pCtx->fVerbose)
2497 {
2498 if (fHostToGuest)
2499 RTPrintf("Copying from host to guest ...\n");
2500 else
2501 RTPrintf("Copying from guest to host ...\n");
2502 if (fDryRun)
2503 RTPrintf("Dry run - no files copied!\n");
2504 }
2505
2506 /* Create the copy context -- it contains all information
2507 * the routines need to know when handling the actual copying. */
2508 PCOPYCONTEXT pContext = NULL;
2509 vrc = ctrlCopyContextCreate(pCtx, fDryRun, fHostToGuest,
2510 fHostToGuest
2511 ? "VBoxManage Guest Control - Copy to guest"
2512 : "VBoxManage Guest Control - Copy from guest", &pContext);
2513 if (RT_FAILURE(vrc))
2514 {
2515 RTMsgError("Unable to create copy context, rc=%Rrc\n", vrc);
2516 return RTEXITCODE_FAILURE;
2517 }
2518
2519 /* If the destination is a path, (try to) create it. */
2520 const char *pszDest = strDest.c_str();
2521/** @todo r=bird: RTPathFilename and RTPathStripFilename won't work
2522 * correctly on non-windows hosts when the guest is from the DOS world (Windows,
2523 * OS/2, DOS). The host doesn't know about DOS slashes, only UNIX slashes and
2524 * will get the wrong idea if some dilligent user does:
2525 *
2526 * copyto myfile.txt 'C:\guestfile.txt'
2527 * or
2528 * copyto myfile.txt 'D:guestfile.txt'
2529 *
2530 * @bugref{6344}
2531 */
2532 if (!RTPathFilename(pszDest))
2533 {
2534 vrc = ctrlCopyDirCreate(pContext, pszDest);
2535 }
2536 else
2537 {
2538 /* We assume we got a file name as destination -- so strip
2539 * the actual file name and make sure the appropriate
2540 * directories get created. */
2541 char *pszDestDir = RTStrDup(pszDest);
2542 AssertPtr(pszDestDir);
2543 RTPathStripFilename(pszDestDir);
2544 vrc = ctrlCopyDirCreate(pContext, pszDestDir);
2545 RTStrFree(pszDestDir);
2546 }
2547
2548 if (RT_SUCCESS(vrc))
2549 {
2550 /*
2551 * Here starts the actual fun!
2552 * Handle all given sources one by one.
2553 */
2554 for (unsigned long s = 0; s < vecSources.size(); s++)
2555 {
2556 char *pszSource = RTStrDup(vecSources[s].GetSource());
2557 AssertPtrBreakStmt(pszSource, vrc = VERR_NO_MEMORY);
2558 const char *pszFilter = vecSources[s].GetFilter();
2559 if (!strlen(pszFilter))
2560 pszFilter = NULL; /* If empty filter then there's no filter :-) */
2561
2562 char *pszSourceRoot;
2563 vrc = ctrlCopyCreateSourceRoot(pszSource, &pszSourceRoot);
2564 if (RT_FAILURE(vrc))
2565 {
2566 RTMsgError("Unable to create source root, rc=%Rrc\n", vrc);
2567 break;
2568 }
2569
2570 if (pCtx->fVerbose)
2571 RTPrintf("Source: %s\n", pszSource);
2572
2573 /** @todo Files with filter?? */
2574 bool fSourceIsFile = false;
2575 bool fSourceExists;
2576
2577 size_t cchSource = strlen(pszSource);
2578 if ( cchSource > 1
2579 && RTPATH_IS_SLASH(pszSource[cchSource - 1]))
2580 {
2581 if (pszFilter) /* Directory with filter (so use source root w/o the actual filter). */
2582 vrc = ctrlCopyDirExistsOnSource(pContext, pszSourceRoot, &fSourceExists);
2583 else /* Regular directory without filter. */
2584 vrc = ctrlCopyDirExistsOnSource(pContext, pszSource, &fSourceExists);
2585
2586 if (fSourceExists)
2587 {
2588 /* Strip trailing slash from our source element so that other functions
2589 * can use this stuff properly (like RTPathStartsWith). */
2590 RTPathStripTrailingSlash(pszSource);
2591 }
2592 }
2593 else
2594 {
2595 vrc = ctrlCopyFileExistsOnSource(pContext, pszSource, &fSourceExists);
2596 if ( RT_SUCCESS(vrc)
2597 && fSourceExists)
2598 {
2599 fSourceIsFile = true;
2600 }
2601 }
2602
2603 if ( RT_SUCCESS(vrc)
2604 && fSourceExists)
2605 {
2606 if (fSourceIsFile)
2607 {
2608 /* Single file. */
2609 char *pszDestFile;
2610 vrc = ctrlCopyTranslatePath(pszSourceRoot, pszSource,
2611 strDest.c_str(), &pszDestFile);
2612 if (RT_SUCCESS(vrc))
2613 {
2614 vrc = ctrlCopyFileToDest(pContext, pszSource,
2615 pszDestFile, 0 /* Flags */);
2616 RTStrFree(pszDestFile);
2617 }
2618 else
2619 RTMsgError("Unable to translate path for \"%s\", rc=%Rrc\n",
2620 pszSource, vrc);
2621 }
2622 else
2623 {
2624 /* Directory (with filter?). */
2625 vrc = ctrlCopyDirToDest(pContext, pszSource, pszFilter,
2626 strDest.c_str(), fFlags);
2627 }
2628 }
2629
2630 ctrlCopyFreeSourceRoot(pszSourceRoot);
2631
2632 if ( RT_SUCCESS(vrc)
2633 && !fSourceExists)
2634 {
2635 RTMsgError("Warning: Source \"%s\" does not exist, skipping!\n",
2636 pszSource);
2637 RTStrFree(pszSource);
2638 continue;
2639 }
2640 else if (RT_FAILURE(vrc))
2641 {
2642 RTMsgError("Error processing \"%s\", rc=%Rrc\n",
2643 pszSource, vrc);
2644 RTStrFree(pszSource);
2645 break;
2646 }
2647
2648 RTStrFree(pszSource);
2649 }
2650 }
2651
2652 ctrlCopyContextFree(pContext);
2653
2654 return RT_SUCCESS(vrc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
2655}
2656
2657static DECLCALLBACK(RTEXITCODE) handleCtrlCopyFrom(PGCTLCMDCTX pCtx)
2658{
2659 return handleCtrlCopy(pCtx, false /* Guest to host */);
2660}
2661
2662static DECLCALLBACK(RTEXITCODE) handleCtrlCopyTo(PGCTLCMDCTX pCtx)
2663{
2664 return handleCtrlCopy(pCtx, true /* Host to guest */);
2665}
2666
2667static DECLCALLBACK(RTEXITCODE) handleCtrlCreateDirectory(PGCTLCMDCTX pCtx)
2668{
2669 AssertPtrReturn(pCtx, RTEXITCODE_FAILURE);
2670
2671 static const RTGETOPTDEF s_aOptions[] =
2672 {
2673 { "--mode", 'm', RTGETOPT_REQ_UINT32 },
2674 { "--parents", 'P', RTGETOPT_REQ_NOTHING }
2675 };
2676
2677 int ch;
2678 RTGETOPTUNION ValueUnion;
2679 RTGETOPTSTATE GetState;
2680 RTGetOptInit(&GetState, pCtx->iArgc, pCtx->ppaArgv,
2681 s_aOptions, RT_ELEMENTS(s_aOptions), pCtx->iFirstArgc, RTGETOPTINIT_FLAGS_OPTS_FIRST);
2682
2683 SafeArray<DirectoryCreateFlag_T> dirCreateFlags;
2684 uint32_t fDirMode = 0; /* Default mode. */
2685 DESTDIRMAP mapDirs;
2686
2687 while ((ch = RTGetOpt(&GetState, &ValueUnion)))
2688 {
2689 /* For options that require an argument, ValueUnion has received the value. */
2690 switch (ch)
2691 {
2692 case 'm': /* Mode */
2693 fDirMode = ValueUnion.u32;
2694 break;
2695
2696 case 'P': /* Create parents */
2697 dirCreateFlags.push_back(DirectoryCreateFlag_Parents);
2698 break;
2699
2700 case VINF_GETOPT_NOT_OPTION:
2701 mapDirs[ValueUnion.psz]; /* Add destination directory to map. */
2702 break;
2703
2704 default:
2705 return errorGetOptEx(USAGE_GUESTCONTROL, USAGE_GSTCTRL_CREATEDIR, ch, &ValueUnion);
2706 }
2707 }
2708
2709 uint32_t cDirs = mapDirs.size();
2710 if (!cDirs)
2711 return errorSyntaxEx(USAGE_GUESTCONTROL, USAGE_GSTCTRL_CREATEDIR,
2712 "No directory to create specified!");
2713
2714 /*
2715 * Create the directories.
2716 */
2717 HRESULT rc = S_OK;
2718 if (pCtx->fVerbose && cDirs)
2719 RTPrintf("Creating %RU32 directories ...\n", cDirs);
2720
2721 DESTDIRMAPITER it = mapDirs.begin();
2722 while ( (it != mapDirs.end())
2723 && !g_fGuestCtrlCanceled)
2724 {
2725 if (pCtx->fVerbose)
2726 RTPrintf("Creating directory \"%s\" ...\n", it->first.c_str());
2727
2728 CHECK_ERROR_BREAK(pCtx->pGuestSession, DirectoryCreate(Bstr(it->first).raw(),
2729 fDirMode, ComSafeArrayAsInParam(dirCreateFlags)));
2730 it++;
2731 }
2732
2733 return FAILED(rc) ? RTEXITCODE_FAILURE : RTEXITCODE_SUCCESS;
2734}
2735
2736static DECLCALLBACK(RTEXITCODE) handleCtrlRemoveDirectory(PGCTLCMDCTX pCtx)
2737{
2738 AssertPtrReturn(pCtx, RTEXITCODE_FAILURE);
2739
2740 static const RTGETOPTDEF s_aOptions[] =
2741 {
2742 { "--recursive", 'R', RTGETOPT_REQ_NOTHING }
2743 };
2744
2745 int ch;
2746 RTGETOPTUNION ValueUnion;
2747 RTGETOPTSTATE GetState;
2748 RTGetOptInit(&GetState, pCtx->iArgc, pCtx->ppaArgv,
2749 s_aOptions, RT_ELEMENTS(s_aOptions), pCtx->iFirstArgc, RTGETOPTINIT_FLAGS_OPTS_FIRST);
2750
2751 bool fRecursive = false;
2752 DESTDIRMAP mapDirs;
2753
2754 while ((ch = RTGetOpt(&GetState, &ValueUnion)))
2755 {
2756 /* For options that require an argument, ValueUnion has received the value. */
2757 switch (ch)
2758 {
2759 case 'R':
2760 fRecursive = true;
2761 break;
2762
2763 case VINF_GETOPT_NOT_OPTION:
2764 mapDirs[ValueUnion.psz]; /* Add destination directory to map. */
2765 break;
2766
2767 default:
2768 return errorGetOptEx(USAGE_GUESTCONTROL, USAGE_GSTCTRL_REMOVEDIR, ch, &ValueUnion);
2769 }
2770 }
2771
2772 uint32_t cDirs = mapDirs.size();
2773 if (!cDirs)
2774 return errorSyntaxEx(USAGE_GUESTCONTROL, USAGE_GSTCTRL_REMOVEDIR,
2775 "No directory to remove specified!");
2776
2777 /*
2778 * Remove the directories.
2779 */
2780 HRESULT rc = S_OK;
2781 if (pCtx->fVerbose && cDirs)
2782 RTPrintf("Removing %RU32 directories ...\n", cDirs);
2783
2784 DESTDIRMAPITER it = mapDirs.begin();
2785 while ( (it != mapDirs.end())
2786 && !g_fGuestCtrlCanceled)
2787 {
2788 if (pCtx->fVerbose)
2789 RTPrintf("%s directory \"%s\" ...\n",
2790 fRecursive ? "Recursively removing" : "Removing",
2791 it->first.c_str());
2792 try
2793 {
2794 if (fRecursive)
2795 {
2796 com::SafeArray<DirectoryRemoveRecFlag_T> aRemRecFlags;
2797 /** @todo Make flags configurable. */
2798 aRemRecFlags.push_back(DirectoryRemoveRecFlag_ContentAndDir);
2799
2800 ComPtr<IProgress> pProgress;
2801 CHECK_ERROR_BREAK(pCtx->pGuestSession, DirectoryRemoveRecursive(Bstr(it->first).raw(),
2802 ComSafeArrayAsInParam(aRemRecFlags),
2803 pProgress.asOutParam()));
2804 if (pCtx->fVerbose)
2805 rc = showProgress(pProgress);
2806 else
2807 rc = pProgress->WaitForCompletion(-1 /* No timeout */);
2808 if (SUCCEEDED(rc))
2809 CHECK_PROGRESS_ERROR(pProgress, ("Directory deletion failed"));
2810
2811 pProgress.setNull();
2812 }
2813 else
2814 CHECK_ERROR_BREAK(pCtx->pGuestSession, DirectoryRemove(Bstr(it->first).raw()));
2815 }
2816 catch (std::bad_alloc)
2817 {
2818 rc = E_OUTOFMEMORY;
2819 break;
2820 }
2821
2822 it++;
2823 }
2824
2825 return FAILED(rc) ? RTEXITCODE_FAILURE : RTEXITCODE_SUCCESS;
2826}
2827
2828static DECLCALLBACK(RTEXITCODE) handleCtrlRemoveFile(PGCTLCMDCTX pCtx)
2829{
2830 AssertPtrReturn(pCtx, RTEXITCODE_FAILURE);
2831
2832 int ch;
2833 RTGETOPTUNION ValueUnion;
2834 RTGETOPTSTATE GetState;
2835 RTGetOptInit(&GetState, pCtx->iArgc, pCtx->ppaArgv,
2836 NULL /* s_aOptions */, 0 /* RT_ELEMENTS(s_aOptions) */,
2837 pCtx->iFirstArgc, RTGETOPTINIT_FLAGS_OPTS_FIRST);
2838
2839 DESTDIRMAP mapDirs;
2840
2841 while ((ch = RTGetOpt(&GetState, &ValueUnion)))
2842 {
2843 /* For options that require an argument, ValueUnion has received the value. */
2844 switch (ch)
2845 {
2846 case VINF_GETOPT_NOT_OPTION:
2847 mapDirs[ValueUnion.psz]; /* Add destination directory to map. */
2848 break;
2849
2850 default:
2851 return errorGetOptEx(USAGE_GUESTCONTROL, USAGE_GSTCTRL_REMOVEFILE, ch, &ValueUnion);
2852 }
2853 }
2854
2855 uint32_t cFiles = mapDirs.size();
2856 if (!cFiles)
2857 return errorSyntaxEx(USAGE_GUESTCONTROL, USAGE_GSTCTRL_REMOVEFILE,
2858 "No file to remove specified!");
2859
2860 /*
2861 * Create the directories.
2862 */
2863 HRESULT rc = S_OK;
2864 if (pCtx->fVerbose && cFiles)
2865 RTPrintf("Removing %RU32 file(s) ...\n", cFiles);
2866
2867 DESTDIRMAPITER it = mapDirs.begin();
2868 while ( (it != mapDirs.end())
2869 && !g_fGuestCtrlCanceled)
2870 {
2871 if (pCtx->fVerbose)
2872 RTPrintf("Removing file \"%s\" ...\n", it->first.c_str());
2873
2874 CHECK_ERROR_BREAK(pCtx->pGuestSession, FileRemove(Bstr(it->first).raw()));
2875
2876 it++;
2877 }
2878
2879 return FAILED(rc) ? RTEXITCODE_FAILURE : RTEXITCODE_SUCCESS;
2880}
2881
2882static DECLCALLBACK(RTEXITCODE) handleCtrlRename(PGCTLCMDCTX pCtx)
2883{
2884 AssertPtrReturn(pCtx, RTEXITCODE_FAILURE);
2885
2886 static const RTGETOPTDEF s_aOptions[] = { { 0 } };
2887
2888 int ch;
2889 RTGETOPTUNION ValueUnion;
2890 RTGETOPTSTATE GetState;
2891 RTGetOptInit(&GetState, pCtx->iArgc, pCtx->ppaArgv,
2892 NULL /*s_aOptions*/, 0 /*RT_ELEMENTS(s_aOptions)*/, pCtx->iFirstArgc, RTGETOPTINIT_FLAGS_OPTS_FIRST);
2893
2894 int vrc = VINF_SUCCESS;
2895
2896 bool fDryrun = false;
2897 std::vector< Utf8Str > vecSources;
2898 Utf8Str strDest;
2899 com::SafeArray<PathRenameFlag_T> aRenameFlags;
2900
2901 try
2902 {
2903 /** @todo Make flags configurable. */
2904 aRenameFlags.push_back(PathRenameFlag_NoReplace);
2905
2906 while ( (ch = RTGetOpt(&GetState, &ValueUnion))
2907 && RT_SUCCESS(vrc))
2908 {
2909 /* For options that require an argument, ValueUnion has received the value. */
2910 switch (ch)
2911 {
2912 /** @todo Implement a --dryrun command. */
2913 /** @todo Implement rename flags. */
2914
2915 case VINF_GETOPT_NOT_OPTION:
2916 vecSources.push_back(Utf8Str(ValueUnion.psz));
2917 strDest = ValueUnion.psz;
2918 break;
2919
2920 default:
2921 return errorGetOptEx(USAGE_GUESTCONTROL, USAGE_GSTCTRL_RENAME, ch, &ValueUnion);
2922 }
2923 }
2924 }
2925 catch (std::bad_alloc)
2926 {
2927 vrc = VERR_NO_MEMORY;
2928 }
2929
2930 if (RT_FAILURE(vrc))
2931 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to initialize, rc=%Rrc\n", vrc);
2932
2933 uint32_t cSources = vecSources.size();
2934 if (!cSources)
2935 return errorSyntaxEx(USAGE_GUESTCONTROL, USAGE_GSTCTRL_RENAME,
2936 "No source(s) to move specified!");
2937 if (cSources < 2)
2938 return errorSyntaxEx(USAGE_GUESTCONTROL, USAGE_GSTCTRL_RENAME,
2939 "No destination specified!");
2940
2941 /* Delete last element, which now is the destination. */
2942 vecSources.pop_back();
2943 cSources = vecSources.size();
2944
2945 HRESULT rc = S_OK;
2946
2947 if (cSources > 1)
2948 {
2949 ComPtr<IGuestFsObjInfo> pFsObjInfo;
2950 rc = pCtx->pGuestSession->DirectoryQueryInfo(Bstr(strDest).raw(), pFsObjInfo.asOutParam());
2951 if (FAILED(rc))
2952 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Destination must be a directory when specifying multiple sources\n");
2953 }
2954
2955 /*
2956 * Rename (move) the entries.
2957 */
2958 if (pCtx->fVerbose && cSources)
2959 RTPrintf("Renaming %RU32 %s ...\n", cSources,
2960 cSources > 1
2961 ? "entries" : "entry");
2962
2963 std::vector< Utf8Str >::iterator it = vecSources.begin();
2964 while ( (it != vecSources.end())
2965 && !g_fGuestCtrlCanceled)
2966 {
2967 bool fSourceIsDirectory = false;
2968 Utf8Str strCurSource = (*it);
2969 Utf8Str strCurDest = strDest;
2970
2971 /** @todo Slooooow, but works for now. */
2972 ComPtr<IGuestFsObjInfo> pFsObjInfo;
2973 rc = pCtx->pGuestSession->FileQueryInfo(Bstr(strCurSource).raw(), pFsObjInfo.asOutParam());
2974 if (FAILED(rc))
2975 {
2976 rc = pCtx->pGuestSession->DirectoryQueryInfo(Bstr(strCurSource).raw(), pFsObjInfo.asOutParam());
2977 fSourceIsDirectory = SUCCEEDED(rc);
2978 }
2979 if (FAILED(rc))
2980 {
2981 if (pCtx->fVerbose)
2982 RTPrintf("Warning: Cannot stat for element \"%s\": No such element\n",
2983 strCurSource.c_str());
2984 it++;
2985 continue; /* Skip. */
2986 }
2987
2988 if (pCtx->fVerbose)
2989 RTPrintf("Renaming %s \"%s\" to \"%s\" ...\n",
2990 fSourceIsDirectory ? "directory" : "file",
2991 strCurSource.c_str(), strCurDest.c_str());
2992
2993 if (!fDryrun)
2994 {
2995 if (fSourceIsDirectory)
2996 {
2997 CHECK_ERROR_BREAK(pCtx->pGuestSession, DirectoryRename(Bstr(strCurSource).raw(),
2998 Bstr(strCurDest).raw(),
2999 ComSafeArrayAsInParam(aRenameFlags)));
3000
3001 /* Break here, since it makes no sense to rename mroe than one source to
3002 * the same directory. */
3003 it = vecSources.end();
3004 break;
3005 }
3006 else
3007 CHECK_ERROR_BREAK(pCtx->pGuestSession, FileRename(Bstr(strCurSource).raw(),
3008 Bstr(strCurDest).raw(),
3009 ComSafeArrayAsInParam(aRenameFlags)));
3010 }
3011
3012 it++;
3013 }
3014
3015 if ( (it != vecSources.end())
3016 && pCtx->fVerbose)
3017 {
3018 RTPrintf("Warning: Not all sources were renamed\n");
3019 }
3020
3021 return FAILED(rc) ? RTEXITCODE_FAILURE : RTEXITCODE_SUCCESS;
3022}
3023
3024static DECLCALLBACK(RTEXITCODE) handleCtrlCreateTemp(PGCTLCMDCTX pCtx)
3025{
3026 AssertPtrReturn(pCtx, RTEXITCODE_FAILURE);
3027
3028 static const RTGETOPTDEF s_aOptions[] =
3029 {
3030 { "--mode", 'm', RTGETOPT_REQ_UINT32 },
3031 { "--directory", 'D', RTGETOPT_REQ_NOTHING },
3032 { "--secure", 's', RTGETOPT_REQ_NOTHING },
3033 { "--tmpdir", 't', RTGETOPT_REQ_STRING }
3034 };
3035
3036 int ch;
3037 RTGETOPTUNION ValueUnion;
3038 RTGETOPTSTATE GetState;
3039 RTGetOptInit(&GetState, pCtx->iArgc, pCtx->ppaArgv,
3040 s_aOptions, RT_ELEMENTS(s_aOptions), pCtx->iFirstArgc, RTGETOPTINIT_FLAGS_OPTS_FIRST);
3041
3042 Utf8Str strTemplate;
3043 uint32_t fMode = 0; /* Default mode. */
3044 bool fDirectory = false;
3045 bool fSecure = false;
3046 Utf8Str strTempDir;
3047
3048 DESTDIRMAP mapDirs;
3049
3050 while ((ch = RTGetOpt(&GetState, &ValueUnion)))
3051 {
3052 /* For options that require an argument, ValueUnion has received the value. */
3053 switch (ch)
3054 {
3055 case 'm': /* Mode */
3056 fMode = ValueUnion.u32;
3057 break;
3058
3059 case 'D': /* Create directory */
3060 fDirectory = true;
3061 break;
3062
3063 case 's': /* Secure */
3064 fSecure = true;
3065 break;
3066
3067 case 't': /* Temp directory */
3068 strTempDir = ValueUnion.psz;
3069 break;
3070
3071 case VINF_GETOPT_NOT_OPTION:
3072 {
3073 if (strTemplate.isEmpty())
3074 strTemplate = ValueUnion.psz;
3075 else
3076 return errorSyntaxEx(USAGE_GUESTCONTROL, USAGE_GSTCTRL_CREATETEMP,
3077 "More than one template specified!\n");
3078 break;
3079 }
3080
3081 default:
3082 return errorGetOptEx(USAGE_GUESTCONTROL, USAGE_GSTCTRL_CREATETEMP, ch, &ValueUnion);
3083 }
3084 }
3085
3086 if (strTemplate.isEmpty())
3087 return errorSyntaxEx(USAGE_GUESTCONTROL, USAGE_GSTCTRL_CREATETEMP,
3088 "No template specified!");
3089
3090 if (!fDirectory)
3091 return errorSyntaxEx(USAGE_GUESTCONTROL, USAGE_GSTCTRL_CREATETEMP,
3092 "Creating temporary files is currently not supported!");
3093
3094 /*
3095 * Create the directories.
3096 */
3097 if (pCtx->fVerbose)
3098 {
3099 if (fDirectory && !strTempDir.isEmpty())
3100 RTPrintf("Creating temporary directory from template '%s' in directory '%s' ...\n",
3101 strTemplate.c_str(), strTempDir.c_str());
3102 else if (fDirectory)
3103 RTPrintf("Creating temporary directory from template '%s' in default temporary directory ...\n",
3104 strTemplate.c_str());
3105 else if (!fDirectory && !strTempDir.isEmpty())
3106 RTPrintf("Creating temporary file from template '%s' in directory '%s' ...\n",
3107 strTemplate.c_str(), strTempDir.c_str());
3108 else if (!fDirectory)
3109 RTPrintf("Creating temporary file from template '%s' in default temporary directory ...\n",
3110 strTemplate.c_str());
3111 }
3112
3113 HRESULT rc = S_OK;
3114 if (fDirectory)
3115 {
3116 Bstr directory;
3117 CHECK_ERROR(pCtx->pGuestSession, DirectoryCreateTemp(Bstr(strTemplate).raw(),
3118 fMode, Bstr(strTempDir).raw(),
3119 fSecure,
3120 directory.asOutParam()));
3121 if (SUCCEEDED(rc))
3122 RTPrintf("Directory name: %ls\n", directory.raw());
3123 }
3124 // else - temporary file not yet implemented
3125
3126 return FAILED(rc) ? RTEXITCODE_FAILURE : RTEXITCODE_SUCCESS;
3127}
3128
3129static DECLCALLBACK(RTEXITCODE) handleCtrlStat(PGCTLCMDCTX pCtx)
3130{
3131 AssertPtrReturn(pCtx, RTEXITCODE_FAILURE);
3132
3133 static const RTGETOPTDEF s_aOptions[] =
3134 {
3135 { "--dereference", 'L', RTGETOPT_REQ_NOTHING },
3136 { "--file-system", 'f', RTGETOPT_REQ_NOTHING },
3137 { "--format", 'c', RTGETOPT_REQ_STRING },
3138 { "--terse", 't', RTGETOPT_REQ_NOTHING }
3139 };
3140
3141 int ch;
3142 RTGETOPTUNION ValueUnion;
3143 RTGETOPTSTATE GetState;
3144 RTGetOptInit(&GetState, pCtx->iArgc, pCtx->ppaArgv,
3145 s_aOptions, RT_ELEMENTS(s_aOptions), pCtx->iFirstArgc, RTGETOPTINIT_FLAGS_OPTS_FIRST);
3146
3147 DESTDIRMAP mapObjs;
3148
3149 while ((ch = RTGetOpt(&GetState, &ValueUnion)))
3150 {
3151 /* For options that require an argument, ValueUnion has received the value. */
3152 switch (ch)
3153 {
3154 case 'L': /* Dereference */
3155 case 'f': /* File-system */
3156 case 'c': /* Format */
3157 case 't': /* Terse */
3158 return errorSyntaxEx(USAGE_GUESTCONTROL, USAGE_GSTCTRL_STAT,
3159 "Command \"%s\" not implemented yet!", ValueUnion.psz);
3160
3161 case VINF_GETOPT_NOT_OPTION:
3162 mapObjs[ValueUnion.psz]; /* Add element to check to map. */
3163 break;
3164
3165 default:
3166 return errorGetOptEx(USAGE_GUESTCONTROL, USAGE_GSTCTRL_STAT, ch, &ValueUnion);
3167 }
3168 }
3169
3170 uint32_t cObjs = mapObjs.size();
3171 if (!cObjs)
3172 return errorSyntaxEx(USAGE_GUESTCONTROL, USAGE_GSTCTRL_STAT,
3173 "No element(s) to check specified!");
3174
3175 HRESULT rc;
3176
3177 /*
3178 * Doing the checks.
3179 */
3180 RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
3181 DESTDIRMAPITER it = mapObjs.begin();
3182 while (it != mapObjs.end())
3183 {
3184 if (pCtx->fVerbose)
3185 RTPrintf("Checking for element \"%s\" ...\n", it->first.c_str());
3186
3187 ComPtr<IGuestFsObjInfo> pFsObjInfo;
3188 rc = pCtx->pGuestSession->FileQueryInfo(Bstr(it->first).raw(), pFsObjInfo.asOutParam());
3189 if (FAILED(rc))
3190 rc = pCtx->pGuestSession->DirectoryQueryInfo(Bstr(it->first).raw(), pFsObjInfo.asOutParam());
3191
3192 if (FAILED(rc))
3193 {
3194 /* If there's at least one element which does not exist on the guest,
3195 * drop out with exitcode 1. */
3196 if (pCtx->fVerbose)
3197 RTPrintf("Cannot stat for element \"%s\": No such element\n",
3198 it->first.c_str());
3199 rcExit = RTEXITCODE_FAILURE;
3200 }
3201 else
3202 {
3203 FsObjType_T objType;
3204 pFsObjInfo->COMGETTER(Type)(&objType);
3205 switch (objType)
3206 {
3207 case FsObjType_File:
3208 RTPrintf("Element \"%s\" found: Is a file\n", it->first.c_str());
3209 break;
3210
3211 case FsObjType_Directory:
3212 RTPrintf("Element \"%s\" found: Is a directory\n", it->first.c_str());
3213 break;
3214
3215 case FsObjType_Symlink:
3216 RTPrintf("Element \"%s\" found: Is a symlink\n", it->first.c_str());
3217 break;
3218
3219 default:
3220 RTPrintf("Element \"%s\" found, type unknown (%ld)\n", it->first.c_str(), objType);
3221 break;
3222 }
3223
3224 /** @todo: Show more information about this element. */
3225 }
3226
3227 it++;
3228 }
3229
3230 return rcExit;
3231}
3232
3233static DECLCALLBACK(RTEXITCODE) handleCtrlUpdateAdditions(PGCTLCMDCTX pCtx)
3234{
3235 AssertPtrReturn(pCtx, RTEXITCODE_FAILURE);
3236
3237 /*
3238 * Check the syntax. We can deduce the correct syntax from the number of
3239 * arguments.
3240 */
3241 Utf8Str strSource;
3242 com::SafeArray<IN_BSTR> aArgs;
3243 bool fWaitStartOnly = false;
3244
3245 static const RTGETOPTDEF s_aOptions[] =
3246 {
3247 { "--source", 's', RTGETOPT_REQ_STRING },
3248 { "--wait-start", 'w', RTGETOPT_REQ_NOTHING }
3249 };
3250
3251 int ch;
3252 RTGETOPTUNION ValueUnion;
3253 RTGETOPTSTATE GetState;
3254 RTGetOptInit(&GetState, pCtx->iArgc, pCtx->ppaArgv, s_aOptions, RT_ELEMENTS(s_aOptions), pCtx->iFirstArgc, 0);
3255
3256 int vrc = VINF_SUCCESS;
3257 while ( (ch = RTGetOpt(&GetState, &ValueUnion))
3258 && RT_SUCCESS(vrc))
3259 {
3260 switch (ch)
3261 {
3262 case 's':
3263 strSource = ValueUnion.psz;
3264 break;
3265
3266 case 'w':
3267 fWaitStartOnly = true;
3268 break;
3269
3270 case VINF_GETOPT_NOT_OPTION:
3271 if (aArgs.size() == 0 && strSource.isEmpty())
3272 strSource = ValueUnion.psz;
3273 else
3274 aArgs.push_back(Bstr(ValueUnion.psz).raw());
3275 break;
3276
3277 default:
3278 return errorGetOptEx(USAGE_GUESTCONTROL, USAGE_GSTCTRL_UPDATEADDS, ch, &ValueUnion);
3279 }
3280 }
3281
3282 if (pCtx->fVerbose)
3283 RTPrintf("Updating Guest Additions ...\n");
3284
3285 HRESULT rc = S_OK;
3286 while (strSource.isEmpty())
3287 {
3288 ComPtr<ISystemProperties> pProperties;
3289 CHECK_ERROR_BREAK(pCtx->handlerArg.virtualBox, COMGETTER(SystemProperties)(pProperties.asOutParam()));
3290 Bstr strISO;
3291 CHECK_ERROR_BREAK(pProperties, COMGETTER(DefaultAdditionsISO)(strISO.asOutParam()));
3292 strSource = strISO;
3293 break;
3294 }
3295
3296 /* Determine source if not set yet. */
3297 if (strSource.isEmpty())
3298 {
3299 RTMsgError("No Guest Additions source found or specified, aborting\n");
3300 vrc = VERR_FILE_NOT_FOUND;
3301 }
3302 else if (!RTFileExists(strSource.c_str()))
3303 {
3304 RTMsgError("Source \"%s\" does not exist!\n", strSource.c_str());
3305 vrc = VERR_FILE_NOT_FOUND;
3306 }
3307
3308 if (RT_SUCCESS(vrc))
3309 {
3310 if (pCtx->fVerbose)
3311 RTPrintf("Using source: %s\n", strSource.c_str());
3312
3313 com::SafeArray<AdditionsUpdateFlag_T> aUpdateFlags;
3314 if (fWaitStartOnly)
3315 {
3316 aUpdateFlags.push_back(AdditionsUpdateFlag_WaitForUpdateStartOnly);
3317 if (pCtx->fVerbose)
3318 RTPrintf("Preparing and waiting for Guest Additions installer to start ...\n");
3319 }
3320
3321 ComPtr<IProgress> pProgress;
3322 CHECK_ERROR(pCtx->pGuest, UpdateGuestAdditions(Bstr(strSource).raw(),
3323 ComSafeArrayAsInParam(aArgs),
3324 /* Wait for whole update process to complete. */
3325 ComSafeArrayAsInParam(aUpdateFlags),
3326 pProgress.asOutParam()));
3327 if (FAILED(rc))
3328 vrc = ctrlPrintError(pCtx->pGuest, COM_IIDOF(IGuest));
3329 else
3330 {
3331 if (pCtx->fVerbose)
3332 rc = showProgress(pProgress);
3333 else
3334 rc = pProgress->WaitForCompletion(-1 /* No timeout */);
3335
3336 if (SUCCEEDED(rc))
3337 CHECK_PROGRESS_ERROR(pProgress, ("Guest additions update failed"));
3338 vrc = ctrlPrintProgressError(pProgress);
3339 if ( RT_SUCCESS(vrc)
3340 && pCtx->fVerbose)
3341 {
3342 RTPrintf("Guest Additions update successful\n");
3343 }
3344 }
3345 }
3346
3347 return RT_SUCCESS(vrc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
3348}
3349
3350static DECLCALLBACK(RTEXITCODE) handleCtrlList(PGCTLCMDCTX pCtx)
3351{
3352 AssertPtrReturn(pCtx, RTEXITCODE_FAILURE);
3353
3354 if (pCtx->iArgc < 1)
3355 return errorSyntaxEx(USAGE_GUESTCONTROL, USAGE_GSTCTRL_LIST,
3356 "Must specify a listing category");
3357
3358 RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
3359
3360 /** Use RTGetOpt here when handling command line args gets more complex. */
3361
3362 bool fListAll = false;
3363 bool fListSessions = false;
3364 bool fListProcesses = false;
3365 bool fListFiles = false;
3366 if ( !RTStrICmp(pCtx->ppaArgv[0], "sessions")
3367 || !RTStrICmp(pCtx->ppaArgv[0], "sess"))
3368 fListSessions = true;
3369 else if ( !RTStrICmp(pCtx->ppaArgv[0], "processes")
3370 || !RTStrICmp(pCtx->ppaArgv[0], "procs"))
3371 fListSessions = fListProcesses = true; /* Showing processes implies showing sessions. */
3372 else if ( !RTStrICmp(pCtx->ppaArgv[0], "files"))
3373 fListSessions = fListFiles = true; /* Showing files implies showing sessions. */
3374 else if (!RTStrICmp(pCtx->ppaArgv[0], "all"))
3375 fListAll = true;
3376
3377 /** @todo Handle "--verbose" using RTGetOpt. */
3378 /** @todo Do we need a machine-readable output here as well? */
3379
3380 if ( fListAll
3381 || fListSessions)
3382 {
3383 HRESULT rc;
3384 do
3385 {
3386 size_t cTotalProcs = 0;
3387 size_t cTotalFiles = 0;
3388
3389 SafeIfaceArray <IGuestSession> collSessions;
3390 CHECK_ERROR_BREAK(pCtx->pGuest, COMGETTER(Sessions)(ComSafeArrayAsOutParam(collSessions)));
3391 size_t cSessions = collSessions.size();
3392
3393 if (cSessions)
3394 {
3395 RTPrintf("Active guest sessions:\n");
3396
3397 /** @todo Make this output a bit prettier. No time now. */
3398
3399 for (size_t i = 0; i < cSessions; i++)
3400 {
3401 ComPtr<IGuestSession> pCurSession = collSessions[i];
3402 if (!pCurSession.isNull())
3403 {
3404 ULONG uID;
3405 CHECK_ERROR_BREAK(pCurSession, COMGETTER(Id)(&uID));
3406 Bstr strName;
3407 CHECK_ERROR_BREAK(pCurSession, COMGETTER(Name)(strName.asOutParam()));
3408 Bstr strUser;
3409 CHECK_ERROR_BREAK(pCurSession, COMGETTER(User)(strUser.asOutParam()));
3410 GuestSessionStatus_T sessionStatus;
3411 CHECK_ERROR_BREAK(pCurSession, COMGETTER(Status)(&sessionStatus));
3412 RTPrintf("\n\tSession #%-3zu ID=%-3RU32 User=%-16ls Status=[%s] Name=%ls",
3413 i, uID, strUser.raw(), ctrlSessionStatusToText(sessionStatus), strName.raw());
3414
3415 if ( fListAll
3416 || fListProcesses)
3417 {
3418 SafeIfaceArray <IGuestProcess> collProcesses;
3419 CHECK_ERROR_BREAK(pCurSession, COMGETTER(Processes)(ComSafeArrayAsOutParam(collProcesses)));
3420 for (size_t a = 0; a < collProcesses.size(); a++)
3421 {
3422 ComPtr<IGuestProcess> pCurProcess = collProcesses[a];
3423 if (!pCurProcess.isNull())
3424 {
3425 ULONG uPID;
3426 CHECK_ERROR_BREAK(pCurProcess, COMGETTER(PID)(&uPID));
3427 Bstr strExecPath;
3428 CHECK_ERROR_BREAK(pCurProcess, COMGETTER(ExecutablePath)(strExecPath.asOutParam()));
3429 ProcessStatus_T procStatus;
3430 CHECK_ERROR_BREAK(pCurProcess, COMGETTER(Status)(&procStatus));
3431
3432 RTPrintf("\n\t\tProcess #%-03zu PID=%-6RU32 Status=[%s] Command=%ls",
3433 a, uPID, ctrlProcessStatusToText(procStatus), strExecPath.raw());
3434 }
3435 }
3436
3437 cTotalProcs += collProcesses.size();
3438 }
3439
3440 if ( fListAll
3441 || fListFiles)
3442 {
3443 SafeIfaceArray <IGuestFile> collFiles;
3444 CHECK_ERROR_BREAK(pCurSession, COMGETTER(Files)(ComSafeArrayAsOutParam(collFiles)));
3445 for (size_t a = 0; a < collFiles.size(); a++)
3446 {
3447 ComPtr<IGuestFile> pCurFile = collFiles[a];
3448 if (!pCurFile.isNull())
3449 {
3450 CHECK_ERROR_BREAK(pCurFile, COMGETTER(Id)(&uID));
3451 CHECK_ERROR_BREAK(pCurFile, COMGETTER(FileName)(strName.asOutParam()));
3452 FileStatus_T fileStatus;
3453 CHECK_ERROR_BREAK(pCurFile, COMGETTER(Status)(&fileStatus));
3454
3455 RTPrintf("\n\t\tFile #%-03zu ID=%-6RU32 Status=[%s] Name=%ls",
3456 a, uID, ctrlFileStatusToText(fileStatus), strName.raw());
3457 }
3458 }
3459
3460 cTotalFiles += collFiles.size();
3461 }
3462 }
3463 }
3464
3465 RTPrintf("\n\nTotal guest sessions: %zu\n", collSessions.size());
3466 if (fListAll || fListProcesses)
3467 RTPrintf("Total guest processes: %zu\n", cTotalProcs);
3468 if (fListAll || fListFiles)
3469 RTPrintf("Total guest files: %zu\n", cTotalFiles);
3470 }
3471 else
3472 RTPrintf("No active guest sessions found\n");
3473
3474 } while (0);
3475
3476 if (FAILED(rc))
3477 rcExit = RTEXITCODE_FAILURE;
3478 }
3479 else
3480 return errorSyntaxEx(USAGE_GUESTCONTROL, USAGE_GSTCTRL_LIST,
3481 "Invalid listing category '%s", pCtx->ppaArgv[0]);
3482
3483 return rcExit;
3484}
3485
3486static DECLCALLBACK(RTEXITCODE) handleCtrlProcessClose(PGCTLCMDCTX pCtx)
3487{
3488 AssertPtrReturn(pCtx, RTEXITCODE_FAILURE);
3489
3490 if (pCtx->iArgc < 1)
3491 return errorSyntaxEx(USAGE_GUESTCONTROL, USAGE_GSTCTRL_PROCESS,
3492 "Must specify at least a PID to close");
3493
3494 static const RTGETOPTDEF s_aOptions[] =
3495 {
3496 { "--session-id", 'i', RTGETOPT_REQ_UINT32 },
3497 { "--session-name", 'n', RTGETOPT_REQ_STRING }
3498 };
3499
3500 int ch;
3501 RTGETOPTUNION ValueUnion;
3502 RTGETOPTSTATE GetState;
3503 RTGetOptInit(&GetState, pCtx->iArgc, pCtx->ppaArgv,
3504 s_aOptions, RT_ELEMENTS(s_aOptions), pCtx->iFirstArgc, RTGETOPTINIT_FLAGS_OPTS_FIRST);
3505
3506 std::vector < uint32_t > vecPID;
3507 ULONG ulSessionID = UINT32_MAX;
3508 Utf8Str strSessionName;
3509
3510 int vrc = VINF_SUCCESS;
3511
3512 while ( (ch = RTGetOpt(&GetState, &ValueUnion))
3513 && RT_SUCCESS(vrc))
3514 {
3515 /* For options that require an argument, ValueUnion has received the value. */
3516 switch (ch)
3517 {
3518 case 'n': /* Session name (or pattern) */
3519 strSessionName = ValueUnion.psz;
3520 break;
3521
3522 case 'i': /* Session ID */
3523 ulSessionID = ValueUnion.u32;
3524 break;
3525
3526 case VINF_GETOPT_NOT_OPTION:
3527 if (pCtx->iArgc == GetState.iNext)
3528 {
3529 /* Treat every else specified as a PID to kill. */
3530 try
3531 {
3532 uint32_t uPID = RTStrToUInt32(ValueUnion.psz);
3533 if (uPID) /** @todo Is this what we want? If specifying PID 0
3534 this is not going to work on most systems anyway. */
3535 vecPID.push_back(uPID);
3536 else
3537 vrc = VERR_INVALID_PARAMETER;
3538 }
3539 catch(std::bad_alloc &)
3540 {
3541 vrc = VERR_NO_MEMORY;
3542 }
3543 }
3544 break;
3545
3546 default:
3547 return errorGetOptEx(USAGE_GUESTCONTROL, USAGE_GSTCTRL_PROCESS, ch, &ValueUnion);
3548 }
3549 }
3550
3551 if (vecPID.empty())
3552 return errorSyntaxEx(USAGE_GUESTCONTROL, USAGE_GSTCTRL_PROCESS,
3553 "At least one PID must be specified to kill!");
3554
3555 if ( strSessionName.isEmpty()
3556 && ulSessionID == UINT32_MAX)
3557 return errorSyntaxEx(USAGE_GUESTCONTROL, USAGE_GSTCTRL_PROCESS,
3558 "No session ID specified!");
3559
3560 if ( !strSessionName.isEmpty()
3561 && ulSessionID != UINT32_MAX)
3562 return errorSyntaxEx(USAGE_GUESTCONTROL, USAGE_GSTCTRL_PROCESS,
3563 "Either session ID or name (pattern) must be specified");
3564
3565 if (RT_FAILURE(vrc))
3566 return errorSyntaxEx(USAGE_GUESTCONTROL, USAGE_GSTCTRL_PROCESS,
3567 "Invalid parameters specified");
3568
3569 HRESULT rc = S_OK;
3570
3571 ComPtr<IGuestSession> pSession;
3572 ComPtr<IGuestProcess> pProcess;
3573 do
3574 {
3575 uint32_t uProcsTerminated = 0;
3576 bool fSessionFound = false;
3577
3578 SafeIfaceArray <IGuestSession> collSessions;
3579 CHECK_ERROR_BREAK(pCtx->pGuest, COMGETTER(Sessions)(ComSafeArrayAsOutParam(collSessions)));
3580 size_t cSessions = collSessions.size();
3581
3582 uint32_t uSessionsHandled = 0;
3583 for (size_t i = 0; i < cSessions; i++)
3584 {
3585 pSession = collSessions[i];
3586 Assert(!pSession.isNull());
3587
3588 ULONG uID; /* Session ID */
3589 CHECK_ERROR_BREAK(pSession, COMGETTER(Id)(&uID));
3590 Bstr strName;
3591 CHECK_ERROR_BREAK(pSession, COMGETTER(Name)(strName.asOutParam()));
3592 Utf8Str strNameUtf8(strName); /* Session name */
3593 if (strSessionName.isEmpty()) /* Search by ID. Slow lookup. */
3594 {
3595 fSessionFound = uID == ulSessionID;
3596 }
3597 else /* ... or by naming pattern. */
3598 {
3599 if (RTStrSimplePatternMatch(strSessionName.c_str(), strNameUtf8.c_str()))
3600 fSessionFound = true;
3601 }
3602
3603 if (fSessionFound)
3604 {
3605 AssertStmt(!pSession.isNull(), break);
3606 uSessionsHandled++;
3607
3608 SafeIfaceArray <IGuestProcess> collProcs;
3609 CHECK_ERROR_BREAK(pSession, COMGETTER(Processes)(ComSafeArrayAsOutParam(collProcs)));
3610
3611 size_t cProcs = collProcs.size();
3612 for (size_t p = 0; p < cProcs; p++)
3613 {
3614 pProcess = collProcs[p];
3615 Assert(!pProcess.isNull());
3616
3617 ULONG uPID; /* Process ID */
3618 CHECK_ERROR_BREAK(pProcess, COMGETTER(PID)(&uPID));
3619
3620 bool fProcFound = false;
3621 for (size_t a = 0; a < vecPID.size(); a++) /* Slow, but works. */
3622 {
3623 fProcFound = vecPID[a] == uPID;
3624 if (fProcFound)
3625 break;
3626 }
3627
3628 if (fProcFound)
3629 {
3630 if (pCtx->fVerbose)
3631 RTPrintf("Terminating process (PID %RU32) (session ID %RU32) ...\n",
3632 uPID, uID);
3633 CHECK_ERROR_BREAK(pProcess, Terminate());
3634 uProcsTerminated++;
3635 }
3636 else
3637 {
3638 if (ulSessionID != UINT32_MAX)
3639 RTPrintf("No matching process(es) for session ID %RU32 found\n",
3640 ulSessionID);
3641 }
3642
3643 pProcess.setNull();
3644 }
3645
3646 pSession.setNull();
3647 }
3648 }
3649
3650 if (!uSessionsHandled)
3651 RTPrintf("No matching session(s) found\n");
3652
3653 if (uProcsTerminated)
3654 RTPrintf("%RU32 %s terminated\n",
3655 uProcsTerminated, uProcsTerminated == 1 ? "process" : "processes");
3656
3657 } while (0);
3658
3659 pProcess.setNull();
3660 pSession.setNull();
3661
3662 return SUCCEEDED(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
3663}
3664
3665static DECLCALLBACK(RTEXITCODE) handleCtrlProcess(PGCTLCMDCTX pCtx)
3666{
3667 AssertPtrReturn(pCtx, RTEXITCODE_FAILURE);
3668
3669 if (pCtx->iArgc < 1)
3670 return errorSyntaxEx(USAGE_GUESTCONTROL, USAGE_GSTCTRL_PROCESS,
3671 "Must specify an action");
3672
3673 /** Use RTGetOpt here when handling command line args gets more complex. */
3674
3675 if ( !RTStrICmp(pCtx->ppaArgv[0], "close")
3676 || !RTStrICmp(pCtx->ppaArgv[0], "kill")
3677 || !RTStrICmp(pCtx->ppaArgv[0], "terminate"))
3678 {
3679 pCtx->iFirstArgc++; /* Skip process action. */
3680 return handleCtrlProcessClose(pCtx);
3681 }
3682
3683 return errorSyntaxEx(USAGE_GUESTCONTROL, USAGE_GSTCTRL_PROCESS,
3684 "Invalid process action '%s'", pCtx->ppaArgv[0]);
3685}
3686
3687static DECLCALLBACK(RTEXITCODE) handleCtrlSessionClose(PGCTLCMDCTX pCtx)
3688{
3689 AssertPtrReturn(pCtx, RTEXITCODE_FAILURE);
3690
3691 if (pCtx->iArgc < 1)
3692 return errorSyntaxEx(USAGE_GUESTCONTROL, USAGE_GSTCTRL_SESSION,
3693 "Must specify at least a session to close");
3694
3695 static const RTGETOPTDEF s_aOptions[] =
3696 {
3697 { "--all", GETOPTDEF_SESSIONCLOSE_ALL, RTGETOPT_REQ_NOTHING },
3698 { "--session-id", 'i', RTGETOPT_REQ_UINT32 },
3699 { "--session-name", 'n', RTGETOPT_REQ_STRING }
3700 };
3701
3702 int ch;
3703 RTGETOPTUNION ValueUnion;
3704 RTGETOPTSTATE GetState;
3705 RTGetOptInit(&GetState, pCtx->iArgc, pCtx->ppaArgv,
3706 s_aOptions, RT_ELEMENTS(s_aOptions), pCtx->iFirstArgc, RTGETOPTINIT_FLAGS_OPTS_FIRST);
3707
3708 ULONG ulSessionID = UINT32_MAX;
3709 Utf8Str strSessionName;
3710
3711 while ((ch = RTGetOpt(&GetState, &ValueUnion)))
3712 {
3713 /* For options that require an argument, ValueUnion has received the value. */
3714 switch (ch)
3715 {
3716 case 'n': /* Session name pattern */
3717 strSessionName = ValueUnion.psz;
3718 break;
3719
3720 case 'i': /* Session ID */
3721 ulSessionID = ValueUnion.u32;
3722 break;
3723
3724 case GETOPTDEF_SESSIONCLOSE_ALL:
3725 strSessionName = "*";
3726 break;
3727
3728 case VINF_GETOPT_NOT_OPTION:
3729 /** @todo Supply a CSV list of IDs or patterns to close? */
3730 break;
3731
3732 default:
3733 return errorGetOptEx(USAGE_GUESTCONTROL, USAGE_GSTCTRL_SESSION, ch, &ValueUnion);
3734 }
3735 }
3736
3737 if ( strSessionName.isEmpty()
3738 && ulSessionID == UINT32_MAX)
3739 return errorSyntaxEx(USAGE_GUESTCONTROL, USAGE_GSTCTRL_SESSION,
3740 "No session ID specified!");
3741
3742 if ( !strSessionName.isEmpty()
3743 && ulSessionID != UINT32_MAX)
3744 return errorSyntaxEx(USAGE_GUESTCONTROL, USAGE_GSTCTRL_SESSION,
3745 "Either session ID or name (pattern) must be specified");
3746
3747 HRESULT rc = S_OK;
3748
3749 do
3750 {
3751 bool fSessionFound = false;
3752 size_t cSessionsHandled = 0;
3753
3754 SafeIfaceArray <IGuestSession> collSessions;
3755 CHECK_ERROR_BREAK(pCtx->pGuest, COMGETTER(Sessions)(ComSafeArrayAsOutParam(collSessions)));
3756 size_t cSessions = collSessions.size();
3757
3758 for (size_t i = 0; i < cSessions; i++)
3759 {
3760 ComPtr<IGuestSession> pSession = collSessions[i];
3761 Assert(!pSession.isNull());
3762
3763 ULONG uID; /* Session ID */
3764 CHECK_ERROR_BREAK(pSession, COMGETTER(Id)(&uID));
3765 Bstr strName;
3766 CHECK_ERROR_BREAK(pSession, COMGETTER(Name)(strName.asOutParam()));
3767 Utf8Str strNameUtf8(strName); /* Session name */
3768
3769 if (strSessionName.isEmpty()) /* Search by ID. Slow lookup. */
3770 {
3771 fSessionFound = uID == ulSessionID;
3772 }
3773 else /* ... or by naming pattern. */
3774 {
3775 if (RTStrSimplePatternMatch(strSessionName.c_str(), strNameUtf8.c_str()))
3776 fSessionFound = true;
3777 }
3778
3779 if (fSessionFound)
3780 {
3781 cSessionsHandled++;
3782
3783 Assert(!pSession.isNull());
3784 if (pCtx->fVerbose)
3785 RTPrintf("Closing guest session ID=#%RU32 \"%s\" ...\n",
3786 uID, strNameUtf8.c_str());
3787 CHECK_ERROR_BREAK(pSession, Close());
3788 if (pCtx->fVerbose)
3789 RTPrintf("Guest session successfully closed\n");
3790
3791 pSession.setNull();
3792 }
3793 }
3794
3795 if (!cSessionsHandled)
3796 {
3797 RTPrintf("No guest session(s) found\n");
3798 rc = E_ABORT; /* To set exit code accordingly. */
3799 }
3800
3801 } while (0);
3802
3803 return SUCCEEDED(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
3804}
3805
3806static DECLCALLBACK(RTEXITCODE) handleCtrlSession(PGCTLCMDCTX pCtx)
3807{
3808 AssertPtrReturn(pCtx, RTEXITCODE_FAILURE);
3809
3810 if (pCtx->iArgc < 1)
3811 return errorSyntaxEx(USAGE_GUESTCONTROL, USAGE_GSTCTRL_SESSION,
3812 "Must specify an action");
3813
3814 /** Use RTGetOpt here when handling command line args gets more complex. */
3815
3816 if ( !RTStrICmp(pCtx->ppaArgv[0], "close")
3817 || !RTStrICmp(pCtx->ppaArgv[0], "kill")
3818 || !RTStrICmp(pCtx->ppaArgv[0], "terminate"))
3819 {
3820 pCtx->iFirstArgc++; /* Skip session action. */
3821 return handleCtrlSessionClose(pCtx);
3822 }
3823
3824 return errorSyntaxEx(USAGE_GUESTCONTROL, USAGE_GSTCTRL_SESSION,
3825 "Invalid session action '%s'", pCtx->ppaArgv[0]);
3826}
3827
3828static DECLCALLBACK(RTEXITCODE) handleCtrlWatch(PGCTLCMDCTX pCtx)
3829{
3830 AssertPtrReturn(pCtx, RTEXITCODE_FAILURE);
3831
3832 /*
3833 * Parse arguments.
3834 */
3835 static const RTGETOPTDEF s_aOptions[] = { { 0 } };
3836
3837 int ch;
3838 RTGETOPTUNION ValueUnion;
3839 RTGETOPTSTATE GetState;
3840 RTGetOptInit(&GetState, pCtx->iArgc, pCtx->ppaArgv,
3841 NULL /*s_aOptions*/, 0 /*RT_ELEMENTS(s_aOptions)*/, pCtx->iFirstArgc, RTGETOPTINIT_FLAGS_OPTS_FIRST);
3842
3843 while ((ch = RTGetOpt(&GetState, &ValueUnion)))
3844 {
3845 /* For options that require an argument, ValueUnion has received the value. */
3846 switch (ch)
3847 {
3848 case VINF_GETOPT_NOT_OPTION:
3849 break;
3850
3851 default:
3852 return errorGetOptEx(USAGE_GUESTCONTROL, USAGE_GSTCTRL_WATCH, ch, &ValueUnion);
3853 }
3854 }
3855
3856 /** @todo Specify categories to watch for. */
3857 /** @todo Specify a --timeout for waiting only for a certain amount of time? */
3858
3859 HRESULT rc;
3860
3861 try
3862 {
3863 ComObjPtr<GuestEventListenerImpl> pGuestListener;
3864 do
3865 {
3866 /* Listener creation. */
3867 pGuestListener.createObject();
3868 pGuestListener->init(new GuestEventListener());
3869
3870 /* Register for IGuest events. */
3871 ComPtr<IEventSource> es;
3872 CHECK_ERROR_BREAK(pCtx->pGuest, COMGETTER(EventSource)(es.asOutParam()));
3873 com::SafeArray<VBoxEventType_T> eventTypes;
3874 eventTypes.push_back(VBoxEventType_OnGuestSessionRegistered);
3875 /** @todo Also register for VBoxEventType_OnGuestUserStateChanged on demand? */
3876 CHECK_ERROR_BREAK(es, RegisterListener(pGuestListener, ComSafeArrayAsInParam(eventTypes),
3877 true /* Active listener */));
3878 /* Note: All other guest control events have to be registered
3879 * as their corresponding objects appear. */
3880
3881 } while (0);
3882
3883 if (pCtx->fVerbose)
3884 RTPrintf("Waiting for events ...\n");
3885
3886 while (!g_fGuestCtrlCanceled)
3887 {
3888 /** @todo Timeout handling (see above)? */
3889 RTThreadSleep(10);
3890 }
3891
3892 if (pCtx->fVerbose)
3893 RTPrintf("Signal caught, exiting ...\n");
3894
3895 if (!pGuestListener.isNull())
3896 {
3897 /* Guest callback unregistration. */
3898 ComPtr<IEventSource> pES;
3899 CHECK_ERROR(pCtx->pGuest, COMGETTER(EventSource)(pES.asOutParam()));
3900 if (!pES.isNull())
3901 CHECK_ERROR(pES, UnregisterListener(pGuestListener));
3902 pGuestListener.setNull();
3903 }
3904 }
3905 catch (std::bad_alloc &)
3906 {
3907 rc = E_OUTOFMEMORY;
3908 }
3909
3910 return SUCCEEDED(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
3911}
3912
3913/**
3914 * Access the guest control store.
3915 *
3916 * @returns program exit code.
3917 * @note see the command line API description for parameters
3918 */
3919int handleGuestControl(HandlerArg *pArg)
3920{
3921 AssertPtrReturn(pArg, VERR_INVALID_POINTER);
3922
3923#ifdef DEBUG_andy_disabled
3924 if (RT_FAILURE(tstTranslatePath()))
3925 return RTEXITCODE_FAILURE;
3926#endif
3927
3928 /* pArg->argv[0] contains the VM name. */
3929 /* pArg->argv[1] contains the guest control command. */
3930 if (pArg->argc < 2)
3931 return errorSyntax(USAGE_GUESTCONTROL, "No sub command specified!");
3932
3933 uint32_t uCmdCtxFlags = 0;
3934 uint32_t uUsage;
3935 GCTLCMD gctlCmd;
3936 if ( !RTStrICmp(pArg->argv[1], "exec")
3937 || !RTStrICmp(pArg->argv[1], "execute"))
3938 {
3939 gctlCmd.pfnHandler = handleCtrlProcessExec;
3940 uUsage = USAGE_GSTCTRL_EXEC;
3941 }
3942 else if (!RTStrICmp(pArg->argv[1], "copyfrom"))
3943 {
3944 gctlCmd.pfnHandler = handleCtrlCopyFrom;
3945 uUsage = USAGE_GSTCTRL_COPYFROM;
3946 }
3947 else if ( !RTStrICmp(pArg->argv[1], "copyto")
3948 || !RTStrICmp(pArg->argv[1], "cp"))
3949 {
3950 gctlCmd.pfnHandler = handleCtrlCopyTo;
3951 uUsage = USAGE_GSTCTRL_COPYTO;
3952 }
3953 else if ( !RTStrICmp(pArg->argv[1], "createdirectory")
3954 || !RTStrICmp(pArg->argv[1], "createdir")
3955 || !RTStrICmp(pArg->argv[1], "mkdir")
3956 || !RTStrICmp(pArg->argv[1], "md"))
3957 {
3958 gctlCmd.pfnHandler = handleCtrlCreateDirectory;
3959 uUsage = USAGE_GSTCTRL_CREATEDIR;
3960 }
3961 else if ( !RTStrICmp(pArg->argv[1], "removedirectory")
3962 || !RTStrICmp(pArg->argv[1], "removedir")
3963 || !RTStrICmp(pArg->argv[1], "rmdir"))
3964 {
3965 gctlCmd.pfnHandler = handleCtrlRemoveDirectory;
3966 uUsage = USAGE_GSTCTRL_REMOVEDIR;
3967 }
3968 else if ( !RTStrICmp(pArg->argv[1], "rm")
3969 || !RTStrICmp(pArg->argv[1], "removefile"))
3970 {
3971 gctlCmd.pfnHandler = handleCtrlRemoveFile;
3972 uUsage = USAGE_GSTCTRL_REMOVEFILE;
3973 }
3974 else if ( !RTStrICmp(pArg->argv[1], "ren")
3975 || !RTStrICmp(pArg->argv[1], "rename")
3976 || !RTStrICmp(pArg->argv[1], "mv"))
3977 {
3978 gctlCmd.pfnHandler = handleCtrlRename;
3979 uUsage = USAGE_GSTCTRL_RENAME;
3980 }
3981 else if ( !RTStrICmp(pArg->argv[1], "createtemporary")
3982 || !RTStrICmp(pArg->argv[1], "createtemp")
3983 || !RTStrICmp(pArg->argv[1], "mktemp"))
3984 {
3985 gctlCmd.pfnHandler = handleCtrlCreateTemp;
3986 uUsage = USAGE_GSTCTRL_CREATETEMP;
3987 }
3988 else if ( !RTStrICmp(pArg->argv[1], "kill") /* Linux. */
3989 || !RTStrICmp(pArg->argv[1], "pkill") /* Solaris / *BSD. */
3990 || !RTStrICmp(pArg->argv[1], "pskill")) /* SysInternals version. */
3991 {
3992 /** @todo What about "taskkill" on Windows? */
3993 uCmdCtxFlags = CTLCMDCTX_FLAGS_SESSION_ANONYMOUS
3994 | CTLCMDCTX_FLAGS_NO_SIGNAL_HANDLER;
3995 gctlCmd.pfnHandler = handleCtrlProcessClose;
3996 uUsage = USAGE_GSTCTRL_KILL;
3997 }
3998 /** @todo Implement "killall"? */
3999 else if ( !RTStrICmp(pArg->argv[1], "stat"))
4000 {
4001 gctlCmd.pfnHandler = handleCtrlStat;
4002 uUsage = USAGE_GSTCTRL_STAT;
4003 }
4004 else if ( !RTStrICmp(pArg->argv[1], "updateadditions")
4005 || !RTStrICmp(pArg->argv[1], "updateadds"))
4006 {
4007 uCmdCtxFlags = CTLCMDCTX_FLAGS_SESSION_ANONYMOUS
4008 | CTLCMDCTX_FLAGS_NO_SIGNAL_HANDLER;
4009 gctlCmd.pfnHandler = handleCtrlUpdateAdditions;
4010 uUsage = USAGE_GSTCTRL_UPDATEADDS;
4011 }
4012 else if ( !RTStrICmp(pArg->argv[1], "list"))
4013 {
4014 uCmdCtxFlags = CTLCMDCTX_FLAGS_SESSION_ANONYMOUS
4015 | CTLCMDCTX_FLAGS_NO_SIGNAL_HANDLER;
4016 gctlCmd.pfnHandler = handleCtrlList;
4017 uUsage = USAGE_GSTCTRL_LIST;
4018 }
4019 else if ( !RTStrICmp(pArg->argv[1], "session"))
4020 {
4021 uCmdCtxFlags = CTLCMDCTX_FLAGS_SESSION_ANONYMOUS
4022 | CTLCMDCTX_FLAGS_NO_SIGNAL_HANDLER;
4023 gctlCmd.pfnHandler = handleCtrlSession;
4024 uUsage = USAGE_GSTCTRL_SESSION;
4025 }
4026 else if ( !RTStrICmp(pArg->argv[1], "process"))
4027 {
4028 uCmdCtxFlags = CTLCMDCTX_FLAGS_SESSION_ANONYMOUS
4029 | CTLCMDCTX_FLAGS_NO_SIGNAL_HANDLER;
4030 gctlCmd.pfnHandler = handleCtrlProcess;
4031 uUsage = USAGE_GSTCTRL_PROCESS;
4032 }
4033 else if ( !RTStrICmp(pArg->argv[1], "watch"))
4034 {
4035 uCmdCtxFlags = CTLCMDCTX_FLAGS_SESSION_ANONYMOUS
4036 | CTLCMDCTX_FLAGS_NO_SIGNAL_HANDLER;
4037 gctlCmd.pfnHandler = handleCtrlWatch;
4038 uUsage = USAGE_GSTCTRL_WATCH;
4039 }
4040 else
4041 return errorSyntax(USAGE_GUESTCONTROL, "Unknown sub command '%s' specified!", pArg->argv[1]);
4042
4043 GCTLCMDCTX cmdCtx;
4044 RT_ZERO(cmdCtx);
4045
4046 RTEXITCODE rcExit = ctrlInitVM(pArg, &cmdCtx, uCmdCtxFlags, uUsage);
4047 if (rcExit == RTEXITCODE_SUCCESS)
4048 {
4049 /* Kick off the actual command handler. */
4050 rcExit = gctlCmd.pfnHandler(&cmdCtx);
4051
4052 ctrlUninitVM(&cmdCtx, cmdCtx.uFlags);
4053 return rcExit;
4054 }
4055
4056 return rcExit;
4057}
4058#endif /* !VBOX_ONLY_DOCS */
4059
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