VirtualBox

source: vbox/trunk/src/VBox/Frontends/VBoxAutostart/VBoxAutostartStop.cpp@ 46845

Last change on this file since 46845 was 43967, checked in by vboxsync, 12 years ago

Autostart: More updates for the Windows service

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.0 KB
Line 
1/* $Id: VBoxAutostartStop.cpp 43967 2012-11-26 19:35:33Z vboxsync $ */
2/** @file
3 * VBoxAutostart - VirtualBox Autostart service, stop machines during system shutdown.
4 */
5
6/*
7 * Copyright (C) 2012 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#include <VBox/com/com.h>
19#include <VBox/com/string.h>
20#include <VBox/com/Guid.h>
21#include <VBox/com/array.h>
22#include <VBox/com/ErrorInfo.h>
23#include <VBox/com/errorprint.h>
24
25#include <iprt/thread.h>
26#include <iprt/stream.h>
27#include <iprt/log.h>
28#include <iprt/assert.h>
29#include <iprt/message.h>
30
31#include <algorithm>
32#include <list>
33#include <string>
34
35#include "VBoxAutostart.h"
36
37using namespace com;
38
39/**
40 * VM list entry.
41 */
42typedef struct AUTOSTOPVM
43{
44 /** ID of the VM to start. */
45 Bstr strId;
46 /** Action to do with the VM. */
47 AutostopType_T enmAutostopType;
48} AUTOSTOPVM;
49
50static HRESULT autostartSaveVMState(ComPtr<IConsole> &console)
51{
52 HRESULT rc = S_OK;
53 ComPtr<IProgress> progress;
54
55 do
56 {
57 /* first pause so we don't trigger a live save which needs more time/resources */
58 bool fPaused = false;
59 rc = console->Pause();
60 if (FAILED(rc))
61 {
62 bool fError = true;
63 if (rc == VBOX_E_INVALID_VM_STATE)
64 {
65 /* check if we are already paused */
66 MachineState_T machineState;
67 CHECK_ERROR_BREAK(console, COMGETTER(State)(&machineState));
68 /* the error code was lost by the previous instruction */
69 rc = VBOX_E_INVALID_VM_STATE;
70 if (machineState != MachineState_Paused)
71 {
72 RTMsgError("Machine in invalid state %d -- %s\n",
73 machineState, machineStateToName(machineState, false));
74 }
75 else
76 {
77 fError = false;
78 fPaused = true;
79 }
80 }
81 if (fError)
82 break;
83 }
84
85 CHECK_ERROR(console, SaveState(progress.asOutParam()));
86 if (FAILED(rc))
87 {
88 if (!fPaused)
89 console->Resume();
90 break;
91 }
92
93 rc = showProgress(progress);
94 CHECK_PROGRESS_ERROR(progress, ("Failed to save machine state"));
95 if (FAILED(rc))
96 {
97 if (!fPaused)
98 console->Resume();
99 }
100 } while (0);
101
102 return rc;
103}
104
105DECLHIDDEN(RTEXITCODE) autostartStopMain(PCFGAST pCfgAst)
106{
107 RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
108 int vrc = VINF_SUCCESS;
109 std::list<AUTOSTOPVM> listVM;
110
111 /*
112 * Build a list of all VMs we need to autostop first, apply the overrides
113 * from the configuration and start the VMs afterwards.
114 */
115 com::SafeIfaceArray<IMachine> machines;
116 HRESULT rc = g_pVirtualBox->COMGETTER(Machines)(ComSafeArrayAsOutParam(machines));
117 if (SUCCEEDED(rc))
118 {
119 /*
120 * Iterate through the collection and construct a list of machines
121 * we have to check.
122 */
123 for (size_t i = 0; i < machines.size(); ++i)
124 {
125 if (machines[i])
126 {
127 BOOL fAccessible;
128 CHECK_ERROR_BREAK(machines[i], COMGETTER(Accessible)(&fAccessible));
129 if (!fAccessible)
130 continue;
131
132 AutostopType_T enmAutostopType;
133 CHECK_ERROR_BREAK(machines[i], COMGETTER(AutostopType)(&enmAutostopType));
134 if (enmAutostopType != AutostopType_Disabled)
135 {
136 AUTOSTOPVM autostopVM;
137
138 CHECK_ERROR_BREAK(machines[i], COMGETTER(Id)(autostopVM.strId.asOutParam()));
139 autostopVM.enmAutostopType = enmAutostopType;
140
141 listVM.push_back(autostopVM);
142 }
143 }
144 }
145
146 if ( SUCCEEDED(rc)
147 && listVM.size())
148 {
149 std::list<AUTOSTOPVM>::iterator it;
150 for (it = listVM.begin(); it != listVM.end(); it++)
151 {
152 MachineState_T enmMachineState;
153 ComPtr<IMachine> machine;
154
155 CHECK_ERROR_BREAK(g_pVirtualBox, FindMachine((*it).strId.raw(),
156 machine.asOutParam()));
157
158 CHECK_ERROR_BREAK(machine, COMGETTER(State)(&enmMachineState));
159
160 /* Wait until the VM changes from a transient state back. */
161 while ( enmMachineState >= MachineState_FirstTransient
162 && enmMachineState <= MachineState_LastTransient)
163 {
164 RTThreadSleep(1000);
165 CHECK_ERROR_BREAK(machine, COMGETTER(State)(&enmMachineState));
166 }
167
168 /* Only power off running machines. */
169 if ( enmMachineState == MachineState_Running
170 || enmMachineState == MachineState_Paused)
171 {
172 ComPtr<IConsole> console;
173 ComPtr<IProgress> progress;
174
175 /* open a session for the VM */
176 CHECK_ERROR_BREAK(machine, LockMachine(g_pSession, LockType_Shared));
177
178 /* get the associated console */
179 CHECK_ERROR_BREAK(g_pSession, COMGETTER(Console)(console.asOutParam()));
180
181 switch ((*it).enmAutostopType)
182 {
183 case AutostopType_SaveState:
184 {
185 rc = autostartSaveVMState(console);
186 break;
187 }
188 case AutostopType_PowerOff:
189 {
190 CHECK_ERROR_BREAK(console, PowerDown(progress.asOutParam()));
191
192 rc = showProgress(progress);
193 CHECK_PROGRESS_ERROR(progress, ("Failed to power off machine"));
194 break;
195 }
196 case AutostopType_AcpiShutdown:
197 {
198 BOOL fGuestEnteredACPI = false;
199 CHECK_ERROR_BREAK(console, GetGuestEnteredACPIMode(&fGuestEnteredACPI));
200 if (fGuestEnteredACPI && enmMachineState == MachineState_Running)
201 {
202 CHECK_ERROR_BREAK(console, PowerButton());
203
204 autostartSvcLogInfo("Waiting for VM \"%ls\" to power off...\n", (*it).strId.raw());
205
206 do
207 {
208 RTThreadSleep(1000);
209 CHECK_ERROR_BREAK(machine, COMGETTER(State)(&enmMachineState));
210 } while (enmMachineState == MachineState_Running);
211 }
212 else
213 {
214 /* Use save state instead and log this to the console. */
215 autostartSvcLogWarning("The guest of VM \"%ls\" does not support ACPI shutdown or is currently paused, saving state...\n",
216 (*it).strId.raw());
217 rc = autostartSaveVMState(console);
218 }
219 break;
220 }
221 default:
222 autostartSvcLogWarning("Unknown autostop type for VM \"%ls\"\n", (*it).strId.raw());
223 }
224 g_pSession->UnlockMachine();
225 }
226 }
227 }
228 }
229
230 return rcExit;
231}
232
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