/usr/lib/python2.7/dist-packages/framework/subsystems/ousaged/generic.py is in fso-frameworkd 0.9.5.9+git20110512-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | #!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
Open Usage Daemon - Generic reference counted Resource Management
(C) 2008 Michael 'Mickey' Lauer <mlauer@vanille-media.de>
(C) 2008 Jan 'Shoragan' Lübbe <jluebbe@lasnet.de>
(C) 2008 Openmoko, Inc.
GPLv2 or later
Package: ousaged
Module: generic
"""
MODULE_NAME = "ousaged"
__version__ = "0.7.1"
DBUS_INTERFACE_PREFIX = "org.freesmartphone.Usage"
DBUS_PATH_PREFIX = "/org/freesmartphone/Usage"
from .resources import ClientResource
from .lowlevel import resumeReason
import framework.patterns.tasklet as tasklet
from framework.patterns import dbuscache
import gobject
import dbus
import dbus.service
import time, os, subprocess
import logging
logger = logging.getLogger( MODULE_NAME )
logger.warning( "THIS SUBSYSTEM IMPLEMENTATION is DEPRECATED. Please use fsousaged instead." )
#----------------------------------------------------------------------------#
# DBus Exceptions specifications specific to this module
class ResourceUnknown( dbus.DBusException ):
_dbus_error_name = "org.freesmartphone.Usage.ResourceUnknown"
class ResourceExists( dbus.DBusException ):
_dbus_error_name = "org.freesmartphone.Usage.ResourceExists"
class ResourceInUse( dbus.DBusException ):
_dbus_error_name = "org.freesmartphone.Usage.ResourceInUse"
#----------------------------------------------------------------------------#
class GenericUsageControl( dbus.service.Object ):
#----------------------------------------------------------------------------#
"""
A Dbus Object implementing org.freesmartphone.Usage.
"""
DBUS_INTERFACE = DBUS_INTERFACE_PREFIX
def __init__( self, bus ):
self.interface = self.DBUS_INTERFACE
self.path = DBUS_PATH_PREFIX
dbus.service.Object.__init__( self, bus, self.path )
self.resources = {}
bus.add_signal_receiver(
self._nameOwnerChangedHandler,
"NameOwnerChanged",
dbus.BUS_DAEMON_IFACE,
dbus.BUS_DAEMON_NAME,
dbus.BUS_DAEMON_PATH
)
logger.info( "%s initialized. Serving %s at %s", self.__class__.__name__, self.interface, self.path )
self.idlenotifier = dbuscache.dbusInterfaceForObjectWithInterface(
"org.freesmartphone.odeviced",
"/org/freesmartphone/Device/IdleNotifier/0",
"org.freesmartphone.Device.IdleNotifier" )
def _addResource( self, resource ):
if not self.resources.get(resource.name, None) is None:
raise ResourceExists( "Resource %s already registered" % resource.name )
self.resources[resource.name] = resource
self.ResourceAvailable( resource.name, True )
def _getResource( self, resourcename ):
r = self.resources.get(resourcename, None)
if r is None:
raise ResourceUnknown( "Unknown resource %s" % resourcename )
return r
@tasklet.tasklet
def _suspend( self ):
"""
The actual suspending tasklet, phase 1 (suspending resources)
"""
self.SystemAction( "suspend" ) # send as early as possible
logger.info( "suspending all resources..." )
for resource in self.resources.values():
logger.debug( "suspending %s", resource.name )
yield resource._suspend()
logger.info( "...completed" )
# FIXME is this additional indirection necessary?
gobject.idle_add( self._suspend2 )
def _suspend2( self ):
self._kernelSuspendAndResume().start()
return False
@tasklet.tasklet
def _kernelSuspendAndResume( self ):
"""
The actual resuming tasklet.
"""
# FIXME might want to traverse /etc/apm.d/... and launch them scripts
logger.info( "triggering kernel suspend" )
if False: # for debugging
import time
time.sleep( 5 )
else:
# ---------------> Good Night!
open( "/sys/power/state", "w" ).write( "mem\n" )
# ---------------< Good Morning!
reason = resumeReason()
logger.info( "kernel has resumed - reason = %s" % reason )
if reason == "LowBattery":
logger.info( "kernel resumed because of low battery. Emergency Shutdown!" )
subprocess.call( "shutdown -h now &", shell=True )
# FIXME trigger shutdown quit
yield None
else:
logger.info( "resuming resources..." )
for resource in self.resources.values():
logger.debug( "resuming %s", resource.name )
yield resource._resume()
logger.info( "...completed." )
dbuscache.dbusCallAsyncDontCare( self.idlenotifier.SetState, "busy" ) # trigger idlenotifier
gobject.idle_add( lambda self=self:self.SystemAction( "resume" ) and False ) # send as late as possible
# keep track of resource name owners
def _nameOwnerChangedHandler( self, name, old_owner, new_owner ):
if old_owner and not new_owner:
for resource in self.resources.values():
resource.cleanup( old_owner ).start()
#
# dbus methods
#
@dbus.service.method( DBUS_INTERFACE, "", "as" )
def ListResources( self ):
return self.resources.keys()
@dbus.service.method( DBUS_INTERFACE, "s", "as" )
def GetResourceUsers( self, resourcename ):
return self._getResource( resourcename ).users
@dbus.service.method( DBUS_INTERFACE, "s", "b" )
def GetResourceState( self, resourcename ):
return self._getResource( resourcename ).isEnabled
@dbus.service.method( DBUS_INTERFACE, "s", "s" )
def GetResourcePolicy( self, resourcename ):
return self._getResource( resourcename ).policy
@dbus.service.method( DBUS_INTERFACE, "ss", "", async_callbacks=( "dbus_ok", "dbus_error" ) )
def SetResourcePolicy( self, resourcename, policy, dbus_ok, dbus_error ):
self._getResource( resourcename ).setPolicy( policy ).start_dbus( dbus_ok, dbus_error )
@dbus.service.method( DBUS_INTERFACE, "s", "", sender_keyword='sender', async_callbacks=( "dbus_ok", "dbus_error" ) )
def RequestResource( self, resourcename, sender, dbus_ok, dbus_error ):
"""
Called by a client to request a resource.
This call will return imediatly, even if the resource need to perform
some enabling actions.
"""
try:
resource = self.resources[resourcename]
except KeyError:
dbus_error( ResourceUnknown( "Known resources are %s" % self.resources.keys() ) )
else:
resource.request( sender ).start_dbus( dbus_ok, dbus_error )
@dbus.service.method( DBUS_INTERFACE, "s", "", sender_keyword='sender', async_callbacks=( "dbus_ok", "dbus_error" ) )
def ReleaseResource( self, resourcename, sender, dbus_ok, dbus_error ):
"""
Called by a client to release a previously requested resource.
This call will return imediatly, even if the resource need to perform
some disabling actions.
"""
try:
resource = self.resources[resourcename]
except KeyError:
dbus_error( ResourceUnknown( "Known resources are %s" % self.resources.keys() ) )
else:
resource.release( sender ).start_dbus( dbus_ok, dbus_error )
@dbus.service.method( DBUS_INTERFACE, "so", "",
sender_keyword='sender',
async_callbacks=( "dbus_ok", "dbus_error" ) )
def RegisterResource( self, resourcename, path, sender, dbus_ok, dbus_error ):
"""
Register a new resource from a client.
The client must provide a name for the resource, and a dbus object
path to an object implementing org.freesmartphone.Resource interface.
"""
if resourcename in self.resources:
dbus_error( ResourceExists( "Resource %s already exists" % resourcename ) )
else:
logger.info( "Register new resource %s", resourcename )
resource = ClientResource( self, resourcename, path, sender )
self._addResource( resource )
dbus_ok()
@dbus.service.method( DBUS_INTERFACE, "", "",
async_callbacks=( "dbus_ok", "dbus_error" ) )
def Suspend( self, dbus_ok, dbus_error ):
"""
Suspend all resources and the system.
"""
# Call the _suspend task connected to the dbus callbacks
self._suspend().start_dbus( dbus_ok, dbus_error )
@dbus.service.method( DBUS_INTERFACE, "", "",
async_callbacks=( "dbus_ok", "dbus_error" ) )
def Shutdown( self, dbus_ok, dbus_error ):
"""
Shutdown the system.
"""
logger.info( "System shutting down..." )
self.SystemAction( "shutdown" ) # send signal
dbus_ok()
# FIXME this is not a clean shutdown
subprocess.call( "shutdown -h now &", shell=True )
@dbus.service.method( DBUS_INTERFACE, "", "",
async_callbacks=( "dbus_ok", "dbus_error" ) )
def Reboot( self, dbus_ok, dbus_error ):
"""
Reboot the system.
"""
logger.info( "System rebooting..." )
# FIXME should we cleanly shutdown resources here -- will it matter?
self.SystemAction( "reboot" ) # send signal
dbus_ok()
subprocess.call( "reboot &", shell=True )
#
# dbus signals
#
@dbus.service.signal( DBUS_INTERFACE, "sba{sv}" )
def ResourceChanged( self, resourcename, state, attributes ):
pass
@dbus.service.signal( DBUS_INTERFACE, "sb" )
def ResourceAvailable( self, resourcename, state ):
pass
@dbus.service.signal( DBUS_INTERFACE, "s" )
def SystemAction( self, action ):
pass
#----------------------------------------------------------------------------#
def factory( prefix, controller ):
#----------------------------------------------------------------------------#
return [ GenericUsageControl( controller.bus ) ]
#----------------------------------------------------------------------------#
if __name__ == "__main__":
#----------------------------------------------------------------------------#
pass
|