VirtualBox

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

Last change on this file since 59381 was 55214, checked in by vboxsync, 10 years ago

Main/Console+Machine+Session+Snapshot: move the save state and snapshot related methods from IConsole to IMachine, with lots of unavoidable code restructuring and cleanup. Also define two new machine states (so that the "Saving" one is specifically for saving state now) which requires more changes everywhere
Frontends: necessary adjustments
doc/SDK: document the changes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.1 KB
Line 
1/* $Id: VBoxAutostartUtils.cpp 55214 2015-04-13 15:53:01Z 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
40using namespace com;
41
42DECLHIDDEN(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
96DECLHIDDEN(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
111DECLHIDDEN(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
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(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
212DECLHIDDEN(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
220DECLHIDDEN(RTEXITCODE) autostartSvcDisplayGetOptError(const char *pszAction, int rc, int argc, char **argv, int iArg, PCRTGETOPTUNION pValue)
221{
222 autostartSvcDisplayError("%s - RTGetOpt failure, %Rrc (%d): %s\n",
223 pszAction, rc, rc, iArg < argc ? argv[iArg] : "<null>");
224 return RTEXITCODE_FAILURE;
225}
226
227DECLHIDDEN(RTEXITCODE) autostartSvcDisplayTooManyArgsError(const char *pszAction, int argc, char **argv, int iArg)
228{
229 Assert(iArg < argc);
230 autostartSvcDisplayError("%s - Too many arguments: %s\n", pszAction, argv[iArg]);
231 return RTEXITCODE_FAILURE;
232}
233
234DECLHIDDEN(int) autostartSetup()
235{
236 autostartSvcOsLogStr("Setting up ...\n", AUTOSTARTLOGTYPE_VERBOSE);
237
238 /*
239 * Initialize COM.
240 */
241 using namespace com;
242 HRESULT hrc = com::Initialize();
243# ifdef VBOX_WITH_XPCOM
244 if (hrc == NS_ERROR_FILE_ACCESS_DENIED)
245 {
246 char szHome[RTPATH_MAX] = "";
247 com::GetVBoxUserHomeDirectory(szHome, sizeof(szHome));
248 return RTMsgErrorExit(RTEXITCODE_FAILURE,
249 "Failed to initialize COM because the global settings directory '%s' is not accessible!", szHome);
250 }
251# endif
252 if (FAILED(hrc))
253 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to initialize COM (%Rhrc)!", hrc);
254
255 hrc = g_pVirtualBoxClient.createInprocObject(CLSID_VirtualBoxClient);
256 if (FAILED(hrc))
257 {
258 RTMsgError("Failed to create the VirtualBoxClient object (%Rhrc)!", hrc);
259 com::ErrorInfo info;
260 if (!info.isFullAvailable() && !info.isBasicAvailable())
261 {
262 com::GluePrintRCMessage(hrc);
263 RTMsgError("Most likely, the VirtualBox COM server is not running or failed to start.");
264 }
265 else
266 com::GluePrintErrorInfo(info);
267 return RTEXITCODE_FAILURE;
268 }
269
270 /*
271 * Setup VirtualBox + session interfaces.
272 */
273 HRESULT rc = g_pVirtualBoxClient->COMGETTER(VirtualBox)(g_pVirtualBox.asOutParam());
274 if (SUCCEEDED(rc))
275 {
276 rc = g_pSession.createInprocObject(CLSID_Session);
277 if (FAILED(rc))
278 RTMsgError("Failed to create a session object (rc=%Rhrc)!", rc);
279 }
280 else
281 RTMsgError("Failed to get VirtualBox object (rc=%Rhrc)!", rc);
282
283 if (FAILED(rc))
284 return VERR_COM_OBJECT_NOT_FOUND;
285
286 return VINF_SUCCESS;
287}
288
289DECLHIDDEN(void) autostartShutdown()
290{
291 autostartSvcOsLogStr("Shutting down ...\n", AUTOSTARTLOGTYPE_VERBOSE);
292
293 g_pSession.setNull();
294 g_pVirtualBox.setNull();
295 g_pVirtualBoxClient.setNull();
296 com::Shutdown();
297}
298
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