1 | /** @file
|
---|
2 | *
|
---|
3 | * VBox frontends: VBoxHeadless frontend:
|
---|
4 | * Testcases
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006-2022 Oracle Corporation
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License (GPL) as published by the Free Software
|
---|
14 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
15 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
16 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | */
|
---|
18 |
|
---|
19 | #include <VBox/com/array.h>
|
---|
20 | #include <VBox/com/com.h>
|
---|
21 | #include <VBox/com/string.h>
|
---|
22 | #include <VBox/com/Guid.h>
|
---|
23 | #include <VBox/com/ErrorInfo.h>
|
---|
24 | #include <VBox/com/errorprint.h>
|
---|
25 |
|
---|
26 | #include <VBox/com/VirtualBox.h>
|
---|
27 |
|
---|
28 | using namespace com;
|
---|
29 |
|
---|
30 | #include <VBox/log.h>
|
---|
31 | #include <iprt/initterm.h>
|
---|
32 | #include <iprt/stream.h>
|
---|
33 |
|
---|
34 |
|
---|
35 | ////////////////////////////////////////////////////////////////////////////////
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Entry point.
|
---|
39 | */
|
---|
40 | int main(int argc, char **argv)
|
---|
41 | {
|
---|
42 | // initialize VBox Runtime
|
---|
43 | RTR3InitExe(argc, &argv, 0);
|
---|
44 |
|
---|
45 | // the below cannot be Bstr because on Linux Bstr doesn't work
|
---|
46 | // until XPCOM (nsMemory) is initialized
|
---|
47 | const char *name = NULL;
|
---|
48 | const char *operation = NULL;
|
---|
49 |
|
---|
50 | // parse the command line
|
---|
51 | if (argc > 1)
|
---|
52 | name = argv [1];
|
---|
53 | if (argc > 2)
|
---|
54 | operation = argv [2];
|
---|
55 |
|
---|
56 | if (!name || !operation)
|
---|
57 | {
|
---|
58 | RTPrintf("\nUsage:\n\n"
|
---|
59 | "%s <machine_name> [on|off|pause|resume]\n\n",
|
---|
60 | argv [0]);
|
---|
61 | return 0;
|
---|
62 | }
|
---|
63 |
|
---|
64 | RTPrintf("\n");
|
---|
65 | RTPrintf("tstHeadless STARTED.\n");
|
---|
66 |
|
---|
67 | RTPrintf("VM name : {%s}\n"
|
---|
68 | "Operation : %s\n\n",
|
---|
69 | name, operation);
|
---|
70 |
|
---|
71 | HRESULT rc;
|
---|
72 |
|
---|
73 | rc = com::Initialize();
|
---|
74 | if (FAILED(rc))
|
---|
75 | {
|
---|
76 | RTPrintf("ERROR: failed to initialize COM!\n");
|
---|
77 | return rc;
|
---|
78 | }
|
---|
79 |
|
---|
80 | do
|
---|
81 | {
|
---|
82 | ComPtr <IVirtualBoxClient> virtualBoxClient;
|
---|
83 | ComPtr <IVirtualBox> virtualBox;
|
---|
84 | ComPtr <ISession> session;
|
---|
85 |
|
---|
86 | RTPrintf("Creating VirtualBox object...\n");
|
---|
87 | rc = virtualBoxClient.createInprocObject(CLSID_VirtualBoxClient);
|
---|
88 | if (SUCCEEDED(rc))
|
---|
89 | rc = virtualBoxClient->COMGETTER(VirtualBox)(virtualBox.asOutParam());
|
---|
90 | if (FAILED(rc))
|
---|
91 | RTPrintf("ERROR: failed to create the VirtualBox object!\n");
|
---|
92 | else
|
---|
93 | {
|
---|
94 | rc = session.createInprocObject(CLSID_Session);
|
---|
95 | if (FAILED(rc))
|
---|
96 | RTPrintf("ERROR: failed to create a session object!\n");
|
---|
97 | }
|
---|
98 |
|
---|
99 | if (FAILED(rc))
|
---|
100 | {
|
---|
101 | com::ErrorInfo info;
|
---|
102 | if (!info.isFullAvailable() && !info.isBasicAvailable())
|
---|
103 | {
|
---|
104 | com::GluePrintRCMessage(rc);
|
---|
105 | RTPrintf("Most likely, the VirtualBox COM server is not running or failed to start.\n");
|
---|
106 | }
|
---|
107 | else
|
---|
108 | com::GluePrintErrorInfo(info);
|
---|
109 | break;
|
---|
110 | }
|
---|
111 |
|
---|
112 | ComPtr <IMachine> m;
|
---|
113 |
|
---|
114 | // find ID by name
|
---|
115 | CHECK_ERROR_BREAK(virtualBox, FindMachine(Bstr(name).raw(),
|
---|
116 | m.asOutParam()));
|
---|
117 |
|
---|
118 | if (!strcmp(operation, "on"))
|
---|
119 | {
|
---|
120 | ComPtr <IProgress> progress;
|
---|
121 | RTPrintf("Opening a new (remote) session...\n");
|
---|
122 | CHECK_ERROR_BREAK(m,
|
---|
123 | LaunchVMProcess(session, Bstr("vrdp").raw(),
|
---|
124 | ComSafeArrayNullInParam(), progress.asOutParam()));
|
---|
125 |
|
---|
126 | RTPrintf("Waiting for the remote session to open...\n");
|
---|
127 | CHECK_ERROR_BREAK(progress, WaitForCompletion(-1));
|
---|
128 |
|
---|
129 | BOOL completed;
|
---|
130 | CHECK_ERROR_BREAK(progress, COMGETTER(Completed)(&completed));
|
---|
131 | ASSERT(completed);
|
---|
132 |
|
---|
133 | LONG resultCode;
|
---|
134 | CHECK_ERROR_BREAK(progress, COMGETTER(ResultCode)(&resultCode));
|
---|
135 | if (FAILED(resultCode))
|
---|
136 | {
|
---|
137 | ProgressErrorInfo info(progress);
|
---|
138 | com::GluePrintErrorInfo(info);
|
---|
139 | }
|
---|
140 | else
|
---|
141 | {
|
---|
142 | RTPrintf("Remote session has been successfully opened.\n");
|
---|
143 | }
|
---|
144 | }
|
---|
145 | else
|
---|
146 | {
|
---|
147 | RTPrintf("Opening an existing session...\n");
|
---|
148 | CHECK_ERROR_BREAK(m, LockMachine(session, LockType_Shared));
|
---|
149 |
|
---|
150 | ComPtr <IConsole> console;
|
---|
151 | CHECK_ERROR_BREAK(session, COMGETTER(Console)(console.asOutParam()));
|
---|
152 |
|
---|
153 | if (!strcmp(operation, "off"))
|
---|
154 | {
|
---|
155 | ComPtr <IProgress> progress;
|
---|
156 | RTPrintf("Powering the VM off...\n");
|
---|
157 | CHECK_ERROR_BREAK(console, PowerDown(progress.asOutParam()));
|
---|
158 |
|
---|
159 | RTPrintf("Waiting for the VM to power down...\n");
|
---|
160 | CHECK_ERROR_BREAK(progress, WaitForCompletion(-1));
|
---|
161 |
|
---|
162 | BOOL completed;
|
---|
163 | CHECK_ERROR_BREAK(progress, COMGETTER(Completed)(&completed));
|
---|
164 | ASSERT(completed);
|
---|
165 |
|
---|
166 | LONG resultCode;
|
---|
167 | CHECK_ERROR_BREAK(progress, COMGETTER(ResultCode)(&resultCode));
|
---|
168 | if (FAILED(resultCode))
|
---|
169 | {
|
---|
170 | ProgressErrorInfo info(progress);
|
---|
171 | com::GluePrintErrorInfo(info);
|
---|
172 | }
|
---|
173 | else
|
---|
174 | {
|
---|
175 | RTPrintf("VM is powered down.\n");
|
---|
176 | }
|
---|
177 | }
|
---|
178 | else
|
---|
179 | if (!strcmp(operation, "pause"))
|
---|
180 | {
|
---|
181 | RTPrintf("Pausing the VM...\n");
|
---|
182 | CHECK_ERROR_BREAK(console, Pause());
|
---|
183 | }
|
---|
184 | else
|
---|
185 | if (!strcmp(operation, "resume"))
|
---|
186 | {
|
---|
187 | RTPrintf("Resuming the VM...\n");
|
---|
188 | CHECK_ERROR_BREAK(console, Resume());
|
---|
189 | }
|
---|
190 | else
|
---|
191 | {
|
---|
192 | RTPrintf("Invalid operation!\n");
|
---|
193 | }
|
---|
194 | }
|
---|
195 |
|
---|
196 | RTPrintf("Closing the session (may fail after power off)...\n");
|
---|
197 | CHECK_ERROR(session, UnlockMachine());
|
---|
198 | }
|
---|
199 | while (0);
|
---|
200 | RTPrintf("\n");
|
---|
201 |
|
---|
202 | com::Shutdown();
|
---|
203 |
|
---|
204 | RTPrintf("tstHeadless FINISHED.\n");
|
---|
205 |
|
---|
206 | return rc;
|
---|
207 | }
|
---|
208 |
|
---|