VirtualBox

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

Last change on this file since 27987 was 27976, checked in by vboxsync, 15 years ago

*: scm cleans up whitespace and adds a new line at the end of ApplianceimplPrivate.h.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.7 KB
Line 
1/* $Id: VBoxManageGuestCtrl.cpp 27976 2010-04-04 14:16:32Z vboxsync $ */
2/** @file
3 * VBoxManage - The 'guestcontrol' command.
4 */
5
6/*
7 * Copyright (C) 2010 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22
23/*******************************************************************************
24* Header Files *
25*******************************************************************************/
26#include "VBoxManage.h"
27
28#include <VBox/com/com.h>
29#include <VBox/com/string.h>
30#include <VBox/com/array.h>
31#include <VBox/com/ErrorInfo.h>
32#include <VBox/com/errorprint.h>
33
34#include <VBox/com/VirtualBox.h>
35#include <VBox/com/EventQueue.h>
36
37#include <VBox/log.h>
38#include <iprt/asm.h>
39#include <iprt/getopt.h>
40#include <iprt/stream.h>
41#include <iprt/string.h>
42#include <iprt/time.h>
43#include <iprt/thread.h>
44
45#ifdef USE_XPCOM_QUEUE
46# include <sys/select.h>
47# include <errno.h>
48#endif
49
50#ifdef RT_OS_DARWIN
51# include <CoreFoundation/CFRunLoop.h>
52#endif
53
54using namespace com;
55
56/**
57 * IVirtualBoxCallback implementation for handling the GuestControlCallback in
58 * relation to the "guestcontrol * wait" command.
59 */
60/** @todo */
61
62void usageGuestControl(void)
63{
64 RTPrintf("VBoxManage guestcontrol execute <vmname>|<uuid>\n"
65 " <path to program> [--arguments \"<arguments>\"] [--environment \"NAME=VALUE NAME=VALUE\"]\n"
66 " [--flags <flags>] [--username <name> [--password <password>]]\n"
67 " [--timeout <msec>]\n"
68 "\n");
69}
70
71static int handleExecProgram(HandlerArg *a)
72{
73 HRESULT rc = S_OK;
74
75 /*
76 * Check the syntax. We can deduce the correct syntax from the number of
77 * arguments.
78 */
79 bool usageOK = true;
80 if (a->argc < 2) /* At least the command we want to execute in the guest should be present :-). */
81 return errorSyntax(USAGE_GUESTCONTROL, "Incorrect parameters");
82
83 Utf8Str Utf8Cmd(a->argv[1]);
84 uint32_t uFlags = 0;
85 Utf8Str Utf8Args;
86 com::SafeArray <BSTR> env;
87 Utf8Str Utf8StdIn;
88 Utf8Str Utf8StdOut;
89 Utf8Str Utf8StdErr;
90 Utf8Str Utf8UserName;
91 Utf8Str Utf8Password;
92 uint32_t uTimeoutMS = 0;
93
94 /* Iterate through all possible commands (if available). */
95 for (int i = 2; usageOK && i < a->argc; i++)
96 {
97 if ( !strcmp(a->argv[i], "--arguments")
98 || !strcmp(a->argv[i], "--args"))
99 {
100 if (i + 1 >= a->argc)
101 usageOK = false;
102 else
103 {
104 Utf8Args = a->argv[i + 1];
105 ++i;
106 }
107 }
108 else if ( !strcmp(a->argv[i], "--environment")
109 || !strcmp(a->argv[i], "--env"))
110 {
111 if (i + 1 >= a->argc)
112 usageOK = false;
113 else
114 {
115 char **papszArg;
116 int cArgs;
117
118 rc = RTGetOptArgvFromString(&papszArg, &cArgs, a->argv[i + 1], NULL);
119 if (RT_SUCCESS(rc))
120 {
121 for (int a = 0; a < cArgs; a++)
122 env.push_back(Bstr(papszArg[a]));
123
124 RTGetOptArgvFree(papszArg);
125 }
126 ++i;
127 }
128 }
129 else if (!strcmp(a->argv[i], "--flags"))
130 {
131 if ( i + 1 >= a->argc
132 || RTStrToUInt32Full(a->argv[i + 1], 10, &uFlags) != VINF_SUCCESS)
133 usageOK = false;
134 else
135 ++i;
136 }
137 else if ( !strcmp(a->argv[i], "--username")
138 || !strcmp(a->argv[i], "--user"))
139 {
140 if (i + 1 >= a->argc)
141 usageOK = false;
142 else
143 {
144 Utf8UserName = a->argv[i + 1];
145 ++i;
146 }
147 }
148 else if ( !strcmp(a->argv[i], "--password")
149 || !strcmp(a->argv[i], "--pwd"))
150 {
151 if (i + 1 >= a->argc)
152 usageOK = false;
153 else
154 {
155 Utf8Password = a->argv[i + 1];
156 ++i;
157 }
158 }
159 else if (!strcmp(a->argv[i], "--timeout"))
160 {
161 if ( i + 1 >= a->argc
162 || RTStrToUInt32Full(a->argv[i + 1], 10, &uTimeoutMS) != VINF_SUCCESS)
163 usageOK = false;
164 else
165 ++i;
166 }
167 /** @todo Add fancy piping stuff here. */
168 else
169 {
170 return errorSyntax(USAGE_GUESTCONTROL,
171 "Invalid parameter '%s'", Utf8Str(a->argv[i]).raw());
172 }
173 }
174
175 if (!usageOK)
176 return errorSyntax(USAGE_GUESTCONTROL, "Incorrect parameters");
177
178 /* If a password was specified, check if we also got a user name. */
179 if ( !Utf8Password.isEmpty()
180 && Utf8UserName.isEmpty())
181 {
182 return errorSyntax(USAGE_GUESTCONTROL,
183 "No user name for password specified!");
184 }
185
186 /* lookup VM. */
187 ComPtr<IMachine> machine;
188 /* assume it's an UUID */
189 rc = a->virtualBox->GetMachine(Bstr(a->argv[0]), machine.asOutParam());
190 if (FAILED(rc) || !machine)
191 {
192 /* must be a name */
193 CHECK_ERROR(a->virtualBox, FindMachine(Bstr(a->argv[0]), machine.asOutParam()));
194 }
195 if (machine)
196 {
197 do
198 {
199 Bstr uuid;
200 machine->COMGETTER(Id)(uuid.asOutParam());
201
202 /* open an existing session for VM - so the VM has to be running */
203 CHECK_ERROR_BREAK(a->virtualBox, OpenExistingSession(a->session, uuid));
204
205 /* get the mutable session machine */
206 a->session->COMGETTER(Machine)(machine.asOutParam());
207
208 /* get the associated console */
209 ComPtr<IConsole> console;
210 CHECK_ERROR_BREAK(a->session, COMGETTER(Console)(console.asOutParam()));
211
212 ComPtr<IGuest> guest;
213 CHECK_ERROR_BREAK(console, COMGETTER(Guest)(guest.asOutParam()));
214
215 ComPtr<IProgress> progress;
216 ULONG uPID = 0;
217 CHECK_ERROR_BREAK(guest, ExecuteProgram(Bstr(Utf8Cmd), uFlags,
218 Bstr(Utf8Args), ComSafeArrayAsInParam(env),
219 Bstr(Utf8StdIn), Bstr(Utf8StdOut), Bstr(Utf8StdErr),
220 Bstr(Utf8UserName), Bstr(Utf8Password), uTimeoutMS,
221 &uPID, progress.asOutParam()));
222 /** @todo Show some progress here? */
223 a->session->Close();
224 } while (0);
225 }
226 return SUCCEEDED(rc) ? 0 : 1;
227}
228
229/**
230 * Access the guest control store.
231 *
232 * @returns 0 on success, 1 on failure
233 * @note see the command line API description for parameters
234 */
235int handleGuestControl(HandlerArg *a)
236{
237 HandlerArg arg = *a;
238 arg.argc = a->argc - 1;
239 arg.argv = a->argv + 1;
240
241 if (a->argc == 0)
242 return errorSyntax(USAGE_GUESTCONTROL, "Incorrect parameters");
243
244 /* switch (cmd) */
245 if (strcmp(a->argv[0], "exec") == 0)
246 return handleExecProgram(&arg);
247
248 /* default: */
249 return errorSyntax(USAGE_GUESTCONTROL, "Incorrect parameters");
250}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette