VirtualBox

source: vbox/trunk/src/VBox/Frontends/VBoxAutostart/VBoxAutostartUtils.cpp@ 92616

Last change on this file since 92616 was 91363, checked in by vboxsync, 3 years ago

FE/VBoxSDL+VirtualBox,Main/Console+Machine+VirtualBox.xidl: VMs which
crash while restoring from the 'Saved' state shouldn't lose their saved
state file. bugref:1484

A new machine state named 'AbortedSaved' has been added which a VM will
enter if it crashes when restoring from the 'Saved' state before the
'Running' state has been reached. A VM in the 'AbortedSaved' machine
state will have its saved state file preserved so that the VM can still be
restored once the cause of the failure to powerUp() and reach the
'Running' state has been resolved.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.0 KB
Line 
1/* $Id: VBoxAutostartUtils.cpp 91363 2021-09-24 13:08:32Z 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-2020 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> /* RTPATH_MAX */
33
34#include "VBoxAutostart.h"
35
36using namespace com;
37
38DECLHIDDEN(const char *) machineStateToName(MachineState_T machineState, bool fShort)
39{
40 switch (machineState)
41 {
42 case MachineState_PoweredOff:
43 return fShort ? "poweroff" : "powered off";
44 case MachineState_Saved:
45 return "saved";
46 case MachineState_Teleported:
47 return "teleported";
48 case MachineState_Aborted:
49 return "aborted";
50 case MachineState_AbortedSaved:
51 return "aborted-saved";
52 case MachineState_Running:
53 return "running";
54 case MachineState_Paused:
55 return "paused";
56 case MachineState_Stuck:
57 return fShort ? "gurumeditation" : "guru meditation";
58 case MachineState_Teleporting:
59 return "teleporting";
60 case MachineState_LiveSnapshotting:
61 return fShort ? "livesnapshotting" : "live snapshotting";
62 case MachineState_Starting:
63 return "starting";
64 case MachineState_Stopping:
65 return "stopping";
66 case MachineState_Saving:
67 return "saving";
68 case MachineState_Restoring:
69 return "restoring";
70 case MachineState_TeleportingPausedVM:
71 return fShort ? "teleportingpausedvm" : "teleporting paused vm";
72 case MachineState_TeleportingIn:
73 return fShort ? "teleportingin" : "teleporting (incoming)";
74 case MachineState_DeletingSnapshotOnline:
75 return fShort ? "deletingsnapshotlive" : "deleting snapshot live";
76 case MachineState_DeletingSnapshotPaused:
77 return fShort ? "deletingsnapshotlivepaused" : "deleting snapshot live paused";
78 case MachineState_OnlineSnapshotting:
79 return fShort ? "onlinesnapshotting" : "online snapshotting";
80 case MachineState_RestoringSnapshot:
81 return fShort ? "restoringsnapshot" : "restoring snapshot";
82 case MachineState_DeletingSnapshot:
83 return fShort ? "deletingsnapshot" : "deleting snapshot";
84 case MachineState_SettingUp:
85 return fShort ? "settingup" : "setting up";
86 case MachineState_Snapshotting:
87 return "snapshotting";
88 default:
89 break;
90 }
91 return "unknown";
92}
93
94DECLHIDDEN(RTEXITCODE) autostartSvcLogErrorV(const char *pszFormat, va_list va)
95{
96 if (*pszFormat)
97 {
98 char *pszMsg = NULL;
99 if (RTStrAPrintfV(&pszMsg, pszFormat, va) != -1)
100 {
101 autostartSvcOsLogStr(pszMsg, AUTOSTARTLOGTYPE_ERROR);
102 RTStrFree(pszMsg);
103 }
104 else
105 autostartSvcOsLogStr(pszFormat, AUTOSTARTLOGTYPE_ERROR);
106 }
107 return RTEXITCODE_FAILURE;
108}
109
110DECLHIDDEN(RTEXITCODE) autostartSvcLogError(const char *pszFormat, ...)
111{
112 va_list va;
113 va_start(va, pszFormat);
114 autostartSvcLogErrorV(pszFormat, va);
115 va_end(va);
116 return RTEXITCODE_FAILURE;
117}
118
119DECLHIDDEN(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
134DECLHIDDEN(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
142DECLHIDDEN(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
157DECLHIDDEN(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
165DECLHIDDEN(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
180DECLHIDDEN(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
188DECLHIDDEN(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
196DECLHIDDEN(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
205DECLHIDDEN(RTEXITCODE) 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 return RTEXITCODE_FAILURE;
211}
212
213DECLHIDDEN(RTEXITCODE) autostartSvcDisplayError(const char *pszFormat, ...)
214{
215 va_list va;
216 va_start(va, pszFormat);
217 autostartSvcDisplayErrorV(pszFormat, va);
218 va_end(va);
219 return RTEXITCODE_FAILURE;
220}
221
222DECLHIDDEN(RTEXITCODE) autostartSvcDisplayGetOptError(const char *pszAction, int rc, PCRTGETOPTUNION pValue)
223{
224 char szMsg[4096];
225 RTGetOptFormatError(szMsg, sizeof(szMsg), rc, pValue);
226 autostartSvcDisplayError("%s - %s", pszAction, szMsg);
227 return RTEXITCODE_SYNTAX;
228}
229
230DECLHIDDEN(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
285DECLHIDDEN(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
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette