1 | /* $Id: VBoxAutostartUtils.cpp 80569 2019-09-03 14:34:21Z 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(RTEXITCODE) 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 | return RTEXITCODE_FAILURE;
|
---|
110 | }
|
---|
111 |
|
---|
112 | DECLHIDDEN(RTEXITCODE) autostartSvcLogError(const char *pszFormat, ...)
|
---|
113 | {
|
---|
114 | va_list va;
|
---|
115 | va_start(va, pszFormat);
|
---|
116 | autostartSvcLogErrorV(pszFormat, va);
|
---|
117 | va_end(va);
|
---|
118 | return RTEXITCODE_FAILURE;
|
---|
119 | }
|
---|
120 |
|
---|
121 | DECLHIDDEN(void) autostartSvcLogVerboseV(const char *pszFormat, va_list va)
|
---|
122 | {
|
---|
123 | if (*pszFormat)
|
---|
124 | {
|
---|
125 | char *pszMsg = NULL;
|
---|
126 | if (RTStrAPrintfV(&pszMsg, pszFormat, va) != -1)
|
---|
127 | {
|
---|
128 | autostartSvcOsLogStr(pszMsg, AUTOSTARTLOGTYPE_VERBOSE);
|
---|
129 | RTStrFree(pszMsg);
|
---|
130 | }
|
---|
131 | else
|
---|
132 | autostartSvcOsLogStr(pszFormat, AUTOSTARTLOGTYPE_VERBOSE);
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | DECLHIDDEN(void) autostartSvcLogVerbose(const char *pszFormat, ...)
|
---|
137 | {
|
---|
138 | va_list va;
|
---|
139 | va_start(va, pszFormat);
|
---|
140 | autostartSvcLogVerboseV(pszFormat, va);
|
---|
141 | va_end(va);
|
---|
142 | }
|
---|
143 |
|
---|
144 | DECLHIDDEN(void) autostartSvcLogWarningV(const char *pszFormat, va_list va)
|
---|
145 | {
|
---|
146 | if (*pszFormat)
|
---|
147 | {
|
---|
148 | char *pszMsg = NULL;
|
---|
149 | if (RTStrAPrintfV(&pszMsg, pszFormat, va) != -1)
|
---|
150 | {
|
---|
151 | autostartSvcOsLogStr(pszMsg, AUTOSTARTLOGTYPE_WARNING);
|
---|
152 | RTStrFree(pszMsg);
|
---|
153 | }
|
---|
154 | else
|
---|
155 | autostartSvcOsLogStr(pszFormat, AUTOSTARTLOGTYPE_WARNING);
|
---|
156 | }
|
---|
157 | }
|
---|
158 |
|
---|
159 | DECLHIDDEN(void) autostartSvcLogInfo(const char *pszFormat, ...)
|
---|
160 | {
|
---|
161 | va_list va;
|
---|
162 | va_start(va, pszFormat);
|
---|
163 | autostartSvcLogInfoV(pszFormat, va);
|
---|
164 | va_end(va);
|
---|
165 | }
|
---|
166 |
|
---|
167 | DECLHIDDEN(void) autostartSvcLogInfoV(const char *pszFormat, va_list va)
|
---|
168 | {
|
---|
169 | if (*pszFormat)
|
---|
170 | {
|
---|
171 | char *pszMsg = NULL;
|
---|
172 | if (RTStrAPrintfV(&pszMsg, pszFormat, va) != -1)
|
---|
173 | {
|
---|
174 | autostartSvcOsLogStr(pszMsg, AUTOSTARTLOGTYPE_INFO);
|
---|
175 | RTStrFree(pszMsg);
|
---|
176 | }
|
---|
177 | else
|
---|
178 | autostartSvcOsLogStr(pszFormat, AUTOSTARTLOGTYPE_INFO);
|
---|
179 | }
|
---|
180 | }
|
---|
181 |
|
---|
182 | DECLHIDDEN(void) autostartSvcLogWarning(const char *pszFormat, ...)
|
---|
183 | {
|
---|
184 | va_list va;
|
---|
185 | va_start(va, pszFormat);
|
---|
186 | autostartSvcLogWarningV(pszFormat, va);
|
---|
187 | va_end(va);
|
---|
188 | }
|
---|
189 |
|
---|
190 | DECLHIDDEN(RTEXITCODE) autostartSvcLogGetOptError(const char *pszAction, int rc, int argc, char **argv, int iArg, PCRTGETOPTUNION pValue)
|
---|
191 | {
|
---|
192 | NOREF(pValue);
|
---|
193 | autostartSvcLogError("%s - RTGetOpt failure, %Rrc (%d): %s",
|
---|
194 | pszAction, rc, rc, iArg < argc ? argv[iArg] : "<null>");
|
---|
195 | return RTEXITCODE_FAILURE;
|
---|
196 | }
|
---|
197 |
|
---|
198 | DECLHIDDEN(RTEXITCODE) autostartSvcLogTooManyArgsError(const char *pszAction, int argc, char **argv, int iArg)
|
---|
199 | {
|
---|
200 | Assert(iArg < argc);
|
---|
201 | autostartSvcLogError("%s - Too many arguments: %s", pszAction, argv[iArg]);
|
---|
202 | for ( ; iArg < argc; iArg++)
|
---|
203 | LogRel(("arg#%i: %s\n", iArg, argv[iArg]));
|
---|
204 | return RTEXITCODE_FAILURE;
|
---|
205 | }
|
---|
206 |
|
---|
207 | DECLHIDDEN(RTEXITCODE) autostartSvcDisplayErrorV(const char *pszFormat, va_list va)
|
---|
208 | {
|
---|
209 | RTStrmPrintf(g_pStdErr, "VBoxSupSvc error: ");
|
---|
210 | RTStrmPrintfV(g_pStdErr, pszFormat, va);
|
---|
211 | Log(("autostartSvcDisplayErrorV: %s", pszFormat)); /** @todo format it! */
|
---|
212 | return RTEXITCODE_FAILURE;
|
---|
213 | }
|
---|
214 |
|
---|
215 | DECLHIDDEN(RTEXITCODE) autostartSvcDisplayError(const char *pszFormat, ...)
|
---|
216 | {
|
---|
217 | va_list va;
|
---|
218 | va_start(va, pszFormat);
|
---|
219 | autostartSvcDisplayErrorV(pszFormat, va);
|
---|
220 | va_end(va);
|
---|
221 | return RTEXITCODE_FAILURE;
|
---|
222 | }
|
---|
223 |
|
---|
224 | DECLHIDDEN(RTEXITCODE) autostartSvcDisplayGetOptError(const char *pszAction, int rc, PCRTGETOPTUNION pValue)
|
---|
225 | {
|
---|
226 | char szMsg[4096];
|
---|
227 | RTGetOptFormatError(szMsg, sizeof(szMsg), rc, pValue);
|
---|
228 | autostartSvcDisplayError("%s - %s", pszAction, szMsg);
|
---|
229 | return RTEXITCODE_SYNTAX;
|
---|
230 | }
|
---|
231 |
|
---|
232 | DECLHIDDEN(int) autostartSetup()
|
---|
233 | {
|
---|
234 | autostartSvcOsLogStr("Setting up ...\n", AUTOSTARTLOGTYPE_VERBOSE);
|
---|
235 |
|
---|
236 | /*
|
---|
237 | * Initialize COM.
|
---|
238 | */
|
---|
239 | using namespace com;
|
---|
240 | HRESULT hrc = com::Initialize();
|
---|
241 | # ifdef VBOX_WITH_XPCOM
|
---|
242 | if (hrc == NS_ERROR_FILE_ACCESS_DENIED)
|
---|
243 | {
|
---|
244 | char szHome[RTPATH_MAX] = "";
|
---|
245 | com::GetVBoxUserHomeDirectory(szHome, sizeof(szHome));
|
---|
246 | return RTMsgErrorExit(RTEXITCODE_FAILURE,
|
---|
247 | "Failed to initialize COM because the global settings directory '%s' is not accessible!", szHome);
|
---|
248 | }
|
---|
249 | # endif
|
---|
250 | if (FAILED(hrc))
|
---|
251 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to initialize COM (%Rhrc)!", hrc);
|
---|
252 |
|
---|
253 | hrc = g_pVirtualBoxClient.createInprocObject(CLSID_VirtualBoxClient);
|
---|
254 | if (FAILED(hrc))
|
---|
255 | {
|
---|
256 | RTMsgError("Failed to create the VirtualBoxClient object (%Rhrc)!", hrc);
|
---|
257 | com::ErrorInfo info;
|
---|
258 | if (!info.isFullAvailable() && !info.isBasicAvailable())
|
---|
259 | {
|
---|
260 | com::GluePrintRCMessage(hrc);
|
---|
261 | RTMsgError("Most likely, the VirtualBox COM server is not running or failed to start.");
|
---|
262 | }
|
---|
263 | else
|
---|
264 | com::GluePrintErrorInfo(info);
|
---|
265 | return RTEXITCODE_FAILURE;
|
---|
266 | }
|
---|
267 |
|
---|
268 | /*
|
---|
269 | * Setup VirtualBox + session interfaces.
|
---|
270 | */
|
---|
271 | HRESULT rc = g_pVirtualBoxClient->COMGETTER(VirtualBox)(g_pVirtualBox.asOutParam());
|
---|
272 | if (SUCCEEDED(rc))
|
---|
273 | {
|
---|
274 | rc = g_pSession.createInprocObject(CLSID_Session);
|
---|
275 | if (FAILED(rc))
|
---|
276 | RTMsgError("Failed to create a session object (rc=%Rhrc)!", rc);
|
---|
277 | }
|
---|
278 | else
|
---|
279 | RTMsgError("Failed to get VirtualBox object (rc=%Rhrc)!", rc);
|
---|
280 |
|
---|
281 | if (FAILED(rc))
|
---|
282 | return VERR_COM_OBJECT_NOT_FOUND;
|
---|
283 |
|
---|
284 | return VINF_SUCCESS;
|
---|
285 | }
|
---|
286 |
|
---|
287 | DECLHIDDEN(void) autostartShutdown()
|
---|
288 | {
|
---|
289 | autostartSvcOsLogStr("Shutting down ...\n", AUTOSTARTLOGTYPE_VERBOSE);
|
---|
290 |
|
---|
291 | g_pSession.setNull();
|
---|
292 | g_pVirtualBox.setNull();
|
---|
293 | g_pVirtualBoxClient.setNull();
|
---|
294 | com::Shutdown();
|
---|
295 | }
|
---|
296 |
|
---|