/usr/lib/python2.7/dist-packages/framework/patterns/processguard.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 | #!/usr/bin/env python
"""
freesmartphone.org Framework Daemon
(C) 2008-2010 Michael 'Mickey' Lauer <mlauer@vanille-media.de>
(C) 2008-2009 Openmoko, Inc.
GPLv2 or later
Package: framework.patterns
Module: processguard
"""
__version__ = "0.3.0"
import gobject
import os, signal, types
MAX_READ = 4096
import logging
logger = logging.getLogger( "mppl.processguard" )
#============================================================================#
class ProcessGuard( object ):
#============================================================================#
#
# private
#
def __init__( self, cmdline ):
"""
Init
"""
if type( cmdline ) == types.ListType:
self._cmdline = cmdline
else:
self._cmdline = cmdline.split()
self._childwatch = None
self._stdoutwatch = None
self._stderrwatch = None
self.hadpid = None
self._reset()
logger.debug( "Created process guard for %s" % repr(self._cmdline) )
def _reset( self ):
"""
Reset
"""
self.pid = None
self.stdin = None
self.stdout = None
self.stderr = None
if self._childwatch is not None:
gobject.source_remove( self._childwatch )
self._childwatch = None
if self._stdoutwatch is not None:
gobject.source_remove( self._stdoutwatch )
self._stdoutwatch = None
if self._stderrwatch is not None:
gobject.source_remove( self._stderrwatch )
self._stderrwatch = None
def _execute( self, options ):
"""
Launch the monitored process
"""
if options is None:
cmdline = self._cmdline
else:
cmdline = [self._cmdline[0]] + options.split()
result = gobject.spawn_async( cmdline,
envp="",
working_directory=os.environ.get( "PWD", "/" ),
flags=gobject.SPAWN_DO_NOT_REAP_CHILD, # needed for child watch
user_data=None,
standard_input=False,
standard_output=True,
standard_error=True )
if result[0] < 0:
raise OSError( "foo" )
self.pid, self.stdin, self.stdout, self.stderr = result
self._childwatch = gobject.child_watch_add( self.pid, self._exitFromChild, priority=100 )
self._stdoutwatch = gobject.io_add_watch( self.stdout, gobject.IO_IN, self._outputFromChild )
self._stderrwatch = gobject.io_add_watch( self.stderr, gobject.IO_IN, self._errorFromChild )
def _outputFromChild( self, source, condition ):
"""
Called on child output
"""
if condition != gobject.IO_IN:
return False
data = os.read( source, MAX_READ )
logger.debug( "%s got data from child: %s" % ( self, repr(data) ) )
if self._onOutput is not None:
self._onOutput( data )
return True # mainloop: call me again
def _errorFromChild( self, source, condition ):
"""
Called on child output (stderr)
"""
if condition != gobject.IO_IN:
return False
data = os.read( source, MAX_READ )
logger.debug( "%s got error from child: %s" % ( self, repr(data) ) )
if self._onError is not None:
self._onError( data )
return True # mainloop: call me again
def _exitFromChild( self, pid, condition, data=None ):
"""
Called after(!) child has exit
"""
exitcode = (condition >> 8) & 0xFF
exitsignal = condition & 0xFF
self.hadpid = pid
self._reset() # self.pid now None
if self._onExit is not None:
self._onExit( pid, exitcode, exitsignal )
def __del__( self ):
"""
Cleanup
"""
self.shutdown()
self._reset()
#
# API
#
def execute( self, options=None, onExit=None, onError=None, onOutput=None ):
"""
Launch the process
Optionally override parameters and setup delegates.
"""
self._onExit = onExit
self._onOutput = onOutput
self._onError = onError
self._execute( options )
def shutdown( self, sig=signal.SIGTERM ):
"""
Shutdown the process.
"""
if self.pid is not None:
logger.info( "shutdown: killing process %d with signal %d", self.pid, sig )
try:
os.kill( self.pid, sig )
except OSError:
logger.info( "shutdown: process already vanished" )
return
try:
os.waitpid( self.pid, os.WNOHANG )
except OSError:
logger.info( "shutdown: waitpid failed" )
return
# is GLib.process_close_pid bound?
else:
logger.info( "shutdown: process already vanished" )
def isRunning( self ):
"""
Returns True, when the process is running. False, otherwise.
"""
return self.pid is not None
#============================================================================#
if __name__ == "__main__":
#============================================================================#
def firstExit( pid, exitcode, exitsignal ):
print "first exit"
p.execute( "/tmp", onExit=secondExit )
def secondExit( pid, exitcode, exitsignal ):
print "second exit"
def thirdExit( pid, exitcode, exitsignal ):
print "third exit"
loop.quit()
def killit():
print "killing..."
p2.shutdown()
return False
loop = gobject.MainLoop()
p = ProcessGuard( "/bin/ls ." )
p.execute( onExit=firstExit )
p2 = ProcessGuard( "/bin/sleep 10" )
p2.execute( onExit=thirdExit )
gobject.timeout_add_seconds( 3, killit )
try:
loop.run()
except KeyboardInterrupt:
loop.quit()
else:
print "OK"
|