1 | /** @file
|
---|
2 | *
|
---|
3 | * VBox frontends: VBoxHeadless frontend:
|
---|
4 | * Testcases
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006-2009 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/com.h>
|
---|
20 | #include <VBox/com/string.h>
|
---|
21 | #include <VBox/com/Guid.h>
|
---|
22 | #include <VBox/com/ErrorInfo.h>
|
---|
23 | #include <VBox/com/errorprint.h>
|
---|
24 | #include <VBox/com/EventQueue.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 -1;
|
---|
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 <IVirtualBox> virtualBox;
|
---|
83 | ComPtr <ISession> session;
|
---|
84 |
|
---|
85 | RTPrintf ("Creating VirtualBox object...\n");
|
---|
86 | rc = virtualBox.createLocalObject (CLSID_VirtualBox);
|
---|
87 | if (FAILED(rc))
|
---|
88 | RTPrintf("ERROR: failed to create the VirtualBox object!\n");
|
---|
89 | else
|
---|
90 | {
|
---|
91 | rc = session.createInprocObject(CLSID_Session);
|
---|
92 | if (FAILED(rc))
|
---|
93 | RTPrintf("ERROR: failed to create a session object!\n");
|
---|
94 | }
|
---|
95 |
|
---|
96 | if (FAILED (rc))
|
---|
97 | {
|
---|
98 | com::ErrorInfo info;
|
---|
99 | if (!info.isFullAvailable() && !info.isBasicAvailable())
|
---|
100 | {
|
---|
101 | com::GluePrintRCMessage(rc);
|
---|
102 | RTPrintf("Most likely, the VirtualBox COM server is not running or failed to start.\n");
|
---|
103 | }
|
---|
104 | else
|
---|
105 | com::GluePrintErrorInfo(info);
|
---|
106 | break;
|
---|
107 | }
|
---|
108 |
|
---|
109 | // create the event queue
|
---|
110 | // (here it is necessary only to process remaining XPCOM/IPC events
|
---|
111 | // after the session is closed)
|
---|
112 | EventQueue eventQ;
|
---|
113 |
|
---|
114 | ComPtr <IMachine> m;
|
---|
115 |
|
---|
116 | // find ID by name
|
---|
117 | CHECK_ERROR_BREAK(virtualBox, FindMachine(Bstr(name).raw(),
|
---|
118 | m.asOutParam()));
|
---|
119 |
|
---|
120 | if (!strcmp(operation, "on"))
|
---|
121 | {
|
---|
122 | ComPtr <IProgress> progress;
|
---|
123 | RTPrintf ("Opening a new (remote) session...\n");
|
---|
124 | CHECK_ERROR_BREAK (m,
|
---|
125 | LaunchVMProcess(session, Bstr("vrdp").raw(),
|
---|
126 | NULL, progress.asOutParam()));
|
---|
127 |
|
---|
128 | RTPrintf ("Waiting for the remote session to open...\n");
|
---|
129 | CHECK_ERROR_BREAK (progress, WaitForCompletion (-1));
|
---|
130 |
|
---|
131 | BOOL completed;
|
---|
132 | CHECK_ERROR_BREAK (progress, COMGETTER(Completed) (&completed));
|
---|
133 | ASSERT (completed);
|
---|
134 |
|
---|
135 | LONG resultCode;
|
---|
136 | CHECK_ERROR_BREAK (progress, COMGETTER(ResultCode) (&resultCode));
|
---|
137 | if (FAILED (resultCode))
|
---|
138 | {
|
---|
139 | ProgressErrorInfo info(progress);
|
---|
140 | com::GluePrintErrorInfo(info);
|
---|
141 | }
|
---|
142 | else
|
---|
143 | {
|
---|
144 | RTPrintf ("Remote session has been successfully opened.\n");
|
---|
145 | }
|
---|
146 | }
|
---|
147 | else
|
---|
148 | {
|
---|
149 | RTPrintf ("Opening an existing session...\n");
|
---|
150 | CHECK_ERROR_BREAK(m, LockMachine(session, LockType_Shared));
|
---|
151 |
|
---|
152 | ComPtr <IConsole> console;
|
---|
153 | CHECK_ERROR_BREAK(session, COMGETTER(Console)(console.asOutParam()));
|
---|
154 |
|
---|
155 | if (!strcmp (operation, "off"))
|
---|
156 | {
|
---|
157 | ComPtr <IProgress> progress;
|
---|
158 | RTPrintf ("Powering the VM off...\n");
|
---|
159 | CHECK_ERROR_BREAK (console, PowerDown(progress.asOutParam()));
|
---|
160 |
|
---|
161 | RTPrintf ("Waiting for the VM to power down...\n");
|
---|
162 | CHECK_ERROR_BREAK (progress, WaitForCompletion (-1));
|
---|
163 |
|
---|
164 | BOOL completed;
|
---|
165 | CHECK_ERROR_BREAK (progress, COMGETTER(Completed) (&completed));
|
---|
166 | ASSERT (completed);
|
---|
167 |
|
---|
168 | LONG resultCode;
|
---|
169 | CHECK_ERROR_BREAK (progress, COMGETTER(ResultCode) (&resultCode));
|
---|
170 | if (FAILED (resultCode))
|
---|
171 | {
|
---|
172 | ProgressErrorInfo info(progress);
|
---|
173 | com::GluePrintErrorInfo(info);
|
---|
174 | }
|
---|
175 | else
|
---|
176 | {
|
---|
177 | RTPrintf ("VM is powered down.\n");
|
---|
178 | }
|
---|
179 | }
|
---|
180 | else
|
---|
181 | if (!strcmp (operation, "pause"))
|
---|
182 | {
|
---|
183 | RTPrintf ("Pausing the VM...\n");
|
---|
184 | CHECK_ERROR_BREAK (console, Pause());
|
---|
185 | }
|
---|
186 | else
|
---|
187 | if (!strcmp (operation, "resume"))
|
---|
188 | {
|
---|
189 | RTPrintf ("Resuming the VM...\n");
|
---|
190 | CHECK_ERROR_BREAK (console, Resume());
|
---|
191 | }
|
---|
192 | else
|
---|
193 | {
|
---|
194 | RTPrintf ("Invalid operation!\n");
|
---|
195 | }
|
---|
196 | }
|
---|
197 |
|
---|
198 | RTPrintf("Closing the session (may fail after power off)...\n");
|
---|
199 | CHECK_ERROR(session, UnlockMachine());
|
---|
200 | }
|
---|
201 | while (0);
|
---|
202 | RTPrintf ("\n");
|
---|
203 |
|
---|
204 | com::Shutdown();
|
---|
205 |
|
---|
206 | RTPrintf ("tstHeadless FINISHED.\n");
|
---|
207 |
|
---|
208 | return rc;
|
---|
209 | }
|
---|
210 |
|
---|