<s> """<STR_LIT:U+0020>""" <EOL> import logging <EOL> import os <EOL> import signal <EOL> import sys <EOL> from chalmers import errors <EOL> from chalmers . utils . daemonize import daemonize <EOL> from . base import ProgramBase <EOL> import pwd <EOL> log = logging . getLogger ( __name__ ) <EOL> def stop_process ( signum , frame ) : <EOL> """<STR_LIT>""" <EOL> log . debug ( "<STR_LIT>" % signum ) <EOL> raise errors . StopProcess ( ) <EOL> class PosixProgram ( ProgramBase ) : <EOL> """<STR_LIT>""" <EOL> @ property <EOL> def is_running ( self ) : <EOL> "<STR_LIT>" <EOL> pid = self . state . get ( '<STR_LIT>' ) <EOL> if not pid : <EOL> return False <EOL> try : <EOL> os . kill ( pid , <NUM_LIT:0> ) <EOL> except OSError : <EOL> return False <EOL> else : <EOL> return True <EOL> def start_as_service ( self ) : <EOL> """<STR_LIT>""" <EOL> daemonize ( self . start_sync ) <EOL> def sighup_handler ( self , signum , frame ) : <EOL> if self . _ignore_sighup : <EOL> log . warn ( "<STR_LIT>" % ( self . name , signum ) ) <EOL> else : <EOL> log . error ( "<STR_LIT>" % ( self . name , signum ) ) <EOL> sys . exit ( - <NUM_LIT:1> ) <EOL> def handle_signals ( self ) : <EOL> self . _ignore_sighup = False <EOL> signal . signal ( signal . SIGHUP , self . sighup_handler ) <EOL> def clear_socket ( self ) : <EOL> '<STR_LIT>' <EOL> if os . path . exists ( self . addr ) : <EOL> log . debug ( "<STR_LIT>" % self . addr ) <EOL> try : <EOL> os . unlink ( self . addr ) <EOL> except OSError as err : <EOL> log . warn ( "<STR_LIT>" % err ) <EOL> def stop ( self , force = False ) : <EOL> try : <EOL> ProgramBase . stop ( self , force = force ) <EOL> finally : <EOL> self . clear_socket ( ) <EOL> def dispatch_bg ( self ) : <EOL> log . warn ( "<STR_LIT>" ) <EOL> if self . pipe_output : <EOL> print ( "<STR_LIT>" % self . data [ '<STR_LIT>' ] ) <EOL> self . _echo . stop ( ) <EOL> log . warn ( "<STR_LIT>" % signal . SIGHUP ) <EOL> self . _ignore_sighup = True <EOL> si = file ( '<STR_LIT>' , '<STR_LIT:r>' ) <EOL> so = file ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> se = file ( '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:0> ) <EOL> os . dup2 ( si . fileno ( ) , sys . stdin . fileno ( ) ) <EOL> os . dup2 ( so . fileno ( ) , sys . stdout . fileno ( ) ) <EOL> os . dup2 ( se . fileno ( ) , sys . stderr . fileno ( ) ) <EOL> def preexec_fn ( self ) : <EOL> signal . signal ( signal . SIGHUP , signal . SIG_IGN ) <EOL> new_mask = self . data . get ( '<STR_LIT>' ) <EOL> user = self . data . get ( '<STR_LIT:user>' ) <EOL> if new_mask : <EOL> os . umask ( new_mask ) <EOL> if user : <EOL> if isinstance ( user , int ) : <EOL> user = pwd . getpwuid ( user ) <EOL> else : <EOL> user = pwd . getpwnam ( user ) <EOL> os . setegid ( user . pw_gid ) <EOL> os . seteuid ( user . pw_uid ) <EOL> def _send_signal ( self , pid , sig ) : <EOL> os . kill ( pid , sig ) </s>
