1 | /* $Id: VBoxAutostartUtils.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxAutostart - VirtualBox Autostart service, start machines during system boot.
|
---|
4 | * Utils used by the windows and posix frontends.
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2012-2019 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/array.h>
|
---|
23 | #include <VBox/com/ErrorInfo.h>
|
---|
24 | #include <VBox/com/errorprint.h>
|
---|
25 |
|
---|
26 | #include <VBox/err.h>
|
---|
27 |
|
---|
28 | #include <iprt/message.h>
|
---|
29 | #include <iprt/thread.h>
|
---|
30 | #include <iprt/stream.h>
|
---|
31 | #include <iprt/log.h>
|
---|
32 | #include <iprt/path.h>
|
---|
33 |
|
---|
34 | #include <algorithm>
|
---|
35 | #include <list>
|
---|
36 | #include <string>
|
---|
37 |
|
---|
38 | #include "VBoxAutostart.h"
|
---|
39 |
|
---|
40 | using namespace com;
|
---|
41 |
|
---|
42 | DECLHIDDEN(const char *) machineStateToName(MachineState_T machineState, bool fShort)
|
---|
43 | {
|
---|
44 | switch (machineState)
|
---|
45 | {
|
---|
46 | case MachineState_PoweredOff:
|
---|
47 | return fShort ? "poweroff" : "powered off";
|
---|
48 | case MachineState_Saved:
|
---|
49 | return "saved";
|
---|
50 | case MachineState_Teleported:
|
---|
51 | return "teleported";
|
---|
52 | case MachineState_Aborted:
|
---|
53 | return "aborted";
|
---|
54 | case MachineState_Running:
|
---|
55 | return "running";
|
---|
56 | case MachineState_Paused:
|
---|
57 | return "paused";
|
---|
58 | case MachineState_Stuck:
|
---|
59 | return fShort ? "gurumeditation" : "guru meditation";
|
---|
60 | case MachineState_Teleporting:
|
---|
61 | return "teleporting";
|
---|
62 | case MachineState_LiveSnapshotting:
|
---|
63 | return fShort ? "livesnapshotting" : "live snapshotting";
|
---|
64 | case MachineState_Starting:
|
---|
65 | return "starting";
|
---|
66 | case MachineState_Stopping:
|
---|
67 | return "stopping";
|
---|
68 | case MachineState_Saving:
|
---|
69 | return "saving";
|
---|
70 | case MachineState_Restoring:
|
---|
71 | return "restoring";
|
---|
72 | case MachineState_TeleportingPausedVM:
|
---|
73 | return fShort ? "teleportingpausedvm" : "teleporting paused vm";
|
---|
74 | case MachineState_TeleportingIn:
|
---|
75 | return fShort ? "teleportingin" : "teleporting (incoming)";
|
---|
76 | case MachineState_DeletingSnapshotOnline:
|
---|
77 | return fShort ? "deletingsnapshotlive" : "deleting snapshot live";
|
---|
78 | case MachineState_DeletingSnapshotPaused:
|
---|
79 | return fShort ? "deletingsnapshotlivepaused" : "deleting snapshot live paused";
|
---|
80 | case MachineState_OnlineSnapshotting:
|
---|
81 | return fShort ? "onlinesnapshotting" : "online snapshotting";
|
---|
82 | case MachineState_RestoringSnapshot:
|
---|
83 | return fShort ? "restoringsnapshot" : "restoring snapshot";
|
---|
84 | case MachineState_DeletingSnapshot:
|
---|
85 | return fShort ? "deletingsnapshot" : "deleting snapshot";
|
---|
86 | case MachineState_SettingUp:
|
---|
87 | return fShort ? "settingup" : "setting up";
|
---|
88 | case MachineState_Snapshotting:
|
---|
89 | return "snapshotting";
|
---|
90 | default:
|
---|
91 | break;
|
---|
92 | }
|
---|
93 | return "unknown";
|
---|
94 | }
|
---|
95 |
|
---|
96 | DECLHIDDEN(void) autostartSvcLogErrorV(const char *pszFormat, va_list va)
|
---|
97 | {
|
---|
98 | if (*pszFormat)
|
---|
99 | {
|
---|
100 | char *pszMsg = NULL;
|
---|
101 | if (RTStrAPrintfV(&pszMsg, pszFormat, va) != -1)
|
---|
102 | {
|
---|
103 | autostartSvcOsLogStr(pszMsg, AUTOSTARTLOGTYPE_ERROR);
|
---|
104 | RTStrFree(pszMsg);
|
---|
105 | }
|
---|
106 | else
|
---|
107 | autostartSvcOsLogStr(pszFormat, AUTOSTARTLOGTYPE_ERROR);
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | DECLHIDDEN(void) autostartSvcLogError(const char *pszFormat, ...)
|
---|
112 | {
|
---|
113 | va_list va;
|
---|
114 | va_start(va, pszFormat);
|
---|
115 | autostartSvcLogErrorV(pszFormat, va);
|
---|
116 | va_end(va);
|
---|
117 | }
|
---|
118 |
|
---|
119 | DECLHIDDEN(void) autostartSvcLogVerboseV(const char *pszFormat, va_list va)
|
---|
120 | {
|
---|
121 | if (*pszFormat)
|
---|
122 | {
|
---|
123 | char *pszMsg = NULL;
|
---|
124 | if (RTStrAPrintfV(&pszMsg, pszFormat, va) != -1)
|
---|
125 | {
|
---|
126 | autostartSvcOsLogStr(pszMsg, AUTOSTARTLOGTYPE_VERBOSE);
|
---|
127 | RTStrFree(pszMsg);
|
---|
128 | }
|
---|
129 | else
|
---|
130 | autostartSvcOsLogStr(pszFormat, AUTOSTARTLOGTYPE_VERBOSE);
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | DECLHIDDEN(void) autostartSvcLogVerbose(const char *pszFormat, ...)
|
---|
135 | {
|
---|
136 | va_list va;
|
---|
137 | va_start(va, pszFormat);
|
---|
138 | autostartSvcLogVerboseV(pszFormat, va);
|
---|
139 | va_end(va);
|
---|
140 | }
|
---|
141 |
|
---|
142 | DECLHIDDEN(void) autostartSvcLogWarningV(const char *pszFormat, va_list va)
|
---|
143 | {
|
---|
144 | if (*pszFormat)
|
---|
145 | {
|
---|
146 | char *pszMsg = NULL;
|
---|
147 | if (RTStrAPrintfV(&pszMsg, pszFormat, va) != -1)
|
---|
148 | {
|
---|
149 | autostartSvcOsLogStr(pszMsg, AUTOSTARTLOGTYPE_WARNING);
|
---|
150 | RTStrFree(pszMsg);
|
---|
151 | }
|
---|
152 | else
|
---|
153 | autostartSvcOsLogStr(pszFormat, AUTOSTARTLOGTYPE_WARNING);
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 | DECLHIDDEN(void) autostartSvcLogInfo(const char *pszFormat, ...)
|
---|
158 | {
|
---|
159 | va_list va;
|
---|
160 | va_start(va, pszFormat);
|
---|
161 | autostartSvcLogInfoV(pszFormat, va);
|
---|
162 | va_end(va);
|
---|
163 | }
|
---|
164 |
|
---|
165 | DECLHIDDEN(void) autostartSvcLogInfoV(const char *pszFormat, va_list va)
|
---|
166 | {
|
---|
167 | if (*pszFormat)
|
---|
168 | {
|
---|
169 | char *pszMsg = NULL;
|
---|
170 | if (RTStrAPrintfV(&pszMsg, pszFormat, va) != -1)
|
---|
171 | {
|
---|
172 | autostartSvcOsLogStr(pszMsg, AUTOSTARTLOGTYPE_INFO);
|
---|
173 | RTStrFree(pszMsg);
|
---|
174 | }
|
---|
175 | else
|
---|
176 | autostartSvcOsLogStr(pszFormat, AUTOSTARTLOGTYPE_INFO);
|
---|
177 | }
|
---|
178 | }
|
---|
179 |
|
---|
180 | DECLHIDDEN(void) autostartSvcLogWarning(const char *pszFormat, ...)
|
---|
181 | {
|
---|
182 | va_list va;
|
---|
183 | va_start(va, pszFormat);
|
---|
184 | autostartSvcLogWarningV(pszFormat, va);
|
---|
185 | va_end(va);
|
---|
186 | }
|
---|
187 |
|
---|
188 | DECLHIDDEN(RTEXITCODE) autostartSvcLogGetOptError(const char *pszAction, int rc, int argc, char **argv, int iArg, PCRTGETOPTUNION pValue)
|
---|
189 | {
|
---|
190 | NOREF(pValue);
|
---|
191 | autostartSvcLogError("%s - RTGetOpt failure, %Rrc (%d): %s",
|
---|
192 | pszAction, rc, rc, iArg < argc ? argv[iArg] : "<null>");
|
---|
193 | return RTEXITCODE_FAILURE;
|
---|
194 | }
|
---|
195 |
|
---|
196 | DECLHIDDEN(RTEXITCODE) autostartSvcLogTooManyArgsError(const char *pszAction, int argc, char **argv, int iArg)
|
---|
197 | {
|
---|
198 | Assert(iArg < argc);
|
---|
199 | autostartSvcLogError("%s - Too many arguments: %s", pszAction, argv[iArg]);
|
---|
200 | for ( ; iArg < argc; iArg++)
|
---|
201 | LogRel(("arg#%i: %s\n", iArg, argv[iArg]));
|
---|
202 | return RTEXITCODE_FAILURE;
|
---|
203 | }
|
---|
204 |
|
---|
205 | DECLHIDDEN(void) autostartSvcDisplayErrorV(const char *pszFormat, va_list va)
|
---|
206 | {
|
---|
207 | RTStrmPrintf(g_pStdErr, "VBoxSupSvc error: ");
|
---|
208 | RTStrmPrintfV(g_pStdErr, pszFormat, va);
|
---|
209 | Log(("autostartSvcDisplayErrorV: %s", pszFormat)); /** @todo format it! */
|
---|
210 | }
|
---|
211 |
|
---|
212 | DECLHIDDEN(void) autostartSvcDisplayError(const char *pszFormat, ...)
|
---|
213 | {
|
---|
214 | va_list va;
|
---|
215 | va_start(va, pszFormat);
|
---|
216 | autostartSvcDisplayErrorV(pszFormat, va);
|
---|
217 | va_end(va);
|
---|
218 | }
|
---|
219 |
|
---|
220 | DECLHIDDEN(RTEXITCODE) autostartSvcDisplayGetOptError(const char *pszAction, int rc, int argc, char **argv, int iArg,
|
---|
221 | PCRTGETOPTUNION pValue)
|
---|
222 | {
|
---|
223 | RT_NOREF(pValue);
|
---|
224 | autostartSvcDisplayError("%s - RTGetOpt failure, %Rrc (%d): %s\n",
|
---|
225 | pszAction, rc, rc, iArg < argc ? argv[iArg] : "<null>");
|
---|
226 | return RTEXITCODE_FAILURE;
|
---|
227 | }
|
---|
228 |
|
---|
229 | DECLHIDDEN(RTEXITCODE) autostartSvcDisplayTooManyArgsError(const char *pszAction, int argc, char **argv, int iArg)
|
---|
230 | {
|
---|
231 | RT_NOREF(argc);
|
---|
232 | Assert(iArg < argc);
|
---|
233 | autostartSvcDisplayError("%s - Too many arguments: %s\n", pszAction, argv[iArg]);
|
---|
234 | return RTEXITCODE_FAILURE;
|
---|
235 | }
|
---|
236 |
|
---|
237 | DECLHIDDEN(int) autostartSetup()
|
---|
238 | {
|
---|
239 | autostartSvcOsLogStr("Setting up ...\n", AUTOSTARTLOGTYPE_VERBOSE);
|
---|
240 |
|
---|
241 | /*
|
---|
242 | * Initialize COM.
|
---|
243 | */
|
---|
244 | using namespace com;
|
---|
245 | HRESULT hrc = com::Initialize();
|
---|
246 | # ifdef VBOX_WITH_XPCOM
|
---|
247 | if (hrc == NS_ERROR_FILE_ACCESS_DENIED)
|
---|
248 | {
|
---|
249 | char szHome[RTPATH_MAX] = "";
|
---|
250 | com::GetVBoxUserHomeDirectory(szHome, sizeof(szHome));
|
---|
251 | return RTMsgErrorExit(RTEXITCODE_FAILURE,
|
---|
252 | "Failed to initialize COM because the global settings directory '%s' is not accessible!", szHome);
|
---|
253 | }
|
---|
254 | # endif
|
---|
255 | if (FAILED(hrc))
|
---|
256 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to initialize COM (%Rhrc)!", hrc);
|
---|
257 |
|
---|
258 | hrc = g_pVirtualBoxClient.createInprocObject(CLSID_VirtualBoxClient);
|
---|
259 | if (FAILED(hrc))
|
---|
260 | {
|
---|
261 | RTMsgError("Failed to create the VirtualBoxClient object (%Rhrc)!", hrc);
|
---|
262 | com::ErrorInfo info;
|
---|
263 | if (!info.isFullAvailable() && !info.isBasicAvailable())
|
---|
264 | {
|
---|
265 | com::GluePrintRCMessage(hrc);
|
---|
266 | RTMsgError("Most likely, the VirtualBox COM server is not running or failed to start.");
|
---|
267 | }
|
---|
268 | else
|
---|
269 | com::GluePrintErrorInfo(info);
|
---|
270 | return RTEXITCODE_FAILURE;
|
---|
271 | }
|
---|
272 |
|
---|
273 | /*
|
---|
274 | * Setup VirtualBox + session interfaces.
|
---|
275 | */
|
---|
276 | HRESULT rc = g_pVirtualBoxClient->COMGETTER(VirtualBox)(g_pVirtualBox.asOutParam());
|
---|
277 | if (SUCCEEDED(rc))
|
---|
278 | {
|
---|
279 | rc = g_pSession.createInprocObject(CLSID_Session);
|
---|
280 | if (FAILED(rc))
|
---|
281 | RTMsgError("Failed to create a session object (rc=%Rhrc)!", rc);
|
---|
282 | }
|
---|
283 | else
|
---|
284 | RTMsgError("Failed to get VirtualBox object (rc=%Rhrc)!", rc);
|
---|
285 |
|
---|
286 | if (FAILED(rc))
|
---|
287 | return VERR_COM_OBJECT_NOT_FOUND;
|
---|
288 |
|
---|
289 | return VINF_SUCCESS;
|
---|
290 | }
|
---|
291 |
|
---|
292 | DECLHIDDEN(void) autostartShutdown()
|
---|
293 | {
|
---|
294 | autostartSvcOsLogStr("Shutting down ...\n", AUTOSTARTLOGTYPE_VERBOSE);
|
---|
295 |
|
---|
296 | g_pSession.setNull();
|
---|
297 | g_pVirtualBox.setNull();
|
---|
298 | g_pVirtualBoxClient.setNull();
|
---|
299 | com::Shutdown();
|
---|
300 | }
|
---|
301 |
|
---|