VBoxManageGuestCtrl.cpp revision dc31047bffe0f7c2dda50da6efe001e0cbf9a0a7
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync/* $Id$ */
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync/** @file
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync * VBoxManage - Implementation of guestcontrol command.
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync */
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync/*
c58f1213e628a545081c70e26c6b67a841cff880vboxsync * Copyright (C) 2010-2012 Oracle Corporation
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync *
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync * available from http://www.virtualbox.org. This file is free software;
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync * you can redistribute it and/or modify it under the terms of the GNU
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync * General Public License (GPL) as published by the Free Software
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync */
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync/*******************************************************************************
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync* Header Files *
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync*******************************************************************************/
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync#include "VBoxManage.h"
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync#ifndef VBOX_ONLY_DOCS
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync#include <VBox/com/com.h>
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync#include <VBox/com/string.h>
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync#include <VBox/com/array.h>
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync#include <VBox/com/ErrorInfo.h>
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync#include <VBox/com/errorprint.h>
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync#include <VBox/com/VirtualBox.h>
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync#include <VBox/com/EventQueue.h>
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync#include <VBox/err.h>
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync#include <VBox/log.h>
230bd8589bba39933ac5ec21482d6186d675e604vboxsync
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync#include <iprt/asm.h>
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync#include <iprt/dir.h>
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync#include <iprt/file.h>
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync#include <iprt/isofs.h>
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync#include <iprt/getopt.h>
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync#include <iprt/list.h>
b72d3233df38e3122eda39b39a27b35c27209615vboxsync#include <iprt/path.h>
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync#include <iprt/thread.h>
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync#include <map>
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync#include <vector>
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync#ifdef USE_XPCOM_QUEUE
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync# include <sys/select.h>
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync# include <errno.h>
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync#endif
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync#include <signal.h>
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync#ifdef RT_OS_DARWIN
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync# include <CoreFoundation/CFRunLoop.h>
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync#endif
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsyncusing namespace com;
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync/**
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync * IVirtualBoxCallback implementation for handling the GuestControlCallback in
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync * relation to the "guestcontrol * wait" command.
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync */
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync/** @todo */
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync/** Set by the signal handler. */
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsyncstatic volatile bool g_fGuestCtrlCanceled = false;
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsynctypedef struct COPYCONTEXT
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync{
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync COPYCONTEXT() : fVerbose(false), fDryRun(false), fHostToGuest(false)
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync {
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync }
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync ComPtr<IGuestSession> pGuestSession;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync bool fVerbose;
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync bool fDryRun;
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync bool fHostToGuest;
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync} COPYCONTEXT, *PCOPYCONTEXT;
230bd8589bba39933ac5ec21482d6186d675e604vboxsync
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync/**
230bd8589bba39933ac5ec21482d6186d675e604vboxsync * An entry for a source element, including an optional DOS-like wildcard (*,?).
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync */
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsyncclass SOURCEFILEENTRY
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync{
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync public:
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync SOURCEFILEENTRY(const char *pszSource, const char *pszFilter)
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync : mSource(pszSource),
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync mFilter(pszFilter) {}
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync SOURCEFILEENTRY(const char *pszSource)
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync : mSource(pszSource)
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync {
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync Parse(pszSource);
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync }
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync const char* GetSource() const
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync {
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync return mSource.c_str();
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync }
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync const char* GetFilter() const
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync {
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync return mFilter.c_str();
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync }
0368e9c310393e82ef37c480b6acbd0f107cf0edvboxsync
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync private:
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync int Parse(const char *pszPath)
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync {
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync if ( !RTFileExists(pszPath)
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync && !RTDirExists(pszPath))
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync {
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync /* No file and no directory -- maybe a filter? */
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync char *pszFilename = RTPathFilename(pszPath);
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync if ( pszFilename
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync && strpbrk(pszFilename, "*?"))
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync {
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync /* Yep, get the actual filter part. */
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync mFilter = RTPathFilename(pszPath);
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync /* Remove the filter from actual sourcec directory name. */
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync RTPathStripFilename(mSource.mutableRaw());
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync mSource.jolt();
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync }
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync }
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync return VINF_SUCCESS; /* @todo */
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync }
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync private:
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync Utf8Str mSource;
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync Utf8Str mFilter;
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync};
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsynctypedef std::vector<SOURCEFILEENTRY> SOURCEVEC, *PSOURCEVEC;
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync/**
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync * An entry for an element which needs to be copied/created to/on the guest.
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync */
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsynctypedef struct DESTFILEENTRY
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync{
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync DESTFILEENTRY(Utf8Str strFileName) : mFileName(strFileName) {}
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync Utf8Str mFileName;
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync} DESTFILEENTRY, *PDESTFILEENTRY;
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync/*
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync * Map for holding destination entires, whereas the key is the destination
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync * directory and the mapped value is a vector holding all elements for this directoy.
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync */
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsynctypedef std::map< Utf8Str, std::vector<DESTFILEENTRY> > DESTDIRMAP, *PDESTDIRMAP;
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsynctypedef std::map< Utf8Str, std::vector<DESTFILEENTRY> >::iterator DESTDIRMAPITER, *PDESTDIRMAPITER;
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync/**
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync * Special exit codes for returning errors/information of a
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync * started guest process to the command line VBoxManage was started from.
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync * Useful for e.g. scripting.
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync *
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync * @note These are frozen as of 4.1.0.
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync */
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsyncenum EXITCODEEXEC
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync{
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync EXITCODEEXEC_SUCCESS = RTEXITCODE_SUCCESS,
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync /* Process exited normally but with an exit code <> 0. */
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync EXITCODEEXEC_CODE = 16,
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync EXITCODEEXEC_FAILED = 17,
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync EXITCODEEXEC_TERM_SIGNAL = 18,
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync EXITCODEEXEC_TERM_ABEND = 19,
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync EXITCODEEXEC_TIMEOUT = 20,
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync EXITCODEEXEC_DOWN = 21,
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync EXITCODEEXEC_CANCELED = 22
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync};
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync/**
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync * RTGetOpt-IDs for the guest execution control command line.
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync */
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsyncenum GETOPTDEF_EXEC
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync{
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync GETOPTDEF_EXEC_IGNOREORPHANEDPROCESSES = 1000,
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync GETOPTDEF_EXEC_NO_PROFILE,
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync GETOPTDEF_EXEC_OUTPUTFORMAT,
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync GETOPTDEF_EXEC_DOS2UNIX,
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync GETOPTDEF_EXEC_UNIX2DOS,
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync GETOPTDEF_EXEC_PASSWORD,
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync GETOPTDEF_EXEC_WAITFOREXIT,
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync GETOPTDEF_EXEC_WAITFORSTDOUT,
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync GETOPTDEF_EXEC_WAITFORSTDERR
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync};
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsyncenum GETOPTDEF_COPY
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync{
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync GETOPTDEF_COPY_DRYRUN = 1000,
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync GETOPTDEF_COPY_FOLLOW,
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync GETOPTDEF_COPY_PASSWORD,
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync GETOPTDEF_COPY_TARGETDIR
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync};
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsyncenum GETOPTDEF_MKDIR
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync{
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync GETOPTDEF_MKDIR_PASSWORD = 1000
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync};
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsyncenum GETOPTDEF_STAT
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync{
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync GETOPTDEF_STAT_PASSWORD = 1000
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync};
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsyncenum OUTPUTTYPE
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync{
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync OUTPUTTYPE_UNDEFINED = 0,
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync OUTPUTTYPE_DOS2UNIX = 10,
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync OUTPUTTYPE_UNIX2DOS = 20
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync};
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsyncstatic int ctrlCopyDirExists(PCOPYCONTEXT pContext, bool bGuest, const char *pszDir, bool *fExists);
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync#endif /* VBOX_ONLY_DOCS */
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsyncvoid usageGuestControl(PRTSTREAM pStrm, const char *pcszSep1, const char *pcszSep2)
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync{
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync RTStrmPrintf(pStrm,
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync "%s guestcontrol %s <vmname>|<uuid>\n"
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync " exec[ute]\n"
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync " --image <path to program> --username <name>\n"
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync " [--passwordfile <file> | --password <password>]\n"
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync " [--domain <domain>] [--verbose] [--timeout <msec>]\n"
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync " [--environment \"<NAME>=<VALUE> [<NAME>=<VALUE>]\"]\n"
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync " [--wait-exit] [--wait-stdout] [--wait-stderr]\n"
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync " [--dos2unix] [--unix2dos]\n"
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync " [-- [<argument1>] ... [<argumentN>]]\n"
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync /** @todo Add a "--" parameter (has to be last parameter) to directly execute
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync * stuff, e.g. "VBoxManage guestcontrol execute <VMName> --username <> ... -- /bin/rm -Rf /foo". */
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync "\n"
b72d3233df38e3122eda39b39a27b35c27209615vboxsync " copyfrom\n"
b72d3233df38e3122eda39b39a27b35c27209615vboxsync " <guest source> <host dest> --username <name>\n"
5981e6935987b08737b730b63a41acc1dd696377vboxsync " [--passwordfile <file> | --password <password>]\n"
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync " [--domain <domain>] [--verbose]\n"
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync " [--dryrun] [--follow] [--recursive]\n"
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync "\n"
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync " copyto|cp\n"
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync " <host source> <guest dest> --username <name>\n"
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync " [--passwordfile <file> | --password <password>]\n"
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync " [--domain <domain>] [--verbose]\n"
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync " [--dryrun] [--follow] [--recursive]\n"
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync "\n"
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync " createdir[ectory]|mkdir|md\n"
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync " <guest directory>... --username <name>\n"
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync " [--passwordfile <file> | --password <password>]\n"
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync " [--domain <domain>] [--verbose]\n"
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync " [--parents] [--mode <mode>]\n"
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync "\n"
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync " stat\n"
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync " <file>... --username <name>\n"
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync " [--passwordfile <file> | --password <password>]\n"
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync " [--domain <domain>] [--verbose]\n"
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync "\n"
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync " updateadditions\n"
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync " [--source <guest additions .ISO>] [--verbose]\n"
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync " [--wait-start]\n"
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync "\n", pcszSep1, pcszSep2);
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync}
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync#ifndef VBOX_ONLY_DOCS
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync/**
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync * Signal handler that sets g_fGuestCtrlCanceled.
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync *
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync * This can be executed on any thread in the process, on Windows it may even be
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync * a thread dedicated to delivering this signal. Do not doing anything
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync * unnecessary here.
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync */
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsyncstatic void guestCtrlSignalHandler(int iSignal)
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync{
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync NOREF(iSignal);
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync ASMAtomicWriteBool(&g_fGuestCtrlCanceled, true);
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync}
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync/**
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync * Installs a custom signal handler to get notified
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync * whenever the user wants to intercept the program.
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync */
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsyncstatic void ctrlSignalHandlerInstall()
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync{
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync signal(SIGINT, guestCtrlSignalHandler);
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync#ifdef SIGBREAK
683eff3070b1b86fe71b71af7fda82766ea19d17vboxsync signal(SIGBREAK, guestCtrlSignalHandler);
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync#endif
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync}
683eff3070b1b86fe71b71af7fda82766ea19d17vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync/**
683eff3070b1b86fe71b71af7fda82766ea19d17vboxsync * Uninstalls a previously installed signal handler.
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync */
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsyncstatic void ctrlSignalHandlerUninstall()
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync{
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync signal(SIGINT, SIG_DFL);
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync#ifdef SIGBREAK
683eff3070b1b86fe71b71af7fda82766ea19d17vboxsync signal(SIGBREAK, SIG_DFL);
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync#endif
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync}
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync/**
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync * Translates a process status to a human readable
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync * string.
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync */
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsyncstatic const char *ctrlExecProcessStatusToText(ProcessStatus_T enmStatus)
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync{
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync switch (enmStatus)
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync {
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync case ProcessStatus_Starting:
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync return "starting";
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync case ProcessStatus_Started:
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync return "started";
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync case ProcessStatus_Paused:
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync return "paused";
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync case ProcessStatus_Terminating:
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync return "terminating";
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync case ProcessStatus_TerminatedNormally:
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync return "successfully terminated";
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync case ProcessStatus_TerminatedSignal:
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync return "terminated by signal";
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync case ProcessStatus_TerminatedAbnormally:
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync return "abnormally aborted";
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync case ProcessStatus_TimedOutKilled:
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync return "timed out";
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync case ProcessStatus_TimedOutAbnormally:
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync return "timed out, hanging";
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync case ProcessStatus_Down:
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync return "killed";
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync case ProcessStatus_Error:
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync return "error";
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync default:
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync break;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync }
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync return "unknown";
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync}
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsyncstatic int ctrlExecProcessStatusToExitCode(ProcessStatus_T enmStatus, ULONG uExitCode)
683eff3070b1b86fe71b71af7fda82766ea19d17vboxsync{
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync int vrc = EXITCODEEXEC_SUCCESS;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync switch (enmStatus)
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync {
683eff3070b1b86fe71b71af7fda82766ea19d17vboxsync case ProcessStatus_Starting:
683eff3070b1b86fe71b71af7fda82766ea19d17vboxsync vrc = EXITCODEEXEC_SUCCESS;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync break;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync case ProcessStatus_Started:
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync vrc = EXITCODEEXEC_SUCCESS;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync break;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync case ProcessStatus_Paused:
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync vrc = EXITCODEEXEC_SUCCESS;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync break;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync case ProcessStatus_Terminating:
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync vrc = EXITCODEEXEC_SUCCESS;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync break;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync case ProcessStatus_TerminatedNormally:
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync vrc = !uExitCode ? EXITCODEEXEC_SUCCESS : EXITCODEEXEC_CODE;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync break;
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync case ProcessStatus_TerminatedSignal:
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync vrc = EXITCODEEXEC_TERM_SIGNAL;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync break;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync case ProcessStatus_TerminatedAbnormally:
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync vrc = EXITCODEEXEC_TERM_ABEND;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync break;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync case ProcessStatus_TimedOutKilled:
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync vrc = EXITCODEEXEC_TIMEOUT;
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync break;
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync case ProcessStatus_TimedOutAbnormally:
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync vrc = EXITCODEEXEC_TIMEOUT;
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync break;
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync case ProcessStatus_Down:
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync /* Service/OS is stopping, process was killed, so
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync * not exactly an error of the started process ... */
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync vrc = EXITCODEEXEC_DOWN;
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync break;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync case ProcessStatus_Error:
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync vrc = EXITCODEEXEC_FAILED;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync break;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync default:
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync AssertMsgFailed(("Unknown exit code (%u) from guest process returned!\n", enmStatus));
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync break;
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync }
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync return vrc;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync}
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsyncstatic int ctrlPrintError(com::ErrorInfo &errorInfo)
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync{
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync if ( errorInfo.isFullAvailable()
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync || errorInfo.isBasicAvailable())
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync {
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync /* If we got a VBOX_E_IPRT error we handle the error in a more gentle way
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync * because it contains more accurate info about what went wrong. */
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync if (errorInfo.getResultCode() == VBOX_E_IPRT_ERROR)
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync RTMsgError("%ls.", errorInfo.getText().raw());
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync else
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync {
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync RTMsgError("Error details:");
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync GluePrintErrorInfo(errorInfo);
683eff3070b1b86fe71b71af7fda82766ea19d17vboxsync }
683eff3070b1b86fe71b71af7fda82766ea19d17vboxsync return VERR_GENERAL_FAILURE; /** @todo */
b72d3233df38e3122eda39b39a27b35c27209615vboxsync }
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync AssertMsgFailedReturn(("Object has indicated no error (%Rhrc)!?\n", errorInfo.getResultCode()),
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync VERR_INVALID_PARAMETER);
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync}
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync
b72d3233df38e3122eda39b39a27b35c27209615vboxsyncstatic int ctrlPrintError(IUnknown *pObj, const GUID &aIID)
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync{
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync com::ErrorInfo ErrInfo(pObj, aIID);
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync return ctrlPrintError(ErrInfo);
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync}
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsyncstatic int ctrlPrintProgressError(ComPtr<IProgress> pProgress)
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync{
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync int vrc = VINF_SUCCESS;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync HRESULT rc;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync do
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync {
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync BOOL fCanceled;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync CHECK_ERROR_BREAK(pProgress, COMGETTER(Canceled)(&fCanceled));
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync if (!fCanceled)
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync {
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync LONG rcProc;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync CHECK_ERROR_BREAK(pProgress, COMGETTER(ResultCode)(&rcProc));
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync if (FAILED(rcProc))
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync {
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync com::ProgressErrorInfo ErrInfo(pProgress);
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync vrc = ctrlPrintError(ErrInfo);
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync }
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync }
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync } while(0);
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync if (FAILED(rc))
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync AssertMsgStmt(NULL, ("Could not lookup progress information\n"), vrc = VERR_COM_UNEXPECTED);
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync return vrc;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync}
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync/**
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync * Un-initializes the VM after guest control usage.
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync */
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsyncstatic void ctrlUninitVM(HandlerArg *pArg)
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync{
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync AssertPtrReturnVoid(pArg);
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync if (pArg->session)
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync pArg->session->UnlockMachine();
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync}
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync/**
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync * Initializes the VM for IGuest operations.
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync *
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync * That is, checks whether it's up and running, if it can be locked (shared
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync * only) and returns a valid IGuest pointer on success.
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync *
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync * @return IPRT status code.
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync * @param pArg Our command line argument structure.
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync * @param pszNameOrId The VM's name or UUID.
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync * @param pGuest Where to return the IGuest interface pointer.
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync */
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsyncstatic int ctrlInitVM(HandlerArg *pArg, const char *pszNameOrId, ComPtr<IGuest> *pGuest)
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync{
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync AssertPtrReturn(pArg, VERR_INVALID_PARAMETER);
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync AssertPtrReturn(pszNameOrId, VERR_INVALID_PARAMETER);
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync /* Lookup VM. */
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync ComPtr<IMachine> machine;
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync /* Assume it's an UUID. */
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync HRESULT rc;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync CHECK_ERROR(pArg->virtualBox, FindMachine(Bstr(pszNameOrId).raw(),
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync machine.asOutParam()));
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync if (FAILED(rc))
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync return VERR_NOT_FOUND;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync /* Machine is running? */
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync MachineState_T machineState;
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync CHECK_ERROR_RET(machine, COMGETTER(State)(&machineState), 1);
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync if (machineState != MachineState_Running)
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync {
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync RTMsgError("Machine \"%s\" is not running (currently %s)!\n",
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync pszNameOrId, machineStateToName(machineState, false));
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync return VERR_VM_INVALID_VM_STATE;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync }
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync do
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync {
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync /* Open a session for the VM. */
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync CHECK_ERROR_BREAK(machine, LockMachine(pArg->session, LockType_Shared));
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync /* Get the associated console. */
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync ComPtr<IConsole> console;
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync CHECK_ERROR_BREAK(pArg->session, COMGETTER(Console)(console.asOutParam()));
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync /* ... and session machine. */
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync ComPtr<IMachine> sessionMachine;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync CHECK_ERROR_BREAK(pArg->session, COMGETTER(Machine)(sessionMachine.asOutParam()));
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync /* Get IGuest interface. */
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync CHECK_ERROR_BREAK(console, COMGETTER(Guest)(pGuest->asOutParam()));
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync } while (0);
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync if (FAILED(rc))
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync ctrlUninitVM(pArg);
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync return SUCCEEDED(rc) ? VINF_SUCCESS : VERR_GENERAL_FAILURE;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync}
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync/**
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync * Prints the desired guest output to a stream.
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync *
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync * @return IPRT status code.
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync * @param pProcess Pointer to appropriate process object.
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync * @param pStrmOutput Where to write the data.
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync * @param hStream Where to read the data from.
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync */
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsyncstatic int ctrlExecPrintOutput(IProcess *pProcess, PRTSTREAM pStrmOutput,
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync ULONG uHandle)
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync{
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync AssertPtrReturn(pProcess, VERR_INVALID_POINTER);
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync AssertPtrReturn(pStrmOutput, VERR_INVALID_POINTER);
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync int vrc = VINF_SUCCESS;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync SafeArray<BYTE> aOutputData;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync HRESULT rc = pProcess->Read(uHandle, _64K, 30 * 1000 /* 30s timeout. */,
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync ComSafeArrayAsOutParam(aOutputData));
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync if (FAILED(rc))
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync vrc = ctrlPrintError(pProcess, COM_IIDOF(IProcess));
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync else
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync {
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync /** @todo implement the dos2unix/unix2dos conversions */
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync vrc = RTStrmWrite(pStrmOutput, aOutputData.raw(), aOutputData.size());
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync if (RT_FAILURE(vrc))
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync RTMsgError("Unable to write output, rc=%Rrc\n", vrc);
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync }
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync return vrc;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync}
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync/**
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync * Returns the remaining time (in ms) based on the start time and a set
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync * timeout value. Returns RT_INDEFINITE_WAIT if no timeout was specified.
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync *
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync * @return RTMSINTERVAL Time left (in ms).
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync * @param u64StartMs Start time (in ms).
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync * @param cMsTimeout Timeout value (in ms).
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync */
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsyncinline RTMSINTERVAL ctrlExecGetRemainingTime(uint64_t u64StartMs, RTMSINTERVAL cMsTimeout)
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync{
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync if (!cMsTimeout || cMsTimeout == RT_INDEFINITE_WAIT) /* If no timeout specified, wait forever. */
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync return RT_INDEFINITE_WAIT;
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync uint32_t u64ElapsedMs = RTTimeMilliTS() - u64StartMs;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync if (u64ElapsedMs >= cMsTimeout)
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync return 0;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync return cMsTimeout - u64ElapsedMs;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync}
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync/* <Missing documentation> */
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsyncstatic int handleCtrlExecProgram(ComPtr<IGuest> pGuest, HandlerArg *pArg)
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync{
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync AssertPtrReturn(pArg, VERR_INVALID_PARAMETER);
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync /*
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync * Parse arguments.
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync */
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync static const RTGETOPTDEF s_aOptions[] =
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync {
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync { "--dos2unix", GETOPTDEF_EXEC_DOS2UNIX, RTGETOPT_REQ_NOTHING },
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync { "--environment", 'e', RTGETOPT_REQ_STRING },
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync { "--flags", 'f', RTGETOPT_REQ_STRING },
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync { "--ignore-operhaned-processes", GETOPTDEF_EXEC_IGNOREORPHANEDPROCESSES, RTGETOPT_REQ_NOTHING },
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync { "--image", 'i', RTGETOPT_REQ_STRING },
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync { "--no-profile", GETOPTDEF_EXEC_NO_PROFILE, RTGETOPT_REQ_NOTHING },
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync { "--username", 'u', RTGETOPT_REQ_STRING },
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync { "--passwordfile", 'p', RTGETOPT_REQ_STRING },
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync { "--password", GETOPTDEF_EXEC_PASSWORD, RTGETOPT_REQ_STRING },
b72d3233df38e3122eda39b39a27b35c27209615vboxsync { "--domain", 'd', RTGETOPT_REQ_STRING },
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync { "--timeout", 't', RTGETOPT_REQ_UINT32 },
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync { "--unix2dos", GETOPTDEF_EXEC_UNIX2DOS, RTGETOPT_REQ_NOTHING },
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync { "--wait-exit", GETOPTDEF_EXEC_WAITFOREXIT, RTGETOPT_REQ_NOTHING },
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync { "--wait-stdout", GETOPTDEF_EXEC_WAITFORSTDOUT, RTGETOPT_REQ_NOTHING },
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync { "--wait-stderr", GETOPTDEF_EXEC_WAITFORSTDERR, RTGETOPT_REQ_NOTHING }
b72d3233df38e3122eda39b39a27b35c27209615vboxsync };
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync int ch;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync RTGETOPTUNION ValueUnion;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync RTGETOPTSTATE GetState;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync RTGetOptInit(&GetState, pArg->argc, pArg->argv, s_aOptions, RT_ELEMENTS(s_aOptions), 0, 0);
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync Utf8Str strCmd;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync com::SafeArray<ProcessCreateFlag_T> aCreateFlags;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync com::SafeArray<ProcessWaitForFlag_T> aWaitFlags;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync com::SafeArray<IN_BSTR> args;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync com::SafeArray<IN_BSTR> env;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync Utf8Str strUsername;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync Utf8Str strPassword;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync Utf8Str strDomain;
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync RTMSINTERVAL cMsTimeout = 0;
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync OUTPUTTYPE eOutputType = OUTPUTTYPE_UNDEFINED;
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync bool fWaitForExit = false;
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync bool fVerbose = false;
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync int vrc = VINF_SUCCESS;
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync /* Wait for process start in any case. This is useful for scripting VBoxManage
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync * when relying on its overall exit code. */
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync aWaitFlags.push_back(ProcessWaitForFlag_Start);
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync while ( (ch = RTGetOpt(&GetState, &ValueUnion))
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync && RT_SUCCESS(vrc))
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync {
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync /* For options that require an argument, ValueUnion has received the value. */
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync switch (ch)
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync {
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync case GETOPTDEF_EXEC_DOS2UNIX:
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync if (eOutputType != OUTPUTTYPE_UNDEFINED)
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync return errorSyntax(USAGE_GUESTCONTROL, "More than one output type (dos2unix/unix2dos) specified!");
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync eOutputType = OUTPUTTYPE_DOS2UNIX;
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync break;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync case 'e': /* Environment */
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync {
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync char **papszArg;
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync int cArgs;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync
b72d3233df38e3122eda39b39a27b35c27209615vboxsync vrc = RTGetOptArgvFromString(&papszArg, &cArgs, ValueUnion.psz, NULL);
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync if (RT_FAILURE(vrc))
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync return errorSyntax(USAGE_GUESTCONTROL, "Failed to parse environment value, rc=%Rrc", vrc);
6d41476175401a18893ea8cb8a40d125eefa04f3vboxsync for (int j = 0; j < cArgs; j++)
b72d3233df38e3122eda39b39a27b35c27209615vboxsync env.push_back(Bstr(papszArg[j]).raw());
b72d3233df38e3122eda39b39a27b35c27209615vboxsync
b72d3233df38e3122eda39b39a27b35c27209615vboxsync RTGetOptArgvFree(papszArg);
b72d3233df38e3122eda39b39a27b35c27209615vboxsync break;
b72d3233df38e3122eda39b39a27b35c27209615vboxsync }
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync case GETOPTDEF_EXEC_IGNOREORPHANEDPROCESSES:
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync aCreateFlags.push_back(ProcessCreateFlag_IgnoreOrphanedProcesses);
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync break;
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync case GETOPTDEF_EXEC_NO_PROFILE:
aCreateFlags.push_back(ProcessCreateFlag_NoProfile);
break;
case 'i':
strCmd = ValueUnion.psz;
break;
/** @todo Add a hidden flag. */
case 'u': /* User name */
strUsername = ValueUnion.psz;
break;
case GETOPTDEF_EXEC_PASSWORD: /* Password */
strPassword = ValueUnion.psz;
break;
case 'p': /* Password file */
{
RTEXITCODE rcExit = readPasswordFile(ValueUnion.psz, &strPassword);
if (rcExit != RTEXITCODE_SUCCESS)
return rcExit;
break;
}
case 'd': /* domain */
strDomain = ValueUnion.psz;
break;
case 't': /* Timeout */
cMsTimeout = ValueUnion.u32;
break;
case GETOPTDEF_EXEC_UNIX2DOS:
if (eOutputType != OUTPUTTYPE_UNDEFINED)
return errorSyntax(USAGE_GUESTCONTROL, "More than one output type (dos2unix/unix2dos) specified!");
eOutputType = OUTPUTTYPE_UNIX2DOS;
break;
case 'v': /* Verbose */
fVerbose = true;
break;
case GETOPTDEF_EXEC_WAITFOREXIT:
aWaitFlags.push_back(ProcessWaitForFlag_Terminate);
fWaitForExit = true;
break;
case GETOPTDEF_EXEC_WAITFORSTDOUT:
aCreateFlags.push_back(ProcessCreateFlag_WaitForStdOut);
aWaitFlags.push_back(ProcessWaitForFlag_StdOut);
fWaitForExit = true;
break;
case GETOPTDEF_EXEC_WAITFORSTDERR:
aCreateFlags.push_back(ProcessCreateFlag_WaitForStdErr);
aWaitFlags.push_back(ProcessWaitForFlag_StdErr);
fWaitForExit = true;
break;
case VINF_GETOPT_NOT_OPTION:
{
if (args.size() == 0 && strCmd.isEmpty())
strCmd = ValueUnion.psz;
else
args.push_back(Bstr(ValueUnion.psz).raw());
break;
}
default:
return RTGetOptPrintError(ch, &ValueUnion);
}
}
if (strCmd.isEmpty())
return errorSyntax(USAGE_GUESTCONTROL, "No command to execute specified!");
if (strUsername.isEmpty())
return errorSyntax(USAGE_GUESTCONTROL, "No user name specified!");
/* Any output conversion not supported yet! */
if (eOutputType != OUTPUTTYPE_UNDEFINED)
return errorSyntax(USAGE_GUESTCONTROL, "Output conversion not implemented yet!");
/*
* Start with the real work.
*/
HRESULT rc = S_OK;
if (fVerbose)
{
if (cMsTimeout == 0)
RTPrintf("Waiting for guest to start process ...\n");
else
RTPrintf("Waiting for guest to start process (within %ums)\n", cMsTimeout);
}
ComPtr<IGuestSession> pGuestSession;
rc = pGuest->CreateSession(Bstr(strUsername).raw(),
Bstr(strPassword).raw(),
Bstr(strDomain).raw(),
Bstr("VBoxManage Guest Control Exec").raw(),
pGuestSession.asOutParam());
if (FAILED(rc))
{
ctrlPrintError(pGuest, COM_IIDOF(IGuest));
return RTEXITCODE_FAILURE;
}
/* Get current time stamp to later calculate rest of timeout left. */
uint64_t u64StartMS = RTTimeMilliTS();
/*
* Execute the process.
*/
ComPtr<IGuestProcess> pProcess;
rc = pGuestSession->ProcessCreate(Bstr(strCmd).raw(),
ComSafeArrayAsInParam(args),
ComSafeArrayAsInParam(env),
ComSafeArrayAsInParam(aCreateFlags),
cMsTimeout,
pProcess.asOutParam());
if (FAILED(rc))
{
ctrlPrintError(pGuestSession, COM_IIDOF(IGuestSession));
return RTEXITCODE_FAILURE;
}
if (fWaitForExit)
{
if (fVerbose)
{
if (cMsTimeout) /* Wait with a certain timeout. */
{
/* Calculate timeout value left after process has been started. */
uint64_t u64Elapsed = RTTimeMilliTS() - u64StartMS;
/* Is timeout still bigger than current difference? */
if (cMsTimeout > u64Elapsed)
RTPrintf("Waiting for process to exit (%ums left) ...\n", cMsTimeout - u64Elapsed);
else
RTPrintf("No time left to wait for process!\n"); /** @todo a bit misleading ... */
}
else /* Wait forever. */
RTPrintf("Waiting for process to exit ...\n");
}
/** @todo does this need signal handling? there's no progress object etc etc */
vrc = RTStrmSetMode(g_pStdOut, 1 /* Binary mode */, -1 /* Code set, unchanged */);
if (RT_FAILURE(vrc))
RTMsgError("Unable to set stdout's binary mode, rc=%Rrc\n", vrc);
vrc = RTStrmSetMode(g_pStdErr, 1 /* Binary mode */, -1 /* Code set, unchanged */);
if (RT_FAILURE(vrc))
RTMsgError("Unable to set stderr's binary mode, rc=%Rrc\n", vrc);
/* Wait for process to exit ... */
RTMSINTERVAL cMsTimeLeft = 1;
bool fReadStdOut, fReadStdErr;
fReadStdOut = fReadStdErr = false;
bool fCompleted = false;
while (!fCompleted && cMsTimeLeft != 0)
{
cMsTimeLeft = ctrlExecGetRemainingTime(u64StartMS, cMsTimeout);
ProcessWaitResult_T waitResult;
rc = pProcess->WaitForArray(ComSafeArrayAsInParam(aWaitFlags), cMsTimeLeft, &waitResult);
if (FAILED(rc))
{
ctrlPrintError(pProcess, COM_IIDOF(IProcess));
return RTEXITCODE_FAILURE;
}
switch (waitResult)
{
case ProcessWaitResult_Start:
{
if (fVerbose)
{
ULONG uPID = 0;
rc = pProcess->COMGETTER(PID)(&uPID);
if (FAILED(rc))
{
ctrlPrintError(pProcess, COM_IIDOF(IProcess));
return RTEXITCODE_FAILURE;
}
RTPrintf("Process '%s' (PID: %u) started\n", strCmd.c_str(), uPID);
}
break;
}
case ProcessWaitResult_StdOut:
fReadStdOut = true;
break;
case ProcessWaitResult_StdErr:
fReadStdErr = true;
break;
case ProcessWaitResult_Terminate:
/* Process terminated, we're done */
fCompleted = true;
break;
case ProcessWaitResult_WaitFlagNotSupported:
{
/* The guest does not support waiting for stdout/err, so
* yield to reduce the CPU load due to busy waiting. */
RTThreadYield(); /* Optional, don't check rc. */
/* Try both, stdout + stderr. */
fReadStdOut = fReadStdErr = true;
break;
}
default:
/* Ignore all other results, let the timeout expire */;
break;
}
if (fReadStdOut) /* Do we need to fetch stdout data? */
{
vrc = ctrlExecPrintOutput(pProcess, g_pStdOut, 1 /* StdOut */);
fReadStdOut = false;
}
if (fReadStdErr) /* Do we need to fetch stdout data? */
{
vrc = ctrlExecPrintOutput(pProcess, g_pStdErr, 2 /* StdErr */);
fReadStdErr = false;
}
if (RT_FAILURE(vrc))
break;
} /* while */
/* Report status back to the user. */
if (fCompleted)
{
ProcessStatus_T status;
rc = pProcess->COMGETTER(Status)(&status);
if (FAILED(rc))
{
ctrlPrintError(pProcess, COM_IIDOF(IProcess));
return RTEXITCODE_FAILURE;
}
LONG exitCode;
rc = pProcess->COMGETTER(ExitCode)(&exitCode);
if (FAILED(rc))
{
ctrlPrintError(pProcess, COM_IIDOF(IProcess));
return RTEXITCODE_FAILURE;
}
if (fVerbose)
RTPrintf("Exit code=%u (Status=%u [%s])\n", exitCode, status, ctrlExecProcessStatusToText(status));
return ctrlExecProcessStatusToExitCode(status, exitCode);
}
else
{
if (fVerbose)
RTPrintf("Process execution aborted!\n");
return EXITCODEEXEC_TERM_ABEND;
}
}
return RT_FAILURE(vrc) || FAILED(rc) ? RTEXITCODE_FAILURE : RTEXITCODE_SUCCESS;
}
/**
* Creates a copy context structure which then can be used with various
* guest control copy functions. Needs to be free'd with ctrlCopyContextFree().
*
* @return IPRT status code.
* @param pGuest Pointer to IGuest interface to use.
* @param fVerbose Flag indicating if we want to run in verbose mode.
* @param fDryRun Flag indicating if we want to run a dry run only.
* @param fHostToGuest Flag indicating if we want to copy from host to guest
* or vice versa.
* @param strUsername Username of account to use on the guest side.
* @param strPassword Password of account to use.
* @param strDomain Domain of account to use.
* @param strSessionName Session name (only for identification purposes).
* @param ppContext Pointer which receives the allocated copy context.
*/
static int ctrlCopyContextCreate(IGuest *pGuest, bool fVerbose, bool fDryRun,
bool fHostToGuest, const Utf8Str &strUsername,
const Utf8Str &strPassword, const Utf8Str &strDomain,
const Utf8Str &strSessionName,
PCOPYCONTEXT *ppContext)
{
AssertPtrReturn(pGuest, VERR_INVALID_POINTER);
PCOPYCONTEXT pContext = new COPYCONTEXT();
AssertPtrReturn(pContext, VERR_NO_MEMORY); /**< @todo r=klaus cannot happen with new */
ComPtr<IGuestSession> pGuestSession;
HRESULT rc = pGuest->CreateSession(Bstr(strUsername).raw(),
Bstr(strPassword).raw(),
Bstr(strDomain).raw(),
Bstr(strSessionName).raw(),
pGuestSession.asOutParam());
if (FAILED(rc))
return ctrlPrintError(pGuest, COM_IIDOF(IGuest));
pContext->fVerbose = fVerbose;
pContext->fDryRun = fDryRun;
pContext->fHostToGuest = fHostToGuest;
pContext->pGuestSession = pGuestSession;
*ppContext = pContext;
return VINF_SUCCESS;
}
/**
* Frees are previously allocated copy context structure.
*
* @param pContext Pointer to copy context to free.
*/
static void ctrlCopyContextFree(PCOPYCONTEXT pContext)
{
if (pContext)
{
if (pContext->pGuestSession)
pContext->pGuestSession->Close();
delete pContext;
}
}
/**
* Translates a source path to a destination path (can be both sides,
* either host or guest). The source root is needed to determine the start
* of the relative source path which also needs to present in the destination
* path.
*
* @return IPRT status code.
* @param pszSourceRoot Source root path. No trailing directory slash!
* @param pszSource Actual source to transform. Must begin with
* the source root path!
* @param pszDest Destination path.
* @param ppszTranslated Pointer to the allocated, translated destination
* path. Must be free'd with RTStrFree().
*/
static int ctrlCopyTranslatePath(const char *pszSourceRoot, const char *pszSource,
const char *pszDest, char **ppszTranslated)
{
AssertPtrReturn(pszSourceRoot, VERR_INVALID_POINTER);
AssertPtrReturn(pszSource, VERR_INVALID_POINTER);
AssertPtrReturn(pszDest, VERR_INVALID_POINTER);
AssertPtrReturn(ppszTranslated, VERR_INVALID_POINTER);
#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} */
AssertReturn(RTPathStartsWith(pszSource, pszSourceRoot), VERR_INVALID_PARAMETER);
#endif
/* Construct the relative dest destination path by "subtracting" the
* source from the source root, e.g.
*
* source root path = "e:\foo\", source = "e:\foo\bar"
* dest = "d:\baz\"
* translated = "d:\baz\bar\"
*/
char szTranslated[RTPATH_MAX];
size_t srcOff = strlen(pszSourceRoot);
AssertReturn(srcOff, VERR_INVALID_PARAMETER);
char *pszDestPath = RTStrDup(pszDest);
AssertPtrReturn(pszDestPath, VERR_NO_MEMORY);
int vrc;
if (!RTPathFilename(pszDestPath))
{
vrc = RTPathJoin(szTranslated, sizeof(szTranslated),
pszDestPath, &pszSource[srcOff]);
}
else
{
char *pszDestFileName = RTStrDup(RTPathFilename(pszDestPath));
if (pszDestFileName)
{
RTPathStripFilename(pszDestPath);
vrc = RTPathJoin(szTranslated, sizeof(szTranslated),
pszDestPath, pszDestFileName);
RTStrFree(pszDestFileName);
}
else
vrc = VERR_NO_MEMORY;
}
RTStrFree(pszDestPath);
if (RT_SUCCESS(vrc))
{
*ppszTranslated = RTStrDup(szTranslated);
#if 0
RTPrintf("Root: %s, Source: %s, Dest: %s, Translated: %s\n",
pszSourceRoot, pszSource, pszDest, *ppszTranslated);
#endif
}
return vrc;
}
#ifdef DEBUG_andy
static int tstTranslatePath()
{
RTAssertSetMayPanic(false /* Do not freak out, please. */);
static struct
{
const char *pszSourceRoot;
const char *pszSource;
const char *pszDest;
const char *pszTranslated;
int iResult;
} aTests[] =
{
/* Invalid stuff. */
{ NULL, NULL, NULL, NULL, VERR_INVALID_POINTER },
#ifdef RT_OS_WINDOWS
/* Windows paths. */
{ "c:\\foo", "c:\\foo\\bar.txt", "c:\\test", "c:\\test\\bar.txt", VINF_SUCCESS },
{ "c:\\foo", "c:\\foo\\baz\\bar.txt", "c:\\test", "c:\\test\\baz\\bar.txt", VINF_SUCCESS },
#else /* RT_OS_WINDOWS */
{ "/home/test/foo", "/home/test/foo/bar.txt", "/opt/test", "/opt/test/bar.txt", VINF_SUCCESS },
{ "/home/test/foo", "/home/test/foo/baz/bar.txt", "/opt/test", "/opt/test/baz/bar.txt", VINF_SUCCESS },
#endif /* !RT_OS_WINDOWS */
/* Mixed paths*/
/** @todo */
{ NULL }
};
size_t iTest = 0;
for (iTest; iTest < RT_ELEMENTS(aTests); iTest++)
{
RTPrintf("=> Test %d\n", iTest);
RTPrintf("\tSourceRoot=%s, Source=%s, Dest=%s\n",
aTests[iTest].pszSourceRoot, aTests[iTest].pszSource, aTests[iTest].pszDest);
char *pszTranslated = NULL;
int iResult = ctrlCopyTranslatePath(aTests[iTest].pszSourceRoot, aTests[iTest].pszSource,
aTests[iTest].pszDest, &pszTranslated);
if (iResult != aTests[iTest].iResult)
{
RTPrintf("\tReturned %Rrc, expected %Rrc\n",
iResult, aTests[iTest].iResult);
}
else if ( pszTranslated
&& strcmp(pszTranslated, aTests[iTest].pszTranslated))
{
RTPrintf("\tReturned translated path %s, expected %s\n",
pszTranslated, aTests[iTest].pszTranslated);
}
if (pszTranslated)
{
RTPrintf("\tTranslated=%s\n", pszTranslated);
RTStrFree(pszTranslated);
}
}
return VINF_SUCCESS; /* @todo */
}
#endif
/**
* Creates a directory on the destination, based on the current copy
* context.
*
* @return IPRT status code.
* @param pContext Pointer to current copy control context.
* @param pszDir Directory to create.
*/
static int ctrlCopyDirCreate(PCOPYCONTEXT pContext, const char *pszDir)
{
AssertPtrReturn(pContext, VERR_INVALID_POINTER);
AssertPtrReturn(pszDir, VERR_INVALID_POINTER);
bool fDirExists;
int vrc = ctrlCopyDirExists(pContext, pContext->fHostToGuest, pszDir, &fDirExists);
if ( RT_SUCCESS(vrc)
&& fDirExists)
{
if (pContext->fVerbose)
RTPrintf("Directory \"%s\" already exists\n", pszDir);
return VINF_SUCCESS;
}
/* If querying for a directory existence fails there's no point of even trying
* to create such a directory. */
if (RT_FAILURE(vrc))
return vrc;
if (pContext->fVerbose)
RTPrintf("Creating directory \"%s\" ...\n", pszDir);
if (pContext->fDryRun)
return VINF_SUCCESS;
if (pContext->fHostToGuest) /* We want to create directories on the guest. */
{
SafeArray<DirectoryCreateFlag_T> dirCreateFlags;
dirCreateFlags.push_back(DirectoryCreateFlag_Parents);
HRESULT rc = pContext->pGuestSession->DirectoryCreate(Bstr(pszDir).raw(),
0700, ComSafeArrayAsInParam(dirCreateFlags));
if (FAILED(rc))
vrc = ctrlPrintError(pContext->pGuestSession, COM_IIDOF(IGuestSession));
}
else /* ... or on the host. */
{
vrc = RTDirCreateFullPath(pszDir, 0700);
if (vrc == VERR_ALREADY_EXISTS)
vrc = VINF_SUCCESS;
}
return vrc;
}
/**
* Checks whether a specific host/guest directory exists.
*
* @return IPRT status code.
* @param pContext Pointer to current copy control context.
* @param bGuest true if directory needs to be checked on the guest
* or false if on the host.
* @param pszDir Actual directory to check.
* @param fExists Pointer which receives the result if the
* given directory exists or not.
*/
static int ctrlCopyDirExists(PCOPYCONTEXT pContext, bool bGuest,
const char *pszDir, bool *fExists)
{
AssertPtrReturn(pContext, false);
AssertPtrReturn(pszDir, false);
AssertPtrReturn(fExists, false);
int vrc = VINF_SUCCESS;
if (bGuest)
{
BOOL fDirExists = FALSE;
HRESULT rc = pContext->pGuestSession->DirectoryExists(Bstr(pszDir).raw(), &fDirExists);
if (FAILED(rc))
vrc = ctrlPrintError(pContext->pGuestSession, COM_IIDOF(IGuestSession));
else
*fExists = fDirExists ? true : false;
}
else
*fExists = RTDirExists(pszDir);
return vrc;
}
/**
* Checks whether a specific directory exists on the destination, based
* on the current copy context.
*
* @return IPRT status code.
* @param pContext Pointer to current copy control context.
* @param pszDir Actual directory to check.
* @param fExists Pointer which receives the result if the
* given directory exists or not.
*/
static int ctrlCopyDirExistsOnDest(PCOPYCONTEXT pContext, const char *pszDir,
bool *fExists)
{
return ctrlCopyDirExists(pContext, pContext->fHostToGuest,
pszDir, fExists);
}
/**
* Checks whether a specific directory exists on the source, based
* on the current copy context.
*
* @return IPRT status code.
* @param pContext Pointer to current copy control context.
* @param pszDir Actual directory to check.
* @param fExists Pointer which receives the result if the
* given directory exists or not.
*/
static int ctrlCopyDirExistsOnSource(PCOPYCONTEXT pContext, const char *pszDir,
bool *fExists)
{
return ctrlCopyDirExists(pContext, !pContext->fHostToGuest,
pszDir, fExists);
}
/**
* Checks whether a specific host/guest file exists.
*
* @return IPRT status code.
* @param pContext Pointer to current copy control context.
* @param bGuest true if file needs to be checked on the guest
* or false if on the host.
* @param pszFile Actual file to check.
* @param fExists Pointer which receives the result if the
* given file exists or not.
*/
static int ctrlCopyFileExists(PCOPYCONTEXT pContext, bool bOnGuest,
const char *pszFile, bool *fExists)
{
AssertPtrReturn(pContext, false);
AssertPtrReturn(pszFile, false);
AssertPtrReturn(fExists, false);
int vrc = VINF_SUCCESS;
if (bOnGuest)
{
BOOL fFileExists = FALSE;
HRESULT rc = pContext->pGuestSession->FileExists(Bstr(pszFile).raw(), &fFileExists);
if (FAILED(rc))
vrc = ctrlPrintError(pContext->pGuestSession, COM_IIDOF(IGuestSession));
else
*fExists = fFileExists ? true : false;
}
else
*fExists = RTFileExists(pszFile);
return vrc;
}
/**
* Checks whether a specific file exists on the destination, based on the
* current copy context.
*
* @return IPRT status code.
* @param pContext Pointer to current copy control context.
* @param pszFile Actual file to check.
* @param fExists Pointer which receives the result if the
* given file exists or not.
*/
static int ctrlCopyFileExistsOnDest(PCOPYCONTEXT pContext, const char *pszFile,
bool *fExists)
{
return ctrlCopyFileExists(pContext, pContext->fHostToGuest,
pszFile, fExists);
}
/**
* Checks whether a specific file exists on the source, based on the
* current copy context.
*
* @return IPRT status code.
* @param pContext Pointer to current copy control context.
* @param pszFile Actual file to check.
* @param fExists Pointer which receives the result if the
* given file exists or not.
*/
static int ctrlCopyFileExistsOnSource(PCOPYCONTEXT pContext, const char *pszFile,
bool *fExists)
{
return ctrlCopyFileExists(pContext, !pContext->fHostToGuest,
pszFile, fExists);
}
/**
* Copies a source file to the destination.
*
* @return IPRT status code.
* @param pContext Pointer to current copy control context.
* @param pszFileSource Source file to copy to the destination.
* @param pszFileDest Name of copied file on the destination.
* @param fFlags Copy flags. No supported at the moment and needs
* to be set to 0.
*/
static int ctrlCopyFileToDest(PCOPYCONTEXT pContext, const char *pszFileSource,
const char *pszFileDest, uint32_t fFlags)
{
AssertPtrReturn(pContext, VERR_INVALID_POINTER);
AssertPtrReturn(pszFileSource, VERR_INVALID_POINTER);
AssertPtrReturn(pszFileDest, VERR_INVALID_POINTER);
AssertReturn(!fFlags, VERR_INVALID_POINTER); /* No flags supported yet. */
if (pContext->fVerbose)
RTPrintf("Copying \"%s\" to \"%s\" ...\n",
pszFileSource, pszFileDest);
if (pContext->fDryRun)
return VINF_SUCCESS;
int vrc = VINF_SUCCESS;
ComPtr<IProgress> pProgress;
HRESULT rc;
if (pContext->fHostToGuest)
{
SafeArray<CopyFileFlag_T> copyFlags;
rc = pContext->pGuestSession->CopyTo(Bstr(pszFileSource).raw(), Bstr(pszFileDest).raw(),
ComSafeArrayAsInParam(copyFlags),
pProgress.asOutParam());
}
else
{
SafeArray<CopyFileFlag_T> copyFlags;
rc = pContext->pGuestSession->CopyFrom(Bstr(pszFileSource).raw(), Bstr(pszFileDest).raw(),
ComSafeArrayAsInParam(copyFlags),
pProgress.asOutParam());
}
if (FAILED(rc))
{
vrc = ctrlPrintError(pContext->pGuestSession, COM_IIDOF(IGuestSession));
}
else
{
if (pContext->fVerbose)
rc = showProgress(pProgress);
else
rc = pProgress->WaitForCompletion(-1 /* No timeout */);
if (SUCCEEDED(rc))
CHECK_PROGRESS_ERROR(pProgress, ("File copy failed"));
vrc = ctrlPrintProgressError(pProgress);
}
return vrc;
}
/**
* Copys a directory (tree) from host to the guest.
*
* @return IPRT status code.
* @param pContext Pointer to current copy control context.
* @param pszSource Source directory on the host to copy to the guest.
* @param pszFilter DOS-style wildcard filter (?, *). Optional.
* @param pszDest Destination directory on the guest.
* @param fFlags Copy flags, such as recursive copying.
* @param pszSubDir Current sub directory to handle. Needs to NULL and only
* is needed for recursion.
*/
static int ctrlCopyDirToGuest(PCOPYCONTEXT pContext,
const char *pszSource, const char *pszFilter,
const char *pszDest, uint32_t fFlags,
const char *pszSubDir /* For recursion. */)
{
AssertPtrReturn(pContext, VERR_INVALID_POINTER);
AssertPtrReturn(pszSource, VERR_INVALID_POINTER);
/* Filter is optional. */
AssertPtrReturn(pszDest, VERR_INVALID_POINTER);
/* Sub directory is optional. */
/*
* Construct current path.
*/
char szCurDir[RTPATH_MAX];
int vrc = RTStrCopy(szCurDir, sizeof(szCurDir), pszSource);
if (RT_SUCCESS(vrc) && pszSubDir)
vrc = RTPathAppend(szCurDir, sizeof(szCurDir), pszSubDir);
if (pContext->fVerbose)
RTPrintf("Processing host directory: %s\n", szCurDir);
/* Flag indicating whether the current directory was created on the
* target or not. */
bool fDirCreated = false;
/*
* Open directory without a filter - RTDirOpenFiltered unfortunately
* cannot handle sub directories so we have to do the filtering ourselves.
*/
PRTDIR pDir = NULL;
if (RT_SUCCESS(vrc))
{
vrc = RTDirOpen(&pDir, szCurDir);
if (RT_FAILURE(vrc))
pDir = NULL;
}
if (RT_SUCCESS(vrc))
{
/*
* Enumerate the directory tree.
*/
while (RT_SUCCESS(vrc))
{
RTDIRENTRY DirEntry;
vrc = RTDirRead(pDir, &DirEntry, NULL);
if (RT_FAILURE(vrc))
{
if (vrc == VERR_NO_MORE_FILES)
vrc = VINF_SUCCESS;
break;
}
switch (DirEntry.enmType)
{
case RTDIRENTRYTYPE_DIRECTORY:
{
/* Skip "." and ".." entries. */
if ( !strcmp(DirEntry.szName, ".")
|| !strcmp(DirEntry.szName, ".."))
break;
if (pContext->fVerbose)
RTPrintf("Directory: %s\n", DirEntry.szName);
if (fFlags & CopyFileFlag_Recursive)
{
char *pszNewSub = NULL;
if (pszSubDir)
pszNewSub = RTPathJoinA(pszSubDir, DirEntry.szName);
else
{
pszNewSub = RTStrDup(DirEntry.szName);
RTPathStripTrailingSlash(pszNewSub);
}
if (pszNewSub)
{
vrc = ctrlCopyDirToGuest(pContext,
pszSource, pszFilter,
pszDest, fFlags, pszNewSub);
RTStrFree(pszNewSub);
}
else
vrc = VERR_NO_MEMORY;
}
break;
}
case RTDIRENTRYTYPE_SYMLINK:
if ( (fFlags & CopyFileFlag_Recursive)
&& (fFlags & CopyFileFlag_FollowLinks))
{
/* Fall through to next case is intentional. */
}
else
break;
case RTDIRENTRYTYPE_FILE:
{
if ( pszFilter
&& !RTStrSimplePatternMatch(pszFilter, DirEntry.szName))
{
break; /* Filter does not match. */
}
if (pContext->fVerbose)
RTPrintf("File: %s\n", DirEntry.szName);
if (!fDirCreated)
{
char *pszDestDir;
vrc = ctrlCopyTranslatePath(pszSource, szCurDir,
pszDest, &pszDestDir);
if (RT_SUCCESS(vrc))
{
vrc = ctrlCopyDirCreate(pContext, pszDestDir);
RTStrFree(pszDestDir);
fDirCreated = true;
}
}
if (RT_SUCCESS(vrc))
{
char *pszFileSource = RTPathJoinA(szCurDir, DirEntry.szName);
if (pszFileSource)
{
char *pszFileDest;
vrc = ctrlCopyTranslatePath(pszSource, pszFileSource,
pszDest, &pszFileDest);
if (RT_SUCCESS(vrc))
{
vrc = ctrlCopyFileToDest(pContext, pszFileSource,
pszFileDest, 0 /* Flags */);
RTStrFree(pszFileDest);
}
RTStrFree(pszFileSource);
}
}
break;
}
default:
break;
}
if (RT_FAILURE(vrc))
break;
}
RTDirClose(pDir);
}
return vrc;
}
/**
* Copys a directory (tree) from guest to the host.
*
* @return IPRT status code.
* @param pContext Pointer to current copy control context.
* @param pszSource Source directory on the guest to copy to the host.
* @param pszFilter DOS-style wildcard filter (?, *). Optional.
* @param pszDest Destination directory on the host.
* @param fFlags Copy flags, such as recursive copying.
* @param pszSubDir Current sub directory to handle. Needs to NULL and only
* is needed for recursion.
*/
static int ctrlCopyDirToHost(PCOPYCONTEXT pContext,
const char *pszSource, const char *pszFilter,
const char *pszDest, uint32_t fFlags,
const char *pszSubDir /* For recursion. */)
{
AssertPtrReturn(pContext, VERR_INVALID_POINTER);
AssertPtrReturn(pszSource, VERR_INVALID_POINTER);
/* Filter is optional. */
AssertPtrReturn(pszDest, VERR_INVALID_POINTER);
/* Sub directory is optional. */
/*
* Construct current path.
*/
char szCurDir[RTPATH_MAX];
int vrc = RTStrCopy(szCurDir, sizeof(szCurDir), pszSource);
if (RT_SUCCESS(vrc) && pszSubDir)
vrc = RTPathAppend(szCurDir, sizeof(szCurDir), pszSubDir);
if (RT_FAILURE(vrc))
return vrc;
if (pContext->fVerbose)
RTPrintf("Processing guest directory: %s\n", szCurDir);
/* Flag indicating whether the current directory was created on the
* target or not. */
bool fDirCreated = false;
SafeArray<DirectoryOpenFlag_T> dirOpenFlags; /* No flags supported yet. */
ComPtr<IGuestDirectory> pDirectory;
HRESULT rc = pContext->pGuestSession->DirectoryOpen(Bstr(szCurDir).raw(), Bstr(pszFilter).raw(),
ComSafeArrayAsInParam(dirOpenFlags),
pDirectory.asOutParam());
if (FAILED(rc))
return ctrlPrintError(pContext->pGuestSession, COM_IIDOF(IGuestSession));
ComPtr<IFsObjInfo> dirEntry;
while (true)
{
rc = pDirectory->Read(dirEntry.asOutParam());
if (FAILED(rc))
break;
FsObjType_T enmType;
dirEntry->COMGETTER(Type)(&enmType);
Bstr strName;
dirEntry->COMGETTER(Name)(strName.asOutParam());
switch (enmType)
{
case FsObjType_Directory:
{
Assert(!strName.isEmpty());
/* Skip "." and ".." entries. */
if ( !strName.compare(Bstr("."))
|| !strName.compare(Bstr("..")))
break;
if (pContext->fVerbose)
{
Utf8Str strDir(strName);
RTPrintf("Directory: %s\n", strDir.c_str());
}
if (fFlags & CopyFileFlag_Recursive)
{
Utf8Str strDir(strName);
char *pszNewSub = NULL;
if (pszSubDir)
pszNewSub = RTPathJoinA(pszSubDir, strDir.c_str());
else
{
pszNewSub = RTStrDup(strDir.c_str());
RTPathStripTrailingSlash(pszNewSub);
}
if (pszNewSub)
{
vrc = ctrlCopyDirToHost(pContext,
pszSource, pszFilter,
pszDest, fFlags, pszNewSub);
RTStrFree(pszNewSub);
}
else
vrc = VERR_NO_MEMORY;
}
break;
}
case FsObjType_Symlink:
if ( (fFlags & CopyFileFlag_Recursive)
&& (fFlags & CopyFileFlag_FollowLinks))
{
/* Fall through to next case is intentional. */
}
else
break;
case FsObjType_File:
{
Assert(!strName.isEmpty());
Utf8Str strFile(strName);
if ( pszFilter
&& !RTStrSimplePatternMatch(pszFilter, strFile.c_str()))
{
break; /* Filter does not match. */
}
if (pContext->fVerbose)
RTPrintf("File: %s\n", strFile.c_str());
if (!fDirCreated)
{
char *pszDestDir;
vrc = ctrlCopyTranslatePath(pszSource, szCurDir,
pszDest, &pszDestDir);
if (RT_SUCCESS(vrc))
{
vrc = ctrlCopyDirCreate(pContext, pszDestDir);
RTStrFree(pszDestDir);
fDirCreated = true;
}
}
if (RT_SUCCESS(vrc))
{
char *pszFileSource = RTPathJoinA(szCurDir, strFile.c_str());
if (pszFileSource)
{
char *pszFileDest;
vrc = ctrlCopyTranslatePath(pszSource, pszFileSource,
pszDest, &pszFileDest);
if (RT_SUCCESS(vrc))
{
vrc = ctrlCopyFileToDest(pContext, pszFileSource,
pszFileDest, 0 /* Flags */);
RTStrFree(pszFileDest);
}
RTStrFree(pszFileSource);
}
else
vrc = VERR_NO_MEMORY;
}
break;
}
default:
RTPrintf("Warning: Directory entry of type %ld not handled, skipping ...\n",
enmType);
break;
}
if (RT_FAILURE(vrc))
break;
}
if (RT_UNLIKELY(FAILED(rc)))
{
switch (rc)
{
case E_ABORT: /* No more directory entries left to process. */
break;
case VBOX_E_FILE_ERROR: /* Current entry cannot be accessed to
to missing rights. */
{
RTPrintf("Warning: Cannot access \"%s\", skipping ...\n",
szCurDir);
break;
}
default:
vrc = ctrlPrintError(pDirectory, COM_IIDOF(IGuestDirectory));
break;
}
}
HRESULT rc2 = pDirectory->Close();
if (FAILED(rc2))
{
int vrc2 = ctrlPrintError(pDirectory, COM_IIDOF(IGuestDirectory));
if (RT_SUCCESS(vrc))
vrc = vrc2;
}
else if (SUCCEEDED(rc))
rc = rc2;
return vrc;
}
/**
* Copys a directory (tree) to the destination, based on the current copy
* context.
*
* @return IPRT status code.
* @param pContext Pointer to current copy control context.
* @param pszSource Source directory to copy to the destination.
* @param pszFilter DOS-style wildcard filter (?, *). Optional.
* @param pszDest Destination directory where to copy in the source
* source directory.
* @param fFlags Copy flags, such as recursive copying.
*/
static int ctrlCopyDirToDest(PCOPYCONTEXT pContext,
const char *pszSource, const char *pszFilter,
const char *pszDest, uint32_t fFlags)
{
if (pContext->fHostToGuest)
return ctrlCopyDirToGuest(pContext, pszSource, pszFilter,
pszDest, fFlags, NULL /* Sub directory, only for recursion. */);
return ctrlCopyDirToHost(pContext, pszSource, pszFilter,
pszDest, fFlags, NULL /* Sub directory, only for recursion. */);
}
/**
* Creates a source root by stripping file names or filters of the specified source.
*
* @return IPRT status code.
* @param pszSource Source to create source root for.
* @param ppszSourceRoot Pointer that receives the allocated source root. Needs
* to be free'd with ctrlCopyFreeSourceRoot().
*/
static int ctrlCopyCreateSourceRoot(const char *pszSource, char **ppszSourceRoot)
{
AssertPtrReturn(pszSource, VERR_INVALID_POINTER);
AssertPtrReturn(ppszSourceRoot, VERR_INVALID_POINTER);
char *pszNewRoot = RTStrDup(pszSource);
AssertPtrReturn(pszNewRoot, VERR_NO_MEMORY);
size_t lenRoot = strlen(pszNewRoot);
if ( lenRoot
&& pszNewRoot[lenRoot - 1] == '/'
&& pszNewRoot[lenRoot - 1] == '\\'
&& lenRoot > 1
&& pszNewRoot[lenRoot - 2] == '/'
&& pszNewRoot[lenRoot - 2] == '\\')
{
*ppszSourceRoot = pszNewRoot;
if (lenRoot > 1)
*ppszSourceRoot[lenRoot - 2] = '\0';
*ppszSourceRoot[lenRoot - 1] = '\0';
}
else
{
/* If there's anything (like a file name or a filter),
* strip it! */
RTPathStripFilename(pszNewRoot);
*ppszSourceRoot = pszNewRoot;
}
return VINF_SUCCESS;
}
/**
* Frees a previously allocated source root.
*
* @return IPRT status code.
* @param pszSourceRoot Source root to free.
*/
static void ctrlCopyFreeSourceRoot(char *pszSourceRoot)
{
RTStrFree(pszSourceRoot);
}
static int handleCtrlCopy(ComPtr<IGuest> guest, HandlerArg *pArg,
bool fHostToGuest)
{
AssertPtrReturn(pArg, VERR_INVALID_PARAMETER);
/** @todo r=bird: This command isn't very unix friendly in general. mkdir
* is much better (partly because it is much simpler of course). The main
* arguments against this is that (1) all but two options conflicts with
* what 'man cp' tells me on a GNU/Linux system, (2) wildchar matching is
* done windows CMD style (though not in a 100% compatible way), and (3)
* that only one source is allowed - efficiently sabotaging default
* wildcard expansion by a unix shell. The best solution here would be
* two different variant, one windowsy (xcopy) and one unixy (gnu cp). */
/*
* IGuest::CopyToGuest is kept as simple as possible to let the developer choose
* what and how to implement the file enumeration/recursive lookup, like VBoxManage
* does in here.
*/
static const RTGETOPTDEF s_aOptions[] =
{
{ "--dryrun", GETOPTDEF_COPY_DRYRUN, RTGETOPT_REQ_NOTHING },
{ "--follow", GETOPTDEF_COPY_FOLLOW, RTGETOPT_REQ_NOTHING },
{ "--username", 'u', RTGETOPT_REQ_STRING },
{ "--passwordfile", 'p', RTGETOPT_REQ_STRING },
{ "--password", GETOPTDEF_COPY_PASSWORD, RTGETOPT_REQ_STRING },
{ "--domain", 'd', RTGETOPT_REQ_STRING },
{ "--recursive", 'R', RTGETOPT_REQ_NOTHING },
{ "--target-directory", GETOPTDEF_COPY_TARGETDIR, RTGETOPT_REQ_STRING },
{ "--verbose", 'v', RTGETOPT_REQ_NOTHING }
};
int ch;
RTGETOPTUNION ValueUnion;
RTGETOPTSTATE GetState;
RTGetOptInit(&GetState, pArg->argc, pArg->argv,
s_aOptions, RT_ELEMENTS(s_aOptions), 0, RTGETOPTINIT_FLAGS_OPTS_FIRST);
Utf8Str strSource;
Utf8Str strDest;
Utf8Str strUsername;
Utf8Str strPassword;
Utf8Str strDomain;
uint32_t fFlags = CopyFileFlag_None;
bool fVerbose = false;
bool fCopyRecursive = false;
bool fDryRun = false;
SOURCEVEC vecSources;
int vrc = VINF_SUCCESS;
while ((ch = RTGetOpt(&GetState, &ValueUnion)))
{
/* For options that require an argument, ValueUnion has received the value. */
switch (ch)
{
case GETOPTDEF_COPY_DRYRUN:
fDryRun = true;
break;
case GETOPTDEF_COPY_FOLLOW:
fFlags |= CopyFileFlag_FollowLinks;
break;
case 'u': /* User name */
strUsername = ValueUnion.psz;
break;
case GETOPTDEF_COPY_PASSWORD: /* Password */
strPassword = ValueUnion.psz;
break;
case 'p': /* Password file */
{
RTEXITCODE rcExit = readPasswordFile(ValueUnion.psz, &strPassword);
if (rcExit != RTEXITCODE_SUCCESS)
return rcExit;
break;
}
case 'd': /* domain */
strDomain = ValueUnion.psz;
break;
case 'R': /* Recursive processing */
fFlags |= CopyFileFlag_Recursive;
break;
case GETOPTDEF_COPY_TARGETDIR:
strDest = ValueUnion.psz;
break;
case 'v': /* Verbose */
fVerbose = true;
break;
case VINF_GETOPT_NOT_OPTION:
{
/* Last argument and no destination specified with
* --target-directory yet? Then use the current
* (= last) argument as destination. */
if ( pArg->argc == GetState.iNext
&& strDest.isEmpty())
{
strDest = ValueUnion.psz;
}
else
{
/* Save the source directory. */
vecSources.push_back(SOURCEFILEENTRY(ValueUnion.psz));
}
break;
}
default:
return RTGetOptPrintError(ch, &ValueUnion);
}
}
if (!vecSources.size())
return errorSyntax(USAGE_GUESTCONTROL,
"No source(s) specified!");
if (strDest.isEmpty())
return errorSyntax(USAGE_GUESTCONTROL,
"No destination specified!");
if (strUsername.isEmpty())
return errorSyntax(USAGE_GUESTCONTROL,
"No user name specified!");
/*
* Done parsing arguments, do some more preparations.
*/
if (fVerbose)
{
if (fHostToGuest)
RTPrintf("Copying from host to guest ...\n");
else
RTPrintf("Copying from guest to host ...\n");
if (fDryRun)
RTPrintf("Dry run - no files copied!\n");
}
/* Create the copy context -- it contains all information
* the routines need to know when handling the actual copying. */
PCOPYCONTEXT pContext = NULL;
vrc = ctrlCopyContextCreate(guest, fVerbose, fDryRun, fHostToGuest,
strUsername, strPassword, strDomain,
"VBoxManage Guest Control Copy", &pContext);
if (RT_FAILURE(vrc))
{
RTMsgError("Unable to create copy context, rc=%Rrc\n", vrc);
return RTEXITCODE_FAILURE;
}
/* If the destination is a path, (try to) create it. */
const char *pszDest = strDest.c_str();
/** @todo r=bird: RTPathFilename and RTPathStripFilename won't work
* correctly on non-windows hosts when the guest is from the DOS world (Windows,
* OS/2, DOS). The host doesn't know about DOS slashes, only UNIX slashes and
* will get the wrong idea if some dilligent user does:
*
* copyto myfile.txt 'C:\guestfile.txt'
* or
* copyto myfile.txt 'D:guestfile.txt'
*
* @bugref{6344}
*/
if (!RTPathFilename(pszDest))
{
vrc = ctrlCopyDirCreate(pContext, pszDest);
}
else
{
/* We assume we got a file name as destination -- so strip
* the actual file name and make sure the appropriate
* directories get created. */
char *pszDestDir = RTStrDup(pszDest);
AssertPtr(pszDestDir);
RTPathStripFilename(pszDestDir);
vrc = ctrlCopyDirCreate(pContext, pszDestDir);
RTStrFree(pszDestDir);
}
if (RT_SUCCESS(vrc))
{
/*
* Here starts the actual fun!
* Handle all given sources one by one.
*/
for (unsigned long s = 0; s < vecSources.size(); s++)
{
char *pszSource = RTStrDup(vecSources[s].GetSource());
AssertPtrBreakStmt(pszSource, vrc = VERR_NO_MEMORY);
const char *pszFilter = vecSources[s].GetFilter();
if (!strlen(pszFilter))
pszFilter = NULL; /* If empty filter then there's no filter :-) */
char *pszSourceRoot;
vrc = ctrlCopyCreateSourceRoot(pszSource, &pszSourceRoot);
if (RT_FAILURE(vrc))
{
RTMsgError("Unable to create source root, rc=%Rrc\n", vrc);
break;
}
if (fVerbose)
RTPrintf("Source: %s\n", pszSource);
/** @todo Files with filter?? */
bool fSourceIsFile = false;
bool fSourceExists;
size_t cchSource = strlen(pszSource);
if ( cchSource > 1
&& RTPATH_IS_SLASH(pszSource[cchSource - 1]))
{
if (pszFilter) /* Directory with filter (so use source root w/o the actual filter). */
vrc = ctrlCopyDirExistsOnSource(pContext, pszSourceRoot, &fSourceExists);
else /* Regular directory without filter. */
vrc = ctrlCopyDirExistsOnSource(pContext, pszSource, &fSourceExists);
if (fSourceExists)
{
/* Strip trailing slash from our source element so that other functions
* can use this stuff properly (like RTPathStartsWith). */
RTPathStripTrailingSlash(pszSource);
}
}
else
{
vrc = ctrlCopyFileExistsOnSource(pContext, pszSource, &fSourceExists);
if ( RT_SUCCESS(vrc)
&& fSourceExists)
{
fSourceIsFile = true;
}
}
if ( RT_SUCCESS(vrc)
&& fSourceExists)
{
if (fSourceIsFile)
{
/* Single file. */
char *pszDestFile;
vrc = ctrlCopyTranslatePath(pszSourceRoot, pszSource,
strDest.c_str(), &pszDestFile);
if (RT_SUCCESS(vrc))
{
vrc = ctrlCopyFileToDest(pContext, pszSource,
pszDestFile, 0 /* Flags */);
RTStrFree(pszDestFile);
}
else
RTMsgError("Unable to translate path for \"%s\", rc=%Rrc\n",
pszSource, vrc);
}
else
{
/* Directory (with filter?). */
vrc = ctrlCopyDirToDest(pContext, pszSource, pszFilter,
strDest.c_str(), fFlags);
}
}
ctrlCopyFreeSourceRoot(pszSourceRoot);
if ( RT_SUCCESS(vrc)
&& !fSourceExists)
{
RTMsgError("Warning: Source \"%s\" does not exist, skipping!\n",
pszSource);
RTStrFree(pszSource);
continue;
}
else if (RT_FAILURE(vrc))
{
RTMsgError("Error processing \"%s\", rc=%Rrc\n",
pszSource, vrc);
RTStrFree(pszSource);
break;
}
RTStrFree(pszSource);
}
}
ctrlCopyContextFree(pContext);
return RT_SUCCESS(vrc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
}
static int handleCtrlCreateDirectory(ComPtr<IGuest> pGuest, HandlerArg *pArg)
{
AssertPtrReturn(pArg, VERR_INVALID_PARAMETER);
/*
* Parse arguments.
*
* Note! No direct returns here, everyone must go thru the cleanup at the
* end of this function.
*/
static const RTGETOPTDEF s_aOptions[] =
{
{ "--mode", 'm', RTGETOPT_REQ_UINT32 },
{ "--parents", 'P', RTGETOPT_REQ_NOTHING },
{ "--username", 'u', RTGETOPT_REQ_STRING },
{ "--passwordfile", 'p', RTGETOPT_REQ_STRING },
{ "--password", GETOPTDEF_MKDIR_PASSWORD, RTGETOPT_REQ_STRING },
{ "--domain", 'd', RTGETOPT_REQ_STRING },
{ "--verbose", 'v', RTGETOPT_REQ_NOTHING }
};
int ch;
RTGETOPTUNION ValueUnion;
RTGETOPTSTATE GetState;
RTGetOptInit(&GetState, pArg->argc, pArg->argv,
s_aOptions, RT_ELEMENTS(s_aOptions), 0, RTGETOPTINIT_FLAGS_OPTS_FIRST);
Utf8Str strUsername;
Utf8Str strPassword;
Utf8Str strDomain;
SafeArray<DirectoryCreateFlag_T> dirCreateFlags;
uint32_t fDirMode = 0; /* Default mode. */
bool fVerbose = false;
DESTDIRMAP mapDirs;
while ((ch = RTGetOpt(&GetState, &ValueUnion)))
{
/* For options that require an argument, ValueUnion has received the value. */
switch (ch)
{
case 'm': /* Mode */
fDirMode = ValueUnion.u32;
break;
case 'P': /* Create parents */
dirCreateFlags.push_back(DirectoryCreateFlag_Parents);
break;
case 'u': /* User name */
strUsername = ValueUnion.psz;
break;
case GETOPTDEF_MKDIR_PASSWORD: /* Password */
strPassword = ValueUnion.psz;
break;
case 'p': /* Password file */
{
RTEXITCODE rcExit = readPasswordFile(ValueUnion.psz, &strPassword);
if (rcExit != RTEXITCODE_SUCCESS)
return rcExit;
break;
}
case 'd': /* domain */
strDomain = ValueUnion.psz;
break;
case 'v': /* Verbose */
fVerbose = true;
break;
case VINF_GETOPT_NOT_OPTION:
{
mapDirs[ValueUnion.psz]; /* Add destination directory to map. */
break;
}
default:
return RTGetOptPrintError(ch, &ValueUnion);
}
}
uint32_t cDirs = mapDirs.size();
if (!cDirs)
return errorSyntax(USAGE_GUESTCONTROL, "No directory to create specified!");
if (strUsername.isEmpty())
return errorSyntax(USAGE_GUESTCONTROL, "No user name specified!");
/*
* Create the directories.
*/
HRESULT hrc = S_OK;
if (fVerbose && cDirs)
RTPrintf("Creating %u directories ...\n", cDirs);
ComPtr<IGuestSession> pGuestSession;
hrc = pGuest->CreateSession(Bstr(strUsername).raw(),
Bstr(strPassword).raw(),
Bstr(strDomain).raw(),
Bstr("VBoxManage Guest Control MkDir").raw(),
pGuestSession.asOutParam());
if (FAILED(hrc))
return ctrlPrintError(pGuest, COM_IIDOF(IGuest));
DESTDIRMAPITER it = mapDirs.begin();
while (it != mapDirs.end())
{
if (fVerbose)
RTPrintf("Creating directory \"%s\" ...\n", it->first.c_str());
hrc = pGuestSession->DirectoryCreate(Bstr(it->first).raw(), fDirMode, ComSafeArrayAsInParam(dirCreateFlags));
if (FAILED(hrc))
{
ctrlPrintError(pGuest, COM_IIDOF(IGuestSession)); /* Return code ignored, save original rc. */
break;
}
it++;
}
if (!pGuestSession.isNull())
pGuestSession->Close();
return FAILED(hrc) ? RTEXITCODE_FAILURE : RTEXITCODE_SUCCESS;
}
static int handleCtrlCreateTemp(ComPtr<IGuest> pGuest, HandlerArg *pArg)
{
AssertPtrReturn(pArg, VERR_INVALID_PARAMETER);
/*
* Parse arguments.
*
* Note! No direct returns here, everyone must go thru the cleanup at the
* end of this function.
*/
static const RTGETOPTDEF s_aOptions[] =
{
{ "--mode", 'm', RTGETOPT_REQ_UINT32 },
{ "--directory", 'D', RTGETOPT_REQ_NOTHING },
{ "--secure", 's', RTGETOPT_REQ_NOTHING },
{ "--tmpdir", 't', RTGETOPT_REQ_STRING },
{ "--username", 'u', RTGETOPT_REQ_STRING },
{ "--passwordfile", 'p', RTGETOPT_REQ_STRING },
{ "--password", GETOPTDEF_MKDIR_PASSWORD, RTGETOPT_REQ_STRING },
{ "--domain", 'd', RTGETOPT_REQ_STRING },
{ "--verbose", 'v', RTGETOPT_REQ_NOTHING }
};
int ch;
RTGETOPTUNION ValueUnion;
RTGETOPTSTATE GetState;
RTGetOptInit(&GetState, pArg->argc, pArg->argv,
s_aOptions, RT_ELEMENTS(s_aOptions), 0, RTGETOPTINIT_FLAGS_OPTS_FIRST);
Utf8Str strUsername;
Utf8Str strPassword;
Utf8Str strDomain;
Utf8Str strTemplate;
uint32_t fMode = 0; /* Default mode. */
bool fDirectory = false;
bool fSecure = false;
Utf8Str strTempDir;
bool fVerbose = false;
DESTDIRMAP mapDirs;
while ((ch = RTGetOpt(&GetState, &ValueUnion)))
{
/* For options that require an argument, ValueUnion has received the value. */
switch (ch)
{
case 'm': /* Mode */
fMode = ValueUnion.u32;
break;
case 'D': /* Create directory */
fDirectory = true;
break;
case 's': /* Secure */
fSecure = true;
break;
case 't': /* Temp directory */
strTempDir = ValueUnion.psz;
break;
case 'u': /* User name */
strUsername = ValueUnion.psz;
break;
case GETOPTDEF_MKDIR_PASSWORD: /* Password */
strPassword = ValueUnion.psz;
break;
case 'p': /* Password file */
{
RTEXITCODE rcExit = readPasswordFile(ValueUnion.psz, &strPassword);
if (rcExit != RTEXITCODE_SUCCESS)
return rcExit;
break;
}
case 'd': /* domain */
strDomain = ValueUnion.psz;
break;
case 'v': /* Verbose */
fVerbose = true;
break;
case VINF_GETOPT_NOT_OPTION:
{
if (strTemplate.isEmpty())
strTemplate = ValueUnion.psz;
else
return errorSyntax(USAGE_GUESTCONTROL,
"More than one template specified!\n");
break;
}
default:
return RTGetOptPrintError(ch, &ValueUnion);
}
}
if (strTemplate.isEmpty())
return errorSyntax(USAGE_GUESTCONTROL, "No template specified!");
if (strUsername.isEmpty())
return errorSyntax(USAGE_GUESTCONTROL, "No user name specified!");
if (!fDirectory)
return errorSyntax(USAGE_GUESTCONTROL, "Creating temporary files is currently not supported!");
/*
* Create the directories.
*/
HRESULT hrc = S_OK;
if (fVerbose)
{
if (fDirectory && !strTempDir.isEmpty())
RTPrintf("Creating temporary directory from template '%s' in directory '%s' ...\n",
strTemplate.c_str(), strTempDir.c_str());
else if (fDirectory)
RTPrintf("Creating temporary directory from template '%s' in default temporary directory ...\n",
strTemplate.c_str());
else if (!fDirectory && !strTempDir.isEmpty())
RTPrintf("Creating temporary file from template '%s' in directory '%s' ...\n",
strTemplate.c_str(), strTempDir.c_str());
else if (!fDirectory)
RTPrintf("Creating temporary file from template '%s' in default temporary directory ...\n",
strTemplate.c_str());
}
ComPtr<IGuestSession> pGuestSession;
hrc = pGuest->CreateSession(Bstr(strUsername).raw(),
Bstr(strPassword).raw(),
Bstr(strDomain).raw(),
Bstr("VBoxManage Guest Control MkTemp").raw(),
pGuestSession.asOutParam());
if (FAILED(hrc))
return ctrlPrintError(pGuest, COM_IIDOF(IGuest));
if (fDirectory)
{
Bstr directory;
hrc = pGuestSession->DirectoryCreateTemp(Bstr(strTemplate).raw(),
fMode, Bstr(strTempDir).raw(),
fSecure,
directory.asOutParam());
if (SUCCEEDED(hrc))
RTPrintf("Directory name: %ls\n", directory.raw());
}
// else - temporary file not yet implemented
if (FAILED(hrc))
ctrlPrintError(pGuest, COM_IIDOF(IGuestSession)); /* Return code ignored, save original rc. */
if (!pGuestSession.isNull())
pGuestSession->Close();
return FAILED(hrc) ? RTEXITCODE_FAILURE : RTEXITCODE_SUCCESS;
}
static int handleCtrlStat(ComPtr<IGuest> pGuest, HandlerArg *pArg)
{
AssertPtrReturn(pArg, VERR_INVALID_PARAMETER);
static const RTGETOPTDEF s_aOptions[] =
{
{ "--dereference", 'L', RTGETOPT_REQ_NOTHING },
{ "--file-system", 'f', RTGETOPT_REQ_NOTHING },
{ "--format", 'c', RTGETOPT_REQ_STRING },
{ "--username", 'u', RTGETOPT_REQ_STRING },
{ "--passwordfile", 'p', RTGETOPT_REQ_STRING },
{ "--password", GETOPTDEF_STAT_PASSWORD, RTGETOPT_REQ_STRING },
{ "--domain", 'd', RTGETOPT_REQ_STRING },
{ "--terse", 't', RTGETOPT_REQ_NOTHING },
{ "--verbose", 'v', RTGETOPT_REQ_NOTHING }
};
int ch;
RTGETOPTUNION ValueUnion;
RTGETOPTSTATE GetState;
RTGetOptInit(&GetState, pArg->argc, pArg->argv,
s_aOptions, RT_ELEMENTS(s_aOptions), 0, RTGETOPTINIT_FLAGS_OPTS_FIRST);
Utf8Str strUsername;
Utf8Str strPassword;
Utf8Str strDomain;
bool fVerbose = false;
DESTDIRMAP mapObjs;
while ((ch = RTGetOpt(&GetState, &ValueUnion)))
{
/* For options that require an argument, ValueUnion has received the value. */
switch (ch)
{
case 'u': /* User name */
strUsername = ValueUnion.psz;
break;
case GETOPTDEF_STAT_PASSWORD: /* Password */
strPassword = ValueUnion.psz;
break;
case 'p': /* Password file */
{
RTEXITCODE rcExit = readPasswordFile(ValueUnion.psz, &strPassword);
if (rcExit != RTEXITCODE_SUCCESS)
return rcExit;
break;
}
case 'd': /* domain */
strDomain = ValueUnion.psz;
break;
case 'L': /* Dereference */
case 'f': /* File-system */
case 'c': /* Format */
case 't': /* Terse */
return errorSyntax(USAGE_GUESTCONTROL, "Command \"%s\" not implemented yet!",
ValueUnion.psz);
break; /* Never reached. */
case 'v': /* Verbose */
fVerbose = true;
break;
case VINF_GETOPT_NOT_OPTION:
{
mapObjs[ValueUnion.psz]; /* Add element to check to map. */
break;
}
default:
return RTGetOptPrintError(ch, &ValueUnion);
}
}
uint32_t cObjs = mapObjs.size();
if (!cObjs)
return errorSyntax(USAGE_GUESTCONTROL, "No element(s) to check specified!");
if (strUsername.isEmpty())
return errorSyntax(USAGE_GUESTCONTROL, "No user name specified!");
ComPtr<IGuestSession> pGuestSession;
HRESULT hrc = pGuest->CreateSession(Bstr(strUsername).raw(),
Bstr(strPassword).raw(),
Bstr(strDomain).raw(),
Bstr("VBoxManage Guest Control Stat").raw(),
pGuestSession.asOutParam());
if (FAILED(hrc))
return ctrlPrintError(pGuest, COM_IIDOF(IGuest));
/*
* Create the directories.
*/
RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
DESTDIRMAPITER it = mapObjs.begin();
while (it != mapObjs.end())
{
if (fVerbose)
RTPrintf("Checking for element \"%s\" ...\n", it->first.c_str());
ComPtr<IGuestFsObjInfo> pFsObjInfo;
hrc = pGuestSession->FileQueryInfo(Bstr(it->first).raw(), pFsObjInfo.asOutParam());
if (FAILED(hrc))
hrc = pGuestSession->DirectoryQueryInfo(Bstr(it->first).raw(), pFsObjInfo.asOutParam());
if (FAILED(hrc))
{
/* If there's at least one element which does not exist on the guest,
* drop out with exitcode 1. */
if (fVerbose)
RTPrintf("Cannot stat for element \"%s\": No such element\n",
it->first.c_str());
rcExit = RTEXITCODE_FAILURE;
}
else
{
FsObjType_T objType;
hrc = pFsObjInfo->COMGETTER(Type)(&objType);
if (FAILED(hrc))
return ctrlPrintError(pGuest, COM_IIDOF(IGuestFsObjInfo));
switch (objType)
{
case FsObjType_File:
RTPrintf("Element \"%s\" found: Is a file\n", it->first.c_str());
break;
case FsObjType_Directory:
RTPrintf("Element \"%s\" found: Is a directory\n", it->first.c_str());
break;
case FsObjType_Symlink:
RTPrintf("Element \"%s\" found: Is a symlink\n", it->first.c_str());
break;
default:
RTPrintf("Element \"%s\" found, type unknown (%ld)\n", it->first.c_str(), objType);
break;
}
/** @todo: Show more information about this element. */
}
it++;
}
if (!pGuestSession.isNull())
pGuestSession->Close();
return rcExit;
}
static int handleCtrlUpdateAdditions(ComPtr<IGuest> guest, HandlerArg *pArg)
{
AssertPtrReturn(pArg, VERR_INVALID_PARAMETER);
/*
* Check the syntax. We can deduce the correct syntax from the number of
* arguments.
*/
Utf8Str strSource;
bool fVerbose = false;
bool fWaitStartOnly = false;
static const RTGETOPTDEF s_aOptions[] =
{
{ "--source", 's', RTGETOPT_REQ_STRING },
{ "--verbose", 'v', RTGETOPT_REQ_NOTHING },
{ "--wait-start", 'w', RTGETOPT_REQ_NOTHING }
};
int ch;
RTGETOPTUNION ValueUnion;
RTGETOPTSTATE GetState;
RTGetOptInit(&GetState, pArg->argc, pArg->argv, s_aOptions, RT_ELEMENTS(s_aOptions), 0, 0);
int vrc = VINF_SUCCESS;
while ( (ch = RTGetOpt(&GetState, &ValueUnion))
&& RT_SUCCESS(vrc))
{
switch (ch)
{
case 's':
strSource = ValueUnion.psz;
break;
case 'v':
fVerbose = true;
break;
case 'w':
fWaitStartOnly = true;
break;
default:
return RTGetOptPrintError(ch, &ValueUnion);
}
}
if (fVerbose)
RTPrintf("Updating Guest Additions ...\n");
HRESULT rc = S_OK;
while (strSource.isEmpty())
{
ComPtr<ISystemProperties> pProperties;
CHECK_ERROR_BREAK(pArg->virtualBox, COMGETTER(SystemProperties)(pProperties.asOutParam()));
Bstr strISO;
CHECK_ERROR_BREAK(pProperties, COMGETTER(DefaultAdditionsISO)(strISO.asOutParam()));
strSource = strISO;
break;
}
/* Determine source if not set yet. */
if (strSource.isEmpty())
{
RTMsgError("No Guest Additions source found or specified, aborting\n");
vrc = VERR_FILE_NOT_FOUND;
}
else if (!RTFileExists(strSource.c_str()))
{
RTMsgError("Source \"%s\" does not exist!\n", strSource.c_str());
vrc = VERR_FILE_NOT_FOUND;
}
if (RT_SUCCESS(vrc))
{
if (fVerbose)
RTPrintf("Using source: %s\n", strSource.c_str());
com::SafeArray<AdditionsUpdateFlag_T> aUpdateFlags;
if (fWaitStartOnly)
{
aUpdateFlags.push_back(AdditionsUpdateFlag_WaitForUpdateStartOnly);
if (fVerbose)
RTPrintf("Preparing and waiting for Guest Additions installer to start ...\n");
}
ComPtr<IProgress> pProgress;
CHECK_ERROR(guest, UpdateGuestAdditions(Bstr(strSource).raw(),
/* Wait for whole update process to complete. */
ComSafeArrayAsInParam(aUpdateFlags),
pProgress.asOutParam()));
if (FAILED(rc))
vrc = ctrlPrintError(guest, COM_IIDOF(IGuest));
else
{
if (fVerbose)
rc = showProgress(pProgress);
else
rc = pProgress->WaitForCompletion(-1 /* No timeout */);
if (SUCCEEDED(rc))
CHECK_PROGRESS_ERROR(pProgress, ("Guest additions update failed"));
vrc = ctrlPrintProgressError(pProgress);
if ( RT_SUCCESS(vrc)
&& fVerbose)
{
RTPrintf("Guest Additions update successful\n");
}
}
}
return RT_SUCCESS(vrc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
}
/**
* Access the guest control store.
*
* @returns program exit code.
* @note see the command line API description for parameters
*/
int handleGuestControl(HandlerArg *pArg)
{
AssertPtrReturn(pArg, VERR_INVALID_PARAMETER);
#ifdef DEBUG_andy_disabled
if (RT_FAILURE(tstTranslatePath()))
return RTEXITCODE_FAILURE;
#endif
HandlerArg arg = *pArg;
arg.argc = pArg->argc - 2; /* Skip VM name and sub command. */
arg.argv = pArg->argv + 2; /* Same here. */
ComPtr<IGuest> guest;
int vrc = ctrlInitVM(pArg, pArg->argv[0] /* VM Name */, &guest);
if (RT_SUCCESS(vrc))
{
int rcExit;
if (pArg->argc < 2)
rcExit = errorSyntax(USAGE_GUESTCONTROL, "No sub command specified!");
else if ( !strcmp(pArg->argv[1], "exec")
|| !strcmp(pArg->argv[1], "execute"))
rcExit = handleCtrlExecProgram(guest, &arg);
else if (!strcmp(pArg->argv[1], "copyfrom"))
rcExit = handleCtrlCopy(guest, &arg, false /* Guest to host */);
else if ( !strcmp(pArg->argv[1], "copyto")
|| !strcmp(pArg->argv[1], "cp"))
rcExit = handleCtrlCopy(guest, &arg, true /* Host to guest */);
else if ( !strcmp(pArg->argv[1], "createdirectory")
|| !strcmp(pArg->argv[1], "createdir")
|| !strcmp(pArg->argv[1], "mkdir")
|| !strcmp(pArg->argv[1], "md"))
rcExit = handleCtrlCreateDirectory(guest, &arg);
else if ( !strcmp(pArg->argv[1], "createtemporary")
|| !strcmp(pArg->argv[1], "createtemp")
|| !strcmp(pArg->argv[1], "mktemp"))
rcExit = handleCtrlCreateTemp(guest, &arg);
else if ( !strcmp(pArg->argv[1], "stat"))
rcExit = handleCtrlStat(guest, &arg);
else if ( !strcmp(pArg->argv[1], "updateadditions")
|| !strcmp(pArg->argv[1], "updateadds"))
rcExit = handleCtrlUpdateAdditions(guest, &arg);
else
rcExit = errorSyntax(USAGE_GUESTCONTROL, "Unknown sub command '%s' specified!", pArg->argv[1]);
ctrlUninitVM(pArg);
return rcExit;
}
return RTEXITCODE_FAILURE;
}
#endif /* !VBOX_ONLY_DOCS */