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