VirtualBox

source: vbox/trunk/src/VBox/Frontends/VBoxManage/VBoxManageControlVM.cpp@ 34604

Last change on this file since 34604 was 34604, checked in by vboxsync, 14 years ago

Use session machine ptr

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 38.6 KB
Line 
1/* $Id: VBoxManageControlVM.cpp 34604 2010-12-02 12:50:36Z vboxsync $ */
2/** @file
3 * VBoxManage - Implementation of controlvm command.
4 */
5
6/*
7 * Copyright (C) 2006-2010 Oracle Corporation
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
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#include <VBox/com/com.h>
23#include <VBox/com/string.h>
24#include <VBox/com/Guid.h>
25#include <VBox/com/array.h>
26#include <VBox/com/ErrorInfo.h>
27#include <VBox/com/errorprint.h>
28#include <VBox/com/EventQueue.h>
29
30#include <VBox/com/VirtualBox.h>
31
32#include <iprt/ctype.h>
33#include <VBox/err.h>
34#include <iprt/getopt.h>
35#include <iprt/stream.h>
36#include <iprt/string.h>
37#include <iprt/uuid.h>
38#include <VBox/log.h>
39
40#include "VBoxManage.h"
41
42#include <list>
43
44
45/**
46 * Parses a number.
47 *
48 * @returns Valid number on success.
49 * @returns 0 if invalid number. All necessary bitching has been done.
50 * @param psz Pointer to the nic number.
51 */
52static unsigned parseNum(const char *psz, unsigned cMaxNum, const char *name)
53{
54 uint32_t u32;
55 char *pszNext;
56 int rc = RTStrToUInt32Ex(psz, &pszNext, 10, &u32);
57 if ( RT_SUCCESS(rc)
58 && *pszNext == '\0'
59 && u32 >= 1
60 && u32 <= cMaxNum)
61 return (unsigned)u32;
62 errorArgument("Invalid %s number '%s'", name, psz);
63 return 0;
64}
65
66
67int handleControlVM(HandlerArg *a)
68{
69 using namespace com;
70 HRESULT rc;
71
72 if (a->argc < 2)
73 return errorSyntax(USAGE_CONTROLVM, "Not enough parameters");
74
75 /* try to find the given machine */
76 ComPtr <IMachine> machine;
77 CHECK_ERROR(a->virtualBox, FindMachine(Bstr(a->argv[0]).raw(),
78 machine.asOutParam()));
79 if (FAILED(rc))
80 return 1;
81
82 /* open a session for the VM */
83 CHECK_ERROR_RET(machine, LockMachine(a->session, LockType_Shared), 1);
84
85 do
86 {
87 /* get the associated console */
88 ComPtr<IConsole> console;
89 CHECK_ERROR_BREAK(a->session, COMGETTER(Console)(console.asOutParam()));
90 /* ... and session machine */
91 ComPtr<IMachine> sessionMachine;
92 CHECK_ERROR_BREAK(a->session, COMGETTER(Machine)(sessionMachine.asOutParam()));
93
94 /* which command? */
95 if (!strcmp(a->argv[1], "pause"))
96 {
97 CHECK_ERROR_BREAK(console, Pause());
98 }
99 else if (!strcmp(a->argv[1], "resume"))
100 {
101 CHECK_ERROR_BREAK(console, Resume());
102 }
103 else if (!strcmp(a->argv[1], "reset"))
104 {
105 CHECK_ERROR_BREAK(console, Reset());
106 }
107 else if (!strcmp(a->argv[1], "unplugcpu"))
108 {
109 if (a->argc <= 1 + 1)
110 {
111 errorArgument("Missing argument to '%s'. Expected CPU number.", a->argv[1]);
112 rc = E_FAIL;
113 break;
114 }
115
116 unsigned n = parseNum(a->argv[2], 32, "CPU");
117
118 CHECK_ERROR_BREAK(sessionMachine, HotUnplugCPU(n));
119 }
120 else if (!strcmp(a->argv[1], "plugcpu"))
121 {
122 if (a->argc <= 1 + 1)
123 {
124 errorArgument("Missing argument to '%s'. Expected CPU number.", a->argv[1]);
125 rc = E_FAIL;
126 break;
127 }
128
129 unsigned n = parseNum(a->argv[2], 32, "CPU");
130
131 CHECK_ERROR_BREAK(sessionMachine, HotPlugCPU(n));
132 }
133 else if (!strcmp(a->argv[1], "cpuexecutioncap"))
134 {
135 if (a->argc <= 1 + 1)
136 {
137 errorArgument("Missing argument to '%s'. Expected execution cap number.", a->argv[1]);
138 rc = E_FAIL;
139 break;
140 }
141
142 unsigned n = parseNum(a->argv[2], 100, "ExecutionCap");
143
144 CHECK_ERROR_BREAK(sessionMachine, COMSETTER(CPUExecutionCap)(n));
145 }
146 else if (!strcmp(a->argv[1], "poweroff"))
147 {
148 ComPtr<IProgress> progress;
149 CHECK_ERROR_BREAK(console, PowerDown(progress.asOutParam()));
150
151 rc = showProgress(progress);
152 if (FAILED(rc))
153 {
154 com::ProgressErrorInfo info(progress);
155 if (info.isBasicAvailable())
156 RTMsgError("Failed to power off machine. Error message: %lS", info.getText().raw());
157 else
158 RTMsgError("Failed to power off machine. No error message available!");
159 }
160 }
161 else if (!strcmp(a->argv[1], "savestate"))
162 {
163 /* first pause so we don't trigger a live save which needs more time/resources */
164 CHECK_ERROR_BREAK(console, Pause());
165
166 ComPtr<IProgress> progress;
167 CHECK_ERROR(console, SaveState(progress.asOutParam()));
168 if (FAILED(rc))
169 {
170 console->Resume();
171 break;
172 }
173
174 rc = showProgress(progress);
175 if (FAILED(rc))
176 {
177 com::ProgressErrorInfo info(progress);
178 if (info.isBasicAvailable())
179 RTMsgError("Failed to save machine state. Error message: %lS", info.getText().raw());
180 else
181 RTMsgError("Failed to save machine state. No error message available!");
182 console->Resume();
183 }
184 }
185 else if (!strcmp(a->argv[1], "acpipowerbutton"))
186 {
187 CHECK_ERROR_BREAK(console, PowerButton());
188 }
189 else if (!strcmp(a->argv[1], "acpisleepbutton"))
190 {
191 CHECK_ERROR_BREAK(console, SleepButton());
192 }
193 else if (!strcmp(a->argv[1], "injectnmi"))
194 {
195 /* get the machine debugger. */
196 ComPtr <IMachineDebugger> debugger;
197 CHECK_ERROR_BREAK(console, COMGETTER(Debugger)(debugger.asOutParam()));
198 CHECK_ERROR_BREAK(debugger, InjectNMI());
199 }
200 else if (!strcmp(a->argv[1], "keyboardputscancode"))
201 {
202 ComPtr<IKeyboard> keyboard;
203 CHECK_ERROR_BREAK(console, COMGETTER(Keyboard)(keyboard.asOutParam()));
204
205 if (a->argc <= 1 + 1)
206 {
207 errorArgument("Missing argument to '%s'. Expected IBM PC AT set 2 keyboard scancode(s) as hex byte(s).", a->argv[1]);
208 rc = E_FAIL;
209 break;
210 }
211
212 std::list<LONG> llScancodes;
213
214 /* Process the command line. */
215 int i;
216 for (i = 1 + 1; i < a->argc; i++)
217 {
218 if ( RT_C_IS_XDIGIT (a->argv[i][0])
219 && RT_C_IS_XDIGIT (a->argv[i][1])
220 && a->argv[i][2] == 0)
221 {
222 uint8_t u8Scancode;
223 int irc = RTStrToUInt8Ex(a->argv[i], NULL, 16, &u8Scancode);
224 if (RT_FAILURE (irc))
225 {
226 RTMsgError("Converting '%s' returned %Rrc!", a->argv[i], rc);
227 rc = E_FAIL;
228 break;
229 }
230
231 llScancodes.push_back(u8Scancode);
232 }
233 else
234 {
235 RTMsgError("Error: '%s' is not a hex byte!", a->argv[i]);
236 rc = E_FAIL;
237 break;
238 }
239 }
240
241 if (FAILED(rc))
242 break;
243
244 /* Send scancodes to the VM. */
245 com::SafeArray<LONG> saScancodes(llScancodes);
246 ULONG codesStored = 0;
247 CHECK_ERROR_BREAK(keyboard, PutScancodes(ComSafeArrayAsInParam(saScancodes),
248 &codesStored));
249 if (codesStored < saScancodes.size())
250 {
251 RTMsgError("Only %d scancodes were stored", codesStored);
252 rc = E_FAIL;
253 break;
254 }
255 }
256 else if (!strncmp(a->argv[1], "setlinkstate", 12))
257 {
258 /* Get the number of network adapters */
259 ULONG NetworkAdapterCount = 0;
260 ComPtr <ISystemProperties> info;
261 CHECK_ERROR_BREAK(a->virtualBox, COMGETTER(SystemProperties)(info.asOutParam()));
262 CHECK_ERROR_BREAK(info, COMGETTER(NetworkAdapterCount)(&NetworkAdapterCount));
263
264 unsigned n = parseNum(&a->argv[1][12], NetworkAdapterCount, "NIC");
265 if (!n)
266 {
267 rc = E_FAIL;
268 break;
269 }
270 if (a->argc <= 1 + 1)
271 {
272 errorArgument("Missing argument to '%s'", a->argv[1]);
273 rc = E_FAIL;
274 break;
275 }
276 /* get the corresponding network adapter */
277 ComPtr<INetworkAdapter> adapter;
278 CHECK_ERROR_BREAK(sessionMachine, GetNetworkAdapter(n - 1, adapter.asOutParam()));
279 if (adapter)
280 {
281 if (!strcmp(a->argv[2], "on"))
282 {
283 CHECK_ERROR_BREAK(adapter, COMSETTER(CableConnected)(TRUE));
284 }
285 else if (!strcmp(a->argv[2], "off"))
286 {
287 CHECK_ERROR_BREAK(adapter, COMSETTER(CableConnected)(FALSE));
288 }
289 else
290 {
291 errorArgument("Invalid link state '%s'", Utf8Str(a->argv[2]).c_str());
292 rc = E_FAIL;
293 break;
294 }
295 }
296 }
297 /* here the order in which strncmp is called is important
298 * cause nictracefile can be very well compared with
299 * nictrace and nic and thus everything will always fail
300 * if the order is changed
301 */
302 else if (!strncmp(a->argv[1], "nictracefile", 12))
303 {
304 /* Get the number of network adapters */
305 ULONG NetworkAdapterCount = 0;
306 ComPtr <ISystemProperties> info;
307 CHECK_ERROR_BREAK(a->virtualBox, COMGETTER(SystemProperties)(info.asOutParam()));
308 CHECK_ERROR_BREAK(info, COMGETTER(NetworkAdapterCount)(&NetworkAdapterCount));
309
310 unsigned n = parseNum(&a->argv[1][12], NetworkAdapterCount, "NIC");
311 if (!n)
312 {
313 rc = E_FAIL;
314 break;
315 }
316 if (a->argc <= 2)
317 {
318 errorArgument("Missing argument to '%s'", a->argv[1]);
319 rc = E_FAIL;
320 break;
321 }
322
323 /* get the corresponding network adapter */
324 ComPtr<INetworkAdapter> adapter;
325 CHECK_ERROR_BREAK(sessionMachine, GetNetworkAdapter(n - 1, adapter.asOutParam()));
326 if (adapter)
327 {
328 BOOL fEnabled;
329 adapter->COMGETTER(Enabled)(&fEnabled);
330 if (fEnabled)
331 {
332 if (a->argv[2])
333 {
334 CHECK_ERROR_RET(adapter, COMSETTER(TraceFile)(Bstr(a->argv[2]).raw()), 1);
335 }
336 else
337 {
338 errorArgument("Invalid filename or filename not specified for NIC %lu", n);
339 rc = E_FAIL;
340 break;
341 }
342 }
343 else
344 RTMsgError("The NIC %d is currently disabled and thus can't change its tracefile", n);
345 }
346 }
347 else if (!strncmp(a->argv[1], "nictrace", 8))
348 {
349 /* Get the number of network adapters */
350 ULONG NetworkAdapterCount = 0;
351 ComPtr <ISystemProperties> info;
352 CHECK_ERROR_BREAK(a->virtualBox, COMGETTER(SystemProperties)(info.asOutParam()));
353 CHECK_ERROR_BREAK(info, COMGETTER(NetworkAdapterCount)(&NetworkAdapterCount));
354
355 unsigned n = parseNum(&a->argv[1][8], NetworkAdapterCount, "NIC");
356 if (!n)
357 {
358 rc = E_FAIL;
359 break;
360 }
361 if (a->argc <= 2)
362 {
363 errorArgument("Missing argument to '%s'", a->argv[1]);
364 rc = E_FAIL;
365 break;
366 }
367
368 /* get the corresponding network adapter */
369 ComPtr<INetworkAdapter> adapter;
370 CHECK_ERROR_BREAK(sessionMachine, GetNetworkAdapter(n - 1, adapter.asOutParam()));
371 if (adapter)
372 {
373 BOOL fEnabled;
374 adapter->COMGETTER(Enabled)(&fEnabled);
375 if (fEnabled)
376 {
377 if (!strcmp(a->argv[2], "on"))
378 {
379 CHECK_ERROR_RET(adapter, COMSETTER(TraceEnabled)(TRUE), 1);
380 }
381 else if (!strcmp(a->argv[2], "off"))
382 {
383 CHECK_ERROR_RET(adapter, COMSETTER(TraceEnabled)(FALSE), 1);
384 }
385 else
386 {
387 errorArgument("Invalid nictrace%lu argument '%s'", n, Utf8Str(a->argv[2]).c_str());
388 rc = E_FAIL;
389 break;
390 }
391 }
392 else
393 RTMsgError("The NIC %d is currently disabled and thus can't change its trace flag", n);
394 }
395 }
396 else if( a->argc > 2
397 && !strncmp(a->argv[1], "natpf", 5))
398 {
399 /* Get the number of network adapters */
400 ULONG NetworkAdapterCount = 0;
401 ComPtr <ISystemProperties> info;
402 ComPtr<INATEngine> engine;
403 CHECK_ERROR_BREAK(a->virtualBox, COMGETTER(SystemProperties)(info.asOutParam()));
404 CHECK_ERROR_BREAK(info, COMGETTER(NetworkAdapterCount)(&NetworkAdapterCount));
405 unsigned n = parseNum(&a->argv[1][5], NetworkAdapterCount, "NIC");
406 if (!n)
407 {
408 rc = E_FAIL;
409 break;
410 }
411 if (a->argc <= 2)
412 {
413 errorArgument("Missing argument to '%s'", a->argv[1]);
414 rc = E_FAIL;
415 break;
416 }
417
418 /* get the corresponding network adapter */
419 ComPtr<INetworkAdapter> adapter;
420 CHECK_ERROR_BREAK(sessionMachine, GetNetworkAdapter(n - 1, adapter.asOutParam()));
421 if (!adapter)
422 {
423 rc = E_FAIL;
424 break;
425 }
426 CHECK_ERROR(adapter, COMGETTER(NatDriver)(engine.asOutParam()));
427 if (!engine)
428 {
429 rc = E_FAIL;
430 break;
431 }
432
433 if (!strcmp(a->argv[2], "delete"))
434 {
435 if (a->argc >= 3)
436 CHECK_ERROR(engine, RemoveRedirect(Bstr(a->argv[3]).raw()));
437 }
438 else
439 {
440#define ITERATE_TO_NEXT_TERM(ch) \
441 do { \
442 while (*ch != ',') \
443 { \
444 if (*ch == 0) \
445 { \
446 return errorSyntax(USAGE_CONTROLVM, \
447 "Missing or invalid argument to '%s'", \
448 a->argv[1]); \
449 } \
450 ch++; \
451 } \
452 *ch = '\0'; \
453 ch++; \
454 } while(0)
455
456 char *strName;
457 char *strProto;
458 char *strHostIp;
459 char *strHostPort;
460 char *strGuestIp;
461 char *strGuestPort;
462 char *strRaw = RTStrDup(a->argv[2]);
463 char *ch = strRaw;
464 strName = RTStrStrip(ch);
465 ITERATE_TO_NEXT_TERM(ch);
466 strProto = RTStrStrip(ch);
467 ITERATE_TO_NEXT_TERM(ch);
468 strHostIp = RTStrStrip(ch);
469 ITERATE_TO_NEXT_TERM(ch);
470 strHostPort = RTStrStrip(ch);
471 ITERATE_TO_NEXT_TERM(ch);
472 strGuestIp = RTStrStrip(ch);
473 ITERATE_TO_NEXT_TERM(ch);
474 strGuestPort = RTStrStrip(ch);
475 NATProtocol_T proto;
476 if (RTStrICmp(strProto, "udp") == 0)
477 proto = NATProtocol_UDP;
478 else if (RTStrICmp(strProto, "tcp") == 0)
479 proto = NATProtocol_TCP;
480 else
481 {
482 return errorSyntax(USAGE_CONTROLVM,
483 "Wrong rule proto '%s' specified -- only 'udp' and 'tcp' are allowed.",
484 strProto);
485 }
486 CHECK_ERROR(engine, AddRedirect(Bstr(strName).raw(), proto, Bstr(strHostIp).raw(),
487 RTStrToUInt16(strHostPort), Bstr(strGuestIp).raw(), RTStrToUInt16(strGuestPort)));
488#undef ITERATE_TO_NEXT_TERM
489 }
490 /* commit changes */
491 if (SUCCEEDED(rc))
492 CHECK_ERROR(sessionMachine, SaveSettings());
493 }
494 else if (!strncmp(a->argv[1], "nic", 3))
495 {
496 /* Get the number of network adapters */
497 ULONG NetworkAdapterCount = 0;
498 ComPtr <ISystemProperties> info;
499 CHECK_ERROR_BREAK(a->virtualBox, COMGETTER(SystemProperties)(info.asOutParam()));
500 CHECK_ERROR_BREAK(info, COMGETTER(NetworkAdapterCount)(&NetworkAdapterCount));
501
502 unsigned n = parseNum(&a->argv[1][3], NetworkAdapterCount, "NIC");
503 if (!n)
504 {
505 rc = E_FAIL;
506 break;
507 }
508 if (a->argc <= 2)
509 {
510 errorArgument("Missing argument to '%s'", a->argv[1]);
511 rc = E_FAIL;
512 break;
513 }
514
515 /* get the corresponding network adapter */
516 ComPtr<INetworkAdapter> adapter;
517 CHECK_ERROR_BREAK(sessionMachine, GetNetworkAdapter(n - 1, adapter.asOutParam()));
518 if (adapter)
519 {
520 BOOL fEnabled;
521 adapter->COMGETTER(Enabled)(&fEnabled);
522 if (fEnabled)
523 {
524 if (!strcmp(a->argv[2], "null"))
525 {
526 CHECK_ERROR_RET(adapter, COMSETTER(Enabled)(TRUE), 1);
527 CHECK_ERROR_RET(adapter, Detach(), 1);
528 }
529 else if (!strcmp(a->argv[2], "nat"))
530 {
531 CHECK_ERROR_RET(adapter, COMSETTER(Enabled)(TRUE), 1);
532 if (a->argc == 4)
533 CHECK_ERROR_RET(adapter, COMSETTER(NATNetwork)(Bstr(a->argv[3]).raw()), 1);
534 CHECK_ERROR_RET(adapter, AttachToNAT(), 1);
535 }
536 else if ( !strcmp(a->argv[2], "bridged")
537 || !strcmp(a->argv[2], "hostif")) /* backward compatibility */
538 {
539 if (a->argc <= 3)
540 {
541 errorArgument("Missing argument to '%s'", a->argv[2]);
542 rc = E_FAIL;
543 break;
544 }
545 CHECK_ERROR_RET(adapter, COMSETTER(Enabled)(TRUE), 1);
546 CHECK_ERROR_RET(adapter, COMSETTER(HostInterface)(Bstr(a->argv[3]).raw()), 1);
547 CHECK_ERROR_RET(adapter, AttachToBridgedInterface(), 1);
548 }
549 else if (!strcmp(a->argv[2], "intnet"))
550 {
551 if (a->argc <= 3)
552 {
553 errorArgument("Missing argument to '%s'", a->argv[2]);
554 rc = E_FAIL;
555 break;
556 }
557 CHECK_ERROR_RET(adapter, COMSETTER(Enabled)(TRUE), 1);
558 CHECK_ERROR_RET(adapter, COMSETTER(InternalNetwork)(Bstr(a->argv[3]).raw()), 1);
559 CHECK_ERROR_RET(adapter, AttachToInternalNetwork(), 1);
560 }
561#if defined(VBOX_WITH_NETFLT)
562 else if (!strcmp(a->argv[2], "hostonly"))
563 {
564 if (a->argc <= 3)
565 {
566 errorArgument("Missing argument to '%s'", a->argv[2]);
567 rc = E_FAIL;
568 break;
569 }
570 CHECK_ERROR_RET(adapter, COMSETTER(Enabled)(TRUE), 1);
571 CHECK_ERROR_RET(adapter, COMSETTER(HostInterface)(Bstr(a->argv[3]).raw()), 1);
572 CHECK_ERROR_RET(adapter, AttachToHostOnlyInterface(), 1);
573 }
574#endif
575 else
576 {
577 errorArgument("Invalid type '%s' specfied for NIC %lu", Utf8Str(a->argv[2]).c_str(), n);
578 rc = E_FAIL;
579 break;
580 }
581 }
582 else
583 RTMsgError("The NIC %d is currently disabled and thus can't change its attachment type", n);
584 }
585 }
586 else if ( !strcmp(a->argv[1], "vrde")
587 || !strcmp(a->argv[1], "vrdp"))
588 {
589 if (!strcmp(a->argv[1], "vrdp"))
590 RTStrmPrintf(g_pStdErr, "Warning: 'vrdp' is deprecated. Use 'vrde'.\n");
591
592 if (a->argc <= 1 + 1)
593 {
594 errorArgument("Missing argument to '%s'", a->argv[1]);
595 rc = E_FAIL;
596 break;
597 }
598 ComPtr<IVRDEServer> vrdeServer;
599 sessionMachine->COMGETTER(VRDEServer)(vrdeServer.asOutParam());
600 ASSERT(vrdeServer);
601 if (vrdeServer)
602 {
603 if (!strcmp(a->argv[2], "on"))
604 {
605 CHECK_ERROR_BREAK(vrdeServer, COMSETTER(Enabled)(TRUE));
606 }
607 else if (!strcmp(a->argv[2], "off"))
608 {
609 CHECK_ERROR_BREAK(vrdeServer, COMSETTER(Enabled)(FALSE));
610 }
611 else
612 {
613 errorArgument("Invalid remote desktop server state '%s'", Utf8Str(a->argv[2]).c_str());
614 rc = E_FAIL;
615 break;
616 }
617 }
618 }
619 else if (!strcmp(a->argv[1], "vrdpport"))
620 {
621 RTStrmPrintf(g_pStdErr, "Warning: 'vrdpport' is deprecated. Use 'setvrdeproperty'.\n");
622
623 if (a->argc <= 1 + 1)
624 {
625 errorArgument("Missing argument to '%s'", a->argv[1]);
626 rc = E_FAIL;
627 break;
628 }
629 ComPtr<IVRDEServer> vrdeServer;
630 sessionMachine->COMGETTER(VRDEServer)(vrdeServer.asOutParam());
631 ASSERT(vrdeServer);
632 if (vrdeServer)
633 {
634 Bstr ports;
635
636 if (!strcmp(a->argv[2], "default"))
637 ports = "0";
638 else
639 ports = a->argv[2];
640
641 CHECK_ERROR_BREAK(vrdeServer, SetVRDEProperty(Bstr("TCP/Ports").raw(), ports.raw()));
642 }
643 }
644 else if ( !strcmp(a->argv[1], "vrdevideochannelquality")
645 || !strcmp(a->argv[1], "vrdpvideochannelquality"))
646 {
647 if (!strcmp(a->argv[1], "vrdpvideochannelquality"))
648 RTStrmPrintf(g_pStdErr, "Warning: 'vrdpvideochannelquality' is deprecated. Use 'vrdevideochannelquality'.\n");
649
650 if (a->argc <= 1 + 1)
651 {
652 errorArgument("Missing argument to '%s'", a->argv[1]);
653 rc = E_FAIL;
654 break;
655 }
656 ComPtr<IVRDEServer> vrdeServer;
657 sessionMachine->COMGETTER(VRDEServer)(vrdeServer.asOutParam());
658 ASSERT(vrdeServer);
659 if (vrdeServer)
660 {
661 unsigned n = parseNum(a->argv[2], 100, "VRDE video redirection quality in percent");
662
663 CHECK_ERROR(vrdeServer, COMSETTER(VideoChannelQuality)(n));
664 }
665 }
666 else if (!strcmp(a->argv[1], "vrdeproperty"))
667 {
668 if (a->argc <= 1 + 1)
669 {
670 errorArgument("Missing argument to '%s'", a->argv[1]);
671 rc = E_FAIL;
672 break;
673 }
674 ComPtr<IVRDEServer> vrdeServer;
675 sessionMachine->COMGETTER(VRDEServer)(vrdeServer.asOutParam());
676 ASSERT(vrdeServer);
677 if (vrdeServer)
678 {
679 /* Parse 'name=value' */
680 char *pszProperty = RTStrDup(a->argv[2]);
681 if (pszProperty)
682 {
683 char *pDelimiter = strchr(pszProperty, '=');
684 if (pDelimiter)
685 {
686 *pDelimiter = '\0';
687
688 Bstr bstrName = pszProperty;
689 Bstr bstrValue = &pDelimiter[1];
690 CHECK_ERROR(vrdeServer, SetVRDEProperty(bstrName.raw(), bstrValue.raw()));
691 }
692 else
693 {
694 errorArgument("Invalid --vrdeproperty argument '%s'", a->argv[2]);
695 rc = E_FAIL;
696 break;
697 }
698 RTStrFree(pszProperty);
699 }
700 else
701 {
702 RTStrmPrintf(g_pStdErr, "Error: Failed to allocate memory for VRDE property '%s'\n", a->argv[2]);
703 rc = E_FAIL;
704 }
705 }
706 if (FAILED(rc))
707 {
708 break;
709 }
710 }
711 else if ( !strcmp(a->argv[1], "usbattach")
712 || !strcmp(a->argv[1], "usbdetach"))
713 {
714 if (a->argc < 3)
715 {
716 errorSyntax(USAGE_CONTROLVM, "Not enough parameters");
717 rc = E_FAIL;
718 break;
719 }
720
721 bool attach = !strcmp(a->argv[1], "usbattach");
722
723 Bstr usbId = a->argv[2];
724 if (Guid(usbId).isEmpty())
725 {
726 // assume address
727 if (attach)
728 {
729 ComPtr <IHost> host;
730 CHECK_ERROR_BREAK(a->virtualBox, COMGETTER(Host)(host.asOutParam()));
731 SafeIfaceArray <IHostUSBDevice> coll;
732 CHECK_ERROR_BREAK(host, COMGETTER(USBDevices)(ComSafeArrayAsOutParam(coll)));
733 ComPtr <IHostUSBDevice> dev;
734 CHECK_ERROR_BREAK(host, FindUSBDeviceByAddress(Bstr(a->argv[2]).raw(),
735 dev.asOutParam()));
736 CHECK_ERROR_BREAK(dev, COMGETTER(Id)(usbId.asOutParam()));
737 }
738 else
739 {
740 SafeIfaceArray <IUSBDevice> coll;
741 CHECK_ERROR_BREAK(console, COMGETTER(USBDevices)(ComSafeArrayAsOutParam(coll)));
742 ComPtr <IUSBDevice> dev;
743 CHECK_ERROR_BREAK(console, FindUSBDeviceByAddress(Bstr(a->argv[2]).raw(),
744 dev.asOutParam()));
745 CHECK_ERROR_BREAK(dev, COMGETTER(Id)(usbId.asOutParam()));
746 }
747 }
748
749 if (attach)
750 CHECK_ERROR_BREAK(console, AttachUSBDevice(usbId.raw()));
751 else
752 {
753 ComPtr <IUSBDevice> dev;
754 CHECK_ERROR_BREAK(console, DetachUSBDevice(usbId.raw(),
755 dev.asOutParam()));
756 }
757 }
758 else if (!strcmp(a->argv[1], "setvideomodehint"))
759 {
760 if (a->argc != 5 && a->argc != 6)
761 {
762 errorSyntax(USAGE_CONTROLVM, "Incorrect number of parameters");
763 rc = E_FAIL;
764 break;
765 }
766 uint32_t xres = RTStrToUInt32(a->argv[2]);
767 uint32_t yres = RTStrToUInt32(a->argv[3]);
768 uint32_t bpp = RTStrToUInt32(a->argv[4]);
769 uint32_t displayIdx = 0;
770 if (a->argc == 6)
771 displayIdx = RTStrToUInt32(a->argv[5]);
772
773 ComPtr<IDisplay> display;
774 CHECK_ERROR_BREAK(console, COMGETTER(Display)(display.asOutParam()));
775 CHECK_ERROR_BREAK(display, SetVideoModeHint(xres, yres, bpp, displayIdx));
776 }
777 else if (!strcmp(a->argv[1], "setcredentials"))
778 {
779 bool fAllowLocalLogon = true;
780 if (a->argc == 7)
781 {
782 if ( strcmp(a->argv[5], "--allowlocallogon")
783 && strcmp(a->argv[5], "-allowlocallogon"))
784 {
785 errorArgument("Invalid parameter '%s'", a->argv[5]);
786 rc = E_FAIL;
787 break;
788 }
789 if (!strcmp(a->argv[6], "no"))
790 fAllowLocalLogon = false;
791 }
792 else if (a->argc != 5)
793 {
794 errorSyntax(USAGE_CONTROLVM, "Incorrect number of parameters");
795 rc = E_FAIL;
796 break;
797 }
798
799 ComPtr<IGuest> guest;
800 CHECK_ERROR_BREAK(console, COMGETTER(Guest)(guest.asOutParam()));
801 CHECK_ERROR_BREAK(guest, SetCredentials(Bstr(a->argv[2]).raw(),
802 Bstr(a->argv[3]).raw(),
803 Bstr(a->argv[4]).raw(),
804 fAllowLocalLogon));
805 }
806#if 0 /* TODO: review & remove */
807 else if (!strcmp(a->argv[1], "dvdattach"))
808 {
809 Bstr uuid;
810 if (a->argc != 3)
811 {
812 errorSyntax(USAGE_CONTROLVM, "Incorrect number of parameters");
813 rc = E_FAIL;
814 break;
815 }
816
817 ComPtr<IMedium> dvdMedium;
818
819 /* unmount? */
820 if (!strcmp(a->argv[2], "none"))
821 {
822 /* nothing to do, NULL object will cause unmount */
823 }
824 /* host drive? */
825 else if (!strncmp(a->argv[2], "host:", 5))
826 {
827 ComPtr<IHost> host;
828 CHECK_ERROR(a->virtualBox, COMGETTER(Host)(host.asOutParam()));
829
830 rc = host->FindHostDVDDrive(Bstr(a->argv[2] + 5), dvdMedium.asOutParam());
831 if (!dvdMedium)
832 {
833 errorArgument("Invalid host DVD drive name \"%s\"",
834 a->argv[2] + 5);
835 rc = E_FAIL;
836 break;
837 }
838 }
839 else
840 {
841 /* first assume it's a UUID */
842 uuid = a->argv[2];
843 rc = a->virtualBox->GetDVDImage(uuid, dvdMedium.asOutParam());
844 if (FAILED(rc) || !dvdMedium)
845 {
846 /* must be a filename, check if it's in the collection */
847 rc = a->virtualBox->FindDVDImage(Bstr(a->argv[2]), dvdMedium.asOutParam());
848 /* not registered, do that on the fly */
849 if (!dvdMedium)
850 {
851 Bstr emptyUUID;
852 CHECK_ERROR(a->virtualBox, OpenDVDImage(Bstr(a->argv[2]), emptyUUID, dvdMedium.asOutParam()));
853 }
854 }
855 if (!dvdMedium)
856 {
857 rc = E_FAIL;
858 break;
859 }
860 }
861
862 /** @todo generalize this, allow arbitrary number of DVD drives
863 * and as a consequence multiple attachments and different
864 * storage controllers. */
865 if (dvdMedium)
866 dvdMedium->COMGETTER(Id)(uuid.asOutParam());
867 else
868 uuid = Guid().toString();
869 CHECK_ERROR(machine, MountMedium(Bstr("IDE Controller"), 1, 0, uuid, FALSE /* aForce */));
870 }
871 else if (!strcmp(a->argv[1], "floppyattach"))
872 {
873 Bstr uuid;
874 if (a->argc != 3)
875 {
876 errorSyntax(USAGE_CONTROLVM, "Incorrect number of parameters");
877 rc = E_FAIL;
878 break;
879 }
880
881 ComPtr<IMedium> floppyMedium;
882
883 /* unmount? */
884 if (!strcmp(a->argv[2], "none"))
885 {
886 /* nothing to do, NULL object will cause unmount */
887 }
888 /* host drive? */
889 else if (!strncmp(a->argv[2], "host:", 5))
890 {
891 ComPtr<IHost> host;
892 CHECK_ERROR(a->virtualBox, COMGETTER(Host)(host.asOutParam()));
893 host->FindHostFloppyDrive(Bstr(a->argv[2] + 5), floppyMedium.asOutParam());
894 if (!floppyMedium)
895 {
896 errorArgument("Invalid host floppy drive name \"%s\"",
897 a->argv[2] + 5);
898 rc = E_FAIL;
899 break;
900 }
901 }
902 else
903 {
904 /* first assume it's a UUID */
905 uuid = a->argv[2];
906 rc = a->virtualBox->GetFloppyImage(uuid, floppyMedium.asOutParam());
907 if (FAILED(rc) || !floppyMedium)
908 {
909 /* must be a filename, check if it's in the collection */
910 rc = a->virtualBox->FindFloppyImage(Bstr(a->argv[2]), floppyMedium.asOutParam());
911 /* not registered, do that on the fly */
912 if (!floppyMedium)
913 {
914 Bstr emptyUUID;
915 CHECK_ERROR(a->virtualBox, OpenFloppyImage(Bstr(a->argv[2]), emptyUUID, floppyMedium.asOutParam()));
916 }
917 }
918 if (!floppyMedium)
919 {
920 rc = E_FAIL;
921 break;
922 }
923 }
924 floppyMedium->COMGETTER(Id)(uuid.asOutParam());
925 CHECK_ERROR(machine, MountMedium(Bstr("Floppy Controller"), 0, 0, uuid, FALSE /* aForce */));
926 }
927#endif /* obsolete dvdattach/floppyattach */
928 else if (!strcmp(a->argv[1], "guestmemoryballoon"))
929 {
930 if (a->argc != 3)
931 {
932 errorSyntax(USAGE_CONTROLVM, "Incorrect number of parameters");
933 rc = E_FAIL;
934 break;
935 }
936 uint32_t uVal;
937 int vrc;
938 vrc = RTStrToUInt32Ex(a->argv[2], NULL, 0, &uVal);
939 if (vrc != VINF_SUCCESS)
940 {
941 errorArgument("Error parsing guest memory balloon size '%s'", a->argv[2]);
942 rc = E_FAIL;
943 break;
944 }
945 /* guest is running; update IGuest */
946 ComPtr <IGuest> guest;
947 rc = console->COMGETTER(Guest)(guest.asOutParam());
948 if (SUCCEEDED(rc))
949 CHECK_ERROR(guest, COMSETTER(MemoryBalloonSize)(uVal));
950 }
951 else if (!strcmp(a->argv[1], "teleport"))
952 {
953 Bstr bstrHostname;
954 uint32_t uMaxDowntime = 250 /*ms*/;
955 uint32_t uPort = UINT32_MAX;
956 uint32_t cMsTimeout = 0;
957 Bstr bstrPassword("");
958 static const RTGETOPTDEF s_aTeleportOptions[] =
959 {
960 { "--host", 'h', RTGETOPT_REQ_STRING }, /** @todo RTGETOPT_FLAG_MANDATORY */
961 { "--hostname", 'h', RTGETOPT_REQ_STRING }, /** @todo remove this */
962 { "--maxdowntime", 'd', RTGETOPT_REQ_UINT32 },
963 { "--port", 'p', RTGETOPT_REQ_UINT32 }, /** @todo RTGETOPT_FLAG_MANDATORY */
964 { "--password", 'P', RTGETOPT_REQ_STRING },
965 { "--timeout", 't', RTGETOPT_REQ_UINT32 },
966 { "--detailed-progress", 'D', RTGETOPT_REQ_NOTHING }
967 };
968 RTGETOPTSTATE GetOptState;
969 RTGetOptInit(&GetOptState, a->argc, a->argv, s_aTeleportOptions, RT_ELEMENTS(s_aTeleportOptions), 2, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
970 int ch;
971 RTGETOPTUNION Value;
972 while ( SUCCEEDED(rc)
973 && (ch = RTGetOpt(&GetOptState, &Value)))
974 {
975 switch (ch)
976 {
977 case 'h': bstrHostname = Value.psz; break;
978 case 'd': uMaxDowntime = Value.u32; break;
979 case 'D': g_fDetailedProgress = true; break;
980 case 'p': uPort = Value.u32; break;
981 case 'P': bstrPassword = Value.psz; break;
982 case 't': cMsTimeout = Value.u32; break;
983 default:
984 errorGetOpt(USAGE_CONTROLVM, ch, &Value);
985 rc = E_FAIL;
986 break;
987 }
988 }
989 if (FAILED(rc))
990 break;
991
992 ComPtr<IProgress> progress;
993 CHECK_ERROR_BREAK(console, Teleport(bstrHostname.raw(), uPort,
994 bstrPassword.raw(),
995 uMaxDowntime,
996 progress.asOutParam()));
997
998 if (cMsTimeout)
999 {
1000 rc = progress->COMSETTER(Timeout)(cMsTimeout);
1001 if (FAILED(rc) && rc != VBOX_E_INVALID_OBJECT_STATE)
1002 CHECK_ERROR_BREAK(progress, COMSETTER(Timeout)(cMsTimeout)); /* lazyness */
1003 }
1004
1005 rc = showProgress(progress);
1006 if (FAILED(rc))
1007 {
1008 com::ProgressErrorInfo info(progress);
1009 if (info.isBasicAvailable())
1010 RTMsgError("Teleportation failed. Error message: %lS", info.getText().raw());
1011 else
1012 RTMsgError("Teleportation failed. No error message available!");
1013 }
1014 }
1015 else
1016 {
1017 errorSyntax(USAGE_CONTROLVM, "Invalid parameter '%s'", Utf8Str(a->argv[1]).c_str());
1018 rc = E_FAIL;
1019 }
1020 } while (0);
1021
1022 a->session->UnlockMachine();
1023
1024 return SUCCEEDED(rc) ? 0 : 1;
1025}
1026
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