VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxControl/VBoxControl.cpp@ 10111

Last change on this file since 10111 was 10111, checked in by vboxsync, 17 years ago

warning

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 9.4 KB
Line 
1/** $Id: VBoxControl.cpp 10111 2008-07-02 14:36:35Z vboxsync $ */
2/** @file
3 * VBoxControl - Guest Additions Command Line Management Interface
4 */
5
6/*
7 * Copyright (C) 2007 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. 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/*******************************************************************************
25* Header Files *
26*******************************************************************************/
27#include <iprt/thread.h>
28#include <iprt/string.h>
29#include <iprt/stream.h>
30#include <iprt/path.h>
31#include <iprt/initterm.h>
32#include <VBox/VBoxGuest.h>
33#include <VBox/version.h>
34#ifdef VBOX_WITH_INFO_SVC
35# include <VBox/HostServices/VBoxInfoSvc.h>
36#endif
37#include "VBoxControl.h"
38
39/*******************************************************************************
40* Global Variables *
41*******************************************************************************/
42/** The program name (derived from argv[0]). */
43char const *g_pszProgName = "";
44/** The current verbosity level. */
45int g_cVerbosity = 0;
46
47
48/**
49 * Displays the program usage message.
50 *
51 * @param u64Which
52 *
53 * @{
54 */
55
56/** Helper function */
57static void doUsage(char const *line, char const *name = "", char const *command = "")
58{
59 RTPrintf("%s %-*s%s", name, 30 - strlen(name), command, line);
60}
61
62/** Enumerate the different parts of the usage we might want to print out */
63enum g_eUsage
64{
65#ifdef VBOX_WITH_INFO_SVC
66 GET_GUEST_PROP,
67 SET_GUEST_PROP,
68#endif
69 USAGE_ALL = UINT32_MAX
70};
71
72static void usage(g_eUsage eWhich = USAGE_ALL)
73{
74 RTPrintf("Usage:\n\n");
75 RTPrintf("%s [-v|--version] print version number and exit\n", g_pszProgName);
76 RTPrintf("%s --nologo ... suppress the logo\n\n", g_pszProgName);
77
78 if ((GET_GUEST_PROP == eWhich) || (USAGE_ALL == eWhich))
79 doUsage("<key>\n", g_pszProgName, "getguestproperty");
80 if ((SET_GUEST_PROP == eWhich) || (USAGE_ALL == eWhich))
81 doUsage("<key> [<value>] (no value deletes key)\n", g_pszProgName, "setguestproperty");
82}
83/** @} */
84
85/**
86 * Displays an error message.
87 *
88 * @param pszFormat The message text.
89 * @param ... Format arguments.
90 */
91static void VBoxControlError(const char *pszFormat, ...)
92{
93 // RTStrmPrintf(g_pStdErr, "%s: error: ", g_pszProgName);
94
95 va_list va;
96 va_start(va, pszFormat);
97 RTStrmPrintfV(g_pStdErr, pszFormat, va);
98 va_end(va);
99}
100
101#ifdef VBOX_WITH_INFO_SVC
102/**
103 * Retrieves a value from the host/guest configuration registry.
104 * This is accessed through the "VBoxSharedInfoSvc" HGCM service.
105 *
106 * @returns 0 on success, 1 on failure
107 * @param key (string) the key which the value is stored under.
108 */
109int getGuestProperty(int argc, char **argv)
110{
111 using namespace svcInfo;
112
113 uint32_t u32ClientID = 0;
114 int rc = VINF_SUCCESS;
115
116 char szValue[KEY_MAX_VALUE_LEN];
117 if (argc != 1)
118 {
119 usage(GET_GUEST_PROP);
120 return 1;
121 }
122 rc = VbglR3InfoSvcConnect(&u32ClientID);
123 if (!RT_SUCCESS(rc))
124 VBoxControlError("Failed to connect to the guest property service, error %Rrc\n", rc);
125 if (RT_SUCCESS(rc))
126 {
127 rc = VbglR3InfoSvcReadKey(u32ClientID, argv[0], szValue, sizeof(szValue), NULL);
128 if (!RT_SUCCESS(rc) && (rc != VERR_NOT_FOUND))
129 VBoxControlError("Failed to retrieve the property value, error %Rrc\n", rc);
130 }
131 if (RT_SUCCESS(rc) || (VERR_NOT_FOUND == rc))
132 {
133 if (RT_SUCCESS(rc))
134 RTPrintf("Value: %s\n", szValue);
135 else
136 RTPrintf("No value set!\n");
137 }
138 if (u32ClientID != 0)
139 VbglR3InfoSvcDisconnect(u32ClientID);
140 return RT_SUCCESS(rc) ? 0 : 1;
141}
142
143
144/**
145 * Writes a value to the host/guest configuration registry.
146 * This is accessed through the "VBoxSharedInfoSvc" HGCM service.
147 *
148 * @returns 0 on success, 1 on failure
149 * @param key (string) the key which the value is stored under.
150 * @param value (string) the value to write. If empty, the key will be
151 * removed.
152 */
153static int setGuestProperty(int argc, char *argv[])
154{
155 if (argc != 1 && argc != 2)
156 {
157 usage();
158 return 1;
159 }
160 char *pszValue = NULL;
161 int rc = VINF_SUCCESS;
162 uint32_t u32ClientID = 0;
163
164 if (2 == argc)
165 pszValue = argv[1];
166 rc = VbglR3InfoSvcConnect(&u32ClientID);
167 if (!RT_SUCCESS(rc))
168 VBoxControlError("Failed to connect to the host/guest registry service, error %Rrc\n", rc);
169 if (RT_SUCCESS(rc))
170 {
171 rc = VbglR3InfoSvcWriteKey(u32ClientID, argv[0], pszValue);
172 if (!RT_SUCCESS(rc))
173 VBoxControlError("Failed to store the property value, error %Rrc\n", rc);
174 }
175 if (u32ClientID != 0)
176 VbglR3InfoSvcDisconnect(u32ClientID);
177 return RT_SUCCESS(rc) ? 0 : 1;
178}
179#endif
180
181/** command handler type */
182typedef DECLCALLBACK(int) FNHANDLER(int argc, char *argv[]);
183typedef FNHANDLER *PFNHANDLER;
184
185/** The table of all registered command handlers. */
186struct COMMANDHANDLER
187{
188 const char *command;
189 PFNHANDLER handler;
190} g_commandHandlers[] =
191{
192#ifdef VBOX_WITH_INFO_SVC
193 { "getguestproperty", getGuestProperty },
194 { "setguestproperty", setGuestProperty }
195#endif
196};
197
198/** Main function */
199int main(int argc, char **argv)
200{
201 /** The application's global return code */
202 int rc = 0;
203 /** An IPRT return code for local use */
204 int rrc = VINF_SUCCESS;
205 /** The index of the command line argument we are currently processing */
206 int iArg = 1;
207 /** Should we show the logo text? */
208 bool showlogo = true;
209 /** Should we print the usage after the logo? For the --help switch. */
210 bool dohelp = false;
211 /** Will we be executing a command or just printing information? */
212 bool onlyinfo = false;
213
214/*
215 * Start by handling command line switches
216 */
217
218 /** Are we finished with handling switches? */
219 bool done = false;
220 while (!done && (iArg < argc))
221 {
222 if ( (0 == strcmp(argv[iArg], "-v"))
223 || (0 == strcmp(argv[iArg], "--version"))
224 )
225 {
226 /* Print version number, and do nothing else. */
227 RTPrintf("%sr%d\n", VBOX_VERSION_STRING, VBoxSVNRev ());
228 onlyinfo = true;
229 showlogo = false;
230 done = true;
231 }
232 else if (0 == strcmp(argv[iArg], "--nologo"))
233 showlogo = false;
234 else if (0 == strcmp(argv[iArg], "--help"))
235 {
236 onlyinfo = true;
237 dohelp = true;
238 done = true;
239 }
240 else
241 /* We have found an argument which isn't a switch. Exit to the
242 * command processing bit. */
243 done = true;
244 if (!done)
245 ++iArg;
246 }
247
248/*
249 * Find the application name, show our logo if the user hasn't suppressed it,
250 * and show the usage if the user asked us to
251 */
252
253 g_pszProgName = RTPathFilename(argv[0]);
254 if (showlogo)
255 RTPrintf("VirtualBox Guest Additions Command Line Management Interface Version "
256 VBOX_VERSION_STRING "\n"
257 "(C) 2008 Sun Microsystems, Inc.\n"
258 "All rights reserved\n\n");
259 if (dohelp)
260 usage();
261
262/*
263 * Do global initialisation for the programme if we will be handling a command
264 */
265
266 if (!onlyinfo)
267 {
268 rrc = RTR3Init(false, 0);
269 if (!RT_SUCCESS(rrc))
270 {
271 VBoxControlError("Failed to initialise the VirtualBox runtime - error %Rrc\n", rrc);
272 rc = 1;
273 }
274 if (0 == rc)
275 {
276 if (!RT_SUCCESS(VbglR3Init()))
277 {
278 VBoxControlError("Could not contact the host system. Make sure that you are running this\n"
279 "application inside a VirtualBox guest system, and that you have sufficient\n"
280 "user permissions.\n");
281 rc = 1;
282 }
283 }
284 }
285
286/*
287 * Now look for an actual command in the argument list and handle it.
288 */
289
290 if (!onlyinfo && (0 == rc))
291 {
292 if (argc > iArg)
293 {
294 /** Is next parameter a known command? */
295 bool found = false;
296 /** And if so, what is its position in the table? */
297 unsigned index = 0;
298 while (index < RT_ELEMENTS(g_commandHandlers) && !found)
299 {
300 if (0 == strcmp(argv[iArg], g_commandHandlers[index].command))
301 found = true;
302 else
303 ++index;
304 }
305 if (found)
306 rc = g_commandHandlers[index].handler(argc - iArg - 1, argv + iArg + 1);
307 else
308 {
309 rc = 1;
310 usage();
311 }
312 }
313 else
314 {
315 /* The user didn't specify a command. */
316 rc = 1;
317 usage();
318 }
319 }
320
321/*
322 * And exit, returning the status
323 */
324
325 return rc;
326}
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