VirtualBox

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

Last change on this file since 28138 was 28132, checked in by vboxsync, 15 years ago

Guest Control: Update (fixed leak in host service, --wait option for VBoxManage).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.5 KB
Line 
1/* $Id: VBoxManageGuestCtrl.cpp 28132 2010-04-09 10:02:00Z 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>] [--wait stdout[,[stderr]]]\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 com::SafeArray <BSTR> args;
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 bool waitForOutput = false;
94
95 /* Iterate through all possible commands (if available). */
96 for (int i = 2; usageOK && i < a->argc; i++)
97 {
98 if ( !strcmp(a->argv[i], "--arguments")
99 || !strcmp(a->argv[i], "--args")
100 || !strcmp(a->argv[i], "--arg"))
101 {
102 if (i + 1 >= a->argc)
103 usageOK = false;
104 else
105 {
106 char **papszArg;
107 int cArgs;
108
109 rc = RTGetOptArgvFromString(&papszArg, &cArgs, a->argv[i + 1], NULL);
110 if (RT_SUCCESS(rc))
111 {
112 for (int a = 0; a < cArgs; a++)
113 args.push_back(Bstr(papszArg[a]));
114
115 RTGetOptArgvFree(papszArg);
116 }
117 ++i;
118 }
119 }
120 else if ( !strcmp(a->argv[i], "--environment")
121 || !strcmp(a->argv[i], "--env"))
122 {
123 if (i + 1 >= a->argc)
124 usageOK = false;
125 else
126 {
127 char **papszArg;
128 int cArgs;
129
130 rc = RTGetOptArgvFromString(&papszArg, &cArgs, a->argv[i + 1], NULL);
131 if (RT_SUCCESS(rc))
132 {
133 for (int a = 0; a < cArgs; a++)
134 env.push_back(Bstr(papszArg[a]));
135
136 RTGetOptArgvFree(papszArg);
137 }
138 ++i;
139 }
140 }
141 else if (!strcmp(a->argv[i], "--flags"))
142 {
143 if ( i + 1 >= a->argc
144 || RTStrToUInt32Full(a->argv[i + 1], 10, &uFlags) != VINF_SUCCESS)
145 usageOK = false;
146 else
147 ++i;
148 }
149 else if ( !strcmp(a->argv[i], "--username")
150 || !strcmp(a->argv[i], "--user"))
151 {
152 if (i + 1 >= a->argc)
153 usageOK = false;
154 else
155 {
156 Utf8UserName = a->argv[i + 1];
157 ++i;
158 }
159 }
160 else if ( !strcmp(a->argv[i], "--password")
161 || !strcmp(a->argv[i], "--pwd"))
162 {
163 if (i + 1 >= a->argc)
164 usageOK = false;
165 else
166 {
167 Utf8Password = a->argv[i + 1];
168 ++i;
169 }
170 }
171 else if (!strcmp(a->argv[i], "--timeout"))
172 {
173 if ( i + 1 >= a->argc
174 || RTStrToUInt32Full(a->argv[i + 1], 10, &uTimeoutMS) != VINF_SUCCESS)
175 usageOK = false;
176 else
177 ++i;
178 }
179 else if (!strcmp(a->argv[i], "--wait"))
180 {
181 if (i + 1 >= a->argc)
182 usageOK = false;
183 else
184 {
185 /** @todo Check for "stdout" or "stderr"! */
186 waitForOutput = true;
187 ++i;
188 }
189 }
190 /** @todo Add fancy piping stuff here. */
191 else
192 {
193 return errorSyntax(USAGE_GUESTCONTROL,
194 "Invalid parameter '%s'", Utf8Str(a->argv[i]).raw());
195 }
196 }
197
198 if (!usageOK)
199 return errorSyntax(USAGE_GUESTCONTROL, "Incorrect parameters");
200
201 /* If a password was specified, check if we also got a user name. */
202 if ( !Utf8Password.isEmpty()
203 && Utf8UserName.isEmpty())
204 {
205 return errorSyntax(USAGE_GUESTCONTROL,
206 "No user name for password specified!");
207 }
208
209 /* lookup VM. */
210 ComPtr<IMachine> machine;
211 /* assume it's an UUID */
212 rc = a->virtualBox->GetMachine(Bstr(a->argv[0]), machine.asOutParam());
213 if (FAILED(rc) || !machine)
214 {
215 /* must be a name */
216 CHECK_ERROR(a->virtualBox, FindMachine(Bstr(a->argv[0]), machine.asOutParam()));
217 }
218 if (machine)
219 {
220 do
221 {
222 Bstr uuid;
223 machine->COMGETTER(Id)(uuid.asOutParam());
224
225 /* open an existing session for VM - so the VM has to be running */
226 CHECK_ERROR_BREAK(a->virtualBox, OpenExistingSession(a->session, uuid));
227
228 /* get the mutable session machine */
229 a->session->COMGETTER(Machine)(machine.asOutParam());
230
231 /* get the associated console */
232 ComPtr<IConsole> console;
233 CHECK_ERROR_BREAK(a->session, COMGETTER(Console)(console.asOutParam()));
234
235 ComPtr<IGuest> guest;
236 CHECK_ERROR_BREAK(console, COMGETTER(Guest)(guest.asOutParam()));
237
238 ComPtr<IProgress> progress;
239 ULONG uPID = 0;
240 CHECK_ERROR_BREAK(guest, ExecuteProcess(Bstr(Utf8Cmd), uFlags,
241 ComSafeArrayAsInParam(args), ComSafeArrayAsInParam(env),
242 Bstr(Utf8StdIn), Bstr(Utf8StdOut), Bstr(Utf8StdErr),
243 Bstr(Utf8UserName), Bstr(Utf8Password), uTimeoutMS,
244 &uPID, progress.asOutParam()));
245 if (waitForOutput)
246 {
247
248 }
249 /** @todo Show some progress here? */
250 a->session->Close();
251 } while (0);
252 }
253 return SUCCEEDED(rc) ? 0 : 1;
254}
255
256/**
257 * Access the guest control store.
258 *
259 * @returns 0 on success, 1 on failure
260 * @note see the command line API description for parameters
261 */
262int handleGuestControl(HandlerArg *a)
263{
264 HandlerArg arg = *a;
265 arg.argc = a->argc - 1;
266 arg.argv = a->argv + 1;
267
268 if (a->argc == 0)
269 return errorSyntax(USAGE_GUESTCONTROL, "Incorrect parameters");
270
271 /* switch (cmd) */
272 if (strcmp(a->argv[0], "exec") == 0)
273 return handleExecProgram(&arg);
274
275 /* default: */
276 return errorSyntax(USAGE_GUESTCONTROL, "Incorrect parameters");
277}
Note: See TracBrowser for help on using the repository browser.

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