VirtualBox

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

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

Guest Control: Update (Main: argument as array, SDK docs).

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