1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
/* $Id: HostPower.cpp $ */
/** @file
* VirtualBox interface to host's power notification service
*/
/*
* Copyright (C) 2006-2023 Oracle and/or its affiliates.
*
* This file is part of VirtualBox base platform packages, as
* available from https://www.virtualbox.org.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, in version 3 of the
* License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses>.
*
* SPDX-License-Identifier: GPL-3.0-only
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#define LOG_GROUP LOG_GROUP_MAIN_HOST
#include "HostPower.h"
#include "LoggingNew.h"
#include <VBox/com/ptr.h>
#include "VirtualBoxImpl.h"
#include "MachineImpl.h"
#include <iprt/mem.h>
#include <iprt/cpp/utils.h>
HostPowerService::HostPowerService(VirtualBox *aVirtualBox)
{
AssertPtr(aVirtualBox);
mVirtualBox = aVirtualBox;
}
HostPowerService::~HostPowerService()
{
}
void HostPowerService::notify(Reason_T aReason)
{
SessionMachinesList machines;
VirtualBox::InternalControlList controls;
HRESULT hrc = S_OK;
switch (aReason)
{
case Reason_HostSuspend:
{
LogFunc(("HOST SUSPEND\n"));
#ifdef VBOX_WITH_RESOURCE_USAGE_API
/* Suspend performance sampling to avoid unnecessary callbacks due to jumps in time. */
PerformanceCollector *perfcollector = mVirtualBox->i_performanceCollector();
if (perfcollector)
perfcollector->suspendSampling();
#endif
mVirtualBox->i_getOpenedMachines(machines, &controls);
/* pause running VMs */
for (VirtualBox::InternalControlList::const_iterator it = controls.begin();
it != controls.end();
++it)
{
ComPtr<IInternalSessionControl> pControl = *it;
/* PauseWithReason() will simply return a failure if
* the VM is in an inappropriate state */
hrc = pControl->PauseWithReason(Reason_HostSuspend);
if (FAILED(hrc))
continue;
/* save the control to un-pause the VM later */
mSessionControls.push_back(pControl);
}
LogRel(("Host suspending: Paused %d VMs\n", mSessionControls.size()));
break;
}
case Reason_HostResume:
{
LogFunc(("HOST RESUME\n"));
size_t resumed = 0;
/* go through VMs we paused on Suspend */
for (size_t i = 0; i < mSessionControls.size(); ++i)
{
/* note that Resume() will simply return a failure if the VM is
* in an inappropriate state (it will also fail if the VM has
* been somehow closed by this time already so that the
* console reference we have is dead) */
hrc = mSessionControls[i]->ResumeWithReason(Reason_HostResume);
if (FAILED(hrc))
continue;
++resumed;
}
LogRel(("Host resumed: Resumed %d VMs\n", resumed));
#ifdef VBOX_WITH_RESOURCE_USAGE_API
/* Resume the performance sampling. */
PerformanceCollector *perfcollector = mVirtualBox->i_performanceCollector();
if (perfcollector)
perfcollector->resumeSampling();
#endif
mSessionControls.clear();
break;
}
case Reason_HostBatteryLow:
{
LogFunc(("BATTERY LOW\n"));
Bstr value;
hrc = mVirtualBox->GetExtraData(Bstr("VBoxInternal2/SavestateOnBatteryLow").raw(), value.asOutParam());
int fGlobal = 0;
if (SUCCEEDED(hrc) && !value.isEmpty())
{
if (value != "0")
fGlobal = 1;
else if (value == "0")
fGlobal = -1;
}
mVirtualBox->i_getOpenedMachines(machines, &controls);
size_t saved = 0;
/* save running VMs */
for (SessionMachinesList::const_iterator it = machines.begin();
it != machines.end();
++it)
{
ComPtr<SessionMachine> pMachine = *it;
hrc = pMachine->GetExtraData(Bstr("VBoxInternal2/SavestateOnBatteryLow").raw(), value.asOutParam());
int fPerVM = 0;
if (SUCCEEDED(hrc) && !value.isEmpty())
{
/* per-VM overrides global */
if (value != "0")
fPerVM = 2;
else if (value == "0")
fPerVM = -2;
}
/* default is true */
if (fGlobal + fPerVM >= 0)
{
ComPtr<IProgress> progress;
/* SessionMachine::i_saveStateWithReason() will return
* a failure if the VM is in an inappropriate state */
hrc = pMachine->i_saveStateWithReason(Reason_HostBatteryLow, progress);
if (FAILED(hrc))
{
LogRel(("SaveState '%s' failed with %Rhrc\n", pMachine->i_getName().c_str(), hrc));
continue;
}
/* Wait until the operation has been completed. */
hrc = progress->WaitForCompletion(-1);
if (SUCCEEDED(hrc))
{
LONG iRc;
progress->COMGETTER(ResultCode)(&iRc);
hrc = (HRESULT)iRc;
}
AssertMsg(SUCCEEDED(hrc), ("SaveState WaitForCompletion failed with %Rhrc (%#08X)\n", hrc, hrc));
if (SUCCEEDED(hrc))
{
LogRel(("SaveState '%s' succeeded\n", pMachine->i_getName().c_str()));
++saved;
}
}
}
LogRel(("Battery Low: saved %d VMs\n", saved));
break;
}
default:
/* nothing */;
}
}
/* vi: set tabstop=4 shiftwidth=4 expandtab: */
|