1 | /* $Id: VBoxAutostartUtils.cpp 43967 2012-11-26 19:35:33Z 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 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_Aborted:
|
---|
51 | return "aborted";
|
---|
52 | case MachineState_Teleported:
|
---|
53 | return "teleported";
|
---|
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_LiveSnapshotting:
|
---|
61 | return fShort ? "livesnapshotting" : "live snapshotting";
|
---|
62 | case MachineState_Teleporting:
|
---|
63 | return "teleporting";
|
---|
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_RestoringSnapshot:
|
---|
77 | return fShort ? "restoringsnapshot" : "restoring snapshot";
|
---|
78 | case MachineState_DeletingSnapshot:
|
---|
79 | return fShort ? "deletingsnapshot" : "deleting snapshot";
|
---|
80 | case MachineState_DeletingSnapshotOnline:
|
---|
81 | return fShort ? "deletingsnapshotlive" : "deleting snapshot live";
|
---|
82 | case MachineState_DeletingSnapshotPaused:
|
---|
83 | return fShort ? "deletingsnapshotlivepaused" : "deleting snapshot live paused";
|
---|
84 | case MachineState_SettingUp:
|
---|
85 | return fShort ? "settingup" : "setting up";
|
---|
86 | default:
|
---|
87 | break;
|
---|
88 | }
|
---|
89 | return "unknown";
|
---|
90 | }
|
---|
91 |
|
---|
92 | DECLHIDDEN(void) autostartSvcLogErrorV(const char *pszFormat, va_list va)
|
---|
93 | {
|
---|
94 | if (*pszFormat)
|
---|
95 | {
|
---|
96 | char *pszMsg = NULL;
|
---|
97 | if (RTStrAPrintfV(&pszMsg, pszFormat, va) != -1)
|
---|
98 | {
|
---|
99 | autostartSvcOsLogStr(pszMsg, AUTOSTARTLOGTYPE_ERROR);
|
---|
100 | RTStrFree(pszMsg);
|
---|
101 | }
|
---|
102 | else
|
---|
103 | autostartSvcOsLogStr(pszFormat, AUTOSTARTLOGTYPE_ERROR);
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | DECLHIDDEN(void) autostartSvcLogError(const char *pszFormat, ...)
|
---|
108 | {
|
---|
109 | va_list va;
|
---|
110 | va_start(va, pszFormat);
|
---|
111 | autostartSvcLogErrorV(pszFormat, va);
|
---|
112 | va_end(va);
|
---|
113 | }
|
---|
114 |
|
---|
115 | DECLHIDDEN(void) autostartSvcLogVerboseV(const char *pszFormat, va_list va)
|
---|
116 | {
|
---|
117 | if (*pszFormat)
|
---|
118 | {
|
---|
119 | char *pszMsg = NULL;
|
---|
120 | if (RTStrAPrintfV(&pszMsg, pszFormat, va) != -1)
|
---|
121 | {
|
---|
122 | autostartSvcOsLogStr(pszMsg, AUTOSTARTLOGTYPE_VERBOSE);
|
---|
123 | RTStrFree(pszMsg);
|
---|
124 | }
|
---|
125 | else
|
---|
126 | autostartSvcOsLogStr(pszFormat, AUTOSTARTLOGTYPE_VERBOSE);
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | DECLHIDDEN(void) autostartSvcLogVerbose(const char *pszFormat, ...)
|
---|
131 | {
|
---|
132 | va_list va;
|
---|
133 | va_start(va, pszFormat);
|
---|
134 | autostartSvcLogVerboseV(pszFormat, va);
|
---|
135 | va_end(va);
|
---|
136 | }
|
---|
137 |
|
---|
138 | DECLHIDDEN(void) autostartSvcLogWarningV(const char *pszFormat, va_list va)
|
---|
139 | {
|
---|
140 | if (*pszFormat)
|
---|
141 | {
|
---|
142 | char *pszMsg = NULL;
|
---|
143 | if (RTStrAPrintfV(&pszMsg, pszFormat, va) != -1)
|
---|
144 | {
|
---|
145 | autostartSvcOsLogStr(pszMsg, AUTOSTARTLOGTYPE_WARNING);
|
---|
146 | RTStrFree(pszMsg);
|
---|
147 | }
|
---|
148 | else
|
---|
149 | autostartSvcOsLogStr(pszFormat, AUTOSTARTLOGTYPE_WARNING);
|
---|
150 | }
|
---|
151 | }
|
---|
152 |
|
---|
153 | DECLHIDDEN(void) autostartSvcLogInfo(const char *pszFormat, ...)
|
---|
154 | {
|
---|
155 | va_list va;
|
---|
156 | va_start(va, pszFormat);
|
---|
157 | autostartSvcLogInfoV(pszFormat, va);
|
---|
158 | va_end(va);
|
---|
159 | }
|
---|
160 |
|
---|
161 | DECLHIDDEN(void) autostartSvcLogInfoV(const char *pszFormat, va_list va)
|
---|
162 | {
|
---|
163 | if (*pszFormat)
|
---|
164 | {
|
---|
165 | char *pszMsg = NULL;
|
---|
166 | if (RTStrAPrintfV(&pszMsg, pszFormat, va) != -1)
|
---|
167 | {
|
---|
168 | autostartSvcOsLogStr(pszMsg, AUTOSTARTLOGTYPE_INFO);
|
---|
169 | RTStrFree(pszMsg);
|
---|
170 | }
|
---|
171 | else
|
---|
172 | autostartSvcOsLogStr(pszFormat, AUTOSTARTLOGTYPE_INFO);
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | DECLHIDDEN(void) autostartSvcLogWarning(const char *pszFormat, ...)
|
---|
177 | {
|
---|
178 | va_list va;
|
---|
179 | va_start(va, pszFormat);
|
---|
180 | autostartSvcLogWarningV(pszFormat, va);
|
---|
181 | va_end(va);
|
---|
182 | }
|
---|
183 |
|
---|
184 | DECLHIDDEN(RTEXITCODE) autostartSvcLogGetOptError(const char *pszAction, int rc, int argc, char **argv, int iArg, PCRTGETOPTUNION pValue)
|
---|
185 | {
|
---|
186 | NOREF(pValue);
|
---|
187 | autostartSvcLogError("%s - RTGetOpt failure, %Rrc (%d): %s",
|
---|
188 | pszAction, rc, rc, iArg < argc ? argv[iArg] : "<null>");
|
---|
189 | return RTEXITCODE_FAILURE;
|
---|
190 | }
|
---|
191 |
|
---|
192 | DECLHIDDEN(RTEXITCODE) autostartSvcLogTooManyArgsError(const char *pszAction, int argc, char **argv, int iArg)
|
---|
193 | {
|
---|
194 | Assert(iArg < argc);
|
---|
195 | autostartSvcLogError("%s - Too many arguments: %s", pszAction, argv[iArg]);
|
---|
196 | for ( ; iArg < argc; iArg++)
|
---|
197 | LogRel(("arg#%i: %s\n", iArg, argv[iArg]));
|
---|
198 | return RTEXITCODE_FAILURE;
|
---|
199 | }
|
---|
200 |
|
---|
201 | DECLHIDDEN(void) autostartSvcDisplayErrorV(const char *pszFormat, va_list va)
|
---|
202 | {
|
---|
203 | RTStrmPrintf(g_pStdErr, "VBoxSupSvc error: ");
|
---|
204 | RTStrmPrintfV(g_pStdErr, pszFormat, va);
|
---|
205 | Log(("autostartSvcDisplayErrorV: %s", pszFormat)); /** @todo format it! */
|
---|
206 | }
|
---|
207 |
|
---|
208 | DECLHIDDEN(void) autostartSvcDisplayError(const char *pszFormat, ...)
|
---|
209 | {
|
---|
210 | va_list va;
|
---|
211 | va_start(va, pszFormat);
|
---|
212 | autostartSvcDisplayErrorV(pszFormat, va);
|
---|
213 | va_end(va);
|
---|
214 | }
|
---|
215 |
|
---|
216 | DECLHIDDEN(RTEXITCODE) autostartSvcDisplayGetOptError(const char *pszAction, int rc, int argc, char **argv, int iArg, PCRTGETOPTUNION pValue)
|
---|
217 | {
|
---|
218 | autostartSvcDisplayError("%s - RTGetOpt failure, %Rrc (%d): %s\n",
|
---|
219 | pszAction, rc, rc, iArg < argc ? argv[iArg] : "<null>");
|
---|
220 | return RTEXITCODE_FAILURE;
|
---|
221 | }
|
---|
222 |
|
---|
223 | DECLHIDDEN(RTEXITCODE) autostartSvcDisplayTooManyArgsError(const char *pszAction, int argc, char **argv, int iArg)
|
---|
224 | {
|
---|
225 | Assert(iArg < argc);
|
---|
226 | autostartSvcDisplayError("%s - Too many arguments: %s\n", pszAction, argv[iArg]);
|
---|
227 | return RTEXITCODE_FAILURE;
|
---|
228 | }
|
---|
229 |
|
---|
230 | DECLHIDDEN(int) autostartSetup()
|
---|
231 | {
|
---|
232 | autostartSvcOsLogStr("Setting up ...\n", AUTOSTARTLOGTYPE_VERBOSE);
|
---|
233 |
|
---|
234 | /*
|
---|
235 | * Initialize COM.
|
---|
236 | */
|
---|
237 | using namespace com;
|
---|
238 | HRESULT hrc = com::Initialize();
|
---|
239 | # ifdef VBOX_WITH_XPCOM
|
---|
240 | if (hrc == NS_ERROR_FILE_ACCESS_DENIED)
|
---|
241 | {
|
---|
242 | char szHome[RTPATH_MAX] = "";
|
---|
243 | com::GetVBoxUserHomeDirectory(szHome, sizeof(szHome));
|
---|
244 | return RTMsgErrorExit(RTEXITCODE_FAILURE,
|
---|
245 | "Failed to initialize COM because the global settings directory '%s' is not accessible!", szHome);
|
---|
246 | }
|
---|
247 | # endif
|
---|
248 | if (FAILED(hrc))
|
---|
249 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to initialize COM (%Rhrc)!", hrc);
|
---|
250 |
|
---|
251 | hrc = g_pVirtualBoxClient.createInprocObject(CLSID_VirtualBoxClient);
|
---|
252 | if (FAILED(hrc))
|
---|
253 | {
|
---|
254 | RTMsgError("Failed to create the VirtualBoxClient object (%Rhrc)!", hrc);
|
---|
255 | com::ErrorInfo info;
|
---|
256 | if (!info.isFullAvailable() && !info.isBasicAvailable())
|
---|
257 | {
|
---|
258 | com::GluePrintRCMessage(hrc);
|
---|
259 | RTMsgError("Most likely, the VirtualBox COM server is not running or failed to start.");
|
---|
260 | }
|
---|
261 | else
|
---|
262 | com::GluePrintErrorInfo(info);
|
---|
263 | return RTEXITCODE_FAILURE;
|
---|
264 | }
|
---|
265 |
|
---|
266 | /*
|
---|
267 | * Setup VirtualBox + session interfaces.
|
---|
268 | */
|
---|
269 | HRESULT rc = g_pVirtualBoxClient->COMGETTER(VirtualBox)(g_pVirtualBox.asOutParam());
|
---|
270 | if (SUCCEEDED(rc))
|
---|
271 | {
|
---|
272 | rc = g_pSession.createInprocObject(CLSID_Session);
|
---|
273 | if (FAILED(rc))
|
---|
274 | RTMsgError("Failed to create a session object (rc=%Rhrc)!", rc);
|
---|
275 | }
|
---|
276 | else
|
---|
277 | RTMsgError("Failed to get VirtualBox object (rc=%Rhrc)!", rc);
|
---|
278 |
|
---|
279 | if (FAILED(rc))
|
---|
280 | return VERR_COM_OBJECT_NOT_FOUND;
|
---|
281 |
|
---|
282 | return VINF_SUCCESS;
|
---|
283 | }
|
---|
284 |
|
---|
285 | DECLHIDDEN(void) autostartShutdown()
|
---|
286 | {
|
---|
287 | autostartSvcOsLogStr("Shutting down ...\n", AUTOSTARTLOGTYPE_VERBOSE);
|
---|
288 |
|
---|
289 | g_pSession.setNull();
|
---|
290 | g_pVirtualBox.setNull();
|
---|
291 | g_pVirtualBoxClient.setNull();
|
---|
292 | com::Shutdown();
|
---|
293 | }
|
---|
294 |
|
---|