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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
|
/***************************************************************************
* *
* Powersave Daemon *
* *
* Copyright (C) 2004,2005 SUSE Linux Products GmbH *
* *
* 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; either version 2 of the License, or (at you *
* option) any later version. *
* *
* 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, write to the Free Software Foundation, Inc., *
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
* *
***************************************************************************/
#include "config.h"
#include "event_management.h"
#include "event.h"
#include "dbus_server.h"
#include "cpufreq_management.h"
#include "globals.h"
#include "main_loop.h"
#include <string>
#include <signal.h>
using namespace Powersave::Globals;
EventManagement::EventManagement() : id_count(0)
{ }
EventManagement::~EventManagement()
{
}
int EventManagement::registerEvent(Event & event, time_t timeout)
{
pDebug(DBG_INFO, "registering event no.'%d'", id_count);
// give the event an unique id
event.setID(id_count);
// the next event will get a higher id
id_count++;
// set the timeout in milliseconds
event.setTimeout(timeout * 1000);
// start the counter which looks if an event timed out
event.start();
// append the event to the list of pending events
Events.push_back(event);
// check each registered event for its timeout
main_loop->updateCallbacks();
return event.ID();
}
bool EventManagement::remove(int id)
{
for (std::list < Event >::iterator it = Events.begin();
it != Events.end(); ++it) {
if (id == it->ID()) {
Events.erase(it);
return true;
}
}
return false;
}
int EventManagement::count() const
{
return Events.size();
}
const string EventManagement::name(int id)
{
for (std::list < Event >::iterator it = Events.begin();
it != Events.end(); ++it) {
if (id == it->ID()) {
return it->name();
}
}
return string();
}
const string EventManagement::cur_prog(int id)
{
for (std::list < Event >::iterator it = Events.begin();
it != Events.end(); ++it) {
if (id == it->ID()) {
if (it->_current_program_2_execute != it->end()) {
return *(it->_current_program_2_execute);
} else {
return string();
}
}
}
return string();
}
int EventManagement::moveOn(int id)
{
for (std::list < Event >::iterator it = Events.begin();
it != Events.end(); ++it) {
if (id == it->ID()) {
it->_current_program_2_execute++;
if (it->_current_program_2_execute == it->end())
return 0;
return 1;
}
}
return -1;
}
Event* EventManagement::event(int id)
{
for (std::list < Event >::iterator it = Events.begin();
it != Events.end(); ++it) {
if (id == it->ID()) {
return &(*it);
}
}
return NULL;
}
void EventManagement::adjustTimeouts(unsigned long sleep_time)
{
for (std::list < Event >::iterator it = Events.begin();
it != Events.end(); ++it) {
it->increaseSleeptime(sleep_time);
}
}
int EventManagement::eventIsRunning(const std::string &eventName)
{
for (std::list < Event >::iterator it = Events.begin();
it != Events.end(); ++it) {
if (eventName == it->name()) {
return 1;
}
}
return 0;
}
void EventManagement::checkScriptReturn(int id, int returnValue, string &comment)
{
// get the program name
string prog = cur_prog(id);
// if a program matching an event was found
if (!prog.empty() || id == -1) {
pDebug(DBG_INFO, "SCRIPT returned: Name: '%s', Return value: %d, "
"Comment: '%s'", prog.c_str(), returnValue, comment.c_str());
} else {
pDebug(DBG_DIAG, "Unknown SCRIPT returned. Return value %d, text: '%s'", returnValue,
comment.c_str());
// allow popup windows also from unknown processes?
// if (returnValue !=2)
return;
}
// enable the buttons when we come back from restore
if (prog.find("restore_after_s") == 0) {
pm->ignore_buttons(false);
}
// evaluate the return code
switch (returnValue) {
// script succeeded
case 0:{
// set the event to the next script
int i = moveOn(id);
// no scripts left, so finished successfully
if (!i) {
const string name = this->name(id);
pDebug(DBG_INFO, "SCRIPT Event %s finished successfully", name.c_str());
// notify clients here
this->remove(id);
} else {
continueEvent(this->event(id), "");
}
break;
}
// script requests a graphical notification from the daemon
case 2:
pm->x_notification(comment);
break;
// script requests a xlock/screensaver from the daemon
case 3:
pm->screenlock(comment);
break;
case 4:
DBus_Server::emitSignal("Progress", comment.c_str());
break;
// script has failed
case 1: {
const string event_name = this->name(id);
pDebug(DBG_INFO, "SCRIPT %s failed to execute", event_name.c_str());
// if the suspend/standby event has failed --> enable buttons
if ((event_name.find("global.suspend") == 0)
|| (event_name.find("global.standby") == 0)) {
pm->ignore_buttons(false);
pm->resumeNetworkManager();
}
// notify clients here
// x_notification(comment);
// remove the event from the list of pending events
this->remove(id);
break;
}
default:
pDebug(DBG_ERR, "Internal error: checkScriptReturn got an unknown return value of %d", returnValue);
break;
}
}
bool EventManagement::continueEvent(Event *event, string acpi_event_line)
{
if (event->_programs_2_execute.empty())
return false;
if (event->isInternalAction()) {
if (!executeInternalAction(event, event->currentAction())) {
pDebug(DBG_DIAG,
"Internal action %s returned an error",
event->currentAction().c_str());
string c = "internal action failed: ";
c.append(event->currentAction());
checkScriptReturn(event->ID(), 1, c);
}
else {
pDebug(DBG_INFO,
"Internal action %s executed",
event->currentAction().c_str());
string c = "internal action successfull: ";
c.append(event->currentAction());
checkScriptReturn(event->ID(), 0, c);
}
}
else {
if (!event->execute_program(acpi_event_line)) {
pDebug(DBG_WARN,
"Could not execute program %s for event %s: %s",
PUB_SCRIPT_DIR, event->name().c_str(), strerror(errno));
string c = "Cannot access or execute: ";
c.append(event->currentAction());
checkScriptReturn(event->ID(), 1, c);
}
}
return true;
}
bool EventManagement::executeInternalAction(Event *event, const string action)
{
bool ret = true;
pDebug(DBG_INFO, "Executing internal action: %s", action.c_str());
if (!action.compare("ignore")) {
/* ... */
} else if (!action.compare("throttle")) {
pm->throttle();
} else if (!action.compare("dethrottle")) {
pm->dethrottle();
}
/* really do the suspend/standby */
else if (!action.compare("do_suspend_to_disk")) {
pm->sleep_wrapper("suspend2disk");
pm->executeScript(ACPI_SLEEP_SCRIPT, "suspend2disk");
pm->sleep_wrapper("suspend2disk");
} else if (!action.compare("do_suspend_to_ram")) {
pm->sleep_wrapper("suspend2ram");
pm->suspend_to_ram();
pm->sleep_wrapper("suspend2ram");
} else if (!action.compare("do_standby")) {
pm->sleep_wrapper("standby");
pm->standby();
pm->sleep_wrapper("standby");
} else if (!action.compare("screen_saver")) {
pm->screenlock(event->name());
}
else if (!action.compare("suspend_networkmanager")) {
pm->suspendNetworkManager();
} else if (!action.compare("resume_networkmanager")) {
pm->resumeNetworkManager();
}
/* throw a suspend/standby event (will trigger other needed
actions for proper suspend/standby */
else if (!action.compare("suspend_to_disk")) {
if (pm->haveSuspend2())
executeEvent("global.suspend2disk.other");
else
executeEvent("global.suspend2disk");
} else if (!action.compare("suspend_to_ram")) {
executeEvent("global.suspend2ram");
} else if (!action.compare("standby")) {
executeEvent("global.standby");
} else if (!action.compare("reread_cpu_capabilities")) {
if (cpufreq->isSupported()) {
cpufreq->rereadFrequencies();
}
} else
ret = false;
return ret;
}
bool EventManagement::executeEvent(const string &eventName, const char *acpi_event_line)
{
/* this is only for acpi events... */
Event execEvent = config_obj->current_scheme->getEvent(eventName);
int eventId;
if (execEvent.name() == "global.suspend2disk" ||
execEvent.name() == "global.suspend2ram" ||
execEvent.name() == "global.standby") {
if (eventIsRunning(execEvent.name())) {
pDebug(DBG_WARN,
"Event %s already running. Ignoring this one",
execEvent.name().c_str());
return true;
}
}
if (execEvent.name() == "default") {
pDebug(DBG_ERR, "Tried to execute non-existing event.");
return false;
}
DBus_Server::emitSignal("PowersaveEvent", eventName.c_str());
// append the event to the list of pending events. Timeout is set to 120 seconds.
// are 2 minutes enough?
eventId = registerEvent(execEvent, 120);
// do not ignore child if daemon terminates
// this is the only event where main process has
// to wait until proxy is finished
if (execEvent.name() == "daemon.terminate")
signal(SIGCHLD, SIG_DFL);
// execute the first script/internal action of the event
continueEvent(&execEvent, acpi_event_line);
return true;
}
bool EventManagement::checkEventTimeouts()
{
// go through all events and check if one of them timed out
std::list < Event >::iterator it;
for (it = Events.begin(); it != Events.end(); it++) {
if (it->timeElapsed() > it->getTimeout()) {
pDebug(DBG_WARN, "Timeout for event %s with id %d reached, "
"removing", it->name().c_str(), it->ID());
Events.erase(it);
/* deleting the object may result in invokation
of a non existing event (it->timeElapsed() above)
-> start reiterating from the beginning of the list
*/
it = Events.begin();
}
}
if (Events.size() == 0)
return false;
return true;
}
|