VirtualBox

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

Last change on this file was 98103, checked in by vboxsync, 16 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 70.6 KB
Line 
1/* $Id: VBoxControl.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * VBoxControl - Guest Additions Command Line Management Interface.
4 */
5
6/*
7 * Copyright (C) 2008-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 <iprt/alloca.h>
33#include <iprt/cpp/autores.h>
34#include <iprt/buildconfig.h>
35#include <iprt/ctype.h>
36#include <iprt/errcore.h>
37#include <iprt/getopt.h>
38#include <iprt/initterm.h>
39#include <iprt/mem.h>
40#include <iprt/message.h>
41#include <iprt/path.h>
42#include <iprt/string.h>
43#include <iprt/stream.h>
44#include <iprt/zip.h>
45#include <VBox/log.h>
46#include <VBox/version.h>
47#include <VBox/VBoxGuestLib.h>
48#ifdef RT_OS_WINDOWS
49# include <iprt/win/windows.h>
50#endif
51#ifdef VBOX_WITH_GUEST_PROPS
52# include <VBox/HostServices/GuestPropertySvc.h>
53#endif
54#ifdef VBOX_WITH_SHARED_FOLDERS
55# include <VBox/shflsvc.h>
56# ifdef RT_OS_OS2
57# define OS2EMX_PLAIN_CHAR
58# define INCL_ERRORS
59# define INCL_DOSFILEMGR
60# include <os2emx.h>
61# endif
62#endif
63#ifdef VBOX_WITH_DPC_LATENCY_CHECKER
64# include <VBox/VBoxGuest.h>
65# include "../VBoxGuest/lib/VBoxGuestR3LibInternal.h" /* HACK ALERT! Using vbglR3DoIOCtl directly!! */
66#endif
67
68
69/*********************************************************************************************************************************
70* Global Variables *
71*********************************************************************************************************************************/
72/** The program name (derived from argv[0]). */
73char const *g_pszProgName = "";
74/** The current verbosity level. */
75int g_cVerbosity = 0;
76
77
78/** @name Displays the program usage message.
79 * @{
80 */
81
82/**
83 * Helper function that does indentation.
84 *
85 * @param pszLine Text.
86 * @param pszName Program name.
87 * @param pszCommand Command/option syntax.
88 */
89static void doUsage(char const *pszLine, char const *pszName = "", char const *pszCommand = "")
90{
91 /* Allow for up to 15 characters command name length (VBoxControl.exe) with
92 * perfect column alignment. Beyond that there's at least one space between
93 * the command if there are command line parameters. */
94 RTPrintf("%s %-*s%s%s\n",
95 pszName,
96 *pszLine ? 35 - strlen(pszName) : 1, pszCommand,
97 *pszLine ? " " : "", pszLine);
98}
99
100/** Enumerate the different parts of the usage we might want to print out */
101enum VBoxControlUsage
102{
103#ifdef RT_OS_WINDOWS
104 GET_VIDEO_ACCEL,
105 SET_VIDEO_ACCEL,
106 VIDEO_FLAGS,
107 LIST_CUST_MODES,
108 ADD_CUST_MODE,
109 REMOVE_CUST_MODE,
110 SET_VIDEO_MODE,
111#endif
112#ifdef VBOX_WITH_GUEST_PROPS
113 GUEST_PROP,
114#endif
115#ifdef VBOX_WITH_SHARED_FOLDERS
116 GUEST_SHAREDFOLDERS,
117#endif
118#if !defined(VBOX_CONTROL_TEST)
119 WRITE_CORE_DUMP,
120#endif
121 WRITE_LOG,
122 TAKE_SNAPSHOT,
123 SAVE_STATE,
124 SUSPEND,
125 POWER_OFF,
126 VERSION,
127 HELP,
128 USAGE_ALL = UINT32_MAX
129};
130
131static RTEXITCODE usage(enum VBoxControlUsage eWhich = USAGE_ALL)
132{
133 RTPrintf("Usage:\n\n");
134 doUsage("print version number and exit", g_pszProgName, "[-V|--version]");
135 doUsage("suppress the logo", g_pszProgName, "--nologo ...");
136 RTPrintf("\n");
137
138 /* Exclude the Windows bits from the test version. Anyone who needs to
139 test them can fix this. */
140#if defined(RT_OS_WINDOWS) && !defined(VBOX_CONTROL_TEST)
141 if (eWhich == GET_VIDEO_ACCEL || eWhich == USAGE_ALL)
142 doUsage("", g_pszProgName, "getvideoacceleration");
143 if (eWhich == SET_VIDEO_ACCEL || eWhich == USAGE_ALL)
144 doUsage("<on|off>", g_pszProgName, "setvideoacceleration");
145 if (eWhich == VIDEO_FLAGS || eWhich == USAGE_ALL)
146 doUsage("<get|set|clear|delete> [hex mask]", g_pszProgName, "videoflags");
147 if (eWhich == LIST_CUST_MODES || eWhich == USAGE_ALL)
148 doUsage("", g_pszProgName, "listcustommodes");
149 if (eWhich == ADD_CUST_MODE || eWhich == USAGE_ALL)
150 doUsage("<width> <height> <bpp>", g_pszProgName, "addcustommode");
151 if (eWhich == REMOVE_CUST_MODE || eWhich == USAGE_ALL)
152 doUsage("<width> <height> <bpp>", g_pszProgName, "removecustommode");
153 if (eWhich == SET_VIDEO_MODE || eWhich == USAGE_ALL)
154 doUsage("<width> <height> <bpp> <screen>", g_pszProgName, "setvideomode");
155#endif
156#ifdef VBOX_WITH_GUEST_PROPS
157 if (eWhich == GUEST_PROP || eWhich == USAGE_ALL)
158 {
159 doUsage("get <property> [--verbose]", g_pszProgName, "guestproperty");
160 doUsage("set <property> [<value> [--flags <flags>]]", g_pszProgName, "guestproperty");
161 doUsage("delete|unset <property>", g_pszProgName, "guestproperty");
162 doUsage("enumerate [--patterns <patterns>]", g_pszProgName, "guestproperty");
163 doUsage("wait <patterns>", g_pszProgName, "guestproperty");
164 doUsage("[--timestamp <last timestamp>]");
165 doUsage("[--timeout <timeout in ms>");
166 }
167#endif
168#ifdef VBOX_WITH_SHARED_FOLDERS
169 if (eWhich == GUEST_SHAREDFOLDERS || eWhich == USAGE_ALL)
170 {
171 doUsage("list [--automount]", g_pszProgName, "sharedfolder");
172# ifdef RT_OS_OS2
173 doUsage("use <drive> <folder>", g_pszProgName, "sharedfolder");
174 doUsage("unuse <drive>", g_pszProgName, "sharedfolder");
175# endif
176 }
177#endif
178
179#if !defined(VBOX_CONTROL_TEST)
180 if (eWhich == WRITE_CORE_DUMP || eWhich == USAGE_ALL)
181 doUsage("", g_pszProgName, "writecoredump");
182#endif
183 if (eWhich == WRITE_LOG || eWhich == USAGE_ALL)
184 doUsage("", g_pszProgName, "writelog [-n|--no-newline] [--] <msg>");
185 if (eWhich == TAKE_SNAPSHOT || eWhich == USAGE_ALL)
186 doUsage("", g_pszProgName, "takesnapshot");
187 if (eWhich == SAVE_STATE || eWhich == USAGE_ALL)
188 doUsage("", g_pszProgName, "savestate");
189 if (eWhich == SUSPEND || eWhich == USAGE_ALL)
190 doUsage("", g_pszProgName, "suspend");
191 if (eWhich == POWER_OFF || eWhich == USAGE_ALL)
192 doUsage("", g_pszProgName, "poweroff");
193 if (eWhich == HELP || eWhich == USAGE_ALL)
194 doUsage("[command]", g_pszProgName, "help");
195 if (eWhich == VERSION || eWhich == USAGE_ALL)
196 doUsage("", g_pszProgName, "version");
197
198 return RTEXITCODE_SUCCESS;
199}
200
201/** @} */
202
203
204/**
205 * Implementation of the '--version' option.
206 *
207 * @returns RTEXITCODE_SUCCESS
208 */
209static RTEXITCODE printVersion(void)
210{
211 RTPrintf("%sr%u\n", VBOX_VERSION_STRING, RTBldCfgRevision());
212 return RTEXITCODE_SUCCESS;
213}
214
215
216/**
217 * Displays an error message.
218 *
219 * @returns RTEXITCODE_FAILURE.
220 * @param pszFormat The message text. No newline.
221 * @param ... Format arguments.
222 */
223static RTEXITCODE VBoxControlError(const char *pszFormat, ...)
224{
225 /** @todo prefix with current command. */
226 va_list va;
227 va_start(va, pszFormat);
228 RTMsgErrorV(pszFormat, va);
229 va_end(va);
230 return RTEXITCODE_FAILURE;
231}
232
233
234/**
235 * Displays a getopt error.
236 *
237 * @returns RTEXITCODE_FAILURE.
238 * @param ch The RTGetOpt return value.
239 * @param pValueUnion The RTGetOpt return data.
240 */
241static RTEXITCODE VBoxCtrlGetOptError(int ch, PCRTGETOPTUNION pValueUnion)
242{
243 /** @todo prefix with current command. */
244 return RTGetOptPrintError(ch, pValueUnion);
245}
246
247
248/**
249 * Displays an syntax error message.
250 *
251 * @returns RTEXITCODE_FAILURE.
252 * @param pszFormat The message text. No newline.
253 * @param ... Format arguments.
254 */
255static RTEXITCODE VBoxControlSyntaxError(const char *pszFormat, ...)
256{
257 /** @todo prefix with current command. */
258 va_list va;
259 va_start(va, pszFormat);
260 RTMsgErrorV(pszFormat, va);
261 va_end(va);
262 return RTEXITCODE_SYNTAX;
263}
264
265#if defined(RT_OS_WINDOWS) && !defined(VBOX_CONTROL_TEST)
266
267decltype(ChangeDisplaySettingsExA) *g_pfnChangeDisplaySettingsExA;
268decltype(ChangeDisplaySettings) *g_pfnChangeDisplaySettingsA;
269decltype(EnumDisplaySettingsA) *g_pfnEnumDisplaySettingsA;
270
271static unsigned nextAdjacentRectXP(RECTL const *paRects, unsigned cRects, unsigned iRect)
272{
273 for (unsigned i = 0; i < cRects; i++)
274 if (paRects[iRect].right == paRects[i].left)
275 return i;
276 return ~0U;
277}
278
279static unsigned nextAdjacentRectXN(RECTL const *paRects, unsigned cRects, unsigned iRect)
280{
281 for (unsigned i = 0; i < cRects; i++)
282 if (paRects[iRect].left == paRects[i].right)
283 return i;
284 return ~0U;
285}
286
287static unsigned nextAdjacentRectYP(RECTL const *paRects, unsigned cRects, unsigned iRect)
288{
289 for (unsigned i = 0; i < cRects; i++)
290 if (paRects[iRect].bottom == paRects[i].top)
291 return i;
292 return ~0U;
293}
294
295unsigned nextAdjacentRectYN(RECTL const *paRects, unsigned cRects, unsigned iRect)
296{
297 for (unsigned i = 0; i < cRects; i++)
298 if (paRects[iRect].top == paRects[i].bottom)
299 return i;
300 return ~0U;
301}
302
303void resizeRect(RECTL *paRects, unsigned cRects, unsigned iPrimary, unsigned iResized, int NewWidth, int NewHeight)
304{
305 RECTL *paNewRects = (RECTL *)alloca(sizeof (RECTL) * cRects);
306 memcpy (paNewRects, paRects, sizeof(RECTL) * cRects);
307 paNewRects[iResized].right += NewWidth - (paNewRects[iResized].right - paNewRects[iResized].left);
308 paNewRects[iResized].bottom += NewHeight - (paNewRects[iResized].bottom - paNewRects[iResized].top);
309
310 /* Verify all pairs of originally adjacent rectangles for all 4 directions.
311 * If the pair has a "good" delta (that is the first rectangle intersects the second)
312 * at a direction and the second rectangle is not primary one (which can not be moved),
313 * move the second rectangle to make it adjacent to the first one.
314 */
315
316 /* X positive. */
317 unsigned iRect;
318 for (iRect = 0; iRect < cRects; iRect++)
319 {
320 /* Find the next adjacent original rect in x positive direction. */
321 unsigned iNextRect = nextAdjacentRectXP (paRects, cRects, iRect);
322 Log(("next %d -> %d\n", iRect, iNextRect));
323
324 if (iNextRect == ~0 || iNextRect == iPrimary)
325 {
326 continue;
327 }
328
329 /* Check whether there is an X intersection between these adjacent rects in the new rectangles
330 * and fix the intersection if delta is "good".
331 */
332 int delta = paNewRects[iRect].right - paNewRects[iNextRect].left;
333
334 if (delta > 0)
335 {
336 Log(("XP intersection right %d left %d, diff %d\n",
337 paNewRects[iRect].right, paNewRects[iNextRect].left,
338 delta));
339
340 paNewRects[iNextRect].left += delta;
341 paNewRects[iNextRect].right += delta;
342 }
343 }
344
345 /* X negative. */
346 for (iRect = 0; iRect < cRects; iRect++)
347 {
348 /* Find the next adjacent original rect in x negative direction. */
349 unsigned iNextRect = nextAdjacentRectXN (paRects, cRects, iRect);
350 Log(("next %d -> %d\n", iRect, iNextRect));
351
352 if (iNextRect == ~0 || iNextRect == iPrimary)
353 {
354 continue;
355 }
356
357 /* Check whether there is an X intersection between these adjacent rects in the new rectangles
358 * and fix the intersection if delta is "good".
359 */
360 int delta = paNewRects[iRect].left - paNewRects[iNextRect].right;
361
362 if (delta < 0)
363 {
364 Log(("XN intersection left %d right %d, diff %d\n",
365 paNewRects[iRect].left, paNewRects[iNextRect].right,
366 delta));
367
368 paNewRects[iNextRect].left += delta;
369 paNewRects[iNextRect].right += delta;
370 }
371 }
372
373 /* Y positive (in the computer sense, top->down). */
374 for (iRect = 0; iRect < cRects; iRect++)
375 {
376 /* Find the next adjacent original rect in y positive direction. */
377 unsigned iNextRect = nextAdjacentRectYP (paRects, cRects, iRect);
378 Log(("next %d -> %d\n", iRect, iNextRect));
379
380 if (iNextRect == ~0 || iNextRect == iPrimary)
381 {
382 continue;
383 }
384
385 /* Check whether there is an Y intersection between these adjacent rects in the new rectangles
386 * and fix the intersection if delta is "good".
387 */
388 int delta = paNewRects[iRect].bottom - paNewRects[iNextRect].top;
389
390 if (delta > 0)
391 {
392 Log(("YP intersection bottom %d top %d, diff %d\n",
393 paNewRects[iRect].bottom, paNewRects[iNextRect].top,
394 delta));
395
396 paNewRects[iNextRect].top += delta;
397 paNewRects[iNextRect].bottom += delta;
398 }
399 }
400
401 /* Y negative (in the computer sense, down->top). */
402 for (iRect = 0; iRect < cRects; iRect++)
403 {
404 /* Find the next adjacent original rect in x negative direction. */
405 unsigned iNextRect = nextAdjacentRectYN (paRects, cRects, iRect);
406 Log(("next %d -> %d\n", iRect, iNextRect));
407
408 if (iNextRect == ~0 || iNextRect == iPrimary)
409 {
410 continue;
411 }
412
413 /* Check whether there is an Y intersection between these adjacent rects in the new rectangles
414 * and fix the intersection if delta is "good".
415 */
416 int delta = paNewRects[iRect].top - paNewRects[iNextRect].bottom;
417
418 if (delta < 0)
419 {
420 Log(("YN intersection top %d bottom %d, diff %d\n",
421 paNewRects[iRect].top, paNewRects[iNextRect].bottom,
422 delta));
423
424 paNewRects[iNextRect].top += delta;
425 paNewRects[iNextRect].bottom += delta;
426 }
427 }
428
429 memcpy (paRects, paNewRects, sizeof (RECTL) * cRects);
430 return;
431}
432
433/* Returns TRUE to try again. */
434static BOOL ResizeDisplayDevice(ULONG Id, DWORD Width, DWORD Height, DWORD BitsPerPixel)
435{
436 BOOL fModeReset = (Width == 0 && Height == 0 && BitsPerPixel == 0);
437
438 DISPLAY_DEVICE DisplayDevice;
439 RT_ZERO(DisplayDevice);
440 DisplayDevice.cb = sizeof(DisplayDevice);
441
442 /* Find out how many display devices the system has */
443 DWORD NumDevices = 0;
444 DWORD i = 0;
445 while (EnumDisplayDevices(NULL, i, &DisplayDevice, 0))
446 {
447 Log(("[%d] %s\n", i, DisplayDevice.DeviceName));
448
449 if (DisplayDevice.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE)
450 {
451 Log(("Found primary device. err %d\n", GetLastError()));
452 NumDevices++;
453 }
454 else if (!(DisplayDevice.StateFlags & DISPLAY_DEVICE_MIRRORING_DRIVER))
455 {
456
457 Log(("Found secondary device. err %d\n", GetLastError()));
458 NumDevices++;
459 }
460
461 RT_ZERO(DisplayDevice);
462 DisplayDevice.cb = sizeof(DisplayDevice);
463 i++;
464 }
465
466 Log(("Found total %d devices. err %d\n", NumDevices, GetLastError()));
467
468 if (NumDevices == 0 || Id >= NumDevices)
469 {
470 Log(("Requested identifier %d is invalid. err %d\n", Id, GetLastError()));
471 return FALSE;
472 }
473
474 DISPLAY_DEVICE *paDisplayDevices = (DISPLAY_DEVICE *)alloca(sizeof (DISPLAY_DEVICE) * NumDevices);
475 DEVMODE *paDeviceModes = (DEVMODE *)alloca(sizeof (DEVMODE) * NumDevices);
476 RECTL *paRects = (RECTL *)alloca(sizeof (RECTL) * NumDevices);
477
478 /* Fetch information about current devices and modes. */
479 DWORD DevNum = 0;
480 DWORD DevPrimaryNum = 0;
481
482 RT_ZERO(DisplayDevice);
483 DisplayDevice.cb = sizeof(DISPLAY_DEVICE);
484
485 i = 0;
486 while (EnumDisplayDevices (NULL, i, &DisplayDevice, 0))
487 {
488 Log(("[%d(%d)] %s\n", i, DevNum, DisplayDevice.DeviceName));
489
490 BOOL fFetchDevice = FALSE;
491
492 if (DisplayDevice.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE)
493 {
494 Log(("Found primary device. err %d\n", GetLastError()));
495 DevPrimaryNum = DevNum;
496 fFetchDevice = TRUE;
497 }
498 else if (!(DisplayDevice.StateFlags & DISPLAY_DEVICE_MIRRORING_DRIVER))
499 {
500
501 Log(("Found secondary device. err %d\n", GetLastError()));
502 fFetchDevice = TRUE;
503 }
504
505 if (fFetchDevice)
506 {
507 if (DevNum >= NumDevices)
508 {
509 Log(("%d >= %d\n", NumDevices, DevNum));
510 return FALSE;
511 }
512
513 paDisplayDevices[DevNum] = DisplayDevice;
514
515 RT_BZERO(&paDeviceModes[DevNum], sizeof(DEVMODE));
516 paDeviceModes[DevNum].dmSize = sizeof(DEVMODE);
517 if (!g_pfnEnumDisplaySettingsA((LPSTR)DisplayDevice.DeviceName, ENUM_REGISTRY_SETTINGS, &paDeviceModes[DevNum]))
518 {
519 Log(("EnumDisplaySettings err %d\n", GetLastError()));
520 return FALSE;
521 }
522
523 Log(("%dx%d at %d,%d\n",
524 paDeviceModes[DevNum].dmPelsWidth,
525 paDeviceModes[DevNum].dmPelsHeight,
526 paDeviceModes[DevNum].dmPosition.x,
527 paDeviceModes[DevNum].dmPosition.y));
528
529 paRects[DevNum].left = paDeviceModes[DevNum].dmPosition.x;
530 paRects[DevNum].top = paDeviceModes[DevNum].dmPosition.y;
531 paRects[DevNum].right = paDeviceModes[DevNum].dmPosition.x + paDeviceModes[DevNum].dmPelsWidth;
532 paRects[DevNum].bottom = paDeviceModes[DevNum].dmPosition.y + paDeviceModes[DevNum].dmPelsHeight;
533 DevNum++;
534 }
535
536 RT_ZERO(DisplayDevice);
537 DisplayDevice.cb = sizeof(DISPLAY_DEVICE);
538 i++;
539 }
540
541 if (Width == 0)
542 Width = paRects[Id].right - paRects[Id].left;
543
544 if (Height == 0)
545 Height = paRects[Id].bottom - paRects[Id].top;
546
547 /* Check whether a mode reset or a change is requested. */
548 if ( !fModeReset
549 && paRects[Id].right - paRects[Id].left == (LONG)Width
550 && paRects[Id].bottom - paRects[Id].top == (LONG)Height
551 && paDeviceModes[Id].dmBitsPerPel == BitsPerPixel)
552 {
553 Log(("VBoxDisplayThread : already at desired resolution.\n"));
554 return FALSE;
555 }
556
557 resizeRect(paRects, NumDevices, DevPrimaryNum, Id, Width, Height);
558#ifdef LOG_ENABLED
559 for (i = 0; i < NumDevices; i++)
560 Log(("[%d]: %d,%d %dx%d\n",
561 i, paRects[i].left, paRects[i].top,
562 paRects[i].right - paRects[i].left,
563 paRects[i].bottom - paRects[i].top));
564#endif /* Log */
565
566 /* Without this, Windows will not ask the miniport for its
567 * mode table but uses an internal cache instead.
568 */
569 DEVMODE tempDevMode;
570 RT_ZERO(tempDevMode);
571 tempDevMode.dmSize = sizeof(DEVMODE);
572 g_pfnEnumDisplaySettingsA(NULL, 0xffffff, &tempDevMode);
573
574 /* Assign the new rectangles to displays. */
575 for (i = 0; i < NumDevices; i++)
576 {
577 paDeviceModes[i].dmPosition.x = paRects[i].left;
578 paDeviceModes[i].dmPosition.y = paRects[i].top;
579 paDeviceModes[i].dmPelsWidth = paRects[i].right - paRects[i].left;
580 paDeviceModes[i].dmPelsHeight = paRects[i].bottom - paRects[i].top;
581
582 paDeviceModes[i].dmFields = DM_POSITION | DM_PELSHEIGHT | DM_PELSWIDTH;
583
584 if ( i == Id
585 && BitsPerPixel != 0)
586 {
587 paDeviceModes[i].dmFields |= DM_BITSPERPEL;
588 paDeviceModes[i].dmBitsPerPel = BitsPerPixel;
589 }
590 Log(("calling pfnChangeDisplaySettingsEx %p\n", RT_CB_LOG_CAST(g_pfnChangeDisplaySettingsExA)));
591 g_pfnChangeDisplaySettingsExA((LPSTR)paDisplayDevices[i].DeviceName,
592 &paDeviceModes[i], NULL, CDS_NORESET | CDS_UPDATEREGISTRY, NULL);
593 Log(("ChangeDisplaySettingsEx position err %d\n", GetLastError()));
594 }
595
596 /* A second call to ChangeDisplaySettings updates the monitor. */
597 LONG status = g_pfnChangeDisplaySettingsA(NULL, 0);
598 Log(("ChangeDisplaySettings update status %d\n", status));
599 if (status == DISP_CHANGE_SUCCESSFUL || status == DISP_CHANGE_BADMODE)
600 {
601 /* Successfully set new video mode or our driver can not set the requested mode. Stop trying. */
602 return FALSE;
603 }
604
605 /* Retry the request. */
606 return TRUE;
607}
608
609static DECLCALLBACK(RTEXITCODE) handleSetVideoMode(int argc, char *argv[])
610{
611 if (argc != 3 && argc != 4)
612 {
613 usage(SET_VIDEO_MODE);
614 return RTEXITCODE_FAILURE;
615 }
616
617 DWORD xres = RTStrToUInt32(argv[0]);
618 DWORD yres = RTStrToUInt32(argv[1]);
619 DWORD bpp = RTStrToUInt32(argv[2]);
620 DWORD scr = 0;
621 if (argc == 4)
622 scr = RTStrToUInt32(argv[3]);
623
624 HMODULE hmodUser = GetModuleHandle("user32.dll");
625 if (hmodUser)
626 {
627 /* ChangeDisplaySettingsExA was probably added in W2K, whereas ChangeDisplaySettingsA
628 and EnumDisplaySettingsA was added in NT 3.51. */
629 g_pfnChangeDisplaySettingsExA = (decltype(g_pfnChangeDisplaySettingsExA))GetProcAddress(hmodUser, "ChangeDisplaySettingsExA");
630 g_pfnChangeDisplaySettingsA = (decltype(g_pfnChangeDisplaySettingsA)) GetProcAddress(hmodUser, "ChangeDisplaySettingsA");
631 g_pfnEnumDisplaySettingsA = (decltype(g_pfnEnumDisplaySettingsA)) GetProcAddress(hmodUser, "EnumDisplaySettingsA");
632
633 Log(("VBoxService: g_pfnChangeDisplaySettingsExA=%p g_pfnChangeDisplaySettingsA=%p g_pfnEnumDisplaySettingsA=%p\n",
634 RT_CB_LOG_CAST(g_pfnChangeDisplaySettingsExA), RT_CB_LOG_CAST(g_pfnChangeDisplaySettingsA),
635 RT_CB_LOG_CAST(g_pfnEnumDisplaySettingsA)));
636
637 if ( g_pfnChangeDisplaySettingsExA
638 && g_pfnChangeDisplaySettingsA
639 && g_pfnEnumDisplaySettingsA)
640 {
641 /* The screen index is 0 based in the ResizeDisplayDevice call. */
642 scr = scr > 0 ? scr - 1 : 0;
643
644 /* Horizontal resolution must be a multiple of 8, round down. */
645 xres &= ~0x7;
646
647 RTPrintf("Setting resolution of display %d to %dx%dx%d ...", scr, xres, yres, bpp);
648 ResizeDisplayDevice(scr, xres, yres, bpp);
649 RTPrintf("done.\n");
650 }
651 else
652 VBoxControlError("Error retrieving API for display change!");
653 }
654 else
655 VBoxControlError("Error retrieving handle to user32.dll!");
656
657 return RTEXITCODE_SUCCESS;
658}
659
660static int checkVBoxVideoKey(HKEY hkeyVideo)
661{
662 RTUTF16 wszValue[128];
663 DWORD cbValue = sizeof(wszValue);
664 DWORD dwKeyType;
665 LONG status = RegQueryValueExW(hkeyVideo, L"Device Description", NULL, &dwKeyType, (LPBYTE)wszValue, &cbValue);
666 if (status == ERROR_SUCCESS)
667 {
668 /* WDDM has additional chars after "Adapter" */
669 static char s_szDeviceDescription[] = "VirtualBox Graphics Adapter";
670 wszValue[sizeof(s_szDeviceDescription) - 1] = '\0';
671 if (RTUtf16ICmpAscii(wszValue, s_szDeviceDescription) == 0)
672 return VINF_SUCCESS;
673 }
674
675 return VERR_NOT_FOUND;
676}
677
678static HKEY getVideoKey(bool writable)
679{
680 HKEY hkeyDeviceMap = 0;
681 LONG status = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "HARDWARE\\DEVICEMAP\\VIDEO", 0, KEY_READ, &hkeyDeviceMap);
682 if (status != ERROR_SUCCESS || !hkeyDeviceMap)
683 {
684 VBoxControlError("Error opening video device map registry key!\n");
685 return 0;
686 }
687
688 HKEY hkeyVideo = 0;
689 ULONG iDevice;
690 DWORD dwKeyType;
691
692 /*
693 * Scan all '\Device\VideoX' REG_SZ keys to find VBox video driver entry.
694 * 'ObjectNumberList' REG_BINARY is an array of 32 bit device indexes (X).
695 */
696
697 /* Get the 'ObjectNumberList' */
698 ULONG cDevices = 0;
699 DWORD adwObjectNumberList[256];
700 DWORD cbValue = sizeof(adwObjectNumberList);
701 status = RegQueryValueExA(hkeyDeviceMap, "ObjectNumberList", NULL, &dwKeyType, (LPBYTE)&adwObjectNumberList[0], &cbValue);
702
703 if ( status == ERROR_SUCCESS
704 && dwKeyType == REG_BINARY)
705 cDevices = cbValue / sizeof(DWORD);
706 else
707 {
708 /* The list might not exists. Use 'MaxObjectNumber' REG_DWORD and build a list. */
709 DWORD dwMaxObjectNumber = 0;
710 cbValue = sizeof(dwMaxObjectNumber);
711 status = RegQueryValueExA(hkeyDeviceMap, "MaxObjectNumber", NULL, &dwKeyType, (LPBYTE)&dwMaxObjectNumber, &cbValue);
712 if ( status == ERROR_SUCCESS
713 && dwKeyType == REG_DWORD)
714 {
715 /* 'MaxObjectNumber' is inclusive. */
716 cDevices = RT_MIN(dwMaxObjectNumber + 1, RT_ELEMENTS(adwObjectNumberList));
717 for (iDevice = 0; iDevice < cDevices; iDevice++)
718 adwObjectNumberList[iDevice] = iDevice;
719 }
720 }
721
722 if (cDevices == 0)
723 {
724 /* Always try '\Device\Video0' as the old code did. Enum can be used in this case in principle. */
725 adwObjectNumberList[0] = 0;
726 cDevices = 1;
727 }
728
729 /* Scan device entries */
730 for (iDevice = 0; iDevice < cDevices; iDevice++)
731 {
732 RTUTF16 wszValueName[64];
733 RTUtf16Printf(wszValueName, RT_ELEMENTS(wszValueName), "\\Device\\Video%u", adwObjectNumberList[iDevice]);
734
735 RTUTF16 wszVideoLocation[256];
736 cbValue = sizeof(wszVideoLocation);
737 status = RegQueryValueExW(hkeyDeviceMap, wszValueName, NULL, &dwKeyType, (LPBYTE)&wszVideoLocation[0], &cbValue);
738
739 /* This value starts with '\REGISTRY\Machine' */
740 if ( status == ERROR_SUCCESS
741 && dwKeyType == REG_SZ
742 && RTUtf16NICmpAscii(wszVideoLocation, RT_STR_TUPLE("\\REGISTRY\\Machine")) == 0)
743 {
744 status = RegOpenKeyExW(HKEY_LOCAL_MACHINE, &wszVideoLocation[18], 0,
745 KEY_READ | (writable ? KEY_WRITE : 0), &hkeyVideo);
746 if (status == ERROR_SUCCESS)
747 {
748 int rc = checkVBoxVideoKey(hkeyVideo);
749 if (RT_SUCCESS(rc))
750 {
751 /* Found, return hkeyVideo to the caller. */
752 break;
753 }
754
755 RegCloseKey(hkeyVideo);
756 hkeyVideo = 0;
757 }
758 }
759 }
760
761 if (hkeyVideo == 0)
762 {
763 VBoxControlError("Error opening video registry key!\n");
764 }
765
766 RegCloseKey(hkeyDeviceMap);
767 return hkeyVideo;
768}
769
770static DECLCALLBACK(RTEXITCODE) handleGetVideoAcceleration(int argc, char *argv[])
771{
772 RT_NOREF2(argc, argv);
773 ULONG status;
774 HKEY hkeyVideo = getVideoKey(false);
775
776 if (hkeyVideo)
777 {
778 /* query the actual value */
779 DWORD fAcceleration = 1;
780 DWORD cbValue = sizeof(fAcceleration);
781 DWORD dwKeyType;
782 status = RegQueryValueExA(hkeyVideo, "EnableVideoAccel", NULL, &dwKeyType, (LPBYTE)&fAcceleration, &cbValue);
783 if (status != ERROR_SUCCESS)
784 RTPrintf("Video acceleration: default\n");
785 else
786 RTPrintf("Video acceleration: %s\n", fAcceleration ? "on" : "off");
787 RegCloseKey(hkeyVideo);
788 }
789 return RTEXITCODE_SUCCESS;
790}
791
792static DECLCALLBACK(RTEXITCODE) handleSetVideoAcceleration(int argc, char *argv[])
793{
794 ULONG status;
795 HKEY hkeyVideo;
796
797 /* must have exactly one argument: the new offset */
798 if ( (argc != 1)
799 || ( RTStrICmp(argv[0], "on")
800 && RTStrICmp(argv[0], "off")))
801 {
802 usage(SET_VIDEO_ACCEL);
803 return RTEXITCODE_FAILURE;
804 }
805
806 hkeyVideo = getVideoKey(true);
807
808 if (hkeyVideo)
809 {
810 int fAccel = 0;
811 if (RTStrICmp(argv[0], "on") == 0)
812 fAccel = 1;
813 /* set a new value */
814 status = RegSetValueExA(hkeyVideo, "EnableVideoAccel", 0, REG_DWORD, (LPBYTE)&fAccel, sizeof(fAccel));
815 if (status != ERROR_SUCCESS)
816 {
817 VBoxControlError("Error %d writing video acceleration status!\n", status);
818 }
819 RegCloseKey(hkeyVideo);
820 }
821 return RTEXITCODE_SUCCESS;
822}
823
824static DECLCALLBACK(RTEXITCODE) videoFlagsGet(void)
825{
826 HKEY hkeyVideo = getVideoKey(false);
827
828 if (hkeyVideo)
829 {
830 DWORD dwFlags = 0;
831 DWORD cbValue = sizeof(dwFlags);
832 DWORD dwKeyType;
833 ULONG status = RegQueryValueExA(hkeyVideo, "VBoxVideoFlags", NULL, &dwKeyType, (LPBYTE)&dwFlags, &cbValue);
834 if (status != ERROR_SUCCESS)
835 RTPrintf("Video flags: default\n");
836 else
837 RTPrintf("Video flags: 0x%08X\n", dwFlags);
838 RegCloseKey(hkeyVideo);
839 return RTEXITCODE_SUCCESS;
840 }
841
842 return RTEXITCODE_FAILURE;
843}
844
845static DECLCALLBACK(RTEXITCODE) videoFlagsDelete(void)
846{
847 HKEY hkeyVideo = getVideoKey(true);
848
849 if (hkeyVideo)
850 {
851 ULONG status = RegDeleteValueA(hkeyVideo, "VBoxVideoFlags");
852 if (status != ERROR_SUCCESS)
853 VBoxControlError("Error %d deleting video flags.\n", status);
854 RegCloseKey(hkeyVideo);
855 return RTEXITCODE_SUCCESS;
856 }
857
858 return RTEXITCODE_FAILURE;
859}
860
861static DECLCALLBACK(RTEXITCODE) videoFlagsModify(bool fSet, int argc, char *argv[])
862{
863 if (argc != 1)
864 {
865 VBoxControlError("Mask required.\n");
866 return RTEXITCODE_FAILURE;
867 }
868
869 uint32_t u32Mask = 0;
870 int rc = RTStrToUInt32Full(argv[0], 16, &u32Mask);
871 if (RT_FAILURE(rc))
872 {
873 VBoxControlError("Invalid video flags mask.\n");
874 return RTEXITCODE_FAILURE;
875 }
876
877 RTEXITCODE exitCode = RTEXITCODE_SUCCESS;
878
879 HKEY hkeyVideo = getVideoKey(true);
880 if (hkeyVideo)
881 {
882 DWORD dwFlags = 0;
883 DWORD cbValue = sizeof(dwFlags);
884 DWORD dwKeyType;
885 ULONG status = RegQueryValueExA(hkeyVideo, "VBoxVideoFlags", NULL, &dwKeyType, (LPBYTE)&dwFlags, &cbValue);
886 if (status != ERROR_SUCCESS)
887 dwFlags = 0;
888
889 dwFlags = fSet ? dwFlags | u32Mask : dwFlags & ~u32Mask;
890
891 status = RegSetValueExA(hkeyVideo, "VBoxVideoFlags", 0, REG_DWORD, (LPBYTE)&dwFlags, sizeof(dwFlags));
892 if (status != ERROR_SUCCESS)
893 {
894 VBoxControlError("Error %d writing video flags.\n", status);
895 exitCode = RTEXITCODE_FAILURE;
896 }
897
898 RegCloseKey(hkeyVideo);
899 }
900 else
901 {
902 exitCode = RTEXITCODE_FAILURE;
903 }
904
905 return exitCode;
906}
907
908static DECLCALLBACK(RTEXITCODE) handleVideoFlags(int argc, char *argv[])
909{
910 /* Must have a keyword and optional value (32 bit hex string). */
911 if (argc != 1 && argc != 2)
912 {
913 VBoxControlError("Invalid number of arguments.\n");
914 usage(VIDEO_FLAGS);
915 return RTEXITCODE_FAILURE;
916 }
917
918 RTEXITCODE exitCode = RTEXITCODE_SUCCESS;
919
920 if (RTStrICmp(argv[0], "get") == 0)
921 {
922 exitCode = videoFlagsGet();
923 }
924 else if (RTStrICmp(argv[0], "delete") == 0)
925 {
926 exitCode = videoFlagsDelete();
927 }
928 else if (RTStrICmp(argv[0], "set") == 0)
929 {
930 exitCode = videoFlagsModify(true, argc - 1, &argv[1]);
931 }
932 else if (RTStrICmp(argv[0], "clear") == 0)
933 {
934 exitCode = videoFlagsModify(false, argc - 1, &argv[1]);
935 }
936 else
937 {
938 VBoxControlError("Invalid command.\n");
939 exitCode = RTEXITCODE_FAILURE;
940 }
941
942 if (exitCode != RTEXITCODE_SUCCESS)
943 {
944 usage(VIDEO_FLAGS);
945 }
946
947 return exitCode;
948}
949
950#define MAX_CUSTOM_MODES 128
951
952/* the table of custom modes */
953struct
954{
955 DWORD xres;
956 DWORD yres;
957 DWORD bpp;
958} customModes[MAX_CUSTOM_MODES] = {{0}};
959
960void getCustomModes(HKEY hkeyVideo)
961{
962 ULONG status;
963 int curMode = 0;
964
965 /* null out the table */
966 RT_ZERO(customModes);
967
968 do
969 {
970 char valueName[20];
971 DWORD xres, yres, bpp = 0;
972 DWORD dwType;
973 DWORD dwLen = sizeof(DWORD);
974
975 RTStrPrintf(valueName, sizeof(valueName), "CustomMode%dWidth", curMode);
976 status = RegQueryValueExA(hkeyVideo, valueName, NULL, &dwType, (LPBYTE)&xres, &dwLen);
977 if (status != ERROR_SUCCESS)
978 break;
979 RTStrPrintf(valueName, sizeof(valueName), "CustomMode%dHeight", curMode);
980 status = RegQueryValueExA(hkeyVideo, valueName, NULL, &dwType, (LPBYTE)&yres, &dwLen);
981 if (status != ERROR_SUCCESS)
982 break;
983 RTStrPrintf(valueName, sizeof(valueName), "CustomMode%dBPP", curMode);
984 status = RegQueryValueExA(hkeyVideo, valueName, NULL, &dwType, (LPBYTE)&bpp, &dwLen);
985 if (status != ERROR_SUCCESS)
986 break;
987
988 /* check if the mode is OK */
989 if ( (xres > (1 << 16))
990 || (yres > (1 << 16))
991 || ( (bpp != 16)
992 && (bpp != 24)
993 && (bpp != 32)))
994 break;
995
996 /* add mode to table */
997 customModes[curMode].xres = xres;
998 customModes[curMode].yres = yres;
999 customModes[curMode].bpp = bpp;
1000
1001 ++curMode;
1002
1003 if (curMode >= MAX_CUSTOM_MODES)
1004 break;
1005 } while(1);
1006}
1007
1008void writeCustomModes(HKEY hkeyVideo)
1009{
1010 ULONG status;
1011 int tableIndex = 0;
1012 int modeIndex = 0;
1013
1014 /* first remove all values */
1015 for (int i = 0; i < MAX_CUSTOM_MODES; i++)
1016 {
1017 char valueName[20];
1018 RTStrPrintf(valueName, sizeof(valueName), "CustomMode%dWidth", i);
1019 RegDeleteValueA(hkeyVideo, valueName);
1020 RTStrPrintf(valueName, sizeof(valueName), "CustomMode%dHeight", i);
1021 RegDeleteValueA(hkeyVideo, valueName);
1022 RTStrPrintf(valueName, sizeof(valueName), "CustomMode%dBPP", i);
1023 RegDeleteValueA(hkeyVideo, valueName);
1024 }
1025
1026 do
1027 {
1028 if (tableIndex >= MAX_CUSTOM_MODES)
1029 break;
1030
1031 /* is the table entry present? */
1032 if ( (!customModes[tableIndex].xres)
1033 || (!customModes[tableIndex].yres)
1034 || (!customModes[tableIndex].bpp))
1035 {
1036 tableIndex++;
1037 continue;
1038 }
1039
1040 RTPrintf("writing mode %d (%dx%dx%d)\n", modeIndex, customModes[tableIndex].xres, customModes[tableIndex].yres, customModes[tableIndex].bpp);
1041 char valueName[20];
1042 RTStrPrintf(valueName, sizeof(valueName), "CustomMode%dWidth", modeIndex);
1043 status = RegSetValueExA(hkeyVideo, valueName, 0, REG_DWORD, (LPBYTE)&customModes[tableIndex].xres,
1044 sizeof(customModes[tableIndex].xres));
1045 RTStrPrintf(valueName, sizeof(valueName), "CustomMode%dHeight", modeIndex);
1046 RegSetValueExA(hkeyVideo, valueName, 0, REG_DWORD, (LPBYTE)&customModes[tableIndex].yres,
1047 sizeof(customModes[tableIndex].yres));
1048 RTStrPrintf(valueName, sizeof(valueName), "CustomMode%dBPP", modeIndex);
1049 RegSetValueExA(hkeyVideo, valueName, 0, REG_DWORD, (LPBYTE)&customModes[tableIndex].bpp,
1050 sizeof(customModes[tableIndex].bpp));
1051
1052 modeIndex++;
1053 tableIndex++;
1054
1055 } while(1);
1056
1057}
1058
1059static DECLCALLBACK(RTEXITCODE) handleListCustomModes(int argc, char *argv[])
1060{
1061 RT_NOREF1(argv);
1062 if (argc != 0)
1063 {
1064 usage(LIST_CUST_MODES);
1065 return RTEXITCODE_FAILURE;
1066 }
1067
1068 HKEY hkeyVideo = getVideoKey(false);
1069
1070 if (hkeyVideo)
1071 {
1072 getCustomModes(hkeyVideo);
1073 for (int i = 0; i < (sizeof(customModes) / sizeof(customModes[0])); i++)
1074 {
1075 if ( !customModes[i].xres
1076 || !customModes[i].yres
1077 || !customModes[i].bpp)
1078 continue;
1079
1080 RTPrintf("Mode: %d x %d x %d\n",
1081 customModes[i].xres, customModes[i].yres, customModes[i].bpp);
1082 }
1083 RegCloseKey(hkeyVideo);
1084 }
1085 return RTEXITCODE_SUCCESS;
1086}
1087
1088static DECLCALLBACK(RTEXITCODE) handleAddCustomMode(int argc, char *argv[])
1089{
1090 if (argc != 3)
1091 {
1092 usage(ADD_CUST_MODE);
1093 return RTEXITCODE_FAILURE;
1094 }
1095
1096 DWORD xres = RTStrToUInt32(argv[0]);
1097 DWORD yres = RTStrToUInt32(argv[1]);
1098 DWORD bpp = RTStrToUInt32(argv[2]);
1099
1100 /** @todo better check including xres mod 8 = 0! */
1101 if ( (xres > (1 << 16))
1102 || (yres > (1 << 16))
1103 || ( (bpp != 16)
1104 && (bpp != 24)
1105 && (bpp != 32)))
1106 {
1107 VBoxControlError("invalid mode specified!\n");
1108 return RTEXITCODE_FAILURE;
1109 }
1110
1111 HKEY hkeyVideo = getVideoKey(true);
1112
1113 if (hkeyVideo)
1114 {
1115 int i;
1116 int fModeExists = 0;
1117 getCustomModes(hkeyVideo);
1118 for (i = 0; i < MAX_CUSTOM_MODES; i++)
1119 {
1120 /* mode exists? */
1121 if ( customModes[i].xres == xres
1122 && customModes[i].yres == yres
1123 && customModes[i].bpp == bpp
1124 )
1125 {
1126 fModeExists = 1;
1127 }
1128 }
1129 if (!fModeExists)
1130 {
1131 for (i = 0; i < MAX_CUSTOM_MODES; i++)
1132 {
1133 /* item free? */
1134 if (!customModes[i].xres)
1135 {
1136 customModes[i].xres = xres;
1137 customModes[i].yres = yres;
1138 customModes[i].bpp = bpp;
1139 break;
1140 }
1141 }
1142 writeCustomModes(hkeyVideo);
1143 }
1144 RegCloseKey(hkeyVideo);
1145 }
1146 return RTEXITCODE_SUCCESS;
1147}
1148
1149static DECLCALLBACK(RTEXITCODE) handleRemoveCustomMode(int argc, char *argv[])
1150{
1151 if (argc != 3)
1152 {
1153 usage(REMOVE_CUST_MODE);
1154 return RTEXITCODE_FAILURE;
1155 }
1156
1157 DWORD xres = RTStrToUInt32(argv[0]);
1158 DWORD yres = RTStrToUInt32(argv[1]);
1159 DWORD bpp = RTStrToUInt32(argv[2]);
1160
1161 HKEY hkeyVideo = getVideoKey(true);
1162
1163 if (hkeyVideo)
1164 {
1165 getCustomModes(hkeyVideo);
1166 for (int i = 0; i < MAX_CUSTOM_MODES; i++)
1167 {
1168 /* correct item? */
1169 if ( (customModes[i].xres == xres)
1170 && (customModes[i].yres == yres)
1171 && (customModes[i].bpp == bpp))
1172 {
1173 RTPrintf("found mode at index %d\n", i);
1174 RT_ZERO(customModes[i]);
1175 break;
1176 }
1177 }
1178 writeCustomModes(hkeyVideo);
1179 RegCloseKey(hkeyVideo);
1180 }
1181
1182 return RTEXITCODE_SUCCESS;
1183}
1184
1185#endif /* RT_OS_WINDOWS */
1186
1187#ifdef VBOX_WITH_GUEST_PROPS
1188/**
1189 * Retrieves a value from the guest property store.
1190 * This is accessed through the "VBoxGuestPropSvc" HGCM service.
1191 *
1192 * @returns Command exit code.
1193 * @note see the command line API description for parameters
1194 */
1195static RTEXITCODE getGuestProperty(int argc, char **argv)
1196{
1197 bool fVerbose = false;
1198 if ( argc == 2
1199 && ( strcmp(argv[1], "-verbose") == 0
1200 || strcmp(argv[1], "--verbose") == 0)
1201 )
1202 fVerbose = true;
1203 else if (argc != 1)
1204 {
1205 usage(GUEST_PROP);
1206 return RTEXITCODE_FAILURE;
1207 }
1208
1209 uint32_t u32ClientId = 0;
1210 int rc = VINF_SUCCESS;
1211
1212 rc = VbglR3GuestPropConnect(&u32ClientId);
1213 if (RT_FAILURE(rc))
1214 VBoxControlError("Failed to connect to the guest property service, error %Rrc\n", rc);
1215
1216 /*
1217 * Here we actually retrieve the value from the host.
1218 */
1219 const char *pszName = argv[0];
1220 char *pszValue = NULL;
1221 uint64_t u64Timestamp = 0;
1222 char *pszFlags = NULL;
1223 /* The buffer for storing the data and its initial size. We leave a bit
1224 * of space here in case the maximum values are raised. */
1225 void *pvBuf = NULL;
1226 uint32_t cbBuf = GUEST_PROP_MAX_VALUE_LEN + GUEST_PROP_MAX_FLAGS_LEN + 1024;
1227 if (RT_SUCCESS(rc))
1228 {
1229 /* Because there is a race condition between our reading the size of a
1230 * property and the guest updating it, we loop a few times here and
1231 * hope. Actually this should never go wrong, as we are generous
1232 * enough with buffer space. */
1233 bool fFinished = false;
1234 for (unsigned i = 0; i < 10 && !fFinished; ++i)
1235 {
1236 void *pvTmpBuf = RTMemRealloc(pvBuf, cbBuf);
1237 if (NULL == pvTmpBuf)
1238 {
1239 rc = VERR_NO_MEMORY;
1240 VBoxControlError("Out of memory\n");
1241 }
1242 else
1243 {
1244 pvBuf = pvTmpBuf;
1245 rc = VbglR3GuestPropRead(u32ClientId, pszName, pvBuf, cbBuf,
1246 &pszValue, &u64Timestamp, &pszFlags,
1247 &cbBuf);
1248 }
1249 if (VERR_BUFFER_OVERFLOW == rc)
1250 /* Leave a bit of extra space to be safe */
1251 cbBuf += 1024;
1252 else
1253 fFinished = true;
1254 }
1255 if (VERR_TOO_MUCH_DATA == rc)
1256 VBoxControlError("Temporarily unable to retrieve the property\n");
1257 else if (RT_FAILURE(rc) && rc != VERR_NOT_FOUND)
1258 VBoxControlError("Failed to retrieve the property value, error %Rrc\n", rc);
1259 }
1260
1261 /*
1262 * And display it on the guest console.
1263 */
1264 if (VERR_NOT_FOUND == rc)
1265 RTPrintf("No value set!\n");
1266 else if (RT_SUCCESS(rc))
1267 {
1268 RTPrintf("Value: %s\n", pszValue);
1269 if (fVerbose)
1270 {
1271 RTPrintf("Timestamp: %lld ns\n", u64Timestamp);
1272 RTPrintf("Flags: %s\n", pszFlags);
1273 }
1274 }
1275
1276 if (u32ClientId != 0)
1277 VbglR3GuestPropDisconnect(u32ClientId);
1278 RTMemFree(pvBuf);
1279 return RT_SUCCESS(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
1280}
1281
1282
1283/**
1284 * Writes a value to the guest property store.
1285 * This is accessed through the "VBoxGuestPropSvc" HGCM service.
1286 *
1287 * @returns Command exit code.
1288 * @note see the command line API description for parameters
1289 */
1290static RTEXITCODE setGuestProperty(int argc, char *argv[])
1291{
1292 /*
1293 * Check the syntax. We can deduce the correct syntax from the number of
1294 * arguments.
1295 */
1296 bool fUsageOK = true;
1297 const char *pszName = NULL;
1298 const char *pszValue = NULL;
1299 const char *pszFlags = NULL;
1300 if (2 == argc)
1301 {
1302 pszValue = argv[1];
1303 }
1304 else if (3 == argc)
1305 fUsageOK = false;
1306 else if (4 == argc)
1307 {
1308 pszValue = argv[1];
1309 if ( strcmp(argv[2], "-flags") != 0
1310 && strcmp(argv[2], "--flags") != 0)
1311 fUsageOK = false;
1312 pszFlags = argv[3];
1313 }
1314 else if (argc != 1)
1315 fUsageOK = false;
1316 if (!fUsageOK)
1317 {
1318 usage(GUEST_PROP);
1319 return RTEXITCODE_FAILURE;
1320 }
1321 /* This is always needed. */
1322 pszName = argv[0];
1323
1324 /*
1325 * Do the actual setting.
1326 */
1327 uint32_t u32ClientId = 0;
1328 int rc = VINF_SUCCESS;
1329 rc = VbglR3GuestPropConnect(&u32ClientId);
1330 if (RT_FAILURE(rc))
1331 VBoxControlError("Failed to connect to the guest property service, error %Rrc\n", rc);
1332 else
1333 {
1334 if (pszFlags != NULL)
1335 rc = VbglR3GuestPropWrite(u32ClientId, pszName, pszValue, pszFlags);
1336 else
1337 rc = VbglR3GuestPropWriteValue(u32ClientId, pszName, pszValue);
1338 if (RT_FAILURE(rc))
1339 VBoxControlError("Failed to store the property value, error %Rrc\n", rc);
1340 }
1341
1342 if (u32ClientId != 0)
1343 VbglR3GuestPropDisconnect(u32ClientId);
1344 return RT_SUCCESS(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
1345}
1346
1347
1348/**
1349 * Deletes a guest property from the guest property store.
1350 * This is accessed through the "VBoxGuestPropSvc" HGCM service.
1351 *
1352 * @returns Command exit code.
1353 * @note see the command line API description for parameters
1354 */
1355static RTEXITCODE deleteGuestProperty(int argc, char *argv[])
1356{
1357 /*
1358 * Check the syntax. We can deduce the correct syntax from the number of
1359 * arguments.
1360 */
1361 bool fUsageOK = true;
1362 const char *pszName = NULL;
1363 if (argc < 1)
1364 fUsageOK = false;
1365 if (!fUsageOK)
1366 {
1367 usage(GUEST_PROP);
1368 return RTEXITCODE_FAILURE;
1369 }
1370 /* This is always needed. */
1371 pszName = argv[0];
1372
1373 /*
1374 * Do the actual setting.
1375 */
1376 uint32_t u32ClientId = 0;
1377 int rc = VbglR3GuestPropConnect(&u32ClientId);
1378 if (RT_FAILURE(rc))
1379 VBoxControlError("Failed to connect to the guest property service, error %Rrc\n", rc);
1380 else
1381 {
1382 rc = VbglR3GuestPropDelete(u32ClientId, pszName);
1383 if (RT_FAILURE(rc))
1384 VBoxControlError("Failed to delete the property value, error %Rrc\n", rc);
1385 }
1386
1387 if (u32ClientId != 0)
1388 VbglR3GuestPropDisconnect(u32ClientId);
1389 return RT_SUCCESS(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
1390}
1391
1392
1393/**
1394 * Enumerates the properties in the guest property store.
1395 * This is accessed through the "VBoxGuestPropSvc" HGCM service.
1396 *
1397 * @returns Command exit code.
1398 * @note see the command line API description for parameters
1399 */
1400static RTEXITCODE enumGuestProperty(int argc, char *argv[])
1401{
1402 /*
1403 * Check the syntax. We can deduce the correct syntax from the number of
1404 * arguments.
1405 */
1406 char const * const *papszPatterns = NULL;
1407 uint32_t cPatterns = 0;
1408 if ( argc > 1
1409 && ( strcmp(argv[0], "-patterns") == 0
1410 || strcmp(argv[0], "--patterns") == 0))
1411 {
1412 papszPatterns = (char const * const *)&argv[1];
1413 cPatterns = argc - 1;
1414 }
1415 else if (argc != 0)
1416 {
1417 usage(GUEST_PROP);
1418 return RTEXITCODE_FAILURE;
1419 }
1420
1421 /*
1422 * Do the actual enumeration.
1423 */
1424 uint32_t u32ClientId = 0;
1425 int rc = VbglR3GuestPropConnect(&u32ClientId);
1426 if (RT_SUCCESS(rc))
1427 {
1428 PVBGLR3GUESTPROPENUM pHandle;
1429 const char *pszName, *pszValue, *pszFlags;
1430 uint64_t u64Timestamp;
1431
1432 rc = VbglR3GuestPropEnum(u32ClientId, papszPatterns, cPatterns, &pHandle,
1433 &pszName, &pszValue, &u64Timestamp, &pszFlags);
1434 if (RT_SUCCESS(rc))
1435 {
1436 while (RT_SUCCESS(rc) && pszName)
1437 {
1438 RTPrintf("Name: %s, value: %s, timestamp: %lld, flags: %s\n",
1439 pszName, pszValue ? pszValue : "", u64Timestamp, pszFlags);
1440
1441 rc = VbglR3GuestPropEnumNext(pHandle, &pszName, &pszValue, &u64Timestamp, &pszFlags);
1442 if (RT_FAILURE(rc))
1443 VBoxControlError("Error while enumerating guest properties: %Rrc\n", rc);
1444 }
1445
1446 VbglR3GuestPropEnumFree(pHandle);
1447 }
1448 else if (VERR_NOT_FOUND == rc)
1449 RTPrintf("No properties found.\n");
1450 else
1451 VBoxControlError("Failed to enumerate the guest properties! Error: %Rrc\n", rc);
1452 VbglR3GuestPropDisconnect(u32ClientId);
1453 }
1454 else
1455 VBoxControlError("Failed to connect to the guest property service! Error: %Rrc\n", rc);
1456 return RT_SUCCESS(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
1457}
1458
1459
1460/**
1461 * Waits for notifications of changes to guest properties.
1462 * This is accessed through the "VBoxGuestPropSvc" HGCM service.
1463 *
1464 * @returns Command exit code.
1465 * @note see the command line API description for parameters
1466 */
1467static RTEXITCODE waitGuestProperty(int argc, char **argv)
1468{
1469 /*
1470 * Handle arguments
1471 */
1472 const char *pszPatterns = NULL;
1473 uint64_t u64TimestampIn = 0;
1474 uint32_t u32Timeout = RT_INDEFINITE_WAIT;
1475 bool fUsageOK = true;
1476 if (argc < 1)
1477 fUsageOK = false;
1478 pszPatterns = argv[0];
1479 for (int i = 1; fUsageOK && i < argc; ++i)
1480 {
1481 if ( strcmp(argv[i], "-timeout") == 0
1482 || strcmp(argv[i], "--timeout") == 0)
1483 {
1484 if ( i + 1 >= argc
1485 || RTStrToUInt32Full(argv[i + 1], 10, &u32Timeout)
1486 != VINF_SUCCESS
1487 )
1488 fUsageOK = false;
1489 else
1490 ++i;
1491 }
1492 else if ( strcmp(argv[i], "-timestamp") == 0
1493 || strcmp(argv[i], "--timestamp") == 0)
1494 {
1495 if ( i + 1 >= argc
1496 || RTStrToUInt64Full(argv[i + 1], 10, &u64TimestampIn)
1497 != VINF_SUCCESS
1498 )
1499 fUsageOK = false;
1500 else
1501 ++i;
1502 }
1503 else
1504 fUsageOK = false;
1505 }
1506 if (!fUsageOK)
1507 {
1508 usage(GUEST_PROP);
1509 return RTEXITCODE_FAILURE;
1510 }
1511
1512 /*
1513 * Connect to the service
1514 */
1515 uint32_t u32ClientId = 0;
1516 int rc = VbglR3GuestPropConnect(&u32ClientId);
1517 if (RT_FAILURE(rc))
1518 VBoxControlError("Failed to connect to the guest property service, error %Rrc\n", rc);
1519
1520 /*
1521 * Retrieve the notification from the host
1522 */
1523 char *pszName = NULL;
1524 char *pszValue = NULL;
1525 uint64_t u64TimestampOut = 0;
1526 char *pszFlags = NULL;
1527 bool fWasDeleted = false;
1528 /* The buffer for storing the data and its initial size. We leave a bit
1529 * of space here in case the maximum values are raised. */
1530 void *pvBuf = NULL;
1531 uint32_t cbBuf = GUEST_PROP_MAX_NAME_LEN + GUEST_PROP_MAX_VALUE_LEN + GUEST_PROP_MAX_FLAGS_LEN + _1K;
1532 /* Because there is a race condition between our reading the size of a
1533 * property and the guest updating it, we loop a few times here and
1534 * hope. Actually this should never go wrong, as we are generous
1535 * enough with buffer space. */
1536 for (unsigned iTry = 0; ; iTry++)
1537 {
1538 pvBuf = RTMemRealloc(pvBuf, cbBuf);
1539 if (pvBuf != NULL)
1540 {
1541 rc = VbglR3GuestPropWait(u32ClientId, pszPatterns, pvBuf, cbBuf,
1542 u64TimestampIn, u32Timeout,
1543 &pszName, &pszValue, &u64TimestampOut,
1544 &pszFlags, &cbBuf, &fWasDeleted);
1545 if (rc == VERR_BUFFER_OVERFLOW && iTry < 10)
1546 {
1547 cbBuf += _1K; /* Add a bit of extra space to be on the safe side. */
1548 continue;
1549 }
1550 if (rc == VERR_TOO_MUCH_DATA)
1551 VBoxControlError("Temporarily unable to get a notification\n");
1552 else if (rc == VERR_INTERRUPTED)
1553 VBoxControlError("The request timed out or was interrupted\n");
1554 else if (RT_FAILURE(rc) && rc != VERR_NOT_FOUND)
1555 VBoxControlError("Failed to get a notification, error %Rrc\n", rc);
1556 }
1557 else
1558 {
1559 VBoxControlError("Out of memory\n");
1560 rc = VERR_NO_MEMORY;
1561 }
1562 break;
1563 }
1564
1565 /*
1566 * And display it on the guest console.
1567 */
1568 if (VERR_NOT_FOUND == rc)
1569 RTPrintf("No value set!\n");
1570 else if (rc == VERR_BUFFER_OVERFLOW)
1571 RTPrintf("Internal error: unable to determine the size of the data!\n");
1572 else if (RT_SUCCESS(rc))
1573 {
1574 if (fWasDeleted)
1575 {
1576 RTPrintf("Property %s was deleted\n", pszName);
1577 }
1578 else
1579 {
1580 RTPrintf("Name: %s\n", pszName);
1581 RTPrintf("Value: %s\n", pszValue);
1582 RTPrintf("Timestamp: %lld ns\n", u64TimestampOut);
1583 RTPrintf("Flags: %s\n", pszFlags);
1584 }
1585 }
1586
1587 if (u32ClientId != 0)
1588 VbglR3GuestPropDisconnect(u32ClientId);
1589 RTMemFree(pvBuf);
1590 return RT_SUCCESS(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
1591}
1592
1593
1594/**
1595 * Access the guest property store through the "VBoxGuestPropSvc" HGCM
1596 * service.
1597 *
1598 * @returns 0 on success, 1 on failure
1599 * @note see the command line API description for parameters
1600 */
1601static DECLCALLBACK(RTEXITCODE) handleGuestProperty(int argc, char *argv[])
1602{
1603 if (argc == 0)
1604 {
1605 usage(GUEST_PROP);
1606 return RTEXITCODE_FAILURE;
1607 }
1608 if (!strcmp(argv[0], "get"))
1609 return getGuestProperty(argc - 1, argv + 1);
1610 if (!strcmp(argv[0], "set"))
1611 return setGuestProperty(argc - 1, argv + 1);
1612 if (!strcmp(argv[0], "delete") || !strcmp(argv[0], "unset"))
1613 return deleteGuestProperty(argc - 1, argv + 1);
1614 if (!strcmp(argv[0], "enumerate"))
1615 return enumGuestProperty(argc - 1, argv + 1);
1616 if (!strcmp(argv[0], "wait"))
1617 return waitGuestProperty(argc - 1, argv + 1);
1618 /* unknown cmd */
1619 usage(GUEST_PROP);
1620 return RTEXITCODE_FAILURE;
1621}
1622
1623#endif
1624#ifdef VBOX_WITH_SHARED_FOLDERS
1625
1626/**
1627 * Lists the Shared Folders provided by the host.
1628 */
1629static RTEXITCODE sharedFolder_list(int argc, char **argv)
1630{
1631 bool fUsageOK = true;
1632 bool fOnlyShowAutoMount = false;
1633 if (argc == 1)
1634 {
1635 if (!strcmp(argv[0], "--automount"))
1636 fOnlyShowAutoMount = true;
1637 else
1638 fUsageOK = false;
1639 }
1640 else if (argc > 1)
1641 fUsageOK = false;
1642 if (!fUsageOK)
1643 {
1644 usage(GUEST_SHAREDFOLDERS);
1645 return RTEXITCODE_SYNTAX;
1646 }
1647
1648 uint32_t u32ClientId;
1649 int rc = VbglR3SharedFolderConnect(&u32ClientId);
1650 if (RT_FAILURE(rc))
1651 VBoxControlError("Failed to connect to the shared folder service, error %Rrc\n", rc);
1652 else
1653 {
1654 PVBGLR3SHAREDFOLDERMAPPING paMappings;
1655 uint32_t cMappings;
1656 rc = VbglR3SharedFolderGetMappings(u32ClientId, fOnlyShowAutoMount, &paMappings, &cMappings);
1657 if (RT_SUCCESS(rc))
1658 {
1659 if (fOnlyShowAutoMount)
1660 RTPrintf("Auto-mounted Shared Folder mappings (%u):\n\n", cMappings);
1661 else
1662 RTPrintf("Shared Folder mappings (%u):\n\n", cMappings);
1663
1664 for (uint32_t i = 0; i < cMappings; i++)
1665 {
1666 char *pszName;
1667 char *pszMntPt;
1668 uint64_t fFlags;
1669 uint32_t uRootIdVer;
1670 rc = VbglR3SharedFolderQueryFolderInfo(u32ClientId, paMappings[i].u32Root, 0,
1671 &pszName, &pszMntPt, &fFlags, &uRootIdVer);
1672 if (RT_SUCCESS(rc))
1673 {
1674 RTPrintf("%02u - %s [idRoot=%u", i + 1, pszName, paMappings[i].u32Root);
1675 if (fFlags & SHFL_MIF_WRITABLE)
1676 RTPrintf(" writable");
1677 else
1678 RTPrintf(" readonly");
1679 if (fFlags & SHFL_MIF_AUTO_MOUNT)
1680 RTPrintf(" auto-mount");
1681 if (fFlags & SHFL_MIF_SYMLINK_CREATION)
1682 RTPrintf(" create-symlink");
1683 if (fFlags & SHFL_MIF_HOST_ICASE)
1684 RTPrintf(" host-icase");
1685 if (fFlags & SHFL_MIF_GUEST_ICASE)
1686 RTPrintf(" guest-icase");
1687 if (*pszMntPt)
1688 RTPrintf(" mnt-pt=%s", pszMntPt);
1689 RTPrintf("]");
1690# ifdef RT_OS_OS2
1691 /* Show drive letters: */
1692 const char *pszOn = " on";
1693 for (char chDrive = 'A'; chDrive <= 'Z'; chDrive++)
1694 {
1695 char szDrive[4] = { chDrive, ':', '\0', '\0' };
1696 union
1697 {
1698 FSQBUFFER2 FsQueryBuf;
1699 char achPadding[512];
1700 } uBuf;
1701 RT_ZERO(uBuf);
1702 ULONG cbBuf = sizeof(uBuf) - 2;
1703 APIRET rcOs2 = DosQueryFSAttach(szDrive, 0, FSAIL_QUERYNAME, &uBuf.FsQueryBuf, &cbBuf);
1704 if (rcOs2 == NO_ERROR)
1705 {
1706 const char *pszFsdName = (const char *)&uBuf.FsQueryBuf.szName[uBuf.FsQueryBuf.cbName + 1];
1707 if ( uBuf.FsQueryBuf.iType == FSAT_REMOTEDRV
1708 && RTStrICmpAscii(pszFsdName, "VBOXSF") == 0)
1709 {
1710 const char *pszMountedName = (const char *)&pszFsdName[uBuf.FsQueryBuf.cbFSDName + 1];
1711 if (RTStrICmp(pszMountedName, pszName) == 0)
1712 {
1713 const char *pszTag = pszMountedName + strlen(pszMountedName) + 1; /* safe */
1714 if (*pszTag != '\0')
1715 RTPrintf("%s %s (%s)", pszOn, szDrive, pszTag);
1716 else
1717 RTPrintf("%s %s", pszOn, szDrive);
1718 pszOn = ",";
1719 }
1720 }
1721 }
1722 }
1723# endif
1724 RTPrintf("\n");
1725
1726 RTStrFree(pszName);
1727 RTStrFree(pszMntPt);
1728 }
1729 else
1730 VBoxControlError("Error while getting the shared folder name for root node = %u, rc = %Rrc\n",
1731 paMappings[i].u32Root, rc);
1732 }
1733 if (!cMappings)
1734 RTPrintf("No Shared Folders available.\n");
1735 VbglR3SharedFolderFreeMappings(paMappings);
1736 }
1737 else
1738 VBoxControlError("Error while getting the shared folder mappings, rc = %Rrc\n", rc);
1739 VbglR3SharedFolderDisconnect(u32ClientId);
1740 }
1741 return RT_SUCCESS(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
1742}
1743
1744# ifdef RT_OS_OS2
1745/**
1746 * Attaches a shared folder to a drive letter.
1747 */
1748static RTEXITCODE sharedFolder_use(int argc, char **argv)
1749{
1750 /*
1751 * Takes a drive letter and a share name as arguments.
1752 */
1753 if (argc != 2)
1754 return VBoxControlSyntaxError("sharedfolder use: expected a drive letter and a shared folder name\n");
1755
1756 const char *pszDrive = argv[0];
1757 if (!RT_C_IS_ALPHA(pszDrive[0]) || pszDrive[1] != ':' || pszDrive[2] != '\0')
1758 return VBoxControlSyntaxError("sharedfolder use: not a drive letter: %s\n", pszDrive);
1759
1760 static const char s_szTag[] = "VBoxControl";
1761 char szzNameAndTag[256];
1762 const char *pszName = argv[1];
1763 size_t cchName = strlen(pszName);
1764 if (cchName < 1)
1765 return VBoxControlSyntaxError("sharedfolder use: shared folder name cannot be empty!\n");
1766 if (cchName + 1 + sizeof(s_szTag) >= sizeof(szzNameAndTag))
1767 return VBoxControlSyntaxError("sharedfolder use: shared folder name is too long! (%s)\n", pszName);
1768
1769 /*
1770 * Do the attaching.
1771 */
1772 memcpy(szzNameAndTag, pszName, cchName);
1773 szzNameAndTag[cchName] = '\0';
1774 memcpy(&szzNameAndTag[cchName + 1], s_szTag, sizeof(s_szTag));
1775
1776 APIRET rcOs2 = DosFSAttach(pszDrive, "VBOXSF", szzNameAndTag, cchName + 1 + sizeof(s_szTag), FS_ATTACH);
1777 if (rcOs2 == NO_ERROR)
1778 return RTEXITCODE_SUCCESS;
1779 if (rcOs2 == ERROR_INVALID_FSD_NAME)
1780 return VBoxControlError("Shared folders IFS not installed?\n");
1781 return VBoxControlError("DosFSAttach/FS_ATTACH failed to attach '%s' to '%s': %u\n", pszName, pszDrive, rcOs2);
1782}
1783
1784/**
1785 * Detaches a shared folder from a drive letter.
1786 */
1787static RTEXITCODE sharedFolder_unuse(int argc, char **argv)
1788{
1789 /*
1790 * Only takes a drive letter as argument.
1791 */
1792 if (argc != 1)
1793 return VBoxControlSyntaxError("sharedfolder unuse: expected drive letter\n");
1794 const char *pszDrive = argv[0];
1795 if (!RT_C_IS_ALPHA(pszDrive[0]) || pszDrive[1] != ':' || pszDrive[2] != '\0')
1796 return VBoxControlSyntaxError("sharedfolder unuse: not a drive letter: %s\n", pszDrive);
1797
1798 /*
1799 * Do the detaching.
1800 */
1801 APIRET rcOs2 = DosFSAttach(pszDrive, "VBOXSF", NULL, 0, FS_DETACH);
1802 if (rcOs2 == NO_ERROR)
1803 return RTEXITCODE_SUCCESS;
1804 return VBoxControlError("DosFSAttach/FS_DETACH failed on '%s': %u\n", pszDrive, rcOs2);
1805}
1806
1807# endif /* RT_OS_OS2 */
1808
1809
1810/**
1811 * Handles Shared Folders control.
1812 *
1813 * @returns 0 on success, 1 on failure
1814 * @note see the command line API description for parameters
1815 * (r=bird: yeah, right. The API description contains nil about params)
1816 */
1817static DECLCALLBACK(RTEXITCODE) handleSharedFolder(int argc, char *argv[])
1818{
1819 if (argc == 0)
1820 {
1821 usage(GUEST_SHAREDFOLDERS);
1822 return RTEXITCODE_FAILURE;
1823 }
1824 if (!strcmp(argv[0], "list"))
1825 return sharedFolder_list(argc - 1, argv + 1);
1826# ifdef RT_OS_OS2
1827 if (!strcmp(argv[0], "use"))
1828 return sharedFolder_use(argc - 1, argv + 1);
1829 if (!strcmp(argv[0], "unuse"))
1830 return sharedFolder_unuse(argc - 1, argv + 1);
1831# endif
1832
1833 usage(GUEST_SHAREDFOLDERS);
1834 return RTEXITCODE_FAILURE;
1835}
1836
1837#endif
1838#if !defined(VBOX_CONTROL_TEST)
1839
1840/**
1841 * @callback_method_impl{FNVBOXCTRLCMDHANDLER, Command: writecoredump}
1842 */
1843static DECLCALLBACK(RTEXITCODE) handleWriteCoreDump(int argc, char *argv[])
1844{
1845 RT_NOREF2(argc, argv);
1846 int rc = VbglR3WriteCoreDump();
1847 if (RT_SUCCESS(rc))
1848 {
1849 RTPrintf("Guest core dump successful.\n");
1850 return RTEXITCODE_SUCCESS;
1851 }
1852 else
1853 {
1854 VBoxControlError("Error while taking guest core dump. rc=%Rrc\n", rc);
1855 return RTEXITCODE_FAILURE;
1856 }
1857}
1858
1859#endif
1860#ifdef VBOX_WITH_DPC_LATENCY_CHECKER
1861
1862/**
1863 * @callback_method_impl{FNVBOXCTRLCMDHANDLER, Command: help}
1864 */
1865static DECLCALLBACK(RTEXITCODE) handleDpc(int argc, char *argv[])
1866{
1867 RT_NOREF(argc, argv);
1868 int rc = VERR_NOT_IMPLEMENTED;
1869# ifndef VBOX_CONTROL_TEST
1870 for (int i = 0; i < 30; i++)
1871 {
1872 VBGLREQHDR Req;
1873 VBGLREQHDR_INIT(&Req, DPC_LATENCY_CHECKER);
1874 rc = vbglR3DoIOCtl(VBGL_IOCTL_DPC_LATENCY_CHECKER, &Req, sizeof(Req));
1875 if (RT_SUCCESS(rc))
1876 RTPrintf("%d\n", i);
1877 else
1878 break;
1879 }
1880# endif
1881 if (RT_FAILURE(rc))
1882 return VBoxControlError("Error. rc=%Rrc\n", rc);
1883 RTPrintf("Samples collection completed.\n");
1884 return RTEXITCODE_SUCCESS;
1885}
1886#endif /* VBOX_WITH_DPC_LATENCY_CHECKER */
1887
1888
1889/**
1890 * @callback_method_impl{FNVBOXCTRLCMDHANDLER, Command: writelog}
1891 */
1892static DECLCALLBACK(RTEXITCODE) handleWriteLog(int argc, char *argv[])
1893{
1894 static const RTGETOPTDEF s_aOptions[] =
1895 {
1896 { "--no-newline", 'n', RTGETOPT_REQ_NOTHING },
1897 };
1898 bool fNoNewline = false;
1899
1900 RTGETOPTSTATE GetOptState;
1901 int rc = RTGetOptInit(&GetOptState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions),
1902 0 /*iFirst*/, RTGETOPTINIT_FLAGS_OPTS_FIRST);
1903 if (RT_SUCCESS(rc))
1904 {
1905 RTGETOPTUNION ValueUnion;
1906 int ch;
1907 while ((ch = RTGetOpt(&GetOptState, &ValueUnion)) != 0)
1908 {
1909 switch (ch)
1910 {
1911 case VINF_GETOPT_NOT_OPTION:
1912 {
1913 size_t cch = strlen(ValueUnion.psz);
1914 if ( fNoNewline
1915 || (cch > 0 && ValueUnion.psz[cch - 1] == '\n') )
1916 rc = VbglR3WriteLog(ValueUnion.psz, cch);
1917 else
1918 {
1919 char *pszDup = (char *)RTMemDupEx(ValueUnion.psz, cch, 2);
1920 if (RT_SUCCESS(rc))
1921 {
1922 pszDup[cch++] = '\n';
1923 pszDup[cch] = '\0';
1924 rc = VbglR3WriteLog(pszDup, cch);
1925 RTMemFree(pszDup);
1926 }
1927 else
1928 rc = VERR_NO_MEMORY;
1929 }
1930 if (RT_FAILURE(rc))
1931 return VBoxControlError("VbglR3WriteLog: %Rrc", rc);
1932 break;
1933 }
1934
1935 case 'n':
1936 fNoNewline = true;
1937 break;
1938
1939 case 'h': return usage(WRITE_LOG);
1940 case 'V': return printVersion();
1941 default:
1942 return VBoxCtrlGetOptError(ch, &ValueUnion);
1943 }
1944 }
1945 }
1946 else
1947 return VBoxControlError("RTGetOptInit: %Rrc", rc);
1948 return RTEXITCODE_SUCCESS;
1949}
1950
1951
1952/**
1953 * @callback_method_impl{FNVBOXCTRLCMDHANDLER, Command: takesnapshot}
1954 */
1955static DECLCALLBACK(RTEXITCODE) handleTakeSnapshot(int argc, char *argv[])
1956{
1957 RT_NOREF2(argc, argv); //VbglR3VmTakeSnapshot(argv[0], argv[1]);
1958 return VBoxControlError("not implemented");
1959}
1960
1961/**
1962 * @callback_method_impl{FNVBOXCTRLCMDHANDLER, Command: savestate}
1963 */
1964static DECLCALLBACK(RTEXITCODE) handleSaveState(int argc, char *argv[])
1965{
1966 RT_NOREF2(argc, argv); //VbglR3VmSaveState();
1967 return VBoxControlError("not implemented");
1968}
1969
1970/**
1971 * @callback_method_impl{FNVBOXCTRLCMDHANDLER, Command: suspend|pause}
1972 */
1973static DECLCALLBACK(RTEXITCODE) handleSuspend(int argc, char *argv[])
1974{
1975 RT_NOREF2(argc, argv); //VbglR3VmSuspend();
1976 return VBoxControlError("not implemented");
1977}
1978
1979/**
1980 * @callback_method_impl{FNVBOXCTRLCMDHANDLER, Command: poweroff|powerdown}
1981 */
1982static DECLCALLBACK(RTEXITCODE) handlePowerOff(int argc, char *argv[])
1983{
1984 RT_NOREF2(argc, argv); //VbglR3VmPowerOff();
1985 return VBoxControlError("not implemented");
1986}
1987
1988/**
1989 * @callback_method_impl{FNVBOXCTRLCMDHANDLER, Command: version}
1990 */
1991static DECLCALLBACK(RTEXITCODE) handleVersion(int argc, char *argv[])
1992{
1993 RT_NOREF1(argv);
1994 if (argc)
1995 return VBoxControlSyntaxError("getversion does not take any arguments");
1996 return printVersion();
1997}
1998
1999/**
2000 * @callback_method_impl{FNVBOXCTRLCMDHANDLER, Command: help}
2001 */
2002static DECLCALLBACK(RTEXITCODE) handleHelp(int argc, char *argv[])
2003{
2004 RT_NOREF2(argc, argv); /* ignore arguments for now. */
2005 usage();
2006 return RTEXITCODE_SUCCESS;
2007}
2008
2009/**
2010 * @callback_method_impl{FNVBOXCTRLCMDHANDLER, Command: ls}
2011 */
2012static DECLCALLBACK(RTEXITCODE) handleLs(int argc, char *argv[])
2013{
2014 return RTFsCmdLs(argc + 1, argv - 1);
2015}
2016
2017/**
2018 * @callback_method_impl{FNVBOXCTRLCMDHANDLER, Command: tar}
2019 */
2020static DECLCALLBACK(RTEXITCODE) handleTar(int argc, char *argv[])
2021{
2022 return RTZipTarCmd(argc + 1, argv - 1);
2023}
2024
2025/**
2026 * @callback_method_impl{FNVBOXCTRLCMDHANDLER, Command: tar}
2027 */
2028static DECLCALLBACK(RTEXITCODE) handleGzip(int argc, char *argv[])
2029{
2030 return RTZipGzipCmd(argc + 1, argv - 1);
2031}
2032
2033/**
2034 * @callback_method_impl{FNVBOXCTRLCMDHANDLER, Command: unzip}
2035 */
2036static DECLCALLBACK(RTEXITCODE) handleUnzip(int argc, char *argv[])
2037{
2038 return RTZipUnzipCmd(argc + 1, argv - 1);
2039}
2040
2041
2042/** command handler type */
2043typedef DECLCALLBACKTYPE(RTEXITCODE, FNVBOXCTRLCMDHANDLER,(int argc, char *argv[]));
2044typedef FNVBOXCTRLCMDHANDLER *PFNVBOXCTRLCMDHANDLER;
2045
2046/** The table of all registered command handlers. */
2047struct COMMANDHANDLER
2048{
2049 const char *pszCommand;
2050 PFNVBOXCTRLCMDHANDLER pfnHandler;
2051 bool fNeedDevice;
2052} g_aCommandHandlers[] =
2053{
2054#if defined(RT_OS_WINDOWS) && !defined(VBOX_CONTROL_TEST)
2055 { "getvideoacceleration", handleGetVideoAcceleration, true },
2056 { "setvideoacceleration", handleSetVideoAcceleration, true },
2057 { "videoflags", handleVideoFlags, true },
2058 { "listcustommodes", handleListCustomModes, true },
2059 { "addcustommode", handleAddCustomMode, true },
2060 { "removecustommode", handleRemoveCustomMode, true },
2061 { "setvideomode", handleSetVideoMode, true },
2062#endif
2063#ifdef VBOX_WITH_GUEST_PROPS
2064 { "guestproperty", handleGuestProperty, true },
2065#endif
2066#ifdef VBOX_WITH_SHARED_FOLDERS
2067 { "sharedfolder", handleSharedFolder, true },
2068#endif
2069#if !defined(VBOX_CONTROL_TEST)
2070 { "writecoredump", handleWriteCoreDump, true },
2071#endif
2072#ifdef VBOX_WITH_DPC_LATENCY_CHECKER
2073 { "dpc", handleDpc, true },
2074#endif
2075 { "writelog", handleWriteLog, true },
2076 { "takesnapshot", handleTakeSnapshot, true },
2077 { "savestate", handleSaveState, true },
2078 { "suspend", handleSuspend, true },
2079 { "pause", handleSuspend, true },
2080 { "poweroff", handlePowerOff, true },
2081 { "powerdown", handlePowerOff, true },
2082 { "getversion", handleVersion, false },
2083 { "version", handleVersion, false },
2084 { "help", handleHelp, false },
2085 /* Hany tricks that doesn't cost much space: */
2086 { "gzip", handleGzip, false },
2087 { "ls", handleLs, false },
2088 { "tar", handleTar, false },
2089 { "unzip", handleUnzip, false },
2090};
2091
2092/** Main function */
2093int main(int argc, char **argv)
2094{
2095 /** The application's global return code */
2096 RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
2097 /** An IPRT return code for local use */
2098 int rrc = VINF_SUCCESS;
2099 /** The index of the command line argument we are currently processing */
2100 int iArg = 1;
2101 /** Should we show the logo text? */
2102 bool fShowLogo = true;
2103 /** Should we print the usage after the logo? For the -help switch. */
2104 bool fDoHelp = false;
2105 /** Will we be executing a command or just printing information? */
2106 bool fOnlyInfo = false;
2107
2108 rrc = RTR3InitExe(argc, &argv, 0);
2109 if (RT_FAILURE(rrc))
2110 return RTMsgInitFailure(rrc);
2111
2112 /*
2113 * Start by handling command line switches
2114 */
2115 /** @todo RTGetOpt conversion of the whole file. */
2116 bool done = false; /**< Are we finished with handling switches? */
2117 while (!done && (iArg < argc))
2118 {
2119 if ( !strcmp(argv[iArg], "-V")
2120 || !strcmp(argv[iArg], "-v")
2121 || !strcmp(argv[iArg], "--version")
2122 || !strcmp(argv[iArg], "-version")
2123 )
2124 {
2125 /* Print version number, and do nothing else. */
2126 printVersion();
2127 fOnlyInfo = true;
2128 fShowLogo = false;
2129 done = true;
2130 }
2131 else if ( !strcmp(argv[iArg], "-nologo")
2132 || !strcmp(argv[iArg], "--nologo"))
2133 fShowLogo = false;
2134 else if ( !strcmp(argv[iArg], "-help")
2135 || !strcmp(argv[iArg], "--help"))
2136 {
2137 fOnlyInfo = true;
2138 fDoHelp = true;
2139 done = true;
2140 }
2141 else
2142 /* We have found an argument which isn't a switch. Exit to the
2143 * command processing bit. */
2144 done = true;
2145 if (!done)
2146 ++iArg;
2147 }
2148
2149 /*
2150 * Find the application name, show our logo if the user hasn't suppressed it,
2151 * and show the usage if the user asked us to
2152 */
2153 g_pszProgName = RTPathFilename(argv[0]);
2154 if (fShowLogo)
2155 RTPrintf(VBOX_PRODUCT " Guest Additions Command Line Management Interface Version "
2156 VBOX_VERSION_STRING "\n"
2157 "Copyright (C) 2008-" VBOX_C_YEAR " " VBOX_VENDOR "\n\n");
2158 if (fDoHelp)
2159 usage();
2160
2161 /*
2162 * Now look for an actual command in the argument list and handle it.
2163 */
2164 if (!fOnlyInfo && rcExit == RTEXITCODE_SUCCESS)
2165 {
2166 if (argc > iArg)
2167 {
2168 /*
2169 * Try locate the command and execute it, complain if not found.
2170 */
2171 unsigned i;
2172 for (i = 0; i < RT_ELEMENTS(g_aCommandHandlers); i++)
2173 if (!strcmp(argv[iArg], g_aCommandHandlers[i].pszCommand))
2174 {
2175 if (g_aCommandHandlers[i].fNeedDevice)
2176 {
2177 rrc = VbglR3Init();
2178 if (RT_FAILURE(rrc))
2179 {
2180 VBoxControlError("Could not contact the host system. Make sure that you are running this\n"
2181 "application inside a VirtualBox guest system, and that you have sufficient\n"
2182 "user permissions.\n");
2183 rcExit = RTEXITCODE_FAILURE;
2184 }
2185 }
2186 if (rcExit == RTEXITCODE_SUCCESS)
2187 rcExit = g_aCommandHandlers[i].pfnHandler(argc - iArg - 1, argv + iArg + 1);
2188 break;
2189 }
2190 if (i >= RT_ELEMENTS(g_aCommandHandlers))
2191 {
2192 usage();
2193 rcExit = RTEXITCODE_SYNTAX;
2194 }
2195 }
2196 else
2197 {
2198 /* The user didn't specify a command. */
2199 usage();
2200 rcExit = RTEXITCODE_SYNTAX;
2201 }
2202 }
2203
2204 /*
2205 * And exit, returning the status
2206 */
2207 return rcExit;
2208}
2209
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use