VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/HostPower.cpp@ 98351

Last change on this file since 98351 was 98288, checked in by vboxsync, 23 months ago

Main/src-server: rc -> hrc/vrc (partial). bugref:10223

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